Unload fix and small tweaks

This commit is contained in:
sardelka9515
2022-10-15 13:52:49 +08:00
parent b48b15b652
commit 411b199a98
14 changed files with 84 additions and 94 deletions

View File

@ -1,53 +1,61 @@
using System;
using System.Collections.Concurrent;
using System.IO;
using System.Threading;
using System.Windows.Forms;
using GTA;
using SHVDN;
using Console = GTA.Console;
namespace RageCoop.Client.Loader
{
public class Main : Script
public class Main : GTA.Script
{
static readonly string GameDir = Directory.GetParent(typeof(SHVDN.ScriptDomain).Assembly.Location).FullName;
static readonly string ScriptsLocation = Path.Combine(GameDir, "RageCoop", "Scripts");
private static readonly ConcurrentQueue<Action> TaskQueue = new ConcurrentQueue<Action>();
static int MainThreadID;
public Main()
{
if (LoaderContext.PrimaryDomain != null) { throw new InvalidOperationException("Improperly placed loader assembly, please re-install to fix this issue"); }
Tick += OnTick;
SHVDN.ScriptDomain.CurrentDomain.Tick += DomainTick;
KeyDown += OnKeyDown;
}
private void OnKeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
Aborted += (s, e) => LoaderContext.UnloadAll();
}
private void OnTick(object sender, EventArgs e)
{
while (Game.IsLoading)
{
Script.Yield();
GTA.Script.Yield();
}
DomainContext.CheckForUnloadRequest();
if (!DomainContext.IsLoaded(ScriptsLocation))
LoaderContext.CheckForUnloadRequest();
if (!LoaderContext.IsLoaded(ScriptsLocation))
{
if (!File.Exists(Path.Combine(ScriptsLocation, "RageCoop.Client.dll")))
{
GTA.UI.Notification.Show("~r~Main assembly is missing, please re-install the client");
Abort();
}
DomainContext.Load(ScriptsLocation);
LoaderContext.Load(ScriptsLocation);
}
}
internal static void QueueToMainThread(Action task)
{
TaskQueue.Enqueue(task);
if (Thread.CurrentThread.ManagedThreadId != MainThreadID)
{
TaskQueue.Enqueue(task);
}
else
{
task();
}
}
private static void DomainTick(object sender, EventArgs e)
{
if (MainThreadID == default) { MainThreadID=Thread.CurrentThread.ManagedThreadId;}
while (TaskQueue.TryDequeue(out var task))
{
try