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:30:03 +01:00
|
|
|
|
using System.Linq;
|
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-25 20:30:03 +01:00
|
|
|
|
engine.AddHostObject("API", 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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// FOR JAVASCRIPT ONLY!
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class ScriptContext
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
2022-03-25 20:30:03 +01:00
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
2022-03-25 21:51:29 +01:00
|
|
|
|
public event EventHandler OnStart, OnStop, OnTick;
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
2022-03-25 19:53:48 +01:00
|
|
|
|
/// </summary>
|
2022-03-25 20:30:03 +01:00
|
|
|
|
/// <param name="message"></param>
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|