2
2
using System . Collections . Concurrent ;
3
3
using System . Security . Principal ;
4
4
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
+ }
6
12
7
13
const string GPURegKey = @"Software\Microsoft\DirectX\UserGpuPreferences" ;
8
14
12
18
13
19
if ( ! winIdentity . IsInRole ( WindowsBuiltInRole . Administrator ) )
14
20
{
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"
16
24
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 ) ;
23
27
24
28
Console . ReadLine ( ) ;
25
29
26
30
return - 1 ;
27
31
}
28
32
29
- Console . WriteLine ( ) ;
30
-
31
- Console . WriteLine ( "[INFO] Checking for Overwolf/CurseForge registry keys" ) ;
33
+ Log ( "Checking for Overwolf/CurseForge registry keys" ) ;
32
34
var cfRegKey = Registry . CurrentUser . OpenSubKey ( @"Software\Overwolf\CurseForge\" ) ;
33
35
if ( cfRegKey != null )
34
36
{
35
37
var cfMCRoot = cfRegKey . GetValue ( "minecraft_root" ) as string ;
36
38
37
39
if ( ! string . IsNullOrWhiteSpace ( cfMCRoot ) )
38
40
{
39
- Console . WriteLine ( $ "[INFO] Found it! Modding path is: { cfMCRoot } ") ;
41
+ Log ( $ "Found it! Modding path is: { cfMCRoot } ") ;
40
42
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") ;
42
44
javaExecutables . AddRange ( cfJavaExecutables . Select ( j => j . FullName ) ) ;
43
45
}
44
46
}
45
47
else
46
48
{
47
- Console . WriteLine ( "[INFO] Didn't find Overwolf/CurseForge.. anyhow..") ;
49
+ Log ( " Didn't find Overwolf/CurseForge.. anyhow..", LogLevel . Warning ) ;
48
50
}
49
51
50
- Console . WriteLine ( ) ;
51
-
52
52
if ( javaExecutables . Count == 0 )
53
53
{
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 ) ;
56
58
}
57
59
else
58
60
{
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]") ;
60
62
var acceptedKeys = new [ ] { ConsoleKey . Y , ConsoleKey . N } ;
61
63
ConsoleKey key ;
62
64
do
63
65
{
64
66
key = Console . ReadKey ( true ) . Key ;
65
67
if ( ! acceptedKeys . Contains ( key ) )
66
68
{
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 ) ;
68
70
}
69
71
} while ( ! acceptedKeys . Contains ( key ) ) ;
70
72
71
73
if ( key == ConsoleKey . N )
72
74
{
73
- Console . WriteLine ( "[INFO] Ok, exiting! Bye!") ;
75
+ Log ( "Oh.. Ok, exiting! Bye!") ;
74
76
return 0 ;
75
77
}
76
78
77
- Console . WriteLine ( $ "[INFO] Fixing { javaExecutables . Count } executables") ;
79
+ Log ( $ "Fixing { javaExecutables . Count } executables in the registry ") ;
78
80
79
81
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 )
81
102
{
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 )
83
105
{
84
- Console . WriteLine ( $ "[INFO] Fixing \" { java } \" for you!") ;
85
- gpuKey . SetValue ( java , "GpuPreference=2;" ) ;
106
+ Log ( $ "Missing key: { java } ", LogLevel . Error ) ;
86
107
}
87
108
88
- gpuKey . Close ( ) ;
109
+ Log ( "You might need to do this manually.. sorry!" , LogLevel . Error ) ;
110
+ return - 1 ;
89
111
}
90
112
91
- Console . WriteLine ( "[INFO] All done! Happy playing!") ;
113
+ Log ( " All done! Happy playing!", LogLevel . Success ) ;
92
114
}
93
115
94
116
Console . ReadLine ( ) ;
97
119
98
120
ConcurrentStack < FileInfo > GetJavaExecutablesFromPath ( string path , string searchPattern = "javaw.exe" )
99
121
{
100
- string [ ] IgnoredFolders = new string [ ] {
122
+ string [ ] IgnoredFolders = [
101
123
"\\ $RECYCLE.BIN" ,
102
124
"\\ System Volume Information" ,
103
125
"\\ Recovery" ,
104
126
"\\ xtp" ,
105
127
"\\ Backups"
106
- } ;
128
+ ] ;
107
129
108
130
var files = new ConcurrentStack < FileInfo > ( ) ;
109
131
110
- var rootFolderFiles = new DirectoryInfo ( path ) . GetFiles ( ) ;
132
+ var rootFolder = new DirectoryInfo ( path ) ;
133
+ var rootFiles = rootFolder . EnumerateFiles ( searchPattern ) ;
111
134
112
- foreach ( var f in rootFolderFiles )
135
+ foreach ( var f in rootFiles )
113
136
{
114
137
files . Push ( f ) ;
115
138
}
116
139
117
140
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 )
119
142
{
120
143
try
121
144
{
@@ -128,7 +151,32 @@ ConcurrentStack<FileInfo> GetJavaExecutablesFromPath(string path, string searchP
128
151
catch
129
152
{
130
153
}
131
- } ) ;
154
+ }
132
155
133
156
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
134
182
}
0 commit comments