Skip to content

Commit

Permalink
Merge branch 'main' into win32-migration
Browse files Browse the repository at this point in the history
  • Loading branch information
neon-nyan committed Nov 21, 2024
2 parents 3ed8c81 + 0be8751 commit 9d63032
Show file tree
Hide file tree
Showing 30 changed files with 406 additions and 297 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using System.Timers;
using static Hi3Helper.Logger;
using static CollapseLauncher.Dialogs.SimpleDialogs;
Expand Down Expand Up @@ -49,9 +50,9 @@ public Playtime(IGameVersionCheck gameVersionManager, IGameSettings gameSettings
}
#nullable disable

public async void CheckDb()
public async Task CheckDb(bool redirectThrow = false)
{
var needUpdate = await _playtime.DbSync();
var needUpdate = await _playtime.DbSync(redirectThrow);
if (needUpdate is not { IsUpdated: true, PlaytimeData: not null }) return;

_playtime = needUpdate.PlaytimeData;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ public void AddMinute()
/// Sync from/to DB at init
/// </summary>
/// <returns>true if require refresh, false if dont.</returns>
public async ValueTask<(bool IsUpdated, CollapsePlaytime? PlaytimeData)> DbSync()
public async ValueTask<(bool IsUpdated, CollapsePlaytime? PlaytimeData)> DbSync(bool redirectThrow = false)
{
LogWriteLine("[CollapsePlaytime::DbSync] Starting sync operation...", LogType.Default, true);
try
Expand Down Expand Up @@ -354,6 +354,11 @@ public void AddMinute()
await SentryHelper.ExceptionHandlerAsync(ex, SentryHelper.ExceptionType.UnhandledOther);
LogWriteLine($"[CollapsePlaytime::DbSync] Failed when trying to do sync operation\r\n{ex}",
LogType.Error, true);
if (redirectThrow)
{
throw;
}

return (false, null);
}
}
Expand Down
6 changes: 6 additions & 0 deletions CollapseLauncher/Classes/Helper/Database/DBHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,12 @@ public static async Task Init(bool redirectThrow = false, bool bypassEnableFlag
}
else LogWriteLine("[DbHandler::Init] Reinitializing database system...");
}
catch (DllNotFoundException e)
{
LogWriteLine("[DbHandler::Init] Error when connecting to database system! Probably missing Visual C/C++ redist!\r\n" + e,
LogType.Error, true);
if (redirectThrow) throw;
}
// No need to handle all these error catcher with sentry
// The error should be handled in the method caller instead
catch (LibsqlException e) when (e.Message.Contains("`api error: `{\"error\":\"Unauthorized: `The JWT is invalid`\"}``",
Expand Down
3 changes: 2 additions & 1 deletion CollapseLauncher/Classes/Interfaces/IGamePlaytime.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using CollapseLauncher.GamePlaytime;
using System;
using System.Diagnostics;
using System.Threading.Tasks;

namespace CollapseLauncher.Interfaces
{
Expand All @@ -12,6 +13,6 @@ internal interface IGamePlaytime : IDisposable
void Reset();
void Update(TimeSpan timeSpan, bool forceUpdateDb = false);
void StartSession(Process proc, DateTime? begin = null);
void CheckDb();
Task CheckDb(bool redirectThrow = false);
}
}
16 changes: 10 additions & 6 deletions CollapseLauncher/Classes/RepairManagement/Genshin/Check.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,10 @@ private async ValueTask CheckAssetAllType(PkgVersionProperties asset, List<PkgVe
bool UsePersistent = (asset.isForceStoreInPersistent && !asset.isForceStoreInStreaming && fileInfoPersistent != null && !fileInfoPersistent.Exists) || asset.isPatch || (!fileInfoStreaming.Exists && !asset.isForceStoreInStreaming);
bool IsPersistentExist = fileInfoPersistent != null && fileInfoPersistent.Exists && fileInfoPersistent.Length == asset.fileSize;
bool IsStreamingExist = fileInfoStreaming.Exists && fileInfoStreaming.Length == asset.fileSize;

// Update the local path to full persistent or streaming path and add asset for missing/unmatched size file
asset.remoteName = UsePersistent ? asset.remoteNamePersistent : asset.remoteName;
if (asset.remoteNamePersistent != null)
asset.remoteName = UsePersistent ? asset.remoteNamePersistent : asset.remoteName;

// Check if the file exist on both persistent and streaming path, then mark the
// streaming path as redundant (unused)
Expand All @@ -184,6 +185,10 @@ private async ValueTask CheckAssetAllType(PkgVersionProperties asset, List<PkgVe
LogWriteLine($"File [T: {asset.type}]: {fileInfoStreaming.FullName} is redundant (exist both in persistent and streaming)", LogType.Warning, true);
}

// Prevent assigning remoteNamePersistent when its null
string repairFile = !string.IsNullOrEmpty(asset.remoteNamePersistent)
&& UsePersistent ? asset.remoteNamePersistent : asset.remoteName;

// Check if the persistent or streaming file doesn't exist
if ((UsePersistent && !IsPersistentExist) || (!IsStreamingExist && !IsPersistentExist))
{
Expand All @@ -199,18 +204,17 @@ private async ValueTask CheckAssetAllType(PkgVersionProperties asset, List<PkgVe

Dispatch(() => AssetEntry.Add(
new AssetProperty<RepairAssetType>(
Path.GetFileName(UsePersistent ? asset.remoteNamePersistent : asset.remoteName),
Path.GetFileName(repairFile),
RepairAssetType.Generic,
Path.GetDirectoryName(UsePersistent ? asset.remoteNamePersistent : asset.remoteName),
Path.GetDirectoryName(repairFile),
asset.fileSize,
null,
HexTool.HexToBytesUnsafe(asset.md5)
)
));
targetAssetIndex.Add(asset);

string remoteName = UsePersistent ? asset.remoteNamePersistent : asset.remoteName;
LogWriteLine($"File [T: {RepairAssetType.Generic}]: {(string.IsNullOrEmpty(remoteName) ? asset.localName : remoteName)} is not found or has unmatched size", LogType.Warning, true);
LogWriteLine($"File [T: {RepairAssetType.Generic}]: {(string.IsNullOrEmpty(repairFile) ? asset.localName : repairFile)} is not found or has unmatched size", LogType.Warning, true);
return;
}

Expand Down
22 changes: 11 additions & 11 deletions CollapseLauncher/CollapseLauncher.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -150,16 +150,16 @@
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="9.0.0" />
<!-- Velopacks' outdated dependencies, remove this once they switched to .NET 9 -->
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.0" />
<PackageReference Include="Velopack" Version="0.0.869" Condition="$(DefineConstants.Contains('USEVELOPACK'))" />
<PackageReference Include="Velopack" Version="0.0.942" Condition="$(DefineConstants.Contains('USEVELOPACK'))" />

<PackageReference Include="CommunityToolkit.Common" Version="8.4.0-preview1" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.0-preview1" />
<PackageReference Include="CommunityToolkit.WinUI.Behaviors" Version="8.1.240916" />
<PackageReference Include="CommunityToolkit.WinUI.Controls.Primitives" Version="8.1.240916" />
<PackageReference Include="CommunityToolkit.WinUI.Media" Version="8.1.240916" />
<PackageReference Include="CommunityToolkit.WinUI.Converters" Version="8.1.240916" />
<PackageReference Include="CommunityToolkit.WinUI.Extensions" Version="8.1.240916" />
<PackageReference Include="CommunityToolkit.WinUI.Controls.Sizers" Version="8.1.240916" />
<PackageReference Include="CommunityToolkit.Common" Version="8.4.0-preview2" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.0-preview2" />
<PackageReference Include="CommunityToolkit.WinUI.Behaviors" Version="8.2.241112-preview1" />
<PackageReference Include="CommunityToolkit.WinUI.Controls.Primitives" Version="8.2.241112-preview1" />
<PackageReference Include="CommunityToolkit.WinUI.Media" Version="8.2.241112-preview1" />
<PackageReference Include="CommunityToolkit.WinUI.Converters" Version="8.2.241112-preview1" />
<PackageReference Include="CommunityToolkit.WinUI.Extensions" Version="8.2.241112-preview1" />
<PackageReference Include="CommunityToolkit.WinUI.Controls.Sizers" Version="8.2.241112-preview1" />

<!--
Only include FFmpegInteropX NuGet if USEFFMPEGFORVIDEOBG is defined in constants.
Expand All @@ -172,10 +172,10 @@
<PackageReference Include="Markdig.Signed" Version="0.38.0" />
<PackageReference Include="Microsoft.Graphics.Win2D" Version="1.3.0" />
<PackageReference Include="Microsoft.NETCore.Targets" Version="6.0.0-preview.4.21253.7" />
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.2895-prerelease" />
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.2950-prerelease" />
<PackageReference Include="Microsoft.Windows.CsWinRT" Version="2.2.0" />
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.26100.1742" />
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.6.240923002" />
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.6.241114003" />
<PackageReference Include="Microsoft.Xaml.Behaviors.WinUI.Managed" Version="2.0.9" />
<PackageReference Include="PhotoSauce.MagicScaler" Version="0.14.2" />
<PackageReference Include="PhotoSauce.NativeCodecs.Libwebp" Version="*-*" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,19 @@ https://go.microsoft.com/fwlink/?LinkID=208121.

<!-- Runtime Patch -->
<TargetLatestRuntimePatch>true</TargetLatestRuntimePatch>

<!-- Configure Sentry org and project -->
<SentryOrg>collapse</SentryOrg>
<SentryProject>collapse-launcher</SentryProject>
<!-- Automatically creates a release when building your application. -->
<SentryCreateRelease>true</SentryCreateRelease>
<!-- Automatically associates commits with the release. -->
<SentrySetCommits>true</SentrySetCommits>
<!-- Optionally provide explicit flags to the set-commits command -->
<SentrySetCommitOptions>--local</SentrySetCommitOptions>
<!-- Sends symbols to Sentry, enabling symbolication of stack traces. -->
<SentryUploadSymbols>false</SentryUploadSymbols>
<!-- Sends sources to Sentry, enabling display of source context. -->
<SentryUploadSources>true</SentryUploadSources>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,19 @@ https://go.microsoft.com/fwlink/?LinkID=208121.

<!-- Runtime Patch -->
<TargetLatestRuntimePatch>true</TargetLatestRuntimePatch>

<!-- Configure Sentry org and project -->
<SentryOrg>collapse</SentryOrg>
<SentryProject>collapse-launcher</SentryProject>
<!-- Automatically creates a release when building your application. -->
<SentryCreateRelease>true</SentryCreateRelease>
<!-- Automatically associates commits with the release. -->
<SentrySetCommits>true</SentrySetCommits>
<!-- Optionally provide explicit flags to the set-commits command -->
<SentrySetCommitOptions>--local</SentrySetCommitOptions>
<!-- Sends symbols to Sentry, enabling symbolication of stack traces. -->
<SentryUploadSymbols>false</SentryUploadSymbols>
<!-- Sends sources to Sentry, enabling display of source context. -->
<SentryUploadSources>true</SentryUploadSources>
</PropertyGroup>
</Project>
2 changes: 2 additions & 0 deletions CollapseLauncher/XAMLs/MainApp/Pages/HomePage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -2468,12 +2468,14 @@
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<FontIcon Grid.Column="1"
x:Name="SyncDbPlaytimeBtnGlyph"
HorizontalAlignment="Right"
VerticalAlignment="Center"
FontFamily="{ThemeResource FontAwesomeSolid}"
FontSize="12"
Glyph="" />
<TextBlock Grid.Column="0"
x:Name="SyncDbPlaytimeBtnText"
HorizontalAlignment="Left"
VerticalAlignment="Center"
FontWeight="SemiBold"
Expand Down
26 changes: 24 additions & 2 deletions CollapseLauncher/XAMLs/MainApp/Pages/HomePage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2417,9 +2417,31 @@ private async void ResetPlaytimeButton_Click(object sender, RoutedEventArgs e)
PlaytimeFlyout.Hide();
}

private void SyncDbPlaytimeButton_Click(object sender, RoutedEventArgs e)
private async void SyncDbPlaytimeButton_Click(object sender, RoutedEventArgs e)
{
CurrentGameProperty._GamePlaytime.CheckDb();
try
{
SyncDbPlaytimeBtnGlyph.Glyph = "\uf110"; // Loading
SyncDbPlaytimeBtnText.Text = Lang._HomePage.GamePlaytime_Idle_SyncDbSyncing;
await CurrentGameProperty._GamePlaytime.CheckDb(true);

await Task.Delay(500);

SyncDbPlaytimeBtnGlyph.Glyph = "\uf00c"; // Completed (check)
SyncDbPlaytimeBtnText.Text = Lang._Misc.Completed + "!";
await Task.Delay(1000);

SyncDbPlaytimeBtnGlyph.Glyph = "\uf021"; // Default
SyncDbPlaytimeBtnText.Text = Lang._HomePage.GamePlaytime_Idle_SyncDb;
}
catch (Exception ex)
{
LogWriteLine($"Failed when trying to sync playtime to database!\r\n{ex}", LogType.Error, true);
ErrorSender.SendException(ex);

SyncDbPlaytimeBtnGlyph.Glyph = "\uf021"; // Default
SyncDbPlaytimeBtnText.Text = Lang._HomePage.GamePlaytime_Idle_SyncDb;
}
}

private void NumberValidationTextBox(TextBox sender, TextBoxBeforeTextChangingEventArgs args)
Expand Down
42 changes: 37 additions & 5 deletions CollapseLauncher/XAMLs/MainApp/Pages/SettingsPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#if !DISABLEDISCORD
using CollapseLauncher.DiscordPresence;
#endif
using CollapseLauncher.CustomControls;
using CollapseLauncher.Dialogs;
using CollapseLauncher.Extension;
using CollapseLauncher.Helper;
Expand Down Expand Up @@ -1493,18 +1494,20 @@ private async void ValidateAndSaveDbButton_Click(object sender, RoutedEventArgs
{
// Show checking bar status
ShowChecking();

// Set the value from prop
DbHandler.Uri = _dbUrl;
DbHandler.Token = _dbToken;
DbHandler.UserId = _dbUserId;

var r = Random.Shared.Next(100); // Generate random int for data verification

await DbHandler.Init(true, true); // Initialize database
await DbHandler.StoreKeyValue("TestKey", r.ToString(), true); // Store random number in TestKey
if (Convert.ToInt32(await DbHandler.QueryKey("TestKey", true)) != r) // Query key and check if value is correct
throw new InvalidDataException("Data validation failed!"); // Throw if value does not match (then catch), unlikely but maybe for really unstable db server
if (Convert.ToInt32(await DbHandler.QueryKey("TestKey", true)) !=
r) // Query key and check if value is correct
throw
new InvalidDataException("Data validation failed!"); // Throw if value does not match (then catch), unlikely but maybe for really unstable db server

// Show success bar status
ShowSuccess();
Expand All @@ -1516,9 +1519,38 @@ await SpawnDialog(
null,
null,
ContentDialogButton.Close,
CustomControls.ContentDialogTheme.Success
ContentDialogTheme.Success
); // Show success dialog
}
catch (DllNotFoundException ex)
{
// No need to revert the value if fail, user is asked to restart the app
ShowFailed(ex);
var res = await SpawnDialog(
Lang._Misc.MissingVcRedist,
Lang._Misc.MissingVcRedistSubtitle,
sender as UIElement,
Lang._Misc.Close,
Lang._Misc.Yes,
null,
ContentDialogButton.Primary,
ContentDialogTheme.Error);
if (res == ContentDialogResult.Primary)
{
await Task.Run(() =>
{
ProcessStartInfo psi = new ProcessStartInfo
{
FileName = "explorer.exe",
Arguments = "https://aka.ms/vs/17/release/vc_redist.x64.exe",
UseShellExecute = true,
Verb = "runas"
};
Process.Start(psi);
});
}
else { await SentryHelper.ExceptionHandlerAsync(ex); }
}
catch (Exception ex)
{
// Revert value if fail
Expand Down
Loading

0 comments on commit 9d63032

Please sign in to comment.