- tweak: refactor funny
This commit is contained in:
20
Nebula.Shared/FileApis/AssemblyApi.cs
Normal file
20
Nebula.Shared/FileApis/AssemblyApi.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using Robust.LoaderApi;
|
||||
|
||||
namespace Nebula.Shared.FileApis;
|
||||
|
||||
public class AssemblyApi : IFileApi
|
||||
{
|
||||
private readonly IFileApi _root;
|
||||
|
||||
public AssemblyApi(IFileApi root)
|
||||
{
|
||||
_root = root;
|
||||
}
|
||||
|
||||
public bool TryOpen(string path, out Stream? stream)
|
||||
{
|
||||
return _root.TryOpen(path, out stream);
|
||||
}
|
||||
|
||||
public IEnumerable<string> AllFiles => _root.AllFiles;
|
||||
}
|
||||
54
Nebula.Shared/FileApis/FileApi.cs
Normal file
54
Nebula.Shared/FileApis/FileApi.cs
Normal file
@@ -0,0 +1,54 @@
|
||||
using Nebula.Shared.FileApis.Interfaces;
|
||||
|
||||
namespace Nebula.Shared.FileApis;
|
||||
|
||||
public class FileApi : IReadWriteFileApi
|
||||
{
|
||||
public string RootPath;
|
||||
|
||||
public FileApi(string rootPath)
|
||||
{
|
||||
RootPath = rootPath;
|
||||
}
|
||||
|
||||
public bool TryOpen(string path, out Stream? stream)
|
||||
{
|
||||
if (File.Exists(Path.Join(RootPath, path)))
|
||||
{
|
||||
stream = File.OpenRead(Path.Join(RootPath, path));
|
||||
return true;
|
||||
}
|
||||
|
||||
stream = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool Save(string path, Stream input)
|
||||
{
|
||||
var currPath = Path.Join(RootPath, path);
|
||||
|
||||
var dirInfo = new DirectoryInfo(Path.GetDirectoryName(currPath));
|
||||
if (!dirInfo.Exists) dirInfo.Create();
|
||||
|
||||
using var stream = File.OpenWrite(currPath);
|
||||
input.CopyTo(stream);
|
||||
stream.Flush();
|
||||
stream.Close();
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool Remove(string path)
|
||||
{
|
||||
if (!Has(path)) return false;
|
||||
File.Delete(Path.Join(RootPath, path));
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool Has(string path)
|
||||
{
|
||||
var currPath = Path.Join(RootPath, path);
|
||||
return File.Exists(currPath);
|
||||
}
|
||||
|
||||
public IEnumerable<string> AllFiles => Directory.EnumerateFiles(RootPath, "*.*", SearchOption.AllDirectories);
|
||||
}
|
||||
30
Nebula.Shared/FileApis/HashApi.cs
Normal file
30
Nebula.Shared/FileApis/HashApi.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using Nebula.Shared.Models;
|
||||
using Robust.LoaderApi;
|
||||
|
||||
namespace Nebula.Shared.FileApis;
|
||||
|
||||
public class HashApi : IFileApi
|
||||
{
|
||||
private readonly IFileApi _fileApi;
|
||||
public Dictionary<string, RobustManifestItem> Manifest;
|
||||
|
||||
public HashApi(List<RobustManifestItem> manifest, IFileApi fileApi)
|
||||
{
|
||||
_fileApi = fileApi;
|
||||
Manifest = new Dictionary<string, RobustManifestItem>();
|
||||
foreach (var item in manifest) Manifest.TryAdd(item.Path, item);
|
||||
}
|
||||
|
||||
public bool TryOpen(string path, out Stream? stream)
|
||||
{
|
||||
if (path[0] == '/') path = path.Substring(1);
|
||||
|
||||
if (Manifest.TryGetValue(path, out var a) && _fileApi.TryOpen(a.Hash, out stream))
|
||||
return true;
|
||||
|
||||
stream = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
public IEnumerable<string> AllFiles => Manifest.Keys;
|
||||
}
|
||||
7
Nebula.Shared/FileApis/Interfaces/IReadWriteFileApi.cs
Normal file
7
Nebula.Shared/FileApis/Interfaces/IReadWriteFileApi.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
using Robust.LoaderApi;
|
||||
|
||||
namespace Nebula.Shared.FileApis.Interfaces;
|
||||
|
||||
public interface IReadWriteFileApi : IFileApi, IWriteFileApi
|
||||
{
|
||||
}
|
||||
8
Nebula.Shared/FileApis/Interfaces/IWriteFileApi.cs
Normal file
8
Nebula.Shared/FileApis/Interfaces/IWriteFileApi.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
namespace Nebula.Shared.FileApis.Interfaces;
|
||||
|
||||
public interface IWriteFileApi
|
||||
{
|
||||
public bool Save(string path, Stream input);
|
||||
public bool Remove(string path);
|
||||
public bool Has(string path);
|
||||
}
|
||||
61
Nebula.Shared/FileApis/ZipFileApi.cs
Normal file
61
Nebula.Shared/FileApis/ZipFileApi.cs
Normal file
@@ -0,0 +1,61 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.IO.Compression;
|
||||
using System.Runtime.InteropServices;
|
||||
using Robust.LoaderApi;
|
||||
|
||||
namespace Nebula.Shared.FileApis;
|
||||
|
||||
public sealed class ZipFileApi : IFileApi
|
||||
{
|
||||
private readonly ZipArchive _archive;
|
||||
private readonly string? _prefix;
|
||||
|
||||
public ZipFileApi(ZipArchive archive, string? prefix)
|
||||
{
|
||||
_archive = archive;
|
||||
_prefix = prefix;
|
||||
}
|
||||
|
||||
public bool TryOpen(string path, [NotNullWhen(true)] out Stream? stream)
|
||||
{
|
||||
var entry = _archive.GetEntry(_prefix != null ? _prefix + path : path);
|
||||
if (entry == null)
|
||||
{
|
||||
stream = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
stream = new MemoryStream();
|
||||
lock (_archive)
|
||||
{
|
||||
using var zipStream = entry.Open();
|
||||
zipStream.CopyTo(stream);
|
||||
}
|
||||
|
||||
stream.Position = 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
public IEnumerable<string> AllFiles
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_prefix != null)
|
||||
return _archive.Entries
|
||||
.Where(e => e.Name != "" && e.FullName.StartsWith(_prefix))
|
||||
.Select(e => e.FullName[_prefix.Length..]);
|
||||
return _archive.Entries
|
||||
.Where(e => e.Name != "")
|
||||
.Select(entry => entry.FullName);
|
||||
}
|
||||
}
|
||||
|
||||
public static ZipFileApi FromPath(string path)
|
||||
{
|
||||
var zipArchive = new ZipArchive(File.OpenRead(path), ZipArchiveMode.Read);
|
||||
|
||||
var prefix = "";
|
||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) prefix = "Space Station 14.app/Contents/Resources/";
|
||||
return new ZipFileApi(zipArchive, prefix);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user