Prechádzať zdrojové kódy

tools: always use project database unless the resource does not exist

Daniele Bartolini 5 rokov pred
rodič
commit
853a5a6d3e

+ 2 - 23
tools/level_editor/level.vala

@@ -210,7 +210,7 @@ public class Level
 
 	public void on_unit_spawned(Guid id, string name, Vector3 pos, Quaternion rot, Vector3 scl)
 	{
-		load_unit(name);
+		_project.load_unit(name);
 
 		_db.add_restore_point((int)ActionType.SPAWN_UNIT, new Guid[] { id });
 		_db.create(id);
@@ -520,27 +520,6 @@ public class Level
 		_client.send_script(sb.str);
 	}
 
-	/// Loads the unit @a name and all its prefabs recursively into the database.
-	private void load_unit(string name)
-	{
-		// If the unit is already loaded.
-		if (_db.has_property(GUID_ZERO, name))
-			return;
-
-		// Try to load from toolchain directory first.
-		string resource_path = name + ".unit";
-		string path = Path.build_filename(_project.toolchain_dir(), resource_path);
-		if (!File.new_for_path(path).query_exists())
-			path = Path.build_filename(_project.source_dir(), resource_path);
-
-		Guid prefab_id = _db.load_more(path, resource_path);
-
-		// Load all prefabs recursively, if any.
-		Value? prefab = _db.get_property(prefab_id, "prefab");
-		if (prefab != null)
-			load_unit((string)prefab);
-	}
-
 	private void generate_spawn_unit_commands(Guid[] unit_ids, StringBuilder sb)
 	{
 		foreach (Guid unit_id in unit_ids)
@@ -548,7 +527,7 @@ public class Level
 			Unit unit = new Unit(_db, unit_id);
 
 			if (unit.has_prefab())
-				load_unit(_db.get_property_string(unit_id, "prefab"));
+				_project.load_unit(_db.get_property_string(unit_id, "prefab"));
 
 			sb.append(LevelEditorApi.spawn_empty_unit(unit_id));
 

+ 4 - 4
tools/level_editor/level_editor.vala

@@ -340,16 +340,16 @@ public class LevelEditorApplication : Gtk.Application
 
 		_data_compiler = new DataCompiler(_compiler);
 
-		_project = new Project(_data_compiler);
+		_database = new Database();
+		_database.key_changed.connect(() => { update_active_window_title(); });
+
+		_project = new Project(_database, _data_compiler);
 		_project.set_toolchain_dir(_toolchain_dir.get_path());
 		_project.register_importer("Sprite", { "png" }, SpriteResource.import, 0.0);
 		_project.register_importer("Mesh", { "mesh" }, MeshResource.import, 1.0);
 		_project.register_importer("Sound", { "wav" }, SoundResource.import, 2.0);
 		_project.register_importer("Texture", { "png", "tga", "dds", "ktx", "pvr" }, TextureResource.import, 2.0);
 
-		_database = new Database();
-		_database.key_changed.connect(() => { update_active_window_title(); });
-
 		_editor = new ConsoleClient();
 		_editor.connected.connect(on_editor_connected);
 		_editor.disconnected.connect(on_editor_disconnected_unexpected);

+ 26 - 1
tools/level_editor/project.vala

@@ -38,6 +38,7 @@ public class Project
 	public File _level_editor_test_level;
 	public File _level_editor_test_package;
 	public string _platform;
+	public Database _database;
 	public Database _files;
 	public HashMap<string, Guid?> _map;
 	public ImporterData _all_extensions_importer_data;
@@ -51,13 +52,14 @@ public class Project
 	public signal void project_reset();
 	public signal void project_loaded();
 
-	public Project(DataCompiler dc)
+	public Project(Database db, DataCompiler dc)
 	{
 #if CROWN_PLATFORM_LINUX
 		_platform = "linux";
 #elif CROWN_PLATFORM_WINDOWS
 		_platform = "windows";
 #endif // CROWN_PLATFORM_LINUX
+		_database = db;
 		_files = new Database();
 		_map = new HashMap<string, Guid?>();
 		_all_extensions_importer_data = ImporterData();
@@ -90,6 +92,29 @@ public class Project
 		project_loaded();
 	}
 
+	/// Loads the unit @a name and all its prefabs recursively into the database.
+	public void load_unit(string name)
+	{
+		// If the unit is already loaded.
+		if (_database.has_property(GUID_ZERO, name))
+			return;
+
+		// Try to load from toolchain directory first.
+		string resource_path = name + ".unit";
+		string path = Path.build_filename(toolchain_dir(), resource_path);
+		if (!File.new_for_path(path).query_exists())
+			path = Path.build_filename(source_dir(), resource_path);
+		if (!File.new_for_path(path).query_exists())
+			return; // Caller can query the database to check for error.
+
+		Guid prefab_id = _database.load_more(path, resource_path);
+
+		// Load all prefabs recursively, if any.
+		Value? prefab = _database.get_property(prefab_id, "prefab");
+		if (prefab != null)
+			load_unit((string)prefab);
+	}
+
 	public void set_toolchain_dir(string toolchain_dir)
 	{
 		_toolchain_dir = File.new_for_path(toolchain_dir);

+ 13 - 7
tools/resource/mesh_resource.vala

@@ -154,15 +154,21 @@ public class MeshResource
 			}
 
 			// Generate .unit
-			Database db = new Database();
-			Guid unit_id = Guid.new_guid();
+			project.load_unit(resource_name);
 
-			// Do not overwrite existing .unit
-			string unit_name = Path.build_filename(project.source_dir(), resource_name) + ".unit";
-			if (File.new_for_path(unit_name).query_exists())
-				unit_id = db.load(unit_name, resource_path + ".unit");
+			Guid unit_id;
+			Database db = project._database;
+
+			if (db.has_property(GUID_ZERO, resource_name + ".unit"))
+			{
+				unit_id = db.get_property_guid(GUID_ZERO, resource_name + ".unit");
+			}
 			else
+			{
+				db = new Database();
+				unit_id = Guid.new_guid();
 				db.create(unit_id);
+			}
 
 			Hashtable mesh = SJSON.load(filename_i);
 			Hashtable mesh_nodes = (Hashtable)mesh["nodes"];
@@ -201,7 +207,7 @@ public class MeshResource
 				create_components(db, unit_id, new_unit_id, material_name, resource_name, entry.key, (Hashtable)entry.value);
 			}
 
-			db.save(unit_name, unit_id);
+			db.save(Path.build_filename(project.source_dir(), resource_path + ".unit"), unit_id);
 		}
 
 		return 0;

+ 13 - 7
tools/resource/sprite_resource.vala

@@ -155,15 +155,21 @@ public class SpriteResource
 			SJSON.save(sprite, Path.build_filename(project.source_dir(), resource_path) + ".sprite");
 
 			// Generate .unit
-			Database db = new Database();
-			Guid unit_id = Guid.new_guid();
+			project.load_unit(resource_name);
 
-			// Do not overwrite existing .unit
-			string unit_name = Path.build_filename(project.source_dir(), resource_path) + ".unit";
-			if (File.new_for_path(unit_name).query_exists())
-				unit_id = db.load(unit_name, resource_path + ".unit");
+			Guid unit_id;
+			Database db = project._database;
+
+			if (db.has_property(GUID_ZERO, resource_name + ".unit"))
+			{
+				unit_id = db.get_property_guid(GUID_ZERO, resource_name + ".unit");
+			}
 			else
+			{
+				db = new Database();
+				unit_id = Guid.new_guid();
 				db.create(unit_id);
+			}
 
 			Unit unit = new Unit(db, unit_id);
 
@@ -294,7 +300,7 @@ public class SpriteResource
 				}
 			}
 
-			db.save(unit_name, unit_id);
+			db.save(Path.build_filename(project.source_dir(), resource_path + ".unit"), unit_id);
 		}
 
 		return 0;