some nitpicky cleanup of the Database.cs file

This commit is contained in:
adelikat 2014-04-14 23:14:39 +00:00
parent f61684c574
commit 54949127ee
1 changed files with 131 additions and 71 deletions

View File

@ -9,51 +9,66 @@ namespace BizHawk.Emulation.Common
{ {
public class CompactGameInfo public class CompactGameInfo
{ {
public string Name; public string Name { get; set; }
public string System; public string System { get; set; }
public string MetaData; public string MetaData { get; set; }
public string Hash; public string Hash { get; set; }
public string Region; public string Region { get; set; }
public RomStatus Status; public RomStatus Status { get; set; }
} }
public static class Database public static class Database
{ {
private static readonly Dictionary<string, CompactGameInfo> db = new Dictionary<string, CompactGameInfo>(); private static readonly Dictionary<string, CompactGameInfo> db = new Dictionary<string, CompactGameInfo>();
static string RemoveHashType(string hash) private static string RemoveHashType(string hash)
{ {
hash = hash.ToUpper(); hash = hash.ToUpper();
if (hash.StartsWith("MD5:")) hash = hash.Substring(4); if (hash.StartsWith("MD5:"))
if (hash.StartsWith("SHA1:")) hash = hash.Substring(5); {
hash = hash.Substring(4);
}
if (hash.StartsWith("SHA1:"))
{
hash = hash.Substring(5);
}
return hash; return hash;
} }
public static GameInfo CheckDatabase(string hash) public static GameInfo CheckDatabase(string hash)
{ {
CompactGameInfo cgi; CompactGameInfo cgi;
string hash_notype = RemoveHashType(hash); var hash_notype = RemoveHashType(hash);
db.TryGetValue(hash_notype, out cgi); db.TryGetValue(hash_notype, out cgi);
if (cgi == null) if (cgi == null)
{ {
Console.WriteLine("DB: hash " + hash + " not in game database."); Console.WriteLine("DB: hash " + hash + " not in game database.");
return null; return null;
} }
return new GameInfo(cgi); return new GameInfo(cgi);
} }
static void LoadDatabase_Escape(string line, string path) private static void LoadDatabase_Escape(string line, string path)
{ {
if (!line.ToUpper().StartsWith("#INCLUDE")) return; if (!line.ToUpper().StartsWith("#INCLUDE"))
{
return;
}
line = line.Substring(8).TrimStart(); line = line.Substring(8).TrimStart();
string filename = Path.Combine(path, line); var filename = Path.Combine(path, line);
if (File.Exists(filename)) if (File.Exists(filename))
{ {
Console.WriteLine("loading external game database {0}", line); Console.WriteLine("loading external game database {0}", line);
LoadDatabase(filename); LoadDatabase(filename);
} }
else else
{
Console.WriteLine("BENIGN: missing external game database {0}", line); Console.WriteLine("BENIGN: missing external game database {0}", line);
}
} }
public static void LoadDatabase(string path) public static void LoadDatabase(string path)
@ -62,44 +77,75 @@ namespace BizHawk.Emulation.Common
{ {
while (reader.EndOfStream == false) while (reader.EndOfStream == false)
{ {
string line = reader.ReadLine(); var line = reader.ReadLine() ?? string.Empty;
try try
{ {
if (line.StartsWith(";")) continue; //comment if (line.StartsWith(";"))
{
continue; // comment
}
if (line.StartsWith("#")) if (line.StartsWith("#"))
{ {
LoadDatabase_Escape(line, Path.GetDirectoryName(path)); LoadDatabase_Escape(line, Path.GetDirectoryName(path));
continue; continue;
} }
if (line.Trim().Length == 0) continue;
string[] items = line.Split('\t');
var Game = new CompactGameInfo if (line.Trim().Length == 0)
{ {
Hash = RemoveHashType(items[0].ToUpper()) continue;
}; }
//remove a hash type identifier. well don't really need them for indexing (theyre just there for human purposes)
var items = line.Split('\t');
var game = new CompactGameInfo
{
Hash = RemoveHashType(items[0].ToUpper())
};
// remove a hash type identifier. well don't really need them for indexing (theyre just there for human purposes)
switch (items[1].Trim()) switch (items[1].Trim())
{ {
case "B": Game.Status = RomStatus.BadDump; break; case "B":
case "V": Game.Status = RomStatus.BadDump; break; game.Status = RomStatus.BadDump;
case "T": Game.Status = RomStatus.TranslatedRom; break; break;
case "O": Game.Status = RomStatus.Overdump; break; case "V":
case "I": Game.Status = RomStatus.BIOS; break; game.Status = RomStatus.BadDump;
case "D": Game.Status = RomStatus.Homebrew; break; break;
case "H": Game.Status = RomStatus.Hack; break; case "T":
case "U": Game.Status = RomStatus.Unknown; break; game.Status = RomStatus.TranslatedRom;
default: Game.Status = RomStatus.GoodDump; break; break;
case "O":
game.Status = RomStatus.Overdump;
break;
case "I":
game.Status = RomStatus.BIOS;
break;
case "D":
game.Status = RomStatus.Homebrew;
break;
case "H":
game.Status = RomStatus.Hack;
break;
case "U":
game.Status = RomStatus.Unknown;
break;
default:
game.Status = RomStatus.GoodDump;
break;
} }
Game.Name = items[2];
Game.System = items[3];
Game.MetaData = items.Length >= 6 ? items[5] : null;
Game.Region = items.Length >= 7 ? items[6] : "";
if (db.ContainsKey(Game.Hash)) game.Name = items[2];
Console.WriteLine("gamedb: Multiple hash entries {0}, duplicate detected on \"{1}\" and \"{2}\"", Game.Hash, Game.Name, db[Game.Hash].Name); game.System = items[3];
game.MetaData = items.Length >= 6 ? items[5] : null;
game.Region = items.Length >= 7 ? items[6] : string.Empty;
db[Game.Hash] = Game; if (db.ContainsKey(game.Hash))
{
Console.WriteLine("gamedb: Multiple hash entries {0}, duplicate detected on \"{1}\" and \"{2}\"", game.Hash, game.Name, db[game.Hash].Name);
}
db[game.Hash] = game;
} }
catch catch
{ {
@ -109,93 +155,105 @@ namespace BizHawk.Emulation.Common
} }
} }
public static GameInfo GetGameInfo(byte[] RomData, string fileName) public static GameInfo GetGameInfo(byte[] romData, string fileName)
{ {
CompactGameInfo cgi; CompactGameInfo cgi;
string hash = string.Format("{0:X8}", CRC32.Calculate(RomData)); var hash = string.Format("{0:X8}", CRC32.Calculate(romData));
if (db.TryGetValue(hash, out cgi)) if (db.TryGetValue(hash, out cgi))
{
return new GameInfo(cgi); return new GameInfo(cgi);
}
hash = Util.Hash_MD5(RomData); hash = Util.Hash_MD5(romData);
if (db.TryGetValue(hash, out cgi)) if (db.TryGetValue(hash, out cgi))
{
return new GameInfo(cgi); return new GameInfo(cgi);
}
hash = Util.Hash_SHA1(RomData); hash = Util.Hash_SHA1(romData);
if (db.TryGetValue(hash, out cgi)) if (db.TryGetValue(hash, out cgi))
{
return new GameInfo(cgi); return new GameInfo(cgi);
}
// rom is not in database. make some best-guesses // rom is not in database. make some best-guesses
var Game = new GameInfo { Hash = hash, Status = RomStatus.NotInDatabase, NotInDatabase = true }; var game = new GameInfo
Console.WriteLine("Game was not in DB. CRC: {0:X8} MD5: {1}", {
CRC32.Calculate(RomData), Hash = hash,
Util.BytesToHexString(System.Security.Cryptography.MD5.Create().ComputeHash(RomData))); Status = RomStatus.NotInDatabase,
NotInDatabase = true
};
string ext = Path.GetExtension(fileName).ToUpperInvariant(); Console.WriteLine(
"Game was not in DB. CRC: {0:X8} MD5: {1}",
CRC32.Calculate(romData),
Util.BytesToHexString(System.Security.Cryptography.MD5.Create().ComputeHash(romData)));
var ext = Path.GetExtension(fileName).ToUpperInvariant();
switch (ext) switch (ext)
{ {
case ".NES": case ".NES":
case ".UNF": case ".UNF":
case ".FDS": case ".FDS":
Game.System = "NES"; game.System = "NES";
break; break;
case ".SFC": case ".SFC":
case ".SMC": case ".SMC":
Game.System = "SNES"; game.System = "SNES";
break; break;
case ".GB": case ".GB":
Game.System = "GB"; game.System = "GB";
break; break;
case ".GBC": case ".GBC":
Game.System = "GBC"; game.System = "GBC";
break; break;
case ".GBA": case ".GBA":
Game.System = "GBA"; game.System = "GBA";
break; break;
case ".SMS": case ".SMS":
Game.System = "SMS"; game.System = "SMS";
break; break;
case ".GG": case ".GG":
Game.System = "GG"; game.System = "GG";
break; break;
case ".SG": case ".SG":
Game.System = "SG"; game.System = "SG";
break; break;
//case ".BIN":
case ".GEN": case ".GEN":
case ".MD": case ".MD":
case ".SMD": case ".SMD":
Game.System = "GEN"; game.System = "GEN";
break; break;
case ".PSF": case ".PSF":
Game.System = "PSX"; game.System = "PSX";
break; break;
case ".PCE": case ".PCE":
Game.System = "PCE"; game.System = "PCE";
break; break;
case ".SGX": case ".SGX":
Game.System = "SGX"; game.System = "SGX";
break; break;
case ".A26": case ".A26":
Game.System = "A26"; game.System = "A26";
break; break;
case ".A78": case ".A78":
Game.System = "A78"; game.System = "A78";
break; break;
case ".COL": case ".COL":
Game.System = "Coleco"; game.System = "Coleco";
break; break;
case ".INT": case ".INT":
Game.System = "INTV"; game.System = "INTV";
break; break;
case ".PRG": case ".PRG":
@ -203,27 +261,29 @@ namespace BizHawk.Emulation.Common
case ".T64": case ".T64":
case ".G64": case ".G64":
case ".CRT": case ".CRT":
Game.System = "C64"; game.System = "C64";
break; break;
case ".Z64": case ".Z64":
case ".V64": case ".V64":
case ".N64": case ".N64":
Game.System = "N64"; game.System = "N64";
break; break;
case ".DEBUG": case ".DEBUG":
Game.System = "DEBUG"; game.System = "DEBUG";
break; break;
} }
Game.Name = Path.GetFileNameWithoutExtension(fileName).Replace('_', ' '); game.Name = Path.GetFileNameWithoutExtension(fileName).Replace('_', ' ');
// If filename is all-caps, then attempt to proper-case the title. // If filename is all-caps, then attempt to proper-case the title.
if (Game.Name == Game.Name.ToUpperInvariant()) if (game.Name == game.Name.ToUpperInvariant())
Game.Name = Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(Game.Name.ToLower()); {
game.Name = Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(game.Name.ToLower());
}
return Game; return game;
} }
} }
} }