2020-08-01 13:25:32 -07:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.IO;
|
2024-03-06 01:04:51 -05:00
|
|
|
|
using Microsoft.Data.Sqlite;
|
2020-12-07 13:57:26 -08:00
|
|
|
|
using SabreTools.Help;
|
2020-12-07 15:08:57 -08:00
|
|
|
|
using SabreTools.IO;
|
2020-08-01 13:25:32 -07:00
|
|
|
|
|
|
|
|
|
|
namespace RombaSharp.Features
|
|
|
|
|
|
{
|
|
|
|
|
|
internal class Import : BaseFeature
|
|
|
|
|
|
{
|
|
|
|
|
|
public const string Value = "Import";
|
|
|
|
|
|
|
|
|
|
|
|
// Unique to RombaSharp
|
|
|
|
|
|
public Import()
|
|
|
|
|
|
{
|
|
|
|
|
|
Name = Value;
|
2024-07-18 01:06:40 -04:00
|
|
|
|
Flags.AddRange(["import"]);
|
2020-08-01 13:25:32 -07:00
|
|
|
|
Description = "Import a database from a formatted CSV file";
|
2020-12-07 13:57:26 -08:00
|
|
|
|
_featureType = ParameterType.Flag;
|
2020-08-01 13:25:32 -07:00
|
|
|
|
LongDescription = "Import a database from a formatted CSV file";
|
2021-02-03 11:10:19 -08:00
|
|
|
|
|
|
|
|
|
|
// Common Features
|
|
|
|
|
|
AddCommonFeatures();
|
2020-08-01 13:25:32 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-05 20:26:38 -05:00
|
|
|
|
public override bool ProcessFeatures(Dictionary<string, Feature?> features)
|
2020-08-01 13:25:32 -07:00
|
|
|
|
{
|
2021-03-19 20:52:11 -07:00
|
|
|
|
// If the base fails, just fail out
|
|
|
|
|
|
if (!base.ProcessFeatures(features))
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
2020-10-07 15:42:30 -07:00
|
|
|
|
logger.Error("This feature is not yet implemented: import");
|
2020-08-01 13:25:32 -07:00
|
|
|
|
|
2024-07-18 01:06:40 -04:00
|
|
|
|
// Ensure the inputs
|
2024-11-12 21:12:06 -05:00
|
|
|
|
var files = PathTool.GetFilesOnly(Inputs).ConvertAll(p => p.CurrentPath);
|
2024-07-18 01:06:40 -04:00
|
|
|
|
Inputs.Clear();
|
|
|
|
|
|
Inputs.AddRange(files);
|
|
|
|
|
|
|
|
|
|
|
|
// Ensure the database connection
|
|
|
|
|
|
var dbc = new SqliteConnection(_connectionString);
|
|
|
|
|
|
var slc = new SqliteCommand();
|
2020-08-01 13:25:32 -07:00
|
|
|
|
dbc.Open();
|
|
|
|
|
|
|
|
|
|
|
|
// Now, for each of these files, attempt to add the data found inside
|
|
|
|
|
|
foreach (string input in Inputs)
|
|
|
|
|
|
{
|
2024-10-19 21:41:08 -04:00
|
|
|
|
var sr = new StreamReader(File.OpenRead(input));
|
2020-08-01 13:25:32 -07:00
|
|
|
|
|
|
|
|
|
|
// The first line should be the hash header
|
2024-03-05 20:26:38 -05:00
|
|
|
|
string? line = sr.ReadLine();
|
2020-08-01 13:25:32 -07:00
|
|
|
|
if (line != "CRC,MD5,SHA-1") // ,Depot
|
|
|
|
|
|
{
|
2020-10-07 15:42:30 -07:00
|
|
|
|
logger.Error($"{input} is not a valid export file");
|
2020-08-01 13:25:32 -07:00
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Define the insert queries
|
|
|
|
|
|
string crcquery = "INSERT OR IGNORE INTO crc (crc) VALUES";
|
|
|
|
|
|
string md5query = "INSERT OR IGNORE INTO md5 (md5) VALUES";
|
|
|
|
|
|
string sha1query = "INSERT OR IGNORE INTO sha1 (sha1) VALUES";
|
|
|
|
|
|
string crcsha1query = "INSERT OR IGNORE INTO crcsha1 (crc, sha1) VALUES";
|
|
|
|
|
|
string md5sha1query = "INSERT OR IGNORE INTO md5sha1 (md5, sha1) VALUES";
|
|
|
|
|
|
|
|
|
|
|
|
// For each line until we hit a blank line...
|
|
|
|
|
|
while (!sr.EndOfStream && line != string.Empty)
|
|
|
|
|
|
{
|
2024-03-05 20:26:38 -05:00
|
|
|
|
line = sr.ReadLine() ?? string.Empty;
|
2020-08-01 13:25:32 -07:00
|
|
|
|
string[] hashes = line.Split(',');
|
|
|
|
|
|
|
|
|
|
|
|
// Loop through the parsed entries
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(hashes[0]))
|
|
|
|
|
|
crcquery += $" (\"{hashes[0]}\"),";
|
|
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(hashes[1]))
|
|
|
|
|
|
md5query += $" (\"{hashes[1]}\"),";
|
|
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(hashes[2]))
|
|
|
|
|
|
{
|
|
|
|
|
|
sha1query += $" (\"{hashes[2]}\"),";
|
|
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(hashes[0]))
|
|
|
|
|
|
crcsha1query += $" (\"{hashes[0]}\", \"{hashes[2]}\"),";
|
|
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(hashes[1]))
|
|
|
|
|
|
md5sha1query += $" (\"{hashes[1]}\", \"{hashes[2]}\"),";
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Now run the queries after fixing them
|
|
|
|
|
|
if (crcquery != "INSERT OR IGNORE INTO crc (crc) VALUES")
|
|
|
|
|
|
{
|
|
|
|
|
|
slc = new SqliteCommand(crcquery.TrimEnd(','), dbc);
|
|
|
|
|
|
slc.ExecuteNonQuery();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (md5query != "INSERT OR IGNORE INTO md5 (md5) VALUES")
|
|
|
|
|
|
{
|
|
|
|
|
|
slc = new SqliteCommand(md5query.TrimEnd(','), dbc);
|
|
|
|
|
|
slc.ExecuteNonQuery();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (sha1query != "INSERT OR IGNORE INTO sha1 (sha1) VALUES")
|
|
|
|
|
|
{
|
|
|
|
|
|
slc = new SqliteCommand(sha1query.TrimEnd(','), dbc);
|
|
|
|
|
|
slc.ExecuteNonQuery();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (crcsha1query != "INSERT OR IGNORE INTO crcsha1 (crc, sha1) VALUES")
|
|
|
|
|
|
{
|
|
|
|
|
|
slc = new SqliteCommand(crcsha1query.TrimEnd(','), dbc);
|
|
|
|
|
|
slc.ExecuteNonQuery();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (md5sha1query != "INSERT OR IGNORE INTO md5sha1 (md5, sha1) VALUES")
|
|
|
|
|
|
{
|
|
|
|
|
|
slc = new SqliteCommand(md5sha1query.TrimEnd(','), dbc);
|
|
|
|
|
|
slc.ExecuteNonQuery();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Now add all of the DAT hashes
|
|
|
|
|
|
// TODO: Do we really need to save the DAT hashes?
|
|
|
|
|
|
|
|
|
|
|
|
sr.Dispose();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
slc.Dispose();
|
|
|
|
|
|
dbc.Dispose();
|
2021-03-19 20:52:11 -07:00
|
|
|
|
return true;
|
2020-08-01 13:25:32 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|