TechbloxModdingAPI/GamecraftModdingAPI/Main.cs

71 lines
3.2 KiB
C#
Raw Normal View History

2019-12-14 04:42:55 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using Harmony;
2019-12-14 04:42:55 +00:00
using GamecraftModdingAPI.Utility;
using GamecraftModdingAPI.Events;
namespace GamecraftModdingAPI
{
/// <summary>
/// The main class of the GamecraftModdingAPI.
/// Use this to initialize the API before calling it.
/// </summary>
2019-12-14 04:42:55 +00:00
static class Main
{
private static HarmonyInstance harmony;
2019-12-17 01:55:52 +00:00
public static bool IsInitialized {
get { return harmony != null; }
}
/// <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()
{
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();
harmony = HarmonyInstance.Create(currentAssembly.GetName().Name);
harmony.PatchAll(currentAssembly);
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));
// init block implementors
Logging.MetaDebugLog($"Initializing Blocks");
Blocks.Movement.Init();
Blocks.Rotation.Init();
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()
{
var currentAssembly = Assembly.GetExecutingAssembly();
harmony.UnpatchAll(currentAssembly.GetName().Name);
2019-12-17 01:55:52 +00:00
Logging.MetaLog($"{currentAssembly.GetName().Name} v{currentAssembly.GetName().Version} shutdown");
2019-12-14 04:42:55 +00:00
}
}
}