Skip to content

Commit

Permalink
Re-fix the TPotS launching race condition.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Hoikas committed Jan 18, 2023
1 parent 021cb77 commit 421f803
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
2 changes: 1 addition & 1 deletion cmake/Dependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
43 changes: 32 additions & 11 deletions korman/plasma_launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# along with Korman. If not, see <http://www.gnu.org/licenses/>.

import argparse
from contextlib import contextmanager
from pathlib import Path
from PyHSPlasma import *
import shutil
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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]
Expand All @@ -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")
Expand All @@ -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:
Expand All @@ -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")
Expand Down

0 comments on commit 421f803

Please sign in to comment.