Browse Source

Checking these in before I refactor them

Marko Pintera 11 năm trước cách đây
mục cha
commit
89723a5273

+ 2 - 0
MBansheeEngine/MBansheeEngine.csproj

@@ -85,7 +85,9 @@
     <Compile Include="SceneObject.cs" />
     <Compile Include="ScriptObject.cs" />
     <Compile Include="SerializableArray.cs" />
+    <Compile Include="SerializableDictionary.cs" />
     <Compile Include="SerializableField.cs" />
+    <Compile Include="SerializableList.cs" />
     <Compile Include="SerializableObject.cs" />
     <Compile Include="SerializableValue.cs" />
     <Compile Include="SerializeObject.cs" />

+ 46 - 11
MBansheeEngine/SerializableArray.cs

@@ -7,38 +7,73 @@ using System.Threading.Tasks;
 
 namespace BansheeEngine
 {
-    public sealed class SerializableArray
+    public sealed class SerializableArray : ScriptObject
     {
+        private object referencedObject;
+        private SerializableField.FieldType elementType;
+        private Type internalElementType;
+        private int[] dimensions;
+
         public SerializableArray(object obj)
         {
-            // TODO - Initialize the array - handle it properly in case obj isn't a valid array
-        }
+            Internal_CreateInstance(this, obj);
 
-        public SerializableField.FieldType ElementType;
-        private int[] dimensions;
-        private int rank;
+            referencedObject = obj;
+        }
 
         public int GetDimension(int rank)
         {
             return dimensions[rank];
         }
 
+        public SerializableField.FieldType ElementType
+        {
+            get { return elementType; }
+        }
+
         public int Rank
         {
             get { return rank;  }
         }
 
-        public SerializableValue GetValue(int id)
+        public T GetValue<T>(params int[] indexes)
+        {
+            if (typeof(T) != internalElementType)
+                throw new Exception("Attempted to retrieve a serializable value using an invalid type. Provided type: " + typeof(T) + ". Needed type: " + internalElementType);
+
+            return (T)Internal_GetValue(mCachedPtr, ArrayIndexesToId(indexes));
+        }
+
+        public void SetValue<T>(T value, params int[] indexes)
+        {
+            if (typeof(T) != internalElementType)
+                throw new Exception("Attempted to set a serializable value using an invalid type. Provided type: " + typeof(T) + ". Needed type: " + internalElementType);
+
+            Internal_SetValue(mCachedPtr, ArrayIndexesToId(indexes), value);
+        }
+
+        private int ArrayIndexesToId(params int[] indexes)
         {
-            return null; // TODO - Return actual SerializableValue
+            int index = 0;
+            int prevDimensionSize = 1;
+
+            for (int i = dimensions.Length - 1; i >= 0; i--)
+            {
+                index += indexes[i] * prevDimensionSize;
+
+                prevDimensionSize *= dimensions[i];
+            }
+
+            return index;
         }
 
-        // TODO - Add getters/setters for all fields
+        [MethodImpl(MethodImplOptions.InternalCall)]
+        private static extern void Internal_CreateInstance(SerializableArray instance, object obj);
 
         [MethodImpl(MethodImplOptions.InternalCall)]
-        private static extern void Internal_SetInt32(IntPtr nativeInstance, int arrayIdx, Int32 value);
+        private static extern void Internal_SetValue(IntPtr nativeInstance, int elementId, object value);
 
         [MethodImpl(MethodImplOptions.InternalCall)]
-        private static extern Int32 Internal_GetInt32(IntPtr nativeInstance, int arrayIdx);
+        private static extern object Internal_GetValue(IntPtr nativeInstance, int elementId);
     }
 }

+ 11 - 0
MBansheeEngine/SerializableDictionary.cs

@@ -0,0 +1,11 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace BansheeEngine
+{
+    class SerializableDictionary
+    {
+    }
+}

+ 79 - 0
MBansheeEngine/SerializableList.cs

@@ -0,0 +1,79 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Runtime.CompilerServices;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace BansheeEngine
+{
+    public sealed class SerializableList : ScriptObject
+    {
+        private object referencedObject;
+        private SerializableField.FieldType elementType;
+        private Type internalElementType;
+        private int[] dimensions;
+
+        public SerializableArray(object obj)
+        {
+            Internal_CreateInstance(this, obj);
+
+            referencedObject = obj;
+        }
+
+        public int GetDimension(int rank)
+        {
+            return dimensions[rank];
+        }
+
+        public SerializableField.FieldType ElementType
+        {
+            get { return elementType; }
+        }
+
+        public int Rank
+        {
+            get { return rank; }
+        }
+
+        public T GetValue<T>(params int[] indexes)
+        {
+            if (typeof(T) != internalElementType)
+                throw new Exception("Attempted to retrieve a serializable value using an invalid type. Provided type: " + typeof(T) + ". Needed type: " + internalElementType);
+
+            return (T)Internal_GetValue(mCachedPtr, ArrayIndexesToId(indexes));
+        }
+
+        public void SetValue<T>(T value, params int[] indexes)
+        {
+            if (typeof(T) != internalElementType)
+                throw new Exception("Attempted to set a serializable value using an invalid type. Provided type: " + typeof(T) + ". Needed type: " + internalElementType);
+
+            Internal_SetValue(mCachedPtr, ArrayIndexesToId(indexes), value);
+        }
+
+        private int ArrayIndexesToId(params int[] indexes)
+        {
+            int index = 0;
+            int prevDimensionSize = 1;
+
+            for (int i = dimensions.Length - 1; i >= 0; i--)
+            {
+                index += indexes[i] * prevDimensionSize;
+
+                prevDimensionSize *= dimensions[i];
+            }
+
+            return index;
+        }
+
+        [MethodImpl(MethodImplOptions.InternalCall)]
+        private static extern void Internal_CreateInstance(SerializableArray instance, object obj);
+
+        [MethodImpl(MethodImplOptions.InternalCall)]
+        private static extern void Internal_SetValue(IntPtr nativeInstance, int elementId, object value);
+
+        [MethodImpl(MethodImplOptions.InternalCall)]
+        private static extern object Internal_GetValue(IntPtr nativeInstance, int elementId);
+    }
+}