修了不少东西
This commit is contained in:
@@ -48,11 +48,99 @@ public static class FirstRunFactoryResetService
|
||||
return;
|
||||
}
|
||||
|
||||
// 物理文件兜底探测(最后一道防线):
|
||||
// HasAnyRecoverableLocalState() 依赖各 Storage 的解密/结构判断,若因根目录别名未覆盖、
|
||||
// 或存档结构异常而误判为"无存档",仍可能走到出厂重置。这里直接扫描磁盘上是否存在
|
||||
// 任何存档物理文件(不解密、只看存在),只要有就绝不重置,宁可等待其它恢复路径。
|
||||
if (HasAnyPhysicalSaveFileOnDisk())
|
||||
{
|
||||
Debug.LogWarning("[FactoryReset] 磁盘上存在存档物理文件但未能读出," +
|
||||
"为避免误删玩家数据,跳过出厂重置并补写初始化标记。");
|
||||
EnsureInitializedMarker();
|
||||
return;
|
||||
}
|
||||
|
||||
Debug.LogWarning("[FactoryReset] 首次运行且无可恢复存档,应用出厂默认设置。");
|
||||
ApplyFactoryReset();
|
||||
EnsureInitializedMarker();
|
||||
}
|
||||
|
||||
// 扫描所有可能的存档根目录(含新旧包名/公司名别名),只要磁盘上存在任意一个存档物理文件
|
||||
// 就返回 true。只判存在、不解密——因此不受 deviceUniqueIdentifier 变化影响。
|
||||
private static bool HasAnyPhysicalSaveFileOnDisk()
|
||||
{
|
||||
try
|
||||
{
|
||||
IReadOnlyList<string> roots = SaveIdentityUtility.GetPersistentRootVariants();
|
||||
for (int i = 0; i < roots.Count; i++)
|
||||
{
|
||||
string root = roots[i];
|
||||
if (string.IsNullOrWhiteSpace(root))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// 1) 加密存档目录 .cache_bridge:递归找任意 .dat / .bak
|
||||
string vaultDir = Path.Combine(root, ".cache_bridge");
|
||||
if (DirectoryHasFileWithAnyExtension(vaultDir, new[] { ".dat", ".bak" }))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
// 2) 设备无关明文备份 player_progress.bbackup
|
||||
if (File.Exists(Path.Combine(root, "player_progress.bbackup")))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
// 3) 恢复镜像目录 .save_recovery:存在任意文件即视为有存档
|
||||
string recoveryDir = Path.Combine(root, ".save_recovery");
|
||||
if (DirectoryHasAnyFile(recoveryDir))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// 探测失败时采取保守策略:报告"存在存档",宁可跳过重置也不误删。
|
||||
Debug.LogWarning("[FactoryReset] 物理存档探测异常,保守跳过出厂重置: " + ex.Message);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static bool DirectoryHasFileWithAnyExtension(string directory, string[] extensions)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(directory) || !Directory.Exists(directory))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int i = 0; i < extensions.Length; i++)
|
||||
{
|
||||
string[] matches = Directory.GetFiles(directory, "*" + extensions[i], SearchOption.AllDirectories);
|
||||
if (matches != null && matches.Length > 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static bool DirectoryHasAnyFile(string directory)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(directory) || !Directory.Exists(directory))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
string[] matches = Directory.GetFiles(directory, "*", SearchOption.AllDirectories);
|
||||
return matches != null && matches.Length > 0;
|
||||
}
|
||||
|
||||
private static bool HasInitializedMarker()
|
||||
{
|
||||
try
|
||||
|
||||
Reference in New Issue
Block a user