Skip to content

Instantly share code, notes, and snippets.

@leonelsr
Last active June 2, 2020 21:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leonelsr/25e06a88cd8fc4efc6f0a9a9568ef27e to your computer and use it in GitHub Desktop.
Save leonelsr/25e06a88cd8fc4efc6f0a9a9568ef27e to your computer and use it in GitHub Desktop.
AutoHotkey XMRig controller tool - Dynamically changes XMRig settings when PC is idle for some time
#SingleInstance force ; Skips the dialog box and replaces the old instance automatically
#Persistent
SetWorkingDir %A_ScriptDir%
/*
* XMRig controller tool
*
* Can be used to dynamically change XMRig settings whenever PC is idle for some time
* At the bottom of this file, there is an example of config.json file for reference
*
* (c) June 2020, @Leonelsr (github.com/leonelsr)
* This code is licensed under MIT license
*/
RunWait, where.exe pwsh.exe , , Hide UseErrorLevel
If (errorlevel = 0) {
; Found it
pwshexe := "pwsh.exe"
savecmd =
(
$cfg | ConvertTo-Json -Depth 100 | Out-File -Encoding utf8NoBOM .\config_background.json
)
} Else {
; Didn't find it
pwshexe := "powershell.exe"
savecmd =
(
$cfg | ConvertTo-Json -Depth 100 | Out-File -Encoding ascii .\config_background.json
)
}
psSet4threads =
(
$cfg = Get-Content .\config_background.json | ConvertFrom-Json
foreach ($item in $cfg.cpu.PsObject.Properties) {
if ($item.Value.threads -ne $null) { $item.Value.threads = 4 }
}
)
psSet4threads .= savecmd
psSet1thread =
(
$cfg = Get-Content .\config_background.json | ConvertFrom-Json
foreach ($item in $cfg.cpu.PsObject.Properties) {
if ($item.Value.threads -ne $null) { $item.Value.threads = 1 }
}
)
psSet1thread .= savecmd
; $cfg | ConvertTo-Json -Depth 100 > .\config_background.json
psSetCUDAon =
(
$cfg = Get-Content .\config_background.json | ConvertFrom-Json
$cfg.cuda.enabled = $True
)
psSetCUDAon .= savecmd
psSetCUDAoff =
(
$cfg = Get-Content .\config_background.json | ConvertFrom-Json
$cfg.cuda.enabled = $False
)
psSetCUDAoff .= savecmd
psIsCUDAon =
(
If ((Get-Content .\config_background.json | ConvertFrom-Json).cuda.enabled) { exit 1 } else { exit 2 }
)
psIsOCLon =
(
If ((Get-Content .\config_background.json | ConvertFrom-Json).opencl.enabled) { exit 1 } else { exit 2 }
)
psSetOCLon =
(
$cfg = Get-Content .\config_background.json | ConvertFrom-Json
$cfg.opencl.enabled = $True
)
psSetOCLon .= savecmd
psSetOCLoff =
(
$cfg = Get-Content .\config_background.json | ConvertFrom-Json
$cfg.opencl.enabled = $False
)
psSetOCLoff .= savecmd
If Not A_IsAdmin {
MsgBox, 4,, Run as admin? (press Yes or No),4
IfMsgBox Yes
{
Loop, %0%
Parameters .= %A_Index% A_Space
Try Run, *RunAs "%A_AhkPath%" "%A_ScriptFullPath%" %Parameters%
ExitApp
}
; else
; MsgBox,,, You pressed No.,5
}
nvidiaIsOn := false
openclIsOn := false
/*
https://www.autohotkey.com/boards/viewtopic.php?t=69710
*/
; Menu, Tray, Icon, pifmgr.dll, 6
; Menu, Tray, Icon, Shell32.dll, 321
Menu, Tray, Icon, xmrig.exe
Menu, Tray, Tip, XMRig Idle control
Menu, Tray, NoStandard
Menu, Tray, Add, &Pause, menuPause
Menu, Tray, Add, &NVIDIA, toggleNvidia
Menu, Tray, Add, &OpenCL, toggleOpencl
Menu, Tray, Add
Menu, Tray, Add, &1 thread, set1thread
Menu, Tray, Add, &4 threads, set4threads
Menu, Tray, Add
Menu, Tray, Add, Run XMRi&g if not already, check_xmrig_running
Menu, Tray, Add, View &Stats, viewStats
Menu, Tray, Add, &Reload, menuReload
Menu, Tray, Add, E&xit, menuExit
;Menu, Tray, Standard
; ...
Menu, Tray, Default, &Pause
; Every 5s
SetTimer, check_idle, 5000
; Every 5min
SetTimer, check_xmrig_running, 300000
; RunWait, %COMSPEC% /v:on /c "Contig.exe -v C:\$Mft & exit !ErrorLevel!" >> %logFilePath%, , Hide UseErrorLevel
; Contig_exitcode := errorlevel
RunWait, %pwshexe% -Command %psIsCUDAon% , , Hide UseErrorLevel
If (errorlevel = 1) {
nvidiaIsOn := true
Menu, Tray, Check, &NVIDIA
}
RunWait, %pwshexe% -Command %psIsOCLon% , , Hide UseErrorLevel
If (errorlevel = 1) {
openclIsOn := true
Menu, Tray, Check, &OpenCL
}
;MsgBox, errlvl is %errorlevel%
Gosub, set1thread
Gosub, check_xmrig_running
return
; ------------------------------------------------
check_idle:
; If A_TimeIdle > 6000
If A_TimeIdle > 300000
{
if not Screen_Saver_On
{
Screen_Saver_On := true
Gosub, set4threads
}
}
else
{
if Screen_Saver_On
{
Screen_Saver_On := false
Gosub, set1thread
}
}
return
check_xmrig_running:
Process, Exist, xmrig.exe ; check to see if xmrig is running
If (ErrorLevel = 0) ; If it is not running
{
Run, xmrig.exe -c config_background.json,,Min
}
return
; ---------------------------------------------------
; Menus
;
menuReload:
Reload
return
menuExit:
ExitApp
return
menuPause:
If (A_IsPaused)
{
Pause, Off
Menu, Tray, Uncheck, &Pause
Menu, Tray, Icon, xmrig.exe
}
Else
{
Menu, Tray, Check, &Pause
Menu, Tray, Icon, imageres.dll, 55,1
Pause, On
}
return
toggleNvidia:
If nvidiaIsOn
{
Run, %pwshexe% -Command %psSetCUDAoff% , , Hide
nvidiaIsOn := false
Menu, Tray, Uncheck, &NVIDIA
;Menu, Tray, Icon,RunningIcon.ico
}
Else
{
Run, %pwshexe% -Command %psSetCUDAon% , , Hide
nvidiaIsOn := true
Menu, Tray, Check, &NVIDIA
;Menu, Tray, Icon,PausedIcon.ico,,1
}
return
toggleOpencl:
If openclIsOn
{
Run, %pwshexe% -Command %psSetOCLoff% , , Hide
openclIsOn := false
Menu, Tray, Uncheck, &OpenCL
;Menu, Tray, Icon,RunningIcon.ico
}
Else
{
Run, %pwshexe% -Command %psSetOCLon% , , Hide
openclIsOn := true
Menu, Tray, Check, &OpenCL
;Menu, Tray, Icon,PausedIcon.ico,,1
}
return
viewStats:
url := "http://workers.xmrig.info/worker?url=http%3A%2F%2F192.168.0.111%3A8808"
Run, %url%
return
set4threads:
TrayTip, xmrig, Using 4 threads, 10, 17
SetTimer, HideTrayTip, -10000
Menu, Tray, Icon, Shell32.dll, 321
Menu, Tray, Check, &4 threads
Menu, Tray, Uncheck, &1 thread
;MsgBox, , Title, set 4 threads, 5
Run, %pwshexe% -Command %psSet4threads% , , Hide
return
set1thread:
TrayTip, xmrig, Using 1 thread, 10, 17
SetTimer, HideTrayTip, -10000
Menu, Tray, Icon, xmrig.exe
Menu, Tray, Check, &1 thread
Menu, Tray, Uncheck, &4 threads
;MsgBox, , Title, set 1 thread, 5
Run, %pwshexe% -Command %psSet1thread% , , Hide
return
HideTrayTip() {
TrayTip ; Attempt to hide it the normal way.
if SubStr(A_OSVersion,1,3) = "10." {
Menu Tray, NoIcon
Sleep 200 ; It may be necessary to adjust this sleep.
Menu Tray, Icon
}
}
; **************
; config_background.json should look like this
;
; "autosave" must be set true
; "background" can be true or false
;
; Inside "cpu", each algorithm should be set in the format:
; { "intensity": 1, "threads": 1, "affinity": -1 }
;
; and "affinity" must be set -1.
;
; (HTTP config just as an example for reference)
;
; **************
/*
{
"http": {
"enabled": true,
"host": "192.168.0.111",
"port": 8808,
"access-token": "...",
"restricted": false
},
"autosave": true,
"background": false,
...
"cpu": {
"enabled": true,
...
"cn": {
"intensity": 1,
"threads": 1,
"affinity": -1
},
"cn-heavy": {
"intensity": 1,
"threads": 1,
"affinity": -1
},
"cn-lite": {
"intensity": 1,
"threads": 1,
"affinity": -1
},
"rx": {
"intensity": 1,
"threads": 1,
"affinity": -1
},
...
},
"opencl": {
"enabled": false,
...
}
"cuda": {
"enabled": true,
...
}
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment