├── compile.bat ├── Core ├── Program.cs ├── config.sample.json ├── Utils │ ├── Formatter.cs │ └── WindowUtils.cs ├── Runner │ ├── AppRunner.cs │ ├── DownloadExecutor.cs │ ├── InteractiveMenu.cs │ └── CliHandler.cs ├── Game.cs ├── Core.csproj ├── Assets.cs ├── Downloader.cs ├── SophonUrl.cs └── AppConfig.cs ├── Sophon ├── Helper │ ├── Delegates.cs │ ├── Converter.cs │ ├── Logger.cs │ ├── TaskExtensions.cs │ ├── ChunkStream.cs │ └── Extension.cs ├── Structs │ ├── SophonChunk.cs │ ├── SophonManifestInfo.cs │ ├── SophonChunksInfo.cs │ ├── SophonPatchBranch.cs │ ├── SophonChunksBranch.cs │ ├── SophonManifestInfoPair.cs │ └── SophonInfosJson.cs ├── Sophon.csproj ├── SophonDownloadSpeedLimiter.cs ├── SophonManifest.cs ├── SophonPatch.cs ├── SophonAsset.Update.cs ├── SophonUpdate.cs ├── SophonAsset.Diff.cs ├── SophonAsset.Download.cs ├── SophonPatchAsset.Download.cs └── SophonPatchAsset.Update.cs ├── .editorconfig ├── README.md └── LICENSE /compile.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | cd Core 3 | dotnet publish -c Release 4 | pause 5 | taskkill /F /IM dotnet.exe -------------------------------------------------------------------------------- /Core/Program.cs: -------------------------------------------------------------------------------- 1 | using System.Threading.Tasks; 2 | 3 | namespace Core 4 | { 5 | public class Program 6 | { 7 | public static async Task Main(string[] args) 8 | { 9 | return await Runner.AppRunner.Run(args); 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Sophon/Helper/Delegates.cs: -------------------------------------------------------------------------------- 1 | namespace Sophon.Helper 2 | { 3 | public delegate void DelegateWriteStreamInfo(long writeBytes); 4 | public delegate void DelegateWriteDownloadInfo(long downloadedBytes, long diskWriteBytes); 5 | public delegate void DelegateDownloadAssetComplete(SophonAsset asset); 6 | } -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | charset = utf-8 5 | end_of_line = lf 6 | indent_size = 4 7 | indent_style = space 8 | insert_final_newline = true 9 | max_line_length = 120 10 | tab_width = 4 11 | trim_trailing_whitespace = true 12 | 13 | [{*.json,*.xml,*.yml,*.html}] 14 | indent_size = 2 15 | -------------------------------------------------------------------------------- /Sophon/Structs/SophonChunk.cs: -------------------------------------------------------------------------------- 1 | namespace Sophon.Structs 2 | { 3 | public struct SophonChunk 4 | { 5 | public SophonChunk() 6 | { 7 | ChunkOldOffset = -1; 8 | } 9 | 10 | public string ChunkName; 11 | public byte[] ChunkHashDecompressed; 12 | public long ChunkOldOffset; 13 | public long ChunkOffset; 14 | public long ChunkSize; 15 | public long ChunkSizeDecompressed; 16 | } 17 | } -------------------------------------------------------------------------------- /Core/config.sample.json: -------------------------------------------------------------------------------- 1 | { 2 | "Region": "OSREL", 3 | "Branch": "main", 4 | "LauncherId": "VYTpXlbWo8", 5 | "PlatApp": "ddxf6vlr1reo", 6 | "Password": "bDL4JUHL625x", 7 | "Threads": 4, 8 | "MaxHttpHandle": 128, 9 | "Silent": false, 10 | "Versions": { 11 | "full": ["5.6", "5.7", "5.8", "6.0", "6.1"], 12 | "update": [ 13 | ["5.5", "5.6"], 14 | ["5.5", "5.7"], 15 | ["5.6", "5.7"], 16 | ["5.6", "5.8"], 17 | ["5.7", "5.8"], 18 | ["5.7", "6.0"], 19 | ["5.8", "6.0"], 20 | ["5.8", "6.1"], 21 | ["6.0", "6.1"] 22 | ] 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Core/Utils/Formatter.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Core.Utils 4 | { 5 | internal class Formatter 6 | { 7 | private static readonly string[] SizeSuffixes = { "B", "KB", "MB", "GB" }; 8 | 9 | public static string FormatSize(double value, int decimalPlaces = 2) 10 | { 11 | if (value < 0) return "-" + FormatSize(-value); 12 | if (value == 0) return "0 B"; 13 | 14 | int mag = Math.Min(SizeSuffixes.Length - 1, (int)Math.Log(value, 1024)); 15 | double adjustedSize = value / Math.Pow(1024, mag); 16 | 17 | return $"{Math.Round(adjustedSize, decimalPlaces)} {SizeSuffixes[mag]}"; 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Sophon/Sophon.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net9.0 5 | x64 6 | 2.0.3 7 | GesthosNetwork © 2025 All rights reserved. 8 | true 9 | NOSTREAMLOCK 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /Core/Runner/AppRunner.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection; 3 | using System.Threading.Tasks; 4 | 5 | namespace Core.Runner 6 | { 7 | public static class AppRunner 8 | { 9 | public static async Task Run(string[] args) 10 | { 11 | Core.Utils.WindowUtils.CenterConsole(); 12 | 13 | if (args.Length > 0) 14 | CliHandler.ParseArgsAndSetConfig(args); 15 | 16 | var version = Assembly 17 | .GetExecutingAssembly() 18 | .GetCustomAttribute()? 19 | .InformationalVersion ?? "unknown"; 20 | 21 | Console.Title = $"HK4E Sophon Downloader v{version}"; 22 | 23 | if (args.Length == 0) 24 | return await InteractiveMenu.RunInteractiveMenu(); 25 | 26 | return await CliHandler.RunWithArgs(args); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Core/Game.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace Core 8 | { 9 | class Game 10 | { 11 | public string GameId { get; set; } = ""; 12 | 13 | public enum GameType 14 | { 15 | hk4e, 16 | } 17 | 18 | static readonly Dictionary<(Region, GameType), string> gameMap = new() 19 | { 20 | {(Region.OSREL, GameType.hk4e), "gopR6Cufr3"}, 21 | {(Region.CNREL, GameType.hk4e), "1Z8W5NHUQb"}, 22 | }; 23 | 24 | public Game(Region region, string id) 25 | { 26 | bool isRel = !Enum.TryParse(id, out GameType game); 27 | if (isRel) 28 | { 29 | this.GameId = id; 30 | } else 31 | { 32 | this.GameId = gameMap[(region, game)]; 33 | } 34 | } 35 | 36 | public string GetGameId() 37 | { 38 | return this.GameId; 39 | } 40 | } 41 | } -------------------------------------------------------------------------------- /Sophon/Helper/Converter.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Text.Json; 3 | using System.Text.Json.Serialization; 4 | 5 | namespace Sophon.Helper 6 | { 7 | public class BoolConverter : JsonConverter 8 | { 9 | public override void Write(Utf8JsonWriter writer, bool value, JsonSerializerOptions options) 10 | { 11 | writer.WriteBooleanValue(value); 12 | } 13 | 14 | public override bool Read(ref Utf8JsonReader reader, Type type, JsonSerializerOptions options) 15 | { 16 | return reader.TokenType switch 17 | { 18 | JsonTokenType.True => true, 19 | JsonTokenType.False => false, 20 | JsonTokenType.String => bool.TryParse(reader.GetString(), out bool boolFromString) 21 | ? boolFromString 22 | : throw new JsonException(), 23 | 24 | JsonTokenType.Number => reader.TryGetInt64(out long boolFromNumber) 25 | ? Convert.ToBoolean(boolFromNumber) 26 | : reader.TryGetDouble(out double boolFromDouble) && Convert.ToBoolean(boolFromDouble), 27 | 28 | _ => throw new JsonException() 29 | }; 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Core/Utils/WindowUtils.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Runtime.InteropServices; 3 | 4 | namespace Core.Utils 5 | { 6 | public static class WindowUtils 7 | { 8 | [DllImport("kernel32.dll", SetLastError = true)] 9 | private static extern IntPtr GetConsoleWindow(); 10 | 11 | [DllImport("user32.dll", SetLastError = true)] 12 | private static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect); 13 | 14 | [DllImport("user32.dll", SetLastError = true)] 15 | private static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint); 16 | 17 | [DllImport("user32.dll")] 18 | private static extern int GetSystemMetrics(int nIndex); 19 | 20 | private struct RECT 21 | { 22 | public int Left, Top, Right, Bottom; 23 | } 24 | 25 | public static void CenterConsole() 26 | { 27 | IntPtr hwnd = GetConsoleWindow(); 28 | if (hwnd == IntPtr.Zero || !GetWindowRect(hwnd, out RECT r)) return; 29 | 30 | int width = r.Right - r.Left; 31 | int height = r.Bottom - r.Top; 32 | int left = (GetSystemMetrics(0) - width) / 2; 33 | int top = (GetSystemMetrics(1) - height) / 2; 34 | 35 | MoveWindow(hwnd, left, top, width, height, true); 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /Sophon/Structs/SophonManifestInfo.cs: -------------------------------------------------------------------------------- 1 | using Sophon.Infos; 2 | 3 | namespace Sophon.Infos 4 | { 5 | public class SophonManifestInfo 6 | { 7 | public string ManifestBaseUrl { get; internal set; } 8 | public string ManifestId { get; internal set; } 9 | public string ManifestChecksumMd5 { get; internal set; } 10 | public bool IsUseCompression { get; internal set; } 11 | public long ManifestSize { get; internal set; } 12 | public long ManifestCompressedSize { get; internal set; } 13 | 14 | public string ManifestFileUrl => $"{ManifestBaseUrl.TrimEnd('/')}/{ManifestId}"; 15 | } 16 | } 17 | 18 | namespace Sophon 19 | { 20 | public static partial class SophonManifest 21 | { 22 | public static SophonManifestInfo CreateManifestInfo( 23 | string manifestBaseUrl, 24 | string manifestChecksumMd5, 25 | string manifestId, 26 | bool isUseCompression, 27 | long manifestSize, 28 | long manifestCompressedSize = 0) 29 | { 30 | return new SophonManifestInfo 31 | { 32 | ManifestBaseUrl = manifestBaseUrl, 33 | ManifestChecksumMd5 = manifestChecksumMd5, 34 | ManifestId = manifestId, 35 | IsUseCompression = isUseCompression, 36 | ManifestSize = manifestSize, 37 | ManifestCompressedSize = manifestCompressedSize 38 | }; 39 | } 40 | } 41 | } -------------------------------------------------------------------------------- /Sophon/SophonDownloadSpeedLimiter.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading; 3 | 4 | namespace Sophon 5 | { 6 | public class SophonDownloadSpeedLimiter 7 | { 8 | internal event EventHandler CurrentChunkProcessingChangedEvent; 9 | internal event EventHandler DownloadSpeedChangedEvent; 10 | 11 | internal long? InitialRequestedSpeed { get; set; } 12 | private EventHandler _innerListener; 13 | internal int CurrentChunkProcessing; 14 | 15 | private SophonDownloadSpeedLimiter(long initialRequestedSpeed) 16 | { 17 | InitialRequestedSpeed = initialRequestedSpeed; 18 | } 19 | 20 | public static SophonDownloadSpeedLimiter CreateInstance(long initialSpeed) => new(initialSpeed); 21 | 22 | public EventHandler GetListener() => _innerListener ??= OnDownloadSpeedChanged; 23 | 24 | private void OnDownloadSpeedChanged(object sender, long newSpeed) 25 | { 26 | InitialRequestedSpeed = newSpeed; 27 | DownloadSpeedChangedEvent?.Invoke(this, newSpeed); 28 | } 29 | 30 | internal void IncrementChunkProcessedCount() 31 | { 32 | int newCount = Interlocked.Increment(ref CurrentChunkProcessing); 33 | CurrentChunkProcessingChangedEvent?.Invoke(this, newCount); 34 | } 35 | 36 | internal void DecrementChunkProcessedCount() 37 | { 38 | int newCount = Interlocked.Decrement(ref CurrentChunkProcessing); 39 | CurrentChunkProcessingChangedEvent?.Invoke(this, newCount); 40 | } 41 | } 42 | } -------------------------------------------------------------------------------- /Sophon/Helper/Logger.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Sophon.Helper 4 | { 5 | public enum LogLevel 6 | { 7 | Info, 8 | Warning, 9 | Error, 10 | Debug 11 | } 12 | 13 | public static class Logger 14 | { 15 | public static event EventHandler LogHandler; 16 | 17 | internal static void PushLogDebug(this object obj, string message) 18 | { 19 | LogHandler?.Invoke(obj, new LogStruct 20 | { 21 | LogLevel = LogLevel.Debug, 22 | Message = message 23 | }); 24 | } 25 | 26 | internal static void PushLogInfo(this object obj, string message) 27 | { 28 | LogHandler?.Invoke(obj, new LogStruct 29 | { 30 | LogLevel = LogLevel.Info, 31 | Message = message 32 | }); 33 | } 34 | 35 | internal static void PushLogWarning(this object obj, string message) 36 | { 37 | LogHandler?.Invoke(obj, new LogStruct 38 | { 39 | LogLevel = LogLevel.Warning, 40 | Message = message 41 | }); 42 | } 43 | 44 | internal static void PushLogError(this object obj, string message) 45 | { 46 | LogHandler?.Invoke(obj, new LogStruct 47 | { 48 | LogLevel = LogLevel.Error, 49 | Message = message 50 | }); 51 | } 52 | } 53 | 54 | public struct LogStruct 55 | { 56 | public LogLevel LogLevel; 57 | public string Message; 58 | } 59 | } -------------------------------------------------------------------------------- /Core/Core.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Sophon.Downloader 5 | Exe 6 | net9.0 7 | x64 8 | win-x64 9 | 2.0.4 10 | GesthosNetwork © 2025 All rights reserved. 11 | true 12 | true 13 | false 14 | ..\bin\ 15 | none 16 | enable 17 | enable 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 41 | 42 | 43 | 44 | 45 | 46 | 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /Sophon/Structs/SophonChunksInfo.cs: -------------------------------------------------------------------------------- 1 | using Sophon.Infos; 2 | using System; 3 | 4 | namespace Sophon.Infos 5 | { 6 | public class SophonChunksInfo : IEquatable 7 | { 8 | public string ChunksBaseUrl { get; set; } 9 | public int ChunksCount { get; set; } 10 | public int FilesCount { get; set; } 11 | public long TotalSize { get; set; } 12 | public long TotalCompressedSize { get; set; } 13 | public bool IsUseCompression { get; set; } 14 | 15 | public bool Equals(SophonChunksInfo other) => 16 | other != null && 17 | ChunksBaseUrl == other.ChunksBaseUrl && 18 | ChunksCount == other.ChunksCount && 19 | FilesCount == other.FilesCount && 20 | TotalSize == other.TotalSize && 21 | TotalCompressedSize == other.TotalCompressedSize && 22 | IsUseCompression == other.IsUseCompression; 23 | 24 | public override bool Equals(object obj) => 25 | obj is SophonChunksInfo other && Equals(other); 26 | 27 | public override int GetHashCode() => 28 | HashCode.Combine( 29 | ChunksBaseUrl, 30 | ChunksCount, 31 | FilesCount, 32 | TotalSize, 33 | TotalCompressedSize, 34 | IsUseCompression 35 | ); 36 | 37 | public SophonChunksInfo CopyWithNewBaseUrl(string newBaseUrl) => new() 38 | { 39 | ChunksBaseUrl = newBaseUrl, 40 | ChunksCount = ChunksCount, 41 | FilesCount = FilesCount, 42 | TotalSize = TotalSize, 43 | TotalCompressedSize = TotalCompressedSize, 44 | IsUseCompression = IsUseCompression 45 | }; 46 | } 47 | } 48 | 49 | namespace Sophon 50 | { 51 | public static partial class SophonManifest 52 | { 53 | public static SophonChunksInfo CreateChunksInfo( 54 | string chunksBaseUrl, 55 | int chunksCount, 56 | int filesCount, 57 | bool isUseCompression, 58 | long totalSize, 59 | long totalCompressedSize = 0) 60 | { 61 | return new SophonChunksInfo 62 | { 63 | ChunksBaseUrl = chunksBaseUrl, 64 | ChunksCount = chunksCount, 65 | FilesCount = filesCount, 66 | IsUseCompression = isUseCompression, 67 | TotalSize = totalSize, 68 | TotalCompressedSize = totalCompressedSize 69 | }; 70 | } 71 | } 72 | } -------------------------------------------------------------------------------- /Core/Runner/DownloadExecutor.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using System.Text; 4 | using System.Threading.Tasks; 5 | using System.Net.Http; 6 | 7 | namespace Core.Runner 8 | { 9 | public static class DownloadExecutor 10 | { 11 | public static async Task RunDownload(string[] args) 12 | { 13 | string action = args[0]; 14 | string gameId = args[1]; 15 | string matchingField = args[2]; 16 | string updateFrom = args[3]; 17 | string updateTo = args.Length >= 6 ? args[4] : ""; 18 | string outputDir = args[^1]; 19 | 20 | if (!AppConfig.Config.Silent) 21 | { 22 | string encoded = "SEs0RSBTb3Bob24gRG93bmxvYWRlciBDb3B5cmlnaHQgKEMpIDIwMjUgR2VzdGhvc05ldHdvcms="; 23 | Console.WriteLine(Encoding.UTF8.GetString(Convert.FromBase64String(encoded))); 24 | } 25 | 26 | Enum.TryParse(AppConfig.Config.Region, out Region region); 27 | BranchType branch = Enum.Parse(AppConfig.Config.Branch, true); 28 | Game game = new(region, gameId); 29 | 30 | SophonUrl urlPrev = new(region, game.GetGameId(), BranchType.Main, AppConfig.Config.LauncherId, AppConfig.Config.PlatApp); 31 | SophonUrl urlNew = new(region, game.GetGameId(), branch, AppConfig.Config.LauncherId, AppConfig.Config.PlatApp); 32 | 33 | if (updateFrom.Count(c => c == '.') == 1) updateFrom += ".0"; 34 | if (!string.IsNullOrWhiteSpace(updateTo) && updateTo.Count(c => c == '.') == 1) updateTo += ".0"; 35 | 36 | if (!AppConfig.Config.Silent) 37 | Console.WriteLine("[INFO] Initializing region, branch, and game info..."); 38 | 39 | try 40 | { 41 | await urlPrev.GetBuildData(); 42 | await urlNew.GetBuildData(); 43 | } 44 | catch (HttpRequestException) 45 | { 46 | Console.ForegroundColor = ConsoleColor.Red; 47 | Console.WriteLine("[ERROR] Unable to connect to the internet."); 48 | Console.ResetColor(); 49 | Console.WriteLine("Press any key to exit..."); 50 | Console.ReadKey(); 51 | return; 52 | } 53 | catch (Exception ex) 54 | { 55 | Console.ForegroundColor = ConsoleColor.Red; 56 | Console.WriteLine($"[ERROR] Unexpected error: {ex.Message}"); 57 | Console.ResetColor(); 58 | Console.WriteLine("Press any key to exit..."); 59 | Console.ReadKey(); 60 | return; 61 | } 62 | 63 | string prevManifest = urlPrev.GetBuildUrl(updateFrom, false); 64 | string newManifest = action == "update" ? urlNew.GetBuildUrl(updateTo, true) : ""; 65 | 66 | if (!AppConfig.Config.Silent) 67 | { 68 | Console.WriteLine(action == "update" 69 | ? $"[INFO] update mode:\nprev = {prevManifest}\nnew = {newManifest}" 70 | : $"[INFO] full mode: manifest = {prevManifest}"); 71 | } 72 | 73 | await Downloader.StartDownload(prevManifest, newManifest, outputDir, matchingField); 74 | } 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /Sophon/Structs/SophonPatchBranch.cs: -------------------------------------------------------------------------------- 1 | using System.Linq; 2 | using System.Net.Http; 3 | using System.Threading; 4 | using System.Threading.Tasks; 5 | using Sophon.Structs; 6 | 7 | namespace Sophon 8 | { 9 | public partial class SophonPatch 10 | { 11 | public static async Task CreateSophonChunkManifestInfoPair( 12 | HttpClient client, 13 | string url, 14 | string versionUpdateFrom, 15 | string matchingField, 16 | CancellationToken token = default) 17 | { 18 | if (string.IsNullOrEmpty(matchingField)) 19 | { 20 | matchingField = "game"; 21 | } 22 | 23 | var patchBranch = await SophonManifest.GetSophonBranchInfo( 24 | client, 25 | url, 26 | SophonContext.Default.SophonManifestPatchBranch, 27 | HttpMethod.Post, 28 | token 29 | ); 30 | 31 | if (patchBranch.Data == null) 32 | { 33 | return new SophonChunkManifestInfoPair 34 | { 35 | IsFound = false, 36 | ReturnCode = patchBranch.ReturnCode, 37 | ReturnMessage = patchBranch.ReturnMessage 38 | }; 39 | } 40 | 41 | var patchIdentity = patchBranch.Data.ManifestIdentityList? 42 | .FirstOrDefault(x => x.MatchingField == matchingField); 43 | 44 | if (patchIdentity == null) 45 | { 46 | return new SophonChunkManifestInfoPair 47 | { 48 | IsFound = false, 49 | ReturnCode = 404, 50 | ReturnMessage = $"Sophon patch with matching field: {matchingField} is not found!" 51 | }; 52 | } 53 | 54 | if (!patchIdentity.DiffTaggedInfo.TryGetValue(versionUpdateFrom, out var chunkInfo)) 55 | { 56 | return new SophonChunkManifestInfoPair 57 | { 58 | IsFound = false, 59 | ReturnCode = 404, 60 | ReturnMessage = $"Sophon patch diff tagged info with version: {versionUpdateFrom} is not found!" 61 | }; 62 | } 63 | 64 | var diffUrlInfo = patchIdentity.DiffUrlInfo; 65 | var manifestUrlInfo = patchIdentity.ManifestUrlInfo; 66 | var manifestFileInfo = patchIdentity.ManifestFileInfo; 67 | 68 | var chunksInfo = SophonManifest.CreateChunksInfo( 69 | diffUrlInfo.UrlPrefix, 70 | chunkInfo.ChunkCount, 71 | chunkInfo.FileCount, 72 | diffUrlInfo.IsCompressed, 73 | chunkInfo.UncompressedSize, 74 | chunkInfo.CompressedSize 75 | ); 76 | 77 | var manifestInfo = SophonManifest.CreateManifestInfo( 78 | manifestUrlInfo.UrlPrefix, 79 | manifestFileInfo.Checksum, 80 | manifestFileInfo.FileName, 81 | manifestUrlInfo.IsCompressed, 82 | manifestFileInfo.UncompressedSize, 83 | manifestFileInfo.CompressedSize 84 | ); 85 | 86 | return new SophonChunkManifestInfoPair 87 | { 88 | ChunksInfo = chunksInfo, 89 | ManifestInfo = manifestInfo, 90 | OtherSophonBuildData = null, 91 | OtherSophonPatchData = patchBranch.Data 92 | }; 93 | } 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /Sophon/Helper/TaskExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading; 3 | using System.Threading.Tasks; 4 | 5 | namespace Sophon.Helper 6 | { 7 | internal delegate Task ActionTimeoutTaskCallback(CancellationToken token); 8 | internal delegate void ActionOnTimeOutRetry(int retryAttemptCount, int retryAttemptTotal, int timeOutSecond, int timeOutStep); 9 | 10 | internal static class TaskExtensions 11 | { 12 | internal const int DefaultTimeoutSec = 20; 13 | internal const int DefaultRetryAttempt = 10; 14 | 15 | internal static async Task WaitForRetryAsync( 16 | Func> funcCallback, 17 | int? timeout = null, 18 | int? timeoutStep = null, 19 | int? retryAttempt = null, 20 | ActionOnTimeOutRetry actionOnRetry = null, 21 | CancellationToken fromToken = default) 22 | { 23 | timeout ??= DefaultTimeoutSec; 24 | timeoutStep ??= 0; 25 | retryAttempt ??= DefaultRetryAttempt; 26 | 27 | int retryAttemptCurrent = 1; 28 | Exception lastException = null; 29 | 30 | while (retryAttemptCurrent < retryAttempt) 31 | { 32 | fromToken.ThrowIfCancellationRequested(); 33 | CancellationTokenSource innerCancellationToken = null; 34 | CancellationTokenSource linkedToken = null; 35 | 36 | try 37 | { 38 | innerCancellationToken = new CancellationTokenSource(TimeSpan.FromSeconds(timeout.Value)); 39 | linkedToken = CancellationTokenSource.CreateLinkedTokenSource(innerCancellationToken.Token, fromToken); 40 | 41 | var callback = funcCallback(); 42 | return await callback(linkedToken.Token); 43 | } 44 | catch (TaskCanceledException) 45 | { 46 | throw; 47 | } 48 | catch (OperationCanceledException) 49 | { 50 | throw; 51 | } 52 | catch (Exception ex) 53 | { 54 | lastException = ex; 55 | actionOnRetry?.Invoke(retryAttemptCurrent, retryAttempt.Value, timeout.Value, timeoutStep.Value); 56 | 57 | if (ex is TimeoutException) 58 | { 59 | Logger.PushLogWarning(null, $"The operation has timed out! Retrying attempt {retryAttemptCurrent}/{retryAttempt}"); 60 | } 61 | else 62 | { 63 | Logger.PushLogError(null, $"The operation has thrown an exception! Retrying attempt {retryAttemptCurrent}/{retryAttempt}\r\n{ex}"); 64 | } 65 | 66 | retryAttemptCurrent++; 67 | timeout += timeoutStep; 68 | } 69 | finally 70 | { 71 | innerCancellationToken?.Dispose(); 72 | linkedToken?.Dispose(); 73 | } 74 | } 75 | 76 | if (lastException != null && !fromToken.IsCancellationRequested) 77 | { 78 | throw lastException is TaskCanceledException 79 | ? new TimeoutException("The operation has timed out with inner exception!", lastException) 80 | : lastException; 81 | } 82 | 83 | throw new TimeoutException("The operation has timed out!"); 84 | } 85 | } 86 | } -------------------------------------------------------------------------------- /Core/Assets.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Net.Http; 4 | using System.Threading; 5 | using System.Threading.Tasks; 6 | using Sophon; 7 | using Sophon.Structs; 8 | 9 | namespace Core 10 | { 11 | internal class Assets 12 | { 13 | public static async Task?, long>> GetAssetsFromManifests( 14 | HttpClient httpClient, 15 | string matchingField, 16 | string prevManifestUrl, 17 | string newManifestUrl, 18 | CancellationTokenSource tokenSource) 19 | { 20 | var assets = new List(); 21 | long updateSize = 0; 22 | 23 | SophonChunkManifestInfoPair? manifestFrom = null; 24 | SophonChunkManifestInfoPair? manifestTo = null; 25 | 26 | try 27 | { 28 | manifestFrom = await SophonManifest.CreateSophonChunkManifestInfoPair( 29 | httpClient, prevManifestUrl, matchingField, tokenSource.Token); 30 | 31 | if (!string.IsNullOrEmpty(newManifestUrl)) 32 | { 33 | manifestTo = await SophonManifest.CreateSophonChunkManifestInfoPair( 34 | httpClient, newManifestUrl, matchingField, tokenSource.Token); 35 | } 36 | 37 | if (manifestFrom?.ManifestInfo == null || manifestFrom?.ChunksInfo == null || 38 | (!string.IsNullOrEmpty(newManifestUrl) && 39 | (manifestTo?.ManifestInfo == null || manifestTo?.ChunksInfo == null))) 40 | { 41 | return Tuple.Create?, long>(null, 0); 42 | } 43 | } 44 | catch 45 | { 46 | return Tuple.Create?, long>(null, 0); 47 | } 48 | 49 | try 50 | { 51 | if (!string.IsNullOrEmpty(newManifestUrl)) 52 | { 53 | await foreach (var asset in SophonUpdate.EnumerateUpdateAsync( 54 | httpClient, manifestFrom!, manifestTo!, true, null, tokenSource.Token)) 55 | { 56 | ProcessUpdateAsset(asset, ref updateSize, ref assets); 57 | } 58 | } 59 | else 60 | { 61 | await foreach (var asset in SophonManifest.EnumerateAsync( 62 | httpClient, manifestFrom!, null, tokenSource.Token)) 63 | { 64 | ProcessAsset(asset, ref updateSize, ref assets); 65 | } 66 | } 67 | } 68 | catch 69 | { 70 | return Tuple.Create?, long>(null, 0); 71 | } 72 | 73 | return Tuple.Create?, long>(assets, updateSize); 74 | } 75 | 76 | private static void ProcessAsset(SophonAsset asset, ref long updateSize, ref List assets) 77 | { 78 | if (!asset.IsDirectory) 79 | { 80 | updateSize += asset.AssetSize; 81 | assets.Add(asset); 82 | } 83 | } 84 | 85 | private static void ProcessUpdateAsset(SophonAsset asset, ref long updateSize, ref List assets) 86 | { 87 | if (asset.IsDirectory) return; 88 | 89 | foreach (var chunk in asset.Chunks) 90 | { 91 | if (chunk.ChunkOldOffset == -1) 92 | { 93 | updateSize += asset.AssetSize; 94 | assets.Add(asset); 95 | break; 96 | } 97 | } 98 | } 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /Sophon/Structs/SophonChunksBranch.cs: -------------------------------------------------------------------------------- 1 | using Sophon.Structs; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.IO; 5 | using System.Linq; 6 | using System.Net.Http; 7 | using System.Text.Json; 8 | using System.Text.Json.Serialization.Metadata; 9 | using System.Threading; 10 | using System.Threading.Tasks; 11 | 12 | namespace Sophon 13 | { 14 | public static partial class SophonManifest 15 | { 16 | public static async Task GetSophonBranchInfo( 17 | HttpClient client, 18 | string url, 19 | JsonTypeInfo jsonTypeInfo, 20 | HttpMethod httpMethod, 21 | CancellationToken token = default) 22 | { 23 | using var requestMessage = new HttpRequestMessage(httpMethod, url); 24 | using var responseMessage = await client.SendAsync(requestMessage, HttpCompletionOption.ResponseHeadersRead, token); 25 | responseMessage.EnsureSuccessStatusCode(); 26 | 27 | await using var responseStream = await responseMessage.Content.ReadAsStreamAsync(token); 28 | return await JsonSerializer.DeserializeAsync(responseStream, jsonTypeInfo, token); 29 | } 30 | 31 | public static async Task CreateSophonChunkManifestInfoPair( 32 | HttpClient client, 33 | string url, 34 | string matchingField = null, 35 | CancellationToken token = default) 36 | { 37 | var sophonBranch = await GetSophonBranchInfo( 38 | client, 39 | url, 40 | SophonContext.Default.SophonManifestBuildBranch, 41 | HttpMethod.Get, 42 | token 43 | ); 44 | 45 | if (sophonBranch.Data == null) 46 | { 47 | return new SophonChunkManifestInfoPair 48 | { 49 | IsFound = false, 50 | ReturnCode = sophonBranch.ReturnCode, 51 | ReturnMessage = sophonBranch.ReturnMessage 52 | }; 53 | } 54 | 55 | matchingField ??= "game"; 56 | 57 | var sophonManifestIdentity = sophonBranch.Data.ManifestIdentityList? 58 | .FirstOrDefault(x => x.MatchingField == matchingField); 59 | 60 | if (sophonManifestIdentity == null) 61 | throw new KeyNotFoundException($"Sophon manifest with matching field: {matchingField} is not found!"); 62 | 63 | return new SophonChunkManifestInfoPair 64 | { 65 | ChunksInfo = (sophonManifestIdentity.ChunkInfo != null && sophonManifestIdentity.ChunksUrlInfo != null) 66 | ? CreateChunksInfo( 67 | sophonManifestIdentity.ChunksUrlInfo.UrlPrefix, 68 | sophonManifestIdentity.ChunkInfo.ChunkCount, 69 | sophonManifestIdentity.ChunkInfo.FileCount, 70 | sophonManifestIdentity.ChunksUrlInfo.IsCompressed, 71 | sophonManifestIdentity.ChunkInfo.UncompressedSize, 72 | sophonManifestIdentity.ChunkInfo.CompressedSize) 73 | : null, 74 | 75 | ManifestInfo = (sophonManifestIdentity.ManifestFileInfo != null && sophonManifestIdentity.ManifestUrlInfo != null) 76 | ? CreateManifestInfo( 77 | sophonManifestIdentity.ManifestUrlInfo.UrlPrefix, 78 | sophonManifestIdentity.ManifestFileInfo.Checksum, 79 | sophonManifestIdentity.ManifestFileInfo.FileName, 80 | sophonManifestIdentity.ManifestUrlInfo.IsCompressed, 81 | sophonManifestIdentity.ManifestFileInfo.UncompressedSize, 82 | sophonManifestIdentity.ManifestFileInfo.CompressedSize) 83 | : null, 84 | 85 | OtherSophonBuildData = sophonBranch.Data 86 | }; 87 | } 88 | } 89 | } -------------------------------------------------------------------------------- /Sophon/SophonManifest.cs: -------------------------------------------------------------------------------- 1 | using Sophon.Helper; 2 | using Sophon.Infos; 3 | using Sophon.Protos; 4 | using Sophon.Structs; 5 | using System; 6 | using System.Collections.Generic; 7 | using System.Linq; 8 | using System.Net.Http; 9 | using System.Runtime.CompilerServices; 10 | using System.Threading; 11 | using System.Threading.Tasks; 12 | using TaskExtensions = Sophon.Helper.TaskExtensions; 13 | using ZstdNet; 14 | 15 | namespace Sophon 16 | { 17 | public static partial class SophonManifest 18 | { 19 | public static async IAsyncEnumerable EnumerateAsync( 20 | HttpClient httpClient, 21 | SophonChunkManifestInfoPair infoPair, 22 | SophonDownloadSpeedLimiter downloadSpeedLimiter = null, 23 | [EnumeratorCancellation] CancellationToken token = default) 24 | { 25 | await foreach (var asset in EnumerateAsync( 26 | httpClient, 27 | infoPair.ManifestInfo, 28 | infoPair.ChunksInfo, 29 | downloadSpeedLimiter).WithCancellation(token)) 30 | { 31 | yield return asset; 32 | } 33 | } 34 | 35 | public static async IAsyncEnumerable EnumerateAsync( 36 | HttpClient httpClient, 37 | SophonManifestInfo manifestInfo, 38 | SophonChunksInfo chunksInfo, 39 | SophonDownloadSpeedLimiter downloadSpeedLimiter = null, 40 | [EnumeratorCancellation] CancellationToken token = default) 41 | { 42 | if (!DllUtils.IsLibraryExist(DllUtils.DllName)) 43 | throw new DllNotFoundException("libzstd is not found!"); 44 | 45 | var manifestProtoTaskCallback = new ActionTimeoutTaskCallback( 46 | async innerToken => 47 | await httpClient.ReadProtoFromManifestInfo(manifestInfo, SophonManifestProto.Parser, innerToken) 48 | ); 49 | 50 | var manifestProto = await TaskExtensions.WaitForRetryAsync( 51 | () => manifestProtoTaskCallback, 52 | TaskExtensions.DefaultTimeoutSec, 53 | null, 54 | null, 55 | null, 56 | token 57 | ); 58 | 59 | foreach (var asset in manifestProto.Assets) 60 | { 61 | yield return AssetProperty2SophonAsset(asset, chunksInfo, downloadSpeedLimiter); 62 | } 63 | } 64 | 65 | internal static SophonAsset AssetProperty2SophonAsset( 66 | SophonManifestAssetProperty asset, 67 | SophonChunksInfo chunksInfo, 68 | SophonDownloadSpeedLimiter downloadSpeedLimiter) 69 | { 70 | if (asset.AssetType != 0 || string.IsNullOrEmpty(asset.AssetHashMd5)) 71 | { 72 | return new SophonAsset 73 | { 74 | AssetName = asset.AssetName, 75 | IsDirectory = true, 76 | DownloadSpeedLimiter = downloadSpeedLimiter 77 | }; 78 | } 79 | 80 | var chunks = asset.AssetChunks.Select(x => new SophonChunk 81 | { 82 | ChunkName = x.ChunkName, 83 | ChunkHashDecompressed = Extension.HexToBytes(x.ChunkDecompressedHashMd5.AsSpan()), 84 | ChunkOffset = x.ChunkOnFileOffset, 85 | ChunkSize = x.ChunkSize, 86 | ChunkSizeDecompressed = x.ChunkSizeDecompressed 87 | }).ToArray(); 88 | 89 | return new SophonAsset 90 | { 91 | AssetName = asset.AssetName, 92 | AssetHash = asset.AssetHashMd5, 93 | AssetSize = asset.AssetSize, 94 | Chunks = chunks, 95 | SophonChunksInfo = chunksInfo, 96 | IsDirectory = false, 97 | DownloadSpeedLimiter = downloadSpeedLimiter 98 | }; 99 | } 100 | } 101 | } -------------------------------------------------------------------------------- /Core/Runner/InteractiveMenu.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using System.Threading.Tasks; 4 | 5 | namespace Core.Runner 6 | { 7 | public static class InteractiveMenu 8 | { 9 | public static async Task RunInteractiveMenu() 10 | { 11 | while (true) 12 | { 13 | Console.Clear(); 14 | Console.WriteLine("=== Sophon Downloader ===\n"); 15 | Console.WriteLine("[1] Full Download"); 16 | Console.WriteLine("[2] Update Download"); 17 | Console.WriteLine("[0] Exit"); 18 | Console.Write("\nChoose: "); 19 | 20 | string input = Console.ReadLine()?.Trim() ?? ""; 21 | if (input == "0") return 0; 22 | if (input == "1") await RunDownloadCategoryMenu("full"); 23 | else if (input == "2") await RunDownloadCategoryMenu("update"); 24 | } 25 | } 26 | 27 | private static async Task RunDownloadCategoryMenu(string mode) 28 | { 29 | string[] langs = AppConfig.Config.Region == "CNREL" 30 | ? new[] { "game", "zh-cn" } 31 | : new[] { "game", "en-us", "ja-jp", "zh-cn", "ko-kr" }; 32 | 33 | while (true) 34 | { 35 | Console.Clear(); 36 | Console.WriteLine($"=== {(mode == "full" ? "Full" : "Update")} Download ===\n"); 37 | 38 | for (int i = 0; i < langs.Length; i++) 39 | Console.WriteLine($"[{i + 1}] {langs[i]}"); 40 | 41 | Console.WriteLine("[0] Back"); 42 | Console.Write("\nChoose: "); 43 | 44 | string input = Console.ReadLine()?.Trim() ?? ""; 45 | if (input == "0") return; 46 | 47 | if (int.TryParse(input, out int c) && c >= 1 && c <= langs.Length) 48 | await RunVersionPickerMenu(mode, langs[c - 1]); 49 | } 50 | } 51 | 52 | private static async Task RunVersionPickerMenu(string mode, string lang) 53 | { 54 | string[][] versions = mode == "full" 55 | ? AppConfig.Config.Versions.Full.Select(v => new[] { v }).ToArray() 56 | : AppConfig.Config.Versions.Update.Select(x => x.ToArray()).ToArray(); 57 | 58 | Region region = Enum.TryParse(AppConfig.Config.Region, out Region parsedRegion) 59 | ? parsedRegion : Region.OSREL; 60 | 61 | string gameId = new Game(region, Game.GameType.hk4e.ToString()).GetGameId(); 62 | 63 | while (true) 64 | { 65 | Console.Clear(); 66 | Console.WriteLine($"=== {(mode == "full" ? "Full" : "Update")} Download: {lang} ===\n"); 67 | 68 | for (int i = 0; i < versions.Length; i++) 69 | { 70 | var v = versions[i]; 71 | string label = mode == "full" ? $"Version {v[0]}" : $"From {v[0]} → {v[1]}"; 72 | Console.WriteLine($"[{i + 1}] {label}"); 73 | } 74 | 75 | Console.WriteLine("[0] Back"); 76 | Console.Write("\nChoose: "); 77 | 78 | string input = Console.ReadLine()?.Trim() ?? ""; 79 | if (input == "0") return; 80 | 81 | if (int.TryParse(input, out int choice) && choice >= 1 && choice <= versions.Length) 82 | { 83 | string[] ver = versions[choice - 1]; 84 | string[] argsToRun = mode == "full" 85 | ? new[] { "full", gameId, lang, ver[0], "Downloads" } 86 | : new[] { "update", gameId, lang, ver[0], ver[1], "Downloads" }; 87 | 88 | Console.Clear(); 89 | Console.WriteLine($"Executing:\nSophon.Downloader.exe {string.Join(" ", argsToRun)}\n"); 90 | 91 | await DownloadExecutor.RunDownload(argsToRun); 92 | Console.WriteLine("\nPress any key to return..."); 93 | Console.ReadKey(); 94 | } 95 | } 96 | } 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## HK4E Sophon Downloader 2 | 3 | A tool to download anime game assets using their new Sophon-based download system. 4 | 5 | Starting from version `5.6`, they transitioned to using **Sophon Chunks** for updates and discontinued distributing ZIP files. 6 | As a result, it is no longer possible to download game assets **without using their Launcher**. 7 | This tool aims to bypass that limitation, so you can download directly, efficiently, and without bloat. 8 | 9 | 10 | ## Features 11 | 12 | - Full and Update download modes 13 | - Uses official API (`getBuild`, `getUrl`, etc.) 14 | - Language/region selector 15 | - Built-in auto validation via real-time API 16 | - Fast, parallel downloads (multi-threaded) 17 | - Zero dependencies 18 | 19 | 20 | ## Requirements 21 | 22 | - Install [.NET 9.0 SDK](https://dotnet.microsoft.com/download/dotnet/9.0) 23 | 24 | 25 | ## Compile Instructions 26 | 27 | To compile the project: 28 | 29 | 1. Just click `compile.bat` 30 | 2. The release output automatically will be in the `bin` folder 31 | 32 | 33 | ## How to Use 34 | 35 | ### Option 1: Interactive Menu (Recommended) 36 | 37 | Just click Sophon.Downloader.exe 38 | You’ll be greeted with: 39 | 40 | ``` 41 | === Sophon Downloader === 42 | 43 | [1] Full Download 44 | [2] Update Download 45 | [0] Exit 46 | ``` 47 | 48 | Navigate with number keys, follow the prompts, and you're good. 49 | It will auto-detect language options and available versions from your config. 50 | 51 | 52 | ### Option 2: CLI Mode (Advanced Users) 53 | 54 | ```cmd 55 | Sophon.Downloader.exe full [options] 56 | Sophon.Downloader.exe update [options] 57 | ``` 58 | 59 | #### Example: 60 | 61 | ```cmd 62 | Sophon.Downloader.exe full gopR6Cufr3 game 6.0 Downloads 63 | Sophon.Downloader.exe update gopR6Cufr3 en-us 6.0 6.1 Downloads --predownload --OSREL --threads=2 --handles=64 64 | ``` 65 | 66 | 67 | ### CLI Options 68 | 69 | | Option | Description | 70 | |--------------------|---------------------------------------------| 71 | | `--region=...` | `OSREL` or `CNREL` (default: OSREL) | 72 | | `--branch=...` | `main` or `predownload` (default: main) | 73 | | `--launcherId=...` | Launcher ID override | 74 | | `--platApp=...` | Platform App ID override | 75 | | `--threads=...` | Number of threads (auto-limited) | 76 | | `--handles=...` | Max HTTP handles (default 128) | 77 | | `--silent` | Disable all console output except errors | 78 | | `-h`, `--help` | Show help info | 79 | 80 | > If your input is garbage, it will fall back to defaults silently. 81 | > You were warned. 82 | 83 | 84 | ## config.json 85 | 86 | This file is auto-generated if not found. You can customize the default region and add more versions. 87 | 88 | Example: 89 | 90 | ```json 91 | { 92 | "Region": "OSREL", 93 | "Branch": "main", 94 | "LauncherId": "VYTpXlbWo8", 95 | "PlatApp": "ddxf6vlr1reo", 96 | "Password": "bDL4JUHL625x", 97 | "Threads": 4, 98 | "MaxHttpHandle": 128, 99 | "Silent": false, 100 | "Versions": { 101 | "full": ["5.6", "5.7", "5.8", "6.0", "6.1"], 102 | "update": [ 103 | ["5.5", "5.6"], 104 | ["5.5", "5.7"], 105 | ["5.6", "5.7"], 106 | ["5.6", "5.8"], 107 | ["5.7", "5.8"], 108 | ["5.7", "6.0"], 109 | ["5.8", "6.0"], 110 | ["5.8", "6.1"], 111 | ["6.0", "6.1"] 112 | ] 113 | } 114 | } 115 | ``` 116 | 117 | 118 | ## Notes 119 | 120 | - If you mess up the config, the app will silently fallback to default values. 121 | - Garbage values like `"Silent": lmao` or `"Threads": 99999` **Silently fixed automatically.** 122 | - Version/tag values are validated **live via the API**, not by regex. 123 | If your version doesn't exist, you'll get a clean `[ERROR] Failed to fetch manifest` — no crash. 124 | - Maximum thread count = your CPU core count. 125 | 126 | 127 | ## Disclaimer 128 | 129 | This tool is for reverse engineering & educational use only. 130 | Not affiliated with miHoYo, Cognosphere, or any official entity. 131 | Do not use this project for public distribution or commercial purposes. 132 | -------------------------------------------------------------------------------- /Sophon/Structs/SophonManifestInfoPair.cs: -------------------------------------------------------------------------------- 1 | using Sophon.Infos; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | 5 | namespace Sophon.Structs 6 | { 7 | public class SophonChunkManifestInfoPair 8 | { 9 | public SophonChunksInfo ChunksInfo { get; internal set; } 10 | public SophonManifestInfo ManifestInfo { get; internal set; } 11 | public SophonManifestBuildData OtherSophonBuildData { get; internal set; } 12 | public SophonManifestPatchData OtherSophonPatchData { get; internal set; } 13 | public bool IsFound { get; internal set; } = true; 14 | public int ReturnCode { get; internal set; } = 0; 15 | public string ReturnMessage { get; internal set; } 16 | 17 | public SophonChunkManifestInfoPair GetOtherManifestInfoPair(string matchingField) 18 | { 19 | var manifestIdentity = OtherSophonBuildData 20 | .ManifestIdentityList? 21 | .FirstOrDefault(x => x.MatchingField == matchingField); 22 | 23 | if (manifestIdentity == null) 24 | throw new KeyNotFoundException($"Sophon manifest with matching field: {matchingField} is not found!"); 25 | 26 | var chunkInfo = manifestIdentity.ChunkInfo; 27 | var chunkUrlInfo = manifestIdentity.ChunksUrlInfo; 28 | var manifestFileInfo = manifestIdentity.ManifestFileInfo; 29 | var manifestUrlInfo = manifestIdentity.ManifestUrlInfo; 30 | 31 | var chunksInfo = SophonManifest.CreateChunksInfo( 32 | chunkUrlInfo.UrlPrefix, 33 | chunkInfo.ChunkCount, 34 | chunkInfo.FileCount, 35 | chunkUrlInfo.IsCompressed, 36 | chunkInfo.UncompressedSize, 37 | chunkInfo.CompressedSize); 38 | 39 | var manifestInfo = SophonManifest.CreateManifestInfo( 40 | manifestUrlInfo.UrlPrefix, 41 | manifestFileInfo.Checksum, 42 | manifestFileInfo.FileName, 43 | manifestUrlInfo.IsCompressed, 44 | manifestFileInfo.UncompressedSize, 45 | manifestFileInfo.CompressedSize); 46 | 47 | return new SophonChunkManifestInfoPair 48 | { 49 | ChunksInfo = chunksInfo, 50 | ManifestInfo = manifestInfo, 51 | OtherSophonBuildData = OtherSophonBuildData, 52 | OtherSophonPatchData = OtherSophonPatchData 53 | }; 54 | } 55 | 56 | public SophonChunkManifestInfoPair GetOtherPatchInfoPair(string matchingField, string versionUpdateFrom) 57 | { 58 | var patchIdentity = OtherSophonPatchData 59 | .ManifestIdentityList? 60 | .FirstOrDefault(x => x.MatchingField == matchingField); 61 | 62 | if (patchIdentity == null) 63 | throw new KeyNotFoundException($"Sophon patch with matching field: {matchingField} is not found!"); 64 | 65 | if (!patchIdentity.DiffTaggedInfo.TryGetValue(versionUpdateFrom, out var chunkInfo)) 66 | throw new KeyNotFoundException($"Sophon patch diff tagged info with tag: {versionUpdateFrom} is not found!"); 67 | 68 | var diffUrlInfo = patchIdentity.DiffUrlInfo; 69 | var manifestFileInfo = patchIdentity.ManifestFileInfo; 70 | var manifestUrlInfo = patchIdentity.ManifestUrlInfo; 71 | 72 | var chunksInfo = SophonManifest.CreateChunksInfo( 73 | diffUrlInfo.UrlPrefix, 74 | chunkInfo.ChunkCount, 75 | chunkInfo.FileCount, 76 | diffUrlInfo.IsCompressed, 77 | chunkInfo.UncompressedSize, 78 | chunkInfo.CompressedSize); 79 | 80 | var manifestInfo = SophonManifest.CreateManifestInfo( 81 | manifestUrlInfo.UrlPrefix, 82 | manifestFileInfo.Checksum, 83 | manifestFileInfo.FileName, 84 | manifestUrlInfo.IsCompressed, 85 | manifestFileInfo.UncompressedSize, 86 | manifestFileInfo.CompressedSize); 87 | 88 | return new SophonChunkManifestInfoPair 89 | { 90 | ChunksInfo = chunksInfo, 91 | ManifestInfo = manifestInfo, 92 | OtherSophonBuildData = OtherSophonBuildData, 93 | OtherSophonPatchData = OtherSophonPatchData 94 | }; 95 | } 96 | } 97 | } -------------------------------------------------------------------------------- /Sophon/Structs/SophonInfosJson.cs: -------------------------------------------------------------------------------- 1 | using Sophon.Helper; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Text.Json.Serialization; 5 | 6 | namespace Sophon.Structs 7 | { 8 | [JsonSerializable(typeof(SophonManifestBuildBranch))] 9 | [JsonSerializable(typeof(SophonManifestPatchBranch))] 10 | public partial class SophonContext : JsonSerializerContext { } 11 | 12 | public class SophonManifestBuildBranch : SophonManifestReturnedResponse 13 | { 14 | [JsonPropertyName("data")] public SophonManifestBuildData Data { get; set; } 15 | } 16 | 17 | public class SophonManifestBuildData : SophonTaggedResponse 18 | { 19 | [JsonPropertyName("manifests")] public List ManifestIdentityList { get; set; } 20 | } 21 | 22 | public class SophonManifestBuildIdentity 23 | { 24 | [JsonNumberHandling(JsonNumberHandling.AllowReadingFromString)] 25 | [JsonPropertyName("category_id")] public int CategoryId { get; set; } 26 | 27 | [JsonPropertyName("category_name")] public string CategoryName { get; set; } 28 | 29 | [JsonPropertyName("matching_field")] public string MatchingField { get; set; } 30 | 31 | [JsonPropertyName("manifest")] public SophonManifestFileInfo ManifestFileInfo { get; set; } 32 | 33 | [JsonPropertyName("manifest_download")] public SophonManifestUrlInfo ManifestUrlInfo { get; set; } 34 | 35 | [JsonPropertyName("stats")] public SophonManifestChunkInfo ChunkInfo { get; set; } 36 | 37 | [JsonPropertyName("chunk_download")] public SophonManifestUrlInfo ChunksUrlInfo { get; set; } 38 | 39 | [JsonPropertyName("deduplicated_stats")] public SophonManifestChunkInfo DeduplicatedChunkInfo { get; set; } 40 | } 41 | 42 | public class SophonManifestPatchBranch : SophonManifestReturnedResponse 43 | { 44 | [JsonPropertyName("data")] public SophonManifestPatchData Data { get; set; } 45 | } 46 | 47 | public class SophonManifestPatchData : SophonTaggedResponse 48 | { 49 | [JsonPropertyName("patch_id")] public string PatchId { get; set; } 50 | 51 | [JsonPropertyName("manifests")] public List ManifestIdentityList { get; set; } 52 | } 53 | 54 | public class SophonManifestPatchIdentity 55 | { 56 | [JsonNumberHandling(JsonNumberHandling.AllowReadingFromString)] 57 | [JsonPropertyName("category_id")] public int CategoryId { get; set; } 58 | 59 | [JsonPropertyName("category_name")] public string CategoryName { get; set; } 60 | 61 | [JsonPropertyName("matching_field")] public string MatchingField { get; set; } 62 | 63 | [JsonPropertyName("manifest")] public SophonManifestFileInfo ManifestFileInfo { get; set; } 64 | 65 | [JsonPropertyName("manifest_download")] public SophonManifestUrlInfo ManifestUrlInfo { get; set; } 66 | 67 | [JsonPropertyName("diff_download")] public SophonManifestUrlInfo DiffUrlInfo { get; set; } 68 | 69 | [JsonPropertyName("stats")] public Dictionary DiffTaggedInfo { get; set; } 70 | } 71 | 72 | public class SophonManifestReturnedResponse 73 | { 74 | [JsonPropertyName("retcode")] public int ReturnCode { get; set; } 75 | 76 | [JsonPropertyName("message")] public string ReturnMessage { get; set; } 77 | } 78 | 79 | public class SophonTaggedResponse 80 | { 81 | [JsonPropertyName("build_id")] public string BuildId { get; set; } 82 | 83 | [JsonPropertyName("tag")] public string TagName { get; set; } 84 | } 85 | 86 | public class SophonManifestFileInfo 87 | { 88 | [JsonPropertyName("id")] public string FileName { get; set; } 89 | 90 | [JsonPropertyName("checksum")] public string Checksum { get; set; } 91 | 92 | [JsonNumberHandling(JsonNumberHandling.AllowReadingFromString)] 93 | [JsonPropertyName("compressed_size")] public long CompressedSize { get; set; } 94 | 95 | [JsonNumberHandling(JsonNumberHandling.AllowReadingFromString)] 96 | [JsonPropertyName("uncompressed_size")] public long UncompressedSize { get; set; } 97 | } 98 | 99 | public class SophonManifestUrlInfo 100 | { 101 | [JsonPropertyName("password")] public string EncryptionPassword { get; set; } 102 | 103 | [JsonPropertyName("url_prefix")] public string UrlPrefix { get; set; } 104 | 105 | [JsonPropertyName("url_suffix")] public string UrlSuffix { get; set; } 106 | 107 | [JsonConverter(typeof(BoolConverter))] 108 | [JsonPropertyName("encryption")] public bool IsEncrypted { get; set; } 109 | 110 | [JsonConverter(typeof(BoolConverter))] 111 | [JsonPropertyName("compression")] public bool IsCompressed { get; set; } 112 | } 113 | 114 | public class SophonManifestChunkInfo 115 | { 116 | [JsonNumberHandling(JsonNumberHandling.AllowReadingFromString)] 117 | [JsonPropertyName("compressed_size")] public long CompressedSize { get; set; } 118 | 119 | [JsonNumberHandling(JsonNumberHandling.AllowReadingFromString)] 120 | [JsonPropertyName("uncompressed_size")] public long UncompressedSize { get; set; } 121 | 122 | [JsonNumberHandling(JsonNumberHandling.AllowReadingFromString)] 123 | [JsonPropertyName("file_count")] public int FileCount { get; set; } 124 | 125 | [JsonNumberHandling(JsonNumberHandling.AllowReadingFromString)] 126 | [JsonPropertyName("chunk_count")] public int ChunkCount { get; set; } 127 | } 128 | } -------------------------------------------------------------------------------- /Core/Runner/CliHandler.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Threading.Tasks; 6 | using Mono.Options; 7 | 8 | namespace Core.Runner 9 | { 10 | public static class CliHandler 11 | { 12 | private static string EnsureNotEmpty(string v, string name) 13 | { 14 | if (string.IsNullOrWhiteSpace(v)) 15 | throw new OptionException($"Missing value for --{name}", name); 16 | return v.Trim(); 17 | } 18 | 19 | public static void ParseArgsAndSetConfig(string[] args) 20 | { 21 | var options = new OptionSet 22 | { 23 | { "region=", "Region: OSREL or CNREL", v => 24 | { 25 | var region = EnsureNotEmpty(v, "region").ToUpperInvariant(); 26 | if (region != "OSREL" && region != "CNREL") 27 | throw new OptionException("Invalid value for --region", "region"); 28 | AppConfig.Config.Region = region; 29 | } 30 | }, 31 | { "branch=", "Branch override", v => 32 | AppConfig.Config.Branch = EnsureNotEmpty(v, "branch") 33 | }, 34 | { "launcherId=", "Launcher ID override", v => 35 | AppConfig.Config.LauncherId = EnsureNotEmpty(v, "launcherId") 36 | }, 37 | { "platApp=", "Platform App ID override", v => 38 | AppConfig.Config.PlatApp = EnsureNotEmpty(v, "platApp") 39 | }, 40 | { "threads=", "Threads to use", v => 41 | { 42 | if (!int.TryParse(v, out int val) || val <= 0) 43 | throw new OptionException("Invalid value for --threads", "threads"); 44 | AppConfig.Config.Threads = val; 45 | } 46 | }, 47 | { "handles=", "HTTP handles", v => 48 | { 49 | if (!int.TryParse(v, out int val) || val <= 0) 50 | throw new OptionException("Invalid value for --handles", "handles"); 51 | AppConfig.Config.MaxHttpHandle = val; 52 | } 53 | }, 54 | { "silent", "Silent mode", _ => AppConfig.Config.Silent = true }, 55 | { "CNREL", "Switch to CN region", _ => 56 | { 57 | AppConfig.Config.Region = "CNREL"; 58 | AppConfig.Config.LauncherId = "jGHBHlcOq1"; 59 | AppConfig.Config.PlatApp = "ddxf5qt290cg"; 60 | } 61 | }, 62 | { "OSREL", "Switch to OS region", _ => 63 | { 64 | AppConfig.Config.Region = "OSREL"; 65 | AppConfig.Config.LauncherId = "VYTpXlbWo8"; 66 | AppConfig.Config.PlatApp = "ddxf6vlr1reo"; 67 | } 68 | }, 69 | { "h|help", "Show help", _ => {} }, 70 | }; 71 | 72 | options.Parse(args); 73 | 74 | if (args.Contains("--main")) 75 | AppConfig.Config.Branch = "main"; 76 | else if (args.Contains("--predownload")) 77 | AppConfig.Config.Branch = "predownload"; 78 | 79 | AppConfig.Config.SetPasswordByBranch(); 80 | } 81 | 82 | public static async Task RunWithArgs(string[] args) 83 | { 84 | bool showHelp = false; 85 | string action = "", gameId = "", updateFrom = "", updateTo = "", outputDir = "", matchingField = ""; 86 | 87 | try 88 | { 89 | List extra = new OptionSet().Parse(args); 90 | int count = extra.Count; 91 | action = count > 1 ? extra[0].ToLowerInvariant() : ""; 92 | 93 | if (action == "full" && count >= 5) 94 | { 95 | gameId = extra[1]; 96 | matchingField = extra[2]; 97 | updateFrom = extra[3]; 98 | outputDir = extra[4]; 99 | } 100 | else if (action == "update" && count >= 6) 101 | { 102 | gameId = extra[1]; 103 | matchingField = extra[2]; 104 | updateFrom = extra[3]; 105 | updateTo = extra[4]; 106 | outputDir = extra[5]; 107 | } 108 | else 109 | { 110 | showHelp = true; 111 | } 112 | 113 | if (!showHelp) 114 | { 115 | string fullPath = Path.GetFullPath(outputDir); 116 | Directory.CreateDirectory(fullPath); 117 | } 118 | } 119 | catch (OptionException e) 120 | { 121 | Console.ForegroundColor = ConsoleColor.Red; 122 | Console.WriteLine("Error: " + e.Message); 123 | Console.ResetColor(); 124 | Console.WriteLine("Use --help to see usage information."); 125 | return 1; 126 | } 127 | 128 | if (showHelp) 129 | { 130 | Console.WriteLine(""" 131 | Sophon Downloader - Command Line Interface 132 | 133 | Usage: 134 | Sophon.Downloader.exe full [options] 135 | Sophon.Downloader.exe update [options] 136 | 137 | Example: 138 | Sophon.Downloader.exe full gopR6Cufr3 game 5.8 Downloads 139 | Sophon.Downloader.exe update gopR6Cufr3 en-us 5.8 6.0 Downloads --predownload --OSREL --threads=2 --handles=64 140 | """); 141 | return 0; 142 | } 143 | 144 | var preparedArgs = action == "full" 145 | ? new[] { action, gameId, matchingField, updateFrom, outputDir } 146 | : new[] { action, gameId, matchingField, updateFrom, updateTo, outputDir }; 147 | 148 | await DownloadExecutor.RunDownload(preparedArgs); 149 | return 0; 150 | } 151 | } 152 | } 153 | -------------------------------------------------------------------------------- /Sophon/Helper/ChunkStream.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Buffers; 3 | using System.IO; 4 | using System.Threading; 5 | using System.Threading.Tasks; 6 | 7 | namespace Sophon 8 | { 9 | public sealed class ChunkStream : Stream 10 | { 11 | private readonly Stream _stream; 12 | private long Start { get; } 13 | private long End { get; } 14 | private long Size => End - Start; 15 | private long CurPos { get; set; } 16 | private long Remain => Size - CurPos; 17 | private bool IsDisposing { get; } 18 | 19 | public ChunkStream(Stream stream, long start, long end, bool isDisposing = false) 20 | { 21 | _stream = stream; 22 | 23 | if (_stream.Length == 0) 24 | throw new Exception("The stream must not have 0 bytes!"); 25 | 26 | if (_stream.Length < start || end > _stream.Length) 27 | throw new ArgumentOutOfRangeException(nameof(stream)); 28 | 29 | _stream.Position = start; 30 | Start = start; 31 | End = end; 32 | CurPos = 0; 33 | IsDisposing = isDisposing; 34 | } 35 | 36 | ~ChunkStream() => Dispose(IsDisposing); 37 | 38 | public override int Read(Span buffer) 39 | { 40 | if (Remain == 0) return 0; 41 | 42 | int toSlice = (int)Math.Min(buffer.Length, Remain); 43 | _stream.Position = Start + CurPos; 44 | int read = _stream.Read(buffer[..toSlice]); 45 | CurPos += read; 46 | return read; 47 | } 48 | 49 | public override async ValueTask ReadAsync(Memory buffer, CancellationToken token = default) 50 | { 51 | if (Remain == 0) return 0; 52 | 53 | int toSlice = (int)Math.Min(buffer.Length, Remain); 54 | _stream.Position = Start + CurPos; 55 | int read = await _stream.ReadAsync(buffer[..toSlice], token); 56 | CurPos += read; 57 | return read; 58 | } 59 | 60 | public override int Read(byte[] buffer, int offset, int count) 61 | { 62 | if (Remain == 0) return 0; 63 | 64 | int toRead = (int)Math.Min(count, Remain); 65 | _stream.Position = Start + CurPos; 66 | int read = _stream.Read(buffer, offset, toRead); 67 | CurPos += read; 68 | return read; 69 | } 70 | 71 | public override async Task ReadAsync(byte[] buffer, int offset, int count, CancellationToken token) 72 | { 73 | if (Remain == 0) return 0; 74 | 75 | int toRead = (int)Math.Min(count, Remain); 76 | _stream.Position = Start + CurPos; 77 | int read = await _stream.ReadAsync(buffer.AsMemory(offset, toRead), token); 78 | CurPos += read; 79 | return read; 80 | } 81 | 82 | public override void Write(ReadOnlySpan buffer) 83 | { 84 | if (Remain == 0) return; 85 | 86 | int toSlice = (int)Math.Min(buffer.Length, Remain); 87 | CurPos += toSlice; 88 | _stream.Write(buffer[..toSlice]); 89 | } 90 | 91 | public override async ValueTask WriteAsync(ReadOnlyMemory buffer, CancellationToken token = default) 92 | { 93 | if (Remain == 0) return; 94 | 95 | int toSlice = (int)Math.Min(buffer.Length, Remain); 96 | CurPos += toSlice; 97 | await _stream.WriteAsync(buffer[..toSlice], token); 98 | } 99 | 100 | public override void Write(byte[] buffer, int offset, int count) 101 | { 102 | int toRead = (int)Math.Min(count, Remain); 103 | int toOffset = offset > Remain ? 0 : offset; 104 | _stream.Position += toOffset; 105 | CurPos += toOffset + toRead; 106 | _stream.Write(buffer, offset, toRead); 107 | } 108 | 109 | public override async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken token) 110 | { 111 | int toRead = (int)Math.Min(count, Remain); 112 | int toOffset = offset > Remain ? 0 : offset; 113 | _stream.Position += toOffset; 114 | CurPos += toOffset + toRead; 115 | await _stream.WriteAsync(buffer.AsMemory(offset, toRead), token); 116 | } 117 | 118 | public override void CopyTo(Stream destination, int bufferSize) 119 | { 120 | byte[] buffer = ArrayPool.Shared.Rent(bufferSize); 121 | try 122 | { 123 | int read; 124 | while ((read = Read(buffer)) > 0) 125 | destination.Write(buffer.AsSpan(0, read)); 126 | } 127 | finally 128 | { 129 | ArrayPool.Shared.Return(buffer); 130 | } 131 | } 132 | 133 | public override async Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken) 134 | { 135 | byte[] buffer = ArrayPool.Shared.Rent(bufferSize); 136 | try 137 | { 138 | int read; 139 | while ((read = await ReadAsync(buffer, cancellationToken)) > 0) 140 | await destination.WriteAsync(buffer.AsMemory(0, read), cancellationToken); 141 | } 142 | finally 143 | { 144 | ArrayPool.Shared.Return(buffer); 145 | } 146 | } 147 | 148 | public override bool CanRead => _stream.CanRead; 149 | public override bool CanSeek => _stream.CanSeek; 150 | public override bool CanWrite => _stream.CanWrite; 151 | public override void Flush() => _stream.Flush(); 152 | public override long Length => Size; 153 | 154 | public override long Position 155 | { 156 | get => CurPos; 157 | set 158 | { 159 | if (value > Size) 160 | throw new IndexOutOfRangeException(); 161 | 162 | CurPos = value; 163 | _stream.Position = CurPos + Start; 164 | } 165 | } 166 | 167 | public override long Seek(long offset, SeekOrigin origin) 168 | { 169 | return origin switch 170 | { 171 | SeekOrigin.Begin => offset > Size 172 | ? throw new ArgumentOutOfRangeException(nameof(offset)) 173 | : _stream.Seek(offset + Start, SeekOrigin.Begin) - Start, 174 | 175 | SeekOrigin.Current => 176 | (_stream.Position - Start + offset > Size) 177 | ? throw new ArgumentOutOfRangeException(nameof(offset)) 178 | : _stream.Seek(offset, SeekOrigin.Current) - Start, 179 | 180 | SeekOrigin.End => 181 | _stream.Position = End - offset, 182 | 183 | _ => throw new ArgumentOutOfRangeException(nameof(origin)), 184 | }; 185 | } 186 | 187 | public override void SetLength(long value) => throw new NotSupportedException(); 188 | 189 | protected override void Dispose(bool disposing) 190 | { 191 | if (disposing) base.Dispose(true); 192 | if (IsDisposing) _stream.Dispose(); 193 | } 194 | } 195 | } -------------------------------------------------------------------------------- /Core/Downloader.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Diagnostics; 4 | using System.IO; 5 | using System.Net; 6 | using System.Net.Http; 7 | using System.Text; 8 | using System.Threading; 9 | using System.Threading.Tasks; 10 | using System.Threading.Tasks.Dataflow; 11 | using Core.Utils; 12 | using Sophon; 13 | using Sophon.Structs; 14 | 15 | namespace Core 16 | { 17 | internal class Downloader 18 | { 19 | private static string _cancelMessage = string.Empty; 20 | private static bool _isRetry = true; 21 | 22 | public static async Task StartDownload(string prevManifestUrl, string newManifestUrl, string outputDir, string matchingField) 23 | { 24 | StartDownload: 25 | 26 | _isRetry = false; 27 | _cancelMessage = "[\"C\"] Stop or [\"R\"] Restart"; 28 | 29 | CancellationTokenSource tokenSource = new(); 30 | HttpClientHandler httpHandler = new() { MaxConnectionsPerServer = AppConfig.Config.MaxHttpHandle }; 31 | HttpClient httpClient = new(httpHandler) 32 | { 33 | DefaultRequestVersion = HttpVersion.Version30, 34 | DefaultVersionPolicy = HttpVersionPolicy.RequestVersionOrLower, 35 | }; 36 | 37 | using (tokenSource) 38 | using (httpHandler) 39 | using (httpClient) 40 | { 41 | if (!Directory.Exists(outputDir)) 42 | Directory.CreateDirectory(outputDir); 43 | 44 | if (!AppConfig.Config.Silent) 45 | Console.WriteLine("Fetching assets..."); 46 | 47 | var result = await Assets.GetAssetsFromManifests( 48 | httpClient, 49 | matchingField, 50 | prevManifestUrl, 51 | newManifestUrl, 52 | tokenSource 53 | ); 54 | 55 | if (result?.Item1 == null) 56 | { 57 | Console.ForegroundColor = ConsoleColor.Red; 58 | Console.WriteLine("[ERROR] Failed to fetch manifest. Please check if the version or parameters are valid."); 59 | Console.ResetColor(); 60 | return 1; 61 | } 62 | 63 | var sophonAssets = result.Item1; 64 | long updateSize = result.Item2; 65 | 66 | long totalSizeDiff = sophonAssets.GetCalculatedDiffSize(true); 67 | string totalSizeDiffUnit = Formatter.FormatSize(totalSizeDiff); 68 | string totalSizeUnit = Formatter.FormatSize(updateSize); 69 | 70 | if (!AppConfig.Config.Silent) 71 | { 72 | Console.WriteLine($"* Found {sophonAssets.Count} assets"); 73 | if (!string.IsNullOrEmpty(newManifestUrl)) 74 | { 75 | Console.WriteLine($"* Update data is {totalSizeDiffUnit}"); 76 | Console.WriteLine($"* Because the full assets will be downloaded, total download size is {totalSizeUnit}"); 77 | } 78 | else 79 | { 80 | Console.WriteLine($"* Total download size is {totalSizeUnit}"); 81 | } 82 | 83 | Console.Write("Continue? (y/n): "); 84 | var input = Console.ReadLine()?.Trim().ToLower(); 85 | if (input != "y" && input != "yes") 86 | { 87 | Console.WriteLine("Aborting..."); 88 | return 0; 89 | } 90 | } 91 | 92 | long currentRead = 0; 93 | Task exitTask = Task.Run(() => AppExitTrigger(tokenSource)); 94 | 95 | var stopwatch = Stopwatch.StartNew(); 96 | 97 | try 98 | { 99 | foreach (string tempFile in Directory.EnumerateFiles(outputDir, "*_tempUpdate", SearchOption.AllDirectories)) 100 | File.Delete(tempFile); 101 | 102 | var downloadTaskQueue = new ActionBlock>(async ctx => 103 | { 104 | var asset = ctx.Item1; 105 | var client = ctx.Item2; 106 | 107 | await asset.WriteUpdateAsync( 108 | client, 109 | outputDir, 110 | outputDir, 111 | outputDir, 112 | false, 113 | read => 114 | { 115 | Interlocked.Add(ref currentRead, read); 116 | string sizeUnit = Formatter.FormatSize(currentRead); 117 | string speedUnit = Formatter.FormatSize(currentRead / stopwatch.Elapsed.TotalSeconds); 118 | 119 | if (!AppConfig.Config.Silent) 120 | { 121 | Console.Write($"{_cancelMessage} | {sizeUnit}/{totalSizeUnit} ({totalSizeDiffUnit} diff) ({speedUnit}/s) \r"); 122 | } 123 | }, 124 | null, null, 125 | tokenSource.Token 126 | ); 127 | 128 | string outputPath = Path.Combine(outputDir, asset.AssetName); 129 | string outputTempPath = outputPath + "_tempUpdate"; 130 | 131 | File.Move(outputTempPath, outputPath, true); 132 | }, 133 | new ExecutionDataflowBlockOptions 134 | { 135 | CancellationToken = tokenSource.Token, 136 | MaxDegreeOfParallelism = AppConfig.Config.Threads, 137 | MaxMessagesPerTask = AppConfig.Config.Threads 138 | }); 139 | 140 | foreach (var asset in sophonAssets) 141 | await downloadTaskQueue.SendAsync(Tuple.Create(asset, httpClient), tokenSource.Token); 142 | 143 | downloadTaskQueue.Complete(); 144 | await downloadTaskQueue.Completion; 145 | } 146 | catch (OperationCanceledException) 147 | { 148 | if (!AppConfig.Config.Silent) 149 | { 150 | Console.WriteLine("\nCancelled!"); 151 | } 152 | } 153 | finally 154 | { 155 | stopwatch.Stop(); 156 | await exitTask; 157 | } 158 | } 159 | 160 | if (_isRetry) 161 | goto StartDownload; 162 | 163 | return 0; 164 | } 165 | 166 | private static void AppExitTrigger(CancellationTokenSource tokenSource) 167 | { 168 | while (true) 169 | { 170 | ConsoleKeyInfo key = Console.ReadKey(true); 171 | switch (key.Key) 172 | { 173 | case ConsoleKey.C: 174 | _cancelMessage = "Canceling..."; 175 | tokenSource.Cancel(); 176 | return; 177 | case ConsoleKey.R: 178 | _isRetry = true; 179 | tokenSource.Cancel(); 180 | return; 181 | } 182 | } 183 | } 184 | } 185 | } 186 | -------------------------------------------------------------------------------- /Core/SophonUrl.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Net.Http; 5 | using System.Text.Json; 6 | using System.Threading.Tasks; 7 | using System.Web; 8 | 9 | namespace Core 10 | { 11 | public class BranchesRoot 12 | { 13 | public int retcode { get; set; } 14 | public string? message { get; set; } 15 | public BranchesData? data { get; set; } 16 | } 17 | 18 | public class BranchesData 19 | { 20 | public List? game_branches { get; set; } 21 | } 22 | 23 | public class BranchesGameBranch 24 | { 25 | public BranchesGame? game { get; set; } 26 | public BranchesMain? main { get; set; } 27 | public BranchesMain? pre_download { get; set; } 28 | } 29 | 30 | public class BranchesGame 31 | { 32 | public string? id { get; set; } 33 | public string? biz { get; set; } 34 | } 35 | 36 | public class BranchesMain 37 | { 38 | public string? package_id { get; set; } 39 | public string? branch { get; set; } 40 | public string? password { get; set; } 41 | public string? tag { get; set; } 42 | public List? diff_tags { get; set; } 43 | public List? categories { get; set; } 44 | } 45 | 46 | public class BranchesCategory 47 | { 48 | public string? category_id { get; set; } 49 | public string? matching_field { get; set; } 50 | } 51 | 52 | public enum Region 53 | { 54 | OSREL, 55 | CNREL 56 | } 57 | 58 | public enum BranchType 59 | { 60 | Main, 61 | PreDownload 62 | } 63 | 64 | public class SophonUrl 65 | { 66 | private string apiBase { get; set; } = ""; 67 | private string sophonBase { get; set; } = ""; 68 | private string gameId { get; set; } = ""; 69 | private BranchType branch { get; set; } 70 | private string launcherId { get; set; } = ""; 71 | private string platApp { get; set; } = ""; 72 | private string gameBiz { get; set; } = ""; 73 | private string packageId { get; set; } = ""; 74 | private string password { get; set; } = ""; 75 | private BranchesRoot branchBackup { get; set; } = new BranchesRoot(); 76 | 77 | public SophonUrl(Region region, string gameId, BranchType branch = BranchType.Main, string launcherIdOverride = "", string platAppOverride = "") 78 | { 79 | UpdateRegion(region); 80 | this.gameId = gameId; 81 | this.branch = branch; 82 | this.launcherId = !string.IsNullOrEmpty(launcherIdOverride) ? launcherIdOverride : AppConfig.Config.LauncherId; 83 | this.platApp = !string.IsNullOrEmpty(platAppOverride) ? platAppOverride : AppConfig.Config.PlatApp; 84 | } 85 | 86 | public void UpdateRegion(Region region) 87 | { 88 | switch (region) 89 | { 90 | case Region.OSREL: 91 | apiBase = "https://sg-hyp-api.hoyoverse.com/hyp/hyp-connect/api/getGameBranches"; 92 | sophonBase = "https://sg-public-api.hoyoverse.com:443/downloader/sophon_chunk/api/getBuild"; 93 | break; 94 | case Region.CNREL: 95 | apiBase = "https://hyp-api.mihoyo.com/hyp/hyp-connect/api/getGameBranches"; 96 | sophonBase = "https://api-takumi.mihoyo.com/downloader/sophon_chunk/api/getBuild"; 97 | break; 98 | default: 99 | throw new ArgumentOutOfRangeException(nameof(region), region, null); 100 | } 101 | } 102 | 103 | public async Task GetBuildData() 104 | { 105 | var uri = new UriBuilder(apiBase); 106 | var query = HttpUtility.ParseQueryString(uri.Query); 107 | 108 | query["game_ids[]"] = gameId; 109 | query["launcher_id"] = launcherId; 110 | uri.Query = query.ToString(); 111 | 112 | string json = await FetchUrl(uri.ToString()); 113 | var obj = JsonSerializer.Deserialize(json); 114 | 115 | string[] data = ParseBuildData(obj, branch); 116 | 117 | if (data[0] != "OK") 118 | { 119 | if (branch == BranchType.PreDownload) 120 | { 121 | packageId = "ScSYQBFhu9"; 122 | password = "ZOJpUiKu4Sme"; 123 | branchBackup = new BranchesRoot(); 124 | return 0; 125 | } 126 | else if (branch == BranchType.Main) 127 | { 128 | packageId = "ScSYQBFhu9"; 129 | password = "bDL4JUHL625x"; 130 | branchBackup = new BranchesRoot(); 131 | return 0; 132 | } 133 | else 134 | { 135 | Console.WriteLine($"Error: {data[1]}"); 136 | return -1; 137 | } 138 | } 139 | 140 | gameBiz = data[1]; 141 | packageId = string.IsNullOrEmpty(data[2]) 142 | ? (branch == BranchType.PreDownload ? "ScSYQBFhu9" : "ScSYQBFhu9") 143 | : data[2]; 144 | password = string.IsNullOrEmpty(data[3]) 145 | ? (branch == BranchType.PreDownload ? "ZOJpUiKu4Sme" : "bDL4JUHL625x") 146 | : data[3]; 147 | 148 | branchBackup = obj!; 149 | return 0; 150 | } 151 | 152 | private string[] ParseBuildData(BranchesRoot? obj, BranchType searchBranch) 153 | { 154 | if (obj == null || obj.retcode != 0 || obj.message != "OK") 155 | return new[] { "ERROR", obj?.message ?? "Unknown error" }; 156 | 157 | var branchObj = GetBranch(obj, searchBranch); 158 | if (branchObj == null) 159 | return new[] { "ERROR", $"Branch {searchBranch} not found" }; 160 | 161 | var gameObj = GetBranchGame(obj); 162 | return new[] { "OK", gameObj?.biz ?? "", branchObj.package_id ?? "", branchObj.password ?? "" }; 163 | } 164 | 165 | public string GetBuildUrl(string version, bool isUpdate = false) 166 | { 167 | var uri = new UriBuilder(sophonBase); 168 | var query = HttpUtility.ParseQueryString(uri.Query); 169 | 170 | query["branch"] = branch.ToString().ToLower(); 171 | query["package_id"] = packageId; 172 | query["password"] = password; 173 | query["plat_app"] = platApp; 174 | 175 | if (branch != BranchType.PreDownload) 176 | query["tag"] = version; 177 | 178 | uri.Query = query.ToString(); 179 | return uri.ToString(); 180 | } 181 | 182 | private static async Task FetchUrl(string url) 183 | { 184 | using var client = new HttpClient(); 185 | return await client.GetStringAsync(url); 186 | } 187 | 188 | private static BranchesGame? GetBranchGame(BranchesRoot obj) 189 | { 190 | return obj.data?.game_branches?.FirstOrDefault()?.game; 191 | } 192 | 193 | private static BranchesMain? GetBranch(BranchesRoot obj, BranchType searchBranch) 194 | { 195 | var branchObj = obj.data?.game_branches?.FirstOrDefault(); 196 | return searchBranch switch 197 | { 198 | BranchType.Main => branchObj?.main, 199 | BranchType.PreDownload => branchObj?.pre_download, 200 | _ => null 201 | }; 202 | } 203 | } 204 | } 205 | -------------------------------------------------------------------------------- /Core/AppConfig.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Text.Json; 7 | 8 | namespace Core 9 | { 10 | public class AppConfig 11 | { 12 | public string Region { get; set; } = "OSREL"; 13 | public string Branch { get; set; } = "main"; 14 | public string LauncherId { get; set; } = "VYTpXlbWo8"; 15 | public string PlatApp { get; set; } = "ddxf6vlr1reo"; 16 | public string Password { get; set; } = "bDL4JUHL625x"; 17 | public int Threads { get; set; } = Math.Max(1, Environment.ProcessorCount / 2); 18 | public int MaxHttpHandle { get; set; } = 128; 19 | public bool Silent { get; set; } = false; 20 | public VersionsConfig Versions { get; set; } = new(); 21 | 22 | private static readonly string ConfigPath = "config.json"; 23 | public static AppConfig Config { get; private set; } = LoadInternal(); 24 | 25 | private static VersionsConfig GetDefaultVersions() => new() 26 | { 27 | Full = new() { "5.6", "5.7", "5.8", "6.0", "6.1" }, 28 | Update = new() 29 | { 30 | new() { "5.5", "5.6" }, new() { "5.5", "5.7" }, 31 | new() { "5.6", "5.7" }, new() { "5.6", "5.8" }, 32 | new() { "5.7", "5.8" }, new() { "5.7", "6.0" }, 33 | new() { "5.8", "6.0" }, new() { "5.8", "6.1" }, 34 | new() { "6.0", "6.1" } 35 | } 36 | }; 37 | 38 | private static AppConfig LoadInternal() 39 | { 40 | if (!File.Exists(ConfigPath)) 41 | { 42 | var cfg = new AppConfig { Versions = GetDefaultVersions() }; 43 | cfg.SetPasswordByBranch(); 44 | cfg.Save(); 45 | Console.WriteLine("[INFO] config.json not found. Created default config."); 46 | return cfg; 47 | } 48 | 49 | try 50 | { 51 | var root = JsonDocument.Parse(File.ReadAllText(ConfigPath)).RootElement; 52 | var cfg = new AppConfig(); 53 | 54 | string? region = root.GetPropertyOrNull("Region")?.GetString()?.ToUpperInvariant(); 55 | if (region is "OSREL" or "CNREL") cfg.Region = region; 56 | 57 | string? branch = root.GetPropertyOrNull("Branch")?.GetString(); 58 | if (!string.IsNullOrWhiteSpace(branch)) cfg.Branch = branch; 59 | 60 | cfg.LauncherId = root.GetPropertyOrNull("LauncherId")?.GetString() ?? cfg.LauncherId; 61 | cfg.PlatApp = root.GetPropertyOrNull("PlatApp")?.GetString() ?? cfg.PlatApp; 62 | 63 | if (root.TryGetInt("Threads", out int t)) 64 | cfg.Threads = t > 0 && t <= Environment.ProcessorCount 65 | ? t 66 | : Math.Max(1, Environment.ProcessorCount / 2); 67 | 68 | if (root.TryGetInt("MaxHttpHandle", out int h)) 69 | cfg.MaxHttpHandle = h > 0 && h <= 512 ? h : 128; 70 | 71 | cfg.Silent = root.TryGetProperty("Silent", out var sp) && 72 | sp.ValueKind == JsonValueKind.True; 73 | 74 | var versions = new VersionsConfig(); 75 | 76 | if (root.TryGetProperty("Versions", out var v)) 77 | { 78 | if (v.TryGetProperty("full", out var full) && full.ValueKind == JsonValueKind.Array) 79 | { 80 | versions.Full = full.EnumerateArray() 81 | .Where(x => x.ValueKind == JsonValueKind.String) 82 | .Select(x => x.GetString()!) 83 | .Where(s => !string.IsNullOrWhiteSpace(s)) 84 | .ToList(); 85 | } 86 | 87 | if (v.TryGetProperty("update", out var upd) && upd.ValueKind == JsonValueKind.Array) 88 | { 89 | foreach (var arr in upd.EnumerateArray() 90 | .Where(x => x.ValueKind == JsonValueKind.Array)) 91 | { 92 | var list = arr.EnumerateArray() 93 | .Where(x => x.ValueKind == JsonValueKind.String) 94 | .Select(x => x.GetString()!) 95 | .Where(s => !string.IsNullOrWhiteSpace(s)) 96 | .ToList(); 97 | 98 | if (list.Count == 2) versions.Update.Add(list); 99 | } 100 | } 101 | } 102 | 103 | if (versions.Full.Count == 0 || versions.Update.Count == 0) 104 | versions = GetDefaultVersions(); 105 | 106 | cfg.Versions = versions; 107 | 108 | if (cfg.Region == "CNREL") 109 | { 110 | cfg.LauncherId = "jGHBHlcOq1"; 111 | cfg.PlatApp = "ddxf5qt290cg"; 112 | } 113 | else 114 | { 115 | cfg.LauncherId = "VYTpXlbWo8"; 116 | cfg.PlatApp = "ddxf6vlr1reo"; 117 | } 118 | 119 | cfg.SetPasswordByBranch(); 120 | 121 | cfg.Save(); 122 | return cfg; 123 | } 124 | catch 125 | { 126 | var fallback = new AppConfig { Versions = GetDefaultVersions() }; 127 | fallback.SetPasswordByBranch(); 128 | fallback.Save(); 129 | return fallback; 130 | } 131 | } 132 | 133 | public void SetPasswordByBranch() 134 | { 135 | Password = Branch switch 136 | { 137 | "main" => "bDL4JUHL625x", 138 | "predownload" => "ZOJpUiKu4Sme", 139 | _ => "" 140 | }; 141 | } 142 | 143 | public void Save() 144 | { 145 | var sb = new StringBuilder(); 146 | 147 | void W(int l, string t) => 148 | sb.AppendLine(new string(' ', l * 2) + t); 149 | 150 | W(0, "{"); 151 | W(1, $"\"Region\": \"{Region}\","); 152 | W(1, $"\"Branch\": \"{Branch}\","); 153 | W(1, $"\"LauncherId\": \"{LauncherId}\","); 154 | W(1, $"\"PlatApp\": \"{PlatApp}\","); 155 | W(1, $"\"Password\": \"{Password}\","); 156 | W(1, $"\"Threads\": {Threads},"); 157 | W(1, $"\"MaxHttpHandle\": {MaxHttpHandle},"); 158 | W(1, $"\"Silent\": {Silent.ToString().ToLower()},"); 159 | W(1, "\"Versions\": {"); 160 | W(2, "\"full\": [" + string.Join(", ", Versions.Full.Select(v => $"\"{v}\"")) + "],"); 161 | W(2, "\"update\": ["); 162 | 163 | for (int i = 0; i < Versions.Update.Count; i++) 164 | { 165 | W(3, 166 | "[" + string.Join(", ", Versions.Update[i].Select(v => $"\"{v}\"")) + "]" + 167 | (i < Versions.Update.Count - 1 ? "," : "")); 168 | } 169 | 170 | W(2, "]"); 171 | W(1, "}"); 172 | W(0, "}"); 173 | 174 | File.WriteAllText(ConfigPath, sb.ToString()); 175 | } 176 | } 177 | 178 | public class VersionsConfig 179 | { 180 | public List Full { get; set; } = new(); 181 | public List> Update { get; set; } = new(); 182 | } 183 | 184 | static class JsonExt 185 | { 186 | public static JsonElement? GetPropertyOrNull(this JsonElement e, string name) => 187 | e.TryGetProperty(name, out var val) ? val : (JsonElement?)null; 188 | 189 | public static bool TryGetInt(this JsonElement e, string name, out int value) 190 | { 191 | value = 0; 192 | return e.TryGetProperty(name, out var val) && val.TryGetInt32(out value); 193 | } 194 | } 195 | } 196 | -------------------------------------------------------------------------------- /Sophon/SophonPatch.cs: -------------------------------------------------------------------------------- 1 | using Sophon.Helper; 2 | using Sophon.Infos; 3 | using Sophon.Protos; 4 | using Sophon.Structs; 5 | using System; 6 | using System.Collections.Generic; 7 | using System.IO; 8 | using System.Linq; 9 | using System.Net.Http; 10 | using System.Runtime.CompilerServices; 11 | using System.Threading; 12 | using ZstdNet; 13 | 14 | namespace Sophon 15 | { 16 | public static partial class SophonPatch 17 | { 18 | private static readonly object DummyInstance = new(); 19 | 20 | public static async IAsyncEnumerable EnumerateUpdateAsync(HttpClient httpClient, 21 | SophonChunkManifestInfoPair infoPair, 22 | string versionTagUpdateFrom, 23 | string downloadOverUrl, 24 | SophonDownloadSpeedLimiter downloadSpeedLimiter = null, 25 | [EnumeratorCancellation] CancellationToken token = default) 26 | { 27 | await foreach (var asset in EnumerateUpdateAsync(httpClient, 28 | infoPair.ManifestInfo, 29 | infoPair.ChunksInfo, 30 | versionTagUpdateFrom, 31 | downloadOverUrl, 32 | downloadSpeedLimiter, 33 | token)) 34 | { 35 | yield return asset; 36 | } 37 | } 38 | 39 | public static async IAsyncEnumerable EnumerateUpdateAsync(HttpClient httpClient, 40 | SophonManifestInfo manifestInfo, 41 | SophonChunksInfo chunksInfo, 42 | string versionTagUpdateFrom, 43 | string downloadOverUrl, 44 | SophonDownloadSpeedLimiter downloadSpeedLimiter = null, 45 | [EnumeratorCancellation] CancellationToken token = default) 46 | { 47 | if (!DllUtils.IsLibraryExist(DllUtils.DllName)) 48 | throw new DllNotFoundException("libzstd is not found!"); 49 | 50 | if (string.IsNullOrEmpty(downloadOverUrl)) 51 | throw new ArgumentNullException(nameof(downloadOverUrl), "DownloadOver URL is not defined!"); 52 | 53 | if (string.IsNullOrEmpty(versionTagUpdateFrom)) 54 | throw new ArgumentNullException(nameof(versionTagUpdateFrom), "Version tag is not defined!"); 55 | 56 | ActionTimeoutTaskCallback manifestFromProtoTaskCallback = async (innerToken) => 57 | await httpClient.ReadProtoFromManifestInfo(manifestInfo, SophonPatchProto.Parser, innerToken); 58 | 59 | SophonPatchProto patchManifestProto = await TaskExtensions 60 | .WaitForRetryAsync( 61 | () => manifestFromProtoTaskCallback, 62 | TaskExtensions.DefaultTimeoutSec, 63 | null, 64 | null, 65 | null, 66 | token); 67 | 68 | SophonChunksInfo chunksInfoDownloadOver = chunksInfo.CopyWithNewBaseUrl(downloadOverUrl); 69 | 70 | foreach (SophonPatchAssetProperty patchAssetProperty in patchManifestProto.PatchAssets) 71 | { 72 | var property = patchAssetProperty; 73 | SophonPatchAssetInfo patchAssetInfo = property.AssetInfos 74 | .FirstOrDefault(x => x.VersionTag.Equals(versionTagUpdateFrom, StringComparison.OrdinalIgnoreCase)); 75 | 76 | if (patchAssetInfo == null) 77 | { 78 | yield return new SophonPatchAsset 79 | { 80 | PatchInfo = chunksInfoDownloadOver, 81 | TargetFileHash = property.AssetHashMd5, 82 | TargetFileSize = property.AssetSize, 83 | TargetFilePath = property.AssetName, 84 | TargetFileDownloadOverBaseUrl = downloadOverUrl, 85 | PatchMethod = SophonPatchMethod.DownloadOver 86 | }; 87 | continue; 88 | } 89 | 90 | var chunk = patchAssetInfo.Chunk; 91 | 92 | if (string.IsNullOrEmpty(chunk.OriginalFileName)) 93 | { 94 | yield return new SophonPatchAsset 95 | { 96 | PatchInfo = chunksInfo, 97 | PatchNameSource = chunk.PatchName, 98 | PatchHash = chunk.PatchMd5, 99 | PatchOffset = chunk.PatchOffset, 100 | PatchSize = chunk.PatchSize, 101 | PatchChunkLength = chunk.PatchLength, 102 | TargetFilePath = property.AssetName, 103 | TargetFileHash = property.AssetHashMd5, 104 | TargetFileSize = property.AssetSize, 105 | TargetFileDownloadOverBaseUrl = downloadOverUrl, 106 | PatchMethod = SophonPatchMethod.CopyOver, 107 | }; 108 | continue; 109 | } 110 | 111 | yield return new SophonPatchAsset 112 | { 113 | PatchInfo = chunksInfo, 114 | PatchNameSource = chunk.PatchName, 115 | PatchHash = chunk.PatchMd5, 116 | PatchOffset = chunk.PatchOffset, 117 | PatchSize = chunk.PatchSize, 118 | PatchChunkLength = chunk.PatchLength, 119 | TargetFilePath = property.AssetName, 120 | TargetFileHash = property.AssetHashMd5, 121 | TargetFileSize = property.AssetSize, 122 | TargetFileDownloadOverBaseUrl = downloadOverUrl, 123 | OriginalFilePath = chunk.OriginalFileName, 124 | OriginalFileSize = chunk.OriginalFileLength, 125 | OriginalFileHash = chunk.OriginalFileMd5, 126 | PatchMethod = SophonPatchMethod.Patch, 127 | }; 128 | } 129 | 130 | foreach (SophonUnusedAssetFile unusedAssetFile in patchManifestProto 131 | .UnusedAssets 132 | .SelectMany(x => x.AssetInfos.FirstOrDefault()?.Assets) 133 | .Where(x => x != null)) 134 | { 135 | yield return new SophonPatchAsset 136 | { 137 | OriginalFileHash = unusedAssetFile.FileMd5, 138 | OriginalFileSize = unusedAssetFile.FileSize, 139 | OriginalFilePath = unusedAssetFile.FileName, 140 | PatchMethod = SophonPatchMethod.Remove 141 | }; 142 | } 143 | } 144 | 145 | public static IEnumerable EnsureOnlyGetDedupPatchAssets(this IEnumerable patchAssetEnumerable) 146 | { 147 | HashSet processedAsset = []; 148 | foreach (SophonPatchAsset asset in patchAssetEnumerable 149 | .Where(x => !string.IsNullOrEmpty(x.PatchNameSource) && processedAsset.Add(x.PatchNameSource))) 150 | { 151 | yield return asset; 152 | } 153 | } 154 | 155 | public static void RemovePatches(this IEnumerable patchAssetEnumerable, string patchOutputDir) 156 | { 157 | foreach (SophonPatchAsset asset in patchAssetEnumerable 158 | .EnsureOnlyGetDedupPatchAssets()) 159 | { 160 | string patchFilePath = Path.Combine(patchOutputDir, asset.PatchNameSource); 161 | 162 | try 163 | { 164 | FileInfo fileInfo = new FileInfo(patchFilePath); 165 | if (fileInfo.Exists) 166 | { 167 | fileInfo.IsReadOnly = false; 168 | fileInfo.Refresh(); 169 | fileInfo.Delete(); 170 | DummyInstance.PushLogDebug($"Removed patch file: {patchFilePath}"); 171 | } 172 | } 173 | catch (Exception ex) 174 | { 175 | DummyInstance.PushLogError($"Failed while trying to remove patch file: {patchFilePath} | {ex}"); 176 | } 177 | } 178 | } 179 | } 180 | } -------------------------------------------------------------------------------- /Sophon/SophonAsset.Update.cs: -------------------------------------------------------------------------------- 1 | using Sophon.Helper; 2 | using Sophon.Structs; 3 | using System; 4 | using System.Buffers; 5 | using System.IO; 6 | using System.Linq; 7 | using System.Net.Http; 8 | using System.Threading; 9 | using System.Threading.Tasks; 10 | 11 | namespace Sophon 12 | { 13 | public partial class SophonAsset 14 | { 15 | public async ValueTask WriteUpdateAsync(HttpClient client, 16 | string oldInputDir, 17 | string newOutputDir, 18 | string chunkDir, 19 | bool removeChunkAfterApply = false, 20 | DelegateWriteStreamInfo writeInfoDelegate = null, 21 | DelegateWriteDownloadInfo downloadInfoDelegate = null, 22 | DelegateDownloadAssetComplete downloadCompleteDelegate = null, 23 | CancellationToken token = default) 24 | { 25 | const string tempExt = "_tempUpdate"; 26 | 27 | this.EnsureOrThrowChunksState(); 28 | this.EnsureOrThrowOutputDirectoryExistence(oldInputDir); 29 | this.EnsureOrThrowOutputDirectoryExistence(newOutputDir); 30 | this.EnsureOrThrowOutputDirectoryExistence(chunkDir); 31 | 32 | var assetName = AssetName; 33 | var oldPath = Path.Combine(oldInputDir, assetName); 34 | var newPath = Path.Combine(newOutputDir, assetName); 35 | var newTempPath = newPath + tempExt; 36 | var newDir = Path.GetDirectoryName(newPath); 37 | 38 | if (!Directory.Exists(newDir) && newDir != null) 39 | Directory.CreateDirectory(newDir); 40 | 41 | var oldInfo = new FileInfo(oldPath).UnassignReadOnlyFromFileInfo(); 42 | var newInfo = new FileInfo(newPath).UnassignReadOnlyFromFileInfo(); 43 | var newTempInfo = new FileInfo(newTempPath).UnassignReadOnlyFromFileInfo(); 44 | 45 | foreach (var chunk in Chunks) 46 | { 47 | await InnerWriteUpdateAsync(client, chunkDir, writeInfoDelegate, downloadInfoDelegate, DownloadSpeedLimiter, 48 | oldInfo, newTempInfo, chunk, removeChunkAfterApply, token); 49 | } 50 | 51 | GC.Collect(); 52 | GC.WaitForPendingFinalizers(); 53 | 54 | if (newTempInfo.FullName != newInfo.FullName) 55 | newTempInfo.Refresh(); 56 | 57 | downloadCompleteDelegate?.Invoke(this); 58 | } 59 | 60 | public async ValueTask WriteUpdateAsync(HttpClient client, 61 | string oldInputDir, 62 | string newOutputDir, 63 | string chunkDir, 64 | bool removeChunkAfterApply = false, 65 | ParallelOptions parallelOptions = null, 66 | DelegateWriteStreamInfo writeInfoDelegate = null, 67 | DelegateWriteDownloadInfo downloadInfoDelegate = null, 68 | DelegateDownloadAssetComplete downloadCompleteDelegate = null) 69 | { 70 | const string tempExt = "_tempUpdate"; 71 | 72 | this.EnsureOrThrowChunksState(); 73 | this.EnsureOrThrowOutputDirectoryExistence(oldInputDir); 74 | this.EnsureOrThrowOutputDirectoryExistence(newOutputDir); 75 | this.EnsureOrThrowOutputDirectoryExistence(chunkDir); 76 | 77 | var assetName = AssetName; 78 | var oldPath = Path.Combine(oldInputDir, assetName); 79 | var newPath = Path.Combine(newOutputDir, assetName); 80 | var newTempPath = newPath + tempExt; 81 | var newDir = Path.GetDirectoryName(newPath); 82 | 83 | if (!Directory.Exists(newDir) && newDir != null) 84 | Directory.CreateDirectory(newDir); 85 | 86 | parallelOptions ??= new ParallelOptions 87 | { 88 | CancellationToken = default, 89 | MaxDegreeOfParallelism = Math.Min(8, Environment.ProcessorCount) 90 | }; 91 | 92 | var oldInfo = new FileInfo(oldPath).UnassignReadOnlyFromFileInfo(); 93 | var newInfo = new FileInfo(newPath).UnassignReadOnlyFromFileInfo(); 94 | var newTempInfo = new FileInfo(newTempPath).UnassignReadOnlyFromFileInfo(); 95 | 96 | if (newInfo.Exists && newInfo.Length == AssetSize) 97 | newTempInfo = newInfo; 98 | 99 | await Parallel.ForEachAsync(Chunks, parallelOptions, async (chunk, ct) => 100 | { 101 | await InnerWriteUpdateAsync(client, chunkDir, writeInfoDelegate, downloadInfoDelegate, 102 | DownloadSpeedLimiter, oldInfo, newTempInfo, chunk, removeChunkAfterApply, ct); 103 | }); 104 | 105 | newTempInfo.Refresh(); 106 | newInfo.Refresh(); 107 | 108 | if (newTempInfo.FullName != newInfo.FullName && newTempInfo.Exists) 109 | { 110 | newInfo.Directory?.Create(); 111 | newTempInfo.MoveTo(newInfo.FullName, true); 112 | } 113 | 114 | downloadCompleteDelegate?.Invoke(this); 115 | } 116 | 117 | private async Task InnerWriteUpdateAsync(HttpClient client, string chunkDir, 118 | DelegateWriteStreamInfo writeInfoDelegate, DelegateWriteDownloadInfo downloadInfoDelegate, 119 | SophonDownloadSpeedLimiter downloadSpeedLimiter, FileInfo oldInfo, 120 | FileInfo newInfo, SophonChunk chunk, bool removeChunkAfterApply, CancellationToken token) 121 | { 122 | Stream input = null; 123 | Stream output = null; 124 | var streamType = SourceStreamType.Internet; 125 | 126 | try 127 | { 128 | if (chunk.ChunkOldOffset != -1 && oldInfo.Exists && oldInfo.Length >= chunk.ChunkOldOffset + chunk.ChunkSizeDecompressed) 129 | { 130 | input = oldInfo.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite); 131 | streamType = SourceStreamType.OldReference; 132 | } 133 | else 134 | { 135 | var name = chunk.GetChunkStagingFilenameHash(this); 136 | var path = Path.Combine(chunkDir, name); 137 | var verifiedPath = path + ".verified"; 138 | var info = new FileInfo(path).UnassignReadOnlyFromFileInfo(); 139 | 140 | if (info.Exists && info.Length != chunk.ChunkSize) 141 | info.Delete(); 142 | else if (info.Exists) 143 | { 144 | input = new FileStream(info.FullName, FileMode.Open, FileAccess.Read, 145 | FileShare.Read, 4096, removeChunkAfterApply ? FileOptions.DeleteOnClose : FileOptions.None); 146 | streamType = SourceStreamType.CachedLocal; 147 | if (File.Exists(verifiedPath)) File.Delete(verifiedPath); 148 | } 149 | } 150 | 151 | output = newInfo.Open(FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite); 152 | await PerformWriteStreamThreadAsync(client, input, streamType, output, chunk, token, 153 | writeInfoDelegate, downloadInfoDelegate, downloadSpeedLimiter); 154 | await output.DisposeAsync(); 155 | } 156 | finally 157 | { 158 | if (input != null) await input.DisposeAsync(); 159 | } 160 | } 161 | 162 | public async ValueTask GetDownloadedPreloadSize(string chunkDir, 163 | string outputDir, bool useCompressedSize, CancellationToken token = default) 164 | { 165 | var fullPath = Path.Combine(outputDir, AssetName); 166 | var info = new FileInfo(fullPath).UnassignReadOnlyFromFileInfo(); 167 | bool exists = info.Exists; 168 | long downloaded = exists ? info.Length : 0L; 169 | 170 | long GetLength(SophonChunk chunk) 171 | { 172 | var name = chunk.GetChunkStagingFilenameHash(this); 173 | var path = Path.Combine(chunkDir, name); 174 | var file = new FileInfo(path).UnassignReadOnlyFromFileInfo(); 175 | var size = useCompressedSize ? chunk.ChunkSize : chunk.ChunkSizeDecompressed; 176 | 177 | if (exists && downloaded == AssetSize && !file.Exists) return 0L; 178 | return file.Exists && file.Length <= chunk.ChunkSize ? size : 0L; 179 | } 180 | 181 | if (Chunks == null || Chunks.Length == 0) return 0L; 182 | if (Chunks.Length < 512) return Chunks.Select(GetLength).Sum(); 183 | 184 | var buffer = ArrayPool.Shared.Rent(Chunks.Length); 185 | try 186 | { 187 | await Task.Run(() => Parallel.For(0, Chunks.Length, i => buffer[i] = GetLength(Chunks[i])), token); 188 | return buffer.Sum(); 189 | } 190 | finally 191 | { 192 | ArrayPool.Shared.Return(buffer); 193 | } 194 | } 195 | } 196 | } -------------------------------------------------------------------------------- /Sophon/SophonUpdate.cs: -------------------------------------------------------------------------------- 1 | using Google.Protobuf.Collections; 2 | using Sophon.Helper; 3 | using Sophon.Infos; 4 | using Sophon.Protos; 5 | using Sophon.Structs; 6 | using System; 7 | using System.Collections.Generic; 8 | using System.Linq; 9 | using System.Net.Http; 10 | using System.Runtime.CompilerServices; 11 | using System.Threading; 12 | using System.Threading.Tasks; 13 | using TaskExtensions = Sophon.Helper.TaskExtensions; 14 | using ZstdNet; 15 | 16 | namespace Sophon 17 | { 18 | public static class SophonUpdate 19 | { 20 | private static readonly object DummyInstance = new(); 21 | 22 | public static async IAsyncEnumerable EnumerateUpdateAsync( 23 | HttpClient httpClient, 24 | SophonChunkManifestInfoPair infoPairOld, 25 | SophonChunkManifestInfoPair infoPairNew, 26 | bool removeChunkAfterApply, 27 | SophonDownloadSpeedLimiter downloadSpeedLimiter = null, 28 | [EnumeratorCancellation] CancellationToken token = default) 29 | { 30 | await foreach (SophonAsset asset in EnumerateUpdateAsync( 31 | httpClient, 32 | infoPairOld.ManifestInfo, 33 | infoPairOld.ChunksInfo, 34 | infoPairNew.ManifestInfo, 35 | infoPairNew.ChunksInfo, 36 | removeChunkAfterApply, 37 | downloadSpeedLimiter, 38 | token)) 39 | { 40 | yield return asset; 41 | } 42 | } 43 | 44 | public static async IAsyncEnumerable EnumerateUpdateAsync( 45 | HttpClient httpClient, 46 | SophonManifestInfo manifestInfoFrom, 47 | SophonChunksInfo chunksInfoFrom, 48 | SophonManifestInfo manifestInfoTo, 49 | SophonChunksInfo chunksInfoTo, 50 | bool removeChunkAfterApply, 51 | SophonDownloadSpeedLimiter downloadSpeedLimiter = null, 52 | [EnumeratorCancellation] CancellationToken token = default) 53 | { 54 | if (!DllUtils.IsLibraryExist(DllUtils.DllName)) 55 | throw new DllNotFoundException("libzstd is not found!"); 56 | 57 | var manifestFromProtoTaskCallback = new ActionTimeoutTaskCallback( 58 | async innerToken => await httpClient.ReadProtoFromManifestInfo(manifestInfoFrom, SophonManifestProto.Parser, innerToken) 59 | ); 60 | 61 | var manifestToProtoTaskCallback = new ActionTimeoutTaskCallback( 62 | async innerToken => await httpClient.ReadProtoFromManifestInfo(manifestInfoTo, SophonManifestProto.Parser, innerToken) 63 | ); 64 | 65 | SophonManifestProto manifestFromProto = await TaskExtensions.WaitForRetryAsync( 66 | () => manifestFromProtoTaskCallback, 67 | TaskExtensions.DefaultTimeoutSec, 68 | null, 69 | null, 70 | null, 71 | token); 72 | 73 | SophonManifestProto manifestToProto = await TaskExtensions.WaitForRetryAsync( 74 | () => manifestToProtoTaskCallback, 75 | TaskExtensions.DefaultTimeoutSec, 76 | null, 77 | null, 78 | null, 79 | token); 80 | 81 | var oldAssetNameIdx = GetProtoAssetHashKvpSet(manifestFromProto, x => x.AssetName); 82 | 83 | var oldAssetNameHashSet = manifestFromProto.Assets.Select(x => x.AssetName).ToHashSet(); 84 | var newAssetNameHashSet = manifestToProto.Assets.Select(x => x.AssetName).ToHashSet(); 85 | 86 | foreach (var newAssetProperty in manifestToProto.Assets.Where(x => 87 | { 88 | bool isOldExist = oldAssetNameHashSet.Contains(x.AssetName); 89 | bool isNewExist = newAssetNameHashSet.Contains(x.AssetName); 90 | return (!isOldExist && isNewExist) || isOldExist; 91 | })) 92 | { 93 | yield return GetPatchedTargetAsset( 94 | oldAssetNameIdx, 95 | manifestFromProto, 96 | newAssetProperty, 97 | chunksInfoFrom, 98 | chunksInfoTo, 99 | downloadSpeedLimiter); 100 | } 101 | } 102 | 103 | public static async ValueTask GetCalculatedDiffSizeAsync( 104 | this IAsyncEnumerable sophonAssetsEnumerable, 105 | bool isGetDecompressSize = true, 106 | CancellationToken token = default) 107 | { 108 | long sizeDiff = 0; 109 | 110 | await foreach (SophonAsset asset in sophonAssetsEnumerable.WithCancellation(token)) 111 | { 112 | if (asset.IsDirectory) continue; 113 | 114 | foreach (var chunk in asset.Chunks) 115 | { 116 | if (chunk.ChunkOldOffset != -1) continue; 117 | sizeDiff += isGetDecompressSize ? chunk.ChunkSizeDecompressed : chunk.ChunkSize; 118 | } 119 | } 120 | 121 | return sizeDiff; 122 | } 123 | 124 | public static long GetCalculatedDiffSize( 125 | this IEnumerable sophonAssetsEnumerable, 126 | bool isGetDecompressSize = true) 127 | { 128 | long sizeDiff = 0; 129 | 130 | foreach (SophonAsset asset in sophonAssetsEnumerable) 131 | { 132 | if (asset.IsDirectory) continue; 133 | 134 | foreach (var chunk in asset.Chunks) 135 | { 136 | if (chunk.ChunkOldOffset != -1) continue; 137 | sizeDiff += isGetDecompressSize ? chunk.ChunkSizeDecompressed : chunk.ChunkSize; 138 | } 139 | } 140 | 141 | return sizeDiff; 142 | } 143 | 144 | private static SophonAsset GetPatchedTargetAsset( 145 | Dictionary oldAssetNameIdx, 146 | SophonManifestProto oldAssetProto, 147 | SophonManifestAssetProperty newAssetProperty, 148 | SophonChunksInfo oldChunksInfo, 149 | SophonChunksInfo newChunksInfo, 150 | SophonDownloadSpeedLimiter downloadSpeedLimiter) 151 | { 152 | if (newAssetProperty.AssetType != 0 || 153 | string.IsNullOrEmpty(newAssetProperty.AssetHashMd5) || 154 | !oldAssetNameIdx.TryGetValue(newAssetProperty.AssetName, out int oldAssetIdx)) 155 | { 156 | return SophonManifest.AssetProperty2SophonAsset(newAssetProperty, newChunksInfo, downloadSpeedLimiter); 157 | } 158 | 159 | var oldAssetProperty = oldAssetProto.Assets[oldAssetIdx]; 160 | if (oldAssetProperty == null) 161 | { 162 | throw new NullReferenceException($"The old asset proto is null for: {newAssetProperty.AssetName} at index: {oldAssetIdx}"); 163 | } 164 | 165 | var patchedChunks = GetSophonChunkWithOldReference( 166 | oldAssetProperty.AssetChunks, 167 | newAssetProperty.AssetChunks, 168 | out bool isNewAssetHasPatch); 169 | 170 | return new SophonAsset 171 | { 172 | AssetName = newAssetProperty.AssetName, 173 | AssetHash = newAssetProperty.AssetHashMd5, 174 | AssetSize = newAssetProperty.AssetSize, 175 | Chunks = patchedChunks, 176 | SophonChunksInfo = newChunksInfo, 177 | SophonChunksInfoAlt = oldChunksInfo, 178 | IsDirectory = false, 179 | IsHasPatch = isNewAssetHasPatch, 180 | DownloadSpeedLimiter = downloadSpeedLimiter 181 | }; 182 | } 183 | 184 | private static SophonChunk[] GetSophonChunkWithOldReference( 185 | RepeatedField oldProtoChunks, 186 | RepeatedField newProtoChunks, 187 | out bool isNewAssetHasPatch) 188 | { 189 | int newLen = newProtoChunks.Count; 190 | var resultChunks = new SophonChunk[newLen]; 191 | isNewAssetHasPatch = false; 192 | 193 | var oldChunkIdx = new Dictionary(); 194 | for (int i = 0; i < oldProtoChunks.Count; i++) 195 | { 196 | if (!oldChunkIdx.TryAdd(oldProtoChunks[i].ChunkDecompressedHashMd5, i)) 197 | DummyInstance.PushLogWarning($"Chunk: {oldProtoChunks[i].ChunkName} is duplicated!"); 198 | } 199 | 200 | for (int i = 0; i < newLen; i++) 201 | { 202 | var newChunkProto = newProtoChunks[i]; 203 | var chunk = new SophonChunk 204 | { 205 | ChunkName = newChunkProto.ChunkName, 206 | ChunkHashDecompressed = Extension.HexToBytes(newChunkProto.ChunkDecompressedHashMd5.AsSpan()), 207 | ChunkOldOffset = -1, 208 | ChunkOffset = newChunkProto.ChunkOnFileOffset, 209 | ChunkSize = newChunkProto.ChunkSize, 210 | ChunkSizeDecompressed = newChunkProto.ChunkSizeDecompressed 211 | }; 212 | 213 | if (oldChunkIdx.TryGetValue(newChunkProto.ChunkDecompressedHashMd5, out int oldIdx)) 214 | { 215 | isNewAssetHasPatch = true; 216 | chunk.ChunkOldOffset = oldProtoChunks[oldIdx].ChunkOnFileOffset; 217 | } 218 | 219 | resultChunks[i] = chunk; 220 | } 221 | 222 | return resultChunks; 223 | } 224 | 225 | private static Dictionary GetProtoAssetHashKvpSet( 226 | SophonManifestProto proto, 227 | Func funcDelegate) 228 | { 229 | var dict = new Dictionary(); 230 | for (int i = 0; i < proto.Assets.Count; i++) 231 | { 232 | dict[funcDelegate(proto.Assets[i])] = i; 233 | } 234 | 235 | return dict; 236 | } 237 | } 238 | } 239 | -------------------------------------------------------------------------------- /Sophon/SophonAsset.Diff.cs: -------------------------------------------------------------------------------- 1 | using Sophon.Helper; 2 | using Sophon.Structs; 3 | using System; 4 | using System.Buffers; 5 | using System.Diagnostics; 6 | using System.IO; 7 | using System.Linq; 8 | using System.Net.Http; 9 | using System.Threading; 10 | using System.Threading.Tasks; 11 | using TaskExtensions = Sophon.Helper.TaskExtensions; 12 | using ZstdStream = ZstdNet.DecompressionStream; 13 | 14 | namespace Sophon 15 | { 16 | public partial class SophonAsset 17 | { 18 | private int _countChunksDownload; 19 | private int _currentChunksDownloadPos; 20 | private int _currentChunksDownloadQueue; 21 | 22 | public async ValueTask DownloadDiffChunksAsync( 23 | HttpClient client, 24 | string chunkDirOutput, 25 | ParallelOptions parallelOptions = null, 26 | DelegateWriteStreamInfo writeInfo = null, 27 | DelegateWriteDownloadInfo reportInfo = null, 28 | DelegateDownloadAssetComplete onComplete = null, 29 | bool forceVerification = false) 30 | { 31 | this.EnsureOrThrowChunksState(); 32 | this.EnsureOrThrowOutputDirectoryExistence(chunkDirOutput); 33 | 34 | _currentChunksDownloadPos = 0; 35 | _countChunksDownload = Chunks.Length; 36 | 37 | parallelOptions ??= new ParallelOptions 38 | { 39 | CancellationToken = CancellationToken.None, 40 | MaxDegreeOfParallelism = Math.Min(8, Environment.ProcessorCount) 41 | }; 42 | 43 | try 44 | { 45 | await Parallel.ForEachAsync(Chunks, parallelOptions, async (chunk, token) => 46 | { 47 | if (chunk.ChunkOldOffset > -1) return; 48 | await PerformWriteDiffChunksThreadAsync(client, chunkDirOutput, chunk, writeInfo, reportInfo, DownloadSpeedLimiter, forceVerification, token).ConfigureAwait(false); 49 | }).ConfigureAwait(false); 50 | } 51 | catch (AggregateException ex) 52 | { 53 | throw ex.Flatten().InnerExceptions.First(); 54 | } 55 | 56 | onComplete?.Invoke(this); 57 | } 58 | 59 | private async ValueTask PerformWriteDiffChunksThreadAsync( 60 | HttpClient client, 61 | string chunkDirOutput, 62 | SophonChunk chunk, 63 | DelegateWriteStreamInfo writeInfo, 64 | DelegateWriteDownloadInfo reportInfo, 65 | SophonDownloadSpeedLimiter limiter, 66 | bool forceVerification, 67 | CancellationToken token) 68 | { 69 | string chunkName = chunk.ChunkName; 70 | string chunkPath = Path.Combine(chunkDirOutput, chunk.GetChunkStagingFilenameHash(this)); 71 | string verifiedPath = chunkPath + ".verified"; 72 | FileInfo chunkFile = new FileInfo(chunkPath).UnassignReadOnlyFromFileInfo(); 73 | 74 | try 75 | { 76 | Interlocked.Increment(ref _currentChunksDownloadPos); 77 | Interlocked.Increment(ref _currentChunksDownloadQueue); 78 | 79 | using FileStream fs = chunkFile.Open(FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite); 80 | long chunkSize = chunk.ChunkSize; 81 | 82 | bool isMismatch = fs.Length != chunkSize; 83 | bool isVerified = File.Exists(verifiedPath) && !isMismatch; 84 | 85 | if (forceVerification || !isVerified) 86 | { 87 | isMismatch = !(chunkName.TryGetChunkXxh64Hash(out var hash) && 88 | await chunk.CheckChunkXxh64HashAsync(AssetName, fs, hash, true, token).ConfigureAwait(false)); 89 | 90 | if (File.Exists(verifiedPath)) File.Delete(verifiedPath); 91 | } 92 | 93 | if (!isMismatch) 94 | { 95 | writeInfo?.Invoke(chunkSize); 96 | reportInfo?.Invoke(chunkSize, 0); 97 | EnsureVerified(verifiedPath); 98 | return; 99 | } 100 | 101 | fs.Position = 0; 102 | await InnerWriteChunkCopyAsync(client, fs, chunk, token, writeInfo, reportInfo, limiter).ConfigureAwait(false); 103 | EnsureVerified(verifiedPath); 104 | } 105 | finally 106 | { 107 | Interlocked.Decrement(ref _currentChunksDownloadQueue); 108 | } 109 | } 110 | 111 | private async ValueTask InnerWriteChunkCopyAsync( 112 | HttpClient client, 113 | Stream outStream, 114 | SophonChunk chunk, 115 | CancellationToken token, 116 | DelegateWriteStreamInfo writeInfo, 117 | DelegateWriteDownloadInfo reportInfo, 118 | SophonDownloadSpeedLimiter limiter) 119 | { 120 | const int retryCount = TaskExtensions.DefaultRetryAttempt; 121 | int currentRetry = 0; 122 | long currentWriteOffset = 0; 123 | long written = 0; 124 | long chunkSize = chunk.ChunkSize; 125 | long chunkOffset = chunk.ChunkOffset; 126 | long chunkSizeDecompressed = chunk.ChunkSizeDecompressed; 127 | string chunkName = chunk.ChunkName; 128 | 129 | if (OperatingSystem.IsWindows() && outStream is FileStream fs) 130 | { 131 | fs.Lock(chunkOffset, chunkSizeDecompressed); 132 | this.PushLogDebug($"Locked stream from 0x{chunkOffset:x8} for length 0x{chunkSizeDecompressed:x8} ({chunkName})"); 133 | } 134 | 135 | long limitBase = limiter?.InitialRequestedSpeed ?? -1; 136 | Stopwatch sw = Stopwatch.StartNew(); 137 | double maxBps = 0, bitUnit = 0; 138 | CalculateBps(); 139 | 140 | if (limiter != null) 141 | { 142 | limiter.CurrentChunkProcessingChangedEvent += (_, _) => CalculateBps(); 143 | limiter.DownloadSpeedChangedEvent += (_, e) => { limitBase = e == 0 ? -1 : e; CalculateBps(); }; 144 | } 145 | 146 | while (true) 147 | { 148 | HttpResponseMessage resp = null; 149 | Stream httpStream = null; 150 | byte[] buffer = ArrayPool.Shared.Rent(BufferSize); 151 | 152 | try 153 | { 154 | using var timeoutCts = new CancellationTokenSource(TimeSpan.FromSeconds(TaskExtensions.DefaultTimeoutSec)); 155 | using var linkedCts = CancellationTokenSource.CreateLinkedTokenSource(token, timeoutCts.Token); 156 | 157 | outStream.SetLength(chunkSize); 158 | outStream.Position = 0; 159 | 160 | resp = await client.GetChunkAndIfAltAsync(chunkName, SophonChunksInfo, SophonChunksInfoAlt, linkedCts.Token).ConfigureAwait(false); 161 | httpStream = await resp.EnsureSuccessStatusCode().Content.ReadAsStreamAsync(linkedCts.Token).ConfigureAwait(false); 162 | 163 | int read; 164 | while ((read = await httpStream.ReadAsync(buffer.AsMemory(0, buffer.Length), linkedCts.Token).ConfigureAwait(false)) > 0) 165 | { 166 | await outStream.WriteAsync(buffer.AsMemory(0, read), linkedCts.Token).ConfigureAwait(false); 167 | currentWriteOffset += read; 168 | writeInfo?.Invoke(read); 169 | reportInfo?.Invoke(read, read); 170 | written += read; 171 | currentRetry = 0; 172 | await ThrottleAsync(); 173 | } 174 | 175 | outStream.Position = 0; 176 | var checkStream = outStream; 177 | 178 | bool isVerified = chunkName.TryGetChunkXxh64Hash(out var hash) 179 | ? await chunk.CheckChunkXxh64HashAsync(AssetName, checkStream, hash, true, linkedCts.Token).ConfigureAwait(false) 180 | : await chunk.CheckChunkMd5HashAsync(SophonChunksInfo.IsUseCompression ? new ZstdStream(checkStream) : checkStream, true, linkedCts.Token).ConfigureAwait(false); 181 | 182 | if (!isVerified) 183 | { 184 | writeInfo?.Invoke(-chunkSizeDecompressed); 185 | reportInfo?.Invoke(-chunkSizeDecompressed, 0); 186 | this.PushLogWarning($"Data corrupted. Retrying chunk {chunkName}..."); 187 | continue; 188 | } 189 | 190 | return; 191 | } 192 | catch (OperationCanceledException) when (token.IsCancellationRequested) 193 | { 194 | throw; 195 | } 196 | catch (Exception ex) 197 | { 198 | if (++currentRetry <= retryCount) 199 | { 200 | writeInfo?.Invoke(-currentWriteOffset); 201 | reportInfo?.Invoke(-currentWriteOffset, 0); 202 | currentWriteOffset = 0; 203 | this.PushLogWarning($"Error downloading chunk {chunkName}, retrying...\n{ex}"); 204 | await Task.Delay(TimeSpan.FromSeconds(1), token).ConfigureAwait(false); 205 | continue; 206 | } 207 | 208 | this.PushLogError($"Failed downloading chunk {chunkName}\n{ex}"); 209 | throw; 210 | } 211 | finally 212 | { 213 | resp?.Dispose(); 214 | if (httpStream != null) await httpStream.DisposeAsync().ConfigureAwait(false); 215 | ArrayPool.Shared.Return(buffer); 216 | limiter?.DecrementChunkProcessedCount(); 217 | } 218 | } 219 | 220 | void CalculateBps() 221 | { 222 | limitBase = limitBase <= 0 ? -1 : Math.Max(64 << 10, limitBase); 223 | double threadCount = Math.Clamp(limiter?.CurrentChunkProcessing ?? 1, 1, 16384); 224 | maxBps = limitBase / threadCount; 225 | bitUnit = 940 - (threadCount - 2) / (16d - 2d) * 400; 226 | } 227 | 228 | async Task ThrottleAsync() 229 | { 230 | if (maxBps <= 0 || written <= 0) return; 231 | 232 | long ms = sw.ElapsedMilliseconds; 233 | if (ms <= 0) return; 234 | 235 | double bps = written * bitUnit / ms; 236 | if (bps <= maxBps) return; 237 | 238 | double sleepMs = written * bitUnit / maxBps - ms; 239 | if (sleepMs > 1) 240 | { 241 | await Task.Delay(TimeSpan.FromMilliseconds(sleepMs), token).ConfigureAwait(false); 242 | sw.Restart(); 243 | written = 0; 244 | } 245 | } 246 | } 247 | 248 | private static void EnsureVerified(string path) 249 | { 250 | if (!File.Exists(path)) File.Create(path).Dispose(); 251 | } 252 | } 253 | } -------------------------------------------------------------------------------- /Sophon/SophonAsset.Download.cs: -------------------------------------------------------------------------------- 1 | using Sophon.Helper; 2 | using Sophon.Infos; 3 | using Sophon.Structs; 4 | using System; 5 | using System.Buffers; 6 | using System.Diagnostics; 7 | using System.IO; 8 | using System.Linq; 9 | using System.Net.Http; 10 | using System.Security.Cryptography; 11 | using System.Threading; 12 | using System.Threading.Tasks; 13 | using System.Threading.Tasks.Dataflow; 14 | using TaskExtensions = Sophon.Helper.TaskExtensions; 15 | using ZstdStream = ZstdNet.DecompressionStream; 16 | 17 | namespace Sophon 18 | { 19 | public partial class SophonAsset 20 | { 21 | private enum SourceStreamType { Internet, CachedLocal, OldReference } 22 | 23 | internal const int BufferSize = 256 << 10; 24 | private const int ZstdBufferSize = 0; 25 | 26 | public string AssetName { get; internal set; } 27 | public long AssetSize { get; internal set; } 28 | public string AssetHash { get; internal set; } 29 | public bool IsDirectory { get; internal set; } 30 | public bool IsHasPatch { get; internal set; } 31 | public SophonChunk[] Chunks { get; internal set; } 32 | internal SophonDownloadSpeedLimiter DownloadSpeedLimiter { get; set; } 33 | internal SophonChunksInfo SophonChunksInfo { get; set; } 34 | internal SophonChunksInfo SophonChunksInfoAlt { get; set; } 35 | 36 | public async ValueTask WriteToStreamAsync(HttpClient client, Stream outStream, DelegateWriteStreamInfo writeInfo = null, DelegateWriteDownloadInfo reportInfo = null, DelegateDownloadAssetComplete onComplete = null, CancellationToken token = default) 37 | { 38 | this.EnsureOrThrowChunksState(); 39 | this.EnsureOrThrowStreamState(outStream); 40 | if (outStream.Length > AssetSize) outStream.SetLength(AssetSize); 41 | 42 | foreach (var chunk in Chunks) 43 | await PerformWriteStreamThreadAsync(client, null, SourceStreamType.Internet, outStream, chunk, token, writeInfo, reportInfo, DownloadSpeedLimiter); 44 | 45 | onComplete?.Invoke(this); 46 | } 47 | 48 | public async ValueTask WriteToStreamAsync(HttpClient client, Func outStreamFunc, ParallelOptions parallelOptions = null, DelegateWriteStreamInfo writeInfo = null, DelegateWriteDownloadInfo reportInfo = null, DelegateDownloadAssetComplete onComplete = null) 49 | { 50 | this.EnsureOrThrowChunksState(); 51 | using var initStream = outStreamFunc(); 52 | this.EnsureOrThrowStreamState(initStream); 53 | if (initStream.Length > AssetSize) initStream.SetLength(AssetSize); 54 | 55 | parallelOptions ??= new ParallelOptions 56 | { 57 | CancellationToken = default, 58 | MaxDegreeOfParallelism = Math.Min(8, Environment.ProcessorCount) 59 | }; 60 | 61 | try 62 | { 63 | using var cts = new CancellationTokenSource(); 64 | using var linkedCts = CancellationTokenSource.CreateLinkedTokenSource(cts.Token, parallelOptions.CancellationToken); 65 | var block = new ActionBlock( 66 | async chunk => 67 | { 68 | using var stream = outStreamFunc(); 69 | await PerformWriteStreamThreadAsync(client, null, SourceStreamType.Internet, stream, chunk, linkedCts.Token, writeInfo, reportInfo, DownloadSpeedLimiter); 70 | }, 71 | new ExecutionDataflowBlockOptions 72 | { 73 | MaxDegreeOfParallelism = parallelOptions.MaxDegreeOfParallelism, 74 | CancellationToken = linkedCts.Token 75 | }); 76 | 77 | foreach (var chunk in Chunks) 78 | await block.SendAsync(chunk, linkedCts.Token); 79 | 80 | block.Complete(); 81 | await block.Completion; 82 | } 83 | catch (AggregateException ex) 84 | { 85 | throw ex.Flatten().InnerExceptions.First(); 86 | } 87 | 88 | onComplete?.Invoke(this); 89 | } 90 | 91 | private async ValueTask PerformWriteStreamThreadAsync(HttpClient client, Stream source, SourceStreamType type, Stream dest, SophonChunk chunk, CancellationToken token, DelegateWriteStreamInfo writeInfo, DelegateWriteDownloadInfo reportInfo, SophonDownloadSpeedLimiter limiter) 92 | { 93 | var (offset, size, hash, name, oldOffset) = (chunk.ChunkOffset, chunk.ChunkSizeDecompressed, chunk.ChunkHashDecompressed, chunk.ChunkName, chunk.ChunkOldOffset); 94 | bool skip = dest.Length >= offset + size && await chunk.CheckChunkMd5HashAsync(dest, false, token); 95 | 96 | if (skip) 97 | { 98 | writeInfo?.Invoke(size); 99 | reportInfo?.Invoke(oldOffset != -1 ? 0 : size, 0); 100 | return; 101 | } 102 | 103 | await InnerWriteStreamToAsync(client, source, type, dest, chunk, token, writeInfo, reportInfo, limiter); 104 | } 105 | 106 | private async ValueTask InnerWriteStreamToAsync(HttpClient client, Stream source, SourceStreamType type, Stream dest, SophonChunk chunk, CancellationToken token, DelegateWriteStreamInfo writeInfo, DelegateWriteDownloadInfo reportInfo, SophonDownloadSpeedLimiter limiter) 107 | { 108 | if ((type != SourceStreamType.Internet && source == null) || (type == SourceStreamType.OldReference && chunk.ChunkOldOffset < 0)) 109 | throw new InvalidOperationException("Invalid source stream or reference offset."); 110 | 111 | const int retryMax = TaskExtensions.DefaultRetryAttempt; 112 | int retry = 0; 113 | long offset = chunk.ChunkOffset, size = chunk.ChunkSizeDecompressed, written = 0; 114 | string name = chunk.ChunkName; 115 | 116 | if (OperatingSystem.IsWindows() && dest is FileStream fs) 117 | { 118 | fs.Lock(offset, size); 119 | this.PushLogDebug($"Locked stream 0x{offset:x8} -> 0x{size:x8} ({name})"); 120 | } 121 | 122 | long limit = limiter?.InitialRequestedSpeed ?? -1; 123 | Stopwatch sw = Stopwatch.StartNew(); 124 | double maxBps = 0, unit = 0; 125 | void CalcBps() 126 | { 127 | limit = limit <= 0 ? -1 : Math.Max(64 << 10, limit); 128 | var t = Math.Clamp(limiter?.CurrentChunkProcessing ?? 1, 1, 16384); 129 | maxBps = limit / t; 130 | unit = 940 - (t - 2) / 14d * 400; 131 | } 132 | 133 | if (limiter != null) 134 | { 135 | limiter.CurrentChunkProcessingChangedEvent += (_, _) => CalcBps(); 136 | limiter.DownloadSpeedChangedEvent += (_, e) => { limit = e == 0 ? -1 : e; CalcBps(); }; 137 | } 138 | CalcBps(); 139 | 140 | while (true) 141 | { 142 | HttpResponseMessage resp = null; 143 | Stream net = null; 144 | using MD5 md5 = MD5.Create(); 145 | byte[] buf = ArrayPool.Shared.Rent(BufferSize); 146 | try 147 | { 148 | using var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(TaskExtensions.DefaultTimeoutSec)); 149 | using var cts = CancellationTokenSource.CreateLinkedTokenSource(token, timeout.Token); 150 | dest.Position = offset; 151 | 152 | if (type == SourceStreamType.Internet) 153 | { 154 | limiter?.IncrementChunkProcessedCount(); 155 | resp = await client.GetChunkAndIfAltAsync(name, SophonChunksInfo, SophonChunksInfoAlt, cts.Token); 156 | net = await resp.EnsureSuccessStatusCode().Content.ReadAsStreamAsync(cts.Token); 157 | source = SophonChunksInfo.IsUseCompression ? new ZstdStream(net, ZstdBufferSize) : net; 158 | } 159 | else if (type == SourceStreamType.CachedLocal && SophonChunksInfo.IsUseCompression) 160 | source = new ZstdStream(source, ZstdBufferSize); 161 | else if (type == SourceStreamType.OldReference) 162 | source.Position = chunk.ChunkOldOffset; 163 | 164 | long remain = size, current = 0; 165 | while (remain > 0) 166 | { 167 | int read = await source.ReadAsync(buf.AsMemory(0, (int)Math.Min(remain, buf.Length)), cts.Token); 168 | if (read == 0) throw new InvalidDataException($"Corrupted chunk {name}. Remain: {remain}"); 169 | await dest.WriteAsync(buf.AsMemory(0, read), cts.Token); 170 | md5.TransformBlock(buf, 0, read, buf, 0); 171 | remain -= read; 172 | current += read; 173 | writeInfo?.Invoke(read); 174 | if (type != SourceStreamType.OldReference) 175 | reportInfo?.Invoke(read, type == SourceStreamType.Internet ? read : 0); 176 | if (type == SourceStreamType.Internet) { written += read; await Throttle(); } 177 | } 178 | 179 | md5.TransformFinalBlock(buf, 0, 0); 180 | if (!md5.Hash.AsSpan().SequenceEqual(chunk.ChunkHashDecompressed)) 181 | { 182 | writeInfo?.Invoke(-current); 183 | if (type != SourceStreamType.OldReference) reportInfo?.Invoke(-current, 0); 184 | this.PushLogWarning($"Corrupt source {type} at {name}. Retrying..."); 185 | type = SourceStreamType.Internet; 186 | continue; 187 | } 188 | 189 | return; 190 | } 191 | catch (Exception ex) when (++retry <= retryMax) 192 | { 193 | writeInfo?.Invoke(-size); 194 | if (type != SourceStreamType.OldReference) reportInfo?.Invoke(-size, 0); 195 | this.PushLogWarning($"Retry {retry}/{retryMax} failed for {name}: {ex.Message}"); 196 | await Task.Delay(1000, token); 197 | type = SourceStreamType.Internet; 198 | continue; 199 | } 200 | finally 201 | { 202 | if (type == SourceStreamType.Internet) limiter?.DecrementChunkProcessedCount(); 203 | if (net != null) await net.DisposeAsync(); 204 | if (source != null && source != net) await source.DisposeAsync(); 205 | ArrayPool.Shared.Return(buf); 206 | } 207 | } 208 | 209 | async Task Throttle() 210 | { 211 | if (maxBps <= 0 || written <= 0) return; 212 | long ms = sw.ElapsedMilliseconds; 213 | if (ms <= 0) return; 214 | double bps = written * unit / ms; 215 | if (bps > maxBps) 216 | { 217 | double sleep = written * unit / maxBps - ms; 218 | if (sleep > 1) 219 | { 220 | await Task.Delay(TimeSpan.FromMilliseconds(sleep), token); 221 | sw.Restart(); 222 | written = 0; 223 | } 224 | } 225 | } 226 | } 227 | } 228 | } -------------------------------------------------------------------------------- /Sophon/Helper/Extension.cs: -------------------------------------------------------------------------------- 1 | using Google.Protobuf; 2 | using Sophon.Infos; 3 | using Sophon.Structs; 4 | using System; 5 | using System.Buffers; 6 | using System.IO; 7 | using System.IO.Hashing; 8 | using System.Net.Http; 9 | using System.Security.Cryptography; 10 | using System.Text; 11 | using System.Threading; 12 | using System.Threading.Tasks; 13 | using ZstdStream = ZstdNet.DecompressionStream; 14 | 15 | namespace Sophon.Helper 16 | { 17 | internal static class Extension 18 | { 19 | private static readonly object DummyInstance = new(); 20 | 21 | private static readonly byte[] LookupFromHexTable = new byte[] 22 | { 23 | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 24 | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 25 | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 26 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,255,255,255,255,255,255, 27 | 255, 10, 11, 12, 13, 14, 15,255,255,255,255,255,255,255,255,255, 28 | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 29 | 255, 10, 11, 12, 13, 14, 15 30 | }; 31 | 32 | private static readonly byte[] LookupFromHexTable16 = new byte[] 33 | { 34 | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 35 | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 36 | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 37 | 0, 16, 32, 48, 64, 80, 96,112,128,144,255,255,255,255,255,255, 38 | 255,160,176,192,208,224,240,255,255,255,255,255,255,255,255,255, 39 | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 40 | 255,160,176,192,208,224,240 41 | }; 42 | 43 | internal static unsafe byte[] HexToBytes(ReadOnlySpan source) 44 | { 45 | if (source.IsEmpty) return Array.Empty(); 46 | if (source.Length % 2 == 1) throw new ArgumentException(); 47 | 48 | int index = 0; 49 | int len = source.Length >> 1; 50 | 51 | fixed (char* sourceRef = source) 52 | { 53 | if (*(int*)sourceRef == 7864368) 54 | { 55 | if (source.Length == 2) 56 | throw new ArgumentException(); 57 | 58 | index += 2; 59 | len -= 1; 60 | } 61 | 62 | byte[] result = new byte[len]; 63 | 64 | fixed (byte* hiRef = LookupFromHexTable16) 65 | fixed (byte* lowRef = LookupFromHexTable) 66 | fixed (byte* resultRef = result) 67 | { 68 | char* s = &sourceRef[index]; 69 | byte* r = resultRef; 70 | 71 | while (*s != 0) 72 | { 73 | byte add; 74 | if (*s > 102 || (*r = hiRef[*s++]) == 255 || *s > 102 || (add = lowRef[*s++]) == 255) 75 | throw new ArgumentException(); 76 | 77 | *r++ += add; 78 | } 79 | 80 | return result; 81 | } 82 | } 83 | } 84 | 85 | internal static string BytesToHex(ReadOnlySpan bytes) 86 | => Convert.ToHexStringLower(bytes); 87 | 88 | internal static SophonChunk SophonPatchAssetAsChunk(this SophonPatchAsset asset, bool fromOriginalFile, bool fromTargetFile, bool isCompressed = false) 89 | { 90 | byte[] hash = HexToBytes((fromOriginalFile ? asset.OriginalFileHash : fromTargetFile ? asset.TargetFileHash : asset.PatchHash).AsSpan()); 91 | string fileName = fromOriginalFile ? asset.OriginalFilePath : fromTargetFile ? asset.TargetFilePath : asset.PatchNameSource; 92 | long fileSize = fromOriginalFile ? asset.OriginalFileSize : fromTargetFile ? asset.TargetFileSize : asset.PatchSize; 93 | 94 | return new SophonChunk 95 | { 96 | ChunkHashDecompressed = hash, 97 | ChunkName = fileName, 98 | ChunkOffset = 0, 99 | ChunkOldOffset = 0, 100 | ChunkSize = fileSize, 101 | ChunkSizeDecompressed = fileSize 102 | }; 103 | } 104 | 105 | internal static async ValueTask CheckChunkXxh64HashAsync( 106 | this SophonChunk chunk, 107 | string assetName, 108 | Stream outStream, 109 | byte[] chunkXxh64Hash, 110 | bool isSingularStream, 111 | CancellationToken token) 112 | { 113 | try 114 | { 115 | var hash = new XxHash64(); 116 | if (!isSingularStream) 117 | outStream.Position = chunk.ChunkOffset; 118 | 119 | await hash.AppendAsync(outStream, token); 120 | 121 | return hash.GetHashAndReset() 122 | .AsSpan() 123 | .SequenceEqual(chunkXxh64Hash); 124 | } 125 | catch (Exception ex) when (!token.IsCancellationRequested) 126 | { 127 | DummyInstance.PushLogWarning( 128 | $"An error occurred while checking XXH64 hash for chunk: {chunk.ChunkName} | 0x{chunk.ChunkOffset:x8} -> L: 0x{chunk.ChunkSizeDecompressed:x8} for: {assetName}\r\n{ex}"); 129 | return false; 130 | } 131 | } 132 | 133 | internal static async ValueTask CheckChunkMd5HashAsync( 134 | this SophonChunk chunk, 135 | Stream outStream, 136 | bool isSingularStream, 137 | CancellationToken token) 138 | { 139 | byte[] buffer = ArrayPool.Shared.Rent(SophonAsset.BufferSize); 140 | int bufferSize = buffer.Length; 141 | using var hash = MD5.Create(); 142 | 143 | try 144 | { 145 | outStream.Position = chunk.ChunkOffset; 146 | long remain = chunk.ChunkSizeDecompressed; 147 | 148 | while (remain > 0) 149 | { 150 | int toRead = (int)Math.Min(bufferSize, remain); 151 | int read = await outStream.ReadAsync(buffer.AsMemory(0, toRead), token); 152 | hash.TransformBlock(buffer, 0, read, buffer, 0); 153 | remain -= read; 154 | } 155 | 156 | hash.TransformFinalBlock(buffer, 0, 0); 157 | return hash.Hash.AsSpan().SequenceEqual(chunk.ChunkHashDecompressed); 158 | } 159 | finally 160 | { 161 | ArrayPool.Shared.Return(buffer); 162 | } 163 | } 164 | 165 | internal static unsafe string GetChunkStagingFilenameHash(this SophonChunk chunk, SophonAsset asset) 166 | { 167 | string concatName = $"{asset.AssetName}${asset.AssetHash}${chunk.ChunkName}"; 168 | byte[] concatBuffer = ArrayPool.Shared.Rent(concatName.Length); 169 | byte[] hash = ArrayPool.Shared.Rent(16); 170 | 171 | try 172 | { 173 | fixed (char* strPtr = concatName) 174 | fixed (byte* bufPtr = concatBuffer) 175 | { 176 | int written = Encoding.UTF8.GetBytes(strPtr, concatName.Length, bufPtr, concatBuffer.Length); 177 | XxHash128.Hash(concatBuffer.AsSpan(0, written), hash); 178 | return BytesToHex(hash); 179 | } 180 | } 181 | finally 182 | { 183 | ArrayPool.Shared.Return(concatBuffer); 184 | ArrayPool.Shared.Return(hash); 185 | } 186 | } 187 | 188 | internal static bool TryGetChunkXxh64Hash(this string fileName, out byte[] outHash) 189 | { 190 | outHash = null; 191 | Span ranges = stackalloc Range[2]; 192 | if (fileName.AsSpan().Split(ranges, '_') != 2) return false; 193 | 194 | var chunkHashSpan = fileName.AsSpan()[ranges[0]]; 195 | if (chunkHashSpan.Length != 16) return false; 196 | 197 | outHash = HexToBytes(chunkHashSpan); 198 | return true; 199 | } 200 | 201 | internal static void EnsureOrThrowOutputDirectoryExistence(this SophonAsset asset, string outputDirPath) 202 | { 203 | if (string.IsNullOrEmpty(outputDirPath)) 204 | throw new ArgumentNullException(nameof(asset), "Directory path cannot be empty or null!"); 205 | 206 | if (!Directory.Exists(outputDirPath)) 207 | throw new DirectoryNotFoundException($"Directory path: {outputDirPath} does not exist!"); 208 | } 209 | 210 | internal static void EnsureOrThrowChunksState(this SophonAsset asset) 211 | { 212 | if (asset.Chunks == null) 213 | throw new NullReferenceException("This asset does not have chunk(s)!"); 214 | } 215 | 216 | internal static void EnsureOrThrowStreamState(this SophonAsset asset, Stream outStream) 217 | { 218 | if (outStream == null) 219 | throw new NullReferenceException("Output stream cannot be null!"); 220 | 221 | if (!outStream.CanRead) 222 | throw new NotSupportedException("Output stream must be readable!"); 223 | 224 | if (!outStream.CanWrite) 225 | throw new NotSupportedException("Output stream must be writable!"); 226 | 227 | if (!outStream.CanSeek) 228 | throw new NotSupportedException("Output stream must be seekable!"); 229 | } 230 | 231 | internal static FileInfo UnassignReadOnlyFromFileInfo(this FileInfo fileInfo) 232 | { 233 | if (fileInfo.Exists && fileInfo.IsReadOnly) 234 | fileInfo.IsReadOnly = false; 235 | 236 | return fileInfo; 237 | } 238 | 239 | internal static async Task GetChunkAndIfAltAsync( 240 | this HttpClient httpClient, 241 | string chunkName, 242 | SophonChunksInfo currentSophonChunkInfo, 243 | SophonChunksInfo altSophonChunkInfo, 244 | CancellationToken token = default) 245 | { 246 | string url = $"{currentSophonChunkInfo.ChunksBaseUrl.TrimEnd('/')}/{chunkName}"; 247 | HttpResponseMessage response = await httpClient.GetAsync(url, HttpCompletionOption.ResponseHeadersRead, token); 248 | 249 | if (response.IsSuccessStatusCode || altSophonChunkInfo == null) 250 | return response; 251 | 252 | response.Dispose(); 253 | return await httpClient.GetChunkAndIfAltAsync(chunkName, altSophonChunkInfo, null, token); 254 | } 255 | 256 | internal static async Task ReadProtoFromManifestInfo( 257 | this HttpClient httpClient, 258 | SophonManifestInfo manifestInfo, 259 | MessageParser messageParser, 260 | CancellationToken innerToken) 261 | where T : IMessage 262 | { 263 | using var response = await httpClient.GetAsync(manifestInfo.ManifestFileUrl, HttpCompletionOption.ResponseHeadersRead, innerToken); 264 | await using var protoStream = await response.EnsureSuccessStatusCode().Content.ReadAsStreamAsync(innerToken); 265 | await using var decompressedStream = manifestInfo.IsUseCompression ? new ZstdStream(protoStream) : protoStream; 266 | 267 | return await Task.Factory.StartNew(() => messageParser.ParseFrom(decompressedStream), innerToken, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default); 268 | } 269 | } 270 | } -------------------------------------------------------------------------------- /Sophon/SophonPatchAsset.Download.cs: -------------------------------------------------------------------------------- 1 | using Sophon.Helper; 2 | using Sophon.Infos; 3 | using Sophon.Structs; 4 | using System; 5 | using System.Buffers; 6 | using System.Diagnostics; 7 | using System.IO; 8 | using System.Net.Http; 9 | using System.Threading; 10 | using System.Threading.Tasks; 11 | using TaskExtensions = Sophon.Helper.TaskExtensions; 12 | using ZstdStream = ZstdNet.DecompressionStream; 13 | 14 | namespace Sophon 15 | { 16 | public enum SophonPatchMethod 17 | { 18 | CopyOver, 19 | DownloadOver, 20 | Patch, 21 | Remove 22 | } 23 | 24 | public partial class SophonPatchAsset 25 | { 26 | internal const int BufferSize = 256 << 10; 27 | 28 | public SophonChunksInfo PatchInfo { get; set; } 29 | public SophonPatchMethod PatchMethod { get; set; } 30 | public string PatchNameSource { get; set; } 31 | public string PatchHash { get; set; } 32 | public long PatchOffset { get; set; } 33 | public long PatchSize { get; set; } 34 | public long PatchChunkLength { get; set; } 35 | public string OriginalFilePath { get; set; } 36 | public string OriginalFileHash { get; set; } 37 | public long OriginalFileSize { get; set; } 38 | public string TargetFilePath { get; set; } 39 | public string TargetFileDownloadOverBaseUrl { get; set; } 40 | public string TargetFileHash { get; set; } 41 | public long TargetFileSize { get; set; } 42 | 43 | #nullable enable 44 | public async Task DownloadPatchAsync(HttpClient client, string patchOutputDir, bool forceVerification = false, Action? downloadReadDelegate = null, SophonDownloadSpeedLimiter? downloadSpeedLimiter = null, CancellationToken token = default) 45 | { 46 | if (PatchMethod is SophonPatchMethod.Remove or SophonPatchMethod.DownloadOver) return; 47 | 48 | string patchNameHashed = PatchNameSource; 49 | string patchFilePathHashed = Path.Combine(patchOutputDir, patchNameHashed); 50 | FileInfo patchFilePathHashedFileInfo = new FileInfo(patchFilePathHashed).UnassignReadOnlyFromFileInfo(); 51 | patchFilePathHashedFileInfo.Directory?.Create(); 52 | 53 | if (!PatchNameSource.TryGetChunkXxh64Hash(out byte[] patchHash)) 54 | patchHash = Extension.HexToBytes(PatchHash.AsSpan()); 55 | 56 | SophonChunk patchAsChunk = new SophonChunk 57 | { 58 | ChunkHashDecompressed = patchHash, 59 | ChunkName = PatchNameSource, 60 | ChunkOffset = 0, 61 | ChunkOldOffset = 0, 62 | ChunkSize = PatchSize, 63 | ChunkSizeDecompressed = PatchSize 64 | }; 65 | 66 | using FileStream fileStream = patchFilePathHashedFileInfo.Open(FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite); 67 | 68 | bool isPatchUnmatched = fileStream.Length != PatchSize; 69 | if (forceVerification) 70 | { 71 | isPatchUnmatched = patchHash.Length > 8 72 | ? !await patchAsChunk.CheckChunkMd5HashAsync(fileStream, true, token) 73 | : !await patchAsChunk.CheckChunkXxh64HashAsync(PatchNameSource, fileStream, patchHash, true, token); 74 | 75 | if (isPatchUnmatched) 76 | { 77 | fileStream.Position = 0; 78 | fileStream.SetLength(0); 79 | } 80 | } 81 | 82 | if (!isPatchUnmatched) 83 | { 84 | downloadReadDelegate?.Invoke(PatchSize); 85 | return; 86 | } 87 | 88 | fileStream.Position = 0; 89 | await InnerWriteChunkCopyAsync(client, fileStream, patchAsChunk, PatchInfo, PatchInfo, null, (_, y) => downloadReadDelegate?.Invoke(y), downloadSpeedLimiter, token); 90 | } 91 | 92 | private async Task InnerWriteChunkCopyAsync(HttpClient client, Stream outStream, SophonChunk chunk, SophonChunksInfo currentSophonChunkInfo, SophonChunksInfo altSophonChunkInfo, DelegateWriteStreamInfo? writeInfoDelegate, DelegateWriteDownloadInfo? downloadInfoDelegate, SophonDownloadSpeedLimiter? downloadSpeedLimiter, CancellationToken token) 93 | { 94 | const int retryCount = TaskExtensions.DefaultRetryAttempt; 95 | int currentRetry = 0; 96 | long currentWriteOffset = 0; 97 | 98 | #if !NOSTREAMLOCK 99 | if (outStream is FileStream fs) 100 | fs.Lock(chunk.ChunkOffset, chunk.ChunkSizeDecompressed); 101 | #endif 102 | 103 | long written = 0; 104 | long thisInstanceDownloadLimitBase = downloadSpeedLimiter?.InitialRequestedSpeed ?? -1; 105 | Stopwatch currentStopwatch = Stopwatch.StartNew(); 106 | 107 | double maximumBytesPerSecond; 108 | double bitPerUnit; 109 | 110 | CalculateBps(); 111 | 112 | if (downloadSpeedLimiter != null) 113 | { 114 | downloadSpeedLimiter.CurrentChunkProcessingChangedEvent += UpdateChunkRangesCountEvent; 115 | downloadSpeedLimiter.DownloadSpeedChangedEvent += DownloadClientDownloadSpeedLimitChanged; 116 | } 117 | 118 | while (true) 119 | { 120 | bool allowDispose = false; 121 | HttpResponseMessage? httpResponseMessage = null; 122 | Stream? httpResponseStream = null; 123 | Stream? sourceStream = null; 124 | byte[] buffer = ArrayPool.Shared.Rent(BufferSize); 125 | 126 | try 127 | { 128 | CancellationTokenSource innerTimeoutToken = new CancellationTokenSource(TimeSpan.FromSeconds(TaskExtensions.DefaultTimeoutSec)); 129 | CancellationTokenSource cooperatedToken = CancellationTokenSource.CreateLinkedTokenSource(token, innerTimeoutToken.Token); 130 | 131 | outStream.Position = 0; 132 | httpResponseMessage = await client.GetChunkAndIfAltAsync(chunk.ChunkName, currentSophonChunkInfo, altSophonChunkInfo, cooperatedToken.Token); 133 | httpResponseStream = await httpResponseMessage.EnsureSuccessStatusCode().Content.ReadAsStreamAsync(cooperatedToken.Token); 134 | 135 | sourceStream = httpResponseStream; 136 | downloadSpeedLimiter?.IncrementChunkProcessedCount(); 137 | int read; 138 | 139 | while ((read = await sourceStream.ReadAsync(buffer.AsMemory(0, BufferSize), cooperatedToken.Token)) > 0) 140 | { 141 | await outStream.WriteAsync(buffer.AsMemory(0, read), cooperatedToken.Token); 142 | currentWriteOffset += read; 143 | writeInfoDelegate?.Invoke(read); 144 | downloadInfoDelegate?.Invoke(read, read); 145 | written += read; 146 | currentRetry = 0; 147 | 148 | innerTimeoutToken.Dispose(); 149 | cooperatedToken.Dispose(); 150 | innerTimeoutToken = new CancellationTokenSource(TimeSpan.FromSeconds(TaskExtensions.DefaultTimeoutSec)); 151 | cooperatedToken = CancellationTokenSource.CreateLinkedTokenSource(token, innerTimeoutToken.Token); 152 | 153 | await ThrottleAsync(); 154 | } 155 | 156 | outStream.Position = 0; 157 | Stream checkHashStream = outStream; 158 | 159 | bool isHashVerified; 160 | if (chunk.ChunkName.TryGetChunkXxh64Hash(out byte[] outHash)) 161 | { 162 | isHashVerified = await chunk.CheckChunkXxh64HashAsync(TargetFilePath, checkHashStream, outHash, true, cooperatedToken.Token); 163 | } 164 | else 165 | { 166 | if (PatchInfo.IsUseCompression) 167 | checkHashStream = new ZstdStream(checkHashStream); 168 | 169 | isHashVerified = await chunk.CheckChunkMd5HashAsync(checkHashStream, true, cooperatedToken.Token); 170 | } 171 | 172 | if (!isHashVerified) 173 | { 174 | writeInfoDelegate?.Invoke(-chunk.ChunkSizeDecompressed); 175 | downloadInfoDelegate?.Invoke(-chunk.ChunkSizeDecompressed, 0); 176 | continue; 177 | } 178 | 179 | return; 180 | } 181 | catch (OperationCanceledException) when (token.IsCancellationRequested) 182 | { 183 | allowDispose = true; 184 | throw; 185 | } 186 | catch (Exception) 187 | { 188 | if (currentRetry < retryCount) 189 | { 190 | writeInfoDelegate?.Invoke(-currentWriteOffset); 191 | downloadInfoDelegate?.Invoke(-currentWriteOffset, 0); 192 | currentWriteOffset = 0; 193 | currentRetry++; 194 | await Task.Delay(TimeSpan.FromSeconds(1), token); 195 | continue; 196 | } 197 | 198 | allowDispose = true; 199 | throw; 200 | } 201 | finally 202 | { 203 | if (allowDispose) 204 | { 205 | httpResponseMessage?.Dispose(); 206 | if (httpResponseStream != null) await httpResponseStream.DisposeAsync(); 207 | if (sourceStream != null) await sourceStream.DisposeAsync(); 208 | } 209 | 210 | downloadSpeedLimiter?.DecrementChunkProcessedCount(); 211 | ArrayPool.Shared.Return(buffer); 212 | } 213 | } 214 | 215 | void CalculateBps() 216 | { 217 | if (thisInstanceDownloadLimitBase <= 0) 218 | thisInstanceDownloadLimitBase = -1; 219 | else 220 | thisInstanceDownloadLimitBase = Math.Max(64 << 10, thisInstanceDownloadLimitBase); 221 | 222 | double threadNum = Math.Clamp(downloadSpeedLimiter?.CurrentChunkProcessing ?? 1, 1, 16 << 10); 223 | maximumBytesPerSecond = thisInstanceDownloadLimitBase / threadNum; 224 | bitPerUnit = 940 - (threadNum - 2) / (16 - 2) * 400; 225 | } 226 | 227 | void DownloadClientDownloadSpeedLimitChanged(object? sender, long e) 228 | { 229 | thisInstanceDownloadLimitBase = e == 0 ? -1 : e; 230 | CalculateBps(); 231 | } 232 | 233 | void UpdateChunkRangesCountEvent(object? sender, int e) 234 | { 235 | CalculateBps(); 236 | } 237 | 238 | async Task ThrottleAsync() 239 | { 240 | if (maximumBytesPerSecond <= 0 || written <= 0) return; 241 | 242 | long elapsedMilliseconds = currentStopwatch.ElapsedMilliseconds; 243 | if (elapsedMilliseconds > 0) 244 | { 245 | double bps = written * bitPerUnit / elapsedMilliseconds; 246 | if (bps > maximumBytesPerSecond) 247 | { 248 | double wakeElapsed = written * bitPerUnit / maximumBytesPerSecond; 249 | double toSleep = wakeElapsed - elapsedMilliseconds; 250 | if (toSleep > 1) 251 | { 252 | await Task.Delay(TimeSpan.FromMilliseconds(toSleep), token); 253 | currentStopwatch.Restart(); 254 | written = 0; 255 | } 256 | } 257 | } 258 | } 259 | } 260 | } 261 | } -------------------------------------------------------------------------------- /Sophon/SophonPatchAsset.Update.cs: -------------------------------------------------------------------------------- 1 | using Sophon.Helper; 2 | using Sophon.Infos; 3 | using Sophon.Structs; 4 | using SharpHDiffPatch.Core; 5 | using System; 6 | using System.Buffers; 7 | using System.IO; 8 | using System.Net.Http; 9 | using System.Threading; 10 | using System.Threading.Tasks; 11 | 12 | #nullable enable 13 | namespace Sophon 14 | { 15 | public partial class SophonPatchAsset 16 | { 17 | public async Task ApplyPatchUpdateAsync(HttpClient client, 18 | string inputDir, 19 | string patchOutputDir, 20 | bool removeOldAssets = true, 21 | Action? downloadReadDelegate = null, 22 | Action? diskWriteDelegate = null, 23 | SophonDownloadSpeedLimiter? downloadSpeedLimiter = null, 24 | CancellationToken token = default) 25 | { 26 | bool isRemove = SophonPatchMethod.Remove == PatchMethod; 27 | bool isCopyOver = SophonPatchMethod.CopyOver == PatchMethod; 28 | bool isPatchHDiff = SophonPatchMethod.Patch == PatchMethod; 29 | string sourceFileNameToCheck = PatchMethod switch 30 | { 31 | SophonPatchMethod.Remove => OriginalFilePath, 32 | SophonPatchMethod.DownloadOver => TargetFilePath, 33 | SophonPatchMethod.Patch => OriginalFilePath, 34 | SophonPatchMethod.CopyOver => TargetFilePath, 35 | _ => throw new InvalidOperationException($"Unsupported patch method: {PatchMethod}") 36 | }; 37 | string sourceFilePathToCheck = Path.Combine(inputDir, sourceFileNameToCheck); 38 | 39 | if (isRemove) 40 | { 41 | if (!removeOldAssets) 42 | return; 43 | 44 | FileInfo removableAssetFileInfo = new FileInfo(sourceFilePathToCheck); 45 | PerformPatchAssetRemove(removableAssetFileInfo); 46 | return; 47 | } 48 | 49 | if (PatchMethod is SophonPatchMethod.DownloadOver or 50 | SophonPatchMethod.CopyOver or 51 | SophonPatchMethod.Patch && 52 | await IsFilePatched(inputDir, token)) 53 | { 54 | diskWriteDelegate?.Invoke(TargetFileSize); 55 | return; 56 | } 57 | 58 | if (!isCopyOver) 59 | { 60 | string sourceFileHashString = PatchMethod switch 61 | { 62 | SophonPatchMethod.Remove => OriginalFileHash, 63 | SophonPatchMethod.DownloadOver => TargetFileHash, 64 | SophonPatchMethod.Patch => OriginalFileHash, 65 | _ => throw new InvalidOperationException($"Unsupported patch method: {PatchMethod}") 66 | }; 67 | 68 | long sourceFileSizeToCheck = PatchMethod switch 69 | { 70 | SophonPatchMethod.Remove => OriginalFileSize, 71 | SophonPatchMethod.DownloadOver => TargetFileSize, 72 | SophonPatchMethod.Patch => OriginalFileSize, 73 | _ => throw new InvalidOperationException($"Unsupported patch method: {PatchMethod}") 74 | }; 75 | 76 | SophonChunk sourceFileToCheckAsChunk = new SophonChunk 77 | { 78 | ChunkHashDecompressed = Extension.HexToBytes(sourceFileHashString.AsSpan()), 79 | ChunkName = sourceFileNameToCheck, 80 | ChunkOffset = 0, 81 | ChunkOldOffset = 0, 82 | ChunkSize = sourceFileSizeToCheck, 83 | ChunkSizeDecompressed = sourceFileSizeToCheck 84 | }; 85 | 86 | FileInfo sourceFileInfoToCheck = new FileInfo(sourceFilePathToCheck); 87 | 88 | bool isNeedCompleteDownload = !(sourceFileInfoToCheck is { Exists: true } && 89 | sourceFileInfoToCheck.Length == sourceFileSizeToCheck); 90 | FileStream? sourceFileStreamToCheck = null; 91 | try 92 | { 93 | if (!isNeedCompleteDownload) 94 | { 95 | sourceFileStreamToCheck = sourceFileInfoToCheck.Open(FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite); 96 | isNeedCompleteDownload = !(sourceFileToCheckAsChunk.ChunkHashDecompressed.Length != 8 ? 97 | await sourceFileToCheckAsChunk.CheckChunkMd5HashAsync(sourceFileStreamToCheck, true, token) : 98 | 99 | await sourceFileToCheckAsChunk.CheckChunkXxh64HashAsync(OriginalFilePath, 100 | sourceFileStreamToCheck, sourceFileToCheckAsChunk.ChunkHashDecompressed, 101 | true, token)); 102 | 103 | if (isNeedCompleteDownload) 104 | { 105 | sourceFileStreamToCheck.Dispose(); 106 | PerformPatchAssetRemove(sourceFileInfoToCheck); 107 | } 108 | else 109 | { 110 | if (!isPatchHDiff) 111 | { 112 | diskWriteDelegate?.Invoke(TargetFileSize); 113 | return; 114 | } 115 | } 116 | } 117 | 118 | if (isNeedCompleteDownload) 119 | { 120 | PatchMethod = SophonPatchMethod.DownloadOver; 121 | } 122 | } 123 | finally 124 | { 125 | if (sourceFileStreamToCheck != null) 126 | await sourceFileStreamToCheck.DisposeAsync(); 127 | } 128 | } 129 | 130 | Task writeDelegateTask = PatchMethod switch 131 | { 132 | SophonPatchMethod.DownloadOver => PerformPatchDownloadOver(client, 133 | inputDir, 134 | downloadReadDelegate, 135 | diskWriteDelegate, 136 | downloadSpeedLimiter, 137 | token), 138 | SophonPatchMethod.CopyOver => PerformPatchCopyOver(inputDir, 139 | patchOutputDir, 140 | diskWriteDelegate, 141 | token), 142 | SophonPatchMethod.Patch => PerformPatchHDiff(inputDir, 143 | patchOutputDir, 144 | diskWriteDelegate, 145 | token), 146 | _ => throw new InvalidOperationException($"Invalid operation while performing patch: {PatchMethod}") 147 | }; 148 | 149 | await writeDelegateTask; 150 | } 151 | 152 | private async Task PerformPatchDownloadOver(HttpClient client, 153 | string inputDir, 154 | Action? downloadReadDelegate, 155 | Action? diskWriteDelegate, 156 | SophonDownloadSpeedLimiter? downloadSpeedLimiter, 157 | CancellationToken token) 158 | { 159 | string targetFilePath = Path.Combine(inputDir, TargetFilePath); 160 | FileInfo targetFileInfo = new FileInfo(targetFilePath); 161 | string targetFilePathTemp = targetFilePath + ".temp"; 162 | FileInfo targetFileInfoTemp = new FileInfo(targetFilePathTemp); 163 | 164 | if (targetFileInfoTemp.Exists) 165 | targetFileInfoTemp.IsReadOnly = false; 166 | 167 | targetFileInfoTemp.Directory?.Create(); 168 | FileStream targetFileStreamTemp = targetFileInfoTemp.Open(FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite); 169 | targetFileInfoTemp.Refresh(); 170 | try 171 | { 172 | SophonChunksInfo targetChunkInfo = PatchInfo.CopyWithNewBaseUrl(TargetFileDownloadOverBaseUrl); 173 | SophonChunk targetFileChunk = this.SophonPatchAssetAsChunk(false, true); 174 | 175 | await InnerWriteChunkCopyAsync(client, 176 | targetFileStreamTemp, 177 | targetFileChunk, 178 | targetChunkInfo, 179 | targetChunkInfo, 180 | writeInfoDelegate: x => 181 | { 182 | diskWriteDelegate?.Invoke(x); 183 | }, 184 | downloadInfoDelegate: (read, write) => 185 | { 186 | downloadReadDelegate?.Invoke(read); 187 | }, 188 | downloadSpeedLimiter, 189 | token: token); 190 | } 191 | finally 192 | { 193 | targetFileStreamTemp.Dispose(); 194 | if (targetFileInfo.Exists) 195 | { 196 | targetFileInfo.IsReadOnly = false; 197 | targetFileInfo.Refresh(); 198 | targetFileInfo.Delete(); 199 | } 200 | 201 | targetFileInfoTemp.MoveTo(targetFilePath); 202 | } 203 | } 204 | 205 | private void PerformPatchAssetRemove(FileInfo originalFileInfo) 206 | { 207 | try 208 | { 209 | if (!originalFileInfo.Exists) 210 | return; 211 | 212 | originalFileInfo.IsReadOnly = false; 213 | originalFileInfo.Refresh(); 214 | originalFileInfo.Delete(); 215 | 216 | this.PushLogDebug($"[Method: Remove] Removing asset file: {OriginalFilePath} is completed!"); 217 | } 218 | catch (Exception ex) 219 | { 220 | this.PushLogError($"An error has occurred while deleting old asset: {originalFileInfo.FullName} | {ex}"); 221 | } 222 | } 223 | 224 | private async Task PerformPatchCopyOver(string inputDir, 225 | string patchOutputDir, 226 | Action? diskWriteDelegate, 227 | CancellationToken token) 228 | { 229 | PatchTargetProperty patchTargetProperty = PatchTargetProperty.Create(patchOutputDir, PatchNameSource, inputDir, TargetFilePath, PatchOffset, PatchChunkLength, true); 230 | 231 | bool isUseCopyToStrategy = PatchChunkLength <= 1 << 20; 232 | 233 | string logMessage = $"[Method: CopyOver][Strategy: {(isUseCopyToStrategy ? "DirectCopyTo" : "BufferedCopy")}] Writing target file: {TargetFilePath} with offset: {PatchOffset:x8} and length: {PatchChunkLength:x8} from {PatchNameSource} is completed!"; 234 | 235 | try 236 | { 237 | if (patchTargetProperty.TargetFileTempStream == null) 238 | { 239 | ArgumentNullException.ThrowIfNull(patchTargetProperty.TargetFileTempStream, 240 | nameof(patchTargetProperty.TargetFileTempStream)); 241 | } 242 | if (patchTargetProperty.PatchChunkStream == null) 243 | { 244 | ArgumentNullException.ThrowIfNull(patchTargetProperty.PatchChunkStream, 245 | nameof(patchTargetProperty.PatchChunkStream)); 246 | } 247 | 248 | if (isUseCopyToStrategy) 249 | { 250 | await patchTargetProperty.PatchChunkStream.CopyToAsync(patchTargetProperty.TargetFileTempStream, token); 251 | diskWriteDelegate?.Invoke(PatchChunkLength); 252 | return; 253 | } 254 | 255 | byte[] buffer = ArrayPool.Shared.Rent(16 << 10); 256 | 257 | try 258 | { 259 | int read; 260 | while ((read = await patchTargetProperty.PatchChunkStream.ReadAsync(buffer, token)) > 0) 261 | { 262 | await patchTargetProperty.TargetFileTempStream.WriteAsync(buffer.AsMemory(0, read), token); 263 | diskWriteDelegate?.Invoke(read); 264 | } 265 | } 266 | finally 267 | { 268 | ArrayPool.Shared.Return(buffer); 269 | } 270 | } 271 | finally 272 | { 273 | this.PushLogDebug(logMessage); 274 | patchTargetProperty.Dispose(); 275 | } 276 | } 277 | 278 | private async Task PerformPatchHDiff(string inputDir, 279 | string patchOutputDir, 280 | Action? diskWriteDelegate, 281 | CancellationToken token) 282 | { 283 | PatchTargetProperty patchTargetProperty = PatchTargetProperty.Create(patchOutputDir, PatchNameSource, inputDir, TargetFilePath, PatchOffset, PatchChunkLength, false); 284 | string logMessage = $"[Method: PatchHDiff] Writing target file: {TargetFilePath} with offset: {PatchOffset:x8} and length: {PatchChunkLength:x8} from {PatchNameSource} is completed!"; 285 | string patchPath = patchTargetProperty.PatchFilePath; 286 | string targetTempPath = patchTargetProperty.TargetFileTempInfo.FullName; 287 | 288 | try 289 | { 290 | await Task.Factory 291 | .StartNew(Impl, token, token, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default) 292 | .ConfigureAwait(false); 293 | this.PushLogDebug(logMessage); 294 | } 295 | finally 296 | { 297 | patchTargetProperty.Dispose(); 298 | } 299 | 300 | return; 301 | 302 | void Impl(object? ctx) 303 | { 304 | HDiffPatch patcher = new HDiffPatch(); 305 | try 306 | { 307 | patcher.Initialize(CreateChunkStream); 308 | 309 | string inputPath = Path.Combine(inputDir, OriginalFilePath); 310 | patcher.Patch(inputPath, targetTempPath, true, diskWriteDelegate, (CancellationToken)ctx!, false, true); 311 | } 312 | catch (Exception ex) 313 | { 314 | this.PushLogDebug($"[Method: PatchHDiff] An error occurred while trying to perform patching on: {OriginalFilePath} -> {TargetFilePath}\r\n{ex}"); 315 | } 316 | } 317 | 318 | ChunkStream CreateChunkStream() 319 | { 320 | FileStream fileStream = File.Open(patchPath, FileMode.Open, FileAccess.Read, FileShare.Read); 321 | ChunkStream chunkStream = new ChunkStream(fileStream, PatchOffset, PatchOffset + PatchChunkLength, true); 322 | 323 | return chunkStream; 324 | } 325 | } 326 | 327 | private async Task IsFilePatched(string inputPath, CancellationToken token) 328 | { 329 | string targetFilePath = Path.Combine(inputPath, TargetFilePath); 330 | FileInfo targetFileInfo = new FileInfo(targetFilePath); 331 | 332 | bool isSizeMatched = targetFileInfo.Exists && TargetFileSize == targetFileInfo.Length; 333 | if (!isSizeMatched) 334 | { 335 | return false; 336 | } 337 | 338 | SophonChunk checkByHashChunk = new SophonChunk 339 | { 340 | ChunkHashDecompressed = Extension.HexToBytes(TargetFileHash.AsSpan()), 341 | ChunkName = TargetFilePath, 342 | ChunkSize = TargetFileSize, 343 | ChunkSizeDecompressed = TargetFileSize, 344 | ChunkOffset = 0, 345 | ChunkOldOffset = 0, 346 | }; 347 | 348 | using FileStream targetFileStream = targetFileInfo.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite); 349 | bool isHashMatched = checkByHashChunk.ChunkHashDecompressed.Length == 8 ? 350 | await checkByHashChunk.CheckChunkXxh64HashAsync(TargetFilePath, 351 | targetFileStream, 352 | checkByHashChunk.ChunkHashDecompressed, 353 | true, 354 | token) : 355 | await checkByHashChunk.CheckChunkMd5HashAsync(targetFileStream, 356 | true, 357 | token); 358 | 359 | return isHashMatched; 360 | } 361 | } 362 | 363 | internal class PatchTargetProperty : IDisposable 364 | { 365 | private FileInfo TargetFileInfo { get; } 366 | public FileInfo TargetFileTempInfo { get; } 367 | public FileStream? TargetFileTempStream { get; } 368 | public string PatchFilePath { get; } 369 | private FileStream? PatchFileStream { get; } 370 | public ChunkStream? PatchChunkStream { get; } 371 | 372 | private PatchTargetProperty(string patchOutputDir, string patchNameSource, string inputDir, string targetFilePath, long patchOffset, long patchLength, bool createStream) 373 | { 374 | PatchFilePath = Path.Combine(patchOutputDir, patchNameSource); 375 | targetFilePath = Path.Combine(inputDir, targetFilePath); 376 | string targetFileTempPath = targetFilePath + ".temp"; 377 | 378 | TargetFileInfo = new FileInfo(targetFilePath); 379 | TargetFileTempInfo = new FileInfo(targetFileTempPath); 380 | TargetFileTempInfo.Directory?.Create(); 381 | 382 | if (TargetFileTempInfo.Exists) 383 | { 384 | TargetFileTempInfo.IsReadOnly = false; 385 | TargetFileTempInfo.Refresh(); 386 | } 387 | 388 | if (!File.Exists(PatchFilePath)) 389 | throw new FileNotFoundException($"Required patch file: {PatchFilePath} is not found!"); 390 | 391 | if (!createStream) 392 | return; 393 | 394 | long patchChunkEnd = patchOffset + patchLength; 395 | TargetFileTempStream = TargetFileTempInfo.Open(FileMode.Create, FileAccess.Write, FileShare.Write); 396 | PatchFileStream = File.Open(PatchFilePath, FileMode.Open, FileAccess.Read, FileShare.Read); 397 | PatchChunkStream = new ChunkStream(PatchFileStream, patchOffset, patchChunkEnd); 398 | } 399 | 400 | public static PatchTargetProperty Create(string patchOutputDir, string patchNameSource, string inputDir, string targetFilePath, long patchOffset, long patchLength, bool createTempStream) 401 | => new(patchOutputDir, patchNameSource, inputDir, targetFilePath, patchOffset, patchLength, createTempStream); 402 | 403 | public void Dispose() 404 | { 405 | PatchChunkStream?.Dispose(); 406 | PatchFileStream?.Dispose(); 407 | TargetFileTempStream?.Dispose(); 408 | 409 | TargetFileTempInfo.Refresh(); 410 | if (TargetFileInfo.Exists) 411 | { 412 | TargetFileInfo.IsReadOnly = false; 413 | TargetFileInfo.Delete(); 414 | } 415 | 416 | TargetFileTempInfo.MoveTo(TargetFileInfo.FullName); 417 | } 418 | } 419 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | HK4E Sophon Downloader 635 | Copyright (C) 2025 GesthosNetwork 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | HK4E Sophon Downloader Copyright (C) 2025 GesthosNetwork 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | 676 | --------------------------------------------------------------------------------