فهرست منبع

Got rid of Packable Dictionary

Kenric Nugteren 2 سال پیش
والد
کامیت
62e23b574e
2فایلهای تغییر یافته به همراه4 افزوده شده و 203 حذف شده
  1. 0 162
      InABox.Core/PackableDictionary.cs
  2. 4 41
      InABox.Core/UserProperties.cs

+ 0 - 162
InABox.Core/PackableDictionary.cs

@@ -1,162 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.IO;
-
-namespace InABox.Core
-{
-    public abstract class PackableDictionary<T> : Dictionary<string, T>, IPackable
-    {
-        public void Pack(BinaryWriter writer)
-        {
-            writer.Write(Count);
-            foreach (var key in Keys)
-            {
-                writer.Write(key);
-                PackItem(writer, this[key]);
-            }
-        }
-
-        public void Unpack(BinaryReader reader)
-        {
-            Clear();
-            var iCount = reader.ReadInt32();
-            for (var i = 0; i < iCount; i++)
-            {
-                var key = reader.ReadString();
-                this[key] = UnpackItem(reader);
-            }
-        }
-
-        public override bool Equals(object obj)
-        {
-            var other = obj as PackableDictionary<T>;
-            if (other == null)
-                return false;
-
-            if (Count != other.Count) // Require equal count.
-                return false;
-
-            foreach (var pair in this)
-                if (other.TryGetValue(pair.Key, out var value))
-                {
-                    if (value == null)
-                    {
-                        if (pair.Value != null)
-                            return false;
-                    }
-                    else if (!value.Equals(pair.Value))
-                    {
-                        return false;
-                    }
-                }
-                else
-                {
-                    return false;
-                }
-
-            return true;
-        }
-
-        public abstract void PackItem(BinaryWriter writer, T value);
-
-        public abstract T UnpackItem(BinaryReader reader);
-    }
-
-    public class PackableStringDictionary : PackableDictionary<string>
-    {
-        public override void PackItem(BinaryWriter writer, string value)
-        {
-            writer.Write(value);
-        }
-
-        public override string UnpackItem(BinaryReader reader)
-        {
-            return reader.ReadString();
-        }
-    }
-
-    public class PackableBooleanDictionary : PackableDictionary<bool>
-    {
-        public override void PackItem(BinaryWriter writer, bool value)
-        {
-            writer.Write(value);
-        }
-
-        public override bool UnpackItem(BinaryReader reader)
-        {
-            return reader.ReadBoolean();
-        }
-    }
-
-    public class PackableObjectDictionary : PackableDictionary<object>
-    {
-        private static readonly Dictionary<Type, short> Types = new Dictionary<Type, short>
-        {
-            { typeof(object), 0x0000 },
-            { typeof(string), 0x0001 },
-            { typeof(string[]), 0x002 },
-            { typeof(sbyte), 0x1000 },
-            { typeof(byte), 0x0011 },
-            { typeof(ushort), 0x0012 },
-            { typeof(uint), 0x0013 },
-            { typeof(ulong), 0x0014 },
-            { typeof(short), 0x0015 },
-            { typeof(int), 0x0016 },
-            { typeof(long), 0x0017 },
-            { typeof(float), 0x0100 },
-            { typeof(double), 0x0101 },
-            { typeof(decimal), 0x0102 },
-            { typeof(TimeSpan), 0x1000 },
-            { typeof(DateTime), 0x1001 },
-            { typeof(Guid), 0x1002 }
-        };
-
-        private static readonly Dictionary<short, Type> Indexes = new Dictionary<short, Type>();
-
-        static PackableObjectDictionary()
-        {
-            foreach (var key in Types.Keys)
-                Indexes[Types[key]] = key;
-        }
-
-        private static short GetIndex(Type type)
-        {
-            if (Types.ContainsKey(type))
-                return Types[type];
-            return 0x0000;
-        }
-
-        public static Type GetType(short index)
-        {
-            if (Indexes.ContainsKey(index))
-                return Indexes[index];
-            return typeof(object);
-        }
-
-
-        public override void PackItem(BinaryWriter writer, object value)
-        {
-            if (value == null)
-            {
-                writer.Write(default(short));
-                writer.Write("");
-            }
-            else
-            {
-                var type = GetIndex(value.GetType());
-                writer.Write(type);
-                writer.Write(CoreUtils.TypedValueToString(value));
-            }
-        }
-
-        public override object UnpackItem(BinaryReader reader)
-        {
-            var index = reader.ReadInt16();
-            var type = GetType(index);
-            var value = reader.ReadString();
-            if (type == typeof(object) && string.IsNullOrEmpty(value))
-                return null;
-            return CoreUtils.StringToTypedValue(value, type);
-        }
-    }
-}

+ 4 - 41
InABox.Core/UserProperties.cs

@@ -7,54 +7,27 @@ using Newtonsoft.Json;
 
 namespace InABox.Core
 {
-    public class UserProperty : IPackable
+    public class UserProperty
     {
         [DoNotSerialize]
         public Type Type { get; set; }
 
         public object? Value { get; set; }
-
-        public void Pack(BinaryWriter writer)
-        {
-            writer.Write(Type.EntityName());
-            writer.Write(Value?.ToString() ?? "");
-        }
-
-        public void Unpack(BinaryReader reader)
-        {
-            Type = CoreUtils.GetEntity(reader.ReadString());
-            Value = CoreUtils.ChangeType(reader.ReadString(), Type);
-        }
-    }
-
-    public class UserPropertyDictionary : PackableDictionary<UserProperty>
-    {
-        public override void PackItem(BinaryWriter writer, UserProperty value)
-        {
-            value.Pack(writer);
-        }
-
-        public override UserProperty UnpackItem(BinaryReader reader)
-        {
-            var result = new UserProperty();
-            result.Unpack(reader);
-            return result;
-        }
     }
 
 
     [Serializable]
-    public class UserProperties : IPackable
+    public class UserProperties
     {
         public delegate void PropertyChangedEventHandler(object sender, string name, object? before, object? after);
 
         public UserProperties()
         {
-            Dictionary = new UserPropertyDictionary();
+            Dictionary = new Dictionary<string, UserProperty>();
             //_dictionary = new Dictionary<string, UserProperty>();
         }
 
-        public UserPropertyDictionary Dictionary { get; set; }
+        public Dictionary<string, UserProperty> Dictionary { get; set; }
 
         //public Type ParentType { get; set; }
 
@@ -85,16 +58,6 @@ namespace InABox.Core
         [JsonIgnore]
         public int Count => Dictionary.Count;
 
-        public void Pack(BinaryWriter writer)
-        {
-            Dictionary.Pack(writer);
-        }
-
-        public void Unpack(BinaryReader reader)
-        {
-            Dictionary.Unpack(reader);
-        }
-
         public override bool Equals(object obj)
         {
             if (!(obj is UserProperties other))