Files
RobustToolbox/SS14.Shared/ContentPack/DirLoader.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.2 KiB
C#

using SS14.Shared.Log;
using SS14.Shared.Utility;
using System.Collections.Generic;
using System.IO;
namespace SS14.Shared.ContentPack
{
public partial class ResourceManager
{
/// <summary>
/// Holds info about a directory that is mounted in the VFS.
/// </summary>
class DirLoader : IContentRoot
{
private readonly DirectoryInfo _directory;
/// <summary>
/// Constructor.
/// </summary>
/// <param name="directory">Directory to mount.</param>
public DirLoader(DirectoryInfo directory)
{
_directory = directory;
}
/// <inheritdoc />
public void Mount()
{
// Looks good to me
// Nothing to check here since the ResourceManager handles checking permissions.
}
/// <inheritdoc />
public bool TryGetFile(ResourcePath relPath, out MemoryStream fileStream)
{
var path = GetPath(relPath);
if (!File.Exists(path))
{
fileStream = null;
return false;
}
var bytes = File.ReadAllBytes(path);
fileStream = new MemoryStream(bytes, false);
return true;
}
internal string GetPath(ResourcePath relPath)
{
return Path.GetFullPath(Path.Combine(_directory.FullName, relPath.ToRelativeSystemPath()));
}
/// <inheritdoc />
public IEnumerable<ResourcePath> FindFiles(ResourcePath path)
{
var fullPath = GetPath(path);
if (!Directory.Exists(fullPath))
{
yield break;
}
var paths = PathHelpers.GetFiles(fullPath);
// GetFiles returns full paths, we want them relative to root
foreach (var filePath in paths)
{
var relpath = filePath.Substring(_directory.FullName.Length);
yield return ResourcePath.FromRelativeSystemPath(relpath);
}
}
}
}
}