Skip to content

Commit 8738231

Browse files
committed
Fix nullability (pt. 2)
1 parent e8a4aa2 commit 8738231

7 files changed

Lines changed: 75 additions & 75 deletions

File tree

CollapseLauncher/Classes/Helper/Metadata/PresetConfig.cs

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using CollapseLauncher.Helper.JsonConverter;
33
using CollapseLauncher.Helper.LauncherApiLoader;
44
using CollapseLauncher.Helper.LauncherApiLoader.HoYoPlay;
5+
using CollapseLauncher.Interfaces.Class;
56
using Hi3Helper;
67
using Hi3Helper.Data;
78
using Hi3Helper.EncTool;
@@ -241,46 +242,45 @@ internal static class PresetConfigExt
241242
string launcherIdOrPasswordHead, string gameIdOrGamePackageIdHead,
242243
string? launcherIdOrPassword, string? gameIdOrGamePackageId)
243244
{
244-
if (string.IsNullOrEmpty(url))
245+
if (string.IsNullOrEmpty(url)
246+
|| string.IsNullOrEmpty(launcherIdOrPassword)
247+
|| string.IsNullOrEmpty(gameIdOrGamePackageId))
245248
return url;
246249

247-
if (string.IsNullOrEmpty(launcherIdOrPassword) || string.IsNullOrEmpty(gameIdOrGamePackageId))
248-
return url;
249-
250-
int urlLen = url.Length + (1 << 10);
251-
char[] urlBuffer = ArrayPool<char>.Shared.Rent(urlLen);
252-
Span<char> urlSpanBuffer = urlBuffer;
253-
ReadOnlySpan<char> urlSpan = url;
250+
int urlLen = url.Length + (1 << 10);
251+
char[] urlBuffer = ArrayPool<char>.Shared.Rent(urlLen);
252+
Span<char> urlSpanBuffer = urlBuffer;
253+
ReadOnlySpan<char> urlSpan = url;
254254

255255
try
256256
{
257-
int urlSpanBufferLen = 0;
258-
Span<Range> splitRanges = stackalloc Range[32];
259-
int urlSplitRangesLen = urlSpan.Split(splitRanges, '?', StringSplitOptions.RemoveEmptyEntries);
257+
int urlSpanBufferLen = 0;
258+
Span<Range> splitRanges = stackalloc Range[32];
259+
int urlSplitRangesLen = urlSpan.Split(splitRanges, '?', StringSplitOptions.RemoveEmptyEntries);
260260
if (urlSplitRangesLen < 2)
261261
return url;
262262

263263
ReadOnlySpan<char> urlPathSpan = urlSpan[splitRanges[0]];
264-
ReadOnlySpan<char> querySpan = urlSpan[splitRanges[1]];
264+
ReadOnlySpan<char> querySpan = urlSpan[splitRanges[1]];
265265

266266
if (!urlPathSpan.TryCopyTo(urlSpanBuffer))
267267
throw new InvalidOperationException("Failed to copy url path string to buffer");
268-
urlSpanBufferLen += splitRanges[0].End.Value - splitRanges[0].Start.Value;
269-
urlSpanBuffer[urlSpanBufferLen++] = '?';
268+
urlSpanBufferLen += splitRanges[0].End.Value - splitRanges[0].Start.Value;
269+
urlSpanBuffer[urlSpanBufferLen++] = '?';
270270

271271
#region Parse and split queries - Sanitize the GameId and GameId query
272272
int querySplitRangesLen = querySpan.Split(splitRanges, '&', StringSplitOptions.RemoveEmptyEntries);
273273
int queryWritten = 0;
274274
Span<char> querySpanBuffer = urlSpanBuffer[urlSpanBufferLen..];
275275
for (int i = querySplitRangesLen - 1; i > -1; i--)
276276
{
277-
Range segmentRange = splitRanges[i];
278-
int segmentLen = segmentRange.End.Value - segmentRange.Start.Value;
277+
Range segmentRange = splitRanges[i];
278+
int segmentLen = segmentRange.End.Value - segmentRange.Start.Value;
279279
ReadOnlySpan<char> querySegment = querySpan[segmentRange];
280280

281281
// Skip GameId or GameId query head
282-
if (querySegment.StartsWith(launcherIdOrPasswordHead, StringComparison.OrdinalIgnoreCase)
283-
|| querySegment.StartsWith(gameIdOrGamePackageIdHead, StringComparison.OrdinalIgnoreCase))
282+
if (querySegment.StartsWith(launcherIdOrPasswordHead, StringComparison.OrdinalIgnoreCase)
283+
|| querySegment.StartsWith(gameIdOrGamePackageIdHead, StringComparison.OrdinalIgnoreCase))
284284
continue;
285285

286286
// Otherwise, add others
@@ -294,7 +294,7 @@ internal static class PresetConfigExt
294294

295295
// Append '&'
296296
if (queryWritten > 0 && querySpanBuffer[queryWritten - 1] != '&'
297-
&& querySpanBuffer[queryWritten - 1] != '?')
297+
&& querySpanBuffer[queryWritten - 1] != '?')
298298
querySpanBuffer[queryWritten++] = '&';
299299

300300
urlSpanBufferLen += queryWritten;
@@ -303,8 +303,8 @@ internal static class PresetConfigExt
303303
#region Append GameId and GameId query
304304
if (!launcherIdOrPasswordHead.TryCopyTo(urlSpanBuffer[urlSpanBufferLen..]))
305305
throw new InvalidOperationException("Failed to copy launcher id or password head string to buffer");
306-
urlSpanBufferLen += launcherIdOrPasswordHead.Length;
307-
urlSpanBuffer[urlSpanBufferLen++] = '=';
306+
urlSpanBufferLen += launcherIdOrPasswordHead.Length;
307+
urlSpanBuffer[urlSpanBufferLen++] = '=';
308308
if (!launcherIdOrPassword.TryCopyTo(urlSpanBuffer[urlSpanBufferLen..]))
309309
throw new InvalidOperationException("Failed to copy launcher id or password value string to buffer");
310310
urlSpanBufferLen += launcherIdOrPassword.Length;
@@ -313,13 +313,13 @@ internal static class PresetConfigExt
313313

314314
if (!gameIdOrGamePackageIdHead.TryCopyTo(urlSpanBuffer[urlSpanBufferLen..]))
315315
throw new InvalidOperationException("Failed to copy game id or package id head string to buffer");
316-
urlSpanBufferLen += gameIdOrGamePackageIdHead.Length;
317-
urlSpanBuffer[urlSpanBufferLen++] = '=';
316+
urlSpanBufferLen += gameIdOrGamePackageIdHead.Length;
317+
urlSpanBuffer[urlSpanBufferLen++] = '=';
318318
if (!gameIdOrGamePackageId.TryCopyTo(urlSpanBuffer[urlSpanBufferLen..]))
319319
throw new InvalidOperationException("Failed to copy game id or package id value string to buffer");
320320
urlSpanBufferLen += gameIdOrGamePackageId.Length;
321321

322-
string returnString = new string(urlBuffer, 0, urlSpanBufferLen);
322+
string returnString = new(urlBuffer, 0, urlSpanBufferLen);
323323
#if DEBUG
324324
LogWriteLine($"URL string's GameId or Password and GameId or PackageId association has been successfully replaced!\r\nSource: {url}\r\nResult: {returnString}", LogType.Debug, true);
325325
#endif
@@ -337,7 +337,8 @@ internal static class PresetConfigExt
337337
[JsonSerializable(typeof(PresetConfig))]
338338
internal sealed partial class PresetConfigJsonContext : JsonSerializerContext;
339339

340-
public class PresetConfig
340+
[GeneratedBindableCustomProperty]
341+
public partial class PresetConfig : NotifyPropertyChanged
341342
{
342343
#region Constants
343344
// ReSharper disable once UnusedMember.Local
@@ -795,12 +796,8 @@ private void SetVoiceLanguageID_StarRail(int langID)
795796
if (!value.DataVersion.TryGetValue(verInt, out GameDataVersion? verData))
796797
{
797798
// Fallback to find the default value anyway
798-
KeyValuePair<int, GameDataVersion>? kvpTemp = value.DataVersion.FirstOrDefault();
799-
if (kvpTemp == null)
800-
return null;
801-
802-
// ReSharper disable once ConstantConditionalAccessQualifier
803-
verData = kvpTemp?.Value;
799+
KeyValuePair<int, GameDataVersion> kvpTemp = value.DataVersion.FirstOrDefault();
800+
verData = kvpTemp.Value;
804801
}
805802

806803
if (verData == null)

CollapseLauncher/Classes/Plugins/PluginManager.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ namespace CollapseLauncher.Plugins;
1818
#nullable enable
1919
internal static partial class PluginManager
2020
{
21-
private const string PluginDirPrefix = "Hi3Helper.Plugin.*";
21+
private const string PluginDirPrefix = "Hi3Helper.Plugin.*";
2222
internal const string ManifestPrefix = "manifest.json";
2323

24-
public static readonly Dictionary<string, PluginInfo> PluginInstances = new(StringComparer.OrdinalIgnoreCase);
24+
public static Dictionary<string, PluginInfo> PluginInstances { get; } = new(StringComparer.OrdinalIgnoreCase);
2525

2626
internal static async Task LoadPlugins(
2727
Dictionary<string, Dictionary<string, PresetConfig>> launcherMetadataConfig,

CollapseLauncher/XAMLs/MainApp/Pages/GameSettingsPages/HonkaiGameSettingsPage.xaml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -987,15 +987,6 @@
987987
Text="{x:Bind helper:Locale.Current.Lang._GameSettingsPage.ApplyBtn, Mode=OneWay}" />
988988
</StackPanel>
989989
</Button>
990-
<TextBlock x:Name="ApplyText"
991-
Grid.Column="1"
992-
Margin="16,-4,0,0"
993-
HorizontalAlignment="Stretch"
994-
VerticalAlignment="Center"
995-
Style="{ThemeResource BodyStrongTextBlockStyle}"
996-
Text="{x:Bind helper:Locale.Current.Lang._GameSettingsPage.SettingsApplied, Mode=OneWay}"
997-
TextWrapping="Wrap"
998-
Visibility="Collapsed" />
999990
<StackPanel Grid.Column="2"
1000991
HorizontalAlignment="Right"
1001992
Orientation="Horizontal">

CollapseLauncher/XAMLs/MainApp/Pages/GameSettingsPages/StarRailGameSettingsPage.xaml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -877,15 +877,6 @@
877877
Text="{x:Bind helper:Locale.Current.Lang._GameSettingsPage.ApplyBtn, Mode=OneWay}" />
878878
</StackPanel>
879879
</Button>
880-
<TextBlock x:Name="ApplyText"
881-
Grid.Column="1"
882-
Margin="16,-4,0,0"
883-
HorizontalAlignment="Stretch"
884-
VerticalAlignment="Center"
885-
Style="{ThemeResource BodyStrongTextBlockStyle}"
886-
Text="{x:Bind helper:Locale.Current.Lang._GameSettingsPage.SettingsApplied, Mode=OneWay}"
887-
TextWrapping="Wrap"
888-
Visibility="Collapsed" />
889880
<StackPanel Grid.Column="2"
890881
HorizontalAlignment="Right"
891882
Orientation="Horizontal">

CollapseLauncher/XAMLs/MainApp/Pages/GameSettingsPages/ZenlessGameSettingsPage.xaml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,15 +1092,6 @@
10921092
Text="{x:Bind helper:Locale.Current.Lang._GameSettingsPage.ApplyBtn, Mode=OneWay}" />
10931093
</StackPanel>
10941094
</Button>
1095-
<TextBlock x:Name="ApplyText"
1096-
Grid.Column="1"
1097-
Margin="16,-4,0,0"
1098-
HorizontalAlignment="Stretch"
1099-
VerticalAlignment="Center"
1100-
Style="{ThemeResource BodyStrongTextBlockStyle}"
1101-
Text="{x:Bind helper:Locale.Current.Lang._GameSettingsPage.SettingsApplied, Mode=OneWay}"
1102-
TextWrapping="Wrap"
1103-
Visibility="Collapsed" />
11041095
<StackPanel Grid.Column="2"
11051096
HorizontalAlignment="Right"
11061097
Orientation="Horizontal">

CollapseLauncher/XAMLs/MainApp/Pages/SettingsPage.xaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2257,15 +2257,15 @@
22572257
<Grid Padding="8,2"
22582258
Background="DarkBlue"
22592259
CornerRadius="8"
2260-
Visibility="{x:Bind innerConfig:LauncherConfig.IsPreview, Mode=OneWay, Converter={StaticResource BooleanVisibilityConverter}}">
2260+
Visibility="{x:Bind IsPreviewBuild, Mode=OneWay, Converter={StaticResource BooleanVisibilityConverter}}">
22612261
<TextBlock FontSize="10"
22622262
FontWeight="Bold"
22632263
Text="Preview" />
22642264
</Grid>
22652265
<Grid Padding="8,2"
22662266
Background="DarkOliveGreen"
22672267
CornerRadius="8"
2268-
Visibility="{x:Bind innerConfig:LauncherConfig.IsPreview, Mode=OneWay, Converter={StaticResource InverseBooleanVisibilityConverter}}">
2268+
Visibility="{x:Bind IsPreviewBuild, Mode=OneWay, Converter={StaticResource InverseBooleanVisibilityConverter}}">
22692269
<TextBlock FontSize="10"
22702270
FontWeight="Bold"
22712271
Text="Stable" />
@@ -3148,7 +3148,7 @@
31483148
</Grid>
31493149
</Grid>
31503150
</Button>
3151-
<ItemsRepeater ItemsSource="{x:Bind plugins:PluginManager.PluginInstances.Values, Mode=OneWay}">
3151+
<ItemsRepeater ItemsSource="{x:Bind PluginInstances.Values, Mode=OneWay}">
31523152
<ItemsRepeater.ItemTemplate>
31533153
<DataTemplate x:DataType="plugins:PluginInfo">
31543154
<Button Margin="0,0,0,4"

CollapseLauncher/XAMLs/MainApp/Pages/SettingsPage.xaml.cs

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
using System;
4343
using System.Collections.Generic;
4444
using System.Collections.ObjectModel;
45+
using System.ComponentModel;
4546
using System.Diagnostics;
4647
using System.IO;
4748
using System.Linq;
@@ -60,7 +61,7 @@
6061
using static Hi3Helper.Logger;
6162
using static Hi3Helper.Shared.Region.LauncherConfig;
6263
using CollapseUIExt = CollapseLauncher.Extension.UIElementExtensions;
63-
64+
using System.Runtime.CompilerServices;
6465
// ReSharper disable AsyncVoidMethod
6566
// ReSharper disable SwitchStatementHandlesSomeKnownEnumValuesWithDefault
6667
// ReSharper disable SwitchStatementMissingSomeEnumCasesNoDefault
@@ -73,7 +74,7 @@
7374
// ReSharper disable RedundantExtendsListEntry
7475
// ReSharper disable HeuristicUnreachableCode
7576

76-
77+
#nullable enable
7778
namespace CollapseLauncher.Pages
7879
{
7980
[GeneratedBindableCustomProperty]
@@ -100,8 +101,21 @@ public int SelectedCDN
100101
}
101102

102103
[GeneratedBindableCustomProperty]
103-
public sealed partial class SettingsPage : Page
104+
public sealed partial class SettingsPage : Page, INotifyPropertyChanged
104105
{
106+
#region INotifyPropertyChanged
107+
108+
public event PropertyChangedEventHandler? PropertyChanged;
109+
110+
// ReSharper disable once UnusedMember.Local
111+
private void OnPropertyChanged([CallerMemberName] string? propertyName = null)
112+
{
113+
// Raise the PropertyChanged event, passing the name of the property whose value has changed.
114+
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
115+
}
116+
117+
#endregion
118+
105119
#region Properties
106120

107121
private const string RepoUrl = "https://github.com/CollapseLauncher/Collapse/commit/";
@@ -117,13 +131,29 @@ public sealed partial class SettingsPage : Page
117131
private Brush _highlightBrush;
118132
private Brush _highlightSelectedBrush;
119133
private readonly List<MethodInfo> _dialogMethods;
120-
private List<string> DialogMethodNames { get; }
121-
public string SelectedDialogMethodName { get; set; }
122-
private CDNSelectionContext CdnSelectionContext { get; } = new();
134+
135+
private List<string> DialogMethodNames { get; }
136+
137+
public string SelectedDialogMethodName
138+
{
139+
get;
140+
set
141+
{
142+
field = value;
143+
OnPropertyChanged();
144+
}
145+
}
146+
147+
private CDNSelectionContext CdnSelectionContext { get; } = new();
123148

124149
private List<TextBlock> FFmpegDecodingModeSelectionItems { get; } = BuildFFmpegDecodingModeSelectionItems();
150+
125151
private List<StackPanel> FFmpegDecodingModeHelpItems { get; } = BuildFFmpegDecodingModeHelpItems();
126152

153+
private Dictionary<string, PluginInfo> PluginInstances => PluginManager.PluginInstances;
154+
155+
private bool IsPreviewBuild => LauncherConfig.IsPreview;
156+
127157
#nullable enable
128158
private string? _previousSearchQuery;
129159
#nullable restore
@@ -143,7 +173,11 @@ .. typeof(SimpleDialogs)
143173
.GetMethods(BindingFlags.Public | BindingFlags.Static)
144174
.Where(m => m.ReturnType == typeof(Task<ContentDialogResult>))
145175
];
146-
176+
177+
// Create brushes for highlighting
178+
_highlightBrush = new SolidColorBrush(Microsoft.UI.Colors.Yellow) { Opacity = 0.3 };
179+
_highlightSelectedBrush = new SolidColorBrush(Microsoft.UI.Colors.Blue) { Opacity = 0.5 };
180+
147181
DialogMethodNames = [.. _dialogMethods.Select(m => m.Name)];
148182

149183
if (DialogMethodNames.Count > 0)
@@ -1725,10 +1759,6 @@ private bool AreShortcutsEnabled
17251759
#region Settings Search
17261760
private void InitializeSettingsSearch()
17271761
{
1728-
// Create brushes for highlighting
1729-
_highlightBrush = new SolidColorBrush(Microsoft.UI.Colors.Yellow) { Opacity = 0.3 };
1730-
_highlightSelectedBrush = new SolidColorBrush(Microsoft.UI.Colors.Blue) { Opacity = 0.5 };
1731-
17321762
// Map all settings controls with their display text
17331763
if (!(_settingsControls.Count < 1)) _settingsControls.Clear();
17341764
ClearHighlighting();
@@ -1894,7 +1924,7 @@ private void AddControlRecursiveSelectible(FrameworkElement element)
18941924

18951925
private void ClearHighlighting()
18961926
{
1897-
foreach (HighlightableControlProperty? control in _highlightedControls)
1927+
foreach (HighlightableControlProperty control in _highlightedControls)
18981928
{
18991929
control.ClearHighlight();
19001930
}

0 commit comments

Comments
 (0)