Skip to content

Commit ac6ba96

Browse files
committed
refactor: Remade the tool in a better way with some new cool logging stuff, validating the registry bits.
1 parent 0a1c96c commit ac6ba96

File tree

2 files changed

+83
-35
lines changed

2 files changed

+83
-35
lines changed

cf-java-gpu-fix/Program.cs

Lines changed: 82 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22
using System.Collections.Concurrent;
33
using System.Security.Principal;
44

5-
Console.WriteLine("Hi! I will attempt to set so that your javaw.exe will use the GPU!");
5+
Log("Hi! I will attempt to set so that your javaw.exe will use the GPU!");
6+
7+
if (!OperatingSystem.IsWindows())
8+
{
9+
Log("This program is only for Windows, sorry!", LogLevel.Error);
10+
return -1;
11+
}
612

713
const string GPURegKey = @"Software\Microsoft\DirectX\UserGpuPreferences";
814

@@ -12,83 +18,99 @@
1218

1319
if (!winIdentity.IsInRole(WindowsBuiltInRole.Administrator))
1420
{
15-
Console.WriteLine();
21+
Log("""
22+
Error: You need to run this program as an administrator
23+
Right click me and use "Run as administrator"
1624
17-
Console.WriteLine("Error: You need to run this program as an administrator");
18-
Console.WriteLine("Right click me and use \"Run as administrator\"");
19-
20-
Console.WriteLine();
21-
22-
Console.WriteLine("(I need it, since I'm gonna rewrite some stuff in your registry)");
25+
(I need it, since I'm gonna rewrite some stuff in your registry)
26+
""", LogLevel.Error);
2327

2428
Console.ReadLine();
2529

2630
return -1;
2731
}
2832

29-
Console.WriteLine();
30-
31-
Console.WriteLine("[INFO] Checking for Overwolf/CurseForge registry keys");
33+
Log("Checking for Overwolf/CurseForge registry keys");
3234
var cfRegKey = Registry.CurrentUser.OpenSubKey(@"Software\Overwolf\CurseForge\");
3335
if (cfRegKey != null)
3436
{
3537
var cfMCRoot = cfRegKey.GetValue("minecraft_root") as string;
3638

3739
if (!string.IsNullOrWhiteSpace(cfMCRoot))
3840
{
39-
Console.WriteLine($"[INFO] Found it! Modding path is: {cfMCRoot}");
41+
Log($"Found it! Modding path is: {cfMCRoot}");
4042
var cfJavaExecutables = GetJavaExecutablesFromPath(cfMCRoot, "javaw.exe");
41-
Console.WriteLine($"[INFO] Found {cfJavaExecutables.Count} executables, adding to list");
43+
Log($"Found {cfJavaExecutables.Count} executables, adding to list");
4244
javaExecutables.AddRange(cfJavaExecutables.Select(j => j.FullName));
4345
}
4446
}
4547
else
4648
{
47-
Console.WriteLine("[INFO] Didn't find Overwolf/CurseForge.. anyhow..");
49+
Log("Didn't find Overwolf/CurseForge.. anyhow..", LogLevel.Warning);
4850
}
4951

50-
Console.WriteLine();
51-
5252
if (javaExecutables.Count == 0)
5353
{
54-
Console.WriteLine("[INFO] Well, this was awkward, we haven't found any \"important\" java executables.");
55-
Console.WriteLine("[INFO] So.. have a nice day!");
54+
Log("""
55+
Well, this was awkward, we haven't found any "important" java executables.
56+
So.. have a nice day!
57+
""", LogLevel.Warning);
5658
}
5759
else
5860
{
59-
Console.WriteLine($"[INFO] Do you want to continue before setting things in the registry? Gonna write {javaExecutables.Count} new keys. [Y/N]");
61+
Log($"Do you want to continue before setting things in the registry? Gonna write {javaExecutables.Count} new keys. [Y/N]");
6062
var acceptedKeys = new[] { ConsoleKey.Y, ConsoleKey.N };
6163
ConsoleKey key;
6264
do
6365
{
6466
key = Console.ReadKey(true).Key;
6567
if (!acceptedKeys.Contains(key))
6668
{
67-
Console.WriteLine($"[ERROR] That was {key}, not Y or N, try again..");
69+
Log($"That was {key}, not Y or N, try again..", LogLevel.Error);
6870
}
6971
} while (!acceptedKeys.Contains(key));
7072

7173
if (key == ConsoleKey.N)
7274
{
73-
Console.WriteLine("[INFO] Ok, exiting! Bye!");
75+
Log("Oh.. Ok, exiting! Bye!");
7476
return 0;
7577
}
7678

77-
Console.WriteLine($"[INFO] Fixing {javaExecutables.Count} executables");
79+
Log($"Fixing {javaExecutables.Count} executables in the registry");
7880

7981
var gpuKey = Registry.CurrentUser.OpenSubKey(GPURegKey, true);
80-
if (gpuKey != null)
82+
if (gpuKey == null)
83+
{
84+
Log("The required registry key was missing, creating new key for you!", LogLevel.Warning);
85+
gpuKey = Registry.CurrentUser.CreateSubKey(GPURegKey);
86+
}
87+
88+
foreach (var java in javaExecutables)
89+
{
90+
Log($"Fixing \"{java}\" for you!");
91+
gpuKey.SetValue(java, "GpuPreference=2;");
92+
}
93+
94+
gpuKey.Close();
95+
gpuKey.Dispose();
96+
gpuKey = null;
97+
98+
gpuKey = Registry.CurrentUser.OpenSubKey(GPURegKey)!;
99+
var keys = gpuKey.GetValueNames();
100+
var missingKeys = javaExecutables.Where(j => !keys.Contains(j)).ToList();
101+
if (missingKeys.Count > 0)
81102
{
82-
foreach (var java in javaExecutables)
103+
Log($"Failed to write {missingKeys.Count} keys to the registry", LogLevel.Error);
104+
foreach (var java in missingKeys)
83105
{
84-
Console.WriteLine($"[INFO] Fixing \"{java}\" for you!");
85-
gpuKey.SetValue(java, "GpuPreference=2;");
106+
Log($"Missing key: {java}", LogLevel.Error);
86107
}
87108

88-
gpuKey.Close();
109+
Log("You might need to do this manually.. sorry!", LogLevel.Error);
110+
return -1;
89111
}
90112

91-
Console.WriteLine("[INFO] All done! Happy playing!");
113+
Log("All done! Happy playing!", LogLevel.Success);
92114
}
93115

94116
Console.ReadLine();
@@ -97,25 +119,26 @@
97119

98120
ConcurrentStack<FileInfo> GetJavaExecutablesFromPath(string path, string searchPattern = "javaw.exe")
99121
{
100-
string[] IgnoredFolders = new string[] {
122+
string[] IgnoredFolders = [
101123
"\\$RECYCLE.BIN",
102124
"\\System Volume Information",
103125
"\\Recovery",
104126
"\\xtp",
105127
"\\Backups"
106-
};
128+
];
107129

108130
var files = new ConcurrentStack<FileInfo>();
109131

110-
var rootFolderFiles = new DirectoryInfo(path).GetFiles();
132+
var rootFolder = new DirectoryInfo(path);
133+
var rootFiles = rootFolder.EnumerateFiles(searchPattern);
111134

112-
foreach (var f in rootFolderFiles)
135+
foreach (var f in rootFiles)
113136
{
114137
files.Push(f);
115138
}
116139

117140
var directories = Directory.GetDirectories(path).Where(d => !IgnoredFolders.Any(i => d.EndsWith(i))).ToList();
118-
Parallel.ForEach(directories, dir =>
141+
foreach (var dir in directories)
119142
{
120143
try
121144
{
@@ -128,7 +151,32 @@ ConcurrentStack<FileInfo> GetJavaExecutablesFromPath(string path, string searchP
128151
catch
129152
{
130153
}
131-
});
154+
}
132155

133156
return files;
157+
}
158+
159+
void Log(string message, LogLevel level = LogLevel.Info)
160+
{
161+
Console.ForegroundColor = level switch
162+
{
163+
LogLevel.Info => ConsoleColor.White,
164+
LogLevel.Warning => ConsoleColor.Yellow,
165+
LogLevel.Error => ConsoleColor.Red,
166+
LogLevel.Success => ConsoleColor.Green,
167+
_ => ConsoleColor.White
168+
};
169+
170+
Console.Write($"[{DateTime.Now:HH:mm:ss} / ");
171+
Console.Write($"{level.ToString().ToUpper()}]");
172+
Console.ResetColor();
173+
Console.WriteLine($" {message}");
174+
}
175+
176+
enum LogLevel
177+
{
178+
Info,
179+
Warning,
180+
Error,
181+
Success
134182
}

cf-java-gpu-fix/cf-java-gpu-fix.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net6.0</TargetFramework>
5+
<TargetFramework>net8.0</TargetFramework>
66
<RootNamespace>cf_java_gpu_fix</RootNamespace>
77
<ImplicitUsings>enable</ImplicitUsings>
88
<Nullable>enable</Nullable>

0 commit comments

Comments
 (0)