yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
wasm_autosave.h
Go to the documentation of this file.
1#ifndef YAZE_APP_PLATFORM_WASM_AUTOSAVE_H_
2#define YAZE_APP_PLATFORM_WASM_AUTOSAVE_H_
3
4#ifdef __EMSCRIPTEN__
5
6#include <functional>
7#include <memory>
8#include <mutex>
9#include <string>
10#include <unordered_map>
11#include <vector>
12
13#include "absl/status/status.h"
14#include "absl/status/statusor.h"
15#include "nlohmann/json.hpp"
16
17namespace yaze {
18namespace platform {
19
29class AutoSaveManager {
30 public:
31 // Callback types
32 using SaveCallback = std::function<nlohmann::json()>;
33 using RestoreCallback = std::function<void(const nlohmann::json&)>;
34
39 static AutoSaveManager& Instance();
40
46 absl::Status Start(int interval_seconds = 60);
47
52 absl::Status Stop();
53
58 bool IsRunning() const;
59
66 void RegisterComponent(const std::string& component_id,
67 SaveCallback save_fn,
68 RestoreCallback restore_fn);
69
74 void UnregisterComponent(const std::string& component_id);
75
80 absl::Status SaveNow();
81
86 void EmergencySave();
87
92 bool HasRecoveryData();
93
98 absl::StatusOr<nlohmann::json> GetRecoveryInfo();
99
104 absl::Status RecoverLastSession();
105
110 absl::Status ClearRecoveryData();
111
117 void SetInterval(int seconds);
118
123 int GetInterval() const { return interval_seconds_; }
124
129 void SetEnabled(bool enabled);
130
135 bool IsEnabled() const { return enabled_; }
136
141 std::chrono::system_clock::time_point GetLastSaveTime() const {
142 return last_save_time_;
143 }
144
149 nlohmann::json GetStatistics() const;
150
151 private:
152 // Private constructor for singleton
153 AutoSaveManager();
154 ~AutoSaveManager();
155
156 // Delete copy and move constructors
157 AutoSaveManager(const AutoSaveManager&) = delete;
158 AutoSaveManager& operator=(const AutoSaveManager&) = delete;
159 AutoSaveManager(AutoSaveManager&&) = delete;
160 AutoSaveManager& operator=(AutoSaveManager&&) = delete;
161
162 // Component registration
163 struct Component {
164 SaveCallback save_fn;
165 RestoreCallback restore_fn;
166 };
167
168 // Internal methods
169 void InitializeEventHandlers();
170 void CleanupEventHandlers();
171 static void TimerCallback(void* user_data);
172 absl::Status PerformSave();
173 nlohmann::json CollectComponentData();
174 absl::Status SaveToStorage(const nlohmann::json& data);
175 absl::StatusOr<nlohmann::json> LoadFromStorage();
176
177 // Storage keys
178 static constexpr const char* kAutoSaveDataKey = "yaze_autosave_data";
179 static constexpr const char* kAutoSaveMetaKey = "yaze_autosave_meta";
180 static constexpr const char* kRecoveryFlagKey = "yaze_has_recovery";
181
182 // Member variables
183 mutable std::mutex mutex_;
184 std::unordered_map<std::string, Component> components_;
185 int interval_seconds_;
186 bool enabled_;
187 bool running_;
188 int timer_id_;
189 std::chrono::system_clock::time_point last_save_time_;
190
191 // Statistics
192 size_t save_count_;
193 size_t error_count_;
194 size_t recovery_count_;
195
196 // Event handler registration state
197 bool event_handlers_initialized_;
198
199 public:
200 // Must be public for emergency save callback access
201 static bool emergency_save_triggered_;
202};
203
204} // namespace platform
205} // namespace yaze
206
207#endif // __EMSCRIPTEN__
208
209#endif // YAZE_APP_PLATFORM_WASM_AUTOSAVE_H_