2019-12-25 15:32:57 -05:00
|
|
|
|
using Jint;
|
|
|
|
|
using SharedLibraryCore.Database.Models;
|
2018-11-05 22:01:29 -05:00
|
|
|
|
using SharedLibraryCore.Interfaces;
|
2018-08-23 17:16:30 -04:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
2019-12-25 15:32:57 -05:00
|
|
|
|
using System.Threading;
|
2018-08-23 17:16:30 -04:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace SharedLibraryCore
|
|
|
|
|
{
|
2020-01-31 21:15:07 -05:00
|
|
|
|
public class ScriptPlugin : IPlugin
|
2018-08-23 17:16:30 -04:00
|
|
|
|
{
|
|
|
|
|
public string Name { get; set; }
|
|
|
|
|
|
|
|
|
|
public float Version { get; set; }
|
|
|
|
|
|
2018-08-26 20:20:47 -04:00
|
|
|
|
public string Author { get; set; }
|
2018-08-23 17:16:30 -04:00
|
|
|
|
|
2020-02-01 13:27:14 -05:00
|
|
|
|
public FileSystemWatcher Watcher { get; private set; }
|
|
|
|
|
|
2020-02-02 17:21:34 -05:00
|
|
|
|
private Engine _scriptEngine;
|
2020-02-01 13:27:14 -05:00
|
|
|
|
private readonly string _fileName;
|
2020-02-02 17:21:34 -05:00
|
|
|
|
private readonly SemaphoreSlim _onProcessing;
|
2019-12-25 15:32:57 -05:00
|
|
|
|
private bool successfullyLoaded;
|
2018-08-23 17:16:30 -04:00
|
|
|
|
|
2020-02-01 13:27:14 -05:00
|
|
|
|
public ScriptPlugin(string filename)
|
2018-08-23 17:16:30 -04:00
|
|
|
|
{
|
2020-02-01 13:27:14 -05:00
|
|
|
|
_fileName = filename;
|
|
|
|
|
Watcher = new FileSystemWatcher()
|
2018-08-23 17:16:30 -04:00
|
|
|
|
{
|
|
|
|
|
Path = $"{Utilities.OperatingDirectory}Plugins{Path.DirectorySeparatorChar}",
|
|
|
|
|
NotifyFilter = NotifyFilters.Size,
|
2020-02-01 13:27:14 -05:00
|
|
|
|
Filter = _fileName.Split(Path.DirectorySeparatorChar).Last()
|
2018-08-23 17:16:30 -04:00
|
|
|
|
};
|
|
|
|
|
|
2020-02-01 13:27:14 -05:00
|
|
|
|
Watcher.EnableRaisingEvents = true;
|
2020-02-02 17:21:34 -05:00
|
|
|
|
_onProcessing = new SemaphoreSlim(1, 1);
|
2019-11-15 15:50:20 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~ScriptPlugin()
|
|
|
|
|
{
|
2020-02-01 13:27:14 -05:00
|
|
|
|
Watcher.Dispose();
|
2020-02-02 17:21:34 -05:00
|
|
|
|
_onProcessing.Dispose();
|
2018-08-23 17:16:30 -04:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-25 15:32:57 -05:00
|
|
|
|
|
2020-02-01 13:27:14 -05:00
|
|
|
|
public async Task Initialize(IManager manager)
|
|
|
|
|
{
|
2020-02-02 17:21:34 -05:00
|
|
|
|
await _onProcessing.WaitAsync();
|
2019-12-25 15:32:57 -05:00
|
|
|
|
|
2020-02-01 13:27:14 -05:00
|
|
|
|
try
|
2019-12-25 15:32:57 -05:00
|
|
|
|
{
|
2020-02-01 13:27:14 -05:00
|
|
|
|
// for some reason we get an event trigger when the file is not finished being modified.
|
|
|
|
|
// this must have been a change in .NET CORE 3.x
|
|
|
|
|
// so if the new file is empty we can't process it yet
|
|
|
|
|
if (new FileInfo(_fileName).Length == 0L)
|
2019-12-25 15:32:57 -05:00
|
|
|
|
{
|
2020-02-01 13:27:14 -05:00
|
|
|
|
return;
|
2019-12-25 15:32:57 -05:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-02 17:21:34 -05:00
|
|
|
|
bool firstRun = _scriptEngine == null;
|
2019-12-25 15:32:57 -05:00
|
|
|
|
|
2020-02-01 13:27:14 -05:00
|
|
|
|
// it's been loaded before so we need to call the unload event
|
|
|
|
|
if (!firstRun)
|
|
|
|
|
{
|
|
|
|
|
await OnUnloadAsync();
|
|
|
|
|
}
|
2018-08-23 17:16:30 -04:00
|
|
|
|
|
2020-02-01 13:27:14 -05:00
|
|
|
|
successfullyLoaded = false;
|
|
|
|
|
string script;
|
2018-10-09 21:19:06 -04:00
|
|
|
|
|
2020-02-01 13:27:14 -05:00
|
|
|
|
using (var stream = new FileStream(_fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
|
2018-10-09 21:19:06 -04:00
|
|
|
|
{
|
2020-02-01 13:27:14 -05:00
|
|
|
|
using (var reader = new StreamReader(stream, Encoding.Default))
|
|
|
|
|
{
|
|
|
|
|
script = await reader.ReadToEndAsync();
|
|
|
|
|
}
|
2018-10-09 21:19:06 -04:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-02 17:21:34 -05:00
|
|
|
|
_scriptEngine = new Engine(cfg =>
|
2020-02-01 13:27:14 -05:00
|
|
|
|
cfg.AllowClr(new[]
|
|
|
|
|
{
|
2018-09-02 17:59:27 -04:00
|
|
|
|
typeof(System.Net.Http.HttpClient).Assembly,
|
2018-11-05 22:01:29 -05:00
|
|
|
|
typeof(EFClient).Assembly,
|
2020-01-17 18:31:53 -05:00
|
|
|
|
typeof(Utilities).Assembly,
|
|
|
|
|
typeof(Encoding).Assembly
|
2020-02-01 13:27:14 -05:00
|
|
|
|
})
|
|
|
|
|
.CatchClrExceptions());
|
2018-08-23 17:16:30 -04:00
|
|
|
|
|
2020-02-02 17:21:34 -05:00
|
|
|
|
_scriptEngine.Execute(script);
|
|
|
|
|
_scriptEngine.SetValue("_localization", Utilities.CurrentLocalization);
|
|
|
|
|
dynamic pluginObject = _scriptEngine.GetValue("plugin").ToObject();
|
2018-08-23 17:16:30 -04:00
|
|
|
|
|
2020-02-01 13:27:14 -05:00
|
|
|
|
Author = pluginObject.author;
|
|
|
|
|
Name = pluginObject.name;
|
|
|
|
|
Version = (float)pluginObject.version;
|
2019-01-26 21:33:37 -05:00
|
|
|
|
|
2020-02-01 13:27:14 -05:00
|
|
|
|
try
|
2019-01-27 19:54:18 -05:00
|
|
|
|
{
|
2020-02-01 13:27:14 -05:00
|
|
|
|
if (pluginObject.isParser)
|
|
|
|
|
{
|
|
|
|
|
await OnLoadAsync(manager);
|
2020-02-02 17:21:34 -05:00
|
|
|
|
IEventParser eventParser = (IEventParser)_scriptEngine.GetValue("eventParser").ToObject();
|
|
|
|
|
IRConParser rconParser = (IRConParser)_scriptEngine.GetValue("rconParser").ToObject();
|
2020-02-01 13:27:14 -05:00
|
|
|
|
manager.AdditionalEventParsers.Add(eventParser);
|
|
|
|
|
manager.AdditionalRConParsers.Add(rconParser);
|
|
|
|
|
}
|
2019-01-27 19:54:18 -05:00
|
|
|
|
}
|
2020-02-01 13:27:14 -05:00
|
|
|
|
catch { }
|
2019-01-27 19:54:18 -05:00
|
|
|
|
|
|
|
|
|
|
2020-02-01 13:27:14 -05:00
|
|
|
|
if (!firstRun)
|
|
|
|
|
{
|
|
|
|
|
await OnLoadAsync(manager);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
successfullyLoaded = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
catch
|
2018-08-23 17:16:30 -04:00
|
|
|
|
{
|
2020-02-01 13:27:14 -05:00
|
|
|
|
throw;
|
2018-08-23 17:16:30 -04:00
|
|
|
|
}
|
2019-12-25 15:32:57 -05:00
|
|
|
|
|
2020-02-01 13:27:14 -05:00
|
|
|
|
finally
|
|
|
|
|
{
|
2020-02-02 17:21:34 -05:00
|
|
|
|
if (_onProcessing.CurrentCount == 0)
|
2020-02-01 13:27:14 -05:00
|
|
|
|
{
|
2020-02-02 17:21:34 -05:00
|
|
|
|
_onProcessing.Release(1);
|
2020-02-01 13:27:14 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-08-23 17:16:30 -04:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-25 15:32:57 -05:00
|
|
|
|
public async Task OnEventAsync(GameEvent E, Server S)
|
2018-08-23 17:16:30 -04:00
|
|
|
|
{
|
2019-12-25 15:32:57 -05:00
|
|
|
|
if (successfullyLoaded)
|
2018-09-04 13:40:29 -04:00
|
|
|
|
{
|
2020-02-02 17:21:34 -05:00
|
|
|
|
await _onProcessing.WaitAsync();
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_scriptEngine.SetValue("_gameEvent", E);
|
|
|
|
|
_scriptEngine.SetValue("_server", S);
|
|
|
|
|
_scriptEngine.SetValue("_IW4MAdminClient", Utilities.IW4MAdminClient(S));
|
|
|
|
|
await Task.FromResult(_scriptEngine.Execute("plugin.onEventAsync(_gameEvent, _server)").GetCompletionValue());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
if (_onProcessing.CurrentCount == 0)
|
|
|
|
|
{
|
|
|
|
|
_onProcessing.Release(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-09-04 13:40:29 -04:00
|
|
|
|
}
|
2018-08-23 17:16:30 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task OnLoadAsync(IManager manager)
|
|
|
|
|
{
|
2020-02-01 13:27:14 -05:00
|
|
|
|
manager.GetLogger(0).WriteDebug($"OnLoad executing for {Name}");
|
2020-02-02 17:21:34 -05:00
|
|
|
|
_scriptEngine.SetValue("_manager", manager);
|
|
|
|
|
return Task.FromResult(_scriptEngine.Execute("plugin.onLoadAsync(_manager)").GetCompletionValue());
|
2018-08-23 17:16:30 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task OnTickAsync(Server S)
|
|
|
|
|
{
|
2020-02-02 17:21:34 -05:00
|
|
|
|
_scriptEngine.SetValue("_server", S);
|
|
|
|
|
return Task.FromResult(_scriptEngine.Execute("plugin.onTickAsync(_server)").GetCompletionValue());
|
2018-08-23 17:16:30 -04:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-25 15:32:57 -05:00
|
|
|
|
public async Task OnUnloadAsync()
|
2019-01-27 19:54:18 -05:00
|
|
|
|
{
|
2019-12-25 15:32:57 -05:00
|
|
|
|
if (successfullyLoaded)
|
|
|
|
|
{
|
2020-02-02 17:21:34 -05:00
|
|
|
|
await Task.FromResult(_scriptEngine.Execute("plugin.onUnloadAsync()").GetCompletionValue());
|
2019-12-25 15:32:57 -05:00
|
|
|
|
}
|
2019-01-27 19:54:18 -05:00
|
|
|
|
}
|
2018-08-23 17:16:30 -04:00
|
|
|
|
}
|
|
|
|
|
}
|