public void addItem(DBItem item) { _masterList[item.ClassName] = item; if (item.Abstract) _nonSpawnableItems[item.ClassName] = item; else _spawnableItems[item.ClassName] = item; }
public DBItem copyToNewName(string newName) { DBItem newDBItem = new DBItem(newName); foreach(KeyValuePair<string, object> keyVal in _properties) { if (keyVal.Key != "classname") newDBItem.setProperty(keyVal.Key, keyVal.Value); } return newDBItem; }
private void handleItem(string itemName, Database db, HashSet <string> closedNames) { if (closedNames.Contains(itemName)) { return; } closedNames.Add(itemName); if (!db.itemExistsInMasterList(itemName)) { return; } // Want to receive properties from all of our parent types DBItem dbItem = db.getItemFromMasterList(itemName); List <string> parentNames = dbItem.getProperty("types") as List <string>; if (parentNames == null) { return; } foreach (string parentName in parentNames) { if (!db.itemExistsInMasterList(parentName)) { continue; } // Have to handle our parents first so they're up to date handleItem(parentName, db, closedNames); // Now grab the item and merge any properties we don't have DBItem parentItem = db.getItemFromMasterList(parentName); foreach (string propertyName in parentItem.getPropertyNames()) { // Special case for the mutables property if (propertyName == "mutables") { if (dbItem.propertyExists("mutables")) { HashSet <string> currentMutables = new HashSet <string>((List <string>)dbItem.getProperty("mutables")); HashSet <string> parentMutables = new HashSet <string>((List <string>)parentItem.getProperty("mutables")); currentMutables.UnionWith(parentMutables); dbItem.setProperty("mutables", new List <string>(currentMutables)); } else { dbItem.setProperty("mutables", new List <string>((List <string>)parentItem.getProperty("mutables"))); } } else if (!dbItem.propertyExists(propertyName)) { dbItem.setProperty(propertyName, parentItem.getProperty(propertyName)); } } } }
public void addItem(DBItem item) { _masterList[item.ClassName] = item; if (item.Abstract) { _nonSpawnableItems[item.ClassName] = item; } else { _spawnableItems[item.ClassName] = item; } }
public DBItem copyToNewName(string newName) { DBItem newDBItem = new DBItem(newName); foreach (KeyValuePair <string, object> keyVal in _properties) { if (keyVal.Key != "classname") { newDBItem.setProperty(keyVal.Key, keyVal.Value); } } return(newDBItem); }
public List <string> filterListByProperty(List <string> itemNames, string propertyName) { List <string> filteredList = new List <string>(); foreach (string itemName in itemNames) { DBItem item = getItem(itemName); if (item != null && item.propertyExists(propertyName) && !item.Spawned) { filteredList.Add(itemName); } } return(filteredList); }
protected static void parseItem(XmlElement element) { string name = element.GetAttribute("name"); if (Database.Instance.itemExists(name)) { Debug.Log(string.Format("WARNING: Duplicated item in database: {0}", name)); return; } DBItem dbitem = new DBItem(name.Trim()); // Parse each of the item's properties foreach (XmlElement property in element.ChildNodes) { string propertyName = property.GetAttribute("name"); object propertyVal = parseProperty(property); dbitem.setProperty(propertyName, propertyVal); } Database.Instance.addItem(dbitem); }
protected bool satisfiesConstraints(DBItem dbitem) { foreach (string propertyName in _propertiesToFilter.Keys) { if (!dbitem.propertyExists(propertyName)) return false; else if (_propertiesToFilter[propertyName] != null) { if (dbitem.getProperty(propertyName) is List<string>) { if (!(dbitem.getProperty(propertyName) as List<string>).Contains(_propertiesToFilter[propertyName] as string)) return false; } else if (!_propertiesToFilter[propertyName].Equals(dbitem.getProperty(propertyName))) { Debug.Log(string.Format("Item {0} failed filter for property {1} expected: {2} actual: {3}", dbitem.ClassName, propertyName, _propertiesToFilter[propertyName], dbitem.getProperty(propertyName))); return false; } } } return true; }
public DBItem copyItem(string itemToCopyName, string newItemName) { if (itemToCopyName == newItemName) { throw new UnityException("Cannot create a copy of a database item with the same name"); } if (!_masterList.ContainsKey(itemToCopyName)) { throw new UnityException("Cannot create copy of database item " + itemToCopyName + ". Item does not exist in database."); } if (_masterList.ContainsKey(newItemName)) { throw new UnityException("cannot create new copy " + newItemName + ". An item already exists with that name."); } DBItem newDBItem = getItem(itemToCopyName).copyToNewName(newItemName); addItem(newDBItem); return(newDBItem); }
public static void createDatabaseFromXml(List <string> xmlFiles, bool verbose) { Database.Instance.clearDatabase(); Database.Instance.removeExtensions(); Database.Instance.addExtension(new ParentExtension()); Database.Instance.addExtension(new ChangesExtension()); Database.Instance.addExtension(new ChangedbyExtension()); foreach (string xmlFilename in xmlFiles) { TextAsset xmlText = Resources.Load(xmlFilename) as TextAsset; XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(xmlText.text); foreach (XmlElement item in xmlDoc.GetElementsByTagName("Items")) { string className = item.GetElementsByTagName("classname")[0].InnerText; if (verbose) { Debug.Log(string.Format("Making Item: {0}", className)); } DBItem dbitem = new DBItem(className.Trim()); XmlNodeList hints = item.GetElementsByTagName("hints"); Dictionary <string, string> hintdict = new Dictionary <string, string>(); foreach (XmlElement hint in hints) { foreach (XmlElement child in hint.ChildNodes) { if (child.InnerText != null && child.InnerText != "") { if (verbose) { Debug.Log(string.Format("Adding {0} hint to {1}: {2}", child.Name, className, child.InnerText)); } hintdict[child.Name] = child.InnerText; } } } if (hintdict.Count > 0) { if (verbose) { Debug.Log("setting hints property to a hints dictionary"); } dbitem.setProperty("hints", hintdict); } XmlNodeList auxtext = item.GetElementsByTagName("text"); Dictionary <string, string> textdict = new Dictionary <string, string>(); foreach (XmlElement txt in auxtext) { foreach (XmlElement child in txt.ChildNodes) { if (child.InnerText != null && child.InnerText != "") { if (verbose) { Debug.Log(string.Format("Adding {0} text to {1}: {2}", child.Name, className, child.InnerText)); } textdict[child.Name] = child.InnerText; } } } if (textdict.Count > 0) { if (verbose) { Debug.Log("setting text property to a text dictionary"); } dbitem.setProperty("text", textdict); } foreach (XmlElement child in item.ChildNodes) { if (child.Name == "hints" || child.Name == "text" || child.Name == "classname" || child.InnerText == null || child.InnerText == "") { continue; } // Check whether this property is a dictionary bool dic = false; foreach (XmlNode grandChild in child.ChildNodes) { if (grandChild.NodeType != XmlNodeType.Element) { continue; } if (grandChild.Name == "dictionary") { dic = true; Dictionary <string, object> dict1 = parseDictionary((XmlElement)grandChild); if (dict1.Count > 0) { if (verbose) { Debug.Log(string.Format("Setting property {0} to a parsed dictionary", child.Name)); } dbitem.setProperty(child.Name, dict1); } } } if (dic) { continue; } // Handle nearly every other type of property string potentialVal = child.InnerText.Trim(); object val; int maybeInt; if (potentialVal == "true" || potentialVal == "True") { val = true; } else if (potentialVal == "false" || potentialVal == "False") { val = false; } else if (potentialVal == "None") { val = null; } else if (int.TryParse(potentialVal, out maybeInt)) { val = maybeInt; } else if (potentialVal[0] == '[') { // Make sure it isn't a list of tuples first if (potentialVal[1] == '(') { potentialVal = potentialVal.Replace("[", "").Replace("]", "").Trim(); List <string> valueList = new List <string>(potentialVal.Split(new char[] { ',' })); val = parseAsTuples(valueList); } // Parse any string starting with an open bracket as a list else { val = parseAsList(potentialVal); } } else if (potentialVal[0] == '(') { List <string> valueList = new List <string>(potentialVal.Split(new char[] { ',' })); val = parseAsTuples(valueList); } else if (potentialVal == "") { continue; } else { val = potentialVal; } if (verbose) { Debug.Log(string.Format("Adding property: ({0}, {1})", child.Name, val)); } dbitem.setProperty(child.Name.Trim(), val); } if (Database.Instance.itemExists(dbitem.ClassName)) { Debug.Log(string.Format("WARNING: Duplicated item in database: {0}", dbitem.ClassName)); } Database.Instance.addItem(dbitem); } } Database.Instance.runExtensions(); }
public static void createDatabaseFromXml(List<string> xmlFiles, bool verbose) { Database.Instance.clearDatabase(); Database.Instance.removeExtensions(); Database.Instance.addExtension(new ParentExtension()); Database.Instance.addExtension(new ChangesExtension()); Database.Instance.addExtension(new ChangedbyExtension()); foreach (string xmlFilename in xmlFiles) { TextAsset xmlText = Resources.Load(xmlFilename) as TextAsset; XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(xmlText.text); foreach (XmlElement item in xmlDoc.GetElementsByTagName("Items")) { string className = item.GetElementsByTagName("classname")[0].InnerText; if (verbose) Debug.Log(string.Format("Making Item: {0}", className)); DBItem dbitem = new DBItem(className.Trim()); XmlNodeList hints = item.GetElementsByTagName("hints"); Dictionary<string, string> hintdict = new Dictionary<string, string>(); foreach (XmlElement hint in hints) { foreach (XmlElement child in hint.ChildNodes) { if (child.InnerText != null && child.InnerText != "") { if (verbose) Debug.Log(string.Format("Adding {0} hint to {1}: {2}", child.Name, className, child.InnerText)); hintdict[child.Name] = child.InnerText; } } } if (hintdict.Count > 0) { if (verbose) Debug.Log("setting hints property to a hints dictionary"); dbitem.setProperty("hints", hintdict); } XmlNodeList auxtext = item.GetElementsByTagName("text"); Dictionary<string, string> textdict = new Dictionary<string, string>(); foreach (XmlElement txt in auxtext) { foreach (XmlElement child in txt.ChildNodes) { if (child.InnerText != null && child.InnerText != "") { if (verbose) Debug.Log(string.Format("Adding {0} text to {1}: {2}", child.Name, className, child.InnerText)); textdict[child.Name] = child.InnerText; } } } if (textdict.Count > 0) { if (verbose) Debug.Log("setting text property to a text dictionary"); dbitem.setProperty("text", textdict); } foreach (XmlElement child in item.ChildNodes) { if (child.Name == "hints" || child.Name == "text" || child.Name == "classname" || child.InnerText == null || child.InnerText == "") continue; // Check whether this property is a dictionary bool dic = false; foreach (XmlNode grandChild in child.ChildNodes) { if (grandChild.NodeType != XmlNodeType.Element) continue; if (grandChild.Name == "dictionary") { dic = true; Dictionary<string, object> dict1 = parseDictionary((XmlElement)grandChild); if (dict1.Count > 0) { if (verbose) Debug.Log(string.Format("Setting property {0} to a parsed dictionary", child.Name)); dbitem.setProperty(child.Name, dict1); } } } if (dic) continue; // Handle nearly every other type of property string potentialVal = child.InnerText.Trim(); object val; int maybeInt; if (potentialVal == "true" || potentialVal == "True") val = true; else if (potentialVal == "false" || potentialVal == "False") val = false; else if (potentialVal == "None") val = null; else if (int.TryParse(potentialVal, out maybeInt)) val = maybeInt; else if (potentialVal[0] == '[') { // Make sure it isn't a list of tuples first if (potentialVal[1] == '(') { potentialVal = potentialVal.Replace("[", "").Replace("]", "").Trim(); List<string> valueList = new List<string>(potentialVal.Split(new char[] {','})); val = parseAsTuples(valueList); } // Parse any string starting with an open bracket as a list else val = parseAsList(potentialVal); } else if (potentialVal[0] == '(') { List<string> valueList = new List<string>(potentialVal.Split(new char[] {','})); val = parseAsTuples(valueList); } else if (potentialVal == "") continue; else val = potentialVal; if (verbose) Debug.Log(string.Format("Adding property: ({0}, {1})", child.Name, val)); dbitem.setProperty(child.Name.Trim(), val); } if (Database.Instance.itemExists(dbitem.ClassName)) Debug.Log(string.Format("WARNING: Duplicated item in database: {0}", dbitem.ClassName)); Database.Instance.addItem(dbitem); } } Database.Instance.runExtensions(); }