2018-08-23 17:16:30 -04:00
|
|
|
|
using SharedLibraryCore.Interfaces;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace SharedLibraryCore
|
|
|
|
|
{
|
|
|
|
|
class ScriptPlugin : IPlugin
|
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
private Jint.Engine ScriptEngine;
|
|
|
|
|
private readonly string FileName;
|
|
|
|
|
private IManager Manager;
|
|
|
|
|
|
|
|
|
|
public ScriptPlugin(string fileName)
|
|
|
|
|
{
|
|
|
|
|
FileName = fileName;
|
|
|
|
|
var watcher = new FileSystemWatcher()
|
|
|
|
|
{
|
|
|
|
|
Path = $"{Utilities.OperatingDirectory}Plugins{Path.DirectorySeparatorChar}",
|
|
|
|
|
NotifyFilter = NotifyFilters.Size,
|
|
|
|
|
Filter = fileName.Split(Path.DirectorySeparatorChar).Last()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
watcher.Changed += Watcher_Changed;
|
|
|
|
|
watcher.EnableRaisingEvents = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async void Watcher_Changed(object sender, FileSystemEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await Initialize(Manager);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Manager.GetLogger().WriteError($"{Utilities.CurrentLocalization.LocalizationIndex["PLUGIN_IMPORTER_ERROR"]} {Name}");
|
|
|
|
|
Manager.GetLogger().WriteDebug(ex.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task Initialize(IManager mgr)
|
|
|
|
|
{
|
2018-08-26 20:20:47 -04:00
|
|
|
|
bool firstRun = ScriptEngine == null;
|
2018-08-23 17:16:30 -04:00
|
|
|
|
// it's been loaded before so we need to call the unload event
|
2018-08-26 20:20:47 -04:00
|
|
|
|
if (!firstRun)
|
2018-08-23 17:16:30 -04:00
|
|
|
|
{
|
|
|
|
|
await OnUnloadAsync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Manager = mgr;
|
|
|
|
|
string script = File.ReadAllText(FileName);
|
2018-09-04 13:40:29 -04:00
|
|
|
|
ScriptEngine = new Jint.Engine(cfg =>
|
|
|
|
|
cfg.AllowClr(new[]
|
2018-08-26 20:20:47 -04:00
|
|
|
|
{
|
2018-09-02 17:59:27 -04:00
|
|
|
|
typeof(System.Net.Http.HttpClient).Assembly,
|
2018-08-26 20:20:47 -04:00
|
|
|
|
typeof(Objects.Player).Assembly,
|
|
|
|
|
})
|
|
|
|
|
.CatchClrExceptions());
|
2018-08-23 17:16:30 -04:00
|
|
|
|
|
|
|
|
|
ScriptEngine.Execute(script);
|
|
|
|
|
ScriptEngine.SetValue("_localization", Utilities.CurrentLocalization);
|
2018-09-08 18:29:30 -04:00
|
|
|
|
ScriptEngine.SetValue("_IW4MAdminClient", Utilities.IW4MAdminClient);
|
2018-08-23 17:16:30 -04:00
|
|
|
|
dynamic pluginObject = ScriptEngine.GetValue("plugin").ToObject();
|
|
|
|
|
|
|
|
|
|
this.Author = pluginObject.author;
|
|
|
|
|
this.Name = pluginObject.name;
|
|
|
|
|
this.Version = (float)pluginObject.version;
|
|
|
|
|
|
2018-08-26 20:20:47 -04:00
|
|
|
|
if (!firstRun)
|
2018-08-23 17:16:30 -04:00
|
|
|
|
{
|
|
|
|
|
await OnLoadAsync(mgr);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task OnEventAsync(GameEvent E, Server S)
|
|
|
|
|
{
|
2018-09-04 13:40:29 -04:00
|
|
|
|
lock (ScriptEngine)
|
|
|
|
|
{
|
|
|
|
|
ScriptEngine.SetValue("_gameEvent", E);
|
|
|
|
|
ScriptEngine.SetValue("_server", S);
|
|
|
|
|
return Task.FromResult(ScriptEngine.Execute("plugin.onEventAsync(_gameEvent, _server)").GetCompletionValue());
|
|
|
|
|
}
|
2018-08-23 17:16:30 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task OnLoadAsync(IManager manager)
|
|
|
|
|
{
|
|
|
|
|
ScriptEngine.SetValue("_manager", manager);
|
|
|
|
|
return Task.FromResult(ScriptEngine.Execute("plugin.onLoadAsync(_manager)").GetCompletionValue());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task OnTickAsync(Server S)
|
|
|
|
|
{
|
|
|
|
|
ScriptEngine.SetValue("_server", S);
|
|
|
|
|
return Task.FromResult(ScriptEngine.Execute("plugin.onTickAsync(_server)").GetCompletionValue());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task OnUnloadAsync() => Task.FromResult(ScriptEngine.Execute("plugin.onUnloadAsync()").GetCompletionValue());
|
|
|
|
|
}
|
|
|
|
|
}
|