Extra2D/Extra2D/include/extra2d/utils/save_store.h

126 lines
4.2 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#pragma once
#include <extra2d/core/types.h>
#include <memory>
#include <string>
#include <unordered_map>
#include <vector>
namespace extra2d {
// ============================================================================
// 存储策略接口
// ============================================================================
class ISaveStrategy {
public:
virtual ~ISaveStrategy() = default;
// 读取原始数据
virtual bool read(const std::string& path, std::vector<u8>& data) = 0;
// 写入原始数据
virtual bool write(const std::string& path, const std::vector<u8>& data) = 0;
// 提交更改Switch存档需要
virtual bool commit() { return true; }
// 是否可用
virtual bool valid() const = 0;
};
// ============================================================================
// 文件存储策略
// ============================================================================
class FileSaveStrategy : public ISaveStrategy {
public:
bool read(const std::string& path, std::vector<u8>& data) override;
bool write(const std::string& path, const std::vector<u8>& data) override;
bool valid() const override { return true; }
};
// ============================================================================
// Switch存档策略仅Switch平台
// ============================================================================
#ifdef __SWITCH__
class SwitchSaveStrategy : public ISaveStrategy {
public:
SwitchSaveStrategy();
~SwitchSaveStrategy();
bool mount(const std::string& mountName);
void unmount();
bool read(const std::string& path, std::vector<u8>& data) override;
bool write(const std::string& path, const std::vector<u8>& data) override;
bool commit() override;
bool valid() const override { return mounted_; }
private:
std::string mountName_;
bool mounted_ = false;
};
#endif
// ============================================================================
// 存档数据(内存表示)
// ============================================================================
struct SaveData {
std::unordered_map<std::string, std::unordered_map<std::string, std::string>> sections;
void set(const std::string& section, const std::string& key, const std::string& value);
std::string get(const std::string& section, const std::string& key, const std::string& defaultValue = "");
bool has(const std::string& section, const std::string& key);
void remove(const std::string& section, const std::string& key);
void clear();
// 序列化/反序列化
std::vector<u8> serialize() const;
bool deserialize(const std::vector<u8>& data);
};
// ============================================================================
// 存档管理器
// ============================================================================
class SaveStore {
public:
SaveStore();
~SaveStore();
// 设置存储策略
void setStrategy(std::unique_ptr<ISaveStrategy> strategy);
// 加载/保存
bool load(const std::string& path);
bool save(const std::string& path = "");
// 数据操作
void set(const std::string& section, const std::string& key, const std::string& value);
void setInt(const std::string& section, const std::string& key, int value);
void setFloat(const std::string& section, const std::string& key, float value);
void setBool(const std::string& section, const std::string& key, bool value);
std::string get(const std::string& section, const std::string& key, const std::string& defaultValue = "");
int getInt(const std::string& section, const std::string& key, int defaultValue = 0);
float getFloat(const std::string& section, const std::string& key, float defaultValue = 0.0f);
bool getBool(const std::string& section, const std::string& key, bool defaultValue = false);
bool has(const std::string& section, const std::string& key);
void remove(const std::string& section, const std::string& key);
void clear();
// 事务
void begin();
bool commit();
void rollback();
private:
SaveData data_;
SaveData backup_; // 事务备份
std::unique_ptr<ISaveStrategy> strategy_;
std::string path_;
bool inTransaction_ = false;
bool dirty_ = false;
};
} // namespace extra2d