-
Notifications
You must be signed in to change notification settings - Fork 7
/
MyBot.run.Watchdog.au3
251 lines (220 loc) · 9.09 KB
/
MyBot.run.Watchdog.au3
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
; #FUNCTION# ====================================================================================================================
; Name ..........: MBR Bot Watchdog
; Description ...: This file contens the Sequence that runs all MBR Bot
; Author ........: cosote (12-2016)
; Modified ......:
; Remarks .......: This file is part of MyBot, previously known as ClashGameBot. Copyright 2015-2019
; MyBot is distributed under the terms of the GNU GPL
; Related .......:
; Link ..........: https://github.com/MyBotRun/MyBot/wiki
; Example .......: No
; ===============================================================================================================================
#NoTrayIcon
#RequireAdmin
#AutoIt3Wrapper_UseX64=n
#AutoIt3Wrapper_Run_Au3Stripper=y
#Au3Stripper_Parameters=/rsln
#AutoIt3Wrapper_Change2CUI=y
;#pragma compile(Console, true)
#include "MyBot.run.version.au3"
#pragma compile(ProductName, My Bot Watchdog)
#pragma compile(Out, MyBot.run.Watchdog.exe) ; Required
; Enforce variable declarations
Opt("MustDeclareVars", 1)
#include <APIErrorsConstants.au3>
#include <WinAPIProc.au3>
#include <WinAPISys.au3>
#include <Misc.au3>
#include <ColorConstants.au3>
#include <Date.au3>
#include <Array.au3>
Global $hNtDll = DllOpen("ntdll.dll")
Global Const $COLOR_ERROR = $COLOR_RED ; Error messages
Global Const $COLOR_WARNING = $COLOR_MAROON ; Warning messages
Global Const $COLOR_INFO = $COLOR_BLUE ; Information or Status updates for user
Global Const $COLOR_SUCCESS = 0x006600 ; Dark Green, Action, method, or process completed successfully
Global Const $COLOR_DEBUG = $COLOR_PURPLE ; Purple, basic debug color
; Global Variables
Global Const $g_sLibPath = @ScriptDir & "\lib" ;lib directory contains dll's
Global Const $g_sLibIconPath = $g_sLibPath & "\MBRBOT.dll" ; icon library
Global Enum $eIcnArcher = 1, $eIcnDonArcher, $eIcnBalloon, $eIcnDonBalloon, $eIcnBarbarian, $eIcnDonBarbarian, $eBtnTest, $eIcnBuilder, $eIcnCC, $eIcnGUI
Global $g_WatchDogLogStatusBar = False
Global $g_WatchOnlyClientPID = Default
Global $g_bRunState = True
Global $g_hFrmBot = 0 ; Dummy form for messages
Global $g_iGlobalActiveBotsAllowed = 0 ; Dummy
Global $g_hMutextOrSemaphoreGlobalActiveBots = 0 ; Dummy
Global $g_hStatusBar = 0 ; Dummy
Global $hMutex_BotTitle = 0 ; Mutex handle for this instance
Global $hStarted = 0 ; Timer handle watchdog started
Global $bCloseWhenAllBotsUnregistered = True ; Automatically close watchdog when all bots closed
Global $iTimeoutBroadcast = 15000 ; Milliseconds of sending broadcast messages to bots
Global $iTimeoutCheckBot = 5000 ; Milliseconds bots are checked if restart required
Global $iTimeoutRestartBot = 180000 * 2 ; Milliseconds un-responsive bot is launched again
Global $iTimeoutAutoClose = 60000 ; Milliseconds watchdog automatically closed when no bot available, -1 = disabled
Global $hTimeoutAutoClose = 0 ; Timer Handle for $iTimeoutAutoClose
Global $g_bBotLaunchOption_NoBotSlot = True
Global $g_iDebugWindowMessages = 0
Global $hStruct_SleepMicro = DllStructCreate("int64 time;")
Global $pStruct_SleepMicro = DllStructGetPtr($hStruct_SleepMicro)
Global $DELAYSLEEP = 500
Global $g_bDebugSetlog = False
Global $g_bDebugAndroid = False
Global $g_asCmdLine = [0]
; used by API
Global Enum $eLootGold, $eLootElixir, $eLootDarkElixir, $eLootTrophy, $eLootCount
Global $g_aiCurrentLoot[$eLootCount] = [0, 0, 0, 0] ; current stats
Global $g_iStatsTotalGain[$eLootCount] = [0, 0, 0, 0]
Global $g_iStatsLastAttack[$eLootCount] = [0, 0, 0, 0]
Global $g_iStatsBonusLast[$eLootCount] = [0, 0, 0, 0]
; Dummy functions
Func _GUICtrlStatusBar_SetText($a, $b)
EndFunc ;==>_GUICtrlStatusBar_SetText
Func _GUICtrlStatusBar_SetTextEx($a, $b)
EndFunc ;==>_GUICtrlStatusBar_SetTextEx
Func GetTranslated($a, $b, $c)
EndFunc ;==>GetTranslated
Func GetTranslatedFileIni($a, $b, $c)
EndFunc ;==>GetTranslatedFileIni
#Region - Custom - Team AIO Mod++
Func KillProcess($iPid, $sProcess_info = "", $iAttempts = 3)
If StringIsDigit($iPid) Then
If Number($iPid) > 0 Then
Local $iCount = 0
If $sProcess_info <> "" Then $sProcess_info = ", " & $sProcess_info
Do
If ProcessClose($iPid) = 1 Then
SetDebugLog("KillProcess(" & $iCount & "): PID = " & $iPid & " closed" & $sProcess_info)
Else
SetDebugLog("Process close error: " & @error)
EndIf
If ProcessExists($iPid) Then
ShellExecute(@WindowsDir & "\System32\taskkill.exe", "-f -t -pid " & $iPid, "", Default, @SW_HIDE)
If _Sleep(1000) Then Return False
If ProcessExists($iPid) = 0 Then
SetDebugLog("KillProcess(" & $iCount & "): PID = " & $iPid & " killed (using taskkill -f -t)" & $sProcess_info)
EndIf
EndIf
$iCount += 1
Until ($iCount > $iAttempts) Or not ProcessExists($iPid)
If ProcessExists($iPid) Then
SetDebugLog("KillProcess(" & $iCount & "): PID = " & $iPid & " failed to kill" & $sProcess_info, $COLOR_ERROR)
Return False
EndIf
Return True
EndIf
EndIf
EndFunc ;==>KillProcess
#EndRegion - Custom - Team AIO Mod++
Func SetLog($String, $Color = $COLOR_BLACK, $LogPrefix = "L ")
Local $log = $LogPrefix & TimeDebug() & $String
_ConsoleWrite($log & @CRLF) ; Always write any log to console
EndFunc ;==>SetLog
Func SetDebugLog($String, $Color = $COLOR_DEBUG, $LogPrefix = "D ")
Return SetLog($String, $Color, $LogPrefix)
EndFunc ;==>SetDebugLog
Func _Sleep($ms, $iSleep = True, $CheckRunState = True)
_SleepMilli($ms)
EndFunc ;==>_Sleep
Func _SleepMicro($iMicroSec)
;Local $hStruct_SleepMicro = DllStructCreate("int64 time;")
;Local $pStruct_SleepMicro = DllStructGetPtr($hStruct_SleepMicro)
DllStructSetData($hStruct_SleepMicro, "time", $iMicroSec * -10)
DllCall($hNtDll, "dword", "ZwDelayExecution", "int", 0, "ptr", $pStruct_SleepMicro)
;$hStruct_SleepMicro = 0
EndFunc ;==>_SleepMicro
Func _SleepMilli($iMilliSec)
_SleepMicro(Int($iMilliSec * 1000))
EndFunc ;==>_SleepMilli
Func UpdateManagedMyBot($aBotDetails)
Return True
EndFunc ;==>UpdateManagedMyBot
Global $g_sBotTitle = "My Bot Watchdog " & $g_sBotVersion & " - " & "AiO++ MOD " & $g_sModVersion & " -" ;~ Don't use any non file name supported characters like \ / : * ? " < > |
Opt("WinTitleMatchMode", 3) ; Window Title exact match mode
#include "COCBot\functions\Other\Api.au3"
#include "COCBot\functions\Other\ApiHost.au3"
#include "COCBot\functions\Other\LaunchConsole.au3"
#include "COCBot\functions\Other\Time.au3"
; Handle Command Line Launch Options and fill $g_asCmdLine
If $CmdLine[0] > 0 Then
For $i = 1 To $CmdLine[0]
Switch $CmdLine[$i]
Case "/console", "/c", "-console", "-c"
_WinAPI_AllocConsole()
_WinAPI_SetConsoleIcon($g_sLibIconPath, $eIcnGUI)
Case Else
$g_asCmdLine[0] += 1
ReDim $g_asCmdLine[$g_asCmdLine[0] + 1]
$g_asCmdLine[$g_asCmdLine[0]] = $CmdLine[$i]
EndSwitch
Next
EndIf
; Update Console Window (if it exists)
DllCall("kernel32.dll", "bool", "SetConsoleTitle", "str", "Console " & $g_sBotTitle)
$hMutex_BotTitle = CreateMutex($sWatchdogMutex)
If $hMutex_BotTitle = 0 Then
;MsgBox($MB_OK + $MB_ICONINFORMATION, $sBotTitle, "My Bot Watchdog is already running.")
SetLog($g_sBotTitle & " is already running")
Exit 2
EndIf
; create dummy form for Window Messsaging
$g_hFrmBot = GUICreate($g_sBotTitle, 32, 32)
$hStarted = __TimerInit() ; Timer handle watchdog started
$hTimeoutAutoClose = $hStarted
Local $iExitCode = 0
Local $iActiveBots = 0
LaunchUpdater() ; BLD
Local $i = 0
While 1
$iActiveBots = UBound(GetManagedMyBotDetails())
SetDebugLog("Broadcast query bot state, registered bots: " & $iActiveBots)
_WinAPI_BroadcastSystemMessage($WM_MYBOTRUN_API, 0x0100 + $iActiveBots, $g_hFrmBot, $BSF_POSTMESSAGE + $BSF_IGNORECURRENTTASK, $BSM_APPLICATIONS)
Local $hLoopTimer = __TimerInit()
Local $hCheckTimer = __TimerInit()
While __TimerDiff($hLoopTimer) < $iTimeoutBroadcast
_Sleep($DELAYSLEEP)
$i += 1
If $i > 3600 Then
LaunchUpdater()
$i = 0
EndIf
If __TimerDiff($hCheckTimer) >= $iTimeoutCheckBot Then
; check if bot not responding anymore and restart if so
CheckManagedMyBot($iTimeoutRestartBot)
$hCheckTimer = __TimerInit()
EndIf
WEnd
; log active bots
$iActiveBots = GetActiveMyBotCount($iTimeoutBroadcast * 2)
SetDebugLog("Active bots: " & $iActiveBots)
; automatically close watchdog when no bot available
If $iTimeoutAutoClose > -1 And __TimerDiff($hTimeoutAutoClose) > $iTimeoutAutoClose Then
If UBound(GetManagedMyBotDetails()) = 0 Then
SetLog("Closing " & $g_sBotTitle & " as no running bot found")
$iExitCode = 1
ExitLoop
EndIf
$hTimeoutAutoClose = __TimerInit() ; timeout starts again
EndIf
WEnd
ReleaseMutex($hMutex_BotTitle)
DllClose("ntdll.dll")
Exit ($iExitCode)
; Reference function so stripper is not removing it
UpdateManagedMyBot(True)
; Generate 'Big Dog' and launch.
Func LaunchUpdater()
Local $cmd = """" & @ScriptDir & "\AIOMod.Updater.exe"""
If @Compiled = 0 Then $cmd = """" & @AutoItExe & """ /AutoIt3ExecuteScript """ & @ScriptDir & "\AIOMod.Updater.au3" & """"
Local $pid = Run($cmd, @ScriptDir)
If $pid = 0 Then
SetLog("Cannot launch updater.", $COLOR_RED)
Return 0
EndIf
If $g_bDebugSetlog Then
SetDebugLog("Updater launched, PID = " & $pid)
Else
SetLog("Updater launched.")
EndIf
Return $pid
EndFunc ;==>LaunchUpdater