Guest User

main.py

a guest
Feb 16th, 2025
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.92 KB | None | 0 0
  1. import os
  2. import shutil
  3. import sys
  4. import tkinter as tk
  5. from tkinter import filedialog, messagebox
  6. import ctypes
  7.  
  8. def is_admin():
  9.     try:
  10.         return ctypes.windll.shell32.IsUserAnAdmin()
  11.     except:
  12.         return False
  13.  
  14. def run_as_admin():
  15.     ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, " ".join(sys.argv), None, 1)
  16.  
  17. def get_minecraft_folder(default_path):
  18.     root = tk.Tk()
  19.     root.withdraw()
  20.    
  21.     if messagebox.askyesno("Folder verification", f"Is this .minecraft folder correct for mods installation?\n\n{default_path}"):
  22.         return default_path
  23.    
  24.     folder_path = filedialog.askdirectory(initialdir=default_path, title="Select .minecraft folder")
  25.     return folder_path if folder_path else default_path
  26.  
  27. def install_files(minecraft_folder):
  28.     if messagebox.askyesno("Confirmation", "Do you want to proceed with file installation?\n\nThis will delete the files in your mods folder to add new ones"):
  29.         source_folder = resource_path('files_for_.minecraft_folder')
  30.        
  31.         for item in os.listdir(source_folder):
  32.             s = os.path.join(source_folder, item)
  33.             d = os.path.join(minecraft_folder, item)
  34.             if os.path.isdir(s):
  35.                 shutil.copytree(s, d, dirs_exist_ok=True)
  36.             else:
  37.                 shutil.copy2(s, d)
  38.  
  39.         activate_resource_packs(minecraft_folder)
  40.        
  41.         messagebox.showinfo("Success", "Files were installed successfully!\n\nYou can delete this file and launch Minecraft in the correct version.")
  42.     else:
  43.         messagebox.showinfo("Cancelled", "Installation was cancelled.")
  44.  
  45. def activate_resource_packs(minecraft_folder):
  46.     resourcepacks_folder = resource_path('files_for_.minecraft_folder/resourcepacks')
  47.     resource_packs = sorted(os.listdir(resourcepacks_folder))
  48.     cleaned_resource_packs = []
  49.     for pack in resource_packs:
  50.         cleaned_name = pack.replace('§', '_').replace(' ', '_')
  51.         cleaned_resource_packs.append(cleaned_name)
  52.     formatted_resource_packs = ','.join(f'"{pack}"' for pack in cleaned_resource_packs)
  53.     options_file_path = os.path.join(minecraft_folder, 'options.txt')
  54.     with open(options_file_path, 'r') as file:
  55.         lines = file.readlines()
  56.     for i in range(len(lines)):
  57.         if lines[i].startswith('resourcePacks:'):
  58.             lines[i] = f'resourcePacks:[{formatted_resource_packs}]\n'
  59.             break
  60.     with open(options_file_path, 'w') as file:
  61.         file.writelines(lines)
  62.  
  63. def resource_path(relative_path):
  64.     if hasattr(sys, '_MEIPASS'):
  65.         return os.path.join(sys._MEIPASS, relative_path)
  66.     return os.path.join(os.path.abspath("."), relative_path)
  67.  
  68. if __name__ == '__main__':
  69.     if not is_admin():
  70.         run_as_admin()
  71.     else:
  72.         default_path = os.path.expandvars(r'%APPDATA%\.minecraft')
  73.         minecraft_folder = get_minecraft_folder(default_path)
  74.         install_files(minecraft_folder)
Advertisement
Add Comment
Please, Sign In to add comment