From 95e85dd57aa6318d29c8297b8454393d6cc55e73 Mon Sep 17 00:00:00 2001 From: Jaka Date: Mon, 11 Oct 2021 19:05:02 +0200 Subject: [PATCH] Added the custom naming option, included the license information in the sources. --- deploy.py | 13 +-- main.py | 116 ++++++++++++++++----- source/filelist_template.xml | 73 ++++++------- source/sunken_tapes_style_ita_template.xml | 7 +- source/sunken_tapes_style_template.xml | 7 +- source/sunken_tapes_template.xml | 20 ++-- 6 files changed, 153 insertions(+), 83 deletions(-) diff --git a/deploy.py b/deploy.py index f4effaa..5ac176c 100644 --- a/deploy.py +++ b/deploy.py @@ -210,13 +210,13 @@ def build_xml_code(data, config): # render the template output_file.write(template0.render(config=config)) - with open("./build/sunken_tapes.xml", "w+", encoding="utf8") as output_file: + with open(f"./build/{config['slug']}.xml", "w+", encoding="utf8") as output_file: # render the template output_file.write(template1.render(tapes=data, config=config, condition_delta=condition_delta, affliction_delta=affliction_delta)) - with open("./build/sunken_tapes_style.xml", "w+", encoding="utf8") as output_file: + with open(f"./build/{config['slug']}_style.xml", "w+", encoding="utf8") as output_file: # render the template output_file.write(template2.render(tapes=data, config=config)) @@ -225,6 +225,10 @@ def deploy(config): try: os.mkdir('./build') except FileExistsError: + logging.info(f"removing old XML files in ./build/:") + for f in Path('./build/').glob("*.xml"): + logging.info(f" {f}") + os.remove(f) pass logging.info("Reading tapes.yaml") @@ -238,10 +242,7 @@ def deploy(config): prepare_images(data, config) build_xml_code(data, config) - if config["override_workshop"]: - mod_directory = "/Mods/Sunken Tapes/" - else: - mod_directory = "/Mods/Sunken Tapez/" + mod_directory = f"/Mods/{config['slug']}/" logging.info(f"removing the old installed mod directory {config['installdir'] + mod_directory}") rmfulldir(config["installdir"] + mod_directory) diff --git a/main.py b/main.py index 8201d37..04c753c 100644 --- a/main.py +++ b/main.py @@ -8,6 +8,25 @@ from pathlib import Path from deploy import download_ffmpeg, download_git, deploy, get_ffmpeg_version, update import logging import sys +import re +import unicodedata + + +def slugify(value, allow_unicode=False): + """ + Taken from https://github.com/django/django/blob/master/django/utils/text.py + Convert to ASCII if 'allow_unicode' is False. Convert spaces or repeated + dashes to single dashes. Remove characters that aren't alphanumerics, + underscores, or hyphens. Convert to lowercase. Also strip leading and + trailing whitespace, dashes, and underscores. + """ + value = str(value) + if allow_unicode: + value = unicodedata.normalize('NFKC', value) + else: + value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore').decode('ascii') + value = re.sub(r'[^\w\s-]', '', value.lower()) + return re.sub(r'[-\s]+', '-', value).strip('-_') def get_curr_screen_geometry(): @@ -28,6 +47,63 @@ def get_curr_screen_geometry(): return geometry +def create_name_frame(container): + frame = ttk.LabelFrame(container, text='Mod Name') + + # Override Workshop checkbox + override_workshop = tk.StringVar() + override_workshop.set("0") + override_workshop_check = ttk.Checkbutton( + frame, + variable=override_workshop, + text='Override Workshop', + command=lambda: override_action()) + + override_workshop_check.pack(side="left", padx=3, pady=3) + + name = tk.StringVar() + name.set("Sunken Tapez") + + name_entry = ttk.Entry(frame, width=30, textvariable=name) + name_entry.pack(side="left", padx=3, pady=3, expand=True, fill="x") + + slug = tk.StringVar() + slug.set(slugify("Sunken Tapez")) + + slug_entry = ttk.Entry(frame, width=30, textvariable=slug) + slug_entry["state"] = "disable" + slug_entry.pack(side="left", padx=3, pady=3, expand=True, fill="x") + + Hovertip(override_workshop_check, + 'Keep this unchecked to prevent Steam Workshop' + '\noverriding your custom installation.' + '\n\nIt exists only for the author\'s convenience.') + + Hovertip(name_entry, + 'Name of the deploy mod. You can customise this' + '\nif you want to create your own version.') + + Hovertip(slug_entry, + 'Slug of the deployed mod name that is' + '\nused for file paths and references.') + + def update_slug(*args): + slug.set(slugify(name.get())) + + name.trace("w", update_slug) + + def override_action(): + if override_workshop.get() == "1": + name.set("Sunken Tapes") + name_entry["state"] = "disable" + slug.set("Sunken Tapes") + elif override_workshop.get() == "0": + name.set("Sunken Tapez") + name_entry["state"] = "normal" + + return frame, name, slug + + def create_install_frame(container): def find_barotrauma_directory(): first_guess = Path("C:/Program Files (x86)/Steam/steamapps/common/Barotrauma") @@ -83,15 +159,6 @@ def create_options_frame(container): text='Buffs', command=lambda: print(buffs.get())) - # Override Workshop checkbox - override_workshop = tk.StringVar() - override_workshop.set("0") - override_workshop_check = ttk.Checkbutton( - frame, - variable=override_workshop, - text='Override Workshop', - command=lambda: print(override_workshop.get())) - Hovertip(use_ita_check, 'Check this box if you use Into The Abyss mod.' '\nITA and Sunken Tapes override the same style file.') @@ -100,14 +167,10 @@ def create_options_frame(container): 'Check this box to enable some songs causing strange effects' '\nThis is the intended default behaviour.') - Hovertip(override_workshop_check, - 'Keep this unchecked to prevent Steam Workshop' - '\noverriding your custom installation.') - for widget in frame.winfo_children(): widget.pack(side="top", padx=3, pady=3, fill="x") - return frame, use_ita, buffs, override_workshop + return frame, use_ita, buffs def create_resolution_frame(container): @@ -141,24 +204,24 @@ def create_deploy_frame(container, config): config_values = {"installdir": config["installdir"].get(), "use_ita": config["use_ita"].get() == "1", "buffs": config["buffs"].get() == "1", - "override_workshop": config["override_workshop"].get() == "1", + "name": config["name"].get(), + "slug": config["slug"].get(), "resolution_x": int(config["resolution_x"].get()), "resolution_y": int(config["resolution_y"].get())} logging.info(f"deploying with config: {config_values}") deploy(config_values) - if config_values["override_workshop"]: - mod_name = "Sunken Tapes" - note = "" + if config_values["name"] != "Sunken Tapes": + note = f'\n\nThis is a custom version and is named {config_values["name"]} to differentiate it from ' \ + + 'the Steam Workshop version that would overwrite it otherwise.' \ + + '\n\nPlease include a link to the original mod if you publish it to Steam Workshop.' else: - mod_name = "Sunken Tapez" - note = '\n\nThis is a custom version and is named with Z (Sunken Tapez) to differentiate it from ' \ - + 'the Steam Workshop version that would overwrite it otherwise.' + note = "" showinfo(title='Success!', - message=f'{mod_name} was successfully installed to:' - f'\n{config["installdir"].get()}' + message=f'{config_values["name"]} was successfully installed to:' + f'\n\n{config_values["installdir"]}/Mods/{config_values["slug"]}' f'\n\nThis installer will now close.' f'{note}') @@ -246,19 +309,22 @@ def create_main_window(): root.title('Sunken Tapes') middle_frame = ttk.Frame(root) + name_frame, name, slug = create_name_frame(root) install_frame, install_dir = create_install_frame(root) - options_frame, use_ita, buffs, override_workshop = create_options_frame(middle_frame) + options_frame, use_ita, buffs = create_options_frame(middle_frame) resolution_frame, width, height = create_resolution_frame(middle_frame) config = {"installdir": install_dir, "use_ita": use_ita, "buffs": buffs, - "override_workshop": override_workshop, + "name": name, + "slug": slug, "resolution_x": width, "resolution_y": height} deploy_frame = create_deploy_frame(root, config) + name_frame.pack(side="top", fill="x", pady=3, padx=3) install_frame.pack(side="top", fill="x", pady=3, padx=3) options_frame.pack(side="left", fill="y", pady=3, padx=3) resolution_frame.pack(side="left", fill="both", pady=3, padx=3, expand=True) diff --git a/source/filelist_template.xml b/source/filelist_template.xml index 072db81..4f39de6 100644 --- a/source/filelist_template.xml +++ b/source/filelist_template.xml @@ -1,38 +1,39 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +{% if config['slug'] != "Sunken Tapes" %}{% else %}{% endif %} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/source/sunken_tapes_style_ita_template.xml b/source/sunken_tapes_style_ita_template.xml index 3a66ff0..2f059f8 100644 --- a/source/sunken_tapes_style_ita_template.xml +++ b/source/sunken_tapes_style_ita_template.xml @@ -1,4 +1,5 @@ +{% if config['slug'] != "Sunken Tapes" %}{% else %}{% endif %}