-
Notifications
You must be signed in to change notification settings - Fork 143
feat(instance): 删除实例前备份截图与投影原理图 [GPT-5.6] #3537
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nya-a-cat
wants to merge
6
commits into
PCL-Community:dev
Choose a base branch
from
nya-a-cat:feat/protect-instance-personal-files
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
624b370
feat(setup): 添加实例回忆文件保护选项
nya-a-cat 78c09b2
feat(instance): 删除前备份回忆文件
nya-a-cat 9ec7b47
fix(instance): 统一隔离实例路径判断
nya-a-cat a110d2d
fix(instance): 简化删除前备份提示
nya-a-cat c58e958
refactor(instance): 收紧回忆文件备份接口
nya-a-cat ec82020
feat(instance): 增加删除前备份询问模式
nya-a-cat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
Plain Craft Launcher 2/Modules/Minecraft/ModPersonalFiles.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| using System.IO; | ||
| using PCL.Core.App; | ||
| using PCL.Core.App.Localization; | ||
| using PCL.Core.UI; | ||
|
|
||
| namespace PCL; | ||
|
|
||
| public static class ModPersonalFiles | ||
| { | ||
| private static string _ArchiveRoot => | ||
| Path.GetFullPath(Path.Combine(ModFolder.mcFolderSelected, "PCL", "PersonalFiles")); | ||
|
|
||
| private static (int FileCount, string TargetFolder) _Backup(McInstance instance) | ||
| { | ||
| var targetFolder = Path.Combine(_ArchiveRoot, instance.Name); | ||
| var instancePath = Path.GetFullPath(instance.PathInstance); | ||
| if (!instancePath.EndsWith(Path.DirectorySeparatorChar)) instancePath += Path.DirectorySeparatorChar; | ||
| if (targetFolder.StartsWith(instancePath, StringComparison.OrdinalIgnoreCase)) | ||
| throw new InvalidOperationException("回忆文件备份目录不能位于实例目录中"); | ||
|
|
||
| var fileCount = _CopyDirectory(Path.Combine(instance.PathIndie, "screenshots"), | ||
| Path.Combine(targetFolder, "screenshots")); | ||
| fileCount += _CopyDirectory(Path.Combine(instance.PathIndie, "schematics"), | ||
| Path.Combine(targetFolder, "schematics")); | ||
|
|
||
| if (fileCount > 0) | ||
| ModBase.Log($"[PersonalFiles] 已备份实例 {instance.Name} 的 {fileCount} 个回忆文件到 {targetFolder}"); | ||
| return (fileCount, targetFolder); | ||
| } | ||
|
|
||
| public static string? GetDeleteHint() | ||
| { | ||
| return Config.Launch.PersonalFilesBackup switch | ||
| { | ||
| PersonalFilesBackupMode.Disabled => null, | ||
| PersonalFilesBackupMode.AskEveryTime => Lang.Text("Instance.PersonalFiles.Backup.AskHint"), | ||
| _ => Lang.Text("Instance.PersonalFiles.Backup.DeleteHint") | ||
| }; | ||
| } | ||
|
|
||
| public static bool TryHandleBeforeDelete(McInstance instance) | ||
| { | ||
| try | ||
| { | ||
| var backupMode = Config.Launch.PersonalFilesBackup; | ||
| if (backupMode == PersonalFilesBackupMode.Disabled) return true; | ||
| if (backupMode == PersonalFilesBackupMode.AskEveryTime) | ||
| { | ||
| if (!_HasScreenshots(instance.PathIndie)) return true; | ||
| var promptResult = ModMain.MyMsgBox( | ||
| Lang.Text("Instance.PersonalFiles.Backup.Ask.Message", instance.Name), | ||
| Lang.Text("Instance.PersonalFiles.Backup.Ask.Title"), | ||
| Lang.Text("Instance.PersonalFiles.Backup.Ask.BackupAndDelete"), | ||
| Lang.Text("Instance.PersonalFiles.Backup.Ask.DeleteWithoutBackup"), | ||
| Lang.Text("Common.Action.Cancel")); | ||
| if (promptResult == 2) return true; | ||
| if (promptResult != 1) return false; | ||
| } | ||
|
|
||
| var result = _Backup(instance); | ||
| if (result.FileCount > 0) | ||
| HintService.Hint(Lang.Text("Instance.PersonalFiles.Backup.Success", result.FileCount, | ||
| result.TargetFolder), HintType.Success); | ||
| return true; | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| ModBase.Log(ex, $"备份实例 {instance.Name} 的回忆文件失败", ModBase.LogLevel.Msgbox, | ||
| userSummary: Lang.Text("Instance.PersonalFiles.Backup.Failed")); | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| private static bool _HasScreenshots(string instanceFolder) | ||
| { | ||
| var screenshotFolder = Path.Combine(instanceFolder, "screenshots"); | ||
| return Directory.Exists(screenshotFolder) && | ||
| Directory.EnumerateFiles(screenshotFolder, "*", _CreateEnumerationOptions()).Any(); | ||
| } | ||
|
|
||
| private static int _CopyDirectory(string sourceFolder, string targetFolder) | ||
| { | ||
| if (!Directory.Exists(sourceFolder)) return 0; | ||
| var fileCount = 0; | ||
| foreach (var sourceFile in Directory.EnumerateFiles(sourceFolder, "*", _CreateEnumerationOptions())) | ||
| { | ||
| var targetFile = Path.Combine(targetFolder, Path.GetRelativePath(sourceFolder, sourceFile)); | ||
| ModBase.CopyFile(sourceFile, targetFile); | ||
| File.SetCreationTimeUtc(targetFile, File.GetCreationTimeUtc(sourceFile)); | ||
| File.SetLastWriteTimeUtc(targetFile, File.GetLastWriteTimeUtc(sourceFile)); | ||
| fileCount += 1; | ||
| } | ||
|
|
||
| return fileCount; | ||
| } | ||
|
|
||
| private static EnumerationOptions _CreateEnumerationOptions() | ||
| { | ||
| return new EnumerationOptions | ||
| { | ||
| RecurseSubdirectories = true, | ||
| IgnoreInaccessible = false, | ||
| AttributesToSkip = FileAttributes.ReparsePoint | ||
| }; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When an isolated instance has a corrupt or unreadable version definition, its state is
McInstanceState.Error, which forcesisHintIndieto false and skips this new backup handler even ifPathIndieis the instance directory. The delete operation still removesPathInstance, so screenshots and schematics are silently lost under both Ask and Always modes; determine isolation from the configured/path relationship or probe the instance directory independently of validation state.Useful? React with 👍 / 👎.