Files
RAGECOOP-V/Client/Loader/LoaderContext.cs

209 lines
7.6 KiB
C#
Raw Normal View History

2022-10-23 19:02:39 +08:00
using System;
using System.Collections.Concurrent;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
2022-10-23 19:02:39 +08:00
using GTA.UI;
using SHVDN;
2022-10-15 17:06:19 +08:00
using Console = GTA.Console;
2022-10-23 19:02:39 +08:00
using Script = GTA.Script;
namespace RageCoop.Client.Loader
{
2022-10-15 13:52:49 +08:00
public class LoaderContext : MarshalByRefObject, IDisposable
{
#region PRIMARY-LOADING-LOGIC
2022-10-15 17:06:19 +08:00
2022-10-23 19:02:39 +08:00
public static ConcurrentDictionary<string, LoaderContext> LoadedDomains =>
new ConcurrentDictionary<string, LoaderContext>(_loadedDomains);
private static readonly ConcurrentDictionary<string, LoaderContext> _loadedDomains =
new ConcurrentDictionary<string, LoaderContext>();
public bool UnloadRequested;
public string BaseDirectory => AppDomain.CurrentDomain.BaseDirectory;
private ScriptDomain CurrentDomain => ScriptDomain.CurrentDomain;
2022-10-23 19:02:39 +08:00
public static void CheckForUnloadRequest()
{
lock (_loadedDomains)
{
foreach (var p in _loadedDomains.Values)
if (p.UnloadRequested)
Unload(p);
}
}
public static bool IsLoaded(string dir)
{
return _loadedDomains.ContainsKey(Path.GetFullPath(dir).ToLower());
}
2022-10-23 19:02:39 +08:00
2022-10-15 13:52:49 +08:00
public static LoaderContext Load(string dir)
{
lock (_loadedDomains)
{
dir = Path.GetFullPath(dir).ToLower();
2022-10-23 19:02:39 +08:00
if (IsLoaded(dir)) throw new Exception("Already loaded");
ScriptDomain newDomain = null;
try
{
2022-10-15 13:52:49 +08:00
dir = Path.GetFullPath(dir).ToLower();
Directory.CreateDirectory(dir);
Exception e = null;
// Load domain in main thread
Main.QueueToMainThread(() =>
{
try
{
/*
var assemblies = new List<string>();
assemblies.Add(typeof(DomainLoader).Assembly.Location);
assemblies.AddRange(typeof(DomainLoader).Assembly.GetReferencedAssemblies()
.Select(x => Assembly.Load(x.FullName).Location)
.Where(x => !string.IsNullOrEmpty(x)));
*/
// Delete API assemblies
2022-10-23 19:02:39 +08:00
Directory.GetFiles(dir, "ScriptHookVDotNet*", SearchOption.AllDirectories).ToList()
.ForEach(x => File.Delete(x));
var ctxAsm = Path.Combine(dir, "RageCoop.Client.Loader.dll");
2022-10-23 19:02:39 +08:00
if (File.Exists(ctxAsm)) File.Delete(ctxAsm);
2022-10-15 13:52:49 +08:00
newDomain = ScriptDomain.Load(
Directory.GetParent(typeof(ScriptDomain).Assembly.Location).FullName, dir);
newDomain.AppDomain.SetData("Primary", ScriptDomain.CurrentDomain);
2022-10-23 19:02:39 +08:00
newDomain.AppDomain.SetData("Console",
ScriptDomain.CurrentDomain.AppDomain.GetData("Console"));
2022-10-15 13:52:49 +08:00
var context = (LoaderContext)newDomain.AppDomain.CreateInstanceFromAndUnwrap(
typeof(LoaderContext).Assembly.Location,
2022-10-23 19:02:39 +08:00
typeof(LoaderContext).FullName, false,
BindingFlags.Instance | BindingFlags.NonPublic,
null,
new object[] { }
, null, null);
newDomain.AppDomain.SetData("RageCoop.Client.LoaderContext", context);
newDomain.Start();
2022-10-15 13:52:49 +08:00
_loadedDomains.TryAdd(dir, context);
}
catch (Exception ex)
{
e = ex;
}
});
// Wait till next tick
2022-10-23 19:02:39 +08:00
Script.Yield();
if (e != null) throw e;
return _loadedDomains[dir];
}
catch (Exception ex)
{
2022-10-23 19:02:39 +08:00
Notification.Show(ex.ToString());
Console.Error(ex);
2022-10-23 19:02:39 +08:00
if (newDomain != null) ScriptDomain.Unload(newDomain);
throw;
}
}
}
2022-10-15 13:52:49 +08:00
public static void Unload(LoaderContext domain)
{
lock (_loadedDomains)
{
Exception ex = null;
var name = domain.CurrentDomain.Name;
Console.Info("Unloading domain: " + name);
Main.QueueToMainThread(() =>
{
try
{
2022-10-15 13:52:49 +08:00
if (!_loadedDomains.TryRemove(domain.BaseDirectory.ToLower(), out _))
throw new Exception("Failed to remove domain from list");
domain.Dispose();
ScriptDomain.Unload(domain.CurrentDomain);
}
2022-10-15 17:06:19 +08:00
catch (Exception e)
{
2022-10-15 13:52:49 +08:00
ex = e;
2022-10-23 19:02:39 +08:00
Notification.Show(ex.ToString());
2022-10-15 13:52:49 +08:00
}
});
2022-10-23 19:02:39 +08:00
Script.Yield();
if (ex != null) throw ex;
Console.Info("Unloaded domain: " + name);
}
}
2022-10-23 19:02:39 +08:00
public static void Unload(string dir)
{
Unload(_loadedDomains[Path.GetFullPath(dir).ToLower()]);
}
2022-10-23 19:02:39 +08:00
public static void UnloadAll()
{
lock (_loadedDomains)
{
2022-10-23 19:02:39 +08:00
foreach (var d in _loadedDomains.Values.ToArray()) Unload(d);
}
}
2022-10-23 19:02:39 +08:00
#endregion
2022-10-23 19:02:39 +08:00
#region LOAD-CONTEXT
2022-10-15 13:52:49 +08:00
private LoaderContext()
{
AppDomain.CurrentDomain.DomainUnload += (s, e) => Dispose();
PrimaryDomain.Tick += Tick;
PrimaryDomain.KeyEvent += KeyEvent;
2022-10-23 19:02:39 +08:00
Console.Info(
$"Loaded domain: {AppDomain.CurrentDomain.FriendlyName}, {AppDomain.CurrentDomain.BaseDirectory}");
}
2022-10-23 19:02:39 +08:00
2022-10-15 13:52:49 +08:00
public static ScriptDomain PrimaryDomain => AppDomain.CurrentDomain.GetData("Primary") as ScriptDomain;
2022-10-23 19:02:39 +08:00
public static LoaderContext CurrentContext =>
AppDomain.CurrentDomain.GetData("RageCoop.Client.LoaderContext") as LoaderContext;
/// <summary>
2022-10-23 19:02:39 +08:00
/// Request the current domain to be unloaded
/// </summary>
public static void RequestUnload()
{
if (PrimaryDomain == null)
2022-10-23 19:02:39 +08:00
throw new NotSupportedException(
"Current domain not loaded by the loader therfore cannot be unloaded automatically");
CurrentContext.UnloadRequested = true;
}
2022-10-23 19:02:39 +08:00
2022-10-15 17:16:47 +08:00
private void Tick()
{
CurrentDomain.DoTick();
}
private void KeyEvent(Keys keys, bool status)
{
CurrentDomain.DoKeyEvent(keys, status);
}
2022-10-23 19:02:39 +08:00
2022-10-19 19:07:46 +08:00
public override object InitializeLifetimeService()
{
// Return null to avoid lifetime restriction on the marshaled object.
return null;
}
2022-10-23 19:02:39 +08:00
public void Dispose()
{
lock (this)
{
2022-10-23 19:02:39 +08:00
if (PrimaryDomain == null) return;
PrimaryDomain.Tick -= Tick;
PrimaryDomain.KeyEvent -= KeyEvent;
AppDomain.CurrentDomain.SetData("Primary", null);
}
}
2022-10-23 19:02:39 +08:00
#endregion
}
2022-10-23 19:02:39 +08:00
}