Play your Steam library's Windows games on a jailbroken PS5.
C++
#include "library/service.h"
#include <atomic>
#include <cassert>
#include <chrono>
#include <limits>
#include <map>
#include <mutex>
#include <thread>
#include <vector>
#include "steam_id.h"
using namespace playden; using namespace playden::steam; using namespace std::chrono_literals;
namespace {
class Sessions final:public SessionRepository {
public: mutable std::atomic<int> loads{0}; std::optional<StoredSession> session=StoredSession{123,"synthetic","private-refresh","private-access",{}}; bool fail=false;
Result<std::optional<StoredSession>> Load()const override{++loads;if(fail)return Result<std::optional<StoredSession>>::Failure(StorageError::kReadFailed,"private");return Result<std::optional<StoredSession>>::Success(session);}
Result<void> Save(const StoredSession&)const override{assert(false);return Result<void>::Success();} Result<void> Clear()const override{assert(false);return Result<void>::Success();}
};
class Backend final:public LibraryBackend {
public: std::atomic<int> calls{0}; std::atomic<bool> block{false},entered{false},release{false},canceled{false}; LibraryResult result{LibraryError::kNone,{{SteamId(42),"Fresh title",{},{},{},12,700}},true};
LibraryResult Fetch(const StoredSession& session,const std::atomic<bool>& cancel)override{assert(session.access_token=="private-access"||session.access_token=="access");++calls;entered=true;while(block&&!release){if(cancel){canceled=true;break;}std::this_thread::sleep_for(1ms);}return result;}
};
class Cache final:public LibraryRepository {
public: mutable std::mutex mutex; mutable std::map<std::uint64_t,LibrarySnapshot> snapshots; mutable std::vector<char> order; mutable std::atomic<int> loads{0},saves{0},clears{0};
mutable std::atomic<bool> block_load{false},load_entered{false},load_release{false},block_save{false},save_entered{false},save_release{false},block_clear{false},clear_entered{false},clear_release{false};
StorageError load_error=StorageError::kNone,save_error=StorageError::kNone,clear_error=StorageError::kNone;
Result<LibrarySnapshot> Load(std::uint64_t a)const override{++loads;load_entered=true;while(block_load&&!load_release)std::this_thread::sleep_for(1ms);std::lock_guard<std::mutex> l(mutex);order.push_back('L');if(load_error!=StorageError::kNone)return Result<LibrarySnapshot>::Failure(load_error,"load");auto i=snapshots.find(a);if(i==snapshots.end())return Result<LibrarySnapshot>::Failure(StorageError::kNotFound,"missing");return Result<LibrarySnapshot>::Success(i->second);}
Result<void> Save(std::uint64_t a,const LibrarySnapshot& s)const override{++saves;save_entered=true;while(block_save&&!save_release)std::this_thread::sleep_for(1ms);std::lock_guard<std::mutex> l(mutex);order.push_back('S');if(save_error!=StorageError::kNone)return Result<void>::Failure(save_error,"save");snapshots[a]=s;return Result<void>::Success();}
Result<void> Clear(std::uint64_t a)const override{++clears;clear_entered=true;while(block_clear&&!clear_release)std::this_thread::sleep_for(1ms);std::lock_guard<std::mutex> l(mutex);order.push_back('C');if(clear_error!=StorageError::kNone)return Result<void>::Failure(clear_error,"clear");snapshots.erase(a);return Result<void>::Success();}
};
LibrarySnapshot Snapshot(std::uint64_t t,std::string title="Cached title"){return {3,t,{{SteamId(42),std::move(title),{},{},{},9,600}},true};}
void Wait(const std::atomic<bool>& v){auto d=std::chrono::steady_clock::now()+2s;while(!v&&std::chrono::steady_clock::now()<d)std::this_thread::sleep_for(1ms);assert(v);}
std::vector<LibraryEvent> Until(LibraryService& s,LibraryEventKind expected){auto d=std::chrono::steady_clock::now()+2s;std::vector<LibraryEvent> out;while(std::chrono::steady_clock::now()<d){for(auto&e:s.Pump(1)){auto k=e.kind;out.push_back(std::move(e));if(k==expected)return out;}std::this_thread::sleep_for(1ms);}assert(false&&"worker deadline");return{};}
void CachedThenNetworkAndSave(){Sessions sessions;Backend backend;backend.block=true;Cache cache;cache.snapshots[123]=Snapshot(111);LibraryService service(backend,sessions,&cache,[]{return std::uint64_t{777};});auto request=service.Refresh(123,7);auto events=Until(service,LibraryEventKind::kCached);assert(events.size()==2&&events[0].kind==LibraryEventKind::kLoading);auto& cached=events.back();assert(cached.request_id==request&&cached.games[0].title=="Cached title"&&cached.cache_available&&cached.retrieved_at_unix_seconds==111&&cached.metadata_partial);Wait(backend.entered);backend.release=true;events=Until(service,LibraryEventKind::kComplete);auto& complete=events.back();assert(!cached.licenses_read&&!complete.licenses_read);assert(complete.games[0].title=="Fresh title"&&complete.cache_available&&complete.retrieved_at_unix_seconds==777&&complete.cache_error==StorageError::kNone&&cache.saves==1);std::lock_guard<std::mutex> l(cache.mutex);assert(cache.snapshots[123].games[0].title=="Fresh title");}
void OfflineCacheOnly(){Sessions sessions;Backend backend;Cache cache;cache.snapshots[123]=Snapshot(222);LibraryService service(backend,sessions,&cache);auto request=service.LoadCached(123,1);auto events=Until(service,LibraryEventKind::kCached);assert(events.back().request_id==request&&events.back().retrieved_at_unix_seconds==222&&backend.calls==0&&sessions.loads==0);service.LoadCached(456,2);events=Until(service,LibraryEventKind::kFailure);assert(events.back().error==LibraryError::kPersistence&&events.back().cache_error==StorageError::kNotFound&&!events.back().cache_available&&backend.calls==0&&sessions.loads==0);cache.load_error=StorageError::kInvalidData;service.LoadCached(123,3);events=Until(service,LibraryEventKind::kFailure);assert(events.back().cache_error==StorageError::kInvalidData);cache.load_error=StorageError::kNone;cache.snapshots[123].retrieved_at_unix_seconds=std::uint64_t{1}<<63;service.LoadCached(123,4);events=Until(service,LibraryEventKind::kFailure);assert(events.back().cache_error==StorageError::kInvalidData);}
void FailureAndSaveError(){ {Sessions s;Backend b;b.result={LibraryError::kNetwork,{},false};Cache c;c.snapshots[123]=Snapshot(333);LibraryService service(b,s,&c);service.Refresh(123,1);Until(service,LibraryEventKind::kCached);auto e=Until(service,LibraryEventKind::kFailure);assert(e.back().error==LibraryError::kNetwork&&e.back().cache_available&&c.saves==0&&c.snapshots[123].games[0].title=="Cached title");} {Sessions s;Backend b;Cache c;c.save_error=StorageError::kWriteFailed;LibraryService service(b,s,&c,[]{return std::uint64_t{444};});service.Refresh(123,1);auto e=Until(service,LibraryEventKind::kComplete);assert(e.back().error==LibraryError::kNone&&e.back().cache_error==StorageError::kWriteFailed&&!e.back().cache_available&&e.back().games[0].title=="Fresh title");}}
void AccountSwitchDuringLoad(){Sessions s;Backend b;Cache c;c.block_load=true;LibraryService service(b,s,&c);service.Refresh(123,1);Wait(c.load_entered);service.Invalidate(2);s.session=StoredSession{456,"other","refresh","access",{}};assert(service.Refresh(456,2)!=0);c.load_release=true;auto e=Until(service,LibraryEventKind::kComplete);assert(e.back().account_id==456&&e.back().account_generation==2&&b.calls==1);}
void SessionIdentityPrecedesCache(){Sessions s;Backend b;Cache c;c.snapshots[456]=Snapshot(1);LibraryService service(b,s,&c);service.Refresh(456,1);auto e=Until(service,LibraryEventKind::kFailure);assert(e.back().error==LibraryError::kNoSession&&c.loads==0&&b.calls==0);s.fail=true;service.Refresh(123,2);e=Until(service,LibraryEventKind::kFailure);assert(e.back().error==LibraryError::kPersistence&&c.loads==0&&b.calls==0);}
void ClearWaitsForSave(){Sessions s;Backend b;Cache c;c.block_save=true;LibraryService service(b,s,&c,[]{return std::uint64_t{555};});service.Refresh(123,1);Wait(c.save_entered);auto request=service.Clear(123,2);assert(request&&service.Refresh(123,3)==0);c.save_release=true;auto e=Until(service,LibraryEventKind::kCleared);assert(e.back().request_id==request&&e.back().account_generation==2&&e.back().cache_error==StorageError::kNone&&!e.back().cache_available);{std::lock_guard<std::mutex>l(c.mutex);assert(!c.snapshots.count(123)&&c.order.size()>=3&&c.order[c.order.size()-2]=='S'&&c.order.back()=='C');}assert(service.Refresh(123,3)!=0);}
void ClearFailureAndShutdown(){ {Sessions s;Backend b;Cache c;c.snapshots[123]=Snapshot(1);c.clear_error=StorageError::kRemoveFailed;LibraryService service(b,s,&c);service.Clear(123,1);auto e=Until(service,LibraryEventKind::kFailure);assert(e.back().error==LibraryError::kPersistence&&e.back().cache_error==StorageError::kRemoveFailed&&e.back().cache_available&&c.snapshots.count(123));} {Sessions s;Backend b;Cache c;c.block_save=true;LibraryService service(b,s,&c);service.Refresh(123,4);Wait(c.save_entered);auto request=service.Clear(123,5);service.Invalidate(6);std::thread stop([&]{service.Shutdown();});std::this_thread::sleep_for(10ms);c.save_release=true;stop.join();auto e=Until(service,LibraryEventKind::kCleared);assert(e.back().request_id==request&&e.back().account_generation==5&&service.Refresh(123,6)==0);std::lock_guard<std::mutex>l(c.mutex);assert(!c.snapshots.count(123)&&c.order[c.order.size()-2]=='S'&&c.order.back()=='C');}}
void ShutdownCancelsNetwork(){Sessions s;Backend b;b.block=true;LibraryService service(b,s);service.Refresh(123,1);Wait(b.entered);service.Shutdown();assert(b.canceled&&service.Pump(8).empty());}
void GenerationsAndNoCache(){Sessions s;Backend b;b.result.licenses_read=true;LibraryService service(b,s);service.Invalidate(5);assert(service.Refresh(123,4)==0&&service.LoadCached(123,4)==0&&service.Clear(123,4)==0);assert(service.Refresh(123,5));auto e=Until(service,LibraryEventKind::kComplete);assert(e.back().cache_error==StorageError::kNone&&!e.back().cache_available&&e.back().licenses_read);}
}
// A library with demos is saved as schema 6, the first that can mark them; one without keeps the older schema.
void DemosChooseTheCacheSchema(){Sessions s;Backend b;Cache c;LibraryService service(b,s,&c,[]{return std::uint64_t{2000};});service.Refresh(123,1);Until(service,LibraryEventKind::kComplete);{std::lock_guard<std::mutex>l(c.mutex);assert(c.snapshots[123].schema_version==4);}b.result.games[0].demo=true;service.Refresh(123,1);auto e=Until(service,LibraryEventKind::kComplete);assert(e.back().games[0].demo);std::lock_guard<std::mutex>l(c.mutex);assert(c.snapshots[123].schema_version==6&&c.snapshots[123].games[0].demo);}
void AcquisitionDatesCarryForward(){Sessions s;Backend b;Cache c;auto cached=Snapshot(1);cached.games[0].added_at_unix_seconds=900;c.snapshots[123]=cached;LibraryService service(b,s,&c,[]{return std::uint64_t{2000};});service.Refresh(123,1);auto e=Until(service,LibraryEventKind::kComplete);assert(e.back().games[0].added_at_unix_seconds==900u);{std::lock_guard<std::mutex>l(c.mutex);assert(c.snapshots[123].games[0].added_at_unix_seconds==900u);}b.result.games[0].added_at_unix_seconds=1000;service.Refresh(123,1);e=Until(service,LibraryEventKind::kComplete);assert(e.back().games[0].added_at_unix_seconds==1000u);}
// Only a library with a family-shared game needs schema 7; demos alone stay at 6 and plain libraries at 4.
void FamilySharedGamesChooseTheCacheSchema(){Sessions s;Backend b;Cache c;LibraryService service(b,s,&c,[]{return std::uint64_t{2000};});b.result.games[0].demo=true;service.Refresh(123,1);Until(service,LibraryEventKind::kComplete);{std::lock_guard<std::mutex>l(c.mutex);assert(c.snapshots[123].schema_version==6);}b.result.games[0].demo=false;b.result.games[0].family_shared=true;service.Refresh(123,1);auto e=Until(service,LibraryEventKind::kComplete);assert(e.back().games[0].family_shared);{std::lock_guard<std::mutex>l(c.mutex);assert(c.snapshots[123].schema_version==7&&c.snapshots[123].games[0].family_shared);}b.result.games[0].family_shared=false;service.Refresh(123,1);Until(service,LibraryEventKind::kComplete);std::lock_guard<std::mutex>l(c.mutex);assert(c.snapshots[123].schema_version==4&&!c.snapshots[123].games[0].family_shared);}
int main(){AcquisitionDatesCarryForward();DemosChooseTheCacheSchema();FamilySharedGamesChooseTheCacheSchema();CachedThenNetworkAndSave();OfflineCacheOnly();FailureAndSaveError();AccountSwitchDuringLoad();SessionIdentityPrecedesCache();ClearWaitsForSave();ClearFailureAndShutdown();ShutdownCancelsNetwork();GenerationsAndNoCache();}