Skip to content

Commit

Permalink
Merge pull request #2279 from DGP-Studio/develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Lightczx authored Dec 13, 2024
2 parents 641bdde + a0df313 commit 1f842c6
Show file tree
Hide file tree
Showing 162 changed files with 1,419 additions and 1,296 deletions.
2 changes: 1 addition & 1 deletion src/Snap.Hutao/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ dotnet_diagnostic.CA1501.severity = suggestion # CA1501: 避免过度继承
dotnet_diagnostic.CA1502.severity = suggestion # CA1502: 避免过度复杂性
dotnet_diagnostic.CA1505.severity = suggestion # CA1505: 避免使用无法维护的代码
dotnet_diagnostic.CA1506.severity = suggestion # CA1506: 避免过度的类耦合
dotnet_diagnostic.CA1508.severity = suggestion # CA1508: 避免死条件代码
dotnet_diagnostic.CA1508.severity = none # CA1508: 避免死条件代码
dotnet_diagnostic.CA1805.severity = suggestion # CA1805: 避免进行不必要的初始化
dotnet_diagnostic.CA1810.severity = suggestion # CA1810: 以内联方式初始化引用类型的静态字段
dotnet_diagnostic.CA1813.severity = suggestion # CA1813: 避免使用非密封特性
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Collections.Immutable;
using System.Runtime.CompilerServices;

namespace Snap.Hutao.Test.BaseClassLibrary;

[TestClass]
public class ImmutableCollectionTest
{
[TestMethod]
public void ImmutableArrayUnsafeRefWrite()
{
ImmutableArray<int> array = [1, 2, 3, 4, 5, 6, 7];
Unsafe.AsRef(in array.AsSpan()[3]) = 8;
Assert.AreEqual(8, array[3]);
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Runtime.CompilerServices;

namespace Snap.Hutao.Test.BaseClassLibrary;
Expand All @@ -13,9 +14,23 @@ public void UnsafeAccessorCanGetInterfaceProperty()
Assert.AreEqual(3, value);
}

[TestMethod]
public void BehaviorTest()
{
DateTimeOffset dto = new(2000, 2, 3, 4, 5, 6, TimeSpan.FromHours(7));
Assert.AreEqual(RefValueGetFieldRef(ref dto), 420);
Assert.ThrowsException<MissingFieldException>(() => RefValueGetFieldRefReadonly(ref dto));
}

[UnsafeAccessor(UnsafeAccessorKind.Method, Name = "get_TestProperty")]
private static extern int InternalGetInterfaceProperty(ITestInterface instance);

[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "_offsetMinutes")]
private static extern ref short RefValueGetFieldRef(ref DateTimeOffset dto);

[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "_offsetMinutes")]
private static extern ref readonly short RefValueGetFieldRefReadonly(ref DateTimeOffset dto);

internal interface ITestInterface
{
internal int TestProperty { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ public class UnlockerIslandFunctionOffsetTest
[TestMethod]
public void GenerateJson()
{
// FunctionOffsetMickeyWonderMethod:
// MickeyWonderMethod:
// public static byte[] AnonymousMethod43(int nType) -> jmp xxxxxxxx -> another xref to xxxxxxxx
// FunctionOffsetMickeyWonderMethodPartner:
// MickeyWonderMethodPartner:
// public static string get_unityVersion() -> jmp
// FunctionOffsetMickeyWonderMethodPartner2:
// MickeyWonderMethodPartner2:
// "4C 8B 05 ?? ?? ?? ?? 4C 89 F1 48 89 FA E8 ?? ?? ?? ?? 90 48 83 C4 28 5B 5F 5E 41 5E C3"
// FunctionOffsetSetFieldOfView:
// SetFieldOfView:
// Camera: public void set_fieldOfView(float value) -> jmp xxxxxxxx
// FunctionOffsetSetTargetFrameRate:
// SetTargetFrameRate:
// public static void set_targetFrameRate(int value) -> jmp xxxxxxxxx (to the end)
// FunctionOffsetSetEnableFogRendering:
// SetEnableFogRendering:
// public static void set_enableFogRendering(bool value) -> jmp xxxxxxxxx (to the end)
// FunctionOffsetOpenTeam: FunctionOffsetOpenTeamPageAccordingly: GOD HELP US
// OpenTeam: OpenTeamPageAccordingly: CheckCanEnter GOD HELP US
UnlockerIslandConfigurationWrapper wrapper = new()
{
Chinese = new()
Expand All @@ -39,6 +39,7 @@ public void GenerateJson()
SetEnableFogRendering = 0x013F6B00,
OpenTeam = 0x08699B90,
OpenTeamPageAccordingly = 0x08695AE0,
CheckCanEnter = 0x0724A030
},
Oversea = new()
{
Expand All @@ -50,6 +51,7 @@ public void GenerateJson()
SetEnableFogRendering = 0x013FBA20,
OpenTeam = 0x086A32A0,
OpenTeamPageAccordingly = 0x086A4C60,
CheckCanEnter = 0x0711ED60
},
};

Expand Down Expand Up @@ -80,5 +82,7 @@ private sealed class UnlockerIslandFunctionOffset
public required uint OpenTeam { get; set; }

public required uint OpenTeamPageAccordingly { get; set; }

public required uint CheckCanEnter { get; set; }
}
}
4 changes: 2 additions & 2 deletions src/Snap.Hutao/Snap.Hutao.Test/Snap.Hutao.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="9.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.6.3" />
<PackageReference Include="MSTest.TestFramework" Version="3.6.3" />
<PackageReference Include="MSTest.TestAdapter" Version="3.6.4" />
<PackageReference Include="MSTest.TestFramework" Version="3.6.4" />
<PackageReference Include="coverlet.collector" Version="6.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
3 changes: 2 additions & 1 deletion src/Snap.Hutao/Snap.Hutao/BannedSymbols.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.BannedApiAnalyzers/BannedApiAnalyzers.Help.md
M:System.Collections.Generic.List`1.ForEach(System.Action{`0})
M:Microsoft.UI.Xaml.Controls.ContentDialog.ShowAsync
M:Microsoft.UI.Xaml.Controls.ContentDialog.ShowAsync(Microsoft.UI.Xaml.Controls.ContentDialogPlacement)
M:Microsoft.UI.Xaml.Controls.ContentDialog.ShowAsync(Microsoft.UI.Xaml.Controls.ContentDialogPlacement)
P:Microsoft.UI.Xaml.UIElement.RasterizationScale
1 change: 1 addition & 0 deletions src/Snap.Hutao/Snap.Hutao/Bootstrap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ private static void InitializeApp(ApplicationInitializationCallbackParams param)
IServiceProvider serviceProvider = Ioc.Default;

_ = serviceProvider.GetRequiredService<ITaskContext>();
XamlApplicationLifetime.DispatcherQueueInitialized = true;
_ = serviceProvider.GetRequiredService<App>();
}
}
2 changes: 1 addition & 1 deletion src/Snap.Hutao/Snap.Hutao/Core/Caching/ImageCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ private static async ValueTask TryConvertToMonoChromeAndSaveFileAsync(string sou
{
using (IMemoryBufferReference reference = sourceBuffer.CreateReference())
{
byte value = theme is ElementTheme.Light ? (byte)0x00 : (byte)0xFF;
byte value = (byte)(theme is ElementTheme.Light ? 0x00 : 0xFF);
SyncConvertToMonoChrome(reference.As<IMemoryBufferByteAccess>(), value);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.

namespace Snap.Hutao.Core.ComponentModel;

internal sealed class AsyncDisposableObservableBox<TNotifyPropertyChanged, T> : IAsyncDisposableObservableBox<T>
where TNotifyPropertyChanged : INotifyPropertyChanged
where T : class?, IDisposable?
{
private readonly TNotifyPropertyChanged source;
private readonly Func<TNotifyPropertyChanged, T> valueFactory;
private readonly string propertyName;

private volatile CancellationTokenSource? cts;
private volatile TaskCompletionSource? tcs;
private bool isDisposed;

public AsyncDisposableObservableBox(T value, TNotifyPropertyChanged source, string propertyName, Func<TNotifyPropertyChanged, T> valueFactory)
{
Value = value;
this.source = source;
this.propertyName = propertyName;
this.valueFactory = valueFactory;
source.PropertyChanged += OnPropertyChanged;
}

public T Value { get; private set; }

public AsyncLock SyncRoot { get; } = new();

public async ValueTask DisposeAsync()
{
if (isDisposed)
{
return;
}

using (await SyncRoot.LockAsync().ConfigureAwait(false))
{
if (isDisposed)
{
return;
}

isDisposed = true;
source.PropertyChanged -= OnPropertyChanged;
if (cts is not null)
{
await cts.CancelAsync().ConfigureAwait(false);

if (tcs is not null)
{
await tcs.Task.ConfigureAwait(false);
}

cts.Dispose();
}

Value?.Dispose();
Value = default!;
}
}

private void OnPropertyChanged(object? sender, PropertyChangedEventArgs args)
{
_ = OnPropertyChangedAsync(args);
}

[SuppressMessage("", "SH003")]
private async Task OnPropertyChangedAsync(PropertyChangedEventArgs args)
{
if (cts is not null)
{
await cts.CancelAsync().ConfigureAwait(false);

if (tcs is not null)
{
await tcs.Task.ConfigureAwait(false);
}

// Must await currentTcs before dispose currentCts.
cts.Dispose();
}

// Capture current tcs reference.
TaskCompletionSource currentTcs = new();
tcs = currentTcs;
cts = new();

try
{
// Capture current cts reference.
CancellationToken token = cts.Token;
using (await SyncRoot.LockAsync().ConfigureAwait(false))
{
if (token.IsCancellationRequested)
{
return;
}

if (args.PropertyName != propertyName)
{
return;
}

Value?.Dispose();
Value = valueFactory(source);
}
}
finally
{
currentTcs.TrySetResult();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.

namespace Snap.Hutao.Core.ComponentModel;

internal interface IAsyncDisposableObservableBox<out T> : IAsyncDisposable
where T : class?, IDisposable?
{
T Value { get; }

AsyncLock SyncRoot { get; }
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,6 @@ internal sealed partial class DefaultClipboardSource : IClipboardProvider
return JsonSerializer.Deserialize<T>(json, options);
}

public bool SetText(string text)
{
try
{
DataPackage content = new() { RequestedOperation = DataPackageOperation.Copy };
content.SetText(text);
Clipboard.SetContent(content);
Clipboard.Flush();
return true;
}
catch
{
return false;
}
}

public async ValueTask<bool> SetTextAsync(string text)
{
try
Expand All @@ -63,23 +47,6 @@ public async ValueTask<bool> SetTextAsync(string text)
}
}

public bool SetBitmap(IRandomAccessStream stream)
{
try
{
RandomAccessStreamReference reference = RandomAccessStreamReference.CreateFromStream(stream);
DataPackage content = new() { RequestedOperation = DataPackageOperation.Copy };
content.SetBitmap(reference);
Clipboard.SetContent(content);
Clipboard.Flush();
return true;
}
catch
{
return false;
}
}

public async ValueTask<bool> SetBitmapAsync(IRandomAccessStream stream)
{
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ internal interface IClipboardProvider
ValueTask<T?> DeserializeFromJsonAsync<T>()
where T : class;

bool SetBitmap(IRandomAccessStream stream);

ValueTask<bool> SetBitmapAsync(IRandomAccessStream stream);

bool SetText(string text);

ValueTask<bool> SetTextAsync(string text);
}
Loading

0 comments on commit 1f842c6

Please sign in to comment.