From 0371ec9133f2cfb18e538e0dca581d00779e370c Mon Sep 17 00:00:00 2001 From: faroukbmiled Date: Sat, 23 Sep 2023 23:45:01 +0100 Subject: [PATCH] fixe issues --- boiiiwd_package/src/library_tab.py | 41 +++++++++++-- boiiiwd_package/src/settings_tab.py | 93 +++++++++++++++++++---------- 2 files changed, 95 insertions(+), 39 deletions(-) diff --git a/boiiiwd_package/src/library_tab.py b/boiiiwd_package/src/library_tab.py index 90828c0..544de77 100644 --- a/boiiiwd_package/src/library_tab.py +++ b/boiiiwd_package/src/library_tab.py @@ -183,6 +183,27 @@ class LibraryTab(ctk.CTkScrollableFrame): with open(file, 'w') as file: json.dump(cleaned_items, file, indent=4) + def is_valid_json_format(self, file): + try: + with open(file, "r") as file: + data = json.load(file) + if not isinstance(data, list): + return False + + for item in data: + if not all(key in item for key in ("id", "text", "date", "folder_name", "json_folder_name")): + return False + + return True + except: + return False + + def rename_invalid_json_file(self, file_path): + timestamp = datetime.now().strftime("%Y%m%d%H%M%S") + base_name, ext = os.path.splitext(file_path) + new_file_path = f"{base_name}_{timestamp}{ext}" + os.rename(file_path, new_file_path) + def filter_items(self, event): filter_text = self.filter_entry.get().lower() for label, button, button_view_list in zip(self.label_list, self.button_list, self.button_view_list): @@ -202,6 +223,12 @@ class LibraryTab(ctk.CTkScrollableFrame): self.refresh_items() if dont_add: + self.label_list.clear() + self.button_list.clear() + self.button_view_list.clear() + self.added_items.clear() + self.added_folders.clear() + self.ids_added.clear() self.refresh_next_time = True maps_folder = Path(boiiiFolder) / "mods" @@ -217,6 +244,8 @@ class LibraryTab(ctk.CTkScrollableFrame): folders_to_process = [mods_folder, maps_folder] items_file = os.path.join(application_path, LIBRARY_FILE) + if not self.is_valid_json_format(items_file): + self.rename_invalid_json_file(items_file) for folder_path in folders_to_process: for zone_path in folder_path.glob("**/zone"): @@ -241,13 +270,13 @@ class LibraryTab(ctk.CTkScrollableFrame): creation_timestamp = None for ff_file in zone_path.glob("*.ff"): if ff_file.exists(): - creation_timestamp = ff_file.stat().st_ctime + creation_timestamp = ff_file.stat().st_mtime break if creation_timestamp is not None: date_added = datetime.fromtimestamp(creation_timestamp).strftime("%d %b, %Y @ %I:%M%p") else: - creation_timestamp = zone_path.stat().st_ctime + creation_timestamp = zone_path.stat().st_mtime date_added = datetime.fromtimestamp(creation_timestamp).strftime("%d %b, %Y @ %I:%M%p") map_count += 1 if item_type == "map" else 0 @@ -347,13 +376,13 @@ class LibraryTab(ctk.CTkScrollableFrame): creation_timestamp = None for ff_file in zone_path.glob("*.ff"): if ff_file.exists(): - creation_timestamp = ff_file.stat().st_ctime + creation_timestamp = ff_file.stat().st_mtime break if creation_timestamp is not None: date_added = datetime.fromtimestamp(creation_timestamp).strftime("%d %b, %Y @ %I:%M%p") else: - creation_timestamp = zone_path.stat().st_ctime + creation_timestamp = zone_path.stat().st_mtime date_added = datetime.fromtimestamp(creation_timestamp).strftime("%d %b, %Y @ %I:%M%p") items_file = os.path.join(application_path, LIBRARY_FILE) @@ -515,10 +544,10 @@ class LibraryTab(ctk.CTkScrollableFrame): creation_timestamp = None for ff_file in json_path.parent.glob("*.ff"): if ff_file.exists(): - creation_timestamp = ff_file.stat().st_ctime + creation_timestamp = ff_file.stat().st_mtime break if not creation_timestamp: - creation_timestamp = json_path.parent.stat().st_ctime + creation_timestamp = json_path.parent.stat().st_mtime if json_path.exists(): workshop_id = extract_json_data(json_path, "PublisherID") or "None" diff --git a/boiiiwd_package/src/settings_tab.py b/boiiiwd_package/src/settings_tab.py index 723e4b1..45c4b17 100644 --- a/boiiiwd_package/src/settings_tab.py +++ b/boiiiwd_package/src/settings_tab.py @@ -183,29 +183,31 @@ class SettingsTab(ctk.CTkFrame): return def theme_options_func(self, option: str): - if option == "Default": - self.boiiiwd_custom_theme(disable_only=True) - save_config("theme", "boiiiwd_theme.json") - if option == "Blue": - self.boiiiwd_custom_theme(disable_only=True) - save_config("theme", "boiiiwd_blue.json") - if option == "Grey": - self.boiiiwd_custom_theme(disable_only=True) - save_config("theme", "boiiiwd_grey.json") - if option == "Ghost": - self.boiiiwd_custom_theme(disable_only=True) - save_config("theme", "boiiiwd_ghost.json") - if option == "Obsidian": - self.boiiiwd_custom_theme(disable_only=True) - save_config("theme", "boiiiwd_obsidian.json") - if option == "NeonBanana": - self.boiiiwd_custom_theme(disable_only=True) - save_config("theme", "boiiiwd_neonbanana.json") + theme_mapping = { + "Default": "boiiiwd_theme.json", + "Blue": "boiiiwd_blue.json", + "Grey": "boiiiwd_grey.json", + "Ghost": "boiiiwd_ghost.json", + "Obsidian": "boiiiwd_obsidian.json", + "NeonBanana": "boiiiwd_neonbanana.json", + "Custom": "boiiiwd_theme.json", + } + + theme_file = theme_mapping.get(option) + if option == "Custom": self.boiiiwd_custom_theme() save_config("theme", "boiiiwd_theme.json") - if not option == "Custom": - if show_message("Restart to take effect!", f"{option} theme has been set ,please restart to take effect", icon="info", _return=True, option_1="Ok", option_2="Restart"): + return + + if theme_file: + self.boiiiwd_custom_theme(disable_only=True) + save_config("theme", theme_file) + else: + return + + if option != "Custom": + if show_message("Restart to take effect!", f"{option} theme has been set, please restart to take effect", icon="info", _return=True, option_1="Ok", option_2="Restart"): try: p = psutil.Process(os.getpid()) for handler in p.open_files() + p.connections(): @@ -213,9 +215,7 @@ class SettingsTab(ctk.CTkFrame): except Exception: pass python = sys.executable - os.execl(python, python, *sys.argv) - else: - pass + os.execl(python, python, *sys.argv) def enable_save_button(self, *args): try: self.save_button.configure(state='normal') @@ -396,7 +396,19 @@ class SettingsTab(ctk.CTkFrame): boiiiFolder = main_app.app.edit_destination_folder.get() maps_folder = os.path.join(boiiiFolder, "mods") mods_folder = os.path.join(boiiiFolder, "usermaps") - folders_to_process = [mods_folder, maps_folder] + + folders_to_process = [] + + if os.path.exists(mods_folder): + folders_to_process.append(mods_folder) + + if os.path.exists(maps_folder): + folders_to_process.append(maps_folder) + + if not os.path.exists(maps_folder) and not os.path.exists(mods_folder): + show_message("Warning -> Check boiii path", f"You don't have any items yet ,from now on item's folders will be named as their {option}") + return 0 + processed_names = set() for folder_path in folders_to_process: for folder_name in os.listdir(folder_path): @@ -426,6 +438,8 @@ class SettingsTab(ctk.CTkFrame): os.rename(folder_to_rename, new_path) processed_names.add(new_folder_name) + return 1 + def change_folder_naming(self, option): main_app.app.title("BOIII Workshop Downloader - Settings ➜ Loading... ⏳") try: @@ -434,9 +448,13 @@ class SettingsTab(ctk.CTkFrame): if not "No items" in lib: if show_message("Renaming", "Would you like to rename all your exisiting item folders now?", _return=True): main_app.app.title("BOIII Workshop Downloader - Settings ➜ Renaming... ⏳") - try :self.rename_all_folders(option) + try : ren_return = self.rename_all_folders(option) except Exception as er: show_message("Error!", f"Error occured when renaming\n{er}"); return - show_message("Done!", "All folders have been renamed", icon="info") + if ren_return == 0: + return 0 + else: + show_message("Done!", "All folders have been renamed", icon="info") + main_app.app.library_tab.load_items(main_app.app.edit_destination_folder.get(), dont_add=True) else: show_message("Heads up!", "Only newly downloaded items will be affected", icon="info") else: @@ -593,11 +611,12 @@ class SettingsTab(ctk.CTkFrame): show_message("No items found", f"No items found in \n{map_folder}") return - for i, workshop_id in enumerate(subfolders, start=1): - json_file_path = os.path.join(map_folder, workshop_id, "workshop.json") + for i, dir_name in enumerate(subfolders, start=1): + json_file_path = os.path.join(map_folder, dir_name, "zone", "workshop.json") copy_button.configure(text=f"Working on -> {i}/{total_folders}") if os.path.exists(json_file_path): + workshop_id = extract_json_data(json_file_path, "PublisherID") mod_type = extract_json_data(json_file_path, "Type") items_file = os.path.join(application_path, LIBRARY_FILE) item_exixsts = main_app.app.library_tab.item_exists_in_file(items_file, workshop_id) @@ -619,10 +638,10 @@ class SettingsTab(ctk.CTkFrame): if mod_type == "mod": path_folder = os.path.join(boiii_folder, "mods") - folder_name_path = os.path.join(path_folder, folder_name, "zone") + folder_name_path = os.path.join(path_folder, folder_name) elif mod_type == "map": path_folder = os.path.join(boiii_folder, "usermaps") - folder_name_path = os.path.join(path_folder, folder_name, "zone") + folder_name_path = os.path.join(path_folder, folder_name) else: show_message("Error", "Invalid workshop type in workshop.json, are you sure this is a map or a mod?.", icon="cancel") continue @@ -630,22 +649,30 @@ class SettingsTab(ctk.CTkFrame): if not item_exixsts: while os.path.exists(os.path.join(path_folder, folder_name)): folder_name += f"_{workshop_id}" - folder_name_path = os.path.join(path_folder, folder_name, "zone") + folder_name_path = os.path.join(path_folder, folder_name) os.makedirs(folder_name_path, exist_ok=True) try: - copy_with_progress(os.path.join(map_folder, workshop_id), folder_name_path) + copy_with_progress(os.path.join(map_folder, dir_name), folder_name_path) except Exception as E: show_message("Error", f"Error copying files: {E}", icon="cancel") continue if cut_var.get(): - remove_tree(os.path.join(map_folder, workshop_id)) + remove_tree(os.path.join(map_folder, dir_name)) main_app.app.library_tab.update_item(main_app.app.edit_destination_folder.get(), workshop_id, mod_type, folder_name) + else: + # if its last folder to check + if i == total_folders: + show_message("Error", f"workshop.json not found in {dir_name}", icon="cancel") + main_app.app.library_tab.load_items(main_app.app.edit_destination_folder.get(), dont_add=True) + return + continue if subfolders: + main_app.app.library_tab.load_items(main_app.app.edit_destination_folder.get(), dont_add=True) main_app.app.show_complete_message(message=f"All items were moved\nYou can run the game now!\nPS: You have to restart the game\n(pressing launch will launch/restarts)") finally: