Example #1
0
        public void ReadLocations(Stream st, bool readTimeStamps = true)
        {
            RegionFile_Location tempLocation = new RegionFile_Location();
            BinaryReader        br           = new BinaryReader(st);

            for (int i = 0; i < 1024; i++)
            {
                tempLocation.offset      = (short)BinaryJava.Read3byteInt(br);
                tempLocation.sectorCount = br.ReadByte();
                if (tempLocation.sectorCount != 0)
                {
                    locations.Add(tempLocation);
                }

                tempLocation = new RegionFile_Location();
            }
            if (readTimeStamps)
            {
                for (int i = 0; i < 1024; i++)
                {
                    if (tempLocation.sectorCount != 0)
                    {
                        locations[i].timestamp = BinaryJava.ReadInt(br);
                    }
                }
            }
        }
Example #2
0
        public static RegionFile_Chunk GetChunkByLocation(RegionFile_Location location, Stream st)
        {
            RegionFile_Chunk tempChunk = new RegionFile_Chunk();

            BinaryReader br = new BinaryReader(st);
            {
                br.ReadBytes(8192);
                long ichi = br.BaseStream.Position;
                br.BaseStream.Position = location.offset * 4096;
                byte[]       chunkData   = br.ReadBytes(location.sectorCount * 4096);
                BinaryReader chunkDataBR = new BinaryReader(new MemoryStream(chunkData));
                tempChunk.length          = BinaryJava.ReadInt(chunkDataBR);
                tempChunk.compressionType = chunkDataBR.ReadByte();
                if (tempChunk.compressionType == 2)
                {
                    //tempChunk.Data = Deflactor.DeCompress(chunkDataBR.ReadBytes(tempChunk.length)); // chunkData.Skip(5).Take(tempChunk.length).ToArray());
                    //File.WriteAllBytes( "decomBytesNewnewnew",Deflactor.DeCompress(chunkDataBR.ReadBytes(tempChunk.length)));
                    tempChunk.tag = RegionFile_Chunk.ReadTags(Deflactor.DeCompress(chunkDataBR.ReadBytes(tempChunk.length)));
                    chunkDataBR.Dispose();
                    return(tempChunk);
                }
                else
                {
                    throw new Exception("Unknown compression type");
                }
            }
        }
Example #3
0
        /// <summary>
        /// 不要忘了Stream.Close(); !
        /// </summary>
        /// <param name="st"></param>
        public void WriteTo(Stream st)
        {
            BinaryWriter bw = new BinaryWriter(st);

            bw.Write((byte)Convert.ToInt32(type));
            if (type != TagType.TAG_End)
            {
                BinaryJava.WriteUTF(bw, name);
                WritePayload(bw);
            }
        }
Example #4
0
        public static Tag ReadFrom(Stream st)
        {
            BinaryReader br   = new BinaryReader(st);
            byte         type = br.ReadByte();
            Tag          tag  = null;

            if (type == 0)
            {
                tag = new Tag(TagType.TAG_End, null, null);
            }
            else if (Enum.GetValues(typeof(TagType)).Length > type)
            {
                //string name = readUTF(br);
                tag = new Tag(GetTypeByIndex(type), BinaryJava.ReadUTF(br), ReadPayload(br, type));
            }
            br.Close();
            return(tag);
        }
Example #5
0
        public static RegionFile_Location ReadLocation(Stream st, short index, bool readTimeStamps = true)
        {
            RegionFile_Location location = new RegionFile_Location();

            using (BinaryReader br = new BinaryReader(st))
            {
                br.BaseStream.Position = index * 4;
                location.offset        = (short)BinaryJava.Read3byteInt(br);
                location.sectorCount   = br.ReadByte();
                if (readTimeStamps)
                {
                    br.BaseStream.Position = index * 4 + 4096;
                    if (location.sectorCount != 0)
                    {
                        location.timestamp = BinaryJava.ReadInt(br);
                    }
                }
                return(location);
            }
        }
Example #6
0
        private void WritePayload(BinaryWriter bw)
        {
            switch (type)
            {
            case TagType.TAG_End:
                break;

            case TagType.TAG_Byte:
                bw.Write((byte)value);
                break;

            case TagType.TAG_Short:
                BinaryJava.WriteShort(bw, (short)value);
                break;

            case TagType.TAG_Int:
                BinaryJava.WriteInt(bw, (int)value);
                break;

            case TagType.TAG_Long:
                BinaryJava.WriteLong(bw, (long)value);
                break;

            case TagType.TAG_Float:
                BinaryJava.WriteFloat(bw, (float)value);
                break;

            case TagType.TAG_Double:
                BinaryJava.WriteDouble(bw, (double)value);
                break;

            case TagType.TAG_Byte_Array:
                byte[] ba = (byte[])value;
                BinaryJava.WriteInt(bw, ba.Length);
                bw.Write(ba);
                break;

            case TagType.TAG_String:
                BinaryJava.WriteUTF(bw, (string)value);
                break;

            case TagType.TAG_List:
                Tag[] list = (Tag[])value;
                bw.Write((byte)Convert.ToInt32(GetListTagType()));
                BinaryJava.WriteInt(bw, list.Length);
                foreach (Tag tt in list)
                {
                    tt.WritePayload(bw);
                }
                break;

            case TagType.TAG_Compound:
                Tag[] subtags = (Tag[])value;
                foreach (Tag st in subtags)
                {
                    Tag     subtag = st;
                    TagType type   = subtag.GetTagType();
                    bw.Write((byte)Convert.ToInt32(type));
                    if (type != TagType.TAG_End)
                    {
                        BinaryJava.WriteUTF(bw, (subtag.GetName()));
                        subtag.WritePayload(bw);
                    }
                }
                break;

            case TagType.TAG_Int_Array:
                int[] ia = (int[])value;
                BinaryJava.WriteInt(bw, ia.Length);
                for (int i = 0; i < ia.Length; i++)
                {
                    BinaryJava.WriteInt(bw, ia[i]);
                }
                break;

            case TagType.TAG_Long_Array:
                long[] la = (long[])value;
                BinaryJava.WriteInt(bw, la.Length);
                for (int i = 0; i < la.Length; i++)
                {
                    BinaryJava.WriteLong(bw, la[i]);
                }
                break;

            default:
                break;
            }
        }
Example #7
0
        private static object ReadPayload(BinaryReader br, byte type)
        {
            switch (type)
            {
            case 0:
                return(null);

            case 1:
                return(br.ReadByte());

            case 2:
                return(BinaryJava.ReadShort(br));

            case 3:
                return(BinaryJava.ReadInt(br));

            case 4:
                return(BinaryJava.ReadLong(br));;

            case 5:
                return(BinaryJava.ReadFloat(br));

            case 6:
                return(BinaryJava.ReadDouble(br));

            case 7:
                int length = BinaryJava.ReadInt(br);
                return(br.ReadBytes(length));

            case 8:
                return(BinaryJava.ReadUTF(br));

            case 9:
                byte  lt = br.ReadByte();
                int   ll = BinaryJava.ReadInt(br);
                Tag[] lo = new Tag[ll];
                for (int i = 0; i < ll; i++)
                {
                    lo[i] = new Tag(GetTypeByIndex(lt), null, ReadPayload(br, lt));
                }
                if (lo.Length == 0)
                {
                    return(GetTypeByIndex(lt));
                }
                else
                {
                    return(lo);
                }

            case 10:
                byte  stt;
                Tag[] tags = new Tag[0];
                do
                {
                    stt = br.ReadByte();
                    string name = null;
                    if (stt != 0)
                    {
                        name = BinaryJava.ReadUTF(br);
                    }
                    Tag[] newTags = new Tag[tags.Length + 1];
                    Array.Copy(tags, 0, newTags, 0, tags.Length);
                    newTags[tags.Length] = new Tag(GetTypeByIndex(stt), name, ReadPayload(br, stt));
                    tags = newTags;
                } while (stt != 0);
                return(tags);

            case 11:
                int   len = BinaryJava.ReadInt(br);
                int[] ia  = new int[len];
                for (int i = 0; i < len; i++)
                {
                    ia[i] = BinaryJava.ReadInt(br);
                }
                return(ia);

            case 12:
                int    len_ = BinaryJava.ReadInt(br);
                long[] ia_  = new long[len_];
                for (int i = 0; i < len_; i++)
                {
                    ia_[i] = BinaryJava.ReadLong(br);
                }
                return(ia_);

            default:
                break;
            }
            return(null);
        }