From 421f8032c80636156ab78cef0742fce71f09f3f4 Mon Sep 17 00:00:00 2001 From: Adam Johnson Date: Mon, 16 Jan 2023 18:55:02 -0500 Subject: [PATCH] Re-fix the TPotS launching race condition. This was fixed in H-uru/libhsplasma#242 originally, but Win32 APIs seem to have terrible performance without user mode buffering, which negatively impacted PRP loading in other tools, such as PRP Shop. Therefore, the fix was reverted in H-uru/libhsplasma#246. This fixes the race condition closer to the point of impact using the Win32 stream introduced by H-uru/libhsplasma#264. --- cmake/Dependencies.cmake | 2 +- korman/plasma_launcher.py | 43 +++++++++++++++++++++++++++++---------- 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/cmake/Dependencies.cmake b/cmake/Dependencies.cmake index 4a4ff3b..311d024 100644 --- a/cmake/Dependencies.cmake +++ b/cmake/Dependencies.cmake @@ -208,7 +208,7 @@ if(korman_BUILD_HSPLASMA) korman_add_external_project(HSPlasma GIT_REPOSITORY "https://github.com/H-uru/libhsplasma.git" # Be sure to increase this as the feature set used by Korman increases - GIT_TAG 4a1fd38dca471008de62209cc89616351161a843 + GIT_TAG 6fa56d979163af8602cd6212449a07400a14b532 # We can only do shallow checkouts if the above is a branch or tag. GIT_SHALLOW FALSE CMAKE_CACHE_ARGS diff --git a/korman/plasma_launcher.py b/korman/plasma_launcher.py index 4937b87..70d36c5 100644 --- a/korman/plasma_launcher.py +++ b/korman/plasma_launcher.py @@ -14,6 +14,7 @@ # along with Korman. If not, see . import argparse +from contextlib import contextmanager from pathlib import Path from PyHSPlasma import * import shutil @@ -55,6 +56,28 @@ def die(*args, **kwargs): sys.stdout.write("DIE\n") sys.exit(1) +@contextmanager +def open_vault_stream(vault_path, fm): + stream_type = globals().get("hsWindowsStream", "hsFileStream") + write("DBG: Opened '{}' stream with provider '{}'", vault_path, stream_type.__name__) + + encrypted = plEncryptedStream.IsFileEncrypted(vault_path) + encryption_type = plEncryptedStream.kEncAuto if fm in {fmRead, fmReadWrite} else plEncryptedStream.kEncXtea + + backing_stream = stream_type().open(vault_path, fm) + if encrypted: + enc_stream = plEncryptedStream().open(backing_stream, fm, encryption_type) + output_stream = enc_stream + else: + output_stream = backing_stream + try: + yield output_stream + finally: + if encrypted: + enc_stream.close() + backing_stream.flush() + backing_stream.close() + def write(*args, **kwargs): assert args if len(args) == 1 and not kwargs: @@ -112,7 +135,8 @@ def find_player_vault(cwd, name): continue store = plVaultStore() - store.Import(str(vault_dat)) + with open_vault_stream(vault_dat, fmRead) as stream: + store.Import(stream) # First node is the Player node... playerNode = store[store.firstNodeID] @@ -135,7 +159,9 @@ def main(): backup_vault_dat(vault_path) vault_prev_autolink = set_link_chronicle(vault_store, args.age) write("DBG: Saving vault...") - vault_store.Export(str(vault_path)) + + with open_vault_stream(vault_path, fmCreate) as stream: + vault_store.Export(stream) # Update init file for this schtuff... init_path = args.cwd.joinpath("init", "net_age.fni") @@ -154,9 +180,6 @@ def main(): # the stale vault... del vault_store - # Sigh... - time.sleep(1.0) - # EXE args plasma_args = [str(executable), "-iinit", "To_Dni"] else: @@ -174,18 +197,16 @@ def main(): finally: # Restore sp vault, if needed. if args.version == "pvPots": - # Path of the Shell seems to have some sort of weird racing with the vault.dat around - # shutdown. This delay helps somewhat in that regard. - time.sleep(1.0) - vault_store = plVaultStore() - vault_store.Import(str(vault_path)) + with open_vault_stream(vault_path, fmRead) as stream: + vault_store.Import(stream) new_prev_autolink = set_link_chronicle(vault_store, vault_prev_autolink, args.age) if new_prev_autolink != args.age: write("DBG: ***Not*** resaving the vault!") else: write("DBG: Resaving vault...") - vault_store.Export(str(vault_path)) + with open_vault_stream(vault_path, fmCreate) as stream: + vault_store.Export(stream) # All good! write("DONE")