- tweak: file managment
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace Nebula.Shared.FileApis;
|
||||
|
||||
public class FileApi : IReadWriteFileApi
|
||||
public sealed class FileApi : IReadWriteFileApi
|
||||
{
|
||||
public string RootPath;
|
||||
|
||||
@@ -13,10 +13,19 @@ public class FileApi : IReadWriteFileApi
|
||||
|
||||
public bool TryOpen(string path, out Stream? stream)
|
||||
{
|
||||
if (File.Exists(Path.Join(RootPath, path)))
|
||||
var fullPath = Path.Join(RootPath, path);
|
||||
if (File.Exists(fullPath))
|
||||
{
|
||||
stream = File.OpenRead(Path.Join(RootPath, path));
|
||||
return true;
|
||||
try
|
||||
{
|
||||
stream = new FileStream(fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
stream = null;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
stream = null;
|
||||
@@ -27,27 +36,43 @@ public class FileApi : IReadWriteFileApi
|
||||
{
|
||||
var currPath = Path.Join(RootPath, path);
|
||||
|
||||
var dirInfo = new DirectoryInfo(Path.GetDirectoryName(currPath));
|
||||
if (!dirInfo.Exists) dirInfo.Create();
|
||||
try
|
||||
{
|
||||
var dirInfo = new DirectoryInfo(Path.GetDirectoryName(currPath) ?? throw new InvalidOperationException());
|
||||
if (!dirInfo.Exists) dirInfo.Create();
|
||||
|
||||
using var stream = File.OpenWrite(currPath);
|
||||
input.CopyTo(stream);
|
||||
stream.Flush();
|
||||
stream.Close();
|
||||
return true;
|
||||
using var stream = new FileStream(currPath, FileMode.Create, FileAccess.Write, FileShare.None);
|
||||
input.CopyTo(stream);
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public bool Remove(string path)
|
||||
{
|
||||
if (!Has(path)) return false;
|
||||
File.Delete(Path.Join(RootPath, path));
|
||||
return true;
|
||||
var fullPath = Path.Join(RootPath, path);
|
||||
try
|
||||
{
|
||||
if (File.Exists(fullPath))
|
||||
{
|
||||
File.Delete(fullPath);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Log exception if necessary
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool Has(string path)
|
||||
{
|
||||
var currPath = Path.Join(RootPath, path);
|
||||
return File.Exists(currPath);
|
||||
var fullPath = Path.Join(RootPath, path);
|
||||
return File.Exists(fullPath);
|
||||
}
|
||||
|
||||
public IEnumerable<string> AllFiles => Directory.EnumerateFiles(RootPath, "*.*", SearchOption.AllDirectories);
|
||||
|
||||
47
Nebula.Shared/Models/ILoadingHandler.cs
Normal file
47
Nebula.Shared/Models/ILoadingHandler.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
namespace Nebula.Shared.Models;
|
||||
|
||||
public interface ILoadingHandler
|
||||
{
|
||||
public void SetJobsCount(int count);
|
||||
public int GetJobsCount();
|
||||
|
||||
public void SetResolvedJobsCount(int count);
|
||||
public int GetResolvedJobsCount();
|
||||
|
||||
public void AppendJob(int count = 1)
|
||||
{
|
||||
SetJobsCount(GetJobsCount() + count);
|
||||
}
|
||||
|
||||
public void AppendResolvedJob(int count = 1)
|
||||
{
|
||||
SetResolvedJobsCount(GetResolvedJobsCount() + count);
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
SetResolvedJobsCount(0);
|
||||
SetJobsCount(0);
|
||||
}
|
||||
|
||||
public QueryJob GetQueryJob()
|
||||
{
|
||||
return new QueryJob(this);
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class QueryJob: IDisposable
|
||||
{
|
||||
private readonly ILoadingHandler _handler;
|
||||
|
||||
public QueryJob(ILoadingHandler handler)
|
||||
{
|
||||
_handler = handler;
|
||||
handler.AppendJob();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_handler.AppendResolvedJob();
|
||||
}
|
||||
}
|
||||
@@ -15,7 +15,7 @@ public partial class ContentService
|
||||
return fileService.ContentFileApi.Has(item.Hash);
|
||||
}
|
||||
|
||||
public async Task<List<RobustManifestItem>> EnsureItems(ManifestReader manifestReader, Uri downloadUri,
|
||||
public async Task<List<RobustManifestItem>> EnsureItems(ManifestReader manifestReader, Uri downloadUri, ILoadingHandler loadingHandler,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
List<RobustManifestItem> allItems = [];
|
||||
@@ -36,14 +36,14 @@ public partial class ContentService
|
||||
|
||||
debugService.Log("Download Count:" + items.Count);
|
||||
|
||||
await Download(downloadUri, items, cancellationToken);
|
||||
await Download(downloadUri, items, loadingHandler, cancellationToken);
|
||||
|
||||
fileService.ManifestItems = allItems;
|
||||
|
||||
return allItems;
|
||||
}
|
||||
|
||||
public async Task<List<RobustManifestItem>> EnsureItems(RobustManifestInfo info,
|
||||
public async Task<List<RobustManifestItem>> EnsureItems(RobustManifestInfo info, ILoadingHandler loadingHandler,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
debugService.Log("Getting manifest: " + info.Hash);
|
||||
@@ -51,7 +51,7 @@ public partial class ContentService
|
||||
if (fileService.ManifestFileApi.TryOpen(info.Hash, out var stream))
|
||||
{
|
||||
debugService.Log("Loading manifest from: " + info.Hash);
|
||||
return await EnsureItems(new ManifestReader(stream), info.DownloadUri, cancellationToken);
|
||||
return await EnsureItems(new ManifestReader(stream), info.DownloadUri, loadingHandler, cancellationToken);
|
||||
}
|
||||
|
||||
debugService.Log("Fetching manifest from: " + info.ManifestUri);
|
||||
@@ -63,14 +63,16 @@ public partial class ContentService
|
||||
fileService.ManifestFileApi.Save(info.Hash, streamContent);
|
||||
streamContent.Seek(0, SeekOrigin.Begin);
|
||||
using var manifestReader = new ManifestReader(streamContent);
|
||||
return await EnsureItems(manifestReader, info.DownloadUri, cancellationToken);
|
||||
return await EnsureItems(manifestReader, info.DownloadUri, loadingHandler, cancellationToken);
|
||||
}
|
||||
|
||||
public async Task Unpack(RobustManifestInfo info, IWriteFileApi otherApi, CancellationToken cancellationToken)
|
||||
public async Task Unpack(RobustManifestInfo info, IWriteFileApi otherApi, ILoadingHandler loadingHandler, CancellationToken cancellationToken)
|
||||
{
|
||||
debugService.Log("Unpack manifest files");
|
||||
var items = await EnsureItems(info, cancellationToken);
|
||||
var items = await EnsureItems(info, loadingHandler, cancellationToken);
|
||||
loadingHandler.AppendJob(items.Count);
|
||||
foreach (var item in items)
|
||||
{
|
||||
if (fileService.ContentFileApi.TryOpen(item.Hash, out var stream))
|
||||
{
|
||||
debugService.Log($"Unpack {item.Hash} to: {item.Path}");
|
||||
@@ -81,9 +83,11 @@ public partial class ContentService
|
||||
{
|
||||
debugService.Error("OH FUCK!! " + item.Path);
|
||||
}
|
||||
loadingHandler.AppendResolvedJob();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task Download(Uri contentCdn, List<RobustManifestItem> toDownload, CancellationToken cancellationToken)
|
||||
public async Task Download(Uri contentCdn, List<RobustManifestItem> toDownload, ILoadingHandler loadingHandler, CancellationToken cancellationToken)
|
||||
{
|
||||
if (toDownload.Count == 0 || cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
@@ -91,6 +95,8 @@ public partial class ContentService
|
||||
return;
|
||||
}
|
||||
|
||||
var downloadJobWatch = loadingHandler.GetQueryJob();
|
||||
|
||||
debugService.Log("Downloading from: " + contentCdn);
|
||||
|
||||
var requestBody = new byte[toDownload.Count * 4];
|
||||
@@ -117,6 +123,8 @@ public partial class ContentService
|
||||
debugService.Log("Downloading is cancelled!");
|
||||
return;
|
||||
}
|
||||
|
||||
downloadJobWatch.Dispose();
|
||||
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
@@ -155,6 +163,9 @@ public partial class ContentService
|
||||
var readBuffer = new byte[1024];
|
||||
|
||||
var i = 0;
|
||||
|
||||
loadingHandler.AppendJob(toDownload.Count);
|
||||
|
||||
foreach (var item in toDownload)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
@@ -230,6 +241,7 @@ public partial class ContentService
|
||||
fileService.ContentFileApi.Save(item.Hash, fileStream);
|
||||
|
||||
debugService.Log("file saved:" + item.Path);
|
||||
loadingHandler.AppendResolvedJob();
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -81,13 +81,16 @@ public sealed class EngineService
|
||||
|
||||
try
|
||||
{
|
||||
return _assemblyService.Mount(_fileService.OpenZip(version, _fileService.EngineFileApi));
|
||||
var api = _fileService.OpenZip(version, _fileService.EngineFileApi);
|
||||
if (api != null) return _assemblyService.Mount(api);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_fileService.EngineFileApi.Remove(version);
|
||||
throw;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public async Task DownloadEngine(string version)
|
||||
|
||||
@@ -51,15 +51,24 @@ public class FileService
|
||||
return new FileApi(Path.Join(RootPath, path));
|
||||
}
|
||||
|
||||
public ZipFileApi OpenZip(string path, IFileApi fileApi)
|
||||
public ZipFileApi? OpenZip(string path, IFileApi fileApi)
|
||||
{
|
||||
if (!fileApi.TryOpen(path, out var zipStream))
|
||||
return null;
|
||||
Stream? zipStream = null;
|
||||
try
|
||||
{
|
||||
if (!fileApi.TryOpen(path, out zipStream))
|
||||
return null;
|
||||
|
||||
var zipArchive = new ZipArchive(zipStream, ZipArchiveMode.Read);
|
||||
var zipArchive = new ZipArchive(zipStream, ZipArchiveMode.Read);
|
||||
|
||||
var prefix = "";
|
||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) prefix = "Space Station 14.app/Contents/Resources/";
|
||||
return new ZipFileApi(zipArchive, prefix);
|
||||
var prefix = "";
|
||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) prefix = "Space Station 14.app/Contents/Resources/";
|
||||
return new ZipFileApi(zipArchive, prefix);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
zipStream?.Dispose();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,13 +3,14 @@ namespace Nebula.Shared.Services;
|
||||
[ServiceRegister]
|
||||
public class PopupMessageService
|
||||
{
|
||||
public Action<object?>? OnPopupRequired;
|
||||
public Action<object>? OnPopupRequired;
|
||||
public Action<object>? OnCloseRequired;
|
||||
public void Popup(object obj)
|
||||
{
|
||||
OnPopupRequired?.Invoke(obj);
|
||||
}
|
||||
public void ClosePopup()
|
||||
public void ClosePopup(object obj)
|
||||
{
|
||||
OnPopupRequired?.Invoke(null);
|
||||
OnCloseRequired?.Invoke(obj);
|
||||
}
|
||||
}
|
||||
@@ -25,17 +25,8 @@ public class RestService
|
||||
|
||||
public async Task<RestResult<T>> GetAsync<T>(Uri uri, CancellationToken cancellationToken)
|
||||
{
|
||||
_debug.Debug("GET " + uri);
|
||||
try
|
||||
{
|
||||
var response = await _client.GetAsync(uri, cancellationToken);
|
||||
return await ReadResult<T>(response, cancellationToken);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_debug.Error("ERROR WHILE CONNECTION " + uri + ": " + ex.Message);
|
||||
return new RestResult<T>(default, ex.Message, HttpStatusCode.RequestTimeout);
|
||||
}
|
||||
var response = await _client.GetAsync(uri, cancellationToken);
|
||||
return await ReadResult<T>(response, cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<T> GetAsyncDefault<T>(Uri uri, T defaultValue, CancellationToken cancellationToken)
|
||||
@@ -46,53 +37,25 @@ public class RestService
|
||||
|
||||
public async Task<RestResult<K>> PostAsync<K, T>(T information, Uri uri, CancellationToken cancellationToken)
|
||||
{
|
||||
_debug.Debug("POST " + uri);
|
||||
try
|
||||
{
|
||||
var json = JsonSerializer.Serialize(information, _serializerOptions);
|
||||
var content = new StringContent(json, Encoding.UTF8, "application/json");
|
||||
var response = await _client.PostAsync(uri, content, cancellationToken);
|
||||
return await ReadResult<K>(response, cancellationToken);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_debug.Debug("ERROR " + ex.Message);
|
||||
return new RestResult<K>(default, ex.Message, HttpStatusCode.RequestTimeout);
|
||||
}
|
||||
var json = JsonSerializer.Serialize(information, _serializerOptions);
|
||||
var content = new StringContent(json, Encoding.UTF8, "application/json");
|
||||
var response = await _client.PostAsync(uri, content, cancellationToken);
|
||||
return await ReadResult<K>(response, cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<RestResult<T>> PostAsync<T>(Stream stream, Uri uri, CancellationToken cancellationToken)
|
||||
{
|
||||
_debug.Debug("POST " + uri);
|
||||
try
|
||||
{
|
||||
using var multipartFormContent =
|
||||
new MultipartFormDataContent("Upload----" + DateTime.Now.ToString(CultureInfo.InvariantCulture));
|
||||
multipartFormContent.Add(new StreamContent(stream), "formFile", "image.png");
|
||||
var response = await _client.PostAsync(uri, multipartFormContent, cancellationToken);
|
||||
return await ReadResult<T>(response, cancellationToken);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_debug.Error("ERROR " + ex.Message);
|
||||
if (ex.StackTrace != null) _debug.Error(ex.StackTrace);
|
||||
return new RestResult<T>(default, ex.Message, HttpStatusCode.RequestTimeout);
|
||||
}
|
||||
using var multipartFormContent =
|
||||
new MultipartFormDataContent("Upload----" + DateTime.Now.ToString(CultureInfo.InvariantCulture));
|
||||
multipartFormContent.Add(new StreamContent(stream), "formFile", "image.png");
|
||||
var response = await _client.PostAsync(uri, multipartFormContent, cancellationToken);
|
||||
return await ReadResult<T>(response, cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<RestResult<T>> DeleteAsync<T>(Uri uri, CancellationToken cancellationToken)
|
||||
{
|
||||
_debug.Debug("DELETE " + uri);
|
||||
try
|
||||
{
|
||||
var response = await _client.DeleteAsync(uri, cancellationToken);
|
||||
return await ReadResult<T>(response, cancellationToken);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_debug.Debug("ERROR " + ex.Message);
|
||||
return new RestResult<T>(default, ex.Message, HttpStatusCode.RequestTimeout);
|
||||
}
|
||||
var response = await _client.DeleteAsync(uri, cancellationToken);
|
||||
return await ReadResult<T>(response, cancellationToken);
|
||||
}
|
||||
|
||||
private async Task<RestResult<T>> ReadResult<T>(HttpResponseMessage response, CancellationToken cancellationToken)
|
||||
|
||||
@@ -12,7 +12,7 @@ public sealed class RunnerService(
|
||||
EngineService engineService,
|
||||
AssemblyService assemblyService)
|
||||
{
|
||||
public async Task PrepareRun(RobustBuildInfo buildInfo, CancellationToken cancellationToken)
|
||||
public async Task PrepareRun(RobustBuildInfo buildInfo, ILoadingHandler loadingHandler, CancellationToken cancellationToken)
|
||||
{
|
||||
debugService.Log("Prepare Content!");
|
||||
|
||||
@@ -21,11 +21,11 @@ public sealed class RunnerService(
|
||||
if (engine is null)
|
||||
throw new Exception("Engine version is not usable: " + buildInfo.BuildInfo.Build.EngineVersion);
|
||||
|
||||
await contentService.EnsureItems(buildInfo.RobustManifestInfo, cancellationToken);
|
||||
await contentService.EnsureItems(buildInfo.RobustManifestInfo, loadingHandler, cancellationToken);
|
||||
await engineService.EnsureEngineModules("Robust.Client.WebView", buildInfo.BuildInfo.Build.EngineVersion);
|
||||
}
|
||||
|
||||
public async Task Run(string[] runArgs, RobustBuildInfo buildInfo, IRedialApi redialApi,
|
||||
public async Task Run(string[] runArgs, RobustBuildInfo buildInfo, IRedialApi redialApi, ILoadingHandler loadingHandler,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
debugService.Log("Start Content!");
|
||||
@@ -35,7 +35,7 @@ public sealed class RunnerService(
|
||||
if (engine is null)
|
||||
throw new Exception("Engine version is not usable: " + buildInfo.BuildInfo.Build.EngineVersion);
|
||||
|
||||
await contentService.EnsureItems(buildInfo.RobustManifestInfo, cancellationToken);
|
||||
await contentService.EnsureItems(buildInfo.RobustManifestInfo, loadingHandler, cancellationToken);
|
||||
|
||||
var extraMounts = new List<ApiMount>
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user