sl.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. /******************************************************************************
  2. Copyright (C) 2019-2020 by Dillon Pentz <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include "importers.hpp"
  15. using namespace std;
  16. using namespace json11;
  17. static string translate_key(const string &sl_key)
  18. {
  19. if (sl_key.substr(0, 6) == "Numpad" && sl_key.size() == 7) {
  20. return "OBS_KEY_NUM" + sl_key.substr(6);
  21. } else if (sl_key.substr(0, 3) == "Key") {
  22. return "OBS_KEY_" + sl_key.substr(3);
  23. } else if (sl_key.substr(0, 5) == "Digit") {
  24. return "OBS_KEY_" + sl_key.substr(5);
  25. } else if (sl_key[0] == 'F' && sl_key.size() < 4) {
  26. return "OBS_KEY_" + sl_key;
  27. }
  28. #define add_translation(str, out) \
  29. if (sl_key == str) { \
  30. return out; \
  31. }
  32. add_translation("Backquote", "OBS_KEY_ASCIITILDE");
  33. add_translation("Backspace", "OBS_KEY_BACKSPACE");
  34. add_translation("Tab", "OBS_KEY_TAB");
  35. add_translation("Space", "OBS_KEY_SPACE");
  36. add_translation("Period", "OBS_KEY_PERIOD");
  37. add_translation("Slash", "OBS_KEY_SLASH");
  38. add_translation("Backslash", "OBS_KEY_BACKSLASH");
  39. add_translation("Minus", "OBS_KEY_MINUS");
  40. add_translation("Comma", "OBS_KEY_COMMA");
  41. add_translation("Plus", "OBS_KEY_PLUS");
  42. add_translation("Quote", "OBS_KEY_APOSTROPHE");
  43. add_translation("Semicolon", "OBS_KEY_SEMICOLON");
  44. add_translation("NumpadSubtract", "OBS_KEY_NUMMINUS");
  45. add_translation("NumpadAdd", "OBS_KEY_NUMPLUS");
  46. add_translation("NumpadDecimal", "OBS_KEY_NUMPERIOD");
  47. add_translation("NumpadDivide", "OBS_KEY_NUMSLASH");
  48. add_translation("NumpadMultiply", "OBS_KEY_NUMASTERISK");
  49. add_translation("Enter", "OBS_KEY_RETURN");
  50. add_translation("CapsLock", "OBS_KEY_CAPSLOCK");
  51. add_translation("NumLock", "OBS_KEY_NUMLOCK");
  52. add_translation("ScrollLock", "OBS_KEY_SCROLLLOCK");
  53. add_translation("Pause", "OBS_KEY_PAUSE");
  54. add_translation("Insert", "OBS_KEY_INSERT");
  55. add_translation("Home", "OBS_KEY_HOME");
  56. add_translation("End", "OBS_KEY_END");
  57. add_translation("Escape", "OBS_KEY_ESCAPE");
  58. add_translation("Delete", "OBS_KEY_DELETE");
  59. add_translation("ArrowUp", "OBS_KEY_UP");
  60. add_translation("ArrowDown", "OBS_KEY_DOWN");
  61. add_translation("ArrowLeft", "OBS_KEY_LEFT");
  62. add_translation("ArrowRight", "OBS_KEY_RIGHT");
  63. add_translation("PageUp", "OBS_KEY_PAGEUP");
  64. add_translation("PageDown", "OBS_KEY_PAGEDOWN");
  65. add_translation("BracketLeft", "OBS_KEY_BRACKETLEFT");
  66. add_translation("BracketRight", "OBS_KEY_BRACKETRIGHT");
  67. #undef add_translation
  68. return "";
  69. }
  70. static string translate_hotkey(const Json &hotkey, const string &source)
  71. {
  72. string name = hotkey["actionName"].string_value();
  73. #define add_translation(in, str, out, source) \
  74. if (in == str) { \
  75. return out + source; \
  76. }
  77. add_translation(name, "TOGGLE_SOURCE_VISIBILITY_SHOW",
  78. "libobs.show_scene_item.", source);
  79. add_translation(name, "TOGGLE_SOURCE_VISIBILITY_HIDE",
  80. "libobs.hide_scene_item.", source);
  81. string empty = "";
  82. add_translation(name, "SWITCH_TO_SCENE", "OBSBasic.SelectScene", empty);
  83. add_translation(name, "TOGGLE_MUTE", "libobs.mute", empty);
  84. add_translation(name, "TOGGLE_UNMUTE", "libobs.unmute", empty);
  85. add_translation(name, "PUSH_TO_MUTE", "libobs.push-to-mute", empty);
  86. add_translation(name, "PUSH_TO_TALK", "libobs.push-to-talk", empty);
  87. add_translation(name, "GAME_CAPTURE_HOTKEY_START", "hotkey_start",
  88. empty);
  89. add_translation(name, "GAME_CAPTURE_HOTKEY_STOP", "hotkey_stop", empty);
  90. return "";
  91. #undef add_translation
  92. }
  93. static bool source_name_exists(const Json::array &sources, const string &name)
  94. {
  95. for (size_t i = 0; i < sources.size(); i++) {
  96. Json item = sources[i];
  97. string source_name = item["name"].string_value();
  98. if (source_name == name)
  99. return true;
  100. }
  101. return false;
  102. }
  103. static string get_source_name_from_id(const Json::array &sources,
  104. const string &id)
  105. {
  106. for (size_t i = 0; i < sources.size(); i++) {
  107. Json item = sources[i];
  108. string source_id = item["sl_id"].string_value();
  109. if (source_id == id)
  110. return item["name"].string_value();
  111. }
  112. return "";
  113. }
  114. static void get_hotkey_bindings(Json::object &out_hotkeys,
  115. const Json &in_hotkeys, const string &name)
  116. {
  117. Json::array hot_arr = in_hotkeys.array_items();
  118. for (size_t i = 0; i < hot_arr.size(); i++) {
  119. Json hotkey = hot_arr[i];
  120. Json::array bindings = hotkey["bindings"].array_items();
  121. Json::array out_hotkey = Json::array{};
  122. string hotkey_name = translate_hotkey(hotkey, name);
  123. for (size_t x = 0; x < bindings.size(); x++) {
  124. Json binding = bindings[x];
  125. Json modifiers = binding["modifiers"];
  126. string key =
  127. translate_key(binding["key"].string_value());
  128. out_hotkey.push_back(
  129. Json::object{{"control", modifiers["ctrl"]},
  130. {"shift", modifiers["shift"]},
  131. {"command", modifiers["meta"]},
  132. {"alt", modifiers["alt"]},
  133. {"key", key}});
  134. }
  135. out_hotkeys[hotkey_name] = out_hotkey;
  136. }
  137. }
  138. static void get_scene_items(const Json::array &out_sources, Json::object &scene,
  139. const Json::array &in)
  140. {
  141. int length = 0;
  142. Json::object hotkeys = scene["hotkeys"].object_items();
  143. Json::array out_items = Json::array{};
  144. for (size_t i = 0; i < in.size(); i++) {
  145. Json item = in[i];
  146. Json in_crop = item["crop"];
  147. string id = item["sourceId"].string_value();
  148. string name = get_source_name_from_id(out_sources, id);
  149. Json::array hotkey_items =
  150. item["hotkeys"]["items"].array_items();
  151. get_hotkey_bindings(hotkeys, hotkey_items, name);
  152. out_items.push_back(Json::object{
  153. {"name", name},
  154. {"id", length++},
  155. {"pos",
  156. Json::object{{"x", item["x"]}, {"y", item["y"]}}},
  157. {"scale", Json::object{{"x", item["scaleX"]},
  158. {"y", item["scaleY"]}}},
  159. {"rot", item["rotation"]},
  160. {"visible", item["visible"]},
  161. {"crop_top", in_crop["top"]},
  162. {"crop_bottom", in_crop["bottom"]},
  163. {"crop_left", in_crop["left"]},
  164. {"crop_right", in_crop["right"]}});
  165. }
  166. scene["hotkeys"] = hotkeys;
  167. scene["settings"] =
  168. Json::object{{"items", out_items}, {"id_counter", length}};
  169. }
  170. static int attempt_import(const Json &root, const string &name, Json &res)
  171. {
  172. Json::array source_arr = root["sources"]["items"].array_items();
  173. Json::array scenes_arr = root["scenes"]["items"].array_items();
  174. Json::array t_arr = root["transitions"]["transitions"].array_items();
  175. string t_id = root["transitions"]["defaultTransitionId"].string_value();
  176. Json::array out_sources = Json::array{};
  177. Json::array out_transitions = Json::array{};
  178. for (size_t i = 0; i < source_arr.size(); i++) {
  179. Json source = source_arr[i];
  180. Json in_hotkeys = source["hotkeys"];
  181. Json::array hotkey_items =
  182. source["hotkeys"]["items"].array_items();
  183. Json in_filters = source["filters"];
  184. Json::array filter_items = in_filters["items"].array_items();
  185. Json in_settings = source["settings"];
  186. Json in_sync = source["syncOffset"];
  187. int sync = (int)(in_sync["sec"].number_value() * 1000000000 +
  188. in_sync["nsec"].number_value());
  189. double vol = source["volume"].number_value();
  190. bool muted = source["muted"].bool_value();
  191. string name = source["name"].string_value();
  192. int monitoring = (int)source["monitoringType"].int_value();
  193. Json::object out_hotkeys = Json::object{};
  194. get_hotkey_bindings(out_hotkeys, hotkey_items, "");
  195. Json::array out_filters = Json::array{};
  196. for (size_t x = 0; x < filter_items.size(); x++) {
  197. Json::object filter = filter_items[x].object_items();
  198. string type = filter["type"].string_value();
  199. filter["id"] = type;
  200. out_filters.push_back(filter);
  201. }
  202. int copy = 1;
  203. string out_name = name;
  204. while (source_name_exists(out_sources, out_name))
  205. out_name = name + "(" + to_string(copy++) + ")";
  206. string sl_id = source["id"].string_value();
  207. out_sources.push_back(
  208. Json::object{{"filters", out_filters},
  209. {"hotkeys", out_hotkeys},
  210. {"id", source["type"]},
  211. {"sl_id", sl_id},
  212. {"settings", in_settings},
  213. {"sync", sync},
  214. {"volume", vol},
  215. {"muted", muted},
  216. {"name", out_name},
  217. {"monitoring_type", monitoring}});
  218. }
  219. string scene_name = "";
  220. for (size_t i = 0; i < scenes_arr.size(); i++) {
  221. Json scene = scenes_arr[i];
  222. Json in_hotkeys = scene["hotkeys"];
  223. Json::array hotkey_items = in_hotkeys["items"].array_items();
  224. Json in_filters = scene["filters"];
  225. Json::array filter_items = in_filters["items"].array_items();
  226. Json in_settings = scene["settings"];
  227. Json in_sync = scene["syncOffset"];
  228. int sync = (int)(in_sync["sec"].number_value() * 1000000000 +
  229. in_sync["nsec"].number_value());
  230. double vol = scene["volume"].number_value();
  231. bool muted = scene["muted"].bool_value();
  232. string name = scene["name"].string_value();
  233. int monitoring = scene["monitoringType"].int_value();
  234. Json::object out_hotkeys = Json::object{};
  235. get_hotkey_bindings(out_hotkeys, hotkey_items, "");
  236. Json::array out_filters = Json::array{};
  237. for (size_t x = 0; x < filter_items.size(); x++) {
  238. Json::object filter = filter_items[x].object_items();
  239. string type = filter["type"].string_value();
  240. filter["id"] = type;
  241. out_filters.push_back(filter);
  242. }
  243. int copy = 1;
  244. string out_name = name;
  245. while (source_name_exists(out_sources, out_name))
  246. out_name = name + "(" + to_string(copy++) + ")";
  247. if (scene_name == "")
  248. scene_name = out_name;
  249. string sl_id = scene["id"].string_value();
  250. Json::object out =
  251. Json::object{{"filters", out_filters},
  252. {"hotkeys", out_hotkeys},
  253. {"id", "scene"},
  254. {"sl_id", sl_id},
  255. {"settings", in_settings},
  256. {"sync", sync},
  257. {"volume", vol},
  258. {"muted", muted},
  259. {"name", out_name},
  260. {"monitoring_type", monitoring},
  261. {"private_settings", Json::object{}}};
  262. Json in_items = scene["sceneItems"];
  263. Json::array items_arr = in_items["items"].array_items();
  264. get_scene_items(out_sources, out, items_arr);
  265. out_sources.push_back(out);
  266. }
  267. string transition_name = "";
  268. for (size_t i = 0; i < t_arr.size(); i++) {
  269. Json transition = t_arr[i];
  270. Json in_settings = transition["settings"];
  271. int duration = transition["duration"].int_value();
  272. string name = transition["name"].string_value();
  273. string id = transition["id"].string_value();
  274. if (id == t_id)
  275. transition_name = name;
  276. out_transitions.push_back(
  277. Json::object{{"id", transition["type"]},
  278. {"settings", in_settings},
  279. {"name", name},
  280. {"duration", duration}});
  281. }
  282. res = Json::object{{"sources", out_sources},
  283. {"transitions", out_transitions},
  284. {"current_scene", scene_name},
  285. {"current_program_scene", scene_name},
  286. {"current_transition", transition_name},
  287. {"name", name == "" ? "Streamlabs Import" : name}};
  288. return IMPORTER_SUCCESS;
  289. }
  290. string SLImporter::Name(const string &path)
  291. {
  292. string name;
  293. string folder = GetFolderFromPath(path);
  294. string manifest_file = GetFilenameFromPath(path);
  295. string manifest_path = folder + "manifest.json";
  296. if (os_file_exists(manifest_path.c_str())) {
  297. BPtr<char> file_data =
  298. os_quick_read_utf8_file(manifest_path.c_str());
  299. string err;
  300. Json data = Json::parse(file_data, err);
  301. if (err == "") {
  302. Json::array collections =
  303. data["collections"].array_items();
  304. bool name_set = false;
  305. for (size_t i = 0, l = collections.size(); i < l; i++) {
  306. Json c = collections[i];
  307. string c_id = c["id"].string_value();
  308. string c_name = c["name"].string_value();
  309. if (c_id == manifest_file) {
  310. name = c_name;
  311. name_set = true;
  312. break;
  313. }
  314. }
  315. if (!name_set) {
  316. name = "Unknown Streamlabs Import";
  317. }
  318. }
  319. } else {
  320. name = "Unknown Streamlabs Import";
  321. }
  322. return name;
  323. }
  324. int SLImporter::ImportScenes(const string &path, string &name, Json &res)
  325. {
  326. BPtr<char> file_data = os_quick_read_utf8_file(path.c_str());
  327. std::string err;
  328. Json data = Json::parse(file_data, err);
  329. if (err != "")
  330. return IMPORTER_ERROR_DURING_CONVERSION;
  331. string node_type = data["nodeType"].string_value();
  332. int result = IMPORTER_ERROR_DURING_CONVERSION;
  333. if (node_type == "RootNode") {
  334. if (name == "") {
  335. string auto_name = Name(path);
  336. result = attempt_import(data, auto_name, res);
  337. } else {
  338. result = attempt_import(data, name, res);
  339. }
  340. }
  341. TranslateOSStudio(res);
  342. return result;
  343. }
  344. bool SLImporter::Check(const string &path)
  345. {
  346. bool check = false;
  347. BPtr<char> file_data = os_quick_read_utf8_file(path.c_str());
  348. if (file_data) {
  349. string err;
  350. Json root = Json::parse(file_data, err);
  351. if (!root.is_null()) {
  352. string node_type = root["nodeType"].string_value();
  353. if (node_type == "RootNode")
  354. check = true;
  355. }
  356. }
  357. return check;
  358. }
  359. OBSImporterFiles SLImporter::FindFiles()
  360. {
  361. OBSImporterFiles res;
  362. #ifdef _WIN32
  363. char dst[512];
  364. int found = os_get_config_path(dst, 512,
  365. "slobs-client\\SceneCollections\\");
  366. if (found == -1)
  367. return res;
  368. os_dir_t *dir = os_opendir(dst);
  369. struct os_dirent *ent;
  370. while ((ent = os_readdir(dir)) != NULL) {
  371. string name = ent->d_name;
  372. if (ent->directory || name[0] == '.' || name == "manifest.json")
  373. continue;
  374. size_t pos = name.find_last_of(".json");
  375. size_t end_pos = name.size() - 1;
  376. if (pos != -1 && pos == end_pos) {
  377. string str = dst + name;
  378. res.push_back(str);
  379. }
  380. }
  381. os_closedir(dir);
  382. #endif
  383. return res;
  384. }