Skip to content
This repository has been archived by the owner on Aug 7, 2022. It is now read-only.

Commit

Permalink
Made extraction async
Browse files Browse the repository at this point in the history
  • Loading branch information
erri120 committed Nov 30, 2019
1 parent f07066f commit 436b092
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 41 deletions.
2 changes: 1 addition & 1 deletion OMODFramework.Test/EndToEndTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public void EndToEnd()

// Test extraction

var data = omod.GetDataFiles();
var data = omod.GetDataFiles().Result;

var fList1 = new List<FileInfo>();
var fList2 = new List<FileInfo>();
Expand Down
11 changes: 6 additions & 5 deletions OMODFramework.Test/OMODTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ You should have received a copy of the GNU General Public License

using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using File = Alphaleonis.Win32.Filesystem.File;
using Path = Alphaleonis.Win32.Filesystem.Path;
Expand Down Expand Up @@ -49,18 +50,18 @@ public void TestOMOD()
}

[TestMethod]
public void TestExtraction()
public async Task TestExtraction()
{
Files.Do(f =>
Files.Do(async f =>
{
var omod = new OMOD(f.FileName);

Assert.IsNotNull(omod);

var data = omod.GetDataFiles();
var data = await omod.GetDataFiles();
Assert.IsNotNull(data);

var plugins = omod.GetPlugins();
var plugins = await omod.GetPlugins();
Assert.IsTrue(omod.AllPlugins.Count == 0 && plugins == null ||
omod.AllPlugins.Count >= 1 && plugins != null);
});
Expand Down Expand Up @@ -130,7 +131,7 @@ public void TestCreation()
Assert.IsTrue(omod.AllPlugins.Count == ops.ESPs.Count);
Assert.IsTrue(omod.AllDataFiles.Count == ops.DataFiles.Count);

var data = omod.GetDataFiles();
var data = omod.GetDataFiles().Result;

Directory.EnumerateFiles(data, "*", SearchOption.AllDirectories).Do(file =>
{
Expand Down
74 changes: 39 additions & 35 deletions OMODFramework/Classes/OMOD.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ You should have received a copy of the GNU General Public License
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Threading.Tasks;
using ICSharpCode.SharpZipLib.Zip;
using OMODFramework.Scripting;
using Path = Alphaleonis.Win32.Filesystem.Path;
Expand Down Expand Up @@ -92,7 +93,7 @@ public Image Image
if (_pD.Image != null)
return _pD.Image;

using (var stream = ExtractWholeFile("image"))
using (var stream = ExtractWholeFile("image").Result)
{
if (stream == null) return null;

Expand All @@ -113,7 +114,7 @@ public OMOD(string path)
FileName = Path.GetFileName(path);
LowerFileName = FileName.ToLower();

using (var configStream = ExtractWholeFile("config"))
using (var configStream = ExtractWholeFile("config").Result)
{
if(configStream == null)
throw new OMODFrameworkException($"Could not find the configuration data for {FileName} !");
Expand Down Expand Up @@ -300,12 +301,12 @@ public static void CreateOMOD(OMODCreationOptions ops, string omodFileName)

public ScriptReturnData RunScript(IScriptFunctions scriptFunctions)
{
return ScriptRunner.ExecuteScript(GetScript(), GetDataFiles(), GetPlugins(), scriptFunctions);
return ScriptRunner.ExecuteScript(GetScript(), GetDataFiles().Result, GetPlugins().Result, scriptFunctions);
}

public ScriptReturnData RunScript(IScriptFunctions scriptFunctions, string data)
{
return ScriptRunner.ExecuteScript(GetScript(), data, GetPlugins(), scriptFunctions);
return ScriptRunner.ExecuteScript(GetScript(), data, GetPlugins().Result, scriptFunctions);
}

public ScriptReturnData RunScript(IScriptFunctions scriptFunctions, string data, string plugin)
Expand All @@ -315,7 +316,7 @@ public ScriptReturnData RunScript(IScriptFunctions scriptFunctions, string data,

private HashSet<string> GetPluginSet()
{
var tempStream = ExtractWholeFile("plugins.crc");
var tempStream = ExtractWholeFile("plugins.crc").Result;
if(tempStream == null) return new HashSet<string>(0);

using (var br = new BinaryReader(tempStream))
Expand All @@ -334,7 +335,7 @@ private HashSet<string> GetPluginSet()

private HashSet<DataFileInfo> GetDataSet()
{
var tempStream = ExtractWholeFile("data.crc");
var tempStream = ExtractWholeFile("data.crc").Result;
if(tempStream == null) return new HashSet<DataFileInfo>(0);

using (var br = new BinaryReader(tempStream))
Expand All @@ -351,19 +352,19 @@ private HashSet<DataFileInfo> GetDataSet()
}
}

public string GetPlugins()
public async Task<string> GetPlugins()
{
return ParseCompressedStream("plugins.crc", "plugins");
return await ParseCompressedStream("plugins.crc", "plugins");
}

public string GetDataFiles()
public async Task<string> GetDataFiles()
{
return ParseCompressedStream("data.crc", "data");
return await ParseCompressedStream("data.crc", "data");
}

public string GetReadme()
{
var s = ExtractWholeFile("readme");
var s = ExtractWholeFile("readme").Result;
if (s == null) return null;

using (var br = new BinaryReader(s))
Expand All @@ -375,7 +376,7 @@ public string GetReadme()

public string GetScript()
{
var s = ExtractWholeFile("script");
var s = ExtractWholeFile("script").Result;
if (s == null) return null;

using (var br = new BinaryReader(s))
Expand All @@ -388,58 +389,61 @@ public string GetScript()
public string GetImage()
{
var bitmapPath = "";
var s = ExtractWholeFile("image", ref bitmapPath);
var s = ExtractWholeFile("image", bitmapPath).Result;
if (s == null)
bitmapPath = null;
else
s.Close();
return bitmapPath;
}

private string ParseCompressedStream(string dataInfo, string dataCompressed)
private async Task<string> ParseCompressedStream(string dataInfo, string dataCompressed)
{
var infoStream = ExtractWholeFile(dataInfo);
var infoStream = await ExtractWholeFile(dataInfo);
if (infoStream == null) return null;

var compressedStream = ExtractWholeFile(dataCompressed);
var path = CompressionHandler.DecompressFiles(infoStream, compressedStream, Compression);
var compressedStream = await ExtractWholeFile(dataCompressed);

var path = await Task.Run(() => CompressionHandler.DecompressFiles(infoStream, compressedStream, Compression));

infoStream.Close();
compressedStream.Close();

return path;
}

private Stream ExtractWholeFile(string s)
private async Task<Stream> ExtractWholeFile(string s)
{
string s2 = null;
return ExtractWholeFile(s, ref s2);
return await ExtractWholeFile(s, null);
}

private Stream ExtractWholeFile(string s, ref string path)
private async Task<Stream> ExtractWholeFile(string s, string path)
{
var ze = ModFile.GetEntry(s);
return ze == null ? null : ExtractWholeFile(ze, ref path);
return ze == null ? null : await ExtractWholeFile(ze, path);
}

private Stream ExtractWholeFile(ZipEntry ze, ref string path)
private async Task<Stream> ExtractWholeFile(ZipEntry ze, string path)
{
var file = ModFile.GetInputStream(ze);
Stream tempStream;
return await Task.Run(() =>
{
var file = ModFile.GetInputStream(ze);
Stream tempStream;

if (path != null || ze.Size > Framework.MaxMemoryStreamSize)
tempStream = Utils.CreateTempFile(out path);
else
tempStream = new MemoryStream((int)ze.Size);
if (path != null || ze.Size > Framework.MaxMemoryStreamSize)
tempStream = Utils.CreateTempFile(out path);
else
tempStream = new MemoryStream((int)ze.Size);

byte[] buffer = new byte[4096];
int i;
byte[] buffer = new byte[4096];
int i;

while((i = file.Read(buffer, 0, 4096)) > 0)
tempStream.Write(buffer, 0, i);
while ((i = file.Read(buffer, 0, 4096)) > 0)
tempStream.Write(buffer, 0, i);

tempStream.Position = 0;
return tempStream;
tempStream.Position = 0;
return tempStream;
});
}
}
}

0 comments on commit 436b092

Please sign in to comment.