public static void Modify(object instance, string propertyName, object value, bool logDetails = false) { Log.Debug($"Modify - {propertyName} to {value}"); PropertyModDetails propertyModDetails = BasePropertyChanger.GetPropertyModDetails(instance, propertyName, logDetails); if (propertyModDetails == null || !propertyModDetails.Found) { if (logDetails) { Log.Error($"{propertyName} not found in instance."); } return; } BasePropertyChanger propertyChanger = PropertyChangerFactory.CreateFromModDetails(propertyModDetails, logDetails); if (propertyChanger != null) { propertyChanger.ValueOverride = value; propertyChanger.ModifyPropertyWithDetails(propertyModDetails); } else if (logDetails) { if (propertyModDetails.attachedPropertyType != null) { Log.Error($"propertyChanger ({propertyName} to {value} of type {propertyModDetails.attachedPropertyType.Name}) is null!"); } else { Log.Error($"propertyChanger ({propertyName} to {value}) is null!"); } } }
public void ModifyProperty(object instance, bool logDetails = false) { if (logDetails) { Talespire.Log.Indent(); } try { if (logDetails) { Talespire.Log.Debug($"instance: {instance}"); Talespire.Log.Debug($"FullPropertyPath: {FullPropertyPath}"); Talespire.Log.Debug($"Value: {Value}"); Talespire.Log.Debug($"ValueOverride: {ValueOverride}"); } PropertyModDetails propertyModDetails = GetPropertyModDetails(instance, FullPropertyPath, logDetails); //if (Name.EndsWith("LifeTime")) // Talespire.Log.Debug($"Setting {Name} to {GetValue()} (in {GetType().Name})..."); propertyModDetails.SetValue(this); } catch (Exception ex) { Talespire.Log.Error($"Error while modifying property \"{FullPropertyPath}\"!"); Talespire.Log.Exception(ex); } if (logDetails) { Talespire.Log.Unindent(); } }
public void ModifyPropertyWithDetails(PropertyModDetails propertyModDetails) { try { Talespire.Log.Debug($"ModifyPropertyWithDetails - propertyModDetails.SetValue(this);"); propertyModDetails.SetValue(this); } catch (Exception ex) { Talespire.Log.Error($"ModifyPropertyWithDetails - Error while modifying property {FullPropertyPath}!"); Talespire.Log.Exception(ex); } }
public static BasePropertyChanger CreateFromModDetails(PropertyModDetails propertyModDetails, bool logErrors = true) { Type propertyType = propertyModDetails.GetPropertyType(); if (propertyType == null) { return(null); } BasePropertyChanger propertyChanger = CreateFromTypeName(propertyType.Name, logErrors); if (propertyChanger != null) { propertyChanger.FullPropertyPath = propertyModDetails.GetName(); } return(propertyChanger); }
public static PropertyModDetails GetPropertyModDetails(object instance, string fullPropertyPath, bool logDetails = false) { if (logDetails) { Talespire.Log.Indent(); } try { if (Guard.IsNull(fullPropertyPath, nameof(fullPropertyPath))) { return(null); } PropertyModDetails propertyModDetails = new PropertyModDetails(); propertyModDetails.instance = null; if (logDetails) { Talespire.Log.Debug($"fullPropertyName: \"{fullPropertyPath}\""); } string[] split = fullPropertyPath.Split('.'); object nextInstance = instance; propertyModDetails.property = null; propertyModDetails.field = null; propertyModDetails.attachedPropertyName = string.Empty; foreach (string propertyName in split) { propertyModDetails.field = null; propertyModDetails.property = null; if (logDetails) { Talespire.Log.Debug($"propertyName = \"{propertyName}\"..."); } if (propertyName.StartsWith("[") && propertyName.EndsWith("]")) { string childName = propertyName.Substring(1, propertyName.Length - 2); if (logDetails) { Talespire.Log.Debug($"Getting child [\"{childName}\"]..."); } if (nextInstance is GameObject gameObject) { nextInstance = gameObject.FindChild(childName, true); if (logDetails && nextInstance == null) { Talespire.Log.Error($"Unable to find child control named \"{childName}\"!!!"); } if (nextInstance != null) { continue; } } else if (nextInstance == null) { Talespire.Log.Error($"Unable find child. nextInstance is null."); } else { Talespire.Log.Error($"Unable find child. nextInstance is not a GameObject. It is a {nextInstance.GetType()}."); } } else if (propertyName.StartsWith("<") && propertyName.EndsWith(">")) { string componentTypeName = propertyName.Substring(1, propertyName.Length - 2); if (logDetails) { Talespire.Log.Debug($"Getting <\"{componentTypeName}\">..."); } if (nextInstance is GameObject gameObject) { if (logDetails) { Talespire.Log.Debug($"nextInstance = gameObject.GetComponent(\"{componentTypeName}\")..."); } nextInstance = gameObject.GetComponent(componentTypeName); if (nextInstance == null) { if (logDetails) { Talespire.Log.Debug($"Component[] components = gameObject.GetComponents(typeof(Component))..."); } Component[] components = gameObject.GetComponents(typeof(Component)); if (components != null) { if (logDetails) { Talespire.Log.Debug($"Iterating child components to find \"{componentTypeName}\"..."); } foreach (Component component in components) { if (component == null) { Talespire.Log.Error($"Missing component (most likely a script?)"); continue; } if (logDetails) { Talespire.Log.Debug($"if (component.GetType().Name (\"{component?.GetType().Name}\") == \"{componentTypeName}\")"); } if (component?.GetType().Name == componentTypeName) { if (logDetails) { Talespire.Log.Warning($" Found \"{component.GetType().Name}\" through iteration!"); } nextInstance = component; break; } } } if (nextInstance == null) { if (logDetails) { Talespire.Log.Error($"Component \"{componentTypeName}\" not found in instance. {propertyName} not found."); } break; } } continue; } } if (logDetails) { Talespire.Log.Debug($"property = nextInstance.GetType().GetProperty(\"{propertyName}\");"); } propertyModDetails.property = nextInstance.GetType().GetProperty(propertyName); if (propertyModDetails.property == null) { if (logDetails) { Talespire.Log.Debug($"property not found. Trying field..."); } propertyModDetails.field = nextInstance.GetType().GetField(propertyName); if (propertyModDetails.field == null) { if (logDetails) { Talespire.Log.Debug($"field not found. Trying TrySetProperty..."); } if (IsAttachedProperty(nextInstance, propertyName, out Type propertyType)) { propertyModDetails.instance = nextInstance; propertyModDetails.attachedPropertyName = propertyName; propertyModDetails.attachedPropertyType = propertyType; break; } propertyModDetails.instance = null; if (logDetails) { Talespire.Log.Error($"Property/Field \"{propertyName}\" not found in instance!"); } break; } } propertyModDetails.SetInstance(ref nextInstance); if (logDetails) { if (nextInstance == null) { Talespire.Log.Debug($"nextInstance is null!"); } } } return(propertyModDetails); } finally { if (logDetails) { Talespire.Log.Unindent(); } } }