Skip to content

Instantly share code, notes, and snippets.

@aaronklaassen
Created November 3, 2018 15:14
Show Gist options
  • Save aaronklaassen/94a588e6b3535fa2eb350a2ac29cc365 to your computer and use it in GitHub Desktop.
Save aaronklaassen/94a588e6b3535fa2eb350a2ac29cc365 to your computer and use it in GitHub Desktop.
simplified ahk template
; 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
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")
ExitApp
}
}
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!")
ExitApp
SetTimer,CloseOnIdle, Off
}
else
{
SetTimer,CloseOnIdle, % idleLimit-A_TimeIdle+150
}
return
; Do this stuff when Esc is pressed
~Esc::
ExitApp
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