TechbloxModdingAPI/GamecraftModdingAPI/Main.cs

125 lines
5.4 KiB
C#
Raw Normal View History

2019-12-14 04:42:55 +00:00
using System;
using System.Reflection;
using GamecraftModdingAPI.Blocks;
2020-05-03 19:31:09 +00:00
using HarmonyLib;
using RobocraftX;
using RobocraftX.Services;
using Svelto.Context;
2019-12-14 04:42:55 +00:00
using GamecraftModdingAPI.Utility;
using GamecraftModdingAPI.Events;
using GamecraftModdingAPI.Tasks;
2019-12-14 04:42:55 +00:00
namespace GamecraftModdingAPI
{
/// <summary>
/// The main class of the GamecraftModdingAPI.
/// Use this to initialize the API before calling it.
/// </summary>
2019-12-19 20:42:50 +00:00
public static class Main
2019-12-14 04:42:55 +00:00
{
2020-05-03 19:31:09 +00:00
private static Harmony harmony;
2019-12-17 01:55:52 +00:00
public static bool IsInitialized {
get { return harmony != null; }
}
private static int referenceCount = 0;
/// <summary>
/// Initializes the GamecraftModdingAPI.
2019-12-17 01:55:52 +00:00
/// Call this as soon as possible after Gamecraft starts up.
/// Ideally, this should be called from your main Plugin class's OnApplicationStart() method.
/// </summary>
2019-12-14 04:42:55 +00:00
public static void Init()
{
referenceCount++;
if (referenceCount > 1) { return; }
2019-12-17 01:55:52 +00:00
if (IsInitialized)
2019-12-14 04:42:55 +00:00
{
2019-12-17 01:55:52 +00:00
Logging.LogWarning("GamecraftModdingAPI.Main.Init() called but API is already initialized!");
return;
2019-12-14 04:42:55 +00:00
}
2019-12-17 01:55:52 +00:00
Logging.MetaDebugLog($"Patching Gamecraft");
var currentAssembly = Assembly.GetExecutingAssembly();
2020-05-03 19:31:09 +00:00
harmony = new Harmony(currentAssembly.GetName().Name);
try
{
harmony.PatchAll(currentAssembly);
}
catch (Exception e)
{ //Can't use ErrorBuilder or Logging.LogException (which eventually uses ErrorBuilder) yet
Logging.Log(e.ToString());
Logging.LogWarning("Failed to patch Gamecraft. Attempting to patch to display error...");
harmony.Patch(AccessTools.Method(typeof(FullGameCompositionRoot), "OnContextInitialized")
.MakeGenericMethod(typeof(UnityContext<FullGameCompositionRoot>)),
new HarmonyMethod(((Action) OnPatchError).Method)); //Can't use lambdas here :(
return;
}
// init utility
Logging.MetaDebugLog($"Initializing Utility");
#pragma warning disable 0612,0618
Utility.GameState.Init();
Utility.VersionTracking.Init();
2019-12-14 18:52:24 +00:00
// create default event emitters
2019-12-17 01:55:52 +00:00
Logging.MetaDebugLog($"Initializing Events");
2019-12-15 07:20:20 +00:00
EventManager.AddEventEmitter(new SimpleEventEmitterEngine(EventType.ApplicationInitialized, "GamecraftModdingAPIApplicationInitializedEventEmitter", false));
EventManager.AddEventEmitter(new SimpleEventEmitterEngine(EventType.Menu, "GamecraftModdingAPIMenuActivatedEventEmitter", false));
2019-12-17 01:55:52 +00:00
EventManager.AddEventEmitter(new SimpleEventEmitterEngine(EventType.MenuSwitchedTo, "GamecraftModdingAPIMenuSwitchedToEventEmitter", false));
2019-12-15 07:20:20 +00:00
EventManager.AddEventEmitter(new SimpleEventEmitterEngine(EventType.Game, "GamecraftModdingAPIGameActivatedEventEmitter", false));
2019-12-17 01:55:52 +00:00
EventManager.AddEventEmitter(new SimpleEventEmitterEngine(EventType.GameReloaded, "GamecraftModdingAPIGameReloadedEventEmitter", false));
EventManager.AddEventEmitter(new SimpleEventEmitterEngine(EventType.GameSwitchedTo, "GamecraftModdingAPIGameSwitchedToEventEmitter", false));
2020-04-07 17:05:00 +00:00
EventManager.AddEventEmitter(GameHostTransitionDeterministicGroupEnginePatch.buildEngine);
EventManager.AddEventEmitter(GameHostTransitionDeterministicGroupEnginePatch.simEngine);
#pragma warning restore 0612,0618
2019-12-17 01:55:52 +00:00
// init block implementors
Logging.MetaDebugLog($"Initializing Blocks");
2020-04-02 13:50:30 +00:00
// init inventory
Inventory.Hotbar.Init();
2020-04-04 18:48:12 +00:00
// init input
Input.FakeInput.Init();
2020-05-12 00:30:16 +00:00
// init object-oriented classes
Player.Init();
Block.Init();
BlockGroup.Init();
Wire.Init();
GameClient.Init();
AsyncUtils.Init();
GamecraftModdingAPI.App.Client.Init();
GamecraftModdingAPI.App.Game.Init();
2019-12-17 01:55:52 +00:00
Logging.MetaLog($"{currentAssembly.GetName().Name} v{currentAssembly.GetName().Version} initialized");
2019-12-14 04:42:55 +00:00
}
/// <summary>
/// Shuts down & cleans up the GamecraftModdingAPI.
2019-12-17 01:55:52 +00:00
/// Call this as late as possible before Gamecraft quits.
/// Ideally, this should be called from your main Plugin class's OnApplicationQuit() method.
/// </summary>
2019-12-14 04:42:55 +00:00
public static void Shutdown()
{
if (referenceCount > 0) { referenceCount--; }
if (referenceCount == 0)
{
if (!IsInitialized)
{
Logging.LogWarning("GamecraftModdingAPI.Main.Shutdown() called but API is not initialized!");
return;
}
Scheduler.Dispose();
var currentAssembly = Assembly.GetExecutingAssembly();
harmony.UnpatchAll(currentAssembly.GetName().Name);
harmony = null;
Logging.MetaLog($"{currentAssembly.GetName().Name} v{currentAssembly.GetName().Version} shutdown");
}
2019-12-14 04:42:55 +00:00
}
private static void OnPatchError()
{
ErrorBuilder.DisplayMustQuitError("Failed to patch Gamecraft!\n" +
"Make sure you're using the latest version of GamecraftModdingAPI or disable mods if the API isn't released yet.");
}
2019-12-14 04:42:55 +00:00
}
}