using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Mono.Data.Sqlite;
using SabreTools.Helper;
namespace SabreTools
{
public partial class DATabase
{
#region Init Methods
///
/// Wrap importing and updating DATs
///
///
private static void InitImport(bool ignore)
{
IImport imp = new ImportTwo(_datroot, _connectionString, _logger, ignore);
imp.UpdateDatabase();
}
///
/// Wrap generating a DAT from the library
///
/// System ID to be used in the DAT (blank means all)
/// 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)
private static void InitGenerate(string systemid, bool norename, bool old)
{
IGenerate gen = new GenerateTwo(systemid, "" /* sourceid */, _datroot, _outroot, _connectionString, _logger, norename, old);
gen.Export();
}
///
/// Wrap generating all standard DATs from the library
///
private static void InitGenerateAll(bool norename, bool old)
{
List systems = new List();
using (SqliteConnection dbc = new SqliteConnection(_connectionString))
{
dbc.Open();
string query = "SELECT id FROM system";
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.Warning("No systems found! Please add a system and then try again.");
return;
}
while (sldr.Read())
{
systems.Add(sldr.GetInt32(0).ToString());
}
}
}
// Loop through the inputs
foreach (string system in systems)
{
_logger.User("Generating DAT for system id " + system);
InitGenerate(system, norename, old);
}
}
}
///
/// Wrap converting and updating DAT file from any format to any format
///
/// Input filename
/// /* Normal DAT header info */
/// New filename
/// New name
/// New description
/// New category
/// New version
/// New date
/// New author
/// New email
/// New homepage
/// New URL
/// New comment
/// New header
/// True to set SuperDAT type, false otherwise
/// None, Split, Full
/// None, Obsolete, Required, Ignore
/// None, Zip, Unzip
/// True to output to ClrMamePro format
/// True to output to Missfile format
/// True to output to RomCenter format
/// True to output to SabreDAT format
/// True to output to Logiqx XML format
/// /* Missfile-specific DAT info */
/// True if games are to be used in output, false if roms are
/// Generic prefix to be added to each line
/// Generic postfix to be added to each line
/// Add quotes to each item
/// Replace all extensions with another
/// Add an extension to all items
/// Add the dat name as a directory prefix
/// Output files in romba format
/// Output files in TSV format
/// /* Filtering info */
/// Name of the game to match (can use asterisk-partials)
/// Name of the rom to match (can use asterisk-partials)
/// Type of the rom to match
/// Find roms greater than or equal to this size
/// Find roms less than or equal to this size
/// Find roms equal to this size
/// CRC of the rom to match (can use asterisk-partials)
/// MD5 of the rom to match (can use asterisk-partials)
/// SHA-1 of the rom to match (can use asterisk-partials)
/// Select roms with nodump status as follows: null (match all), true (match Nodump only), false (exclude Nodump)
/// /* Output DAT info */
/// Optional param for output directory
/// True to clean the game names to WoD standard, false otherwise (default)
/// True to dedupe the roms in the DAT, false otherwise (default)
private static void InitUpdate(string input,
/* Normal DAT header info */
string filename,
string name,
string description,
string category,
string version,
string date,
string author,
string email,
string homepage,
string url,
string comment,
string header,
bool superdat,
string forcemerge,
string forcend,
string forcepack,
bool outputCMP,
bool outputMiss,
bool outputRC,
bool outputSD,
bool outputXML,
/* Missfile-specific DAT info */
bool usegame,
string prefix,
string postfix,
bool quotes,
string repext,
string addext,
bool datprefix,
bool romba,
bool tsv,
/* Filtering info */
string gamename,
string romname,
string romtype,
long sgt,
long slt,
long seq,
string crc,
string md5,
string sha1,
bool? nodump,
/* Output DAT info */
string outdir,
bool clean,
bool dedup)
{
// Set the special flags
ForceMerging fm = ForceMerging.None;
switch (forcemerge.ToLowerInvariant())
{
case "none":
default:
fm = ForceMerging.None;
break;
case "split":
fm = ForceMerging.Split;
break;
case "full":
fm = ForceMerging.Full;
break;
}
ForceNodump fn = ForceNodump.None;
switch (forcend.ToLowerInvariant())
{
case "none":
default:
fn = ForceNodump.None;
break;
case "obsolete":
fn = ForceNodump.Obsolete;
break;
case "required":
fn = ForceNodump.Required;
break;
case "ignore":
fn = ForceNodump.Ignore;
break;
}
ForcePacking fp = ForcePacking.None;
switch (forcepack.ToLowerInvariant())
{
case "none":
default:
fp = ForcePacking.None;
break;
case "zip":
fp = ForcePacking.Zip;
break;
case "unzip":
fp = ForcePacking.Unzip;
break;
}
// Normalize the extensions
addext = (addext == "" || addext.StartsWith(".") ? addext : "." + addext);
repext = (repext == "" || repext.StartsWith(".") ? repext : "." + repext);
// Populate the DatData object
DatData userInputDat = new DatData
{
FileName = filename,
Name = name,
Description = description,
Category = category,
Version = version,
Date = date,
Author = author,
Email = email,
Homepage = homepage,
Url = url,
Comment = comment,
Header = header,
Type = (superdat ? "SuperDAT" : null),
ForceMerging = fm,
ForceNodump = fn,
ForcePacking = fp,
MergeRoms = dedup,
UseGame = usegame,
Prefix = prefix,
Postfix = postfix,
Quotes = quotes,
RepExt = repext,
AddExt = addext,
GameName = datprefix,
Romba = romba,
TSV = tsv,
};
if (outputCMP)
{
userInputDat.OutputFormat = OutputFormat.ClrMamePro;
DatTools.Update(input, userInputDat, outdir, clean, gamename, romname, romtype, sgt, slt, seq, crc, md5, sha1, nodump, _logger);
}
if (outputMiss || romba)
{
userInputDat.OutputFormat = OutputFormat.MissFile;
DatTools.Update(input, userInputDat, outdir, clean, gamename, romname, romtype, sgt, slt, seq, crc, md5, sha1, nodump, _logger);
}
if (outputRC)
{
userInputDat.OutputFormat = OutputFormat.RomCenter;
DatTools.Update(input, userInputDat, outdir, clean, gamename, romname, romtype, sgt, slt, seq, crc, md5, sha1, nodump, _logger);
}
if (outputSD)
{
userInputDat.OutputFormat = OutputFormat.SabreDat;
DatTools.Update(input, userInputDat, outdir, clean, gamename, romname, romtype, sgt, slt, seq, crc, md5, sha1, nodump, _logger);
}
if (outputXML)
{
userInputDat.OutputFormat = OutputFormat.Xml;
DatTools.Update(input, userInputDat, outdir, clean, gamename, romname, romtype, sgt, slt, seq, crc, md5, sha1, nodump, _logger);
}
if (!outputCMP && !(outputMiss || romba) && !outputRC && !outputSD && !outputXML)
{
DatTools.Update(input, userInputDat, outdir, clean, gamename, romname, romtype, sgt, slt, seq, crc, md5, sha1, nodump, _logger);
}
}
///
/// Wrap creating a DAT file from files or a directory
///
/// List of innput filenames
/// New filename
/// New name
/// New description
/// New category
/// New version
/// New author
/// True to set forcepacking="unzip" on the created file, false otherwise
/// True to output in CMP format, false to output in Logiqx XML
/// True to enable reading a directory like a Romba depot, false otherwise
/// True to enable SuperDAT-style reading, false otherwise
/// True to disable getting MD5 hash, false otherwise
/// True to disable getting SHA-1 hash, false otherwise
/// True if the date should be omitted from the DAT, false otherwise
/// True if archives should be treated as files, false otherwise
/// True if GZIP archives should be treated as files, false otherwise
/// Name of the directory to create a temp folder in (blank is current directory
private static void InitDatFromDir(List inputs,
string filename,
string name,
string description,
string category,
string version,
string author,
bool forceunpack,
bool old,
bool romba,
bool superdat,
bool noMD5,
bool noSHA1,
bool bare,
bool archivesAsFiles,
bool enableGzip,
string tempDir)
{
// Create a new DATFromDir object and process the inputs
DatData datdata = new DatData
{
FileName = description,
Name = name,
Description = description,
Category = category,
Version = version,
Date = DateTime.Now.ToString("yyyy-MM-dd"),
Author = author,
ForcePacking = (forceunpack ? ForcePacking.Unzip : ForcePacking.None),
OutputFormat = (old ? OutputFormat.ClrMamePro : OutputFormat.Xml),
Romba = romba,
Type = (superdat ? "SuperDAT" : ""),
Roms = new Dictionary>(),
};
DATFromDir dfd = new DATFromDir(inputs, datdata, noMD5, noSHA1, bare, archivesAsFiles, enableGzip, tempDir, _logger);
bool success = dfd.Start();
// If we failed, show the help
if (!success)
{
Console.WriteLine();
Build.Help();
}
}
///
/// Wrap trimming and merging a single DAT
///
/// Input file or folder to be converted
/// Root directory to base path lengths on
/// True is games should not be renamed
/// True if forcepacking="unzip" should be included
private static void InitTrimMerge(string input, string root, bool rename, bool force)
{
// Strip any quotations from the name
input = input.Replace("\"", "");
if (input != "" && (File.Exists(input) || Directory.Exists(input)))
{
TrimMerge sg = new TrimMerge(input, root, rename, force, _logger);
sg.Process();
return;
}
}
///
/// Wrap merging, diffing, and deduping 2 or mor DATs
///
/// A List of Strings representing the DATs or DAT folders to be merged
/// Internal name of the DAT
/// Description and external name of the DAT
/// Category for the DAT
/// Version of the DAT
/// Author of the DAT
/// True if a DiffDat of all inputs is wanted, false otherwise
/// True if the outputted file should remove duplicates, false otherwise
/// True if the date should be omitted from the DAT, false otherwise
/// True if the forcepacking="unzip" tag is to be added, false otherwise
/// True if a old-style DAT should be output, false otherwise
/// True if DATs should be merged in SuperDAT style, false otherwise
/// True if the outputted diffs should be cascaded, false otherwise
/// True if cascaded diffs overwrite the source files, false otherwise
/// Output directory for the files (blank is default)
/// True to clean the game names to WoD standard, false otherwise (default)
private static void InitMergeDiff(List inputs, string name, string desc, string cat, string version, string author,
bool diff, bool dedup, bool bare, bool forceunpack, bool old, bool superdat, bool cascade, bool inplace, string outdir = "", bool clean = false)
{
// Make sure there are no folders in inputs
List newInputs = new List();
foreach (string input in inputs)
{
if (Directory.Exists(input.Replace("\"", "")))
{
foreach (string file in Directory.EnumerateFiles(input.Replace("\"", ""), "*", SearchOption.AllDirectories))
{
try
{
newInputs.Add(Path.GetFullPath(file) + "¬" + Path.GetFullPath(input.Replace("\"", "")));
}
catch (PathTooLongException)
{
_logger.Warning("The path for " + file + " was too long");
}
catch (Exception ex)
{
_logger.Error(ex.ToString());
}
}
}
else if (File.Exists(input.Replace("\"", "")))
{
try
{
newInputs.Add(Path.GetFullPath(input.Replace("\"", "")) + "¬" + Path.GetDirectoryName(Path.GetFullPath(input.Replace("\"", ""))));
}
catch (PathTooLongException)
{
_logger.Warning("The path for " + input.Replace("\"", "") + " was too long");
}
catch (Exception ex)
{
_logger.Error(ex.ToString());
}
}
}
MergeDiff md = new MergeDiff(newInputs, name, desc, cat, version, author, diff, dedup, bare, forceunpack, old, superdat, cascade, inplace, outdir, clean, _logger);
md.Process();
}
///
/// Wrap splitting a DAT by 2 extensions
///
/// Input files or folders to be split
/// First extension to split on
/// Second extension to split on
/// Output directory for the split files
private static void InitExtSplit(List inputs, string exta, string extb, string outdir)
{
// Verify the input files
foreach (string input in inputs)
{
if (!File.Exists(input.Replace("\"", "")) && !Directory.Exists(input.Replace("\"", "")))
{
_logger.Error(input + " is not a valid file or folder!");
Console.WriteLine();
Build.Help();
return;
}
}
// Strip any quotations from the names
exta = exta.Replace("\"", "");
extb = extb.Replace("\"", "");
outdir = outdir.Replace("\"", "");
// Convert comma-separated strings to list
List extaList = exta.Split(',').ToList();
List extbList = extb.Split(',').ToList();
Split es = new Split(inputs, extaList, extbList, outdir, _logger);
es.Process();
return;
}
///
/// Wrap splitting a DAT by best available hashes
///
/// List of inputs to be used
/// Output directory for the split files
private static void InitHashSplit(List inputs, string outdir)
{
// Strip any quotations from the names
outdir = outdir.Replace("\"", "");
// Verify the input files
foreach (string input in inputs)
{
if (!File.Exists(input.Replace("\"", "")) && !Directory.Exists(input.Replace("\"", "")))
{
_logger.Error(input + " is not a valid file or folder!");
Console.WriteLine();
Build.Help();
return;
}
}
// If so, run the program
Split hs = new Split(inputs, outdir, _logger);
hs.Process();
}
///
/// Wrap getting statistics on a DAT or folder of DATs
///
/// List of inputs to be used
/// True to show individual DAT statistics, false otherwise
private static void InitStats(List inputs, bool single)
{
List newinputs = new List();
foreach (string input in inputs)
{
if (File.Exists(input.Replace("\"", "")))
{
newinputs.Add(input.Replace("\"", ""));
}
if (Directory.Exists(input.Replace("\"", "")))
{
foreach (string file in Directory.GetFiles(input.Replace("\"", ""), "*", SearchOption.AllDirectories))
{
newinputs.Add(file.Replace("\"", ""));
}
}
}
Logger statlog = new Logger(true, "stats.txt");
statlog.Start();
Stats stats = new Stats(newinputs, single, statlog);
stats.Process();
statlog.Close();
}
///
/// Wrap adding a new source to the database
///
/// Source name
/// Source URL(s)
private static void InitAddSource(string name, string url)
{
if (DBTools.AddSource(name, url, _connectionString))
{
_logger.Log("Source " + name + " added!");
}
else
{
_logger.Error("Source " + name + " could not be added!");
}
}
///
/// Wrap removing an existing source from the database
///
/// Source ID to be removed from the database
private static void InitRemoveSource(string sourceid)
{
int srcid = -1;
if (Int32.TryParse(sourceid, out srcid))
{
if (DBTools.RemoveSource(srcid, _connectionString))
{
_logger.Log("Source '" + srcid + "' removed!");
}
else
{
_logger.Error("Source with id '" + srcid + "' could not be removed.");
}
}
else
{
_logger.Error("Invalid input");
}
}
///
/// Wrap adding a new system to the database
///
/// Manufacturer name
/// System name
private static void InitAddSystem(string manufacturer, string system)
{
if (DBTools.AddSystem(manufacturer, system, _connectionString))
{
_logger.Log("System " + manufacturer + " - " + system + " added!");
}
else
{
_logger.Error("System " + manufacturer + " - " + system + " could not be added!");
}
}
///
/// Wrap removing an existing system from the database
///
/// System ID to be removed from the database
private static void InitRemoveSystem(string systemid)
{
int sysid = -1;
if (Int32.TryParse(systemid, out sysid))
{
if (DBTools.RemoveSystem(sysid, _connectionString))
{
_logger.Log("System '" + sysid + "' removed!");
}
else
{
_logger.Error("System with id '" + sysid + "' could not be removed.");
}
}
else
{
_logger.Error("Invalid input");
}
}
#endregion
#region OBSOLETE
///
/// Wrap converting DAT file from any format to any format
///
///
///
/// Optional param for output directory
/// True to clean the game names to WoD standard, false otherwise (default)
private static void InitConvert(string filename, OutputFormat outputFormat, string outdir, bool clean)
{
// Clean the input strings
outdir = outdir.Replace("\"", "");
if (outdir != "")
{
outdir = Path.GetFullPath(outdir) + Path.DirectorySeparatorChar;
}
filename = filename.Replace("\"", "");
if (File.Exists(filename))
{
_logger.User("Converting \"" + Path.GetFileName(filename) + "\"");
DatData datdata = new DatData
{
OutputFormat = outputFormat,
MergeRoms = false,
};
datdata = DatTools.Parse(filename, 0, 0, datdata, _logger, true, clean);
// If the extension matches, append ".new" to the filename
string extension = (datdata.OutputFormat == OutputFormat.Xml || datdata.OutputFormat == OutputFormat.SabreDat ? ".xml" : ".dat");
if (outdir == "" && Path.GetExtension(filename) == extension)
{
datdata.FileName += ".new";
}
Output.WriteDatfile(datdata, (outdir == "" ? Path.GetDirectoryName(filename) : outdir), _logger);
}
else if (Directory.Exists(filename))
{
filename = Path.GetFullPath(filename) + Path.DirectorySeparatorChar;
foreach (string file in Directory.EnumerateFiles(filename, "*", SearchOption.AllDirectories))
{
_logger.User("Converting \"" + Path.GetFullPath(file).Remove(0, filename.Length) + "\"");
DatData datdata = new DatData
{
OutputFormat = outputFormat,
MergeRoms = false,
};
datdata = DatTools.Parse(file, 0, 0, datdata, _logger, true, clean);
// If the extension matches, append ".new" to the filename
string extension = (datdata.OutputFormat == OutputFormat.Xml || datdata.OutputFormat == OutputFormat.SabreDat ? ".xml" : ".dat");
if (outdir == "" && Path.GetExtension(file) == extension)
{
datdata.FileName += ".new";
}
Output.WriteDatfile(datdata, (outdir == "" ? Path.GetDirectoryName(file) : outdir + Path.GetDirectoryName(file).Remove(0, filename.Length - 1)), _logger);
}
}
else
{
_logger.Error("I'm sorry but " + filename + " doesn't exist!");
}
return;
}
///
/// Wrap converting a DAT to missfile
///
/// File to be converted
/// True if games are to be used in output, false if roms are
/// Generic prefix to be added to each line
/// Generic postfix to be added to each line
/// Add quotes to each item
/// Replace all extensions with another
/// Add an extension to all items
/// Add the dat name as a directory prefix
/// Output files in romba format
/// Output files in TSV format
private static void InitConvertMiss(string input, bool usegame, string prefix, string postfix, bool quotes,
string repext, string addext, bool gamename, bool romba, bool tsv)
{
// Strip any quotations from the name
input = input.Replace("\"", "");
if (input != "" && File.Exists(input))
{
// Get the full input name
input = Path.GetFullPath(input);
// Get the output name
string name = Path.GetFileNameWithoutExtension(input) + "-miss";
// Read in the roms from the DAT and then write them to the file
_logger.User("Converting " + input);
DatData datdata = new DatData
{
OutputFormat = OutputFormat.MissFile,
UseGame = usegame,
Prefix = prefix,
Postfix = postfix,
AddExt = addext,
RepExt = repext,
Quotes = quotes,
GameName = gamename,
Romba = romba,
TSV = tsv,
};
datdata = DatTools.Parse(input, 0, 0, datdata, _logger);
datdata.FileName += "-miss";
datdata.Name += "-miss";
datdata.Description += "-miss";
// Normalize the extensions
addext = (addext == "" || addext.StartsWith(".") ? addext : "." + addext);
repext = (repext == "" || repext.StartsWith(".") ? repext : "." + repext);
Output.WriteDatfile(datdata, Path.GetDirectoryName(input), _logger);
_logger.User(input + " converted to: " + name);
return;
}
else
{
_logger.Error("I'm sorry but " + input + "doesn't exist!");
}
}
#endregion
}
}