1+ using Microsoft . Win32 ;
2+ using System . Collections . Concurrent ;
3+ using System . Security . Principal ;
4+
5+ Console . WriteLine ( "Hi! I will attempt to set so that your javaw.exe will use the GPU!" ) ;
6+
7+ const string GPURegKey = @"Software\Microsoft\DirectX\UserGpuPreferences" ;
8+
9+ var javaExecutables = new List < string > ( ) ;
10+
11+ var winIdentity = new WindowsPrincipal ( WindowsIdentity . GetCurrent ( ) ) ;
12+
13+ if ( ! winIdentity . IsInRole ( WindowsBuiltInRole . Administrator ) )
14+ {
15+ Console . WriteLine ( ) ;
16+
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)" ) ;
23+
24+ Console . ReadLine ( ) ;
25+
26+ return - 1 ;
27+ }
28+
29+ Console . WriteLine ( ) ;
30+
31+ Console . WriteLine ( "[INFO] Checking for Overwolf/CurseForge registry keys" ) ;
32+ var cfRegKey = Registry . CurrentUser . OpenSubKey ( @"Software\Overwolf\CurseForge\" ) ;
33+ if ( cfRegKey != null )
34+ {
35+ var cfMCRoot = cfRegKey . GetValue ( "minecraft_root" ) as string ;
36+
37+ if ( ! string . IsNullOrWhiteSpace ( cfMCRoot ) )
38+ {
39+ Console . WriteLine ( $ "[INFO] Found it! Modding path is: { cfMCRoot } ") ;
40+ var cfJavaExecutables = GetJavaExecutablesFromPath ( cfMCRoot , "javaw.exe" ) ;
41+ Console . WriteLine ( $ "[INFO] Found { cfJavaExecutables . Count } executables, adding to list") ;
42+ javaExecutables . AddRange ( cfJavaExecutables . Select ( j => j . FullName ) ) ;
43+ }
44+ }
45+ else
46+ {
47+ Console . WriteLine ( "[INFO] Didn't find Overwolf/CurseForge.. anyhow.." ) ;
48+ }
49+
50+ Console . WriteLine ( ) ;
51+
52+ if ( javaExecutables . Count == 0 )
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!" ) ;
56+ }
57+ else
58+ {
59+ Console . WriteLine ( $ "[INFO] Do you want to continue before setting things in the registry? Gonna write { javaExecutables . Count } new keys. [Y/N]") ;
60+ var acceptedKeys = new [ ] { ConsoleKey . Y , ConsoleKey . N } ;
61+ ConsoleKey key ;
62+ do
63+ {
64+ key = Console . ReadKey ( true ) . Key ;
65+ if ( ! acceptedKeys . Contains ( key ) )
66+ {
67+ Console . WriteLine ( $ "[ERROR] That was { key } , not Y or N, try again..") ;
68+ }
69+ } while ( ! acceptedKeys . Contains ( key ) ) ;
70+
71+ if ( key == ConsoleKey . N )
72+ {
73+ Console . WriteLine ( "[INFO] Ok, exiting! Bye!" ) ;
74+ return 0 ;
75+ }
76+
77+ Console . WriteLine ( $ "[INFO] Fixing { javaExecutables . Count } executables") ;
78+
79+ var gpuKey = Registry . CurrentUser . OpenSubKey ( GPURegKey , true ) ;
80+ if ( gpuKey != null )
81+ {
82+ foreach ( var java in javaExecutables )
83+ {
84+ Console . WriteLine ( $ "[INFO] Fixing \" { java } \" for you!") ;
85+ gpuKey . SetValue ( java , "GpuPreference=2;" ) ;
86+ }
87+
88+ gpuKey . Close ( ) ;
89+ }
90+
91+ Console . WriteLine ( "[INFO] All done! Happy playing!" ) ;
92+ }
93+
94+ Console . ReadLine ( ) ;
95+
96+ return 0 ;
97+
98+ ConcurrentStack < FileInfo > GetJavaExecutablesFromPath ( string path , string searchPattern = "javaw.exe" )
99+ {
100+ string [ ] IgnoredFolders = new string [ ] {
101+ "\\ $RECYCLE.BIN" ,
102+ "\\ System Volume Information" ,
103+ "\\ Recovery" ,
104+ "\\ xtp" ,
105+ "\\ Backups"
106+ } ;
107+
108+ var files = new ConcurrentStack < FileInfo > ( ) ;
109+
110+ var rootFolderFiles = new DirectoryInfo ( path ) . GetFiles ( ) ;
111+
112+ foreach ( var f in rootFolderFiles )
113+ {
114+ files . Push ( f ) ;
115+ }
116+
117+ var directories = Directory . GetDirectories ( path ) . Where ( d => ! IgnoredFolders . Any ( i => d . EndsWith ( i ) ) ) . ToList ( ) ;
118+ Parallel . ForEach ( directories , dir =>
119+ {
120+ try
121+ {
122+ var _files = new DirectoryInfo ( dir ) . EnumerateFiles ( searchPattern , SearchOption . AllDirectories ) . Where ( d => ! d . Attributes . HasFlag ( FileAttributes . Directory ) ) . ToArray ( ) ;
123+ if ( _files . Length > 0 )
124+ {
125+ files . PushRange ( _files ) ;
126+ }
127+ }
128+ catch
129+ {
130+ }
131+ } ) ;
132+
133+ return files ;
134+ }
0 commit comments