2016-03-18 01:17:39 -07:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Data.SQLite;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
|
using System.Xml;
|
|
|
|
|
|
|
2016-03-29 13:48:10 -07:00
|
|
|
|
using SabreTools.Helper;
|
2016-03-18 01:17:39 -07:00
|
|
|
|
|
2016-03-29 13:48:10 -07:00
|
|
|
|
namespace SabreTools
|
2016-03-18 01:17:39 -07:00
|
|
|
|
{
|
2016-03-29 14:49:03 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Import data into the database from existing DATs
|
|
|
|
|
|
/// </summary>
|
2016-03-24 12:46:02 -07:00
|
|
|
|
public class Import
|
2016-03-18 01:17:39 -07:00
|
|
|
|
{
|
|
|
|
|
|
// Private instance variables
|
|
|
|
|
|
private string _filepath;
|
|
|
|
|
|
private string _connectionString;
|
2016-03-28 18:40:35 -07:00
|
|
|
|
private Logger _logger;
|
2016-03-18 01:17:39 -07:00
|
|
|
|
|
|
|
|
|
|
// Regex File Name Patterns
|
|
|
|
|
|
private static string _defaultPattern = @"^(.+?) - (.+?) \((.*) (.*)\)\.dat$";
|
|
|
|
|
|
private static string _mamePattern = @"^(.*)\.xml$";
|
2016-04-04 23:11:29 -07:00
|
|
|
|
private static string _noIntroPattern = @"^(.*?) \((\d{8}-\d{6})_CM\)\.dat$";
|
2016-04-05 18:52:43 +02:00
|
|
|
|
private static string _noIntroNumberedPattern = @"(.*? - .*?) \(\d.*?_CM\).dat";
|
2016-04-04 17:20:59 -07:00
|
|
|
|
private static string _noIntroSpecialPattern = @"(.*? - .*?) \((\d{8})\)\.dat";
|
2016-03-18 01:17:39 -07:00
|
|
|
|
private static string _redumpPattern = @"^(.*?) \((\d{8} \d{2}-\d{2}-\d{2})\)\.dat$";
|
2016-04-04 19:37:18 -07:00
|
|
|
|
private static string _redumpBiosPattern = @"^(.*?) \(\d+\) \((\d{4}-\d{2}-\d{2})\)\.dat$";
|
2016-03-18 01:17:39 -07:00
|
|
|
|
private static string _tosecPattern = @"^(.*?) - .* \(TOSEC-v(\d{4}-\d{2}-\d{2})_CM\)\.dat$";
|
2016-04-05 16:11:58 -07:00
|
|
|
|
private static string _tosecSpecialPatternA = @"^(.*? - .*?) - .* \(TOSEC-v(\d{4}-\d{2}-\d{2})_CM\)\.dat$";
|
|
|
|
|
|
private static string _tosecSpecialPatternB = @"^(.*? - .*? - .*?) - .* \(TOSEC-v(\d{4}-\d{2}-\d{2})_CM\)\.dat$";
|
2016-04-04 20:00:07 -07:00
|
|
|
|
private static string _truripPattern = @"^(.*) - .* \(trurip_XML\)\.dat$";
|
2016-03-18 01:17:39 -07:00
|
|
|
|
|
|
|
|
|
|
// Regex Mapped Name Patterns
|
|
|
|
|
|
private static string _remappedPattern = @"^(.*) - (.*)$";
|
|
|
|
|
|
|
|
|
|
|
|
// Regex Date Patterns
|
|
|
|
|
|
private static string _defaultDatePattern = @"(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})";
|
2016-04-04 20:16:09 -07:00
|
|
|
|
private static string _noIntroDatePattern = @"(\d{4})(\d{2})(\d{2})-(\d{2})(\d{2})(\d{2})";
|
|
|
|
|
|
private static string _noIntroSpecialDatePattern = @"(\d{4})(\d{2})(\d{2})";
|
2016-03-18 01:17:39 -07:00
|
|
|
|
private static string _redumpDatePattern = @"(\d{4})(\d{2})(\d{2}) (\d{2})-(\d{2})-(\d{2})";
|
2016-03-18 15:01:00 -07:00
|
|
|
|
private static string _tosecDatePattern = @"(\d{4})-(\d{2})-(\d{2})";
|
2016-03-18 01:17:39 -07:00
|
|
|
|
|
2016-03-29 14:49:03 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Possible DAT import classes
|
|
|
|
|
|
/// </summary>
|
2016-03-18 01:17:39 -07:00
|
|
|
|
private enum DatType
|
|
|
|
|
|
{
|
|
|
|
|
|
none = 0,
|
2016-03-29 13:41:55 -07:00
|
|
|
|
Custom,
|
|
|
|
|
|
MAME,
|
|
|
|
|
|
NoIntro,
|
|
|
|
|
|
Redump,
|
|
|
|
|
|
TOSEC,
|
|
|
|
|
|
TruRip,
|
2016-03-18 01:17:39 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Public instance variables
|
|
|
|
|
|
public string FilePath
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return _filepath; }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-03-29 14:49:03 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Initialize an Import object with the given information
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="filepath">Path to the file that is going to be imported</param>
|
|
|
|
|
|
/// <param name="connectionString">Connection string for SQLite</param>
|
|
|
|
|
|
/// <param name="logger">Logger object for file or console output</param>
|
2016-03-28 18:40:35 -07:00
|
|
|
|
public Import(string filepath, string connectionString, Logger logger)
|
2016-03-18 01:17:39 -07:00
|
|
|
|
{
|
|
|
|
|
|
if (File.Exists(filepath))
|
|
|
|
|
|
{
|
|
|
|
|
|
_filepath = filepath;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new IOException("File " + filepath + " does not exist!");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_connectionString = connectionString;
|
2016-03-28 17:54:24 -07:00
|
|
|
|
_logger = logger;
|
2016-03-18 01:17:39 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-03-29 14:49:03 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Import the data from file into the database
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns>True if the data was imported, false otherwise</returns>
|
2016-03-18 01:17:39 -07:00
|
|
|
|
public bool ImportData ()
|
|
|
|
|
|
{
|
|
|
|
|
|
// Determine which dattype we have
|
|
|
|
|
|
string filename = Path.GetFileName(_filepath);
|
|
|
|
|
|
GroupCollection fileinfo;
|
|
|
|
|
|
DatType type = DatType.none;
|
|
|
|
|
|
|
|
|
|
|
|
if (Regex.IsMatch(filename, _mamePattern))
|
|
|
|
|
|
{
|
|
|
|
|
|
fileinfo = Regex.Match(filename, _mamePattern).Groups;
|
2016-03-29 13:41:55 -07:00
|
|
|
|
type = DatType.MAME;
|
2016-03-18 01:17:39 -07:00
|
|
|
|
}
|
2016-04-04 20:16:09 -07:00
|
|
|
|
else if (Regex.IsMatch(filename, _noIntroPattern))
|
2016-03-18 01:17:39 -07:00
|
|
|
|
{
|
2016-04-04 20:16:09 -07:00
|
|
|
|
fileinfo = Regex.Match(filename, _noIntroPattern).Groups;
|
2016-03-29 13:41:55 -07:00
|
|
|
|
type = DatType.NoIntro;
|
2016-03-18 01:17:39 -07:00
|
|
|
|
}
|
2016-04-04 16:20:08 -07:00
|
|
|
|
// For numbered DATs only
|
|
|
|
|
|
else if (Regex.IsMatch(filename, _noIntroNumberedPattern))
|
|
|
|
|
|
{
|
|
|
|
|
|
fileinfo = Regex.Match(filename, _noIntroNumberedPattern).Groups;
|
|
|
|
|
|
type = DatType.NoIntro;
|
|
|
|
|
|
}
|
2016-04-04 14:34:46 -07:00
|
|
|
|
// For N-Gage and Gizmondo only
|
|
|
|
|
|
else if (Regex.IsMatch(filename, _noIntroSpecialPattern))
|
|
|
|
|
|
{
|
2016-04-04 20:16:09 -07:00
|
|
|
|
fileinfo = Regex.Match(filename, _noIntroSpecialPattern).Groups;
|
2016-04-04 14:34:46 -07:00
|
|
|
|
type = DatType.NoIntro;
|
|
|
|
|
|
}
|
2016-03-18 01:17:39 -07:00
|
|
|
|
else if (Regex.IsMatch(filename, _redumpPattern))
|
|
|
|
|
|
{
|
|
|
|
|
|
fileinfo = Regex.Match(filename, _redumpPattern).Groups;
|
2016-03-29 13:41:55 -07:00
|
|
|
|
type = DatType.Redump;
|
2016-03-18 01:17:39 -07:00
|
|
|
|
}
|
2016-04-04 19:37:18 -07:00
|
|
|
|
// For special BIOSes only
|
|
|
|
|
|
else if (Regex.IsMatch(filename, _redumpBiosPattern))
|
|
|
|
|
|
{
|
|
|
|
|
|
fileinfo = Regex.Match(filename, _redumpBiosPattern).Groups;
|
|
|
|
|
|
type = DatType.Redump;
|
|
|
|
|
|
}
|
2016-03-18 01:17:39 -07:00
|
|
|
|
else if (Regex.IsMatch(filename, _tosecPattern))
|
|
|
|
|
|
{
|
|
|
|
|
|
fileinfo = Regex.Match(filename, _tosecPattern).Groups;
|
2016-03-29 13:41:55 -07:00
|
|
|
|
type = DatType.TOSEC;
|
2016-03-18 01:17:39 -07:00
|
|
|
|
}
|
|
|
|
|
|
else if (Regex.IsMatch(filename, _truripPattern))
|
|
|
|
|
|
{
|
|
|
|
|
|
fileinfo = Regex.Match(filename, _truripPattern).Groups;
|
2016-03-29 13:41:55 -07:00
|
|
|
|
type = DatType.TruRip;
|
2016-03-18 01:17:39 -07:00
|
|
|
|
}
|
|
|
|
|
|
else if (Regex.IsMatch(filename, _defaultPattern))
|
|
|
|
|
|
{
|
|
|
|
|
|
fileinfo = Regex.Match(filename, _defaultPattern).Groups;
|
2016-03-29 13:41:55 -07:00
|
|
|
|
type = DatType.Custom;
|
2016-03-18 01:17:39 -07:00
|
|
|
|
}
|
|
|
|
|
|
// If the type is still unmatched, the data can't be imported yet
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2016-03-30 13:36:52 -07:00
|
|
|
|
_logger.Error("File " + filename + " cannot be imported at this time because it is not a known pattern.\nPlease try again with an unrenamed version.");
|
2016-03-18 01:17:39 -07:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-04-04 19:37:18 -07:00
|
|
|
|
_logger.Log("Type detected: " + type.ToString());
|
|
|
|
|
|
|
2016-03-18 01:17:39 -07:00
|
|
|
|
// Check for and extract import information from the file name based on type
|
|
|
|
|
|
string manufacturer = "";
|
|
|
|
|
|
string system = "";
|
|
|
|
|
|
string source = "";
|
|
|
|
|
|
string datestring = "";
|
|
|
|
|
|
string date = "";
|
|
|
|
|
|
|
|
|
|
|
|
switch (type)
|
|
|
|
|
|
{
|
2016-03-29 13:41:55 -07:00
|
|
|
|
case DatType.MAME:
|
2016-03-18 17:37:07 -07:00
|
|
|
|
if (!Remapping.MAME.ContainsKey(fileinfo[1].Value))
|
|
|
|
|
|
{
|
2016-03-30 13:36:52 -07:00
|
|
|
|
_logger.Error("The filename " + fileinfo[1].Value + " could not be mapped! Please check the mappings and try again");
|
2016-03-18 17:37:07 -07:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2016-03-18 01:17:39 -07:00
|
|
|
|
GroupCollection mameInfo = Regex.Match(Remapping.MAME[fileinfo[1].Value], _remappedPattern).Groups;
|
|
|
|
|
|
|
|
|
|
|
|
manufacturer = mameInfo[1].Value;
|
|
|
|
|
|
system = mameInfo[2].Value;
|
|
|
|
|
|
source = "MAME";
|
2016-03-18 19:47:10 -07:00
|
|
|
|
date = File.GetLastWriteTime(_filepath).ToString("yyyy-MM-dd HH:mm:ss");
|
2016-03-18 01:17:39 -07:00
|
|
|
|
break;
|
2016-03-29 13:41:55 -07:00
|
|
|
|
case DatType.NoIntro:
|
2016-03-18 17:37:07 -07:00
|
|
|
|
if (!Remapping.NoIntro.ContainsKey(fileinfo[1].Value))
|
|
|
|
|
|
{
|
2016-03-30 13:36:52 -07:00
|
|
|
|
_logger.Error("The filename " + fileinfo[1].Value + " could not be mapped! Please check the mappings and try again");
|
2016-03-18 17:37:07 -07:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2016-03-18 01:17:39 -07:00
|
|
|
|
GroupCollection nointroInfo = Regex.Match(Remapping.NoIntro[fileinfo[1].Value], _remappedPattern).Groups;
|
|
|
|
|
|
|
|
|
|
|
|
manufacturer = nointroInfo[1].Value;
|
|
|
|
|
|
system = nointroInfo[2].Value;
|
|
|
|
|
|
source = "no-Intro";
|
2016-04-04 16:20:08 -07:00
|
|
|
|
if (fileinfo.Count < 2)
|
|
|
|
|
|
{
|
|
|
|
|
|
date = File.GetLastWriteTime(_filepath).ToString("yyyy-MM-dd HH:mm:ss");
|
|
|
|
|
|
}
|
2016-04-04 20:16:09 -07:00
|
|
|
|
else if (Regex.IsMatch(fileinfo[2].Value, _noIntroDatePattern))
|
2016-04-04 16:20:08 -07:00
|
|
|
|
{
|
|
|
|
|
|
datestring = fileinfo[2].Value;
|
2016-04-04 20:16:09 -07:00
|
|
|
|
GroupCollection niDateInfo = Regex.Match(datestring, _noIntroDatePattern).Groups;
|
2016-04-04 16:20:08 -07:00
|
|
|
|
date = niDateInfo[1].Value + "-" + niDateInfo[2].Value + "-" + niDateInfo[3].Value + " " +
|
|
|
|
|
|
niDateInfo[4].Value + ":" + niDateInfo[5].Value + ":" + niDateInfo[6].Value;
|
|
|
|
|
|
}
|
2016-04-04 20:16:09 -07:00
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
datestring = fileinfo[2].Value;
|
|
|
|
|
|
GroupCollection niDateInfo = Regex.Match(datestring, _noIntroSpecialDatePattern).Groups;
|
|
|
|
|
|
date = niDateInfo[1].Value + "-" + niDateInfo[2].Value + "-" + niDateInfo[3].Value + " 00:00:00";
|
2016-04-04 23:11:29 -07:00
|
|
|
|
}
|
2016-03-18 01:17:39 -07:00
|
|
|
|
break;
|
2016-03-29 13:41:55 -07:00
|
|
|
|
case DatType.Redump:
|
2016-03-18 17:37:07 -07:00
|
|
|
|
if (!Remapping.Redump.ContainsKey(fileinfo[1].Value))
|
|
|
|
|
|
{
|
2016-04-04 19:02:20 -07:00
|
|
|
|
// Handle special case mappings found only in Redump
|
2016-04-04 18:01:38 -07:00
|
|
|
|
fileinfo = Regex.Match(filename, _redumpBiosPattern).Groups;
|
|
|
|
|
|
|
|
|
|
|
|
if (!Remapping.Redump.ContainsKey(fileinfo[1].Value))
|
|
|
|
|
|
{
|
|
|
|
|
|
_logger.Error("The filename " + fileinfo[1].Value + " could not be mapped! Please check the mappings and try again");
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2016-03-18 17:37:07 -07:00
|
|
|
|
}
|
2016-03-18 01:17:39 -07:00
|
|
|
|
GroupCollection redumpInfo = Regex.Match(Remapping.Redump[fileinfo[1].Value], _remappedPattern).Groups;
|
|
|
|
|
|
|
|
|
|
|
|
manufacturer = redumpInfo[1].Value;
|
|
|
|
|
|
system = redumpInfo[2].Value;
|
|
|
|
|
|
source = "Redump";
|
|
|
|
|
|
datestring = fileinfo[2].Value;
|
2016-04-04 19:37:18 -07:00
|
|
|
|
if (Regex.IsMatch(datestring, _redumpDatePattern))
|
|
|
|
|
|
{
|
|
|
|
|
|
GroupCollection rdDateInfo = Regex.Match(datestring, _redumpDatePattern).Groups;
|
|
|
|
|
|
date = rdDateInfo[1].Value + "-" + rdDateInfo[2].Value + "-" + rdDateInfo[3].Value + " " +
|
|
|
|
|
|
rdDateInfo[4].Value + ":" + rdDateInfo[5].Value + ":" + rdDateInfo[6].Value;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
GroupCollection rdDateInfo = Regex.Match(datestring, _tosecDatePattern).Groups;
|
|
|
|
|
|
date = rdDateInfo[1].Value + "-" + rdDateInfo[2].Value + "-" + rdDateInfo[3].Value + " 00:00:00";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-03-18 01:17:39 -07:00
|
|
|
|
break;
|
2016-03-29 13:41:55 -07:00
|
|
|
|
case DatType.TOSEC:
|
2016-03-18 17:37:07 -07:00
|
|
|
|
if (!Remapping.TOSEC.ContainsKey(fileinfo[1].Value))
|
|
|
|
|
|
{
|
2016-03-30 12:52:35 -07:00
|
|
|
|
// Handle special case mappings found only in TOSEC
|
2016-04-05 16:11:58 -07:00
|
|
|
|
fileinfo = Regex.Match(filename, _tosecSpecialPatternA).Groups;
|
2016-03-30 12:52:35 -07:00
|
|
|
|
|
|
|
|
|
|
if (!Remapping.TOSEC.ContainsKey(fileinfo[1].Value))
|
|
|
|
|
|
{
|
2016-04-05 16:11:58 -07:00
|
|
|
|
fileinfo = Regex.Match(filename, _tosecSpecialPatternB).Groups;
|
|
|
|
|
|
|
|
|
|
|
|
if (!Remapping.TOSEC.ContainsKey(fileinfo[1].Value))
|
|
|
|
|
|
{
|
|
|
|
|
|
_logger.Error("The filename " + fileinfo[1].Value + " could not be mapped! Please check the mappings and try again");
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2016-03-30 12:52:35 -07:00
|
|
|
|
}
|
2016-03-18 17:37:07 -07:00
|
|
|
|
}
|
2016-03-18 01:17:39 -07:00
|
|
|
|
GroupCollection tosecInfo = Regex.Match(Remapping.TOSEC[fileinfo[1].Value], _remappedPattern).Groups;
|
|
|
|
|
|
|
|
|
|
|
|
manufacturer = tosecInfo[1].Value;
|
|
|
|
|
|
system = tosecInfo[2].Value;
|
|
|
|
|
|
source = "TOSEC";
|
|
|
|
|
|
datestring = fileinfo[2].Value;
|
|
|
|
|
|
GroupCollection toDateInfo = Regex.Match(datestring, _tosecDatePattern).Groups;
|
|
|
|
|
|
date = toDateInfo[1].Value + "-" + toDateInfo[2].Value + "-" + toDateInfo[3].Value + " 00:00:00";
|
|
|
|
|
|
break;
|
2016-03-29 13:41:55 -07:00
|
|
|
|
case DatType.TruRip:
|
2016-03-18 17:37:07 -07:00
|
|
|
|
if (!Remapping.TruRip.ContainsKey(fileinfo[1].Value))
|
|
|
|
|
|
{
|
2016-03-30 13:36:52 -07:00
|
|
|
|
_logger.Error("The filename " + fileinfo[1].Value + " could not be mapped! Please check the mappings and try again");
|
2016-03-18 17:37:07 -07:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2016-03-18 01:17:39 -07:00
|
|
|
|
GroupCollection truripInfo = Regex.Match(Remapping.TruRip[fileinfo[1].Value], _remappedPattern).Groups;
|
|
|
|
|
|
|
|
|
|
|
|
manufacturer = truripInfo[1].Value;
|
|
|
|
|
|
system = truripInfo[2].Value;
|
|
|
|
|
|
source = "trurip";
|
2016-03-18 19:47:10 -07:00
|
|
|
|
date = File.GetLastWriteTime(_filepath).ToString("yyyy-MM-dd HH:mm:ss");
|
2016-03-18 01:17:39 -07:00
|
|
|
|
break;
|
2016-03-29 13:41:55 -07:00
|
|
|
|
case DatType.Custom:
|
2016-03-18 01:17:39 -07:00
|
|
|
|
default:
|
|
|
|
|
|
manufacturer = fileinfo[1].Value;
|
|
|
|
|
|
system = fileinfo[2].Value;
|
|
|
|
|
|
source = fileinfo[3].Value;
|
|
|
|
|
|
datestring = fileinfo[4].Value;
|
|
|
|
|
|
|
|
|
|
|
|
GroupCollection cDateInfo = Regex.Match(datestring, _defaultDatePattern).Groups;
|
|
|
|
|
|
date = cDateInfo[1].Value + "-" + cDateInfo[2].Value + "-" + cDateInfo[3].Value + " " +
|
|
|
|
|
|
cDateInfo[4].Value + ":" + cDateInfo[5].Value + ":" + cDateInfo[6].Value;
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Check to make sure that the manufacturer and system are valid according to the database
|
|
|
|
|
|
int sysid = -1;
|
|
|
|
|
|
string query = "SELECT id FROM systems WHERE manufacturer='" + manufacturer + "' AND system='" + system +"'";
|
|
|
|
|
|
using (SQLiteConnection dbc = new SQLiteConnection(_connectionString))
|
|
|
|
|
|
{
|
|
|
|
|
|
dbc.Open();
|
|
|
|
|
|
using (SQLiteCommand slc = new SQLiteCommand(query, dbc))
|
|
|
|
|
|
{
|
|
|
|
|
|
using (SQLiteDataReader sldr = slc.ExecuteReader())
|
|
|
|
|
|
{
|
|
|
|
|
|
// If nothing is found, tell the user and exit
|
|
|
|
|
|
if (!sldr.HasRows)
|
|
|
|
|
|
{
|
2016-04-04 19:05:07 -07:00
|
|
|
|
_logger.Error("No suitable system for '" + filename + "' (" + manufacturer + " " + system + ") found! Please add the system and then try again.");
|
2016-03-18 01:17:39 -07:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Set the system ID from the first found value
|
|
|
|
|
|
sldr.Read();
|
|
|
|
|
|
sysid = sldr.GetInt32(0);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Check to make sure that the source is valid according to the database
|
|
|
|
|
|
int srcid = -1;
|
|
|
|
|
|
query = "SELECT id FROM sources WHERE name='" + source + "'";
|
|
|
|
|
|
using (SQLiteConnection dbc = new SQLiteConnection(_connectionString))
|
|
|
|
|
|
{
|
|
|
|
|
|
dbc.Open();
|
|
|
|
|
|
using (SQLiteCommand slc = new SQLiteCommand(query, dbc))
|
|
|
|
|
|
{
|
|
|
|
|
|
using (SQLiteDataReader sldr = slc.ExecuteReader())
|
|
|
|
|
|
{
|
|
|
|
|
|
// If nothing is found, tell the user and exit
|
|
|
|
|
|
if (!sldr.HasRows)
|
|
|
|
|
|
{
|
2016-03-30 14:18:37 -07:00
|
|
|
|
_logger.Error("No suitable source for '" + filename + "' found! Please add the source and then try again.");
|
2016-03-18 01:17:39 -07:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Set the source ID from the first found value
|
|
|
|
|
|
sldr.Read();
|
|
|
|
|
|
srcid = sldr.GetInt32(0);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-03-19 12:54:15 -07:00
|
|
|
|
// Attempt to load the current file as XML
|
2016-03-19 14:31:59 -07:00
|
|
|
|
bool superdat = false;
|
2016-03-19 12:54:15 -07:00
|
|
|
|
XmlDocument doc = new XmlDocument();
|
2016-03-18 01:17:39 -07:00
|
|
|
|
try
|
|
|
|
|
|
{
|
2016-03-19 12:54:15 -07:00
|
|
|
|
doc.LoadXml(File.ReadAllText(_filepath));
|
|
|
|
|
|
}
|
2016-04-06 00:01:54 -07:00
|
|
|
|
catch (XmlException)
|
2016-03-19 12:54:15 -07:00
|
|
|
|
{
|
|
|
|
|
|
doc.LoadXml(Converters.RomVaultToXML(File.ReadAllLines(_filepath)).ToString());
|
|
|
|
|
|
}
|
2016-03-18 15:01:00 -07:00
|
|
|
|
|
2016-03-19 12:54:15 -07:00
|
|
|
|
// Experimental looping using only XML parsing
|
|
|
|
|
|
XmlNode node = doc.FirstChild;
|
|
|
|
|
|
if (node != null && node.Name == "xml")
|
|
|
|
|
|
{
|
2016-03-22 14:58:48 -07:00
|
|
|
|
// Skip over everything that's not an element
|
|
|
|
|
|
while (node.NodeType != XmlNodeType.Element)
|
2016-03-19 14:18:52 -07:00
|
|
|
|
{
|
|
|
|
|
|
node = node.NextSibling;
|
|
|
|
|
|
}
|
2016-04-04 23:11:29 -07:00
|
|
|
|
}
|
2016-03-22 14:58:48 -07:00
|
|
|
|
|
|
|
|
|
|
// Once we find the main body, enter it
|
|
|
|
|
|
if (node != null && (node.Name == "datafile" || node.Name == "softwarelist"))
|
|
|
|
|
|
{
|
2016-03-19 12:54:15 -07:00
|
|
|
|
node = node.FirstChild;
|
|
|
|
|
|
}
|
2016-03-22 14:58:48 -07:00
|
|
|
|
|
|
|
|
|
|
// Skip the header if it exists
|
2016-03-19 12:54:15 -07:00
|
|
|
|
if (node != null && node.Name == "header")
|
|
|
|
|
|
{
|
2016-03-22 14:58:48 -07:00
|
|
|
|
// Check for SuperDAT mode
|
2016-03-19 14:31:59 -07:00
|
|
|
|
if (node.SelectSingleNode("name").InnerText.Contains(" - SuperDAT"))
|
|
|
|
|
|
{
|
|
|
|
|
|
superdat = true;
|
|
|
|
|
|
}
|
2016-03-22 14:58:48 -07:00
|
|
|
|
|
2016-03-29 12:11:58 -07:00
|
|
|
|
// Skip over anything that's not an element
|
2016-03-22 14:58:48 -07:00
|
|
|
|
while (node.NodeType != XmlNodeType.Element)
|
|
|
|
|
|
{
|
|
|
|
|
|
node = node.NextSibling;
|
|
|
|
|
|
}
|
2016-03-19 12:54:15 -07:00
|
|
|
|
}
|
2016-03-18 01:17:39 -07:00
|
|
|
|
|
2016-03-19 12:54:15 -07:00
|
|
|
|
while (node != null)
|
|
|
|
|
|
{
|
2016-03-22 14:58:48 -07:00
|
|
|
|
if (node.NodeType == XmlNodeType.Element && (node.Name == "machine" || node.Name == "game" || node.Name == "software"))
|
2016-03-19 02:16:26 -07:00
|
|
|
|
{
|
2016-03-19 14:18:52 -07:00
|
|
|
|
long gameid = -1;
|
2016-03-19 14:31:59 -07:00
|
|
|
|
string tempname = "";
|
|
|
|
|
|
if (node.Name == "software")
|
2016-03-19 14:18:52 -07:00
|
|
|
|
{
|
2016-03-19 14:31:59 -07:00
|
|
|
|
tempname = node.SelectSingleNode("description").InnerText;
|
2016-04-04 23:11:29 -07:00
|
|
|
|
}
|
2016-03-19 14:18:52 -07:00
|
|
|
|
else
|
|
|
|
|
|
{
|
2016-04-02 21:45:39 -07:00
|
|
|
|
// There are rare cases where a malformed XML will not have the required attributes. We can only skip them.
|
|
|
|
|
|
if (node.Attributes.Count == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
_logger.Error(@"A node with malformed XML has been found!
|
2016-04-04 23:11:29 -07:00
|
|
|
|
For RV DATs, please make sure that all names and descriptions are quoted.
|
|
|
|
|
|
For XML DATs, make sure that the DAT has all required information.");
|
2016-04-02 21:45:39 -07:00
|
|
|
|
node = node.NextSibling;
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
2016-03-19 14:31:59 -07:00
|
|
|
|
tempname = node.Attributes["name"].Value;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (superdat)
|
|
|
|
|
|
{
|
|
|
|
|
|
tempname = Regex.Match(tempname, @".*?\\(.*)").Groups[1].Value;
|
2016-03-19 14:18:52 -07:00
|
|
|
|
}
|
2016-03-19 14:31:59 -07:00
|
|
|
|
|
|
|
|
|
|
gameid = AddGame(sysid, tempname, srcid);
|
2016-03-19 14:18:52 -07:00
|
|
|
|
|
2016-03-19 12:54:15 -07:00
|
|
|
|
// Get the roms from the machine
|
|
|
|
|
|
if (node.HasChildNodes)
|
2016-03-18 01:17:39 -07:00
|
|
|
|
{
|
2016-03-19 12:54:15 -07:00
|
|
|
|
// If this node has children, traverse the children
|
|
|
|
|
|
foreach (XmlNode child in node.ChildNodes)
|
2016-03-18 15:01:00 -07:00
|
|
|
|
{
|
2016-03-19 12:54:15 -07:00
|
|
|
|
// If we find a rom or disk, add it
|
2016-04-05 13:59:03 -07:00
|
|
|
|
if (child.NodeType == XmlNodeType.Element && (child.Name == "rom" || child.Name == "disk"))
|
2016-03-19 02:16:26 -07:00
|
|
|
|
{
|
2016-04-03 18:06:37 -07:00
|
|
|
|
// Take care of hex-sized files
|
2016-04-04 13:37:20 -07:00
|
|
|
|
long size = -1;
|
2016-04-03 18:06:37 -07:00
|
|
|
|
if (child.Attributes["size"] != null && child.Attributes["size"].Value.Contains("0x"))
|
|
|
|
|
|
{
|
2016-04-04 13:37:20 -07:00
|
|
|
|
size = Convert.ToInt64(child.Attributes["size"].Value, 16);
|
2016-04-03 18:06:37 -07:00
|
|
|
|
}
|
|
|
|
|
|
else if (child.Attributes["size"] != null)
|
|
|
|
|
|
{
|
2016-04-04 13:37:20 -07:00
|
|
|
|
size = Int64.Parse(child.Attributes["size"].Value);
|
2016-04-04 23:11:29 -07:00
|
|
|
|
}
|
2016-04-03 18:06:37 -07:00
|
|
|
|
|
2016-03-29 14:49:03 -07:00
|
|
|
|
AddRom(
|
2016-03-19 12:54:15 -07:00
|
|
|
|
child.Name,
|
|
|
|
|
|
gameid,
|
|
|
|
|
|
child.Attributes["name"].Value,
|
|
|
|
|
|
date,
|
2016-04-03 18:06:37 -07:00
|
|
|
|
size,
|
2016-03-28 14:28:51 -07:00
|
|
|
|
(child.Attributes["crc"] != null ? child.Attributes["crc"].Value.ToLowerInvariant().Trim() : ""),
|
|
|
|
|
|
(child.Attributes["md5"] != null ? child.Attributes["md5"].Value.ToLowerInvariant().Trim() : ""),
|
|
|
|
|
|
(child.Attributes["sha1"] != null ? child.Attributes["sha1"].Value.ToLowerInvariant().Trim() : "")
|
2016-04-04 23:11:29 -07:00
|
|
|
|
);
|
2016-03-19 12:54:15 -07:00
|
|
|
|
}
|
|
|
|
|
|
// If we find the signs of a software list, traverse the children
|
2016-03-22 14:58:48 -07:00
|
|
|
|
else if (child.NodeType == XmlNodeType.Element && child.Name == "part" && child.HasChildNodes)
|
2016-03-19 12:54:15 -07:00
|
|
|
|
{
|
|
|
|
|
|
foreach (XmlNode part in child.ChildNodes)
|
2016-03-19 02:16:26 -07:00
|
|
|
|
{
|
2016-03-19 12:54:15 -07:00
|
|
|
|
// If we find a dataarea, traverse the children
|
2016-03-22 14:58:48 -07:00
|
|
|
|
if (part.NodeType == XmlNodeType.Element && part.Name == "dataarea")
|
2016-03-19 02:16:26 -07:00
|
|
|
|
{
|
2016-03-19 12:54:15 -07:00
|
|
|
|
foreach (XmlNode data in part.ChildNodes)
|
2016-03-19 02:16:26 -07:00
|
|
|
|
{
|
2016-03-19 12:54:15 -07:00
|
|
|
|
// If we find a rom or disk, add it
|
2016-04-02 21:52:04 -07:00
|
|
|
|
if (data.NodeType == XmlNodeType.Element && (data.Name == "rom" || data.Name == "disk") && data.Attributes["name"] != null)
|
2016-03-19 02:16:26 -07:00
|
|
|
|
{
|
2016-04-03 18:06:37 -07:00
|
|
|
|
// Take care of hex-sized files
|
2016-04-04 13:37:20 -07:00
|
|
|
|
long size = -1;
|
2016-04-03 18:06:37 -07:00
|
|
|
|
if (data.Attributes["size"] != null && data.Attributes["size"].Value.Contains("0x"))
|
|
|
|
|
|
{
|
2016-04-04 13:37:20 -07:00
|
|
|
|
size = Convert.ToInt64(data.Attributes["size"].Value, 16);
|
2016-04-03 18:06:37 -07:00
|
|
|
|
}
|
|
|
|
|
|
else if (data.Attributes["size"] != null)
|
|
|
|
|
|
{
|
2016-04-04 13:37:20 -07:00
|
|
|
|
size = Int64.Parse(data.Attributes["size"].Value);
|
2016-04-03 18:06:37 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-03-29 14:49:03 -07:00
|
|
|
|
AddRom(
|
2016-03-19 12:54:15 -07:00
|
|
|
|
data.Name,
|
|
|
|
|
|
gameid,
|
|
|
|
|
|
data.Attributes["name"].Value,
|
|
|
|
|
|
date,
|
2016-04-03 18:06:37 -07:00
|
|
|
|
size,
|
2016-03-28 14:28:51 -07:00
|
|
|
|
(data.Attributes["crc"] != null ? data.Attributes["crc"].Value.ToLowerInvariant().Trim() : ""),
|
|
|
|
|
|
(data.Attributes["md5"] != null ? data.Attributes["md5"].Value.ToLowerInvariant().Trim() : ""),
|
|
|
|
|
|
(data.Attributes["sha1"] != null ? data.Attributes["sha1"].Value.ToLowerInvariant().Trim() : "")
|
2016-03-19 12:54:15 -07:00
|
|
|
|
);
|
2016-03-19 02:16:26 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2016-03-18 15:01:00 -07:00
|
|
|
|
}
|
2016-03-18 01:17:39 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2016-03-19 12:54:15 -07:00
|
|
|
|
node = node.NextSibling;
|
2016-03-18 01:17:39 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-03-29 14:49:03 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Add a game to the database if it doesn't already exist
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="sysid">System ID for the game to be added with</param>
|
|
|
|
|
|
/// <param name="machinename">Name of the game to be added</param>
|
|
|
|
|
|
/// <param name="srcid">Source ID for the game to be added with</param>
|
|
|
|
|
|
/// <returns>Game ID of the inserted (or found) game, -1 on error</returns>
|
2016-03-18 01:17:39 -07:00
|
|
|
|
private long AddGame(int sysid, string machinename, int srcid)
|
|
|
|
|
|
{
|
|
|
|
|
|
// WoD gets rid of anything past the first "(" or "[" as the name, we will do the same
|
|
|
|
|
|
string stripPattern = @"(([[(].*[\)\]] )?([^([]+))";
|
|
|
|
|
|
Regex stripRegex = new Regex(stripPattern);
|
|
|
|
|
|
Match stripMatch = stripRegex.Match(machinename);
|
|
|
|
|
|
machinename = stripMatch.Groups[1].Value;
|
|
|
|
|
|
|
|
|
|
|
|
// Run the name through the filters to make sure that it's correct
|
|
|
|
|
|
machinename = Style.NormalizeChars(machinename);
|
|
|
|
|
|
machinename = Style.RussianToLatin(machinename);
|
|
|
|
|
|
machinename = Style.SearchPattern(machinename);
|
2016-03-21 21:33:12 -07:00
|
|
|
|
machinename = machinename.Trim();
|
2016-03-18 01:17:39 -07:00
|
|
|
|
|
|
|
|
|
|
long gameid = -1;
|
|
|
|
|
|
string query = "SELECT id FROM games WHERE system=" + sysid +
|
|
|
|
|
|
" AND name='" + machinename.Replace("'", "''") + "'" +
|
|
|
|
|
|
" AND source=" + srcid;
|
|
|
|
|
|
|
|
|
|
|
|
using (SQLiteConnection dbc = new SQLiteConnection(_connectionString))
|
|
|
|
|
|
{
|
|
|
|
|
|
dbc.Open();
|
|
|
|
|
|
using (SQLiteCommand slc = new SQLiteCommand(query, dbc))
|
|
|
|
|
|
{
|
|
|
|
|
|
using (SQLiteDataReader sldr = slc.ExecuteReader())
|
|
|
|
|
|
{
|
|
|
|
|
|
// If nothing is found, add the game and get the insert ID
|
|
|
|
|
|
if (!sldr.HasRows)
|
|
|
|
|
|
{
|
|
|
|
|
|
query = "INSERT INTO games (system, name, source)" +
|
|
|
|
|
|
" VALUES (" + sysid + ", '" + machinename.Replace("'", "''") + "', " + srcid + ")";
|
|
|
|
|
|
|
|
|
|
|
|
using (SQLiteCommand slc2 = new SQLiteCommand(query, dbc))
|
|
|
|
|
|
{
|
|
|
|
|
|
slc2.ExecuteNonQuery();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
query = "SELECT last_insert_rowid()";
|
|
|
|
|
|
using (SQLiteCommand slc2 = new SQLiteCommand(query, dbc))
|
|
|
|
|
|
{
|
|
|
|
|
|
gameid = (long)slc2.ExecuteScalar();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
// Otherwise, retrieve the ID
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
sldr.Read();
|
|
|
|
|
|
gameid = sldr.GetInt64(0);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return gameid;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-03-29 14:49:03 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Add a file to the database if it doesn't already exist
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="romtype">File type (either "rom" or "disk")</param>
|
|
|
|
|
|
/// <param name="gameid">ID of the parent game to be mapped to</param>
|
|
|
|
|
|
/// <param name="name">File name</param>
|
|
|
|
|
|
/// <param name="date">Last updated date</param>
|
|
|
|
|
|
/// <param name="size">File size in bytes</param>
|
|
|
|
|
|
/// <param name="crc">CRC32 hash of the file</param>
|
|
|
|
|
|
/// <param name="md5">MD5 hash of the file</param>
|
|
|
|
|
|
/// <param name="sha1">SHA-1 hash of the file</param>
|
|
|
|
|
|
/// <returns>True if the file exists or could be added, false on error</returns>
|
2016-04-04 13:37:20 -07:00
|
|
|
|
private bool AddRom(string romtype, long gameid, string name, string date, long size, string crc, string md5, string sha1)
|
2016-03-18 01:17:39 -07:00
|
|
|
|
{
|
2016-03-18 19:19:01 -07:00
|
|
|
|
// WOD origninally stripped out any subdirs from the imported files, we do the same
|
|
|
|
|
|
name = Path.GetFileName(name);
|
|
|
|
|
|
|
2016-03-18 01:17:39 -07:00
|
|
|
|
// Run the name through the filters to make sure that it's correct
|
|
|
|
|
|
name = Style.NormalizeChars(name);
|
|
|
|
|
|
name = Style.RussianToLatin(name);
|
2016-03-18 19:19:01 -07:00
|
|
|
|
name = Regex.Replace(name, @"(.*) \.(.*)", "$1.$2");
|
2016-03-18 01:17:39 -07:00
|
|
|
|
|
|
|
|
|
|
if (romtype != "rom" && romtype != "disk")
|
|
|
|
|
|
{
|
|
|
|
|
|
romtype = "rom";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Check to see if this exact file is in the database already
|
|
|
|
|
|
string query = @"
|
|
|
|
|
|
SELECT files.id FROM files
|
|
|
|
|
|
JOIN checksums
|
|
|
|
|
|
ON files.id=checksums.file
|
|
|
|
|
|
WHERE files.name='" + name.Replace("'", "''") + @"'
|
|
|
|
|
|
AND files.type='" + romtype + @"'
|
|
|
|
|
|
AND files.setid=" + gameid + " " +
|
2016-04-04 23:11:29 -07:00
|
|
|
|
" AND checksums.size=" + size +
|
|
|
|
|
|
" AND checksums.crc='" + crc + "'" +
|
|
|
|
|
|
" AND checksums.md5='" + md5 + "'" +
|
|
|
|
|
|
" AND checksums.sha1='" + sha1 + "'";
|
2016-03-18 01:17:39 -07:00
|
|
|
|
using (SQLiteConnection dbc = new SQLiteConnection(_connectionString))
|
|
|
|
|
|
{
|
|
|
|
|
|
dbc.Open();
|
|
|
|
|
|
using (SQLiteCommand slc = new SQLiteCommand(query, dbc))
|
|
|
|
|
|
{
|
|
|
|
|
|
using (SQLiteDataReader sldr = slc.ExecuteReader())
|
|
|
|
|
|
{
|
|
|
|
|
|
// If the file doesn't exist, add it
|
|
|
|
|
|
if (!sldr.HasRows)
|
|
|
|
|
|
{
|
|
|
|
|
|
query = @"
|
|
|
|
|
|
INSERT INTO files (setid, name, type, lastupdated)
|
|
|
|
|
|
VALUES (" + gameid + ", '" + name.Replace("'", "''") + "', '" + romtype + "', '" + date + "')";
|
|
|
|
|
|
using (SQLiteCommand slc2 = new SQLiteCommand(query, dbc))
|
|
|
|
|
|
{
|
|
|
|
|
|
int affected = slc2.ExecuteNonQuery();
|
|
|
|
|
|
|
|
|
|
|
|
// If the insert was successful, add the checksums for the file
|
|
|
|
|
|
if (affected >= 1)
|
|
|
|
|
|
{
|
|
|
|
|
|
query = "SELECT last_insert_rowid()";
|
|
|
|
|
|
long romid = -1;
|
|
|
|
|
|
using (SQLiteCommand slc3 = new SQLiteCommand(query, dbc))
|
|
|
|
|
|
{
|
|
|
|
|
|
romid = (long)slc3.ExecuteScalar();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-03-18 17:37:07 -07:00
|
|
|
|
query = @"INSERT INTO checksums (file, size, crc, md5, sha1) VALUES (" +
|
|
|
|
|
|
romid + ", " + size + ", '" + crc + "'" + ", '" + md5 + "'" + ", '" + sha1 + "')";
|
2016-03-18 01:17:39 -07:00
|
|
|
|
using (SQLiteCommand slc3 = new SQLiteCommand(query, dbc))
|
|
|
|
|
|
{
|
|
|
|
|
|
affected = slc3.ExecuteNonQuery();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// If the insert of the checksums failed, that's bad
|
|
|
|
|
|
if (affected < 1)
|
|
|
|
|
|
{
|
2016-03-30 13:36:52 -07:00
|
|
|
|
_logger.Error("There was an error adding checksums for " + name + " to the database!");
|
2016-03-18 01:17:39 -07:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
// Otherwise, something happened which is bad
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2016-03-30 13:36:52 -07:00
|
|
|
|
_logger.Error("There was an error adding " + name + " to the database!");
|
2016-03-18 01:17:39 -07:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|