Exemplo n.º 1
0
        public override int ReadAttribute(byte[] data, int offset, FieldInfo f, object target, Dictionary <string, byte[]> other)
        {
            PointerArray namesPointer = (PointerArray)target.GetType().GetField(pointerName).GetValue(target);

            Pointer[] pointers = (Pointer[])target.GetType().GetField(varName).GetValue(target);

            byte[] sourceData = data;
            if (source != "")
            {
                sourceData = other[source];
            }

            int pos = offset;

            Type elemType = f.FieldType.GetElementType();

            BinaryStructure[] objs = (BinaryStructure[])Array.CreateInstance(elemType, namesPointer.max);

            for (int i = 0; i < namesPointer.max; i++)
            {
                pos = (int)pointers[i].offset;
                //Debug.Log("Pointer points to " + pos);
                BinaryStructure obj = (BinaryStructure)Activator.CreateInstance(elemType);
                pos     = obj.Load(sourceData, other, pos);
                objs[i] = obj;
            }

            f.SetValue(target, objs);

            return(pos);
        }
Exemplo n.º 2
0
        public int Load(byte[] data, Dictionary <string, byte[]> other, int start)
        {
            // Add this class to BinaryCache if we haven't yet
            BinaryCache.RegisterType(GetType());

            int offset = start;

            // Serialize the data

            // Using reflection, get all the public properties of this class
            foreach (FieldInfo p in BinaryCache.fieldInfo[GetType()])
            {
                //Debug.Log("Reading " + p.Name + " at offset " + offset);
                if (BinaryConversion.conversions.ContainsKey(p.FieldType))
                {
                    offset = SetAttribute(p, data, offset);
                    //Debug.Log("Set value of " + p.Name + " to " + p.GetValue(this).ToString());
                    continue;
                }

                //Debug.Log("Type " + p.FieldType + " is not primitive");

                var attributes = p.GetCustomAttributes <BinaryAttribute>();

                if (attributes.Count() == 0)
                {
                    // We need to set the field by serializing into a custom data type
                    if (!p.FieldType.IsSubclassOf(typeof(BinaryStructure)))
                    {
                        throw new Exception("Trying to serialize something that's not inherited from BinaryStructure");
                    }

                    //Debug.Log("Reading attribute as a sub-structure");
                    BinaryStructure child = (BinaryStructure)Activator.CreateInstance(p.FieldType);
                    offset = child.Load(data, other, offset);

                    p.SetValue(this, child);
                }
                else if (attributes.Count() == 1)
                {
                    //Debug.Log("Reading attribute using BinaryAttribute");

                    BinaryAttribute b = attributes.First();
                    offset = b.ReadAttribute(data, offset, p, this, other);
                }
                else
                {
                    throw new Exception("Invalid number of attributes " + attributes.Count() + " found");
                }

                object val = p.GetValue(this);
                //Debug.Log("Set value of " + p.Name + " to " + val);
            }

            return(offset);
        }
Exemplo n.º 3
0
        public override int ReadAttribute(byte[] data, int offset, FieldInfo f, object target, Dictionary <string, byte[]> other)
        {
            Array array = Array.CreateInstance(t, count);

            // TODO: Support non-primitive arrays
            for (int i = 0; i < count; i++)
            {
                array.SetValue(BinaryStructure.ConvertValue(t, data, offset), i);
                offset += BinaryConversion.sizes[t];
            }

            f.SetValue(target, array);
            return(offset);
        }
Exemplo n.º 4
0
            public override int ReadAttribute(byte[] data, int offset, FieldInfo f, object target, Dictionary <string, byte[]> other)
            {
                try
                {
                    Debug.Log(target.GetType().GetField("name").GetValue(target));
                    Debug.Log(target.GetType().GetField("vertexSize").GetValue(target));
                    Debug.Log(target.GetType().GetField("numVertices").GetValue(target));
                }
                catch
                {
                }

                // If we have an offset to use, use it
                if (offset != null)
                {
                    offset = Convert.ToInt32(target.GetType().GetField(offsetVar).GetValue(target));
                }

                // If target has a variable with name "varName", use the value of that variable
                // Otherwise, call the function "varName" on target
                int count = 0;

                if (target.GetType().GetField(varName) != null)
                {
                    count = Convert.ToInt32(target.GetType().GetField(varName).GetValue(target));
                }
                else
                {
                    count = Convert.ToInt32(target.GetType().GetMethod(varName).Invoke(target, new object[]
                    {
                        data, offset, f, target, other
                    }));
                }
                BinaryStructure[] objs = (BinaryStructure[])Array.CreateInstance(t, count);

                for (int i = 0; i < count; i++)
                {
                    BinaryStructure obj = (BinaryStructure)Activator.CreateInstance(t);
                    offset  = obj.Load(data, other, offset);
                    objs[i] = obj;
                }

                f.SetValue(target, objs);

                return(offset);
            }
Exemplo n.º 5
0
        public override int ReadAttribute(byte[] data, int offset, FieldInfo f, object target, Dictionary <string, byte[]> other)
        {
            bool active = (bool)target.GetType().GetMethod(func).Invoke(target, new object[]
            {
                data, offset, f, target, other
            });

            //Debug.Log("Active = " + active);

            if (active)
            {
                BinaryStructure child = (BinaryStructure)Activator.CreateInstance(f.FieldType);
                offset = child.Load(data, other, offset);

                f.SetValue(target, child);
            }

            return(offset);
        }
Exemplo n.º 6
0
        public override int ReadAttribute(byte[] data, int offset, FieldInfo f, object target, Dictionary <string, byte[]> other)
        {
            //Debug.Log("Reading attribute " + f.Name + " starting at offset " + offset);
            byte[] sourceData = data;
            if (source != "")
            {
                sourceData = other[this.source];
            }

            // Get the offset value
            int sourceOffset = (int)(uint)target.GetType().GetField(this.offset).GetValue(target);

            BinaryStructure child     = (BinaryStructure)Activator.CreateInstance(f.FieldType);
            int             newOffset = child.Load(sourceData, other, sourceOffset);

            f.SetValue(target, child);

            return(offset);
        }
Exemplo n.º 7
0
        public override int ReadAttribute(byte[] data, int offset, FieldInfo f, object target, Dictionary <string, byte[]> other)
        {
            byte[] sourceData = data;
            if (source != "")
            {
                sourceData = other[source];
            }

            PointerArray pointer = (PointerArray)target.GetType().GetField(varName).GetValue(target);
            int          pos     = (int)pointer.pointer;

            if (offsetName != "")
            {
                pos += (int)(uint)target.GetType().GetField(offsetName).GetValue(target);
            }

            Type elemType = f.FieldType.GetElementType();

            BinaryStructure[] objs = (BinaryStructure[])Array.CreateInstance(elemType, pointer.used);

            for (int i = 0; i < pointer.used; i++)
            {
                BinaryStructure obj = (BinaryStructure)Activator.CreateInstance(elemType);
                pos     = obj.Load(sourceData, other, pos);
                objs[i] = obj;
            }

            f.SetValue(target, objs);

            if (pointer.used == 0 || !movePos)
            {
                return(offset);
            }

            return(pos);
        }