diff --git a/Gaseous.sln b/Gaseous.sln index 2e09c95..d15120a 100644 --- a/Gaseous.sln +++ b/Gaseous.sln @@ -9,6 +9,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "gaseous-signature-parser", EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "gaseous-romsignatureobject", "gaseous-romsignatureobject\gaseous-romsignatureobject.csproj", "{9DCD243D-37CE-4562-8411-B5242B687D4F}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "gaseous-signature-ingestor", "gaseous-signature-ingestor\gaseous-signature-ingestor.csproj", "{86DF6E45-2C2B-4C30-AEC1-E7EF8C5CEA7D}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "gaseous-tools", "gaseous-tools\gaseous-tools.csproj", "{08FE408A-5EC1-4110-ABD8-D19A1155B8CE}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -35,6 +39,14 @@ Global {9DCD243D-37CE-4562-8411-B5242B687D4F}.Debug|Any CPU.Build.0 = Debug|Any CPU {9DCD243D-37CE-4562-8411-B5242B687D4F}.Release|Any CPU.ActiveCfg = Release|Any CPU {9DCD243D-37CE-4562-8411-B5242B687D4F}.Release|Any CPU.Build.0 = Release|Any CPU + {86DF6E45-2C2B-4C30-AEC1-E7EF8C5CEA7D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {86DF6E45-2C2B-4C30-AEC1-E7EF8C5CEA7D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {86DF6E45-2C2B-4C30-AEC1-E7EF8C5CEA7D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {86DF6E45-2C2B-4C30-AEC1-E7EF8C5CEA7D}.Release|Any CPU.Build.0 = Release|Any CPU + {08FE408A-5EC1-4110-ABD8-D19A1155B8CE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {08FE408A-5EC1-4110-ABD8-D19A1155B8CE}.Debug|Any CPU.Build.0 = Debug|Any CPU + {08FE408A-5EC1-4110-ABD8-D19A1155B8CE}.Release|Any CPU.ActiveCfg = Release|Any CPU + {08FE408A-5EC1-4110-ABD8-D19A1155B8CE}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/gaseous-signature-ingestor/Program.cs b/gaseous-signature-ingestor/Program.cs new file mode 100644 index 0000000..7988159 --- /dev/null +++ b/gaseous-signature-ingestor/Program.cs @@ -0,0 +1,126 @@ +using System; +using System.IO; +using MySql.Data.MySqlClient; +using gaseous_romsignatureobject; +using gaseous_signature_parser.parsers; +using gaseous_tools; + +string configPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".gaseous-server"); + +// process command line +string[] commandLineArgs = Environment.GetCommandLineArgs(); + +string tosecXML = ""; +string inArgument = ""; +foreach (string commandLineArg in commandLineArgs) +{ + if (commandLineArg != commandLineArgs[0]) + { + if (inArgument == "") + { + switch (commandLineArg.ToLower()) + { + case "-tosecpath": + inArgument = commandLineArg.ToLower(); + break; + default: + break; + } + } + else + { + switch (inArgument) + { + case "-tosecpath": + tosecXML = commandLineArg; + break; + default: + break; + } + inArgument = ""; + } + } +} + +// check if configPath is valid and create it if not +if (!Directory.Exists(configPath)) +{ + Directory.CreateDirectory(configPath); +} + +// connect to database +string cs = @"server=localhost;userid=gaseous;password=gaseous;database=gaseous"; +Database db = new gaseous_tools.Database(Database.databaseType.MySql, cs); + +// process provided files +Console.WriteLine("Processing input files:"); +if (Directory.Exists(tosecXML)) +{ + Console.WriteLine("Processing TOSEC data files", ConsoleColor.Green); + Console.WriteLine(""); + Console.WriteLine(""); + + tosecXML = Path.GetFullPath(tosecXML); + string[] tosecPathContents = Directory.GetFiles(tosecXML); + int lineFileNameLength = 0; + for (UInt16 i = 0; i < tosecPathContents.Length; ++i) + { + string tosecXMLFile = tosecPathContents[i]; + + string statusOutput = i + " / " + tosecPathContents.Length + " : " + Path.GetFileName(tosecXMLFile); + Console.SetCursorPosition(0, Console.CursorTop - 2); + Console.WriteLine("\r " + statusOutput.PadRight(lineFileNameLength, ' ') + "\r"); + lineFileNameLength = statusOutput.Length; + + Console.WriteLine("Parsing file"); + + TosecParser tosecParser = new TosecParser(); + RomSignatureObject tosecObject = tosecParser.Parse(tosecXMLFile); + + // store in database + foreach (RomSignatureObject.Game gameObject in tosecObject.Games) + { + string sql = ""; + Dictionary dbDict = new Dictionary(); + System.Data.DataTable sigDB; + + // store platform + if (gameObject.System != null) + { + sql = "SELECT * FROM signatures_platforms WHERE platform=@platform"; + dbDict = new Dictionary(); + dbDict.Add("platform", gameObject.System); + + sigDB = db.ExecuteCMD(sql, dbDict); + if (sigDB.Rows.Count == 0) + { + // entry not present, insert it + Console.SetCursorPosition(0, Console.CursorTop - 1); + Console.WriteLine("Saving platform: " + gameObject.System); + + sql = "INSERT INTO signatures_platforms (platform) VALUES (@platform)"; + db.ExecuteCMD(sql, dbDict); + } + } + + // store publisher + if (gameObject.Publisher != null) + { + sql = "SELECT * FROM signatures_publishers WHERE publisher=@publisher"; + dbDict = new Dictionary(); + dbDict.Add("publisher", gameObject.Publisher); + + sigDB = db.ExecuteCMD(sql, dbDict); + if (sigDB.Rows.Count == 0) + { + // entry not present, insert it + Console.SetCursorPosition(0, Console.CursorTop - 1); + Console.WriteLine("Saving publisher: " + gameObject.Publisher); + + sql = "INSERT INTO signatures_publishers (publisher) VALUES (@publisher)"; + db.ExecuteCMD(sql, dbDict); + } + } + } + } +} \ No newline at end of file diff --git a/gaseous-signature-ingestor/gaseous-signature-ingestor.csproj b/gaseous-signature-ingestor/gaseous-signature-ingestor.csproj new file mode 100644 index 0000000..9434a6f --- /dev/null +++ b/gaseous-signature-ingestor/gaseous-signature-ingestor.csproj @@ -0,0 +1,19 @@ + + + + Exe + net7.0 + gaseous_signature_ingestor + enable + enable + + + + + + + + + + + diff --git a/gaseous-tools/Database.cs b/gaseous-tools/Database.cs new file mode 100644 index 0000000..31e1bc1 --- /dev/null +++ b/gaseous-tools/Database.cs @@ -0,0 +1,111 @@ +using System; +using System.Data; +using System.Data.SqlClient; +using System.Diagnostics; +using MySql.Data.MySqlClient; + +namespace gaseous_tools +{ + public class Database + { + public Database() + { + + } + + public Database(databaseType Type, string ConnectionString) + { + _ConnectorType = Type; + _ConnectionString = ConnectionString; + } + + public enum databaseType + { + MySql + } + + string _ConnectionString = ""; + + public string ConnectionString + { + get + { + return _ConnectionString; + } + set + { + _ConnectionString = value; + } + } + + databaseType? _ConnectorType = null; + + public databaseType? ConnectorType + { + get + { + return _ConnectorType; + } + set + { + _ConnectorType = value; + } + } + + + public DataTable ExecuteCMD(string Command, Dictionary Parameters, int Timeout = 30) + { + switch (_ConnectorType) + { + case databaseType.MySql: + MySQLServerConnector conn = new MySQLServerConnector(_ConnectionString); + return (DataTable)conn.ExecCMD(Command, Parameters, Timeout); + default: + return new DataTable(); + } + } + + private partial class MySQLServerConnector + { + private string DBConn = ""; + + public MySQLServerConnector(string ConnectionString) + { + DBConn = ConnectionString; + } + + public DataTable ExecCMD(string SQL, Dictionary Parameters, int Timeout) + { + DataTable RetTable = new DataTable(); + + MySqlConnection conn = new MySqlConnection(DBConn); + conn.Open(); + + MySqlCommand cmd = new MySqlCommand + { + Connection = conn, + CommandText = SQL, + CommandTimeout = Timeout + }; + + foreach (string Parameter in Parameters.Keys) + { + cmd.Parameters.AddWithValue(Parameter, Parameters[Parameter]); + } + + try + { + RetTable.Load(cmd.ExecuteReader()); + } catch (Exception ex) { + Trace.WriteLine("Error executing " + SQL); + Trace.WriteLine("Full exception: " + ex.ToString()); + } + + conn.Close(); + + return RetTable; + } + } + } +} + diff --git a/gaseous-tools/gaseous-tools.csproj b/gaseous-tools/gaseous-tools.csproj new file mode 100644 index 0000000..3d07bdc --- /dev/null +++ b/gaseous-tools/gaseous-tools.csproj @@ -0,0 +1,13 @@ + + + + net7.0 + gaseous_tools + enable + enable + + + + + +