Skip to content

Commit 1b2d110

Browse files
authored
Major CodeQA - 20250119 (#673)
# Main Goal This PR contains huge CodeQA and some code refactoring for some functionalities, which hopefully improve the maintainability of the codebase further. ## What's changed? - **[Imp]** CodeQA based on Qodana and ReSharper suggestions - **[New]** Introducing ``Hash`` extension for hashing ``FileStream``, ``string``, ``Span<T>`` and ``FileInfo`` with both synchronous and asynchronous version of the methods. The methods uses generic type of any member of ``HashAlgorithm`` (Such as: ``MD5``, ``SHA*``, ``HMACSHA*``, etc.) and ``NonCryptographicHashAlgorithm`` (Such as: ``Crc32``, ``XxHash*``, etc.) to specify which kind of hash to be used. - **[Imp]** Separating ``InternalAppJSONContext`` members into its own ``JsonContext``es. - **[Imp]** Update H.NotifyIcon to work with CsWin32 v0.3.162. - **[Imp]** Update NuGet packages. - **[Imp]** Recompile and Update StaticLibs for NativeAOT libraries link. - Update Rust compiler for ``csharp_bindings`` to rustc 1.84.0 (9fc6b4312 2025-01-07) (Stable) - Remove dependency to ``MSVCRT`` library (Manual VC++ 2019/2022 Redist installation should no longer be needed) and fully utilize ``UCRT`` as its runtime library. - Update libwebp libraries to: https://github.com/webmproject/libwebp/tree/f8f241071001e1a3807f34a26a03a742ea843458 - Favor speed + inline optimizaton build (/O2 + /Ob2 + /Oi + /Ot) - Recompile all static libraries using Microsoft Visual C++ 2022 (19.43.34618) compiler. - Optimize Rust library, ``csharp_bindings`` to use full LTO. - **[Imp]** Update Hi3Helper.Sophon submodule. ## PR Status : - Overall Status : Done - Commits : Done - Synced to base (Collapse:main) : Not yet - Build status : OK - Crashing : No - Bug found caused by PR : {int} ### Templates <details> <summary>Changelog Prefixes</summary> ``` **[New]** **[Imp]** **[Fix]** **[Loc]** **[Doc]** ``` </details>
2 parents 381e4a4 + c1a666e commit 1b2d110

File tree

328 files changed

+10305
-9693
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

328 files changed

+10305
-9693
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
packages/*
77

88
CollapseLauncher/Deps/*
9+
CollapseLauncher/StaticLib/*.lib
10+
CollapseLauncher/StaticLib/**/*.lib
911
CollapseLauncher/Invoker/*
1012
**/Generated Files/**
1113
*.psd

CollapseLauncher/App.xaml

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
44
xmlns:animatedvisuals="using:Microsoft.UI.Xaml.Controls.AnimatedVisuals"
55
xmlns:conv="using:CollapseLauncher.Pages"
6-
xmlns:interactions="using:Microsoft.Xaml.Interactions.Core"
76
xmlns:interactivity="using:Microsoft.Xaml.Interactivity"
87
xmlns:controls="using:Microsoft.UI.Xaml.Controls"
98
xmlns:primitives="using:Microsoft.UI.Xaml.Controls.Primitives"
@@ -2190,13 +2189,13 @@
21902189
<!-- Force ShouldConstrainToRootBounds to True -->
21912190
<!-- Fix https://github.com/microsoft/microsoft-ui-xaml/issues/8657 -->
21922191
<interactivity:Interaction.Behaviors>
2193-
<interactions:DataTriggerBehavior Binding="{Binding ShouldConstrainToRootBounds, ElementName=Popup}"
2192+
<interactivity:DataTriggerBehavior Binding="{Binding ShouldConstrainToRootBounds, ElementName=Popup}"
21942193
ComparisonCondition="Equal"
21952194
Value="False">
2196-
<interactions:ChangePropertyAction PropertyName="ShouldConstrainToRootBounds"
2195+
<interactivity:ChangePropertyAction PropertyName="ShouldConstrainToRootBounds"
21972196
TargetObject="{Binding ElementName=Popup}"
21982197
Value="True" />
2199-
</interactions:DataTriggerBehavior>
2198+
</interactivity:DataTriggerBehavior>
22002199
</interactivity:Interaction.Behaviors>
22012200
<Border x:Name="PopupBorder"
22022201
Margin="0,-0.5,0,-1"
@@ -2627,13 +2626,13 @@
26272626
<!-- Force ShouldConstrainToRootBounds to True -->
26282627
<!-- Fix https://github.com/microsoft/microsoft-ui-xaml/issues/8657 -->
26292628
<interactivity:Interaction.Behaviors>
2630-
<interactions:DataTriggerBehavior Binding="{Binding ShouldConstrainToRootBounds, ElementName=Popup}"
2629+
<interactivity:DataTriggerBehavior Binding="{Binding ShouldConstrainToRootBounds, ElementName=Popup}"
26312630
ComparisonCondition="Equal"
26322631
Value="False">
2633-
<interactions:ChangePropertyAction PropertyName="ShouldConstrainToRootBounds"
2632+
<interactivity:ChangePropertyAction PropertyName="ShouldConstrainToRootBounds"
26342633
TargetObject="{Binding ElementName=Popup}"
26352634
Value="True" />
2636-
</interactions:DataTriggerBehavior>
2635+
</interactivity:DataTriggerBehavior>
26372636
</interactivity:Interaction.Behaviors>
26382637
<Border x:Name="PopupBorder"
26392638
Margin="0,-0.5,0,-1"

CollapseLauncher/App.xaml.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,23 @@
1414
using Windows.UI;
1515
using static CollapseLauncher.InnerLauncherConfig;
1616
using static Hi3Helper.Logger;
17+
// ReSharper disable SwitchStatementMissingSomeEnumCasesNoDefault
18+
// ReSharper disable CommentTypo
19+
// ReSharper disable StringLiteralTypo
1720

1821
namespace CollapseLauncher
1922
{
2023
public partial class App
2124
{
22-
public static bool IsAppKilled = false;
25+
public static bool IsAppKilled { get; set; } = false;
2326

2427
public App()
2528
{
2629
if (DebugSettings != null)
2730
{
28-
#if ENABLEFRAMECOUNTER
31+
#if ENABLEFRAMECOUNTER
2932
DebugSettings.EnableFrameRateCounter = true;
30-
#endif
33+
#endif
3134
#if DEBUG
3235
DebugSettings.LayoutCycleDebugBreakLevel = LayoutCycleDebugBreakLevel.High;
3336
DebugSettings.LayoutCycleTracingLevel = LayoutCycleTracingLevel.High;
@@ -68,7 +71,7 @@ public App()
6871
RequestedTheme = IsAppThemeLight ? ApplicationTheme.Light : ApplicationTheme.Dark;
6972
PInvoke.SetPreferredAppMode(PInvoke.ShouldAppsUseDarkMode() ? PreferredAppMode.AllowDark : PreferredAppMode.Default);
7073

71-
this.InitializeComponent();
74+
InitializeComponent();
7275
}
7376

7477
protected override void OnLaunched(LaunchActivatedEventArgs args)

CollapseLauncher/Classes/CachesManagement/Honkai/Check.cs

Lines changed: 60 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using CollapseLauncher.Interfaces;
33
using Hi3Helper;
44
using System;
5+
using System.Buffers;
56
using System.Collections.Generic;
67
using System.IO;
78
using System.Linq;
@@ -10,6 +11,7 @@
1011
using System.Threading.Tasks;
1112
using static Hi3Helper.Locale;
1213
using static Hi3Helper.Logger;
14+
// ReSharper disable CommentTypo
1315

1416
namespace CollapseLauncher
1517
{
@@ -18,20 +20,20 @@ internal partial class HonkaiCache
1820
private async Task<List<CacheAsset>> Check(List<CacheAsset> assetIndex, CancellationToken token)
1921
{
2022
// Initialize asset index for the return
21-
List<CacheAsset> returnAsset = new List<CacheAsset>();
23+
List<CacheAsset> returnAsset = [];
2224

2325
// Set Indetermined status as false
24-
_status.IsProgressAllIndetermined = false;
26+
Status.IsProgressAllIndetermined = false;
2527

2628
// Show the asset entry panel
27-
_status.IsAssetEntryPanelShow = true;
29+
Status.IsAssetEntryPanelShow = true;
2830

2931
try
3032
{
3133
// Create the cache directory if it doesn't exist
32-
if (!Directory.Exists(_gamePath))
34+
if (!Directory.Exists(GamePath))
3335
{
34-
Directory.CreateDirectory(_gamePath!);
36+
Directory.CreateDirectory(GamePath!);
3537
}
3638

3739
// Check for unused files
@@ -40,7 +42,7 @@ private async Task<List<CacheAsset>> Check(List<CacheAsset> assetIndex, Cancella
4042
// Do check in parallelization.
4143
await Parallel.ForEachAsync(assetIndex!, new ParallelOptions
4244
{
43-
MaxDegreeOfParallelism = _threadCount,
45+
MaxDegreeOfParallelism = ThreadCount,
4446
CancellationToken = token
4547
}, async (asset, localToken) =>
4648
{
@@ -56,10 +58,19 @@ private async Task<List<CacheAsset>> Check(List<CacheAsset> assetIndex, Cancella
5658
return returnAsset;
5759
}
5860

61+
private readonly SearchValues<string> _unusedSearchValues = SearchValues.Create([
62+
"output_log",
63+
"Crashes",
64+
"Verify.txt",
65+
"APM",
66+
"FBData",
67+
"asb.dat"
68+
], StringComparison.OrdinalIgnoreCase);
69+
5970
private void CheckUnusedAssets(List<CacheAsset> assetIndex, List<CacheAsset> returnAsset)
6071
{
6172
// Directory info and if the directory doesn't exist, return
62-
DirectoryInfo directoryInfo = new DirectoryInfo(_gamePath);
73+
DirectoryInfo directoryInfo = new DirectoryInfo(GamePath);
6374
if (!directoryInfo.Exists)
6475
{
6576
return;
@@ -69,54 +80,51 @@ private void CheckUnusedAssets(List<CacheAsset> assetIndex, List<CacheAsset> ret
6980
foreach (FileInfo fileInfo in directoryInfo.EnumerateFiles("*", SearchOption.AllDirectories)
7081
.EnumerateNoReadOnly())
7182
{
72-
string filePath = fileInfo.FullName;
73-
74-
if (!filePath.Contains("output_log", StringComparison.OrdinalIgnoreCase)
75-
&& !filePath.Contains("Crashes", StringComparison.OrdinalIgnoreCase)
76-
&& !filePath.Contains("Verify.txt", StringComparison.OrdinalIgnoreCase)
77-
&& !filePath.Contains("APM", StringComparison.OrdinalIgnoreCase)
78-
&& !filePath.Contains("FBData", StringComparison.OrdinalIgnoreCase)
79-
&& !filePath.Contains("asb.dat", StringComparison.OrdinalIgnoreCase)
80-
&& !assetIndex.Exists(x => x.ConcatPath == fileInfo.FullName))
83+
ReadOnlySpan<char> filePath = fileInfo.FullName;
84+
85+
if (filePath.ContainsAny(_unusedSearchValues)
86+
|| assetIndex.Exists(x => x.ConcatPath == fileInfo.FullName))
8187
{
82-
// Increment the total found count
83-
_progressAllCountFound++;
84-
85-
// Add asset to the returnAsset
86-
CacheAsset asset = new CacheAsset()
87-
{
88-
BasePath = Path.GetDirectoryName(filePath),
89-
N = Path.GetFileName(filePath),
90-
DataType = CacheAssetType.Unused,
91-
CS = fileInfo.Length,
92-
CRC = null,
93-
Status = CacheAssetStatus.Unused,
94-
IsUseLocalPath = true
95-
};
96-
returnAsset!.Add(asset);
97-
98-
// Add to asset entry display
99-
Dispatch(() => AssetEntry!.Add(new AssetProperty<CacheAssetType>(
100-
asset.N,
101-
asset.DataType,
102-
asset.BasePath,
103-
asset.CS,
104-
null,
105-
null
106-
))
107-
);
108-
109-
LogWriteLine($"File: {asset.ConcatPath} is unused!", LogType.Warning, true);
88+
continue;
11089
}
90+
91+
// Increment the total found count
92+
ProgressAllCountFound++;
93+
94+
// Add asset to the returnAsset
95+
CacheAsset asset = new CacheAsset
96+
{
97+
BasePath = Path.GetDirectoryName(fileInfo.FullName),
98+
N = Path.GetFileName(fileInfo.FullName),
99+
DataType = CacheAssetType.Unused,
100+
CS = fileInfo.Length,
101+
CRC = null,
102+
Status = CacheAssetStatus.Unused,
103+
IsUseLocalPath = true
104+
};
105+
returnAsset!.Add(asset);
106+
107+
// Add to asset entry display
108+
Dispatch(() => AssetEntry!.Add(new AssetProperty<CacheAssetType>(
109+
asset.N,
110+
asset.DataType,
111+
asset.BasePath,
112+
asset.CS,
113+
null,
114+
null
115+
))
116+
);
117+
118+
LogWriteLine($"File: {asset.ConcatPath} is unused!", LogType.Warning, true);
111119
}
112120
}
113121

114122
private async ValueTask CheckAsset(CacheAsset asset, List<CacheAsset> returnAsset, CancellationToken token)
115123
{
116124
// Increment the count and update the status
117-
Interlocked.Add(ref _progressAllCountCurrent, 1);
118-
_status.ActivityStatus = string.Format(Lang!._CachesPage!.CachesStatusChecking!, asset!.DataType, asset.N);
119-
_status.ActivityAll = string.Format(Lang._CachesPage.CachesTotalStatusChecking!, _progressAllCountCurrent, _progressAllCountTotal);
125+
Interlocked.Add(ref ProgressAllCountCurrent, 1);
126+
Status.ActivityStatus = string.Format(Lang!._CachesPage!.CachesStatusChecking!, asset!.DataType, asset.N);
127+
Status.ActivityAll = string.Format(Lang._CachesPage.CachesTotalStatusChecking!, ProgressAllCountCurrent, ProgressAllCountTotal);
120128

121129
// Assign the file info.
122130
FileInfo fileInfo = new FileInfo(asset.ConcatPath).EnsureNoReadOnly(out bool isExist);
@@ -136,15 +144,15 @@ private async ValueTask CheckAsset(CacheAsset asset, List<CacheAsset> returnAsse
136144
}
137145

138146
// Skip CRC check if fast method is used
139-
if (_useFastMethod)
147+
if (UseFastMethod)
140148
{
141149
return;
142150
}
143151

144152
// If above passes, then run the CRC check
145153
await using FileStream fs = await NaivelyOpenFileStreamAsync(fileInfo, FileMode.Open, FileAccess.Read, FileShare.Read);
146154
// Calculate the asset CRC (SHA1)
147-
byte[] hashArray = await GetCryptoHashAsync<HMACSHA1>(fs, _gameSalt, true, true, token);
155+
byte[] hashArray = await GetCryptoHashAsync<HMACSHA1>(fs, GameSalt, true, true, token);
148156

149157
// If the asset CRC doesn't match, then add the file to asset index.
150158
if (!IsArrayMatch(asset.CRCArray, hashArray))
@@ -159,9 +167,9 @@ private void AddGenericCheckAsset(CacheAsset asset, CacheAssetStatus assetStatus
159167
lock (this)
160168
{
161169
// Set Indetermined status as false
162-
_status.IsProgressAllIndetermined = false;
163-
_progressAllCountFound++;
164-
_progressAllSizeFound += asset!.CS;
170+
Status.IsProgressAllIndetermined = false;
171+
ProgressAllCountFound++;
172+
ProgressAllSizeFound += asset!.CS;
165173
}
166174

167175
// Add file into asset index

0 commit comments

Comments
 (0)