GameSettingsSystem.cs

Game settings container and static settings system. GameSettings holds audio, gameplay and debug toggles. GameSettingsSystem exposes a singleton Current, loads/saves the settings JSON via FileSystem.Data and copies runtime values from a Manager and CrosshairComponent before writing.

File Access
using System;
using Sandbox;
using Sandbox.Audio;

public class GameSettings
{
	public float MasterVolume { get; set; } = 70f;
	public float MusicVolume { get; set; } = 45f;
	public float SfxVolume { get; set; } = 100f;
	public float CrosshairScale { get; set; } = 1f;

	public bool DontSpawnRandomEnemies { get; set; } = true;
	public bool GodMode { get; set; } = false;
	public int Difficulty { get; set; }
	public bool LaunchEnemies { get; set; } = false;
	public bool PlayerMaxDmg { get; set; } = false;

	public bool DebugBulletBounce { get; set; } = false;
	public bool DebugBulletPierce { get; set; } = false;
	public bool DebugBulletSplash { get; set; } = false;
	public bool IsOrthoCamera { get; set; } = false;

	public bool DontConfirmRestart { get; set; } = false;
	public int LobbyPrivacy { get; set; } = 0;
	public bool DisableChat { get; set; } = false;

	public bool LeaderboardShowFriends { get; set; } = false;
}

public partial class GameSettingsSystem
{
	private static GameSettings current { get; set; }
	public static GameSettings Current
	{
		get
		{
			if ( current is null ) Load();
			return current;
		}
		set
		{
			current = value;
		}
	}

	public static string FilePath => "ss2_settings.json";

	public static void Save()
	{
		Current.DontSpawnRandomEnemies = Manager.Instance.DontSpawnRandomEnemies;
		Current.GodMode = Manager.Instance.GodMode;

		Current.Difficulty = Manager.Instance.Difficulty;

		Current.LaunchEnemies = Manager.Instance.LaunchEnemies;
		Current.PlayerMaxDmg = Manager.Instance.PlayerMaxDmg;

		Current.DebugBulletBounce = Manager.Instance.DebugBulletBounce;
		Current.DebugBulletPierce = Manager.Instance.DebugBulletPierce;
		Current.DebugBulletSplash = Manager.Instance.DebugBulletSplash;

		Current.IsOrthoCamera = Manager.Instance.IsOrthoCamera;

		Current.DontConfirmRestart = Manager.Instance.DontConfirmRestart;
		Current.LobbyPrivacy = Manager.Instance.LobbyPrivacy;

		Current.LeaderboardShowFriends = Manager.Instance.LeaderboardShowFriends;

		var crosshair = Manager.Instance?.Components.Get<CrosshairComponent>();
		if ( crosshair != null )
			Current.CrosshairScale = crosshair.Scale;

		//Mixer.Master.Volume = Current.MasterVolume;
		//var channel = Mixer.Master.GetChildren();
		//channel[0].Volume = Current.MusicVolume;
		//channel[1].Volume = Current.SFXVolume;
		//channel[2].Volume = Current.UIVolume;
		//channel[3].Volume = Current.NPCVolume;

		FileSystem.Data.WriteJson<GameSettings>( FilePath, Current );
	}

	public static void Load()
	{
		Current = FileSystem.Data.ReadJsonSafe<GameSettings>( FilePath, new() );
	}
}