-
Notifications
You must be signed in to change notification settings - Fork 4
/
FBA.cs
373 lines (308 loc) · 15.4 KB
/
FBA.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
using DamienG.Security.Cryptography;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Text;
using System.Xml.Serialization;
namespace MvsxPackBuilder
{
public class FBA
{
public class FbaGameMetadata
{
public FbaGameMetadata()
{
}
public bool DoesRomFileExists = false;
public bool DoesRomCrcMatch = true; // assume true until we run the extensive validation check
public StringBuilder ValidationLog = new StringBuilder();
public StringBuilder CrcLog = new StringBuilder();
}
private datafile[] Datafiles;
private Dictionary<string, int>[] NameToDatIndexMapping;
private FbaGameMetadata[][] GameMetadaPerPlatform;
public Dictionary<string, int> GetNameToDatIndexMapping(SupportedPlatforms platform)
{
return NameToDatIndexMapping[(int)platform];
}
public enum SupportedPlatforms : int
{
Arcade = 0,
//Colecovision,
//GameGear,
//MasterSystem,
Megadrive,
//MSX1,
//PCEngine,
//SG1000,
//SuperGrafx,
//TurboGrafx16
};
static public string[] PlatformPrefix = new string[] {
"",
//"cv_",
//"gg_",
//"sms_",
"md_",
//"msx_",
//"pce_",
//"sg1k_",
//"sgx_",
//"tg_",
};
static public string[] RomFolder = new string[] {
"roms",
//"coleco",
//"gamegear",
//"sms",
"megadriv",
//"msx",
//"pce",
//"sg1000",
//"sgx",
//"tg16",
};
static public FBA.SupportedPlatforms GetPlaformFromDirName(string dirName)
{
string filename = Path.GetFileName(dirName);
int platformCount = Enum.GetNames(typeof(FBA.SupportedPlatforms)).Length;
// skip arcade, as this is our fallthrough
for (int platformIndex = 1; platformIndex < platformCount; ++platformIndex)
{
if (filename.StartsWith(FBA.PlatformPrefix[platformIndex]))
{
return (FBA.SupportedPlatforms)platformIndex;
}
}
return FBA.SupportedPlatforms.Arcade;
}
static public string GetRomsPrefixFolderFromPlatform(FBA.SupportedPlatforms platform, bool ignoreArcadePath)
{
if(ignoreArcadePath && platform == SupportedPlatforms.Arcade)
{
return "";
}
return FBA.RomFolder[(int)platform];
}
static public string GetGameNameWithoutPlatform(FBA.SupportedPlatforms platform, string iniDirName)
{
string filename = Path.GetFileName(iniDirName);
if (FBA.PlatformPrefix[(int)platform].Length != 0)
{
filename = filename.Remove(0, FBA.PlatformPrefix[(int)platform].Length);
}
return filename;
}
static public string GetGameNameWithPlatform(FBA.SupportedPlatforms platform, string gameName)
{
if (platform != SupportedPlatforms.Arcade)
{
string tmp = PlatformPrefix[(int)platform] + gameName;
return Path.Combine(RomFolder[(int)platform], tmp);
}
return gameName;
}
public List<string> GetFbaRomPathFromGameIndex(string RomsFolder, FBA.SupportedPlatforms platform, Int32 gameIndex)
{
List<string> romList = new List<string>();
string rompath = Path.Combine(RomsFolder, GetRomsPrefixFolderFromPlatform(platform, false));
datafile romset = GetDatafileFromPlatorm(platform);
Dictionary<string, int> nameToGameIndex = GetNameToDatIndexMapping(platform);
game fbaGame = romset.game[gameIndex];
while(fbaGame != null)
{
romList.Add(Path.Combine(rompath, fbaGame.name + ".zip"));
// check for parent hierarchy
if (!string.IsNullOrEmpty(fbaGame.romof))
{
Int32 parentIndex = 0;
if (nameToGameIndex.TryGetValue(fbaGame.romof, out parentIndex))
{
fbaGame = romset.game[parentIndex];
continue;
}
}
fbaGame = null;
}
return romList;
}
public void ValidateRomsExists(string RomsFolder, out StringBuilder log, out List<int> missingGameIndices)
{
log = new StringBuilder();
missingGameIndices = new List<int>();
foreach (SupportedPlatforms platform in Enum.GetValues(typeof(SupportedPlatforms)))
{
datafile currentDatFile = GetDatafileFromPlatorm(platform);
string rompathPrefix = GetRomsPrefixFolderFromPlatform(platform, false);
string rompath = Path.Combine(RomsFolder, rompathPrefix);
if (!Directory.Exists(rompath))
{
log.AppendLine(String.Format("[ERROR] Folder {2} does not exist: Roms from platform {0} needs to be in the {1} subfolder. ", platform, rompathPrefix, rompath));
return;
}
for (int gameIndex = 0; gameIndex < currentDatFile.game.Length; ++gameIndex)
{
FbaGameMetadata metaData = GetFbaGameMetadataFromIndex(platform, gameIndex);
game currentGame = currentDatFile.game[gameIndex];
string filename = Path.ChangeExtension(currentGame.name, @".zip");
string filepath = Path.Combine(rompath, filename);
metaData.DoesRomFileExists = File.Exists(filepath);
metaData.ValidationLog.Clear();
if (!metaData.DoesRomFileExists)
{
metaData.ValidationLog.AppendLine(string.Format("[WARNING] Game \"{0}\" is missing, file needs to be placed here: {1}", currentGame.description, filepath));
missingGameIndices.Add(gameIndex);
}
log.Append(metaData.ValidationLog);
}
}
}
// TODO: move this onto an async job, as this can take a while
// TODO: handle merge set correctly.
private void ValidateRomsCRC(string RomsFolder, FBA.SupportedPlatforms platform, out StringBuilder log, out List<int> invalidGameIndices)
{
log = new StringBuilder();
invalidGameIndices = new List<int>();
datafile currentDatFile = GetDatafileFromPlatorm(platform);
string rompathPrefix = GetRomsPrefixFolderFromPlatform(platform, false);
string rompath = Path.Combine(RomsFolder, rompathPrefix);
if (!Directory.Exists(rompath))
{
log.AppendLine(String.Format("[ERROR] Folder {2} does not exist: Roms from platform {0} needs to be in the {1} subfolder. ", platform, rompathPrefix, rompath));
return;
}
for (int gameIndex = 0; gameIndex < currentDatFile.game.Length; ++gameIndex)
{
game currentGame = currentDatFile.game[gameIndex];
string filename = Path.ChangeExtension(currentGame.name, @".zip");
string filepath = Path.Combine(rompath, filename);
FbaGameMetadata metaData = GetFbaGameMetadataFromIndex(platform, gameIndex);
metaData.DoesRomCrcMatch = false;
metaData.CrcLog.Clear();
if (!File.Exists(filepath))
{
metaData.CrcLog.AppendLine(string.Format("[WARNING] Game \"{0}\" is missing, file needs to be placed here: {1}", currentGame.description, filepath));
invalidGameIndices.Add(gameIndex);
continue;
}
using (ZipArchive archive = ZipFile.Open(filepath, ZipArchiveMode.Read))
{
bool IsValid = true;
foreach (rom theRom in currentGame.rom)
{
ZipArchiveEntry zipEntry = archive.GetEntry(theRom.name);
if (zipEntry == null)
{
metaData.CrcLog.AppendLine(string.Format("[ERROR] Game \"{0}\" [{2}.zip] is missing rom {1}", currentGame.description, theRom.name, currentGame.name));
IsValid = false;
continue;
}
var memory = new byte[zipEntry.Length];
Span<byte> buffer = new Span<byte>(memory);
if (zipEntry.Open().Read(buffer) != zipEntry.Length)
{
metaData.CrcLog.AppendLine(string.Format("[ERROR] Game \"{0}\" [{2}.zip] Unable to read rom {1} in zip archive", currentGame.description, theRom.name, currentGame.name));
IsValid = false;
continue;
}
// validate CRC
UInt32 theCrc = Crc32.Compute(buffer.ToArray());
string crcInHex = theCrc.ToString("X").PadLeft(8, '0').ToLower();
if (crcInHex != theRom.crc)
{
metaData.CrcLog.AppendLine(string.Format("[ERROR] Game \"{0}\" [{4}.zip] rom {1} has an invalid CRC {2}, expected {3}", currentGame.description, theRom.name, crcInHex, theRom.crc, currentGame.name));
IsValid = false;
continue;
}
}
if (IsValid == false)
{
invalidGameIndices.Add(gameIndex);
}
}
log.Append(metaData.CrcLog);
}
}
static public datafile LoadRomdat(string filename)
{
datafile datFile = new datafile();
// start the load of the xml files
XmlSerializer serializer = new XmlSerializer(typeof(datafile));
using (Stream reader = new FileStream(filename, FileMode.Open))
{
// Call the Deserialize method to restore the object's state.
datFile = (datafile)serializer.Deserialize(reader);
}
return datFile;
}
public bool LoadAllRomdats(string RomdatFolder)
{
int platformCount = Enum.GetNames(typeof(FBA.SupportedPlatforms)).Length;
Datafiles = new datafile[platformCount];
Datafiles[(int)FBA.SupportedPlatforms.Arcade] = FBA.LoadRomdat(Path.Combine(RomdatFolder, @"FB Alpha v0.2.97.43 (ClrMame Pro XML).dat"));
//Datafiles[(int)FBA.SupportedPlatforms.Colecovision] = FBA.LoadRomdat(Path.Combine(RomdatFolder, @"FB Alpha v0.2.97.43 (ClrMame Pro XML, ColecoVision only).dat"));
//Datafiles[(int)FBA.SupportedPlatforms.GameGear] = FBA.LoadRomdat(Path.Combine(RomdatFolder, @"FB Alpha v0.2.97.43 (ClrMame Pro XML, Game Gear only).dat"));
//Datafiles[(int)FBA.SupportedPlatforms.MasterSystem] = FBA.LoadRomdat(Path.Combine(RomdatFolder, @"FB Alpha v0.2.97.43 (ClrMame Pro XML, Master System only).dat"));
Datafiles[(int)FBA.SupportedPlatforms.Megadrive] = FBA.LoadRomdat(Path.Combine(RomdatFolder, @"FB Alpha v0.2.97.43 (ClrMame Pro XML, Megadrive only).dat"));
//Datafiles[(int)FBA.SupportedPlatforms.MSX1] = FBA.LoadRomdat(Path.Combine(RomdatFolder, @"FB Alpha v0.2.97.43 (ClrMame Pro XML, MSX 1 Games only).dat"));
//Datafiles[(int)FBA.SupportedPlatforms.PCEngine] = FBA.LoadRomdat(Path.Combine(RomdatFolder, @"FB Alpha v0.2.97.43 (ClrMame Pro XML, PC-Engine only).dat"));
//Datafiles[(int)FBA.SupportedPlatforms.SG1000] = FBA.LoadRomdat(Path.Combine(RomdatFolder, @"FB Alpha v0.2.97.43 (ClrMame Pro XML, Sega SG-1000 only).dat"));
//Datafiles[(int)FBA.SupportedPlatforms.SuperGrafx] = FBA.LoadRomdat(Path.Combine(RomdatFolder, @"FB Alpha v0.2.97.43 (ClrMame Pro XML, SuprGrafx only).dat"));
//Datafiles[(int)FBA.SupportedPlatforms.TurboGrafx16] = FBA.LoadRomdat(Path.Combine(RomdatFolder, @"FB Alpha v0.2.97.43 (ClrMame Pro XML, TurboGrafx16 only).dat"));
// allocate name to index mapping
NameToDatIndexMapping = new Dictionary<string, int>[platformCount];
PopulateNameToDatIndexMapping(FBA.SupportedPlatforms.Arcade);
//PopulateNameToDatIndexMapping(FBA.SupportedPlatforms.Colecovision);
//PopulateNameToDatIndexMapping(FBA.SupportedPlatforms.GameGear);
//PopulateNameToDatIndexMapping(FBA.SupportedPlatforms.MasterSystem);
PopulateNameToDatIndexMapping(FBA.SupportedPlatforms.Megadrive);
//PopulateNameToDatIndexMapping(FBA.SupportedPlatforms.MSX1);
//PopulateNameToDatIndexMapping(FBA.SupportedPlatforms.PCEngine);
//PopulateNameToDatIndexMapping(FBA.SupportedPlatforms.SG1000);
//PopulateNameToDatIndexMapping(FBA.SupportedPlatforms.SuperGrafx);
//PopulateNameToDatIndexMapping(FBA.SupportedPlatforms.TurboGrafx16);
// allocate metadata
GameMetadaPerPlatform = new FbaGameMetadata[platformCount][];
for(Int32 platformIndex = 0; platformIndex < platformCount; ++platformIndex)
{
GameMetadaPerPlatform[platformIndex] = new FbaGameMetadata[Datafiles[platformIndex].game.Length];
for(Int32 index = 0; index < GameMetadaPerPlatform[platformIndex].Length; ++index)
{
GameMetadaPerPlatform[platformIndex][index] = new FbaGameMetadata();
}
}
return true;
}
public void PopulateNameToDatIndexMapping(FBA.SupportedPlatforms platform)
{
// populate the NameToIndex dictionary
datafile currentDatafile = GetDatafileFromPlatorm(platform);
NameToDatIndexMapping[(int)platform] = new Dictionary<string, int>();
for (Int32 GameIndex = 0; GameIndex < currentDatafile.game.Length; ++GameIndex)
{
NameToDatIndexMapping[(int)platform].Add(currentDatafile.game[GameIndex].name, GameIndex);
}
}
public Int32 GetFbaGameIndexFromRomName(FBA.SupportedPlatforms platform, string romName)
{
return string.IsNullOrEmpty(romName) ? -1 : GetNameToDatIndexMapping(platform)[romName];
}
public FbaGameMetadata GetFbaGameMetadataFromIndex(FBA.SupportedPlatforms platform, Int32 gameIndex)
{
if (gameIndex != -1)
{
return GameMetadaPerPlatform[(int)platform][gameIndex];
}
return null;
}
public game GetGameFromIndex(FBA.SupportedPlatforms platform, int index)
{
return Datafiles[(int)platform].game[index];
}
public datafile GetDatafileFromPlatorm(FBA.SupportedPlatforms platform)
{
return Datafiles[(int)platform];
}
}
}