Example #1
0
        public override Level Read(Stream src, string name, bool metadata)
        {
            using (GZipStream s = new GZipStream(src, CompressionMode.Decompress)) {
                Level     lvl = new Level(name, 0, 0, 0);
                DatReader r   = new DatReader();
                r.src = new BinaryReader(s);

                int signature = r.ReadInt32();
                // Format version 0 - preclassic to classic 0.12
                //  (technically this format doesn't have a signature,
                //   but 99% of such maps will start with these 4 bytes)
                if (signature == 0x01010101)
                {
                    return(ReadFormat0(lvl, s));
                }

                // All valid .dat maps must start with these 4 bytes
                if (signature != 0x271BB788)
                {
                    throw new InvalidDataException("Invalid .dat map signature");
                }

                switch (r.ReadUInt8())
                {
                // Format version 1 - classic 0.13
                case 0x01: return(ReadFormat1(lvl, r));

                // Format version 2 - classic 0.15 to 0.30
                case 0x02: return(ReadFormat2(lvl, r));
                }
                throw new InvalidDataException("Invalid .dat map version");
            }
        }
Example #2
0
        public override Level Read(Stream src, string name, bool metadata)
        {
            using (GZipStream s = new GZipStream(src, CompressionMode.Decompress)) {
                Level     lvl = new Level(name, 0, 0, 0);
                DatReader r   = new DatReader();
                r.src = new BinaryReader(s);

                if (r.ReadInt32() != 0x271BB788 || r.ReadUInt8() != 0x02)
                {
                    throw new InvalidDataException("Unexpected constant in .dat file");
                }

                if (r.ReadUInt16() != 0xACED)
                {
                    throw new InvalidDataException("Invalid stream magic");
                }
                if (r.ReadUInt16() != 0x0005)
                {
                    throw new InvalidDataException("Invalid stream version");
                }

                JObject obj = (JObject)ReadObject(r);
                ParseRootObject(lvl, obj);
                return(lvl);
            }
        }
Example #3
0
        static unsafe object Value(DatReader r, char type)
        {
            if (type == 'B')
            {
                return(r.ReadUInt8());
            }
            if (type == 'C')
            {
                return((char)r.ReadUInt16());
            }
            if (type == 'D')
            {
                long tmp = r.ReadInt64(); return(*(double *)(&tmp));
            }
            if (type == 'F')
            {
                int tmp = r.ReadInt32(); return(*(float *)(&tmp));
            }
            if (type == 'I')
            {
                return(r.ReadInt32());
            }
            if (type == 'J')
            {
                return(r.ReadInt64());
            }
            if (type == 'S')
            {
                return(r.ReadInt16());
            }
            if (type == 'Z')
            {
                return(r.ReadUInt8() != 0);
            }
            if (type == 'L')
            {
                return(ReadObject(r));
            }
            if (type == '[')
            {
                return(ReadObject(r));
            }

            throw new InvalidDataException("Invalid value code: " + type);
        }
Example #4
0
        static object PrevObject(DatReader r)
        {
            int handle = r.ReadInt32() - baseWireHandle;

            if (handle >= 0 && handle < r.handles.Count)
            {
                return(r.handles[handle]);
            }
            throw new InvalidDataException("Invalid stream handle: " + handle);
        }
Example #5
0
 static object ReadContent(DatReader r, byte typeCode)
 {
     if (typeCode == TC_BLOCKDATA)
     {
         return(r.ReadBytes(r.ReadUInt8()));
     }
     else if (typeCode == TC_BLOCKDATALONG)
     {
         return(r.ReadBytes(r.ReadInt32()));
     }
     else
     {
         return(ReadObject(r, typeCode));
     }
 }
Example #6
0
        static JArray NewArray(DatReader r)
        {
            JArray array = new JArray();

            array.Desc = ClassDesc(r);
            r.handles.Add(array);
            char type = array.Desc.Name[1];
            int  size = r.ReadInt32();

            if (type == 'B')
            {
                array.Values = r.ReadBytes(size);
            }
            else
            {
                object[] values = new object[size];
                for (int i = 0; i < values.Length; i++)
                {
                    values[i] = Value(r, type);
                }
                array.Values = values;
            }
            return(array);
        }