TechbloxModdingAPI/GamecraftModdingAPI/Main.cs

103 lines
4.3 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;
2020-05-03 19:31:09 +00:00
using HarmonyLib;
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);
2019-12-17 01:55:52 +00:00
harmony.PatchAll(currentAssembly);
// init utility
Logging.MetaDebugLog($"Initializing Utility");
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);
2019-12-17 01:55:52 +00:00
// init block implementors
Logging.MetaDebugLog($"Initializing Blocks");
Blocks.Movement.Init();
Blocks.Rotation.Init();
Blocks.Signals.Init();
2020-01-03 00:14:26 +00:00
Blocks.Placement.Init();
2020-02-29 18:20:03 +00:00
Blocks.Tweakable.Init();
2020-04-12 23:31:06 +00:00
Blocks.Removal.Init();
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();
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
}
}
}