fix plugin error spam with multi-servers

This commit is contained in:
RaidMax 2020-02-02 16:21:34 -06:00
parent 60ff33834e
commit 2872d02c37

View File

@ -20,9 +20,9 @@ namespace SharedLibraryCore
public FileSystemWatcher Watcher { get; private set; } public FileSystemWatcher Watcher { get; private set; }
private Engine ScriptEngine; private Engine _scriptEngine;
private readonly string _fileName; private readonly string _fileName;
private readonly SemaphoreSlim _fileChanging; private readonly SemaphoreSlim _onProcessing;
private bool successfullyLoaded; private bool successfullyLoaded;
public ScriptPlugin(string filename) public ScriptPlugin(string filename)
@ -36,19 +36,19 @@ namespace SharedLibraryCore
}; };
Watcher.EnableRaisingEvents = true; Watcher.EnableRaisingEvents = true;
_fileChanging = new SemaphoreSlim(1, 1); _onProcessing = new SemaphoreSlim(1, 1);
} }
~ScriptPlugin() ~ScriptPlugin()
{ {
Watcher.Dispose(); Watcher.Dispose();
_fileChanging.Dispose(); _onProcessing.Dispose();
} }
public async Task Initialize(IManager manager) public async Task Initialize(IManager manager)
{ {
await _fileChanging.WaitAsync(); await _onProcessing.WaitAsync();
try try
{ {
@ -60,7 +60,7 @@ namespace SharedLibraryCore
return; return;
} }
bool firstRun = ScriptEngine == null; bool firstRun = _scriptEngine == null;
// it's been loaded before so we need to call the unload event // it's been loaded before so we need to call the unload event
if (!firstRun) if (!firstRun)
@ -79,7 +79,7 @@ namespace SharedLibraryCore
} }
} }
ScriptEngine = new Engine(cfg => _scriptEngine = new Engine(cfg =>
cfg.AllowClr(new[] cfg.AllowClr(new[]
{ {
typeof(System.Net.Http.HttpClient).Assembly, typeof(System.Net.Http.HttpClient).Assembly,
@ -89,9 +89,9 @@ namespace SharedLibraryCore
}) })
.CatchClrExceptions()); .CatchClrExceptions());
ScriptEngine.Execute(script); _scriptEngine.Execute(script);
ScriptEngine.SetValue("_localization", Utilities.CurrentLocalization); _scriptEngine.SetValue("_localization", Utilities.CurrentLocalization);
dynamic pluginObject = ScriptEngine.GetValue("plugin").ToObject(); dynamic pluginObject = _scriptEngine.GetValue("plugin").ToObject();
Author = pluginObject.author; Author = pluginObject.author;
Name = pluginObject.name; Name = pluginObject.name;
@ -102,8 +102,8 @@ namespace SharedLibraryCore
if (pluginObject.isParser) if (pluginObject.isParser)
{ {
await OnLoadAsync(manager); await OnLoadAsync(manager);
IEventParser eventParser = (IEventParser)ScriptEngine.GetValue("eventParser").ToObject(); IEventParser eventParser = (IEventParser)_scriptEngine.GetValue("eventParser").ToObject();
IRConParser rconParser = (IRConParser)ScriptEngine.GetValue("rconParser").ToObject(); IRConParser rconParser = (IRConParser)_scriptEngine.GetValue("rconParser").ToObject();
manager.AdditionalEventParsers.Add(eventParser); manager.AdditionalEventParsers.Add(eventParser);
manager.AdditionalRConParsers.Add(rconParser); manager.AdditionalRConParsers.Add(rconParser);
} }
@ -126,9 +126,9 @@ namespace SharedLibraryCore
finally finally
{ {
if (_fileChanging.CurrentCount == 0) if (_onProcessing.CurrentCount == 0)
{ {
_fileChanging.Release(1); _onProcessing.Release(1);
} }
} }
} }
@ -137,31 +137,49 @@ namespace SharedLibraryCore
{ {
if (successfullyLoaded) if (successfullyLoaded)
{ {
ScriptEngine.SetValue("_gameEvent", E); await _onProcessing.WaitAsync();
ScriptEngine.SetValue("_server", S);
ScriptEngine.SetValue("_IW4MAdminClient", Utilities.IW4MAdminClient(S)); try
await Task.FromResult(ScriptEngine.Execute("plugin.onEventAsync(_gameEvent, _server)").GetCompletionValue()); {
_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);
}
}
} }
} }
public Task OnLoadAsync(IManager manager) public Task OnLoadAsync(IManager manager)
{ {
manager.GetLogger(0).WriteDebug($"OnLoad executing for {Name}"); manager.GetLogger(0).WriteDebug($"OnLoad executing for {Name}");
ScriptEngine.SetValue("_manager", manager); _scriptEngine.SetValue("_manager", manager);
return Task.FromResult(ScriptEngine.Execute("plugin.onLoadAsync(_manager)").GetCompletionValue()); return Task.FromResult(_scriptEngine.Execute("plugin.onLoadAsync(_manager)").GetCompletionValue());
} }
public Task OnTickAsync(Server S) public Task OnTickAsync(Server S)
{ {
ScriptEngine.SetValue("_server", S); _scriptEngine.SetValue("_server", S);
return Task.FromResult(ScriptEngine.Execute("plugin.onTickAsync(_server)").GetCompletionValue()); return Task.FromResult(_scriptEngine.Execute("plugin.onTickAsync(_server)").GetCompletionValue());
} }
public async Task OnUnloadAsync() public async Task OnUnloadAsync()
{ {
if (successfullyLoaded) if (successfullyLoaded)
{ {
await Task.FromResult(ScriptEngine.Execute("plugin.onUnloadAsync()").GetCompletionValue()); await Task.FromResult(_scriptEngine.Execute("plugin.onUnloadAsync()").GetCompletionValue());
} }
} }
} }