コード例 #1
0
ファイル: McLevelLoading.cs プロジェクト: welterde/Spacecraft
        public static Map Load(string filename)
        {
            StreamReader RawReader = new StreamReader(filename);
            GZipStream Reader = new GZipStream(RawReader.BaseStream, CompressionMode.Decompress);

            BinaryTag Tree = NbtParser.ParseTagStream(Reader);

            Reader.Close();
            RawReader.Close();

            BinaryTag MapTag  = new BinaryTag(){ Payload = null, };

            // Find the MapTag data.
            foreach (var Item in (BinaryTag[])Tree.Payload)
            {
                if (Item.Name == "Map")
                {
                    MapTag = Item;
                    break;
                }
            }

            if (!(MapTag.Payload is BinaryTag[]))
                throw new IOException("Map tree did not or contained invalid Map tag!");

            BinaryTag[] Items = MapTag.Payload as BinaryTag[];

            Map MapInProgress = new Map();

            foreach (var Item in Items)
            {
                switch (Item.Name)
                {
                    case "Width":
                        MapInProgress.xdim = (short)Item.Payload;
                        break;
                    case "Height":
                        MapInProgress.ydim = (short) Item.Payload;
                        break;
                    case "Length":
                        MapInProgress.zdim = (short)Item.Payload;
                        break;
                    case "Blocks":
                        MapInProgress.CopyBlocks((byte[])Item.Payload, 0);
                        break;
                    case "Spawn":
                        BinaryTag[] List = (BinaryTag[]) Item.Payload;
                        short x = (short) List[0].Payload;
                        short y = (short)List[1].Payload;
                        short z = (short)List[2].Payload;

                        x *= 32;
                        y *= 32;
                        z *= 32;

                        MapInProgress.SetSpawn(new Position(x, y, z), 0);

                        break;
                    default:
                        break;
                }
            }

            // We do our own optimizations.
            MapInProgress.ReplaceAll(Block.StillWater, Block.Water, -1);
            MapInProgress.ReplaceAll(Block.StillLava, Block.Lava, -1);

            return MapInProgress;
        }
コード例 #2
0
        public static Map Load(string fileName)
        {
            Spacecraft.Log("Converting " + fileName);
            byte[] temp = new byte[8];
            Map map = new Map();
            byte[] data;
            int length;
            try {
                using(FileStream stream = File.OpenRead(fileName)) {
                    stream.Seek(-4, SeekOrigin.End);
                    stream.Read(temp, 0, sizeof(int));
                    stream.Seek(0, SeekOrigin.Begin);
                    length = BitConverter.ToInt32(temp, 0);
                    data = new byte[length];
                    using( GZipStream reader = new GZipStream(stream, CompressionMode.Decompress)) {
                        reader.Read(data, 0, length);
                    }
                }

                //if( data[0] == 0xBE && data[1] == 0xEE && data[2] == 0xEF ) {
                for( int i = 0; i < length - 1; i++ ) {
                    if( data[i] == 0xAC && data[i + 1] == 0xED ) {

                        // bypassing the header crap
                        int pointer = i + 6;
                        Array.Copy( data, pointer, temp, 0, sizeof( short ) );
                        pointer += IPAddress.HostToNetworkOrder( BitConverter.ToInt16( temp, 0 ) );
                        pointer += 13;

                        int headerEnd = 0;
                        // find the end of serialization listing
                        for( headerEnd = pointer; headerEnd < data.Length - 1; headerEnd++ ) {
                            if( data[headerEnd] == 0x78 && data[headerEnd + 1] == 0x70 ) {
                                headerEnd += 2;
                                break;
                            }
                        }

                        // start parsing serialization listing
                        int offset = 0;
                        while( pointer < headerEnd ) {
                            if( data[pointer] == 'Z' ) offset++;
                            else if( data[pointer] == 'I' || data[pointer] == 'F' ) offset += 4;
                            else if( data[pointer] == 'J' ) offset += 8;

                            pointer += 1;
                            Array.Copy( data, pointer, temp, 0, sizeof( short ) );
                            short skip = IPAddress.HostToNetworkOrder( BitConverter.ToInt16( temp, 0 ) );
                            pointer += 2;

                            // look for relevant variables
                            Array.Copy( data, headerEnd + offset - 4, temp, 0, sizeof( int ) );
                            if( MemCmp( data, pointer, "width" ) ) {
                                map.xdim = (short)IPAddress.HostToNetworkOrder( BitConverter.ToInt32( temp, 0 ) );
                            } else if( MemCmp( data, pointer, "depth" ) ) {
                                map.ydim = (short)IPAddress.HostToNetworkOrder( BitConverter.ToInt32( temp, 0 ) );
                            } else if( MemCmp( data, pointer, "height" ) ) {
                                map.zdim = (short)IPAddress.HostToNetworkOrder( BitConverter.ToInt32( temp, 0 ) );
                            } else if( MemCmp( data, pointer, "xSpawn" ) ) {
                                map.spawn.x = (short)(IPAddress.HostToNetworkOrder( BitConverter.ToInt32( temp, 0 ) )*32+16);
                            } else if( MemCmp( data, pointer, "ySpawn" ) ) {
                                map.spawn.y = (short)(IPAddress.HostToNetworkOrder( BitConverter.ToInt32( temp, 0 ) ) * 32 + 16);
                            } else if( MemCmp( data, pointer, "zSpawn" ) ) {
                                map.spawn.z = (short)(IPAddress.HostToNetworkOrder( BitConverter.ToInt32( temp, 0 ) ) * 32 + 16);
                            }

                            pointer += skip;
                        }

                        // find the start of the block array
                        bool foundBlockArray = false;
                        offset = Array.IndexOf<byte>( data, 0x00, headerEnd );
                        while( offset != -1 && offset < data.Length - 2 ) {
                            if( data[offset] == 0x00 && data[offset + 1] == 0x78 && data[offset + 2] == 0x70 ) {
                                foundBlockArray = true;
                                pointer = offset + 7;
                            }
                            offset = Array.IndexOf<byte>( data, 0x00, offset + 1 );
                        }

                        // copy the block array... or fail
                        if( foundBlockArray ) {
                            map.CopyBlocks( data, pointer );
                            if( !map.ValidateBlockTypes() ) {
                                throw new Exception( "Map validation failed: unknown block types found. Either parsing has done wrong, or this is an incompatible format." );
                            }
                        } else {
                            throw new Exception( "Could not locate block array." );
                        }
                        break;
                    }
                }
            } catch( Exception ex ) {
                Spacecraft.LogError("Conversion failed", ex);
                return null;
            }

            // replace all still fluids with their normal equivalents
            // vanilla server does optimizations that make most fluids still when nothing is happening nearby
            map.ReplaceAll(Block.StillLava, Block.Lava, map.Length);
            map.ReplaceAll(Block.StillWater, Block.Water, map.Length);

            map.Save(Map.levelName);
            Spacecraft.Log("Conversion completed succesfully");
            return map;
        }