Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import json
- import shutil
- import tkinter as tk
- from tkinter import filedialog, messagebox, simpledialog
- from tkinter import ttk
- import configparser
- # Define paths for configuration and directories
- CONFIG_FILE = os.path.join(os.getcwd(), 'installer_config.ini')
- APICO_MOD_DIRECTORY = os.path.expandvars(r'%APPDATA%\APICO\mods')
- APICO_SETTINGS_FILE = os.path.expandvars(r'%APPDATA%\APICO\settings.json')
- LOCAL_MOD_DIRECTORY = os.path.join(os.getcwd(), 'mods')
- FILES_DIRECTORY = os.path.join(os.getcwd(), 'files')
- class ModInstaller:
- def __init__(self, root):
- self.root = root
- self.root.title("APICO Mod Installer")
- self.root.geometry("600x500")
- self.root.configure(bg='#2c3e50')
- # Paths for mod installation
- self.mod_directory = None
- self.apico_mod_directory = APICO_MOD_DIRECTORY
- self.apico_settings_file = APICO_SETTINGS_FILE
- # Load configuration from file
- self.config = configparser.ConfigParser()
- self.load_config()
- # Configure UI styles
- style = ttk.Style()
- style.configure("TLabel", background='#2c3e50', foreground='#ecf0f1', font=("Arial", 12))
- style.configure("TButton", font=("Arial", 10), padding=5)
- style.configure("TFrame", background='#2c3e50')
- # Create main frame for UI elements
- main_frame = ttk.Frame(root)
- main_frame.pack(expand=True, fill=tk.BOTH, padx=20, pady=20)
- # UI Elements
- self.label = ttk.Label(main_frame, text="Select the mod folder containing your files:")
- self.label.pack(pady=10)
- # Buttons for browsing APICO directories
- self.browse_mods_folder_button = ttk.Button(main_frame, text="Browse APICO Mods Folder", command=self.browse_apico_mods_folder)
- self.browse_mods_folder_button.pack(pady=5)
- self.browse_settings_file_button = ttk.Button(main_frame, text="Browse APICO Settings File", command=self.browse_apico_settings_file)
- self.browse_settings_file_button.pack(pady=5)
- # Button to create a new mod
- self.create_mod_button = ttk.Button(main_frame, text="Create New Mod", command=self.create_new_mod)
- self.create_mod_button.pack(pady=10)
- # Button to install selected mod
- self.install_button = ttk.Button(main_frame, text="Install Mod", command=self.install_mod, state=tk.DISABLED)
- self.install_button.pack(pady=20)
- # Listbox to display available mods
- self.mod_listbox = tk.Listbox(main_frame, bg='#34495e', fg='#ecf0f1', selectbackground='#16a085', font=("Arial", 10))
- self.mod_listbox.pack(fill=tk.BOTH, expand=True, pady=10)
- self.mod_listbox.bind('<<ListboxSelect>>', self.on_mod_select)
- # Ensure 'mods' directory exists
- if not os.path.exists(LOCAL_MOD_DIRECTORY):
- os.makedirs(LOCAL_MOD_DIRECTORY)
- else:
- self.list_local_mods()
- # Update button states
- self.update_install_button_state()
- def load_config(self):
- """Load configuration file."""
- if os.path.exists(CONFIG_FILE):
- self.config.read(CONFIG_FILE)
- if 'Paths' in self.config:
- self.apico_mod_directory = self.config['Paths'].get('apico_mod_directory', APICO_MOD_DIRECTORY)
- self.apico_settings_file = self.config['Paths'].get('apico_settings_file', APICO_SETTINGS_FILE)
- def save_config(self):
- """Save updated configuration file."""
- if 'Paths' not in self.config:
- self.config['Paths'] = {}
- self.config['Paths']['apico_mod_directory'] = self.apico_mod_directory
- self.config['Paths']['apico_settings_file'] = self.apico_settings_file
- with open(CONFIG_FILE, 'w') as configfile:
- self.config.write(configfile)
- def list_local_mods(self):
- """List available mods in the local 'mods' directory."""
- local_mods = os.listdir(LOCAL_MOD_DIRECTORY)
- self.mod_listbox.delete(0, tk.END) # Clear previous entries
- if local_mods:
- for mod in local_mods:
- self.mod_listbox.insert(tk.END, mod)
- else:
- self.mod_listbox.insert(tk.END, "No mods found in the local 'mods' folder.")
- def browse_apico_mods_folder(self):
- """Allow user to select APICO mods folder."""
- selected_directory = filedialog.askdirectory()
- if selected_directory:
- self.apico_mod_directory = selected_directory
- self.save_config()
- messagebox.showinfo("APICO Mods Folder Selected", f"APICO Mods Folder set to: {self.apico_mod_directory}")
- self.update_install_button_state()
- def browse_apico_settings_file(self):
- """Allow user to select APICO settings file."""
- selected_file = filedialog.askopenfilename(filetypes=[("JSON files", "*.json")])
- if selected_file:
- self.apico_settings_file = selected_file
- self.save_config()
- messagebox.showinfo("APICO Settings File Selected", f"APICO Settings File set to: {self.apico_settings_file}")
- self.update_install_button_state()
- def on_mod_select(self, event):
- """Handle mod selection event."""
- selected_indices = self.mod_listbox.curselection()
- if selected_indices:
- self.mod_directory = os.path.join(LOCAL_MOD_DIRECTORY, self.mod_listbox.get(selected_indices[0]))
- else:
- self.mod_directory = None
- self.update_install_button_state()
- def update_install_button_state(self):
- """Enable or disable install button based on selection."""
- if self.apico_mod_directory and self.apico_settings_file and self.mod_directory:
- self.install_button.config(state=tk.NORMAL)
- else:
- self.install_button.config(state=tk.DISABLED)
- def install_mod(self):
- """Install the selected mod by copying files to APICO mod directory."""
- if not self.mod_directory:
- messagebox.showerror("Error", "Please select a mod first.")
- return
- try:
- mod_name = os.path.basename(os.path.normpath(self.mod_directory))
- target_mod_path = os.path.join(self.apico_mod_directory, mod_name)
- # Remove existing mod folder if it exists
- if os.path.exists(target_mod_path):
- shutil.rmtree(target_mod_path)
- shutil.copytree(self.mod_directory, target_mod_path)
- messagebox.showinfo("Success", f"Mod '{mod_name}' successfully installed.")
- # Update settings.json
- self.update_settings(mod_name)
- except Exception as e:
- messagebox.showerror("Error", f"An error occurred while installing the mod: {str(e)}")
- def update_settings(self, mod_name):
- """Update APICO settings file to register the installed mod."""
- if not os.path.exists(self.apico_settings_file):
- messagebox.showerror("Error", "APICO settings.json not found.")
- return
- try:
- with open(self.apico_settings_file, 'r') as f:
- settings = json.load(f)
- settings.setdefault('downloaded_mods', []).append(mod_name)
- settings.setdefault('active_mods', []).append(mod_name)
- with open(self.apico_settings_file, 'w') as f:
- json.dump(settings, f, indent=4)
- messagebox.showinfo("Success", f"Mod '{mod_name}' added to APICO settings.")
- except Exception as e:
- messagebox.showerror("Error", f"Error updating settings: {str(e)}")
- # Run application
- if __name__ == "__main__":
- root = tk.Tk()
- installer = ModInstaller(root)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement