Files
NebulaLauncher/Nebula.Shared/Services/FileService.cs

74 lines
2.0 KiB
C#
Raw Normal View History

2024-12-22 16:38:47 +03:00
using System.IO.Compression;
using System.Runtime.InteropServices;
2025-01-05 17:05:23 +03:00
using Nebula.Shared.FileApis;
using Nebula.Shared.FileApis.Interfaces;
using Nebula.Shared.Models;
2024-12-22 16:38:47 +03:00
using Robust.LoaderApi;
2025-01-05 17:05:23 +03:00
namespace Nebula.Shared.Services;
2024-12-22 16:38:47 +03:00
2024-12-27 08:22:17 +03:00
[ServiceRegister]
2024-12-22 16:38:47 +03:00
public class FileService
{
2025-01-12 15:15:01 +03:00
public static readonly string RootPath = Path.Join(Environment.GetFolderPath(
Environment.SpecialFolder.ApplicationData), "Datum");
2024-12-22 16:38:47 +03:00
private readonly DebugService _debugService;
2025-01-14 22:10:16 +03:00
public readonly IReadWriteFileApi ConfigurationApi;
2024-12-22 16:38:47 +03:00
public readonly IReadWriteFileApi ContentFileApi;
public readonly IReadWriteFileApi EngineFileApi;
public readonly IReadWriteFileApi ManifestFileApi;
2025-01-14 22:10:16 +03:00
2024-12-22 16:38:47 +03:00
private HashApi? _hashApi;
public FileService(DebugService debugService)
{
_debugService = debugService;
2025-01-12 15:15:01 +03:00
ContentFileApi = CreateFileApi("content");
EngineFileApi = CreateFileApi("engine");
ManifestFileApi = CreateFileApi("manifest");
ConfigurationApi = CreateFileApi("config");
2024-12-22 16:38:47 +03:00
}
public List<RobustManifestItem> ManifestItems
{
set => _hashApi = new HashApi(value, ContentFileApi);
}
public HashApi HashApi
{
get
{
if (_hashApi is null) throw new Exception("Hash API is not initialized!");
return _hashApi;
}
set => _hashApi = value;
}
public IReadWriteFileApi CreateFileApi(string path)
{
return new FileApi(Path.Join(RootPath, path));
}
2025-01-08 18:00:06 +03:00
public ZipFileApi? OpenZip(string path, IFileApi fileApi)
2024-12-22 16:38:47 +03:00
{
2025-01-08 18:00:06 +03:00
Stream? zipStream = null;
try
{
if (!fileApi.TryOpen(path, out zipStream))
return null;
2024-12-22 16:38:47 +03:00
2025-01-08 18:00:06 +03:00
var zipArchive = new ZipArchive(zipStream, ZipArchiveMode.Read);
2024-12-22 16:38:47 +03:00
2025-01-08 18:00:06 +03:00
var prefix = "";
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) prefix = "Space Station 14.app/Contents/Resources/";
return new ZipFileApi(zipArchive, prefix);
}
2025-01-19 22:52:29 +03:00
catch (Exception)
2025-01-08 18:00:06 +03:00
{
zipStream?.Dispose();
throw;
}
2024-12-22 16:38:47 +03:00
}
}