Skip to content

Commit

Permalink
Adding fallback to perform full update if no incremental update
Browse files Browse the repository at this point in the history
  • Loading branch information
neon-nyan committed Jan 2, 2025
1 parent fd4a680 commit 7a553be
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
// ReSharper disable GrammarMistakeInComment

using CollapseLauncher.Dialogs;
using CollapseLauncher.Extension;
using CollapseLauncher.Helper;
using Hi3Helper;
using Hi3Helper.Data;
Expand All @@ -15,6 +16,7 @@
using Hi3Helper.Shared.Region;
using Hi3Helper.Sophon;
using Hi3Helper.Sophon.Structs;
using Microsoft.UI.Xaml.Controls;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
Expand Down Expand Up @@ -108,7 +110,7 @@ protected virtual void CleanupTempSophonVerifiedFiles()
#endregion

#region Sophon Download and Install/Update/Preload Methods
public virtual async Task StartPackageInstallSophon(GameInstallStateEnum gameState)
public virtual async Task StartPackageInstallSophon(GameInstallStateEnum gameState, bool fallbackFromUpdate = false)
{
// Set the flag to false
_isSophonDownloadCompleted = false;
Expand Down Expand Up @@ -199,39 +201,84 @@ await _gameVersionManager.GamePreset

// Add the tag query to the Url
requestedUrl += $"&tag={requestedVersion.ToString()}";
#endif

// Initialize the info pair list
var sophonInfoPairList = new List<SophonChunkManifestInfoPair>();

// Get the info pair based on info provided above (for main game file)
var sophonMainInfoPair = await
SophonManifest.CreateSophonChunkManifestInfoPair(
httpClient,
requestedUrl,
"game",
_token.Token);
#endif

// Ensure that the manifest is ordered based on _gameVoiceLanguageLocaleIdOrdered
RearrangeSophonDataLocaleOrder(sophonMainInfoPair.OtherSophonData);

// Add the manifest to the pair list
sophonInfoPairList.Add(sophonMainInfoPair);
try
{
// Initialize the info pair list
var sophonInfoPairList = new List<SophonChunkManifestInfoPair>();

// Get the info pair based on info provided above (for main game file)
var sophonMainInfoPair = await
SophonManifest.CreateSophonChunkManifestInfoPair(
httpClient,
requestedUrl,
"game",
_token.Token);

// Ensure that the manifest is ordered based on _gameVoiceLanguageLocaleIdOrdered
RearrangeSophonDataLocaleOrder(sophonMainInfoPair.OtherSophonData);

// Add the manifest to the pair list
sophonInfoPairList.Add(sophonMainInfoPair);

List<string> voLanguageList =
GetSophonLanguageDisplayDictFromVoicePackList(sophonMainInfoPair.OtherSophonData);

// Get Audio Choices first.
// If the fallbackFromUpdate flag is set, then don't show the dialog and instead
// use the default language (ja-jp) as the fallback and read the existing audio_lang file
List<int> addedVo;
int setAsDefaultVo = GetSophonLocaleCodeIndex(
sophonMainInfoPair.OtherSophonData,
"ja-jp"
);

if (fallbackFromUpdate)
{
addedVo = [];
if (!File.Exists(_gameAudioLangListPathStatic))
{
addedVo.Add(setAsDefaultVo);
}
else
{
string[] voLangList = await File.ReadAllLinesAsync(_gameAudioLangListPathStatic);
foreach (string voLang in voLangList)
{
string? voLocaleId = GetLanguageLocaleCodeByLanguageString(
voLang
#if DEBUG
, true
#endif
);

if (string.IsNullOrEmpty(voLocaleId))
{
continue;
}

List<string> voLanguageList =
GetSophonLanguageDisplayDictFromVoicePackList(sophonMainInfoPair.OtherSophonData);
int voLocaleIndex = GetSophonLocaleCodeIndex(
sophonMainInfoPair.OtherSophonData,
voLocaleId
);
addedVo.Add(voLocaleIndex);
}

// Get Audio Choices first
(List<int> addedVo, int setAsDefaultVo) =
await SimpleDialogs.Dialog_ChooseAudioLanguageChoice(
voLanguageList,
GetSophonLocaleCodeIndex(
sophonMainInfoPair.OtherSophonData,
"ja-jp"
));
if (addedVo.Count == 0)
{
addedVo.Add(setAsDefaultVo);
}
}
}
else
{
(addedVo, setAsDefaultVo) =
await SimpleDialogs.Dialog_ChooseAudioLanguageChoice(
voLanguageList,
setAsDefaultVo);
}

try
{
if (addedVo == null || setAsDefaultVo < 0)
{
throw new TaskCanceledException();
Expand Down Expand Up @@ -437,7 +484,6 @@ private async Task<List<SophonAsset>> GetSophonAssetListFromPair(
return sophonAssetList;
}


public virtual async Task StartPackageUpdateSophon(GameInstallStateEnum gameState, bool isPreloadMode)
{
// Set the flag to false
Expand Down Expand Up @@ -513,8 +559,53 @@ await _gameVersionManager.GamePreset
SophonDownloadSpeedLimiter.CreateInstance(LauncherConfig.DownloadSpeedLimitCached);

// Add base game diff data
await AddSophonDiffAssetsToList(httpClient, requestedBaseUrlFrom, requestedBaseUrlTo,
sophonUpdateAssetList, "game", downloadSpeedLimiter);
bool isSuccess = await AddSophonDiffAssetsToList(
httpClient,
requestedBaseUrlFrom,
requestedBaseUrlTo,
sophonUpdateAssetList,
"game",
downloadSpeedLimiter);

// If it doesn't success to get the base diff, then fallback to actually download the whole game
if (!isSuccess)
{
Logger.LogWriteLine($"The current game version: {requestedVersionFrom.ToString()} is too obsolete and incremental update is unavailable. Falling back to full update", LogType.Warning, true);

// Spawn the confirmation dialog
ContentDialogResult fallbackResultConfirm = await SimpleDialogs.SpawnDialog(
Locale.Lang._Dialogs.SophonIncrementUpdateUnavailTitle,
new TextBlock
{
TextWrapping = Microsoft.UI.Xaml.TextWrapping.Wrap
}
.AddTextBlockLine(string.Format(Locale.Lang._Dialogs.SophonIncrementUpdateUnavailSubtitle1, requestedVersionFrom.ToString()))
.AddTextBlockLine(Locale.Lang._Dialogs.SophonIncrementUpdateUnavailSubtitle2, Microsoft.UI.Text.FontWeights.Bold)
.AddTextBlockLine(Locale.Lang._Dialogs.SophonIncrementUpdateUnavailSubtitle3)
.AddTextBlockNewLine(2)
.AddTextBlockLine(
string.Format(Locale.Lang._Dialogs.SophonIncrementUpdateUnavailSubtitle4,
Locale.Lang._Misc.YesContinue,
Locale.Lang._Misc.NoCancel),
Microsoft.UI.Text.FontWeights.Bold,
10),
_parentUI,
Locale.Lang._Misc.NoCancel,
Locale.Lang._Misc.YesContinue,
defaultButton: ContentDialogButton.Primary,
dialogTheme: CustomControls.ContentDialogTheme.Warning);

// If cancelled, then throw
if (ContentDialogResult.Primary != fallbackResultConfirm)
{
throw new TaskCanceledException("The full update routine has been cancelled by the user");
}

// Otherwise, continue updating the entire game
gameState = GameInstallStateEnum.NotInstalled;
await StartPackageInstallSophon(gameState, true);
return;
}

// If the game has lang list path, then add it
if (_gameAudioLangListPath != null)
Expand Down Expand Up @@ -749,20 +840,33 @@ await SimpleDialogs.Dialog_InsufficientDriveSpace(_parentUI, driveInfo.TotalFree
#endregion

#region Sophon Asset Package Methods

private async Task AddSophonDiffAssetsToList(HttpClient httpClient,
string requestedUrlFrom,
string requestedUrlTo,
List<SophonAsset> sophonPreloadAssetList,
string matchingField,
SophonDownloadSpeedLimiter downloadSpeedLimiter)
private async Task<bool> AddSophonDiffAssetsToList(HttpClient httpClient,
string requestedUrlFrom,
string requestedUrlTo,
List<SophonAsset> sophonPreloadAssetList,
string matchingField,
SophonDownloadSpeedLimiter downloadSpeedLimiter)
{
// Get the manifest pair for both previous (from) and next (to) version
SophonChunkManifestInfoPair requestPairFrom = await SophonManifest
.CreateSophonChunkManifestInfoPair(httpClient, requestedUrlFrom, matchingField, _token.Token);
SophonChunkManifestInfoPair requestPairTo = await SophonManifest
.CreateSophonChunkManifestInfoPair(httpClient, requestedUrlTo, matchingField, _token.Token);

// If the request pair source is not found, then return false
if (!requestPairFrom.IsFound)
{
Logger.LogWriteLine($"Sophon manifest for source via URL: {requestedUrlFrom} is not found! Return message: {requestPairFrom.ReturnMessage} ({requestPairFrom.ReturnCode})", LogType.Warning, true);
return false;
}

// If the request pair target is not found, then return false
if (!requestPairTo.IsFound)
{
Logger.LogWriteLine($"Sophon manifest for target via URL: {requestedUrlTo} is not found! Return message: {requestPairTo.ReturnMessage} ({requestPairTo.ReturnCode})", LogType.Warning, true);
return false;
}

// Ensure that the manifest is ordered based on _gameVoiceLanguageLocaleIdOrdered
RearrangeSophonDataLocaleOrder(requestPairFrom.OtherSophonData);
RearrangeSophonDataLocaleOrder(requestPairTo.OtherSophonData);
Expand All @@ -775,6 +879,9 @@ private async Task AddSophonDiffAssetsToList(HttpClient httpClie
{
sophonPreloadAssetList.Add(sophonAsset);
}

// Return as success
return true;
}

private async Task AddSophonAdditionalVODiffAssetsToList(HttpClient httpClient,
Expand Down
5 changes: 5 additions & 0 deletions Hi3Helper.Core/Lang/Locale/LangDialogs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,11 @@ public sealed partial class LangDialogs

public string DbGenerateUid_Title { get; set; } = LangFallback?._Dialogs.DbGenerateUid_Title;
public string DbGenerateUid_Content { get; set; } = LangFallback?._Dialogs.DbGenerateUid_Content;
public string SophonIncrementUpdateUnavailTitle { get; set; } = LangFallback?._Dialogs.SophonIncrementUpdateUnavailTitle;
public string SophonIncrementUpdateUnavailSubtitle1 { get; set; } = LangFallback?._Dialogs.SophonIncrementUpdateUnavailSubtitle1;
public string SophonIncrementUpdateUnavailSubtitle2 { get; set; } = LangFallback?._Dialogs.SophonIncrementUpdateUnavailSubtitle2;
public string SophonIncrementUpdateUnavailSubtitle3 { get; set; } = LangFallback?._Dialogs.SophonIncrementUpdateUnavailSubtitle3;
public string SophonIncrementUpdateUnavailSubtitle4 { get; set; } = LangFallback?._Dialogs.SophonIncrementUpdateUnavailSubtitle4;
}
}
#endregion
Expand Down
8 changes: 7 additions & 1 deletion Hi3Helper.Core/Lang/en_US.json
Original file line number Diff line number Diff line change
Expand Up @@ -995,7 +995,13 @@
"CloseOverlay": "Close Overlay",

"DbGenerateUid_Title": "Are you sure you wish to change your user ID?",
"DbGenerateUid_Content": "Changing the current user ID will cause the associated data to be lost if you lose it."
"DbGenerateUid_Content": "Changing the current user ID will cause the associated data to be lost if you lose it.",

"SophonIncrementUpdateUnavailTitle": "Incremental Update is Unavailable: Version is Too Obsolete!",
"SophonIncrementUpdateUnavailSubtitle1": "Your game version: {0}",
"SophonIncrementUpdateUnavailSubtitle2": " is too obsolete",
"SophonIncrementUpdateUnavailSubtitle3": " and incremental update for your version is not available. However, you could still update your game by re-downloading the entire thing from scratch.",
"SophonIncrementUpdateUnavailSubtitle4": "Click \"{0}\" to continue updating the entire thing or click \"{1}\" to cancel the process"
},

"_FileMigrationProcess": {
Expand Down
2 changes: 1 addition & 1 deletion Hi3Helper.Sophon

0 comments on commit 7a553be

Please sign in to comment.