Skip to content

Commit

Permalink
Merge pull request #98 from tanjeffreyz/dev
Browse files Browse the repository at this point in the history
Fixed shortcut errors with insufficient privileges, restricted recording points during routines
- hoping to change 'Reload routine' to work as an instant restart even while the bot is running
  • Loading branch information
tanjeffreyz authored May 29, 2022
2 parents d5aa516 + e261229 commit 98774ea
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 13 deletions.
45 changes: 35 additions & 10 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,67 @@
"""Creates a desktop shortcut that can run Auto Maple from anywhere."""

import os
import sys
import ctypes
import argparse
import win32com.client as client


MAX_DEPTH = 1 # Run at most MAX_DEPTH additional times


def run_as_admin():
if args.depth < MAX_DEPTH:
print('\n[!] Insufficient privileges, re-running as administrator')
ctypes.windll.shell32.ShellExecuteW(
None,
'runas',
sys.executable,
' '.join(sys.argv + [f'--depth {args.depth + 1}']),
None,
1
)
print(' ~ Finished setting up Auto Maple')
exit(0)


def create_desktop_shortcut():
"""Creates and saves a desktop shortcut using absolute paths"""
print('\n[~] Creating desktop shortcut for Auto Maple:')
CWD = os.getcwd()
TARGET = os.path.join(os.environ['WINDIR'], 'System32', 'cmd.exe')
PATH = os.path.join(os.environ['USERPROFILE'], 'Desktop', 'Auto Maple.lnk')
cwd = os.getcwd()
target = os.path.join(os.environ['WINDIR'], 'System32', 'cmd.exe')

flag = "/c"
if args.stay:
flag = "/k"
print(" - Leaving command prompt open after program finishes")

shell = client.Dispatch('WScript.Shell')
shortcut = shell.CreateShortCut(PATH)
shortcut.Targetpath = TARGET
shortcut.Arguments = flag + f' \"cd {CWD} & python main.py\"'
shortcut.IconLocation = os.path.join(CWD, 'assets', 'icon.ico')
shortcut.save()
shortcut_path = os.path.join(shell.SpecialFolders('Desktop'), 'Auto Maple.lnk')
shortcut = shell.CreateShortCut(shortcut_path)
shortcut.Targetpath = target
shortcut.Arguments = flag + f' \"cd {cwd} & python main.py\"'
shortcut.IconLocation = os.path.join(cwd, 'assets', 'icon.ico')
try:
shortcut.save()
except:
run_as_admin()

# Enable "run as administrator"
with open(PATH, 'rb') as lnk:
with open(shortcut_path, 'rb') as lnk:
arr = bytearray(lnk.read())

arr[0x15] = arr[0x15] | 0x20 # Set the 6th bit of 21st byte to 1

with open(PATH, 'wb') as lnk:
with open(shortcut_path, 'wb') as lnk:
lnk.write(arr)
print(' - Enabled the "Run as Administrator" option')
print(' ~ Successfully created Auto Maple shortcut')


if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--depth', type=int, default=0)
parser.add_argument('--stay', action='store_true')
args = parser.parse_args()

Expand Down
2 changes: 1 addition & 1 deletion src/modules/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def _solve_rune(self, model):
adjust(*self.rune_pos).execute()
time.sleep(0.2)
press(self.config['Interact'], 1, down_time=0.2) # Inherited from Configurable

print('\nSolving rune:')
inferences = []
for _ in range(15):
Expand Down
17 changes: 15 additions & 2 deletions src/modules/listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Listener(Configurable):
'Reload routine': 'f6',
'Record position': 'f7'
}
BLOCK_DELAY = 1 # Delay after blocking restricted button press

def __init__(self):
"""Initializes this Listener object's main thread."""
Expand All @@ -24,6 +25,7 @@ def __init__(self):

self.enabled = False
self.ready = False
self.block_time = 0
self.thread = threading.Thread(target=self._main)
self.thread.daemon = True

Expand All @@ -49,16 +51,27 @@ def _main(self):
Listener.toggle_enabled()
elif kb.is_pressed(self.config['Reload routine']):
Listener.reload_routine()
elif kb.is_pressed(self.config['Record position']):
elif self.restricted_pressed('Record position'):
Listener.record_position()
time.sleep(0.01)

def restricted_pressed(self, action):
"""Returns whether the key bound to ACTION is pressed only if the bot is disabled."""

if kb.is_pressed(self.config[action]):
if not config.enabled:
return True
now = time.time()
if now - self.block_time > Listener.BLOCK_DELAY:
print(f"\n[!] Cannot use '{action}' while Auto Maple is enabled")
self.block_time = now
return False

@staticmethod
def toggle_enabled():
"""Resumes or pauses the current routine. Plays a sound to notify the user."""

config.bot.rune_active = False
config.bot.alert_active = False

if not config.enabled:
Listener.recalibrate_minimap() # Recalibrate only when being enabled.
Expand Down

0 comments on commit 98774ea

Please sign in to comment.