Advertisement
davedumas0

APICO_dev_mod_installer

Nov 15th, 2024 (edited)
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.70 KB | None | 0 0
  1. import os
  2. import json
  3. import shutil
  4. import tkinter as tk
  5. from tkinter import filedialog, messagebox, simpledialog
  6. from tkinter import ttk
  7. import configparser
  8.  
  9. # Define paths for configuration and directories
  10. CONFIG_FILE = os.path.join(os.getcwd(), 'installer_config.ini')
  11. APICO_MOD_DIRECTORY = os.path.expandvars(r'%APPDATA%\APICO\mods')
  12. APICO_SETTINGS_FILE = os.path.expandvars(r'%APPDATA%\APICO\settings.json')
  13. LOCAL_MOD_DIRECTORY = os.path.join(os.getcwd(), 'mods')
  14. FILES_DIRECTORY = os.path.join(os.getcwd(), 'files')
  15.  
  16. class ModInstaller:
  17.     def __init__(self, root):
  18.         self.root = root
  19.         self.root.title("APICO Mod Installer")
  20.         self.root.geometry("600x500")
  21.         self.root.configure(bg='#2c3e50')
  22.  
  23.         # Paths for mod installation
  24.         self.mod_directory = None
  25.         self.apico_mod_directory = APICO_MOD_DIRECTORY
  26.         self.apico_settings_file = APICO_SETTINGS_FILE
  27.  
  28.         # Load configuration from file
  29.         self.config = configparser.ConfigParser()
  30.         self.load_config()
  31.  
  32.         # Configure UI styles
  33.         style = ttk.Style()
  34.         style.configure("TLabel", background='#2c3e50', foreground='#ecf0f1', font=("Arial", 12))
  35.         style.configure("TButton", font=("Arial", 10), padding=5)
  36.         style.configure("TFrame", background='#2c3e50')
  37.  
  38.         # Create main frame for UI elements
  39.         main_frame = ttk.Frame(root)
  40.         main_frame.pack(expand=True, fill=tk.BOTH, padx=20, pady=20)
  41.  
  42.         # UI Elements
  43.         self.label = ttk.Label(main_frame, text="Select the mod folder containing your files:")
  44.         self.label.pack(pady=10)
  45.  
  46.         # Buttons for browsing APICO directories
  47.         self.browse_mods_folder_button = ttk.Button(main_frame, text="Browse APICO Mods Folder", command=self.browse_apico_mods_folder)
  48.         self.browse_mods_folder_button.pack(pady=5)
  49.  
  50.         self.browse_settings_file_button = ttk.Button(main_frame, text="Browse APICO Settings File", command=self.browse_apico_settings_file)
  51.         self.browse_settings_file_button.pack(pady=5)
  52.  
  53.         # Button to create a new mod
  54.         self.create_mod_button = ttk.Button(main_frame, text="Create New Mod", command=self.create_new_mod)
  55.         self.create_mod_button.pack(pady=10)
  56.  
  57.         # Button to install selected mod
  58.         self.install_button = ttk.Button(main_frame, text="Install Mod", command=self.install_mod, state=tk.DISABLED)
  59.         self.install_button.pack(pady=20)
  60.  
  61.         # Listbox to display available mods
  62.         self.mod_listbox = tk.Listbox(main_frame, bg='#34495e', fg='#ecf0f1', selectbackground='#16a085', font=("Arial", 10))
  63.         self.mod_listbox.pack(fill=tk.BOTH, expand=True, pady=10)
  64.         self.mod_listbox.bind('<<ListboxSelect>>', self.on_mod_select)
  65.  
  66.         # Ensure 'mods' directory exists
  67.         if not os.path.exists(LOCAL_MOD_DIRECTORY):
  68.             os.makedirs(LOCAL_MOD_DIRECTORY)
  69.         else:
  70.             self.list_local_mods()
  71.  
  72.         # Update button states
  73.         self.update_install_button_state()
  74.  
  75.     def load_config(self):
  76.         """Load configuration file."""
  77.         if os.path.exists(CONFIG_FILE):
  78.             self.config.read(CONFIG_FILE)
  79.             if 'Paths' in self.config:
  80.                 self.apico_mod_directory = self.config['Paths'].get('apico_mod_directory', APICO_MOD_DIRECTORY)
  81.                 self.apico_settings_file = self.config['Paths'].get('apico_settings_file', APICO_SETTINGS_FILE)
  82.  
  83.     def save_config(self):
  84.         """Save updated configuration file."""
  85.         if 'Paths' not in self.config:
  86.             self.config['Paths'] = {}
  87.         self.config['Paths']['apico_mod_directory'] = self.apico_mod_directory
  88.         self.config['Paths']['apico_settings_file'] = self.apico_settings_file
  89.  
  90.         with open(CONFIG_FILE, 'w') as configfile:
  91.             self.config.write(configfile)
  92.  
  93.     def list_local_mods(self):
  94.         """List available mods in the local 'mods' directory."""
  95.         local_mods = os.listdir(LOCAL_MOD_DIRECTORY)
  96.         self.mod_listbox.delete(0, tk.END)  # Clear previous entries
  97.         if local_mods:
  98.             for mod in local_mods:
  99.                 self.mod_listbox.insert(tk.END, mod)
  100.         else:
  101.             self.mod_listbox.insert(tk.END, "No mods found in the local 'mods' folder.")
  102.  
  103.     def browse_apico_mods_folder(self):
  104.         """Allow user to select APICO mods folder."""
  105.         selected_directory = filedialog.askdirectory()
  106.         if selected_directory:
  107.             self.apico_mod_directory = selected_directory
  108.             self.save_config()
  109.             messagebox.showinfo("APICO Mods Folder Selected", f"APICO Mods Folder set to: {self.apico_mod_directory}")
  110.         self.update_install_button_state()
  111.  
  112.     def browse_apico_settings_file(self):
  113.         """Allow user to select APICO settings file."""
  114.         selected_file = filedialog.askopenfilename(filetypes=[("JSON files", "*.json")])
  115.         if selected_file:
  116.             self.apico_settings_file = selected_file
  117.             self.save_config()
  118.             messagebox.showinfo("APICO Settings File Selected", f"APICO Settings File set to: {self.apico_settings_file}")
  119.         self.update_install_button_state()
  120.  
  121.     def on_mod_select(self, event):
  122.         """Handle mod selection event."""
  123.         selected_indices = self.mod_listbox.curselection()
  124.         if selected_indices:
  125.             self.mod_directory = os.path.join(LOCAL_MOD_DIRECTORY, self.mod_listbox.get(selected_indices[0]))
  126.         else:
  127.             self.mod_directory = None
  128.         self.update_install_button_state()
  129.  
  130.     def update_install_button_state(self):
  131.         """Enable or disable install button based on selection."""
  132.         if self.apico_mod_directory and self.apico_settings_file and self.mod_directory:
  133.             self.install_button.config(state=tk.NORMAL)
  134.         else:
  135.             self.install_button.config(state=tk.DISABLED)
  136.  
  137.     def install_mod(self):
  138.         """Install the selected mod by copying files to APICO mod directory."""
  139.         if not self.mod_directory:
  140.             messagebox.showerror("Error", "Please select a mod first.")
  141.             return
  142.         try:
  143.             mod_name = os.path.basename(os.path.normpath(self.mod_directory))
  144.             target_mod_path = os.path.join(self.apico_mod_directory, mod_name)
  145.  
  146.             # Remove existing mod folder if it exists
  147.             if os.path.exists(target_mod_path):
  148.                 shutil.rmtree(target_mod_path)
  149.  
  150.             shutil.copytree(self.mod_directory, target_mod_path)
  151.             messagebox.showinfo("Success", f"Mod '{mod_name}' successfully installed.")
  152.  
  153.             # Update settings.json
  154.             self.update_settings(mod_name)
  155.         except Exception as e:
  156.             messagebox.showerror("Error", f"An error occurred while installing the mod: {str(e)}")
  157.  
  158.     def update_settings(self, mod_name):
  159.         """Update APICO settings file to register the installed mod."""
  160.         if not os.path.exists(self.apico_settings_file):
  161.             messagebox.showerror("Error", "APICO settings.json not found.")
  162.             return
  163.         try:
  164.             with open(self.apico_settings_file, 'r') as f:
  165.                 settings = json.load(f)
  166.             settings.setdefault('downloaded_mods', []).append(mod_name)
  167.             settings.setdefault('active_mods', []).append(mod_name)
  168.             with open(self.apico_settings_file, 'w') as f:
  169.                 json.dump(settings, f, indent=4)
  170.             messagebox.showinfo("Success", f"Mod '{mod_name}' added to APICO settings.")
  171.         except Exception as e:
  172.             messagebox.showerror("Error", f"Error updating settings: {str(e)}")
  173.  
  174. # Run application
  175. if __name__ == "__main__":
  176.     root = tk.Tk()
  177.     installer = ModInstaller(root)
  178.     root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement