namespace ET
{
/// <summary>
/// 额外存档:设置
/// </summary>
public class MSaveSetting : MSaveBase
{
public override void InitDefault()
{
}
}
/// <summary>
/// 主存档,按槽位区分。
/// 自动槽位0,1,2...
/// 手动槽位0,1,2...
/// </summary>
public class MSaveGame : MSaveBase
{
public byte[] playerHideBag; // 全部使用Hidebag,增强【改名】【删除代码】带来的兼容问题
public override void InitDefault()
{
}
}
public abstract class MSaveBase
{
public long SaveTime;
public abstract void InitDefault();
}
public class MSaveManager
{
[StaticField] public static MSaveManager Ins;
public string UserDir;
private string UserSaveDir;
private const string Manual = "manual";
private const int ManualBackupCount = 5; //手动存档最大个数
private const string Auto = "auto";
private const int AutoBackupCount = 10; //自动存档最大个数
private const string Only = "only"; // 唯一存档
public static void Initialize(string rootDir, string userId)
{
Ins = new MSaveManager();
Ins.UserDir = Path.Combine(rootDir, userId);
Ins.UserSaveDir = Path.Combine(Ins.UserDir, "Save");
Ins.LoadAll();
}
private MSaveSetting MSaveSetting_Only;
private MSaveGame MSaveGame_Only;
private MSaveGame[] MSaveGame_Manual = new MSaveGame[ManualBackupCount];
private MSaveGame[] MSaveGame_Auto = new MSaveGame[AutoBackupCount];
public void LoadAll()
{
MSaveSetting_Only = GetSaveSetting();
MSaveGame_Only = GetSaveGame_Only();
for (int i = 0; i < ManualBackupCount; i++)
{
MSaveGame_Manual[i] = null;
GetSaveGame_Manual(i);
}
for (int i = 0; i < AutoBackupCount; i++)
{
MSaveGame_Auto[i] = null;
GetSaveGame_Auto(i);
}
}
/// <summary>
/// root/player1/auto_0,1,2.dat
/// root/player1/manual_0,1,2.dat
/// </summary>
private string GetSaveFullPath(string fileName)
{
return Path.Combine(this.UserSaveDir, $"/{fileName}.dat");
}
/// <summary>
/// 没有返回null,不会新建
/// </summary>
public MSaveGame GetSaveGame_Manual(int slot = 0)
{
if (slot >= ManualBackupCount)
{
$"存档id越界{slot}".WriteErrorLine();
return null;
}
if (MSaveGame_Manual[slot] == null)
{
string saveDataFile = this.GetSaveFullPath($"{Manual}_{slot}");
MSaveGame_Manual[slot] = Load<MSaveGame>(saveDataFile, false);
}
return MSaveGame_Manual[slot];
}
/// <summary>
/// 没有返回null,不会新建
/// </summary>
public MSaveGame GetSaveGame_Auto(int slot = 0)
{
if (slot >= AutoBackupCount)
{
$"存档id越界{slot}".WriteErrorLine();
return null;
}
if (MSaveGame_Auto[slot] == null)
{
string saveDataFile = this.GetSaveFullPath($"{Auto}_{slot}");
MSaveGame_Auto[slot] = Load<MSaveGame>(saveDataFile, false);
}
return MSaveGame_Auto[slot];
}
/// <summary>
/// 没有会新建
/// </summary>
public MSaveGame GetSaveGame_Only()
{
if (MSaveGame_Only == null)
{
string saveDataFile = this.GetSaveFullPath($"{Only}_MSaveGame");
MSaveGame_Only = Load<MSaveGame>(saveDataFile, true);
}
return MSaveGame_Only;
}
/// <summary>
/// 没有会新建
/// </summary>
public MSaveSetting GetSaveSetting()
{
if (MSaveSetting_Only == null)
{
string saveDataFile = this.GetSaveFullPath($"{Only}_MSaveSetting");
MSaveSetting_Only = Load<MSaveSetting>(saveDataFile, true);
}
return MSaveSetting_Only;
}
/// <summary>
/// 运行时,凡是调用过Save的数据,都会进行保存。
/// 保存是实时保存到:当前最新存档。
/// </summary>
private void Save<T>(T classData, string fullPath) where T : MSaveBase, new()
{
if (classData == null)
{
"存储错误,数据为空".WriteErrorLine();
return;
}
string json = MongoHelper.ToJson(classData);
string data = MEncryptHelper.Encrypt(json, MEncryptHelper.APP_SECRET);
StreamWriter streamWriter = File.CreateText(fullPath);
streamWriter.Write(data);
streamWriter.Close();
}
private T Load<T>(string fullPath, bool needNew) where T : MSaveBase, new()
{
if (!File.Exists(fullPath))
{
if (!needNew)
{
return null;
}
T t = new T();
t.InitDefault();
this.Save<T>(t, fullPath);
}
StreamReader streamReader = File.OpenText(fullPath);
string data = streamReader.ReadToEnd();
//对数据进行解密,32位解密密钥
data = MEncryptHelper.Decrypt(data, MEncryptHelper.APP_SECRET);
streamReader.Close();
T loadedData = MongoHelper.FromJson<T>(data);
return loadedData;
}
public void DeleteAllData()
{
FileHelper.CleanDirectory(UserSaveDir);
}
}
}
2024-11-21【Unity】记录
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 这周为了迎接考试,正在进行紧张的复习中。感觉时间都不够分配,自然花在娃的时间上变少了,专心做我的事。虽然小娃一...
- baba-s/awesome-unity-open-source-on-github: A categorized...