Browse Source

cdb multi data files support

Nicolas Cannasse 5 năm trước cách đây
mục cha
commit
dfb534abc5
5 tập tin đã thay đổi với 112 bổ sung53 xóa
  1. 3 1
      hide/Ide.hx
  2. 1 2
      hide/comp/cdb/Cell.hx
  3. 61 14
      hide/comp/cdb/DataFiles.hx
  4. 5 0
      hide/comp/cdb/Editor.hx
  5. 42 36
      hide/comp/cdb/ObjEditor.hx

+ 3 - 1
hide/Ide.hx

@@ -667,7 +667,9 @@ class Ide {
 			return; // cancel load
 		database = new cdb.Database();
 		if( !exists ) return;
-		try {
+		if( isDebugger ) {
+			database.load(getFile(databaseFile).toString());
+		} else try {
 			database.load(getFile(databaseFile).toString());
 		} catch( e : Dynamic ) {
 			error(e);

+ 1 - 2
hide/comp/cdb/Cell.hx

@@ -181,8 +181,7 @@ class Cell extends Component {
 				'<span class="error">#MISSING</span>';
 			else {
 				var id = c.scope != null ? makeId(scope,c.scope,v) : v;
-				var uniq = editor.base.getSheet(sheet.name).index.get(id);
-				uniq == null || uniq.obj == obj ? v : '<span class="error">#DUP($v)</span>';
+				editor.isUniqueID(sheet,obj,id) ? v : '<span class="error">#DUP($v)</span>';
 			}
 		case TString if( c.kind == Script ):
 			v == "" ? "&nbsp;" : colorizeScript(c,v);

+ 61 - 14
hide/comp/cdb/DataFiles.hx

@@ -44,13 +44,18 @@ class DataFiles {
 		var ide = Ide.inst;
 		var lines : Array<Dynamic> = [];
 		var linesData : Array<DataProps> = [];
+		var separators = [];
+		var separatorTitles = [];
 		@:privateAccess {
 			sheet.sheet.lines = lines;
 			sheet.sheet.linesData = linesData;
+			sheet.sheet.separators = separators;
+			sheet.props.separatorTitles = separatorTitles;
 		}
-		if( !sys.FileSystem.exists(ide.getPath(sheet.props.dataFiles)) )
-			return;
 		function loadFile( file : String ) {
+			var needSep = true;
+			var levelID = file.split("/").pop().split(".").shift();
+			levelID = levelID.charAt(0).toUpperCase()+levelID.substr(1);
 			function loadRec( p : hrt.prefab.Prefab ) {
 				if( p.getCdbModel() == sheet ) {
 					var dprops : DataProps = {
@@ -58,6 +63,13 @@ class DataFiles {
 						path : p.getAbsPath(),
 						origin : haxe.Json.stringify(p.props),
 					};
+					if( needSep ) {
+						separators.push(lines.length);
+						separatorTitles.push(file);
+						needSep = false;
+					}
+					if( sheet.idCol != null && Reflect.field(p.props,sheet.idCol.name) == "" )
+						Reflect.setField(p.props,sheet.idCol.name,levelID+"_"+p.name);
 					linesData.push(dprops);
 					lines.push(p.props);
 				}
@@ -70,20 +82,52 @@ class DataFiles {
 				ide.fileWatcher.register(file, onFileChanged);
 			}
 		}
-		loadFile(sheet.props.dataFiles);
+
+		var path = sheet.props.dataFiles.split("/");
+		function gatherRec( curPath : Array<String>, i : Int ) {
+			var part = path[i++];
+			if( part == null ) {
+				var file = curPath.join("/");
+				if( sys.FileSystem.exists(ide.getPath(file)) ) loadFile(file);
+				return;
+			}
+			if( part.indexOf("*") < 0 ) {
+				curPath.push(part);
+				gatherRec(curPath,i);
+				curPath.pop();
+			} else {
+				var path = curPath.join("/");
+				var dir = ide.getPath(path);
+				if( !sys.FileSystem.isDirectory(dir) )
+					return;
+				if( !watching.exists(path) ) {
+					watching.set(path, true);
+					ide.fileWatcher.register(path, onFileChanged, true);
+				}
+				var reg = new EReg("^"+part.split(".").join("\\.").split("*").join(".*")+"$","");
+				for( f in sys.FileSystem.readDirectory(dir) ) {
+					if( !reg.match(f) ) continue;
+					curPath.push(f);
+					gatherRec(curPath,i);
+					curPath.pop();
+				}
+			}
+		}
+		gatherRec([],0);
 	}
 
 	public static function save( ?onSaveBase, ?force ) {
 		var ide = Ide.inst;
 		var temp = [];
+		var titles = [];
 		var prefabs = new Map();
 		for( s in base.sheets )
 			if( s.props.dataFiles != null ) {
-				var ldata = @:privateAccess s.sheet.linesData;
-				temp.push({ lines : s.lines, data : ldata });
+				var sheet = @:privateAccess s.sheet;
+				var ldata = sheet.linesData;
 				for( i in 0...s.lines.length ) {
 					var o = s.lines[i];
-					var p : DataProps = ldata[i];
+					var p : DataProps = sheet.linesData[i];
 					var str = haxe.Json.stringify(o);
 					if( str != p.origin || force ) {
 						p.origin = str;
@@ -99,10 +143,12 @@ class DataFiles {
 							inst.props = o;
 					}
 				}
-				@:privateAccess {
-					Reflect.deleteField(s.sheet,"lines");
-					Reflect.deleteField(s.sheet,"linesData");
-				}
+				temp.push(Reflect.copy(sheet));
+				titles.push(sheet.props.separatorTitles);
+				Reflect.deleteField(sheet,"lines");
+				Reflect.deleteField(sheet,"linesData");
+				Reflect.deleteField(sheet.props,"separatorTitles");
+				sheet.separators = [];
 			}
 		for( file => pf in prefabs ) {
 			skip++;
@@ -113,10 +159,11 @@ class DataFiles {
 		for( s in base.sheets ) {
 			if( s.props.dataFiles != null ) {
 				var d = temp.shift();
-				@:privateAccess {
-					s.sheet.lines = d.lines;
-					s.sheet.linesData = d.data;
-				}
+				var sheet = @:privateAccess s.sheet;
+				sheet.lines = d.lines;
+				sheet.linesData = d.linesData;
+				sheet.separators = d.separators;
+				sheet.props.separatorTitles = titles.shift();
 			}
 		}
 	}

+ 5 - 0
hide/comp/cdb/Editor.hx

@@ -493,6 +493,11 @@ class Editor extends Component {
 			}
 	}
 
+	function isUniqueID( sheet : cdb.Sheet, obj : {}, id : String ) {
+		var uniq = base.getSheet(sheet.name).index.get(id);
+		return uniq == null || uniq.obj == obj;
+	}
+
 	public function refresh( ?state : UndoState ) {
 
 		if( state == null )

+ 42 - 36
hide/comp/cdb/ObjEditor.hx

@@ -2,47 +2,53 @@ package hide.comp.cdb;
 
 class ObjEditor extends Editor {
 
-    public dynamic function onChange(propName : String) {}
-
-    var obj : {};
-
-    public function new( sheet : cdb.Sheet, props, obj : {}, ?parent : Element ) {
-        this.displayMode = AllProperties;
-        this.obj = obj;
-        var api = {
-            load : function(v:Any) {
-                var obj2 = haxe.Json.parse((v:String));
-                for( f in Reflect.fields(obj) )
-                    Reflect.deleteField(obj,f);
-                for( f in Reflect.fields(obj2) )
-                    Reflect.setField(obj, f, Reflect.field(obj2,f));
-            },
-            copy : function() return (haxe.Json.stringify(obj) : Any),
-            save : function() throw "assert",
-        };
-        super(props, api);
-        sheet = makePseudoSheet(sheet);
+	public dynamic function onChange(propName : String) {}
+
+	var obj : {};
+
+	public function new( sheet : cdb.Sheet, props, obj : {}, ?parent : Element ) {
+		this.displayMode = AllProperties;
+		this.obj = obj;
+		var api = {
+			load : function(v:Any) {
+				var obj2 = haxe.Json.parse((v:String));
+				for( f in Reflect.fields(obj) )
+					Reflect.deleteField(obj,f);
+				for( f in Reflect.fields(obj2) )
+					Reflect.setField(obj, f, Reflect.field(obj2,f));
+			},
+			copy : function() return (haxe.Json.stringify(obj) : Any),
+			save : function() throw "assert",
+		};
+		super(props, api);
+		sheet = makePseudoSheet(sheet);
 		show(sheet, parent);
-    }
+	}
 
-    override function syncSheet(?base:cdb.Database, ?name:String) {
-        super.syncSheet(base, name);
-        currentSheet = makePseudoSheet(currentSheet);
-    }
+	override function isUniqueID( sheet : cdb.Sheet, obj : {}, id : String ) {
+        // we don't know yet how to make sure that our object view
+        // is the same as the object in CDB indexed data
+		return true;
+	}
 
-    function makePseudoSheet( sheet : cdb.Sheet ) {
-        var sheetData = Reflect.copy(@:privateAccess sheet.sheet);
-        sheetData.linesData = null;
-        sheetData.lines = [for( i in 0...sheet.columns.length ) obj];
-        return new cdb.Sheet(sheet.base, sheetData);
-    }
+	override function syncSheet(?base:cdb.Database, ?name:String) {
+		super.syncSheet(base, name);
+		currentSheet = makePseudoSheet(currentSheet);
+	}
 
-    override function save() {
-    }
+	function makePseudoSheet( sheet : cdb.Sheet ) {
+		var sheetData = Reflect.copy(@:privateAccess sheet.sheet);
+		sheetData.linesData = null;
+		sheetData.lines = [for( i in 0...sheetData.columns.length ) obj];
+		return new cdb.Sheet(sheet.base, sheetData);
+	}
+
+	override function save() {
+	}
 
 	override function changeObject(line:Line, column:cdb.Data.Column, value:Dynamic) {
-        super.changeObject(line, column, value);
-        onChange(column.name);
-    }
+		super.changeObject(line, column, value);
+		onChange(column.name);
+	}
 
 }