- tweak: refactor funny

This commit is contained in:
2025-01-05 17:05:23 +03:00
parent 5b24f915a2
commit 8619e248fd
67 changed files with 485 additions and 492 deletions

View 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;
}

View 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);
}

View 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;
}

View File

@@ -0,0 +1,7 @@
using Robust.LoaderApi;
namespace Nebula.Shared.FileApis.Interfaces;
public interface IReadWriteFileApi : IFileApi, IWriteFileApi
{
}

View 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);
}

View 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);
}
}