A stable modding interface between Techblox and mods https://mod.exmods.org/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
3.2KB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Reflection;
  7. using Harmony;
  8. using GamecraftModdingAPI.Utility;
  9. using GamecraftModdingAPI.Events;
  10. namespace GamecraftModdingAPI
  11. {
  12. /// <summary>
  13. /// The main class of the GamecraftModdingAPI.
  14. /// Use this to initialize the API before calling it.
  15. /// </summary>
  16. static class Main
  17. {
  18. private static HarmonyInstance harmony;
  19. public static bool IsInitialized {
  20. get { return harmony != null; }
  21. }
  22. /// <summary>
  23. /// Initializes the GamecraftModdingAPI.
  24. /// Call this as soon as possible after Gamecraft starts up.
  25. /// Ideally, this should be called from your main Plugin class's OnApplicationStart() method.
  26. /// </summary>
  27. public static void Init()
  28. {
  29. if (IsInitialized)
  30. {
  31. Logging.LogWarning("GamecraftModdingAPI.Main.Init() called but API is already initialized!");
  32. return;
  33. }
  34. Logging.MetaDebugLog($"Patching Gamecraft");
  35. var currentAssembly = Assembly.GetExecutingAssembly();
  36. harmony = HarmonyInstance.Create(currentAssembly.GetName().Name);
  37. harmony.PatchAll(currentAssembly);
  38. // create default event emitters
  39. Logging.MetaDebugLog($"Initializing Events");
  40. EventManager.AddEventEmitter(new SimpleEventEmitterEngine(EventType.ApplicationInitialized, "GamecraftModdingAPIApplicationInitializedEventEmitter", false));
  41. EventManager.AddEventEmitter(new SimpleEventEmitterEngine(EventType.Menu, "GamecraftModdingAPIMenuActivatedEventEmitter", false));
  42. EventManager.AddEventEmitter(new SimpleEventEmitterEngine(EventType.MenuSwitchedTo, "GamecraftModdingAPIMenuSwitchedToEventEmitter", false));
  43. EventManager.AddEventEmitter(new SimpleEventEmitterEngine(EventType.Game, "GamecraftModdingAPIGameActivatedEventEmitter", false));
  44. EventManager.AddEventEmitter(new SimpleEventEmitterEngine(EventType.GameReloaded, "GamecraftModdingAPIGameReloadedEventEmitter", false));
  45. EventManager.AddEventEmitter(new SimpleEventEmitterEngine(EventType.GameSwitchedTo, "GamecraftModdingAPIGameSwitchedToEventEmitter", false));
  46. // init block implementors
  47. Logging.MetaDebugLog($"Initializing Blocks");
  48. Blocks.Movement.Init();
  49. Blocks.Rotation.Init();
  50. Logging.MetaLog($"{currentAssembly.GetName().Name} v{currentAssembly.GetName().Version} initialized");
  51. }
  52. /// <summary>
  53. /// Shuts down & cleans up the GamecraftModdingAPI.
  54. /// Call this as late as possible before Gamecraft quits.
  55. /// Ideally, this should be called from your main Plugin class's OnApplicationQuit() method.
  56. /// </summary>
  57. public static void Shutdown()
  58. {
  59. var currentAssembly = Assembly.GetExecutingAssembly();
  60. harmony.UnpatchAll(currentAssembly.GetName().Name);
  61. Logging.MetaLog($"{currentAssembly.GetName().Name} v{currentAssembly.GetName().Version} shutdown");
  62. }
  63. }
  64. }