Fix default values getting changed and add test

This commit is contained in:
Norbi Peti 2021-06-23 01:58:01 +02:00
parent 76faa69c74
commit 74d5a5c6b1
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
3 changed files with 50 additions and 6 deletions

View file

@ -135,6 +135,25 @@ namespace TechbloxModdingAPI.Blocks
Assert.Pass("Setting all possible properties of all registered API block types succeeded."); Assert.Pass("Setting all possible properties of all registered API block types succeeded.");
} }
[APITestCase(TestType.EditMode)]
public static IEnumerator<TaskContract> TestDefaultValue()
{
for (int i = 0; i < 2; i++)
{ //Tests shared defaults
var block = Block.PlaceNew(BlockIDs.Cube, 1);
while (!block.Exists)
yield return Yield.It;
block.Remove();
while (block.Exists)
yield return Yield.It;
if(!Assert.Equal(block.Position, default,
$"Block position default value {block.Position} is incorrect, should be 0.",
$"Block position default value {block.Position} matches default."))
yield break;
block.Position = 4;
}
}
[APITestCase(TestType.EditMode)] [APITestCase(TestType.EditMode)]
public static void TestDampedSpring() public static void TestDampedSpring()
{ {

View file

@ -1,11 +1,8 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Linq;
using System.Reflection; using System.Reflection;
using System.Text; using System.Text;
using System.Text.RegularExpressions;
using DataLoader;
using TechbloxModdingAPI.App; using TechbloxModdingAPI.App;
using HarmonyLib; using HarmonyLib;
using IllusionInjector; using IllusionInjector;
@ -14,12 +11,13 @@ using RobocraftX.FrontEnd;
using Unity.Mathematics; using Unity.Mathematics;
using UnityEngine; using UnityEngine;
using RobocraftX.Common.Input; using RobocraftX.Common.Input;
using ServiceLayer; using Svelto.Tasks;
using Svelto.Tasks.Lean;
using TechbloxModdingAPI.Blocks; using TechbloxModdingAPI.Blocks;
using TechbloxModdingAPI.Commands; using TechbloxModdingAPI.Commands;
using TechbloxModdingAPI.Input; using TechbloxModdingAPI.Input;
using TechbloxModdingAPI.Interface.IMGUI;
using TechbloxModdingAPI.Players; using TechbloxModdingAPI.Players;
using TechbloxModdingAPI.Tasks;
using TechbloxModdingAPI.Utility; using TechbloxModdingAPI.Utility;
namespace TechbloxModdingAPI.Tests namespace TechbloxModdingAPI.Tests
@ -288,6 +286,32 @@ namespace TechbloxModdingAPI.Tests
{ {
Game.CurrentGame().EnableScreenshotTaker(); Game.CurrentGame().EnableScreenshotTaker();
}).Build(); }).Build();
CommandBuilder.Builder("testPositionDefault", "Tests the Block.Position property's default value.")
.Action(() =>
{
IEnumerator<TaskContract> Loop()
{
for (int i = 0; i < 2; i++)
{
Console.WriteLine("A");
var block = Block.PlaceNew(BlockIDs.Cube, 1);
Console.WriteLine("B");
while (!block.Exists)
yield return Yield.It;
Console.WriteLine("C");
block.Remove();
Console.WriteLine("D");
while (block.Exists)
yield return Yield.It;
Console.WriteLine("E - Pos: " + block.Position);
block.Position = 4;
Console.WriteLine("F - Pos: " + block.Position);
}
}
Loop().RunOn(Scheduler.leanRunner);
}).Build();
#if TEST #if TEST
TestRoot.RunTests(); TestRoot.RunTests();
#endif #endif

View file

@ -58,6 +58,7 @@ namespace TechbloxModdingAPI.Utility
/// <returns>The value or the default value</returns> /// <returns>The value or the default value</returns>
public ref T Get() public ref T Get()
{ {
CompRefCache.Default = default; //The default value can be changed by mods
if (state == State.Empty) return ref CompRefCache.Default; if (state == State.Empty) return ref CompRefCache.Default;
if ((state & State.Initializer) != State.Empty) return ref initializer.GetOrCreate<T>(); if ((state & State.Initializer) != State.Empty) return ref initializer.GetOrCreate<T>();
if ((state & State.Native) != State.Empty) return ref array[index]; if ((state & State.Native) != State.Empty) return ref array[index];
@ -73,7 +74,7 @@ namespace TechbloxModdingAPI.Utility
/// <summary> /// <summary>
/// Creates an instance of a struct T that can be referenced. /// Creates an instance of a struct T that can be referenced.
/// </summary> /// </summary>
internal struct CompRefCache private struct CompRefCache
{ {
public static T Default; public static T Default;
} }