Fission FSN
Loading...
Searching...
No Matches
fsCCloudSaveSync.h
Go to the documentation of this file.
1#ifndef fsCCloudSaveSynchdr
2#define fsCCloudSaveSynchdr
3
5
6#include <cstdint>
7#include <filesystem>
8#include <functional>
9#include <memory>
10#include <string>
11#include <vector>
12
13// A local profile as the sync layer sees it. fileKey is the profile's file name, stable across
14// index shifts; it is only meaningful on this device.
16{
17 std::string fileKey;
18 std::string playerName;
19 std::filesystem::path path;
20};
21
22// Mirrors one local profile (the "linked" one) to a single cloud slot, newest save wins.
23// Engine-free so it can be unit tested against the localFolder provider.
25{
26public:
27 struct Config
28 {
29 std::filesystem::path statePath; // local sidecar, e.g. <user settings>/cloudsave.state
30 std::string deviceId;
31 std::string slotName{"profile"};
32 std::int64_t debounceMs{10000};
33 std::int64_t bootTimeoutMs{3000};
34
35 // Injectable for tests; defaults are the system/steady clocks and a real sleep.
36 std::function<std::int64_t()> wallTimeMsGet;
37 std::function<std::int64_t()> steadyTimeMsGet;
38 std::function<void(std::int64_t)> sleepMs;
39 std::function<void(const std::string&)> log;
40 };
41
42 enum class eBootAction
43 {
44 eNONE, // nothing to do in the game; local data stands (or was already updated on disk)
45 eAPPLIED, // cloud data was written over the linked profile's file before it was loaded
46 eRESTORE // cloud holds a profile this device lacks: create it, then call restoreComplete()
47 };
48
56
57 fsCCloudSaveSync(std::unique_ptr<fsCloudSaveService> service, Config config);
59
60 // Blocks up to bootTimeoutMs. Call before any profile file is loaded for play.
61 // If the pull fails, nothing is uploaded this session; the next launch retries.
62 BootResult boot(const std::vector<fsCloudSaveProfile>& profiles);
63 void restoreComplete(const fsCloudSaveProfile& profile);
64
65 // After the current profile was written locally.
66 void profileSaved(const fsCloudSaveProfile& profile);
67 void profileDeleted(const std::string& fileKey);
68
69 void update();
70 void flush(); // upload now if dirty, without waiting
71 void shutdown(std::int64_t timeoutMs);
72
73 bool readyGet() const;
74 bool dirtyGet() const;
75 const std::string& linkedFileKeyGet() const;
77
78 // Blob framing, exposed for tests.
79 struct Blob
80 {
81 std::int64_t generation{0};
82 std::int64_t wallTimeMs{0};
83 std::string deviceId;
84 std::string playerName;
85 std::string payload;
86 };
87 static std::string blobEncode(const Blob& blob);
88 static bool blobDecode(const std::string& data, Blob& blob);
89 static std::string hashGet(const std::string& data);
90
91private:
92 enum class eState
93 {
94 eIDLE,
96 eBLOCKED,
97 eRESTORING,
98 eREADY
99 };
100
101 struct State
102 {
103 std::string linkedFileKey;
104 std::string linkedPlayerName;
105 std::string syncedHash; // payload hash both sides agreed on at the last sync
106 std::int64_t syncedGeneration{0};
107 std::string localHash; // hash of the last local save we saw
108 std::int64_t localWallTimeMs{0}; // when the linked profile last changed locally
109 };
110
111 void stateLoad();
112 void stateSave() const;
113
114 void logMsg(const std::string& msg) const;
115 bool waitFor(const std::function<bool()>& done, std::int64_t timeoutMs);
116 void cloudApply(const fsCloudSaveProfile& profile, const Blob& cloud, bool backupLocal);
117 void dirtyMark(bool immediate);
118 void uploadStart();
119 void link(const fsCloudSaveProfile& profile);
120
121 std::unique_ptr<fsCloudSaveService> mService;
122 Config mConfig;
123 State mState;
124 eState mSyncState{eState::eIDLE};
125
126 fsCloudSaveProfile mLinked;
127 bool mCloudEmpty{false};
128 bool mDirty{false};
129 std::int64_t mDirtySinceMs{0};
130 bool mWriteInFlight{false};
131};
132
133#endif
BootResult boot(const std::vector< fsCloudSaveProfile > &profiles)
Definition fsCCloudSaveSync.cpp:136
bool readyGet() const
Definition fsCCloudSaveSync.cpp:441
static std::string blobEncode(const Blob &blob)
Definition fsCCloudSaveSync.cpp:461
~fsCCloudSaveSync()
Definition fsCCloudSaveSync.cpp:128
void shutdown(std::int64_t timeoutMs)
Definition fsCCloudSaveSync.cpp:420
void flush()
Definition fsCCloudSaveSync.cpp:411
const std::string & linkedFileKeyGet() const
Definition fsCCloudSaveSync.cpp:451
void profileSaved(const fsCloudSaveProfile &profile)
Definition fsCCloudSaveSync.cpp:329
fsCCloudSaveSync(std::unique_ptr< fsCloudSaveService > service, Config config)
Definition fsCCloudSaveSync.cpp:102
void restoreComplete(const fsCloudSaveProfile &profile)
Definition fsCCloudSaveSync.cpp:302
static std::string hashGet(const std::string &data)
Definition fsCCloudSaveSync.cpp:505
void update()
Definition fsCCloudSaveSync.cpp:400
fsCloudSaveService * serviceGet() const
Definition fsCCloudSaveSync.cpp:456
eBootAction
Definition fsCCloudSaveSync.h:43
@ eNONE
Definition fsCCloudSaveSync.h:44
@ eRESTORE
Definition fsCCloudSaveSync.h:46
@ eAPPLIED
Definition fsCCloudSaveSync.h:45
bool dirtyGet() const
Definition fsCCloudSaveSync.cpp:446
static bool blobDecode(const std::string &data, Blob &blob)
Definition fsCCloudSaveSync.cpp:473
void profileDeleted(const std::string &fileKey)
Definition fsCCloudSaveSync.cpp:374
Definition fsCloudSaveService.h:11
fsCloudSaveResult
Definition fsCloudSaveTypes.h:7
@ eUNAVAILABLE
Definition fsCloudSaveTypes.h:10
@ eOK
Definition fsCloudSaveTypes.h:8
Definition fsCCloudSaveSync.h:80
std::string deviceId
Definition fsCCloudSaveSync.h:83
std::int64_t wallTimeMs
Definition fsCCloudSaveSync.h:82
std::string payload
Definition fsCCloudSaveSync.h:85
std::string playerName
Definition fsCCloudSaveSync.h:84
std::int64_t generation
Definition fsCCloudSaveSync.h:81
Definition fsCCloudSaveSync.h:50
std::string restoreData
Definition fsCCloudSaveSync.h:54
std::string restorePlayerName
Definition fsCCloudSaveSync.h:53
fsCloudSaveResult result
Definition fsCCloudSaveSync.h:52
eBootAction action
Definition fsCCloudSaveSync.h:51
Definition fsCCloudSaveSync.h:28
std::string deviceId
Definition fsCCloudSaveSync.h:30
std::string slotName
Definition fsCCloudSaveSync.h:31
std::function< std::int64_t()> wallTimeMsGet
Definition fsCCloudSaveSync.h:36
std::int64_t debounceMs
Definition fsCCloudSaveSync.h:32
std::function< std::int64_t()> steadyTimeMsGet
Definition fsCCloudSaveSync.h:37
std::int64_t bootTimeoutMs
Definition fsCCloudSaveSync.h:33
std::function< void(std::int64_t)> sleepMs
Definition fsCCloudSaveSync.h:38
std::function< void(const std::string &)> log
Definition fsCCloudSaveSync.h:39
std::filesystem::path statePath
Definition fsCCloudSaveSync.h:29
Definition fsCCloudSaveSync.h:16
std::filesystem::path path
Definition fsCCloudSaveSync.h:19
std::string playerName
Definition fsCCloudSaveSync.h:18
std::string fileKey
Definition fsCCloudSaveSync.h:17