API remoting

This commit is contained in:
sardelka9515
2022-10-08 23:49:48 +08:00
parent 54c7f4ce92
commit 5585876005
34 changed files with 41235 additions and 262 deletions

View File

@ -1,41 +1,41 @@
using System;
using System.Collections.Generic;
using SHVDN;
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using SHVDN;
using RageCoop.Core;
using GTA.Native;
using System.Reflection;
using System.Windows.Forms;
namespace RageCoop.Client.Scripting
{
public class ResourceDomain : MarshalByRefObject, IDisposable
internal class ResourceDomain : MarshalByRefObject, IDisposable
{
public static ResourceDomain Instance;
readonly ScriptDomain RootDomain;
ScriptDomain CurrentDomain => ScriptDomain.CurrentDomain;
internal ResourceDomain(ScriptDomain root)
public static ScriptDomain PrimaryDomain;
public static string blah = "blah";
private ScriptDomain CurrentDomain => ScriptDomain.CurrentDomain;
private ResourceDomain(ScriptDomain primary)
{
RootDomain = root;
PrimaryDomain = primary;
// Bridge to current ScriptDomain
root.Tick += Tick;
root.KeyEvent += KeyEvent;
primary.Tick += Tick;
primary.KeyEvent += KeyEvent;
AppDomain.CurrentDomain.SetData("Primary",false);
Main.Console.PrintInfo("Loaded scondary domain: " + AppDomain.CurrentDomain.Id + " " + Main.IsPrimaryDomain);
}
public static void Load()
public static void Load(string dir = @"RageCoop\Scripts")
{
if (Instance != null)
{
throw new Exception("Already loaded");
}
else if (!Main.IsPrimaryDomain)
{
throw new InvalidOperationException("Cannot load in another domain");
}
ScriptDomain domain = null;
try
{
var dir = Path.GetFullPath(@"RageCoop\Scripts");
dir = Path.GetFullPath(dir);
if (Directory.Exists(dir))
{
@ -63,7 +63,8 @@ namespace RageCoop.Client.Scripting
Main.QueueToMainThread(() =>
{
domain = ScriptDomain.Load(Directory.GetParent(typeof(ScriptDomain).Assembly.Location).FullName, dir);
domain.AppDomain.SetData("Console",ScriptDomain.CurrentDomain.AppDomain.GetData("Console"));
domain.AppDomain.SetData("Console", ScriptDomain.CurrentDomain.AppDomain.GetData("Console"));
domain.AppDomain.SetData("RageCoop.Client.API", API.GetInstance());
Instance = (ResourceDomain)domain.AppDomain.CreateInstanceFromAndUnwrap(typeof(ResourceDomain).Assembly.Location, typeof(ResourceDomain).FullName, false, BindingFlags.NonPublic | BindingFlags.Instance, null, new object[] { ScriptDomain.CurrentDomain }, null, null);
domain.Start();
});
@ -81,6 +82,7 @@ namespace RageCoop.Client.Scripting
}
}
}
public static void Unload()
{
if (Instance == null)
@ -91,19 +93,21 @@ namespace RageCoop.Client.Scripting
ScriptDomain.Unload(Instance.CurrentDomain);
Instance = null;
}
void Tick(object sender, EventArgs args)
private void Tick(object sender, EventArgs args)
{
CurrentDomain.DoTick();
}
void KeyEvent(Keys keys, bool status)
private void KeyEvent(Keys keys, bool status)
{
CurrentDomain.DoKeyEvent(keys, status);
}
public void Dispose()
{
RootDomain.Tick -= Tick;
RootDomain.KeyEvent -= KeyEvent;
PrimaryDomain.Tick -= Tick;
PrimaryDomain.KeyEvent -= KeyEvent;
}
}
}