sl.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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 &root,
  104. const Json::array &sources,
  105. const string &id)
  106. {
  107. for (size_t i = 0; i < sources.size(); i++) {
  108. Json item = sources[i];
  109. string source_id = item["sl_id"].string_value();
  110. if (source_id == id)
  111. return item["name"].string_value();
  112. }
  113. Json::array scene_arr = root["scenes"]["items"].array_items();
  114. for (size_t i = 0; i < scene_arr.size(); i++) {
  115. Json item = scene_arr[i];
  116. string source_id = item["id"].string_value();
  117. if (source_id == id) {
  118. string name = item["name"].string_value();
  119. int copy = 1;
  120. string out_name = name;
  121. while (source_name_exists(sources, out_name))
  122. out_name = name + "(" + to_string(copy++) + ")";
  123. return out_name;
  124. }
  125. }
  126. return "";
  127. }
  128. static void get_hotkey_bindings(Json::object &out_hotkeys,
  129. const Json &in_hotkeys, const string &name)
  130. {
  131. Json::array hot_arr = in_hotkeys.array_items();
  132. for (size_t i = 0; i < hot_arr.size(); i++) {
  133. Json hotkey = hot_arr[i];
  134. Json::array bindings = hotkey["bindings"].array_items();
  135. Json::array out_hotkey = Json::array{};
  136. string hotkey_name = translate_hotkey(hotkey, name);
  137. for (size_t x = 0; x < bindings.size(); x++) {
  138. Json binding = bindings[x];
  139. Json modifiers = binding["modifiers"];
  140. string key =
  141. translate_key(binding["key"].string_value());
  142. out_hotkey.push_back(
  143. Json::object{{"control", modifiers["ctrl"]},
  144. {"shift", modifiers["shift"]},
  145. {"command", modifiers["meta"]},
  146. {"alt", modifiers["alt"]},
  147. {"key", key}});
  148. }
  149. out_hotkeys[hotkey_name] = out_hotkey;
  150. }
  151. }
  152. static void get_scene_items(const Json &root, const Json::array &out_sources,
  153. Json::object &scene, const Json::array &in)
  154. {
  155. int length = 0;
  156. Json::object hotkeys = scene["hotkeys"].object_items();
  157. Json::array out_items = Json::array{};
  158. for (size_t i = 0; i < in.size(); i++) {
  159. Json item = in[i];
  160. Json in_crop = item["crop"];
  161. string id = item["sourceId"].string_value();
  162. string name = get_source_name_from_id(root, out_sources, id);
  163. Json::array hotkey_items =
  164. item["hotkeys"]["items"].array_items();
  165. get_hotkey_bindings(hotkeys, hotkey_items, name);
  166. out_items.push_back(Json::object{
  167. {"name", name},
  168. {"id", length++},
  169. {"pos",
  170. Json::object{{"x", item["x"]}, {"y", item["y"]}}},
  171. {"scale", Json::object{{"x", item["scaleX"]},
  172. {"y", item["scaleY"]}}},
  173. {"rot", item["rotation"]},
  174. {"visible", item["visible"]},
  175. {"crop_top", in_crop["top"]},
  176. {"crop_bottom", in_crop["bottom"]},
  177. {"crop_left", in_crop["left"]},
  178. {"crop_right", in_crop["right"]}});
  179. }
  180. scene["hotkeys"] = hotkeys;
  181. scene["settings"] =
  182. Json::object{{"items", out_items}, {"id_counter", length}};
  183. }
  184. static void translate_screen_capture(Json::object &out_settings, string &type)
  185. {
  186. string subtype_info =
  187. out_settings["capture_source_list"].string_value();
  188. size_t pos = subtype_info.find(':');
  189. string subtype = subtype_info.substr(0, pos);
  190. if (subtype == "game") {
  191. type = "game_capture";
  192. } else if (subtype == "monitor") {
  193. type = "monitor_capture";
  194. out_settings["monitor"] = subtype_info.substr(pos);
  195. } else if (subtype == "window") {
  196. type = "window_capture";
  197. out_settings["cursor"] = out_settings["capture_cursor"];
  198. out_settings["window"] =
  199. out_settings["capture_window_line"].string_value();
  200. out_settings["capture_cursor"] = nullptr;
  201. }
  202. out_settings["auto_capture_rules_path"] = nullptr;
  203. out_settings["auto_placeholder_image"] = nullptr;
  204. out_settings["auto_placeholder_message"] = nullptr;
  205. out_settings["capture_source_list"] = nullptr;
  206. out_settings["capture_window_line"] = nullptr;
  207. }
  208. static int attempt_import(const Json &root, const string &name, Json &res)
  209. {
  210. Json::array source_arr = root["sources"]["items"].array_items();
  211. Json::array scenes_arr = root["scenes"]["items"].array_items();
  212. Json::array t_arr = root["transitions"]["transitions"].array_items();
  213. string t_id = root["transitions"]["defaultTransitionId"].string_value();
  214. Json::array out_sources = Json::array{};
  215. Json::array out_transitions = Json::array{};
  216. for (size_t i = 0; i < source_arr.size(); i++) {
  217. Json source = source_arr[i];
  218. Json in_hotkeys = source["hotkeys"];
  219. Json::array hotkey_items =
  220. source["hotkeys"]["items"].array_items();
  221. Json in_filters = source["filters"];
  222. Json::array filter_items = in_filters["items"].array_items();
  223. Json in_settings = source["settings"];
  224. Json in_sync = source["syncOffset"];
  225. int sync = (int)(in_sync["sec"].number_value() * 1000000000 +
  226. in_sync["nsec"].number_value());
  227. double vol = source["volume"].number_value();
  228. bool muted = source["muted"].bool_value();
  229. string name = source["name"].string_value();
  230. int monitoring = (int)source["monitoringType"].int_value();
  231. Json::object out_hotkeys = Json::object{};
  232. get_hotkey_bindings(out_hotkeys, hotkey_items, "");
  233. Json::array out_filters = Json::array{};
  234. for (size_t x = 0; x < filter_items.size(); x++) {
  235. Json::object filter = filter_items[x].object_items();
  236. string type = filter["type"].string_value();
  237. filter["id"] = type;
  238. out_filters.push_back(filter);
  239. }
  240. int copy = 1;
  241. string out_name = name;
  242. while (source_name_exists(out_sources, out_name))
  243. out_name = name + "(" + to_string(copy++) + ")";
  244. string sl_id = source["id"].string_value();
  245. string type = source["type"].string_value();
  246. Json::object out_settings = in_settings.object_items();
  247. if (type == "screen_capture") {
  248. translate_screen_capture(out_settings, type);
  249. }
  250. out_sources.push_back(
  251. Json::object{{"filters", out_filters},
  252. {"hotkeys", out_hotkeys},
  253. {"id", type},
  254. {"sl_id", sl_id},
  255. {"settings", out_settings},
  256. {"sync", sync},
  257. {"volume", vol},
  258. {"muted", muted},
  259. {"name", out_name},
  260. {"monitoring_type", monitoring}});
  261. }
  262. string scene_name = "";
  263. for (size_t i = 0; i < scenes_arr.size(); i++) {
  264. Json scene = scenes_arr[i];
  265. Json in_hotkeys = scene["hotkeys"];
  266. Json::array hotkey_items = in_hotkeys["items"].array_items();
  267. Json in_filters = scene["filters"];
  268. Json::array filter_items = in_filters["items"].array_items();
  269. Json in_settings = scene["settings"];
  270. string name = scene["name"].string_value();
  271. Json::object out_hotkeys = Json::object{};
  272. get_hotkey_bindings(out_hotkeys, hotkey_items, "");
  273. Json::array out_filters = Json::array{};
  274. for (size_t x = 0; x < filter_items.size(); x++) {
  275. Json::object filter = filter_items[x].object_items();
  276. string type = filter["type"].string_value();
  277. filter["id"] = type;
  278. out_filters.push_back(filter);
  279. }
  280. int copy = 1;
  281. string out_name = name;
  282. while (source_name_exists(out_sources, out_name))
  283. out_name = name + "(" + to_string(copy++) + ")";
  284. if (scene_name.empty())
  285. scene_name = out_name;
  286. string sl_id = scene["id"].string_value();
  287. Json::object out =
  288. Json::object{{"filters", out_filters},
  289. {"hotkeys", out_hotkeys},
  290. {"id", "scene"},
  291. {"sl_id", sl_id},
  292. {"settings", in_settings},
  293. {"volume", 1.0},
  294. {"name", out_name},
  295. {"private_settings", Json::object{}}};
  296. Json in_items = scene["sceneItems"];
  297. Json::array items_arr = in_items["items"].array_items();
  298. get_scene_items(root, out_sources, out, items_arr);
  299. out_sources.push_back(out);
  300. }
  301. string transition_name = "";
  302. for (size_t i = 0; i < t_arr.size(); i++) {
  303. Json transition = t_arr[i];
  304. Json in_settings = transition["settings"];
  305. int duration = transition["duration"].int_value();
  306. string name = transition["name"].string_value();
  307. string id = transition["id"].string_value();
  308. if (id == t_id)
  309. transition_name = name;
  310. out_transitions.push_back(
  311. Json::object{{"id", transition["type"]},
  312. {"settings", in_settings},
  313. {"name", name},
  314. {"duration", duration}});
  315. }
  316. res = Json::object{{"sources", out_sources},
  317. {"transitions", out_transitions},
  318. {"current_scene", scene_name},
  319. {"current_program_scene", scene_name},
  320. {"current_transition", transition_name},
  321. {"name", name == "" ? "Streamlabs Import" : name}};
  322. return IMPORTER_SUCCESS;
  323. }
  324. string SLImporter::Name(const string &path)
  325. {
  326. string name;
  327. string folder = GetFolderFromPath(path);
  328. string manifest_file = GetFilenameFromPath(path);
  329. string manifest_path = folder + "manifest.json";
  330. if (os_file_exists(manifest_path.c_str())) {
  331. BPtr<char> file_data =
  332. os_quick_read_utf8_file(manifest_path.c_str());
  333. string err;
  334. Json data = Json::parse(file_data, err);
  335. if (err == "") {
  336. Json::array collections =
  337. data["collections"].array_items();
  338. bool name_set = false;
  339. for (size_t i = 0, l = collections.size(); i < l; i++) {
  340. Json c = collections[i];
  341. string c_id = c["id"].string_value();
  342. string c_name = c["name"].string_value();
  343. if (c_id == manifest_file) {
  344. name = std::move(c_name);
  345. name_set = true;
  346. break;
  347. }
  348. }
  349. if (!name_set) {
  350. name = "Unknown Streamlabs Import";
  351. }
  352. }
  353. } else {
  354. name = "Unknown Streamlabs Import";
  355. }
  356. return name;
  357. }
  358. int SLImporter::ImportScenes(const string &path, string &name, Json &res)
  359. {
  360. BPtr<char> file_data = os_quick_read_utf8_file(path.c_str());
  361. std::string err;
  362. Json data = Json::parse(file_data, err);
  363. if (err != "")
  364. return IMPORTER_ERROR_DURING_CONVERSION;
  365. string node_type = data["nodeType"].string_value();
  366. int result = IMPORTER_ERROR_DURING_CONVERSION;
  367. if (node_type == "RootNode") {
  368. if (name == "") {
  369. string auto_name = Name(path);
  370. result = attempt_import(data, auto_name, res);
  371. } else {
  372. result = attempt_import(data, name, res);
  373. }
  374. }
  375. QDir dir(path.c_str());
  376. TranslateOSStudio(res);
  377. TranslatePaths(res, QDir::cleanPath(dir.filePath("..")).toStdString());
  378. return result;
  379. }
  380. bool SLImporter::Check(const string &path)
  381. {
  382. bool check = false;
  383. BPtr<char> file_data = os_quick_read_utf8_file(path.c_str());
  384. if (file_data) {
  385. string err;
  386. Json root = Json::parse(file_data, err);
  387. if (!root.is_null()) {
  388. string node_type = root["nodeType"].string_value();
  389. if (node_type == "RootNode")
  390. check = true;
  391. }
  392. }
  393. return check;
  394. }
  395. OBSImporterFiles SLImporter::FindFiles()
  396. {
  397. OBSImporterFiles res;
  398. #if defined(_WIN32) || defined(__APPLE__)
  399. char dst[512];
  400. int found =
  401. os_get_config_path(dst, 512, "slobs-client/SceneCollections/");
  402. if (found == -1)
  403. return res;
  404. os_dir_t *dir = os_opendir(dst);
  405. struct os_dirent *ent;
  406. while ((ent = os_readdir(dir)) != NULL) {
  407. string name = ent->d_name;
  408. if (ent->directory || name[0] == '.' || name == "manifest.json")
  409. continue;
  410. size_t pos = name.find_last_of(".json");
  411. size_t end_pos = name.size() - 1;
  412. if (pos != -1 && pos == end_pos) {
  413. string str = dst + name;
  414. res.push_back(str);
  415. }
  416. }
  417. os_closedir(dir);
  418. #endif
  419. return res;
  420. }