Play your Steam library's Windows games on a jailbroken PS5.
C++
Keep the Steam session in memory when asked
Leonardo Lobato82c1ebe0d4ab
diff --git a/src/steam/local_session_repository.cpp b/src/steam/local_session_repository.cppindex ac11ae4..3a51e20 100644--- a/src/steam/local_session_repository.cpp+++ b/src/steam/local_session_repository.cpp@@ -5,8 +5,8 @@#include <sys/stat.h>namespace playden {-LocalSessionRepository::LocalSessionRepository(std::string root, DiagnosticReport report)- : root_(std::move(root)),storage_(root_),store_(storage_),report_(std::move(report)) {}+LocalSessionRepository::LocalSessionRepository(std::string root, DiagnosticReport report, SessionVault* vault)+ : root_(std::move(root)),storage_(root_),store_(storage_),report_(std::move(report)),vault_(vault) {}Result<void> LocalSessionRepository::Prepare() const {if (prepared_) return Result<void>::Success();if (root_.empty() || root_.find('\0') != std::string::npos)@@ -48,22 +48,78 @@ Result<void> LocalSessionRepository::Prepare() const {prepared_ = true;return Result<void>::Success();}-Result<std::optional<StoredSession>> LocalSessionRepository::Load() const {+Result<std::optional<StoredSession>> LocalSessionRepository::LoadSaved() const {const auto ready = Prepare();if (!ready.ok()) return Result<std::optional<StoredSession>>::Failure(ready.error(),ready.message());auto result = store_.Load();if (!result.ok() && report_) report_("session storage load failed: " + result.message());return result;}-Result<void> LocalSessionRepository::Save(const StoredSession& session) const {+Result<void> LocalSessionRepository::SaveToDisk(const StoredSession& session) const {const auto ready = Prepare();if (!ready.ok()) return ready;return store_.Save(session);}-Result<void> LocalSessionRepository::Clear() const {+Result<void> LocalSessionRepository::ClearSaved() const {// Clearing must remain possible even if the private-write probe now fails.if (root_.empty() || root_.find('\0') != std::string::npos)return Result<void>::Failure(StorageError::kInvalidPath,"Session storage path is invalid");- return store_.Clear();+ const auto cleared = store_.Clear();+ if (!cleared.ok() && report_) report_("session storage clear failed: " + cleared.message());+ return cleared;+}+Result<std::optional<StoredSession>> LocalSessionRepository::Load() const {+ if (!vault_) return LoadSaved();+ std::lock_guard<std::mutex> lock(vault_->mutex_);+ // The session in memory wins: it is the newest sign-in, whichever the setting.+ if (vault_->session_) return Result<std::optional<StoredSession>>::Success(*vault_->session_);+ if (!vault_->persist_) return Result<std::optional<StoredSession>>::Success(std::nullopt);+ return LoadSaved();+}+Result<void> LocalSessionRepository::Save(const StoredSession& session) const {+ if (!vault_) return SaveToDisk(session);+ std::lock_guard<std::mutex> lock(vault_->mutex_);+ if (vault_->persist_) {+ auto saved = SaveToDisk(session);+ if (saved.ok()) vault_->Drop();+ return saved;+ }+ // A session.v1 that can't be deleted fails the sign-in: the player asked for no token on disk.+ vault_->Keep(session);+ auto cleared = ClearSaved();+ if (!cleared.ok()) vault_->Drop();+ return cleared;+}+Result<void> LocalSessionRepository::Clear() const {+ if (!vault_) return ClearSaved();+ std::lock_guard<std::mutex> lock(vault_->mutex_);+ vault_->Drop();+ return ClearSaved();+}+Result<void> LocalSessionRepository::SetPersist(bool persist) const {+ if (!vault_) return Result<void>::Success();+ std::lock_guard<std::mutex> lock(vault_->mutex_);+ if (vault_->persist_ == persist) return Result<void>::Success();+ vault_->persist_ = persist;+ if (persist) {+ if (!vault_->session_) return Result<void>::Success();+ auto saved = SaveToDisk(*vault_->session_);+ // Not saved: the session stays in memory and keeps working until Playden closes.+ if (!saved.ok()) {+ if (report_) report_("session storage save failed: " + saved.message());+ return saved;+ }+ vault_->Drop();+ return saved;+ }+ if (!vault_->session_) {+ auto loaded = LoadSaved();+ if (loaded.ok() && loaded.value()) {+ vault_->Keep(*loaded.value());+ steam::ZeroizeSecret(loaded.value()->refresh_token);+ steam::ZeroizeSecret(loaded.value()->access_token);+ }+ }+ return ClearSaved();}}diff --git a/src/steam/local_session_repository.h b/src/steam/local_session_repository.hindex c0952c7..787650b 100644--- a/src/steam/local_session_repository.h+++ b/src/steam/local_session_repository.h@@ -1,24 +1,35 @@#pragma once#include "steam/session_store.h"+#include "steam/session_vault.h"#include <functional>namespace playden {// Lazy filesystem preparation runs only on the calling service worker. The// configured root's parent must already exist; failures never enable fixtures.+// With a vault, "Stay signed in" decides where the session lives: on, in+// session.v1 as before; off, only in the vault, and session.v1 is deleted.+// Without one, the session is always saved.class LocalSessionRepository final : public SessionRepository {public:using DiagnosticReport = std::function<void(const std::string&)>;// Reports operation/error metadata only; never session bytes or account data.- explicit LocalSessionRepository(std::string root, DiagnosticReport report = {});+ explicit LocalSessionRepository(std::string root, DiagnosticReport report = {}, SessionVault* vault = nullptr);Result<std::optional<StoredSession>> Load() const override;Result<void> Save(const StoredSession& session) const override;Result<void> Clear() const override;+ // The player changed "Stay signed in". Off moves a saved session into the vault and deletes+ // session.v1; on saves the session the vault holds. The setting changes even if the disk fails.+ Result<void> SetPersist(bool persist) const;private:Result<void> Prepare() const;+ Result<std::optional<StoredSession>> LoadSaved() const;+ Result<void> SaveToDisk(const StoredSession& session) const;+ Result<void> ClearSaved() const;std::string root_;mutable Storage storage_;SessionStore store_;mutable bool prepared_ = false;DiagnosticReport report_;+ SessionVault* vault_;};}diff --git a/src/steam/session_vault.h b/src/steam/session_vault.hnew file mode 100644index 0000000..844911d--- /dev/null+++ b/src/steam/session_vault.h@@ -0,0 +1,39 @@+#pragma once+#include "steam/secret.h"+#include "steam/session_store.h"+#include <mutex>+#include <optional>++namespace playden {+// "Stay signed in", shared by every LocalSessionRepository of a run. Off, the Steam session lives+// here, in memory, and never on disk. The repositories read, write and delete session.v1 under the+// vault's lock, so a change of the setting can't race a sign-in that is being saved.+class SessionVault {+ public:+ explicit SessionVault(bool persist = true) : persist_(persist) {}+ ~SessionVault() { Drop(); }+ SessionVault(const SessionVault&) = delete;+ SessionVault& operator=(const SessionVault&) = delete;+ bool persist() const {+ std::lock_guard<std::mutex> lock(mutex_);+ return persist_;+ }++ private:+ friend class LocalSessionRepository;+ // The caller holds mutex_.+ void Keep(const StoredSession& session) {+ Drop();+ session_ = session;+ }+ void Drop() {+ if (!session_) return;+ steam::ZeroizeSecret(session_->refresh_token);+ steam::ZeroizeSecret(session_->access_token);+ session_.reset();+ }+ mutable std::mutex mutex_;+ bool persist_;+ std::optional<StoredSession> session_;+};+}diff --git a/tests/local_session_repository_test.cpp b/tests/local_session_repository_test.cppindex 79b6781..4b07d9d 100644--- a/tests/local_session_repository_test.cpp+++ b/tests/local_session_repository_test.cpp@@ -51,5 +51,71 @@ int main() {LocalSessionRepository absent(missing.string()); assert(!absent.Load().ok());assert(absent.Clear().ok());assert(unavailable.Load().message().find("synthetic-refresh-secret")==std::string::npos);++ // "Stay signed in" off: the session lives in the vault every repository of the run shares.+ const auto vault_root=base/"vault";+ std::filesystem::create_directories(vault_root);+ const auto saved_file=vault_root/"session.v1";+ {+ LocalSessionRepository writer(vault_root.string());+ assert(writer.Save(session).ok() && std::filesystem::exists(saved_file));+ }+ SessionVault vault(false);+ LocalSessionRepository auth(vault_root.string(),{},&vault);+ LocalSessionRepository library(vault_root.string(),{},&vault);+ result=library.Load(); assert(result.ok() && !result.value() && "off never reads session.v1");+ StoredSession other{456,"synthetic-other","synthetic-other-refresh","synthetic-other-access",7u};+ assert(auth.Save(other).ok());+ assert(!std::filesystem::exists(saved_file) && "off deletes session.v1 and writes none");+ result=library.Load(); assert(result.ok() && result.value()->steam_id==456 && result.value()->cell_id==7u);+ {+ // A new process (a new vault) finds nothing.+ SessionVault rebooted(false);+ LocalSessionRepository after(vault_root.string(),{},&rebooted);+ assert(after.Load().ok() && !after.Load().value());+ }+ // Turning it on saves the session the vault holds; the file then serves new runs.+ assert(auth.SetPersist(true).ok() && vault.persist() && std::filesystem::exists(saved_file));+ {+ LocalSessionRepository fresh(vault_root.string());+ result=fresh.Load(); assert(result.ok() && result.value() && result.value()->steam_id==456);+ }+ // On: Save writes the file, as without a vault.+ assert(auth.Save(session).ok());+ result=LocalSessionRepository(vault_root.string()).Load(); assert(result.ok() && result.value()->steam_id==123);+ result=library.Load(); assert(result.ok() && result.value()->steam_id==123);+ // Off again while signed in: the saved session moves into memory and the file goes.+ assert(library.SetPersist(false).ok() && !vault.persist() && !std::filesystem::exists(saved_file));+ result=auth.Load(); assert(result.ok() && result.value() && result.value()->steam_id==123);+ // Sign-out clears memory and disk, whichever repository asks.+ { LocalSessionRepository writer(vault_root.string()); assert(writer.Save(other).ok()); }+ assert(library.Clear().ok() && !std::filesystem::exists(saved_file));+ result=auth.Load(); assert(result.ok() && !result.value());+ // Off and session.v1 can't be deleted: the sign-in fails and nothing stays in memory.+ {+ LocalSessionRepository writer(vault_root.string()); assert(writer.Save(session).ok());+ chmod(vault_root.c_str(),0500);+ if (unlink(saved_file.c_str())!=0) { // root ignores the mode; nothing to check then+ assert(!auth.Save(other).ok());+ result=auth.Load(); assert(result.ok() && !result.value());+ }+ chmod(vault_root.c_str(),0700);+ }+ // Many workers read and write the shared vault at once.+ std::atomic<bool> go{false};+ std::atomic<int> bad_loads{0};+ std::vector<std::thread> workers;+ for (int worker=0;worker<4;++worker) workers.emplace_back([&,worker] {+ LocalSessionRepository mine(vault_root.string(),{},&vault);+ while (!go.load()) std::this_thread::yield();+ for (int attempt=0;attempt<200;++attempt) {+ if (worker==0) { assert(mine.Save(attempt%2 ? session : other).ok()); continue; }+ const auto loaded=mine.Load();+ if (!loaded.ok() || (loaded.value() && loaded.value()->steam_id!=123 && loaded.value()->steam_id!=456)) ++bad_loads;+ }+ });+ go=true;+ for (auto& worker:workers) worker.join();+ assert(bad_loads==0 && !std::filesystem::exists(saved_file));std::filesystem::remove_all(base);}