-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNiceHash Monitor.ahk
347 lines (294 loc) · 7.51 KB
/
NiceHash Monitor.ahk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
; NiceHash Monitor
; by chinagreenelvis
; Version 0.04
#NoEnv
#SingleInstance force
#Persistent
SetWorkingDir %A_ScriptDir%
DetectHiddenWindows, On
SetTitleMatchMode, 1
Global UserProfile
EnvGet, UserProfile, USERPROFILE
Global RunningPrograms := []
Global INIFile := "NiceHashMonitor.ini"
Global QuickMiner := 0
Global MinimizeMiner := 1
Global NiceHashMinerLocation
Global NiceHashQuickMinerLocation
Global HideIcon := 0
Global OverClockCommandsEnabled := 1
Global OverClockOnCommand
Global OverClockOffCommand
Global NiceHashExecutable
Global NiceHashWindow
Global NiceHashLocation
Global NiceHashLocationDir
Global AProcessIsRunning
Global AProcessWasRunning
Global EnableAuto := 1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; SETTINGS
ReadINI()
{
IfNotExist, %INIFile%
{
INIText =
(
[Settings]
[Programs]
[ProgramDirs]
[ExcludedPrograms]
[ExcludedProgramDirs]
[OverClock]
)
FileAppend, %INIText%, %INIFile%
}
SetINI("QuickMiner", INIFile, "Settings", "QuickMiner", QuickMiner)
SetINI("MinimizeMiner", INIFile, "Settings", "MinimizeMiner", MinimizeMiner)
SetINI("NiceHashMinerLocation", INIFile, "Settings", "NiceHashMinerLocation", USERPROFILE "\AppData\Local\Programs\NiceHash Miner\NiceHashMiner.exe")
SetINI("NiceHashQuickMinerLocation", INIFile, "Settings", "NiceHashQuickMinerLocation", "C:\NiceHash\NiceHash QuickMiner\NiceHashQuickMiner.exe")
SetINI("HideIcon", INIFile, "Settings", "HideIcon", HideIcon)
SetINI("OverClockCommandsEnabled", INIFile, "OverClock", "OverClockCommandsEnabled", OverClockCommandsEnabled)
SetINI("OverClockOnCommand", INIFile, "OverClock", "OverClockOnCommand", "C:\Program Files (x86)\MSI Afterburner\MSIAfterburner.exe -Profile2 -q")
SetINI("OverClockOffCommand", INIFile, "OverClock", "OverClockOffCommand", "C:\Program Files (x86)\MSI Afterburner\MSIAfterburner.exe -Profile1 -q")
If (QuickMiner)
{
NiceHashExecutable := "NiceHashQuickMiner.exe"
NiceHashWindow := "NiceHash QuickMiner"
NiceHashLocation := NiceHashQuickMinerLocation
}
Else
{
NiceHashExecutable := "app_nhm.exe"
NiceHashWindow := "NiceHash Miner"
NiceHashLocation := NiceHashMinerLocation
}
SplitPath, NiceHashLocation, , NiceHashLocationDir
}
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; MENU
Menu, Tray, NoStandard
Menu, Tray, NoDefault
Menu, Tray, Add, Hide Icon, MenuHideIcon
Menu, Tray, Add, Open INI, MenuOpenINI
Menu, Tray, Add, Force Start NiceHash, MenuStart
Menu, Tray, Add, Stop NiceHash, MenuStop
Menu, Tray, Add, Set NiceHash to Auto, MenuAuto
Menu, Tray, Add, Quit NiceHash Monitor, MenuQuit
MenuHideIcon()
{
IniWrite, 1, %INIFile%, Settings, HideIcon
ReadINI()
}
MenuOpenINI()
{
Run, %INIFile%
}
MenuStart()
{
AProcessIsRunning := NULL
EnableAuto := 0
MinerStart()
}
MenuStop()
{
AProcessIsRunning := NULL
EnableAuto := 0
MinerStop()
}
MenuAuto()
{
EnableAuto := 1
CheckProcesses()
}
MenuQuit()
{
ExitApp
}
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; TEST
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; START
WMI := ComObjGet("winmgmts:")
ComObjConnect(createSink := ComObjCreate("WbemScripting.SWbemSink"), EventSinkCreate)
ComObjConnect(deleteSink := ComObjCreate("WbemScripting.SWbemSink"), EventSinkDelete)
WMI.ExecNotificationQueryAsync(createSink, "select * from __InstanceCreationEvent Within 1 Where TargetInstance ISA 'Win32_Process'")
WMI.ExecNotificationQueryAsync(deleteSink, "select * from __InstanceDeletionEvent Within 1 Where TargetInstance ISA 'Win32_Process'")
ReadINI()
CheckProcesses()
Return
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; STANDARD FUNCTIONS
ProcessExist(Name)
{
Process, Exist, %Name%
return Errorlevel
}
SetINI(OutPutVar, FileName, Section, Key, DefaultSetting := 0)
{
IniRead, %OutPutVar%, %FileName%, %Section%, %Key%
If %OutPutVar% = ERROR
{
;MsgBox, That key does not exist in %FileName%, %Section%, %Key%
IniWrite, %DefaultSetting%, %FileName%, %Section%, %Key%
IniRead, %OutPutVar%, %FileName%, %Section%, %Key%
}
}
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; CUSTOM FUNCTIONS
;; PROCESS MONITOR
;; help provided by teadrinker https://www.autohotkey.com/boards/viewtopic.php?f=76&t=96818&p=430272#p430272
class EventSinkCreate
{
OnObjectReady(obj)
{
If (EnableAuto)
{
CheckProcesses()
}
}
}
class EventSinkDelete
{
OnObjectReady(obj)
{
If (EnableAuto)
{
CheckProcesses()
}
}
}
CheckProcesses()
{
AProcessWasRunning := AProcessIsRunning
AProcessIsRunning := 0
For Process in ComObjGet("winmgmts:").ExecQuery("Select * from Win32_Process")
{
If (ProcessStopsMining(Process))
{
AProcessIsRunning := 1
}
}
If (EnableAuto) && (AProcessWasRunning != AProcessIsRunning)
{
If (!AProcessIsRunning)
{
MinerStart()
}
Else
{
MinerStop()
}
}
}
ProcessStopsMining(Process)
{
ReadINI()
ProcessName := Process.Name
;MsgBox, % ProcessName
ProcessImageName := GetProcessImageName(Process.ProcessID)
If (ProcessImageName)
{
if (IsExcludedProcess(ProcessName, ProcessImageName))
{
return False
}
;MsgBox, % ProcessImageName
IniRead, Programs, %INIFile%, Programs
Loop, Parse, Programs, `n
{
;MsgBox, %A_LoopField%
If ProcessName contains %A_LoopField%
{
Return True
}
}
IniRead, ProgramDirs, %INIFile%, ProgramDirs
Loop, Parse, ProgramDirs, `n
{
;MsgBox, %A_LoopField%
If ProcessImageName contains %A_LoopField%
{
Return True
}
}
}
}
IsExcludedProcess(ProcessName, ProcessImageName)
{
;MsgBox, % ProcessImageName
IniRead, ExcludedPrograms, %INIFile%, ExcludedPrograms
Loop, Parse, ExcludedPrograms, `n
{
;MsgBox, %A_LoopField%
If ProcessName contains %A_LoopField%
{
Return True
}
}
IniRead, ExcludedProgramDirs, %INIFile%, ExcludedProgramDirs
Loop, Parse, ExcludedProgramDirs, `n
{
;MsgBox, %A_LoopField%
If ProcessImageName contains %A_LoopField%
{
Return True
}
}
}
GetProcessImageName(PID)
{
Static access := PROCESS_QUERY_INFORMATION := 0x400
If !hProc := DllCall("OpenProcess", "UInt", access, "Int", 0, "UInt", PID, "Ptr")
{
Return
}
VarSetCapacity(ImagePath, 1024, 0)
DllCall("QueryFullProcessImageName", "Ptr", hProc, "UInt", 0, "Str", ImagePath, "UIntP", 512)
DllCall("CloseHandle", "Ptr", hProc)
Return ImagePath
}
;; MINER CONTROL
MinerStart()
{
ReadINI()
If (!ProcessExist(NiceHashExecutable))
{
;MsgBox, Starting NiceHash Miner
Run, %NiceHashLocation%, %NiceHashLocationDir%
If (!QuickMiner && MinimizeMiner)
{
WaitForWindow:
Loop
{
WinGet, WindowList, List, %NiceHashWindow% ahk_exe %NiceHashExecutable%
Loop, %WindowList%
{
Window := % "ahk_id" . WindowList%A_Index%
;MsgBox, %Window%
WinGet, WindowStyle, Style, %Window%
If (WindowStyle & 0x20000)
{
Sleep, 1000
WinMinimize, %Window%
Break WaitForWindow
}
}
}
}
}
If (OverClockCommandsEnabled)
{
Run, %OverClockOnCommand%
}
}
MinerStop()
{
ReadINI()
If (ProcessExist(NiceHashExecutable))
{
;MsgBox, Closing NiceHash Miner
WinClose, %NiceHashWindow% ahk_exe %NiceHashExecutable%
WinWaitClose, %NiceHashWindow% ahk_exe %NiceHashExecutable%
}
If (OverClockCommandsEnabled)
{
Run, %OverClockOffCommand%
}
}
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; TIMERS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; HOTKEYS