/// <summary> /// Upgrade the properties of an entity saved with an older version of the game. Properties that should be upgraded are defined using "Upgrade" elements in the config file. /// for example, <Upgrade gameversion="0.9.2.0" scale="0.5"/> would force the scale of the entity to 0.5 if it was saved with a version prior to 0.9.2.0. /// </summary> /// <param name="entity">The entity to upgrade</param> /// <param name="configElement">The XML element to get the upgrade instructions from (e.g. the config of an item prefab)</param> /// <param name="savedVersion">The game version the entity was saved with</param> public static void UpgradeGameVersion(ISerializableEntity entity, XElement configElement, Version savedVersion) { foreach (XElement subElement in configElement.Elements()) { if (!subElement.Name.ToString().Equals("upgrade", StringComparison.OrdinalIgnoreCase)) { continue; } var upgradeVersion = new Version(subElement.GetAttributeString("gameversion", "0.0.0.0")); if (savedVersion >= upgradeVersion) { continue; } foreach (XAttribute attribute in subElement.Attributes()) { string attributeName = attribute.Name.ToString().ToLowerInvariant(); if (attributeName == "gameversion") { continue; } if (entity.SerializableProperties.TryGetValue(attributeName, out SerializableProperty property)) { property.TrySetValue(entity, attribute.Value); } else if (entity is Item item1) { foreach (ISerializableEntity component in item1.AllPropertyObjects) { if (component.SerializableProperties.TryGetValue(attributeName, out SerializableProperty componentProperty)) { componentProperty.TrySetValue(component, attribute.Value); } } } } if (entity is Item item2) { XElement componentElement = subElement.FirstElement(); if (componentElement == null) { continue; } ItemComponent itemComponent = item2.Components.First(c => c.Name == componentElement.Name.ToString()); if (itemComponent == null) { continue; } foreach (XElement element in componentElement.Elements()) { switch (element.Name.ToString().ToLowerInvariant()) { case "requireditem": case "requireditems": itemComponent.requiredItems.Clear(); itemComponent.DisabledRequiredItems.Clear(); itemComponent.SetRequiredItems(element); break; } } } } }