Skip to content

Commit 3f77709

Browse files
committed
优化备份功能
1 parent 3000aff commit 3f77709

File tree

1 file changed

+105
-70
lines changed

1 file changed

+105
-70
lines changed

ContextMenuManager/Methods/BackupHelper.cs

Lines changed: 105 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,26 @@ public void RestoreItems(string filePath, List<string> sceneTexts, RestoreMode r
195195
private BackupMode backupMode; // 目前备份模式
196196
private RestoreMode restoreMode; // 目前恢复模式
197197

198+
private RegistryKey GetRegistryKeySafe(string regPath)
199+
{
200+
if (backup)
201+
{
202+
try
203+
{
204+
RegistryEx.GetRootAndSubRegPath(regPath, out var root, out var keyPath);
205+
return root.OpenSubKey(keyPath, false);
206+
}
207+
catch
208+
{
209+
return null;
210+
}
211+
}
212+
else
213+
{
214+
return RegistryEx.GetRegistryKey(regPath);
215+
}
216+
}
217+
198218
// 删除弃用版本的备份
199219
private void CheckDeprecatedBackup()
200220
{
@@ -349,29 +369,26 @@ private void BackupRestoreItem(MyListItem item, string itemName, string keyName,
349369

350370
private bool CheckItemNeedChange(string itemName, string keyName, BackupItemType itemType, bool currentItemData)
351371
{
352-
foreach (var item in sceneRestoreList)
372+
var item = GetItem(currentScene, keyName, itemType);
373+
if (item != null)
353374
{
354-
// 成功匹配到后的处理方式:KeyName和ItemType匹配后检查ItemVisible
355-
if (item.KeyName == keyName && item.ItemType == itemType)
375+
var itemData = false;
376+
try
356377
{
357-
var itemData = false;
358-
try
359-
{
360-
itemData = Convert.ToBoolean(item.ItemData);
361-
}
362-
catch
363-
{
364-
return false;
365-
}
366-
if (itemData != currentItemData)
367-
{
368-
restoreList.Add(new RestoreChangedItem(currentScene, itemName, itemData.ToString()));
369-
return true;
370-
}
371-
else
372-
{
373-
return false;
374-
}
378+
itemData = Convert.ToBoolean(item.ItemData);
379+
}
380+
catch
381+
{
382+
return false;
383+
}
384+
if (itemData != currentItemData)
385+
{
386+
restoreList.Add(new RestoreChangedItem(currentScene, itemName, itemData.ToString()));
387+
return true;
388+
}
389+
else
390+
{
391+
return false;
375392
}
376393
}
377394
if ((restoreMode == RestoreMode.DisableNotOnList && currentItemData) ||
@@ -410,32 +427,29 @@ private void BackupRestoreItem(MyListItem item, string itemName, string keyName,
410427

411428
private bool CheckItemNeedChange(string itemName, string keyName, BackupItemType itemType, int currentItemData, out int restoreItemData)
412429
{
413-
foreach (var item in sceneRestoreList)
430+
var item = GetItem(currentScene, keyName, itemType);
431+
if (item != null)
414432
{
415-
// 成功匹配到后的处理方式:KeyName和ItemType匹配后检查itemData
416-
if (item.KeyName == keyName && item.ItemType == itemType)
433+
int itemData;
434+
try
417435
{
418-
int itemData;
419-
try
420-
{
421-
itemData = Convert.ToInt32(item.ItemData);
422-
}
423-
catch
424-
{
425-
restoreItemData = 0;
426-
return false;
427-
}
428-
if (itemData != currentItemData)
429-
{
430-
restoreList.Add(new RestoreChangedItem(currentScene, itemName, itemData.ToString()));
431-
restoreItemData = itemData;
432-
return true;
433-
}
434-
else
435-
{
436-
restoreItemData = 0;
437-
return false;
438-
}
436+
itemData = Convert.ToInt32(item.ItemData);
437+
}
438+
catch
439+
{
440+
restoreItemData = 0;
441+
return false;
442+
}
443+
if (itemData != currentItemData)
444+
{
445+
restoreList.Add(new RestoreChangedItem(currentScene, itemName, itemData.ToString()));
446+
restoreItemData = itemData;
447+
return true;
448+
}
449+
else
450+
{
451+
restoreItemData = 0;
452+
return false;
439453
}
440454
}
441455
restoreItemData = 0;
@@ -469,23 +483,20 @@ private void BackupRestoreItem(MyListItem item, string itemName, string keyName,
469483

470484
private bool CheckItemNeedChange(string itemName, string keyName, BackupItemType itemType, string currentItemData, out string restoreItemData)
471485
{
472-
foreach (var item in sceneRestoreList)
486+
var item = GetItem(currentScene, keyName, itemType);
487+
if (item != null)
473488
{
474-
// 成功匹配到后的处理方式:KeyName和ItemType匹配后检查itemData
475-
if (item.KeyName == keyName && item.ItemType == itemType)
489+
var itemData = item.ItemData;
490+
if (itemData != currentItemData)
476491
{
477-
var itemData = item.ItemData;
478-
if (itemData != currentItemData)
479-
{
480-
restoreList.Add(new RestoreChangedItem(currentScene, itemName, itemData.ToString()));
481-
restoreItemData = itemData;
482-
return true;
483-
}
484-
else
485-
{
486-
restoreItemData = "";
487-
return false;
488-
}
492+
restoreList.Add(new RestoreChangedItem(currentScene, itemName, itemData.ToString()));
493+
restoreItemData = itemData;
494+
return true;
495+
}
496+
else
497+
{
498+
restoreItemData = "";
499+
return false;
489500
}
490501
}
491502
restoreItemData = "";
@@ -653,7 +664,7 @@ private void GetShellListItems()
653664
break;
654665
case Scenes.Library:
655666
var AddedScenePathes = new string[] { MENUPATH_LIBRARY_BACKGROUND, MENUPATH_LIBRARY_USER };
656-
RegTrustedInstaller.TakeRegKeyOwnerShip(scenePath);
667+
if (!backup) RegTrustedInstaller.TakeRegKeyOwnerShip(scenePath);
657668
for (var j = 0; j < AddedScenePathes.Length; j++)
658669
{
659670
scenePath = AddedScenePathes[j];
@@ -687,16 +698,16 @@ private void GetShellListItems(string scenePath)
687698
private void GetBackupItems(string scenePath)
688699
{
689700
if (scenePath == null) return;
690-
RegTrustedInstaller.TakeRegKeyOwnerShip(scenePath);
701+
if (!backup) RegTrustedInstaller.TakeRegKeyOwnerShip(scenePath);
691702
GetBackupShellItems(GetShellPath(scenePath));
692703
GetBackupShellExItems(GetShellExPath(scenePath));
693704
}
694705

695706
private void GetBackupShellItems(string shellPath)
696707
{
697-
using var shellKey = RegistryEx.GetRegistryKey(shellPath);
708+
using var shellKey = GetRegistryKeySafe(shellPath);
698709
if (shellKey == null) return;
699-
RegTrustedInstaller.TakeRegTreeOwnerShip(shellKey.Name);
710+
if (!backup) RegTrustedInstaller.TakeRegTreeOwnerShip(shellKey.Name);
700711
foreach (var keyName in shellKey.GetSubKeyNames())
701712
{
702713
var regPath = $@"{shellPath}\{keyName}";
@@ -718,10 +729,10 @@ private void GetBackupShellItems(string shellPath)
718729
private void GetBackupShellExItems(string shellExPath)
719730
{
720731
var names = new List<string>();
721-
using var shellExKey = RegistryEx.GetRegistryKey(shellExPath);
732+
using var shellExKey = GetRegistryKeySafe(shellExPath);
722733
if (shellExKey == null) return;
723734
var isDragDrop = currentScene == Scenes.DragDrop;
724-
RegTrustedInstaller.TakeRegTreeOwnerShip(shellExKey.Name);
735+
if (!backup) RegTrustedInstaller.TakeRegTreeOwnerShip(shellExKey.Name);
725736
var dic = ShellExItem.GetPathAndGuids(shellExPath, isDragDrop);
726737
FoldGroupItem groupItem = null;
727738
if (isDragDrop)
@@ -760,7 +771,7 @@ private void GetBackupShellExItems(string shellExPath)
760771

761772
private void GetBackupStoreItems()
762773
{
763-
using var shellKey = RegistryEx.GetRegistryKey(ShellItem.CommandStorePath);
774+
using var shellKey = GetRegistryKeySafe(ShellItem.CommandStorePath);
764775
foreach (var itemName in shellKey.GetSubKeyNames())
765776
{
766777
if (AppConfig.HideSysStoreItems && itemName.StartsWith("Windows.", StringComparison.OrdinalIgnoreCase)) continue;
@@ -1025,7 +1036,7 @@ private void GetWinXListItems()
10251036
private void GetIEItems()
10261037
{
10271038
var names = new List<string>();
1028-
using var ieKey = RegistryEx.GetRegistryKey(IEList.IEPath);
1039+
using var ieKey = GetRegistryKeySafe(IEList.IEPath);
10291040
if (ieKey == null) return;
10301041
foreach (var part in IEItem.MeParts)
10311042
{
@@ -1435,6 +1446,9 @@ public sealed class BackupList
14351446
// 备份列表/恢复列表缓存区
14361447
private static List<BackupItem> backupRestoreList = new();
14371448

1449+
// 备份查找表
1450+
private static readonly Dictionary<string, BackupItem> backupLookup = new();
1451+
14381452
// 单场景恢复列表暂存区
14391453
public static List<BackupItem> sceneRestoreList = new();
14401454

@@ -1454,15 +1468,29 @@ static BackupList()
14541468
namespaces.Add(string.Empty, string.Empty);
14551469
}
14561470

1471+
private static string GetLookupKey(Scenes scene, string keyName, BackupItemType type)
1472+
{
1473+
return $"{scene}|{keyName}|{type}";
1474+
}
1475+
1476+
public static BackupItem GetItem(Scenes scene, string keyName, BackupItemType type)
1477+
{
1478+
var key = GetLookupKey(scene, keyName, type);
1479+
return backupLookup.TryGetValue(key, out var item) ? item : null;
1480+
}
1481+
14571482
public static void AddItem(string keyName, BackupItemType backupItemType, string itemData, Scenes scene)
14581483
{
1459-
backupRestoreList.Add(new BackupItem
1484+
var item = new BackupItem
14601485
{
14611486
KeyName = keyName,
14621487
ItemType = backupItemType,
14631488
ItemData = itemData,
14641489
BackupScene = scene,
1465-
});
1490+
};
1491+
backupRestoreList.Add(item);
1492+
var key = GetLookupKey(scene, keyName, backupItemType);
1493+
if (!backupLookup.ContainsKey(key)) backupLookup.Add(key, item);
14661494
}
14671495

14681496
public static void AddItem(string keyName, BackupItemType backupItemType, bool itemData, Scenes scene)
@@ -1483,6 +1511,7 @@ public static int GetBackupListCount()
14831511
public static void ClearBackupList()
14841512
{
14851513
backupRestoreList.Clear();
1514+
backupLookup.Clear();
14861515
}
14871516

14881517
public static void SaveBackupList(string filePath)
@@ -1517,6 +1546,12 @@ public static void LoadBackupList(string filePath)
15171546

15181547
// 获取BackupList对象
15191548
backupRestoreList = myData.BackupList;
1549+
backupLookup.Clear();
1550+
foreach (var item in backupRestoreList)
1551+
{
1552+
var key = GetLookupKey(item.BackupScene, item.KeyName, item.ItemType);
1553+
if (!backupLookup.ContainsKey(key)) backupLookup.Add(key, item);
1554+
}
15201555
}
15211556

15221557
public static void LoadTempRestoreList(Scenes scene)

0 commit comments

Comments
 (0)