ERSCMU/legacy/ERSCMU-tkinter.py

597 lines
25 KiB
Python

import os
import requests
import zipfile
import shutil
import json
import tkinter as tk
from tkinter import messagebox
from tkinter import filedialog
from datetime import datetime
import configparser
CONFIG_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'mod_updater_config.json')
DEFAULT_MOD_PATH = r"C:\Program Files (x86)\Steam\steamapps\common\ELDEN RING\Game\SeamlessCoop"
GITHUB_API_URL = 'https://api.github.com/repos/LukeYui/EldenRingSeamlessCoopRelease/releases/latest'
BATCH_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'launch_mod.bat')
INI_FILE = 'ersc_settings.ini'
DEFAULT_VALUES = {
'GAMEPLAY': {
'allow_invaders': '1',
'death_debuffs': '1',
'allow_summons': '1',
'overhead_player_display': '0'
},
'SCALING': {
'enemy_health_scaling': '35',
'enemy_damage_scaling': '0',
'enemy_posture_scaling': '15',
'boss_health_scaling': '100',
'boss_damage_scaling': '0',
'boss_posture_scaling': '20'
},
'SAVE': {
'save_file_extension': 'co2'
},
'LANGUAGE': {
'mod_language_override': ''
}
}
FIRST_RUN = 1
def ensure_vocabulary(config):
if "vocabulary" not in config:
config["vocabulary"] = {
"GAMEPLAY": {
"allow_invaders": {"0": "Off", "1": "On"},
"death_debuffs": {"0": "Off", "1": "On"},
"allow_summons": {"0": "Off", "1": "On"},
"overhead_player_display": {
"0": "Normal",
"1": "None",
"2": "Display player ping",
"3": "Display player soul level",
"4": "Display player death count",
"5": "Display player ping & level"
}
},
"SCALING": {},
"SAVE": {},
"PASSWORD": {},
"LANGUAGE": {}
}
return config
def camelcase(text):
return ' '.join([word.capitalize() for word in text.split('_')])
def load_config():
if os.path.exists(CONFIG_FILE):
with open(CONFIG_FILE, 'r') as f:
config = json.load(f)
else:
config = {
"installed_version": "N/A",
"mod_path": DEFAULT_MOD_PATH,
"launcher_name": "ersc_launcher.exe",
"last_updated": "N/A"
}
return ensure_vocabulary(config)
def save_config(config):
with open(CONFIG_FILE, 'w') as f:
json.dump(config, f, indent=4)
def get_latest_release_info():
response = requests.get(GITHUB_API_URL)
response.raise_for_status()
return response.json()
def get_installed_version(mod_path):
version_file = os.path.join(mod_path, 'version.txt')
if os.path.exists(version_file):
with open(version_file, 'r') as f:
return f.read().strip()
return ""
def download_latest_release(download_url, dest_path):
response = requests.get(download_url, stream=True)
response.raise_for_status()
with open(dest_path, 'wb') as f:
for chunk in response.iter_content(chunk_size=8192):
f.write(chunk)
def backup_old_mod(mod_path, installed_version, config):
backup_dir = os.path.dirname(os.path.abspath(__file__))
backup_path = os.path.join(backup_dir, f"ER-SC-{installed_version}.zip")
with zipfile.ZipFile(backup_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
for root, _, files in os.walk(mod_path):
for file in files:
zipf.write(os.path.join(root, file),
os.path.relpath(os.path.join(root, file), mod_path))
launcher_path = os.path.join(os.path.dirname(mod_path), config.get('launcher_name', 'ersc_launcher.exe'))
if os.path.exists(launcher_path):
zipf.write(launcher_path, os.path.basename(launcher_path))
def extract_zip(zip_path, extract_to):
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
zip_ref.extractall(extract_to)
def find_launcher(mod_path):
for root, _, files in os.walk(os.path.dirname(mod_path)):
for file in files:
if file.endswith('.exe') and file != 'eldenring.exe' and file != 'start_protected_game.exe':
return file
return None
def merge_ini_files(old_ini, new_ini):
old_config = configparser.ConfigParser()
old_config.read(old_ini)
new_config = configparser.ConfigParser()
new_config.read(new_ini)
for section in old_config.sections():
if not new_config.has_section(section):
new_config.add_section(section)
for key, value in old_config.items(section):
new_config.set(section, key, value)
with open(new_ini, 'w') as configfile:
new_config.write(configfile)
return new_config
def check_ini_files(mod_path):
old_ini = os.path.join(mod_path, INI_FILE)
new_ini = os.path.join(mod_path, INI_FILE)
if os.path.exists(old_ini) and os.path.exists(new_ini):
merged_config = merge_ini_files(old_ini, new_ini)
with open(new_ini, 'w') as configfile:
merged_config.write(configfile)
elif os.path.exists(old_ini) and not os.path.exists(new_ini):
shutil.copy(old_ini, new_ini)
def update_mod(latest_version, download_url, release_info):
config = load_config()
mod_path = config["mod_path"]
installed_version = get_installed_version(mod_path)
try:
zip_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'SeamlessCoop.zip')
download_latest_release(download_url, zip_path)
if installed_version:
backup_old_mod(mod_path, installed_version, config)
shutil.rmtree(mod_path)
old_launcher = config.get('launcher_name', 'ersc_launcher.exe')
old_launcher_path = os.path.join(os.path.dirname(mod_path), old_launcher)
if os.path.exists(old_launcher_path):
os.remove(old_launcher_path)
extract_zip(zip_path, os.path.dirname(mod_path))
os.remove(zip_path)
new_launcher = find_launcher(mod_path)
if new_launcher:
config['launcher_name'] = new_launcher
with open(os.path.join(mod_path, 'version.txt'), 'w') as f:
f.write(latest_version)
config['installed_version'] = latest_version
config['last_updated'] = datetime.strptime(release_info['published_at'], "%Y-%m-%dT%H:%M:%SZ").strftime("%Y-%m-%d %H:%M:%S")
save_config(config)
check_ini_files(mod_path)
create_batch_script()
update_info_text()
messagebox.showinfo("Update Complete", f"Updated to version {latest_version}.")
except Exception as e:
messagebox.showerror("Error", f"Update failed: {e}")
def check_for_updates():
global FIRST_RUN
config = load_config()
mod_path = config["mod_path"]
installed_version = get_installed_version(mod_path)
try:
release_info = get_latest_release_info()
latest_version = release_info['tag_name']
download_url = next(asset['browser_download_url'] for asset in release_info['assets'] if asset['name'].endswith('.zip'))
except Exception as e:
messagebox.showerror("Error", f"Failed to fetch update info: {e}")
return
if installed_version != latest_version:
response = messagebox.askyesno("Update Available", f"A new version ({latest_version}) is available. Do you want to update?")
if response:
update_mod(latest_version, download_url, release_info)
elif FIRST_RUN == 0:
response = messagebox.askyesno("No Updates", "No new updates available. Do you want to force update?")
if response:
update_mod(latest_version, download_url, release_info)
def launch_mod():
config = load_config()
ini_path = os.path.join(config["mod_path"], INI_FILE)
password = read_password_from_ini(ini_path)
if not password:
messagebox.showerror("Error", "Session password cannot be empty.")
return
os.startfile(BATCH_FILE)
def browse_folder():
folder_selected = filedialog.askdirectory()
if folder_selected:
config = load_config()
if os.path.basename(folder_selected) == "SeamlessCoop":
config["mod_path"] = folder_selected
save_config(config)
mod_path_entry.delete(0, tk.END)
mod_path_entry.insert(0, folder_selected)
create_batch_script()
update_info_text()
else:
messagebox.showerror("Error", "Please select the SeamlessCoop folder.")
def update_info_text():
config = load_config()
installed_version = config.get('installed_version', 'Unknown')
last_updated = config.get('last_updated', 'Unknown')
launcher_path = os.path.join(os.path.dirname(config["mod_path"]), config['launcher_name'])
wdir = os.path.join(os.path.dirname(os.path.abspath(__file__)))
info_text.set(f"Current Version: {installed_version}\nLast Updated: {last_updated}\nLauncher Path: {launcher_path}\nWorking Directory: {wdir}")
def create_batch_script():
config = load_config()
launcher_path = os.path.join(os.path.dirname(config["mod_path"]), config['launcher_name'])
with open(BATCH_FILE, 'w') as f:
f.write(f'@echo off\ncd /d "{os.path.dirname(launcher_path)}"\nstart {config["launcher_name"]}\n')
def read_password_from_ini(ini_path):
if os.path.exists(ini_path):
config_parser = configparser.ConfigParser()
config_parser.read(ini_path)
if config_parser.has_option('PASSWORD', 'cooppassword'):
return config_parser.get('PASSWORD', 'cooppassword')
return ""
def update_password():
config = load_config()
ini_path = os.path.join(config["mod_path"], INI_FILE)
password = read_password_from_ini(ini_path)
if password:
password_entry.delete(0, tk.END)
password_entry.insert(0, password)
def save_password_to_ini(password, ini_path):
if os.path.exists(ini_path):
config_parser = configparser.ConfigParser()
config_parser.read(ini_path)
if not config_parser.has_section('PASSWORD'):
config_parser.add_section('PASSWORD')
config_parser.set('PASSWORD', 'cooppassword', password)
with open(ini_path, 'w') as configfile:
config_parser.write(configfile)
def save_password():
new_password = password_entry.get()
if not new_password:
messagebox.showerror("Error", "Session password cannot be empty.")
return
config = load_config()
ini_path = os.path.join(config["mod_path"], INI_FILE)
save_password_to_ini(new_password, ini_path)
def toggle_password():
if password_entry.cget('show') == '':
password_entry.config(show='*')
toggle_button.config(text='Show')
else:
password_entry.config(show='')
toggle_button.config(text='Hide')
def get_locale_options(mod_path):
locale_path = os.path.join(mod_path, 'locale')
if not os.path.exists(locale_path):
return ["Default"]
return ["Default"] + [os.path.splitext(f)[0].capitalize() for f in os.listdir(locale_path) if f.endswith('.json')]
def auto_discover_mod_path():
messagebox.showinfo("Auto-Discovery", "Auto-discovery might take a few minutes. Please wait...")
drives = [f"{chr(letter)}:\\" for letter in range(65, 91) if os.path.exists(f"{chr(letter)}:\\")]
potential_paths = []
# First, check Program Files directories
program_files_dirs = []
for drive in drives:
program_files_dirs.extend([
os.path.join(drive, 'Program Files', 'Steam'),
os.path.join(drive, 'Program Files (x86)', 'Steam')
])
for dir in program_files_dirs:
if os.path.exists(dir):
for root, dirs, files in os.walk(dir):
if 'steamapps' in dirs:
steamapps_path = os.path.join(root, 'steamapps')
elden_ring_path = os.path.join(steamapps_path, 'common', 'ELDEN RING', 'Game')
if os.path.exists(elden_ring_path):
if not os.path.exists(os.path.join(elden_ring_path, 'SeamlessCoop')):
response = messagebox.askyesno("Mod Not Found", f"SeamlessCoop mod not found in {elden_ring_path}. Do you want to install the mod?")
if response:
install_mod(elden_ring_path)
else:
potential_paths.append(os.path.join(elden_ring_path, 'SeamlessCoop'))
dirs[:] = [] # Stop further recursion
if not potential_paths:
# Check other directories
for drive in drives:
for root, dirs, files in os.walk(drive):
if len(root.split(os.sep)) - 1 > 5: # Limit to 5 directories deep
dirs[:] = [] # Don't recurse any deeper
continue
if 'steamapps' in dirs:
steamapps_path = os.path.join(root, 'steamapps')
elden_ring_path = os.path.join(steamapps_path, 'common', 'ELDEN RING', 'Game')
if os.path.exists(elden_ring_path):
if not os.path.exists(os.path.join(elden_ring_path, 'SeamlessCoop')):
response = messagebox.askyesno("Mod Not Found", f"SeamlessCoop mod not found in {elden_ring_path}. Do you want to install the mod?")
if response:
install_mod(elden_ring_path)
else:
potential_paths.append(os.path.join(elden_ring_path, 'SeamlessCoop'))
dirs[:] = [] # Stop further recursion
if potential_paths:
mod_path = potential_paths[0]
config = load_config()
config["mod_path"] = mod_path
save_config(config)
mod_path_entry.delete(0, tk.END)
mod_path_entry.insert(0, mod_path)
create_batch_script()
update_info_text()
messagebox.showinfo("Auto-Discovery Complete", f"Mod path set to: {mod_path}")
else:
messagebox.showerror("Auto-Discovery Failed", "Failed to auto-discover the mod path. Please select it manually.")
browse_folder()
def install_mod(elden_ring_path):
try:
release_info = get_latest_release_info()
latest_version = release_info['tag_name']
download_url = next(asset['browser_download_url'] for asset in release_info['assets'] if asset['name'].endswith('.zip'))
zip_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'SeamlessCoop.zip')
download_latest_release(download_url, zip_path)
extract_zip(zip_path, elden_ring_path)
os.remove(zip_path)
mod_path = os.path.join(elden_ring_path, 'SeamlessCoop')
config = load_config()
config["mod_path"] = mod_path
save_config(config)
new_launcher = find_launcher(mod_path)
if new_launcher:
config['launcher_name'] = new_launcher
with open(os.path.join(mod_path, 'version.txt'), 'w') as f:
f.write(latest_version)
config['installed_version'] = latest_version
config['last_updated'] = datetime.strptime(release_info['published_at'], "%Y-%m-%dT%H:%M:%SZ").strftime("%Y-%m-%d %H:%M:%S")
save_config(config)
check_ini_files(mod_path)
create_batch_script()
update_info_text()
messagebox.showinfo("Installation Complete", f"Installed version {latest_version}.")
except Exception as e:
messagebox.showerror("Error", f"Installation failed: {e}")
def open_settings_window():
settings_window = tk.Toplevel(root)
settings_window.title("Settings")
config = load_config()
ini_path = os.path.join(config["mod_path"], INI_FILE)
config_parser = configparser.ConfigParser()
config_parser.read(ini_path)
vocabulary = config["vocabulary"]
settings = {}
def save_settings():
for section, entries in settings.items():
for key, widget in entries.items():
if section == 'GAMEPLAY' and key in ['allow_invaders', 'death_debuffs', 'allow_summons']:
value = '1' if widget.get() == "On" else '0'
elif section == 'GAMEPLAY' and key == 'overhead_player_display':
value = list(vocabulary[section][key].keys())[list(vocabulary[section][key].values()).index(widget.get())]
elif section == 'LANGUAGE' and key == 'mod_language_override':
value = '' if widget.get() == "Default" else widget.get().lower()
else:
value = widget.get()
if section == 'SCALING' and not value.isdigit():
messagebox.showerror("Error", f"Invalid value for {key} in {section}")
return
if section == 'SAVE' and value.endswith('sl2'):
messagebox.showerror("Error", "Value for SAVE should not be set to sl2")
return
config_parser.set(section, key, value)
with open(ini_path, 'w') as configfile:
config_parser.write(configfile)
settings_window.destroy()
messagebox.showinfo("Settings Saved", "Settings have been saved successfully.")
row = 0
for section in config_parser.sections():
if section == 'PASSWORD':
continue
section_label = tk.Label(settings_window, text=section, font=("Arial", 12, "bold"))
section_label.grid(row=row, column=0, columnspan=3, pady=(10, 0))
row += 1
settings[section] = {}
for key, value in config_parser.items(section):
display_name = camelcase(key)
default_value = DEFAULT_VALUES.get(section, {}).get(key, "")
if section == 'GAMEPLAY':
if key in ['allow_invaders', 'death_debuffs', 'allow_summons']:
label = tk.Label(settings_window, text=display_name)
label.grid(row=row, column=0, sticky="e")
var = tk.StringVar(value="On" if value == "1" else "Off")
toggle = tk.Checkbutton(settings_window, textvariable=var, variable=var, onvalue="On", offvalue="Off")
toggle.grid(row=row, column=1, padx=5, pady=5)
settings[section][key] = var
reset_button = tk.Button(settings_window, text="Reset", command=lambda v=var, d=default_value: v.set("On" if d == "1" else "Off"))
reset_button.grid(row=row, column=2)
elif key == 'overhead_player_display':
label = tk.Label(settings_window, text=display_name)
label.grid(row=row, column=0, sticky="e")
dropdown = tk.StringVar(settings_window)
options = vocabulary[section][key]
dropdown.set(options[value])
dropdown_menu = tk.OptionMenu(settings_window, dropdown, *options.values())
dropdown_menu.grid(row=row, column=1)
settings[section][key] = dropdown
reset_button = tk.Button(settings_window, text="Reset", command=lambda d=dropdown, dv=default_value: d.set(options[dv]))
reset_button.grid(row=row, column=2)
elif section == 'SAVE':
label = tk.Label(settings_window, text=display_name)
label.grid(row=row, column=0, sticky="e")
entry = tk.Entry(settings_window, width=30)
entry.grid(row=row, column=1, padx=5, pady=5)
entry.insert(0, value)
settings[section][key] = entry
disclaimer = tk.Label(settings_window, text="(Do not use sl2)", font=("Arial", 8, "italic"))
disclaimer.grid(row=row, column=2, padx=5, pady=5)
reset_button = tk.Button(settings_window, text="Reset", command=lambda e=entry, dv=default_value: e.delete(0, tk.END) or e.insert(0, dv))
reset_button.grid(row=row, column=3, padx=5, pady=5)
elif section == 'LANGUAGE':
label = tk.Label(settings_window, text=display_name)
label.grid(row=row, column=0, sticky="e")
dropdown = tk.StringVar(settings_window)
options = get_locale_options(config["mod_path"])
dropdown.set(value.capitalize() if value else "Default")
dropdown_menu = tk.OptionMenu(settings_window, dropdown, *options)
dropdown_menu.grid(row=row, column=1)
settings[section][key] = dropdown
reset_button = tk.Button(settings_window, text="Reset", command=lambda d=dropdown: d.set("Default"))
reset_button.grid(row=row, column=2)
else:
label = tk.Label(settings_window, text=display_name)
label.grid(row=row, column=0, sticky="e")
entry = tk.Entry(settings_window, width=30)
entry.grid(row=row, column=1, padx=5, pady=5)
entry.insert(0, value)
settings[section][key] = entry
reset_button = tk.Button(settings_window, text="Reset", command=lambda e=entry, dv=default_value: e.delete(0, tk.END) or e.insert(0, dv))
reset_button.grid(row=row, column=2)
row += 1
save_button = tk.Button(settings_window, text="Save Settings", command=save_settings)
save_button.grid(row=row, column=0, columnspan=3, pady=10)
def show_info():
config = load_config()
installed_version = config.get('installed_version', 'Unknown')
last_updated = config.get('last_updated', 'Unknown')
launcher_path = os.path.join(os.path.dirname(config["mod_path"]), config['launcher_name'])
messagebox.showinfo("Info", f"Current Version: {installed_version}\nLast Updated: {last_updated}\nLauncher Path: {launcher_path}")
def show_about():
about_text = """
MIT License
Copyright (c) 2024 Franz Rolfsvaag
"Elden Ring Seamless Coop Mod Updater" is in no ways affiliated with the creators of Elden Ring or the creator of the Seamless Coop mod.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
messagebox.showinfo("About", about_text)
def initial_update_check():
global FIRST_RUN
check_for_updates()
FIRST_RUN=0
def create_gui():
global root, mod_path_entry, info_text, password_entry, toggle_button, first_run
root = tk.Tk()
root.title("Elden Ring Seamless Coop Mod Updater")
menubar = tk.Menu(root)
root.config(menu=menubar)
file_menu = tk.Menu(menubar, tearoff=0)
file_menu.add_command(label="Settings", command=open_settings_window)
file_menu.add_command(label="Check for Updates", command=check_for_updates)
file_menu.add_command(label="Info", command=show_info)
file_menu.add_command(label="About", command=show_about)
menubar.add_cascade(label="Menu", menu=file_menu)
frame = tk.Frame(root, padx=10, pady=10)
frame.pack(padx=10, pady=10)
mod_path_label = tk.Label(frame, text="Mod Folder:")
mod_path_label.grid(row=0, column=0, sticky="e")
config = load_config()
mod_path_entry = tk.Entry(frame, width=50)
mod_path_entry.grid(row=0, column=1, padx=5)
mod_path_entry.insert(0, config["mod_path"])
browse_button = tk.Button(frame, text="Browse", command=browse_folder)
browse_button.grid(row=0, column=2)
auto_button = tk.Button(frame, text="Auto", command=auto_discover_mod_path)
auto_button.grid(row=0, column=3)
check_updates_button = tk.Button(frame, text="Check for Updates", command=check_for_updates)
check_updates_button.grid(row=1, column=0, columnspan=4, pady=10)
password_label = tk.Label(frame, text="Session Password:")
password_label.grid(row=2, column=0, sticky="e")
password_entry = tk.Entry(frame, width=50, show='*')
password_entry.grid(row=2, column=1, padx=5)
toggle_button = tk.Button(frame, text="Show", command=toggle_password)
toggle_button.grid(row=2, column=2)
save_password_button = tk.Button(frame, text="Save Password", command=save_password)
save_password_button.grid(row=2, column=3)
launch_button = tk.Button(frame, text="Launch Seamless Coop", command=launch_mod)
launch_button.grid(row=3, column=0, columnspan=4, pady=10)
info_text = tk.StringVar()
info_label = tk.Label(frame, textvariable=info_text, justify="left")
info_label.grid(row=5, column=0, columnspan=4, pady=10)
update_info_text()
update_password()
initial_update_check()
root.mainloop()
if __name__ == "__main__":
create_gui()
# TODO
# - Add auto-conversion of vanilla save
#
# Command to convert into executable
# python -m PyInstaller --noconsole --onefile --distpath "C:\Users\fraro\ERSC Updater\output" "C:\Users\fraro\ERSC Updater\ERSC Updater.py"