소스 검색

Got rid of FastBinaryWriter/Reader

Kenric Nugteren 2 년 전
부모
커밋
4b52ce87df
5개의 변경된 파일17개의 추가작업 그리고 238개의 파일을 삭제
  1. 10 0
      InABox.Core/DataTable.cs
  2. 0 128
      InABox.Core/FastBinaryReader.cs
  3. 0 105
      InABox.Core/FastBinaryWriter.cs
  4. 5 3
      InABox.Core/IPackable.cs
  5. 2 2
      InABox.Database/Stores/Store.cs

+ 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}");

+ 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);
     }
 }

+ 2 - 2
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));