Browse Source

[cdb] Fix prefab corruption when saving multiple prefabs that have the same absPath

Clément Espeute 1 year ago
parent
commit
e6f76daf20
3 changed files with 24 additions and 12 deletions
  1. 1 1
      hide/comp/SceneEditor.hx
  2. 21 10
      hide/comp/cdb/DataFiles.hx
  3. 2 1
      hrt/prefab/Unknown.hx

+ 1 - 1
hide/comp/SceneEditor.hx

@@ -1761,7 +1761,7 @@ class SceneEditor {
 			return true;
 			return true;
 		}
 		}
 
 
-		var onRenameFunc = function(e, name) {
+		var onRenameFunc = function(e : hrt.prefab.Prefab, name) {
 			var oldName = e.name;
 			var oldName = e.name;
 			e.name = name;
 			e.name = name;
 
 

+ 21 - 10
hide/comp/cdb/DataFiles.hx

@@ -40,6 +40,8 @@ class DataFiles {
 		var levelID = file.split("/").pop().split(".").shift();
 		var levelID = file.split("/").pop().split(".").shift();
 		levelID = levelID.charAt(0).toUpperCase()+levelID.substr(1);
 		levelID = levelID.charAt(0).toUpperCase()+levelID.substr(1);
 
 
+		var allMap : Map<String, Array<hrt.prefab.Prefab>> = [];
+
 		function loadRec( p : hrt.prefab.Prefab, parent : hrt.prefab.Prefab, toRemove : Array<DataProps> ) {
 		function loadRec( p : hrt.prefab.Prefab, parent : hrt.prefab.Prefab, toRemove : Array<DataProps> ) {
 			// Initiliaze to remove list with the full list of lines data.
 			// Initiliaze to remove list with the full list of lines data.
 			if (parent == null) {
 			if (parent == null) {
@@ -57,13 +59,15 @@ class DataFiles {
 					origin : haxe.Json.stringify(p.props)
 					origin : haxe.Json.stringify(p.props)
 				};
 				};
 
 
-				if( parent != null ) {
-					for( c in parent.children ) {
-						if( c == p ) break;
-						if( c.name == p.name ) dprops.index++;
-					}
+				// deduplicate prefabs that have the same absPath
+				var all = allMap.get(dprops.path);
+				if (all == null) {
+					all = getPrefabsByPath(p.getRoot(), dprops.path);
+					allMap.set(dprops.path, all);
 				}
 				}
 
 
+				dprops.index = all.indexOf(p);
+
 				if( sheet.idCol != null && Reflect.field(p.props,sheet.idCol.name) == "" )
 				if( sheet.idCol != null && Reflect.field(p.props,sheet.idCol.name) == "" )
 					Reflect.setField(p.props,sheet.idCol.name,levelID+"_"+p.name+(dprops.index == 0 ? "" : ""+dprops.index));
 					Reflect.setField(p.props,sheet.idCol.name,levelID+"_"+p.name+(dprops.index == 0 ? "" : ""+dprops.index));
 
 
@@ -552,6 +556,9 @@ class DataFiles {
 			var content = null;
 			var content = null;
 			var levelID = file.split("/").pop().split(".").shift();
 			var levelID = file.split("/").pop().split(".").shift();
 			levelID = levelID.charAt(0).toUpperCase()+levelID.substr(1);
 			levelID = levelID.charAt(0).toUpperCase()+levelID.substr(1);
+
+			var allMap : Map<String, Array<hrt.prefab.Prefab>> = [];
+
 			function loadRec( p : hrt.prefab.Prefab, parent : hrt.prefab.Prefab ) {
 			function loadRec( p : hrt.prefab.Prefab, parent : hrt.prefab.Prefab ) {
 				if( p.getCdbType() == sheetName ) {
 				if( p.getCdbType() == sheetName ) {
 					var dprops : DataProps = {
 					var dprops : DataProps = {
@@ -560,12 +567,16 @@ class DataFiles {
 						index : 0,
 						index : 0,
 						origin : haxe.Json.stringify(p.props),
 						origin : haxe.Json.stringify(p.props),
 					};
 					};
-					if( parent != null ) {
-						for( c in parent.children ) {
-							if( c == p ) break;
-							if( c.name == p.name ) dprops.index++;
-						}
+
+					// deduplicate prefabs that have the same absPath
+					var all = allMap.get(dprops.path);
+					if (all == null) {
+						all = getPrefabsByPath(p.getRoot(), dprops.path);
+						allMap.set(dprops.path, all);
 					}
 					}
+
+					dprops.index = all.indexOf(p);
+
 					if( sheet.idCol != null && Reflect.field(p.props,sheet.idCol.name) == "" )
 					if( sheet.idCol != null && Reflect.field(p.props,sheet.idCol.name) == "" )
 						Reflect.setField(p.props,sheet.idCol.name,levelID+"_"+p.name+(dprops.index == 0 ? "" : ""+dprops.index));
 						Reflect.setField(p.props,sheet.idCol.name,levelID+"_"+p.name+(dprops.index == 0 ? "" : ""+dprops.index));
 					if( content == null ) {
 					if( content == null ) {

+ 2 - 1
hrt/prefab/Unknown.hx

@@ -30,7 +30,8 @@ class Unknown extends Prefab {
 
 
 	override function save() {
 	override function save() {
 		var to : Dynamic = {};
 		var to : Dynamic = {};
-		to.name = name;
+		if (name != "")
+			to.name = name;
 		for (f in Reflect.fields(data)) {
 		for (f in Reflect.fields(data)) {
 			Reflect.setField(to, f, copyValue(Reflect.getProperty(data, f)));
 			Reflect.setField(to, f, copyValue(Reflect.getProperty(data, f)));
 		}
 		}