From 9dcdcb58463eb24614814b7388c97ced9f0fbb14 Mon Sep 17 00:00:00 2001 From: Kemal Setya Adhi Date: Mon, 13 Jan 2025 00:18:10 +0700 Subject: [PATCH] Move Sophon.Test into its own submodule --- Hi3Helper.Sophon | 2 +- .../Hi3Helper.Sophon.Test.csproj | 25 -- .../Hi3Helper.Sophon.Test.sln | 31 -- Hi3Helper.Sophon.Test/Program.cs | 356 ------------------ .../PublishProfiles/FolderProfile.pubxml | 20 - .../Properties/launchSettings.json | 20 - 6 files changed, 1 insertion(+), 453 deletions(-) delete mode 100644 Hi3Helper.Sophon.Test/Hi3Helper.Sophon.Test.csproj delete mode 100644 Hi3Helper.Sophon.Test/Hi3Helper.Sophon.Test/Hi3Helper.Sophon.Test.sln delete mode 100644 Hi3Helper.Sophon.Test/Program.cs delete mode 100644 Hi3Helper.Sophon.Test/Properties/PublishProfiles/FolderProfile.pubxml delete mode 100644 Hi3Helper.Sophon.Test/Properties/launchSettings.json diff --git a/Hi3Helper.Sophon b/Hi3Helper.Sophon index 122c6193f..3872035a3 160000 --- a/Hi3Helper.Sophon +++ b/Hi3Helper.Sophon @@ -1 +1 @@ -Subproject commit 122c6193f6e577fe21d28d7832c75e4251d0c5b3 +Subproject commit 3872035a3ea72b03a9f720d42b6179741b16943b diff --git a/Hi3Helper.Sophon.Test/Hi3Helper.Sophon.Test.csproj b/Hi3Helper.Sophon.Test/Hi3Helper.Sophon.Test.csproj deleted file mode 100644 index a38a928b6..000000000 --- a/Hi3Helper.Sophon.Test/Hi3Helper.Sophon.Test.csproj +++ /dev/null @@ -1,25 +0,0 @@ - - - Exe - net462 - disable - x64 - Hi3Helper.Sophon.Test.Main - DEMODIFF - preview - - - - portable - - - - - - - - - - - - diff --git a/Hi3Helper.Sophon.Test/Hi3Helper.Sophon.Test/Hi3Helper.Sophon.Test.sln b/Hi3Helper.Sophon.Test/Hi3Helper.Sophon.Test/Hi3Helper.Sophon.Test.sln deleted file mode 100644 index 00c992e1e..000000000 --- a/Hi3Helper.Sophon.Test/Hi3Helper.Sophon.Test/Hi3Helper.Sophon.Test.sln +++ /dev/null @@ -1,31 +0,0 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 17 -VisualStudioVersion = 17.11.34929.205 -MinimumVisualStudioVersion = 10.0.40219.1 -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Hi3Helper.Sophon.Universal", "..\..\Hi3Helper.Sophon\Hi3Helper.Sophon.Universal.csproj", "{58B8538F-C2F2-47AD-A8C1-966040B7C976}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Hi3Helper.Sophon.Test", "..\Hi3Helper.Sophon.Test.csproj", "{43CEB3FE-530B-4CE4-833A-40A4720104E0}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|x64 = Debug|x64 - Release|x64 = Release|x64 - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {58B8538F-C2F2-47AD-A8C1-966040B7C976}.Debug|x64.ActiveCfg = Debug|x64 - {58B8538F-C2F2-47AD-A8C1-966040B7C976}.Debug|x64.Build.0 = Debug|x64 - {58B8538F-C2F2-47AD-A8C1-966040B7C976}.Release|x64.ActiveCfg = Release|x64 - {58B8538F-C2F2-47AD-A8C1-966040B7C976}.Release|x64.Build.0 = Release|x64 - {43CEB3FE-530B-4CE4-833A-40A4720104E0}.Debug|x64.ActiveCfg = Debug|x64 - {43CEB3FE-530B-4CE4-833A-40A4720104E0}.Debug|x64.Build.0 = Debug|x64 - {43CEB3FE-530B-4CE4-833A-40A4720104E0}.Release|x64.ActiveCfg = Release|x64 - {43CEB3FE-530B-4CE4-833A-40A4720104E0}.Release|x64.Build.0 = Release|x64 - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection - GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {A5298644-AC74-413B-876B-881C03B58503} - EndGlobalSection -EndGlobal diff --git a/Hi3Helper.Sophon.Test/Program.cs b/Hi3Helper.Sophon.Test/Program.cs deleted file mode 100644 index cbc59ff38..000000000 --- a/Hi3Helper.Sophon.Test/Program.cs +++ /dev/null @@ -1,356 +0,0 @@ -using Hi3Helper.Sophon.Helper; -using Hi3Helper.Sophon.Infos; -using Hi3Helper.Sophon.Structs; -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.IO; -using System.Linq; -using System.Net; -using System.Net.Http; -using System.Threading; -using System.Threading.Tasks; - -namespace Hi3Helper.Sophon.Test -{ - public class MainApp - { - static string CancelMessage = ""; - static bool IsRetry = false; - static string[] SizeSuffixes = { "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" }; - - private static int UsageHelp() - { - string executableName = Process.GetCurrentProcess().ProcessName; -#if !DEMODIFF - Console.WriteLine($"{executableName} [Sophon Branch Url] [Matching field name (usually, you can set \"game\" as the value)] [Download Output Path] [OPTIONAL: Amount of threads to be used (Default: {Environment.ProcessorCount})] [OPTIONAL: Amount of max. connection used for Http Client (Default: 128)]"); -#else - Console.WriteLine($"{executableName} [Preload/Update] [Sophon Branch Url From] [Sophon Branch Url To] [Matching field name (usually, you can set \"game\" as the value)] [Old Directory Path] [New Directory Path] [OPTIONAL: Amount of threads to be used (Default: {Environment.ProcessorCount})] [OPTIONAL: Amount of max. connection used for Http Client (Default: 128)]"); -#endif - return 1; - } - - public static async Task Main(params string[] args) - { - int threads = Environment.ProcessorCount; - int maxHttpHandle = 128; - -#if !DEMODIFF - if (args.Length < 3) - return UsageHelp(); - - if (args.Length > 3 && int.TryParse(args[3], out threads)) - Console.WriteLine($"Thread count has been set to: {threads} for downloading!"); - - if (args.Length > 4 && int.TryParse(args[4], out maxHttpHandle)) - Console.WriteLine($"HTTP Client maximum connection has been set to: {maxHttpHandle} handles!"); - - string outputDir = args[2]; -#else - bool isPreloadMode = false; - - if (args.Length < 6) - return UsageHelp(); - - if (!((isPreloadMode = args[0].Equals("Preload", StringComparison.OrdinalIgnoreCase)) || args[0].Equals("Update", StringComparison.OrdinalIgnoreCase))) - return UsageHelp(); - - if (args.Length > 6 && int.TryParse(args[6], out threads)) - Console.WriteLine($"Thread count has been set to: {threads} for downloading!"); - - if (args.Length > 7 && int.TryParse(args[7], out maxHttpHandle)) - Console.WriteLine($"HTTP Client maximum connection has been set to: {maxHttpHandle} handles!"); - - string outputDir = args[4]; -#endif - - // Logger.LogHandler += Logger_LogHandler; - - StartDownload: - using (CancellationTokenSource tokenSource = new CancellationTokenSource()) - { - CancelMessage = "[\"C\"] Stop or [\"R\"] Restart"; - using (HttpClientHandler httpHandler = new HttpClientHandler - { - MaxConnectionsPerServer = maxHttpHandle - }) - using (HttpClient httpClient = new HttpClient(httpHandler) - { -#if NET6_0_OR_GREATER - DefaultRequestVersion = HttpVersion.Version30, - DefaultVersionPolicy = HttpVersionPolicy.RequestVersionOrLower -#endif - }) - { -#if !DEMODIFF - SophonChunkManifestInfoPair manifestPair = await SophonManifest.CreateSophonChunkManifestInfoPair(httpClient, args[0], args[1], tokenSource.Token); - - SophonChunksInfo sophonChunksInfo = manifestPair.ChunksInfo; - SophonManifestInfo sophonManifestInfo = manifestPair.ManifestInfo; -#else - string outputNewDir = args[5]; - - SophonChunkManifestInfoPair manifestPairFrom = await SophonManifest.CreateSophonChunkManifestInfoPair(httpClient, args[1], args[3], tokenSource.Token); - - SophonChunksInfo sophonChunksInfoFrom = manifestPairFrom.ChunksInfo; - SophonManifestInfo sophonManifestInfoFrom = manifestPairFrom.ManifestInfo; - - SophonChunkManifestInfoPair manifestPairTo = await SophonManifest.CreateSophonChunkManifestInfoPair(httpClient, args[2], args[3], tokenSource.Token); - - SophonChunksInfo sophonChunksInfoTo = manifestPairTo.ChunksInfo; - SophonManifestInfo sophonManifestInfoTo = manifestPairTo.ManifestInfo; -#endif - - long currentRead = 0; - Task.Run(() => AppExitKeyTrigger(tokenSource)); - - ParallelOptions parallelOptions = - new ParallelOptions - { - CancellationToken = tokenSource.Token, - MaxDegreeOfParallelism = threads - }; - - Stopwatch stopwatch = Stopwatch.StartNew(); - - try - { - string chunkOutPath = Path.Combine(outputDir, "chunk_collapse"); - if (!Directory.Exists(chunkOutPath)) - Directory.CreateDirectory(chunkOutPath); - - if (!Directory.Exists(outputNewDir)) - Directory.CreateDirectory(outputNewDir); -#if !DEMODIFF - string totalSizeUnit = SummarizeSizeSimple(sophonChunksInfo.TotalSize); - string totalSizeDiffUnit = "0 B"; -#else - long totalSizeDiff = await SophonUpdate.GetCalculatedDiffSizeAsync(httpClient, manifestPairFrom, manifestPairTo, false, tokenSource.Token); - string totalSizeDiffUnit = SummarizeSizeSimple(totalSizeDiff); - string totalSizeUnit = SummarizeSizeSimple(manifestPairTo.ChunksInfo.TotalSize); -#endif - - foreach (string fileTemp in Directory.EnumerateFiles(outputDir, "*_tempUpdate", SearchOption.AllDirectories)) - { - File.Delete(fileTemp); - } - - IsRetry = false; -#if NET6_0_OR_GREATER - await Parallel.ForEachAsync( -#if !DEMODIFF - SophonManifest.EnumerateAsync( - httpClient, - sophonManifestInfo, - sophonChunksInfo, - tokenSource.Token) -#else - SophonUpdate.EnumerateUpdateAsync(httpClient, manifestPairFrom, manifestPairTo) -#endif - , - parallelOptions, - async (asset, token) => - { - if (asset.IsDirectory) - return; - - string outputAssetPath = Path.Combine(outputDir, asset.AssetName!); - string outputAssetDir = Path.GetDirectoryName(outputAssetPath); - - if (!string.IsNullOrEmpty(outputAssetDir) && !Directory.Exists(outputAssetDir)) - Directory.CreateDirectory(outputAssetDir); - -#if !DEMODIFF - await asset.WriteToStreamAsync( - httpClient, - // fileStream, - () => new FileStream(outputAssetPath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite), - parallelOptions, - (read) => - { - Interlocked.Add(ref currentRead, read); - string sizeUnit = SummarizeSizeSimple(currentRead); - string speedUnit = SummarizeSizeSimple(currentRead / stopwatch.Elapsed.TotalSeconds); - Console.Write($"{CancelMessage} | {sizeUnit}/{totalSizeUnit} ({totalSizeDiffUnit} diff) -> {currentRead} ({speedUnit}/s) \r"); - }, - (asset) => - { - Console.WriteLine($"Downloaded: {asset.AssetName}"); - } - ); -#else - if (isPreloadMode) - { - await asset.DownloadDiffChunksAsync( - httpClient, - chunkOutPath, - parallelOptions, - (read) => - { - Interlocked.Add(ref currentRead, read); - string sizeUnit = SummarizeSizeSimple(currentRead); - string speedUnit = SummarizeSizeSimple(currentRead / stopwatch.Elapsed.TotalSeconds); - Console.Write($"{CancelMessage} | {sizeUnit}/{totalSizeUnit} ({totalSizeDiffUnit} diff) -> {currentRead} ({speedUnit}/s) \r"); - } - ); - } - else - { - await asset.WriteUpdateAsync( - httpClient, - outputDir, - outputNewDir, - chunkOutPath, - parallelOptions, - (read) => - { - Interlocked.Add(ref currentRead, read); - string sizeUnit = SummarizeSizeSimple(currentRead); - string speedUnit = SummarizeSizeSimple(currentRead / stopwatch.Elapsed.TotalSeconds); - Console.Write($"{CancelMessage} | {sizeUnit}/{totalSizeUnit} ({totalSizeDiffUnit} diff) -> {currentRead} ({speedUnit}/s) \r"); - }, - (asset) => - { - Console.WriteLine($"Downloaded: {asset.AssetName}"); - } - ); - } -#endif - }); -#else - await foreach (SophonAsset asset in -#if !DEMODIFF - await SophonManifest.GetAssetListAsync( - httpClient, - sophonManifestInfo, - sophonChunksInfo, - tokenSource.Token) -#else - SophonUpdate.EnumerateUpdateAsync(httpClient, manifestPairFrom, manifestPairTo).WithCancellation(tokenSource.Token) -#endif - ) - { - if (asset.IsDirectory) - continue; - - string outputAssetPath = Path.Combine(outputDir, asset.AssetName); - string outputAssetDir = Path.GetDirectoryName(outputAssetPath); - - if (!string.IsNullOrEmpty(outputAssetDir) && !Directory.Exists(outputAssetDir)) - Directory.CreateDirectory(outputAssetDir); - - // using FileStream fileStream = new FileStream(outputAssetPath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite); - -#if !DEMODIFF - await asset.WriteToStreamAsync( - httpClient, - // fileStream, - () => new FileStream(outputAssetPath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite), - parallelOptions, - (read) => - { - Interlocked.Add(ref currentRead, read); - string sizeUnit = SummarizeSizeSimple(currentRead); - string speedUnit = SummarizeSizeSimple(currentRead / stopwatch.Elapsed.TotalSeconds); - Console.Write($"{CancelMessage} | {sizeUnit}/{totalSizeUnit} ({totalSizeDiffUnit} diff) -> {currentRead} ({speedUnit}/s) \r"); - }, - (assetDone) => - { - Console.WriteLine($"Downloaded: {asset.AssetName}"); - } - ); -#else - if (isPreloadMode) - { - await asset.DownloadDiffChunksAsync( - httpClient, - chunkOutPath, - parallelOptions, - (read) => - { - Interlocked.Add(ref currentRead, read); - string sizeUnit = SummarizeSizeSimple(currentRead); - string speedUnit = SummarizeSizeSimple(currentRead / stopwatch.Elapsed.TotalSeconds); - Console.Write($"{CancelMessage} | {sizeUnit}/{totalSizeUnit} ({totalSizeDiffUnit} diff) -> {currentRead} ({speedUnit}/s) \r"); - } - ); - } - else - { - await asset.WriteUpdateAsync( - httpClient, - outputDir, - outputNewDir, - chunkOutPath, - parallelOptions, - (read) => - { - Interlocked.Add(ref currentRead, read); - string sizeUnit = SummarizeSizeSimple(currentRead); - string speedUnit = SummarizeSizeSimple(currentRead / stopwatch.Elapsed.TotalSeconds); - Console.Write($"{CancelMessage} | {sizeUnit}/{totalSizeUnit} ({totalSizeDiffUnit} diff) -> {currentRead} ({speedUnit}/s) \r"); - }, - (asset) => - { - Console.WriteLine($"Downloaded: {asset.AssetName}"); - } - ); - } -#endif - } -#endif - } - catch (OperationCanceledException) - { - Console.WriteLine("Download has been cancelled!"); - } - finally - { - stopwatch.Stop(); - } - } - } - - if (IsRetry) - goto StartDownload; - - return 0; - } - - private static string SummarizeSizeSimple(double value, int decimalPlaces = 2) - { - byte mag = (byte)Math.Log(value, 1000); - - return string.Format("{0} {1}", Math.Round(value / (1L << (mag * 10)), decimalPlaces), SizeSuffixes[mag]); - } - - private static void AppExitKeyTrigger(CancellationTokenSource tokenSource) - { - while (true) - { - ConsoleKeyInfo keyInfo = Console.ReadKey(); - switch (keyInfo.Key) - { - case ConsoleKey.C: - CancelMessage = "Cancelling download..."; - tokenSource.Cancel(); - return; - case ConsoleKey.R: - IsRetry = true; - CancelMessage = "Retrying download..."; - tokenSource.Cancel(); - return; - } - } - } - - private static void Logger_LogHandler(object sender, LogStruct e) - { -#if !DEBUG - // if (e.LogLevel == LogLevel.Debug) return; -#endif - - Console.WriteLine($"[{e.LogLevel}] {e.Message}"); - } - } -} \ No newline at end of file diff --git a/Hi3Helper.Sophon.Test/Properties/PublishProfiles/FolderProfile.pubxml b/Hi3Helper.Sophon.Test/Properties/PublishProfiles/FolderProfile.pubxml deleted file mode 100644 index 4b82f5548..000000000 --- a/Hi3Helper.Sophon.Test/Properties/PublishProfiles/FolderProfile.pubxml +++ /dev/null @@ -1,20 +0,0 @@ - - - - - Release - x64 - buildtest - FileSystem - <_TargetId>Folder - net8.0 - win-x64 - true - false - false - true - true - - \ No newline at end of file diff --git a/Hi3Helper.Sophon.Test/Properties/launchSettings.json b/Hi3Helper.Sophon.Test/Properties/launchSettings.json deleted file mode 100644 index d33e0a50a..000000000 --- a/Hi3Helper.Sophon.Test/Properties/launchSettings.json +++ /dev/null @@ -1,20 +0,0 @@ -{ - "profiles": { - "WSL": { - "commandName": "WSL2", - "distributionName": "" - }, - "Hi3Helper.Sophon.Test - Download": { - "commandName": "Project", - "commandLineArgs": "\"https://sg-public-api.hoyoverse.com/downloader/sophon_chunk/api/getBuild?branch=main&package_id=ScSYQBFhu9&password=bDL4JUHL625x&plat_app=ddxf6vlr1reo\" \"game\" \"C:\\myGit\\testSophos\" 1" - }, - "Hi3Helper.Sophon.Test - Preload Chunks": { - "commandName": "Project", - "commandLineArgs": "Preload \"https://sg-public-api.hoyoverse.com/downloader/sophon_chunk/api/getBuild?branch=main&package_id=ScSYQBFhu9&password=bDL4JUHL625x&plat_app=ddxf6vlr1reo&tag=4.6.0\" \"https://sg-public-api.hoyoverse.com/downloader/sophon_chunk/api/getBuild?branch=main&package_id=ScSYQBFhu9&password=bDL4JUHL625x&plat_app=ddxf6vlr1reo\" \"game\" \"C:\\myGit\\testSophos\" \"C:\\myGit\\newTestSophos\"" - }, - "Hi3Helper.Sophon.Test - Update": { - "commandName": "Project", - "commandLineArgs": "Update \"https://sg-public-api.hoyoverse.com/downloader/sophon_chunk/api/getBuild?branch=main&package_id=ScSYQBFhu9&password=bDL4JUHL625x&plat_app=ddxf6vlr1reo&tag=4.6.0\" \"https://sg-public-api.hoyoverse.com/downloader/sophon_chunk/api/getBuild?branch=main&package_id=ScSYQBFhu9&password=bDL4JUHL625x&plat_app=ddxf6vlr1reo\" \"game\" \"C:\\myGit\\testSophos\" \"C:\\myGit\\newTestSophos\"" - } - } -} \ No newline at end of file