Files
SabreTools/SabreToolsUI/SabreToolsUI.cs
2016-03-29 21:55:54 -07:00

145 lines
3.6 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SQLite;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SabreTools
{
public partial class SabreToolsUI : Form
{
private static string _dbName = "DATabase.sqlite";
private static string _connectionString = "Data Source=" + _dbName + ";Version = 3;";
public SabreToolsUI()
{
AllocConsole();
InitializeComponent();
}
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool AllocConsole();
private void quitToolStripMenuItem_Click(object sender, EventArgs e)
{
Environment.Exit(0);
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("SabreTools designed and coded by: Matt Nadareski (darksabre76)\nTested by: @tractivo", "About");
}
private static object[] GetAllSystems()
{
List<object> objs = new List<object>();
Process.Start("DATabase.exe", "--skip");
string query = @"
SELECT DISTINCT systems.id, systems.manufacturer, systems.system
FROM systems JOIN games ON systems.id=games.system
ORDER BY systems.manufacturer, systems.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)
{
while (sldr.Read())
{
objs.Add(sldr.GetString(1) + " - " + sldr.GetString(2) + " (" + sldr.GetInt32(0) + ")");
}
}
}
}
}
return objs.ToArray();
}
private static object[] GetAllSources()
{
List<object> objs = new List<object>();
Process.Start("DATabase.exe", "--skip");
string query = @"
SELECT DISTINCT sources.id, sources.name
FROM sources JOIN games on sources.id=games.source
ORDER BY sources.name COLLATE NOCASE";
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)
{
while (sldr.Read())
{
objs.Add(sldr.GetString(1) + " (" + sldr.GetInt32(0) + ")");
}
}
}
}
}
return objs.ToArray();
}
private void button1_Click(object sender, EventArgs e)
{
string systems = "";
string sources = "";
CheckedListBox.CheckedItemCollection cil = this.systemsCheckedListBox.CheckedItems;
foreach (object ci in cil)
{
string id = Regex.Match(ci.ToString(), @".*? \((.*?)\)").Groups[1].Value;
systems += (systems == "" ? id : "," + id);
}
cil = this.sourcesCheckedListBox.CheckedItems;
foreach (object ci in cil)
{
string id = Regex.Match(ci.ToString(), @".*? \((.*?)\)").Groups[1].Value;
sources += (sources == "" ? id : "," + id);
}
bool old = this.oldCheckBox.Checked;
bool norename = !this.renameCheckBox.Checked;
string args = "-l -g" +
(old ? " -old" : "") +
(norename ? " -nr" : "") +
(systems != "" ? " system=" + systems : "") +
(sources != "" ? " source=" + sources : "");
Process.Start("DATabase.exe", args);
}
private void button2_Click(object sender, EventArgs e)
{
Process.Start("DATabase.exe", "-l -ga");
}
}
}