From 2e17a5202bf864283550e55ea658011ef500de2e Mon Sep 17 00:00:00 2001 From: Kemal Setya Adhi Date: Sun, 17 Oct 2021 23:23:52 +0700 Subject: [PATCH] Add my Local Commit --- Hi3HelperGUI/Classes/Data/BlockData.cs | 31 +- .../Classes/Data/Tools/HttpClientTool.cs | 8 +- Hi3HelperGUI/Classes/GUI/BlockSection.cs | 57 +++- Hi3HelperGUI/Classes/GUI/UpdateSection.cs | 4 + .../Preset/Classes/XMFDictionaryClasses.cs | 20 ++ Hi3HelperGUI/MainWindow.xaml | 285 ++++++++++-------- Hi3HelperGUI/MainWindow.xaml.cs | 9 + .../PublishProfiles/FolderProfile.pubxml.user | 2 +- Hi3HelperGUI/config/fileconfig.json | 4 +- 9 files changed, 267 insertions(+), 153 deletions(-) diff --git a/Hi3HelperGUI/Classes/Data/BlockData.cs b/Hi3HelperGUI/Classes/Data/BlockData.cs index 0a0381310..dd8e48efc 100644 --- a/Hi3HelperGUI/Classes/Data/BlockData.cs +++ b/Hi3HelperGUI/Classes/Data/BlockData.cs @@ -28,7 +28,7 @@ public class BlockData : XMFDictionaryClasses #if DEBUG // 4 KiB buffer - readonly byte[] buffer = new byte[4096]; + byte[] buffer = new byte[4096]; #else // 512 KiB buffer readonly byte[] buffer = new byte[524288]; @@ -94,20 +94,18 @@ public void CheckIntegrity(PresetConfigClasses input, CancellationToken token) FileMode.Open, FileAccess.Read)) { + OnProgressChanged(new CheckingBlockProgressChangedStatus() + { + BlockHash = i.BlockHash, + CurrentBlockPos = z, + BlockCount = y + }); + foreach (XMFFileProperty j in i.BlockContent) { chunkSize = (int)j.FileSize; chunkBuffer = new MemoryStream(); - OnProgressChanged(new CheckingBlockProgressChangedStatus() - { - BlockHash = i.BlockHash, - ChunkHash = j.FileHash, - ChunkName = j.FileName, - CurrentBlockPos = z, - BlockCount = y - }); - while (chunkSize > 0) { token.ThrowIfCancellationRequested(); @@ -117,7 +115,13 @@ public void CheckIntegrity(PresetConfigClasses input, CancellationToken token) chunkSize -= byteSize; } + /* + token.ThrowIfCancellationRequested(); + _ = fileStream.Read(buffer = new byte[chunkSize], 0, chunkSize); + */ + totalRead += j.FileSize; + // totalRead += chunkSize; chunkBuffer.Position = 0; @@ -217,7 +221,7 @@ public void BlockRepair(List i, CancellationToken token) currentChunkPos = 0; currentBlockPos++; chunkCount = b.BlockContent.Count; - downloadSize = b.BlockSize < b.BlockExistingSize ? b.BlockSize : (b.BlockSize - b.BlockExistingSize); + downloadSize = b.BlockSize; blockHash = b.BlockHash; remotePath = $"{remoteAddress}{blockHash.ToLowerInvariant()}"; localPath = Path.Combine(a.ActualGameDataLocation, @@ -257,7 +261,7 @@ void RepairCorruptedBlock(in XMFBlockList blockProp, in FileInfo blockInfo, in C currentChunkPos++; chunkBuffer = new MemoryStream(); fileStream.Position = chunkProp.StartOffset; - DownloadContent($"{remotePath}.c/{chunkProp.FileName}", chunkBuffer, chunkProp, -1, -1, token, + DownloadContent($"{remotePath}.wmv", chunkBuffer, chunkProp, chunkProp.StartOffset, chunkProp.StartOffset + chunkProp.FileSize, token, $"Down: {blockProp.BlockHash} Offset {NumberToHexString(chunkProp.StartOffset)} Size {NumberToHexString(chunkProp.FileSize)}"); OnProgressChanged(new RepairingBlockProgressChangedStatus() @@ -377,8 +381,6 @@ private void DownloadEventConverter(object sender, DownloadProgressChanged e) public class CheckingBlockProgressChangedStatus : EventArgs { public string BlockHash { get; set; } - public string ChunkName { get; set; } - public string ChunkHash { get; set; } public int CurrentBlockPos { get; set; } public int BlockCount { get; set; } } @@ -389,7 +391,6 @@ public class CheckingBlockProgressChanged : EventArgs public long ChunkSize { get; set; } public long BytesRead { get; set; } public long TotalBlockSize { get; set; } - public float ProgressPercentage { get => ((float)BytesRead / (float)TotalBlockSize) * 100; } } public class RepairingBlockProgressChangedStatus : EventArgs diff --git a/Hi3HelperGUI/Classes/Data/Tools/HttpClientTool.cs b/Hi3HelperGUI/Classes/Data/Tools/HttpClientTool.cs index 84d9ffe61..45929b385 100644 --- a/Hi3HelperGUI/Classes/Data/Tools/HttpClientTool.cs +++ b/Hi3HelperGUI/Classes/Data/Tools/HttpClientTool.cs @@ -116,6 +116,7 @@ void UseStream(string input, string output, long startOffset, long endOffset, st { token.ThrowIfCancellationRequested(); long existingLength; + long contentLength; FileInfo fileinfo = new(output); if (isStream) @@ -131,7 +132,12 @@ void UseStream(string input, string output, long startOffset, long endOffset, st request.Headers.Range = new RangeHeaderValue(existingLength, null); using HttpResponseMessage response = httpClient.Send(request, HttpCompletionOption.ResponseHeadersRead, token); - long contentLength = existingLength + (response.Content.Headers.ContentRange.Length - response.Content.Headers.ContentRange.From) ?? 0; + + if (startOffset != -1 && endOffset != -1) + contentLength = endOffset - startOffset; + else + contentLength = existingLength + (response.Content.Headers.ContentRange.Length - response.Content.Headers.ContentRange.From) ?? 0; + resumabilityStatus = new DownloadStatusChanged((int)response.StatusCode == 206); if (!isStream) diff --git a/Hi3HelperGUI/Classes/GUI/BlockSection.cs b/Hi3HelperGUI/Classes/GUI/BlockSection.cs index c5afde95c..0c01d9e77 100644 --- a/Hi3HelperGUI/Classes/GUI/BlockSection.cs +++ b/Hi3HelperGUI/Classes/GUI/BlockSection.cs @@ -1,9 +1,11 @@ using System; +using System.Collections.Generic; using System.Windows.Threading; using System.Threading; using System.Threading.Tasks; using System.IO; using System.Windows; +using Newtonsoft.Json; using Hi3HelperGUI.Preset; using Hi3HelperGUI.Data; @@ -30,6 +32,7 @@ private void BlockCheckCancel(object sender, RoutedEventArgs e) public async void DoBlockCheck() { + ToggleBlockPlaceholder(false); BlockCheckTokenSource = new CancellationTokenSource(); CancellationToken token = BlockCheckTokenSource.Token; @@ -41,11 +44,13 @@ public async void DoBlockCheck() { LogWriteLine($"Block Checking Cancelled!", LogType.Warning, true); ChangeBlockRepairStatus($"Block Checking Cancelled!", true); + ToggleBlockPlaceholder(true); } } public async void DoBlockRepair() { + ToggleBlockPlaceholder(false); BlockCheckTokenSource = new CancellationTokenSource(); CancellationToken token = BlockCheckTokenSource.Token; @@ -58,6 +63,9 @@ public async void DoBlockRepair() { LogWriteLine($"Block Repairing Cancelled!", LogType.Warning, true); ChangeBlockRepairStatus($"Block Repairing Cancelled!", true, false); + blockData.DisposeAll(); + RefreshBlockTreeView(); + ToggleBlockPlaceholder(true); } } @@ -69,10 +77,8 @@ public async Task FetchBlockDictionary(CancellationToken token) await Task.Run(() => { blockData.DisposeAll(); - Dispatcher.Invoke(() => - { - BlockChunkTreeView.ItemsSource = blockData.BrokenBlocksRegion; - }); + RefreshBlockTreeView(); + foreach (PresetConfigClasses i in ConfigStore.Config) { blockDictStream = new MemoryStream(); @@ -109,17 +115,50 @@ await Task.Run(() => blockData.CheckingProgressChangedStatus -= BlockProgressChanged; blockDictStream.Dispose(); + + // File.WriteAllText(@"C:\Users\neon-nyan\Documents\git\myApp\Hi3Helper\test.json", JsonConvert.SerializeObject(blockData.BrokenBlocksRegion)); } blockData.DisposeAssets(); - Dispatcher.Invoke(() => + RefreshBlockTreeView(); + }); + } + + List BlockGenerateTreeView() + { + List zoneName = new(); + List blockName; + List chunkProperties; + + foreach (KeyValuePair> i in blockData.BrokenBlocksRegion) + { + blockName = new(); + foreach (XMFDictionaryClasses.XMFBlockList j in i.Value) { - BlockChunkTreeView.ItemsSource = blockData.BrokenBlocksRegion; + chunkProperties = new(); + foreach (XMFDictionaryClasses.XMFFileProperty k in j.BlockContent) + { + chunkProperties.Add(new ChunkProperties { ChunkOffset = $"0x{NumberToHexString(k.StartOffset)}", ChunkSize = $"0x{NumberToHexString(k.FileSize)}" }); + } + blockName.Add(new BlockName { + BlockHash = j.BlockHash, + BlockStatus = $" [{(j.BlockMissing ? "Missing" : j.BlockExistingSize > 0 && j.BlockContent.Count == 0 ? "Incomplete" : $"{j.BlockContent.Count} Chunk(s)")}]", + ChunkItems = chunkProperties + }); + } + zoneName.Add(new GameZoneName { + ZoneName = i.Key, + ZoneStatus = $" [{blockName.Count} Blocks]", + BlockItems = blockName }); - }); + } + + return zoneName; } + void RefreshBlockTreeView() => Dispatcher.Invoke(() => { BlockChunkTreeView.ItemsSource = BlockGenerateTreeView(); }); + public async Task RunBlockRepair(CancellationToken token) { await Task.Run(() => @@ -131,6 +170,8 @@ await Task.Run(() => blockData.RepairingProgressChanged -= BlockProgressChanged; blockData.RepairingProgressChangedStatus -= BlockProgressChanged; + blockData.DisposeAll(); + RefreshBlockTreeView(); }); } private void BlockProgressChanged(object sender, CheckingBlockProgressChangedStatus e) @@ -176,6 +217,8 @@ private void BlockProgressChanged(object sender, RepairingBlockProgressChanged e }, DispatcherPriority.Background); } + private void ToggleBlockPlaceholder(bool a) => Dispatcher.Invoke(() => BlockSectionPlaceHolder.Visibility = a ? Visibility.Visible : Visibility.Collapsed); + private void RefreshBlockCheckProgressBar(double cur = 0, double max = 100) { Dispatcher.Invoke(() => { diff --git a/Hi3HelperGUI/Classes/GUI/UpdateSection.cs b/Hi3HelperGUI/Classes/GUI/UpdateSection.cs index 89f67aaef..3273364fc 100644 --- a/Hi3HelperGUI/Classes/GUI/UpdateSection.cs +++ b/Hi3HelperGUI/Classes/GUI/UpdateSection.cs @@ -34,6 +34,7 @@ private async void FetchUpdateData() await Task.Run(() => { (ConfigStore.UpdateFilesTotalSize, ConfigStore.UpdateFilesTotalDownloaded) = (0, 0); + ToggleUpdatePlaceholder(false); RefreshUpdateProgressBar(); ConfigStore.UpdateFiles = new List(); RefreshUpdateProgressLabel(); @@ -53,6 +54,7 @@ await Task.Run(() => if (!(ConfigStore.UpdateFilesTotalSize > 0)) { ChangeUpdateStatus($"Your files are already up-to-date!", true); + ToggleUpdatePlaceholder(true); return; } ChangeUpdateStatus($"{ConfigStore.UpdateFiles.Count} files ({SummarizeSizeSimple(ConfigStore.UpdateFilesTotalSize)}) are ready to be updated. Click Download to start the update!", true); @@ -124,6 +126,8 @@ await Task.Run(() => { return false; } + private void ToggleUpdatePlaceholder(bool a) => Dispatcher.Invoke(() => UpdateSectionPlaceHolder.Visibility = a ? Visibility.Visible : Visibility.Collapsed); + private void RefreshUpdateProgressLabel(string i = "none") => Dispatcher.Invoke(() => UpdateProgressLabel.Content = i); // private void RefreshUpdateListView() => Dispatcher.Invoke(() => UpdateListView.Items.Refresh()); diff --git a/Hi3HelperGUI/Classes/Preset/Classes/XMFDictionaryClasses.cs b/Hi3HelperGUI/Classes/Preset/Classes/XMFDictionaryClasses.cs index c6b2b4a8b..32e34f3dd 100644 --- a/Hi3HelperGUI/Classes/Preset/Classes/XMFDictionaryClasses.cs +++ b/Hi3HelperGUI/Classes/Preset/Classes/XMFDictionaryClasses.cs @@ -3,6 +3,26 @@ namespace Hi3HelperGUI.Preset { + public class ChunkProperties + { + public string ChunkOffset { get; set; } + public string ChunkSize { get; set; } + } + + public class BlockName + { + public string BlockHash { get; set; } + public string BlockStatus { get; set; } + public List ChunkItems { get; set; } + } + + public class GameZoneName + { + public string ZoneName { get; set; } + public string ZoneStatus { get; set; } + public List BlockItems { get; set; } + } + public class XMFDictionaryClasses { public class VersionFile diff --git a/Hi3HelperGUI/MainWindow.xaml b/Hi3HelperGUI/MainWindow.xaml index b58f774cc..4cce8d0d3 100644 --- a/Hi3HelperGUI/MainWindow.xaml +++ b/Hi3HelperGUI/MainWindow.xaml @@ -4,7 +4,9 @@ xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:Hi3HelperGUI" - xmlns:Json="clr-namespace:Newtonsoft.Json;assembly=Newtonsoft.Json" x:Class="Hi3HelperGUI.MainWindow" + xmlns:Json="clr-namespace:Newtonsoft.Json;assembly=Newtonsoft.Json" + xmlns:Preset="clr-namespace:Hi3HelperGUI.Preset" + x:Class="Hi3HelperGUI.MainWindow" mc:Ignorable="d" Title="Hi3Helper" Height="528" Width="800" MinWidth="800" MinHeight="528" ResizeMode="CanResize"> @@ -40,130 +42,168 @@ - - + + + + + + + + + + - - - - - - - -