2022-03-25 19:53:48 +01:00
|
|
|
|
using System;
|
2022-03-25 20:09:45 +01:00
|
|
|
|
using System.IO;
|
2022-03-25 20:41:59 +01:00
|
|
|
|
using System.Reflection;
|
2022-03-25 20:09:45 +01:00
|
|
|
|
using System.Collections.Generic;
|
2022-03-25 19:53:48 +01:00
|
|
|
|
|
2022-03-25 20:41:59 +01:00
|
|
|
|
using Microsoft.ClearScript;
|
2022-03-25 19:53:48 +01:00
|
|
|
|
using Microsoft.ClearScript.V8;
|
|
|
|
|
|
|
|
|
|
using GTA;
|
|
|
|
|
|
|
|
|
|
namespace CoopClient
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Don't use this!
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class JavascriptHook : Script
|
|
|
|
|
{
|
2022-03-25 20:09:45 +01:00
|
|
|
|
private static List<V8ScriptEngine> ScriptEngines;
|
2022-03-25 19:53:48 +01:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Don't use this!
|
|
|
|
|
/// </summary>
|
|
|
|
|
public JavascriptHook()
|
|
|
|
|
{
|
|
|
|
|
Tick += Ontick;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Ontick(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (!Main.MainNetworking.IsOnServer())
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-25 21:51:29 +01:00
|
|
|
|
lock (ScriptEngines)
|
2022-03-25 19:53:48 +01:00
|
|
|
|
{
|
2022-03-25 21:51:29 +01:00
|
|
|
|
ScriptEngines.ForEach(engine => engine.Script.API.InvokeTick());
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-03-25 20:09:45 +01:00
|
|
|
|
|
2022-03-25 21:51:29 +01:00
|
|
|
|
internal static void LoadAll()
|
|
|
|
|
{
|
|
|
|
|
ScriptEngines = new List<V8ScriptEngine>();
|
2022-03-25 19:53:48 +01:00
|
|
|
|
|
2022-03-25 21:51:29 +01:00
|
|
|
|
string serverAddress = Main.MainSettings.LastServerAddress.Replace(":", ".");
|
2022-03-25 21:07:07 +01:00
|
|
|
|
|
2022-03-25 21:51:29 +01:00
|
|
|
|
if (!Directory.Exists("scripts\\resources\\" + serverAddress))
|
|
|
|
|
{
|
|
|
|
|
try
|
2022-03-25 20:09:45 +01:00
|
|
|
|
{
|
2022-03-25 21:51:29 +01:00
|
|
|
|
Directory.CreateDirectory("scripts\\resources\\" + serverAddress);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
// TODO
|
2022-03-25 20:09:45 +01:00
|
|
|
|
}
|
2022-03-25 21:51:29 +01:00
|
|
|
|
}
|
2022-03-25 20:30:03 +01:00
|
|
|
|
|
2022-03-25 21:51:29 +01:00
|
|
|
|
lock (ScriptEngines)
|
|
|
|
|
{
|
2022-03-25 21:07:07 +01:00
|
|
|
|
foreach (string script in Directory.GetFiles("scripts\\resources\\" + serverAddress, "*.js"))
|
2022-03-25 20:09:45 +01:00
|
|
|
|
{
|
2022-03-25 20:30:03 +01:00
|
|
|
|
V8ScriptEngine engine = new V8ScriptEngine();
|
2022-03-25 20:09:45 +01:00
|
|
|
|
|
2022-03-25 21:51:29 +01:00
|
|
|
|
engine.AddHostObject("SHVDN", new HostTypeCollection(Assembly.LoadFrom("ScriptHookVDotNet3.dll")));
|
2022-03-25 20:41:59 +01:00
|
|
|
|
engine.AddHostObject("LemonUI", new HostTypeCollection(Assembly.LoadFrom("scripts\\LemonUI.SHVDN3.dll")));
|
2022-03-27 19:49:23 +02:00
|
|
|
|
engine.AddHostObject("API", HostItemFlags.PrivateAccess, new ScriptContext());
|
2022-03-25 20:09:45 +01:00
|
|
|
|
|
2022-03-25 20:30:03 +01:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
engine.Execute(File.ReadAllText(script));
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
// TODO
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
2022-03-25 21:51:29 +01:00
|
|
|
|
engine.Script.API.InvokeStart();
|
2022-03-25 20:30:03 +01:00
|
|
|
|
ScriptEngines.Add(engine);
|
|
|
|
|
}
|
2022-03-25 20:09:45 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-03-25 21:51:29 +01:00
|
|
|
|
}
|
2022-03-25 20:09:45 +01:00
|
|
|
|
|
2022-03-25 21:51:29 +01:00
|
|
|
|
internal static void StopAll()
|
|
|
|
|
{
|
|
|
|
|
lock (ScriptEngines)
|
|
|
|
|
{
|
|
|
|
|
ScriptEngines.ForEach(engine => engine.Script.API.InvokeStop());
|
|
|
|
|
ScriptEngines.Clear();
|
|
|
|
|
}
|
2022-03-25 19:53:48 +01:00
|
|
|
|
}
|
2022-03-26 17:50:34 +01:00
|
|
|
|
|
|
|
|
|
internal static void InvokePlayerConnect(string username, long nethandle)
|
|
|
|
|
{
|
|
|
|
|
lock (ScriptEngines)
|
|
|
|
|
{
|
|
|
|
|
ScriptEngines.ForEach(engine => engine.Script.API.InvokePlayerConnect(username, nethandle));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal static void InvokePlayerDisonnect(string username, long nethandle, string reason = null)
|
|
|
|
|
{
|
|
|
|
|
lock (ScriptEngines)
|
|
|
|
|
{
|
|
|
|
|
ScriptEngines.ForEach(engine => engine.Script.API.InvokePlayerDisonnect(username, nethandle, reason));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal static void InvokeChatMessage(string from, string message)
|
|
|
|
|
{
|
|
|
|
|
lock (ScriptEngines)
|
|
|
|
|
{
|
|
|
|
|
ScriptEngines.ForEach(engine => engine.Script.API.InvokeChatMessage(from, message));
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-03-25 19:53:48 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-27 19:49:23 +02:00
|
|
|
|
internal class ScriptContext
|
2022-03-25 19:53:48 +01:00
|
|
|
|
{
|
2022-03-27 19:49:23 +02:00
|
|
|
|
#region DELEGATES
|
2022-03-26 17:50:34 +01:00
|
|
|
|
public delegate void PlayerConnectEvent(string username, long nethandle, string reason);
|
|
|
|
|
public delegate void ChatMessageEvent(string from, string message);
|
2022-03-27 19:49:23 +02:00
|
|
|
|
#endregion
|
2022-03-26 17:50:34 +01:00
|
|
|
|
|
2022-03-27 19:49:23 +02:00
|
|
|
|
#region EVENTS
|
2022-03-25 21:51:29 +01:00
|
|
|
|
public event EventHandler OnStart, OnStop, OnTick;
|
2022-03-26 17:50:34 +01:00
|
|
|
|
public event PlayerConnectEvent OnPlayerConnect, OnPlayerDisconnect;
|
|
|
|
|
public event ChatMessageEvent OnChatMessage;
|
2022-03-25 20:30:03 +01:00
|
|
|
|
|
2022-03-25 21:51:29 +01:00
|
|
|
|
internal void InvokeStart()
|
2022-03-25 20:30:03 +01:00
|
|
|
|
{
|
2022-03-25 21:51:29 +01:00
|
|
|
|
OnStart?.Invoke(this, EventArgs.Empty);
|
2022-03-25 20:30:03 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-25 21:51:29 +01:00
|
|
|
|
internal void InvokeStop()
|
2022-03-25 20:30:03 +01:00
|
|
|
|
{
|
2022-03-25 21:51:29 +01:00
|
|
|
|
OnStop?.Invoke(this, EventArgs.Empty);
|
|
|
|
|
}
|
2022-03-25 20:30:03 +01:00
|
|
|
|
|
2022-03-25 21:51:29 +01:00
|
|
|
|
internal void InvokeTick()
|
|
|
|
|
{
|
|
|
|
|
OnTick?.Invoke(this, EventArgs.Empty);
|
2022-03-25 20:30:03 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-26 17:50:34 +01:00
|
|
|
|
internal void InvokePlayerConnect(string username, long nethandle)
|
|
|
|
|
{
|
|
|
|
|
OnPlayerConnect?.Invoke(username, nethandle, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal void InvokePlayerDisonnect(string username, long nethandle, string reason)
|
|
|
|
|
{
|
|
|
|
|
OnPlayerDisconnect?.Invoke(username, nethandle, reason);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal void InvokeChatMessage(string from, string message)
|
|
|
|
|
{
|
|
|
|
|
OnChatMessage?.Invoke(from, message);
|
|
|
|
|
}
|
2022-03-27 19:49:23 +02:00
|
|
|
|
#endregion
|
2022-03-26 17:50:34 +01:00
|
|
|
|
|
2022-03-25 21:51:29 +01:00
|
|
|
|
public void SendLocalMessage(string message)
|
2022-03-25 19:53:48 +01:00
|
|
|
|
{
|
|
|
|
|
Main.MainChat.AddMessage("JAVASCRIPT", message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|