mirror of
https://github.com/Facepunch/sbox-public.git
synced 2026-08-01 08:18:20 -04:00
* Move all unit tests to Engine/Tests * Fix Margin.EdgeSubtract * Fix Capsule.Contains * EnvironmentVariables.Remove if it's null * Fixed ray trace never returning startedsolid * Fix HistoryList.Navigate on empty list * Fix GameObject.WorldPosition accepting NaNs * Fix flex: initial expanding to the wrong grow/shrink * Translation.TryConvert shouldn't throw on invalid enum strings * Skip sound file tests on machines with no audio device
51 lines
1.5 KiB
C#
51 lines
1.5 KiB
C#
using System;
|
|
using Sandbox.Utility;
|
|
|
|
namespace SceneTests;
|
|
|
|
#nullable enable
|
|
|
|
/// <summary>
|
|
/// Helper methods for writing scene tests.
|
|
/// </summary>
|
|
internal static class Helpers
|
|
{
|
|
/// <summary>
|
|
/// Registers a prefab file in <see cref="ResourceLibrary"/> with the given <paramref name="resourcePath"/> and
|
|
/// with a root object defined by <paramref name="rootObjectJson"/>. Dispose the return value to unregister it.
|
|
/// </summary>
|
|
public static IDisposable RegisterPrefabFromJson( string resourcePath, string rootObjectJson )
|
|
{
|
|
var wrappedRootObject = "{ \"RootObject\": " + rootObjectJson + "}";
|
|
|
|
var prefabFile = new PrefabFile();
|
|
prefabFile.LoadFromJson( wrappedRootObject );
|
|
prefabFile.RegisterWeakResourceId( resourcePath );
|
|
prefabFile.Register( resourcePath );
|
|
|
|
return new DisposeAction( () => Game.Resources.Unregister( prefabFile ) );
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates a <see cref="SceneFile"/> with the given <paramref name="resourcePath"/>, populates it with game objects as given
|
|
/// by <paramref name="gameObjectsJson"/>, then loads it with <see cref="Scene.Load(SceneLoadOptions)"/>.
|
|
/// </summary>
|
|
public static Scene LoadSceneFromJson( string resourcePath, params string[] gameObjectsJson )
|
|
{
|
|
var scene = new Scene();
|
|
|
|
using var _ = scene.Push();
|
|
|
|
var sceneFile = new SceneFile { GameObjects = gameObjectsJson.Select( Sandbox.Json.ParseToJsonObject ).ToArray() };
|
|
|
|
sceneFile.RegisterWeakResourceId( resourcePath );
|
|
|
|
var options = new SceneLoadOptions();
|
|
options.SetScene( sceneFile );
|
|
|
|
scene.Load( options );
|
|
|
|
return scene;
|
|
}
|
|
}
|