-
Notifications
You must be signed in to change notification settings - Fork 143
fix(custom-page): 修复自定义主页不能正常跳转的问题 #3528
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
Pigeon0v0
wants to merge
3
commits into
dev
Choose a base branch
from
fix/custom-page-link
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
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| <local:MyPageRight x:Class="PCL.PageHelpDetail" | ||
| xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
| xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
| xmlns:local="clr-namespace:PCL" | ||
| PanScroll="{Binding ElementName=PanBack}"> | ||
| <local:MyScrollViewer x:Name="PanBack" | ||
| VerticalScrollBarVisibility="Auto" | ||
| HorizontalScrollBarVisibility="Disabled"> | ||
| <StackPanel x:Name="PanCustom" Margin="25,25,25,10"> | ||
| <StackPanel.Resources> | ||
| <Style TargetType="TextBlock" BasedOn="{StaticResource BasedOnTextBlock}"> | ||
| <Setter Property="TextWrapping" Value="Wrap" /> | ||
| </Style> | ||
| <Style TargetType="local:MyCard"> | ||
| <Setter Property="Margin" Value="0,0,0,15" /> | ||
| </Style> | ||
| <Style TargetType="Image"> | ||
| <Setter Property="RenderOptions.BitmapScalingMode" Value="HighQuality" /> | ||
| <Setter Property="HorizontalAlignment" Value="Center" /> | ||
| </Style> | ||
| </StackPanel.Resources> | ||
| </StackPanel> | ||
| </local:MyScrollViewer> | ||
| </local:MyPageRight> |
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,86 @@ | ||
| using System.IO; | ||
| using System.Security; | ||
| using System.Windows; | ||
| using PCL.Core.App.Localization; | ||
|
|
||
| namespace PCL; | ||
|
|
||
| public partial class PageHelpDetail : IRefreshable | ||
| { | ||
| internal sealed record HelpContent(string SourcePath, string Title, string Xaml); | ||
|
|
||
| private HelpContent _content = null!; | ||
|
|
||
| public string Title => _content.Title; | ||
|
|
||
| internal PageHelpDetail(HelpContent content) | ||
| { | ||
| InitializeComponent(); | ||
| Loaded += (_, _) => PanBack.ScrollToHome(); | ||
| ApplyContent(content); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// 从帮助 JSON 及同名 XAML 文件读取详情页数据。失败会抛出异常。 | ||
| /// </summary> | ||
| internal static HelpContent LoadContent(string jsonPath) | ||
| { | ||
| if (!File.Exists(jsonPath)) | ||
| throw new FileNotFoundException("未找到帮助 JSON 文件", jsonPath); | ||
|
|
||
| var json = ModMain.ArgumentReplace(File.ReadAllText(jsonPath), SecurityElement.Escape); | ||
| using var document = JsonDocument.Parse(json); | ||
| if (!document.RootElement.TryGetProperty("Title", out var titleElement) || | ||
| titleElement.ValueKind != JsonValueKind.String || | ||
| string.IsNullOrWhiteSpace(titleElement.GetString())) | ||
| throw new ArgumentException("帮助 JSON 中未找到有效的 Title 项", nameof(jsonPath)); | ||
|
|
||
| var xamlPath = Path.ChangeExtension(jsonPath, ".xaml"); | ||
| if (!File.Exists(xamlPath)) | ||
| throw new FileNotFoundException("未找到帮助 JSON 对应的 XAML 文件", xamlPath); | ||
|
|
||
| var xaml = File.ReadAllText(xamlPath); | ||
| if (string.IsNullOrWhiteSpace(xaml)) | ||
| throw new InvalidDataException("帮助 XAML 文件为空"); | ||
|
|
||
| return new HelpContent(jsonPath, titleElement.GetString()!, xaml); | ||
| } | ||
|
|
||
| private void ApplyContent(HelpContent content) | ||
| { | ||
| // 修改时应同时修改 PageLaunchRight.LoadContent。 | ||
| var xaml = ModMain.ArgumentReplace(content.Xaml); | ||
| while (xaml.Contains("xmlns")) | ||
| xaml = xaml.RegexReplace("xmlns[^\"']*(\"|')[^\"']*(\"|')", "").Replace("xmlns", ""); | ||
| xaml = | ||
| $"<StackPanel xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xmlns:sys=\"clr-namespace:System;assembly=System.Runtime\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" xmlns:local=\"clr-namespace:PCL;assembly=Plain Craft Launcher 2\">{xaml}</StackPanel>"; | ||
|
|
||
| var element = (UIElement)ModBase.GetObjectFromXML(xaml, out var sanitizeResult); | ||
| foreach (var unsupported in sanitizeResult.UnsupportedTypesFound) | ||
| HintService.Hint(Lang.Text("Event.Sanitize.UnsupportedTypeHint", unsupported), HintType.Error); | ||
| foreach (var unknown in sanitizeResult.UnrecognizedTypes) | ||
| HintService.Hint(Lang.Text("Event.Sanitize.UnknownTypeHint", unknown), HintType.Error); | ||
|
|
||
| _content = content; | ||
| PanCustom.Children.Clear(); | ||
| PanCustom.Children.Add(element); | ||
| if (ModMain.frmMain?.pageCurrent.page == FormMain.PageType.HelpDetail) | ||
| ModMain.frmMain.PageNameRefresh(); | ||
| } | ||
|
|
||
| public void Refresh() | ||
| { | ||
| try | ||
| { | ||
| ApplyContent(LoadContent(_content.SourcePath)); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| ModBase.Log( | ||
| ex, | ||
| "刷新帮助详情页失败", | ||
| ModBase.LogLevel.Msgbox, | ||
| userSummary: Lang.Text("Event.Error.ExecutionFailed", EventType.OpenHelp, _content.SourcePath)); | ||
| } | ||
| } | ||
| } | ||
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 the help JSON contains a launcher placeholder such as
{path}or{version_path},ArgumentReplacepasses its Windows value throughSecurityElement.Escape, which does not escape backslashes for JSON. The resulting value contains sequences such asC:\PCL/\P, soJsonDocument.Parsetreats them as invalid JSON escapes and the custom help page never opens. Use JSON string escaping (or parse/replace the relevant JSON value) instead of XML escaping.Useful? React with 👍 / 👎.