Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract AsarFile to separate class and add progress callback (sync) #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions sync/asardotnet/AsarExtractEvent.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
39 changes: 13 additions & 26 deletions sync/asardotnet/AsarExtractor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -59,11 +60,12 @@ public Boolean Extract(AsarArchive archive, String filepath, String destination)
return false;
}

private List<AFile> _filesToExtract;
private List<AsarFile> _filesToExtract;
private bool _emptyDir = false;
public event EventHandler<AsarExtractEvent> FileExtracted;

public Boolean ExtractAll(AsarArchive archive, String destination, bool emptyDir = false) {
_filesToExtract = new List<AFile>();
_filesToExtract = new List<AsarFile>();

/* ENABLE FOR EMPTY FOLDERS (ONLY IF NEEDED) */
_emptyDir = emptyDir;
Expand All @@ -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) {
Expand All @@ -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;

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

Expand All @@ -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);
}
}
Expand Down
30 changes: 30 additions & 0 deletions sync/asardotnet/AsarFile.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}