Add new block IDs, a property, 2 tests and fixes

This commit is contained in:
Norbi Peti 2020-12-20 00:05:02 +01:00
parent 6a90739197
commit 879901f4b9
5 changed files with 77 additions and 17 deletions

View file

@ -171,7 +171,7 @@ namespace GamecraftModdingAPI
egid = new EGID(id, group.Value);
if (typeToGroup.TryGetValue(type, out var gr)
&& gr.All(egs => egs != group.Value)) //If this subclass has a specific group, then use that - so Block should still work
throw new BlockTypeException($"Incompatible block type! Type {type.Name} belongs to group {gr.Select(g => g.ToString()).Aggregate((a, b) => a + ", " + b)} instead of {group.Value}");
throw new BlockTypeException($"Incompatible block type! Type {type.Name} belongs to group {gr.Select(g => ((uint)g).ToString()).Aggregate((a, b) => a + ", " + b)} instead of {(uint)group.Value}");
}
if (initializers.TryGetValue(type, out var func))

View file

@ -71,8 +71,9 @@ namespace GamecraftModdingAPI.Blocks
GlassConeSegment,
GlassCylinder,
GlassSphere,
Lever, //63 - two IDs skipped
PlayerSpawn = 66, //Crashes without special handling
Lever, //63
WoodenSlatsDoor = 65,
PlayerSpawn, //Crashes without special handling
SmallSpawn,
MediumSpawn,
LargeSpawn,
@ -86,10 +87,17 @@ namespace GamecraftModdingAPI.Blocks
DampedSpring,
ServoPiston,
StepperPiston,
PneumaticPiston,
PneumaticPiston, //80
PneumaticHinge,
PneumaticAxle, //82
PilotSeat = 90, //Might crash
PneumaticAxle,
WindowedDoor,
Bench,
Chair,
Stool,
DampedHingeSpring,
PlainGlassDoor,
PlainWoodenDoor,
PilotSeat, //Might crash
PassengerSeat,
PilotControls,
GrassCube,
@ -148,8 +156,9 @@ namespace GamecraftModdingAPI.Blocks
WoodCylinder,
WoodHemisphere,
WoodSphere,
BrickCube, //149
BrickSlicedCube = 151,
BrickCube,
DampedAxleSpring, //150
BrickSlicedCube,
BrickSlope,
BrickCorner,
ConcreteCube,
@ -355,6 +364,7 @@ namespace GamecraftModdingAPI.Blocks
HexNetCylinder = 797,
HexNetHemisphere,
HexNetSphere,
HexNetTubeCorner //800
HexNetTubeCorner, //800
CenterOfMassBlock = 1346
}
}

View file

@ -1,6 +1,7 @@
using System;
using Gamecraft.Wires;
using Unity.Mathematics;
using GamecraftModdingAPI;
using GamecraftModdingAPI.Tests;
@ -60,19 +61,31 @@ namespace GamecraftModdingAPI.Blocks
Assert.Errorless(() => { b = newBlock.Specialise<Piston>(); }, "Block.Specialize<Piston>() raised an exception: ", "Block.Specialize<Piston>() completed without issue.");
if (!Assert.NotNull(b, "Block.Specialize<Piston>() returned null, possibly because it failed silently.", "Specialized Piston is not null.")) return;
if (!Assert.CloseTo(b.MaximumExtension, 1.01f, $"Piston.MaximumExtension {b.MaximumExtension} does not equal default value, possibly because it failed silently.", "Piston.MaximumExtension is close enough to default.")) return;
if (!Assert.CloseTo(b.MaximumForce, 750f, $"Piston.MaximumForce {b.MaximumForce} does not equal default value, possibly because it failed silently.", "Piston.MaximumForce is close enough to default.")) return;
if (!Assert.CloseTo(b.MaximumForce, 1.0f, $"Piston.MaximumForce {b.MaximumForce} does not equal default value, possibly because it failed silently.", "Piston.MaximumForce is close enough to default.")) return;
}
[APITestCase(TestType.EditMode)]
[APITestCase(TestType.EditMode)]
public static void TestServo()
{
Block newBlock = Block.PlaceNew(BlockIDs.ServoAxle, Unity.Mathematics.float3.zero + 1);
Servo b = null; // Note: the assignment operation is a lambda, which slightly confuses the compiler
Assert.Errorless(() => { b = newBlock.Specialise<Servo>(); }, "Block.Specialize<Servo>() raised an exception: ", "Block.Specialize<Servo>() completed without issue.");
if (!Assert.NotNull(b, "Block.Specialize<Servo>() returned null, possibly because it failed silently.", "Specialized Servo is not null.")) return;
if (!Assert.CloseTo(b.MaximumAngle, 180f, $"Servo.MaximumAngle {b.MaximumAngle} does not equal default value, possibly because it failed silently.", "Servo.MaximumAngle is close enough to default.")) return;
Block newBlock = Block.PlaceNew(BlockIDs.ServoAxle, Unity.Mathematics.float3.zero + 1);
Servo b = null; // Note: the assignment operation is a lambda, which slightly confuses the compiler
Assert.Errorless(() => { b = newBlock.Specialise<Servo>(); }, "Block.Specialize<Servo>() raised an exception: ", "Block.Specialize<Servo>() completed without issue.");
if (!Assert.NotNull(b, "Block.Specialize<Servo>() returned null, possibly because it failed silently.", "Specialized Servo is not null.")) return;
if (!Assert.CloseTo(b.MaximumAngle, 180f, $"Servo.MaximumAngle {b.MaximumAngle} does not equal default value, possibly because it failed silently.", "Servo.MaximumAngle is close enough to default.")) return;
if (!Assert.CloseTo(b.MinimumAngle, -180f, $"Servo.MinimumAngle {b.MinimumAngle} does not equal default value, possibly because it failed silently.", "Servo.MinimumAngle is close enough to default.")) return;
if (!Assert.CloseTo(b.MaximumForce, 750f, $"Servo.MaximumForce {b.MaximumForce} does not equal default value, possibly because it failed silently.", "Servo.MaximumForce is close enough to default.")) return;
if (!Assert.CloseTo(b.MaximumForce, 60f, $"Servo.MaximumForce {b.MaximumForce} does not equal default value, possibly because it failed silently.", "Servo.MaximumForce is close enough to default.")) return;
}
[APITestCase(TestType.EditMode)]
public static void TestDampedSpring()
{
Block newBlock = Block.PlaceNew(BlockIDs.DampedSpring, Unity.Mathematics.float3.zero + 1);
DampedSpring b = null; // Note: the assignment operation is a lambda, which slightly confuses the compiler
Assert.Errorless(() => { b = newBlock.Specialise<DampedSpring>(); }, "Block.Specialize<Servo>() raised an exception: ", "Block.Specialize<DampedSpring>() completed without issue.");
if (!Assert.NotNull(b, "Block.Specialize<DampedSpring>() returned null, possibly because it failed silently.", "Specialized DampedSpring is not null.")) return;
if (!Assert.CloseTo(b.Stiffness, 1.0f, $"DampedSpring.Stiffness {b.Stiffness} does not equal default value, possibly because it failed silently.", "DampedSpring.Stiffness is close enough to default.")) return;
if (!Assert.CloseTo(b.Damping, 0.1f, $"DampedSpring.Damping {b.Damping} does not equal default value, possibly because it failed silently.", "DampedSpring.Damping is close enough to default.")) return;
if (!Assert.CloseTo(b.MaxExtension, 0.3f, $"DampedSpring.MaxExtension {b.MaxExtension} does not equal default value, possibly because it failed silently.", "DampedSpring.MaxExtension is close enough to default.")) return;
}
[APITestCase(TestType.Game)]
@ -119,6 +132,13 @@ namespace GamecraftModdingAPI.Blocks
if (!Assert.Errorless(() => { newWire = b.Connect(0, target, 0);})) return;
if (!Assert.NotNull(newWire, "SignalingBlock.Connect(...) returned null, possible because it failed silently.", "SignalingBlock.Connect(...) returned a non-null value.")) return;
}
[APITestCase(TestType.EditMode)]
public static void TestSpecialiseError()
{
Block newBlock = Block.PlaceNew(BlockIDs.Bench, new float3(1, 1, 1));
if (Assert.Errorful<BlockTypeException>(() => newBlock.Specialise<MusicBlock>(), "Block.Specialise<MusicBlock>() was expected to error on a bench block.", "Block.Specialise<MusicBlock>() errored as expected for a bench block.")) return;
}
}
#endif
}

View file

@ -44,5 +44,16 @@ namespace GamecraftModdingAPI.Blocks
set => BlockEngine.SetBlockInfo(this,
(ref DampedSpringReadOnlyStruct ljf, float val) => ljf.springDamping = val, value);
}
/// <summary>
/// The spring's maximum extension.
/// </summary>
public float MaxExtension
{
get => BlockEngine.GetBlockInfo(this, (DampedSpringReadOnlyStruct ljf) => ljf.maxExtent);
set => BlockEngine.SetBlockInfo(this,
(ref DampedSpringReadOnlyStruct ljf, float val) => ljf.maxExtent = val, value);
}
}
}

View file

@ -119,6 +119,25 @@ namespace GamecraftModdingAPI.Tests
return true;
}
public static bool Errorful<T>(Action tryThis, string err = null, string success = null) where T : Exception
{
if (err == null) err = $"{tryThis} was expected to error but completed without errors.";
if (success == null) success = $"{tryThis} completed with an expected error.";
try
{
tryThis();
}
catch (T e)
{
TestRoot.TestsPassed = true;
Log(PASS + success + " " + e.GetType().Name + ": " + e.Message);
return true;
}
Log(FAIL + err);
TestRoot.TestsPassed = false;
return false;
}
public static bool CloseTo(float a, float b, string err = null, string success = null, float delta = float.Epsilon)
{
if (err == null) err = $"{a} is not within {delta} of {b}.";