Files
RobustToolbox/SS14.Shared/ContentPack/PackLoader.cs
Pieter-Jan Briers d7414930ff RSI support (#552)
* RSI WiP

* More work but we're doing bsdiff now

* RSI loading seems to mostly work.

* Vector2u deserialization test.

* Add in packages again.

* This is the part where I realize I need a path manipulation library.

* The start of a path class but it's late so I'm going to bed.

* HIGHLY theoretical ResourcePath code.

Partially tested but not really.

* Allow x86 for unit tests I guess jesus christ.

Thanks Microsoft for still not shipping x64 VS in 2018.

* Resource paths work & are tested.

* I missed a doc spot.

* ResourcePaths implemented on the server.

* Client works with resource paths.

TIME FOR A REFACTOR.

* Some work but this might be a stupid idea so I migh throw it in the trash.

* Resources refactored completely.

They now only get the requested resourcepath.
They're in charge of opening files to load.

* RSI Loader WORKS.

* Update AudioResource for new loading support.

* Fix package references.

* Fix more references.

* Gonna work now?
2018-04-12 21:53:19 +02:00

73 lines
2.3 KiB
C#

using System.Collections.Generic;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
using SS14.Shared.Log;
using SS14.Shared.Utility;
namespace SS14.Shared.ContentPack
{
public partial class ResourceManager
{
/// <summary>
/// Loads a zipped content pack into the VFS.
/// </summary>
class PackLoader : IContentRoot
{
private readonly FileInfo _pack;
private ZipFile _zip;
/// <summary>
/// Constructor.
/// </summary>
/// <param name="pack">The zip file to mount in the VFS.</param>
public PackLoader(FileInfo pack)
{
_pack = pack;
}
/// <inheritdoc />
public void Mount()
{
Logger.Info($"[RES] Loading ContentPack: {_pack.FullName}...");
var zipFileStream = File.OpenRead(_pack.FullName);
_zip = new ZipFile(zipFileStream);
}
/// <inheritdoc />
public bool TryGetFile(ResourcePath relPath, out MemoryStream fileStream)
{
var entry = _zip.GetEntry(relPath.ToRootedPath().ToString());
if (entry == null)
{
fileStream = null;
return false;
}
// this caches the deflated entry stream in memory
// this way people can read the stream however many times they want to,
// without the performance hit of deflating it every time.
fileStream = new MemoryStream();
using (var zipStream = _zip.GetInputStream(entry))
{
zipStream.CopyTo(fileStream);
fileStream.Position = 0;
}
return true;
}
/// <inheritdoc />
public IEnumerable<ResourcePath> FindFiles(ResourcePath path)
{
foreach (ZipEntry zipEntry in _zip)
{
if (zipEntry.IsFile && zipEntry.Name.StartsWith(path.ToRootedPath().ToString()))
yield return new ResourcePath(zipEntry.Name).ToRelativePath();
}
}
}
}
}