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

165 lines
4.2 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-03-15 22:31:31 +03:00
if(!Directory.Exists(RootPath))
Directory.CreateDirectory(RootPath);
2025-01-12 15:15:01 +03:00
ContentFileApi = CreateFileApi("content");
EngineFileApi = CreateFileApi("engine");
ManifestFileApi = CreateFileApi("manifest");
ConfigurationApi = CreateFileApi("config");
2025-03-14 18:32:03 +03:00
}
2024-12-22 16:38:47 +03:00
2025-03-14 18:32:03 +03:00
public bool CheckMigration(ILoadingHandler loadingHandler)
{
_debugService.Log("Checking migration...");
var migrationList = ContentFileApi.AllFiles.Where(f => !f.Contains("\\")).ToList();
if(migrationList.Count == 0) return false;
_debugService.Log($"Found {migrationList.Count} migration files. Starting migration...");
Task.Run(() => DoMigration(loadingHandler, migrationList));
return true;
}
private void DoMigration(ILoadingHandler loadingHandler, List<string> migrationList)
{
loadingHandler.SetJobsCount(migrationList.Count);
Parallel.ForEach(migrationList, (f,_)=>MigrateFile(f,loadingHandler));
if (loadingHandler is IDisposable disposable)
{
disposable.Dispose();
2024-12-22 16:38:47 +03:00
}
}
2025-03-14 18:32:03 +03:00
private void MigrateFile(string file, ILoadingHandler loadingHandler)
{
if(!ContentFileApi.TryOpen(file, out var stream))
{
loadingHandler.AppendResolvedJob();
return;
}
ContentFileApi.Save(HashApi.GetManifestPath(file), stream);
stream.Dispose();
ContentFileApi.Remove(file);
loadingHandler.AppendResolvedJob();
}
2024-12-22 16:38:47 +03:00
public IReadWriteFileApi CreateFileApi(string path)
{
return new FileApi(Path.Join(RootPath, path));
}
2025-04-20 15:43:57 +03:00
public IReadWriteFileApi EnsureTempDir(out string path)
{
path = Path.Combine(Path.GetTempPath(), "tempThink"+Path.GetRandomFileName());
Directory.CreateDirectory(path);
return new FileApi(path);
}
2024-12-22 16:38:47 +03:00
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
}
2025-03-14 18:32:03 +03:00
}
public sealed class ConsoleLoadingHandler : ILoadingHandler
{
private int _currJobs;
private float _percent;
private int _resolvedJobs;
public void SetJobsCount(int count)
{
_currJobs = count;
UpdatePercent();
Draw();
}
public int GetJobsCount()
{
return _currJobs;
}
public void SetResolvedJobsCount(int count)
{
_resolvedJobs = count;
UpdatePercent();
Draw();
}
public int GetResolvedJobsCount()
{
return _resolvedJobs;
}
private void UpdatePercent()
{
if (_currJobs == 0)
{
_percent = 0;
return;
}
if (_resolvedJobs > _currJobs) return;
_percent = _resolvedJobs / (float)_currJobs;
}
private void Draw()
{
var barCount = 10;
var fullCount = (int)(barCount * _percent);
var emptyCount = barCount - fullCount;
Console.Write("\r");
for (var i = 0; i < fullCount; i++) Console.Write("#");
for (var i = 0; i < emptyCount; i++) Console.Write(" ");
Console.Write($"\t {_resolvedJobs}/{_currJobs}");
}
2024-12-22 16:38:47 +03:00
}