Skip to content
Open
Show file tree
Hide file tree
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
127 changes: 127 additions & 0 deletions RetroBat/RetroBat/AutostartManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
using System;
using System.IO;
using Microsoft.Win32;

namespace RetroBat
{
internal static class AutostartManager
{
public static void CleanupLegacyShortcut()
{
try
{
string startupFolder = Environment.GetFolderPath(Environment.SpecialFolder.Startup);
string linkStartup = Path.Combine(startupFolder, "RetroBat.lnk");

if (File.Exists(linkStartup))
{
try { File.Delete(linkStartup); }
catch (Exception ex) { SimpleLogger.Instance.Warning("Failed to delete legacy RetroBat.lnk: " + ex.Message); }
}
}
catch (Exception ex) { SimpleLogger.Instance.Warning("CleanupStartup failed: " + ex.Message); }
}

public static void Apply(int autostartMode, string appFolder, string appExe)
{
if (autostartMode == 1)
{
AddToStartupFolder(appFolder, appExe);
RemoveFromStartupReg();
}
else if (autostartMode == 2)
{
AddToStartupReg(appFolder, appExe);
RemoveFromStartupFolder("RetroBat");
}
else
{
RemoveFromStartupReg();
RemoveFromStartupFolder("RetroBat");
}
}

private static void AddToStartupReg(string appPath, string appExe)
{
SimpleLogger.Instance.Info("Setting RetroBat to launch at startup.");

string batPath = Path.Combine(appPath, appExe);

string regValue = string.Format(
"cmd.exe /c \"cd /d {0} && start \"\" \"{1}\"\"\"",
appPath,
batPath
);

try
{
RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true);
key.SetValue("RetroBat", regValue);
SimpleLogger.Instance.Info("RetroBat set in registry to startup.");
}
catch (Exception ex)
{
SimpleLogger.Instance.Warning("Failed to set startup registry key: " + ex.Message);
}
}

private static void AddToStartupFolder(string exePath, string shortcutName)
{
try
{
string startupFolder = Environment.GetFolderPath(Environment.SpecialFolder.Startup);
string exeName = Path.GetFileNameWithoutExtension(shortcutName);
string batPath = Path.Combine(startupFolder, exeName + ".bat");
string exe = Path.Combine(exePath, shortcutName);

// Write a simple batch file to start RetroBat
string batContent = $"@echo off{Environment.NewLine}cd /d \"{exePath}\"{Environment.NewLine}\"{exe}\"";
File.WriteAllText(batPath, batContent);

SimpleLogger.Instance.Info("RetroBat batch added to Startup folder: " + batPath);
}
catch (Exception ex)
{
SimpleLogger.Instance.Warning("Failed to add RetroBat to Startup folder: " + ex.Message);
}
}

private static void RemoveFromStartupFolder(string shortcutName)
{
try
{
string startupFolder = Environment.GetFolderPath(Environment.SpecialFolder.Startup);
string batPath = Path.Combine(startupFolder, shortcutName + ".bat");

if (File.Exists(batPath))
{
File.Delete(batPath);
SimpleLogger.Instance.Info("RetroBat removed from Startup folder: " + batPath);
}
else
{
SimpleLogger.Instance.Info("RetroBat startup batch not found, nothing to remove.");
}
}
catch (Exception ex)
{
SimpleLogger.Instance.Warning("Failed to remove RetroBat from Startup folder: " + ex.Message);
}
}

private static void RemoveFromStartupReg()
{
SimpleLogger.Instance.Info("Ensuring RetroBat does not launch at startup.");

try
{
RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true);
key.DeleteValue("RetroBat");
}
catch (Exception ex)
{
SimpleLogger.Instance.Warning("Failed to remove startup registry key: " + ex.Message);
}
}
}
}
95 changes: 95 additions & 0 deletions RetroBat/RetroBat/DpiAwarenessManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.Win32;

namespace RetroBat
{
internal static class DpiAwarenessManager
{
public static void ApplyOverridesIfNeeded(string appFolder)
{
if (!HasDpiScaling())
return;

string dpiFile = Path.Combine(appFolder, "system", "tools", "dpi_awareness.txt");

if (!File.Exists(dpiFile))
return;

try
{
var dpiLines = File.ReadAllLines(dpiFile);

if (dpiLines.Length > 0)
{
foreach (var dpiLine in dpiLines)
{
string dpiExePath = Path.Combine(appFolder, dpiLine.Trim());

if (File.Exists(dpiExePath))
SetDpiAwarenessOverride(dpiExePath, true);
}
}
}
catch (Exception ex) { SimpleLogger.Instance.Warning("Failed to apply DPI awareness overrides: " + ex.Message); }
}

public static bool HasDpiScaling()
{
using (var key = Registry.LocalMachine.OpenSubKey(
@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\FontDPI"))
{
object val = key != null ? key.GetValue("LogPixels") : null;
if (val is int dpi)
return dpi != 96;
}

using (var key = Registry.CurrentUser.OpenSubKey(
@"Control Panel\Desktop"))
{
object val = key != null ? key.GetValue("LogPixels") : null;
if (val is int dpi)
return dpi != 96;
}

return false;
}

public static void SetDpiAwarenessOverride(string exePath, bool enable)
{
const string keyPath = @"Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers";

RegistryKey key = Registry.CurrentUser.OpenSubKey(keyPath, true)
?? Registry.CurrentUser.CreateSubKey(keyPath);

if (key == null)
return;

using (key)
{
string current = key.GetValue(exePath) as string ?? string.Empty;

var flags = new HashSet<string>(current.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries));

if (enable)
{
if (flags.Contains("HIGHDPIAWARE"))
return;
flags.Add("HIGHDPIAWARE");
}
else
{
if (!flags.Contains("HIGHDPIAWARE"))
return;
flags.Remove("HIGHDPIAWARE");
}

if (flags.Count == 0)
key.DeleteValue(exePath, false);
else
key.SetValue(exePath, string.Join(" ", flags), RegistryValueKind.String);
}
}
}
}
Loading