Skip to content

Commit

Permalink
Move from dotnetzip to sharpcompress; remove unnecessary references
Browse files Browse the repository at this point in the history
  • Loading branch information
ManlyMarco committed Jan 21, 2020
1 parent 991298b commit bdf4088
Show file tree
Hide file tree
Showing 16 changed files with 859 additions and 208 deletions.
23 changes: 11 additions & 12 deletions KKManager.Core/Data/Zipmods/SideloaderModLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Linq;
using Ionic.Zip;
using KKManager.Util;

namespace KKManager.Data.Zipmods
{
Expand Down Expand Up @@ -73,20 +73,19 @@ public static SideloaderModInfo LoadFromFile(string filename)
if (!IsValidZipmodExtension(location.Extension))
throw new ArgumentException($"The file {filename} has an invalid extension and can't be a zipmod", nameof(filename));

using (var zf = ZipFile.Read(location.FullName))
using (var zf = SharpCompress.Archives.ArchiveFactory.Open(location))
{
var manifestEntry = zf.Entries.FirstOrDefault(x =>
x.FileName.Equals("manifest.xml", StringComparison.OrdinalIgnoreCase));
var manifestEntry = zf.Entries.FirstOrDefault(x => PathTools.PathsEqual(x.Key, "manifest.xml"));

if (manifestEntry == null)
{
if(zf.Entries.Any(x => x.IsDirectory && x.FileName.StartsWith("abdata", StringComparison.OrdinalIgnoreCase)))
if (zf.Entries.Any(x => x.IsDirectory && PathTools.PathsEqual(x.Key, "abdata")))
throw new NotSupportedException("The file is a hardmod and cannot be installed automatically. It's recommeded to look for a sideloader version.");

throw new InvalidDataException("manifest.xml was not found in the mod archive. Make sure this is a zipmod.");
}

using (var fileStream = manifestEntry.OpenReader())
using (var fileStream = manifestEntry.OpenEntryStream())
{
var manifest = XDocument.Load(fileStream, LoadOptions.None);

Expand All @@ -103,25 +102,25 @@ public static SideloaderModInfo LoadFromFile(string filename)
var images = new List<Image>();
// TODO load from drive instead of caching to ram
foreach (var imageFile in zf.Entries
.Where(x => ".jpg".Equals(Path.GetExtension(x.FileName), StringComparison.OrdinalIgnoreCase) ||
".png".Equals(Path.GetExtension(x.FileName), StringComparison.OrdinalIgnoreCase))
.OrderBy(x => x.FileName).Take(3))
.Where(x => ".jpg".Equals(Path.GetExtension(x.Key), StringComparison.OrdinalIgnoreCase) ||
".png".Equals(Path.GetExtension(x.Key), StringComparison.OrdinalIgnoreCase))
.OrderBy(x => x.Key).Take(3))
{
try
{
using (var stream = imageFile.OpenReader())
using (var stream = imageFile.OpenEntryStream())
using (var img = Image.FromStream(stream))
{
images.Add(img.GetThumbnailImage(200, 200, null, IntPtr.Zero));
}
}
catch (SystemException ex)
{
Console.WriteLine($"Failed to load image \"{imageFile.FileName}\" from mod archive \"{zf.Name}\" with error: {ex.Message}");
Console.WriteLine($"Failed to load image \"{imageFile.Key}\" from mod archive \"{location.Name}\" with error: {ex.Message}");
}
}

var contents = zf.EntryFileNames.Select(x => x.Replace('/', '\\')).ToList();
var contents = zf.Entries.Where(x => !x.IsDirectory).Select(x => x.Key.Replace('/', '\\')).ToList();

return new SideloaderModInfo(location, guid, name, version, author, description, website, images, contents);
}
Expand Down
22 changes: 10 additions & 12 deletions KKManager.Core/Functions/ModInstaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
using System.Linq;
using System.Reactive.Linq;
using System.Threading;
using Ionic.Zip;
using KKManager.Data.Plugins;
using KKManager.Data.Zipmods;
using KKManager.Util;

namespace KKManager.Functions
{
Expand Down Expand Up @@ -53,18 +51,18 @@ public static void InstallFromUnknownFile(string fileName)
throw new InvalidDataException("The file format is not supported, or the file is broken");
}

private static void InstallFromArchive(string fileName)
/*private static void InstallFromArchive(string fileName)
{
using (var archive = ZipFile.Read(fileName))
using (var archive = SharpCompress.Archives.ArchiveFactory.Open(fileName))
{
var any = false;
var bepin = archive.Entries.FirstOrDefault(x => x.IsDirectory && StringTools.PathsEqual(x.FileName, "bepinex"));
var bepin = archive.Entries.FirstOrDefault(x => x.IsDirectory && PathTools.PathsEqual(x.Key, "bepinex"));
if (bepin != null)
{
//bug refuses to work, throws access denied when trying to extract over existing bepinex
// refuses to work, throws access denied when trying to extract over existing bepinex
bepin.Extract(InstallDirectoryHelper.KoikatuDirectory.FullName, ExtractExistingFileAction.OverwriteSilently);
any = true;
/* Alternative way? Go file per file and create dirs as needed and delete old files as needed
// Alternative way? Go file per file and create dirs as needed and delete old files as needed
using (ZipFile zip1 = ZipFile.Read(fileName))
{
var selection = (from e in zip1.Entries
Expand All @@ -78,10 +76,10 @@ private static void InstallFromArchive(string fileName)
{
e.Extract(outputDirectory);
}
}*/
}
}
var mods = archive.Entries.FirstOrDefault(x => x.IsDirectory && StringTools.PathsEqual(x.FileName, "mods"));
var mods = archive.Entries.FirstOrDefault(x => x.IsDirectory && PathTools.PathsEqual(x.Key, "mods"));
if (mods != null)
{
mods.Extract(InstallDirectoryHelper.KoikatuDirectory.FullName, ExtractExistingFileAction.OverwriteSilently);
Expand All @@ -91,7 +89,7 @@ private static void InstallFromArchive(string fileName)
if (!any)
throw new InvalidDataException("The archive is in an unknown format. If it contains a mod extract it and try again.");
/*if (!any)
if (!any)
{
var entries = archive.Where(x => !x.IsDirectory)
.Select(x => new { entry = x, ext = Path.GetExtension(x.FileName) })
Expand Down Expand Up @@ -124,9 +122,9 @@ private static void InstallFromArchive(string fileName)
container.Extract();
}
}
}*/
}
}
}
}*/

private static void InstallPlugin(string fileName)
{
Expand Down
14 changes: 3 additions & 11 deletions KKManager.Core/KKManager.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@
<DebugSymbols>true</DebugSymbols>
</PropertyGroup>
<ItemGroup>
<Reference Include="DotNetZip, Version=1.13.5.0, Culture=neutral, PublicKeyToken=6583c7c814667745, processorArchitecture=MSIL">
<HintPath>..\packages\DotNetZip.1.13.5\lib\net40\DotNetZip.dll</HintPath>
</Reference>
<Reference Include="MessagePack, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b4a0369545f0a1be, processorArchitecture=MSIL">
<HintPath>..\packages\MessagePack.2.0.335\lib\netstandard2.0\MessagePack.dll</HintPath>
</Reference>
Expand All @@ -49,31 +46,27 @@
<HintPath>..\packages\ObjectListView.Official.2.9.1\lib\net20\ObjectListView.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="SharpCompress, Version=0.24.0.0, Culture=neutral, PublicKeyToken=afb0a02973931d96, processorArchitecture=MSIL">
<HintPath>..\packages\SharpCompress.0.24.0\lib\net45\SharpCompress.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Drawing" />
<Reference Include="System.Management" />
<Reference Include="System.Memory, Version=4.0.1.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Memory.4.5.3\lib\netstandard2.0\System.Memory.dll</HintPath>
</Reference>
<Reference Include="System.Numerics">
<Private>False</Private>
</Reference>
<Reference Include="System.Reactive, Version=4.3.0.0, Culture=neutral, PublicKeyToken=94bc3704cddfc263, processorArchitecture=MSIL">
<HintPath>..\packages\System.Reactive.4.3.2\lib\net46\System.Reactive.dll</HintPath>
</Reference>
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.Runtime.Serialization.Primitives, Version=4.1.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Runtime.Serialization.Primitives.4.3.0\lib\net46\System.Runtime.Serialization.Primitives.dll</HintPath>
<Private>False</Private>
<Private>True</Private>
</Reference>
<Reference Include="System.Web" />
<Reference Include="System.Windows" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
<Reference Include="WindowsBase" />
</ItemGroup>
Expand Down Expand Up @@ -116,7 +109,6 @@
<Compile Include="Util\ResourceUtils.cs" />
<Compile Include="Util\RetryHelper.cs" />
<Compile Include="Util\SimpleExpandTypeConverter.cs" />
<Compile Include="Util\StringTools.cs" />
<Compile Include="Util\UniversalDragAndDrop.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
15 changes: 15 additions & 0 deletions KKManager.Core/Util/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System;
using System.ComponentModel;
using System.IO;
using System.Windows.Forms;
using SharpCompress.Archives;
using SharpCompress.Readers;

namespace KKManager.Util
{
Expand Down Expand Up @@ -31,5 +34,17 @@ public static void SafeInvoke(this Control obj, Action action)
}
}
}

public static void ExtractArchiveToDirectory(this IArchive archive, string targetDirectory)
{
Directory.CreateDirectory(targetDirectory);
var extractor = archive.ExtractAllEntries();
while (extractor.MoveToNextEntry())
{
var path = Path.Combine(targetDirectory, extractor.Entry.Key);
if (extractor.Entry.IsDirectory) Directory.CreateDirectory(path);
else extractor.WriteEntryTo(path);
}
}
}
}
91 changes: 0 additions & 91 deletions KKManager.Core/Util/StringTools.cs

This file was deleted.

2 changes: 1 addition & 1 deletion KKManager.Core/packages.config
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="DotNetZip" version="1.13.5" targetFramework="net462" />
<package id="MessagePack" version="2.0.335" targetFramework="net462" />
<package id="MessagePack.Annotations" version="2.0.335" targetFramework="net462" />
<package id="Mono.Cecil" version="0.11.1" targetFramework="net462" />
<package id="Newtonsoft.Json" version="12.0.3" targetFramework="net462" />
<package id="ObjectListView.Official" version="2.9.1" targetFramework="net462" />
<package id="SharpCompress" version="0.24.0" targetFramework="net462" />
<package id="System.Buffers" version="4.5.0" targetFramework="net462" />
<package id="System.Memory" version="4.5.3" targetFramework="net462" />
<package id="System.Numerics.Vectors" version="4.5.0" targetFramework="net462" />
Expand Down
6 changes: 0 additions & 6 deletions KKManager.SB3UGS/KKManager.SB3UGS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,6 @@
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
<Reference Include="UnityBase">
<HintPath>..\libs\SB3UGS\plugins\UnityBase.dll</HintPath>
<Private>False</Private>
Expand Down
16 changes: 2 additions & 14 deletions KKManager.SB3UGS/SB3UGS_Initializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
using System.IO;
using System.Linq;
using System.Reflection;
using KKManager.Util;
using SB3Utility;
using SharpCompress.Archives;
using SharpCompress.Readers;

namespace KKManager.SB3UGS
{
Expand Down Expand Up @@ -40,7 +40,7 @@ private static void Initialize()
throw new IOException("Archive " + target.Name + " is not valid or is corrupted");

directoryName.Create();
ExtractArchiveToDirectory(extr, directoryName.FullName);
extr.ExtractArchiveToDirectory(directoryName.FullName);

_pluginDirName.Refresh();
if (!_pluginDirName.Exists)
Expand All @@ -55,18 +55,6 @@ private static void Initialize()
LoadPlugin(Path.Combine(_pluginDirName.FullName, "SB3UtilityPlugins.dll"));
}

private static void ExtractArchiveToDirectory(IArchive extr, string directoryPath)
{
Directory.CreateDirectory(directoryPath);
var extractor = extr.ExtractAllEntries();
while (extractor.MoveToNextEntry())
{
var path = Path.Combine(directoryPath, extractor.Entry.Key);
if (extractor.Entry.IsDirectory) Directory.CreateDirectory(path);
else extractor.WriteEntryTo(path);
}
}

public static bool CheckIsAvailable()
{
if (_pluginDirName == null)
Expand Down
2 changes: 1 addition & 1 deletion KKManager.Updater/Data/FileContentsCalculator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
using System.IO;
using System.Linq;
using System.Windows.Forms;
using Ionic.Crc;
using KKManager.SB3UGS;
using KKManager.Updater.Utils;
using MessagePack;

namespace KKManager.Updater.Data
Expand Down
Loading

0 comments on commit bdf4088

Please sign in to comment.