Files
sbox-public/engine/Sandbox.Engine/Resources/Model/Model.Static.cs
s&box team 71f266059a Open source release
This commit imports the C# engine code and game files, excluding C++ source code.

[Source-Commit: ceb3d758046e50faa6258bc3b658a30c97743268]
2025-11-24 09:05:18 +00:00

69 lines
1.7 KiB
C#

using NativeEngine;
namespace Sandbox;
public partial class Model
{
internal static readonly Dictionary<long, WeakReference<Model>> Loaded = new();
/// <summary>
/// Cached <see cref="Model"/> instance from native, or creates
/// </summary>
internal static Model FromNative( IModel native, bool procedural = false, string name = null )
{
if ( native.IsNull || !native.IsStrongHandleValid() )
return null;
var instanceId = native.GetBindingPtr().ToInt64();
if ( NativeResourceCache.TryGetValue<Model>( instanceId, out var model ) )
{
// If we're using a cached one we don't need this handle, we'll leak
native.DestroyStrongHandle();
return model;
}
model = new Model( native, name ?? native.GetModelName(), procedural );
NativeResourceCache.Add( instanceId, model );
// Keeping this because some legacy game loop depends on this for logic, can't be fucked solving for legacy.
Loaded[instanceId] = new WeakReference<Model>( model );
return model;
}
/// <summary>
/// Returns a static <see cref="ModelBuilder"/> instance, allowing for runtime model creation.
/// </summary>
public static ModelBuilder Builder => new();
/// <summary>
/// A cube model
/// </summary>
public static Model Cube { get; internal set; }
/// <summary>
/// A sphere model
/// </summary>
public static Model Sphere { get; internal set; }
/// <summary>
/// A plane model
/// </summary>
public static Model Plane { get; internal set; }
/// <summary>
/// An error model
/// </summary>
public static Model Error { get; internal set; }
internal static void Init()
{
Cube = Load( "models/dev/box.vmdl" );
Sphere = Load( "models/dev/sphere.vmdl" );
Plane = Load( "models/dev/plane.vmdl" );
Error = Load( "models/dev/error.vmdl" );
}
}