Initial implementation of configuration support for script plugins

This commit is contained in:
RaidMax 2021-05-14 21:52:55 -05:00
parent 4d21680d59
commit 1595c1fa99
4 changed files with 137 additions and 1 deletions

View File

@ -0,0 +1,15 @@
using System.Collections.Generic;
using SharedLibraryCore.Interfaces;
namespace IW4MAdmin.Application.Configuration
{
public class ScriptPluginConfiguration : Dictionary<string, Dictionary<string, object>>, IBaseConfiguration
{
public string Name() => nameof(ScriptPluginConfiguration);
public IBaseConfiguration Generate()
{
return new ScriptPluginConfiguration();
}
}
}

View File

@ -168,6 +168,7 @@ namespace IW4MAdmin.Application.Misc
}
}
_scriptEngine.SetValue("_configHandler", new ScriptPluginConfigurationWrapper(Name, _scriptEngine));
await OnLoadAsync(manager);
try

View File

@ -0,0 +1,90 @@
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using IW4MAdmin.Application.Configuration;
using Jint;
using Jint.Native;
using Newtonsoft.Json.Linq;
namespace IW4MAdmin.Application.Misc
{
public class ScriptPluginConfigurationWrapper
{
private readonly BaseConfigurationHandler<ScriptPluginConfiguration> _handler;
private readonly ScriptPluginConfiguration _config;
private readonly string _pluginName;
private readonly Engine _scriptEngine;
public ScriptPluginConfigurationWrapper(string pluginName, Engine scriptEngine)
{
_handler = new BaseConfigurationHandler<ScriptPluginConfiguration>("ScriptPluginSettings");
_config = _handler.Configuration() ??
(ScriptPluginConfiguration) new ScriptPluginConfiguration().Generate();
_pluginName = pluginName;
_scriptEngine = scriptEngine;
}
private static int? AsInteger(double d)
{
return int.TryParse(d.ToString(CultureInfo.InvariantCulture), out var parsed) ? parsed : (int?) null;
}
public async Task SetValue(string key, object value)
{
var castValue = value;
if (value is double d)
{
castValue = AsInteger(d) ?? value;
}
if (value is object[] array && array.All(item => item is double d && AsInteger(d) != null))
{
castValue = array.Select(item => (int) item).ToArray();
}
if (!_config.ContainsKey(_pluginName))
{
_config.Add(_pluginName, new Dictionary<string, object>());
}
var plugin = _config[_pluginName];
if (plugin.ContainsKey(key))
{
plugin[key] = castValue;
}
else
{
plugin.Add(key, castValue);
}
_handler.Set(_config);
await _handler.Save();
}
public JsValue GetValue(string key)
{
if (!_config.ContainsKey(_pluginName))
{
return JsValue.Undefined;
}
if (!_config[_pluginName].ContainsKey(key))
{
return JsValue.Undefined;
}
var item = _config[_pluginName][key];
if (item is JArray array)
{
item = array.ToObject<List<dynamic>>();
}
return JsValue.FromObject(_scriptEngine, item);
}
}
}

View File

@ -39,7 +39,7 @@ let commands = [{
let plugin = {
author: 'RaidMax',
version: 1.0,
version: 1.1,
name: 'Ping Pong Sample Command Plugin',
onEventAsync: function (gameEvent, server) {
@ -48,6 +48,36 @@ let plugin = {
onLoadAsync: function (manager) {
this.logger = _serviceResolver.ResolveService("ILogger");
this.logger.WriteDebug("sample plugin loaded");
const intArray = [
1337,
1505,
999
];
const stringArray = [
"ping",
"pong",
"hello"
];
_configHandler.SetValue("SampleIntegerValue", 123);
_configHandler.SetValue("SampleStringValue", this.author);
_configHandler.SetValue("SampleFloatValue", this.version);
_configHandler.SetValue("SampleNumericalArray", intArray);
_configHandler.SetValue("SampleStringArray", stringArray);
this.logger.WriteDebug(_configHandler.GetValue("SampleIntegerValue"));
this.logger.WriteDebug(_configHandler.GetValue("SampleStringValue"));
this.logger.WriteDebug(_configHandler.GetValue("SampleFloatValue"));
_configHandler.GetValue("SampleNumericalArray").forEach((element) => {
this.logger.WriteDebug(element);
});
_configHandler.GetValue("SampleStringArray").forEach((element) => {
this.logger.WriteDebug(element);
});
},
onUnloadAsync: function () {