Beispiel #1
0
        private void SaveGR2(string outPath, Root root)
        {
            root.PreSave();
            var writer = new LSLib.Granny.GR2.GR2Writer();

            writer.Format         = Options.Is64Bit ? Magic.Format.LittleEndian64 : Magic.Format.LittleEndian32;
            writer.AlternateMagic = Options.AlternateSignature;
            writer.VersionTag     = Options.VersionTag;


            if (Options.UseObsoleteVersionTag)
            {
                // Use an obsolete version tag to prevent Granny from memory mapping the structs
                writer.VersionTag -= 1;
            }

            var body = writer.Write(root);

            writer.Dispose();

            FileStream f = new FileStream(outPath, FileMode.Create, System.IO.FileAccess.Write, FileShare.None);

            f.Write(body, 0, body.Length);
            f.Close();
            f.Dispose();
        }
        public void Write(GR2Writer writer, WritableSection section, MemberDefinition member, object obj)
        {
            var items = obj as List <UInt16>;

            for (int i = 0; i < items.Count; i++)
            {
                section.Writer.Write(items[i]);
            }
        }
Beispiel #3
0
        private void LoadAttributes(FieldInfo info, GR2Writer writer)
        {
            var attrs = info.GetCustomAttributes(typeof(SerializationAttribute), true);

            if (attrs.Length > 0)
            {
                SerializationAttribute serialization = attrs[0] as SerializationAttribute;

                if (serialization.Section != SectionType.Invalid)
                {
                    PreferredSection = serialization.Section;
                }

                DataArea = serialization.DataArea;

                if (serialization.Type != MemberType.Invalid)
                {
                    Type = serialization.Type;
                }

                if (serialization.TypeSelector != null)
                {
                    TypeSelector = Activator.CreateInstance(serialization.TypeSelector) as VariantTypeSelector;
                }

                if (serialization.SectionSelector != null)
                {
                    SectionSelector = Activator.CreateInstance(serialization.SectionSelector) as SectionSelector;
                }

                if (serialization.Serializer != null)
                {
                    Serializer = Activator.CreateInstance(serialization.Serializer) as NodeSerializer;
                }

                if (writer != null && serialization.Prototype != null)
                {
                    WriteDefinition = writer.LookupStructDefinition(serialization.Prototype);
                }

                if (serialization.Name != null)
                {
                    GrannyName = serialization.Name;
                }

                Prototype         = serialization.Prototype;
                SerializationKind = serialization.Kind;
                ArraySize         = serialization.ArraySize;
                MinVersion        = serialization.MinVersion;
                MaxVersion        = serialization.MaxVersion;
            }
        }
Beispiel #4
0
        public WritableSection(SectionType type, GR2Writer writer)
        {
            Type = type;
            MainStream = new MemoryStream();
            MainWriter = new BinaryWriter(MainStream);

            DataStream = new MemoryStream();
            DataWriter = new BinaryWriter(DataStream);

            Writer = MainWriter;
            Header = InitHeader();
            GR2 = writer;
        }
Beispiel #5
0
        public WritableSection(SectionType type, GR2Writer writer)
        {
            Type       = type;
            MainStream = new MemoryStream();
            MainWriter = new BinaryWriter(MainStream);

            DataStream = new MemoryStream();
            DataWriter = new BinaryWriter(DataStream);

            Writer = MainWriter;
            Header = InitHeader();
            GR2    = writer;
        }
Beispiel #6
0
        public void LoadFromType(Type type, GR2Writer writer)
        {
            Type = type;

            var attrs = type.GetCustomAttributes(typeof(StructSerializationAttribute), true);

            if (attrs.Length > 0)
            {
                StructSerializationAttribute serialization = attrs[0] as StructSerializationAttribute;
                MixedMarshal = serialization.MixedMarshal;
            }

            foreach (var field in type.GetFields(BindingFlags.Instance | BindingFlags.Public))
            {
                var member = MemberDefinition.CreateFromFieldInfo(field, writer);
                if (member.SerializationKind != SerializationKind.None)
                {
                    Members.Add(MemberDefinition.CreateFromFieldInfo(field, writer));
                }
            }
        }
Beispiel #7
0
        public static MemberDefinition CreateFromFieldInfo(FieldInfo info, GR2Writer writer)
        {
            var member = new MemberDefinition();
            var type   = info.FieldType;

            member.Name           = info.Name;
            member.GrannyName     = info.Name;
            member.Extra          = new UInt32[] { 0, 0, 0 };
            member.CachedField    = info;
            member.HasCachedField = true;

            member.LoadAttributes(info, writer);

            if (type.IsArray)
            {
                if (member.ArraySize == 0)
                {
                    throw new InvalidOperationException("SerializationAttribute.ArraySize must be set for fixed size arrays");
                }
                type = type.GetElementType();
            }

            if (member.Type == MemberType.Invalid)
            {
                if (type == typeof(SByte))
                {
                    member.Type = MemberType.Int8;
                }
                else if (type == typeof(Byte))
                {
                    member.Type = MemberType.UInt8;
                }
                else if (type == typeof(Int16))
                {
                    member.Type = MemberType.Int16;
                }
                else if (type == typeof(UInt16))
                {
                    member.Type = MemberType.UInt16;
                }
                else if (type == typeof(Int32))
                {
                    member.Type = MemberType.Int32;
                }
                else if (type == typeof(UInt32))
                {
                    member.Type = MemberType.UInt32;
                }
                else if (type == typeof(Half))
                {
                    member.Type = MemberType.Real16;
                }
                else if (type == typeof(Single))
                {
                    member.Type = MemberType.Real32;
                }
                else if (type == typeof(string))
                {
                    member.Type = MemberType.String;
                }
                else if (type == typeof(Transform))
                {
                    member.Type = MemberType.Transform;
                }
                else if (type == typeof(object) || type.IsAbstract || type.IsInterface)
                {
                    member.Type = MemberType.VariantReference;
                }
                else if (type.GetInterfaces().Contains(typeof(IList <object>)))
                {
                    member.Type = MemberType.ReferenceToVariantArray;
                }
                else if (type.GetInterfaces().Contains(typeof(System.Collections.IList)))
                {
                    member.Type = MemberType.ReferenceToArray; // or ArrayOfReferences?
                }
                else
                {
                    member.Type = MemberType.Reference; // or Inline?
                }
            }

            if (member.SerializationKind != SerializationKind.None && member.WriteDefinition == null && writer != null)
            {
                if (member.Type == MemberType.Inline || member.Type == MemberType.Reference)
                {
                    member.WriteDefinition = writer.LookupStructDefinition(type);
                }
                else if (member.Type == MemberType.ReferenceToArray || member.Type == MemberType.ArrayOfReferences)
                {
                    member.WriteDefinition = writer.LookupStructDefinition(type.GetGenericArguments().Single());
                }
            }


            return(member);
        }
Beispiel #8
0
 public void Write(GR2Writer writer, WritableSection section, MemberDefinition member, object obj)
 {
     (obj as Vertex).Serialize(section);
 }
Beispiel #9
0
        private void SaveGR2(string outPath, Root root)
        {
            root.PreSave();
            var writer = new LSLib.Granny.GR2.GR2Writer();

            writer.Format = Options.Is64Bit ? Magic.Format.LittleEndian64 : Magic.Format.LittleEndian32;
            writer.AlternateMagic = Options.AlternateSignature;
            writer.VersionTag = Options.VersionTag;


            if (Options.UseObsoleteVersionTag)
            {
                // Use an obsolete version tag to prevent Granny from memory mapping the structs
                writer.VersionTag -= 1;
            }

            var body = writer.Write(root);
            writer.Dispose();

            FileStream f = new FileStream(outPath, FileMode.Create, System.IO.FileAccess.Write, FileShare.None);
            f.Write(body, 0, body.Length);
            f.Close();
            f.Dispose();
        }
Beispiel #10
0
 public void Write(GR2Writer writer, WritableSection section, MemberDefinition member, object obj)
 {
     var items = obj as List<UInt16>;
     for (int i = 0; i < items.Count; i++)
         section.Writer.Write(items[i]);
 }