-
Notifications
You must be signed in to change notification settings - Fork 16
/
Program.cs
314 lines (276 loc) · 12.7 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
using System;
using System.IO;
using AutomaticTypeMapper;
using EOLib.IO.Actions;
using EOLib.IO.Map;
using EOLib.IO.Repositories;
using EOLib.IO.Services;
namespace BatchMap
{
public static class Program
{
private static ITypeRegistry _typeRegistry;
private static IPubFileProvider _pubProvider;
private static IMapFileProvider _mapFileProvider;
private static void Main(string[] args)
{
if (args.Length != 3)
{
Console.WriteLine("Usage: BatchMap.exe <srcmap|srcdir> <dstmap|dstdir> <pubdir>");
return;
}
var srcFilePath = args[0];
var dstFilePath = args[1];
var pubFilePath = args[2];
var singleFileProcess = false;
if (srcFilePath.ToLower().EndsWith(".emf") && !dstFilePath.ToLower().EndsWith(".emf"))
{
Console.WriteLine("Invalid: single map cannot be processed into output directory. Specify destination emf file.");
return;
}
if (dstFilePath.ToLower().EndsWith(".emf") && !srcFilePath.ToLower().EndsWith(".emf"))
{
Console.WriteLine("Invalid: map directory cannot be processed into single output map. Specify destination output directory.");
return;
}
if (srcFilePath.ToLower().EndsWith(".emf") && dstFilePath.ToLower().EndsWith(".emf"))
{
singleFileProcess = true;
if (!File.Exists(srcFilePath))
{
Console.WriteLine("Invalid input: input file does not exist!");
return;
}
if (File.Exists(dstFilePath))
{
char inp;
do
{
Console.Write("Destination file exists. Overwrite? [y/n] ");
string input = Console.ReadLine() ?? "";
inp = input.Length > 0 ? input[0] : ' ';
} while (inp != 'y' && inp != 'n' && inp != 'Y' && inp != 'N');
if (inp == 'n' || inp == 'N')
{
Console.WriteLine("Will not overwrite existing file. Exiting.");
return;
}
}
}
else
{
if (!Directory.Exists(srcFilePath) || Directory.GetFiles(srcFilePath, "*.emf").Length == 0)
{
Console.WriteLine("Invalid input: source directory does not exist or is missing maps!");
return;
}
if (Directory.Exists(dstFilePath) && Directory.GetFiles(dstFilePath, "*.emf").Length > 0)
{
char inp;
do
{
Console.WriteLine("Destination directory contains emf files. Overwrite? [y/n] ");
string input = Console.ReadLine() ?? "";
inp = input.Length > 0 ? input[0] : ' ';
} while (inp != 'y' && inp != 'n' && inp != 'Y' && inp != 'N');
if (inp == 'n' || inp == 'N')
{
Console.WriteLine("Will not overwrite existing files. Exiting.");
return;
}
}
else if (!Directory.Exists(dstFilePath))
{
Directory.CreateDirectory(dstFilePath);
}
}
SetupDependencies();
try
{
var actions = _typeRegistry.Resolve<IPubFileLoadActions>();
actions.LoadItemFileByName(Path.Combine(pubFilePath, "dat001.eif"));
actions.LoadNPCFileByName(Path.Combine(pubFilePath, "dtn001.enf"));
}
catch
{
Console.WriteLine("Error loading pub files!");
_typeRegistry.Dispose();
return;
}
try
{
ProcessFiles(srcFilePath, dstFilePath, singleFileProcess);
}
catch (Exception ex)
{
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Exception was thrown: ");
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine(ex.Message);
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("\nCall stack: \n");
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine(ex.StackTrace);
}
_typeRegistry.Dispose();
}
private static void SetupDependencies()
{
_typeRegistry = new UnityRegistry("EOLib.IO");
_typeRegistry.RegisterDiscoveredTypes();
_pubProvider = _typeRegistry.Resolve<IPubFileProvider>();
_mapFileProvider = _typeRegistry.Resolve<IMapFileProvider>();
}
private static void ProcessFiles(string src, string dst, bool singleFile)
{
var mapFileLoadActions = _typeRegistry.Resolve<IMapFileLoadActions>();
var mapFileSaveService = _typeRegistry.Resolve<IMapFileSaveService>();
var inFiles = singleFile ? new[] {src} : Directory.GetFiles(src, "*.emf");
for (int mapIndex = 0; mapIndex < inFiles.Length; ++mapIndex)
{
var mapID = new MapPathToIDConverter().ConvertFromPathToID(inFiles[mapIndex]);
mapFileLoadActions.LoadMapFileByName(inFiles[mapIndex]);
var mapFile = _mapFileProvider.MapFiles[mapID];
var changesMade = false;
//todo: find way to store actual input data, since invalid tiles/warps will be auto-removed
//for (int i = mapFile.TileRows.Count - 1; i >= 0; --i)
//{
// var tr = mapFile.TileRows[i];
// for (int j = tr.EntityItems.Count - 1; j >= 0; --j)
// {
// var tt = tr.EntityItems[j];
// if (tt.X > mapFile.Properties.Width || tr.Y > mapFile.Properties.Height)
// {
// Console.WriteLine("[MAP {0}] Tile {1}x{2} ({3}) is out of map bounds. Removing.",
// mapID, tt.X, tr.Y, Enum.GetName(typeof(TileSpec), tt.Value));
// mapFile.RemoveTileAt(tr.Y, tt.X);
// changesMade = true;
// }
// }
//}
//for (int i = mapFile.WarpRows.Count - 1; i >= 0; --i)
//{
// var tr = mapFile.WarpRows[i];
// for (int j = tr.EntityItems.Count - 1; j >= 0; --j)
// {
// var tt = tr.EntityItems[j];
// if (tt.X > mapFile.Properties.Width || tr.Y > mapFile.Properties.Height)
// {
// Console.WriteLine("[MAP {0}] Warp {1}x{2} is out of map bounds. Removing.", mapID, tt.X, tr.Y);
// mapFile.RemoveWarpAt(tr.Y, tt.X);
// changesMade = true;
// }
// }
//}
for(int ndx = mapFile.NPCSpawns.Count - 1; ndx >= 0; --ndx)
{
var npcSpawn = mapFile.NPCSpawns[ndx];
var npcRec = _pubProvider.ENFFile[npcSpawn.ID];
if (npcSpawn.ID > _pubProvider.ENFFile.Length || npcRec == null)
{
Console.WriteLine("[MAP {0}] NPC Spawn {1}x{2} uses non-existent NPC #{3}. Removing.", mapID, npcSpawn.X, npcSpawn.Y, npcSpawn.ID);
mapFile = mapFile.RemoveNPCSpawn(npcSpawn);
changesMade = true;
continue;
}
if (npcSpawn.X > mapFile.Properties.Width || npcSpawn.Y > mapFile.Properties.Height)
{
Console.WriteLine("[MAP {0}] NPC Spawn {1}x{2} ({3}) is out of map bounds. Removing.", mapID, npcSpawn.X, npcSpawn.Y, npcRec.Name);
mapFile = mapFile.RemoveNPCSpawn(npcSpawn);
changesMade = true;
continue;
}
if (!TileIsValidNPCSpawnPoint(mapFile, npcSpawn.X, npcSpawn.Y))
{
Console.WriteLine("[MAP {0}] NPC Spawn {1}x{2} ({3}) is invalid...", mapID, npcSpawn.X, npcSpawn.Y, npcRec.Name);
var found = false;
for (int row = npcSpawn.Y - 2; row < npcSpawn.Y + 2; ++row)
{
if (found) break;
for (int col = npcSpawn.X - 2; col < npcSpawn.X + 2; ++col)
{
if (found) break;
if (TileIsValidNPCSpawnPoint(mapFile, col, row))
{
Console.WriteLine("[MAP {0}] Found valid spawn point. Continuing.", mapID);
found = true;
}
}
}
if (!found)
{
Console.WriteLine("[MAP {0}] NPC couldn't spawn anywhere valid! Removing.", mapID);
mapFile = mapFile.RemoveNPCSpawn(npcSpawn);
changesMade = true;
}
}
}
for(int ndx = mapFile.Chests.Count - 1; ndx >= 0; --ndx)
{
var chestSpawn = mapFile.Chests[ndx];
var rec = _pubProvider.EIFFile[chestSpawn.ItemID];
if (chestSpawn.ItemID > _pubProvider.EIFFile.Length || rec == null)
{
Console.WriteLine("[MAP {0}] Chest Spawn {1}x{2} uses non-existent Item #{3}. Removing.", mapID, chestSpawn.X, chestSpawn.Y, chestSpawn.ItemID);
mapFile = mapFile.RemoveChestSpawn(chestSpawn);
changesMade = true;
continue;
}
if (chestSpawn.X > mapFile.Properties.Width ||
chestSpawn.Y > mapFile.Properties.Height ||
mapFile.Tiles[chestSpawn.Y, chestSpawn.X] != TileSpec.Chest)
{
Console.WriteLine("[MAP {0}] Chest Spawn {1}x{2} points to a non-chest. Removing.", mapID, chestSpawn.X, chestSpawn.Y);
mapFile = mapFile.RemoveChestSpawn(chestSpawn);
changesMade = true;
}
}
if (!changesMade)
{
Console.WriteLine("Map {0} processed without any errors. No changes made.", mapID);
continue;
}
if (mapIndex == 0 && singleFile && inFiles.Length == 1)
{
mapFileSaveService.SaveFile(dst, mapFile);
break;
}
mapFileSaveService.SaveFile(
Path.Combine(dst, $"{mapID,5:D5}.emf"),
mapFile);
}
}
private static bool TileIsValidNPCSpawnPoint(IMapFile EMF, int x, int y)
{
if (EMF.Warps[y, x] != null)
return false;
switch (EMF.Tiles[y, x])
{
case TileSpec.Wall:
case TileSpec.ChairDown:
case TileSpec.ChairLeft:
case TileSpec.ChairRight:
case TileSpec.ChairUp:
case TileSpec.ChairDownRight:
case TileSpec.ChairUpLeft:
case TileSpec.ChairAll:
case TileSpec.Chest:
case TileSpec.BankVault:
case TileSpec.NPCBoundary:
case TileSpec.MapEdge:
case TileSpec.Board1:
case TileSpec.Board2:
case TileSpec.Board3:
case TileSpec.Board4:
case TileSpec.Board5:
case TileSpec.Board6:
case TileSpec.Board7:
case TileSpec.Board8:
case TileSpec.Jukebox:
return false;
}
return true;
}
}
}