/// <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); } } }
private static void ProcessFiles(string src, string dst, bool singleFile) { string[] inFiles = singleFile ? new[] { src } : Directory.GetFiles(src, "*.emf"); for (int map = 0; map < inFiles.Length; ++map) { MapFile EMF = new MapFile(inFiles[map]); bool changesMade = false; string lastPart = inFiles[map].Substring(inFiles[map].Contains('\\') ? inFiles[map].LastIndexOf('\\') + 1 : 0, inFiles[map].Length - inFiles[map].LastIndexOf('\\') - 1); for (int i = EMF.TileRows.Count - 1; i >= 0; --i) { TileRow tr = EMF.TileRows[i]; for (int j = tr.tiles.Count - 1; j >= 0; --j) { Tile tt = tr.tiles[j]; if (tt.x > EMF.Width || tr.y > EMF.Height) { Console.WriteLine("[MAP {3}] Tile {0}x{1} ({2}) is out of map bounds. Removing.", tt.x, tr.y, Enum.GetName(typeof(TileSpec), tt.spec), lastPart); tr.tiles.RemoveAt(j); changesMade = true; } } } for (int i = EMF.WarpRows.Count - 1; i >= 0; --i) { WarpRow tr = EMF.WarpRows[i]; for (int j = tr.tiles.Count - 1; j >= 0; --j) { Warp tt = tr.tiles[j]; if (tt.x > EMF.Width || tr.y > EMF.Height) { Console.WriteLine("[MAP {2}] Warp {0}x{1} is out of map bounds. Removing.", tt.x, tr.y, lastPart); tr.tiles.RemoveAt(j); changesMade = true; } } } for (int i = EMF.NPCSpawns.Count - 1; i >= 0; --i) { NPCSpawn npc = EMF.NPCSpawns[i]; NPCRecord npcRec = (NPCRecord)ENF.Data.Find(rec => ((NPCRecord)rec).ID == npc.id); if (npc.id > ENF.Data.Count || npcRec == null) { Console.WriteLine("[MAP {0}] NPC Spawn {1}x{2} uses non-existent NPC #{3}. Removing.", lastPart, npc.x, npc.y, npc.id); EMF.NPCSpawns.RemoveAt(i); changesMade = true; continue; } if (npc.x > EMF.Width || npc.y > EMF.Height) { Console.WriteLine("[MAP {0}] NPC Spawn {1}x{2} ({3}) is out of map bounds. Removing.", lastPart, npc.x, npc.y, npcRec.Name); EMF.NPCSpawns.RemoveAt(i); changesMade = true; continue; } if (!CheckTile(EMF, npc.x, npc.y)) { Console.WriteLine("[MAP {0}] NPC Spawn {1}x{2} ({3}) is invalid...", lastPart, npc.x, npc.y, npcRec.Name); bool found = false; for (int row = npc.y - 2; row < npc.y + 2; ++row) { if (found) { break; } for (int col = npc.x - 2; col < npc.x + 2; ++col) { if (found) { break; } if (CheckTile(EMF, col, row)) { Console.WriteLine("[MAP {0}] Found valid spawn point. Continuing.", lastPart); found = true; } } } if (!found) { Console.WriteLine("[MAP {0}] NPC couldn't spawn anywhere valid! Removing."); EMF.NPCSpawns.RemoveAt(i); changesMade = true; } } } for (int i = EMF.Chests.Count - 1; i >= 0; --i) { MapChest chest = EMF.Chests[i]; ItemRecord rec = EIF.GetItemRecordByID(chest.item); if (chest.item > EIF.Data.Count || rec == null) { Console.WriteLine("[MAP {0}] Chest Spawn {1}x{2} uses non-existent Item #{3}. Removing.", lastPart, chest.x, chest.y, chest.item); EMF.Chests.RemoveAt(i); changesMade = true; continue; } if (chest.x > EMF.Width || chest.y > EMF.Height || (EMF.TileLookup[chest.y, chest.x] ?? new Tile { spec = TileSpec.Wall }).spec != TileSpec.Chest) { Console.WriteLine("[MAP {0}] Chest Spawn {1}x{2} points to a non-chest. Removing.", lastPart, chest.x, chest.y); EMF.Chests.RemoveAt(i); changesMade = true; } } if (!changesMade) { Console.WriteLine("Map {0} processed without any errors. No changes made.", lastPart); continue; } if (map == 0 && singleFile && inFiles.Length == 1) { EMF.Save(dst); break; } EMF.Save(Path.Combine(dst, lastPart)); } }