-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMagePlugin.cs
More file actions
94 lines (81 loc) · 2.48 KB
/
MagePlugin.cs
File metadata and controls
94 lines (81 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#pragma warning disable CS8618
#pragma warning disable CS8603
using BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Logging;
using BlackMagicAPI;
using FishNet.Managing;
using FishNet.Transporting;
using HarmonyLib;
using MageAPI.Attributes;
using MageAPI.Modules;
using MageAPI.Modules.Events.Core;
using MageAPI.Mono;
using MageAPI.Patches.Client;
using System.Collections;
using System.Reflection;
namespace MageAPI;
/// <inheritdoc/>
[BepInProcess("MageArena")]
[BepInPlugin(ModMetaData.GUID, ModMetaData.PLUGIN_NAME, ModMetaData.VERSION)]
public class MagePlugin : BaseUnityPlugin
{
internal static MagePlugin Instance { get; private set; }
internal static Harmony? Harmony;
internal static ManualLogSource Log => Instance._log;
private ManualLogSource? _log;
/// <inheritdoc/>
public static string modsync = "all";
private void Awake()
{
_log = Logger;
Instance = this;
Harmony = new(ModMetaData.GUID);
Harmony.PatchAll();
Debugger.Create();
CustomLayers.Init();
StartCoroutine(CoAwake());
Log.LogInfo($"MageAPI v{ModMetaData.VERSION} loaded!");
}
private static IEnumerator CoAwake()
{
while (!Chainloader._loaded)
{
yield return null;
}
RpcHarmonyPatch.PatchAll();
InstanceModulePatch.ItemInstancePatch.Patch();
while (NetworkManager.Instances.FirstOrDefault()?.ClientManager == null)
{
yield return null;
}
NetworkManager.Instances.First().ClientManager.OnRemoteConnectionState += (args) =>
{
if (args.ConnectionState == RemoteConnectionState.Stopped)
{
if (NetworkManager.Instances?.FirstOrDefault()?.ServerManager?.Clients?.TryGetValue(args.ConnectionId, out var conn) == true)
{
Events.Client.ClientDisconnectEvent.After.InvokeEvent((conn, null));
}
}
};
}
internal static void Patch(MethodBase[] methods, HarmonyMethod? prefix = null, HarmonyMethod? postfix = null)
{
if (Harmony == null) return;
try
{
foreach (var method in methods)
{
if (method != null)
{
Harmony.Patch(original: method, prefix: prefix, postfix: postfix);
}
}
}
catch (Exception ex)
{
Log.LogError($"Failed to patch: {ex}");
}
}
}