IW4M-Admin/Application/Plugin/Script/ScriptPluginConfigurationWrapper.cs

131 lines
3.6 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
2023-04-04 19:24:13 -04:00
using System.Globalization;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
using IW4MAdmin.Application.Configuration;
using Jint;
using Jint.Native;
using Jint.Native.Json;
2023-04-04 19:24:13 -04:00
using SharedLibraryCore.Interfaces;
namespace IW4MAdmin.Application.Plugin.Script;
public class ScriptPluginConfigurationWrapper
{
public event Action<JsValue, Delegate> ConfigurationUpdated;
2023-04-04 19:24:13 -04:00
private readonly ScriptPluginConfiguration _config;
private readonly IConfigurationHandlerV2<ScriptPluginConfiguration> _configHandler;
private readonly Engine _scriptEngine;
private readonly JsonParser _engineParser;
private readonly List<(string, Delegate)> _updateCallbackActions = new();
2023-04-04 19:24:13 -04:00
private string _pluginName;
public ScriptPluginConfigurationWrapper(string pluginName, Engine scriptEngine, IConfigurationHandlerV2<ScriptPluginConfiguration> configHandler)
{
_pluginName = pluginName;
_scriptEngine = scriptEngine;
_configHandler = configHandler;
_configHandler.Updated += OnConfigurationUpdated;
2023-04-04 19:24:13 -04:00
_config = configHandler.Get("ScriptPluginSettings", new ScriptPluginConfiguration()).GetAwaiter().GetResult();
_engineParser = new JsonParser(_scriptEngine);
2023-04-04 19:24:13 -04:00
}
~ScriptPluginConfigurationWrapper()
{
_configHandler.Updated -= OnConfigurationUpdated;
}
2023-04-04 19:24:13 -04:00
public void SetName(string name)
{
_pluginName = name;
}
public async Task SetValue(string key, object value)
{
var castValue = value;
if (value is double doubleValue)
{
castValue = AsInteger(doubleValue) ?? value;
}
if (value is object[] array && array.All(item => item is double d && AsInteger(d) != null))
{
castValue = array.Select(item => AsInteger((double)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);
}
await _configHandler.Set(_config);
}
public JsValue GetValue(string key) => GetValue(key, null);
2023-04-04 19:24:13 -04:00
public JsValue GetValue(string key, Delegate updateCallback)
2023-04-04 19:24:13 -04:00
{
if (!_config.ContainsKey(_pluginName))
{
return JsValue.Undefined;
}
if (!_config[_pluginName].ContainsKey(key))
{
return JsValue.Undefined;
}
var item = _config[_pluginName][key];
if (item is JsonElement { ValueKind: JsonValueKind.Array } jElem)
{
item = jElem.Deserialize<List<dynamic>>();
}
if (updateCallback is not null)
{
_updateCallbackActions.Add((key, updateCallback));
}
try
{
return _engineParser.Parse(item!.ToString()!);
}
catch
{
// ignored
}
2023-04-04 19:24:13 -04:00
return JsValue.FromObject(_scriptEngine, item);
}
private static int? AsInteger(double value)
{
return int.TryParse(value.ToString(CultureInfo.InvariantCulture), out var parsed) ? parsed : null;
}
private void OnConfigurationUpdated(ScriptPluginConfiguration config)
{
foreach (var callback in _updateCallbackActions)
{
ConfigurationUpdated?.Invoke(GetValue(callback.Item1), callback.Item2);
}
}
2023-04-04 19:24:13 -04:00
}