From e59fd81307ab8ad1748c49f31834ecad086471a2 Mon Sep 17 00:00:00 2001 From: Zack Rauen Date: Tue, 9 Oct 2018 13:30:53 -0400 Subject: [PATCH] extract AsarFile add progress callback --- sync/asardotnet/AsarExtractEvent.cs | 22 ++++++++++++++++ sync/asardotnet/AsarExtractor.cs | 39 ++++++++++------------------- sync/asardotnet/AsarFile.cs | 30 ++++++++++++++++++++++ 3 files changed, 65 insertions(+), 26 deletions(-) create mode 100644 sync/asardotnet/AsarExtractEvent.cs create mode 100644 sync/asardotnet/AsarFile.cs diff --git a/sync/asardotnet/AsarExtractEvent.cs b/sync/asardotnet/AsarExtractEvent.cs new file mode 100644 index 0000000..f27f1b2 --- /dev/null +++ b/sync/asardotnet/AsarExtractEvent.cs @@ -0,0 +1,22 @@ +using System; + +namespace asardotnet +{ + + public class AsarExtractEvent : EventArgs + { + public AsarFile File { get; } + public double Index { get; } + public double Total { get; } + public double Progress { get; } + + public AsarExtractEvent(AsarFile file, double index, double total) + { + File = file; + Index = index; + Total = total; + + Progress = Math.Round(index / total * 100, 2); + } + } +} diff --git a/sync/asardotnet/AsarExtractor.cs b/sync/asardotnet/AsarExtractor.cs index 9ec1982..e61873f 100644 --- a/sync/asardotnet/AsarExtractor.cs +++ b/sync/asardotnet/AsarExtractor.cs @@ -37,7 +37,8 @@ using Newtonsoft.Json; using Newtonsoft.Json.Linq; -namespace asardotnet { +namespace asardotnet +{ public class AsarExtractor { public Boolean Extract(AsarArchive archive, String filepath, String destination) { @@ -59,11 +60,12 @@ public Boolean Extract(AsarArchive archive, String filepath, String destination) return false; } - private List _filesToExtract; + private List _filesToExtract; private bool _emptyDir = false; + public event EventHandler FileExtracted; public Boolean ExtractAll(AsarArchive archive, String destination, bool emptyDir = false) { - _filesToExtract = new List(); + _filesToExtract = new List(); /* ENABLE FOR EMPTY FOLDERS (ONLY IF NEEDED) */ _emptyDir = emptyDir; @@ -74,7 +76,9 @@ public Boolean ExtractAll(AsarArchive archive, String destination, bool emptyDir byte[] bytes = archive.GetBytes(); - foreach(AFile aFile in _filesToExtract) { + int filesDone = 0; + + foreach(AsarFile aFile in _filesToExtract) { int size = aFile.GetSize(); int offset = archive.GetBaseOffset() + aFile.GetOffset(); if(size > -1) { @@ -86,31 +90,14 @@ public Boolean ExtractAll(AsarArchive archive, String destination, bool emptyDir if(_emptyDir) Utilities.CreateDirectory(destination + aFile.GetPath()); } + filesDone++; + + FileExtracted?.Invoke(this, new AsarExtractEvent(aFile, filesDone, _filesToExtract.Count)); } return false; } - private struct AFile { - private String _path; - public String GetPath() { return _path; } - private int _size; - public int GetSize() { return _size; } - private int _offset; - public int GetOffset() { return _offset; } - - public AFile(String path, String fileName, int size, int offset) { - path = path.Replace("['", "").Replace("']", ""); - path = path.Substring(0, path.Length - fileName.Length); - path = path.Replace(".files.", "/").Replace("files.", ""); - path += fileName; - - _path = path; - _size = size; - _offset = offset; - } - } - private void TokenIterator(JToken jToken) { JProperty jProperty = jToken as JProperty; @@ -121,7 +108,7 @@ private void TokenIterator(JToken jToken) { if(nextProp.Name == "files") { /* ENABLE FOR EMPTY FOLDERS (ONLY IF NEEDED) */ if(_emptyDir) { - AFile afile = new AFile(prop.Path, "", size, offset); + AsarFile afile = new AsarFile(prop.Path, "", size, offset); _filesToExtract.Add(afile); } @@ -135,7 +122,7 @@ private void TokenIterator(JToken jToken) { } if(size > -1 && offset > -1) { - AFile afile = new AFile(prop.Path, prop.Name, size, offset); + AsarFile afile = new AsarFile(prop.Path, prop.Name, size, offset); _filesToExtract.Add(afile); } } diff --git a/sync/asardotnet/AsarFile.cs b/sync/asardotnet/AsarFile.cs new file mode 100644 index 0000000..5000277 --- /dev/null +++ b/sync/asardotnet/AsarFile.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace asardotnet +{ + public struct AsarFile + { + private String _path; + public String GetPath() { return _path; } + private int _size; + public int GetSize() { return _size; } + private int _offset; + public int GetOffset() { return _offset; } + + public AsarFile(String path, String fileName, int size, int offset) + { + path = path.Replace("['", "").Replace("']", ""); + path = path.Substring(0, path.Length - fileName.Length); + path = path.Replace(".files.", "/").Replace("files.", ""); + path += fileName; + + _path = path; + _size = size; + _offset = offset; + } + } +}