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 string Name;
public string System;
public string MetaData;
public string Hash;
public string Region;
public RomStatus Status;
public string Name { get; set; }
public string System { get; set; }
public string MetaData { get; set; }
public string Hash { get; set; }
public string Region { get; set; }
public RomStatus Status { get; set; }
}
public static class Database
{
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();
if (hash.StartsWith("MD5:")) hash = hash.Substring(4);
if (hash.StartsWith("SHA1:")) hash = hash.Substring(5);
if (hash.StartsWith("MD5:"))
{
hash = hash.Substring(4);
}
if (hash.StartsWith("SHA1:"))
{
hash = hash.Substring(5);
}
return hash;
}
public static GameInfo CheckDatabase(string hash)
{
CompactGameInfo cgi;
string hash_notype = RemoveHashType(hash);
var hash_notype = RemoveHashType(hash);
db.TryGetValue(hash_notype, out cgi);
if (cgi == null)
{
Console.WriteLine("DB: hash " + hash + " not in game database.");
return null;
}
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();
string filename = Path.Combine(path, line);
var filename = Path.Combine(path, line);
if (File.Exists(filename))
{
Console.WriteLine("loading external game database {0}", line);
LoadDatabase(filename);
}
else
{
Console.WriteLine("BENIGN: missing external game database {0}", line);
}
}
public static void LoadDatabase(string path)
@ -62,44 +77,75 @@ namespace BizHawk.Emulation.Common
{
while (reader.EndOfStream == false)
{
string line = reader.ReadLine();
var line = reader.ReadLine() ?? string.Empty;
try
{
if (line.StartsWith(";")) continue; //comment
if (line.StartsWith(";"))
{
continue; // comment
}
if (line.StartsWith("#"))
{
LoadDatabase_Escape(line, Path.GetDirectoryName(path));
continue;
}
if (line.Trim().Length == 0) continue;
string[] 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)
if (line.Trim().Length == 0)
{
continue;
}
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())
{
case "B": Game.Status = RomStatus.BadDump; break;
case "V": Game.Status = RomStatus.BadDump; break;
case "T": Game.Status = RomStatus.TranslatedRom; 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;
case "B":
game.Status = RomStatus.BadDump;
break;
case "V":
game.Status = RomStatus.BadDump;
break;
case "T":
game.Status = RomStatus.TranslatedRom;
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))
Console.WriteLine("gamedb: Multiple hash entries {0}, duplicate detected on \"{1}\" and \"{2}\"", Game.Hash, Game.Name, db[Game.Hash].Name);
game.Name = items[2];
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
{
@ -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;
string hash = string.Format("{0:X8}", CRC32.Calculate(RomData));
var hash = string.Format("{0:X8}", CRC32.Calculate(romData));
if (db.TryGetValue(hash, out cgi))
{
return new GameInfo(cgi);
}
hash = Util.Hash_MD5(RomData);
hash = Util.Hash_MD5(romData);
if (db.TryGetValue(hash, out cgi))
{
return new GameInfo(cgi);
}
hash = Util.Hash_SHA1(RomData);
hash = Util.Hash_SHA1(romData);
if (db.TryGetValue(hash, out cgi))
{
return new GameInfo(cgi);
}
// rom is not in database. make some best-guesses
var Game = new GameInfo { Hash = hash, Status = RomStatus.NotInDatabase, NotInDatabase = true };
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 game = new GameInfo
{
Hash = hash,
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)
{
case ".NES":
case ".UNF":
case ".FDS":
Game.System = "NES";
game.System = "NES";
break;
case ".SFC":
case ".SMC":
Game.System = "SNES";
game.System = "SNES";
break;
case ".GB":
Game.System = "GB";
game.System = "GB";
break;
case ".GBC":
Game.System = "GBC";
game.System = "GBC";
break;
case ".GBA":
Game.System = "GBA";
game.System = "GBA";
break;
case ".SMS":
Game.System = "SMS";
game.System = "SMS";
break;
case ".GG":
Game.System = "GG";
game.System = "GG";
break;
case ".SG":
Game.System = "SG";
game.System = "SG";
break;
//case ".BIN":
case ".GEN":
case ".MD":
case ".SMD":
Game.System = "GEN";
game.System = "GEN";
break;
case ".PSF":
Game.System = "PSX";
game.System = "PSX";
break;
case ".PCE":
Game.System = "PCE";
game.System = "PCE";
break;
case ".SGX":
Game.System = "SGX";
game.System = "SGX";
break;
case ".A26":
Game.System = "A26";
game.System = "A26";
break;
case ".A78":
Game.System = "A78";
game.System = "A78";
break;
case ".COL":
Game.System = "Coleco";
game.System = "Coleco";
break;
case ".INT":
Game.System = "INTV";
game.System = "INTV";
break;
case ".PRG":
@ -203,27 +261,29 @@ namespace BizHawk.Emulation.Common
case ".T64":
case ".G64":
case ".CRT":
Game.System = "C64";
game.System = "C64";
break;
case ".Z64":
case ".V64":
case ".N64":
Game.System = "N64";
game.System = "N64";
break;
case ".DEBUG":
Game.System = "DEBUG";
game.System = "DEBUG";
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 (Game.Name == Game.Name.ToUpperInvariant())
Game.Name = Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(Game.Name.ToLower());
if (game.Name == game.Name.ToUpperInvariant())
{
game.Name = Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(game.Name.ToLower());
}
return Game;
return game;
}
}
}