From 6dce87fb661b80b2a4c86c04984177539eba5ebd Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Sun, 17 May 2020 23:23:06 +0200 Subject: [PATCH] Documentation, added invalid block ID, error handling --- GamecraftModdingAPI/Block.cs | 51 +++++++++++++++++-- GamecraftModdingAPI/Blocks/BlockIDs.cs | 5 +- GamecraftModdingAPI/Main.cs | 2 +- .../Tests/GamecraftModdingAPIPluginTest.cs | 13 +++++ .../Utility/DebugInterfaceEngine.cs | 26 +++++----- .../{DebugInterface.cs => GameClient.cs} | 6 +-- 6 files changed, 78 insertions(+), 25 deletions(-) rename GamecraftModdingAPI/Utility/{DebugInterface.cs => GameClient.cs} (79%) diff --git a/GamecraftModdingAPI/Block.cs b/GamecraftModdingAPI/Block.cs index db220dc..88fcc3d 100644 --- a/GamecraftModdingAPI/Block.cs +++ b/GamecraftModdingAPI/Block.cs @@ -1,7 +1,9 @@ using System; using Svelto.ECS; +using Svelto.ECS.EntityStructs; using RobocraftX.Common; +using RobocraftX.Blocks.Scaling; using Unity.Mathematics; using GamecraftModdingAPI.Blocks; @@ -9,6 +11,9 @@ using GamecraftModdingAPI.Utility; namespace GamecraftModdingAPI { + /// + /// A single (perhaps scaled) block. Properties may return default values if the block is removed and then setting them is ignored. + /// public class Block { private static readonly PlacementEngine PlacementEngine = new PlacementEngine(); @@ -73,7 +78,8 @@ namespace GamecraftModdingAPI public EGID Id { get; } /// - /// The block's current position. + /// The block's current position or zero if the block no longer exists. + /// A block is 0.2 wide by default in terms of position. /// public float3 Position { @@ -85,7 +91,7 @@ namespace GamecraftModdingAPI } /// - /// The block's current rotation in degrees. + /// The block's current rotation in degrees or zero if the block doesn't exist. /// public float3 Rotation { @@ -97,10 +103,42 @@ namespace GamecraftModdingAPI } /// - /// The block's type (ID). + /// The block's non-uniform scale or zero if the block's invalid. Independent of the uniform scaling. /// - public BlockIDs Type => (BlockIDs) (BlockEngine.GetBlockInfo(Id)?.DBID ?? 0); + public float3 Scale + { + get => BlockEngine.GetBlockInfo(Id)?.scale ?? float3.zero; + set + { + var def = new ScalingEntityStruct(); + BlockEngine.GetBlockInfo(Id, ref def).scale = value; + } + } + + /// + /// The block's uniform scale or zero if the block's invalid. Also sets the non-uniform scale. + /// + public int UniformScale + { + get => BlockEngine.GetBlockInfo(Id)?.desiredScaleFactor ?? 0; + set + { + var def = new BlockPlacementScaleEntityStruct(); + ref var scaleStruct = ref BlockEngine.GetBlockInfo(Id, ref def); + scaleStruct.blockPlacementHeight = scaleStruct.blockPlacementWidth = + scaleStruct.desiredScaleFactor = scaleStruct.snapGridScale = value; + Scale = new float3(value, value, value); + } + } + + /// + /// The block's type (ID). Returns BlockIDs.Invalid if the block doesn't exist anymore. + /// + public BlockIDs Type => (BlockIDs) (BlockEngine.GetBlockInfo(Id)?.DBID ?? ushort.MaxValue); + /// + /// The block's color. Returns BlockColors.Default if the block no longer exists. + /// public BlockColor Color { get @@ -118,10 +156,13 @@ namespace GamecraftModdingAPI } } + /// + /// Whether the block exists. The other properties will return a default value if the block doesn't exist. + /// public bool Exists => BlockEngine.BlockExists(Id); /// - /// Returns an array of blocks that are connected to this one. + /// Returns an array of blocks that are connected to this one. Returns an empty array if the block doesn't exist. /// public Block[] GetConnectedCubes() => BlockEngine.GetConnectedBlocks(Id); diff --git a/GamecraftModdingAPI/Blocks/BlockIDs.cs b/GamecraftModdingAPI/Blocks/BlockIDs.cs index bba2583..a4d07ff 100644 --- a/GamecraftModdingAPI/Blocks/BlockIDs.cs +++ b/GamecraftModdingAPI/Blocks/BlockIDs.cs @@ -3,9 +3,10 @@ namespace GamecraftModdingAPI.Blocks /// /// Possible block types /// - public enum BlockIDs + public enum BlockIDs : ushort { - AluminiumCube, + Invalid = ushort.MaxValue, + AluminiumCube = 0, AxleS, HingeS = 3, MotorS, diff --git a/GamecraftModdingAPI/Main.cs b/GamecraftModdingAPI/Main.cs index 3b15de8..5a65baf 100644 --- a/GamecraftModdingAPI/Main.cs +++ b/GamecraftModdingAPI/Main.cs @@ -72,7 +72,7 @@ namespace GamecraftModdingAPI // init object-oriented classes Player.Init(); Block.Init(); - DebugInterface.Init(); + GameClient.Init(); Logging.MetaLog($"{currentAssembly.GetName().Name} v{currentAssembly.GetName().Version} initialized"); } diff --git a/GamecraftModdingAPI/Tests/GamecraftModdingAPIPluginTest.cs b/GamecraftModdingAPI/Tests/GamecraftModdingAPIPluginTest.cs index 2e97df7..e37a5bb 100644 --- a/GamecraftModdingAPI/Tests/GamecraftModdingAPIPluginTest.cs +++ b/GamecraftModdingAPI/Tests/GamecraftModdingAPIPluginTest.cs @@ -179,6 +179,8 @@ namespace GamecraftModdingAPI.Tests CommandBuilder.Builder("Error", "Throw an error to make sure SimpleCustomCommandEngine's wrapper catches it.") .Action(() => { throw new Exception("Error Command always throws an error"); }) .Build(); + + GameClient.SetDebugInfo("lookedAt", LookedAt); /* CommandManager.AddCommand(new SimpleCustomCommandEngine((float d) => { UnityEngine.Camera.main.fieldOfView = d; }, @@ -235,6 +237,17 @@ namespace GamecraftModdingAPI.Tests } } + private Player player; + + private string LookedAt() + { + if (player == null) + player = new Player(Players.PlayerType.Local); + Block block = player.GetBlockLookedAt(); + if (block == null) return "Block: none"; + return "Block: " + block.Type + "\nColor: " + block.Color + "\n" + "At: " + block.Position; + } + public void OnFixedUpdate() { } public void OnLateUpdate() { } diff --git a/GamecraftModdingAPI/Utility/DebugInterfaceEngine.cs b/GamecraftModdingAPI/Utility/DebugInterfaceEngine.cs index 2d470bb..b8b0222 100644 --- a/GamecraftModdingAPI/Utility/DebugInterfaceEngine.cs +++ b/GamecraftModdingAPI/Utility/DebugInterfaceEngine.cs @@ -19,7 +19,6 @@ namespace GamecraftModdingAPI.Utility private static Dictionary> _extraInfo=new Dictionary>(); public void Ready() { - SetInfo("lookedAt", LookedAt); } public EntitiesDB entitiesDB { get; set; } @@ -31,16 +30,6 @@ namespace GamecraftModdingAPI.Utility public void SetInfo(string id, Func contentGetter) => _extraInfo[id] = contentGetter; public bool RemoveInfo(string id) => _extraInfo.Remove(id); - private Player player; - private string LookedAt() - { - if (player == null) - player = new Player(PlayerType.Local); - Block block = player.GetBlockLookedAt(); - if (block == null) return "Block: none"; - return "Block: " + block.Type + "\nColor: " + block.Color + "\n" + "At: " + block.Position; - } - public string Name { get; } = "GamecraftModdingAPIDebugInterfaceGameEngine"; public bool isRemovable { get; } = true; @@ -63,7 +52,7 @@ namespace GamecraftModdingAPI.Utility } catch (Exception e) { - Console.WriteLine(e); + Logging.LogException(e, "Failed to inject AddInfo method for the debug display!"); } return list; @@ -71,8 +60,17 @@ namespace GamecraftModdingAPI.Utility public static void AddInfo(StringBuffer sb) { - foreach (var info in _extraInfo.Values) - sb.Append(info() + "\n"); + foreach (var info in _extraInfo) + { + try + { + sb.Append(info.Value() + "\n"); + } + catch (Exception e) + { + Logging.LogException(e, "Unable to get info for " + info.Key); + } + } } public static MethodInfo TargetMethod() diff --git a/GamecraftModdingAPI/Utility/DebugInterface.cs b/GamecraftModdingAPI/Utility/GameClient.cs similarity index 79% rename from GamecraftModdingAPI/Utility/DebugInterface.cs rename to GamecraftModdingAPI/Utility/GameClient.cs index 1d5499e..98d8ccb 100644 --- a/GamecraftModdingAPI/Utility/DebugInterface.cs +++ b/GamecraftModdingAPI/Utility/GameClient.cs @@ -3,7 +3,7 @@ using GamecraftModdingAPI.Blocks; namespace GamecraftModdingAPI.Utility { - public static class DebugInterface + public static class GameClient { private static DebugInterfaceEngine _engine = new DebugInterfaceEngine(); @@ -13,14 +13,14 @@ namespace GamecraftModdingAPI.Utility /// /// A global ID for the custom information /// A function that returns the current information - public static void SetInfo(string id, Func contentGetter) => _engine.SetInfo(id, contentGetter); + 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 RemoveInfo(string id) => _engine.RemoveInfo(id); + public static bool RemoveDebugInfo(string id) => _engine.RemoveInfo(id); public static void Init() {