Bläddra i källkod

Merge pull request #2457 from VodBox/sl-dup-fix

UI: Fix importing SL collections with repeated names
Jim 5 år sedan
förälder
incheckning
934a9e8bae
1 ändrade filer med 41 tillägg och 17 borttagningar
  1. 41 17
      UI/importers/sl.cpp

+ 41 - 17
UI/importers/sl.cpp

@@ -107,20 +107,28 @@ static string translate_hotkey(const Json &hotkey, const string &source)
 #undef add_translation
 }
 
-static string get_source_name_from_id(const Json &root, const string &id)
+static bool source_name_exists(const Json::array &sources, const string &name)
 {
-	Json::array source_arr = root["sources"]["items"].array_items();
-	Json::array scene_arr = root["scenes"]["items"].array_items();
+	for (size_t i = 0; i < sources.size(); i++) {
+		Json item = sources[i];
+		string source_name = item["name"].string_value();
 
-	source_arr.insert(source_arr.end(), scene_arr.begin(), scene_arr.end());
+		if (source_name == name)
+			return true;
+	}
 
-	for (size_t i = 0; i < source_arr.size(); i++) {
-		Json item = source_arr[i];
-		string source_id = item["id"].string_value();
+	return false;
+}
 
-		if (source_id == id) {
+static string get_source_name_from_id(const Json::array &sources,
+				      const string &id)
+{
+	for (size_t i = 0; i < sources.size(); i++) {
+		Json item = sources[i];
+		string source_id = item["sl_id"].string_value();
+
+		if (source_id == id)
 			return item["name"].string_value();
-		}
 	}
 
 	return "";
@@ -156,7 +164,7 @@ static void get_hotkey_bindings(Json::object &out_hotkeys,
 	}
 }
 
-static void get_scene_items(const Json &root, Json::object &scene,
+static void get_scene_items(const Json::array &out_sources, Json::object &scene,
 			    const Json::array &in)
 {
 	int length = 0;
@@ -169,7 +177,7 @@ static void get_scene_items(const Json &root, Json::object &scene,
 
 		Json in_crop = item["crop"];
 		string id = item["sourceId"].string_value();
-		string name = get_source_name_from_id(root, id);
+		string name = get_source_name_from_id(out_sources, id);
 
 		Json::array hotkey_items =
 			item["hotkeys"]["items"].array_items();
@@ -239,15 +247,23 @@ static int attempt_import(const Json &root, const string &name, Json &res)
 			out_filters.push_back(filter);
 		}
 
+		int copy = 1;
+		string out_name = name;
+		while (source_name_exists(out_sources, out_name))
+			out_name = name + "(" + to_string(copy++) + ")";
+
+		string sl_id = source["id"].string_value();
+
 		out_sources.push_back(
 			Json::object{{"filters", out_filters},
 				     {"hotkeys", out_hotkeys},
 				     {"id", source["type"]},
+				     {"sl_id", sl_id},
 				     {"settings", in_settings},
 				     {"sync", sync},
 				     {"volume", vol},
 				     {"muted", muted},
-				     {"name", name},
+				     {"name", out_name},
 				     {"monitoring_type", monitoring}});
 	}
 
@@ -256,9 +272,6 @@ static int attempt_import(const Json &root, const string &name, Json &res)
 	for (size_t i = 0; i < scenes_arr.size(); i++) {
 		Json scene = scenes_arr[i];
 
-		if (scene_name == "")
-			scene_name = scene["name"].string_value();
-
 		Json in_hotkeys = scene["hotkeys"];
 		Json::array hotkey_items = in_hotkeys["items"].array_items();
 		Json in_filters = scene["filters"];
@@ -287,22 +300,33 @@ static int attempt_import(const Json &root, const string &name, Json &res)
 			out_filters.push_back(filter);
 		}
 
+		int copy = 1;
+		string out_name = name;
+		while (source_name_exists(out_sources, out_name))
+			out_name = name + "(" + to_string(copy++) + ")";
+
+		if (scene_name == "")
+			scene_name = out_name;
+
+		string sl_id = scene["id"].string_value();
+
 		Json::object out =
 			Json::object{{"filters", out_filters},
 				     {"hotkeys", out_hotkeys},
 				     {"id", "scene"},
+				     {"sl_id", sl_id},
 				     {"settings", in_settings},
 				     {"sync", sync},
 				     {"volume", vol},
 				     {"muted", muted},
-				     {"name", name},
+				     {"name", out_name},
 				     {"monitoring_type", monitoring},
 				     {"private_settings", Json::object{}}};
 
 		Json in_items = scene["sceneItems"];
 		Json::array items_arr = in_items["items"].array_items();
 
-		get_scene_items(root, out, items_arr);
+		get_scene_items(out_sources, out, items_arr);
 
 		out_sources.push_back(out);
 	}