Esempio n. 1
0
        static Map LoadFCM(World _world, string fileName)
        {
            FileStream fs  = null;
            Map        map = new Map(_world);

            try {
                fs = File.OpenRead(fileName);
                if (map.ReadHeader(fs))
                {
                    map.ReadMetadata(fs);
                    map.ReadBlocks(fs);
                    if (!map.ValidateBlockTypes())
                    {
                        throw new Exception("Invalid block types detected. File is possibly corrupt.");
                    }
                    _world.log.Log("Loaded map succesfully from {0}", LogType.SystemActivity, fileName);
                    return(map);
                }
                else
                {
                    return(null);
                }
            } catch (EndOfStreamException) {
                _world.log.Log("Map.Load: Unexpected end of file - possible corruption!", LogType.Error);
                return(null);
            } catch (Exception ex) {
                _world.log.Log("Map.Load: Error trying to read from \"{0}\": {1}", LogType.Error, fileName, ex.Message);
                return(null);
            } finally {
                if (fs != null)
                {
                    fs.Close();
                }
            }
        }
Esempio n. 2
0
        public static Map Load(World world, string fileName)
        {
            world.log.Log("Converting {0}...", LogType.SystemActivity, fileName);
            byte[] temp = new byte[8];
            Map    map  = new Map(world);

            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 += Server.htons(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 = Server.htons(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.widthX = (ushort)Server.htons(BitConverter.ToInt32(temp, 0));
                            }
                            else if (MemCmp(data, pointer, "depth"))
                            {
                                map.height = (ushort)Server.htons(BitConverter.ToInt32(temp, 0));
                            }
                            else if (MemCmp(data, pointer, "height"))
                            {
                                map.widthY = (ushort)Server.htons(BitConverter.ToInt32(temp, 0));
                            }
                            else if (MemCmp(data, pointer, "xSpawn"))
                            {
                                map.spawn.x = (short)(Server.htons(BitConverter.ToInt32(temp, 0)) * 32 + 16);
                            }
                            else if (MemCmp(data, pointer, "ySpawn"))
                            {
                                map.spawn.h = (short)(Server.htons(BitConverter.ToInt32(temp, 0)) * 32 + 16);
                            }
                            else if (MemCmp(data, pointer, "zSpawn"))
                            {
                                map.spawn.y = (short)(Server.htons(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) {
                world.log.Log("Conversion failed: {0}", LogType.Error, ex.Message);
                world.log.Log(ex.StackTrace, LogType.Debug);
                return(null);
            }
            map.Save();
            world.log.Log("Conversion completed succesfully succesful.", LogType.SystemActivity, fileName);
            return(map);
        }
Esempio n. 3
0
        public Map Load( Stream MapStream )
        {
            // Reset the seeker to the front of the stream
            // This should probably be done differently.
            MapStream.Seek( 0, SeekOrigin.Begin );

            Map m = new Map();

            // Setup a GZipStream to decompress and read the map file
            using ( GZipStream gs = new GZipStream( MapStream, CompressionMode.Decompress, true ) ) {
                BinaryReader bs = new BinaryReader( gs );

                // Read in the magic number
                if ( bs.ReadByte() != 0xbe || bs.ReadByte() != 0xee || bs.ReadByte() != 0xef ) {
                    throw new FormatException( "MinerCPP map header is incorrect." );
                }

                // Read in the map dimesions
                // Saved in big endian for who-know-what reason.
                // XYZ(?)
                m.widthX = IPAddress.NetworkToHostOrder( bs.ReadInt16() );
                m.height = IPAddress.NetworkToHostOrder( bs.ReadInt16() );
                m.widthY = IPAddress.NetworkToHostOrder( bs.ReadInt16() );

                // Read in the spawn location
                // XYZ(?)
                m.spawn.x = IPAddress.NetworkToHostOrder( bs.ReadInt16() );
                m.spawn.h = IPAddress.NetworkToHostOrder( bs.ReadInt16() );
                m.spawn.y = IPAddress.NetworkToHostOrder( bs.ReadInt16() );

                // Read in the spawn orientation
                m.spawn.r = bs.ReadByte();
                m.spawn.l = bs.ReadByte();

                // Skip over the block count, totally useless
                bs.ReadInt32();

                // Read in the map data
                m.blocks = bs.ReadBytes( m.GetBlockCount() );
                if ( !m.ValidateBlockTypes( true ) ) {
                    throw new Exception( "Unrecognized block types in the map." );
                }
            }

            return m;
        }
Esempio n. 4
0
        public Map Load( Stream MapStream )
        {
            byte[] temp = new byte[8];
            Map map = new Map();
            byte[] data;
            int length;
            try {
                MapStream.Seek( -4, SeekOrigin.End );
                MapStream.Read( temp, 0, sizeof( int ) );
                MapStream.Seek( 0, SeekOrigin.Begin );
                length = BitConverter.ToInt32( temp, 0 );
                data = new byte[length];
                using( GZipStream reader = new GZipStream( MapStream, CompressionMode.Decompress, true ) ) {
                    reader.Read( data, 0, length );
                }

                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 += Server.SwapBytes( 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 = Server.SwapBytes( 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.widthX = (ushort)Server.SwapBytes( BitConverter.ToInt32( temp, 0 ) );
                            } else if( MemCmp( data, pointer, "depth" ) ) {
                                map.height = (ushort)Server.SwapBytes( BitConverter.ToInt32( temp, 0 ) );
                            } else if( MemCmp( data, pointer, "height" ) ) {
                                map.widthY = (ushort)Server.SwapBytes( BitConverter.ToInt32( temp, 0 ) );
                            } else if( MemCmp( data, pointer, "xSpawn" ) ) {
                                map.spawn.x = (short)(Server.SwapBytes( BitConverter.ToInt32( temp, 0 ) ) * 32 + 16);
                            } else if( MemCmp( data, pointer, "ySpawn" ) ) {
                                map.spawn.h = (short)(Server.SwapBytes( BitConverter.ToInt32( temp, 0 ) ) * 32 + 16);
                            } else if( MemCmp( data, pointer, "zSpawn" ) ) {
                                map.spawn.y = (short)(Server.SwapBytes( 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( true ) ) {
                                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 ) {
                Logger.Log( "Conversion failed: {0}", LogType.Error, ex.Message );
                Logger.Log( ex.StackTrace, LogType.Debug );
                return null;
            }

            return map;
        }
Esempio n. 5
0
        public Map Load( Stream MapStream )
        {
            MapStream.Seek( 0, SeekOrigin.Begin );
            GZipStream gs = new GZipStream( MapStream, CompressionMode.Decompress, true );
            NBTag tag = NBTag.ReadStream( gs );

            Map map = new Map();

            NBTag mapTag = tag["Map"];
            map.widthX = mapTag["Width"].GetShort();
            map.height = mapTag["Height"].GetShort();
            map.widthY = mapTag["Length"].GetShort();

            if( !map.ValidateHeader() ) {
                throw new Exception( "One or more of the map dimensions are invalid." );
            }

            map.blocks = mapTag["Blocks"].GetBytes();
            map.ValidateBlockTypes( false );

            map.spawn.x = mapTag["Spawn"][0].GetShort();
            map.spawn.h = mapTag["Spawn"][1].GetShort();
            map.spawn.y = mapTag["Spawn"][2].GetShort();
            map.spawn.r = 0;
            map.spawn.l = 0;

            return map;
        }