Skip to content

Commit

Permalink
Add my Local Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
neon-nyan committed Oct 17, 2021
1 parent 1db8428 commit 2e17a52
Show file tree
Hide file tree
Showing 9 changed files with 267 additions and 153 deletions.
31 changes: 16 additions & 15 deletions Hi3HelperGUI/Classes/Data/BlockData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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();
Expand All @@ -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;

Expand Down Expand Up @@ -217,7 +221,7 @@ public void BlockRepair(List<PresetConfigClasses> 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,
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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; }
}
Expand All @@ -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
Expand Down
8 changes: 7 additions & 1 deletion Hi3HelperGUI/Classes/Data/Tools/HttpClientTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
57 changes: 50 additions & 7 deletions Hi3HelperGUI/Classes/GUI/BlockSection.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -30,6 +32,7 @@ private void BlockCheckCancel(object sender, RoutedEventArgs e)

public async void DoBlockCheck()
{
ToggleBlockPlaceholder(false);
BlockCheckTokenSource = new CancellationTokenSource();
CancellationToken token = BlockCheckTokenSource.Token;

Expand All @@ -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;

Expand All @@ -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);
}
}

Expand All @@ -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();
Expand Down Expand Up @@ -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<GameZoneName> BlockGenerateTreeView()
{
List<GameZoneName> zoneName = new();
List<BlockName> blockName;
List<ChunkProperties> chunkProperties;

foreach (KeyValuePair<string, List<XMFDictionaryClasses.XMFBlockList>> 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(() =>
Expand All @@ -131,6 +170,8 @@ await Task.Run(() =>

blockData.RepairingProgressChanged -= BlockProgressChanged;
blockData.RepairingProgressChangedStatus -= BlockProgressChanged;
blockData.DisposeAll();
RefreshBlockTreeView();
});
}
private void BlockProgressChanged(object sender, CheckingBlockProgressChangedStatus e)
Expand Down Expand Up @@ -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(() => {
Expand Down
4 changes: 4 additions & 0 deletions Hi3HelperGUI/Classes/GUI/UpdateSection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ private async void FetchUpdateData()
await Task.Run(() =>
{
(ConfigStore.UpdateFilesTotalSize, ConfigStore.UpdateFilesTotalDownloaded) = (0, 0);
ToggleUpdatePlaceholder(false);
RefreshUpdateProgressBar();
ConfigStore.UpdateFiles = new List<UpdateDataProperties>();
RefreshUpdateProgressLabel();
Expand All @@ -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);
Expand Down Expand Up @@ -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());
Expand Down
20 changes: 20 additions & 0 deletions Hi3HelperGUI/Classes/Preset/Classes/XMFDictionaryClasses.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ChunkProperties> ChunkItems { get; set; }
}

public class GameZoneName
{
public string ZoneName { get; set; }
public string ZoneStatus { get; set; }
public List<BlockName> BlockItems { get; set; }
}

public class XMFDictionaryClasses
{
public class VersionFile
Expand Down
Loading

0 comments on commit 2e17a52

Please sign in to comment.