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;
|
2019-12-16 00:35:59 +00:00
|
|
|
|
|
|
|
|
|
using Harmony;
|
|
|
|
|
|
2019-12-14 04:42:55 +00:00
|
|
|
|
using GamecraftModdingAPI.Utility;
|
|
|
|
|
using GamecraftModdingAPI.Events;
|
2019-12-25 19:25:53 +00:00
|
|
|
|
using GamecraftModdingAPI.Tasks;
|
2019-12-14 04:42:55 +00:00
|
|
|
|
|
|
|
|
|
namespace GamecraftModdingAPI
|
|
|
|
|
{
|
2019-12-16 00:35:59 +00:00
|
|
|
|
/// <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
|
|
|
|
{
|
|
|
|
|
private static HarmonyInstance harmony;
|
2019-12-16 00:35:59 +00:00
|
|
|
|
|
2019-12-17 01:55:52 +00:00
|
|
|
|
public static bool IsInitialized {
|
|
|
|
|
get { return harmony != null; }
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-25 21:16:17 +00:00
|
|
|
|
private static int referenceCount = 0;
|
|
|
|
|
|
2019-12-16 00:35:59 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes the GamecraftModdingAPI.
|
2019-12-17 01:55:52 +00:00
|
|
|
|
/// Call this as soon as possible after Gamecraft starts up.
|
2019-12-16 00:35:59 +00:00
|
|
|
|
/// 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-25 21:16:17 +00:00
|
|
|
|
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();
|
|
|
|
|
harmony = HarmonyInstance.Create(currentAssembly.GetName().Name);
|
|
|
|
|
harmony.PatchAll(currentAssembly);
|
2019-12-25 21:16:17 +00:00
|
|
|
|
// init utility
|
|
|
|
|
Logging.MetaDebugLog($"Initializing Utility");
|
|
|
|
|
Utility.GameState.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));
|
|
|
|
|
// init block implementors
|
|
|
|
|
Logging.MetaDebugLog($"Initializing Blocks");
|
|
|
|
|
Blocks.Movement.Init();
|
|
|
|
|
Blocks.Rotation.Init();
|
2019-12-25 19:25:53 +00:00
|
|
|
|
Blocks.Signals.Init();
|
2020-01-03 00:14:26 +00:00
|
|
|
|
Blocks.Placement.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
|
|
|
|
}
|
|
|
|
|
|
2019-12-16 00:35:59 +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.
|
2019-12-16 00:35:59 +00:00
|
|
|
|
/// 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()
|
|
|
|
|
{
|
2019-12-25 21:16:17 +00:00
|
|
|
|
if (referenceCount > 0) { referenceCount--; }
|
|
|
|
|
if (referenceCount == 0)
|
2019-12-25 19:25:53 +00:00
|
|
|
|
{
|
2019-12-25 21:16:17 +00:00
|
|
|
|
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-25 19:25:53 +00:00
|
|
|
|
}
|
2019-12-14 04:42:55 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|