2021-05-14 22:52:55 -04:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
using System.Linq;
|
2022-03-23 14:34:04 -04:00
|
|
|
|
using System.Text.Json;
|
2021-05-14 22:52:55 -04:00
|
|
|
|
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;
|
2022-01-28 10:35:01 -05:00
|
|
|
|
private ScriptPluginConfiguration _config;
|
2021-05-14 22:52:55 -04:00
|
|
|
|
private readonly string _pluginName;
|
|
|
|
|
private readonly Engine _scriptEngine;
|
|
|
|
|
|
|
|
|
|
public ScriptPluginConfigurationWrapper(string pluginName, Engine scriptEngine)
|
|
|
|
|
{
|
|
|
|
|
_handler = new BaseConfigurationHandler<ScriptPluginConfiguration>("ScriptPluginSettings");
|
|
|
|
|
_pluginName = pluginName;
|
|
|
|
|
_scriptEngine = scriptEngine;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-28 10:35:01 -05:00
|
|
|
|
public async Task InitializeAsync()
|
|
|
|
|
{
|
|
|
|
|
await _handler.BuildAsync();
|
|
|
|
|
_config = _handler.Configuration() ??
|
|
|
|
|
(ScriptPluginConfiguration) new ScriptPluginConfiguration().Generate();
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-14 22:52:55 -04:00
|
|
|
|
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))
|
|
|
|
|
{
|
2021-05-15 10:20:49 -04:00
|
|
|
|
castValue = array.Select(item => AsInteger((double)item)).ToArray();
|
2021-05-14 22:52:55 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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];
|
|
|
|
|
|
2022-03-23 14:34:04 -04:00
|
|
|
|
if (item is JsonElement { ValueKind: JsonValueKind.Array } jElem)
|
2021-05-14 22:52:55 -04:00
|
|
|
|
{
|
2022-03-23 14:34:04 -04:00
|
|
|
|
item = jElem.Deserialize<List<dynamic>>();
|
2021-05-14 22:52:55 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return JsValue.FromObject(_scriptEngine, item);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-01-28 10:35:01 -05:00
|
|
|
|
}
|