Skip to content

Commit 1a46a96

Browse files
authored
Merge pull request #86 from tanjeffreyz/dev
Separating resources by command book name to avoid collisions
2 parents b70fee0 + 57f4db1 commit 1a46a96

File tree

6 files changed

+40
-14
lines changed

6 files changed

+40
-14
lines changed

src/gui/menu/file.py

+18-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import tkinter as tk
23
from src.common import config, utils
34
from src.gui.interfaces import MenuBarItem
45
from tkinter.filedialog import askopenfilename, asksaveasfilename
@@ -14,7 +15,14 @@ def __init__(self, parent, **kwargs):
1415
self.add_command(label='Save Routine', command=utils.async_callback(self, File._save_routine))
1516
self.add_separator()
1617
self.add_command(label='Load Command Book', command=utils.async_callback(self, File._load_commands))
17-
self.add_command(label='Load Routine', command=utils.async_callback(self, File._load_routine))
18+
self.add_command(
19+
label='Load Routine',
20+
command=utils.async_callback(self, File._load_routine),
21+
state=tk.DISABLED
22+
)
23+
24+
def enable_routine_state(self):
25+
self.entryconfig('Load Routine', state=tk.NORMAL)
1826

1927
@staticmethod
2028
@utils.run_if_disabled('\n[!] Cannot create a new routine while Auto Maple is enabled')
@@ -30,7 +38,7 @@ def _new_routine():
3038
@staticmethod
3139
@utils.run_if_disabled('\n[!] Cannot save routines while Auto Maple is enabled')
3240
def _save_routine():
33-
file_path = asksaveasfilename(initialdir=os.path.join(config.RESOURCES_DIR, 'routines'),
41+
file_path = asksaveasfilename(initialdir=get_routines_dir(),
3442
title='Save routine',
3543
filetypes=[('*.csv', '*.csv')],
3644
defaultextension='*.csv')
@@ -46,7 +54,7 @@ def _load_routine():
4654
'Would you like to proceed anyways?',
4755
icon='warning'):
4856
return
49-
file_path = askopenfilename(initialdir=os.path.join(config.RESOURCES_DIR, 'routines'),
57+
file_path = askopenfilename(initialdir=get_routines_dir(),
5058
title='Select a routine',
5159
filetypes=[('*.csv', '*.csv')])
5260
if file_path:
@@ -66,3 +74,10 @@ def _load_commands():
6674
filetypes=[('*.py', '*.py')])
6775
if file_path:
6876
config.bot.load_commands(file_path)
77+
78+
79+
def get_routines_dir():
80+
target = os.path.join(config.RESOURCES_DIR, 'routines', config.bot.module_name)
81+
if not os.path.exists(target):
82+
os.makedirs(target)
83+
return target

src/modules/bot.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def __init__(self):
3737
self.rune_pos = (0, 0)
3838
self.rune_closest_pos = (0, 0) # Location of the Point closest to rune
3939
self.submodules = {}
40+
self.module_name = None
4041
self.buff = components.Buff()
4142

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

167168
# Import the desired command book file
168169
module_name = splitext(basename(file))[0]
169-
module = __import__(f'resources.command_books.{module_name}', fromlist=[''])
170+
target = '.'.join(['resources', 'command_books', module_name])
171+
module = __import__(target, fromlist=[''])
170172

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

199201
if not step_found and not movement_found:
200-
print(f" ! Error: Must either implement both the 'move' and 'adjust' commands, "
202+
print(f" ! Error: Must either implement both 'Move' and 'Adjust' commands, "
201203
f"or the function 'step'.")
202204
if required_found and (step_found or movement_found):
205+
self.module_name = module_name
203206
self.command_book = new_cb
204207
self.buff = new_cb['buff']()
205208
components.step = new_step
209+
config.gui.menu.file.enable_routine_state()
206210
config.gui.view.status.set_cb(basename(file))
207211
config.routine.clear()
208-
print(f"[~] Successfully loaded command book '{module_name}'.")
212+
print(f" ~ Successfully loaded command book '{module_name}'.")
209213
return True
210214
else:
211-
print(f"[!] Command book '{module_name}' was not loaded.")
215+
print(f" ! Command book '{module_name}' was not loaded.")
212216
return False
213217

214218
def update_submodules(self, force=False):

src/modules/gui.py

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ def __init__(self):
4040

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

4445
def set_routine(self, arr):
4546
self.routine_var.set(arr)

src/routine/layout.py

+11-5
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ def __iter__(self):
6464
class Layout:
6565
"""Uses a quadtree to represent possible player positions in a map layout."""
6666

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

7069
def __init__(self, name):
@@ -266,13 +265,13 @@ def load(routine):
266265
"""
267266

268267
layout_name = splitext(basename(routine))[0]
269-
target = f'{Layout.LAYOUTS_DIR}/{layout_name}'
268+
target = os.path.join(get_layouts_dir(), layout_name)
270269
if isfile(target):
271-
print(f" ~ Found existing Layout file at '{target}'.")
270+
print(f" - Found existing Layout file at '{target}'.")
272271
with open(target, 'rb') as file:
273272
return pickle.load(file)
274273
else:
275-
print(f" ~ Created new Layout file at '{target}'.")
274+
print(f" - Created new Layout file at '{target}'.")
276275
new_layout = Layout(layout_name)
277276
new_layout.save()
278277
return new_layout
@@ -285,5 +284,12 @@ def save(self):
285284
:return: None
286285
"""
287286

288-
with open(join(Layout.LAYOUTS_DIR, self.name), 'wb') as file:
287+
layouts_dir = get_layouts_dir()
288+
if not os.path.exists(layouts_dir):
289+
os.makedirs(layouts_dir)
290+
with open(join(layouts_dir, self.name), 'wb') as file:
289291
pickle.dump(self, file)
292+
293+
294+
def get_layouts_dir():
295+
return os.path.join(config.RESOURCES_DIR, 'layouts', config.bot.module_name)

src/routine/routine.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ def load(self, file=None):
226226
config.layout = Layout.load(file)
227227
config.gui.view.status.set_routine(basename(file))
228228
config.gui.edit.minimap.draw_default()
229-
print(f"[~] Finished loading routine '{basename(splitext(file)[0])}'.")
229+
print(f" ~ Finished loading routine '{basename(splitext(file)[0])}'.")
230230

231231
def compile(self, file):
232232
self.labels = {}

0 commit comments

Comments
 (0)