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

153 lines
3.7 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;
2025-07-02 21:32:51 +03:00
using Nebula.Shared.Services.Logging;
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
2025-07-02 21:32:51 +03:00
private readonly ILogger _logger;
2024-12-22 16:38:47 +03:00
public FileService(DebugService debugService)
{
2025-07-02 21:32:51 +03:00
_logger = debugService.GetLogger(this);
2025-03-15 22:31:31 +03:00
if(!Directory.Exists(RootPath))
Directory.CreateDirectory(RootPath);
2025-03-14 18:32:03 +03:00
}
2024-12-22 16:38:47 +03:00
public IReadWriteFileApi CreateFileApi(string path)
{
2025-07-02 21:32:51 +03:00
_logger.Debug($"Creating file api for {path}");
2024-12-22 16:38:47 +03:00
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);
2025-07-02 21:32:51 +03:00
_logger.Debug($"Ensuring temp directory for {path}");
2025-04-20 15:43:57 +03:00
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-07-02 21:32:51 +03:00
public void RemoveAllFiles(string fileApiName,ILoadingHandler loadingHandler, CancellationToken cancellationToken)
{
_logger.Debug($"Deleting files from {fileApiName}");
var path = Path.Combine(RootPath, fileApiName);
var di = new DirectoryInfo(path);
var files = di.GetFiles();
var dirs = di.GetDirectories();
loadingHandler.AppendJob(files.Length);
loadingHandler.AppendJob(dirs.Length);
if(cancellationToken.IsCancellationRequested)
return;
foreach (var file in files)
{
if(cancellationToken.IsCancellationRequested)
return;
file.Delete();
loadingHandler.AppendResolvedJob();
}
foreach (var dir in dirs)
{
if(cancellationToken.IsCancellationRequested)
return;
dir.Delete(true);
loadingHandler.AppendResolvedJob();
}
}
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
}