Skip to content

Commit

Permalink
added a button to extract modpack with the .mkwf.pack extension
Browse files Browse the repository at this point in the history
  • Loading branch information
Faraphel committed Feb 9, 2022
1 parent 5dfaa35 commit 2e5c7ec
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 12 deletions.
2 changes: 2 additions & 0 deletions source/Common.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from source.Game import Game
from source.Gui.Main import Main
from source.Gui.TrackConfiguration import TrackConfiguration
from source.Gui.SelectPack import SelectPack
from source.Translation import Translator
from source.Error import ErrorLogger

Expand All @@ -27,4 +28,5 @@ def __init__(self):
self.gui_main = Main(common=self)

def show_gui_track_configuration(self): TrackConfiguration(common=self)
def show_gui_add_pack(self): SelectPack(common=self)
def mainloop(self): self.gui_main.mainloop()
35 changes: 26 additions & 9 deletions source/Gui/Main.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ def __init__(self, common) -> None:
self.common.translate("Error"),
self.common.translate("There is no pack in the ./Pack/ directory.")
)
self.quit()
#self.quit()

self.is_dev_version = False # Is this installer version a dev ?
self.is_track_configuration_edited = False
self.stringvar_ctconfig = StringVar(value=self.available_packs[0])
self.stringvar_ctconfig = StringVar(value=self.available_packs[0] if self.available_packs else None)
self.stringvar_language = StringVar(value=self.common.option.language)
self.stringvar_game_format = StringVar(value=self.common.option.format)
self.boolvar_dont_check_for_update = BooleanVar(value=self.common.option.dont_check_for_update)
Expand All @@ -60,9 +60,17 @@ def __init__(self, common) -> None:
textvariable=self.stringvar_ctconfig,
width=30
)
self.combobox_ctconfig_path.grid(row=1, column=1, sticky="NEWS", columnspan=2)
self.combobox_ctconfig_path.grid(row=1, column=1, sticky="NEWS")
self.combobox_ctconfig_path.bind("<<ComboboxSelected>>", lambda x=None: self.reload_ctconfig())
self.reload_ctconfig()

self.button_add_mod = Button(
self.frame_ctconfig,
text="+",
relief=RIDGE,
command=self.common.show_gui_add_pack,
width=2
)
self.button_add_mod.grid(row=1, column=2)

# Jeu
self.frame_game_path = LabelFrame(self.root, text=self.common.translate("Original game"))
Expand Down Expand Up @@ -249,10 +257,19 @@ def _func(init: bool = False):
self.menu_help.add_command(label="Github Wiki", command=lambda: webbrowser.open(GITHUB_HELP_PAGE_URL))
self.menu_help.add_command(label="Discord", command=lambda: webbrowser.open(DISCORD_URL))

self.reload_ctconfig()

def reload_ctconfig(self) -> None:
self.common.ct_config.load_ctconfig_file(
ctconfig_file=self.get_ctconfig_path_pack(self.stringvar_ctconfig.get())
)
self.available_packs = self.get_available_packs()
if self.available_packs: self.state_button(enable=True)
else: self.state_button(enable=False)

self.combobox_ctconfig_path.configure(values=self.available_packs)

if self.stringvar_ctconfig.get():
self.common.ct_config.load_ctconfig_file(
ctconfig_file=self.get_ctconfig_path_pack(self.stringvar_ctconfig.get())
)

@staticmethod
def get_available_packs() -> list:
Expand Down Expand Up @@ -352,9 +369,9 @@ def state_button(self, enable: bool = True) -> None:
:param enable: are the button enabled ?
"""
button = [
self.button_do_everything,
self.button_select_path,
self.combobox_ctconfig_path,
self.button_select_path,
self.button_do_everything
]
for widget in button:
if enable: widget.config(state=NORMAL)
Expand Down
78 changes: 76 additions & 2 deletions source/Gui/SelectPack.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,77 @@
from tkinter import *
from tkinter import ttk
from tkinter import filedialog
from tkinter import messagebox
import zipfile
import os


class SelectPack:
def __init__(self):
pass
def __init__(self, common):
self.common = common

self.root = Toplevel(self.common.gui_main.root)
self.root.title(self.common.translate("Add a pack"))
self.root.iconbitmap("./icon.ico")
self.root.resizable(False, False)
self.root.grab_set()

self.entry_modpack_path = Entry(self.root, width=50)
self.entry_modpack_path.grid(row=1, column=1, sticky="NEWS")

def select_path():
path = filedialog.askopenfilename(
filetypes=((self.common.translate("MKW Pack"), r"*.mkwf.pack"),)
)
if os.path.exists(path):
self.entry_modpack_path.delete(0, END)
self.entry_modpack_path.insert(0, path)

self.button_select_path = Button(
self.root,
text="...",
relief=RIDGE,
command=lambda: self.root.after(0, select_path)
)
self.button_select_path.grid(row=1, column=2)

def extract_pack():
self.progressbar_extract.grid(row=3, column=1, columnspan=2, sticky="NEWS")
try:
path = self.entry_modpack_path.get()
*packname, _, _ = os.path.basename(path).split(".")
packname = ".".join(packname)

with zipfile.ZipFile(path) as zip_pack:
zip_pack.extractall(f"./Pack/{packname}/")

self.common.gui_main.reload_ctconfig()

messagebox.showinfo(
self.common.translate("Extraction"),
self.common.translate("The mod have been extracted !")
)
self.root.destroy()

except Exception as e:
self.progressbar_extract.grid_forget()
raise e

self.button_extract_modpack = Button(
self.root,
text=self.common.translate("Extract the modpack"),
relief=RIDGE,
command=extract_pack
)
self.button_extract_modpack.grid(row=2, column=1, columnspan=2, sticky="NEWS")

self.progressbar_extract = ttk.Progressbar(self.root)
self.progressbar_extract.configure(mode="indeterminate")
self.progressbar_extract.start(50)

def state_button(self, enable=True):
for button in [
self.button_extract_modpack
]:
if enable: button.config(state=NORMAL)
else: button.config(state=DISABLED)
7 changes: 6 additions & 1 deletion translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@
"value2": "valeur2",
"Save track configuration": "Sauvegarder la configuration de course",
"track configuration": "configuration de course",
"Load track configuration": "Charger la configuration de course"
"Load track configuration": "Charger la configuration de course",
"Add a pack": "Ajouter un pack",
"Extract the modpack": "Extraire le modpack",
"MKW Pack": "Pack MKW",
"Extraction": "Extraction",
"The mod have been extracted !": "Le mod à bien été extrait !"
}
}

0 comments on commit 2e5c7ec

Please sign in to comment.