1
- import PySimpleGUI as sg
1
+ # -*- coding: utf-8 -*-
2
+ """
3
+ Created on Mon Aug 29 20:46:07 2022
4
+
5
+ @author: Zed
6
+ """
7
+ from datetime import datetime
8
+ from os .path import exists
9
+ import json
2
10
import os
11
+ import pathlib
12
+ import progressbar
3
13
import requests
4
- import json
5
- import urllib
6
14
import shutil
7
- import traceback
8
- from datetime import datetime
9
- from os .path import exists
10
- from pathlib import Path
11
15
import subprocess
12
- AppdataPATH = os .getenv ('APPDATA' ) + "\\ OpenGOAL-UnofficialModLauncher\\ "
16
+ import urllib
17
+ import traceback
13
18
19
+ pbar = None
14
20
def show_progress (block_num , block_size , total_size ):
15
21
if total_size > 0 :
16
- try :
17
- window ['progress_bar' ].UpdateBar (block_num * block_size , total_size )
18
- except Exception as e :
19
- pass # Handle the exception if the window or element does not exist
22
+ global pbar
23
+ if pbar is None :
24
+ pbar = progressbar .ProgressBar (maxval = total_size )
25
+ pbar .start ()
26
+
27
+ downloaded = block_num * block_size
28
+ if downloaded < total_size :
29
+ pbar .update (downloaded )
30
+ else :
31
+ pbar .finish ()
32
+ pbar = None
20
33
21
34
def try_remove_file (file ):
22
35
if exists (file ):
@@ -25,117 +38,91 @@ def try_remove_file(file):
25
38
def try_remove_dir (dir ):
26
39
if exists (dir ):
27
40
shutil .rmtree (dir )
28
-
29
- def check_for_updates ():
41
+
42
+ def downloadNewestLauncher ():
30
43
31
-
32
- launch_url = "https://api.github.com/repos/OpenGOAL-Unofficial-Mods/OpenGoal-ModLauncher-dev/releases"
33
- response = requests . get ( url = launch_url , params = { 'address' : "yolo" })
34
-
44
+ InstallDir = AppdataPATH
45
+
46
+ launchUrl = "https://api.github.com/repos/OpenGOAL-Unofficial-Mods/OpenGoal-ModLauncher-dev/releases"
47
+ response = requests . get ( url = launchUrl , params = { 'address' : "yolo" })
35
48
if response is not None and response .status_code == 200 :
36
- r = json .loads (json .dumps (response .json ()))
37
- latest_release = datetime .strptime (r [0 ].get ("published_at" ).replace ("T" , " " ).replace ("Z" , "" ),
38
- '%Y-%m-%d %H:%M:%S' )
39
- latest_release_assets_url = (json .loads (
40
- json .dumps (requests .get (url = r [0 ].get ("assets_url" ), params = {'address' : "yolo" }).json ())))[0 ].get (
41
- "browser_download_url" )
49
+ # didnt get rate limited yay
50
+ r = json .loads (json .dumps (response .json ()))
51
+ LatestRel = datetime .strptime (r [0 ].get ("published_at" ).replace ("T" ," " ).replace ("Z" ,"" ),'%Y-%m-%d %H:%M:%S' )
52
+ LatestRelAssetsURL = (json .loads (json .dumps (requests .get (url = r [0 ].get ("assets_url" ), params = {'address' :"yolo" }).json ())))[0 ].get ("browser_download_url" )
42
53
else :
43
- print ("WARNING: Failed to query GitHub API, you might be rate-limited. Using default fallback release instead." )
44
- latest_release = datetime (2023 , 7 , 23 )
45
- latest_release_assets_url = "https://github.com/OpenGOAL-Unofficial-Mods/OpenGoal-ModLauncher-dev/releases/download/v1.10fixoldpckernel/openGOALModLauncher.exe"
54
+ # fall back to some hard-coded release, surely we won't forget to update this occasionally
55
+ print ("WARNING: failed to query github API, you might be rate-limited. Using default fallback release instead." )
56
+ LatestRel = datetime (2023 , 7 , 23 )
57
+ LatestRelAssetsURL = "https://github.com/OpenGOAL-Unofficial-Mods/OpenGoal-ModLauncher-dev/releases/download/v1.10fixoldpckernel/openGOALModLauncher.exe"
46
58
47
- last_write = datetime (2020 , 5 , 17 )
48
- if os .path .exists (AppdataPATH + "\\ OpengoalModLauncher.exe" ):
49
- last_write = datetime .utcfromtimestamp (Path (AppdataPATH + "\\ OpengoalModLauncher.exe" ).stat ().st_mtime )
59
+ LastWrite = datetime (2020 , 5 , 17 )
60
+ if ( os .path .exists (AppdataPATH + "\\ OpengoalModLauncher.exe" ) ):
61
+ LastWrite = datetime .utcfromtimestamp ( pathlib . Path (AppdataPATH + "\\ OpengoalModLauncher.exe" ).stat ().st_mtime )
50
62
51
- need_update = bool ((last_write < latest_release ))
52
-
53
- window ['installed_version' ].update (f"Currently installed version created on: { last_write .strftime ('%Y-%m-%d %H:%M:%S' )} " )
54
- window ['newest_version' ].update (f"Newest version created on: { latest_release .strftime ('%Y-%m-%d %H:%M:%S' )} " )
55
-
56
- if need_update :
57
- window ['update_status' ].update ("An update is available. Click 'Update' to install." )
58
- window ['update_button' ].update (visible = True )
59
- window ['launch_button' ].update (visible = False )
60
- else :
61
- window ['update_status' ].update ("You are up to date." )
62
- window ['update_button' ].update (visible = False )
63
- window ['launch_button' ].update (visible = True )
64
-
65
- def download_newest_mod ():
66
- AppdataPATH = os .getenv ('APPDATA' ) + "\\ OpenGOAL-UnofficialModLauncher\\ "
67
-
68
- launch_url = "https://api.github.com/repos/OpenGOAL-Unofficial-Mods/OpenGoal-ModLauncher-dev/releases"
69
- response = requests .get (url = launch_url , params = {'address' : "yolo" })
70
-
71
- if response is not None and response .status_code == 200 :
72
- r = json .loads (json .dumps (response .json ()))
73
- latest_release = datetime .strptime (r [0 ].get ("published_at" ).replace ("T" , " " ).replace ("Z" , "" ),
74
- '%Y-%m-%d %H:%M:%S' )
75
- latest_release_assets_url = (json .loads (
76
- json .dumps (requests .get (url = r [0 ].get ("assets_url" ), params = {'address' : "yolo" }).json ())))[0 ].get (
77
- "browser_download_url" )
78
- else :
79
- print ("WARNING: Failed to query GitHub API, you might be rate-limited. Using default fallback release instead." )
80
- latest_release = datetime (2023 , 7 , 23 )
81
- latest_release_assets_url = "https://github.com/OpenGOAL-Unofficial-Mods/OpenGoal-ModLauncher-dev/releases/download/v1.10fixoldpckernel/openGOALModLauncher.exe"
63
+ #update checks
82
64
83
- last_write = datetime (2020 , 5 , 17 )
84
- if os .path .exists (AppdataPATH + "\\ OpengoalModLauncher.exe" ):
85
- last_write = datetime .utcfromtimestamp (Path (AppdataPATH + "\\ OpengoalModLauncher.exe" ).stat ().st_mtime )
65
+ needUpdate = bool ((LastWrite < LatestRel ))
86
66
87
- need_update = bool ((last_write < latest_release ))
67
+ print ("Currently installed version created on: " + LastWrite .strftime ('%Y-%m-%d %H:%M:%S' ))
68
+ print ("Newest version created on: " + LatestRel .strftime ('%Y-%m-%d %H:%M:%S' ))
88
69
89
- if need_update :
90
- window ['update_status' ].update ("Starting Update..." )
70
+ if (needUpdate ):
71
+
72
+ #start the actual update method if needUpdate is true
73
+ print ("Starting Update..." )
74
+ #download update from github
75
+ # Create a new directory because it does not exist
91
76
try_remove_dir (AppdataPATH + "/temp" )
92
77
if not os .path .exists (AppdataPATH + "/temp" ):
78
+ print ("Creating install dir: " + AppdataPATH )
93
79
os .makedirs (AppdataPATH + "/temp" )
94
80
95
- window ['update_status' ].update ("Downloading update from " + latest_release_assets_url )
96
- file = urllib .request .urlopen (latest_release_assets_url )
97
- urllib .request .urlretrieve (latest_release_assets_url , AppdataPATH + "/temp/OpengoalModLauncher.exe" , show_progress )
98
- window ['update_status' ].update ("Done downloading" )
99
-
100
- window ['update_status' ].update ("Removing previous installation " + AppdataPATH )
101
- try_remove_dir (AppdataPATH + "/data" )
102
- try_remove_file (AppdataPATH + "/gk.exe" )
103
- try_remove_file (AppdataPATH + "/goalc.exe" )
104
- try_remove_file (AppdataPATH + "/extractor.exe" )
105
-
106
- window ['update_status' ].update ("Extracting update" )
107
- temp_dir = AppdataPATH + "/temp"
108
- try_remove_file (temp_dir + "/updateDATA.zip" )
109
- sub_dir = temp_dir
110
- all_files = os .listdir (sub_dir )
111
- for f in all_files :
112
- shutil .move (sub_dir + "/" + f , AppdataPATH + "/" + f )
113
- try_remove_dir (temp_dir )
114
- window ['update_status' ].update ("Update complete" )
115
- window ['update_button' ].update (visible = False )
116
- window ['launch_button' ].update (visible = True )
117
-
118
- layout = [
119
- [sg .Text ("OpenGOAL Mod Updater" , font = ("Helvetica" , 16 ))],
120
- [sg .Text ("Installed Version:" , size = (20 , 1 )), sg .Text ("" , size = (20 , 1 ), key = 'installed_version' )],
121
- [sg .Text ("Newest Version:" , size = (20 , 1 )), sg .Text ("" , size = (20 , 1 ), key = 'newest_version' )],
122
- [sg .ProgressBar (100 , orientation = 'h' , size = (20 , 20 ), key = 'progress_bar' )],
123
- [sg .Text ("" , size = (40 , 1 ), key = 'update_status' )],
124
- [sg .Button ("Check for Updates" ), sg .Button ("Update" , visible = False , key = 'update_button' ), sg .Button ("Launch" , visible = False , key = 'launch_button' ), sg .Button ("Exit" )]
125
- ]
126
-
127
- window = sg .Window ("OpenGOAL Mod Updater" , layout , finalize = True )
128
-
129
- while True :
130
- event , values = window .read ()
131
- if event == sg .WIN_CLOSED or event == "Exit" :
132
- break
133
- elif event == "Check for Updates" :
134
- check_for_updates ()
135
- elif event == "update_button" :
136
- download_newest_mod ()
137
- elif event == "launch_button" :
138
- window .close ()
139
- subprocess .call ([ AppdataPATH + "openGOALModLauncher.exe" ])
140
-
141
- window .close ()
81
+ print ("Downloading update from " + LatestRelAssetsURL )
82
+ file = urllib .request .urlopen (LatestRelAssetsURL )
83
+ print (file .length )
84
+
85
+ urllib .request .urlretrieve (LatestRelAssetsURL , AppdataPATH + "/temp/OpengoalModLauncher.exe" , show_progress )
86
+ print ("Done downloading" )
87
+
88
+
89
+ #delete any previous installation
90
+ print ("Removing previous installation " + AppdataPATH )
91
+ try_remove_dir (InstallDir + "/data" )
92
+ try_remove_file (InstallDir + "/gk.exe" )
93
+ try_remove_file (InstallDir + "/goalc.exe" )
94
+ try_remove_file (InstallDir + "/extractor.exe" )
95
+ print ("Extracting update" )
96
+ TempDir = InstallDir + "/temp"
97
+
98
+
99
+ #delete the update archive
100
+ try_remove_file (TempDir + "/updateDATA.zip" )
101
+
102
+ SubDir = TempDir
103
+ print ("Moving files from " + SubDir + " up to " + InstallDir )
104
+ allfiles = os .listdir (SubDir )
105
+ for f in allfiles :
106
+ shutil .move (SubDir + "/" + f , InstallDir + "/" + f )
107
+ try_remove_dir (TempDir )
108
+
109
+ # check for launcher update
110
+ try :
111
+ AppdataPATH = os .getenv ('APPDATA' ) + "\\ OpenGOAL-UnofficalModLauncher\\ "
112
+ print (AppdataPATH )
113
+
114
+ if os .path .exists (AppdataPATH ) == False :
115
+ print ("Creating Directory " + AppdataPATH )
116
+ os .mkdir (AppdataPATH )
117
+
118
+ downloadNewestLauncher ()
119
+ except Exception as e :
120
+ print ("An unexcepted error occurred: " , e )
121
+ traceback .print_exc ()
122
+
123
+ # run launcher
124
+ try :
125
+ subprocess .call ([AppdataPATH + "OpengoalModLauncher.exe" ])
126
+ except Exception as e :
127
+ print ("An unexcepted error occurred: " , e )
128
+ traceback .print_exc ()
0 commit comments