forked from jose-cavalo-se71/integration-steam
-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a44df8b
commit eb1d70a
Showing
13 changed files
with
356 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import sys | ||
import pathlib | ||
import shutil | ||
import os | ||
import typing as t | ||
import psutil | ||
import time | ||
from collections import Counter | ||
|
||
|
||
STEAM_PATH = R'C:\Program Files (x86)\Steam\steam.exe' | ||
GALAXY_PATH = R'C:\Program Files (x86)\GOG Galaxy\GalaxyClient.exe' | ||
NETHOOK_PATH = None # R'C:COMPLETE_ME\\NetHook2\NetHook2.dll' | ||
|
||
|
||
def find_steam_ps(initial: t.Optional[psutil.Process]=None) -> t.Optional[psutil.Process]: | ||
if initial and initial.is_running(): | ||
return initial | ||
for p in psutil.process_iter(['exe'], ad_value=''): | ||
if STEAM_PATH == p.info['exe']: | ||
return p | ||
return None | ||
|
||
|
||
def restart_steam(): | ||
p = psutil.Popen([STEAM_PATH, "-shutdown", "-silent"]) | ||
p.wait() | ||
steam_ps = find_steam_ps() | ||
if steam_ps: | ||
steam_ps.wait(timeout=10) | ||
return psutil.Popen(STEAM_PATH) | ||
|
||
|
||
def restart_galaxy(): | ||
p = psutil.Popen([GALAXY_PATH, '/command=shutdown']) | ||
p.wait() | ||
psutil.Popen(GALAXY_PATH) | ||
|
||
|
||
def inject_nethook(steam_ps=None): | ||
"""Requires running as admin""" | ||
timeout = time.time() + 10 | ||
while True: | ||
steam_ps = find_steam_ps(steam_ps) | ||
assert steam_ps, 'NetHook requires Steam running' | ||
if time.time() > timeout: | ||
print('timeout on loading steamclint.dll by Steam') | ||
break | ||
if [dll for dll in steam_ps.memory_maps() if 'steamclient.dll' in dll.path]: | ||
print('steamclient.dll loaded. Injecting nethook') | ||
break | ||
cmd = f'rundll32 "{NETHOOK_PATH}",Inject' | ||
psutil.Popen(cmd) | ||
|
||
|
||
def copy_results_to_common_dir(dest_dir, must_include=''): | ||
os.makedirs(dest_dir, exist_ok=True) | ||
steam_nethook_dir = pathlib.PurePath(STEAM_PATH).parent / 'nethook' | ||
for d in os.listdir(steam_nethook_dir): | ||
if steam_nethook_dir.name == d: # avoid recurssion | ||
continue | ||
for f in os.listdir(steam_nethook_dir / d): | ||
if must_include not in f: | ||
continue | ||
src = steam_nethook_dir / d / f | ||
dest = pathlib.PurePath(dest_dir) / (src.name + '_' + d + src.suffix) # originalname_timestamp.bin | ||
shutil.copy(src, dest) | ||
|
||
|
||
def print_statistics(first_signals=10): | ||
steam_nethook_dir = pathlib.PurePath(STEAM_PATH).parent / 'nethook' | ||
signals = Counter() | ||
for root, dirs, files in os.walk(steam_nethook_dir): | ||
signal_names = [] | ||
for i, f in enumerate(sorted(files)): | ||
if i > first_signals: | ||
break | ||
signal_names.append(f.split('_')[-1]) | ||
signals.update(signal_names) | ||
print(signals) | ||
|
||
|
||
def main(): | ||
if NETHOOK_PATH is None: | ||
print('set up nethook path in script first') | ||
|
||
try: | ||
no = int(sys.argv[1]) | ||
except (IndexError, ValueError): | ||
print('Usage: <script> <number_of_restarts> <path_to_copy_chosen_signals>') | ||
return | ||
|
||
try: | ||
output = sys.argv[2] | ||
except IndexError: | ||
output = 'output_nethook' | ||
|
||
for i in range(no): | ||
try: | ||
print(f'Running {i+1}. time...') | ||
steam_proc: psutil.Popen = restart_steam() | ||
inject_nethook(steam_proc) | ||
time.sleep(12) # Steam startup | ||
except Exception as e: | ||
print(repr(e)) | ||
continue | ||
|
||
print_statistics() | ||
copy_results_to_common_dir(output, must_include='ClientLogOnResponse') | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import base64 | ||
import logging | ||
|
||
from persistent_cache_state import PersistentCacheState | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class LocalMachineCache: | ||
|
||
def __init__(self, persistent_cache: dict, cache_state: PersistentCacheState): | ||
self._cache_state = cache_state | ||
self._persistent_cache = persistent_cache | ||
|
||
@property | ||
def machine_id(self) -> bytes: | ||
return base64.b64decode(self._persistent_cache.get('machine_id', '')) | ||
|
||
@machine_id.setter | ||
def machine_id(self, machine_id: bytes): | ||
self._persistent_cache['machine_id'] = base64.b64encode(machine_id).decode('utf-8') | ||
self._cache_state.modified = True | ||
logger.info("Set new machine ID: %s" % machine_id) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.