Files
sbox-public/engine/Sandbox.Engine/Game/Mount/SceneLoader.cs
Sol Williams b0747c1311 Mounts: map thumbs (#5472)
* SceneLoader can provide thumbs (a custom override or from disk)

* mount_generatethumb(s) concmds - writes mount scene thumb to disk on the correct path

* Quake: tag info_intermission as map_preview

* quake map thumbs

* help is backwards whoops

* guard against disposing the singleton ffs
2026-07-28 21:39:58 +01:00

86 lines
2.0 KiB
C#

using Sandbox.Engine;
namespace Sandbox.Mounting;
/// <summary>
/// Base class for a mount loader that produces a <see cref="SceneFile"/> built in code.
/// Override <see cref="BuildScene"/> and create GameObjects - they become part of the
/// mounted scene. Registration and teardown are handled for you.
/// </summary>
public abstract class SceneLoader<T> : ResourceLoader<T>, IThumbnailProvider where T : BaseGameMount
{
public Texture Thumbnail
{
get
{
if ( !_thumbnail.IsValid() )
{
_thumbnail = GetThumbnail()
?? Texture.LoadFromFileSystem( $"/thumbs/{Host.Ident}/{RelativePath.WithExtension( ".png" )}", GlobalContext.Menu.FileMount, false )
?? Texture.Invalid;
}
if ( ReferenceEquals( _thumbnail, Texture.Invalid ) )
return null;
return _thumbnail;
}
}
SceneFile _scene;
Texture _thumbnail;
protected sealed override object Load()
{
var scene = new Scene();
scene.SetDeterministicId( (Path ?? string.Empty).ToGuid() );
using ( scene.Push() )
{
BuildScene();
}
_scene = GetOrRegister( Path );
scene.ToSceneFile( _scene );
scene.Destroy();
_scene.PostLoadInternal();
return _scene;
}
/// <summary>
/// Build the scene contents. Any GameObjects created here become part of the mounted scene.
/// </summary>
protected abstract void BuildScene();
/// <summary>
/// Load or create the thumbnail image for this scene, the result is cached.
/// </summary>
protected virtual Texture GetThumbnail() { return null; }
protected sealed override void Shutdown()
{
_scene?.DestroyInternal();
_scene = null;
if ( !ReferenceEquals( _thumbnail, Texture.Invalid ) )
_thumbnail?.Destroy();
_thumbnail = null;
}
static SceneFile GetOrRegister( string path )
{
var resourcePath = path ?? string.Empty;
var fixedPath = Resource.FixPath( resourcePath );
var scene = Game.Resources.GetByIdLong<SceneFile>( fixedPath.FastHash64() );
if ( scene is not null )
return scene;
scene = new SceneFile();
scene.Register( resourcePath );
return scene;
}
}