A value stored as a compressed integer.
Beispiel #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="reader"></param>
        public FieldInfo(BinaryReader reader)
        {
            Value      = reader.ReadByte();
            Size       = 0;
            ArrayIndex = 0;
            switch (SizeForm)
            {
            case FieldTypeSize.Size1: Size = 1; break;

            case FieldTypeSize.Size2: Size = 2; break;

            case FieldTypeSize.Size4: Size = 4; break;

            case FieldTypeSize.Size12: Size = 12; break;

            case FieldTypeSize.Size16: Size = 16; break;

            case FieldTypeSize.SizeByte: Size = reader.ReadByte(); break;

            case FieldTypeSize.SizeShort: Size = reader.ReadUInt16(); break;

            case FieldTypeSize.SizeInt: Size = reader.ReadInt32(); break;

            default: throw new Exception();
            }
            if (IsArray && Type != FieldTypeBase.Boolean)
            {
                ArrayIndex = UIndex.Read(reader);
            }
        }
Beispiel #2
0
            /// <summary></summary>
            public override object Read(object target, Package package, System.IO.BinaryReader reader, long end)
            {
                int count = UIndex.Read(reader);

                if (count != 0)
                {
                    throw new Exception();
                }
                return(count);
            }
Beispiel #3
0
 internal void Load(BinaryReader reader)
 {
     ObjectClassReference = Package.ReadReference(reader);
     SuperClassReference  = Package.ReadReference(reader);
     Group = reader.ReadInt32();
     Name  = Package.ReadNameValue(reader);
     Flags = (ExportFlag)reader.ReadUInt32();
     Size  = UIndex.Read(reader);
     if (Size != 0)
     {
         Offset = UIndex.Read(reader);
     }
 }
Beispiel #4
0
        /// <summary></summary>
        public static List <T> ReadIndexCountList <T>(this BinaryReader reader, Func <T> function, bool nullIfEmpty = false)
        {
            int count = UIndex.Read(reader);

            if (count == 0 && nullIfEmpty)
            {
                return(null);
            }
            var list = new List <T>(count);

            for (int index = 0; index < count; index++)
            {
                list.Add(function());
            }
            return(list);
        }
Beispiel #5
0
        /// <summary></summary>
        public static T[] ReadIndexCountArray <T>(this BinaryReader reader, Func <T> function, bool nullIfEmpty = false)
        {
            int count = UIndex.Read(reader);

            if (count == 0 && nullIfEmpty)
            {
                return(null);
            }
            var array = new T[count];

            for (int index = 0; index < count; index++)
            {
                array[index] = function();
            }
            return(array);
        }
Beispiel #6
0
            /// <summary></summary>
            public override object Read(object target, Package package, BinaryReader reader, long end)
            {
                int count = UIndex.Read(reader);

                if (count == 0)
                {
                    return(null);
                }
                SCG.List <T> result = new SCG.List <T>(count);

                for (var index = 0; index < count; index++)
                {
                    var value = new T();
                    value.Package = package;
                    value.Load(reader, end);
                    result.Add(value);
                }

                return(result);
            }
Beispiel #7
0
        static object LoadSingle(Package package, BinaryReader reader, FieldInfo info, bool inArray = false)
        {
            if (info.Size < 1)
            {
                throw new Exception();
            }
            switch (info.Type)
            {
            case FieldTypeBase.Byte:
                if (!inArray && info.Size != 1)
                {
                    throw new Exception();
                }
                return(reader.ReadByte());

            case FieldTypeBase.Float:
                if (!inArray && info.Size != 4)
                {
                    throw new Exception();
                }
                return(reader.ReadSingle());

            case FieldTypeBase.Integer:
                if (!inArray && info.Size != 4)
                {
                    throw new Exception();
                }
                return(reader.ReadInt32());

            case FieldTypeBase.Name:
                return(package.ReadNameIndex(reader).Value);

            case FieldTypeBase.Object:
                return(package.ReadReference(reader));

            case FieldTypeBase.Str:
                var stringLength = reader.ReadByte();
                if (stringLength != info.Size - 1)
                {
                    throw new Exception("String length isn't the field size minus 1.");
                }
                return(reader.ReadStringz(stringLength, Encoding.ASCII));

            case FieldTypeBase.Struct:
                string name;

                if (info.IsLiteralSizeForm)
                {
                    name      = package.Names[info.Size].Value;
                    info.Size = UIndex.Read(reader);
                }
                else
                {
                    name = package.ReadNameIndex(reader).Value;
                }

                var    end = reader.BaseStream.Position + info.Size;
                object result;

                switch (name)
                {
                case "InitialAllianceInfo":
                    result = InitialAllianceInfo.Read(reader, package);
                    break;

                case "PointRegion":
                    if (!inArray && info.Size < 6)
                    {
                        throw new Exception("PointRegion structure size must be at least 6 bytes.");
                    }
                    result = PointRegion.Read(reader, package);
                    break;

                case "Rotator":
                    result = Rotator.Read(reader);
                    break;

                case "Scale":
                    if (!inArray && info.Size != 17)
                    {
                        throw new Exception("Scale structure size is a constant 17 bytes.");
                    }
                    result = Scale.Read(reader);
                    break;

                case "Vector":
                    if (!inArray && info.Size != 12)
                    {
                        throw new Exception("Vector structure size is a constant 12 bytes.");
                    }
                    result = reader.ReadVector3f();
                    break;

                default:
                    throw new Exception("Unhandled or unknown struct type " + name + ".");
                }

                if (!inArray && reader.BaseStream.Position != end)
                {
                    throw new Exception("Structure wasn't fully devoured or was overconsumed.");
                }
                return(result);

            default:
                throw new Exception("Unknown or unhandled type " + info.Type + ".");
            }
        }
Beispiel #8
0
            /// <summary></summary>
            public override object Read(object target, Package package, BinaryReader reader, long end)
            {
                int length = UIndex.Read(reader);

                return(reader.ReadStringz(length, Encoding.ASCII));
            }
Beispiel #9
0
 /// <summary></summary>
 public override object Read(object target, Package package, BinaryReader reader, long end)
 {
     return((int)UIndex.Read(reader));
 }
Beispiel #10
0
 public override object Read(object target, Package package, BinaryReader reader, long end)
 {
     int count = UIndex.Read(reader); return(reader.ReadBytes(count));
 }
Beispiel #11
0
        /// <summary>
        /// Read a <see cref="Name"/> reference from the <see cref="BinaryReader"/>.
        /// </summary>
        /// <param name="reader">The <see cref="BinaryReader"/> to read from.</param>
        /// <returns>The <see cref="Name"/> object.</returns>
        public Name ReadNameIndex(BinaryReader reader)
        {
            int index = UIndex.Read(reader);

            return(Names[index]);
        }
Beispiel #12
0
        /// <summary>Read a reference from the <see cref="BinaryReader"/>.</summary>
        /// <param name="reader">The <see cref="BinaryReader"/> to read from.</param>
        /// <returns>The resolved reference or <c>null</c>.</returns>
        public Reference ReadReference(BinaryReader reader)
        {
            int index = UIndex.Read(reader);

            return(ResolveReference(index));
        }