103 lines
3.3 KiB
C#
103 lines
3.3 KiB
C#
using System.IO;
|
|
using UnityEngine;
|
|
|
|
public static class LegacyPlainSaveMigrator
|
|
{
|
|
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
|
|
private static void MigrateLegacyPlainFiles()
|
|
{
|
|
string root = Application.persistentDataPath;
|
|
MigrateSingleFile("daily_task", "runtime", Path.Combine(root, "daily_tasks.json"));
|
|
MigrateSingleFile("mail_state", "runtime", Path.Combine(root, "mail_state.json"));
|
|
MigrateSingleFile("store_state", "runtime", Path.Combine(root, "storeSystem_state.json"));
|
|
|
|
MigratePattern("song_runtime", root, "SongData_*.json", fileNameWithoutExt =>
|
|
{
|
|
if (fileNameWithoutExt.StartsWith("SongData_"))
|
|
{
|
|
return fileNameWithoutExt.Substring("SongData_".Length);
|
|
}
|
|
|
|
return fileNameWithoutExt;
|
|
});
|
|
|
|
MigratePattern("song_export", Path.Combine(root, "SongDataJson"), "*.json", fileNameWithoutExt => fileNameWithoutExt);
|
|
MigratePattern("song_export", Path.Combine(root, "song_json_export"), "*.json", fileNameWithoutExt => fileNameWithoutExt);
|
|
}
|
|
|
|
private static void MigrateSingleFile(string category, string key, string legacyPath)
|
|
{
|
|
if (!File.Exists(legacyPath))
|
|
{
|
|
return;
|
|
}
|
|
|
|
try
|
|
{
|
|
string json = File.ReadAllText(legacyPath);
|
|
if (string.IsNullOrEmpty(json))
|
|
{
|
|
File.Delete(legacyPath);
|
|
return;
|
|
}
|
|
|
|
SecureSaveVault.SaveRawJson(category, key, json, legacyPath);
|
|
}
|
|
catch (System.Exception ex)
|
|
{
|
|
Debug.LogWarning($"[LegacyPlainSaveMigrator] Failed to migrate {legacyPath}: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
private static void MigratePattern(string category, string directory, string searchPattern, System.Func<string, string> keyResolver)
|
|
{
|
|
if (!Directory.Exists(directory))
|
|
{
|
|
return;
|
|
}
|
|
|
|
string[] files = Directory.GetFiles(directory, searchPattern, SearchOption.TopDirectoryOnly);
|
|
for (int i = 0; i < files.Length; i++)
|
|
{
|
|
string legacyPath = files[i];
|
|
try
|
|
{
|
|
string json = File.ReadAllText(legacyPath);
|
|
if (string.IsNullOrEmpty(json))
|
|
{
|
|
File.Delete(legacyPath);
|
|
continue;
|
|
}
|
|
|
|
string key = keyResolver != null ? keyResolver(Path.GetFileNameWithoutExtension(legacyPath)) : Path.GetFileNameWithoutExtension(legacyPath);
|
|
SecureSaveVault.SaveRawJson(category, key, json, legacyPath);
|
|
}
|
|
catch (System.Exception ex)
|
|
{
|
|
Debug.LogWarning($"[LegacyPlainSaveMigrator] Failed to migrate {legacyPath}: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
TryDeleteDirectoryIfEmpty(directory);
|
|
}
|
|
|
|
private static void TryDeleteDirectoryIfEmpty(string directory)
|
|
{
|
|
try
|
|
{
|
|
if (!Directory.Exists(directory))
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (Directory.GetFiles(directory).Length == 0 && Directory.GetDirectories(directory).Length == 0)
|
|
{
|
|
Directory.Delete(directory, false);
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
}
|
|
}
|
|
}
|