Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
469 lines (440 loc) · 13.4 KB
/
Copy pathmain.py
File metadata and controls
469 lines (440 loc) · 13.4 KB
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
import asyncio
from datetime import UTC, datetime
from os import environ, path
from pathlib import Path
from re import split
from subprocess import Popen, run
from sys import argv, platform, stdout
from time import sleep
from traceback import print_tb
from urllib import parse
from xml import parsers
from aiohttp import ClientSession
from discord import File, Object, Webhook
from dotenv import load_dotenv
from requests import exceptions, get
from rss_parser import RSSParser
_print = print
def print(*args, **kwargs): # type: ignore
_print(f'[{datetime.now(UTC)}]', *args, **kwargs) # type: ignore
stdout.flush()
STEAM_COMPAT_DATA_PATH = path.join(
Path.home(),
'.local',
'share',
'Steam',
'steamapps',
'compatdata',
'2016590',
)
STEAM_PATH = path.join(
Path.home(),
'.local',
'share',
'Steam',
)
TAVERN_PATH = path.join(
Path.home(),
'.local',
'share',
'Steam',
'steamapps',
'common',
'Dark and Darker',
'Tavern.exe',
)
PROTON_PATH = path.join(
Path.home(),
'.local',
'share',
'Steam',
'steamapps',
'common',
'Proton - Experimental',
'proton',
)
STEAMCMD_PATH = path.join(
Path.home(),
'.local',
'share',
'Steam',
'steamcmd',
'steamcmd.sh',
)
USMAP_PATH = path.join(
Path.home(),
'.local',
'share',
'Steam',
'steamapps',
'common',
'Dark and Darker',
'DungeonCrawler',
'Binaries',
'Win64',
'Mappings.usmap',
)
UE4SS_PATH = path.join(
Path.cwd(),
'UE4SS',
)
WIN64_PATH = path.join(
Path.home(),
'.local',
'share',
'Steam',
'steamapps',
'common',
'Dark and Darker',
'DungeonCrawler',
'Binaries',
'Win64',
)
UPDATE_CHECK_MINUTES = 10
def main():
print('Checking requirements...')
if check_requirements() > 0:
return
print(
'All requirements passed. '
f'Now checking for an update every {UPDATE_CHECK_MINUTES} minutes.'
)
current_build = 0
current_version = ''
backoff = 30
while True:
# Check if Dark and Darker has a newer build
fetch_build = get_latest_build()
fetch_version = get_latest_version()
print(
f'{current_build} - {current_version}'
f' vs {fetch_build} - {fetch_version}'
)
if fetch_build <= current_build or fetch_version == current_version:
sleep(
60 * UPDATE_CHECK_MINUTES
) # Build did not change, wait x minutes
continue
if current_build == 0 or current_version == '':
current_build = fetch_build
current_version = fetch_version
if '-f' not in argv:
sleep(
60 * UPDATE_CHECK_MINUTES
) # Build did not change, wait x minutes
continue
print('Dark and Darker has updated!')
current_build = fetch_build
current_version = fetch_version
print('Running SteamCMD...')
steamcmd_proc = run(
[
'bash',
STEAMCMD_PATH,
'+login '
f'{environ.get("STEAM_USERNAME")} '
f'{environ.get("STEAM_PASSWORD")} '
f'{get_steamguard()}',
'+app_update 2016590',
'+quit',
],
capture_output=True,
text=True,
)
while (
steamcmd_proc.returncode > 0
or 'ERROR' in steamcmd_proc.stdout
or (
'-f' not in argv
and 'already up to date' in steamcmd_proc.stdout
)
):
print('SteamCMD STDOUT: \n', steamcmd_proc.stdout)
print('SteamCMD STDERR: \n', steamcmd_proc.stderr)
print(
'SteamCMD exited with code',
steamcmd_proc.returncode,
'Retrying in ',
backoff,
' seconds...',
)
sleep(backoff)
backoff = backoff + backoff # exponential backoff
steamcmd_proc = run(
[
'bash',
STEAMCMD_PATH,
'+login '
f'{environ.get("STEAM_USERNAME")} '
f'{environ.get("STEAM_PASSWORD")} '
f'{get_steamguard()}',
'+app_update 2016590',
'+quit',
],
capture_output=True,
text=True,
)
print('Updated Dark and Darker successfully.')
backoff = 30
print('Copying UE4SS to Dark and Darker...')
copy_proc = run(
[
'cp',
'-rf',
f'{UE4SS_PATH}/.',
WIN64_PATH,
],
capture_output=True,
text=True,
)
if copy_proc.returncode > 0:
raise ValueError(
'Copy operation failed! '
f'Return code: {copy_proc.returncode}\n'
f'STDOUT:\n{copy_proc.stdout}\n'
f'STDERR:\n{copy_proc.stderr}'
)
print('UE4SS installed successfully.')
# Removes USMap file, if one already exists
Path.unlink(Path(USMAP_PATH), True)
while not path.exists(USMAP_PATH):
print('Running Dark and Darker...')
proton_env = environ.copy()
proton_env['PROTON_NO_ESYNC'] = '1'
proton_env['PROTON_LOG'] = '1'
proton_env['WINEDEBUG'] = '+timestamp,+pid,+tid'
',+seh,+debugstr,+module'
proton_env['STEAM_COMPAT_CLIENT_INSTALL_PATH'] = STEAM_PATH
proton_env['STEAM_COMPAT_DATA_PATH'] = STEAM_COMPAT_DATA_PATH
proton_env['PROTON_USE_XALIA'] = '0'
Popen(
[
'xvfb-run',
'-e',
'/dev/stdout',
'-n',
'98',
PROTON_PATH,
'run',
TAVERN_PATH,
'-server=localhost',
'-steam=1',
'-taverntype=steam',
'-tavernapp=dad',
'-nullrhi',
'-nullrhi',
'-nosound',
'-unattended',
'-dx11',
],
text=True,
env=proton_env,
)
missing_times = 0
# Check if USMap file exists
while missing_times <= 6 and not path.exists(USMAP_PATH):
sleep(20)
print('Checking for USMap file...')
missing_times += 1
if not path.exists(USMAP_PATH):
print('Assuming this run failed. Restarting Dark and Darker...')
nuke_wine()
sleep(1)
nuke_wine()
print('Found USMap!')
print('Sending USMap to webhook...')
asyncio.run(send_usmap_to_webhook(current_build))
print('Sent USMap to webhook!')
# Loop here
# Nukes wine processes
def nuke_wine():
run(['killall', '-SIGKILL', 'winedevice.exe'])
run(
[
'killall',
'-SIGTERM',
'wineserver',
'Tavern.exe',
'TavernDart.exe',
'TavernWorker.exe',
'DungeonCrawler.exe',
'GameThread',
'services.exe',
'explorer.exe',
'conhost.exe',
'tabtip.exe',
'plugplay.exe',
'rpcss.exe',
'svchost.exe',
'xalia.exe',
'steam.exe',
'crashpad_handle',
]
)
async def send_usmap_to_webhook(build: int):
version = get_latest_version()
with open(USMAP_PATH, 'rb') as map_file:
discord_file = File(
fp=map_file,
filename=parse.quote(
f'{version}.usmap', safe='/', encoding=None, errors=None
),
)
async with ClientSession() as session:
webhook = Webhook.from_url(
f'{environ.get("WEBHOOK_URL")}', session=session
)
threadid = environ.get('DISCORD_THREAD_ID')
roleid = environ.get('DISCORD_ROLE_ID')
roleid = (
f'-# <@&{roleid}> - Click Channels & Roles to get this role'
if roleid
else ''
)
if threadid and int(threadid) > 0:
await webhook.send(
f'New usmap from version {version}'
f' - Steam build {str(build)}\n'
'-# Source: <https://github.com/ynot01/headless-darker>\n'
f'{roleid}',
file=discord_file,
thread=Object(threadid),
)
else:
await webhook.send(
f'New usmap from version {version}'
f' - Steam build {str(build)}\n'
'-# Source: <https://github.com/ynot01/headless-darker>\n'
f'{roleid}',
file=discord_file,
)
def get_steamguard() -> str:
steamguard_proc = run(
['steamguard', '--username', f'{environ.get("STEAM_USERNAME")}'],
capture_output=True,
text=True,
)
backoff = 30
while steamguard_proc.returncode > 0:
print('Steamguard-CLI STDOUT: \n', steamguard_proc.stdout)
print('Steamguard-CLI STDERR: \n', steamguard_proc.stderr)
print(
'Steamguard-CLI exited with code',
steamguard_proc.returncode,
'Retrying in 30 seconds...',
)
sleep(backoff)
backoff = backoff + backoff # exponential backoff
steamguard_proc = run(
['steamguard', '--username', f'{environ.get("STEAM_USERNAME")}'],
capture_output=True,
text=True,
)
backoff = 30
return steamguard_proc.stdout
def check_requirements() -> int:
if platform != 'linux':
print(
'This project is only supports Linux servers and your platform is',
platform,
)
if platform == 'win32' or platform == 'cygwin' or platform == 'msys':
print(
'On Windows, you can try running the project via WSL: '
'https://learn.microsoft.com/en-us/windows/wsl/install'
)
return 1
load_dotenv()
if environ.get('STEAM_USERNAME') is None:
print('STEAM_USERNAME environment variable was not defined')
return 1
if environ.get('STEAM_PASSWORD') is None:
print('STEAM_PASSWORD environment variable was not defined')
return 1
if not path.isdir(STEAM_PATH):
print('Steam was not found at $HOME/.local/share/Steam/')
return 1
if not path.exists(TAVERN_PATH):
print(
'Tavern.exe was not found at '
"$HOME/.local/share/Steam/steamapps/common/'Dark and Darker'/"
)
return 1
if not path.isdir(UE4SS_PATH):
print(
'UE4SS folder not found in current working directory. '
'Please run from the project root!'
)
return 1
if not path.exists(PROTON_PATH):
print(
'proton executable was not found at '
"$HOME/.local/share/Steam/steamapps/common/'Proton - Experimental'/"
)
return 1
if not path.exists(STEAMCMD_PATH):
print(
'steamcmd.sh was not found at '
'$HOME/.local/share/Steam/steamapps/steamcmd/'
)
return 1
if not path.exists(
path.join(
Path.home(),
'.config',
'steamguard-cli',
'maFiles',
f'{environ.get("STEAM_USERNAME")}.maFile',
)
):
print(
'steamguard-cli credential file was not found at '
f'$HOME/.config/steamguard-cli/maFiles/{environ.get("STEAM_USERNAME")}.maFile'
)
return 1
if not path.exists(
path.join(
Path.home(),
'.config',
'steamguard-cli',
'maFiles',
'manifest.json',
)
):
print(
'manifest.json was not found at '
'$HOME/.config/steamguard-cli/maFiles/'
)
return 1
return 0
def get_latest_version() -> str:
try:
version_url = 'http://cdn.darkanddarker.com/Dark%20and%20Darker/Build/BuildVersion.txt'
return get(version_url).text.rstrip()
except exceptions.RequestException as err:
print_tb(err.__traceback__)
print(err.__class__, ':', err)
return 'UnknownVersion'
def get_latest_build() -> int:
try:
rss_url = 'https://steamdb.info/api/PatchnotesRSS/?appid=2016590'
response = get(rss_url)
rss = RSSParser.parse(response.text)
largest_build = 0
for item in rss.channel.items:
splitbuild = split('#', item.guid.content)
if len(splitbuild) > 1:
buildnum = int(splitbuild[1])
if buildnum > largest_build:
largest_build = buildnum
return largest_build
except exceptions.RequestException as err:
print_tb(err.__traceback__)
print(err.__class__, ':', err)
return 0
except parsers.expat.ExpatError as err:
print_tb(err.__traceback__)
print(err.__class__, ':', err)
return 0
if __name__ == '__main__':
main()