sl.cpp 15 KB

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