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

63 lines
1.9 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
2025-03-13 14:58:47 +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;
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
2025-03-13 14:58:47 +03:00
// Some migrating think
foreach(var file in ContentFileApi.AllFiles){
if(file.Contains("\\") || !ContentFileApi.TryOpen(file, out var stream)) continue;
ContentFileApi.Save(HashApi.GetManifestPath(file), stream);
stream.Dispose();
ContentFileApi.Remove(file);
2024-12-22 16:38:47 +03:00
}
}
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
return new ZipFileApi(zipArchive, "");
2025-01-08 18:00:06 +03:00
}
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
}
}