-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScriptManager.ahk
More file actions
145 lines (132 loc) · 5.65 KB
/
ScriptManager.ahk
File metadata and controls
145 lines (132 loc) · 5.65 KB
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
#Requires AutoHotkey v2.0.18
#SingleInstance Force
Persistent
#Include utils\Logger.ahk
#Include utils\DotEnv.ahk
; Add script to Windows Registry startup using short paths to handle spaces
try {
if !RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run", "ScriptManager") {
; Convert paths to short format (8.3)
shortAhkPath := FileExist(A_AhkPath) ? GetShortPathName(A_AhkPath) : A_AhkPath
shortScriptPath := FileExist(A_ScriptFullPath) ? GetShortPathName(A_ScriptFullPath) : A_ScriptFullPath
; Build command with short paths
startupCommand := Format('"{1}" "{2}"', shortAhkPath, shortScriptPath)
; Write to registry
RegWrite(startupCommand, "REG_SZ", "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run", "ScriptManager")
Logger.Log("ScriptManager", "Successfully added to startup")
}
} catch as err {
Logger.Log("ScriptManager", "Registry error: " err.Message)
throw Error("Registry error: " err.Message)
}
GetShortPathName(longPath) {
buf := Buffer(260 * 2, 0) ; 260 is MAX_PATH
if DllCall("GetShortPathNameW", "Str", longPath, "Ptr", buf, "UInt", 260)
return StrGet(buf)
return longPath
}
DotEnv.Load()
class ScriptManager {
; Define the configuration for the script manager
static CONFIG := {
CheckInterval: 5000,
Scripts: [
{
ScriptPath: A_ScriptDir "\scripts\MatchAcceptor.ahk",
WindowTitle: "ahk_class RCLIENT ahk_exe LeagueClientUx.exe",
ScriptType: "Regular",
},
{
ScriptPath: A_ScriptDir "\scripts\LoadingScreenTimer.ahk",
WindowTitle: "ahk_class RiotWindowClass ahk_exe League of Legends.exe",
ScriptType: "One-Time",
},
{
ScriptPath: A_ScriptDir "\scripts\VDEExiter.ahk",
WindowTitle: DotEnv.Get("VDE_WINDOW_TITLE"),
ScriptType: "Regular",
}
],
}
__New() {
; Initialize the script PIDs map and set the check timer
this.scriptPids := Map()
this.checkTimerFn := this._CheckScripts.Bind(this)
OnExit(this._OnExit.Bind(this))
}
Init() {
Logger.Log("ScriptManager", "Started")
SetTimer(this.checkTimerFn, ScriptManager.CONFIG.CheckInterval)
}
_CheckScripts() {
for script in ScriptManager.CONFIG.Scripts {
scriptName := StrSplit(script.ScriptPath, "\").Pop()
if WinExist(script.WindowTitle) {
; Check if the script should be started
if !this.scriptPids.Has(scriptName) {
Logger.Log("ScriptManager", "Starting " scriptName " [" script.ScriptType "]")
pid := this._LaunchScript(script)
if pid {
this.scriptPids[scriptName] := pid
}
; Check if the script is still running
} else if !ProcessExist(this.scriptPids[scriptName]) {
if script.ScriptType == "One-Time" {
; For one-time scripts; log that they were closed and update the type
Logger.Log("ScriptManager", "Closing " scriptName " [" script.ScriptType "]")
script.ScriptType := "One-Time-Closed"
} else if script.ScriptType == "Regular" {
; For regular scripts; delete the old PID and start them again
this.scriptPids.Delete(scriptName)
Logger.Log("ScriptManager", "Starting " scriptName " [" script.ScriptType "]")
pid := this._LaunchScript(script)
if pid {
this.scriptPids[scriptName] := pid
}
}
}
; WindowTitle no longer exists
} else if this.scriptPids.Has(scriptName) {
; For regular scripts; log that they were closed
if script.ScriptType == "Regular"
Logger.Log("ScriptManager", "Closing " scriptName " [" script.ScriptType "]")
; For one-time scripts; reset the type
else if script.ScriptType == "One-Time-Closed"
script.ScriptType := "One-Time"
; For all scripts; close the process and remove it from the map
ProcessClose(this.scriptPids[scriptName])
this.scriptPids.Delete(scriptName)
}
}
}
_LaunchScript(script) {
; Launch the script and return its PID
try {
Run('"' A_AhkPath '" "' script.ScriptPath '"',, "Hide", &pid)
return pid
}
Logger.Log("ScriptManager", "Failed to launch script: " script.ScriptPath)
return 0
}
_OnExit(reason, code) {
; Close all running scripts
for script in ScriptManager.CONFIG.Scripts {
scriptName := StrSplit(script.ScriptPath, "\").Pop()
if this.scriptPids.Has(scriptName) {
Logger.Log(RegExReplace(scriptName, "\.ahk$"), "Stopped")
Logger.Log("ScriptManager", "Closing " scriptName " [" script.ScriptType "]")
ProcessClose(this.scriptPids[scriptName])
this.scriptPids.Delete(scriptName)
}
}
Logger.Log("ScriptManager", "Stopped")
}
}
; Initialize and run
try {
manager := ScriptManager()
manager.Init()
} catch as err {
Logger.Log("ScriptManager", "Critical error: " err.Message)
throw Error("Failed to initialize ScriptManager: " err.Message)
}