forked from Factorio-Access/FactorioAccess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_factorio.py
377 lines (327 loc) · 13.2 KB
/
update_factorio.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
import json
import subprocess
import re
import http, http.client, http.cookiejar
import urllib
import urllib.request
import os
import getpass
import zipfile
import webbrowser
import fa_paths
import time
from shutil import rmtree
from sys import platform
download_package_map = {
"win32":"win64-manual",
"Darwin":"osx",
"linux":"linux64"
}
download_package = download_package_map[platform]
if not download_package:
raise ValueError("Unsupported Platform:"+platform)
package_map = {
('win64','full'):"core-win64",
('linux64','full'):"core-linux64",
('mac','full'):"core-mac",
}
FACTORIO_INSTALL_PATH = "./"
PLAYER_DATA_PATH = os.path.join(fa_paths.WRITE_DIR, "player-data.json")
TEMP_PATH = os.path.join(fa_paths.WRITE_DIR, 'temp')
class NoRedirection_for_get_token_e(urllib.request.HTTPErrorProcessor):
def https_response(self, request, response):
if response.code == 302 and request.full_url == "https://www.factorio.com/get-token":
return response
return super().https_response(request,response)
class NoRedirectHandler(urllib.request.HTTPRedirectHandler):
def http_error_302(self, req, fp, code, msg, headers):
if req.full_url == "https://www.factorio.com/get-token":
infourl = urllib.response.addinfourl(fp, headers, req.get_full_url())
infourl.status = code
return infourl
return super().http_error_302(req, fp, code, msg, headers)
opener = urllib.request.build_opener(
NoRedirection_for_get_token_e(),
urllib.request.HTTPCookieProcessor(http.cookiejar.CookieJar()),
urllib.request.HTTPSHandler(debuglevel=0) #change to 1 for testing 0 for production
)
#cloudfare rejects the default user agent
opener.addheaders = [('User-agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36')]
def prompt_login():
username=""
while len(username) == 0 :
username=input("Factorio Username:")
password=""
while len(password) == 0 :
password=getpass.getpass("Factorio Password: ")
return {
"username":username,
"password":password
}
def service_token_promt():
username=""
while len(username) == 0 :
username=input("Factorio Username:")
token=''
while True:
print("To get your service token, which is required for updates, and most multiplayer functions, please follow the instructions below:")
print("1. Go to https://factorio.com/profile in your browser. An option to launch is at the end of the instructions.")
print('2. Once logged in and on your profile page, Click the link with the text "reveal".')
print("3. Once clicked, your token string will be just before the link that will have disapeared. The token consists of a string of 30 numbers and letters between a and f. The text after the token starts with an i.")
token=input("4. Enter your token here, or l to to to open the page for you, or n to skip for now.")
token=token.strip()
if re.fullmatch(r'[nN][oO]?',token):
token=''
break
if re.fullmatch(r'[\da-f]{30}',token):
break
if re.fullmatch(r'[Ll](aunch)?',token):
webbrowser.open('https://factorio.com/profile')
continue
print("The token entered did not match the expected format. Please try again, or enter no to skip")
return {"username":username,"token":token}
def api_log_in():
params=prompt_login()
params['api_version']='4'
params['require_game_ownership']='true'
encoded_params= urllib.parse.urlencode(params).encode('utf-8')
with opener.open('https://auth.factorio.com/api-login',encoded_params) as response:
json_resp = json.load(response)
return json_resp
def scrape_CSRF_token(page_html):
for input_element in re.finditer(r'<\s*input([^>]*)>',page_html,re.IGNORECASE):
input_html = input_element.group(1)
if re.search(r'\bname\s*=[\s"]*csrf_token[\s"]', input_html, re.IGNORECASE):
token_match = re.search(r'\bvalue\s*=\s*"([^\s"]+)"',input_html,re.IGNORECASE)
if token_match:
return token_match.group(1)
return None
def scrape_username(page_html):
token_match = re.search(r'\bhref\s*=\s*"/profile"[^>]*>\s*([^<\s]+)\s*<',page_html,re.IGNORECASE)
if token_match:
return token_match.group(1)
return None
def site_log_in():
with opener.open("https://www.factorio.com/login") as response:
page_html=response.read().decode()
username = scrape_username(page_html)
if username:
return username
token = scrape_CSRF_token(page_html)
if not token:
print("login CSRF token not found")
return False
cred = prompt_login()
params={
'csrf_token':token,
'next_url':'',
'next_mods':'False',
'username_or_email':cred['username'],
'password':cred['password']
}
encoded_params= urllib.parse.urlencode(params).encode('utf-8')
req = urllib.request.Request("https://www.factorio.com/login",
encoded_params,
{'referer':'https://www.factorio.com/login'}
)
with opener.open(req) as response:
html = response.read().decode()
username=scrape_username(html)
if not username:
print(html)
return username
def get_service_token_through_site():
req = urllib.request.Request("https://www.factorio.com/get-token"
,b""
,{'accept':'application/json, text/javascript, */*; q=0.01'}
)
with opener.open(req) as response:
print(response)
data = response.read()
print(data)
json_token = json.load(data.decode())
return json_token.token
def get_latest_stable():
with opener.open("https://factorio.com/api/latest-releases") as response:
json_page = json.load(response)
return json_page['stable']['alpha']
def download(url,filename):
with open(filename,'wb') as fp, opener.open(url) as dl:
print(f"saving {url} to {filename}")
length = dl.getheader('content-length')
buffsize = 4096
if length:
length = int(length)
if length > 4096*20:
print(f'Downloading {length} bytes')
bytes_done = 0
last_percent = -1
last_reported=time.time()
while True:
buffer = dl.read(buffsize)
if not buffer:
break
fp.write(buffer)
bytes_done += len(buffer)
if length:
percent = bytes_done*100//length
if percent>last_percent and time.time()>= 5 + last_reported:
print(f"{percent}%")
last_percent=percent
last_reported= time.time()
if length and length > 4096*20:
print("Done")
def delete_dir_if_exists(dirname):
if os.path.exists(dirname):
print("deleteing "+dirname)
rmtree(dirname)
def overwrite_factorio_intall_from_new_zip(filename):
delete_dir_if_exists(FACTORIO_INSTALL_PATH+'bin')
delete_dir_if_exists(FACTORIO_INSTALL_PATH+'data')
delete_dir_if_exists(FACTORIO_INSTALL_PATH+'doc-html')
print("extracting new installation")
with zipfile.ZipFile(filename) as zp:
my_path = zipfile.Path(zp)
nested_dir = next(my_path.iterdir())
print(nested_dir.name)
#zp.extractall(FACTORIO_INSTALL_PATH)
print("done extracting. Deleting download.")
#function totally a work in progress don't attempt to use
def install():
username=site_log_in()
if not username:
print("Login Failed")
return False
#token=get_service_token_through_site()
version = input('Enter version to download. Leave blank for latest stable:')
if not version:
version= get_latest_stable()
os.makedirs(TEMP_PATH, exist_ok=True)
filename=TEMP_PATH+'factorio-'+version+'-'+download_package
print("Downloading version "+version)
download(f"https://www.factorio.com/get-download/{version}/alpha/{download_package}",filename)
overwrite_factorio_intall_from_new_zip(filename)
def set_player_data(player):
with open(PLAYER_DATA_PATH,'w') as player_file:
json.dump(player,player_file)
def get_player_data():
with open(PLAYER_DATA_PATH) as player_file:
return json.load(player_file)
def get_credentials(quiet=False):
if not os.path.exists(PLAYER_DATA_PATH):
if not quiet:
print("Player data does not exist yet. Please start the game in single player first.")
return None
player = get_player_data()
if not player["service-username"] or not player["service-token"]:
log_res = service_token_promt()#api_log_in()
if not log_res:
print("Not logged in")
return None
player["service-username"] = log_res['username']
player["service-token"] = log_res['token']
set_player_data(player)
return {
"username":player["service-username"],
"token":player["service-token"]
}
def get_current_version():
version_str = subprocess.check_output(fa_paths.BIN + " --version").decode('utf-8')
version_re = r"Version:\s*([\d\.]+)\s*\(\s*([^,]+),\s*([^,]+),\s*([^)]+)\)"
maybe_match = re.match(version_re , version_str)
if not maybe_match:
print("could not match version string", version_str)
return None
groups= maybe_match.groups()
check_type = (groups[2],groups[3])
if not check_type in package_map:
print("could not identify package type from:", (groups[2],groups[3]))
return None
return {"from":groups[0],"package":package_map[check_type]}
def check_for_updates(credentials,connection,current_version):
print("cheing for factotio updates...")
params=credentials.copy()
params["apiVersion"]=2
connection.request("GET",'/get-available-versions?'+urllib.parse.urlencode(credentials))
resp = connection.getresponse()
if resp.status != 200:
print("error: "+ resp.status + " " + resp.reason)
return None
availble = json.load(resp)
if not availble:
print("couldn't get any updates")
return None
if current_version['package'] not in availble:
print("no available verions match package. Versions are:")
for ver in availble.keys():
print('\t',ver)
return None
versions = availble[current_version['package']]
upgrade_list=[]
version = current_version['from']
for upgrade in versions:
if 'stable' in upgrade:
stable = upgrade['stable']
found = True
while found and version!=stable:
found=False
for upgrade in versions:
if 'from' in upgrade and upgrade['from']==version:
upgrade_list.append(upgrade)
version = upgrade['to']
found=True
break
return upgrade_list
def update_filename(current_version,update):
return os.path.join(TEMP_PATH,current_version['package']+'-'+update['from']+'-'+update['to']+'-update.zip')
def prep_update(credentials, current_version, update_canidates):
os.makedirs(TEMP_PATH, exist_ok=True)
params=credentials.copy()
params['package']=current_version['package']
params['apiVersion']=2
params['isTarget']='false'
for i, update in enumerate(update_canidates):
if i+1 == len(update_canidates):
params['isTarget']='true'
this_params = params | update
print('Downloading '+update['to'])
download(f"https://updater.factorio.com/updater/get-download?"+urllib.parse.urlencode(this_params),update_filename(params,update))
print('Finished Downloads')
return
def execute_update(current_version, update_canidates):
print(current_version,update_canidates)
params=[fa_paths.BIN]
for update in update_canidates:
file = os.path.abspath(update_filename(current_version,update))
params.append('--apply-update')
params.append(file)
print(params)
print(subprocess.check_output(params).decode('utf-8'))
#todo subprocess spawns another process and exits, casueing the cleanup to proceed before the update completes.
def cleanup_update(current_version, update_canidates):
for update in update_canidates:
file = os.path.abspath(update_filename(current_version,update))
os.remove(file)
def do_update(confirm=True):
credentials = get_credentials()
if not credentials:
return False
current_version = get_current_version()
if not current_version:
return False
connection = http.client.HTTPSConnection("updater.factorio.com")
update_canidates = check_for_updates(credentials,connection,current_version)
connection.close()
if not update_canidates:
print('no updates available')
return False
if confirm:
input("update to "+update_canidates[-1]['to']+'? Enter to continue.')
else:
print("updating to "+update_canidates[-1]['to'])
prep_update(credentials, current_version, update_canidates)
execute_update(current_version, update_canidates)
cleanup_update(current_version, update_canidates)
print("all-done")
if "__main__" == __name__:
do_update()