Bladeren bron

Fixing my grave errors with Original Values

Kenric Nugteren 9 maanden geleden
bovenliggende
commit
a88393cffc
2 gewijzigde bestanden met toevoegingen van 15 en 104 verwijderingen
  1. 8 51
      InABox.Core/BaseObject.cs
  2. 7 53
      InABox.Core/Serialization.cs

+ 8 - 51
InABox.Core/BaseObject.cs

@@ -290,27 +290,14 @@ namespace InABox.Core
             {
                 try
                 {
-                    var prop = key.Contains(".") 
-                        ? CoreUtils.GetProperty(GetType(), key) 
-                        : GetType().GetRuntimeProperty(key);
+                    var prop = DatabaseSchema.Property(GetType(), key);
                     if(prop != null)
                     {
-                        if (prop.SetMethod != null)
-                        {
-                            var val = OriginalValues[key];
-                            // Funky 64bit stuff here?
-                            if (prop.PropertyType == typeof(int) && val?.GetType() == typeof(long))
-                                val = Convert.ToInt32(val);
-                            prop.SetValue(this, val);
-                        }
-                    }
-                    else if (UserProperties.ContainsKey(key))
-                    {
-                        UserProperties[key] = value;
+                        prop.Setter()(this, value);
                     }
                     else
                     {
-                        Logger.Send(LogType.Error, "", $"'{key}' is neither a runtime property nor custom property of {GetType().Name}");
+                        Logger.Send(LogType.Error, "", $"'{key}' is not a property of {GetType().Name}");
                     }
                 }
                 catch (Exception e)
@@ -464,12 +451,7 @@ namespace InABox.Core
                 prop.Setter()(Object, value);
                 if(BaseObjectExtensions.HasChanged(oldValue, value))
                 {
-                    var parent = prop.Parent != null ? prop.Parent.Getter()(Object) as BaseObject : Object;
-                    if(parent != null)
-                    {
-                        var localPropName = prop is StandardProperty stdProp ? stdProp.Property.Name : prop.Name;
-                        parent.OriginalValues[localPropName] = oldValue;
-                    }
+                    Object.OriginalValues[prop.Name] = oldValue;
                 }
             }
             Object.SetObserving(bObs);
@@ -532,30 +514,9 @@ namespace InABox.Core
                 {
                     var isLocal = !property.HasParentEntityLink()
                         || (property.Parent?.HasParentEntityLink() != true && property.Name.EndsWith(".ID"));
-                    if (isLocal)
+                    if (isLocal && (all || sender.HasOriginalValue(property.Name)))
                     {
-                        if (all)
-                        {
-                            result[property.Name] = property.Getter()(sender);
-                        }
-                        else
-                        {
-                            if(property is StandardProperty stdProp)
-                            {
-                                var parent = property.Parent != null ? property.Parent.Getter()(sender) as BaseObject : sender;
-                                if(parent?.HasOriginalValue(stdProp.Property.Name) == true)
-                                {
-                                    result[property.Name] = property.Getter()(sender);
-                                }
-                            }
-                            else if(property is CustomProperty customProp)
-                            {
-                                if (sender.HasOriginalValue(customProp.Name))
-                                {
-                                    result[property.Name] = property.Getter()(sender);
-                                }
-                            }
-                        }
+                        result[property.Name] = property.Getter()(sender);
                     }
                 }
             }
@@ -573,13 +534,9 @@ namespace InABox.Core
                 {
                     var isLocal = !property.HasParentEntityLink()
                         || (property.Parent?.HasParentEntityLink() != true && property.Name.EndsWith(".ID"));
-                    if (isLocal)
+                    if (isLocal && sender.OriginalValues.TryGetValue(property.Name, out var value))
                     {
-                        var parent = property.Parent != null ? property.Parent.Getter()(sender) as BaseObject : sender;
-                        var localPropName = property is StandardProperty stdProp ? stdProp.Property.Name : property.Name;
-                        if (parent != null)
-                            if (parent.OriginalValues.TryGetValue(localPropName, out var value))
-                                result[property.Name] = value;
+                        result[property.Name] = value;
                     }
                 }
             }

+ 7 - 53
InABox.Core/Serialization.cs

@@ -660,53 +660,17 @@ namespace InABox.Core
             DatabaseSchema.Properties(type)
                 .Where(x => !(x is StandardProperty st) || IsSerializable(type,st));
 
-        private static void GetOriginalValues(BaseObject obj, string? parent, List<Tuple<Type, string, object?>> values)
+        private static void WriteOriginalValues<TObject>(this CoreBinaryWriter writer, TObject obj)
+            where TObject : BaseObject
         {
-            parent = parent != null ? $"{parent}." : "";
-
+            var originalValues = new List<Tuple<Type, string, object?>>();
             foreach (var (key, value) in obj.OriginalValues)
             {
-                // EnclosedEntities and EntityLinks will be updated through the recursive code below,
-                // so we should not need to serialise the entire object again..
-                if (DatabaseSchema.Property(obj.GetType(), key) is IProperty prop
-                    && !prop.PropertyType.GetInterfaces().Contains(typeof(IEnclosedEntity))
-                    && !prop.PropertyType.GetInterfaces().Contains(typeof(IEntityLink))
-                    )
-                {
-                    values.Add(new Tuple<Type, string, object?>(prop.PropertyType, parent + key, value));
-                }
-            }
-            var props = obj.GetType().GetProperties().Where(x =>
-                x.GetCustomAttribute<DoNotSerialize>() == null
-                && x.GetCustomAttribute<DoNotPersist>() == null
-                && x.GetCustomAttribute<AggregateAttribute>() == null
-                && x.GetCustomAttribute<FormulaAttribute>() == null
-                && x.GetCustomAttribute<ConditionAttribute>() == null
-                && x.GetCustomAttribute<ComplexFormulaAttribute>() == null
-                && x.GetCustomAttribute<ChildEntityAttribute>() == null
-                && x.CanWrite);
-            foreach (var prop in props)
-            {
-                if (prop.PropertyType.GetInterfaces().Contains(typeof(IEnclosedEntity)))
-                {
-                    if (prop.GetValue(obj) is BaseObject child)
-                        GetOriginalValues(child, parent + prop.Name, values);
-                }
-                else if (prop.PropertyType.GetInterfaces().Contains(typeof(IEntityLink)))
+                if (DatabaseSchema.Property(obj.GetType(), key) is IProperty prop)
                 {
-                    if (prop.GetValue(obj) is BaseObject child && child.HasOriginalValue("ID"))
-                    {
-                        values.Add(new Tuple<Type, string, object?>(typeof(Guid), parent + prop.Name + ".ID", child.OriginalValues["ID"]));
-                    }
+                    originalValues.Add(new Tuple<Type, string, object?>(prop.PropertyType, key, value));
                 }
             }
-        }
-
-        private static void WriteOriginalValues<TObject>(this CoreBinaryWriter writer, TObject obj)
-            where TObject : BaseObject
-        {
-            var originalValues = new List<Tuple<Type, string, object?>>();
-            GetOriginalValues(obj, null, originalValues);
 
             writer.Write(originalValues.Count);
             foreach (var (type, key, value) in originalValues)
@@ -718,7 +682,7 @@ namespace InABox.Core
                 }
                 catch (Exception e)
                 {
-                    
+                    CoreUtils.LogException("", e, "Error serialising OriginalValues");
                 }
             }
         }
@@ -732,17 +696,7 @@ namespace InABox.Core
                 if (DatabaseSchema.Property(obj.GetType(), key) is IProperty prop)
                 {
                     var value = reader.ReadBinaryValue(prop.PropertyType);
-                    if (prop.Parent is null)
-                    {
-                        obj.OriginalValues[prop.Name] = value;
-                    }
-                    else
-                    {
-                        if (prop.Parent.Getter()(obj) is BaseObject parent)
-                        {
-                            parent.OriginalValues[prop.Name.Split('.').Last()] = value;
-                        }
-                    }
+                    obj.OriginalValues[prop.Name] = value;
                 }
             }
         }