Skip to content

Commit

Permalink
get gacha records by cloud game
Browse files Browse the repository at this point in the history
  • Loading branch information
Scighost committed May 24, 2024
1 parent 88d1860 commit e1448e5
Show file tree
Hide file tree
Showing 9 changed files with 257 additions and 3 deletions.
19 changes: 19 additions & 0 deletions src/Starward.Language/Lang.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/Starward.Language/Lang.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1398,4 +1398,10 @@ Do you accept the risk and continue to use it?</value>
<data name="HoyolabToolboxPage_BattleChronicle" xml:space="preserve">
<value>Battle Chronicle</value>
</data>
<data name="CloudGameGachaWindow_OpenTheGachaRecordsPageInGameAndThenClickTheButtonOnTheRight" xml:space="preserve">
<value>Open the gacha records page in game, and then click the button on the right.</value>
</data>
<data name="GachaLogPage_ViaCloudGame" xml:space="preserve">
<value>via Cloud Game</value>
</data>
</root>
6 changes: 6 additions & 0 deletions src/Starward.Language/Lang.zh-CN.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1398,4 +1398,10 @@
<data name="HoyolabToolboxPage_BattleChronicle" xml:space="preserve">
<value>战绩</value>
</data>
<data name="GachaLogPage_ViaCloudGame" xml:space="preserve">
<value>通过云游戏获取</value>
</data>
<data name="CloudGameGachaWindow_OpenTheGachaRecordsPageInGameAndThenClickTheButtonOnTheRight" xml:space="preserve">
<value>在游戏中打开抽卡记录页面,然后点击右边的按键。</value>
</data>
</root>
3 changes: 3 additions & 0 deletions src/Starward/Messages/UpdateGachaLogMessage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace Starward.Messages;

public record UpdateGachaLogMessage(string Url);
46 changes: 46 additions & 0 deletions src/Starward/MyWindows/CloudGameGachaWindow.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="utf-8" ?>
<local:WindowEx x:Class="Starward.MyWindows.CloudGameGachaWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:lang="using:Starward.Language"
xmlns:local="using:Starward.MyWindows"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<Grid x:Name="RootGrid"
Padding="0,32,0,0"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
Loaded="RootGrid_Loaded">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>

<TextBlock x:Name="TextBlock_Info"
Margin="12,0,12,0"
VerticalAlignment="Center"
Foreground="{ThemeResource TextFillColorSecondaryBrush}"
Text="{x:Bind lang:Lang.CloudGameGachaWindow_OpenTheGachaRecordsPageInGameAndThenClickTheButtonOnTheRight}"
TextWrapping="Wrap" />
<TextBlock x:Name="TextBlock_Error"
Margin="12,0,12,0"
VerticalAlignment="Center"
Foreground="{ThemeResource SystemFillColorCriticalBrush}"
TextWrapping="Wrap"
Visibility="Collapsed" />
<Button Grid.Column="1"
Click="Button_Click"
Content="获取抽卡记录" />

<WebView2 x:Name="webview"
Grid.Row="1"
Grid.ColumnSpan="2" />

</Grid>

</local:WindowEx>
139 changes: 139 additions & 0 deletions src/Starward/MyWindows/CloudGameGachaWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
using CommunityToolkit.Mvvm.Messaging;
using Microsoft.UI.Xaml;
using Starward.Core;
using Starward.Messages;
using System;
using System.Runtime.InteropServices;
using System.Text.Json.Nodes;
using System.Web;

// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.

namespace Starward.MyWindows;

/// <summary>
/// An empty window that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class CloudGameGachaWindow : WindowEx
{


private const string SPAN_WEB_PREFIX_YS_CN = "https://webstatic.mihoyo.com/hk4e/event/e20190909gacha-v3/index.html";

private const string SPAN_WEB_PREFIX_SR_CN = "https://webstatic.mihoyo.com/hkrpg/event/e20211215gacha-v2/index.html";



public GameBiz GameBiz { get; set; }




public CloudGameGachaWindow()
{
this.InitializeComponent();
AppWindow.TitleBar.ExtendsContentIntoTitleBar = true;
if (ShouldSystemUseDarkMode())
{
RootGrid.RequestedTheme = ElementTheme.Dark;
}
else
{
RootGrid.RequestedTheme = ElementTheme.Light;
}
AdaptTitleBarButtonColorToActuallTheme();
}



private void RootGrid_Loaded(object sender, RoutedEventArgs e)
{
try
{
// todo WinAppSDK 升级到 1.5 后,为云游戏的网页缓存设置单独的文件夹
if (GameBiz.ToGame() is GameBiz.GenshinImpact)
{
webview.Source = new Uri("https://ys.mihoyo.com/cloud/");
}
if (GameBiz.ToGame() is GameBiz.StarRail)
{
webview.Source = new Uri("https://sr.mihoyo.com/cloud/");
}
}
catch (Exception)
{

}

}





private async void Button_Click(object sender, RoutedEventArgs e)
{
try
{
await webview.EnsureCoreWebView2Async();
string result = await webview.CoreWebView2.ExecuteScriptAsync("document.documentElement.outerHTML");
string? html = JsonNode.Parse(result)?.ToString();
if (!string.IsNullOrWhiteSpace(html))
{
string? url = GetMatchUrl(GameBiz, html);
if (!string.IsNullOrWhiteSpace(url))
{
url = HttpUtility.HtmlDecode(url);
WeakReferenceMessenger.Default.Send(new UpdateGachaLogMessage(url));
TextBlock_Info.Visibility = Visibility.Visible;
TextBlock_Error.Visibility = Visibility.Collapsed;
return;
}
}
TextBlock_Info.Visibility = Visibility.Collapsed;
TextBlock_Error.Visibility = Visibility.Visible;
TextBlock_Error.Text = Lang.GachaLogPage_CannotFindURL;
}
catch (Exception ex)
{
TextBlock_Info.Visibility = Visibility.Collapsed;
TextBlock_Error.Visibility = Visibility.Visible;
TextBlock_Error.Text = ex.Message;
}
}




private static string? GetMatchUrl(GameBiz gameBiz, string html)
{
string? prefix = gameBiz.ToGame() switch
{
GameBiz.GenshinImpact => SPAN_WEB_PREFIX_YS_CN,
GameBiz.StarRail => SPAN_WEB_PREFIX_SR_CN,
_ => null
};
if (prefix is not null)
{
ReadOnlySpan<char> span = html.AsSpan();
int index = span.LastIndexOf(prefix);
if (index >= 0)
{
var length = span[index..].IndexOfAny("\"");
return new(span.Slice(index, length));
}
}
return null;
}





[return: MarshalAs(UnmanagedType.Bool)]
[LibraryImport("uxtheme.dll", EntryPoint = "#138", SetLastError = true)]
private static partial bool ShouldSystemUseDarkMode();


}
1 change: 1 addition & 0 deletions src/Starward/MyWindows/WindowEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public virtual void Show()
{
AppWindow.Show(true);
AppWindow.MoveInZOrderAtTop();
User32.ShowWindow(WindowHandle, ShowWindowCommand.SW_SHOWNORMAL);
User32.SetForegroundWindow(WindowHandle);
WeakReferenceMessenger.Default.Send(new WindowStateChangedMessage(false));
}
Expand Down
4 changes: 3 additions & 1 deletion src/Starward/Pages/GachaLogPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
xmlns:sc="using:Starward.Controls"
xmlns:sm="using:Starward.Models"
x:DefaultBindMode="OneWay"
Loaded="Page_Loaded"
mc:Ignorable="d">

<Grid>
Expand Down Expand Up @@ -56,6 +55,9 @@
Text="{x:Bind lang:Lang.GachaLogPage_UpdateAll}" />
<!-- 输入 URL -->
<MenuFlyoutItem Command="{x:Bind InputUrlCommand}" Text="{x:Bind lang:Lang.GachaLogPage_InputURL}" />
<MenuFlyoutItem x:Name="MenuFlyoutItem_CloudGame"
Command="{x:Bind OpenCloudGameWindowCommand}"
Text="{x:Bind lang:Lang.GachaLogPage_ViaCloudGame}" />
</MenuFlyout>
</SplitButton.Flyout>
</SplitButton>
Expand Down
36 changes: 34 additions & 2 deletions src/Starward/Pages/GachaLogPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using Microsoft.Extensions.Logging;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
Expand All @@ -13,6 +14,7 @@
using Starward.Core;
using Starward.Core.Gacha;
using Starward.Helpers;
using Starward.Messages;
using Starward.Models;
using Starward.Services;
using Starward.Services.Gacha;
Expand Down Expand Up @@ -50,7 +52,6 @@ public sealed partial class GachaLogPage : PageBase
public GachaLogPage()
{
this.InitializeComponent();

}


Expand All @@ -73,6 +74,10 @@ protected override void OnNavigatedTo(NavigationEventArgs e)
_gachaLogService = AppConfig.GetService<StarRailGachaService>();
Image_Emoji.Source = new BitmapImage(AppConfig.EmojiPom);
}
if (biz.IsGlobalServer())
{
MenuFlyoutItem_CloudGame.Visibility = Visibility.Collapsed;
}
}
}

Expand Down Expand Up @@ -166,15 +171,28 @@ partial void OnGachaLanguageChanged(string? value)
private int errorCount = 0;


private async void Page_Loaded(object sender, RoutedEventArgs e)

protected override async void OnLoaded()
{
await Task.Delay(16);
WeakReferenceMessenger.Default.Register<UpdateGachaLogMessage>(this, (s, m) =>
{
MainWindow.Current.Show();
_ = UpdateGachaLogInternalAsync(m.Url);
});
Initialize();
await UpdateWikiDataAsync();
}



protected override void OnUnloaded()
{
WeakReferenceMessenger.Default.UnregisterAll(this);
}



private void Initialize()
{
try
Expand Down Expand Up @@ -448,6 +466,20 @@ private async Task InputUrlAsync()
}




[RelayCommand]
private void OpenCloudGameWindow()
{
try
{
new CloudGameGachaWindow { GameBiz = CurrentGameBiz }.Activate();
}
catch { }
}



#endregion


Expand Down

0 comments on commit e1448e5

Please sign in to comment.