Added : Module / Events / Patches Structure and System

This commit is contained in:
2026-03-06 23:37:32 +01:00
parent af40d24900
commit 61c519be05
5 changed files with 483 additions and 2 deletions

View File

@@ -1,16 +1,98 @@
using BepInEx;
using System;
using System.Collections.Generic;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using JordanMod.Events;
using JordanMod.Modules;
namespace JordanMod;
[BepInAutoPlugin]
public partial class Plugin : BaseUnityPlugin
{
public static Plugin Instance { get; private set; } = null!;
internal static ManualLogSource Log { get; private set; } = null!;
private static Harmony? _harmony;
private ModuleManager? _moduleManager;
private static readonly List<Type> _globalPatches = [];
private void Awake()
{
Instance = this;
ConfigHandler.Initialize(Config);
Log = Logger;
Log.LogInfo($"Plugin {Name} is loaded!");
Debug.Log("JordanMod is starting...");
SetupEvents();
SetupModules();
SetupPatches();
}
protected void SetupEvents()
{
GlobalEventListener.Initialize();
}
protected void SetupModules()
{
_moduleManager = new ModuleManager();
_moduleManager.Initialize();
}
protected void SetupPatches()
{
_harmony ??= new Harmony(Info.Metadata.GUID);
Type[] patches = [.. _globalPatches];
if (_moduleManager != null)
{
foreach (Module module in _moduleManager.GetAllModules())
{
patches = [.. patches, .. module.GetPatches()];
}
}
foreach (var patchType in patches)
{
try
{
_harmony.PatchAll(patchType);
}
catch (Exception e)
{
Log.LogError($"Failed to patch {patchType.Name}: {e}");
}
}
}
protected void RemovePatches()
{
if (_harmony == null) return;
try
{
_harmony.UnpatchSelf();
_harmony = null;
}
catch (Exception e)
{
Log.LogError($"Failed to remove patches: {e}");
}
}
private void Update()
{
_moduleManager?.Update();
}
private void FixedUpdate()
{
_moduleManager?.FixedUpdate();
}
private void OnDestroy()
{
RemovePatches();
_moduleManager?.Destroy();
}
}