Added LoadAll and StopAll function. Some Javascript changes
This commit is contained in:
@ -8,7 +8,6 @@ using Microsoft.ClearScript;
|
||||
using Microsoft.ClearScript.V8;
|
||||
|
||||
using GTA;
|
||||
using GTA.Native;
|
||||
|
||||
namespace CoopClient
|
||||
{
|
||||
@ -17,8 +16,6 @@ namespace CoopClient
|
||||
/// </summary>
|
||||
public class JavascriptHook : Script
|
||||
{
|
||||
private bool LoadedEngine = false;
|
||||
|
||||
private static List<V8ScriptEngine> ScriptEngines;
|
||||
|
||||
/// <summary>
|
||||
@ -36,31 +33,37 @@ namespace CoopClient
|
||||
return;
|
||||
}
|
||||
|
||||
if (!LoadedEngine)
|
||||
lock (ScriptEngines)
|
||||
{
|
||||
LoadedEngine = true;
|
||||
ScriptEngines.ForEach(engine => engine.Script.API.InvokeTick());
|
||||
}
|
||||
}
|
||||
|
||||
ScriptEngines = new List<V8ScriptEngine>();
|
||||
internal static void LoadAll()
|
||||
{
|
||||
ScriptEngines = new List<V8ScriptEngine>();
|
||||
|
||||
string serverAddress = Main.MainSettings.LastServerAddress.Replace(":", ".");
|
||||
string serverAddress = Main.MainSettings.LastServerAddress.Replace(":", ".");
|
||||
|
||||
if (!Directory.Exists("scripts\\resources\\" + serverAddress))
|
||||
if (!Directory.Exists("scripts\\resources\\" + serverAddress))
|
||||
{
|
||||
try
|
||||
{
|
||||
try
|
||||
{
|
||||
Directory.CreateDirectory("scripts\\resources\\" + serverAddress);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// TODO
|
||||
}
|
||||
Directory.CreateDirectory("scripts\\resources\\" + serverAddress);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// TODO
|
||||
}
|
||||
}
|
||||
|
||||
lock (ScriptEngines)
|
||||
{
|
||||
foreach (string script in Directory.GetFiles("scripts\\resources\\" + serverAddress, "*.js"))
|
||||
{
|
||||
V8ScriptEngine engine = new V8ScriptEngine();
|
||||
|
||||
engine.AddHostObject("SHV", new HostTypeCollection(Assembly.LoadFrom("ScriptHookVDotNet3.dll")));
|
||||
engine.AddHostObject("SHVDN", new HostTypeCollection(Assembly.LoadFrom("ScriptHookVDotNet3.dll")));
|
||||
engine.AddHostObject("LemonUI", new HostTypeCollection(Assembly.LoadFrom("scripts\\LemonUI.SHVDN3.dll")));
|
||||
engine.AddHostObject("API", new ScriptContext());
|
||||
|
||||
@ -74,12 +77,20 @@ namespace CoopClient
|
||||
}
|
||||
finally
|
||||
{
|
||||
engine.Script.API.InvokeStart();
|
||||
ScriptEngines.Add(engine);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ScriptEngines.ForEach(engine => engine.Script.API.InvokeRender());
|
||||
internal static void StopAll()
|
||||
{
|
||||
lock (ScriptEngines)
|
||||
{
|
||||
ScriptEngines.ForEach(engine => engine.Script.API.InvokeStop());
|
||||
ScriptEngines.Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -91,33 +102,28 @@ namespace CoopClient
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public event EventHandler OnRender;
|
||||
public event EventHandler OnStart, OnStop, OnTick;
|
||||
|
||||
internal void InvokeRender()
|
||||
internal void InvokeStart()
|
||||
{
|
||||
OnRender?.Invoke(this, EventArgs.Empty);
|
||||
OnStart?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="hash"></param>
|
||||
/// <param name="args"></param>
|
||||
public void CallNative(string hash, params object[] args)
|
||||
internal void InvokeStop()
|
||||
{
|
||||
if (!Hash.TryParse(hash, out Hash ourHash))
|
||||
{
|
||||
return;
|
||||
}
|
||||
OnStop?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
|
||||
Function.Call(ourHash, args.Select(o => new InputArgument(o)).ToArray());
|
||||
internal void InvokeTick()
|
||||
{
|
||||
OnTick?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="message"></param>
|
||||
public void SendMessage(string message)
|
||||
public void SendLocalMessage(string message)
|
||||
{
|
||||
Main.MainChat.AddMessage("JAVASCRIPT", message);
|
||||
}
|
||||
|
@ -129,6 +129,8 @@ namespace CoopClient
|
||||
|
||||
COOPAPI.Connected();
|
||||
GTA.UI.Notification.Show("~g~Connected!");
|
||||
|
||||
JavascriptHook.LoadAll();
|
||||
}
|
||||
break;
|
||||
case NetConnectionStatus.Disconnected:
|
||||
@ -153,6 +155,8 @@ namespace CoopClient
|
||||
|
||||
COOPAPI.Disconnected(reason);
|
||||
GTA.UI.Notification.Show("~r~Disconnected: " + reason);
|
||||
|
||||
JavascriptHook.StopAll();
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
@ -1 +1 @@
|
||||
API.SendMessage('test.js loaded lalala');
|
||||
API.SendLocalMessage('test.js loaded lalala');
|
@ -1,17 +1,24 @@
|
||||
var testVar = 0;
|
||||
var testVar2 = false;
|
||||
function testFunction(message)
|
||||
{
|
||||
SHVDN.GTA.UI.Notification.Show(message);
|
||||
}
|
||||
|
||||
// Called every frame
|
||||
API.OnRender.connect(function() {
|
||||
if (!testVar2)
|
||||
var messageCount = 0;
|
||||
|
||||
// Called every tick
|
||||
API.OnTick.connect(function() {
|
||||
if (messageCount < 5)
|
||||
{
|
||||
testVar2 = true;
|
||||
|
||||
SHV.GTA.UI.Notification.Show("test2.js loaded!");
|
||||
API.SendLocalMessage("testFunction " + ++messageCount);
|
||||
}
|
||||
});
|
||||
|
||||
if (testVar < 10)
|
||||
{
|
||||
API.SendMessage("Test2 number: " + ++testVar);
|
||||
}
|
||||
// Called on connect
|
||||
API.OnStart.connect(function() {
|
||||
testFunction("hello test2.js!");
|
||||
});
|
||||
|
||||
// Called on disconnect
|
||||
API.OnStop.connect(function() {
|
||||
testFunction("bye test2.js!");
|
||||
});
|
Reference in New Issue
Block a user