Browse Source

Merge remote-tracking branch 'origin/kenric' into frank

Frank van den Bos 2 years ago
parent
commit
dd06cca655

+ 5 - 3
InABox.Core/CoreAttribute.cs

@@ -1,4 +1,6 @@
-namespace InABox.Core
+using System.IO;
+
+namespace InABox.Core
 {
     [Caption("Attributes")]
     public class CoreAttribute : BaseObject, ISequenceable, IPackable
@@ -7,14 +9,14 @@
 
         public string Value { get; set; }
 
-        public void Pack(FastBinaryWriter writer)
+        public void Pack(BinaryWriter writer)
         {
             writer.Write(Sequence);
             writer.Write(Name);
             writer.Write(Value);
         }
 
-        public void Unpack(FastBinaryReader reader)
+        public void Unpack(BinaryReader reader)
         {
             Sequence = reader.ReadInt64();
             Name = reader.ReadString();

+ 10 - 0
InABox.Core/DataTable.cs

@@ -830,6 +830,10 @@ namespace InABox.Core
             {
                 writer.Write((value as LoggablePropertyAttribute)?.Format ?? "");
             }
+            else if (typeof(IPackable).IsAssignableFrom(type) && value is IPackable pack)
+            {
+                pack.Pack(writer);
+            }
             else
             {
                 throw new Exception($"Invalid type; Target DataType is {type} and value DataType is {value?.GetType().ToString() ?? "null"}");
@@ -939,6 +943,12 @@ namespace InABox.Core
                     ? null
                     : new LoggablePropertyAttribute() { Format = format };
             }
+            else if (typeof(IPackable).IsAssignableFrom(type))
+            {
+                var packable = (Activator.CreateInstance(type) as IPackable)!;
+                packable.Unpack(reader);
+                return packable;
+            }
             else
             {
                 throw new Exception($"Invalid type; Target DataType is {type}");

+ 1 - 1
InABox.Core/Dimensions/DimensionUnitLink.cs

@@ -8,7 +8,7 @@ namespace InABox.Core
         {
         }
 
-        protected DimensionUnitLink(Func<Entity>? entity) : base(entity)
+        protected DimensionUnitLink(Func<BaseObject>? entity) : base(entity)
         {
         }
 

+ 2 - 2
InABox.Core/Dimensions/Dimensions.cs

@@ -11,7 +11,7 @@ namespace InABox.Core
 
         public Dimensions() : base() { }
         
-        public Dimensions(Func<Entity> entity) : base(entity) { }
+        public Dimensions(Func<BaseObject> entity) : base(entity) { }
 
         [EditorSequence(1)]
         [RequiredColumn]
@@ -65,7 +65,7 @@ namespace InABox.Core
         protected override void Init()
         {
             base.Init();
-            Unit = Activator.CreateInstance(typeof(TLink), new object[] { new Func<Entity>(LinkedEntity) }) as TLink;
+            Unit = Activator.CreateInstance(typeof(TLink), new object[] { new Func<BaseObject>(LinkedEntity) }) as TLink;
             Unit.PropertyChanged += (s, e) =>
             {
                 if(e.PropertyName == "ID")

+ 3 - 3
InABox.Core/EnclosedEntity.cs

@@ -11,17 +11,17 @@ namespace InABox.Core
     public abstract class EnclosedEntity : BaseObject, IEnclosedEntity
     {
         
-        private Func<Entity>? _linkedentity;
+        private Func<BaseObject>? _linkedentity;
         
         [DoNotSerialize]
-        protected Entity? LinkedEntity() => _linkedentity?.Invoke();
+        protected BaseObject? LinkedEntity() => _linkedentity?.Invoke();
 
         //[Obsolete("Please supply linked Entity")]
         public EnclosedEntity()
         {
         }
 
-        public EnclosedEntity(Func<Entity>? entity) 
+        public EnclosedEntity(Func<BaseObject>? entity) 
         {
             _linkedentity = entity;
         }

+ 3 - 3
InABox.Core/EntityLink.cs

@@ -23,17 +23,17 @@ namespace InABox.Core
     public abstract class EntityLink<T> : BaseObject, IEntityLink<T> where T : BaseObject, new()
     {
 
-        private Func<Entity>? _linkedentity;
+        private Func<BaseObject>? _linkedentity;
         
         [DoNotSerialize]
-        protected Entity? LinkedEntity() => _linkedentity?.Invoke();
+        protected BaseObject? LinkedEntity() => _linkedentity?.Invoke();
 
         //[Obsolete("Please supply linked Entity")]
         public EntityLink()
         {
         }
 
-        public EntityLink(Func<Entity>? entity) 
+        public EntityLink(Func<BaseObject>? entity) 
         {
             _linkedentity = entity;
         }

+ 0 - 128
InABox.Core/FastBinaryReader.cs

@@ -1,128 +0,0 @@
-using System;
-using System.Text;
-
-namespace InABox.Core
-{
-    public class FastBinaryReader
-    {
-        private readonly byte[] _data;
-        private int index = -1;
-
-        public FastBinaryReader(byte[] data)
-        {
-            _data = data;
-            index = 0;
-        }
-
-        public bool ReadBoolean()
-        {
-            var result = _data[index] != 0;
-            index++;
-            return result;
-        }
-
-        public byte ReadByte()
-        {
-            var result = _data[index];
-            index++;
-            return result;
-        }
-
-        public short ReadInt16()
-        {
-            var result = BitConverter.ToInt16(_data, index);
-            index += 2;
-            return result;
-        }
-
-        public int ReadInt32()
-        {
-            var result = BitConverter.ToInt32(_data, index);
-            index += 4;
-            return result;
-        }
-
-        public long ReadInt64()
-        {
-            var result = BitConverter.ToInt64(_data, index);
-            index += 8;
-            return result;
-        }
-
-        public double ReadFloat()
-        {
-            var result = BitConverter.ToSingle(_data, index);
-            index += 4;
-            return result;
-        }
-
-        public double ReadDouble()
-        {
-            var result = BitConverter.ToDouble(_data, index);
-            index += 8;
-            return result;
-        }
-
-
-        protected internal int Read7BitEncodedInt()
-        {
-            // Read out an Int32 7 bits at a time.  The high bit
-            // of the byte when on means to continue reading more bytes.
-            var count = 0;
-            var shift = 0;
-            byte b;
-            do
-            {
-                // Check for a corrupted stream.  Read a max of 5 bytes.
-                // In a future version, add a DataFormatException.
-                if (shift == 5 * 7) // 5 bytes max per Int32, shift += 7
-                    throw new FormatException("Format_Bad7BitInt32");
-
-                // ReadByte handles end of stream cases for us.
-                b = ReadByte();
-                count |= (b & 0x7F) << shift;
-                shift += 7;
-            } while ((b & 0x80) != 0);
-
-            return count;
-        }
-
-
-        public string ReadString()
-        {
-            var length = Read7BitEncodedInt();
-            var result = Encoding.UTF8.GetString(_data, index, length);
-            index += length;
-            return result;
-        }
-
-        public DateTime ReadDateTime()
-        {
-            var result = ReadInt64();
-            return DateTime.FromBinary(result);
-        }
-
-        public TimeSpan ReadTimeSpan()
-        {
-            var result = ReadInt64();
-            return new TimeSpan(result);
-        }
-
-        public Guid ReadGuid()
-        {
-            var buf = new byte[16];
-            Buffer.BlockCopy(_data, index, buf, 0, 16);
-            var result = new Guid(buf);
-            index += 16;
-            return result;
-        }
-
-        public byte[] ReadBytes(int length)
-        {
-            var result = new byte[length];
-            Buffer.BlockCopy(_data, index, result, 0, length);
-            index += length;
-            return result;
-        }
-    }
-}

+ 0 - 105
InABox.Core/FastBinaryWriter.cs

@@ -1,105 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-
-namespace InABox.Core
-{
-    public class FastBinaryWriter : IDisposable
-    {
-        private List<byte[]> _data = new List<byte[]>();
-
-        public void Dispose()
-        {
-            _data.Clear();
-        }
-
-        public byte[] GetBuffer()
-        {
-            var result = new byte[_data.Sum(a => a.Length)];
-            var offset = 0;
-            foreach (var array in _data)
-            {
-                Buffer.BlockCopy(array, 0, result, offset, array.Length);
-                offset += array.Length;
-            }
-
-            return result;
-        }
-
-        public void Write(bool value)
-        {
-            _data.Add(BitConverter.GetBytes(value));
-        }
-
-        public void Write(byte value)
-        {
-            _data.Add(BitConverter.GetBytes(value));
-        }
-
-        public void Write(short value)
-        {
-            _data.Add(BitConverter.GetBytes(value));
-        }
-
-        public void Write(int value)
-        {
-            _data.Add(BitConverter.GetBytes(value));
-        }
-
-        public void Write(long value)
-        {
-            _data.Add(BitConverter.GetBytes(value));
-        }
-
-        public void Write(float value)
-        {
-            _data.Add(BitConverter.GetBytes(value));
-        }
-
-        public void Write(double value)
-        {
-            _data.Add(BitConverter.GetBytes(value));
-        }
-
-        protected void Write7BitEncodedInt(int value)
-        {
-            // Write out an int 7 bits at a time.  The high bit of the byte,
-            // when on, tells reader to continue reading more bytes.
-            var v = (uint)value; // support negative numbers
-            while (v >= 0x80)
-            {
-                Write((byte)(v | 0x80));
-                v >>= 7;
-            }
-
-            Write((byte)v);
-        }
-
-        public void Write(string value)
-        {
-            Write7BitEncodedInt(value.Length);
-            _data.Add(Encoding.UTF8.GetBytes(value));
-        }
-
-        public void Write(DateTime value)
-        {
-            Write(value.ToBinary());
-        }
-
-        public void Write(TimeSpan value)
-        {
-            Write(value.Ticks);
-        }
-
-        public void Write(Guid value)
-        {
-            _data.Add(value.ToByteArray());
-        }
-
-        public void Write(byte[] value)
-        {
-            _data.Add(value);
-        }
-    }
-}

+ 5 - 3
InABox.Core/IPackable.cs

@@ -1,8 +1,10 @@
-namespace InABox.Core
+using System.IO;
+
+namespace InABox.Core
 {
     public interface IPackable
     {
-        void Pack(FastBinaryWriter writer);
-        void Unpack(FastBinaryReader reader);
+        void Pack(BinaryWriter writer);
+        void Unpack(BinaryReader reader);
     }
 }

+ 0 - 161
InABox.Core/PackableDictionary.cs

@@ -1,161 +0,0 @@
-using System;
-using System.Collections.Generic;
-
-namespace InABox.Core
-{
-    public abstract class PackableDictionary<T> : Dictionary<string, T>, IPackable
-    {
-        public void Pack(FastBinaryWriter writer)
-        {
-            writer.Write(Count);
-            foreach (var key in Keys)
-            {
-                writer.Write(key);
-                PackItem(writer, this[key]);
-            }
-        }
-
-        public void Unpack(FastBinaryReader 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(FastBinaryWriter writer, T value);
-
-        public abstract T UnpackItem(FastBinaryReader reader);
-    }
-
-    public class PackableStringDictionary : PackableDictionary<string>
-    {
-        public override void PackItem(FastBinaryWriter writer, string value)
-        {
-            writer.Write(value);
-        }
-
-        public override string UnpackItem(FastBinaryReader reader)
-        {
-            return reader.ReadString();
-        }
-    }
-
-    public class PackableBooleanDictionary : PackableDictionary<bool>
-    {
-        public override void PackItem(FastBinaryWriter writer, bool value)
-        {
-            writer.Write(value);
-        }
-
-        public override bool UnpackItem(FastBinaryReader 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(FastBinaryWriter 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(FastBinaryReader 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);
-        }
-    }
-}

+ 3 - 2
InABox.Core/PackableList.cs

@@ -1,4 +1,5 @@
 using System.Collections;
+using System.IO;
 
 namespace InABox.Core
 {
@@ -8,14 +9,14 @@ namespace InABox.Core
 
     public class PackableList<T> : ObservableList<T>, IPackableList where T : BaseObject, IPackable, new()
     {
-        public void Pack(FastBinaryWriter writer)
+        public void Pack(BinaryWriter writer)
         {
             writer.Write(Count);
             foreach (var item in this)
                 item.Pack(writer);
         }
 
-        public void Unpack(FastBinaryReader reader)
+        public void Unpack(BinaryReader reader)
         {
             var iCount = reader.ReadInt32();
             for (var i = 0; i < iCount; i++)

+ 6 - 1
InABox.Core/Profiler.cs

@@ -25,7 +25,12 @@ namespace InABox.Core
         public long GetElapsedMilliseconds() => _stopwatch.ElapsedMilliseconds;
         public void Pause() => _stopwatch.Stop();
         public void Resume() => _stopwatch.Start();
-        public void Restart() => _stopwatch.Restart();
+        public long Restart()
+        {
+            var ms = _stopwatch.ElapsedMilliseconds;
+            _stopwatch.Restart();
+            return ms;
+        }
 
         public void Log()
         {

+ 5 - 41
InABox.Core/UserProperties.cs

@@ -1,59 +1,33 @@
 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
+using System.IO;
 using System.Linq;
 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(FastBinaryWriter writer)
-        {
-            writer.Write(Type.EntityName());
-            writer.Write(Value?.ToString() ?? "");
-        }
-
-        public void Unpack(FastBinaryReader reader)
-        {
-            Type = CoreUtils.GetEntity(reader.ReadString());
-            Value = CoreUtils.ChangeType(reader.ReadString(), Type);
-        }
-    }
-
-    public class UserPropertyDictionary : PackableDictionary<UserProperty>
-    {
-        public override void PackItem(FastBinaryWriter writer, UserProperty value)
-        {
-            value.Pack(writer);
-        }
-
-        public override UserProperty UnpackItem(FastBinaryReader 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; }
 
@@ -84,16 +58,6 @@ namespace InABox.Core
         [JsonIgnore]
         public int Count => Dictionary.Count;
 
-        public void Pack(FastBinaryWriter writer)
-        {
-            Dictionary.Pack(writer);
-        }
-
-        public void Unpack(FastBinaryReader reader)
-        {
-            Dictionary.Unpack(reader);
-        }
-
         public override bool Equals(object obj)
         {
             if (!(obj is UserProperties other))

+ 3 - 4
InABox.Database/Stores/Store.cs

@@ -90,7 +90,7 @@ namespace InABox.Database
             Provider.Save(tracking);
         }
 
-        protected IStore FindSubStore(Type t)
+        public IStore FindSubStore(Type t)
         {
             var defType = typeof(Store<>).MakeGenericType(t);
 
@@ -106,7 +106,7 @@ namespace InABox.Database
             return result;
         }
 
-        protected IStore<TEntity> FindSubStore<TEntity>() where TEntity : Entity, new()
+        public IStore<TEntity> FindSubStore<TEntity>() where TEntity : Entity, new()
         {
             var defType = typeof(Store<>).MakeGenericType(typeof(TEntity));
 
@@ -599,11 +599,10 @@ namespace InABox.Database
 
             //UpdateExternalLinks(entity, false);
 
+            entity = RunScript(ScriptType.AfterSave, new[] { entity }).First();
             entity.CommitChanges();
 
             //CloseSession("Save", false);
-
-            entity = RunScript(ScriptType.AfterSave, new[] { entity }).First();
             //}
             //catch (Exception e)
             //{

+ 11 - 7
inabox.database.sqlite/SQLiteProvider.cs

@@ -1157,7 +1157,10 @@ namespace InABox.Database.SQLite
                 if (!IsNull(o))
                 {
                     if (o is byte[] array)
-                        packable.Unpack(new FastBinaryReader(array));
+                    {
+                        using (var ms = new MemoryStream(array))
+                            packable.Unpack(new BinaryReader(ms));
+                    }
                 }
 
                 return packable;
@@ -1255,12 +1258,13 @@ namespace InABox.Database.SQLite
                     return ms.GetBuffer();
                 }
 
-            if (o is IPackable)
-                using (var writer = new FastBinaryWriter())
-                {
-                    ((IPackable)o).Pack(writer);
-                    return writer.GetBuffer();
-                }
+            if (o is IPackable pack)
+            {
+                using var ms = new MemoryStream();
+                using var writer = new BinaryWriter(ms);
+                pack.Pack(writer);
+                return ms.ToArray();
+            }
 
             if (o.GetType() == typeof(double) && o.Equals(default(double)))
                 return DBNull.Value;