Skip to content

Commit

Permalink
feat(sqlite): link run id with overlay hash
Browse files Browse the repository at this point in the history
closes #248
  • Loading branch information
DorielRivalet committed Jan 26, 2024
1 parent 884e4f6 commit 852cbf7
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions MHFZ_Overlay/Services/DatabaseService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1830,6 +1830,30 @@ FinalTimeValue ASC

Logger.Debug("Inserted into QuestsCourse table");

sql = @"INSERT INTO QuestsOverlayHash (
OverlayHash,
RunID
) VALUES (
@OverlayHash,
@RunID
)";

using (var cmd = new SQLiteCommand(sql, conn))
{
var hash = GetOverlayHash();

cmd.Parameters.AddWithValue("@OverlayHash", hash);
cmd.Parameters.AddWithValue("@RunID", runID);
cmd.ExecuteNonQuery();
}

Logger.Debug("Inserted into QuestsOverlayHash table");


// TODO more tables



var gearName = s.GearDescriptionExport;
if (string.IsNullOrEmpty(gearName))
{
Expand Down Expand Up @@ -2958,6 +2982,49 @@ private static void HandleError(SQLiteTransaction? transaction, Exception ex)
LoggingService.WriteCrashLog(ex, $"SQLite error (version: {serverVersion})");
}

public string GetOverlayHash()
{
var overlayHash = string.Empty;

// Find the path of the first found process with the name "MHFZ_Overlay.exe"
var exeName = "MHFZ_Overlay.exe";
var processes = Process.GetProcessesByName(Path.GetFileNameWithoutExtension(exeName));
var exePath = string.Empty;
if (processes.Length > 0)
{
var module = processes[0].MainModule;
if (module == null)
{
return string.Empty;
}
else
{
exePath = module.FileName;
}
}
else
{
exePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, exeName);
}

if (exePath == null)
{
return string.Empty;
}

// Calculate the SHA256 hash of the executable
using (var sha256 = SHA256.Create())
{
using (var stream = File.OpenRead(exePath))
{
var hash = sha256.ComputeHash(stream);
overlayHash = BitConverter.ToString(hash).Replace("-", string.Empty);
}
}

return overlayHash;
}

/// <summary>
/// Stores the overlay hash.
/// </summary>
Expand Down Expand Up @@ -5205,6 +5272,17 @@ FOREIGN KEY(RunID) REFERENCES Quests(RunID)
cmd.ExecuteNonQuery();
}

sql = @"CREATE TABLE IF NOT EXISTS QuestsOverlayHash(
QuestsOverlayHashID INTEGER PRIMARY KEY AUTOINCREMENT,
OverlayHash TEXT NOT NULL DEFAULT '',
RunID INTEGER NOT NULL,
FOREIGN KEY(RunID) REFERENCES Quests(RunID)
)";
using (var cmd = new SQLiteCommand(sql, conn))
{
cmd.ExecuteNonQuery();
}

// a mh game but like a MUD. hunt in-game to get many kinds of points for this game. hunt and tame monsters. challenge other CPU players/monsters.
sql = @"CREATE TABLE IF NOT EXISTS GachaMaterial(
GachaMaterialID INTEGER PRIMARY KEY,
Expand Down

0 comments on commit 852cbf7

Please sign in to comment.