Fix fake input

This commit is contained in:
Norbi Peti 2021-04-20 01:23:39 +02:00
parent 677c8b0907
commit cc4850a073
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
3 changed files with 78 additions and 32 deletions

View file

@ -12,27 +12,41 @@ namespace GamecraftModdingAPI.Input
{ {
private static readonly FakeInputEngine inputEngine = new FakeInputEngine(); private static readonly FakeInputEngine inputEngine = new FakeInputEngine();
/// <summary> /// <summary>
/// Customize the player input. /// Customize the local input.
/// </summary> /// </summary>
/// <param name="input">The custom input.</param> /// <param name="input">The custom input.</param>
/// <param name="playerID">The player. Omit this to use the local player.</param> public static void CustomInput(LocalInputEntityStruct input)
public static void CustomInput(LocalInputEntityStruct input, uint playerID = uint.MaxValue)
{ {
if (playerID == uint.MaxValue) inputEngine.SendCustomInput(input);
{
playerID = inputEngine.GetLocalPlayerID();
}
inputEngine.SendCustomInput(input, playerID);
} }
public static LocalInputEntityStruct GetInput(uint playerID = uint.MaxValue) /// <summary>
/// Customize the player input.
/// </summary>
/// <param name="input">The custom input.</param>
/// <param name="playerID">The player. Omit this to use the local player.</param>
public static void CustomPlayerInput(LocalPlayerInputEntityStruct input, uint playerID = uint.MaxValue)
{ {
if (playerID == uint.MaxValue) if (playerID == uint.MaxValue)
{ {
playerID = inputEngine.GetLocalPlayerID(); playerID = inputEngine.GetLocalPlayerID();
} }
return inputEngine.GetInput(playerID); 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);
} }
/// <summary> /// <summary>
@ -59,7 +73,7 @@ namespace GamecraftModdingAPI.Input
{ {
playerID = inputEngine.GetLocalPlayerID(); playerID = inputEngine.GetLocalPlayerID();
} }
ref LocalInputEntityStruct currentInput = ref inputEngine.GetInputRef(playerID); ref LocalInputEntityStruct currentInput = ref inputEngine.GetInputRef();
//Utility.Logging.CommandLog($"Current sim frame {currentInput.frame}"); //Utility.Logging.CommandLog($"Current sim frame {currentInput.frame}");
// set inputs // set inputs
switch(hotbar) switch(hotbar)
@ -107,10 +121,10 @@ namespace GamecraftModdingAPI.Input
{ {
playerID = inputEngine.GetLocalPlayerID(); playerID = inputEngine.GetLocalPlayerID();
} }
ref LocalInputEntityStruct currentInput = ref inputEngine.GetInputRef(playerID); ref LocalPlayerInputEntityStruct currentInput = ref inputEngine.GetPlayerInputRef(playerID);
//Utility.Logging.CommandLog($"Current sim frame {currentInput.frame}"); //Utility.Logging.CommandLog($"Current sim frame {currentInput.frame}");
// set inputs - TODO // set inputs
/*if (toggleMode) currentInput.actionMask |= RobocraftX.Common.Input.ActionInput.ToggleTimeRunningMode; if (toggleMode) currentInput.actionMask |= RobocraftX.Common.Input.ActionInput.ToggleTimeRunningMode;
if (forward) currentInput.actionMask |= RobocraftX.Common.Input.ActionInput.Forward; if (forward) currentInput.actionMask |= RobocraftX.Common.Input.ActionInput.Forward;
if (backward) currentInput.actionMask |= RobocraftX.Common.Input.ActionInput.Backward; if (backward) currentInput.actionMask |= RobocraftX.Common.Input.ActionInput.Backward;
if (up) currentInput.actionMask |= RobocraftX.Common.Input.ActionInput.Up; 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 (rotateBlockCounterclockwise) currentInput.actionMask |= RobocraftX.Common.Input.ActionInput.RotateBlockAnticlockwise;
if (cutSelection) currentInput.actionMask |= RobocraftX.Common.Input.ActionInput.CutSelection; if (cutSelection) currentInput.actionMask |= RobocraftX.Common.Input.ActionInput.CutSelection;
if (copySelection) currentInput.actionMask |= RobocraftX.Common.Input.ActionInput.CopySelection; 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() public static void Init()

View file

@ -1,5 +1,6 @@
using System; using System;
using RobocraftX.Common;
using RobocraftX.Common.Input; using RobocraftX.Common.Input;
using RobocraftX.Players; using RobocraftX.Players;
using Svelto.ECS; using Svelto.ECS;
@ -29,9 +30,9 @@ namespace GamecraftModdingAPI.Input
IsReady = true; 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<LocalInputEntityStruct>(egid)) if (entitiesDB.Exists<LocalInputEntityStruct>(egid))
{ {
ref LocalInputEntityStruct ies = ref entitiesDB.QueryEntity<LocalInputEntityStruct>(egid); ref LocalInputEntityStruct ies = ref entitiesDB.QueryEntity<LocalInputEntityStruct>(egid);
@ -41,22 +42,50 @@ namespace GamecraftModdingAPI.Input
else return false; 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); EGID egid = new EGID(playerID, remote ? InputExclusiveGroups.RemotePlayers : InputExclusiveGroups.LocalPlayers);
if (entitiesDB.Exists<LocalInputEntityStruct>(egid)) if (entitiesDB.Exists<LocalPlayerInputEntityStruct>(egid))
{ {
return entitiesDB.QueryEntity<LocalInputEntityStruct>(egid); ref LocalPlayerInputEntityStruct ies = ref entitiesDB.QueryEntity<LocalPlayerInputEntityStruct>(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<LocalInputEntityStruct>(egid))
{
return entitiesDB.QueryEntity<LocalInputEntityStruct>(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<LocalPlayerInputEntityStruct>(egid))
{
return entitiesDB.QueryEntity<LocalPlayerInputEntityStruct>(egid);
}
else return default;
}
public ref LocalInputEntityStruct GetInputRef()
{
EGID egid = CommonExclusiveGroups.GameStateEGID;
return ref entitiesDB.QueryEntity<LocalInputEntityStruct>(egid); return ref entitiesDB.QueryEntity<LocalInputEntityStruct>(egid);
} }
public ref LocalPlayerInputEntityStruct GetPlayerInputRef(uint playerID, bool remote = false)
{
EGID egid = new EGID(playerID, remote ? InputExclusiveGroups.RemotePlayers : InputExclusiveGroups.LocalPlayers);
return ref entitiesDB.QueryEntity<LocalPlayerInputEntityStruct>(egid);
}
public uint GetLocalPlayerID() public uint GetLocalPlayerID()
{ {
return LocalPlayerIDUtility.GetLocalPlayerID(entitiesDB); return LocalPlayerIDUtility.GetLocalPlayerID(entitiesDB);

View file

@ -472,8 +472,11 @@ namespace GamecraftModdingAPI.Tests
public override void OnUpdate() public override void OnUpdate()
{ {
if(UnityEngine.Input.GetKeyDown(KeyCode.Backslash)) if (UnityEngine.Input.GetKeyDown(KeyCode.End))
FakeInput.CustomInput(new LocalInputEntityStruct{commandLineToggleInput = true}); {
Console.WriteLine("Pressed button to toggle console");
FakeInput.CustomInput(new LocalInputEntityStruct {commandLineToggleInput = true});
}
} }
[HarmonyPatch] [HarmonyPatch]