Ejemplo n.º 1
0
 public static void WriteBaseProperty(this Stream stream, BaseProperty value)
 {
     stream.WriteInt32(value.NameIdx);
     stream.WriteInt32(value.TypeIdx);
     stream.WriteInt32(value.PropertySize);
     stream.WriteInt32(value.ArrayIdx);
 }
        public static void WriteProperty(this Stream stream, string fieldName, Type fieldType, 
            object fieldValue, List<string> nameTable, int? arrayIndex = null)
        {
            if (fieldType.IsArray)
            {
                var array = (Array)fieldValue;
                for (var i = 0; i < array.Length; i++)
                {
                    var value = array.GetValue(i);
                    if (value != null)
                        stream.WriteProperty(fieldName, fieldType.GetElementType(), array.GetValue(i), nameTable, i);
                }
                return;
            }

            var fieldBodyStream = new MemoryStream();

            var baseProperty = new BaseProperty
            {
                NameIdx = AddToNameTable(nameTable, fieldName),
                ArrayIdx = arrayIndex ?? 0
            };

            if (fieldType == typeof (int))
            {
                baseProperty.TypeIdx = AddToNameTable(nameTable, "IntProperty");
                var value = (int) fieldValue;
                fieldBodyStream.WriteInt32(value);
            }
            else if (fieldType == typeof (bool))
            {
                baseProperty.TypeIdx = AddToNameTable(nameTable, "BoolProperty");
                var value = (bool) fieldValue;
                fieldBodyStream.WriteBool(value);
            }
            else if (fieldType.IsEnum)
            {
                baseProperty.TypeIdx = AddToNameTable(nameTable, "ByteProperty");
                var value1 = AddToNameTable(nameTable, fieldType.Name); // Enum type name
                var value2 = AddToNameTable(nameTable, fieldValue.ToString()); // Enum value
                fieldBodyStream.WriteInt32(value1);
                fieldBodyStream.WriteInt32(value2);
            }
            else if (fieldType == typeof (float))
            {
                baseProperty.TypeIdx = AddToNameTable(nameTable, "FloatProperty");
                var value = (float) fieldValue;
                fieldBodyStream.WriteFloat(value);
            }
            else if (fieldType == typeof (NameProperty))
            {
                baseProperty.TypeIdx = AddToNameTable(nameTable, "NameProperty");
                var nameProp = (NameProperty) fieldValue;
                var value = AddToNameTable(nameTable, nameProp.Name);
                fieldBodyStream.WriteInt32(value);
            }
            else if (fieldType == typeof (string))
            {
                baseProperty.TypeIdx = AddToNameTable(nameTable, "StrProperty");
                var value = (string) fieldValue;
                switch (fieldName)
                {
                    case "SaveDetails":
                    case "FriendlyName":
                        fieldBodyStream.WriteUnicodeString(value);
                        break;
                    default:
                        fieldBodyStream.WriteString(value);
                        break;
                }
                //fieldBodyStream.WriteUnicodeString(value); // Save every string as unicode
            }
            else if (fieldType.IsGenericType && (fieldType.GetGenericTypeDefinition() == typeof(List<>)))
            {
                baseProperty.TypeIdx = AddToNameTable(nameTable, "ArrayProperty");
                var list = fieldValue as IList;
                var elementType = fieldType.GetGenericArguments()[0];
                fieldBodyStream.WriteInt32(list.Count); // numElements
                for (var i = 0; i < list.Count; i++)
                {
                    if (elementType == typeof (int))
                    {
                        fieldBodyStream.WriteInt32((int) list[i]);
                    }
                    else if (elementType == typeof (string))
                    {
                        fieldBodyStream.WriteString((string) list[i]);
                    }
                    else if (elementType == typeof (NameProperty))
                    {
                        var value = AddToNameTable(nameTable, ((NameProperty) list[i]).Name);
                        fieldBodyStream.WriteInt32(value);
                    }
                    else
                    {
                        fieldBodyStream.SerializeObject(list[i], nameTable);
                    }
                }
            }
            else if (!fieldType.IsGenericType && !fieldType.IsValueType && !fieldType.IsPrimitive && fieldType.IsClass)
            {
                baseProperty.TypeIdx = AddToNameTable(nameTable, "StructProperty");

                fieldBodyStream.WriteInt32(AddToNameTable(nameTable, fieldType.Name)); // StructNameIdx

                if (fieldType == typeof (Vector))
                {
                    var value = (Vector) fieldValue;
                    fieldBodyStream.WriteFloat(value.X);
                    fieldBodyStream.WriteFloat(value.Y);
                    fieldBodyStream.WriteFloat(value.Z);
                }
                else if (fieldType == typeof (Rotator))
                {
                    var value = (Rotator) fieldValue;
                    fieldBodyStream.WriteInt32(value.Pitch);
                    fieldBodyStream.WriteInt32(value.Yaw);
                    fieldBodyStream.WriteInt32(value.Roll);
                }
                else
                {
                    fieldBodyStream.SerializeObject(fieldValue, nameTable);
                }
            }

            baseProperty.PropertySize = (int) fieldBodyStream.Length;
            stream.WriteBaseProperty(baseProperty);
            fieldBodyStream.WriteTo(stream);
        }
 public static void WriteBaseProperty(this Stream stream, BaseProperty value)
 {
     stream.WriteInt32(value.NameIdx);
     stream.WriteInt32(value.TypeIdx);
     stream.WriteInt32(value.PropertySize);
     stream.WriteInt32(value.ArrayIdx);
 }
Ejemplo n.º 4
0
        public static void WriteProperty(this Stream stream, string fieldName, Type fieldType,
                                         object fieldValue, List <string> nameTable, int?arrayIndex = null)
        {
            if (fieldType.IsArray)
            {
                var array = (Array)fieldValue;
                for (var i = 0; i < array.Length; i++)
                {
                    var value = array.GetValue(i);
                    if (value != null)
                    {
                        stream.WriteProperty(fieldName, fieldType.GetElementType(), array.GetValue(i), nameTable, i);
                    }
                }
                return;
            }

            var fieldBodyStream = new MemoryStream();

            var baseProperty = new BaseProperty
            {
                NameIdx  = AddToNameTable(nameTable, fieldName),
                ArrayIdx = arrayIndex ?? 0
            };

            if (fieldType == typeof(int))
            {
                baseProperty.TypeIdx = AddToNameTable(nameTable, "IntProperty");
                var value = (int)fieldValue;
                fieldBodyStream.WriteInt32(value);
            }
            else if (fieldType == typeof(bool))
            {
                baseProperty.TypeIdx = AddToNameTable(nameTable, "BoolProperty");
                var value = (bool)fieldValue;
                fieldBodyStream.WriteBool(value);
            }
            else if (fieldType.IsEnum)
            {
                baseProperty.TypeIdx = AddToNameTable(nameTable, "ByteProperty");
                var value1 = AddToNameTable(nameTable, fieldType.Name);        // Enum type name
                var value2 = AddToNameTable(nameTable, fieldValue.ToString()); // Enum value
                fieldBodyStream.WriteInt32(value1);
                fieldBodyStream.WriteInt32(value2);
            }
            else if (fieldType == typeof(float))
            {
                baseProperty.TypeIdx = AddToNameTable(nameTable, "FloatProperty");
                var value = (float)fieldValue;
                fieldBodyStream.WriteFloat(value);
            }
            else if (fieldType == typeof(NameProperty))
            {
                baseProperty.TypeIdx = AddToNameTable(nameTable, "NameProperty");
                var nameProp = (NameProperty)fieldValue;
                var value    = AddToNameTable(nameTable, nameProp.Name);
                fieldBodyStream.WriteInt32(value);
            }
            else if (fieldType == typeof(string))
            {
                baseProperty.TypeIdx = AddToNameTable(nameTable, "StrProperty");
                var value = (string)fieldValue;
                switch (fieldName)
                {
                case "SaveDetails":
                case "FriendlyName":
                    fieldBodyStream.WriteUnicodeString(value);
                    break;

                default:
                    fieldBodyStream.WriteString(value);
                    break;
                }
                //fieldBodyStream.WriteUnicodeString(value); // Save every string as unicode
            }
            else if (fieldType.IsGenericType && (fieldType.GetGenericTypeDefinition() == typeof(List <>)))
            {
                baseProperty.TypeIdx = AddToNameTable(nameTable, "ArrayProperty");
                var list        = fieldValue as IList;
                var elementType = fieldType.GetGenericArguments()[0];
                fieldBodyStream.WriteInt32(list.Count); // numElements
                for (var i = 0; i < list.Count; i++)
                {
                    if (elementType == typeof(int))
                    {
                        fieldBodyStream.WriteInt32((int)list[i]);
                    }
                    else if (elementType == typeof(string))
                    {
                        fieldBodyStream.WriteString((string)list[i]);
                    }
                    else if (elementType == typeof(NameProperty))
                    {
                        var value = AddToNameTable(nameTable, ((NameProperty)list[i]).Name);
                        fieldBodyStream.WriteInt32(value);
                    }
                    else
                    {
                        fieldBodyStream.SerializeObject(list[i], nameTable);
                    }
                }
            }
            else if (!fieldType.IsGenericType && !fieldType.IsValueType && !fieldType.IsPrimitive && fieldType.IsClass)
            {
                baseProperty.TypeIdx = AddToNameTable(nameTable, "StructProperty");

                fieldBodyStream.WriteInt32(AddToNameTable(nameTable, fieldType.Name)); // StructNameIdx

                if (fieldType == typeof(Vector))
                {
                    var value = (Vector)fieldValue;
                    fieldBodyStream.WriteFloat(value.X);
                    fieldBodyStream.WriteFloat(value.Y);
                    fieldBodyStream.WriteFloat(value.Z);
                }
                else if (fieldType == typeof(Rotator))
                {
                    var value = (Rotator)fieldValue;
                    fieldBodyStream.WriteInt32(value.Pitch);
                    fieldBodyStream.WriteInt32(value.Yaw);
                    fieldBodyStream.WriteInt32(value.Roll);
                }
                else
                {
                    fieldBodyStream.SerializeObject(fieldValue, nameTable);
                }
            }

            baseProperty.PropertySize = (int)fieldBodyStream.Length;
            stream.WriteBaseProperty(baseProperty);
            fieldBodyStream.WriteTo(stream);
        }