-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathupdate_factorio.py
446 lines (384 loc) · 14.4 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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
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 time
from fa_paths import BIN, TEMP, PLAYER_DATA
from shutil import rmtree
from sys import platform
debug = False
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 = "./"
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":
info_url = urllib.response.addinfourl(fp, headers, req.get_full_url())
info_url.status = code
return info_url
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
)
# cSpell:words cloudfare addheaders KHTML
# 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_prompt():
username = ""
while not re.fullmatch(r"[\w.-]+", username):
username = input("Factorio Username:")
token = ""
while True:
print(
"To get your service token, which is required for updates, and many 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 disappeared. 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): # cSpell:disable-line
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")
buff_size = 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(buff_size)
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("deleting " + dirname)
rmtree(dirname)
def overwrite_factorio_install_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, exist_ok=True)
filename = os.path.join(TEMP, "factorio-" + version + "-" + download_package)
print("Downloading version " + version)
download(
f"https://www.factorio.com/get-download/{version}/alpha/{download_package}",
filename,
)
overwrite_factorio_install_from_new_zip(filename)
def set_player_data(player):
with open(PLAYER_DATA, "w", encoding="utf8") as player_file:
json.dump(player, player_file, ensure_ascii=False, indent=2)
def get_player_data(quiet=False):
try:
with open(PLAYER_DATA, encoding="utf8") as player_file:
return json.load(player_file)
except FileNotFoundError:
if not quiet:
print(
"Player data does not exist yet. Please start the game in single player first."
)
return None
def get_credentials(quiet=False, reset=False):
player = get_player_data(quiet)
if reset:
player["service-username"] = ""
if not player["service-username"] or not player["service-token"]:
log_res = service_token_prompt() # 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([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("checking for Factorio 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
available = json.load(resp)
if not available:
print("couldn't get any updates")
return None
if current_version["package"] not in available:
print("no available versions match package. Versions are:")
for ver in available.keys():
print("\t", ver)
return None
versions = available[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,
current_version["package"]
+ "-"
+ update["from"]
+ "-"
+ update["to"]
+ "-update.zip",
)
def prep_update(credentials, current_version, update_candidates):
os.makedirs(TEMP, exist_ok=True)
params = credentials.copy()
params["package"] = current_version["package"]
params["apiVersion"] = 2
params["isTarget"] = "false"
for i, update in enumerate(update_candidates):
if i + 1 == len(update_candidates):
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 process_exists():
# cSpell:disable-next-line
call = "TASKLIST", "/FI", "imagename eq Factorio.exe"
if subprocess.check_output(call).splitlines()[3:]:
return True
def execute_update(current_version, update_candidates):
applying = re.compile(r"Applying update .*-(\d+\.\d+\.\d+)-update")
print(current_version, update_candidates)
params = [BIN]
for update in update_candidates:
file = os.path.abspath(update_filename(current_version, update))
params.append("--apply-update")
params.append(file)
print(params)
child = subprocess.Popen(params, stdout=subprocess.PIPE)
if current_version["package"] == "core-win64":
# the windows UAC makes it so we can't monitor the output
while process_exists():
time.sleep(1)
else:
for line in child.stdout:
print(line)
if m := applying.search(line.decode()):
print(f"Applying Update:{m[1]}")
elif debug:
print("--", line.decode(), end="")
def cleanup_update(current_version, update_candidates):
for update in update_candidates:
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
print(f"Version: {current_version['from']}")
connection = http.client.HTTPSConnection("updater.factorio.com")
update_candidates = check_for_updates(credentials, connection, current_version)
connection.close()
if not update_candidates:
print("no updates available")
return False
if confirm:
input("update to " + update_candidates[-1]["to"] + "? Enter to continue.")
else:
print("updating to " + update_candidates[-1]["to"])
prep_update(credentials, current_version, update_candidates)
execute_update(current_version, update_candidates)
cleanup_update(current_version, update_candidates)
print("all-done")
if "__main__" == __name__:
do_update()