Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement CMD command for ChangeTheme. #959

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 113 additions & 3 deletions RetroBar/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using System;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text.Json;

namespace RetroBar
{
Expand All @@ -7,6 +11,7 @@ internal sealed class Program
private const string MutexName = "RetroBar";
private const int MutexAttempts = 10;
private const int MutexWaitMs = 1000;
private static readonly string settingsFilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "RetroBar", "settings.json");

private static System.Threading.Mutex _retroBarMutex;

Expand All @@ -16,21 +21,29 @@ internal sealed class Program
[STAThread]
public static int Main(string[] args)
{
// Check if the -ChangeTheme argument is provided
if (args.Length > 0 && args[0] == "-ChangeTheme" && args.Length > 1)
{
string themeName = args[1];
ChangeTheme(themeName);
return 0; // Exit immediately after processing the command
}

// If no command line argument is given, start the normal RetroBar application
if (!SingleInstanceCheck())
{
return 1;
}

// Proceed with the normal application flow
App app = new App();
app.InitializeComponent();

return app.Run();
}

private static bool GetMutex()
{
_retroBarMutex = new System.Threading.Mutex(true, MutexName, out bool ok);

return ok;
}

Expand All @@ -52,5 +65,102 @@ private static bool SingleInstanceCheck()

return false;
}

// Method to change the theme and update the settings.json file
private static void ChangeTheme(string themeName)
{
// Kill any running instances of retrobar.exe
KillRetroBarProcesses();

// Ensure the folder exists
string directoryPath = Path.GetDirectoryName(settingsFilePath);
if (!Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
}

// Check if the settings file exists
if (!File.Exists(settingsFilePath))
{
// If file does nott exist, create a new one with the theme property
Dictionary<string, object> initialSettings = new()
{
["Theme"] = themeName
};
WriteJsonToFile(initialSettings);
return;
}

// Read the existing settings.json file
try
{
string jsonContent = File.ReadAllText(settingsFilePath);

// Parse the existing JSON into a Dictionary
Dictionary<string, object> settings = JsonSerializer.Deserialize<Dictionary<string, object>>(jsonContent);

// Check if the theme property exists and update it
if (settings != null)
{
if (settings.ContainsKey("Theme"))
{
settings["Theme"] = themeName;
}
else
{
settings.Add("Theme", themeName);
}

// Write the updated JSON back to the file
WriteJsonToFile(settings);
}
}
catch (Exception ex)
{
Debug.WriteLine($"Error reading or updating settings: {ex.Message}");
}
}

private static void WriteJsonToFile(object jsonObject)
{
try
{
// Serialize the object to JSON string with indentation
string jsonString = JsonSerializer.Serialize(jsonObject, new JsonSerializerOptions { WriteIndented = true });

// Write the serialized JSON string to the file
File.WriteAllText(settingsFilePath, jsonString);
}
catch (Exception ex)
{
Debug.WriteLine($"Error writing to file: {ex.Message}");
}
}

private static void KillRetroBarProcesses()
{
try
{
// Get the current process ID (this is the one we want to keep running)
int currentProcessId = System.Diagnostics.Process.GetCurrentProcess().Id;

// Get all processes running with the name "retrobar.exe"
Process[] processes = System.Diagnostics.Process.GetProcessesByName("retrobar");

// Iterate over all retrobar.exe processes
foreach (var process in processes)
{
// Skip the process that is currently running this command
if (process.Id != currentProcessId)
{
process.Kill();
}
}
}
catch (Exception ex)
{
Debug.WriteLine($"Error killing processes: {ex.Message}");
}
}
}
}