ModpackUpdater/Main.py

137 lines
4.7 KiB
Python
Raw Normal View History

2024-06-20 07:39:09 -06:00
from ftplib import FTP
2024-06-19 21:10:32 -06:00
import os
import io
import json
import tempfile
import subprocess
2024-06-19 21:36:55 -06:00
import time
2024-06-20 07:39:09 -06:00
import tkinter as tk
from tkinter import filedialog
import configparser
2024-06-20 08:23:23 -06:00
import customtkinter
from customtkinter import *
2024-06-20 07:39:09 -06:00
# Initialize a configparser object for managing the configuration file
config = configparser.ConfigParser()
config_file_path = 'config.ini' # Path to the configuration file
def select_prism_instance_path():
global prism_instance_path_entry
directory = filedialog.askdirectory()
prism_instance_path_entry.delete(0, tk.END)
prism_instance_path_entry.insert(0, directory)
# Store the selected path in the configuration file
config['PRISM'] = {'InstancePath': directory}
with open(config_file_path, 'w') as configfile:
config.write(configfile)
2024-06-19 21:10:32 -06:00
def main():
2024-06-20 07:39:09 -06:00
global prism_instance_path_entry
2024-06-19 21:10:32 -06:00
instance_name = "Familycraft Season 7"
prism_command = "prismlauncher"
2024-06-20 07:39:09 -06:00
# Default paths based on OS
2024-06-19 21:10:32 -06:00
if os.name == 'nt':
2024-06-20 07:39:09 -06:00
prism_command = os.getenv('LOCALAPPDATA') + "/Programs/PrismLauncher/prismlauncher.exe"
default_prism_instance_path = os.getenv("APPDATA") + "/PrismLauncher/instances"
else:
prism_command = "prismlauncher" # Update with the correct path for Linux/Mac
default_prism_instance_path = os.getenv("HOME") + "/.local/share/PrismLauncher/instances"
# Load stored Prism instance path from the configuration file
if os.path.exists(config_file_path):
config.read(config_file_path)
if 'PRISM' in config and 'InstancePath' in config['PRISM']:
stored_prism_instance_path = config['PRISM']['InstancePath']
else:
stored_prism_instance_path = default_prism_instance_path
else:
stored_prism_instance_path = default_prism_instance_path
# GUI for selecting Prism instance path
2024-06-20 08:23:23 -06:00
app = CTk()
set_appearance_mode("dark")
#root.title("Select Prism Instance Path")
2024-06-20 07:39:09 -06:00
# Label for instructions
2024-06-20 08:23:23 -06:00
label = CTkLabel(master=app, text="Select Prism instance path:")
2024-06-20 07:39:09 -06:00
label.pack(pady=10)
# Entry widget for Prism instance path
2024-06-20 08:23:23 -06:00
prism_instance_path_entry = CTkEntry(master=app, width=50)
2024-06-20 07:39:09 -06:00
prism_instance_path_entry.pack(pady=5)
# If a stored Prism instance path exists, pre-fill the entry widget
if stored_prism_instance_path:
prism_instance_path_entry.insert(0, stored_prism_instance_path)
# Button to select directory
2024-06-20 08:23:23 -06:00
select_button = CTkButton(master=app, text="Browse...", command=select_prism_instance_path)
2024-06-20 07:39:09 -06:00
select_button.pack(pady=5)
# Function to proceed with installation
def start_installation():
prism_instance_path = prism_instance_path_entry.get()
2024-06-20 08:23:23 -06:00
app.destroy() # Close the GUI window
2024-06-20 07:39:09 -06:00
perform_installation(instance_name, prism_command, prism_instance_path)
# Button to start installation
2024-06-20 08:23:23 -06:00
install_button = CTkButton(master=app, text="Launch Minecraft", command=start_installation)
2024-06-20 07:39:09 -06:00
install_button.pack(pady=10)
2024-06-20 08:23:23 -06:00
app.mainloop()
2024-06-20 07:39:09 -06:00
def perform_installation(instance_name, prism_command, prism_instance_path):
2024-06-19 21:10:32 -06:00
ftp = FTP("gitea.piwalker.net")
ftp.login()
2024-06-20 07:39:09 -06:00
# Fetching versions.json from FTP
2024-06-19 21:10:32 -06:00
bio = io.BytesIO()
ftp.retrbinary("RETR versions.json", bio.write)
bio.seek(0)
versions = json.load(bio)
bio.close()
2024-06-20 07:39:09 -06:00
# Checking current version
version = "0.0.0"
version_file_path = os.path.join(prism_instance_path, instance_name, ".minecraft", "version.txt")
if os.path.exists(version_file_path):
with open(version_file_path, 'r') as fp:
2024-06-19 21:36:55 -06:00
version = fp.readline().rstrip()
2024-06-20 07:39:09 -06:00
# Checking if update is needed
if version != versions[-1]["Version"]:
print(f"Current version: {version}")
print(f"Latest version: {versions[-1]['Version']}")
# Downloading modpack
2024-06-19 21:10:32 -06:00
with tempfile.TemporaryDirectory() as temp_dir:
2024-06-20 07:39:09 -06:00
modpack_file_path = os.path.join(temp_dir, instance_name + ".mrpack")
with open(modpack_file_path, 'wb') as modpack:
ftp.retrbinary("RETR " + versions[-1]["File"], modpack.write)
# Running PrismLauncher with modpack
p = subprocess.Popen([prism_command, '-I', modpack_file_path])
# Waiting for installation to complete
2024-06-19 21:36:55 -06:00
while True:
time.sleep(5)
2024-06-20 07:39:09 -06:00
if os.path.exists(version_file_path):
with open(version_file_path, 'r') as fp:
2024-06-19 21:38:32 -06:00
version = fp.readline().rstrip()
2024-06-20 07:39:09 -06:00
if version == versions[-1]["Version"]:
break
# Launching PrismLauncher with the instance
2024-06-19 21:10:32 -06:00
subprocess.run([prism_command, '-l', instance_name])
2024-06-20 07:39:09 -06:00
if __name__ == "__main__":
main()
2024-06-19 21:10:32 -06:00