Example #1
0
        private void Load(string fileName)
        {
            Rid = new byte[4];
            Name = String.Empty;
            NPCSpawns = new List<NPCSpawn>();
            Unknowns = new List<byte[]>();
            Chests = new List<MapChest>();
            TileRows = new List<TileRow>();
            WarpRows = new List<WarpRow>();
            GfxRows = new List<GFXRow>[(int)MapLayers.NUM_LAYERS];
            Signs = new List<MapSign>();

            for (int layer = (int)MapLayers.GroundTile; layer < (int)MapLayers.NUM_LAYERS; ++layer)
            {
                GfxRows[layer] = new List<GFXRow>();
            }

            Packet file = new Packet(File.ReadAllBytes(fileName));
            if (file.Length == 0)
                throw new FileLoadException("The file is empty.");
            file.ReadPos = 0; //use packet wrapper for convenience functions/decoding

            if (file.GetFixedString(3) != "EMF")
                throw new Exception("Corrupt or not an EMF file");

            FileSize = file.Length;

            Rid = file.GetBytes(4);
            byte[] rawname = file.GetBytes(24);
            Name = _decodeMapString(rawname);

            //account for override in Rid to make a map PK - byte 0 is 0xff and byte 1 is 0x01
            IsPK = (pkByte = file.GetChar()) == 3 || (Rid[0] == 0xff && Rid[1] == 0x01);
            Effect = (MapEffect)file.GetChar();
            Music = file.GetChar();
            MusicExtra = file.GetChar();
            AmbientNoise = file.GetShort();
            Width = file.GetChar();
            Height = file.GetChar();
            FillTile = file.GetShort();
            MapAvailable = (availByte = file.GetChar()) == 1;
            CanScroll = (scrollByte = file.GetChar()) == 1;
            RelogX = file.GetChar();
            RelogY = file.GetChar();
            Unknown2 = file.GetChar();

            WarpLookup = new Warp[Height + 1, Width + 1];
            TileLookup = new Tile[Height + 1, Width + 1];

            int innersize;
            int outersize = file.GetChar();

            for (int i = 0; i < outersize; ++i)
            {
                NPCSpawns.Add(new NPCSpawn
                {
                    x = file.GetChar(),
                    y = file.GetChar(),
                    id = file.GetShort(),
                    spawnType = file.GetChar(),
                    spawnTime = file.GetShort(),
                    amount = file.GetChar()
                });
            }

            outersize = file.GetChar();

            for (int i = 0; i < outersize; ++i)
            {
                Unknowns.Add(file.GetBytes(4));
            }

            outersize = file.GetChar();

            for (int i = 0; i < outersize; ++i)
            {
                Chests.Add(new MapChest
                {
                    x = file.GetChar(),
                    y = file.GetChar(),
                    key = (ChestKey)file.GetShort(),
                    slot = file.GetChar(),
                    item = file.GetShort(),
                    time = file.GetShort(),
                    amount = file.GetThree()
                });
            }

            outersize = file.GetChar();

            for (int i = 0; i < outersize; ++i)
            {
                byte y = file.GetChar();
                innersize = file.GetChar();

                TileRow row = new TileRow
                {
                    y = y,
                    tiles = new List<Tile>(innersize)
                };

                for (int ii = 0; ii < innersize; ++ii)
                {
                    Tile t;
                    row.tiles.Add(t = new Tile
                    {
                        x = file.GetChar(),
                        spec = (TileSpec)file.GetChar()
                    });
                    TileLookup[row.y, t.x] = t;
                }

                TileRows.Add(row);
            }

            outersize = file.GetChar();

            for (int i = 0; i < outersize; ++i)
            {
                byte y = file.GetChar();
                innersize = file.GetChar();

                WarpRow row = new WarpRow { y = y, tiles = new List<Warp>(innersize) };

                for (int ii = 0; ii < innersize; ++ii)
                {
                    Warp w;
                    row.tiles.Add(w = new Warp
                    {
                        x = file.GetChar(),
                        warpMap = file.GetShort(),
                        warpX = file.GetChar(),
                        warpY = file.GetChar(),
                        levelRequirement = file.GetChar(),
                        door = (DoorSpec)file.GetShort()
                    });

                    if (row.y <= Height && w.x <= Width)
                        WarpLookup[row.y, w.x] = w;
                }

                WarpRows.Add(row);
            }

            GFXLookup = new List<int[,]>((int)MapLayers.NUM_LAYERS);
            for (int i = 0; i < (int)MapLayers.NUM_LAYERS; ++i)
            {
                GFXLookup.Add(new int[Height + 1, Width + 1]);
                for (int row = 0; row <= Height; row++)
                {
                    for (int col = 0; col <= Width; col++)
                    {
                        GFXLookup[i][row, col] = -1;
                    }
                }
            }

            for (int layer = 0; layer < (int)MapLayers.NUM_LAYERS; ++layer)
            {
                if (file.ReadPos == file.Length)
                    break;

                outersize = file.GetChar();
                GfxRows[layer] = new List<GFXRow>(outersize);

                for (int i = 0; i < outersize; ++i)
                {
                    byte y = file.GetChar();
                    innersize = file.GetChar();

                    GFXRow row = new GFXRow { y = y, tiles = new List<GFX>(innersize) };

                    for (int ii = 0; ii < innersize; ++ii)
                    {
                        byte tempx = file.GetChar();
                        ushort temptile = (ushort)file.GetShort();
                        if (row.y <= Height && tempx <= Width)
                        {
                            GFXLookup[layer][row.y, tempx] = temptile;
                        }
                        row.tiles.Add(new GFX { x = tempx, tile = temptile });
                    }

                    GfxRows[layer].Add(row);
                }
            }

            if (file.ReadPos == file.Length)
                return;

            outersize = file.GetChar();

            for (int i = 0; i < outersize; ++i)
            {
                MapSign sign = new MapSign {x = file.GetChar(), y = file.GetChar()};

                int msgLength = file.GetShort() - 1;
                string data = _decodeMapString(file.GetBytes(msgLength));
                int titleLength = file.GetChar();
                sign.title = data.Substring(0, titleLength);
                sign.message = data.Substring(titleLength);

                Signs.Add(sign);
            }
        }
Example #2
0
        private void Load(string fileName)
        {
            Rid       = new byte[4];
            Name      = String.Empty;
            NPCSpawns = new List <NPCSpawn>();
            Unknowns  = new List <byte[]>();
            Chests    = new List <MapChest>();
            TileRows  = new List <TileRow>();
            WarpRows  = new List <WarpRow>();
            GfxRows   = new List <GFXRow> [(int)MapLayers.NUM_LAYERS];
            Signs     = new List <MapSign>();

            for (int layer = (int)MapLayers.GroundTile; layer < (int)MapLayers.NUM_LAYERS; ++layer)
            {
                GfxRows[layer] = new List <GFXRow>();
            }

            Packet file = new Packet(File.ReadAllBytes(fileName));

            if (file.Length == 0)
            {
                throw new FileLoadException("The file is empty.");
            }
            file.ReadPos = 0;             //use packet wrapper for convenience functions/decoding

            if (file.GetFixedString(3) != "EMF")
            {
                throw new Exception("Corrupt or not an EMF file");
            }

            FileSize = file.Length;

            Rid = file.GetBytes(4);
            byte[] rawname = file.GetBytes(24);
            Name = _decodeMapString(rawname);

            IsPK         = (pkByte = file.GetChar()) == 3;
            Effect       = (MapEffect)file.GetChar();
            Music        = file.GetChar();
            MusicExtra   = file.GetChar();
            AmbientNoise = file.GetShort();
            Width        = file.GetChar();
            Height       = file.GetChar();
            FillTile     = file.GetShort();
            MapAvailable = (availByte = file.GetChar()) == 1;
            CanScroll    = (scrollByte = file.GetChar()) == 1;
            RelogX       = file.GetChar();
            RelogY       = file.GetChar();
            Unknown2     = file.GetChar();

            WarpLookup = new Warp[Height + 1, Width + 1];
            TileLookup = new Tile[Height + 1, Width + 1];

            int innersize;
            int outersize = file.GetChar();

            for (int i = 0; i < outersize; ++i)
            {
                NPCSpawns.Add(new NPCSpawn
                {
                    x         = file.GetChar(),
                    y         = file.GetChar(),
                    id        = file.GetShort(),
                    spawnType = file.GetChar(),
                    spawnTime = file.GetShort(),
                    amount    = file.GetChar()
                });
            }

            outersize = file.GetChar();

            for (int i = 0; i < outersize; ++i)
            {
                Unknowns.Add(file.GetBytes(4));
            }

            outersize = file.GetChar();

            for (int i = 0; i < outersize; ++i)
            {
                Chests.Add(new MapChest
                {
                    x      = file.GetChar(),
                    y      = file.GetChar(),
                    key    = (ChestKey)file.GetShort(),
                    slot   = file.GetChar(),
                    item   = file.GetShort(),
                    time   = file.GetShort(),
                    amount = file.GetThree()
                });
            }

            outersize = file.GetChar();

            for (int i = 0; i < outersize; ++i)
            {
                byte y = file.GetChar();
                innersize = file.GetChar();

                TileRow row = new TileRow
                {
                    y     = y,
                    tiles = new List <Tile>(innersize)
                };

                for (int ii = 0; ii < innersize; ++ii)
                {
                    Tile t;
                    row.tiles.Add(t = new Tile
                    {
                        x    = file.GetChar(),
                        spec = (TileSpec)file.GetChar()
                    });
                    TileLookup[row.y, t.x] = t;
                }

                TileRows.Add(row);
            }

            outersize = file.GetChar();

            for (int i = 0; i < outersize; ++i)
            {
                byte y = file.GetChar();
                innersize = file.GetChar();

                WarpRow row = new WarpRow {
                    y = y, tiles = new List <Warp>(innersize)
                };

                for (int ii = 0; ii < innersize; ++ii)
                {
                    Warp w;
                    row.tiles.Add(w = new Warp
                    {
                        x                = file.GetChar(),
                        warpMap          = file.GetShort(),
                        warpX            = file.GetChar(),
                        warpY            = file.GetChar(),
                        levelRequirement = file.GetChar(),
                        door             = (DoorSpec)file.GetShort()
                    });

                    if (row.y <= Height && w.x <= Width)
                    {
                        WarpLookup[row.y, w.x] = w;
                    }
                }

                WarpRows.Add(row);
            }

            GFXLookup = new List <int[, ]>((int)MapLayers.NUM_LAYERS);
            for (int i = 0; i < (int)MapLayers.NUM_LAYERS; ++i)
            {
                GFXLookup.Add(new int[Height + 1, Width + 1]);
                for (int row = 0; row <= Height; row++)
                {
                    for (int col = 0; col <= Width; col++)
                    {
                        GFXLookup[i][row, col] = -1;
                    }
                }
            }

            for (int layer = 0; layer < (int)MapLayers.NUM_LAYERS; ++layer)
            {
                if (file.ReadPos == file.Length)
                {
                    break;
                }

                outersize      = file.GetChar();
                GfxRows[layer] = new List <GFXRow>(outersize);

                for (int i = 0; i < outersize; ++i)
                {
                    byte y = file.GetChar();
                    innersize = file.GetChar();

                    GFXRow row = new GFXRow {
                        y = y, tiles = new List <GFX>(innersize)
                    };

                    for (int ii = 0; ii < innersize; ++ii)
                    {
                        byte   tempx    = file.GetChar();
                        ushort temptile = (ushort)file.GetShort();
                        if (row.y <= Height && tempx <= Width)
                        {
                            GFXLookup[layer][row.y, tempx] = temptile;
                        }
                        row.tiles.Add(new GFX {
                            x = tempx, tile = temptile
                        });
                    }

                    GfxRows[layer].Add(row);
                }
            }

            if (file.ReadPos == file.Length)
            {
                return;
            }

            outersize = file.GetChar();

            for (int i = 0; i < outersize; ++i)
            {
                MapSign sign = new MapSign {
                    x = file.GetChar(), y = file.GetChar()
                };

                int    msgLength   = file.GetShort() - 1;
                string data        = _decodeMapString(file.GetBytes(msgLength));
                int    titleLength = file.GetChar();
                sign.title   = data.Substring(0, titleLength);
                sign.message = data.Substring(titleLength);

                Signs.Add(sign);
            }
        }