2016-06-13 23:54:13 -07:00
|
|
|
|
using Mono.Data.Sqlite;
|
|
|
|
|
|
using SabreTools.Helper;
|
|
|
|
|
|
using System;
|
2016-03-18 01:17:39 -07:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.IO;
|
2016-03-28 17:54:24 -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>
|
|
|
|
|
|
/// Generate a DAT from the data in the database
|
|
|
|
|
|
/// </summary>
|
2016-09-19 18:04:24 -07:00
|
|
|
|
public class Generate : IGenerate
|
2016-03-18 01:17:39 -07:00
|
|
|
|
{
|
|
|
|
|
|
// Private instance variables
|
2016-03-24 17:38:08 -07:00
|
|
|
|
private string _systems;
|
|
|
|
|
|
private string _sources;
|
2016-09-16 16:35:58 -07:00
|
|
|
|
private string _outDir;
|
2016-03-18 01:17:39 -07:00
|
|
|
|
private string _connectionString;
|
2016-03-24 13:23:13 -07:00
|
|
|
|
private bool _norename;
|
2016-03-18 01:17:39 -07:00
|
|
|
|
private bool _old;
|
|
|
|
|
|
|
|
|
|
|
|
// Private required variables
|
2016-03-28 18:40:35 -07:00
|
|
|
|
private Logger _logger;
|
2016-03-18 01:17:39 -07:00
|
|
|
|
|
2016-03-29 14:49:03 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Initialize a Generate object with the given information
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="systems">Comma-separated list of systems to be included in the DAT (blank means all)</param>
|
|
|
|
|
|
/// <param name="sources">Comma-separated list of sources to be included in the DAT (blank means all)</param>
|
2016-09-16 16:35:58 -07:00
|
|
|
|
/// <param name="outDir">The output folder where the generated DAT will be put; blank means the current directory</param>
|
2016-03-29 14:49:03 -07:00
|
|
|
|
/// <param name="connectionString">Connection string for SQLite</param>
|
|
|
|
|
|
/// <param name="logger">Logger object for file or console output</param>
|
|
|
|
|
|
/// <param name="norename">True if files should not be renamed with system and/or source in merged mode (default false)</param>
|
2016-04-20 21:55:28 -07:00
|
|
|
|
/// <param name="old">True if the output file should be in ClrMamePro format (default false)</param>
|
2016-09-16 16:35:58 -07:00
|
|
|
|
public Generate(string systems, string sources, string outDir, string connectionString, Logger logger, bool norename = false, bool old = false)
|
2016-03-18 01:17:39 -07:00
|
|
|
|
{
|
2016-03-24 14:07:32 -07:00
|
|
|
|
_systems = systems;
|
|
|
|
|
|
_sources = sources;
|
2016-03-18 01:17:39 -07:00
|
|
|
|
_connectionString = connectionString;
|
2016-03-24 13:23:13 -07:00
|
|
|
|
_norename = norename;
|
2016-03-18 01:17:39 -07:00
|
|
|
|
_old = old;
|
2016-03-28 17:54:24 -07:00
|
|
|
|
_logger = logger;
|
2016-03-18 01:17:39 -07:00
|
|
|
|
|
2016-03-31 16:24:58 -07:00
|
|
|
|
// Take care of special outfolder cases
|
2016-09-16 16:35:58 -07:00
|
|
|
|
_outDir = (outDir == "" ? Environment.CurrentDirectory + Path.DirectorySeparatorChar :
|
|
|
|
|
|
(!outDir.EndsWith(Path.DirectorySeparatorChar.ToString()) ? outDir + Path.DirectorySeparatorChar : outDir)
|
2016-03-31 16:24:58 -07:00
|
|
|
|
);
|
2016-09-16 16:35:58 -07:00
|
|
|
|
if (_outDir != "" && !Directory.Exists(_outDir))
|
2016-03-31 16:40:28 -07:00
|
|
|
|
{
|
2016-09-16 16:35:58 -07:00
|
|
|
|
Directory.CreateDirectory(_outDir);
|
2016-03-31 16:40:28 -07:00
|
|
|
|
}
|
2016-03-18 01:17:39 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-03-29 14:49:03 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Generate a DAT file that is represented by the data in the Generate object.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns>True if the file could be created, false otherwise</returns>
|
2016-03-18 01:17:39 -07:00
|
|
|
|
public bool Export()
|
|
|
|
|
|
{
|
2016-03-24 17:46:47 -07:00
|
|
|
|
// 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)
|
2016-04-04 23:11:29 -07:00
|
|
|
|
{
|
2016-03-31 16:40:28 -07:00
|
|
|
|
_logger.Warning("This source (" + id + ") is import-only so a DAT cannot be created. We apologize for the inconvenience.");
|
2016-03-24 17:46:47 -07:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-03-18 01:17:39 -07:00
|
|
|
|
// Get the system name, if applicable
|
2016-03-24 17:38:08 -07:00
|
|
|
|
string systemname = "";
|
|
|
|
|
|
if (_systems != "")
|
2016-03-18 01:17:39 -07:00
|
|
|
|
{
|
2016-03-24 17:38:08 -07:00
|
|
|
|
string query = "SELECT manufacturer, system FROM systems WHERE id in (" + _systems + ")";
|
2016-04-22 00:39:46 -07:00
|
|
|
|
//string query = "SELECT manufacturer, name FROM system WHERE id in (" + _systems + ")";
|
|
|
|
|
|
|
2016-04-20 17:02:15 -07:00
|
|
|
|
using (SqliteConnection dbc = new SqliteConnection(_connectionString))
|
2016-03-18 01:17:39 -07:00
|
|
|
|
{
|
|
|
|
|
|
dbc.Open();
|
2016-04-20 17:02:15 -07:00
|
|
|
|
using (SqliteCommand slc = new SqliteCommand(query, dbc))
|
2016-03-18 01:17:39 -07:00
|
|
|
|
{
|
2016-04-20 17:02:15 -07:00
|
|
|
|
using (SqliteDataReader sldr = slc.ExecuteReader())
|
2016-03-18 01:17:39 -07:00
|
|
|
|
{
|
|
|
|
|
|
// If there are no games for this combination, return nothing
|
|
|
|
|
|
if (!sldr.HasRows)
|
|
|
|
|
|
{
|
2016-03-30 13:36:52 -07:00
|
|
|
|
_logger.Error("No system could be found with id in \"" + _systems + "\". Please check and try again.");
|
2016-03-18 01:17:39 -07:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-03-24 14:23:12 -07:00
|
|
|
|
// 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) :
|
2016-03-24 17:38:08 -07:00
|
|
|
|
"; " + sldr.GetString(0) + " - " + sldr.GetString(1));
|
2016-03-24 14:23:12 -07:00
|
|
|
|
tempsize++;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// If there are more than 3 systems, just put "etc." on the end
|
|
|
|
|
|
if (sldr.Read())
|
|
|
|
|
|
{
|
2016-03-24 17:38:08 -07:00
|
|
|
|
systemname += "; etc.";
|
2016-03-24 14:23:12 -07:00
|
|
|
|
}
|
2016-03-18 01:17:39 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2016-03-24 17:38:08 -07:00
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
systemname = "ALL";
|
|
|
|
|
|
}
|
2016-03-18 01:17:39 -07:00
|
|
|
|
|
2016-03-24 17:38:08 -07:00
|
|
|
|
string sourcename = "";
|
|
|
|
|
|
if (_sources != "")
|
2016-03-18 01:17:39 -07:00
|
|
|
|
{
|
2016-03-24 17:38:08 -07:00
|
|
|
|
string query = "SELECT name FROM sources WHERE id in (" + _sources + ")";
|
2016-04-22 00:39:46 -07:00
|
|
|
|
//string query = "SELECT name FROM source WHERE id in (" + _sources + ")";
|
2016-03-29 21:46:27 -07:00
|
|
|
|
|
2016-04-20 17:02:15 -07:00
|
|
|
|
using (SqliteConnection dbc = new SqliteConnection(_connectionString))
|
2016-03-18 01:17:39 -07:00
|
|
|
|
{
|
|
|
|
|
|
dbc.Open();
|
2016-04-20 17:02:15 -07:00
|
|
|
|
using (SqliteCommand slc = new SqliteCommand(query, dbc))
|
2016-03-18 01:17:39 -07:00
|
|
|
|
{
|
2016-04-20 17:02:15 -07:00
|
|
|
|
using (SqliteDataReader sldr = slc.ExecuteReader())
|
2016-03-18 01:17:39 -07:00
|
|
|
|
{
|
|
|
|
|
|
// If there are no games for this combination, return nothing
|
|
|
|
|
|
if (!sldr.HasRows)
|
|
|
|
|
|
{
|
2016-03-30 13:36:52 -07:00
|
|
|
|
_logger.Error("No source could be found with id in \"" + _sources + "\". Please check and try again.");
|
2016-03-18 01:17:39 -07:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-03-24 14:23:12 -07:00
|
|
|
|
// Retrieve and build the source name from all retrieved
|
|
|
|
|
|
int tempsize = 0;
|
|
|
|
|
|
while (sldr.Read() && tempsize < 3)
|
|
|
|
|
|
{
|
2016-03-24 17:38:08 -07:00
|
|
|
|
sourcename += (tempsize == 0 ? sldr.GetString(0) : "; " + sldr.GetString(0));
|
2016-03-24 14:23:12 -07:00
|
|
|
|
tempsize++;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// If there are more than 3 systems, just put "etc." on the end
|
|
|
|
|
|
if (sldr.Read())
|
|
|
|
|
|
{
|
2016-03-24 17:38:08 -07:00
|
|
|
|
sourcename += "; etc.";
|
2016-03-24 14:23:12 -07:00
|
|
|
|
}
|
2016-03-18 01:17:39 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2016-03-24 17:38:08 -07:00
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
sourcename = "Merged";
|
|
|
|
|
|
}
|
2016-03-18 01:17:39 -07:00
|
|
|
|
|
|
|
|
|
|
// Retrieve the list of processed roms
|
2016-09-19 18:04:24 -07:00
|
|
|
|
Dictionary<string, List<DatItem>> dict = ProcessRoms();
|
2016-03-18 01:17:39 -07:00
|
|
|
|
|
2016-03-18 15:01:00 -07:00
|
|
|
|
// If the output is null, nothing was found so return false
|
2016-05-16 21:52:49 -07:00
|
|
|
|
if (dict.Count == 0)
|
2016-03-18 15:01:00 -07:00
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-03-18 01:17:39 -07:00
|
|
|
|
// Create a name for the file based on the retrieved information
|
|
|
|
|
|
string version = DateTime.Now.ToString("yyyyMMddHHmmss");
|
2016-04-07 11:17:47 -07:00
|
|
|
|
string intname = systemname + " (" + sourcename + ")";
|
2016-03-18 01:17:39 -07:00
|
|
|
|
string datname = systemname + " (" + sourcename + " " + version + ")";
|
|
|
|
|
|
|
2016-09-19 20:08:25 -07:00
|
|
|
|
DatFile datdata = new DatFile
|
2016-05-16 21:52:49 -07:00
|
|
|
|
{
|
|
|
|
|
|
Name = intname,
|
|
|
|
|
|
Description = datname,
|
|
|
|
|
|
Version = version,
|
|
|
|
|
|
Date = version,
|
|
|
|
|
|
Category = "The Wizard of DATz",
|
|
|
|
|
|
Author = "The Wizard of DATz",
|
|
|
|
|
|
ForcePacking = ForcePacking.None,
|
2016-09-09 15:51:47 -07:00
|
|
|
|
OutputFormat = (_old ? OutputFormat.ClrMamePro : OutputFormat.Xml),
|
2016-09-01 23:41:19 -07:00
|
|
|
|
Files = dict,
|
2016-05-16 21:52:49 -07:00
|
|
|
|
};
|
|
|
|
|
|
|
2016-09-19 20:08:25 -07:00
|
|
|
|
return DatFile.WriteDatfile(datdata, _outDir, _logger);
|
2016-03-18 01:17:39 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-03-29 14:49:03 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Preprocess the rom data that is to be included in the outputted DAT
|
|
|
|
|
|
/// </summary>
|
2016-09-01 23:41:19 -07:00
|
|
|
|
/// <returns>A List of Rom objects containing all information about the files</returns>
|
2016-09-19 18:04:24 -07:00
|
|
|
|
public Dictionary<string, List<DatItem>> ProcessRoms()
|
2016-03-18 01:17:39 -07:00
|
|
|
|
{
|
2016-09-19 18:04:24 -07:00
|
|
|
|
Dictionary<string, List<DatItem>> roms = new Dictionary<string, List<DatItem>>();
|
2016-03-18 01:17:39 -07:00
|
|
|
|
|
2016-03-24 17:38:08 -07:00
|
|
|
|
// Check if we have listed sources or systems
|
2016-03-24 20:56:04 -07:00
|
|
|
|
bool sysmerged = (_systems == "" || _systems.Split(',').Length > 1);
|
|
|
|
|
|
bool srcmerged = (_sources == "" || _sources.Split(',').Length > 1);
|
2016-03-18 01:17:39 -07:00
|
|
|
|
bool merged = sysmerged || srcmerged;
|
|
|
|
|
|
|
2016-04-22 11:08:02 -07:00
|
|
|
|
// BEGIN COMMENT
|
2016-03-18 01:17:39 -07:00
|
|
|
|
string query = @"
|
2016-03-22 00:12:36 -07:00
|
|
|
|
SELECT DISTINCT systems.manufacturer AS manufacturer, systems.system AS system, systems.id AS systemid,
|
2016-03-18 01:17:39 -07:00
|
|
|
|
sources.name AS source, sources.url AS url, sources.id AS sourceid,
|
2016-04-17 14:33:04 -07:00
|
|
|
|
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
|
2016-03-18 01:17:39 -07:00
|
|
|
|
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" +
|
2016-03-24 17:38:08 -07:00
|
|
|
|
(_systems != "" || _sources != "" ? "\nWHERE" : "") +
|
|
|
|
|
|
(_sources != "" ? " sources.id in (" + _sources + ")" : "") +
|
|
|
|
|
|
(_systems != "" && _sources != "" ? " AND" : "") +
|
|
|
|
|
|
(_systems != "" ? " systems.id in (" + _systems + ")" : "") +
|
2016-03-22 00:12:36 -07:00
|
|
|
|
"\nORDER BY " +
|
2016-04-17 11:12:37 -07:00
|
|
|
|
(merged ? "checksums.size, checksums.crc, systems.id, sources.id, files.lastupdated DESC, checksums.md5, checksums.sha1"
|
2016-03-22 00:12:36 -07:00
|
|
|
|
: "systems.id, sources.id, games.name, files.name");
|
2016-03-19 15:44:35 -07:00
|
|
|
|
|
2016-04-20 17:02:15 -07:00
|
|
|
|
using (SqliteConnection dbc = new SqliteConnection(_connectionString))
|
2016-03-18 01:17:39 -07:00
|
|
|
|
{
|
|
|
|
|
|
dbc.Open();
|
2016-04-20 17:02:15 -07:00
|
|
|
|
using (SqliteCommand slc = new SqliteCommand(query, dbc))
|
2016-03-18 01:17:39 -07:00
|
|
|
|
{
|
2016-04-20 17:02:15 -07:00
|
|
|
|
using (SqliteDataReader sldr = slc.ExecuteReader())
|
2016-03-18 01:17:39 -07:00
|
|
|
|
{
|
|
|
|
|
|
// If there are no games for this combination, return nothing
|
|
|
|
|
|
if (!sldr.HasRows)
|
|
|
|
|
|
{
|
2016-03-30 13:36:52 -07:00
|
|
|
|
_logger.Error("No games could be found with those inputs. Please check and try again.");
|
2016-03-18 01:17:39 -07:00
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-03-22 00:12:36 -07:00
|
|
|
|
// Retrieve and process the roms for merging
|
2016-03-18 01:17:39 -07:00
|
|
|
|
while (sldr.Read())
|
|
|
|
|
|
{
|
2016-09-19 18:04:24 -07:00
|
|
|
|
DatItem temp;
|
|
|
|
|
|
string key = "";
|
|
|
|
|
|
switch (sldr.GetString(8).ToLowerInvariant())
|
2016-03-21 22:06:33 -07:00
|
|
|
|
{
|
2016-09-19 18:04:24 -07:00
|
|
|
|
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).HashData.Size + "-" + ((Disk)temp).HashData.CRC;
|
|
|
|
|
|
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 = ((Disk)temp).HashData.Size + "-" + ((Disk)temp).HashData.CRC;
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
2016-03-18 01:17:39 -07:00
|
|
|
|
|
2016-04-19 00:07:40 -07:00
|
|
|
|
// Rename the game associated if it's still valid and we allow renames
|
|
|
|
|
|
if (merged && !_norename)
|
2016-03-18 01:17:39 -07:00
|
|
|
|
{
|
2016-09-19 18:04:24 -07:00
|
|
|
|
temp.MachineName = temp.MachineName +
|
|
|
|
|
|
(sysmerged ? " [" + temp.Manufacturer + " - " + temp.System + "]" : "") +
|
|
|
|
|
|
(srcmerged ? " [" + temp.Source + "]" : "");
|
2016-03-18 01:17:39 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-05-16 21:52:49 -07:00
|
|
|
|
if (roms.ContainsKey(key))
|
|
|
|
|
|
{
|
|
|
|
|
|
roms[key].Add(temp);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2016-09-19 18:04:24 -07:00
|
|
|
|
List<DatItem> templist = new List<DatItem>();
|
2016-05-16 21:52:49 -07:00
|
|
|
|
templist.Add(temp);
|
|
|
|
|
|
roms.Add(key, templist);
|
|
|
|
|
|
}
|
2016-03-18 01:17:39 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-04-19 00:07:40 -07:00
|
|
|
|
// If we're in a merged mode, merge and then resort by the correct parameters
|
2016-03-28 14:28:51 -07:00
|
|
|
|
if (merged)
|
2016-03-22 00:12:36 -07:00
|
|
|
|
{
|
2016-05-16 21:52:49 -07:00
|
|
|
|
foreach (string key in roms.Keys)
|
|
|
|
|
|
{
|
2016-09-19 18:04:24 -07:00
|
|
|
|
roms[key] = DatItem.Merge(roms[key], _logger);
|
2016-05-16 21:52:49 -07:00
|
|
|
|
}
|
2016-03-28 14:28:51 -07:00
|
|
|
|
}
|
2016-04-22 11:08:02 -07:00
|
|
|
|
// END COMMENT
|
2016-03-22 00:12:36 -07:00
|
|
|
|
|
2016-04-22 01:11:35 -07:00
|
|
|
|
/*
|
2016-04-22 11:08:02 -07:00
|
|
|
|
// This block would replace the whole block above between BEGIN COMMENT and END COMMENT
|
2016-04-22 01:11:35 -07:00
|
|
|
|
string query = @"
|
|
|
|
|
|
SELECT hash.id AS id, hash.size AS size, hash.crc AS crc, hash.md5 AS md5, hash.sha1 AS sha1,
|
2016-04-22 12:15:19 -07:00
|
|
|
|
a.key AS key, a.value AS value,
|
2016-04-22 01:11:35 -07:00
|
|
|
|
source.id, source.name, source.url,
|
|
|
|
|
|
system.id, system.manufacturer, system.name
|
|
|
|
|
|
FROM hash
|
2016-04-22 12:15:19 -07:00
|
|
|
|
JOIN hashdata a
|
|
|
|
|
|
ON hash.id=a.hashid
|
|
|
|
|
|
JOIN hashdata b
|
|
|
|
|
|
ON a.hashid=b.hashid
|
2016-04-22 01:11:35 -07:00
|
|
|
|
JOIN gamesystem
|
2016-04-22 12:15:19 -07:00
|
|
|
|
ON b.value=gamesystem.game
|
2016-04-22 01:11:35 -07:00
|
|
|
|
JOIN gamesource
|
2016-04-22 12:15:19 -07:00
|
|
|
|
ON b.value=gamesource.game
|
2016-04-22 01:11:35 -07:00
|
|
|
|
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
|
2016-04-22 12:15:19 -07:00
|
|
|
|
int systemid = -1, sourceid = -1;
|
|
|
|
|
|
long lastid = -1, size = -1;
|
|
|
|
|
|
string name = "", game = "", type = "", manufacturer = "", system = "", source = "", url = "", crc = "", md5 = "", sha1 = "";
|
2016-04-22 01:11:35 -07:00
|
|
|
|
while (sldr.Read())
|
|
|
|
|
|
{
|
|
|
|
|
|
// If the hash is different than the last
|
2016-04-22 12:15:19 -07:00
|
|
|
|
if (lastid != -1 && sldr.GetInt64(0) != lastid)
|
2016-04-22 01:11:35 -07:00
|
|
|
|
{
|
2016-09-01 23:41:19 -07:00
|
|
|
|
Rom temp = new Rom
|
2016-04-22 01:11:35 -07:00
|
|
|
|
{
|
2016-04-22 12:15:19 -07:00
|
|
|
|
Manufacturer = manufacturer,
|
|
|
|
|
|
System = system,
|
|
|
|
|
|
SystemID = systemid,
|
|
|
|
|
|
Source = source,
|
|
|
|
|
|
URL = url,
|
|
|
|
|
|
SourceID = sourceid,
|
2016-04-22 01:11:35 -07:00
|
|
|
|
Game = game,
|
|
|
|
|
|
Name = name,
|
|
|
|
|
|
Type = type,
|
2016-04-22 12:15:19 -07:00
|
|
|
|
Size = size,
|
|
|
|
|
|
CRC = crc,
|
|
|
|
|
|
MD5 = md5,
|
|
|
|
|
|
SHA1 = sha1,
|
2016-04-22 01:11:35 -07:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Rename the game associated if it's still valid and we allow renames
|
|
|
|
|
|
if (merged && !_norename)
|
|
|
|
|
|
{
|
2016-09-01 23:41:19 -07:00
|
|
|
|
temp.Machine = temp.Machine +
|
2016-04-22 01:11:35 -07:00
|
|
|
|
(sysmerged ? " [" + temp.Manufacturer + " - " + temp.System + "]" : "") +
|
|
|
|
|
|
(srcmerged ? " [" + temp.Source + "]" : "");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-05-16 21:52:49 -07:00
|
|
|
|
string key = temp.Size + "-" + temp.CRC;
|
|
|
|
|
|
if (roms.ContainsKey(key))
|
|
|
|
|
|
{
|
|
|
|
|
|
roms[key].Add(temp);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2016-09-01 23:41:19 -07:00
|
|
|
|
List<Rom> templist = new List<Rom>();
|
2016-05-16 21:52:49 -07:00
|
|
|
|
templist.Add(temp);
|
|
|
|
|
|
roms.Add(key, templist);
|
|
|
|
|
|
}
|
2016-04-22 01:11:35 -07:00
|
|
|
|
|
|
|
|
|
|
// Reset the variables
|
|
|
|
|
|
game = "";
|
|
|
|
|
|
name = "";
|
|
|
|
|
|
type = "";
|
|
|
|
|
|
}
|
2016-04-22 12:15:19 -07:00
|
|
|
|
|
|
|
|
|
|
// 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))
|
2016-04-22 01:11:35 -07:00
|
|
|
|
{
|
2016-04-22 12:15:19 -07:00
|
|
|
|
case "game":
|
|
|
|
|
|
game = sldr.GetString(6);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case "name":
|
|
|
|
|
|
name = sldr.GetString(6);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case "type":
|
|
|
|
|
|
type = sldr.GetString(6);
|
|
|
|
|
|
break;
|
2016-04-22 01:11:35 -07:00
|
|
|
|
}
|
2016-04-22 12:15:19 -07:00
|
|
|
|
|
|
|
|
|
|
lastid = sldr.GetInt64(0);
|
2016-04-22 01:11:35 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// If we're in a merged mode, merge
|
|
|
|
|
|
if (merged)
|
|
|
|
|
|
{
|
2016-05-16 21:52:49 -07:00
|
|
|
|
foreach (string key in roms.Keys)
|
|
|
|
|
|
{
|
|
|
|
|
|
roms[key] = RomManipulation.Merge(roms[key]);
|
|
|
|
|
|
}
|
2016-04-22 01:11:35 -07:00
|
|
|
|
}
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2016-05-16 21:52:49 -07:00
|
|
|
|
/*
|
|
|
|
|
|
// THIS CODE SHOULD BE PUT IN WriteToDatFromDict
|
|
|
|
|
|
|
2016-03-22 00:12:36 -07:00
|
|
|
|
// Now check rename within games
|
|
|
|
|
|
string lastname = "", lastgame = "";
|
|
|
|
|
|
for (int i = 0; i < roms.Count; i++)
|
|
|
|
|
|
{
|
2016-09-01 23:41:19 -07:00
|
|
|
|
Rom rom = roms[i];
|
2016-03-22 00:12:36 -07:00
|
|
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
|
}
|
2016-09-01 23:41:19 -07:00
|
|
|
|
if (rom.Machine != "")
|
2016-03-22 00:12:36 -07:00
|
|
|
|
{
|
2016-09-01 23:41:19 -07:00
|
|
|
|
samegame = (lastgame == rom.Machine);
|
2016-03-22 00:12:36 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lastname = rom.Name;
|
2016-09-01 23:41:19 -07:00
|
|
|
|
lastgame = rom.Machine;
|
2016-03-22 00:12:36 -07:00
|
|
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
|
}
|
2016-05-16 21:52:49 -07:00
|
|
|
|
*/
|
2016-03-22 00:12:36 -07:00
|
|
|
|
|
2016-03-18 01:17:39 -07:00
|
|
|
|
return roms;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|