2022-03-25 19:53:48 +01:00
|
|
|
|
using System;
|
2022-03-25 20:09:45 +01:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Collections.Generic;
|
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
|
|
|
|
|
{
|
|
|
|
|
private bool LoadedEngine = false;
|
|
|
|
|
|
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 20:09:45 +01:00
|
|
|
|
if (LoadedEngine)
|
2022-03-25 19:53:48 +01:00
|
|
|
|
{
|
2022-03-25 20:09:45 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ScriptEngines = new List<V8ScriptEngine>();
|
2022-03-25 19:53:48 +01:00
|
|
|
|
|
2022-03-25 20:09:45 +01:00
|
|
|
|
if (!Directory.Exists("scripts\\resources"))
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Directory.CreateDirectory("scripts\\resources");
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
// TODO
|
|
|
|
|
}
|
2022-03-25 19:53:48 +01:00
|
|
|
|
}
|
2022-03-25 20:09:45 +01:00
|
|
|
|
|
|
|
|
|
foreach (string script in Directory.GetFiles("scripts\\resources", "*.js"))
|
|
|
|
|
{
|
|
|
|
|
V8ScriptEngine engine = new V8ScriptEngine();
|
|
|
|
|
|
|
|
|
|
engine.AddHostObject("script", new ScriptContext());
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
engine.Execute(File.ReadAllText(script));
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
// TODO
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
ScriptEngines.Add(engine);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LoadedEngine = true;
|
2022-03-25 19:53:48 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// FOR JAVASCRIPT ONLY!
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class ScriptContext
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Don't use this!
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void SendMessage(string message)
|
|
|
|
|
{
|
|
|
|
|
Main.MainChat.AddMessage("JAVASCRIPT", message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|