From cc4850a0730821d031528fbf675867c532793d2b Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Tue, 20 Apr 2021 01:23:39 +0200 Subject: [PATCH] Fix fake input --- GamecraftModdingAPI/Input/FakeInput.cs | 58 ++++++++++++------- GamecraftModdingAPI/Input/FakeInputEngine.cs | 45 +++++++++++--- .../Tests/GamecraftModdingAPIPluginTest.cs | 7 ++- 3 files changed, 78 insertions(+), 32 deletions(-) diff --git a/GamecraftModdingAPI/Input/FakeInput.cs b/GamecraftModdingAPI/Input/FakeInput.cs index 1d5564d..031a841 100644 --- a/GamecraftModdingAPI/Input/FakeInput.cs +++ b/GamecraftModdingAPI/Input/FakeInput.cs @@ -12,27 +12,41 @@ namespace GamecraftModdingAPI.Input { private static readonly FakeInputEngine inputEngine = new FakeInputEngine(); - /// - /// Customize the player input. - /// - /// The custom input. - /// The player. Omit this to use the local player. - public static void CustomInput(LocalInputEntityStruct input, uint playerID = uint.MaxValue) + /// + /// Customize the local input. + /// + /// The custom input. + public static void CustomInput(LocalInputEntityStruct input) { - if (playerID == uint.MaxValue) - { - playerID = inputEngine.GetLocalPlayerID(); - } - inputEngine.SendCustomInput(input, playerID); + inputEngine.SendCustomInput(input); } - public static LocalInputEntityStruct GetInput(uint playerID = uint.MaxValue) + /// + /// Customize the player input. + /// + /// The custom input. + /// The player. Omit this to use the local player. + public static void CustomPlayerInput(LocalPlayerInputEntityStruct input, uint playerID = uint.MaxValue) { if (playerID == uint.MaxValue) - { - playerID = inputEngine.GetLocalPlayerID(); - } - return inputEngine.GetInput(playerID); + { + playerID = inputEngine.GetLocalPlayerID(); + } + inputEngine.SendCustomPlayerInput(input, playerID); + } + + public static LocalInputEntityStruct GetInput() + { + return inputEngine.GetInput(); + } + + public static LocalPlayerInputEntityStruct GetPlayerInput(uint playerID = uint.MaxValue) + { + if (playerID == uint.MaxValue) + { + playerID = inputEngine.GetLocalPlayerID(); + } + return inputEngine.GetPlayerInput(playerID); } /// @@ -59,7 +73,7 @@ namespace GamecraftModdingAPI.Input { playerID = inputEngine.GetLocalPlayerID(); } - ref LocalInputEntityStruct currentInput = ref inputEngine.GetInputRef(playerID); + ref LocalInputEntityStruct currentInput = ref inputEngine.GetInputRef(); //Utility.Logging.CommandLog($"Current sim frame {currentInput.frame}"); // set inputs switch(hotbar) @@ -107,10 +121,10 @@ namespace GamecraftModdingAPI.Input { playerID = inputEngine.GetLocalPlayerID(); } - ref LocalInputEntityStruct currentInput = ref inputEngine.GetInputRef(playerID); - //Utility.Logging.CommandLog($"Current sim frame {currentInput.frame}"); - // set inputs - TODO - /*if (toggleMode) currentInput.actionMask |= RobocraftX.Common.Input.ActionInput.ToggleTimeRunningMode; + ref LocalPlayerInputEntityStruct currentInput = ref inputEngine.GetPlayerInputRef(playerID); + //Utility.Logging.CommandLog($"Current sim frame {currentInput.frame}"); + // set inputs + if (toggleMode) currentInput.actionMask |= RobocraftX.Common.Input.ActionInput.ToggleTimeRunningMode; if (forward) currentInput.actionMask |= RobocraftX.Common.Input.ActionInput.Forward; if (backward) currentInput.actionMask |= RobocraftX.Common.Input.ActionInput.Backward; if (up) currentInput.actionMask |= RobocraftX.Common.Input.ActionInput.Up; @@ -134,7 +148,7 @@ namespace GamecraftModdingAPI.Input if (rotateBlockCounterclockwise) currentInput.actionMask |= RobocraftX.Common.Input.ActionInput.RotateBlockAnticlockwise; if (cutSelection) currentInput.actionMask |= RobocraftX.Common.Input.ActionInput.CutSelection; if (copySelection) currentInput.actionMask |= RobocraftX.Common.Input.ActionInput.CopySelection; - if (deleteSelection) currentInput.actionMask |= RobocraftX.Common.Input.ActionInput.DeleteSelection;*/ + if (deleteSelection) currentInput.actionMask |= RobocraftX.Common.Input.ActionInput.DeleteSelection; } public static void Init() diff --git a/GamecraftModdingAPI/Input/FakeInputEngine.cs b/GamecraftModdingAPI/Input/FakeInputEngine.cs index 11d5f3a..9b85c97 100644 --- a/GamecraftModdingAPI/Input/FakeInputEngine.cs +++ b/GamecraftModdingAPI/Input/FakeInputEngine.cs @@ -1,5 +1,6 @@ using System; +using RobocraftX.Common; using RobocraftX.Common.Input; using RobocraftX.Players; using Svelto.ECS; @@ -29,9 +30,9 @@ namespace GamecraftModdingAPI.Input IsReady = true; } - public bool SendCustomInput(LocalInputEntityStruct input, uint playerID, bool remote = false) + public bool SendCustomInput(LocalInputEntityStruct input) { - EGID egid = new EGID(playerID, remote ? InputExclusiveGroups.RemotePlayers : InputExclusiveGroups.LocalPlayers); + EGID egid = CommonExclusiveGroups.GameStateEGID; if (entitiesDB.Exists(egid)) { ref LocalInputEntityStruct ies = ref entitiesDB.QueryEntity(egid); @@ -41,22 +42,50 @@ namespace GamecraftModdingAPI.Input else return false; } - public LocalInputEntityStruct GetInput(uint playerID, bool remote = false) + public bool SendCustomPlayerInput(LocalPlayerInputEntityStruct input, uint playerID, bool remote = false) { EGID egid = new EGID(playerID, remote ? InputExclusiveGroups.RemotePlayers : InputExclusiveGroups.LocalPlayers); - if (entitiesDB.Exists(egid)) + if (entitiesDB.Exists(egid)) { - return entitiesDB.QueryEntity(egid); + ref LocalPlayerInputEntityStruct ies = ref entitiesDB.QueryEntity(egid); + ies = input; + return true; } - else return default(LocalInputEntityStruct); + else return false; } - public ref LocalInputEntityStruct GetInputRef(uint playerID, bool remote = false) + public LocalInputEntityStruct GetInput() { - EGID egid = new EGID(playerID, remote ? InputExclusiveGroups.RemotePlayers : InputExclusiveGroups.LocalPlayers); + EGID egid = CommonExclusiveGroups.GameStateEGID; + if (entitiesDB.Exists(egid)) + { + return entitiesDB.QueryEntity(egid); + } + else return default(LocalInputEntityStruct); + } + + public LocalPlayerInputEntityStruct GetPlayerInput(uint playerID, bool remote = false) + { + EGID egid = new EGID(playerID, remote ? InputExclusiveGroups.RemotePlayers : InputExclusiveGroups.LocalPlayers); + if (entitiesDB.Exists(egid)) + { + return entitiesDB.QueryEntity(egid); + } + else return default; + } + + public ref LocalInputEntityStruct GetInputRef() + { + EGID egid = CommonExclusiveGroups.GameStateEGID; return ref entitiesDB.QueryEntity(egid); } + public ref LocalPlayerInputEntityStruct GetPlayerInputRef(uint playerID, bool remote = false) + { + EGID egid = new EGID(playerID, remote ? InputExclusiveGroups.RemotePlayers : InputExclusiveGroups.LocalPlayers); + return ref entitiesDB.QueryEntity(egid); + } + public uint GetLocalPlayerID() { return LocalPlayerIDUtility.GetLocalPlayerID(entitiesDB); diff --git a/GamecraftModdingAPI/Tests/GamecraftModdingAPIPluginTest.cs b/GamecraftModdingAPI/Tests/GamecraftModdingAPIPluginTest.cs index 973cd4a..126fd7d 100644 --- a/GamecraftModdingAPI/Tests/GamecraftModdingAPIPluginTest.cs +++ b/GamecraftModdingAPI/Tests/GamecraftModdingAPIPluginTest.cs @@ -472,8 +472,11 @@ namespace GamecraftModdingAPI.Tests public override void OnUpdate() { - if(UnityEngine.Input.GetKeyDown(KeyCode.Backslash)) - FakeInput.CustomInput(new LocalInputEntityStruct{commandLineToggleInput = true}); + if (UnityEngine.Input.GetKeyDown(KeyCode.End)) + { + Console.WriteLine("Pressed button to toggle console"); + FakeInput.CustomInput(new LocalInputEntityStruct {commandLineToggleInput = true}); + } } [HarmonyPatch]