public void Write(string fileName) { using (EOFile file = new EOFile(File.Open(fileName, FileMode.Create, FileAccess.Write))) { file.AddString("EMF"); file.AddInt(0); byte[] padName = new byte[24]; for (int i = 0; i < 24; ++i) padName[i] = 0xFF; byte[] encName = EncodeString(name); Array.Resize(ref encName, Math.Min(24, encName.Length)); Array.Copy(encName, 0, padName, 24 - encName.Length, encName.Length); file.AddBytes(padName); file.AddChar((byte)type); file.AddChar((byte)effect); file.AddChar(music); file.AddChar(musicExtra); file.AddShort((short)ambientNoise); file.AddChar(width); file.AddChar(height); file.AddShort((short)fillTile); file.AddChar(mapAvailable); file.AddChar(canScroll); file.AddChar(relogX); file.AddChar(relogY); file.AddChar(unknown); file.AddChar((byte)npcs.Count); foreach (NPC npc in npcs) { file.AddChar(npc.x); file.AddChar(npc.y); file.AddShort((short)npc.id); file.AddChar(npc.spawnType); file.AddShort((short)npc.spawnTime); file.AddChar(npc.amount); } file.AddChar((byte)unknowns.Count); foreach (Unknown unknown_ in unknowns) { file.AddBytes(unknown_.data); } file.AddChar((byte)chests.Count); foreach (Chest chest in chests) { file.AddChar(chest.x); file.AddChar(chest.y); file.AddShort((short)chest.key); file.AddChar(chest.slot); file.AddShort((short)chest.item); file.AddShort((short)chest.time); file.AddThree((int)chest.amount); } file.AddChar((byte)tileRows.Count); foreach (TileRow row in tileRows) { file.AddChar(row.y); file.AddChar((byte)row.tiles.Count); foreach (Tile tile in row.tiles) { file.AddChar(tile.x); file.AddChar((byte)tile.spec); } } file.AddChar((byte)warprows.Count); foreach (WarpRow row in warprows) { file.AddChar(row.y); file.AddChar((byte)row.tiles.Count); foreach (Warp warp in row.tiles) { file.AddChar(warp.x); file.AddShort((short)warp.warpMap); file.AddChar(warp.warpX); file.AddChar(warp.warpY); file.AddChar(warp.levelRequirement); file.AddShort((short)warp.door); } } for (int layer = 0; layer < GFXLayers; ++layer) { file.AddChar((byte)gfxRows[layer].Count); foreach (GFXRow row in gfxRows[layer]) { file.AddChar(row.y); file.AddChar((byte)row.tiles.Count); foreach (GFX gfx in row.tiles) { file.AddChar(gfx.x); file.AddShort((short)gfx.tile); } } } file.AddChar((byte)signs.Count); foreach (Sign sign in signs) { file.AddChar(sign.x); file.AddChar(sign.y); file.AddShort((short)(sign.title.Length + sign.message.Length + 1)); file.AddBytes(EncodeString(sign.title + sign.message)); file.AddChar((byte)sign.title.Length); } revisionId = file.WriteHash(3); } }
/// <summary> /// Read an EMF file /// </summary> /// <param name="fileName">File to read the EMF data from</param> public void Read(string fileName) { int outersize; int innersize; using (EOFile file = new EOFile(File.Open(fileName, FileMode.Open, FileAccess.Read))) { if (file.GetFixedString(3) != "EMF") throw new Exception("Corrupt or not an EMF file"); revisionId = file.GetBytes(4); byte[] rawname = file.GetBytes(24); for (int i = 0; i < 24; ++i) { if (rawname[i] == 0xFF) { Array.Resize(ref rawname, i); break; } } name = DecodeString(rawname); type = (Type)file.GetChar(); effect = (Effect)file.GetChar(); music = file.GetChar(); musicExtra = file.GetChar(); ambientNoise = (ushort)file.GetShort(); width = file.GetChar(); height = file.GetChar(); fillTile = (ushort)file.GetShort(); mapAvailable = file.GetChar(); canScroll = file.GetChar(); relogX = file.GetChar(); relogY = file.GetChar(); unknown = file.GetChar(); outersize = file.GetChar(); for (int i = 0; i < outersize; ++i) { npcs.Add(new NPC() { x = file.GetChar(), y = file.GetChar(), id = (ushort)file.GetShort(), spawnType = file.GetChar(), spawnTime = (ushort)file.GetShort(), amount = file.GetChar() }); } outersize = file.GetChar(); for (int i = 0; i < outersize; ++i) { unknowns.Add(new Unknown() { data = file.GetBytes(5) }); } outersize = file.GetChar(); for (int i = 0; i < outersize; ++i) { chests.Add(new Chest() { x = file.GetChar(), y = file.GetChar(), key = (ushort)file.GetShort(), slot = file.GetChar(), item = (ushort)file.GetShort(), time = (ushort)file.GetShort(), amount = (uint)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) { row.tiles.Add(new Tile() { x = file.GetChar(), spec = (TileSpec)file.GetChar() }); } 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) { row.tiles.Add(new Warp() { x = file.GetChar(), warpMap = (ushort)file.GetShort(), warpX = file.GetChar(), warpY = file.GetChar(), levelRequirement = file.GetChar(), door = (ushort)file.GetShort() }); } warprows.Add(row); } for (int layer = 0; layer < GFXLayers; ++layer) { 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) }; row.tiles = new List<GFX>(innersize); for (int ii = 0; ii < innersize; ++ii) { row.tiles.Add(new GFX() { x = file.GetChar(), tile = (ushort)file.GetShort() }); } gfxRows[layer].Add(row); } } outersize = file.GetChar(); for (int i = 0; i < outersize; ++i) { Sign sign = new Sign(); sign.x = file.GetChar(); sign.y = file.GetChar(); int msgLength = file.GetShort() - 1; string data = DecodeString(file.GetBytes(msgLength)); int titleLength = file.GetChar(); sign.title = data.Substring(0, titleLength); sign.message = data.Substring(titleLength); signs.Add(sign); } } }