- tweak: Migrate v3 profiles
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Text.Json;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Nebula.Shared.FileApis.Interfaces;
|
||||
using Nebula.Shared.Models;
|
||||
using Nebula.Shared.Services.Logging;
|
||||
using Robust.LoaderApi;
|
||||
|
||||
@@ -30,23 +32,116 @@ public static class ConVarBuilder
|
||||
|
||||
return new ConVar<T>(name, defaultValue);
|
||||
}
|
||||
|
||||
public static ConVar<T> BuildWithMigration<T>(string name, IConfigurationMigration migration, T? defaultValue = default)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(name))
|
||||
throw new ArgumentException("ConVar name cannot be null or whitespace.", nameof(name));
|
||||
|
||||
ConfigurationService.AddConfigurationMigration(migration);
|
||||
|
||||
return new ConVar<T>(name, defaultValue);
|
||||
}
|
||||
}
|
||||
|
||||
public interface IConfigurationMigration
|
||||
{
|
||||
public Task DoMigrate(ConfigurationService configurationService, IServiceProvider serviceProvider, ILoadingHandler loadingHandler);
|
||||
}
|
||||
|
||||
public abstract class BaseConfigurationMigration<T1,T2> : IConfigurationMigration
|
||||
{
|
||||
protected ConVar<T1> OldConVar;
|
||||
protected ConVar<T2> NewConVar;
|
||||
|
||||
public BaseConfigurationMigration(string oldName, string newName)
|
||||
{
|
||||
OldConVar = ConVarBuilder.Build<T1>(oldName);
|
||||
NewConVar = ConVarBuilder.Build<T2>(newName);
|
||||
}
|
||||
|
||||
public async Task DoMigrate(ConfigurationService configurationService, IServiceProvider serviceProvider, ILoadingHandler loadingHandler)
|
||||
{
|
||||
var oldValue = configurationService.GetConfigValue(OldConVar);
|
||||
if(oldValue == null) return;
|
||||
|
||||
var newValue = await Migrate(serviceProvider, oldValue, loadingHandler);
|
||||
configurationService.SetConfigValue(NewConVar, newValue);
|
||||
configurationService.ClearConfigValue(OldConVar);
|
||||
}
|
||||
|
||||
protected abstract Task<T2> Migrate(IServiceProvider serviceProvider, T1 oldValue, ILoadingHandler loadingHandler);
|
||||
}
|
||||
|
||||
public class MigrationQueue(List<IConfigurationMigration> migrations) : IConfigurationMigration
|
||||
{
|
||||
public async Task DoMigrate(ConfigurationService configurationService, IServiceProvider serviceProvider , ILoadingHandler loadingHandler)
|
||||
{
|
||||
foreach (var migration in migrations)
|
||||
{
|
||||
await migration.DoMigrate(configurationService, serviceProvider, loadingHandler);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class MigrationQueueBuilder
|
||||
{
|
||||
public static MigrationQueueBuilder Instance => new();
|
||||
|
||||
private readonly List<IConfigurationMigration> _migrations = [];
|
||||
|
||||
public MigrationQueueBuilder With(IConfigurationMigration migration)
|
||||
{
|
||||
_migrations.Add(migration);
|
||||
return this;
|
||||
}
|
||||
|
||||
public MigrationQueue Build()
|
||||
{
|
||||
return new MigrationQueue(_migrations);
|
||||
}
|
||||
}
|
||||
|
||||
[ServiceRegister]
|
||||
public class ConfigurationService
|
||||
{
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
private static List<IConfigurationMigration> _migrations = [];
|
||||
|
||||
public static void AddConfigurationMigration(IConfigurationMigration configurationMigration)
|
||||
{
|
||||
_migrations.Add(configurationMigration);
|
||||
}
|
||||
|
||||
public delegate void OnConfigurationChangedDelegate<in T>(T value);
|
||||
|
||||
public IReadWriteFileApi ConfigurationApi { get; init; }
|
||||
public IReadWriteFileApi ConfigurationApi { get; }
|
||||
|
||||
private readonly ILogger _logger;
|
||||
|
||||
public ConfigurationService(FileService fileService, DebugService debugService)
|
||||
public ConfigurationService(FileService fileService, DebugService debugService, IServiceProvider serviceProvider)
|
||||
{
|
||||
_serviceProvider = serviceProvider;
|
||||
_logger = debugService.GetLogger(this);
|
||||
ConfigurationApi = fileService.CreateFileApi("config");
|
||||
}
|
||||
|
||||
public void MigrateConfigs(ILoadingHandler loadingHandler)
|
||||
{
|
||||
Task.Run(async () =>
|
||||
{
|
||||
foreach (var migration in _migrations)
|
||||
{
|
||||
await migration.DoMigrate(this, _serviceProvider, loadingHandler);
|
||||
}
|
||||
|
||||
if (loadingHandler is IDisposable disposable)
|
||||
{
|
||||
disposable.Dispose();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public ConfigChangeSubscriberDisposable<T> SubscribeVarChanged<T>(ConVar<T> convar, OnConfigurationChangedDelegate<T?> @delegate, bool invokeNow = false)
|
||||
{
|
||||
convar.OnValueChanged += @delegate;
|
||||
@@ -112,12 +207,17 @@ public class ConfigurationService
|
||||
return false;
|
||||
}
|
||||
|
||||
public void ClearConfigValue<T>(ConVar<T> conVar)
|
||||
{
|
||||
ConfigurationApi.Remove(GetFileName(conVar));
|
||||
conVar.OnValueChanged?.Invoke(conVar.DefaultValue);
|
||||
}
|
||||
|
||||
public void SetConfigValue<T>(ConVar<T> conVar, T? value)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
ConfigurationApi.Remove(GetFileName(conVar));
|
||||
conVar.OnValueChanged?.Invoke(conVar.DefaultValue);
|
||||
ClearConfigValue(conVar);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user