Przeglądaj źródła

tools: handle prefabs correctly

Daniele Bartolini 5 lat temu
rodzic
commit
696bd0a4ac
1 zmienionych plików z 155 dodań i 100 usunięć
  1. 155 100
      tools/level_editor/unit.vala

+ 155 - 100
tools/level_editor/unit.vala

@@ -20,39 +20,53 @@ public class Unit
 		_prefabs = prefabs != null ? prefabs : new Database();
 	}
 
-	public Value? get_component_property(Guid component_id, string key)
+	public Value? prefab_get_component_property(Guid component_id, string key, string prefab)
 	{
+		Value? val;
+
 		// Search in components
+		val = _prefabs.get_property(GUID_ZERO, prefab + ".components");
+		if (val != null)
 		{
-			Value? components = _db.get_property(_unit, "components");
-			if (components != null && ((HashSet<Guid?>)components).contains(component_id))
-				return _db.get_property(component_id, key);
+			if (((HashSet<Guid?>)val).contains(component_id))
+				return _prefabs.get_property(component_id, key);
 		}
 
-		// Search in modified components
-		{
-			Value? value = _db.get_property(_unit, "modified_components.#" + component_id.to_string() + "." + key);
-			if (value != null)
-				return value;
-		}
+		// Search in modified_components
+		val = _prefabs.get_property(GUID_ZERO, prefab + ".modified_components.#" + component_id.to_string() + "." + key);
+		if (val != null)
+			return val;
+
+		// Search in prefab
+		val = _prefabs.get_property(GUID_ZERO, prefab + ".prefab");
+		if (val != null)
+			return prefab_get_component_property(component_id, key, (string)val);
+
+		return null;
+	}
 
-		// Search in prefab's components
+	public Value? get_component_property(Guid component_id, string key)
+	{
+		Value? val;
+
+		// Search in components
+		val = _db.get_property(_unit, "components");
+		if (val != null)
 		{
-			Value? value = _db.get_property(_unit, "prefab");
-			if (value != null)
-			{
-				string prefab = (string)value;
-				Value? pcvalue = _prefabs.get_property(GUID_ZERO, prefab + ".components");
-				if (pcvalue != null)
-				{
-					HashSet<Guid?> prefab_components = (HashSet<Guid?>)pcvalue;
-					if (prefab_components.contains(component_id))
-						return _prefabs.get_property(component_id, key);
-				}
-			}
+			if (((HashSet<Guid?>)val).contains(component_id))
+				return _db.get_property(component_id, key);
 		}
 
-		assert(false);
+		// Search in modified_components
+		val = _db.get_property(_unit, "modified_components.#" + component_id.to_string() + "." + key);
+		if (val != null)
+			return val;
+
+		// Search in prefab
+		val = _db.get_property(_unit, "prefab");
+		if (val != null)
+			return prefab_get_component_property(component_id, key, (string)val);
+
 		return null;
 	}
 
@@ -89,13 +103,11 @@ public class Unit
 	public void set_component_property_bool(Guid component_id, string key, bool val)
 	{
 		// Search in components
+		Value? components = _db.get_property(_unit, "components");
+		if (components != null && ((HashSet<Guid?>)components).contains(component_id))
 		{
-			Value? components = _db.get_property(_unit, "components");
-			if (components != null && ((HashSet<Guid?>)components).contains(component_id))
-			{
-				_db.set_property_bool(component_id, key, val);
-				return;
-			}
+			_db.set_property_bool(component_id, key, val);
+			return;
 		}
 
 		_db.set_property_bool(_unit, "modified_components.#" + component_id.to_string() + "." + key, val);
@@ -104,13 +116,11 @@ public class Unit
 	public void set_component_property_double(Guid component_id, string key, double val)
 	{
 		// Search in components
+		Value? components = _db.get_property(_unit, "components");
+		if (components != null && ((HashSet<Guid?>)components).contains(component_id))
 		{
-			Value? components = _db.get_property(_unit, "components");
-			if (components != null && ((HashSet<Guid?>)components).contains(component_id))
-			{
-				_db.set_property_double(component_id, key, val);
-				return;
-			}
+			_db.set_property_double(component_id, key, val);
+			return;
 		}
 
 		_db.set_property_double(_unit, "modified_components.#" + component_id.to_string() + "." + key, val);
@@ -119,13 +129,11 @@ public class Unit
 	public void set_component_property_string(Guid component_id, string key, string val)
 	{
 		// Search in components
+		Value? components = _db.get_property(_unit, "components");
+		if (components != null && ((HashSet<Guid?>)components).contains(component_id))
 		{
-			Value? components = _db.get_property(_unit, "components");
-			if (components != null && ((HashSet<Guid?>)components).contains(component_id))
-			{
-				_db.set_property_string(component_id, key, val);
-				return;
-			}
+			_db.set_property_string(component_id, key, val);
+			return;
 		}
 
 		_db.set_property_string(_unit, "modified_components.#" + component_id.to_string() + "." + key, val);
@@ -134,13 +142,11 @@ public class Unit
 	public void set_component_property_guid(Guid component_id, string key, Guid val)
 	{
 		// Search in components
+		Value? components = _db.get_property(_unit, "components");
+		if (components != null && ((HashSet<Guid?>)components).contains(component_id))
 		{
-			Value? components = _db.get_property(_unit, "components");
-			if (components != null && ((HashSet<Guid?>)components).contains(component_id))
-			{
-				_db.set_property_guid(component_id, key, val);
-				return;
-			}
+			_db.set_property_guid(component_id, key, val);
+			return;
 		}
 
 		_db.set_property_guid(_unit, "modified_components.#" + component_id.to_string() + "." + key, val);
@@ -149,13 +155,11 @@ public class Unit
 	public void set_component_property_vector3(Guid component_id, string key, Vector3 val)
 	{
 		// Search in components
+		Value? components = _db.get_property(_unit, "components");
+		if (components != null && ((HashSet<Guid?>)components).contains(component_id))
 		{
-			Value? components = _db.get_property(_unit, "components");
-			if (components != null && ((HashSet<Guid?>)components).contains(component_id))
-			{
-				_db.set_property_vector3(component_id, key, val);
-				return;
-			}
+			_db.set_property_vector3(component_id, key, val);
+			return;
 		}
 
 		_db.set_property_vector3(_unit, "modified_components.#" + component_id.to_string() + "." + key, val);
@@ -164,83 +168,134 @@ public class Unit
 	public void set_component_property_quaternion(Guid component_id, string key, Quaternion val)
 	{
 		// Search in components
+		Value? components = _db.get_property(_unit, "components");
+		if (components != null && ((HashSet<Guid?>)components).contains(component_id))
 		{
-			Value? components = _db.get_property(_unit, "components");
-			if (components != null && ((HashSet<Guid?>)components).contains(component_id))
-			{
-				_db.set_property_quaternion(component_id, key, val);
-				return;
-			}
+			_db.set_property_quaternion(component_id, key, val);
+			return;
 		}
 
 		_db.set_property_quaternion(_unit, "modified_components.#" + component_id.to_string() + "." + key, val);
 	}
 
-	public static bool has_component_static(out Guid component_id, string component_type, Database db, Database prefabs_db, Guid unit_id)
+	public static bool prefab_has_component_static(out Guid component_id, string component_type, Database prefabs_db, string prefab)
 	{
+		Value? val;
+
 		// Search in components
+		val = prefabs_db.get_property(GUID_ZERO, prefab + ".components");
+		if (val != null)
 		{
-			Value? value = db.get_property(unit_id, "components");
-			if (value != null)
+			foreach (Guid id in (HashSet<Guid?>)val)
 			{
-				HashSet<Guid?> components = (HashSet<Guid?>)value;
-				foreach (Guid id in components)
+				if((string)prefabs_db.get_property(id, "type") == component_type)
 				{
-					if((string)db.get_property(id, "type") == component_type)
-					{
-						component_id = id;
-						return true;
-					}
+					component_id = id;
+					return true;
 				}
 			}
 		}
 
+		// Search in modified_components
+		string[] keys = prefabs_db.get_keys(GUID_ZERO);
+		foreach (string key in keys)
 		{
-			string[] keys = db.get_keys(unit_id);
-			foreach (string m in keys)
+			if (!key.has_prefix(prefab + ".modified_components.#"))
+				continue;
+
+			// 0                   21                                   58  62
+			// |                    |                                    |   |
+			// modified_components.#f56420ad-7f9c-4cca-aca5-350f366e0dc0.type
+			int aa = 21 + prefab.length+1;
+			int bb = 57 + prefab.length+1;
+			int cc = 58 + prefab.length+1;
+			int dd = 62 + prefab.length+1;
+			string id = key[aa:bb];
+			string suffix = key[cc:dd];
+
+			if (!suffix.has_prefix("type"))
+				continue;
+
+			Value? type = prefabs_db.get_property(GUID_ZERO, key);
+			if (type != null && (string)type == component_type)
 			{
-				if (!m.has_prefix("modified_components.#"))
-					continue;
+				component_id = Guid.parse(id);
+				return true;
+			}
+		}
+
+		// Search in prefab
+		val = prefabs_db.get_property(GUID_ZERO, prefab + ".prefab");
+		if (val != null)
+		{
+			return prefab_has_component_static(out component_id
+				, component_type
+				, prefabs_db
+				, (string)val
+				);
+		}
 
-				// 0                   21                                   58  62
-				// |                    |                                    |   |
-				// modified_components.#f56420ad-7f9c-4cca-aca5-350f366e0dc0.type
-				string id = m[21:57];
-				string type_or_name = m[58:62];
+		component_id = GUID_ZERO;
+		return false;
+	}
 
-				if (!type_or_name.has_prefix("type"))
-					continue;
+	public static bool has_component_static(out Guid component_id, string component_type, Database db, Database prefabs_db, Guid unit_id)
+	{
+		Value? val;
 
-				Value? type = db.get_property(unit_id, m);
-				if (type != null && (string)type == component_type)
+		// Search in components
+		val = db.get_property(unit_id, "components");
+		if (val != null)
+		{
+			foreach (Guid id in (HashSet<Guid?>)val)
+			{
+				if((string)db.get_property(id, "type") == component_type)
 				{
-					component_id = Guid.parse(id);
+					component_id = id;
 					return true;
 				}
 			}
 		}
 
+		// Search in modified_components
+		string[] keys = db.get_keys(unit_id);
+		foreach (string key in keys)
 		{
-			Value? value = db.get_property(unit_id, "prefab");
-			if (value != null)
+			if (!key.has_prefix("modified_components.#"))
+				continue;
+
+			// 0                   21                                   58  62
+			// |                    |                                    |   |
+			// modified_components.#f56420ad-7f9c-4cca-aca5-350f366e0dc0.type
+			int aa = 21;
+			int bb = 57;
+			int cc = 58;
+			int dd = 62;
+			string id = key[aa:bb];
+			string suffix = key[cc:dd];
+
+			if (!suffix.has_prefix("type"))
+				continue;
+
+			Value? type = db.get_property(unit_id, key);
+			if (type != null && (string)type == component_type)
 			{
-				string prefab = (string)value;
-				Value? pcvalue = prefabs_db.get_property(GUID_ZERO, prefab + ".components");
-				if (pcvalue != null)
-				{
-					HashSet<Guid?> prefab_components = (HashSet<Guid?>)pcvalue;
-					foreach (Guid id in prefab_components)
-					{
-						if((string)prefabs_db.get_property(id, "type") == component_type)
-						{
-							component_id = id;
-							return true;
-						}
-					}
-				}
+				component_id = Guid.parse(id);
+				return true;
 			}
 		}
 
+		// Search in prefab
+		val = db.get_property(unit_id, "prefab");
+		if (val != null)
+		{
+			return prefab_has_component_static(out component_id
+				, component_type
+				, prefabs_db
+				, (string)val
+				);
+		}
+
 		component_id = GUID_ZERO;
 		return false;
 	}