Skip to content

Commit

Permalink
implement log system
Browse files Browse the repository at this point in the history
  • Loading branch information
Lesueur Benjamin committed Aug 20, 2020
1 parent 7b7ba57 commit 1838ab6
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 33 deletions.
89 changes: 74 additions & 15 deletions DockerForm/DatabaseManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public static void UpdateFilesAndRegistries(DockerGame game, string path_dest, s
foreach (GameSettings setting in game.Settings.Values.Where(a => a.IsEnabled))
{
string filename = Environment.ExpandEnvironmentVariables(setting.GetUri(game));
string file = Path.GetFileName(filename);

if (!File.Exists(filename))
{
Expand All @@ -30,34 +31,88 @@ public static void UpdateFilesAndRegistries(DockerGame game, string path_dest, s

if (setting.Type == SettingsType.File)
{
// We store the data
byte[] s_file = File.ReadAllBytes(filename);

// 1. Save current settings
if (updateDB)
setting.data[path_game] = File.ReadAllBytes(filename);
{
if ((setting.data.ContainsKey(path_game) && !Equality(s_file, setting.data[path_game])) || !setting.data.ContainsKey(path_game))
{
setting.data[path_game] = s_file;
Form1.UpdateLog("[" + game.Name + "]" + " database data were updated for file [" + file + "]");
}
else
{
Form1.UpdateLog("[" + game.Name + "]" + " database data update skipped for file [" + file + "] - similar");
}
}

// 2. Restore proper settings
if (updateFILE && setting.data.ContainsKey(path_dest))
if (updateFILE)
{
File.WriteAllBytes(filename, setting.data[path_dest]);
File.SetLastWriteTime(filename, game.LastCheck);
if (setting.data.ContainsKey(path_dest))
{
if (!Equality(s_file, setting.data[path_dest]))
{
File.WriteAllBytes(filename, setting.data[path_dest]);
File.SetLastWriteTime(filename, game.LastCheck);
Form1.UpdateLog("[" + game.Name + "]" + " local data were restored for file [" + file + "]");
}
else
{
Form1.UpdateLog("[" + game.Name + "]" + " local data restore skipped for file [" + file + "] - similar");
}
}
else
{
Form1.UpdateLog("[" + game.Name + "]" + " local data restore skipped for file [" + file + "] - no database data available");
}
}
}
else if(setting.Type == SettingsType.Registry)
{
// We generate a temporary reg file
string tempfile = Path.Combine(Form1.path_application, "temp.reg");
RegistryManager.ExportKey(filename, tempfile);

// We store the data
byte[] s_file = File.ReadAllBytes(tempfile);

// 1. Save current settings
if (updateDB)
{
RegistryManager.ExportKey(filename, tempfile);
setting.data[path_game] = File.ReadAllBytes(tempfile);
if ((setting.data.ContainsKey(path_game) && !Equality(s_file, setting.data[path_game])) || !setting.data.ContainsKey(path_game))
{
setting.data[path_game] = s_file;
Form1.UpdateLog("[" + game.Name + "]" + " database registry data were updated for file [" + file + "]");
}
else
{
Form1.UpdateLog("[" + game.Name + "]" + " database registry data update skipped for file [" + file + "] - similar");
}
}

// 2. Restore proper settings
if (updateFILE && setting.data.ContainsKey(path_dest))
if (updateFILE)
{
File.WriteAllBytes(tempfile, setting.data[path_dest]);
RegistryManager.RestoreKey(tempfile);
if(setting.data.ContainsKey(path_dest))
{
if (!Equality(s_file, setting.data[path_dest]))
{
File.WriteAllBytes(tempfile, setting.data[path_dest]);
RegistryManager.RestoreKey(tempfile);
Form1.UpdateLog("[" + game.Name + "]" + " local registry data were restored for file [" + file + "]");
}
else
{
Form1.UpdateLog("[" + game.Name + "]" + " local registry data restore skipped for file [" + file + "] - similar");
}
}
else
{
Form1.UpdateLog("[" + game.Name + "]" + " local registry data restore skipped for file [" + file + "] - no database data available");
}
}

// Delete the temporary reg file
Expand All @@ -77,6 +132,8 @@ public static void UpdateFilesAndRegistries(bool DockStatus)
{
string path_db = DockStatus ? Form1.VideoControllers[true].Name : Form1.VideoControllers[false].Name;

Form1.UpdateLog("Updating database with docking status set to: " + DockStatus);

// Scroll the provided database
foreach (DockerGame game in GameDB.Values)
{
Expand All @@ -86,7 +143,7 @@ public static void UpdateFilesAndRegistries(bool DockStatus)
UpdateFilesAndRegistries(game, path_db, crc_value, true, true, false, path_db);
}

Form1.SendNotification("All settings have been updated for (" + path_db + ")", true);
Form1.SendNotification("Database has been updated for (" + path_db + ")", true);
}

public static bool Equality(byte[] a1, byte[] b1)
Expand Down Expand Up @@ -144,9 +201,9 @@ public static void SanityCheck()
{
switch(game.ErrorCode)
{
case ErrorCode.MissingExecutable: Form1.SendNotification(game.Name + " has an unreachable executable.", true); break;
case ErrorCode.MissingFolder: Form1.SendNotification(game.Name + " has an unreachable folder.", true); break;
case ErrorCode.MissingSettings: Form1.SendNotification(game.Name + " has no settings defined.", true); break;
case ErrorCode.MissingExecutable: Form1.UpdateLog("[" + game.Name + "]" + " has an unreachable executable"); break;
case ErrorCode.MissingFolder: Form1.UpdateLog("[" + game.Name + "]" + " has an unreachable folder"); break;
case ErrorCode.MissingSettings: Form1.UpdateLog("[" + game.Name + "]" + " has no settings defined"); break;
}

continue;
Expand Down Expand Up @@ -196,15 +253,17 @@ public static void SanityCheck()

if (path_db != crc_value)
{
Form1.SendNotification("CRC missmatch detected for " + game.Name + ". Settings will be restored. (CRC: " + crc_value + ", Current: " + path_db + ")", true, true);

// Overwrite current database and restore last known settings
UpdateFilesAndRegistries(game, crc_value, path_db, true, true, false, path_db);

Form1.SendNotification(game.Name + " settings were restored due to a CRC missmatch (was: " + crc_value + ", now is: " + path_db + ").", true);

continue;
}
else if (file.LastWriteTime > game.LastCheck || !Equality(fileBytes,fileDBBytes))
{
Form1.SendNotification("Database sync conflict detected for " + game.Name, true, true);

DialogBox dialogBox = new DialogBox();
dialogBox.UpdateDialogBox("Database Sync Conflict", game.Name, game.LastCheck, file.LastWriteTime);
DialogResult dialogResult = dialogBox.ShowDialog();
Expand Down
83 changes: 65 additions & 18 deletions DockerForm/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,27 +40,72 @@ public partial class Form1 : Form
// Form vars
private static Form1 _instance;
private static Thread ThreadGPU, ThreadDB;
private static DateTime LogTime;

public static void SendNotification(string input, bool pushToast)
public static void InitiateLog()
{
if (!ToastNotifications)
return;
string filename = "DockerForm.log";
if (File.Exists(filename))
File.Delete(filename);

File.CreateText(filename).Close();

UpdateLog("Initialization complete");
}

public static void UpdateLog(string input, bool IsError = false)
{
string filename = "DockerForm.log";
LogTime = DateTime.Now;

string type = IsError ? "ERROR" : "LOG";

using (StreamWriter sw = File.AppendText(filename))
sw.WriteLine(LogTime + "\t" + type + "\t\t" + input);
}

public static void SendNotification(string input, bool pushToast, bool pushLog = false)
{
_instance.BeginInvoke(new Action(() => _instance.debugTextBox.Text = input));

if (!ToastNotifications)
return;

if (pushToast)
{
_instance.BeginInvoke(new Action(() => _instance.notifyIcon1.BalloonTipText = input));
_instance.BeginInvoke(new Action(() => _instance.notifyIcon1.ShowBalloonTip(1000)));
_instance.notifyIcon1.BalloonTipText = input;
_instance.notifyIcon1.ShowBalloonTip(1000);
}

if (pushLog)
UpdateLog(input);
}

public static void StatusMonitor(object data)
{
while (IsRunning)
{
if(IsHardwareReady && (IsHardwareNew || IsFirstBoot))
UpdateGameDatabase();
if (IsHardwareReady)
{
if (IsFirstBoot)
{
InitiateLog();
UpdateLog("iGPU: " + (VideoControllers.ContainsKey(false) ? VideoControllers[false].Name : "none"));

if (MonitorProcesses)
{
Thread ThreadEXE = new Thread(ProcessMonitor);
ThreadEXE.Start();
}
}

if (IsHardwareNew)
UpdateLog("eGPU: " + (VideoControllers.ContainsKey(true) ? VideoControllers[true].Name : "none"));

if (IsHardwareNew || IsFirstBoot)
UpdateGameDatabase();
}

Thread.Sleep(1000);
}
}
Expand Down Expand Up @@ -107,7 +152,10 @@ on p.Id equals (int)(uint)mo["ProcessId"]
continue;

if (!DatabaseManager.GameProcesses.ContainsKey(game))
{
DatabaseManager.GameProcesses.Add(game, item.Process);
UpdateLog("Process [" + item.Process.Id + "] " + item.Process.ProcessName + " has started");
}
}
}
}
Expand All @@ -126,10 +174,11 @@ on p.Id equals (int)(uint)mo["ProcessId"]
DatabaseManager.UpdateFilesAndRegistries(game, path_game, path_game, true, false, true, path_game);

DatabaseManager.GameProcesses.Remove(game);
UpdateLog("Process [" + proc.Id + "] " + proc.ProcessName + " has halted");
}
}
}
catch (Exception ex) { SendNotification("ProcessMonitor: " + ex.Message, true); }
catch (Exception ex) { UpdateLog("ProcessMonitor: " + ex.Message, true); }
Thread.Sleep(1000);
}
}
Expand Down Expand Up @@ -175,7 +224,7 @@ public static void VideoControllerMonitor(object data)
IsHardwareNew = (prevDockStatus != DockStatus);
prevDockStatus = DockStatus;
}
catch (Exception ex) { SendNotification("VideoControllerMonitor: " + ex.Message, true); }
catch (Exception ex) { UpdateLog("VideoControllerMonitor: " + ex.Message, true); }
Thread.Sleep(1000);
}
}
Expand All @@ -195,7 +244,7 @@ public static void UpdateFormIcons()
_instance.notifyIcon1.Icon = myIcon;
_instance.Icon = myIcon;
}
catch (Exception ex) { SendNotification("UpdateFormIcons: " + ex.Message, true); }
catch (Exception ex) { UpdateLog("UpdateFormIcons: " + ex.Message, true); }
}

public void InsertOrUpdateGameItem(DockerGame game)
Expand All @@ -206,6 +255,8 @@ public void InsertOrUpdateGameItem(DockerGame game)
{
GameList.Items.Add(newitem);
DatabaseManager.GameDB[game.GUID] = game;

UpdateLog("[" + game.Name + "] has been added to the database");
}

// Update current title
Expand Down Expand Up @@ -236,7 +287,7 @@ public void UpdateGameList()
reader.Dispose();
}
}
catch (Exception ex) { SendNotification("UpdateGameList: " + ex.Message, true); }
catch (Exception ex) { UpdateLog("UpdateGameList: " + ex.Message, true); }
}

// Update the DockerGame database
Expand Down Expand Up @@ -306,12 +357,6 @@ private void Form1_Shown(object sender, System.EventArgs e)

ThreadGPU.Start();
ThreadDB.Start();

if (MonitorProcesses)
{
Thread ThreadEXE = new Thread(ProcessMonitor);
ThreadEXE.Start();
}
}

private void Form1_FormClosing(Object sender, FormClosingEventArgs e)
Expand Down Expand Up @@ -481,6 +526,7 @@ private void removeTheGameToolStripMenuItem_Click(object sender, EventArgs e)

DatabaseManager.GameDB.TryRemove(item.Guid, out game);
GameList.Items.Remove(item);
UpdateLog("[" + game.Name + "] has been removed from the database");
}
}
}
Expand All @@ -504,11 +550,12 @@ private void findAGameToolStripMenuItem_Click(object sender, EventArgs e)

// only display the settings window when an executable has been picked.
if (currentSettings.GetIsReady())
currentSettings.Show();
{
DockerGame game = currentSettings.GetGame();
if (!DatabaseManager.GameDB.ContainsKey(game.GUID))
currentSettings.Show();
else
SendNotification(game.Name + " already exists in your current database", true);
}
}

Expand Down

0 comments on commit 1838ab6

Please sign in to comment.