|
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Reflection;
-
- using Harmony;
-
- 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>
- static class Main
- {
- private static HarmonyInstance harmony;
-
- public static bool IsInitialized {
- get { return harmony != null; }
- }
-
- /// <summary>
- /// Initializes the GamecraftModdingAPI.
- /// Call this as soon as possible after Gamecraft starts up.
- /// Ideally, this should be called from your main Plugin class's OnApplicationStart() method.
- /// </summary>
- public static void Init()
- {
- if (IsInitialized)
- {
- Logging.LogWarning("GamecraftModdingAPI.Main.Init() called but API is already initialized!");
- return;
- }
- Logging.MetaDebugLog($"Patching Gamecraft");
- var currentAssembly = Assembly.GetExecutingAssembly();
- harmony = HarmonyInstance.Create(currentAssembly.GetName().Name);
- harmony.PatchAll(currentAssembly);
- // create default event emitters
- Logging.MetaDebugLog($"Initializing Events");
- EventManager.AddEventEmitter(new SimpleEventEmitterEngine(EventType.ApplicationInitialized, "GamecraftModdingAPIApplicationInitializedEventEmitter", false));
- EventManager.AddEventEmitter(new SimpleEventEmitterEngine(EventType.Menu, "GamecraftModdingAPIMenuActivatedEventEmitter", false));
- EventManager.AddEventEmitter(new SimpleEventEmitterEngine(EventType.MenuSwitchedTo, "GamecraftModdingAPIMenuSwitchedToEventEmitter", false));
- EventManager.AddEventEmitter(new SimpleEventEmitterEngine(EventType.Game, "GamecraftModdingAPIGameActivatedEventEmitter", false));
- 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");
- }
-
- /// <summary>
- /// Shuts down & cleans up the GamecraftModdingAPI.
- /// Call this as late as possible before Gamecraft quits.
- /// Ideally, this should be called from your main Plugin class's OnApplicationQuit() method.
- /// </summary>
- public static void Shutdown()
- {
- var currentAssembly = Assembly.GetExecutingAssembly();
- harmony.UnpatchAll(currentAssembly.GetName().Name);
- Logging.MetaLog($"{currentAssembly.GetName().Name} v{currentAssembly.GetName().Version} shutdown");
- }
- }
- }
|