almost done
This commit is contained in:
@@ -1,12 +1,16 @@
|
||||
using System.Collections.Concurrent;
|
||||
using System.Reflection;
|
||||
using System.Text.Json;
|
||||
using Json.Schema;
|
||||
using Json.Schema.Generation;
|
||||
using NLog;
|
||||
using Torch;
|
||||
using Torch.API;
|
||||
using Torch.Managers;
|
||||
using Torch.Server;
|
||||
using Torch.Server.Managers;
|
||||
using Torch.Server.ViewModels;
|
||||
using TorchRemote.Plugin.Refiners;
|
||||
using TorchRemote.Plugin.Utils;
|
||||
|
||||
namespace TorchRemote.Plugin.Managers;
|
||||
@@ -17,6 +21,8 @@ public class SettingManager : Manager
|
||||
|
||||
[Dependency]
|
||||
private readonly InstanceManager _instanceManager = null!;
|
||||
[Dependency]
|
||||
private readonly PluginManager _pluginManager = null!;
|
||||
|
||||
public SettingManager(ITorchBase torchInstance) : base(torchInstance)
|
||||
{
|
||||
@@ -26,25 +32,66 @@ public class SettingManager : Manager
|
||||
{
|
||||
base.Attach();
|
||||
_instanceManager.InstanceLoaded += InstanceManagerOnInstanceLoaded;
|
||||
|
||||
RegisterSetting("Torch Config", Torch.Config, typeof(TorchConfig));
|
||||
|
||||
foreach (var plugin in _pluginManager.Plugins.Values)
|
||||
{
|
||||
var type = plugin.GetType();
|
||||
object persistentInstance;
|
||||
|
||||
const BindingFlags flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
|
||||
|
||||
bool IsSuitable(MemberInfo m, Type t) =>
|
||||
t.IsGenericType && typeof(Persistent<>).IsAssignableFrom(t.GetGenericTypeDefinition()) &&
|
||||
(m.Name.Contains(
|
||||
"config", StringComparison.InvariantCultureIgnoreCase) ||
|
||||
m.Name.Contains(
|
||||
"cfg", StringComparison.InvariantCultureIgnoreCase));
|
||||
|
||||
var fields = type.GetFields(flags).Where(b => IsSuitable(b, b.FieldType)).ToArray();
|
||||
var props = type.GetProperties(flags).Where(b => IsSuitable(b, b.PropertyType)).ToArray();
|
||||
|
||||
if (fields.FirstOrDefault() is { } field)
|
||||
{
|
||||
persistentInstance = field.GetValue(plugin);
|
||||
}
|
||||
else if (props.FirstOrDefault() is { } prop)
|
||||
{
|
||||
persistentInstance = prop.GetValue(plugin);
|
||||
}
|
||||
else
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (persistentInstance is null)
|
||||
continue;
|
||||
|
||||
var persistentType = persistentInstance.GetType();
|
||||
var getter = persistentType.GetProperty("Data")!;
|
||||
|
||||
RegisterSetting(plugin.Name, getter.GetValue(persistentInstance), persistentType.GenericTypeArguments[0]);
|
||||
}
|
||||
}
|
||||
|
||||
private void InstanceManagerOnInstanceLoaded(ConfigDedicatedViewModel config)
|
||||
{
|
||||
RegisterSetting(config.SessionSettings, typeof(SessionSettingsViewModel));
|
||||
RegisterSetting("Session Settings", config.SessionSettings, typeof(SessionSettingsViewModel));
|
||||
}
|
||||
|
||||
public void RegisterSetting(object value, Type type, bool includeOnlyDisplay = true)
|
||||
public void RegisterSetting(string name, object value, Type type)
|
||||
{
|
||||
var builder = new JsonSchemaBuilder().FromType(type, new()
|
||||
{
|
||||
PropertyNamingMethod = input => Statics.SerializerOptions.PropertyNamingPolicy!.ConvertName(input)
|
||||
PropertyNamingMethod = input => Statics.SerializerOptions.PropertyNamingPolicy!.ConvertName(input),
|
||||
Refiners = { new DisplayAttributeRefiner() }
|
||||
});
|
||||
|
||||
Settings[type.FullName!] = new(type.Name, type, builder.Build(), value, includeOnlyDisplay);
|
||||
Log.Info("Registered {0} type", type.FullName);
|
||||
Settings[type.FullName!] = new(name, type, builder.Build(), value);
|
||||
Log.Info("Registered {0} type with name {1}", type.FullName, name);
|
||||
}
|
||||
|
||||
public IDictionary<string, Setting> Settings { get; } = new ConcurrentDictionary<string, Setting>();
|
||||
}
|
||||
|
||||
public record Setting(string Name, Type Type, JsonSchema Schema, object Value, bool IncludeDisplayOnly);
|
||||
public record Setting(string Name, Type Type, JsonSchema Schema, object Value);
|
Reference in New Issue
Block a user