2022-10-23 19:02:39 +08:00
|
|
|
|
using System;
|
2022-10-15 11:09:17 +08:00
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
using System.IO;
|
2022-10-15 13:52:49 +08:00
|
|
|
|
using System.Threading;
|
2022-10-23 19:02:39 +08:00
|
|
|
|
using GTA;
|
|
|
|
|
using GTA.UI;
|
|
|
|
|
using SHVDN;
|
2022-10-15 11:09:17 +08:00
|
|
|
|
using Console = GTA.Console;
|
2022-10-23 19:02:39 +08:00
|
|
|
|
using Script = GTA.Script;
|
|
|
|
|
|
2022-10-15 11:09:17 +08:00
|
|
|
|
namespace RageCoop.Client.Loader
|
|
|
|
|
{
|
2022-10-23 19:02:39 +08:00
|
|
|
|
public class Main : Script
|
2022-10-15 11:09:17 +08:00
|
|
|
|
{
|
2022-10-23 19:02:39 +08:00
|
|
|
|
private static readonly string GameDir = Directory.GetParent(typeof(ScriptDomain).Assembly.Location).FullName;
|
2022-10-15 17:06:19 +08:00
|
|
|
|
private static readonly string ScriptsLocation = Path.Combine(GameDir, "RageCoop", "Scripts");
|
2022-10-15 11:09:17 +08:00
|
|
|
|
private static readonly ConcurrentQueue<Action> TaskQueue = new ConcurrentQueue<Action>();
|
2022-10-15 17:06:19 +08:00
|
|
|
|
private static int MainThreadID;
|
2022-10-23 19:02:39 +08:00
|
|
|
|
|
2022-10-15 11:09:17 +08:00
|
|
|
|
public Main()
|
|
|
|
|
{
|
2022-10-23 19:02:39 +08:00
|
|
|
|
if (LoaderContext.PrimaryDomain != null)
|
|
|
|
|
throw new InvalidOperationException(
|
|
|
|
|
"Improperly placed loader assembly, please re-install to fix this issue");
|
2022-10-15 11:09:17 +08:00
|
|
|
|
Tick += OnTick;
|
2022-10-23 19:02:39 +08:00
|
|
|
|
ScriptDomain.CurrentDomain.Tick += DomainTick;
|
2022-10-15 13:52:49 +08:00
|
|
|
|
Aborted += (s, e) => LoaderContext.UnloadAll();
|
2022-10-15 11:09:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnTick(object sender, EventArgs e)
|
|
|
|
|
{
|
2022-10-23 19:02:39 +08:00
|
|
|
|
while (Game.IsLoading) Yield();
|
2022-10-15 13:52:49 +08:00
|
|
|
|
LoaderContext.CheckForUnloadRequest();
|
|
|
|
|
if (!LoaderContext.IsLoaded(ScriptsLocation))
|
2022-10-15 11:09:17 +08:00
|
|
|
|
{
|
|
|
|
|
if (!File.Exists(Path.Combine(ScriptsLocation, "RageCoop.Client.dll")))
|
|
|
|
|
{
|
2022-10-23 19:02:39 +08:00
|
|
|
|
Notification.Show("~r~Main assembly is missing, please re-install the client");
|
2022-10-15 11:09:17 +08:00
|
|
|
|
Abort();
|
|
|
|
|
}
|
2022-10-23 19:02:39 +08:00
|
|
|
|
|
2022-10-15 13:52:49 +08:00
|
|
|
|
LoaderContext.Load(ScriptsLocation);
|
2022-10-15 11:09:17 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal static void QueueToMainThread(Action task)
|
|
|
|
|
{
|
2022-10-15 13:52:49 +08:00
|
|
|
|
if (Thread.CurrentThread.ManagedThreadId != MainThreadID)
|
|
|
|
|
TaskQueue.Enqueue(task);
|
|
|
|
|
else
|
|
|
|
|
task();
|
2022-10-15 11:09:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-15 17:16:47 +08:00
|
|
|
|
private static void DomainTick()
|
2022-10-15 11:09:17 +08:00
|
|
|
|
{
|
2022-10-23 19:02:39 +08:00
|
|
|
|
if (MainThreadID == default) MainThreadID = Thread.CurrentThread.ManagedThreadId;
|
2022-10-15 11:09:17 +08:00
|
|
|
|
while (TaskQueue.TryDequeue(out var task))
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
task.Invoke();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Console.Error(ex.ToString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-10-23 19:02:39 +08:00
|
|
|
|
}
|