自动保存Beatmap

This commit is contained in:
SoulliesOfficial
2025-03-01 21:26:16 -05:00
parent 6aba331079
commit 860d7393fb
12 changed files with 1640 additions and 9 deletions

View File

@@ -2,6 +2,7 @@ using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using Ichni.Editor;
using Ichni.RhythmGame;
using Ichni.RhythmGame.Beatmap;
@@ -30,6 +31,7 @@ namespace Ichni
public LoadManager loadManager;
public ExportManager exportManager;
public BeatmapClipManager beatmapClipManager;
public AutoSaveManager autoSaveManager;
public ProjectManager()
{
@@ -37,6 +39,7 @@ namespace Ichni
loadManager = new LoadManager();
exportManager = new ExportManager();
beatmapClipManager = new BeatmapClipManager();
autoSaveManager = new AutoSaveManager();
}
public void GenerateProject(string projectName)
@@ -286,4 +289,77 @@ namespace Ichni
clip.ForEach(e => e.ExecuteBM());
}
}
public class AutoSaveManager
{
private string autoSavePath => Application.streamingAssetsPath + "/AutoSave/" +
EditorManager.instance.projectInformation.projectName;
private string GetAutoSavePath(string autoSaveName) => autoSavePath + "/" + autoSaveName + ".json";
private float autoSaveInterval => EditorManager.instance.editorSettings.autoSaveInterval;
private int maximumAutoSaveCount => EditorManager.instance.editorSettings.maximumAutoSaveCount;
public float autoSaveTimer;
public AutoSaveManager()
{
autoSaveTimer = 0;
}
public void UpdateAutoSave()
{
autoSaveTimer += Time.deltaTime;
if (autoSaveTimer >= autoSaveInterval)
{
AutoSave();
autoSaveTimer = 0;
}
}
private void AutoSave()
{
List<string> saveFiles = GetSortedSaveFiles();
if (saveFiles.Count > 0)
{
// 删除最旧的存档(如果超过数量)
if (saveFiles.Count >= maximumAutoSaveCount)
{
string oldestSave = saveFiles[saveFiles.Count - 1];
File.Delete(oldestSave);
saveFiles.RemoveAt(saveFiles.Count - 1);
}
// 依次重命名存档
for (int i = saveFiles.Count - 1; i >= 0; i--)
{
string oldPath = saveFiles[i];
string newPath = GetAutoSavePath($"AutoSave_{i + 1}");
File.Move(oldPath, newPath);
}
}
// 保存最新存档
string newestSavePath = GetAutoSavePath("AutoSave_0");
SaveBeatMap(newestSavePath);
}
private void SaveBeatMap(string autoSavePath)
{
EditorManager.instance.beatmapContainer.SaveBM();
ES3.Save("BeatMap", EditorManager.instance.beatmapContainer.matchedBM as BeatmapContainer_BM,
autoSavePath, ProjectManager.SaveSettings);
}
private List<string> GetSortedSaveFiles()
{
if(!ES3.DirectoryExists(autoSavePath))
{
Directory.CreateDirectory(autoSavePath);
}
List<string> saveFiles = new List<string>(Directory.GetFiles(autoSavePath, "AutoSave_*.es3"));
saveFiles.Sort(string.Compare);
return saveFiles;
}
}
}