Skip to content

Commit

Permalink
fix calculating error of play time & show game process when game is r…
Browse files Browse the repository at this point in the history
…unning
  • Loading branch information
Scighost committed Jan 7, 2025
1 parent 9a29738 commit b8a1c1a
Show file tree
Hide file tree
Showing 21 changed files with 309 additions and 28 deletions.
1 change: 1 addition & 0 deletions src/Starward/Features/GameLauncher/GameLauncherPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
<local:StartGameButton HorizontalAlignment="Right"
CanExecute="{x:Bind StartGameButtonCanExecute}"
Command="{x:Bind ClickStartGameButtonCommand}"
RunningGameInfo="{x:Bind RunningGameInfo}"
SettingCommand="{x:Bind OpenGameLauncherSettingDialogCommand}"
State="{x:Bind GameState}" />

Expand Down
71 changes: 69 additions & 2 deletions src/Starward/Features/GameLauncher/GameLauncherPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
using Starward.Frameworks;
using Starward.Helpers;
using System;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Timers;
using Windows.ApplicationModel.DataTransfer;
using Windows.Storage;

Expand Down Expand Up @@ -125,6 +127,7 @@ private async void CheckGameVersion()
{
try
{
StartGameButtonCanExecute = true;
GameState = GameState.Waiting;
GameInstallPath = _gameLauncherService.GetGameInstallPath(CurrentGameId, out bool storageRemoved);
IsInstallPathRemovableTipEnabled = storageRemoved;
Expand All @@ -144,10 +147,15 @@ private async void CheckGameVersion()
GameState = GameState.ResumeDownload;
return;
}
if (await CheckGameRunningAsync())
{
return;
}
latestGameVersion = await _gamePackageService.GetLatestGameVersionAsync(CurrentGameId);
if (latestGameVersion > localGameVersion)
{
GameState = GameState.UpdateGame;
return;
}
}
catch (Exception ex)
Expand Down Expand Up @@ -248,20 +256,78 @@ private void OnRemovableStorageDeviceChanged(object _, RemovableStorageDeviceCha



private Timer processTimer;


private async void UpdateGameState()
[ObservableProperty]
private partial Process? GameProcess { get; set; }
partial void OnGameProcessChanged(Process? oldValue, Process? newValue)
{
try
oldValue?.Dispose();
processTimer?.Stop();
if (processTimer is null)
{
processTimer = new(1000);
processTimer.Elapsed += (_, _) => CheckGameExited();
}
if (newValue != null)
{
processTimer?.Start();
RunningGameInfo = $"{newValue.ProcessName}.exe ({newValue.Id})";
}
else
{
RunningGameInfo = null;
_logger.LogInformation("Game process exited");
}
}



public string? RunningGameInfo { get; set => SetProperty(ref field, value); }





private async Task<bool> CheckGameRunningAsync()
{
try
{
GameProcess = await _gameLauncherService.GetGameProcessAsync(CurrentGameId);
if (GameProcess != null)
{
StartGameButtonCanExecute = false;
GameState = GameState.GameIsRunning;
_logger.LogInformation("Game is running ({name}, {pid})", GameProcess.ProcessName, GameProcess.Id);
return true;
}
}
catch { }
return false;
}




private void CheckGameExited()
{
try
{
if (GameProcess != null)
{
if (GameProcess.HasExited)
{
DispatcherQueue.TryEnqueue(CheckGameVersion);
GameProcess = null;
}
}
}
catch { }
}




[RelayCommand]
private async Task StartGameAsync()
Expand All @@ -276,6 +342,7 @@ private async Task StartGameAsync()
else
{
GameState = GameState.GameIsRunning;
GameProcess = process1;
}
}
catch (Exception ex)
Expand Down
9 changes: 4 additions & 5 deletions src/Starward/Features/GameLauncher/StartGameButton.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
ContentTransitions="{TemplateBinding ContentTransitions}"
CornerRadius="{TemplateBinding CornerRadius}"
Foreground="{TemplateBinding Foreground}">

<ContentPresenter.BackgroundTransition>
<BrushTransition Duration="0:0:0.083" />
</ContentPresenter.BackgroundTransition>
Expand Down Expand Up @@ -174,6 +173,7 @@
Height="20"
Margin="12,0,0,0"
HorizontalAlignment="Left"
IsHitTestVisible="False"
Visibility="{x:Bind ProgressRing_IndeterminateLoading_Visibility}" />

<!-- 游戏设置 -->
Expand Down Expand Up @@ -203,16 +203,15 @@

<!-- 下载进度 -->
<FlyoutBase.AttachedFlyout>
<Flyout x:Name="Flyout_DownloadProgress"
AreOpenCloseAnimationsEnabled="False"
OverlayInputPassThroughElement="{x:Bind Grid_Root}"
Placement="Top">
<Flyout x:Name="Flyout_DownloadProgress" AreOpenCloseAnimationsEnabled="False">
<Flyout.FlyoutPresenterStyle>
<Style BasedOn="{StaticResource DefaultFlyoutPresenterStyle}" TargetType="FlyoutPresenter">
<Setter Property="BorderThickness" Value="0" />
<Setter Property="Background" Value="{ThemeResource CustomAcrylicBrush}" />
</Style>
</Flyout.FlyoutPresenterStyle>
<Grid>
<TextBlock Foreground="{ThemeResource TextFillColorSecondaryBrush}" Text="{x:Bind RunningGameInfo}" />
<!-- todo 下载进度 -->
</Grid>
</Flyout>
Expand Down
12 changes: 10 additions & 2 deletions src/Starward/Features/GameLauncher/StartGameButton.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ public bool CanExecute



public string? RunningGameInfo { get; set => SetProperty(ref field, value); }




public bool TextBlock_StartGame_Visibility => State is GameState.StartGame;
public bool TextBlock_GameIsRunning_Visibility => State is GameState.GameIsRunning;
Expand Down Expand Up @@ -132,9 +136,13 @@ private void UpdateButtonState()
private void Grid_Root_PointerEntered(object sender, PointerRoutedEventArgs e)
{
PointerOver = true;
if (State is GameState.Downloading)
if (State is GameState.GameIsRunning or GameState.Downloading)
{
FlyoutBase.ShowAttachedFlyout(Grid_Root);
Flyout_DownloadProgress.ShowAt(Grid_Root, new FlyoutShowOptions
{
Placement = FlyoutPlacementMode.Top,
ShowMode = FlyoutShowMode.Transient,
});
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Media;
using System;
using Windows.UI;

namespace Starward.Features.GameRecord;

internal class ColorToSolidColorBrushConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
return new SolidColorBrush((Color)value);
}

public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using Microsoft.UI.Xaml.Data;
using Starward.Core.GameRecord.Genshin.SpiralAbyss;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Starward.Features.GameRecord.Genshin;

public class SpiralAbyssBattleAvatarsSelectConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
if (value is SpiralAbyssLevel level)
{
if (parameter is "1")
{
if (level.Battles?.Count > 1)
{
return level.Battles[1].Avatars;
}
}
else
{
return level.Battles?.FirstOrDefault()?.Avatars!;
}
}
if (value is IList<SpiralAbyssBattle> battles)
{
if (parameter is "1")
{
if (battles.Count > 1)
{
return battles[1].Avatars;
}
}
else
{
return battles.FirstOrDefault()?.Avatars!;
}
}
return null!;
}

public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@
xmlns:local="using:Starward.Features.GameRecord.Genshin"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:sc="using:Starward.Controls"
xmlns:scv="using:Starward.Converters"
xmlns:sf="using:Starward.Frameworks"
xmlns:sp="using:Starward.Pages"
x:DefaultBindMode="OneWay"
mc:Ignorable="d">

<Page.Resources>
<BitmapImage x:Key="AbyssIcon" UriSource="ms-appx:///Assets/Image/UI_Icon_Tower_Star.png" />
<scv:SpiralAbyssBattleAvatarsSelectConverter x:Key="SpiralAbyssBattleAvatarsSelectConverter" />
<local:SpiralAbyssBattleAvatarsSelectConverter x:Key="SpiralAbyssBattleAvatarsSelectConverter" />
</Page.Resources>

<Grid>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:sc="using:Starward.Controls"
xmlns:sf="using:Starward.Frameworks"
xmlns:sfg="using:Starward.Features.GameRecord"
xmlns:sp="using:Starward.Pages"
x:DefaultBindMode="OneWay"
mc:Ignorable="d">

<Page.Resources>
<sfg:ColorToSolidColorBrushConverter x:Key="ColorToSolidColorBrushConverter" />
</Page.Resources>

<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Microsoft.UI;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Media;
using System;
using Windows.UI;

namespace Starward.Features.GameRecord.StarRail;

internal class SimulatedUniverseBuffBgConverter : IValueConverter
{


private static SolidColorBrush Rank1Brush = new SolidColorBrush(Color.FromArgb(0xFF, 0x68, 0x68, 0x70));
private static SolidColorBrush Rank2Brush = new SolidColorBrush(Color.FromArgb(0xFF, 0x53, 0x79, 0xB5));
private static SolidColorBrush Rank3Brush = new SolidColorBrush(Color.FromArgb(0xFF, 0xBC, 0x9B, 0x6E));
private static SolidColorBrush Rank0Brush = new SolidColorBrush(Colors.Transparent);

public object Convert(object value, Type targetType, object parameter, string language)
{
return (int)value switch
{
1 => Rank1Brush,
2 => Rank2Brush,
3 => Rank3Brush,
_ => Rank0Brush,
};
}

public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using Microsoft.UI.Xaml.Data;
using System;

namespace Starward.Features.GameRecord.StarRail;

internal class SimulatedUniverseBuffIconConverter : IValueConverter
{

private const string Icon_120 = "ms-appx:///Assets/Image/RogueInterveneKnight.png";
private const string Icon_121 = "ms-appx:///Assets/Image/RogueInterveneMemory.png";
private const string Icon_122 = "ms-appx:///Assets/Image/RogueInterveneWarlock.png";
private const string Icon_123 = "ms-appx:///Assets/Image/RogueIntervenePirest.png";
private const string Icon_124 = "ms-appx:///Assets/Image/RogueInterveneRogue.png";
private const string Icon_125 = "ms-appx:///Assets/Image/RogueInterveneWarrior.png";
private const string Icon_126 = "ms-appx:///Assets/Image/RogueInterveneJoy.png";
private const string Icon_127 = "ms-appx:///Assets/Image/RogueIntervenePropagation.png";
private const string Icon_128 = "ms-appx:///Assets/Image/RogueInterveneMage.png";
private const string TransparentBackground = "ms-appx:///Assets/Image/Transparent.png";

public object Convert(object value, Type targetType, object parameter, string language)
{
return (int)value switch
{
120 => Icon_120,
121 => Icon_121,
122 => Icon_122,
123 => Icon_123,
124 => Icon_124,
125 => Icon_125,
126 => Icon_126,
127 => Icon_127,
128 => Icon_128,
_ => TransparentBackground,
};
}

public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,17 @@
xmlns:local="using:Starward.Features.GameRecord.StarRail"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:sc="using:Starward.Controls"
xmlns:scv="using:Starward.Converters"
xmlns:sf="using:Starward.Frameworks"
xmlns:sp="using:Starward.Pages"
xmlns:su="using:Starward.Core.GameRecord.StarRail.SimulatedUniverse"
x:DefaultBindMode="OneWay"
mc:Ignorable="d">

<Page.Resources>
<scv:SimulatedUniverseWorldIconConverter x:Key="SimulatedUniverseWorldIconConverter" />
<scv:SimulatedUniverseBuffIconConverter x:Key="SimulatedUniverseBuffIconConverter" />
<scv:SimulatedUniverseBuffBgConverter x:Key="SimulatedUniverseBuffBgConverter" />
<scv:SimulatedUniverseRomanNumberConverter x:Key="SimulatedUniverseRomanNumberConverter" />
<local:SimulatedUniverseWorldIconConverter x:Key="SimulatedUniverseWorldIconConverter" />
<local:SimulatedUniverseBuffIconConverter x:Key="SimulatedUniverseBuffIconConverter" />
<local:SimulatedUniverseBuffBgConverter x:Key="SimulatedUniverseBuffBgConverter" />
<local:SimulatedUniverseRomanNumberConverter x:Key="SimulatedUniverseRomanNumberConverter" />
</Page.Resources>

<Grid>
Expand Down
Loading

0 comments on commit b8a1c1a

Please sign in to comment.