BizHawk/BizHawk.Emulation/Database/Database.cs

92 lines
3.2 KiB
C#
Raw Normal View History

2011-01-11 02:55:51 +00:00
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading;
namespace BizHawk
{
public class GameInfo
{
public string Name;
public string System;
public string MetaData;
public int CRC32;
public string[] GetOptions()
{
if (string.IsNullOrEmpty(MetaData))
return new string[0];
return MetaData.Split(';').Where(opt => string.IsNullOrEmpty(opt) == false).ToArray();
}
}
public static class Database
{
private static Dictionary<int, GameInfo> db = new Dictionary<int, GameInfo>();
public static void LoadDatabase(string path)
{
using (var reader = new StreamReader(new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)))
{
reader.ReadLine(); // Skip header row
while (reader.EndOfStream == false)
{
string line = reader.ReadLine();
2011-01-13 06:10:50 +00:00
try
{
if (line.Trim().Length == 0) continue;
string[] items = line.Split('\t');
var Game = new GameInfo();
Game.CRC32 = Int32.Parse(items[0], NumberStyles.HexNumber);
Game.Name = items[2];
Game.System = items[3];
Game.MetaData = items.Length >= 6 ? items[5] : null;
db[Game.CRC32] = Game;
} catch (Exception e)
{
Console.WriteLine("Error parsing database entry: "+line);
}
2011-01-11 02:55:51 +00:00
}
}
}
public static GameInfo GetGameInfo(byte[] RomData, string fileName)
{
int crc = CRC32.Calculate(RomData);
if (db.ContainsKey(crc))
return db[crc];
// rom is not in database. make some best-guesses
var Game = new GameInfo();
Game.CRC32 = crc;
Game.MetaData = "NotInDatabase";
string ext = Path.GetExtension(fileName).ToUpperInvariant();
switch (ext)
{
case ".SMS": Game.System = "SMS"; break;
case ".GG" : Game.System = "GG"; break;
case ".SG" : Game.System = "SG"; break;
case ".PCE": Game.System = "PCE"; break;
case ".SGX": Game.System = "SGX"; break;
case ".GB" : Game.System = "GB"; break;
case ".BIN":
case ".SMD": Game.System = "GEN"; break;
case ".NES": Game.System = "NES"; break;
}
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());
return Game;
}
}
}