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

135 lines
3.9 KiB
C#
Raw Normal View History

2024-12-22 16:38:47 +03:00
using System.Diagnostics.CodeAnalysis;
2024-12-27 08:22:17 +03:00
using System.Text.Json;
using Nebula.Shared.FileApis.Interfaces;
using Nebula.Shared.Services.Logging;
using Robust.LoaderApi;
2024-12-22 16:38:47 +03:00
2025-01-05 17:05:23 +03:00
namespace Nebula.Shared.Services;
2024-12-22 16:38:47 +03:00
2024-12-27 19:15:33 +03:00
public class ConVar<T>
2024-12-22 16:38:47 +03:00
{
2024-12-28 08:29:01 +03:00
public ConVar(string name, T? defaultValue = default)
2024-12-22 16:38:47 +03:00
{
2024-12-28 08:29:01 +03:00
Name = name ?? throw new ArgumentNullException(nameof(name));
2024-12-22 16:38:47 +03:00
DefaultValue = defaultValue;
}
2025-01-14 22:10:16 +03:00
public string Name { get; }
public Type Type => typeof(T);
public T? DefaultValue { get; }
2024-12-27 19:15:33 +03:00
}
2024-12-22 16:38:47 +03:00
2024-12-27 19:15:33 +03:00
public static class ConVarBuilder
{
public static ConVar<T> Build<T>(string name, T? defaultValue = default)
2024-12-22 16:38:47 +03:00
{
2024-12-28 08:29:01 +03:00
if (string.IsNullOrWhiteSpace(name))
throw new ArgumentException("ConVar name cannot be null or whitespace.", nameof(name));
2024-12-27 19:15:33 +03:00
return new ConVar<T>(name, defaultValue);
2024-12-22 16:38:47 +03:00
}
}
[ServiceRegister]
public class ConfigurationService
{
public IReadWriteFileApi ConfigurationApi { get; init; }
private readonly ILogger _logger;
2024-12-27 08:22:17 +03:00
public ConfigurationService(FileService fileService, DebugService debugService)
2024-12-22 16:38:47 +03:00
{
_logger = debugService.GetLogger(this);
ConfigurationApi = fileService.CreateFileApi("config");
2024-12-22 16:38:47 +03:00
}
2024-12-27 19:15:33 +03:00
public T? GetConfigValue<T>(ConVar<T> conVar)
2024-12-22 16:38:47 +03:00
{
2024-12-28 08:29:01 +03:00
ArgumentNullException.ThrowIfNull(conVar);
2024-12-22 16:38:47 +03:00
2024-12-27 08:22:17 +03:00
try
{
if (ConfigurationApi.TryOpen(GetFileName(conVar), out var stream))
2024-12-28 08:29:01 +03:00
using (stream)
{
var obj = JsonSerializer.Deserialize<T>(stream);
if (obj != null)
{
_logger.Log($"Successfully loaded config: {conVar.Name}");
2024-12-28 08:29:01 +03:00
return obj;
}
}
2024-12-27 08:22:17 +03:00
}
catch (Exception e)
{
_logger.Error($"Error loading config for {conVar.Name}: {e.Message}");
2024-12-27 08:22:17 +03:00
}
2024-12-28 08:29:01 +03:00
_logger.Log($"Using default value for config: {conVar.Name}");
2024-12-28 08:29:01 +03:00
return conVar.DefaultValue;
2024-12-22 16:38:47 +03:00
}
2025-05-01 20:58:43 +03:00
public bool TryGetConfigValue<T>(ConVar<T> conVar,
[NotNullWhen(true)] out T? value)
{
ArgumentNullException.ThrowIfNull(conVar);
value = default;
try
{
if (ConfigurationApi.TryOpen(GetFileName(conVar), out var stream))
2025-05-01 20:58:43 +03:00
using (stream)
{
var obj = JsonSerializer.Deserialize<T>(stream);
if (obj != null)
{
_logger.Log($"Successfully loaded config: {conVar.Name}");
2025-05-01 20:58:43 +03:00
value = obj;
return true;
}
}
}
catch (Exception e)
{
_logger.Error($"Error loading config for {conVar.Name}: {e.Message}");
2025-05-01 20:58:43 +03:00
}
_logger.Log($"Using default value for config: {conVar.Name}");
2025-05-01 20:58:43 +03:00
return false;
}
2024-12-22 16:38:47 +03:00
2024-12-28 08:29:01 +03:00
public void SetConfigValue<T>(ConVar<T> conVar, T value)
2024-12-22 16:38:47 +03:00
{
2024-12-28 08:29:01 +03:00
ArgumentNullException.ThrowIfNull(conVar);
if (value == null) throw new ArgumentNullException(nameof(value));
2025-01-14 22:10:16 +03:00
2024-12-28 08:29:01 +03:00
if (!conVar.Type.IsInstanceOfType(value))
{
_logger.Error(
2025-01-14 22:10:16 +03:00
$"Type mismatch for config {conVar.Name}. Expected {conVar.Type}, got {value.GetType()}.");
2024-12-28 08:29:01 +03:00
return;
}
2024-12-27 08:22:17 +03:00
try
{
_logger.Log($"Saving config: {conVar.Name}");
2024-12-28 08:29:01 +03:00
var serializedData = JsonSerializer.Serialize(value);
using var stream = new MemoryStream();
2024-12-30 22:23:55 +03:00
using var writer = new StreamWriter(stream);
writer.Write(serializedData);
writer.Flush();
stream.Seek(0, SeekOrigin.Begin);
2024-12-28 08:29:01 +03:00
ConfigurationApi.Save(GetFileName(conVar), stream);
2024-12-27 08:22:17 +03:00
}
catch (Exception e)
{
_logger.Error($"Error saving config for {conVar.Name}: {e.Message}");
2024-12-27 08:22:17 +03:00
}
2024-12-22 16:38:47 +03:00
}
2024-12-28 08:29:01 +03:00
private static string GetFileName<T>(ConVar<T> conVar)
2024-12-22 16:38:47 +03:00
{
2024-12-28 08:29:01 +03:00
return $"{conVar.Name}.json";
2024-12-27 08:22:17 +03:00
}
2024-12-22 16:38:47 +03:00
}