almost done

This commit is contained in:
zznty
2022-10-03 21:49:31 +07:00
parent f001aa4a7e
commit 5e508600f9
19 changed files with 435 additions and 145 deletions

View File

@@ -51,17 +51,17 @@ public class ApiServerManager : Manager
Statics.ChatModule = chatModule;
_server = new WebServer(o => o
.WithUrlPrefix(_config.Listener.UrlPrefix)
.WithMicrosoftHttpListener())
.WithLocalSessionManager()
.WithModule(apiModule
.WithController<ServerController>()
.WithController<SettingsController>()
.WithController<WorldsController>()
.WithController<ChatController>())
.WithModule(new LogsModule("/api/live/logs", true))
.WithModule(chatModule)
.WithBearerToken("/api", new SymmetricSecurityKey(Convert.FromBase64String(_config.SecurityKey)), new BasicAuthorizationServerProvider());
.WithUrlPrefix(_config.Listener.UrlPrefix)
.WithMicrosoftHttpListener())
.WithLocalSessionManager()
.WithModule(apiModule
.WithController<ServerController>()
.WithController<SettingsController>()
.WithController<WorldsController>()
.WithController<ChatController>())
.WithModule(new LogsModule("/api/live/logs", true))
.WithModule(chatModule)
.WithBearerToken("/api", new SymmetricSecurityKey(Convert.FromBase64String(_config.SecurityKey)), new BasicAuthorizationServerProvider());
}
public override void Attach()
@@ -92,4 +92,4 @@ public class ApiServerManager : Manager
return Convert.ToBase64String(aes.Key);
}
}
}

View File

@@ -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);