From 94c0c1370b0ca8c793b14687b3993d154eb2e949 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Sun, 30 May 2021 01:34:30 +0200 Subject: [PATCH] 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 --- TechbloxModdingAPI/App/Game.cs | 59 +++++++++++++------ .../Blocks/Engines/PlacementEngine.cs | 2 +- .../Blocks/Engines/RemovalEngine.cs | 2 +- .../Blocks/Engines/ScalingEngine.cs | 2 +- TechbloxModdingAPI/Inventory/Hotbar.cs | 46 --------------- TechbloxModdingAPI/Inventory/HotbarEngine.cs | 55 ----------------- .../HotbarSlotSelectionHandlerEnginePatch.cs | 2 +- TechbloxModdingAPI/Main.cs | 3 - .../Persistence/SerializerManager.cs | 2 +- TechbloxModdingAPI/Players/PlayerEngine.cs | 40 +++++++------ .../Tests/TechbloxModdingAPIPluginTest.cs | 4 +- TechbloxModdingAPI/Utility/GameClient.cs | 30 ---------- 12 files changed, 69 insertions(+), 178 deletions(-) delete mode 100644 TechbloxModdingAPI/Inventory/Hotbar.cs delete mode 100644 TechbloxModdingAPI/Inventory/HotbarEngine.cs delete mode 100644 TechbloxModdingAPI/Utility/GameClient.cs diff --git a/TechbloxModdingAPI/App/Game.cs b/TechbloxModdingAPI/App/Game.cs index 4630ed9..1d37945 100644 --- a/TechbloxModdingAPI/App/Game.cs +++ b/TechbloxModdingAPI/App/Game.cs @@ -396,19 +396,20 @@ namespace TechbloxModdingAPI.App /// /// 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. /// /// Debug info identifier. - /// Content getter. - public void AddDebugInfo(string id, Func contentGetter) + /// A function that returns the current information. + public void AddDebugInfo(string id, Func contentGetter) { - if (!VerifyMode()) return; - if (menuMode) - { - throw new GameNotFoundException("Game object references a menu item but AddDebugInfo only works on the currently-loaded game"); - } - debugOverlayEngine.SetInfo(id, contentGetter); - debugIds.Add(id); + if (!VerifyMode()) return; + if (menuMode) + { + throw new GameNotFoundException("Game object references a menu item but AddDebugInfo only works on the currently-loaded game"); + } + debugOverlayEngine.SetInfo("game_" + id, contentGetter); + debugIds.Add(id); } /// @@ -418,14 +419,36 @@ namespace TechbloxModdingAPI.App /// Debug info identifier. public bool RemoveDebugInfo(string id) { - if (!VerifyMode()) return false; - if (menuMode) - { - throw new GameNotFoundException("Game object references a menu item but RemoveDebugInfo only works on the currently-loaded game"); - } - if (!debugIds.Contains(id)) return false; - debugOverlayEngine.RemoveInfo(id); - return debugIds.Remove(id); + if (!VerifyMode()) return false; + if (menuMode) + { + throw new GameNotFoundException("Game object references a menu item but RemoveDebugInfo only works on the currently-loaded game"); + } + if (!debugIds.Contains(id)) return false; + debugOverlayEngine.RemoveInfo("game_" + id); + return debugIds.Remove(id); + } + + /// + /// 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. + /// + /// Debug info identifier. + /// A function that returns the current information. + public static void AddPersistentDebugInfo(string id, Func contentGetter) + { + debugOverlayEngine.SetInfo("persistent_" + id, contentGetter); + } + + /// + /// Remove persistent information from the in-game debug display. + /// + /// true, if debug info was removed, false otherwise. + /// Debug info identifier. + public static bool RemovePersistentDebugInfo(string id) + { + return debugOverlayEngine.RemoveInfo("persistent_" + id); } /// diff --git a/TechbloxModdingAPI/Blocks/Engines/PlacementEngine.cs b/TechbloxModdingAPI/Blocks/Engines/PlacementEngine.cs index b076d96..196df83 100644 --- a/TechbloxModdingAPI/Blocks/Engines/PlacementEngine.cs +++ b/TechbloxModdingAPI/Blocks/Engines/PlacementEngine.cs @@ -113,7 +113,7 @@ namespace TechbloxModdingAPI.Blocks.Engines public bool isRemovable => false; [HarmonyPatch] - public class FactoryObtainerPatch + class FactoryObtainerPatch { static void Postfix(BlockEntityFactory blockEntityFactory, IEntityFactory entityFactory) { diff --git a/TechbloxModdingAPI/Blocks/Engines/RemovalEngine.cs b/TechbloxModdingAPI/Blocks/Engines/RemovalEngine.cs index 4d4897f..b753d3f 100644 --- a/TechbloxModdingAPI/Blocks/Engines/RemovalEngine.cs +++ b/TechbloxModdingAPI/Blocks/Engines/RemovalEngine.cs @@ -44,7 +44,7 @@ namespace TechbloxModdingAPI.Blocks.Engines public bool isRemovable => false; [HarmonyPatch] - public class FactoryObtainerPatch + class FactoryObtainerPatch { static void Postfix(IEntityFunctions entityFunctions, MachineGraphConnectionEntityFactory machineGraphConnectionEntityFactory) diff --git a/TechbloxModdingAPI/Blocks/Engines/ScalingEngine.cs b/TechbloxModdingAPI/Blocks/Engines/ScalingEngine.cs index 5ab84f2..9ca77f0 100644 --- a/TechbloxModdingAPI/Blocks/Engines/ScalingEngine.cs +++ b/TechbloxModdingAPI/Blocks/Engines/ScalingEngine.cs @@ -41,7 +41,7 @@ namespace TechbloxModdingAPI.Blocks.Engines } [HarmonyPatch] - public class PhysicsEnginePatch + class PhysicsEnginePatch { static void Postfix(IReactOnAddAndRemove __instance) { diff --git a/TechbloxModdingAPI/Inventory/Hotbar.cs b/TechbloxModdingAPI/Inventory/Hotbar.cs deleted file mode 100644 index 92bca86..0000000 --- a/TechbloxModdingAPI/Inventory/Hotbar.cs +++ /dev/null @@ -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(); - - /// - /// Switch the block in the player's hand - /// - /// The block to switch to. - /// The player. Omit this to use the local player. - 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 - } - - /// - /// Gets the block in the player's hand - /// - /// The equipped block. - public static BlockIDs GetEquippedBlock() - { - return HotbarSlotSelectionHandlerEnginePatch.EquippedPartID; - } - - public static void Init() - { - GameEngineManager.AddGameEngine(hotbarEngine); - } - } -} diff --git a/TechbloxModdingAPI/Inventory/HotbarEngine.cs b/TechbloxModdingAPI/Inventory/HotbarEngine.cs deleted file mode 100644 index 66f8c78..0000000 --- a/TechbloxModdingAPI/Inventory/HotbarEngine.cs +++ /dev/null @@ -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(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); - } - } -} diff --git a/TechbloxModdingAPI/Inventory/HotbarSlotSelectionHandlerEnginePatch.cs b/TechbloxModdingAPI/Inventory/HotbarSlotSelectionHandlerEnginePatch.cs index 1a068f0..79c6d5a 100644 --- a/TechbloxModdingAPI/Inventory/HotbarSlotSelectionHandlerEnginePatch.cs +++ b/TechbloxModdingAPI/Inventory/HotbarSlotSelectionHandlerEnginePatch.cs @@ -9,7 +9,7 @@ using TechbloxModdingAPI.Blocks; namespace TechbloxModdingAPI.Inventory { [HarmonyPatch] - public class HotbarSlotSelectionHandlerEnginePatch + class HotbarSlotSelectionHandlerEnginePatch { private static int selectedBlockInt = 0; diff --git a/TechbloxModdingAPI/Main.cs b/TechbloxModdingAPI/Main.cs index a390021..610aa8e 100644 --- a/TechbloxModdingAPI/Main.cs +++ b/TechbloxModdingAPI/Main.cs @@ -65,8 +65,6 @@ namespace TechbloxModdingAPI Utility.GameState.Init(); // init block implementors Logging.MetaDebugLog($"Initializing Blocks"); - // init inventory - Inventory.Hotbar.Init(); // init input Input.FakeInput.Init(); // init object-oriented classes @@ -75,7 +73,6 @@ namespace TechbloxModdingAPI BlockGroup.Init(); Wire.Init(); Logging.MetaDebugLog($"Initializing Client"); - GameClient.Init(); Client.Init(); Game.Init(); // init UI diff --git a/TechbloxModdingAPI/Persistence/SerializerManager.cs b/TechbloxModdingAPI/Persistence/SerializerManager.cs index 04079cf..fe11320 100644 --- a/TechbloxModdingAPI/Persistence/SerializerManager.cs +++ b/TechbloxModdingAPI/Persistence/SerializerManager.cs @@ -58,7 +58,7 @@ namespace TechbloxModdingAPI.Persistence return _serializers.Count; } - public static void RegisterSerializers(EnginesRoot enginesRoot) + internal static void RegisterSerializers(EnginesRoot enginesRoot) { _lastEnginesRoot = enginesRoot; IEntitySerialization ies = enginesRoot.GenerateEntitySerializer(); diff --git a/TechbloxModdingAPI/Players/PlayerEngine.cs b/TechbloxModdingAPI/Players/PlayerEngine.cs index f5fbb26..506c55d 100644 --- a/TechbloxModdingAPI/Players/PlayerEngine.cs +++ b/TechbloxModdingAPI/Players/PlayerEngine.cs @@ -102,23 +102,17 @@ namespace TechbloxModdingAPI.Players public bool SetLocation(uint playerId, float3 location, bool exitSeat = true) { if (entitiesDB == null) return false; - var characterGroups = CharacterExclusiveGroups.AllCharacters; - for (int i = 0; i < characterGroups.count; i++) - { - EGID egid = new EGID(playerId, characterGroups[i]); - if (entitiesDB.Exists(egid)) - { - ref RigidBodyEntityStruct rbes = ref entitiesDB.QueryEntity(egid); - if (characterGroups[i] == CharacterExclusiveGroups.InPilotSeatGroup && exitSeat) - { - entitiesDB.QueryEntity(egid).instantExit = true; - entitiesDB.PublishEntityChange(egid); - } - rbes.position = location; - return true; - } - } - return false; + var rbesOpt = GetCharacterStruct(playerId, out var group); + if (!rbesOpt) + return false; + if (group == CharacterExclusiveGroups.InPilotSeatGroup && exitSeat) + { + EGID egid = new EGID(playerId, group); + entitiesDB.QueryEntity(egid).instantExit = true; + entitiesDB.PublishEntityChange(egid); + } + rbesOpt.Get().position = location; + return true; } public bool GetGameOverScreen(uint playerId) @@ -137,9 +131,14 @@ namespace TechbloxModdingAPI.Players // reusable methods - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public OptionalRef GetCharacterStruct(uint playerId) where T : unmanaged, IEntityComponent + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public OptionalRef GetCharacterStruct(uint playerId) where T : unmanaged, IEntityComponent => + GetCharacterStruct(playerId, out _); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public OptionalRef GetCharacterStruct(uint playerId, out ExclusiveGroupStruct group) where T : unmanaged, IEntityComponent { + group = default; if (GameState.IsBuildMode()) return entitiesDB.QueryEntityOptional(new EGID(playerId, Techblox.FlyCam.FlyCam.Group)); @@ -149,7 +148,10 @@ namespace TechbloxModdingAPI.Players EGID egid = new EGID(playerId, characterGroups[i]); var opt = entitiesDB.QueryEntityOptional(egid); if (opt.Exists) + { + group = characterGroups[i]; return opt; + } } return new OptionalRef(); diff --git a/TechbloxModdingAPI/Tests/TechbloxModdingAPIPluginTest.cs b/TechbloxModdingAPI/Tests/TechbloxModdingAPIPluginTest.cs index 8f5298f..f618211 100644 --- a/TechbloxModdingAPI/Tests/TechbloxModdingAPIPluginTest.cs +++ b/TechbloxModdingAPI/Tests/TechbloxModdingAPIPluginTest.cs @@ -25,7 +25,7 @@ namespace TechbloxModdingAPI.Tests /// Modding API implemented as a standalone IPA Plugin. /// Ideally, TechbloxModdingAPI should be loaded by another mod; not itself /// - public class TechbloxModdingAPIPluginTest : IllusionPlugin.IEnhancedPlugin + class TechbloxModdingAPIPluginTest : IllusionPlugin.IEnhancedPlugin { private static Harmony harmony { get; set; } @@ -194,7 +194,7 @@ namespace TechbloxModdingAPI.Tests Game.CurrentGame().ToggleTimeMode(); }).Build(); - GameClient.SetDebugInfo("InstalledMods", InstalledMods); + Game.AddPersistentDebugInfo("InstalledMods", InstalledMods); Block.Placed += (sender, args) => Logging.MetaDebugLog("Placed block " + args.Block); Block.Removed += (sender, args) => diff --git a/TechbloxModdingAPI/Utility/GameClient.cs b/TechbloxModdingAPI/Utility/GameClient.cs deleted file mode 100644 index f63b270..0000000 --- a/TechbloxModdingAPI/Utility/GameClient.cs +++ /dev/null @@ -1,30 +0,0 @@ -using System; -using TechbloxModdingAPI.Blocks; - -namespace TechbloxModdingAPI.Utility -{ - public static class GameClient - { - private static DebugInterfaceEngine _engine = new DebugInterfaceEngine(); - - /// - /// 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. - /// - /// A global ID for the custom information - /// A function that returns the current information - public static void SetDebugInfo(string id, Func contentGetter) => _engine.SetInfo(id, contentGetter); - - /// - /// Removes an information provided by a plugin. - /// - /// The ID of the custom information - /// - public static bool RemoveDebugInfo(string id) => _engine.RemoveInfo(id); - - public static void Init() - { - GameEngineManager.AddGameEngine(_engine); - } - } -} \ No newline at end of file