mirror of
https://github.com/RaidMax/IW4M-Admin.git
synced 2025-06-13 08:38:19 -05:00
fix issue with script plugins not reloading (AB#2)
fix issues with collation on MySQL (AB#1)
This commit is contained in:
@ -1,9 +1,11 @@
|
||||
using SharedLibraryCore.Database.Models;
|
||||
using Jint;
|
||||
using SharedLibraryCore.Database.Models;
|
||||
using SharedLibraryCore.Interfaces;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SharedLibraryCore
|
||||
@ -16,14 +18,17 @@ namespace SharedLibraryCore
|
||||
|
||||
public string Author { get; set; }
|
||||
|
||||
private Jint.Engine ScriptEngine;
|
||||
private Engine ScriptEngine;
|
||||
private readonly string FileName;
|
||||
private IManager Manager;
|
||||
private readonly FileSystemWatcher _watcher;
|
||||
private readonly SemaphoreSlim _fileChanging;
|
||||
private bool successfullyLoaded;
|
||||
|
||||
public ScriptPlugin(string fileName)
|
||||
{
|
||||
FileName = fileName;
|
||||
_fileChanging = new SemaphoreSlim(1, 1);
|
||||
_watcher = new FileSystemWatcher()
|
||||
{
|
||||
Path = $"{Utilities.OperatingDirectory}Plugins{Path.DirectorySeparatorChar}",
|
||||
@ -38,30 +43,51 @@ namespace SharedLibraryCore
|
||||
~ScriptPlugin()
|
||||
{
|
||||
_watcher.Dispose();
|
||||
_fileChanging.Dispose();
|
||||
}
|
||||
|
||||
private async void Watcher_Changed(object sender, FileSystemEventArgs e)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _fileChanging.WaitAsync();
|
||||
await Initialize(Manager);
|
||||
}
|
||||
|
||||
catch (Exception ex)
|
||||
{
|
||||
Manager.GetLogger(0).WriteError(Utilities.CurrentLocalization.LocalizationIndex["PLUGIN_IMPORTER_ERROR"].FormatExt(Name));
|
||||
Manager.GetLogger(0).WriteDebug(ex.Message);
|
||||
}
|
||||
|
||||
finally
|
||||
{
|
||||
if (_fileChanging.CurrentCount == 0)
|
||||
{
|
||||
_fileChanging.Release(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task Initialize(IManager mgr)
|
||||
{
|
||||
// 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)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
bool firstRun = ScriptEngine == null;
|
||||
|
||||
// it's been loaded before so we need to call the unload event
|
||||
if (!firstRun)
|
||||
{
|
||||
await OnUnloadAsync();
|
||||
}
|
||||
|
||||
successfullyLoaded = false;
|
||||
Manager = mgr;
|
||||
string script;
|
||||
|
||||
@ -73,7 +99,7 @@ namespace SharedLibraryCore
|
||||
}
|
||||
}
|
||||
|
||||
ScriptEngine = new Jint.Engine(cfg =>
|
||||
ScriptEngine = new Engine(cfg =>
|
||||
cfg.AllowClr(new[]
|
||||
{
|
||||
typeof(System.Net.Http.HttpClient).Assembly,
|
||||
@ -85,14 +111,13 @@ namespace SharedLibraryCore
|
||||
ScriptEngine.SetValue("_localization", Utilities.CurrentLocalization);
|
||||
dynamic pluginObject = ScriptEngine.GetValue("plugin").ToObject();
|
||||
|
||||
this.Author = pluginObject.author;
|
||||
this.Name = pluginObject.name;
|
||||
this.Version = (float)pluginObject.version;
|
||||
|
||||
Author = pluginObject.author;
|
||||
Name = pluginObject.name;
|
||||
Version = (float)pluginObject.version;
|
||||
|
||||
try
|
||||
{
|
||||
if (pluginObject.isParser)
|
||||
if(pluginObject.isParser)
|
||||
{
|
||||
await OnLoadAsync(mgr);
|
||||
IEventParser eventParser = (IEventParser)ScriptEngine.GetValue("eventParser").ToObject();
|
||||
@ -108,21 +133,38 @@ namespace SharedLibraryCore
|
||||
{
|
||||
await OnLoadAsync(mgr);
|
||||
}
|
||||
|
||||
successfullyLoaded = true;
|
||||
}
|
||||
|
||||
public Task OnEventAsync(GameEvent E, Server S)
|
||||
public async Task OnEventAsync(GameEvent E, Server S)
|
||||
{
|
||||
lock (ScriptEngine)
|
||||
if (successfullyLoaded)
|
||||
{
|
||||
ScriptEngine.SetValue("_gameEvent", E);
|
||||
ScriptEngine.SetValue("_server", S);
|
||||
ScriptEngine.SetValue("_IW4MAdminClient", Utilities.IW4MAdminClient(S));
|
||||
return Task.FromResult(ScriptEngine.Execute("plugin.onEventAsync(_gameEvent, _server)").GetCompletionValue());
|
||||
try
|
||||
{
|
||||
await _fileChanging.WaitAsync();
|
||||
ScriptEngine.SetValue("_gameEvent", E);
|
||||
ScriptEngine.SetValue("_server", S);
|
||||
ScriptEngine.SetValue("_IW4MAdminClient", Utilities.IW4MAdminClient(S));
|
||||
ScriptEngine.Execute("plugin.onEventAsync(_gameEvent, _server)").GetCompletionValue();
|
||||
}
|
||||
|
||||
catch { }
|
||||
|
||||
finally
|
||||
{
|
||||
if (_fileChanging.CurrentCount == 0)
|
||||
{
|
||||
_fileChanging.Release(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Task OnLoadAsync(IManager manager)
|
||||
{
|
||||
Manager.GetLogger(0).WriteDebug($"OnLoad executing for {Name}");
|
||||
ScriptEngine.SetValue("_manager", manager);
|
||||
return Task.FromResult(ScriptEngine.Execute("plugin.onLoadAsync(_manager)").GetCompletionValue());
|
||||
}
|
||||
@ -133,9 +175,13 @@ namespace SharedLibraryCore
|
||||
return Task.FromResult(ScriptEngine.Execute("plugin.onTickAsync(_server)").GetCompletionValue());
|
||||
}
|
||||
|
||||
public Task OnUnloadAsync()
|
||||
public async Task OnUnloadAsync()
|
||||
{
|
||||
return Task.FromResult(ScriptEngine.Execute("plugin.onUnloadAsync()").GetCompletionValue());
|
||||
if (successfullyLoaded)
|
||||
{
|
||||
Manager.GetLogger(0).WriteDebug($"OnUnLoad executing for {Name}");
|
||||
await Task.FromResult(ScriptEngine.Execute("plugin.onUnloadAsync()").GetCompletionValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user