Removed 2 non-OOP classes and fixed fly cam teleport

Remvoed Hotbar and GameClient since their functions are also implemented in OOP classes
Added static methods to add/remove persistent debug info
Made some patches and other things internal
Added support for FlyCams in SetLocation
This commit is contained in:
Norbi Peti 2021-05-30 01:34:30 +02:00
parent b8fd14d934
commit 94c0c1370b
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
12 changed files with 69 additions and 178 deletions

View file

@ -396,19 +396,20 @@ namespace TechbloxModdingAPI.App
/// <summary> /// <summary>
/// Add information to the in-game debug display. /// Add information to the in-game debug display.
/// When this object is garbage collected, this debug info is automatically removed. /// When this object is garbage collected, this debug info is automatically removed.
/// The provided getter function is called each frame so make sure it returns quickly.
/// </summary> /// </summary>
/// <param name="id">Debug info identifier.</param> /// <param name="id">Debug info identifier.</param>
/// <param name="contentGetter">Content getter.</param> /// <param name="contentGetter">A function that returns the current information.</param>
public void AddDebugInfo(string id, Func<string> contentGetter) public void AddDebugInfo(string id, Func<string> contentGetter)
{ {
if (!VerifyMode()) return; if (!VerifyMode()) return;
if (menuMode) if (menuMode)
{ {
throw new GameNotFoundException("Game object references a menu item but AddDebugInfo only works on the currently-loaded game"); throw new GameNotFoundException("Game object references a menu item but AddDebugInfo only works on the currently-loaded game");
} }
debugOverlayEngine.SetInfo(id, contentGetter); debugOverlayEngine.SetInfo("game_" + id, contentGetter);
debugIds.Add(id); debugIds.Add(id);
} }
/// <summary> /// <summary>
@ -418,14 +419,36 @@ namespace TechbloxModdingAPI.App
/// <param name="id">Debug info identifier.</param> /// <param name="id">Debug info identifier.</param>
public bool RemoveDebugInfo(string id) public bool RemoveDebugInfo(string id)
{ {
if (!VerifyMode()) return false; if (!VerifyMode()) return false;
if (menuMode) if (menuMode)
{ {
throw new GameNotFoundException("Game object references a menu item but RemoveDebugInfo only works on the currently-loaded game"); throw new GameNotFoundException("Game object references a menu item but RemoveDebugInfo only works on the currently-loaded game");
} }
if (!debugIds.Contains(id)) return false; if (!debugIds.Contains(id)) return false;
debugOverlayEngine.RemoveInfo(id); debugOverlayEngine.RemoveInfo("game_" + id);
return debugIds.Remove(id); return debugIds.Remove(id);
}
/// <summary>
/// Add information to the in-game debug display.
/// This debug info will be present for all games until it is manually removed.
/// The provided getter function is called each frame so make sure it returns quickly.
/// </summary>
/// <param name="id">Debug info identifier.</param>
/// <param name="contentGetter">A function that returns the current information.</param>
public static void AddPersistentDebugInfo(string id, Func<string> contentGetter)
{
debugOverlayEngine.SetInfo("persistent_" + id, contentGetter);
}
/// <summary>
/// Remove persistent information from the in-game debug display.
/// </summary>
/// <returns><c>true</c>, if debug info was removed, <c>false</c> otherwise.</returns>
/// <param name="id">Debug info identifier.</param>
public static bool RemovePersistentDebugInfo(string id)
{
return debugOverlayEngine.RemoveInfo("persistent_" + id);
} }
/// <summary> /// <summary>

View file

@ -113,7 +113,7 @@ namespace TechbloxModdingAPI.Blocks.Engines
public bool isRemovable => false; public bool isRemovable => false;
[HarmonyPatch] [HarmonyPatch]
public class FactoryObtainerPatch class FactoryObtainerPatch
{ {
static void Postfix(BlockEntityFactory blockEntityFactory, IEntityFactory entityFactory) static void Postfix(BlockEntityFactory blockEntityFactory, IEntityFactory entityFactory)
{ {

View file

@ -44,7 +44,7 @@ namespace TechbloxModdingAPI.Blocks.Engines
public bool isRemovable => false; public bool isRemovable => false;
[HarmonyPatch] [HarmonyPatch]
public class FactoryObtainerPatch class FactoryObtainerPatch
{ {
static void Postfix(IEntityFunctions entityFunctions, static void Postfix(IEntityFunctions entityFunctions,
MachineGraphConnectionEntityFactory machineGraphConnectionEntityFactory) MachineGraphConnectionEntityFactory machineGraphConnectionEntityFactory)

View file

@ -41,7 +41,7 @@ namespace TechbloxModdingAPI.Blocks.Engines
} }
[HarmonyPatch] [HarmonyPatch]
public class PhysicsEnginePatch class PhysicsEnginePatch
{ {
static void Postfix(IReactOnAddAndRemove<UECSPhysicsEntityCreationStruct> __instance) static void Postfix(IReactOnAddAndRemove<UECSPhysicsEntityCreationStruct> __instance)
{ {

View file

@ -1,46 +0,0 @@
using System;
using RobocraftX.Common.Input;
using RobocraftX.Multiplayer.Input;
using HarmonyLib;
using TechbloxModdingAPI.Blocks;
using TechbloxModdingAPI.Utility;
namespace TechbloxModdingAPI.Inventory
{
public static class Hotbar
{
private static readonly HotbarEngine hotbarEngine = new HotbarEngine();
/// <summary>
/// Switch the block in the player's hand
/// </summary>
/// <param name="block">The block to switch to.</param>
/// <param name="playerID">The player. Omit this to use the local player.</param>
public static void EquipBlock(BlockIDs block, uint playerID = uint.MaxValue)
{
if (playerID == uint.MaxValue)
{
playerID = hotbarEngine.GetLocalPlayerID();
}
hotbarEngine.SelectBlock((int) block, playerID);
// cubeSelectedByPick = true will crash the game
// (this would be equivalent to mouse middle click pick block action)
// reason: the game expects a Dictionary entry for the tweaked stats
}
/// <summary>
/// Gets the block in the player's hand
/// </summary>
/// <returns>The equipped block.</returns>
public static BlockIDs GetEquippedBlock()
{
return HotbarSlotSelectionHandlerEnginePatch.EquippedPartID;
}
public static void Init()
{
GameEngineManager.AddGameEngine(hotbarEngine);
}
}
}

View file

@ -1,55 +0,0 @@
using System;
using RobocraftX.Character;
using RobocraftX.GUI.Hotbar;
using RobocraftX.Players;
using RobocraftX.Common;
using RobocraftX.Common.Input;
using RobocraftX.Common.Players;
using Svelto.ECS;
using TechbloxModdingAPI.Blocks;
using TechbloxModdingAPI.Utility;
using RobocraftX.Blocks;
using Techblox.FlyCam;
using TechbloxModdingAPI.Engines;
namespace TechbloxModdingAPI.Inventory
{
public class HotbarEngine : IApiEngine
{
public string Name { get; } = "TechbloxModdingAPIHotbarGameEngine";
public EntitiesDB entitiesDB { set; private get; }
public bool isRemovable => false;
public bool IsInGame = false;
public void Dispose()
{
IsInGame = false;
}
public void Ready()
{
IsInGame = true;
}
public bool SelectBlock(int block, uint playerID, bool cubeSelectedByPick = false)
{
if (!entitiesDB.TryQueryEntitiesAndIndex<EquippedPartStruct>(playerID, Techblox.FlyCam.FlyCam.Group,
out var index, out var inputs))
return false;
inputs[index].CubeSelectedByPick = cubeSelectedByPick;
inputs[index].SelectedDBPartID = block;
// TODO: expose the rest of the input functionality
return true;
}
public uint GetLocalPlayerID()
{
return LocalPlayerIDUtility.GetLocalPlayerID(entitiesDB);
}
}
}

View file

@ -9,7 +9,7 @@ using TechbloxModdingAPI.Blocks;
namespace TechbloxModdingAPI.Inventory namespace TechbloxModdingAPI.Inventory
{ {
[HarmonyPatch] [HarmonyPatch]
public class HotbarSlotSelectionHandlerEnginePatch class HotbarSlotSelectionHandlerEnginePatch
{ {
private static int selectedBlockInt = 0; private static int selectedBlockInt = 0;

View file

@ -65,8 +65,6 @@ namespace TechbloxModdingAPI
Utility.GameState.Init(); Utility.GameState.Init();
// init block implementors // init block implementors
Logging.MetaDebugLog($"Initializing Blocks"); Logging.MetaDebugLog($"Initializing Blocks");
// init inventory
Inventory.Hotbar.Init();
// init input // init input
Input.FakeInput.Init(); Input.FakeInput.Init();
// init object-oriented classes // init object-oriented classes
@ -75,7 +73,6 @@ namespace TechbloxModdingAPI
BlockGroup.Init(); BlockGroup.Init();
Wire.Init(); Wire.Init();
Logging.MetaDebugLog($"Initializing Client"); Logging.MetaDebugLog($"Initializing Client");
GameClient.Init();
Client.Init(); Client.Init();
Game.Init(); Game.Init();
// init UI // init UI

View file

@ -58,7 +58,7 @@ namespace TechbloxModdingAPI.Persistence
return _serializers.Count; return _serializers.Count;
} }
public static void RegisterSerializers(EnginesRoot enginesRoot) internal static void RegisterSerializers(EnginesRoot enginesRoot)
{ {
_lastEnginesRoot = enginesRoot; _lastEnginesRoot = enginesRoot;
IEntitySerialization ies = enginesRoot.GenerateEntitySerializer(); IEntitySerialization ies = enginesRoot.GenerateEntitySerializer();

View file

@ -102,23 +102,17 @@ namespace TechbloxModdingAPI.Players
public bool SetLocation(uint playerId, float3 location, bool exitSeat = true) public bool SetLocation(uint playerId, float3 location, bool exitSeat = true)
{ {
if (entitiesDB == null) return false; if (entitiesDB == null) return false;
var characterGroups = CharacterExclusiveGroups.AllCharacters; var rbesOpt = GetCharacterStruct<RigidBodyEntityStruct>(playerId, out var group);
for (int i = 0; i < characterGroups.count; i++) if (!rbesOpt)
{ return false;
EGID egid = new EGID(playerId, characterGroups[i]); if (group == CharacterExclusiveGroups.InPilotSeatGroup && exitSeat)
if (entitiesDB.Exists<RigidBodyEntityStruct>(egid)) {
{ EGID egid = new EGID(playerId, group);
ref RigidBodyEntityStruct rbes = ref entitiesDB.QueryEntity<RigidBodyEntityStruct>(egid); entitiesDB.QueryEntity<CharacterPilotSeatEntityStruct>(egid).instantExit = true;
if (characterGroups[i] == CharacterExclusiveGroups.InPilotSeatGroup && exitSeat) entitiesDB.PublishEntityChange<CharacterPilotSeatEntityStruct>(egid);
{ }
entitiesDB.QueryEntity<CharacterPilotSeatEntityStruct>(egid).instantExit = true; rbesOpt.Get().position = location;
entitiesDB.PublishEntityChange<CharacterPilotSeatEntityStruct>(egid); return true;
}
rbes.position = location;
return true;
}
}
return false;
} }
public bool GetGameOverScreen(uint playerId) public bool GetGameOverScreen(uint playerId)
@ -137,9 +131,14 @@ namespace TechbloxModdingAPI.Players
// reusable methods // reusable methods
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public OptionalRef<T> GetCharacterStruct<T>(uint playerId) where T : unmanaged, IEntityComponent public OptionalRef<T> GetCharacterStruct<T>(uint playerId) where T : unmanaged, IEntityComponent =>
GetCharacterStruct<T>(playerId, out _);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public OptionalRef<T> GetCharacterStruct<T>(uint playerId, out ExclusiveGroupStruct group) where T : unmanaged, IEntityComponent
{ {
group = default;
if (GameState.IsBuildMode()) if (GameState.IsBuildMode())
return entitiesDB.QueryEntityOptional<T>(new EGID(playerId, Techblox.FlyCam.FlyCam.Group)); return entitiesDB.QueryEntityOptional<T>(new EGID(playerId, Techblox.FlyCam.FlyCam.Group));
@ -149,7 +148,10 @@ namespace TechbloxModdingAPI.Players
EGID egid = new EGID(playerId, characterGroups[i]); EGID egid = new EGID(playerId, characterGroups[i]);
var opt = entitiesDB.QueryEntityOptional<T>(egid); var opt = entitiesDB.QueryEntityOptional<T>(egid);
if (opt.Exists) if (opt.Exists)
{
group = characterGroups[i];
return opt; return opt;
}
} }
return new OptionalRef<T>(); return new OptionalRef<T>();

View file

@ -25,7 +25,7 @@ namespace TechbloxModdingAPI.Tests
/// Modding API implemented as a standalone IPA Plugin. /// Modding API implemented as a standalone IPA Plugin.
/// Ideally, TechbloxModdingAPI should be loaded by another mod; not itself /// Ideally, TechbloxModdingAPI should be loaded by another mod; not itself
/// </summary> /// </summary>
public class TechbloxModdingAPIPluginTest : IllusionPlugin.IEnhancedPlugin class TechbloxModdingAPIPluginTest : IllusionPlugin.IEnhancedPlugin
{ {
private static Harmony harmony { get; set; } private static Harmony harmony { get; set; }
@ -194,7 +194,7 @@ namespace TechbloxModdingAPI.Tests
Game.CurrentGame().ToggleTimeMode(); Game.CurrentGame().ToggleTimeMode();
}).Build(); }).Build();
GameClient.SetDebugInfo("InstalledMods", InstalledMods); Game.AddPersistentDebugInfo("InstalledMods", InstalledMods);
Block.Placed += (sender, args) => Block.Placed += (sender, args) =>
Logging.MetaDebugLog("Placed block " + args.Block); Logging.MetaDebugLog("Placed block " + args.Block);
Block.Removed += (sender, args) => Block.Removed += (sender, args) =>

View file

@ -1,30 +0,0 @@
using System;
using TechbloxModdingAPI.Blocks;
namespace TechbloxModdingAPI.Utility
{
public static class GameClient
{
private static DebugInterfaceEngine _engine = new DebugInterfaceEngine();
/// <summary>
/// Saves the extra information to be displayed on the debug view.
/// The provided getter function is called each time the view updates so make sure it returns quickly.
/// </summary>
/// <param name="id">A global ID for the custom information</param>
/// <param name="contentGetter">A function that returns the current information</param>
public static void SetDebugInfo(string id, Func<string> contentGetter) => _engine.SetInfo(id, contentGetter);
/// <summary>
/// Removes an information provided by a plugin.
/// </summary>
/// <param name="id">The ID of the custom information</param>
/// <returns></returns>
public static bool RemoveDebugInfo(string id) => _engine.RemoveInfo(id);
public static void Init()
{
GameEngineManager.AddGameEngine(_engine);
}
}
}