Skip to content

Commit

Permalink
Merge pull request #86 from tanjeffreyz/dev
Browse files Browse the repository at this point in the history
Separating resources by command book name to avoid collisions
  • Loading branch information
tanjeffreyz authored May 27, 2022
2 parents b70fee0 + 57f4db1 commit 1a46a96
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 14 deletions.
21 changes: 18 additions & 3 deletions src/gui/menu/file.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import tkinter as tk
from src.common import config, utils
from src.gui.interfaces import MenuBarItem
from tkinter.filedialog import askopenfilename, asksaveasfilename
Expand All @@ -14,7 +15,14 @@ def __init__(self, parent, **kwargs):
self.add_command(label='Save Routine', command=utils.async_callback(self, File._save_routine))
self.add_separator()
self.add_command(label='Load Command Book', command=utils.async_callback(self, File._load_commands))
self.add_command(label='Load Routine', command=utils.async_callback(self, File._load_routine))
self.add_command(
label='Load Routine',
command=utils.async_callback(self, File._load_routine),
state=tk.DISABLED
)

def enable_routine_state(self):
self.entryconfig('Load Routine', state=tk.NORMAL)

@staticmethod
@utils.run_if_disabled('\n[!] Cannot create a new routine while Auto Maple is enabled')
Expand All @@ -30,7 +38,7 @@ def _new_routine():
@staticmethod
@utils.run_if_disabled('\n[!] Cannot save routines while Auto Maple is enabled')
def _save_routine():
file_path = asksaveasfilename(initialdir=os.path.join(config.RESOURCES_DIR, 'routines'),
file_path = asksaveasfilename(initialdir=get_routines_dir(),
title='Save routine',
filetypes=[('*.csv', '*.csv')],
defaultextension='*.csv')
Expand All @@ -46,7 +54,7 @@ def _load_routine():
'Would you like to proceed anyways?',
icon='warning'):
return
file_path = askopenfilename(initialdir=os.path.join(config.RESOURCES_DIR, 'routines'),
file_path = askopenfilename(initialdir=get_routines_dir(),
title='Select a routine',
filetypes=[('*.csv', '*.csv')])
if file_path:
Expand All @@ -66,3 +74,10 @@ def _load_commands():
filetypes=[('*.py', '*.py')])
if file_path:
config.bot.load_commands(file_path)


def get_routines_dir():
target = os.path.join(config.RESOURCES_DIR, 'routines', config.bot.module_name)
if not os.path.exists(target):
os.makedirs(target)
return target
12 changes: 8 additions & 4 deletions src/modules/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def __init__(self):
self.rune_pos = (0, 0)
self.rune_closest_pos = (0, 0) # Location of the Point closest to rune
self.submodules = {}
self.module_name = None
self.buff = components.Buff()

self.command_book = {}
Expand Down Expand Up @@ -166,7 +167,8 @@ def load_commands(self, file):

# Import the desired command book file
module_name = splitext(basename(file))[0]
module = __import__(f'resources.command_books.{module_name}', fromlist=[''])
target = '.'.join(['resources', 'command_books', module_name])
module = __import__(target, fromlist=[''])

# Check if the 'step' function has been implemented
step_found = False
Expand Down Expand Up @@ -197,18 +199,20 @@ def load_commands(self, file):
new_cb[name] = command

if not step_found and not movement_found:
print(f" ! Error: Must either implement both the 'move' and 'adjust' commands, "
print(f" ! Error: Must either implement both 'Move' and 'Adjust' commands, "
f"or the function 'step'.")
if required_found and (step_found or movement_found):
self.module_name = module_name
self.command_book = new_cb
self.buff = new_cb['buff']()
components.step = new_step
config.gui.menu.file.enable_routine_state()
config.gui.view.status.set_cb(basename(file))
config.routine.clear()
print(f"[~] Successfully loaded command book '{module_name}'.")
print(f" ~ Successfully loaded command book '{module_name}'.")
return True
else:
print(f"[!] Command book '{module_name}' was not loaded.")
print(f" ! Command book '{module_name}' was not loaded.")
return False

def update_submodules(self, force=False):
Expand Down
1 change: 1 addition & 0 deletions src/modules/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def __init__(self):

self.navigation.pack(expand=True, fill='both')
self.navigation.bind('<<NotebookTabChanged>>', self._resize_window)
self.root.focus()

def set_routine(self, arr):
self.routine_var.set(arr)
Expand Down
16 changes: 11 additions & 5 deletions src/routine/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ def __iter__(self):
class Layout:
"""Uses a quadtree to represent possible player positions in a map layout."""

LAYOUTS_DIR = os.path.join(config.RESOURCES_DIR, 'layouts')
TOLERANCE = settings.move_tolerance / 2

def __init__(self, name):
Expand Down Expand Up @@ -266,13 +265,13 @@ def load(routine):
"""

layout_name = splitext(basename(routine))[0]
target = f'{Layout.LAYOUTS_DIR}/{layout_name}'
target = os.path.join(get_layouts_dir(), layout_name)
if isfile(target):
print(f" ~ Found existing Layout file at '{target}'.")
print(f" - Found existing Layout file at '{target}'.")
with open(target, 'rb') as file:
return pickle.load(file)
else:
print(f" ~ Created new Layout file at '{target}'.")
print(f" - Created new Layout file at '{target}'.")
new_layout = Layout(layout_name)
new_layout.save()
return new_layout
Expand All @@ -285,5 +284,12 @@ def save(self):
:return: None
"""

with open(join(Layout.LAYOUTS_DIR, self.name), 'wb') as file:
layouts_dir = get_layouts_dir()
if not os.path.exists(layouts_dir):
os.makedirs(layouts_dir)
with open(join(layouts_dir, self.name), 'wb') as file:
pickle.dump(self, file)


def get_layouts_dir():
return os.path.join(config.RESOURCES_DIR, 'layouts', config.bot.module_name)
2 changes: 1 addition & 1 deletion src/routine/routine.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ def load(self, file=None):
config.layout = Layout.load(file)
config.gui.view.status.set_routine(basename(file))
config.gui.edit.minimap.draw_default()
print(f"[~] Finished loading routine '{basename(splitext(file)[0])}'.")
print(f" ~ Finished loading routine '{basename(splitext(file)[0])}'.")

def compile(self, file):
self.labels = {}
Expand Down

0 comments on commit 1a46a96

Please sign in to comment.