sl.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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 int attempt_import(const Json &root, const string &name, Json &res)
  185. {
  186. Json::array source_arr = root["sources"]["items"].array_items();
  187. Json::array scenes_arr = root["scenes"]["items"].array_items();
  188. Json::array t_arr = root["transitions"]["transitions"].array_items();
  189. string t_id = root["transitions"]["defaultTransitionId"].string_value();
  190. Json::array out_sources = Json::array{};
  191. Json::array out_transitions = Json::array{};
  192. for (size_t i = 0; i < source_arr.size(); i++) {
  193. Json source = source_arr[i];
  194. Json in_hotkeys = source["hotkeys"];
  195. Json::array hotkey_items =
  196. source["hotkeys"]["items"].array_items();
  197. Json in_filters = source["filters"];
  198. Json::array filter_items = in_filters["items"].array_items();
  199. Json in_settings = source["settings"];
  200. Json in_sync = source["syncOffset"];
  201. int sync = (int)(in_sync["sec"].number_value() * 1000000000 +
  202. in_sync["nsec"].number_value());
  203. double vol = source["volume"].number_value();
  204. bool muted = source["muted"].bool_value();
  205. string name = source["name"].string_value();
  206. int monitoring = (int)source["monitoringType"].int_value();
  207. Json::object out_hotkeys = Json::object{};
  208. get_hotkey_bindings(out_hotkeys, hotkey_items, "");
  209. Json::array out_filters = Json::array{};
  210. for (size_t x = 0; x < filter_items.size(); x++) {
  211. Json::object filter = filter_items[x].object_items();
  212. string type = filter["type"].string_value();
  213. filter["id"] = type;
  214. out_filters.push_back(filter);
  215. }
  216. int copy = 1;
  217. string out_name = name;
  218. while (source_name_exists(out_sources, out_name))
  219. out_name = name + "(" + to_string(copy++) + ")";
  220. string sl_id = source["id"].string_value();
  221. out_sources.push_back(
  222. Json::object{{"filters", out_filters},
  223. {"hotkeys", out_hotkeys},
  224. {"id", source["type"]},
  225. {"sl_id", sl_id},
  226. {"settings", in_settings},
  227. {"sync", sync},
  228. {"volume", vol},
  229. {"muted", muted},
  230. {"name", out_name},
  231. {"monitoring_type", monitoring}});
  232. }
  233. string scene_name = "";
  234. for (size_t i = 0; i < scenes_arr.size(); i++) {
  235. Json scene = scenes_arr[i];
  236. Json in_hotkeys = scene["hotkeys"];
  237. Json::array hotkey_items = in_hotkeys["items"].array_items();
  238. Json in_filters = scene["filters"];
  239. Json::array filter_items = in_filters["items"].array_items();
  240. Json in_settings = scene["settings"];
  241. string name = scene["name"].string_value();
  242. Json::object out_hotkeys = Json::object{};
  243. get_hotkey_bindings(out_hotkeys, hotkey_items, "");
  244. Json::array out_filters = Json::array{};
  245. for (size_t x = 0; x < filter_items.size(); x++) {
  246. Json::object filter = filter_items[x].object_items();
  247. string type = filter["type"].string_value();
  248. filter["id"] = type;
  249. out_filters.push_back(filter);
  250. }
  251. int copy = 1;
  252. string out_name = name;
  253. while (source_name_exists(out_sources, out_name))
  254. out_name = name + "(" + to_string(copy++) + ")";
  255. if (scene_name.empty())
  256. scene_name = out_name;
  257. string sl_id = scene["id"].string_value();
  258. Json::object out =
  259. Json::object{{"filters", out_filters},
  260. {"hotkeys", out_hotkeys},
  261. {"id", "scene"},
  262. {"sl_id", sl_id},
  263. {"settings", in_settings},
  264. {"volume", 1.0},
  265. {"name", out_name},
  266. {"private_settings", Json::object{}}};
  267. Json in_items = scene["sceneItems"];
  268. Json::array items_arr = in_items["items"].array_items();
  269. get_scene_items(root, out_sources, out, items_arr);
  270. out_sources.push_back(out);
  271. }
  272. string transition_name = "";
  273. for (size_t i = 0; i < t_arr.size(); i++) {
  274. Json transition = t_arr[i];
  275. Json in_settings = transition["settings"];
  276. int duration = transition["duration"].int_value();
  277. string name = transition["name"].string_value();
  278. string id = transition["id"].string_value();
  279. if (id == t_id)
  280. transition_name = name;
  281. out_transitions.push_back(
  282. Json::object{{"id", transition["type"]},
  283. {"settings", in_settings},
  284. {"name", name},
  285. {"duration", duration}});
  286. }
  287. res = Json::object{{"sources", out_sources},
  288. {"transitions", out_transitions},
  289. {"current_scene", scene_name},
  290. {"current_program_scene", scene_name},
  291. {"current_transition", transition_name},
  292. {"name", name == "" ? "Streamlabs Import" : name}};
  293. return IMPORTER_SUCCESS;
  294. }
  295. string SLImporter::Name(const string &path)
  296. {
  297. string name;
  298. string folder = GetFolderFromPath(path);
  299. string manifest_file = GetFilenameFromPath(path);
  300. string manifest_path = folder + "manifest.json";
  301. if (os_file_exists(manifest_path.c_str())) {
  302. BPtr<char> file_data =
  303. os_quick_read_utf8_file(manifest_path.c_str());
  304. string err;
  305. Json data = Json::parse(file_data, err);
  306. if (err == "") {
  307. Json::array collections =
  308. data["collections"].array_items();
  309. bool name_set = false;
  310. for (size_t i = 0, l = collections.size(); i < l; i++) {
  311. Json c = collections[i];
  312. string c_id = c["id"].string_value();
  313. string c_name = c["name"].string_value();
  314. if (c_id == manifest_file) {
  315. name = std::move(c_name);
  316. name_set = true;
  317. break;
  318. }
  319. }
  320. if (!name_set) {
  321. name = "Unknown Streamlabs Import";
  322. }
  323. }
  324. } else {
  325. name = "Unknown Streamlabs Import";
  326. }
  327. return name;
  328. }
  329. int SLImporter::ImportScenes(const string &path, string &name, Json &res)
  330. {
  331. BPtr<char> file_data = os_quick_read_utf8_file(path.c_str());
  332. std::string err;
  333. Json data = Json::parse(file_data, err);
  334. if (err != "")
  335. return IMPORTER_ERROR_DURING_CONVERSION;
  336. string node_type = data["nodeType"].string_value();
  337. int result = IMPORTER_ERROR_DURING_CONVERSION;
  338. if (node_type == "RootNode") {
  339. if (name == "") {
  340. string auto_name = Name(path);
  341. result = attempt_import(data, auto_name, res);
  342. } else {
  343. result = attempt_import(data, name, res);
  344. }
  345. }
  346. TranslateOSStudio(res);
  347. return result;
  348. }
  349. bool SLImporter::Check(const string &path)
  350. {
  351. bool check = false;
  352. BPtr<char> file_data = os_quick_read_utf8_file(path.c_str());
  353. if (file_data) {
  354. string err;
  355. Json root = Json::parse(file_data, err);
  356. if (!root.is_null()) {
  357. string node_type = root["nodeType"].string_value();
  358. if (node_type == "RootNode")
  359. check = true;
  360. }
  361. }
  362. return check;
  363. }
  364. OBSImporterFiles SLImporter::FindFiles()
  365. {
  366. OBSImporterFiles res;
  367. #ifdef _WIN32
  368. char dst[512];
  369. int found = os_get_config_path(dst, 512,
  370. "slobs-client\\SceneCollections\\");
  371. if (found == -1)
  372. return res;
  373. os_dir_t *dir = os_opendir(dst);
  374. struct os_dirent *ent;
  375. while ((ent = os_readdir(dir)) != NULL) {
  376. string name = ent->d_name;
  377. if (ent->directory || name[0] == '.' || name == "manifest.json")
  378. continue;
  379. size_t pos = name.find_last_of(".json");
  380. size_t end_pos = name.size() - 1;
  381. if (pos != -1 && pos == end_pos) {
  382. string str = dst + name;
  383. res.push_back(str);
  384. }
  385. }
  386. os_closedir(dir);
  387. #endif
  388. return res;
  389. }