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); egid = new EGID(id, group.Value);
if (typeToGroup.TryGetValue(type, out var gr) 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 && 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)) if (initializers.TryGetValue(type, out var func))

View file

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

View file

@ -1,6 +1,7 @@
using System; using System;
using Gamecraft.Wires; using Gamecraft.Wires;
using Unity.Mathematics;
using GamecraftModdingAPI; using GamecraftModdingAPI;
using GamecraftModdingAPI.Tests; using GamecraftModdingAPI.Tests;
@ -60,7 +61,7 @@ namespace GamecraftModdingAPI.Blocks
Assert.Errorless(() => { b = newBlock.Specialise<Piston>(); }, "Block.Specialize<Piston>() raised an exception: ", "Block.Specialize<Piston>() completed without issue."); 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.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.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)]
@ -72,7 +73,19 @@ namespace GamecraftModdingAPI.Blocks
if (!Assert.NotNull(b, "Block.Specialize<Servo>() returned null, possibly because it failed silently.", "Specialized Servo is not null.")) return; 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.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.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)] [APITestCase(TestType.Game)]
@ -119,6 +132,13 @@ namespace GamecraftModdingAPI.Blocks
if (!Assert.Errorless(() => { newWire = b.Connect(0, target, 0);})) return; 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; 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 #endif
} }

View file

@ -44,5 +44,16 @@ namespace GamecraftModdingAPI.Blocks
set => BlockEngine.SetBlockInfo(this, set => BlockEngine.SetBlockInfo(this,
(ref DampedSpringReadOnlyStruct ljf, float val) => ljf.springDamping = val, value); (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; 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) 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}."; if (err == null) err = $"{a} is not within {delta} of {b}.";