This repository has been archived by the owner on Apr 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
auto-craft.au3
90 lines (68 loc) · 2.88 KB
/
auto-craft.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
#include <MsgBoxConstants.au3>
Func _WinAPI_GetKeyState($iKey)
Local $aResult = DllCall("User32.dll", "int", "GetKeyState", "int", $iKey)
ExitOnCondition(@error, 'Error getting numlock state')
Return $aResult[0]
EndFunc
;~ stops program execution if condition fails
Func ExitOnCondition($condition, $message)
If $condition Then
MsgBox($MB_OK, 'Program stopped', StringFormat('%s\nCrafted %s items in %s seconds', $message, $totalItems, $totalTime))
Exit 1
EndIf
EndFunc
;~ parses key data for macroses
Func ParseMacros($iniFile, $sectionPrefix)
Local $sections = IniReadSectionNames($iniFile)
ExitOnCondition(@error, StringFormat('Error parsing [%s]', $iniFile))
Local $macros = ObjCreate('System.Collections.ArrayList')
For $i = 1 To $sections[0]
If StringLeft($sections[$i], StringLen($sectionPrefix)) = $sectionPrefix Then
$macros.Add(ReadKeyData($iniFile, $sections[$i]))
EndIf
Next
Return $macros
EndFunc
Func IsPositiveInteger($value)
Return StringIsInt($value) And $value > 0
EndFunc
;~ checks if key data is correct
Func CheckKeyData($keyData)
Return $keyData[0] <> '' And IsPositiveInteger($keyData[1])
EndFunc
;~ reads key data as array from ini file
Func ReadKeyData($iniFile, $section, $check = True)
Local $keyData[2]
$keyData[0] = IniRead($iniFile, $section, 'Key', '')
$keyData[1] = IniRead($iniFile, $section, 'Time', 0)
ExitOnCondition($check = True And Not CheckKeyData($keyData), StringFormat('Incorrect values in [%s] section', $section))
Return $keyData
EndFunc
Func SendControlAndSleep($hWin, $winName, $key, $sleepTime)
ExitOnCondition(Not (IsHWnD ($hWin) and WinExists ($winName) <> '0'), 'There is no game window')
ControlSend($hWin, "", "", $key)
Sleep($sleepTime * 1000)
$totalTime += $sleepTime
EndFunc
$iniFile = 'config.ini'
$winName = IniRead($iniFile, 'General', 'WindowName', '')
$hWin = WinGetHandle($winName)
$timeLimit = IniRead($iniFile, 'General', 'TimeLimit', 0)
$totalTime = 0
$maxItems = IniRead($iniFile, 'General', 'MaxItems', 0)
$totalItems = 0
ExitOnCondition(Not IsPositiveInteger($timeLimit) And Not IsPositiveInteger($maxItems), 'Either time or item limit should be specified!')
$macros = ParseMacros($iniFile, IniRead($iniFile, 'General', 'MacroSectionPrefix', 'Macro'))
ExitOnCondition($macros.Count = 0, 'There is no macro data in config')
While 1
ExitOnCondition((IsPositiveInteger($timeLimit) And $totalTime >= $timeLimit) Or (IsPositiveInteger($maxItems) And $totalItems >= $maxItems), 'Finished!')
ExitOnCondition(Not _WinAPI_GetKeyState(0x90), 'Numlock is disabled')
For $i = 0 To 4
SendControlAndSleep($hWin, $winName, '{NUMPAD0}', 1)
Next
For $i = 0 To $macros.Count - 1
$item = $macros.Item($i)
SendControlAndSleep($hWin, $winName, $item[0], $item[1])
Next
$totalItems += 1
WEnd