1
+ using System ;
1
2
using MelonLoader ;
2
3
using System . Diagnostics ;
4
+ using System . Threading . Tasks ;
3
5
4
6
namespace GamePriority ;
5
7
public class GamePriorityChanger : MelonMod {
6
8
public static class BuildInfo {
7
9
public const string Name = "GamePriority" ;
8
10
public const string Author = "Lily" ;
9
11
public const string Company = "Minty Labs" ;
10
- public const string Version = "2.0.0 " ;
12
+ public const string Version = "2.0.1 " ;
11
13
public const string DownloadLink = "https://github.com/MintLily/GamePriority" ;
12
14
}
13
15
@@ -17,17 +19,29 @@ public static class BuildInfo {
17
19
private const string ModCategory = "GamePriority" ;
18
20
private const string SetGamePriority = "SetGamePriorityToHigh" ;
19
21
22
+ private int _failedProcessPriorityChange ;
23
+
20
24
public override void OnApplicationStart ( ) {
21
25
_modCategory = MelonPreferences . CreateCategory ( ModCategory , "Game Priority" ) ;
22
26
_isSetToHigh = _modCategory . CreateEntry ( SetGamePriority , false , "Set game priority to high" ) ;
23
27
24
28
ApplyChanges ( ) ;
25
29
}
26
30
27
- public override void OnPreferencesSaved ( ) => ApplyChanges ( ) ;
31
+ private void ApplyChanges ( ) => Task . Run ( async ( ) => await DelayedRun ( ) ) ;
28
32
29
- private void ApplyChanges ( ) {
30
- using var p = Process . GetCurrentProcess ( ) ;
31
- p . PriorityClass = _isSetToHigh . Value ? ProcessPriorityClass . High : ProcessPriorityClass . Normal ;
33
+ private async Task DelayedRun ( ) {
34
+ await Task . Delay ( 5000 ) ;
35
+ try {
36
+ using var p = Process . GetCurrentProcess ( ) ;
37
+ p . PriorityClass = _isSetToHigh . Value ? ProcessPriorityClass . High : ProcessPriorityClass . Normal ;
38
+ }
39
+ catch ( Exception e ) {
40
+ MelonLogger . Error ( $ "Failed to change ProcessPriority, retying ...\n EXCEPTION:\n { e } \n STACK TRACE:\n { e . StackTrace } ") ;
41
+ if ( _failedProcessPriorityChange >= 5 )
42
+ return ;
43
+ ApplyChanges ( ) ;
44
+ _failedProcessPriorityChange += 1 ;
45
+ }
32
46
}
33
47
}
0 commit comments