Skip to content

Commit

Permalink
Shutdown the world at the end of the tick like BYOND does
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyberboss committed Jul 20, 2024
1 parent b8ada05 commit 6d4997c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
15 changes: 15 additions & 0 deletions OpenDreamRuntime/DreamManager.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Threading;

using DMCompiler.Bytecode;
using DMCompiler.Json;
using OpenDreamRuntime.Objects;
Expand Down Expand Up @@ -33,6 +35,7 @@ public sealed partial class DreamManager {
[Dependency] private readonly IEntitySystemManager _entitySystemManager = default!;
[Dependency] private readonly IStatusHost _statusHost = default!;
[Dependency] private readonly IDependencyCollection _dependencyCollection = default!;
[Dependency] private readonly IBaseServer _server = default!;

private ServerAppearanceSystem? _appearanceSystem;

Expand All @@ -59,6 +62,8 @@ public sealed partial class DreamManager {

private ISawmill _sawmill = default!;

private string? _queuedShutdownReason;

//TODO This arg is awful and temporary until RT supports cvar overrides in unit tests
public void PreInitialize(string? jsonPath) {
_sawmill = Logger.GetSawmill("opendream");
Expand Down Expand Up @@ -99,6 +104,12 @@ public void Update() {
return;

_procScheduler.Process();
var shutdownReason = Interlocked.Exchange(ref _queuedShutdownReason, null);
if (shutdownReason != null) {
_server.Shutdown(shutdownReason);
return;
}

UpdateStat();
_dreamMapManager.UpdateTiles();
DreamObjectSavefile.FlushAllUpdates();
Expand Down Expand Up @@ -340,6 +351,10 @@ public void HandleException(Exception e, string msg = "", string file = "", int

WorldInstance.SpawnProc("Error", usr: null, new DreamValue(obj));
}

public void QueueShutdown(string reason) {
_queuedShutdownReason = reason;
}
}

public enum RefType : uint {
Expand Down
4 changes: 1 addition & 3 deletions OpenDreamRuntime/Objects/Types/DreamObjectWorld.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using OpenDreamRuntime.Resources;
using OpenDreamShared;
using OpenDreamShared.Dream;
using Robust.Server;
using Robust.Shared;
using Robust.Shared.Configuration;
using Robust.Shared.Network;
Expand All @@ -15,7 +14,6 @@ namespace OpenDreamRuntime.Objects.Types;
public sealed class DreamObjectWorld : DreamObject {
public override bool ShouldCallNew => false; // Gets called manually later

[Dependency] private readonly IBaseServer _server = default!;
[Dependency] private readonly IGameTiming _gameTiming = default!;
[Dependency] private readonly INetManager _netManager = default!;
[Dependency] private readonly IConfigurationManager _cfg = default!;
Expand Down Expand Up @@ -88,7 +86,7 @@ public DreamObjectWorld(DreamObjectDefinition objectDefinition) :
protected override void HandleDeletion() {
base.HandleDeletion();

_server.Shutdown("world was deleted");
DreamManager.QueueShutdown("world was deleted");
}

protected override bool TryGetVar(string varName, out DreamValue value) {
Expand Down
5 changes: 1 addition & 4 deletions OpenDreamRuntime/Procs/Native/DreamProcNativeWorld.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using Byond.TopicSender;
using OpenDreamRuntime.Objects;
using OpenDreamRuntime.Objects.Types;
using Robust.Server;

namespace OpenDreamRuntime.Procs.Native;

Expand Down Expand Up @@ -164,9 +163,7 @@ public static DreamValue NativeProc_Profile(NativeProc.Bundle bundle, DreamObjec
[DreamProc("Reboot")]
[DreamProcParameter("reason", Type = DreamValue.DreamValueTypeFlag.Float)]
public static DreamValue NativeProc_Reboot(NativeProc.Bundle bundle, DreamObject? src, DreamObject? usr) {
var server = IoCManager.Resolve<IBaseServer>();

server.Shutdown("/world.Reboot() was called but restarting is very broken");
bundle.DreamManager.QueueShutdown("/world.Reboot() was called but restarting is very broken");
return DreamValue.Null;
}

Expand Down

0 comments on commit 6d4997c

Please sign in to comment.