2016-03-18 01:17:39 -07:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Data.SQLite;
|
|
|
|
|
|
using System.IO;
|
2016-03-19 02:16:26 -07:00
|
|
|
|
using System.Xml.Linq;
|
|
|
|
|
|
|
2016-03-24 14:03:22 -07:00
|
|
|
|
using DATabase.Helper;
|
|
|
|
|
|
|
2016-03-18 01:17:39 -07:00
|
|
|
|
namespace DATabase
|
|
|
|
|
|
{
|
|
|
|
|
|
class Program
|
|
|
|
|
|
{
|
2016-03-24 16:17:33 -07:00
|
|
|
|
private static string _dbName = "DATabase.sqlite";
|
|
|
|
|
|
private static string _connectionString = "Data Source=" + _dbName + ";Version = 3;";
|
2016-03-18 01:17:39 -07:00
|
|
|
|
|
|
|
|
|
|
static void Main(string[] args)
|
|
|
|
|
|
{
|
2016-03-24 16:17:33 -07:00
|
|
|
|
// Perform initial setup and verification
|
2016-03-24 21:09:35 -07:00
|
|
|
|
DBTools.EnsureDatabase(_dbName, _connectionString);
|
2016-03-18 01:17:39 -07:00
|
|
|
|
Remapping.CreateRemappings();
|
2016-03-24 16:25:43 -07:00
|
|
|
|
Console.Clear();
|
2016-03-18 01:17:39 -07:00
|
|
|
|
|
2016-03-24 21:05:10 -07:00
|
|
|
|
/*
|
|
|
|
|
|
// Show runtime header
|
2016-03-24 21:25:40 -07:00
|
|
|
|
Console.WriteLine(
|
|
|
|
|
|
@"+-------------------------------------------------------------------+
|
2016-03-24 21:05:10 -07:00
|
|
|
|
| |
|
|
|
|
|
|
| DATabase 0.0.4.1 |
|
|
|
|
|
|
| |
|
|
|
|
|
|
| by Matt Nadareski (darksabre76) |
|
|
|
|
|
|
| |
|
2016-03-24 21:25:40 -07:00
|
|
|
|
+-------------------------------------------------------------------+
|
|
|
|
|
|
");
|
2016-03-24 21:05:10 -07:00
|
|
|
|
*/
|
|
|
|
|
|
|
2016-03-18 01:17:39 -07:00
|
|
|
|
// If there's not enough arguments, show the help screen
|
2016-03-24 13:23:25 -07:00
|
|
|
|
if (args.Length == 0)
|
2016-03-18 01:17:39 -07:00
|
|
|
|
{
|
|
|
|
|
|
Help();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-03-24 13:23:25 -07:00
|
|
|
|
// Determine which switches are enabled (with values if necessary)
|
|
|
|
|
|
bool help = false, import = false, generate = false, convert = false,
|
2016-03-24 20:56:04 -07:00
|
|
|
|
listsys = false, listsrc = false, norename = false, old = false;
|
2016-03-24 14:05:37 -07:00
|
|
|
|
string systems = "", sources = "", input = "";
|
2016-03-24 13:23:25 -07:00
|
|
|
|
foreach (string arg in args)
|
2016-03-18 01:17:39 -07:00
|
|
|
|
{
|
2016-03-24 13:23:25 -07:00
|
|
|
|
help = help || (arg == "-h" || arg == "-?" || arg == "--help");
|
|
|
|
|
|
import = import || (arg == "-i" || arg == "--import");
|
|
|
|
|
|
generate = generate || (arg == "-g" || arg == "--generate");
|
|
|
|
|
|
convert = convert || (arg == "-c" || arg == "--convert");
|
2016-03-24 16:10:01 -07:00
|
|
|
|
listsys = listsys || (arg == "-lsy" || arg == "--list-systems");
|
|
|
|
|
|
listsrc = listsrc || (arg == "-lso" || arg == "--list-sources");
|
2016-03-24 13:23:25 -07:00
|
|
|
|
norename = norename || (arg == "-nr" || arg == "--no-rename");
|
2016-03-24 21:05:10 -07:00
|
|
|
|
old = old || (arg == "-old" || arg == "--romvault");
|
2016-03-24 15:22:20 -07:00
|
|
|
|
systems = (arg.StartsWith("system=") && systems == "" ? arg.Split('=')[1] : systems);
|
|
|
|
|
|
sources = (arg.StartsWith("source=") && sources == "" ? arg.Split('=')[1] : sources);
|
2016-03-24 14:53:27 -07:00
|
|
|
|
|
|
|
|
|
|
// Take care of the two distinct input name possibilites; prioritize the input tag
|
2016-03-24 15:22:20 -07:00
|
|
|
|
input = (arg.StartsWith("input=") && input == "" ? arg.Split('=')[1] : input);
|
|
|
|
|
|
input = (!arg.StartsWith("-") && !arg.StartsWith("source=") && !arg.StartsWith("system=") && !arg.StartsWith("input=") && input == "" ? arg : input);
|
2016-03-24 13:23:25 -07:00
|
|
|
|
}
|
2016-03-18 01:17:39 -07:00
|
|
|
|
|
2016-03-24 16:29:27 -07:00
|
|
|
|
// If more than one switch is enabled or help is set, show the help screen
|
2016-03-24 16:38:32 -07:00
|
|
|
|
if (help || !(import ^ generate ^ listsys ^ listsrc))
|
2016-03-24 13:23:25 -07:00
|
|
|
|
{
|
|
|
|
|
|
Help();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2016-03-18 01:17:39 -07:00
|
|
|
|
|
2016-03-24 13:23:25 -07:00
|
|
|
|
// Now take care of each mode in succesion
|
|
|
|
|
|
|
|
|
|
|
|
// Import a file or folder
|
|
|
|
|
|
if (import)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Check to see if the second argument is a file that exists
|
|
|
|
|
|
if (input != "" && File.Exists(input))
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine(input);
|
|
|
|
|
|
Import imp = new Import(input, _connectionString);
|
|
|
|
|
|
imp.ImportData();
|
|
|
|
|
|
}
|
|
|
|
|
|
// Check to see if the second argument is a directory that exists
|
|
|
|
|
|
else if (input != "" && Directory.Exists(input))
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (string filename in Directory.GetFiles(input, "*", SearchOption.TopDirectoryOnly))
|
2016-03-18 01:17:39 -07:00
|
|
|
|
{
|
2016-03-24 13:23:25 -07:00
|
|
|
|
Console.WriteLine(filename);
|
|
|
|
|
|
Import imp = new Import(filename, _connectionString);
|
|
|
|
|
|
imp.ImportData();
|
2016-03-18 01:17:39 -07:00
|
|
|
|
}
|
2016-03-24 13:23:25 -07:00
|
|
|
|
}
|
|
|
|
|
|
// Otherwise, show help
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
Help();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Generate a DAT
|
|
|
|
|
|
else if (generate)
|
|
|
|
|
|
{
|
2016-03-24 17:38:08 -07:00
|
|
|
|
Generate gen = new Generate(systems, sources, _connectionString, norename, old);
|
2016-03-24 13:23:25 -07:00
|
|
|
|
gen.Export();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// List all available sources
|
|
|
|
|
|
else if (listsrc)
|
|
|
|
|
|
{
|
|
|
|
|
|
string query = @"
|
2016-03-18 01:17:39 -07:00
|
|
|
|
SELECT DISTINCT sources.id, sources.name
|
|
|
|
|
|
FROM sources JOIN games on sources.id=games.source
|
|
|
|
|
|
ORDER BY sources.name COLLATE NOCASE";
|
2016-03-24 13:23:25 -07:00
|
|
|
|
using (SQLiteConnection dbc = new SQLiteConnection(_connectionString))
|
|
|
|
|
|
{
|
|
|
|
|
|
dbc.Open();
|
|
|
|
|
|
using (SQLiteCommand slc = new SQLiteCommand(query, dbc))
|
2016-03-18 01:17:39 -07:00
|
|
|
|
{
|
2016-03-24 13:23:25 -07:00
|
|
|
|
using (SQLiteDataReader sldr = slc.ExecuteReader())
|
2016-03-18 01:17:39 -07:00
|
|
|
|
{
|
2016-03-24 13:23:25 -07:00
|
|
|
|
// If nothing is found, tell the user and exit
|
|
|
|
|
|
if (!sldr.HasRows)
|
2016-03-18 01:17:39 -07:00
|
|
|
|
{
|
2016-03-24 13:23:25 -07:00
|
|
|
|
Console.WriteLine("Error: No sources found! Please add a source and then try again.");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2016-03-18 01:17:39 -07:00
|
|
|
|
|
2016-03-24 13:23:25 -07:00
|
|
|
|
Console.WriteLine("Available Sources (id <= name):");
|
|
|
|
|
|
while (sldr.Read())
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine(sldr.GetInt32(0) + "\t<=\t" + sldr.GetString(1));
|
2016-03-18 01:17:39 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2016-03-24 13:23:25 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2016-03-18 01:17:39 -07:00
|
|
|
|
|
2016-03-24 13:23:25 -07:00
|
|
|
|
// List all available systems
|
|
|
|
|
|
else if (listsys)
|
|
|
|
|
|
{
|
|
|
|
|
|
string query = @"
|
2016-03-18 01:17:39 -07:00
|
|
|
|
SELECT DISTINCT systems.id, systems.manufacturer, systems.system
|
|
|
|
|
|
FROM systems JOIN games ON systems.id=games.system
|
|
|
|
|
|
ORDER BY systems.manufacturer, systems.system";
|
2016-03-24 13:23:25 -07:00
|
|
|
|
using (SQLiteConnection dbc = new SQLiteConnection(_connectionString))
|
|
|
|
|
|
{
|
|
|
|
|
|
dbc.Open();
|
|
|
|
|
|
using (SQLiteCommand slc = new SQLiteCommand(query, dbc))
|
2016-03-18 01:17:39 -07:00
|
|
|
|
{
|
2016-03-24 13:23:25 -07:00
|
|
|
|
using (SQLiteDataReader sldr = slc.ExecuteReader())
|
2016-03-18 01:17:39 -07:00
|
|
|
|
{
|
2016-03-24 13:23:25 -07:00
|
|
|
|
// If nothing is found, tell the user and exit
|
|
|
|
|
|
if (!sldr.HasRows)
|
2016-03-18 01:17:39 -07:00
|
|
|
|
{
|
2016-03-24 13:23:25 -07:00
|
|
|
|
Console.WriteLine("Error: No systems found! Please add a system and then try again.");
|
|
|
|
|
|
return;
|
2016-03-18 01:17:39 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-03-24 13:23:25 -07:00
|
|
|
|
Console.WriteLine("Available Systems (id <= name):");
|
|
|
|
|
|
while (sldr.Read())
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine(sldr.GetInt32(0) + "\t<=\t" + sldr.GetString(1) + " - " + sldr.GetString(2));
|
|
|
|
|
|
}
|
2016-03-19 02:16:26 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2016-03-24 13:23:25 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2016-03-19 02:16:26 -07:00
|
|
|
|
|
2016-03-24 13:23:25 -07:00
|
|
|
|
// Convert RV DAT to XML DAT
|
|
|
|
|
|
else if (convert)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (File.Exists(input))
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine("Converting " + input);
|
|
|
|
|
|
XElement conv = Converters.RomVaultToXML(File.ReadAllLines(input));
|
|
|
|
|
|
FileStream fs = File.OpenWrite(Path.GetFileNameWithoutExtension(input) + ".new.xml");
|
|
|
|
|
|
StreamWriter sw = new StreamWriter(fs);
|
|
|
|
|
|
sw.Write(conv);
|
|
|
|
|
|
sw.Close();
|
|
|
|
|
|
fs.Close();
|
|
|
|
|
|
Console.WriteLine("Converted file: " + Path.GetFileNameWithoutExtension(input) + ".new.xml");
|
|
|
|
|
|
}
|
|
|
|
|
|
// Otherwise, show help
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2016-03-18 01:17:39 -07:00
|
|
|
|
Help();
|
2016-03-24 13:23:25 -07:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
2016-03-18 01:17:39 -07:00
|
|
|
|
}
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static void Help ()
|
|
|
|
|
|
{
|
2016-03-24 21:05:10 -07:00
|
|
|
|
Console.Clear();
|
2016-03-18 01:17:39 -07:00
|
|
|
|
Console.Write(@"
|
|
|
|
|
|
DATabase - Import and Generate DAT files
|
|
|
|
|
|
-----------------------------------------
|
2016-03-24 17:38:08 -07:00
|
|
|
|
Usage: DATabase [option] [filename|dirname|<system=sy,...> <source=so,...>]
|
2016-03-18 01:17:39 -07:00
|
|
|
|
|
2016-03-24 16:10:01 -07:00
|
|
|
|
Options:
|
|
|
|
|
|
-h, -?, --help Show this help
|
|
|
|
|
|
-i, --import Start tool in import mode
|
|
|
|
|
|
A filename or folder is required to run
|
|
|
|
|
|
-g, --generate Start tool in generate mode
|
2016-03-24 17:38:08 -07:00
|
|
|
|
system=sy,... List of system IDs
|
|
|
|
|
|
source=so,... List of source IDs
|
|
|
|
|
|
-nr, --no-rename Don't auto-rename games
|
2016-03-24 21:05:10 -07:00
|
|
|
|
-old, --romvault Produce a DAT in RV format
|
2016-03-24 16:10:01 -07:00
|
|
|
|
-lso, --list-sources List all sources (id <= name)
|
|
|
|
|
|
-lsy, --list-systems List all systems (id <= name)
|
|
|
|
|
|
-c, --convert Convert a RV DAT to XML
|
|
|
|
|
|
A filename or folder is required to run
|
2016-03-18 01:17:39 -07:00
|
|
|
|
|
2016-03-24 14:48:53 -07:00
|
|
|
|
Filenames and directories can't start with '-', 'system=', or 'source='
|
2016-03-24 14:53:27 -07:00
|
|
|
|
unless prefixed by 'input='
|
2016-03-18 01:17:39 -07:00
|
|
|
|
");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|