Files
sbox-public/engine/Sandbox.AppSystem/StandaloneAppSystem.cs
Lorenz Junglas 6808d8768e Shutdown fixes (#3553)
* Stop generating solutions via -test flag add -generatesolution

* Add TestAppSystem remove Application.InitUnitTest

Avoids some hacks and also makes sure our tests are as close to a real AppSystem as possible.

* Add shutdown unit test

shuts down an re-inits the engine

* Properly dispose native resources hold by managed during shutdown

Should fix a bunch of crashes

* Fix filesystem and networking tests

* StandaloneTest does proper Game Close

* Make sure package tests clean up properly

* Make sure menu scene and resources are released on shutdown

* Report leaked scenes on shutdown

* Ensure DestroyImmediate is not used on scenes

* Fix unmounting in unit tests not clearing native refs

* Force destroy native resource on ResourceLib Clear
2025-12-08 15:55:11 +01:00

77 lines
1.7 KiB
C#

using Sandbox.Engine;
using System.IO;
using System.Text.Json;
using System.Threading.Tasks;
namespace Sandbox;
public class StandaloneAppSystem : AppSystem
{
private StandaloneManifest LoadManifest()
{
// Load game info from file
var manifestPath = Path.Combine( Standalone.GamePath, Standalone.ManifestName );
var manifestContents = File.ReadAllText( manifestPath );
var properties = JsonSerializer.Deserialize<StandaloneManifest>( manifestContents );
return properties;
}
public override void Init()
{
LoadSteamDll();
base.Init();
// Standalone setup
Standalone.SetupFromManifest( LoadManifest() );
Application.IsStandalone = true;
Application.AppId = Standalone.Manifest.AppId;
CreateGame();
var createInfo = new AppSystemCreateInfo()
{
WindowTitle = Standalone.Manifest.Name,
Flags = AppSystemFlags.IsGameApp | AppSystemFlags.IsStandaloneGame
};
if ( Utility.CommandLine.HasSwitch( "-headless" ) )
createInfo.Flags |= AppSystemFlags.IsConsoleApp;
InitGame( createInfo );
LoadStandaloneGame();
}
private Task _standaloneLoadTask;
private void LoadStandaloneGame()
{
_standaloneLoadTask = IGameInstanceDll.Current.LoadGamePackageAsync( Standalone.Manifest.Ident, GameLoadingFlags.Host, default );
}
protected override bool RunFrame()
{
EngineLoop.RunFrame( _appSystem, out bool wantsToQuit );
// Still loading
if ( _standaloneLoadTask is not null )
{
if ( _standaloneLoadTask.IsCompleted )
{
_standaloneLoadTask.GetAwaiter().GetResult();
_standaloneLoadTask = null;
}
}
// Quit next loop after load, if we are testing
else if ( Utility.CommandLine.HasSwitch( "-test-standalone" ) )
{
Game.Close();
}
return !wantsToQuit;
}
}