diff --git a/SabreTools.Helper/Objects/Generate.cs b/SabreTools.Helper/Objects/Generate.cs deleted file mode 100644 index 7abfc15a..00000000 --- a/SabreTools.Helper/Objects/Generate.cs +++ /dev/null @@ -1,467 +0,0 @@ -using Mono.Data.Sqlite; -using SabreTools.Helper; -using System; -using System.Collections.Generic; -using System.IO; - -namespace SabreTools -{ - /// - /// Generate a DAT from the data in the database - /// - public class Generate : IGenerate - { - // Private instance variables - private string _systems; - private string _sources; - private string _outDir; - private string _connectionString; - private bool _norename; - private bool _old; - - // Private required variables - private Logger _logger; - - /// - /// Initialize a Generate object with the given information - /// - /// Comma-separated list of systems to be included in the DAT (blank means all) - /// Comma-separated list of sources to be included in the DAT (blank means all) - /// The output folder where the generated DAT will be put; blank means the current directory - /// Connection string for SQLite - /// Logger object for file or console output - /// True if files should not be renamed with system and/or source in merged mode (default false) - /// True if the output file should be in ClrMamePro format (default false) - public Generate(string systems, string sources, string outDir, string connectionString, Logger logger, bool norename = false, bool old = false) - { - _systems = systems; - _sources = sources; - _connectionString = connectionString; - _norename = norename; - _old = old; - _logger = logger; - - // Take care of special outfolder cases - _outDir = (outDir == "" ? Environment.CurrentDirectory + Path.DirectorySeparatorChar : - (!outDir.EndsWith(Path.DirectorySeparatorChar.ToString()) ? outDir + Path.DirectorySeparatorChar : outDir) - ); - if (_outDir != "" && !Directory.Exists(_outDir)) - { - Directory.CreateDirectory(_outDir); - } - } - - /// - /// Generate a DAT file that is represented by the data in the Generate object. - /// - /// True if the file could be created, false otherwise - public bool Export() - { - // Check to see if the source is an import-only. If so, tell the user and exit - int id = 0; - if (_sources != "" && Int32.TryParse(_sources, out id) && id <= 14) - { - _logger.Warning("This source (" + id + ") is import-only so a DAT cannot be created. We apologize for the inconvenience."); - return false; - } - - // Get the system name, if applicable - string systemname = ""; - if (_systems != "") - { - string query = "SELECT manufacturer, system FROM systems WHERE id in (" + _systems + ")"; - //string query = "SELECT manufacturer, name FROM system WHERE id in (" + _systems + ")"; - - using (SqliteConnection dbc = new SqliteConnection(_connectionString)) - { - dbc.Open(); - using (SqliteCommand slc = new SqliteCommand(query, dbc)) - { - using (SqliteDataReader sldr = slc.ExecuteReader()) - { - // If there are no games for this combination, return nothing - if (!sldr.HasRows) - { - _logger.Error("No system could be found with id in \"" + _systems + "\". Please check and try again."); - return false; - } - - // Retrieve and build the system name from all retrieved - int tempsize = 0; - while (sldr.Read() && tempsize < 3) - { - systemname += (tempsize == 0 ? - sldr.GetString(0) + " - " + sldr.GetString(1) : - "; " + sldr.GetString(0) + " - " + sldr.GetString(1)); - tempsize++; - } - - // If there are more than 3 systems, just put "etc." on the end - if (sldr.Read()) - { - systemname += "; etc."; - } - } - } - } - } - else - { - systemname = "ALL"; - } - - string sourcename = ""; - if (_sources != "") - { - string query = "SELECT name FROM sources WHERE id in (" + _sources + ")"; - //string query = "SELECT name FROM source WHERE id in (" + _sources + ")"; - - using (SqliteConnection dbc = new SqliteConnection(_connectionString)) - { - dbc.Open(); - using (SqliteCommand slc = new SqliteCommand(query, dbc)) - { - using (SqliteDataReader sldr = slc.ExecuteReader()) - { - // If there are no games for this combination, return nothing - if (!sldr.HasRows) - { - _logger.Error("No source could be found with id in \"" + _sources + "\". Please check and try again."); - return false; - } - - // Retrieve and build the source name from all retrieved - int tempsize = 0; - while (sldr.Read() && tempsize < 3) - { - sourcename += (tempsize == 0 ? sldr.GetString(0) : "; " + sldr.GetString(0)); - tempsize++; - } - - // If there are more than 3 systems, just put "etc." on the end - if (sldr.Read()) - { - sourcename += "; etc."; - } - } - } - } - } - else - { - sourcename = "Merged"; - } - - // Retrieve the list of processed roms - Dictionary> dict = ProcessRoms(); - - // If the output is null, nothing was found so return false - if (dict.Count == 0) - { - return false; - } - - // Create a name for the file based on the retrieved information - string version = DateTime.Now.ToString("yyyyMMddHHmmss"); - string intname = systemname + " (" + sourcename + ")"; - string datname = systemname + " (" + sourcename + " " + version + ")"; - - DatFile datdata = new DatFile - { - Name = intname, - Description = datname, - Version = version, - Date = version, - Category = "The Wizard of DATz", - Author = "The Wizard of DATz", - ForcePacking = ForcePacking.None, - OutputFormat = (_old ? OutputFormat.ClrMamePro : OutputFormat.Xml), - Files = dict, - }; - - return DatFile.WriteDatfile(datdata, _outDir, _logger); - } - - /// - /// Preprocess the rom data that is to be included in the outputted DAT - /// - /// A List of Rom objects containing all information about the files - public Dictionary> ProcessRoms() - { - Dictionary> roms = new Dictionary>(); - - // Check if we have listed sources or systems - bool sysmerged = (_systems == "" || _systems.Split(',').Length > 1); - bool srcmerged = (_sources == "" || _sources.Split(',').Length > 1); - bool merged = sysmerged || srcmerged; - - // BEGIN COMMENT - string query = @" -SELECT DISTINCT systems.manufacturer AS manufacturer, systems.system AS system, systems.id AS systemid, - sources.name AS source, sources.url AS url, sources.id AS sourceid, - games.name AS game, files.name AS name, files.type AS type, - checksums.size AS size, checksums.crc AS crc, checksums.md5 AS md5, checksums.sha1 AS sha1, - files.lastupdated AS lastupdated -FROM systems -JOIN games - ON systems.id=games.system -JOIN sources - ON games.source=sources.id -JOIN files - ON games.id=files.setid -JOIN checksums - ON files.id=checksums.file" + - (_systems != "" || _sources != "" ? "\nWHERE" : "") + - (_sources != "" ? " sources.id in (" + _sources + ")" : "") + - (_systems != "" && _sources != "" ? " AND" : "") + - (_systems != "" ? " systems.id in (" + _systems + ")" : "") + -"\nORDER BY " + - (merged ? "checksums.size, checksums.crc, systems.id, sources.id, files.lastupdated DESC, checksums.md5, checksums.sha1" - : "systems.id, sources.id, games.name, files.name"); - - using (SqliteConnection dbc = new SqliteConnection(_connectionString)) - { - dbc.Open(); - using (SqliteCommand slc = new SqliteCommand(query, dbc)) - { - using (SqliteDataReader sldr = slc.ExecuteReader()) - { - // If there are no games for this combination, return nothing - if (!sldr.HasRows) - { - _logger.Error("No games could be found with those inputs. Please check and try again."); - return null; - } - - // Retrieve and process the roms for merging - while (sldr.Read()) - { - DatItem temp; - string key = ""; - switch (sldr.GetString(8).ToLowerInvariant()) - { - case "disk": - temp = new Disk(sldr.GetString(7), sldr.GetString(11), sldr.GetString(12), false, sldr.GetString(13), sldr.GetString(6), - sldr.GetString(6), null, sldr.GetString(0), null, null, null, null, false, null, null, sldr.GetInt32(2), - sldr.GetString(1), sldr.GetInt32(5), sldr.GetString(3)); - - key = ((Disk)temp).MD5; - break; - case "rom": - default: - temp = new Rom(sldr.GetString(7), sldr.GetInt64(9), sldr.GetString(10), sldr.GetString(11), sldr.GetString(12), false, - sldr.GetString(13), sldr.GetString(6), null, sldr.GetString(6), null, sldr.GetString(0), null, null, null, null, false, - null, null, sldr.GetInt32(2), sldr.GetString(1), sldr.GetInt32(5), sldr.GetString(3)); - - key = ((Rom)temp).Size + "-" + ((Rom)temp).CRC; - break; - } - - // Rename the game associated if it's still valid and we allow renames - if (merged && !_norename) - { - temp.MachineName = temp.MachineName + - (sysmerged ? " [" + temp.Manufacturer + " - " + temp.System + "]" : "") + - (srcmerged ? " [" + temp.Source + "]" : ""); - } - - if (roms.ContainsKey(key)) - { - roms[key].Add(temp); - } - else - { - List templist = new List(); - templist.Add(temp); - roms.Add(key, templist); - } - } - } - } - } - - // If we're in a merged mode, merge and then resort by the correct parameters - if (merged) - { - foreach (string key in roms.Keys) - { - roms[key] = DatItem.Merge(roms[key], _logger); - } - } - // END COMMENT - - /* - // This block would replace the whole block above between BEGIN COMMENT and END COMMENT - string query = @" -SELECT hash.id AS id, hash.size AS size, hash.crc AS crc, hash.md5 AS md5, hash.sha1 AS sha1, - a.key AS key, a.value AS value, - source.id, source.name, source.url, - system.id, system.manufacturer, system.name -FROM hash -JOIN hashdata a - ON hash.id=a.hashid -JOIN hashdata b - ON a.hashid=b.hashid -JOIN gamesystem - ON b.value=gamesystem.game -JOIN gamesource - ON b.value=gamesource.game -JOIN system - ON gamesystem.systemid=system.id -JOIN source - ON gamesource.sourceid=source.id" + -(_systems != "" || _sources != "" ? "\nWHERE" : "") + - (_sources != "" ? " source.id in (" + _sources + ")" : "") + - (_systems != "" && _sources != "" ? " AND" : "") + - (_systems != "" ? " system.id in (" + _systems + ")" : "") + -"\nORDER BY hash.id"; - - using (SqliteConnection dbc = new SqliteConnection(_connectionString)) - { - dbc.Open(); - using (SqliteCommand slc = new SqliteCommand(query, dbc)) - { - using (SqliteDataReader sldr = slc.ExecuteReader()) - { - // If there are no games for this combination, return nothing - if (!sldr.HasRows) - { - _logger.Error("No games could be found with those inputs. Please check and try again."); - return null; - } - - // Retrieve and process the roms for merging - int systemid = -1, sourceid = -1; - long lastid = -1, size = -1; - string name = "", game = "", type = "", manufacturer = "", system = "", source = "", url = "", crc = "", md5 = "", sha1 = ""; - while (sldr.Read()) - { - // If the hash is different than the last - if (lastid != -1 && sldr.GetInt64(0) != lastid) - { - Rom temp = new Rom - { - Manufacturer = manufacturer, - System = system, - SystemID = systemid, - Source = source, - URL = url, - SourceID = sourceid, - Game = game, - Name = name, - Type = type, - Size = size, - CRC = crc, - MD5 = md5, - SHA1 = sha1, - }; - - // Rename the game associated if it's still valid and we allow renames - if (merged && !_norename) - { - temp.Machine = temp.Machine + - (sysmerged ? " [" + temp.Manufacturer + " - " + temp.System + "]" : "") + - (srcmerged ? " [" + temp.Source + "]" : ""); - } - - string key = temp.Size + "-" + temp.CRC; - if (roms.ContainsKey(key)) - { - roms[key].Add(temp); - } - else - { - List templist = new List(); - templist.Add(temp); - roms.Add(key, templist); - } - - // Reset the variables - game = ""; - name = ""; - type = ""; - } - - // Get all of the current ROM information - manufacturer = sldr.GetString(11); - system = sldr.GetString(12); - systemid = sldr.GetInt32(10); - source = sldr.GetString(8); - url = sldr.GetString(9); - sourceid = sldr.GetInt32(7); - size = sldr.GetInt64(1); - crc = sldr.GetString(2); - md5 = sldr.GetString(3); - sha1 = sldr.GetString(4); - - switch (sldr.GetString(5)) - { - case "game": - game = sldr.GetString(6); - break; - case "name": - name = sldr.GetString(6); - break; - case "type": - type = sldr.GetString(6); - break; - } - - lastid = sldr.GetInt64(0); - } - } - } - } - - // If we're in a merged mode, merge - if (merged) - { - foreach (string key in roms.Keys) - { - roms[key] = RomManipulation.Merge(roms[key]); - } - } - */ - - /* - // THIS CODE SHOULD BE PUT IN WriteToDatFromDict - - // Now check rename within games - string lastname = "", lastgame = ""; - for (int i = 0; i < roms.Count; i++) - { - Rom rom = roms[i]; - - // Now relable any roms that have the same name inside of the same game - bool samename = false, samegame = false; - if (rom.Name != "") - { - samename = (lastname == rom.Name); - } - if (rom.Machine != "") - { - samegame = (lastgame == rom.Machine); - } - - lastname = rom.Name; - lastgame = rom.Machine; - - // If the name and set are the same, rename it with whatever is different - if (samename && samegame) - { - rom.Name = Regex.Replace(rom.Name, @"^(.*)(\..*)", "$1(" + - (rom.CRC != "" ? rom.CRC : - (rom.MD5 != "" ? rom.MD5 : - (rom.SHA1 != "" ? rom.SHA1 : "Alt"))) + - ")$2"); - } - - // Assign back just in case - roms[i] = rom; - } - */ - - return roms; - } - } -} diff --git a/SabreTools.Helper/Objects/Import.cs b/SabreTools.Helper/Objects/Import.cs deleted file mode 100644 index 1287c4de..00000000 --- a/SabreTools.Helper/Objects/Import.cs +++ /dev/null @@ -1,652 +0,0 @@ -using Mono.Data.Sqlite; -using SabreTools.Helper; -using System.Collections.Generic; -using System.IO; -using System.Text.RegularExpressions; - -namespace SabreTools -{ - /// - /// Import data into the database from existing DATs - /// - public class Import : IImport - { - // Private instance variables - private string _filepath; - private string _connectionString; - private Logger _logger; - - /// - /// Initialize an Import object with the given information - /// - /// Path to the file that is going to be imported - /// Connection string for SQLite - /// Logger object for file or console output - public Import(string filepath, string connectionString, Logger logger) - { - _filepath = filepath; - _connectionString = connectionString; - _logger = logger; - } - - /// - /// Import the data from file into the database - /// - /// True if the data was imported, false otherwise - public bool UpdateDatabase() - { - // If file doesn't exist, error and return - if (!File.Exists(_filepath)) - { - _logger.Error("File '" + _filepath + "' doesn't exist"); - return false; - } - - // Determine which dattype we have - string filename = Path.GetFileName(_filepath); - GroupCollection fileinfo; - DatType type = DatType.none; - - if (Regex.IsMatch(filename, Constants.NonGoodPattern)) - { - fileinfo = Regex.Match(filename, Constants.NonGoodPattern).Groups; - type = DatType.NonGood; - } - else if (Regex.IsMatch(filename, Constants.NonGoodSpecialPattern)) - { - fileinfo = Regex.Match(filename, Constants.NonGoodSpecialPattern).Groups; - type = DatType.NonGood; - } - else if (Regex.IsMatch(filename, Constants.GoodPattern)) - { - fileinfo = Regex.Match(filename, Constants.GoodPattern).Groups; - type = DatType.Good; - } - else if (Regex.IsMatch(filename, Constants.GoodXmlPattern)) - { - fileinfo = Regex.Match(filename, Constants.GoodXmlPattern).Groups; - type = DatType.Good; - } - else if (Regex.IsMatch(filename, Constants.MaybeIntroPattern)) - { - fileinfo = Regex.Match(filename, Constants.MaybeIntroPattern).Groups; - type = DatType.MaybeIntro; - } - else if (Regex.IsMatch(filename, Constants.NoIntroPattern)) - { - fileinfo = Regex.Match(filename, Constants.NoIntroPattern).Groups; - type = DatType.NoIntro; - } - // For numbered DATs only - else if (Regex.IsMatch(filename, Constants.NoIntroNumberedPattern)) - { - fileinfo = Regex.Match(filename, Constants.NoIntroNumberedPattern).Groups; - type = DatType.NoIntro; - } - // For N-Gage and Gizmondo only - else if (Regex.IsMatch(filename, Constants.NoIntroSpecialPattern)) - { - fileinfo = Regex.Match(filename, Constants.NoIntroSpecialPattern).Groups; - type = DatType.NoIntro; - } - else if (Regex.IsMatch(filename, Constants.RedumpPattern)) - { - fileinfo = Regex.Match(filename, Constants.RedumpPattern).Groups; - type = DatType.Redump; - } - // For special BIOSes only - else if (Regex.IsMatch(filename, Constants.RedumpBiosPattern)) - { - fileinfo = Regex.Match(filename, Constants.RedumpBiosPattern).Groups; - type = DatType.Redump; - } - else if (Regex.IsMatch(filename, Constants.TosecPattern)) - { - fileinfo = Regex.Match(filename, Constants.TosecPattern).Groups; - type = DatType.TOSEC; - } - else if (Regex.IsMatch(filename, Constants.TruripPattern)) - { - fileinfo = Regex.Match(filename, Constants.TruripPattern).Groups; - type = DatType.TruRip; - } - else if (Regex.IsMatch(filename, Constants.ZandroPattern)) - { - filename = "Nintendo - Super Nintendo Entertainment System (Zandro " + File.GetLastWriteTime(_filepath).ToString("yyyyMMddHHmmss") + ").dat"; - fileinfo = Regex.Match(filename, Constants.DefaultPattern).Groups; - type = DatType.Custom; - } - else if (Regex.IsMatch(filename, Constants.DefaultPattern)) - { - fileinfo = Regex.Match(filename, Constants.DefaultPattern).Groups; - type = DatType.Custom; - } - else if (Regex.IsMatch(filename, Constants.DefaultSpecialPattern)) - { - fileinfo = Regex.Match(filename, Constants.DefaultSpecialPattern).Groups; - type = DatType.Custom; - } - else if (Regex.IsMatch(filename, Constants.MamePattern)) - { - fileinfo = Regex.Match(filename, Constants.MamePattern).Groups; - type = DatType.MAME; - } - // If the type is still unmatched, the data can't be imported yet - else - { - _logger.Warning("File " + filename + " cannot be imported at this time because it is not a known pattern.\nPlease try again with an unrenamed version."); - return false; - } - - _logger.Log("Type detected: " + type.ToString()); - - // 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) - { - case DatType.Good: - if (!Mappings.DatMaps["Good"].ContainsKey(fileinfo[1].Value)) - { - _logger.Warning("The filename " + fileinfo[1].Value + " could not be mapped! Please check the mappings and try again"); - return false; - } - GroupCollection goodInfo = Regex.Match(Mappings.DatMaps["Good"][fileinfo[1].Value], Constants.RemappedPattern).Groups; - - manufacturer = goodInfo[1].Value; - system = goodInfo[2].Value; - source = "Good"; - date = File.GetLastWriteTime(_filepath).ToString("yyyy-MM-dd HH:mm:ss"); - break; - case DatType.MAME: - if (!Mappings.DatMaps["MAME"].ContainsKey(fileinfo[1].Value)) - { - _logger.Warning("The filename " + fileinfo[1].Value + " could not be mapped! Please check the mappings and try again"); - return false; - } - GroupCollection mameInfo = Regex.Match(Mappings.DatMaps["MAME"][fileinfo[1].Value], Constants.RemappedPattern).Groups; - - manufacturer = mameInfo[1].Value; - system = mameInfo[2].Value; - source = "MAME"; - date = File.GetLastWriteTime(_filepath).ToString("yyyy-MM-dd HH:mm:ss"); - break; - case DatType.MaybeIntro: - if (!Mappings.DatMaps["MaybeIntro"].ContainsKey(fileinfo[1].Value)) - { - _logger.Warning("The filename " + fileinfo[1].Value + " could not be mapped! Please check the mappings and try again"); - return false; - } - GroupCollection maybeIntroInfo = Regex.Match(Mappings.DatMaps["MaybeIntro"][fileinfo[1].Value], Constants.RemappedPattern).Groups; - - manufacturer = maybeIntroInfo[1].Value; - system = maybeIntroInfo[2].Value; - source = "Maybe-Intro"; - datestring = fileinfo[2].Value; - GroupCollection miDateInfo = Regex.Match(datestring, Constants.NoIntroSpecialDatePattern).Groups; - date = miDateInfo[1].Value + "-" + miDateInfo[2].Value + "-" + miDateInfo[3].Value + " 00:00:00"; - break; - case DatType.NoIntro: - if (!Mappings.DatMaps["NoIntro"].ContainsKey(fileinfo[1].Value)) - { - _logger.Warning("The filename " + fileinfo[1].Value + " could not be mapped! Please check the mappings and try again"); - return false; - } - GroupCollection nointroInfo = Regex.Match(Mappings.DatMaps["NoIntro"][fileinfo[1].Value], Constants.RemappedPattern).Groups; - - manufacturer = nointroInfo[1].Value; - system = nointroInfo[2].Value; - source = "no-Intro"; - if (fileinfo.Count < 2) - { - date = File.GetLastWriteTime(_filepath).ToString("yyyy-MM-dd HH:mm:ss"); - } - else if (Regex.IsMatch(fileinfo[2].Value, Constants.NoIntroDatePattern)) - { - datestring = fileinfo[2].Value; - GroupCollection niDateInfo = Regex.Match(datestring, Constants.NoIntroDatePattern).Groups; - date = niDateInfo[1].Value + "-" + niDateInfo[2].Value + "-" + niDateInfo[3].Value + " " + - niDateInfo[4].Value + ":" + niDateInfo[5].Value + ":" + niDateInfo[6].Value; - } - else - { - datestring = fileinfo[2].Value; - GroupCollection niDateInfo = Regex.Match(datestring, Constants.NoIntroSpecialDatePattern).Groups; - date = niDateInfo[1].Value + "-" + niDateInfo[2].Value + "-" + niDateInfo[3].Value + " 00:00:00"; - } - break; - case DatType.NonGood: - if (!Mappings.DatMaps["NonGood"].ContainsKey(fileinfo[1].Value)) - { - _logger.Warning("The filename " + fileinfo[1].Value + " could not be mapped! Please check the mappings and try again"); - return false; - } - GroupCollection nonGoodInfo = Regex.Match(Mappings.DatMaps["NonGood"][fileinfo[1].Value], Constants.RemappedPattern).Groups; - - manufacturer = nonGoodInfo[1].Value; - system = nonGoodInfo[2].Value; - source = "NonGood"; - date = File.GetLastWriteTime(_filepath).ToString("yyyy-MM-dd HH:mm:ss"); - break; - case DatType.Redump: - if (!Mappings.DatMaps["Redump"].ContainsKey(fileinfo[1].Value)) - { - // Handle special case mappings found only in Redump - fileinfo = Regex.Match(filename, Constants.RedumpBiosPattern).Groups; - - if (!Mappings.DatMaps["Redump"].ContainsKey(fileinfo[1].Value)) - { - _logger.Warning("The filename " + fileinfo[1].Value + " could not be mapped! Please check the mappings and try again"); - return false; - } - } - GroupCollection redumpInfo = Regex.Match(Mappings.DatMaps["Redump"][fileinfo[1].Value], Constants.RemappedPattern).Groups; - - manufacturer = redumpInfo[1].Value; - system = redumpInfo[2].Value; - source = "Redump"; - datestring = fileinfo[2].Value; - if (Regex.IsMatch(datestring, Constants.RedumpDatePattern)) - { - GroupCollection rdDateInfo = Regex.Match(datestring, Constants.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, Constants.TosecDatePattern).Groups; - date = rdDateInfo[1].Value + "-" + rdDateInfo[2].Value + "-" + rdDateInfo[3].Value + " 00:00:00"; - } - - break; - case DatType.TOSEC: - if (!Mappings.DatMaps["TOSEC"].ContainsKey(fileinfo[1].Value)) - { - // Handle special case mappings found only in TOSEC - fileinfo = Regex.Match(filename, Constants.TosecSpecialPatternA).Groups; - - if (!Mappings.DatMaps["TOSEC"].ContainsKey(fileinfo[1].Value)) - { - fileinfo = Regex.Match(filename, Constants.TosecSpecialPatternB).Groups; - - if (!Mappings.DatMaps["TOSEC"].ContainsKey(fileinfo[1].Value)) - { - _logger.Warning("The filename " + fileinfo[1].Value + " could not be mapped! Please check the mappings and try again"); - return false; - } - } - } - GroupCollection tosecInfo = Regex.Match(Mappings.DatMaps["TOSEC"][fileinfo[1].Value], Constants.RemappedPattern).Groups; - - manufacturer = tosecInfo[1].Value; - system = tosecInfo[2].Value; - source = "TOSEC"; - datestring = fileinfo[2].Value; - GroupCollection toDateInfo = Regex.Match(datestring, Constants.TosecDatePattern).Groups; - date = toDateInfo[1].Value + "-" + toDateInfo[2].Value + "-" + toDateInfo[3].Value + " 00:00:00"; - break; - case DatType.TruRip: - if (!Mappings.DatMaps["TruRip"].ContainsKey(fileinfo[1].Value)) - { - _logger.Warning("The filename " + fileinfo[1].Value + " could not be mapped! Please check the mappings and try again"); - return false; - } - GroupCollection truripInfo = Regex.Match(Mappings.DatMaps["TruRip"][fileinfo[1].Value], Constants.RemappedPattern).Groups; - - manufacturer = truripInfo[1].Value; - system = truripInfo[2].Value; - source = "trurip"; - date = File.GetLastWriteTime(_filepath).ToString("yyyy-MM-dd HH:mm:ss"); - break; - case DatType.Custom: - default: - manufacturer = fileinfo[1].Value; - system = fileinfo[2].Value; - source = fileinfo[3].Value; - datestring = fileinfo[4].Value; - - GroupCollection cDateInfo = Regex.Match(datestring, Constants.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 +"'"; - //string query = "SELECT id FROM system WHERE manufacturer='" + manufacturer + "' AND name='" + 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) - { - _logger.Error("No suitable system for '" + filename + "' (" + manufacturer + " " + system + ") found! Please add the system and then try again."); - 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 + "'"; - //query = "SELECT id FROM source 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) - { - _logger.Error("No suitable source for '" + filename + "' found! Please add the source and then try again."); - return false; - } - - // Set the source ID from the first found value - sldr.Read(); - srcid = sldr.GetInt32(0); - } - } - } - - // Get all roms that are found in the DAT to see what needs to be added - DatFile datdata = new DatFile(); - DatFile.Parse(_filepath, sysid, srcid, ref datdata, _logger); - - // Sort inputted roms into games - SortedDictionary> sortable = new SortedDictionary>(); - long count = 0; - foreach (List roms in datdata.Files.Values) - { - List newroms = roms; - if (datdata.MergeRoms) - { - newroms = DatItem.Merge(newroms, _logger); - } - - foreach (Rom rom in newroms) - { - count++; - string key = rom.SystemID.ToString().PadLeft(10, '0') + "-" + rom.SourceID.ToString().PadLeft(10, '0') + "-" + rom.MachineName.ToLowerInvariant(); - if (sortable.ContainsKey(key)) - { - sortable[key].Add(rom); - } - else - { - List temp = new List(); - temp.Add(rom); - sortable.Add(key, temp); - } - } - } - - // Loop over all roms, checking for adds - foreach (string key in sortable.Keys) - { - List roms = sortable[key]; - DatItem.Sort(ref roms, true); - - long gameid = -1; - using (SqliteConnection dbc = new SqliteConnection(_connectionString)) - { - dbc.Open(); - - // For each game, check for a new ID - gameid = AddGame(sysid, roms[0].MachineName, srcid, dbc); - - foreach (Rom rom in roms) - { - // BEGIN COMMENT - // Try to add the rom with the game information - AddRom(rom, gameid, date, dbc); - // END COMMENT - - /* - // Try to add the romdata - AddHash(rom, sysid, srcid, date, dbc); - */ - } - } - } - - return true; - } - - /// - /// Add a game to the database if it doesn't already exist - /// - /// System ID for the game to be added with - /// Name of the game to be added - /// Source ID for the game to be added with - /// SQLite database connection to use - /// Game ID of the inserted (or found) game, -1 on error - private long AddGame(int sysid, string machinename, int srcid, SqliteConnection dbc) - { - // 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); - machinename = machinename.Trim(); - - long gameid = -1; - string query = "SELECT id FROM games WHERE system=" + sysid + - " AND name='" + machinename.Replace("'", "''") + "'" + - " AND source=" + srcid; - - 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_insertConstants.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; - } - - /// - /// Add a file to the database if it doesn't already exist - /// - /// Rom object representing the rom - /// ID of the parent game to be mapped to - /// Last updated date - /// SQLite database connection to use - /// True if the file exists or could be added, false on error - private bool AddRom(Rom rom, long gameid, string date, SqliteConnection dbc) - { - // WOD origninally stripped out any subdirs from the imported files, we do the same - rom.Name = Path.GetFileName(rom.Name); - - // Run the name through the filters to make sure that it's correct - rom.Name = Style.NormalizeChars(rom.Name); - rom.Name = Style.RussianToLatin(rom.Name); - rom.Name = Regex.Replace(rom.Name, @"(.*) \.(.*)", "$1.$2"); - - if (rom.Type != ItemType.Rom && rom.Type != ItemType.Disk) - { - rom.Type = ItemType.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='" + rom.Name.Replace("'", "''") + @"' - AND files.type='" + rom.Type + @"' - AND files.setid=" + gameid + - " AND checksums.size=" + rom.Size + - " AND checksums.crc='" + rom.CRC + "'" + - " AND checksums.md5='" + rom.MD5 + "'" + - " AND checksums.sha1='" + rom.SHA1 + "'"; - - using (SqliteCommand slc = new SqliteCommand(query, dbc)) - { - using (SqliteDataReader sldr = slc.ExecuteReader()) - { - // If the file doesn't exist, add it with its checksums - if (!sldr.HasRows) - { - query = @"BEGIN; -INSERT INTO files (setid, name, type, lastupdated) -VALUES (" + gameid + ", '" + rom.Name.Replace("'", "''") + "', '" + rom.Type + "', '" + date + @"'); -INSERT INTO checksums (file, size, crc, md5, sha1) -VALUES ((SELECT last_insertConstants.Rowid()), " + rom.Size + ", '" + rom.CRC + "'" + ", '" + rom.MD5 + "'" + ", '" + rom.SHA1 + @"'); -COMMIT;"; - using (SqliteCommand slc2 = new SqliteCommand(query, dbc)) - { - int affected = slc2.ExecuteNonQuery(); - - // If the insert was unsuccessful, something bad happened - if (affected < 1) - { - _logger.Error("There was an error adding " + rom.Name + " to the database!"); - return false; - } - } - } - } - } - - return true; - } - - /// - /// Add a hash to the database if it doesn't exist already - /// - /// Rom object representing the rom - /// System ID for the game to be added with - /// Source ID for the game to be added with - /// Last updated date - /// SQLite database connection to use - /// True if the hash exists or could be added, false on error - /// This is currently unused. It is a test method for the new SabreTools DB schema - private bool AddHash(Rom rom, int sysid, int srcid, string date, SqliteConnection dbc) - { - // Process the game name - - // 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(rom.MachineName); - rom.MachineName = stripMatch.Groups[1].Value; - - // Run the name through the filters to make sure that it's correct - rom.MachineName = Style.NormalizeChars(rom.MachineName); - rom.MachineName = Style.RussianToLatin(rom.MachineName); - rom.MachineName = Style.SearchPattern(rom.MachineName); - rom.MachineName = rom.MachineName.Trim(); - - // Process the rom name - - // WOD origninally stripped out any subdirs from the imported files, we do the same - rom.Name = Path.GetFileName(rom.Name); - - // Run the name through the filters to make sure that it's correct - rom.Name = Style.NormalizeChars(rom.Name); - rom.Name = Style.RussianToLatin(rom.Name); - rom.Name = Regex.Replace(rom.Name, @"(.*) \.(.*)", "$1.$2"); - - // Retrieve or insert the hash - long hashid = -1; - string query = "SELECT id FROM hash WHERE size=" + rom.Size + " AND crc='" + rom.CRC + "' AND md5='" + rom.MD5 + "' AND sha1='" + rom.SHA1 + "'"; - using (SqliteCommand slc = new SqliteCommand(query, dbc)) - { - using (SqliteDataReader sldr = slc.ExecuteReader()) - { - // If nothing is found, add the hash and get the insert ID - if (!sldr.HasRows) - { - query = "INSERT INTO hash (size, crc, md5, sha1)" + - " VALUES (" + rom.Size + ", '" + rom.CRC + "', '" + rom.MD5 + "', '" + rom.SHA1 + "')"; - - using (SqliteCommand slc2 = new SqliteCommand(query, dbc)) - { - slc2.ExecuteNonQuery(); - } - - query = "SELECT last_insertConstants.Rowid()"; - using (SqliteCommand slc2 = new SqliteCommand(query, dbc)) - { - hashid = (long)slc2.ExecuteScalar(); - } - } - // Otherwise, retrieve the ID - else - { - sldr.Read(); - hashid = sldr.GetInt64(0); - } - } - } - - // Ignore or insert the file and game - query = @"BEGIN; -INSERT OR IGNORE INTO hashdata (hashid, key, value) VALUES " + - "(" + hashid + ", 'name', '" + rom.Name.Replace("'", "''") + "'), " + - "(" + hashid + ", 'game', '" + rom.MachineName.Replace("'", "''") + "'), " + - "(" + hashid + ", 'type', '" + rom.Type + "'), " + - "(" + hashid + ", 'lastupdated', '" + date + @"'); -INSERT OR IGNORE INTO gamesystem (game, systemid) VALUES ('" + rom.MachineName.Replace("'", "''") + "', " + sysid + @"); -INSERT OR IGNORE INTO gamesource (game, sourceid) VALUES ('" + rom.MachineName.Replace("'", "''") + "', " + srcid + @"); -COMMIT;"; - - using (SqliteCommand slc = new SqliteCommand(query, dbc)) - { - int ret = slc.ExecuteNonQuery(); - if ((SQLiteErrorCode)ret == SQLiteErrorCode.Error) - { - _logger.Error("A SQLite error has occurred: " + ((SQLiteErrorCode)ret).ToString()); - return false; - } - } - - return true; - } - } -} diff --git a/SabreTools.Helper/SabreTools.Helper.csproj b/SabreTools.Helper/SabreTools.Helper.csproj index 24aa4802..8f9c4a3c 100644 --- a/SabreTools.Helper/SabreTools.Helper.csproj +++ b/SabreTools.Helper/SabreTools.Helper.csproj @@ -108,10 +108,8 @@ - -