Files
sbox-public/engine/Sandbox.Engine/Core/GlobalContext/GlobalContext.Static.cs
Tony Ferguson a1cf5f72a2 See if AsyncLocal for GlobalContext fixes context getting dropped in an async task (#3700)
Should resolve Dresser getting confused about context, failing to find clothing

Fixes #3685, Fixes Facepunch/sbox-issues#9672
2026-01-09 10:04:33 +00:00

48 lines
1.2 KiB
C#

using System.Threading;
namespace Sandbox.Engine;
internal partial class GlobalContext
{
private static readonly AsyncLocal<GlobalContext> _current = new AsyncLocal<GlobalContext>();
/// <summary>
/// The context used for the menu system
/// </summary>
public static GlobalContext Menu;
/// <summary>
/// The context used for the game. This is the default context.
/// </summary>
public static GlobalContext Game;
/// <summary>
/// The current active context
/// </summary>
public static GlobalContext Current
{
get => _current.Value ?? Game;
set => _current.Value = value;
}
/// <summary>
/// The global context for the game, which holds references to various systems and libraries used throughout the game.
/// </summary>
static GlobalContext()
{
Game = new GlobalContext();
Menu = new GlobalContext();
_current.Value = Game;
}
/// <summary>
/// Throws an exception when called from client or server.
/// </summary>
public static void AssertMenu( [System.Runtime.CompilerServices.CallerMemberName] string memberName = "" )
{
if ( Current != Menu )
throw new System.Exception( $"{memberName} should only be called in Menu scope!" );
}
}