DOF/sqr/Core/BaseClass/UserStorage.nut

68 lines
1.5 KiB
Plaintext

/*
文件名:UserStorage.nut
路径:Core/BaseClass/UserStorage.nut
创建日期:2024-11-14 08:43
文件用途:用户存档类
*/
class Storage {
Data = null;
// 构造函数
constructor() {
Data = {};
}
//储存数据
function SetItem(Key, Value) {
Data.rawset(Key, Value);
}
//储存数据列表
function SetItemList(T) {
foreach(Key, Value in T) {
Data.rawset(Key, Value);
}
}
//获取数据
function GetItem(Key) {
if (Data.rawin(Key)) return Data.rawget(Key);
return null;
}
//获取数据列表
function GetItemList(KeyList) {
local T = {};
foreach(Key in KeyList) {
local Buf = GetItem(Key);
T.Key <- Buf;
}
return T;
}
function Load(Path) {
try {
local FileObj = file(Path, "r");
local IO = blobex(FileObj.readblob(FileObj.len()));
local SaveStr = IO.GetString(IO.len());
Data = Json.Decode(SaveStr);
FileObj.close();
return true;
} catch (exception) {
print("未读取到存档文件");
print(exception);
return false;
}
}
function Save(Path) {
local SaveStr = Json.Encode(Data);
local FileObj = file(Path, "w");
foreach(char in SaveStr) {
FileObj.writen(char, 'b');
}
FileObj.flush();
FileObj.close();
}
}