Skip to content

Commit

Permalink
Now checks and enable developer mode if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
ignYoqzii authored Aug 18, 2024
1 parent 72239a8 commit 3474420
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 3 deletions.
80 changes: 80 additions & 0 deletions StarZ Launcher/StarZLauncher/Classes/DeveloperModeManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using Microsoft.Win32;
using StarZLauncher.Windows;
using System;
using System.Diagnostics;

namespace StarZLauncher.Classes
{
public class DeveloperModeManager
{
private const string RegistryPath = @"SOFTWARE\Microsoft\Windows\CurrentVersion\AppModelUnlock";
private const string AllowAllTrustedAppsValueName = "AllowAllTrustedApps";
private const string AllowDevelopmentWithoutDevLicenseValueName = "AllowDevelopmentWithoutDevLicense";

public static void EnableDeveloperMode()
{
if (IsDeveloperModeEnabled())
{
return;
}

string[] commands =
{
@$"reg add ""HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\AppModelUnlock"" /t REG_DWORD /f /v ""{AllowAllTrustedAppsValueName}"" /d ""1""",
@$"reg add ""HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\AppModelUnlock"" /t REG_DWORD /f /v ""{AllowDevelopmentWithoutDevLicenseValueName}"" /d ""1"""
};

foreach (var command in commands)
{
ExecutePowerShellCommand(command);
}
}

private static void ExecutePowerShellCommand(string command)
{
ProcessStartInfo psi = new("powershell.exe", command)
{
Verb = "runas", // Run PowerShell as administrator
CreateNoWindow = true, // Run without opening a window
WindowStyle = ProcessWindowStyle.Hidden // Hide the window
};

try
{
using (Process process = Process.Start(psi))
{
process?.WaitForExit();

// Check if the process exited with a non-zero exit code, indicating an error
if (process != null && process.ExitCode != 0)
{
throw new Exception($"PowerShell command failed with exit code {process.ExitCode}.");
}
}
}
catch (Exception ex)
{
throw new Exception($"There was an error enabling Developer Mode. It is recommended to enable it manually. {ex.Message}");
}
}

public static bool IsDeveloperModeEnabled()
{
try
{
using var key = Registry.LocalMachine.OpenSubKey(RegistryPath, false);
if (key == null)
{
throw new Exception("Registry key not found.");
}

var value = key.GetValue(AllowDevelopmentWithoutDevLicenseValueName);
return value is int intValue && intValue == 1;
}
catch (Exception ex)
{
throw new Exception($"An unexpected error occurred: {ex.Message}");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ private static async Task DownloadVersionAsync(string version, ProgressBar progr
await UnregisterAndDeleteVersionAsync(versionFolderPath);
await LoadVersionsAsync();
VersionHelper.LoadInstalledMinecraftVersion();
isRunning = false;
}
isRunning = false;
return;
}
else
Expand Down Expand Up @@ -348,4 +348,4 @@ await Task.Run(() =>
isRunning = false; // Reset installation in progress flag
}
}
}
}
13 changes: 13 additions & 0 deletions StarZ Launcher/StarZLauncher/Classes/PackageHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,19 @@ private static async Task<string> GetGameVersionFromJson(string packageVersion)

public static void RegisterAppPackage(string packagePath)
{
Application.Current.Dispatcher.Invoke(() =>
{
try
{
DeveloperModeManager.EnableDeveloperMode();
}
catch (Exception ex)
{
StarZMessageBox.ShowDialog($"Closing application to avoid corruption. Try enabling Developer Mode manually. {ex.Message}", "Error !", false);
Application.Current.Shutdown();
}
});

var packageUri = new Uri(packagePath);
var deploymentOperation = PackageManager.RegisterPackageAsync(packageUri, null, DeploymentOptions.DevelopmentMode);

Expand Down
1 change: 0 additions & 1 deletion StarZ Launcher/StarZLauncher/Classes/ToolsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,5 @@ public static void VSyncDisable()

StarZMessageBox.ShowDialog("V-Sync is now disabled!", "Success !", false);
}

}
}

0 comments on commit 3474420

Please sign in to comment.