Documentation, added invalid block ID, error handling

This commit is contained in:
Norbi Peti 2020-05-17 23:23:06 +02:00
parent 1c5ce37fce
commit 6dce87fb66
6 changed files with 78 additions and 25 deletions

View file

@ -1,7 +1,9 @@
using System; using System;
using Svelto.ECS; using Svelto.ECS;
using Svelto.ECS.EntityStructs;
using RobocraftX.Common; using RobocraftX.Common;
using RobocraftX.Blocks.Scaling;
using Unity.Mathematics; using Unity.Mathematics;
using GamecraftModdingAPI.Blocks; using GamecraftModdingAPI.Blocks;
@ -9,6 +11,9 @@ using GamecraftModdingAPI.Utility;
namespace GamecraftModdingAPI namespace GamecraftModdingAPI
{ {
/// <summary>
/// A single (perhaps scaled) block. Properties may return default values if the block is removed and then setting them is ignored.
/// </summary>
public class Block public class Block
{ {
private static readonly PlacementEngine PlacementEngine = new PlacementEngine(); private static readonly PlacementEngine PlacementEngine = new PlacementEngine();
@ -73,7 +78,8 @@ namespace GamecraftModdingAPI
public EGID Id { get; } public EGID Id { get; }
/// <summary> /// <summary>
/// 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.
/// </summary> /// </summary>
public float3 Position public float3 Position
{ {
@ -85,7 +91,7 @@ namespace GamecraftModdingAPI
} }
/// <summary> /// <summary>
/// The block's current rotation in degrees. /// The block's current rotation in degrees or zero if the block doesn't exist.
/// </summary> /// </summary>
public float3 Rotation public float3 Rotation
{ {
@ -97,10 +103,42 @@ namespace GamecraftModdingAPI
} }
/// <summary> /// <summary>
/// The block's type (ID). /// The block's non-uniform scale or zero if the block's invalid. Independent of the uniform scaling.
/// </summary> /// </summary>
public BlockIDs Type => (BlockIDs) (BlockEngine.GetBlockInfo<DBEntityStruct>(Id)?.DBID ?? 0); public float3 Scale
{
get => BlockEngine.GetBlockInfo<ScalingEntityStruct>(Id)?.scale ?? float3.zero;
set
{
var def = new ScalingEntityStruct();
BlockEngine.GetBlockInfo(Id, ref def).scale = value;
}
}
/// <summary>
/// The block's uniform scale or zero if the block's invalid. Also sets the non-uniform scale.
/// </summary>
public int UniformScale
{
get => BlockEngine.GetBlockInfo<BlockPlacementScaleEntityStruct>(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);
}
}
/// <summary>
/// The block's type (ID). Returns BlockIDs.Invalid if the block doesn't exist anymore.
/// </summary>
public BlockIDs Type => (BlockIDs) (BlockEngine.GetBlockInfo<DBEntityStruct>(Id)?.DBID ?? ushort.MaxValue);
/// <summary>
/// The block's color. Returns BlockColors.Default if the block no longer exists.
/// </summary>
public BlockColor Color public BlockColor Color
{ {
get get
@ -118,10 +156,13 @@ namespace GamecraftModdingAPI
} }
} }
/// <summary>
/// Whether the block exists. The other properties will return a default value if the block doesn't exist.
/// </summary>
public bool Exists => BlockEngine.BlockExists(Id); public bool Exists => BlockEngine.BlockExists(Id);
/// <summary> /// <summary>
/// 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.
/// </summary> /// </summary>
public Block[] GetConnectedCubes() => BlockEngine.GetConnectedBlocks(Id); public Block[] GetConnectedCubes() => BlockEngine.GetConnectedBlocks(Id);

View file

@ -3,9 +3,10 @@ namespace GamecraftModdingAPI.Blocks
/// <summary> /// <summary>
/// Possible block types /// Possible block types
/// </summary> /// </summary>
public enum BlockIDs public enum BlockIDs : ushort
{ {
AluminiumCube, Invalid = ushort.MaxValue,
AluminiumCube = 0,
AxleS, AxleS,
HingeS = 3, HingeS = 3,
MotorS, MotorS,

View file

@ -72,7 +72,7 @@ namespace GamecraftModdingAPI
// init object-oriented classes // init object-oriented classes
Player.Init(); Player.Init();
Block.Init(); Block.Init();
DebugInterface.Init(); GameClient.Init();
Logging.MetaLog($"{currentAssembly.GetName().Name} v{currentAssembly.GetName().Version} initialized"); Logging.MetaLog($"{currentAssembly.GetName().Name} v{currentAssembly.GetName().Version} initialized");
} }

View file

@ -179,6 +179,8 @@ namespace GamecraftModdingAPI.Tests
CommandBuilder.Builder("Error", "Throw an error to make sure SimpleCustomCommandEngine's wrapper catches it.") CommandBuilder.Builder("Error", "Throw an error to make sure SimpleCustomCommandEngine's wrapper catches it.")
.Action(() => { throw new Exception("Error Command always throws an error"); }) .Action(() => { throw new Exception("Error Command always throws an error"); })
.Build(); .Build();
GameClient.SetDebugInfo("lookedAt", LookedAt);
/* /*
CommandManager.AddCommand(new SimpleCustomCommandEngine<float>((float d) => { UnityEngine.Camera.main.fieldOfView = d; }, CommandManager.AddCommand(new SimpleCustomCommandEngine<float>((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 OnFixedUpdate() { }
public void OnLateUpdate() { } public void OnLateUpdate() { }

View file

@ -19,7 +19,6 @@ namespace GamecraftModdingAPI.Utility
private static Dictionary<string, Func<string>> _extraInfo=new Dictionary<string, Func<string>>(); private static Dictionary<string, Func<string>> _extraInfo=new Dictionary<string, Func<string>>();
public void Ready() public void Ready()
{ {
SetInfo("lookedAt", LookedAt);
} }
public EntitiesDB entitiesDB { get; set; } public EntitiesDB entitiesDB { get; set; }
@ -31,16 +30,6 @@ namespace GamecraftModdingAPI.Utility
public void SetInfo(string id, Func<string> contentGetter) => _extraInfo[id] = contentGetter; public void SetInfo(string id, Func<string> contentGetter) => _extraInfo[id] = contentGetter;
public bool RemoveInfo(string id) => _extraInfo.Remove(id); 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 string Name { get; } = "GamecraftModdingAPIDebugInterfaceGameEngine";
public bool isRemovable { get; } = true; public bool isRemovable { get; } = true;
@ -63,7 +52,7 @@ namespace GamecraftModdingAPI.Utility
} }
catch (Exception e) catch (Exception e)
{ {
Console.WriteLine(e); Logging.LogException(e, "Failed to inject AddInfo method for the debug display!");
} }
return list; return list;
@ -71,8 +60,17 @@ namespace GamecraftModdingAPI.Utility
public static void AddInfo(StringBuffer sb) public static void AddInfo(StringBuffer sb)
{ {
foreach (var info in _extraInfo.Values) foreach (var info in _extraInfo)
sb.Append(info() + "\n"); {
try
{
sb.Append(info.Value() + "\n");
}
catch (Exception e)
{
Logging.LogException(e, "Unable to get info for " + info.Key);
}
}
} }
public static MethodInfo TargetMethod() public static MethodInfo TargetMethod()

View file

@ -3,7 +3,7 @@ using GamecraftModdingAPI.Blocks;
namespace GamecraftModdingAPI.Utility namespace GamecraftModdingAPI.Utility
{ {
public static class DebugInterface public static class GameClient
{ {
private static DebugInterfaceEngine _engine = new DebugInterfaceEngine(); private static DebugInterfaceEngine _engine = new DebugInterfaceEngine();
@ -13,14 +13,14 @@ namespace GamecraftModdingAPI.Utility
/// </summary> /// </summary>
/// <param name="id">A global ID for the custom information</param> /// <param name="id">A global ID for the custom information</param>
/// <param name="contentGetter">A function that returns the current information</param> /// <param name="contentGetter">A function that returns the current information</param>
public static void SetInfo(string id, Func<string> contentGetter) => _engine.SetInfo(id, contentGetter); public static void SetDebugInfo(string id, Func<string> contentGetter) => _engine.SetInfo(id, contentGetter);
/// <summary> /// <summary>
/// Removes an information provided by a plugin. /// Removes an information provided by a plugin.
/// </summary> /// </summary>
/// <param name="id">The ID of the custom information</param> /// <param name="id">The ID of the custom information</param>
/// <returns></returns> /// <returns></returns>
public static bool RemoveInfo(string id) => _engine.RemoveInfo(id); public static bool RemoveDebugInfo(string id) => _engine.RemoveInfo(id);
public static void Init() public static void Init()
{ {