Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
175 changes: 105 additions & 70 deletions ContextMenuManager/Methods/BackupHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,26 @@ public void RestoreItems(string filePath, List<string> sceneTexts, RestoreMode r
private BackupMode backupMode; // 目前备份模式
private RestoreMode restoreMode; // 目前恢复模式

private RegistryKey GetRegistryKeySafe(string regPath)
{
if (backup)
{
try
{
RegistryEx.GetRootAndSubRegPath(regPath, out var root, out var keyPath);
return root.OpenSubKey(keyPath, false);
}
catch
{
return null;
}
}
else
{
return RegistryEx.GetRegistryKey(regPath);
}
}

// 删除弃用版本的备份
private void CheckDeprecatedBackup()
{
Expand Down Expand Up @@ -349,29 +369,26 @@ private void BackupRestoreItem(MyListItem item, string itemName, string keyName,

private bool CheckItemNeedChange(string itemName, string keyName, BackupItemType itemType, bool currentItemData)
{
foreach (var item in sceneRestoreList)
var item = GetItem(currentScene, keyName, itemType);
if (item != null)
{
// 成功匹配到后的处理方式:KeyName和ItemType匹配后检查ItemVisible
if (item.KeyName == keyName && item.ItemType == itemType)
var itemData = false;
try
{
var itemData = false;
try
{
itemData = Convert.ToBoolean(item.ItemData);
}
catch
{
return false;
}
if (itemData != currentItemData)
{
restoreList.Add(new RestoreChangedItem(currentScene, itemName, itemData.ToString()));
return true;
}
else
{
return false;
}
itemData = Convert.ToBoolean(item.ItemData);
}
catch
{
return false;
}
if (itemData != currentItemData)
{
restoreList.Add(new RestoreChangedItem(currentScene, itemName, itemData.ToString()));
return true;
}
else
{
return false;
}
}
if ((restoreMode == RestoreMode.DisableNotOnList && currentItemData) ||
Expand Down Expand Up @@ -410,32 +427,29 @@ private void BackupRestoreItem(MyListItem item, string itemName, string keyName,

private bool CheckItemNeedChange(string itemName, string keyName, BackupItemType itemType, int currentItemData, out int restoreItemData)
{
foreach (var item in sceneRestoreList)
var item = GetItem(currentScene, keyName, itemType);
if (item != null)
{
// 成功匹配到后的处理方式:KeyName和ItemType匹配后检查itemData
if (item.KeyName == keyName && item.ItemType == itemType)
int itemData;
try
{
int itemData;
try
{
itemData = Convert.ToInt32(item.ItemData);
}
catch
{
restoreItemData = 0;
return false;
}
if (itemData != currentItemData)
{
restoreList.Add(new RestoreChangedItem(currentScene, itemName, itemData.ToString()));
restoreItemData = itemData;
return true;
}
else
{
restoreItemData = 0;
return false;
}
itemData = Convert.ToInt32(item.ItemData);
}
catch
{
restoreItemData = 0;
return false;
}
if (itemData != currentItemData)
{
restoreList.Add(new RestoreChangedItem(currentScene, itemName, itemData.ToString()));
restoreItemData = itemData;
return true;
}
else
{
restoreItemData = 0;
return false;
}
}
restoreItemData = 0;
Expand Down Expand Up @@ -469,23 +483,20 @@ private void BackupRestoreItem(MyListItem item, string itemName, string keyName,

private bool CheckItemNeedChange(string itemName, string keyName, BackupItemType itemType, string currentItemData, out string restoreItemData)
{
foreach (var item in sceneRestoreList)
var item = GetItem(currentScene, keyName, itemType);
if (item != null)
{
// 成功匹配到后的处理方式:KeyName和ItemType匹配后检查itemData
if (item.KeyName == keyName && item.ItemType == itemType)
var itemData = item.ItemData;
if (itemData != currentItemData)
{
var itemData = item.ItemData;
if (itemData != currentItemData)
{
restoreList.Add(new RestoreChangedItem(currentScene, itemName, itemData.ToString()));
restoreItemData = itemData;
return true;
}
else
{
restoreItemData = "";
return false;
}
restoreList.Add(new RestoreChangedItem(currentScene, itemName, itemData.ToString()));
restoreItemData = itemData;
return true;
}
else
{
restoreItemData = "";
return false;
}
}
restoreItemData = "";
Expand Down Expand Up @@ -653,7 +664,7 @@ private void GetShellListItems()
break;
case Scenes.Library:
var AddedScenePathes = new string[] { MENUPATH_LIBRARY_BACKGROUND, MENUPATH_LIBRARY_USER };
RegTrustedInstaller.TakeRegKeyOwnerShip(scenePath);
if (!backup) RegTrustedInstaller.TakeRegKeyOwnerShip(scenePath);
for (var j = 0; j < AddedScenePathes.Length; j++)
{
scenePath = AddedScenePathes[j];
Expand Down Expand Up @@ -687,16 +698,16 @@ private void GetShellListItems(string scenePath)
private void GetBackupItems(string scenePath)
{
if (scenePath == null) return;
RegTrustedInstaller.TakeRegKeyOwnerShip(scenePath);
if (!backup) RegTrustedInstaller.TakeRegKeyOwnerShip(scenePath);
GetBackupShellItems(GetShellPath(scenePath));
GetBackupShellExItems(GetShellExPath(scenePath));
}

private void GetBackupShellItems(string shellPath)
{
using var shellKey = RegistryEx.GetRegistryKey(shellPath);
using var shellKey = GetRegistryKeySafe(shellPath);
if (shellKey == null) return;
RegTrustedInstaller.TakeRegTreeOwnerShip(shellKey.Name);
if (!backup) RegTrustedInstaller.TakeRegTreeOwnerShip(shellKey.Name);
foreach (var keyName in shellKey.GetSubKeyNames())
{
var regPath = $@"{shellPath}\{keyName}";
Expand All @@ -718,10 +729,10 @@ private void GetBackupShellItems(string shellPath)
private void GetBackupShellExItems(string shellExPath)
{
var names = new List<string>();
using var shellExKey = RegistryEx.GetRegistryKey(shellExPath);
using var shellExKey = GetRegistryKeySafe(shellExPath);
if (shellExKey == null) return;
var isDragDrop = currentScene == Scenes.DragDrop;
RegTrustedInstaller.TakeRegTreeOwnerShip(shellExKey.Name);
if (!backup) RegTrustedInstaller.TakeRegTreeOwnerShip(shellExKey.Name);
var dic = ShellExItem.GetPathAndGuids(shellExPath, isDragDrop);
FoldGroupItem groupItem = null;
if (isDragDrop)
Expand Down Expand Up @@ -760,7 +771,7 @@ private void GetBackupShellExItems(string shellExPath)

private void GetBackupStoreItems()
{
using var shellKey = RegistryEx.GetRegistryKey(ShellItem.CommandStorePath);
using var shellKey = GetRegistryKeySafe(ShellItem.CommandStorePath);
foreach (var itemName in shellKey.GetSubKeyNames())
{
if (AppConfig.HideSysStoreItems && itemName.StartsWith("Windows.", StringComparison.OrdinalIgnoreCase)) continue;
Expand Down Expand Up @@ -1025,7 +1036,7 @@ private void GetWinXListItems()
private void GetIEItems()
{
var names = new List<string>();
using var ieKey = RegistryEx.GetRegistryKey(IEList.IEPath);
using var ieKey = GetRegistryKeySafe(IEList.IEPath);
if (ieKey == null) return;
foreach (var part in IEItem.MeParts)
{
Expand Down Expand Up @@ -1435,6 +1446,9 @@ public sealed class BackupList
// 备份列表/恢复列表缓存区
private static List<BackupItem> backupRestoreList = new();

// 备份查找表
private static readonly Dictionary<string, BackupItem> backupLookup = new();

// 单场景恢复列表暂存区
public static List<BackupItem> sceneRestoreList = new();

Expand All @@ -1454,15 +1468,29 @@ static BackupList()
namespaces.Add(string.Empty, string.Empty);
}

private static string GetLookupKey(Scenes scene, string keyName, BackupItemType type)
{
return $"{scene}|{keyName}|{type}";
}

public static BackupItem GetItem(Scenes scene, string keyName, BackupItemType type)
{
var key = GetLookupKey(scene, keyName, type);
return backupLookup.TryGetValue(key, out var item) ? item : null;
}

public static void AddItem(string keyName, BackupItemType backupItemType, string itemData, Scenes scene)
{
backupRestoreList.Add(new BackupItem
var item = new BackupItem
{
KeyName = keyName,
ItemType = backupItemType,
ItemData = itemData,
BackupScene = scene,
});
};
backupRestoreList.Add(item);
var key = GetLookupKey(scene, keyName, backupItemType);
if (!backupLookup.ContainsKey(key)) backupLookup.Add(key, item);
}

public static void AddItem(string keyName, BackupItemType backupItemType, bool itemData, Scenes scene)
Expand All @@ -1483,6 +1511,7 @@ public static int GetBackupListCount()
public static void ClearBackupList()
{
backupRestoreList.Clear();
backupLookup.Clear();
}

public static void SaveBackupList(string filePath)
Expand Down Expand Up @@ -1517,6 +1546,12 @@ public static void LoadBackupList(string filePath)

// 获取BackupList对象
backupRestoreList = myData.BackupList;
backupLookup.Clear();
foreach (var item in backupRestoreList)
{
var key = GetLookupKey(item.BackupScene, item.KeyName, item.ItemType);
if (!backupLookup.ContainsKey(key)) backupLookup.Add(key, item);
}
}

public static void LoadTempRestoreList(Scenes scene)
Expand Down
Loading