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 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
{
/// <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
{
private static readonly PlacementEngine PlacementEngine = new PlacementEngine();
@ -73,7 +78,8 @@ namespace GamecraftModdingAPI
public EGID Id { get; }
/// <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>
public float3 Position
{
@ -85,7 +91,7 @@ namespace GamecraftModdingAPI
}
/// <summary>
/// The block's current rotation in degrees.
/// The block's current rotation in degrees or zero if the block doesn't exist.
/// </summary>
public float3 Rotation
{
@ -97,10 +103,42 @@ namespace GamecraftModdingAPI
}
/// <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>
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
{
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);
/// <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>
public Block[] GetConnectedCubes() => BlockEngine.GetConnectedBlocks(Id);

View file

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

View file

@ -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");
}

View file

@ -180,6 +180,8 @@ namespace GamecraftModdingAPI.Tests
.Action(() => { throw new Exception("Error Command always throws an error"); })
.Build();
GameClient.SetDebugInfo("lookedAt", LookedAt);
/*
CommandManager.AddCommand(new SimpleCustomCommandEngine<float>((float d) => { UnityEngine.Camera.main.fieldOfView = d; },
"SetFOV", "Set the player camera's field of view"));
@ -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() { }

View file

@ -19,7 +19,6 @@ namespace GamecraftModdingAPI.Utility
private static Dictionary<string, Func<string>> _extraInfo=new Dictionary<string, Func<string>>();
public void Ready()
{
SetInfo("lookedAt", LookedAt);
}
public EntitiesDB entitiesDB { get; set; }
@ -31,16 +30,6 @@ namespace GamecraftModdingAPI.Utility
public void SetInfo(string id, Func<string> 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()

View file

@ -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
/// </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 SetInfo(string id, Func<string> contentGetter) => _engine.SetInfo(id, contentGetter);
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 RemoveInfo(string id) => _engine.RemoveInfo(id);
public static bool RemoveDebugInfo(string id) => _engine.RemoveInfo(id);
public static void Init()
{