From 8145c6c23f6bf4863a41628c13b879a37ab4f3a9 Mon Sep 17 00:00:00 2001 From: NGnius Date: Tue, 18 Feb 2020 13:59:02 -0500 Subject: [PATCH] Fix GameSwitchedTo event with some statefulness --- ...dPatch.cs => GameActivatedComposePatch.cs} | 24 ++++++-- .../Events/GameReloadedPatch.cs | 4 +- .../Events/GameSwitchedToPatch.cs | 14 +++-- .../Tests/GamecraftModdingAPIPluginTest.cs | 58 +++++++++++-------- GamecraftModdingAPI/Utility/Dependency.cs | 10 ++++ 5 files changed, 74 insertions(+), 36 deletions(-) rename GamecraftModdingAPI/Events/{GameActivatedPatch.cs => GameActivatedComposePatch.cs} (60%) create mode 100644 GamecraftModdingAPI/Utility/Dependency.cs diff --git a/GamecraftModdingAPI/Events/GameActivatedPatch.cs b/GamecraftModdingAPI/Events/GameActivatedComposePatch.cs similarity index 60% rename from GamecraftModdingAPI/Events/GameActivatedPatch.cs rename to GamecraftModdingAPI/Events/GameActivatedComposePatch.cs index 13f4c59..19511f7 100644 --- a/GamecraftModdingAPI/Events/GameActivatedPatch.cs +++ b/GamecraftModdingAPI/Events/GameActivatedComposePatch.cs @@ -17,18 +17,34 @@ namespace GamecraftModdingAPI.Events /// /// Patch of RobocraftX.FullGameCompositionRoot.ActivateGame() /// - [HarmonyPatch] - class GameActivatedPatch + [HarmonyPatch] + class GameActivatedComposePatch { + public static bool IsGameSwitching = false; + + public static bool IsGameReloading = false; + public static void Postfix(ref EnginesRoot enginesRoot) { // register custom game engines - GameEngineManager.RegisterEngines(enginesRoot); + GameEngineManager.RegisterEngines(enginesRoot); // A new EnginesRoot is always created when ActivateGame is called // so all event emitters and handlers must be re-registered. - EventManager.RegisterEngines(enginesRoot); + EventManager.RegisterEngines(enginesRoot); Logging.Log("Dispatching Game Activated event"); EventManager.GetEventEmitter("GamecraftModdingAPIGameActivatedEventEmitter").Emit(); + if (IsGameSwitching) + { + IsGameSwitching = false; + Logging.Log("Dispatching Game Switched To event"); + EventManager.GetEventEmitter("GamecraftModdingAPIGameSwitchedToEventEmitter").Emit(); + } + if (IsGameReloading) + { + IsGameReloading = false; + Logging.Log("Dispatching Game Reloaded event"); + EventManager.GetEventEmitter("GamecraftModdingAPIGameReloadedEventEmitter").Emit(); + } } public static MethodBase TargetMethod() diff --git a/GamecraftModdingAPI/Events/GameReloadedPatch.cs b/GamecraftModdingAPI/Events/GameReloadedPatch.cs index 7c8cfe3..becd4dc 100644 --- a/GamecraftModdingAPI/Events/GameReloadedPatch.cs +++ b/GamecraftModdingAPI/Events/GameReloadedPatch.cs @@ -19,9 +19,7 @@ namespace GamecraftModdingAPI.Events { public static void Postfix() { - // Event emitters and handlers should already be registered by GameActivatedPatch - Logging.Log("Dispatching Game Reloaded event"); - EventManager.GetEventEmitter("GamecraftModdingAPIGameReloadedEventEmitter").Emit(); + GameActivatedComposePatch.IsGameReloading = true; } } } diff --git a/GamecraftModdingAPI/Events/GameSwitchedToPatch.cs b/GamecraftModdingAPI/Events/GameSwitchedToPatch.cs index 69b6aff..eae4652 100644 --- a/GamecraftModdingAPI/Events/GameSwitchedToPatch.cs +++ b/GamecraftModdingAPI/Events/GameSwitchedToPatch.cs @@ -3,25 +3,27 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using System.Reflection; using Harmony; using RobocraftX; +using RobocraftX.CR.MainGame; +using Svelto.ECS; using GamecraftModdingAPI.Utility; namespace GamecraftModdingAPI.Events { /// - /// Patch of RobocraftX.FullGameCompositionRoot.SwitchToGame() + /// Patch of RobocraftX.FullGameCompositionRoot.ActivateGame() + /// (scheduled for execution during RobocraftX.FullGameCompositionRoot.SwitchToGame()) /// - [HarmonyPatch(typeof(FullGameCompositionRoot), "SwitchToGame")] + [HarmonyPatch(typeof(FullGameCompositionRoot), "SwitchToGame")] class GameSwitchedToPatch { - public static void Postfix() + public static void Prefix() { - // Event emitters and handlers should already be registered by GameActivated event - Logging.Log("Not dispatching Game Switched To event"); - //EventManager.GetEventEmitter("GamecraftModdingAPIGameSwitchedToEventEmitter").Emit(); + GameActivatedComposePatch.IsGameSwitching = true; } } } diff --git a/GamecraftModdingAPI/Tests/GamecraftModdingAPIPluginTest.cs b/GamecraftModdingAPI/Tests/GamecraftModdingAPIPluginTest.cs index 5a62e12..09a1215 100644 --- a/GamecraftModdingAPI/Tests/GamecraftModdingAPIPluginTest.cs +++ b/GamecraftModdingAPI/Tests/GamecraftModdingAPIPluginTest.cs @@ -26,7 +26,7 @@ namespace GamecraftModdingAPI.Tests { private static HarmonyInstance harmony { get; set; } - public string[] Filter { get; } = new string[] { "Gamecraft" }; + public string[] Filter { get; } = new string[] { "Gamecraft", "GamecraftPreview" }; public string Name { get; } = Assembly.GetExecutingAssembly().GetName().Name; @@ -49,8 +49,9 @@ namespace GamecraftModdingAPI.Tests //MinimumSpecsCheckPatch.ForcePassMinimumSpecCheck = true; // disable some Gamecraft analytics //AnalyticsDisablerPatch.DisableAnalytics = true; - // disable background music - AudioTools.SetVolume(0.0f, "Music"); + // disable background music + Logging.MetaDebugLog("Audio Mixers: "+string.Join(",", AudioTools.GetMixers())); + //AudioTools.SetVolume(0.0f, "Music"); // The game now sets this from settings again after this is called /*if (!FMODUnity.RuntimeManager.HasBankLoaded("Modded")) { @@ -75,27 +76,38 @@ namespace GamecraftModdingAPI.Tests EventType.GameSwitchedTo, "gameswitch API debug")); // debug/test commands - CommandManager.AddCommand(new SimpleCustomCommandEngine(() => { UnityEngine.Application.Quit(); }, + if (Dependency.Hell("ExtraCommands")) + { + CommandManager.AddCommand(new SimpleCustomCommandEngine(() => { UnityEngine.Application.Quit(); }, "Exit", "Close Gamecraft without any prompts")); - CommandManager.AddCommand(new SimpleCustomCommandEngine((float d) => { UnityEngine.Camera.main.fieldOfView = d; }, - "SetFOV", "Set the player camera's field of view")); - CommandManager.AddCommand(new SimpleCustomCommandEngine( - (x,y,z) => { - bool success = GamecraftModdingAPI.Blocks.Movement.MoveConnectedBlocks( - GamecraftModdingAPI.Blocks.BlockIdentifiers.LatestBlockID, - new Unity.Mathematics.float3(x, y, z)); - if (!success) - { - GamecraftModdingAPI.Utility.Logging.CommandLogError("Blocks can only be moved in Build mode!"); - } - }, "MoveLastBlock", "Move the most-recently-placed block, and any connected blocks by the given offset")); - CommandManager.AddCommand(new SimpleCustomCommandEngine( - (x,y,z) => { Blocks.Placement.PlaceBlock(Blocks.BlockIDs.AluminiumCube, new Unity.Mathematics.float3(x, y, z)); }, - "PlaceAluminium", "Place a block of aluminium at the given coordinates")); - Analytics.DeltaDNAHelper.PlayerLifetimeParameters plp = new Analytics.DeltaDNAHelper.PlayerLifetimeParameters(); - CommandManager.AddCommand(new SimpleCustomCommandEngine( - (s) => { Analytics.DeltaDNAHelper.SendActionCompletedEvent(in plp, s.Replace(", ", " ")); }, - "SendAnalyticsAction", "Send an analytics action")); + CommandManager.AddCommand(new SimpleCustomCommandEngine((float d) => { UnityEngine.Camera.main.fieldOfView = d; }, + "SetFOV", "Set the player camera's field of view")); + CommandManager.AddCommand(new SimpleCustomCommandEngine( + (x, y, z) => { + bool success = GamecraftModdingAPI.Blocks.Movement.MoveConnectedBlocks( + GamecraftModdingAPI.Blocks.BlockIdentifiers.LatestBlockID, + new Unity.Mathematics.float3(x, y, z)); + if (!success) + { + GamecraftModdingAPI.Utility.Logging.CommandLogError("Blocks can only be moved in Build mode!"); + } + }, "MoveLastBlock", "Move the most-recently-placed block, and any connected blocks by the given offset")); + CommandManager.AddCommand(new SimpleCustomCommandEngine( + (x, y, z) => { Blocks.Placement.PlaceBlock(Blocks.BlockIDs.AluminiumCube, new Unity.Mathematics.float3(x, y, z)); }, + "PlaceAluminium", "Place a block of aluminium at the given coordinates")); + Analytics.DeltaDNAHelper.PlayerLifetimeParameters plp = new Analytics.DeltaDNAHelper.PlayerLifetimeParameters(); + CommandManager.AddCommand(new SimpleCustomCommandEngine( + (s) => { Analytics.DeltaDNAHelper.SendActionCompletedEvent(in plp, s.Replace(", ", " ")); }, + "SendAnalyticsAction", "Send an analytics action")); + } + if (Dependency.Hell("GamecraftScripting", new Version("0.0.1.0"))) + { + Logging.LogWarning("You're in GamecraftScripting dependency hell"); + } + else + { + Logging.Log("Compatible GamecraftScripting detected"); + } } public void OnFixedUpdate() { } diff --git a/GamecraftModdingAPI/Utility/Dependency.cs b/GamecraftModdingAPI/Utility/Dependency.cs new file mode 100644 index 0000000..854dff3 --- /dev/null +++ b/GamecraftModdingAPI/Utility/Dependency.cs @@ -0,0 +1,10 @@ +using System; +namespace GamecraftModdingAPI +{ + public class Dependency + { + public Dependency() + { + } + } +}