Skip to content

Instantly share code, notes, and snippets.

@nhaskins
Created November 2, 2018 16:59
Show Gist options
  • Save nhaskins/7b739a25b1b0849b5000fdd39c93f724 to your computer and use it in GitHub Desktop.
Save nhaskins/7b739a25b1b0849b5000fdd39c93f724 to your computer and use it in GitHub Desktop.
example-template.ahk.template.txt
; DEFAULT RUN SCRIPT
#Persistent
#MaxHotkeysPerInterval 200
#SingleInstance Force
debug := {DEBUG_OUTPUT}
executable := "{GAME_PATH}"
SplitPath, executable , , game_dir
exec_file := "{GAME_FILE}"
game_name := "{GAME_NAME}"
forceQuitHoldTime := {ESC_HOLD}000
idleLimit := {IDLE_TIME}000
initialWait := {IDLE_INITIAL}000
launcher_pid := ReadLauncherPid()
launcher_window_id = := ReadWindowLauncherID();
start := SecondsToday()
WriteLog("START ---------------- " . A_Now)
SetTitleMatchMode, 2
; RUN THE GAME
Run, %executable%, , , process_id_1
WriteLog("Launched " . executable . " with pid " . process_id_1)
SetTimer, InitialWait, -%initialWait% ; negative period disables timer after first trigger
MouseMove 3000, 3000, 0
; This is the function that quits the game (hopefully)
KillApp()
{
ExitApp
global process_id_1
global process_id_2
WriteLog("Killing app with pids " . process_id_1 . " and " . process_id_2)
; Nuke the site from orbit. SOMETHING should work....
WinKill, ahk_exe %executable% ; Tries to close using .exe
WinKill, ahk_exe %exec_file%
WinKill, ahk_pid process_id_2 ; Tries to close using process id
WinKill, ahk_pid process_id_1
WinKill, %game_name%
Run, TaskKill /f /pid %process_id_2%
Run, TaskKill /f /pid %process_id_1%
Run, TaskKill /f /im %exec_file%
SetTitleMatchMode, RegEx
IfWinExist, i)WinnitronLauncher
{
WriteLog("Found Winnitron Launcher window via title. ID: " . WinExist("A"))
WinActivate, i)WinnitronLauncher
WinWaitActive, i)WinnitronLauncher, , 1
PostMessage, 0x112, 0xF030,,, i)WinnitronLauncher ; 0x112 = WM_SYSCOMMAND, 0xF030 = SC_MAXIMIZE
if ErrorLevel
WriteLog("Error activating Launcher via title: " . A_LastError)
}
if launcher_pid
{
WriteLog("Attempting to find Winnitron Launcher window via PID (" . launcher_pid . ").")
WinActivate, ahk_pid %launcher_pid%
WinWaitActive, ahk_pid %launcher_pid%, , 1
PostMessage, 0x112, 0xF030,,, ahk_pid %launcher_pid%
if ErrorLevel
WriteLog("Error activating Launcher via PID: " . A_LastError)
}
if launcher_window_id
{
WriteLog("Attempting to find Winnitron Launcher window via ID (" . launcher_window_id . ").")
WinActivate, ahk_id launcher_window_id
WinWaitActive, ahk_id launcher_window_id, , 1
PostMessage, 0x112, 0xF030,,, ahk_id %launcher_window_id%
if ErrorLevel
WriteLog("Error activating Launcher via Window ID: " . A_LastError)
}
ExitApp
}
Sleep, 2000 ;give the game some time to boot
Loop {
IfWinExist, ahk_exe %executable%
WinActivate ; use the window found above
}
Loop
{
; Ensure that the AHK script exits when the game does, because it's *this*
; process that the Launcher is watching so it knows when to wake up and
; kick back to menu.
Process, Exist, %process_id_1%
if (ErrorLevel == 0) {
WriteLog("detected game not running")
KillApp()
}
}
InitialWait:
; Some games launch a second process
WinGet, process_id_2, PID, %game_name%
WriteLog("Completed initial wait (pid2: " . process_id_2 . ")")
SetTimer, CloseOnIdle, % idleLimit+150
return
; This is the timer
CloseOnIdle:
if (A_TimeIdle >= idleLimit)
{
WriteLog("Idle timeout!")
KillApp()
SetTimer,CloseOnIdle, Off
}
else
{
SetTimer,CloseOnIdle, % idleLimit-A_TimeIdle+150
}
return
; Do this stuff when Esc is pressed
~Esc::
KillApp()
If escIsDown
return
escIsDown := true
SetTimer, WaitForESCRelease, %forceQuitHoldTime%
return
; Do this stuff when Esc is UP
~Esc Up::
SetTimer, WaitForESCRelease, Off
escIsDown := false
return
WaitForESCRelease:
SetTimer, WaitForESCRelease, Off
KillApp()
return
ReadLauncherPid() {
EnvGet, homedrive, HOMEDRIVE
EnvGet, homepath, HOMEPATH
pid_file := homedrive . homepath . "\winnitron.pid"
FileReadLine, pid, %pid_file%, 1
WriteLog("Winnitron Launcher PID: " . pid)
return pid
}
ReadWindowLauncherID() {
EnvGet, homedrive, HOMEDRIVE
EnvGet, homepath, HOMEPATH
pid_file := homedrive . homepath . "\winnitron.pid"
FileReadLine, wid, %pid_file%, 2
WriteLog("Winnitron Launcher Window ID: " . wid)
return wid
}
; DEBUGGING STUFF
; Number of seconds since midnight.
SecondsToday() {
return A_Hour * 3600 + A_Min * 60 + A_Sec
}
WriteLog(message)
{
global debug
global start
global game_dir
if (debug) {
runningTimeSec := SecondsToday() - start
debugLog := game_dir . "\ahk_output.txt"
FileAppend,
(
%runningTimeSec%s %A_Tab% %message%
), %debugLog%, UTF-8
}
}
; KEYMAPS BELOW
{KEYMAP}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment