Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 21 additions & 20 deletions Launcher with autoupdater.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
from datetime import datetime
from os.path import exists
from pathlib import Path
from appdirs import AppDirs
import subprocess
AppdataPATH = os.getenv('APPDATA') + "\\OpenGOAL-UnofficialModLauncher\\"

dirs = AppDirs(roaming=True)
AppdataPATH = Path(dirs.user_data_dir)/"OpenGOAL-UnofficialModLauncher"

def show_progress(block_num, block_size, total_size):
if total_size > 0:
Expand Down Expand Up @@ -45,8 +48,8 @@ def check_for_updates():
latest_release_assets_url = "https://github.com/OpenGOAL-Unofficial-Mods/OpenGoal-ModLauncher-dev/releases/download/v1.10fixoldpckernel/openGOALModLauncher.exe"

last_write = datetime(2020, 5, 17)
if os.path.exists(AppdataPATH + "\\OpengoalModLauncher.exe"):
last_write = datetime.utcfromtimestamp(Path(AppdataPATH + "\\OpengoalModLauncher.exe").stat().st_mtime)
if (AppdataPATH/"OpengoalModLauncher.exe").exists():
last_write = datetime.utcfromtimestamp((AppdataPATH/"OpengoalModLauncher.exe").stat().st_mtime)

need_update = bool((last_write < latest_release))

Expand All @@ -63,8 +66,6 @@ def check_for_updates():
window['launch_button'].update(visible=True)

def download_newest_mod():
AppdataPATH = os.getenv('APPDATA') + "\\OpenGOAL-UnofficialModLauncher\\"

launch_url = "https://api.github.com/repos/OpenGOAL-Unofficial-Mods/OpenGoal-ModLauncher-dev/releases"
response = requests.get(url=launch_url, params={'address': "yolo"})

Expand All @@ -81,35 +82,35 @@ def download_newest_mod():
latest_release_assets_url = "https://github.com/OpenGOAL-Unofficial-Mods/OpenGoal-ModLauncher-dev/releases/download/v1.10fixoldpckernel/openGOALModLauncher.exe"

last_write = datetime(2020, 5, 17)
if os.path.exists(AppdataPATH + "\\OpengoalModLauncher.exe"):
last_write = datetime.utcfromtimestamp(Path(AppdataPATH + "\\OpengoalModLauncher.exe").stat().st_mtime)
if (AppdataPATH/"OpengoalModLauncher.exe").exists():
last_write = datetime.utcfromtimestamp((AppdataPATH/"OpengoalModLauncher.exe").stat().st_mtime)

need_update = bool((last_write < latest_release))

if need_update:
window['update_status'].update("Starting Update...")
try_remove_dir(AppdataPATH + "/temp")
if not os.path.exists(AppdataPATH + "/temp"):
os.makedirs(AppdataPATH + "/temp")
try_remove_dir(AppdataPATH/"temp")
if not os.path.exists(AppdataPATH/"temp"):
os.makedirs(AppdataPATH/"temp")

window['update_status'].update("Downloading update from " + latest_release_assets_url)
file = urllib.request.urlopen(latest_release_assets_url)
urllib.request.urlretrieve(latest_release_assets_url, AppdataPATH + "/temp/OpengoalModLauncher.exe", show_progress)
urllib.request.urlretrieve(latest_release_assets_url, AppdataPATH/"/temp/OpengoalModLauncher.exe", show_progress)
window['update_status'].update("Done downloading")

window['update_status'].update("Removing previous installation " + AppdataPATH)
try_remove_dir(AppdataPATH + "/data")
try_remove_file(AppdataPATH + "/gk.exe")
try_remove_file(AppdataPATH + "/goalc.exe")
try_remove_file(AppdataPATH + "/extractor.exe")
window['update_status'].update(f"Removing previous installation {AppdataPATH}")
try_remove_dir(AppdataPATH/"data")
try_remove_file(AppdataPATH/"gk.exe")
try_remove_file(AppdataPATH/"goalc.exe")
try_remove_file(AppdataPATH/"extractor.exe")

window['update_status'].update("Extracting update")
temp_dir = AppdataPATH + "/temp"
try_remove_file(temp_dir + "/updateDATA.zip")
temp_dir = AppdataPATH/"temp"
try_remove_file(temp_dir/"updateDATA.zip")
sub_dir = temp_dir
all_files = os.listdir(sub_dir)
for f in all_files:
shutil.move(sub_dir + "/" + f, AppdataPATH + "/" + f)
shutil.move(sub_dir/f, AppdataPATH/f)
try_remove_dir(temp_dir)
window['update_status'].update("Update complete")
window['update_button'].update(visible=False)
Expand All @@ -136,6 +137,6 @@ def download_newest_mod():
download_newest_mod()
elif event == "launch_button":
window.close()
subprocess.call([ AppdataPATH + "openGOALModLauncher.exe"])
subprocess.call([ AppdataPATH/"openGOALModLauncher.exe"])

window.close()
65 changes: 30 additions & 35 deletions openGOALModLauncher.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@
from pathlib import Path
import random


dirs = AppDirs(roaming=True)

# C:\Users\USERNAME\AppData\Roaming\OpenGOAL-Mods\
ModFolderPATH = Path(dirs.user_data_dir) / "OpenGOAL-Mods"

sg.theme("DarkBlue3")


Expand All @@ -45,11 +51,11 @@ def exitWithError():
# Folder where script is placed, It looks in this for the Exectuable
if getattr(sys, "frozen", False):
# If we are a pyinstaller exe get the path of this file, not python
LauncherDir = os.path.dirname(os.path.realpath(sys.executable))
LauncherDir = Path(sys.executable).resolve().parent

# Detect if a user has downloaded a release directly, if so point them to the autoupdater
if (
LauncherDir != os.getenv("APPDATA") + "\\OpenGOAL-UnofficalModLauncher"
LauncherDir != Path(dirs.user_data_dir) / "OpenGOAL-UnofficalModLauncher"
and os.getlogin() != "NinjaPC"
and os.environ["COMPUTERNAME"] != "DESKTOP-BBN1CMN"
):
Expand Down Expand Up @@ -80,15 +86,10 @@ def exitWithError():
# if we are running the .py directly use this path
LauncherDir = os.path.dirname(__file__)

installpath = str(LauncherDir + "\\resources\\")
installpath = Path(LauncherDir) / "resources"

# intialize default variables so they are never null

dirs = AppDirs(roaming=True)

# C:\Users\USERNAME\AppData\Roaming\OpenGOAL-Mods\
ModFolderPATH = os.path.join(dirs.user_data_dir, "OpenGOAL-Mods", "")

# grab images from web

# url to splash screen image
Expand Down Expand Up @@ -131,8 +132,8 @@ def fetch_image(url): # Accept the URL parameter
loadingimage = getPNGFromURL("https://raw.githubusercontent.com/OpenGOAL-Unofficial-Mods/OpenGoal-ModLauncher-dev/main/resources/modlauncher-loading-0.png")

# make the modfolderpath if first install
if not os.path.exists(ModFolderPATH):
os.makedirs(ModFolderPATH)
if not ModFolderPATH.exists():
ModFolderPATH.mkdir()

table_headings = [
"id",
Expand Down Expand Up @@ -210,8 +211,9 @@ def fetch_image(url): # Accept the URL parameter

def getRefreshedTableData(sort_col_idx):
# Load data from the local file if it exists
local_file_path = "resources/jak1_mods.json"
if os.path.exists(local_file_path):
local_file_path = Path("resources") / "jak1_mods.json"
if local_file_path.exists():
# TODO: don't leak fd
local_mods = json.loads(open(local_file_path, "r").read())

# Load data from the remote URL
Expand All @@ -221,7 +223,7 @@ def getRefreshedTableData(sort_col_idx):
# Initialize an empty dictionary to store the combined data
mod_dict = {}

if os.path.exists(local_file_path):
if local_file_path.exists():
# Merge the remote and local data while removing duplicates
mod_dict = {**remote_mods, **local_mods}
else:
Expand All @@ -243,30 +245,25 @@ def getRefreshedTableData(sort_col_idx):
mod["install_date"] = "Not Installed"
mod["access_date"] = "Not Installed"

mod_path = ModFolderPATH/mod_id
mod_gk_path = mod_path/"gk.exe"
# determine local install/access datetime
if mod_id in installed_mod_subfolders:
mod[
"install_date"
] = f"{datetime.fromtimestamp(installed_mod_subfolders[mod_id]):%Y-%m-%d %H:%M}"

if exists(f"{ModFolderPATH}/{mod_id}/gk.exe"):
gk_stat = os.stat(f"{ModFolderPATH}/{mod_id}/gk.exe")
mod[
"access_date"
] = f"{datetime.fromtimestamp(gk_stat.st_atime):%Y-%m-%d %H:%M}"
elif mod_name in installed_mod_subfolders:
# previous installation using mod_name (will migrate after this step)
mod[
"install_date"
] = f"{datetime.fromtimestamp(installed_mod_subfolders[mod_name]):%Y-%m-%d %H:%M}"
# migrate folder to use mod_id instead of mod_name
shutil.move(ModFolderPATH + "/" + mod_name, ModFolderPATH + "/" + mod_id)
(ModFolderPATH/mod_name).rename(mod_path)

if exists(f"{ModFolderPATH}/{mod_id}/gk.exe"):
gk_stat = os.stat(f"{ModFolderPATH}/{mod_id}/gk.exe")
mod[
"access_date"
] = f"{datetime.fromtimestamp(gk_stat.st_atime):%Y-%m-%d %H:%M}"
if mod_gk_path.exists():
mod[
"access_date"
] = f"{datetime.fromtimestamp(mod_gk_path.stat().st_atime):%Y-%m-%d %H:%M}"

mod["contributors"] = ", ".join(mod["contributors"])
mod["tags"].sort()
Expand Down Expand Up @@ -846,7 +843,7 @@ def loading_screen_with_thread(thread):
subfolders = [f.name for f in os.scandir(ModFolderPATH) if f.is_dir()]

if tmpModSelected in subfolders:
dir = dirs.user_data_dir + "\\OpenGOAL-Mods\\" + tmpModSelected
dir = ModFolderPATH/tmpModSelected
launcherUtils.openFolder(dir)
else:
sg.Popup("Selected mod is not installed", keep_on_top=True, icon=iconfile)
Expand All @@ -858,10 +855,9 @@ def loading_screen_with_thread(thread):
[linkType, tmpModURL] = githubUtils.identifyLinkType(tmpModURL)
subfolders = [f.name for f in os.scandir(ModFolderPATH) if f.is_dir()]
if tmpModSelected in subfolders:
dir = dirs.user_data_dir + "\\OpenGOAL-Mods\\" + tmpModSelected
dir = ModFolderPATH/tmpModSelected
ans = sg.popup_ok_cancel(
"Confirm: re-extracting "
+ dir,
f"Confirm: re-extracting {dir}",
icon=iconfile,
)
if ans == "OK":
Expand All @@ -878,10 +874,9 @@ def loading_screen_with_thread(thread):
[linkType, tmpModURL] = githubUtils.identifyLinkType(tmpModURL)
subfolders = [f.name for f in os.scandir(ModFolderPATH) if f.is_dir()]
if tmpModSelected in subfolders:
dir = dirs.user_data_dir + "\\OpenGOAL-Mods\\" + tmpModSelected
dir = ModFolderPATH/tmpModSelected
ans = sg.popup_ok_cancel(
"Confirm: recompiling "
+ dir,
f"Confirm: recompiling {dir}",
icon=iconfile,
)
if ans == "OK":
Expand All @@ -895,8 +890,8 @@ def loading_screen_with_thread(thread):
subfolders = [f.name for f in os.scandir(ModFolderPATH) if f.is_dir()]

if tmpModSelected in subfolders:
dir = dirs.user_data_dir + "\\OpenGOAL-Mods\\" + tmpModSelected
ans = sg.popup_ok_cancel("Confirm: uninstalling " + dir, icon=iconfile)
dir = ModFolderPATH/tmpModSelected
ans = sg.popup_ok_cancel(f"Confirm: uninstalling {dir}", icon=iconfile)
if ans == "OK":
launcherUtils.try_remove_dir(dir)
reset()
Expand Down Expand Up @@ -940,7 +935,7 @@ def loading_screen_with_thread(thread):
LATEST_TABLE_DATA = getRefreshedTableData(None)
window["-MODTABLE-"].update(values=LATEST_TABLE_DATA)
elif event == "-VIEWISOFOLDER-":
dir = dirs.user_data_dir + "\\OpenGOAL-Mods\\_iso_data"
dir = ModFolderPATH/"_iso_data"
launcherUtils.ensure_jak_folders_exist()
launcherUtils.openFolder(dir)
elif event == "-JAKMODSWEB-":
Expand Down
Loading