- shit: Cleanup this mess

This commit is contained in:
2025-01-14 22:10:16 +03:00
parent a0b2cfd677
commit 08e518602b
71 changed files with 1022 additions and 943 deletions

View File

@@ -4,6 +4,7 @@ using System.Runtime.InteropServices;
using System.Runtime.Loader;
using Nebula.Shared.FileApis;
using Robust.LoaderApi;
using SharpZstd.Interop;
namespace Nebula.Shared.Services;
@@ -13,23 +14,23 @@ public class AssemblyService
private readonly List<Assembly> _assemblies = new();
private readonly DebugService _debugService;
private readonly HashSet<string> _resolvingAssemblies = new();
public AssemblyService(DebugService debugService)
{
_debugService = debugService;
SharpZstd.Interop.ZstdImportResolver.ResolveLibrary += (name, assembly1, path) =>
ZstdImportResolver.ResolveLibrary += (name, assembly1, path) =>
{
if (name.Equals("SharpZstd.Native"))
{
_debugService.Debug("RESOLVING SHARPZSTD THINK: " + name + " " + path);
GetRuntimeInfo(out string platform, out string architecture, out string extension);
string fileName = GetDllName(platform, architecture, extension);
GetRuntimeInfo(out var platform, out var architecture, out var extension);
var fileName = GetDllName(platform, architecture, extension);
if (NativeLibrary.TryLoad(fileName, assembly1, path, out var nativeLibrary))
{
return nativeLibrary;
}
if (NativeLibrary.TryLoad(fileName, assembly1, path, out var nativeLibrary)) return nativeLibrary;
}
return IntPtr.Zero;
};
}
@@ -98,8 +99,6 @@ public class AssemblyService
assemblyApi.TryOpen($"{name}.pdb", out pdb);
return true;
}
private readonly HashSet<string> _resolvingAssemblies = new HashSet<string>();
private Assembly? OnAssemblyResolving(AssemblyLoadContext context, AssemblyName name, AssemblyApi assemblyApi)
{
@@ -108,7 +107,7 @@ public class AssemblyService
_debugService.Debug($"Already resolving {name.Name}, skipping.");
return null; // Prevent recursive resolution
}
try
{
_resolvingAssemblies.Add(name.FullName);
@@ -130,18 +129,18 @@ public class AssemblyService
if (NativeLibrary.TryLoad(a, out var handle))
return handle;
_debugService.Error("Loading dll error! Not found");
return IntPtr.Zero;
}
public static string GetDllName(
string platform,
string architecture,
string extension)
{
string name = $"SharpZstd.Native.{extension}";
var name = $"SharpZstd.Native.{extension}";
return name;
}
@@ -172,24 +171,14 @@ public class AssemblyService
}
if (RuntimeInformation.ProcessArchitecture == Architecture.X64)
{
architecture = "x64";
}
else if (RuntimeInformation.ProcessArchitecture == Architecture.X86)
{
architecture = "x86";
}
else if (RuntimeInformation.ProcessArchitecture == Architecture.Arm)
{
architecture = "arm";
}
else if (RuntimeInformation.ProcessArchitecture == Architecture.Arm64)
{
architecture = "arm64";
}
else
{
throw new PlatformNotSupportedException("Unsupported process architecture.");
}
}
}

View File

@@ -4,37 +4,37 @@ using Nebula.Shared.Models.Auth;
namespace Nebula.Shared.Services;
[ServiceRegister]
public partial class AuthService(
RestService restService,
DebugService debugService,
public class AuthService(
RestService restService,
DebugService debugService,
CancellationService cancellationService)
{
private readonly HttpClient _httpClient = new();
public CurrentAuthInfo? SelectedAuth { get; internal set; }
public string Reason = "";
public CurrentAuthInfo? SelectedAuth { get; internal set; }
public async Task<bool> Auth(AuthLoginPassword authLoginPassword)
{
var authServer = authLoginPassword.AuthServer;
var login = authLoginPassword.Login;
var password = authLoginPassword.Password;
debugService.Debug($"Auth to {authServer}api/auth/authenticate {login}");
var authUrl = new Uri($"{authServer}api/auth/authenticate");
var result =
await restService.PostAsync<AuthenticateResponse, AuthenticateRequest>(
new AuthenticateRequest(login, password), authUrl, cancellationService.Token);
if (result.Value is null)
{
Reason = result.Message;
return false;
}
SelectedAuth = new CurrentAuthInfo(result.Value.UserId,
SelectedAuth = new CurrentAuthInfo(result.Value.UserId,
new LoginToken(result.Value.Token, result.Value.ExpireTime), authLoginPassword);
return true;
@@ -68,4 +68,5 @@ public partial class AuthService(
}
public sealed record CurrentAuthInfo(Guid UserId, LoginToken Token, AuthLoginPassword AuthLoginPassword);
public record AuthLoginPassword(string Login, string Password, string AuthServer);
public record AuthLoginPassword(string Login, string Password, string AuthServer);

View File

@@ -5,15 +5,15 @@ namespace Nebula.Shared.Services;
public class ConVar<T>
{
public string Name { get; }
public Type Type => typeof(T);
public T? DefaultValue { get; }
public ConVar(string name, T? defaultValue = default)
{
Name = name ?? throw new ArgumentNullException(nameof(name));
DefaultValue = defaultValue;
}
public string Name { get; }
public Type Type => typeof(T);
public T? DefaultValue { get; }
}
public static class ConVarBuilder
@@ -30,8 +30,8 @@ public static class ConVarBuilder
[ServiceRegister]
public class ConfigurationService
{
private readonly FileService _fileService;
private readonly DebugService _debugService;
private readonly FileService _fileService;
public ConfigurationService(FileService fileService, DebugService debugService)
{
@@ -46,7 +46,6 @@ public class ConfigurationService
try
{
if (_fileService.ConfigurationApi.TryOpen(GetFileName(conVar), out var stream))
{
using (stream)
{
var obj = JsonSerializer.Deserialize<T>(stream);
@@ -56,7 +55,6 @@ public class ConfigurationService
return obj;
}
}
}
}
catch (Exception e)
{
@@ -71,10 +69,11 @@ public class ConfigurationService
{
ArgumentNullException.ThrowIfNull(conVar);
if (value == null) throw new ArgumentNullException(nameof(value));
if (!conVar.Type.IsInstanceOfType(value))
{
_debugService.Error($"Type mismatch for config {conVar.Name}. Expected {conVar.Type}, got {value.GetType()}.");
_debugService.Error(
$"Type mismatch for config {conVar.Name}. Expected {conVar.Type}, got {value.GetType()}.");
return;
}
@@ -105,7 +104,8 @@ public class ConfigurationService
public static class ConfigExtensions
{
public static bool TryGetConfigValue<T>(this ConfigurationService configurationService, ConVar<T> conVar, [NotNullWhen(true)] out T? value)
public static bool TryGetConfigValue<T>(this ConfigurationService configurationService, ConVar<T> conVar,
[NotNullWhen(true)] out T? value)
{
ArgumentNullException.ThrowIfNull(configurationService);
ArgumentNullException.ThrowIfNull(conVar);

View File

@@ -15,7 +15,8 @@ public partial class ContentService
return fileService.ContentFileApi.Has(item.Hash);
}
public async Task<List<RobustManifestItem>> EnsureItems(ManifestReader manifestReader, Uri downloadUri, ILoadingHandler loadingHandler,
public async Task<List<RobustManifestItem>> EnsureItems(ManifestReader manifestReader, Uri downloadUri,
ILoadingHandler loadingHandler,
CancellationToken cancellationToken)
{
List<RobustManifestItem> allItems = [];
@@ -66,7 +67,8 @@ public partial class ContentService
return await EnsureItems(manifestReader, info.DownloadUri, loadingHandler, cancellationToken);
}
public async Task Unpack(RobustManifestInfo info, IWriteFileApi otherApi, ILoadingHandler loadingHandler, CancellationToken cancellationToken)
public async Task Unpack(RobustManifestInfo info, IWriteFileApi otherApi, ILoadingHandler loadingHandler,
CancellationToken cancellationToken)
{
debugService.Log("Unpack manifest files");
var items = await EnsureItems(info, loadingHandler, cancellationToken);
@@ -83,11 +85,13 @@ public partial class ContentService
{
debugService.Error("OH FUCK!! " + item.Path);
}
loadingHandler.AppendResolvedJob();
}
}
public async Task Download(Uri contentCdn, List<RobustManifestItem> toDownload, ILoadingHandler loadingHandler, CancellationToken cancellationToken)
public async Task Download(Uri contentCdn, List<RobustManifestItem> toDownload, ILoadingHandler loadingHandler,
CancellationToken cancellationToken)
{
if (toDownload.Count == 0 || cancellationToken.IsCancellationRequested)
{
@@ -110,7 +114,8 @@ public partial class ContentService
var request = new HttpRequestMessage(HttpMethod.Post, contentCdn);
request.Headers.Add(
"X-Robust-Download-Protocol",
varService.GetConfigValue(CurrentConVar.ManifestDownloadProtocolVersion).ToString(CultureInfo.InvariantCulture));
varService.GetConfigValue(CurrentConVar.ManifestDownloadProtocolVersion)
.ToString(CultureInfo.InvariantCulture));
request.Content = new ByteArrayContent(requestBody);
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
@@ -123,7 +128,7 @@ public partial class ContentService
debugService.Log("Downloading is cancelled!");
return;
}
downloadJobWatch.Dispose();
response.EnsureSuccessStatusCode();
@@ -163,9 +168,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)

View File

@@ -5,20 +5,19 @@ namespace Nebula.Shared.Services;
[ServiceRegister]
public class DebugService : IDisposable
{
public ILogger Logger;
private static string LogPath = Path.Combine(FileService.RootPath, "log");
public DateTime LogDate = DateTime.Now;
public ILogger Logger;
private FileStream LogStream;
private StreamWriter LogWriter;
public DebugService(ILogger logger)
{
Logger = logger;
//if (!Directory.Exists(LogPath))
// Directory.CreateDirectory(LogPath);
// Directory.CreateDirectory(LogPath);
//var filename = String.Format("{0:yyyy-MM-dd}.txt", DateTime.Now);
//LogStream = File.Open(Path.Combine(LogPath, filename),
@@ -26,6 +25,12 @@ public class DebugService : IDisposable
//LogWriter = new StreamWriter(LogStream);
}
public void Dispose()
{
LogWriter.Dispose();
LogStream.Dispose();
}
public void Debug(string message)
{
Log(LoggerCategory.Debug, message);
@@ -44,16 +49,10 @@ public class DebugService : IDisposable
public void Error(Exception e)
{
Error(e.Message + "\r\n" + e.StackTrace);
if(e.InnerException != null)
if (e.InnerException != null)
Error(e.InnerException);
}
public void Dispose()
{
LogWriter.Dispose();
LogStream.Dispose();
}
private void Log(LoggerCategory category, string message)
{
Logger.Log(category, message);

View File

@@ -14,12 +14,12 @@ public sealed class EngineService
private readonly RestService _restService;
private readonly IServiceProvider _serviceProvider;
private readonly ConfigurationService _varService;
private readonly Task _currInfoTask;
public Dictionary<string, Module> ModuleInfos = default!;
public Dictionary<string, EngineVersionInfo> VersionInfos = default!;
private Task _currInfoTask;
public EngineService(RestService restService, DebugService debugService, ConfigurationService varService,
FileService fileService, IServiceProvider serviceProvider, AssemblyService assemblyService)
{
@@ -52,7 +52,7 @@ public sealed class EngineService
public EngineBuildInfo? GetVersionInfo(string version)
{
CheckAndWaitValidation();
if (!VersionInfos.TryGetValue(version, out var foundVersion))
return null;
@@ -120,7 +120,7 @@ public sealed class EngineService
public EngineBuildInfo? GetModuleBuildInfo(string moduleName, string version)
{
CheckAndWaitValidation();
if (!ModuleInfos.TryGetValue(moduleName, out var module) ||
!module.Versions.TryGetValue(version, out var value))
return null;
@@ -140,7 +140,7 @@ public sealed class EngineService
public string ResolveModuleVersion(string moduleName, string engineVersion)
{
CheckAndWaitValidation();
var engineVersionObj = Version.Parse(engineVersion);
var module = ModuleInfos[moduleName];
var selectedVersion = module.Versions.Select(kv => new { Version = Version.Parse(kv.Key), kv.Key, kv.Value })
@@ -192,9 +192,9 @@ public sealed class EngineService
private void CheckAndWaitValidation()
{
if (_currInfoTask.IsCompleted)
if (_currInfoTask.IsCompleted)
return;
_debugService.Debug("thinks is not done yet, please wait");
_currInfoTask.Wait();
}

View File

@@ -14,12 +14,12 @@ public class FileService
Environment.SpecialFolder.ApplicationData), "Datum");
private readonly DebugService _debugService;
public readonly IReadWriteFileApi ConfigurationApi;
public readonly IReadWriteFileApi ContentFileApi;
public readonly IReadWriteFileApi EngineFileApi;
public readonly IReadWriteFileApi ManifestFileApi;
public readonly IReadWriteFileApi ConfigurationApi;
private HashApi? _hashApi;
public FileService(DebugService debugService)

View File

@@ -8,43 +8,43 @@ public class HubService
private readonly ConfigurationService _configurationService;
private readonly RestService _restService;
private readonly List<ServerHubInfo> _serverList = new();
public Action<HubServerChangedEventArgs>? HubServerChangedEventArgs;
public Action? HubServerLoaded;
private readonly List<ServerHubInfo> _serverList = new();
public IReadOnlyList<ServerHubInfo> ServerList => _serverList;
private bool _isUpdating = false;
public bool IsUpdating => _isUpdating;
public HubService(ConfigurationService configurationService, RestService restService)
{
_configurationService = configurationService;
_restService = restService;
UpdateHub();
}
public IReadOnlyList<ServerHubInfo> ServerList => _serverList;
public bool IsUpdating { get; private set; }
public async void UpdateHub()
{
if(_isUpdating) return;
if (IsUpdating) return;
_serverList.Clear();
_isUpdating = true;
IsUpdating = true;
HubServerChangedEventArgs?.Invoke(new HubServerChangedEventArgs([], HubServerChangeAction.Clear));
foreach (var urlStr in _configurationService.GetConfigValue(CurrentConVar.Hub)!)
{
var servers = await _restService.GetAsyncDefault<List<ServerHubInfo>>(new Uri(urlStr), [], CancellationToken.None);
var servers =
await _restService.GetAsyncDefault<List<ServerHubInfo>>(new Uri(urlStr), [], CancellationToken.None);
_serverList.AddRange(servers);
HubServerChangedEventArgs?.Invoke(new HubServerChangedEventArgs(servers, HubServerChangeAction.Add));
}
_isUpdating = false;
IsUpdating = false;
HubServerLoaded?.Invoke();
}
}
public class HubServerChangedEventArgs : EventArgs
@@ -61,5 +61,7 @@ public class HubServerChangedEventArgs : EventArgs
public enum HubServerChangeAction
{
Add, Remove, Clear,
Add,
Remove,
Clear
}

View File

@@ -3,12 +3,14 @@ namespace Nebula.Shared.Services;
[ServiceRegister]
public class PopupMessageService
{
public Action<object>? OnPopupRequired;
public Action<object>? OnCloseRequired;
public Action<object>? OnPopupRequired;
public void Popup(object obj)
{
OnPopupRequired?.Invoke(obj);
}
public void ClosePopup(object obj)
{
OnCloseRequired?.Invoke(obj);

View File

@@ -12,7 +12,8 @@ public sealed class RunnerService(
EngineService engineService,
AssemblyService assemblyService)
{
public async Task PrepareRun(RobustBuildInfo buildInfo, ILoadingHandler loadingHandler, CancellationToken cancellationToken)
public async Task PrepareRun(RobustBuildInfo buildInfo, ILoadingHandler loadingHandler,
CancellationToken cancellationToken)
{
debugService.Log("Prepare Content!");
@@ -20,12 +21,13 @@ 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, loadingHandler, cancellationToken);
await engineService.EnsureEngineModules("Robust.Client.WebView", buildInfo.BuildInfo.Build.EngineVersion);
}
public async Task Run(string[] runArgs, RobustBuildInfo buildInfo, IRedialApi redialApi, ILoadingHandler loadingHandler,
public async Task Run(string[] runArgs, RobustBuildInfo buildInfo, IRedialApi redialApi,
ILoadingHandler loadingHandler,
CancellationToken cancellationToken)
{
debugService.Log("Start Content!");
@@ -49,7 +51,8 @@ public sealed class RunnerService(
var args = new MainArgs(runArgs, engine, redialApi, extraMounts);
if (!assemblyService.TryOpenAssembly(varService.GetConfigValue(CurrentConVar.RobustAssemblyName)!, engine, out var clientAssembly))
if (!assemblyService.TryOpenAssembly(varService.GetConfigValue(CurrentConVar.RobustAssemblyName)!, engine,
out var clientAssembly))
throw new Exception("Unable to locate Robust.Client.dll in engine build!");
if (!assemblyService.TryGetLoader(clientAssembly, out var loader))