-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Utils.ahk
701 lines (519 loc) · 17.7 KB
/
Utils.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Modular Simulator Controller System - Utility Functions ;;;
;;; ;;;
;;; Author: Oliver Juwig (TheBigO) ;;;
;;; License: (2024) Creative Commons - BY-NC-SA ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;-------------------------------------------------------------------------;;;
;;; Global Include Section ;;;
;;;-------------------------------------------------------------------------;;;
#Include "Constants.ahk"
#Include "Variables.ahk"
#Include "Configuration.ahk"
#Include "Debug.ahk"
#Include "MultiMap.ahk"
#Include "Files.ahk"
#Include "Startup.ahk"
;;;-------------------------------------------------------------------------;;;
;;; Local Include Section ;;;
;;;-------------------------------------------------------------------------;;;
#Include "..\Libraries\CLR.ahk"
#Include "..\Libraries\Messages.ahk"
#Include "..\Libraries\Task.ahk"
;;;-------------------------------------------------------------------------;;;
;;; Private Classes Declaration Section ;;;
;;;-------------------------------------------------------------------------;;;
class TriggerDetectorTask extends Task {
iCallback := false
iOptions := []
iJoysticks := []
Options {
Get {
return this.iOptions
}
}
CallBack {
Get {
return this.iCallback
}
}
Stopped {
Get {
return super.Stopped
}
Set {
if value
ToolTip(, , 1)
return (super.Stopped := value)
}
}
Joysticks[key?] {
Get {
return (isSet(key) ? this.iJoysticks[key] : this.iJoysticks)
}
}
__New(callback, options, arguments*) {
this.iOptions := options
this.iCallback := callback
super.__New(false, arguments*)
}
run() {
local joysticks := []
local joyName
loop 16 { ; Query each joystick number to find out which ones exist.
joyName := (GetKeyState(A_Index . "JoyName") ? "D" : "")
if (joyName != "")
joysticks.Push(A_Index)
}
this.iJoysticks := joysticks
return TriggerDetectorContinuation(Task.CurrentTask)
}
}
class TriggerDetectorContinuation extends Continuation {
__New(task, arguments*) {
super.__New(task, false, arguments*)
}
run() {
local key := false
local found, joysticks, joystickNumber, joy_buttons, joy_name, joy_state, buttons_down, joy_info
local axis_info, buttonsDown, callback
if !this.Task.Stopped {
found := false
if GetKeyState("Esc") {
this.stop()
return false
}
key := (inList(this.Task.Options, "Key") ? this.detectKey(inList(this.Task.Options, "Multi")) : false)
if key {
if InStr(key, "Esc") {
this.stop()
return false
}
found := true
ToolTip(key, , , 1)
}
else if inList(this.Task.Options, "Joy") {
joysticks := this.Task.Joysticks
loop joysticks.Length {
joystickNumber := joysticks[1]
joysticks.RemoveAt(1)
joysticks.Push(joystickNumber)
; SetFormat Float, 03 ; Omit decimal point from axis position percentages.
joy_buttons := GetKeyState(joystickNumber . "JoyButtons")
joy_name := GetKeyState(joystickNumber . "JoyName")
joy_info := GetKeyState(joystickNumber . "JoyInfo")
buttons_down := ""
buttons := []
loop joy_buttons {
if GetKeyState(joystickNumber . "joy" . A_Index) {
buttons_down := (buttons_down . A_Space . A_Index)
found := A_Index
}
}
axis_info := ("X" . (GetKeyState(joystickNumber . "JoyX") ? "D" : "U"))
axis_info := (axis_info . A_Space . A_Space . "Y" . (GetKeyState(joystickNumber . "JoyY") ? "D" : "U"))
if InStr(joy_info, "Z")
axis_info := (axis_info . A_Space . A_Space . "Z" . (GetKeyState(joystickNumber . "JoyZ") ? "D" : "U"))
if InStr(joy_info, "R")
axis_info := (axis_info . A_Space . A_Space . "R" . (GetKeyState(joystickNumber . "JoyR") ? "D" : "U"))
if InStr(joy_info, "U")
axis_info := (axis_info . A_Space . A_Space . "U" . (GetKeyState(joystickNumber . "JoyU") ? "D" : "U"))
if InStr(joy_info, "V")
axis_info := (axis_info . "" . A_Space . "" . A_Space . "V" . (GetKeyState(joystickNumber "JoyV", ) ? "D" : "U"))
if InStr(joy_info, "P")
axis_info := (axis_info . A_Space . A_Space . "POV" . (GetKeyState(joystickNumber "JoyPOV") ? "D" : "U"))
buttonsDown := translate("Buttons Down:")
}
until found
if found
ToolTip(joy_name . " (#" joystickNumber "):`n" . axis_info . "`n" . buttonsDown . A_Space . buttons_down, , , 1)
}
if found {
if !key
key := (joystickNumber . "Joy" . found)
A_Clipboard := key
if this.Task.Callback {
this.Task.Callback.Call(key)
this.stop()
return false
}
else
return TriggerDetectorContinuation(this.Task, 2000)
}
else
ToolTip(translate("Waiting..."), , , 1)
return TriggerDetectorContinuation(this.Task, 0)
}
else {
this.stop()
return false
}
}
detectKey(multi) {
local input := InputHook("T0.1")
local key, expired
static lastTicks := false
static lastKeys := []
expired := (lastTicks ? ((A_TickCount - lastTicks) > 500) : false)
if !multi
lastKeys := []
input.KeyOpt("{All}", "IE")
input.VisibleText := false
input.VisibleNonText := false
input.Start()
input.Wait()
key := input.EndKey
input.Stop()
if (key && (key != ""))
if multi {
if !expired {
if (lastKeys.Length = 0)
lastTicks := A_TickCount
if !inList(lastKeys, key)
lastKeys.Push(key)
}
}
else
return key
if (multi && expired) {
lastTicks := false
if (lastKeys.Length > 0) {
key := this.createHotkey(lastKeys)
lastKeys := []
return key
}
else
return false
}
return false
}
createHotkey(keys) {
local baseKeys := []
local baseKey := kUndefined
local ignore, key, modifiers
loop 26
baseKeys.Push(Chr(Ord("a") + A_Index - 1))
loop 10 {
baseKeys.Push(String(A_Index - 1))
baseKeys.Push("Numpad" . (A_Index - 1))
}
loop 24
baseKeys.Push("F" . A_Index)
for ignore, key in ["Space", "BackSpace", "Tab", "Enter", "Up", "Down", "Left", "Right"
, "Home", "End", "Delete", "Insert", "PgUp", "PgDn"]
baseKeys.Push(key)
for ignore, key in keys.Clone()
if inList(baseKeys, key) {
if (baseKey == kUndefined)
baseKey := key
keys := remove(keys, key)
}
if (baseKey != kUndefined) {
modifiers := ""
for ignore, key in keys
switch key, false {
case "LShift":
modifiers .= "<+"
case "RShift":
modifiers .= ">+"
case "LControl":
modifiers .= "<^"
case "RControl":
modifiers .= ">^"
case "LAlt":
modifiers .= "<!"
case "RAlt":
modifiers .= ">!"
case "AltGr":
modifiers .= "<^>!"
case "Win":
modifiers .= "#"
}
return (modifiers . baseKey)
}
else
return values2String(" & ", keys*)
}
}
;;;-------------------------------------------------------------------------;;;
;;; Private Function Declaration Section ;;;
;;;-------------------------------------------------------------------------;;;
doApplications(applications, callback) {
local ignore, application, pid
for ignore, application in applications {
pid := ProcessExist(InStr(application, ".exe") ? application : (application . ".exe"))
if pid
callback.Call(pid)
}
}
;;;-------------------------------------------------------------------------;;;
;;; Public Function Declaration Section ;;;
;;;-------------------------------------------------------------------------;;;
getControllerState(configuration?, force := false) {
local load := true
local pid, tries, options, exePath, fileName
if kLogStartup
logMessage(kLogOff, "Requesting controller configuration - Start...")
try {
if !isSet(configuration)
configuration := false
else if (configuration == false)
load := false
else if (configuration == true)
configuration := false
pid := ProcessExist("Simulator Controller.exe")
if force
deleteFile(kTempDirectory . "Simulator Controller.state")
if (isSet(isProperInstallation) && isProperInstallation() && load && FileExist(kUserConfigDirectory . "Simulator Controller.install"))
if (!pid && (configuration || !FileExist(kTempDirectory . "Simulator Controller.state"))) {
try {
if configuration {
fileName := temporaryFileName("Config", "ini")
writeMultiMap(fileName, configuration)
options := (" -Configuration `"" . fileName . "`"")
}
else
options := ""
exePath := ("`"" . kBinariesDirectory . "Simulator Controller.exe`" -NoStartup -NoUpdate" . options)
RunWait(exePath, kBinariesDirectory)
}
catch Any as exception {
logMessage(kLogCritical, translate("Cannot start Simulator Controller (") . exePath . translate(") - please rebuild the applications in the binaries folder (") . kBinariesDirectory . translate(")"))
return newMultiMap()
}
}
else if (!FileExist(kTempDirectory . "Simulator Controller.state") && pid && (StrSplit(A_ScriptName, ".")[1] != "Simulator Controller")) {
if (pid != ProcessExist())
messageSend(kFileMessage, "Controller", "writeControllerState", pid)
Sleep(1000)
tries := 30
while (tries-- > 0) {
if FileExist(kTempDirectory . "Simulator Controller.state")
break
Sleep(200)
}
}
return readMultiMap(kTempDirectory . "Simulator Controller.state")
}
finally {
if kLogStartup
logMessage(kLogOff, "Requesting controller configuration - Done...")
}
}
createGUID() {
local guid, pGuid, sGuid, size
pGuid := Buffer(16, 0)
if !DllCall("ole32.dll\CoCreateGuid", "ptr", pGuid) {
sGuid := Buffer((38 + 1) * 2, 0)
if (DllCall("ole32.dll\StringFromGUID2", "ptr", pGuid, "ptr", sGuid, "int", sGuid.Size)) {
guid := StrGet(sGuid)
return SubStr(SubStr(guid, 1, StrLen(guid) - 1), 2)
}
}
return ""
}
callSimulator(simulator, options := "", protocol?) {
local exePath, dataFile, data
local connector, curWorkingDir, buf
local dllName, dllFile
static defaultProtocol := getMultiMapValue(readMultiMap(getFileName("Core Settings.ini", kUserConfigDirectory, kConfigDirectory)), "Simulator", "Data Provider", "DLL")
static protocols := CaseInsenseMap("AC", "CLR", "ACC", "DLL", "R3E", "DLL", "IRC", "DLL"
, "AMS2", "DLL", "PCARS2", "DLL", "RF2", "CLR", "LMU", "CLR")
static connectors := CaseInsenseMap()
if (defaultProtocol = "EXE")
protocol := "EXE"
else if (!isSet(protocol) && protocols.Has(simulator))
protocol := protocols[simulator]
try {
if (protocol = "DLL") {
if connectors.Has(simulator . ".DLL")
connector := connectors[simulator . ".DLL"]
else {
curWorkingDir := A_WorkingDir
SetWorkingDir(kBinariesDirectory . "Connectors\")
try {
connector := DllCall("LoadLibrary", "Str", simulator . " SHM Connector.dll", "Ptr")
DLLCall(simulator . " SHM Connector\open")
connectors[simulator . ".DLL"] := connector
}
finally {
SetWorkingDir(curWorkingDir)
}
}
buf := Buffer(1024 * 1024)
DllCall(simulator . " SHM Connector\call", "AStr", options, "Ptr", buf, "Int", buf.Size)
data := parseMultiMap(StrGet(buf, "UTF-8"))
}
else if (protocol = "CLR") {
if connectors.Has(simulator . ".CLR")
connector := connectors[simulator . ".CLR"]
else {
dllName := (simulator . " SHM Connector.dll")
dllFile := (kBinariesDirectory . "Connectors\" . dllName)
if (!FileExist(dllFile))
throw "Unable to find " . dllName . " in " . kBinariesDirectory . "..."
connector := CLR_LoadLibrary(dllFile).CreateInstance("SHMConnector.SHMConnector")
if (!connector.Open() && !isDebug())
throw "Cannot startup " . dllName . " in " . kBinariesDirectory . "..."
connectors[simulator . ".CLR"] := connector
}
data := parseMultiMap(connector.Call(options))
}
else if (protocol = "EXE") {
exePath := (kBinariesDirectory . "Providers\" . simulator . " SHM Provider.exe")
if !FileExist(exePath)
throw "File not found..."
DirCreate(kTempDirectory . simulator . " Data")
dataFile := temporaryFileName(simulator . " Data\SHM", "data")
RunWait(A_ComSpec . " /c `"`"" . exePath . "`" `"" . options . "`" > `"" . dataFile . "`"`"", , "Hide")
data := readMultiMap(dataFile)
deleteFile(dataFile)
}
setMultiMapValue(data, "Session Data", "Simulator", simulator)
return data
}
catch Any as exception {
if (protocol = "EXE") {
logError(exception, true)
logMessage(kLogCritical, substituteVariables(translate("Cannot start %simulator% %protocol% Provider (")
, {simulator: simulator, protocol: protocol})
. exePath . translate(") - please rebuild the applications in the binaries folder (")
. kBinariesDirectory . translate(")"))
showMessage(substituteVariables(translate("Cannot start %simulator% %protocol% Provider (%exePath%) - please check the configuration...")
, {exePath: exePath, simulator: simulator, protocol: "SHM"})
, translate("Modular Simulator Controller System"), "Alert.png", 5000, "Center", "Bottom", 800)
return newMultiMap()
}
else {
logError(exception)
return callSimulator(simulator, options, "EXE")
}
}
}
broadcastMessage(applications, message, arguments*) {
if (arguments.Length > 0)
doApplications(applications, messageSend.Bind(kFileMessage, "Core", message . ":" . values2String(";", arguments*)))
else
doApplications(applications, messageSend.Bind(kFileMessage, "Core", message))
}
exitProcess(urgent := false) {
global kGuardExit
if urgent
kGuardExit := false
ExitApp(0)
}
exitProcesses(title, message, silent := false, force := false, excludes := [], urgent := false) {
local foregroundApps := kForegroundApps
local backgroundApps := kBackgroundApps
local pid, hasFGProcesses, hasBGProcesses, ignore, app, translator, msgResult, processes
computeTargets(targets) {
local ignore, exclude
for ignore, exclude in excludes
targets := remove(targets, exclude)
return targets
}
pid := ProcessExist()
for ignore, app in excludes {
foregroundApps := remove(foregroundApps, app)
backgroundApps := remove(backgroundApps, app)
}
while true {
hasFGProcesses := false
hasBGProcesses := false
for ignore, app in foregroundApps
if ProcessExist(app . ".exe") {
hasFGProcesses := true
break
}
for ignore, app in backgroundApps
if ProcessExist(app ".exe") {
hasBGProcesses := true
break
}
if (hasFGProcesses && !silent) {
translator := translateMsgBoxButtons.Bind(["Continue", "Cancel"])
OnMessage(0x44, translator)
msgResult := withBlockedWindows(MsgBox, translate(message), translate(title), 8500)
OnMessage(0x44, translator, 0)
if (msgResult = "Yes") {
if (GetKeyState("Ctrl") && (force = "CANCEL"))
return true
else if !force
continue
}
else
return false
}
if hasFGProcesses
if force {
if (urgent = "Kill")
doApplications(computeTargets(foregroundApps), ProcessClose)
else
broadcastMessage(computeTargets(foregroundApps), "exitProcess", urgent)
}
else
return false
if hasBGProcesses
if (urgent = "Kill")
doApplications(computeTargets(foregroundApps), ProcessClose)
else
broadcastMessage(computeTargets(backgroundApps), "exitProcess", urgent)
return true
}
}
triggerDetector(callback := false, options := ["Joy", "Key"]) {
static detectorTask := false
if (callback = "Active")
return (detectorTask && !detectorTask.Stopped)
else {
if (detectorTask && detectorTask.Stopped)
detectorTask := false
if detectorTask {
detectorTask.stop()
detectorTask := false
}
else if (callback != "Stop") {
detectorTask := TriggerDetectorTask(callback, options, 100)
detectorTask.start()
}
}
}
testAssistants(configurator, assistants := kRaceAssistants, booster := false) {
local configuration := configurator.getSimulatorConfiguration()
local configurationFile := temporaryFileName("Simulator Configuration", "ini")
local thePlugin, ignore, assistant, options, parameter, value
deleteConfiguration(*) {
deleteFile(configurationFile)
return false
}
writeMultiMap(configurationFile, configuration)
if !isDebug()
OnExit(deleteConfiguration)
Run(kBinariesDirectory . "Voice Server.exe -Debug true -Configuration `"" . configurationFile . "`"")
Sleep(2000)
language := getMultiMapValue(configuration, "Voice Control", "Language", getLanguage())
for ignore, assistant in assistants {
thePlugin := Plugin(assistant, configuration)
options := ""
for ignore, parameter in ["Name", "Language", "Synthesizer", "Speaker", "SpeakerVocalics", "Recognizer", "Listener"]
if thePlugin.hasArgument("raceAssistant" . parameter) {
value := thePlugin.getArgumentValue("raceAssistant" . parameter)
if ((value = "On") || (value = kTrue))
value := true
else if ((value = "Off") || (value = kFalse))
value := false
options .= (" -" . parameter . " `"" . value . "`"")
}
if booster
for ignore, parameter in ["SpeakerBooster", "ListenerBooster", "ConversationBooster", "AgentBooster"]
if thePlugin.hasArgument("raceAssistant" . parameter) {
value := thePlugin.getArgumentValue("raceAssistant" . parameter)
if ((value = "On") || (value = kTrue))
value := true
else if ((value = "Off") || (value = kFalse))
value := false
options .= (" -" . parameter . " `"" . value . "`"")
}
Run(kBinariesDirectory . assistant . ".exe -Logo true -Debug true -Configuration `"" . configurationFile . "`"" . options)
}
}