Skip to content

Commit

Permalink
Update sophon and HYP install mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
neon-nyan committed May 25, 2024
1 parent 9382212 commit a272fdc
Show file tree
Hide file tree
Showing 8 changed files with 290 additions and 164 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace CollapseLauncher.Helper.JsonConverter
{
public class RegionResourcePluginValidateConverter : JsonConverter<List<RegionResourcePluginValidate>>
{
public override bool CanConvert(Type type)
{
return true;
}

public override List<RegionResourcePluginValidate> Read(
ref Utf8JsonReader reader,
Type typeToConvert,
JsonSerializerOptions options)
{
string valueString = EmptiedBackslash(reader.ValueSpan);
List<RegionResourcePluginValidate> returnList = valueString.Deserialize<List<RegionResourcePluginValidate>>(InternalAppJSONContext.Default);

return returnList;
}

private unsafe string EmptiedBackslash(ReadOnlySpan<byte> span)
{
Span<byte> buffer = new byte[span.Length];
int indexIn = 0;
int indexOut = 0;
while (indexIn < span.Length)
{
if (span[indexIn] == '\\')
{
++indexIn;
continue;
}

buffer[indexOut] = span[indexIn];
++indexIn;
++indexOut;
}

fixed (byte* bufferPtr = buffer)
{
return Encoding.UTF8.GetString(bufferPtr, indexOut);
}
}

public override void Write(
Utf8JsonWriter writer,
List<RegionResourcePluginValidate> baseType,
JsonSerializerOptions options)
{

throw new JsonException($"Serializing is not supported!");
}
}
}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using CollapseLauncher.Helper.JsonConverter;
using System.Collections.Generic;
using System.Text.Json.Serialization;

#nullable enable
Expand Down Expand Up @@ -34,8 +35,8 @@ public class LauncherPackages
[JsonPropertyName("main")]
public PackagePartition? MainPackage { get; set; }

[JsonPropertyName("plugin")]
public PackagePartition? PluginPackage { get; set; }
[JsonPropertyName("plugins")]
public List<PackagePluginSections>? PluginPackageSections { get; set; }

[JsonPropertyName("pre_download")]
public PackagePartition? PreDownload { get; set; }
Expand All @@ -53,13 +54,28 @@ public class GameDetail
public class PackagePartition
{
[JsonPropertyName("major")]
public PackageSections? CurrentVersion { get; set; }
public PackageResourceSections? CurrentVersion { get; set; }

[JsonPropertyName("patches")]
public List<PackageSections>? Patches { get; set; }
public List<PackageResourceSections>? Patches { get; set; }
}

public class PackageSections
public class PackagePluginSections
{
[JsonPropertyName("plugin_id")]
public string? PluginId { get; set; }

[JsonPropertyName("plugin_pkg")]
public PackageDetails? PluginPackage { get; set; }

[JsonPropertyName("release_id")]
public string? ReleaseId { get; set; }

[JsonPropertyName("version")]
public string? Version { get; set; }
}

public class PackageResourceSections
{
[JsonPropertyName("audio_pkgs")]
public List<PackageDetails>? AudioPackages { get; set; }
Expand Down Expand Up @@ -100,8 +116,11 @@ public class PackageDetails
public string? ChannelSDKPkg { get; init; }

[JsonPropertyName("command")]
// TODO: Figure out the property purpose
public string? Command { get; init; }
public string? PackageRunCommand { get; init; }

[JsonPropertyName("validation")]
[JsonConverter(typeof(RegionResourcePluginValidateConverter))]
public List<RegionResourcePluginValidate>? PackageAssetValidationList { get; init; }

[JsonPropertyName("language")]
public string? Language { get; init; }
Expand Down
3 changes: 3 additions & 0 deletions CollapseLauncher/Classes/Helper/Metadata/PresetConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ internal class PresetConfig
[JsonConverter(typeof(ServeV3StringConverter))]
public string? GameName { get; set; }

[JsonConverter(typeof(ServeV3StringConverter))]
public string? LauncherBizName { get; init; }

[JsonConverter(typeof(ServeV3StringConverter))]
public string? LauncherSpriteURL { get; init; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2057,8 +2057,8 @@ private async Task GetPackagesRemoteSize(List<GameInstallPackage> packageList, C
#endregion
#region Virtual Methods - StartPackageDownload

private enum CompletenessStatus { Running, Completed, Cancelled, Idle }
private void UpdateCompletenessStatus(CompletenessStatus status)
protected enum CompletenessStatus { Running, Completed, Cancelled, Idle }
protected void UpdateCompletenessStatus(CompletenessStatus status)
{
switch (status)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ public override async Task StartPackageDownload(bool skipDialog)

try
{
// Set background status
UpdateCompletenessStatus(CompletenessStatus.Running);

// Reset status and progress properties
ResetStatusAndProgressProperty();

Expand Down Expand Up @@ -137,8 +140,37 @@ await Parallel.ForEachAsync(
async (asset, threadToken) => await RunSophonAssetDownloadThread(httpClient, asset, parallelOptions));
}

// Rename temporary files
foreach (SophonChunkManifestInfoPair sophonDownloadInfoPair in sophonInfoPairList)
{
await Parallel.ForEachAsync(
SophonManifest.EnumerateAsync(httpClient, sophonDownloadInfoPair),
parallelOptions,
async (asset, threadToken) =>

Check warning on line 149 in CollapseLauncher/Classes/InstallManagement/Genshin/GenshinSophonInstall.cs

View workflow job for this annotation

GitHub Actions / build (Release, x64, net8.0-windows10.0.22621.0)

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 149 in CollapseLauncher/Classes/InstallManagement/Genshin/GenshinSophonInstall.cs

View workflow job for this annotation

GitHub Actions / build (Release, x64, net8.0-windows10.0.22621.0)

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 149 in CollapseLauncher/Classes/InstallManagement/Genshin/GenshinSophonInstall.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Async function without await expression

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning

Code scanning / QDNET

Async function without await expression Warning

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
{
// If the asset is a dictionary, then return
if (asset.IsDirectory) return;

// Get the file path and start the write process
string assetName = asset.AssetName;
string filePath = EnsureCreationOfDirectory(Path.Combine(_gamePath, assetName)) + "_tempSophon";
string origFilePath = Path.Combine(_gamePath, assetName);

if (File.Exists(filePath))
File.Move(filePath, origFilePath, true);
});
}

// Set background status
UpdateCompletenessStatus(CompletenessStatus.Completed);
IsDownloadCompleted = true;
}
catch (Exception)
{
// Set background status
UpdateCompletenessStatus(CompletenessStatus.Cancelled);
throw;
}
finally
{
// Unsubscribe the logger event
Expand Down Expand Up @@ -193,7 +225,13 @@ private async ValueTask RunSophonAssetDownloadThread(HttpClient client, SophonAs
if (asset.IsDirectory) return;

// Get the file path and start the write process
string filePath = EnsureCreationOfDirectory(Path.Combine(_gamePath, asset.AssetName));
string assetName = asset.AssetName;
string filePath = EnsureCreationOfDirectory(Path.Combine(_gamePath, assetName));

// Use "_tempSophon" if file is new or if "_tempSophon" file exist. Otherwise use original file if exist
if (!File.Exists(filePath) || File.Exists(filePath + "_tempSophon"))
filePath += "_tempSophon";

await asset.WriteToStreamAsync(
client,
() => new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite),
Expand Down
55 changes: 1 addition & 54 deletions CollapseLauncher/Classes/RegionManagement/RegionClasses.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using CollapseLauncher.Helper.Image;
using CollapseLauncher.Helper.JsonConverter;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading;

Expand Down Expand Up @@ -147,58 +146,6 @@ public class RegionResourceVersion : IRegionResourceCopyable<RegionResourceVersi
};
}

public class RegionResourcePluginValidateConverter : JsonConverter<List<RegionResourcePluginValidate>>
{
public override bool CanConvert(Type type)
{
return true;
}

public override List<RegionResourcePluginValidate> Read(
ref Utf8JsonReader reader,
Type typeToConvert,
JsonSerializerOptions options)
{
string valueString = EmptiedBackslash(reader.ValueSpan);
List<RegionResourcePluginValidate> returnList = valueString.Deserialize<List<RegionResourcePluginValidate>>(InternalAppJSONContext.Default);

return returnList;
}

private unsafe string EmptiedBackslash(ReadOnlySpan<byte> span)
{
Span<byte> buffer = new byte[span.Length];
int indexIn = 0;
int indexOut = 0;
while (indexIn < span.Length)
{
if (span[indexIn] == '\\')
{
++indexIn;
continue;
}

buffer[indexOut] = span[indexIn];
++indexIn;
++indexOut;
}

fixed (byte* bufferPtr = buffer)
{
return Encoding.UTF8.GetString(bufferPtr, indexOut);
}
}

public override void Write(
Utf8JsonWriter writer,
List<RegionResourcePluginValidate> baseType,
JsonSerializerOptions options)
{

throw new JsonException($"Serializing is not supported!");
}
}

public class HomeMenuPanel : IRegionResourceCopyable<HomeMenuPanel>
{
public List<MenuPanelProp> sideMenuPanel { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion Hi3Helper.Sophon

0 comments on commit a272fdc

Please sign in to comment.