Files
sbox-public/engine/Sandbox.Engine/Systems/Project/ProjectSettings/ProjectSettings.cs
Tony Ferguson 5a9c566730 Configurable GameObjectSystems (#3871)
* Project Settings moved to a window (child of main editor window)
* Can configure [Property] marked GameObjectSystem properties in Project Settings
* Moved Project menu options to the top right of the editor, added quick-access Project Settings button
2026-01-28 15:30:52 +00:00

74 lines
2.2 KiB
C#

using Sandbox.Audio;
namespace Sandbox;
public class ProjectSettings
{
/// <summary>
/// Get the <see cref="CollisionRules"/> from the active project settings.
/// </summary>
public static CollisionRules Collision => Get<CollisionRules>( "Collision.config" );
/// <summary>
/// Get the <see cref="Input"/> from the active project settings.
/// </summary>
public static InputSettings Input => Get<InputSettings>( "Input.config" );
/// <summary>
/// Get the <see cref="NetworkingSettings"/> from the active project settings.
/// </summary>
public static NetworkingSettings Networking => Get<NetworkingSettings>( "Networking.config" );
/// <summary>
/// Get the <see cref="MixerSettings"/> from the active project settings.
/// </summary>
internal static MixerSettings Mixer => Get<MixerSettings>( "Mixer.config" );
/// <summary>
/// Get the <see cref="CursorSettings"/> from the active project settings.
/// </summary>
internal static CursorSettings Cursor => Get<CursorSettings>( "Cursors.config" );
/// <summary>
/// Get the <see cref="PhysicsSettings"/> from the active project settings.
/// </summary>
public static PhysicsSettings Physics => Get<PhysicsSettings>( "Physics.config" );
/// <summary>
/// Get the <see cref="SystemsConfig"/> from the active project settings.
/// </summary>
public static SystemsConfig Systems => Get<SystemsConfig>( "Systems.config" );
/// <summary>
/// Reset any stored references to Project Settings.
/// </summary>
internal static void ClearCache()
{
_cache.Clear();
}
static Dictionary<string, ConfigData> _cache = new();
/// <summary>
/// Gets or creates a default version of this config data. You can safely call this multiple times
/// and it will return the same object. The cache is cleared automatically when the project changes,
/// or when it's hotloaded.
/// </summary>
public static T Get<T>( string filename ) where T : ConfigData, new()
{
if ( _cache.TryGetValue( filename, out var result ) && result is T t )
return t;
var txt = EngineFileSystem.ProjectSettings?.ReadAllText( BaseFileSystem.NormalizeFilename( filename ) );
var config = new T();
_cache[filename] = config;
if ( !string.IsNullOrEmpty( txt ) )
{
config.Deserialize( txt );
}
return config;
}
}