1. #cs ----------------------------------------------------------------------------
  2.  
  3. AutoIt Version: 3.2.10.0
  4. Author: Mixam
  5.  
  6. Script Function:
  7. Run Awesom-O 24/7
  8.  
  9. #ce ----------------------------------------------------------------------------
  10.  
  11. #include <String.au3>
  12. #include <Misc.au3>
  13. #include <WinAPI.au3>
  14. #include <File.au3>
  15. #include <GuiToolBar.au3>
  16.  
  17. Global $thisprogram = "MixamAO beta5"
  18. Global $redvexname = IniRead($thisprogram & ".ini", "Settings", "RedVex", "")
  19. Global $preventrd = IniRead($thisprogram & ".ini", "Settings", "RDprevention", "1")
  20. Global $managelogs = IniRead($thisprogram & ".ini", "Settings", "ManageLogs", "1")
  21. Global $maxgametimesecs = IniRead($thisprogram & ".ini", "Settings", "MaxGameLength", 600)
  22. Global $banwait = IniRead($thisprogram & ".ini", "Settings", "BanWaitTime", 3600000)
  23. Global $redvexdir = IniRead($thisprogram & ".ini", "Settings", "RedVexDir", "")
  24. Global $refreshtray = IniRead($thisprogram & ".ini", "Settings", "RefreshTray", 1)
  25. Global $redvexexe = ($redvexdir & $redvexname & ".exe")
  26. Global $gamename = StringTrimRight(IniRead($redvexdir & "Awesom-O.ini", "Bot0", "GameName", ""), 2)
  27. Global $gamelog = $redvexdir & "games.txt"
  28. Global $sleep = IniRead($thisprogram & ".ini", "Settings", "Sleep", "1000")
  29. Global $diablofull = IniRead($redvexdir & "Awesom-O.ini", "Settings", "GameExe", "Diablo II.exe")
  30. Global $diabloprocess = StringTrimLeft($diablofull, StringInStr($diablofull, "\", 0, -1))
  31. $diabloprocess = StringTrimLeft($diabloprocess, StringInStr($diabloprocess, "/", 0, -1))
  32. Global $line = 0
  33. Global $mixamAOlog = (@ScriptDir & "\MixamAO.log")
  34. Global $lastgamestr, $runforsecs, $rdgametime
  35. Global $nextgamenumber = 1
  36. Global $newlogfolder = @ScriptDir & "\Game Logs\" & @YEAR & "-" & @MON & "-" & @MDAY & " " & @HOUR & "." & @MIN & "." & @SEC
  37.  
  38. Opt("TrayAutoPause",0)
  39. TraySetClick(8)
  40.  
  41. If _Singleton($thisprogram, 1) = 0 Then
  42. MsgBox(4096, "Warning", "An occurence of " & $thisprogram & " is already running")
  43. _FileWriteLog(@ScriptDir & "\MixamAO.log", "ERROR: An occurence of " & $thisprogram & " is already running")
  44. Opt("OnExitFunc", "_normalexit")
  45. Exit
  46. EndIf
  47.  
  48. If ProcessExists($redvexname & ".exe") Then
  49. MsgBox(4096, "Warning", "RedVex is running already. Hitting OK will close redvex and D2 and start MixamAO")
  50. _FileWriteLog(@ScriptDir & "\MixamAO.log", "ERROR: RedVex is running already.")
  51. _Kill()
  52. EndIf
  53.  
  54. If Not FileExists($thisprogram & ".ini") Then
  55. MsgBox(4096, "Error", "Can't find " & $thisprogram & ".ini :(")
  56. _FileWriteLog(@ScriptDir & "\MixamAO.log", "ERROR: Can't find " & $thisprogram & ".ini :(")
  57. Exit
  58. EndIf
  59.  
  60. If Not FileExists($mixamAOlog) Then
  61. _FileCreate(@ScriptDir & "\MixamAO.log")
  62. _FileWriteLog(@ScriptDir & "\MixamAO.log", "Notice: MixamAO.log was missing. It has been created")
  63. EndIf
  64.  
  65. If Not FileExists($redvexexe) Then
  66. MsgBox(4096, "Error", "Can't find " & $redvexexe & " :(")
  67. _FileWriteLog(@ScriptDir & "\MixamAO.log", "ERROR: Can't find " & $redvexexe & " :(")
  68. Exit
  69. EndIf
  70.  
  71.  
  72. _FileWriteLog(@ScriptDir & "\MixamAO.log", "Notice: MixamAO Started")
  73. _Load()
  74.  
  75. While 1
  76. Sleep($sleep)
  77. _CrashCheck()
  78. WEnd
  79.  
  80. ; _Load runs redvex then waits for Awesom-O to open. Then it sleeps for 2 mins to
  81. ; allow the first game to be made before _LastGameSecsAgo runs.
  82.  
  83. Func _Load()
  84. Run($redvexexe & " -run -hide", $redvexdir)
  85. _FileWriteLog(@ScriptDir & "\MixamAO.log", "Notice: Redvex Loaded")
  86. Sleep(120000)
  87. EndFunc ;==>_Load
  88.  
  89. ; _Kill closes redvex and all D2's you have open. Be aware that it doesn't just
  90. ; close the bot's window, it closes all D2 windows. Also cleans the tray now.
  91. ; Also now saves all the log files to Game Logs folder in the MixamAO folder
  92.  
  93. Func _Kill()
  94. ProcessClose($redvexname & ".exe")
  95. ProcessClose($diabloprocess)
  96.  
  97. If ProcessExists($diabloprocess) Then
  98. While ProcessExists($diabloprocess)
  99. ProcessClose($diabloprocess)
  100. WEnd
  101. EndIf
  102.  
  103. _FileWriteLog(@ScriptDir & "\MixamAO.log", "Notice: Redvex and D2 killed")
  104. IniWrite($redvexdir & "Awesom-O.ini", "Bot0", "StartNumber", $nextgamenumber)
  105. _FileWriteLog(@ScriptDir & "\MixamAO.log", "Notice: game start number changed to " & $nextgamenumber)
  106. _CleanTray($redvexname, $refreshtray)
  107.  
  108. If $managelogs = 1 Then
  109. DirCreate($newlogfolder)
  110. FileCopy ($redvexdir & "\Awesom-O.log", $newlogfolder & "\" & @YEAR & "-" & @MON & "-" & @MDAY & " " & @HOUR & "." & @MIN & "." & @SEC & " Awesom-O.log")
  111. EndIf
  112. EndFunc ;==>_Kill
  113.  
  114. ; _CrashCheck checks if its been over your set max time since the last game was created
  115. ; or if redvex has closed and if either happens, it kills and restarts D2 and RedVex
  116. ; It now also checks for r/d and if some errors pop up.
  117.  
  118. Func _CrashCheck()
  119. $lastgamestr = _FindLastGame()
  120. $runforsecs = _LastGameSecsAgo()
  121. $rdgametime = _RDGameSecsAgo()
  122.  
  123. If $lastgamestr = "Empty" Then
  124. $nextgamenumber = 1
  125. Else
  126. $nextgamenumber = _FindLastGameNumber() + 1
  127. EndIf
  128.  
  129. Local $bansleep = $banwait
  130.  
  131. While StringInStr($lastgamestr, "IP Ban")
  132. _FileWriteLog(@ScriptDir & "\MixamAO.log", "Notice: IP Ban detected")
  133. _Kill()
  134. _FileWriteLog(@ScriptDir & "\MixamAO.log", "Notice: Waiting " & $bansleep & "ms for ban to be over")
  135. Sleep($bansleep)
  136. $bansleep = $bansleep * 2
  137. _Load()
  138. Sleep(120000)
  139. $lastgamestr = _FindLastGame()
  140. WEnd
  141.  
  142. If $runforsecs > $maxgametimesecs Then
  143. _FileWriteLog(@ScriptDir & "\MixamAO.log", "Notice: " & $maxgametimesecs & "secs is up. Crash detected")
  144. _Kill()
  145. _Load()
  146. EndIf
  147.  
  148. If $rdgametime < 3600 Then
  149. If $preventrd = 1 Then
  150. _Kill()
  151. _FileWriteLog(@ScriptDir & "\MixamAO.log", "Notice: 19 games made in " & $rdgametime & "secs. Waiting " & (3660 - $rdgametime) & "secs to restart")
  152. Sleep((3660 - $rdgametime) * 1000)
  153. _Load()
  154. EndIf
  155. EndIf
  156.  
  157. If WinExists("End Program - Diablo_0") Then
  158. WinClose("End Program - Diablo_0")
  159. _FileWriteLog(@ScriptDir & "\MixamAO.log", "Notice: End Program window detected. Closing D2")
  160.  
  161. If ProcessExists($diabloprocess) Then
  162. While ProcessExists($diabloprocess)
  163. ProcessClose($diabloprocess)
  164. WEnd
  165. EndIf
  166. EndIf
  167.  
  168. If WinExists("Diablo_0: Diablo II.exe - Application Error") Then
  169. WinClose("Diablo_0: Diablo II.exe - Application Error")
  170. _FileWriteLog(@ScriptDir & "\MixamAO.log", "Notice: Application Error window detected. Closing D2")
  171.  
  172. If ProcessExists($diabloprocess) Then
  173. While ProcessExists($diabloprocess)
  174. ProcessClose($diabloprocess)
  175. WEnd
  176. EndIf
  177. EndIf
  178.  
  179. If WinExists($redvexname & ".exe") Then
  180. WinClose($redvexname & ".exe")
  181. _FileWriteLog(@ScriptDir & "\MixamAO.log", "Notice: Redvex Error Detected")
  182. _Kill()
  183. _Load()
  184. EndIf
  185.  
  186. If WinExists("Microsoft Visual C++ Runtime Library") Then
  187. WinClose("Microsoft Visual C++ Runtime Library")
  188. _FileWriteLog(@ScriptDir & "\MixamAO.log", "Notice: Microsoft Visual C++ Runtime Library Error detected")
  189. _Kill()
  190. _Load()
  191. EndIf
  192.  
  193. If Not ProcessExists($redvexname & ".exe") Then
  194. _FileWriteLog(@ScriptDir & "\MixamAO.log", "Notice: Detected that Redvex is not running")
  195. _Kill()
  196. _Load()
  197. EndIf
  198. EndFunc ;==>_CrashCheck
  199.  
  200. ; _FindLastGame searches the log file for the last entry with the game name in it then
  201. ; returns a string with that last game name and the time the game was made. It also now
  202. ; returns a string if there is a ban message for r/d support
  203.  
  204. Func _FindLastGame()
  205. Local $logfile = FileOpen($gamelog, 0)
  206. Local $linestr
  207.  
  208. If $logfile = -1 Then
  209. MsgBox(4096, "Error", "Unable to locate the game log.")
  210. _FileWriteLog(@ScriptDir & "\MixamAO.log", "ERROR: Unable to locate the game log")
  211. Exit
  212. EndIf
  213.  
  214. While 1
  215. $line = $line + 1
  216. $linestr = FileReadLine($logfile, $line)
  217.  
  218. If @error = -1 Then
  219. While $line >= 0
  220. $line = $line - 1
  221. $linestr = FileReadLine($logfile, $line)
  222.  
  223. If StringInStr($linestr, $gamename) Or StringInStr($linestr, "IP Ban") Then
  224. $linestr = FileReadLine($logfile, $line - 1) & FileReadLine($logfile, $line)
  225. ExitLoop 2
  226. EndIf
  227. WEnd
  228.  
  229. $linstr = "Empty"
  230. ExitLoop
  231. EndIf
  232. WEnd
  233.  
  234. FileClose($logfile)
  235. Return $linestr
  236. EndFunc ;==>_FindLastGame
  237.  
  238. ; _FindLastGameNumber() returns the last game number made in the games.txt file
  239.  
  240. Func _FindLastGameNumber()
  241. Local $logfile = FileOpen($gamelog, 0)
  242. Local $linestr
  243.  
  244. If $logfile = -1 Then
  245. MsgBox(4096, "Error", "Unable to locate the game log.")
  246. _FileWriteLog(@ScriptDir & "\MixamAO.log", "ERROR: Unable to locate the game log")
  247. Exit
  248. EndIf
  249.  
  250. If ($line = 0 And $nextgamenumber = 1) Then
  251. _FileWriteLog(@ScriptDir & "\MixamAO.log", "Notice: No games found, setting last game number to 0")
  252. FileClose($logfile)
  253. Sleep(5000)
  254. Return 0
  255. ElseIf ($line = 0 And $nextgamenumber <> 1) Then
  256. _FileWriteLog(@ScriptDir & "\MixamAO.log", "Notice: No games found, setting last game number to last found lastgamenumber")
  257. Return $nextgamenumber - 1
  258. EndIf
  259.  
  260. While 1
  261. If $line = 0 Then
  262. _FileWriteLog(@ScriptDir & "\MixamAO.log", "Notice: No games found, setting last game number to last found lastgamenumber")
  263. Return $nextgamenumber - 1
  264. EndIf
  265.  
  266. $linestr = FileReadLine($logfile, $line)
  267.  
  268. If StringInStr($linestr, $gamename) Then
  269. $linestr = StringTrimLeft($linestr, (StringInStr($linestr, $gamename, 0, -1) + StringLen($gamename) - 1))
  270. ExitLoop
  271. EndIf
  272.  
  273. $line = $line - 1
  274. WEnd
  275.  
  276. FileClose($logfile)
  277. Return $linestr
  278. EndFunc ;==>_FindLastGameNumber
  279.  
  280. ; _LastGameSecsAgo searches the string returned by _FindLastGame and compares its time
  281. ; to the current time to see how long ago the game was made. Not sure if Number()
  282. ; is required here or not yet. Have to test without it. The +86400 is for when the game was
  283. ; made at 00:??:??
  284.  
  285. Func _LastGameSecsAgo()
  286. If $lastgamestr = "Empty" Then
  287. Sleep(120000)
  288. $lastgamestr = _FindLastGame()
  289.  
  290. If $lastgamestr = "Empty" Then
  291. Return ($maxgametimesecs + 1)
  292. EndIf
  293. EndIf
  294.  
  295. Local $gamehour = Number(StringMid($lastgamestr, 12, 2))
  296. Local $gamemin = Number(StringMid($lastgamestr, 15, 2))
  297. Local $gamesec = Number(StringMid($lastgamestr, 18, 2))
  298. Local $secsago = (@HOUR * 3600 + @MIN * 60 + @SEC) - ($gamehour * 3600 + $gamemin * 60 + $gamesec)
  299.  
  300. If $secsago < 0 Then
  301. $secsago = $secsago + 86400
  302. EndIf
  303.  
  304. Return $secsago
  305. EndFunc ;==>_LastGameSecsAgo
  306.  
  307. ; This function checks how long ago the 19th game was made. This is so i can detect if
  308. ; it is likely that 20 games will be made in an hour or less. For preventing r/d.
  309.  
  310. Func _RDGameSecsAgo()
  311. Local $logfile = FileOpen($gamelog, 0)
  312. Local $rdgame
  313.  
  314. If $line < 39 Then
  315. FileClose($logfile)
  316. Return 3601
  317. Else
  318. $rdgame = FileReadLine($logfile, $line - 39)
  319. EndIf
  320.  
  321. Local $gamehour = Number(StringMid($rdgame, 12, 2))
  322. Local $gamemin = Number(StringMid($rdgame, 15, 2))
  323. Local $gamesec = Number(StringMid($rdgame, 18, 2))
  324. Local $secsago = (@HOUR * 3600 + @MIN * 60 + @SEC) - ($gamehour * 3600 + $gamemin * 60 + $gamesec)
  325.  
  326. If $secsago < 0 Then
  327. $secsago = $secsago + 86400
  328. EndIf
  329.  
  330. FileClose($logfile)
  331. Return $secsago
  332. EndFunc ;==>_RDGameSecsAgo
  333.  
  334. ; This function clicks on each icon with the name = $iTitle to remove the old icons at the moment it does
  335. ; The Redvex icons, but if kingsob brings back the Awesom-O icon then I can clean that too.
  336.  
  337. Func _CleanTray($iTitle, $a)
  338. If $a = 1 Then
  339. Local $hToolbar, $ItemCount, $ID, $i
  340. $hToolbar = ControlGetHandle('[Class:Shell_TrayWnd]', '', '[Class:ToolbarWindow32;Instance:1]')
  341. If @error Then Return
  342.  
  343. $ItemCount = _GUICtrlToolbar_ButtonCount($hToolbar)
  344. If $ItemCount = 0 Then Return
  345.  
  346. For $i = 0 To $ItemCount - 1
  347. $ID = _GUICtrlToolbar_IndexToCommand($hToolbar, $i)
  348. If StringInStr(_GUICtrlToolbar_GetButtonText($hToolbar, $ID), $iTitle) Then
  349. _GUICtrlToolbar_ClickButton2($hToolbar, $ID)
  350. Sleep(50)
  351. EndIf
  352. Next
  353. _FileWriteLog(@ScriptDir & "\MixamAO.log", "Notice: Tray cleaned")
  354. EndIf
  355. EndFunc
  356.  
  357. ; #FUNCTION# ====================================================================================================
  358. ; Name...........: _GUICtrlToolbar_ClickButton
  359. ; Description ...: Clicks a specific button
  360. ; Syntax.........: _GUICtrlToolbar_ClickButton($hWnd, $iCommandID[, $sButton = "left"[, $fMove = False[, $iClicks = 1[, $iSpeed = 1]]]])
  361. ; Parameters ....: $hWnd - Handle to the control
  362. ; $iCommandID - Button command ID
  363. ; $sButton - Button to click
  364. ; $fMove - Mouse movement flag:
  365. ; | True - Mouse will be moved
  366. ; |False - Mouse will not be moved
  367. ; $iClicks - Number of clicks
  368. ; $iSpeed - Mouse movement speed
  369. ; Return values .:
  370. ; Author ........: Paul Campbell (PaulIA)
  371. ; Modified.......: Gary Frost
  372. ; Remarks .......:
  373. ; Related .......: _GUICtrlToolbar_ClickAccel, _GUICtrlToolbar_ClickIndex
  374. ; Link ..........;
  375. ; Example .......; Yes
  376. ; ==============================================================================================================
  377.  
  378. Func _GUICtrlToolbar_ClickButton2($hWnd, $iCommandID, $sButton = "left", $fMove = False, $iClicks = 1, $iSpeed = 1)
  379. If $Debug_TB Then _GUICtrlToolbar_ValidateClassName($hWnd)
  380. Local $tPoint, $tRect, $iX, $iY, $iMode, $aPos
  381.  
  382. $tRect = _GUICtrlToolbar_GetButtonRectEx($hWnd, $iCommandID)
  383. $tPoint = _WinAPI_PointFromRect2($tRect)
  384. $tPoint = _WinAPI_ClientToScreen($hWnd, $tPoint)
  385. _WinAPI_GetXYFromPoint2($tPoint, $iX, $iY)
  386. If Not $fMove Then
  387. $iMode = Opt("MouseCoordMode", 1)
  388. $aPos = MouseGetPos()
  389. Opt("MouseCoordMode", $iMode)
  390. _WinAPI_ShowCursor(False)
  391. MouseClick($sButton, $iX, $iY, $iClicks, $iSpeed)
  392. MouseMove($aPos[0], $aPos[1], 0)
  393. _WinAPI_ShowCursor(True)
  394. Else
  395. MouseClick($sButton, $iX, $iY, $iClicks, $iSpeed)
  396. EndIf
  397. EndFunc ;==>_GUICtrlToolbar_ClickButton
  398.  
  399. ; #FUNCTION# ====================================================================================================
  400. ; Description ...: Returns the top/left coordinates of a tagRECT as a tagPOINT structure
  401. ; Parameters ....: $tRect - tagRECT structure
  402. ; $fCenter - If True, the return will be a point at the center of the rectangle, otherwise the left/top
  403. ; +coordinates are returned.
  404. ; Return values .: Success - tagPOINT structure
  405. ; Author ........: Paul Campbell (PaulIA)
  406. ; Remarks .......: This function is used to get the click position for many of the click functions in the library
  407. ; Related .......:
  408. ; ===============================================================================================================
  409.  
  410. Func _WinAPI_PointFromRect2(ByRef $tRect, $fCenter = True)
  411. Local $iX1, $iY1, $iX2, $iY2, $tPoint
  412.  
  413. $iX1 = DllStructGetData($tRect, "Left")
  414. $iY1 = DllStructGetData($tRect, "Top")
  415. $iX2 = DllStructGetData($tRect, "Right")
  416. $iY2 = DllStructGetData($tRect, "Bottom")
  417. If $fCenter Then
  418. $iX1 = $iX1 + (($iX2 - $iX1) / 2)
  419. $iY1 = $iY1 + (($iY2 - $iY1) / 2)
  420. EndIf
  421. $tPoint = DllStructCreate($tagPOINT)
  422. DllStructSetData($tPoint, "X", $iX1)
  423. DllStructSetData($tPoint, "Y", $iY1)
  424. Return $tPoint
  425. EndFunc ;==>_WinAPI_PointFromRect
  426.  
  427. ; #FUNCTION# ====================================================================================================
  428. ; Description ...: Returns the X/Y values from a tagPOINT structure
  429. ; Parameters ....: $tPoint - tagPOINT structure
  430. ; $iX - X value
  431. ; $iY - Y value
  432. ; Return values .:
  433. ; Author ........: Paul Campbell (PaulIA)
  434. ; Remarks .......: This function extracts the X/Y values from a tagPOINT structure
  435. ; Related .......: _Lib_GetPointFromXY, _Lib_GetXYFromRect
  436. ; ===============================================================================================================
  437.  
  438. Func _WinAPI_GetXYFromPoint2(ByRef $tPoint, ByRef $iX, ByRef $iY)
  439. $iX = DllStructGetData($tPoint, "X")
  440. $iY = DllStructGetData($tPoint, "Y")
  441. EndFunc ;==>_WinAPI_GetXYFromPoint
  442.  
  443. ; This function changes the defualt exit for auto it to run _kill then exit so exiting MixamAO
  444. ; will also exit D2 and Redvex
  445.  
  446. Func _cleangameslog()
  447. _FileCreate ($gamelog & ".temp.txt")
  448. Local $lines = _FileCountLines($gamelog)
  449. Local $hgamelog = FileOpen($gamelog, 0)
  450.  
  451. For $x = ($lines - 39) to $lines
  452. If $x > 0 Then
  453. FileWriteLine($gamelog & ".temp.txt", FileReadLine($hgamelog, $x))
  454. EndIf
  455. Next
  456.  
  457. FileClose($hgamelog)
  458. FileMove($gamelog & ".temp.txt", $gamelog, 1)
  459. EndFunc
  460.  
  461. Func _normalexit()
  462. Exit
  463. EndFunc
  464.  
  465. Func OnAutoItExit()
  466. _Kill()
  467. IniWrite($redvexdir & "Awesom-O.ini", "Bot0", "StartNumber", 1)
  468. _FileWriteLog(@ScriptDir & "\MixamAO.log", "Notice: MixamAO Closed Manually. Game start number changed back to 1")
  469. If $managelogs = 1 Then
  470. DirCreate($newlogfolder)
  471. FileMove (@ScriptDir & "\MixamAO.log", $newlogfolder & "\MixamAO.log")
  472. _FileCreate(@ScriptDir & "\MixamAO.log")
  473. FileCopy ($gamelog, $newlogfolder & "\games.txt")
  474. _cleangameslog()
  475. $line = 0
  476. EndIf
  477. Exit
  478. EndFunc ;==>OnAutoItExit