static helper
using System;
using System.IO;
using System.Linq;
using Editor;
using Sandbox;
namespace LevelTools;
// The first-party tools keep equivalents of these private to their own classes, so this library carries its own copy
internal static class McpHelpers
{
internal static SceneEditorSession ActiveSession()
{
return SceneEditorSession.Active ?? throw new Exception( "No scene is open in the editor" );
}
// Undo and unsaved changes belong to the session that owns the object, not whichever tab is active
internal static SceneEditorSession SessionOf( GameObject go )
{
return go.Scene?.Editor as SceneEditorSession ?? ActiveSession();
}
// Read-only tools also work on the running game's scene when no editor session is active
internal static Scene ReadScene()
{
return SceneEditorSession.Active?.Scene ?? Game.ActiveScene ?? throw new Exception( "No scene is open in the editor" );
}
// Active session first, so an id shared by duplicated scenes means the object in the tab being looked at
internal static GameObject FindByGuid( string id )
{
if ( !Guid.TryParse( id, out var guid ) )
throw new Exception( $"'{id}' isn't a guid - scene_tree and find_game_objects show object ids" );
foreach ( var session in SceneEditorSession.All.OrderBy( x => x != SceneEditorSession.Active ) )
{
var found = session.Scene?.Directory?.FindByGuid( guid );
// The scene registers itself in its own directory; a tool handed its id would act on the whole scene
if ( found is Scene )
throw new Exception( $"{guid} is the scene itself, not a game object in it - scene_tree shows object ids" );
if ( found is not null ) return found;
}
throw new Exception( $"No game object {guid} in any open scene" );
}
// Engine JSON accepts NaN and infinity, and every comparison with NaN is false, so no range check stops them
internal static void RequireFinite( string name, params float[] values )
{
if ( !values.All( float.IsFinite ) )
throw new Exception( $"{name} must be finite - NaN and infinity aren't allowed" );
}
internal static void RequirePositive( string name, Vector3 v )
{
RequireFinite( name, v.x, v.y, v.z );
if ( v.x <= 0 || v.y <= 0 || v.z <= 0 )
throw new Exception( $"{name} {v} needs every component above zero" );
}
// Tool-supplied paths may only reach files inside the open project
internal static string ProjectPath( string path )
{
var root = Project.Current?.RootDirectory?.FullName ?? throw new Exception( "No project is currently open" );
return ResolveInside( root, path, "the project folder", "heightmaps/island.png" );
}
// Prefab paths are relative to Assets, where the asset system registers them
internal static string AssetsPath( string path )
{
var root = Project.Current?.GetAssetsPath() ?? throw new Exception( "No project is currently open" );
var full = ResolveInside( root, path, "the Assets folder", "prefabs/crate.prefab" );
if ( !full.EndsWith( ".prefab", StringComparison.OrdinalIgnoreCase ) )
throw new Exception( $"'{path}' isn't a .prefab file - give a path like 'prefabs/crate.prefab'" );
return full;
}
internal static string ResolveInside( string root, string path, string rootName, string example )
{
if ( string.IsNullOrWhiteSpace( path ) )
throw new Exception( $"Give a path relative to {rootName}, e.g. '{example}'" );
root = Path.TrimEndingDirectorySeparator( Path.GetFullPath( root ) ) + Path.DirectorySeparatorChar;
var full = Path.GetFullPath( Path.Combine( root, path.Replace( '\\', '/' ).TrimStart( '/' ) ) );
if ( !full.StartsWith( root, StringComparison.OrdinalIgnoreCase ) )
throw new Exception( $"'{path}' resolves outside {rootName} - give a path relative to it, e.g. '{example}'" );
return full;
}
}