-
Notifications
You must be signed in to change notification settings - Fork 0
/
nsdbg.py
executable file
·424 lines (312 loc) · 12.7 KB
/
nsdbg.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
#!/usr/bin/env python3
from abc import ABC, abstractmethod
import os
import argparse
import subprocess
import ntpath
from pathlib import Path
import logging
import sys
from io import BytesIO
from zipfile import ZipFile
from urllib.request import urlopen
import time
import signal
import psutil
try:
from protontricks import (
find_steam_path, find_proton_app,
get_steam_apps, get_steam_lib_paths,
util, winetricks
)
except ImportError:
raise Exception("nsdbg needs Protontricks to function")
SCRIPT = os.path.abspath(os.path.dirname(__file__))
CACHE_DIR = os.path.join(SCRIPT, "cache")
os.makedirs(CACHE_DIR, exist_ok=True)
def prepend_args(x, y, delim):
return (y + delim + x) if y else x
def append_args(x, y, delim):
return (x + delim + y) if y else x
def enable_logging(info=False):
level = logging.DEBUG if info else logging.INFO
logging.basicConfig(
stream=sys.stderr, level=level,
format="%(name)s (%(levelname)s): %(message)s")
# make Protontricks less verbose
pt_log = logging.getLogger("protontricks")
pt_log.setLevel(logging.WARNING)
def get_args():
parser = argparse.ArgumentParser(
prog="nsdbg",
description="Script to help debugging Northstar on Linux"
)
parser.add_argument("--compat", choices=["wine", "proton"], default="proton", help="Selects what compatibility layer will be used")
parser.add_argument("debugger", choices=["x64dbg", "winedbg"], help="Specify which debugger you want to run the game in")
parser.add_argument("--verbose", action="store_true", help="Enable verbose logging")
parser.add_argument("--no-ea", action="store_true", help="Don't start the EA App")
parser.add_argument("--persist-ea", action="store_true", help="Keep EA Desktop open")
args = parser.parse_args()
return args
pargs = get_args()
enable_logging(pargs.verbose)
log = logging.getLogger("nsdbg")
### Game ###
TITANFALL2_APPID = 1237970
class Game:
def __init__(self):
self.log = logging.getLogger("nsdbg.game")
self.game_dir = self.find_titanfall2()
self.log.info(f"Using game at {self.game_dir}")
def find_titanfall2(self):
game_dir = os.getenv("TF2_GAME_DIR")
if not game_dir:
self.log.info("TF2_GAME_DIR not specified, checking steam")
game_dir = self.__find_titanfall2_steam()
return game_dir
def __find_titanfall2_steam(self):
steam_path, steam_root = find_steam_path()
steam_lib_paths = get_steam_lib_paths(steam_path)
if not steam_lib_paths:
raise Exception("Could not find Steam libraries")
steam_apps = get_steam_apps(steam_root, steam_path, steam_lib_paths)
if not steam_apps:
raise Exception("Could not find any steam apps")
for a in steam_apps:
if a.appid == TITANFALL2_APPID:
return a.install_path
return None
### Game/ ###
### Compat ###
class CompatBase(ABC):
def __init__(self, game):
self.log = logging.getLogger("nsdbg.compat")
self.log.debug(f"Using {self.__class__.__name__}")
self.game = game
@abstractmethod
def run(self, *args, **kwargs):
...
@abstractmethod
def start_ea(self, **kwargs):
...
def is_ea_running(self) -> bool:
possible_names = [
"EADesktop.exe",
"EABackgroundSer"
]
ea_desktop_running = any(p.name() for p in psutil.process_iter(attrs=['name']) if p.name() in possible_names)
return ea_desktop_running
def wait_for_ea(self, attempts: int = 10):
# The EA app is not the fastest thing, give it a second to launch
counter = 0
self.log.info("Waiting for EA Desktop app to start")
while counter < attempts:
if self.is_ea_running():
return
time.sleep(5)
self.log.info("Skipping wait")
def maybe_start_ea(self, **kwargs):
if self.is_ea_running():
self.log.debug("EA App already running, not starting it again")
return None
return self.start_ea(**kwargs)
class CompatWine(CompatBase):
# Program Files\Electronic Arts\EA Desktop\EA Desktop
ea_desktop_path = ("Program Files", "Electronic Arts", "EA Desktop", "EA Desktop")
ea_desktop_exe = "EADesktop.exe"
def run(self, *pargs, **kwargs):
env_vars = dict(os.environ)
env_vars.setdefault("TERM", "xterm")
# Wine
env_vars.setdefault("WINEDEBUG", "-all")
env_vars["WINEDLLOVERRIDES"] = append_args("wsock32=n", env_vars.get("WINEDLLOVERRIDES"), ";")
# DXVK
env_vars.setdefault("DXVK_LOG_LEVEL", "none")
# VKD3D
env_vars.setdefault("VKD3D_DEBUG", "none")
env_vars.setdefault("VKD3D_SHADER_DEBUG", "none")
kwargs.update({
"close_fds": True,
"env": env_vars
})
cmd = ["wine"] + list(pargs)
self.log.debug(f"Running {cmd}")
return subprocess.Popen(
cmd,
**kwargs
)
def __get_wineprefix(self) -> str:
p = os.getenv("WINEPREFIX")
if p:
return p
# Wine explicitly uses $HOME
home = os.getenv("HOME")
if not home:
raise Exception("Unable to find Wine prefix")
return os.path.join(home, ".wine")
def __ea_installed(self) -> bool:
prefix_ea_exe = os.path.join(self.__get_wineprefix(), "drive_c", *self.ea_desktop_path, self.ea_desktop_exe)
return os.path.isfile(prefix_ea_exe)
def __winetricks(self, verb: str, **kwargs):
winetricks_path = winetricks.get_winetricks_path()
if not winetricks_path:
raise Exception("Failed to find winetricks")
winetricks_cmd = [winetricks_path, '--unattended'] + verb.split(' ')
log.debug(f"Installing '{verb}' via winetricks")
env_vars = dict(os.environ)
env_vars.setdefault("WINEDEBUG", "-all")
env_vars['WINETRICKS_LATEST_VERSION_CHECK'] = 'disabled'
env_vars['LD_PRELOAD'] = ''
p = subprocess.Popen(
winetricks_cmd,
**kwargs
)
p.wait()
return p
def start_ea(self, **kwargs):
if not self.__ea_installed():
self.log.info("EA Desktop was not found")
installer = os.path.join(self.game.game_dir, "__Installer", "Origin", "redist", "internal", "EAappInstaller.exe")
self.log.info("Installing needed dependencies")
self.__winetricks('d3dcompiler_47')
self.log.info("Installing EA Desktop")
self.run(installer).wait()
if not self.__ea_installed():
raise Exception("Failed to install EA Desktop")
ea_path = ntpath.join("C:\\", *self.ea_desktop_path, self.ea_desktop_exe)
self.log.info("Starting EA app")
p = self.run(ea_path, **kwargs)
self.wait_for_ea()
return p
class CompatProton(CompatBase):
compatdir = None
compattool = None
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.steam_path, steam_root = find_steam_path()
steam_lib_paths = get_steam_lib_paths(self.steam_path)
if not steam_lib_paths:
raise Exception("Could not find Steam libraries")
steam_apps = get_steam_apps(steam_root, self.steam_path, steam_lib_paths)
if not steam_apps:
raise Exception("Could not find any steam apps")
for a in steam_apps:
if a.appid == TITANFALL2_APPID and a.prefix_path:
self.compatdir = a.prefix_path
break
if not self.compatdir:
raise Exception("Failed to find Titanfall 2 prefix")
if self.compatdir.name == "pfx":
self.compatdir = (self.compatdir / "..").resolve()
self.log.debug(f"Compatibility directory: {self.compatdir}")
proton_app = find_proton_app(self.steam_path, steam_apps, TITANFALL2_APPID)
self.compattool = proton_app.install_path
if not self.compattool:
raise Exception("Failed to find Proton version")
self.log.debug(f"Compatibility tool: {self.compattool}")
def run(self, *args, **kwargs):
env_vars = dict(os.environ)
env_vars.setdefault("TERM", "xterm")
# Proton
env_vars.setdefault("SteamGameId", str(TITANFALL2_APPID))
env_vars.setdefault("SteamAppId", str(TITANFALL2_APPID))
env_vars.setdefault("STEAM_COMPAT_CLIENT_INSTALL_PATH", self.steam_path)
# Wine
env_vars.setdefault("WINELOADAERNOEXEC", "1")
env_vars["PATH"] = append_args(f"{self.compattool}/files/bin", env_vars.get("PATH"), ":")
env_vars.setdefault("WINEDEBUG", "-all")
env_vars["WINEDLLPATH"] = prepend_args(f"{self.compattool}/files/lib64/wine:{self.compattool}/files/lib/wine", env_vars.get("WINEDLLPATH"), ":")
env_vars.setdefault("LD_LIBRARY_PATH", append_args(f"{self.compattool}/files/lib64/:{self.compattool}/files/lib/:{self.game.game_dir}", env_vars.get("LD_LIBRARY_PATH"), ":"))
env_vars.setdefault("WINEPREFIX", self.__get_wineprefix())
env_vars.setdefault("WINEESYNC", "1")
env_vars.setdefault("WINEFSYNC", "1")
env_vars["WINEDLLOVERRIDES"] = append_args("wsock32=n,b;steam.exe=b;dotnetfx35.exe=b;dotnetfx35setup.exe=b;beclient.dll=b,n;beclient_x64.dll=b,n;d3d11=n;d3d10core=n;d3d9=n;dxgi=n;d3d12=n;d3d12core=n", env_vars.get("WINEDLLOVERRIDES"), ";")
env_vars.setdefault("WINE_LARGE_ADDRESS_AWARE", "1")
env_vars["GST_PLUGIN_SYSTEM_PATH_1_0"] = prepend_args(f"{self.compattool}/files/lib64/gstreamer-1.0:{self.compattool}/files/lib/gstreamer-1.0", env_vars.get("GST_PLUGIN_SYSTEM_PATH_1_0"), ":")
env_vars.setdefault("WINE_GST_REGISTRY_DIR", f"{self.compatdir}/gstreamer-1.0/")
# DXVK
env_vars.setdefault("DXVK_LOG_LEVEL", "none")
# VKD3D
env_vars.setdefault("VKD3D_DEBUG", "none")
env_vars.setdefault("VKD3D_SHADER_DEBUG", "none")
self.log.debug(f"Using prefix: {env_vars.get('WINEPREFIX')}")
kwargs.update({
"stdin": subprocess.DEVNULL,
"close_fds": True,
"env": env_vars
})
cmd = [f"{self.compattool}/files/bin/wine"] + list(args)
self.log.debug(f"Running {cmd}")
return subprocess.Popen(
cmd,
**kwargs
)
def __get_wineprefix(self) -> str:
return str(os.path.join(self.compatdir, "pfx"))
def start_ea(self, **kwargs):
# link2ea implicitly authenticates via Steam
p = self.run("steam.exe", "link2ea://launchgame/0?platform=steam&theme=tf2", **kwargs)
self.wait_for_ea()
return p
compat_map = {
"wine": CompatWine,
"proton": CompatProton,
}
#x = CompatWine()
### Compat/ ###
### Debugger ###
class DebuggerBase(ABC):
def __init__(self, game, compat):
self.log = logging.getLogger("nsdbg.debugger")
self.log.debug(f"Using {self.__class__.__name__}")
self.game = game
self.compat = compat
@abstractmethod
def run(self):
...
class DebuggerX64DBG(DebuggerBase):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.path = os.path.join(CACHE_DIR, "x64dbg")
os.makedirs(self.path, exist_ok=True)
path_64 = os.path.join(self.path, "release", "x64")
self.path_64_exe = os.path.join(path_64, "x64dbg.exe")
if not os.path.isfile(self.path_64_exe):
self.__download()
def __download(self):
self.log.info("Downloading x64dbg")
resp = urlopen("https://sourceforge.net/projects/x64dbg/files/latest/download")
archive = ZipFile(BytesIO(resp.read()))
self.log.info("Extracting x64dbg")
archive.extractall(path=self.path)
def run(self, *pargs, **kwargs):
self.log.info("Starting x64dbg")
kwargs.update({
"cwd": self.game.game_dir
})
return self.compat.run(self.path_64_exe, **kwargs)
class DebuggerWinedbg(DebuggerBase):
def run(self, *pargs, **kwargs):
return self.compat.run("winedbg", *pargs, **kwargs)
debug_map = {
"x64dbg": DebuggerX64DBG,
"winedbg": DebuggerWinedbg,
}
### Debugger/ ###
def main():
if os.name != "posix":
raise Exception("Running on unsupported Operating System")
g = Game()
c = compat_map[pargs.compat](g)
d = debug_map[pargs.debugger](g, c)
ea = None
if not pargs.no_ea:
# Spawn EA in a new session
ea = c.maybe_start_ea(preexec_fn=os.setsid)
d.run().wait()
if not pargs.persist_ea and ea:
log.info("Terminating EA Desktop")
# Kill the whole session
os.killpg(os.getpgid(ea.pid), signal.SIGTERM)
if __name__ == "__main__":
main()