Skip to content

Commit

Permalink
Create IOpenDreamGameTiming
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyberboss committed Feb 10, 2024
1 parent 7dce998 commit 66dddbc
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 9 deletions.
5 changes: 2 additions & 3 deletions Content.Tests/ContentUnitTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
using System.Reflection;
using OpenDreamClient;
using OpenDreamRuntime;
using OpenDreamRuntime.Procs;
using OpenDreamRuntime.Rendering;
using OpenDreamShared;
using OpenDreamShared.Rendering;
using Robust.Shared.Analyzers;
using Robust.Shared.IoC;
Expand All @@ -23,11 +23,10 @@ public class ContentUnitTest : RobustUnitTest {
protected override void OverrideIoC() {
base.OverrideIoC();

SharedOpenDreamIoC.Register();

if (Project == UnitTestProject.Server) {
ServerContentIoC.Register(unitTests: true);
IoCManager.Register<IDreamMapManager, DummyDreamMapManager>();
IoCManager.Register<IOpenDreamGameTiming, DummyOpenDreamGameTiming>();
} else if (Project == UnitTestProject.Client) {
ClientContentIoC.Register();
}
Expand Down
12 changes: 11 additions & 1 deletion Content.Tests/DMTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@ public sealed class DMTests : ContentUnitTest {
private const string InitializeEnvironment = "./environment.dme";
private const string TestsDirectory = "Tests";

[Dependency] IOpenDreamGameTiming _gameTiming = default!;
[Dependency] private readonly DreamManager _dreamMan = default!;
[Dependency] private readonly DreamObjectTree _objectTree = default!;
[Dependency] private readonly ProcScheduler _procScheduler = default!;
[Dependency] private readonly ITaskManager _taskManager = default!;
[Dependency] private readonly IGameTiming gameTiming = default!;

DummyOpenDreamGameTiming GameTiming => (DummyOpenDreamGameTiming)_gameTiming;

[Flags]
public enum DMTestFlags {
Expand Down Expand Up @@ -74,17 +78,21 @@ public void TestFiles(string sourceFile, DMTestFlags testFlags) {
}

_procScheduler.ClearState();
GameTiming.CurTick = GameTick.Zero;

Assert.That(compiledFile is not null && File.Exists(compiledFile), "Failed to compile DM source file");
Assert.That(_dreamMan.LoadJson(compiledFile), $"Failed to load {compiledFile}");
_dreamMan.StartWorld();

(bool successfulRun, DreamValue? returned, Exception? exception) = RunTest();

int expectedThreads;
if (testFlags.HasFlag(DMTestFlags.NoReturn)) {
Assert.That(returned.HasValue, Is.False, "proc returned unexpectedly");
expectedThreads = 1;
} else {
Assert.That(returned.HasValue, "proc did not return (did it hit an exception?)");
expectedThreads = 0;
}

if (testFlags.HasFlag(DMTestFlags.RuntimeError)) {
Expand All @@ -101,7 +109,7 @@ public void TestFiles(string sourceFile, DMTestFlags testFlags) {
}

var threads = _procScheduler.InspectThreads().ToList();
Assert.That(threads.Count == 0 && !_procScheduler.HasProcsSleeping && !_procScheduler.HasProcsQueued, $"One or more threads did not finish!");
Assert.That(threads.Count == expectedThreads && !_procScheduler.HasProcsSleeping && !_procScheduler.HasProcsQueued, $"One or more threads did not finish!");

Cleanup(compiledFile);
TestContext.WriteLine($"--- PASS {sourceFile}");
Expand Down Expand Up @@ -136,6 +144,8 @@ public void TestFiles(string sourceFile, DMTestFlags testFlags) {
_dreamMan.Update();
_taskManager.ProcessPendingTasks();

GameTiming.CurTick = new GameTick(_gameTiming.CurTick.Value + 1);

if (watch.Elapsed.TotalSeconds > 5) {
Assert.Fail("Test timed out");
}
Expand Down
26 changes: 26 additions & 0 deletions Content.Tests/DummyOpenDreamGameTiming.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using OpenDreamRuntime.Procs;

using Robust.Shared.IoC;
using Robust.Shared.Timing;

using System;

namespace Content.Tests {
sealed class DummyOpenDreamGameTiming : IOpenDreamGameTiming {

[Dependency] IGameTiming gameTiming = null!;

public GameTick CurTick { get; set; }

public TimeSpan LastTick => gameTiming.LastTick;

public TimeSpan RealTime => gameTiming.RealTime;

public TimeSpan TickPeriod => gameTiming.TickPeriod;

public byte TickRate {
get => gameTiming.TickRate;
set => gameTiming.TickRate = value;
}
}
}
1 change: 0 additions & 1 deletion OpenDreamClient/Rendering/ClientImagesSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
namespace OpenDreamClient.Rendering;
internal sealed class ClientImagesSystem : SharedClientImagesSystem {
[Dependency] private readonly IEntityManager _entityManager = default!;
[Dependency] private readonly IGameTiming _gameTiming = default!;
[Dependency] private readonly ClientAppearanceSystem _appearanceSystem = default!;

private readonly Dictionary<Vector3, List<NetEntity>> _turfClientImages = new();
Expand Down
2 changes: 1 addition & 1 deletion OpenDreamRuntime/DreamManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public sealed partial class DreamManager {
[Dependency] private readonly ProcScheduler _procScheduler = default!;
[Dependency] private readonly DreamResourceManager _dreamResourceManager = default!;
[Dependency] private readonly ITaskManager _taskManager = default!;
[Dependency] private readonly IGameTiming _gameTiming = default!;
[Dependency] private readonly IOpenDreamGameTiming _gameTiming = default!;
[Dependency] private readonly DreamObjectTree _objectTree = default!;
[Dependency] private readonly IEntityManager _entityManager = default!;
[Dependency] private readonly IEntitySystemManager _entitySystemManager = default!;
Expand Down
5 changes: 3 additions & 2 deletions OpenDreamRuntime/Objects/Types/DreamObjectWorld.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Net;
using System.Net.Sockets;

using OpenDreamRuntime.Procs;
using OpenDreamRuntime.Procs.Native;
using OpenDreamRuntime.Resources;
using OpenDreamShared;
Expand All @@ -8,15 +10,14 @@
using Robust.Shared;
using Robust.Shared.Configuration;
using Robust.Shared.Network;
using Robust.Shared.Timing;

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 IOpenDreamGameTiming _gameTiming = default!;
[Dependency] private readonly INetManager _netManager = default!;
[Dependency] private readonly IConfigurationManager _cfg = default!;

Expand Down
15 changes: 15 additions & 0 deletions OpenDreamRuntime/Procs/IOpenDreamGameTiming.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Robust.Shared.Timing;

namespace OpenDreamRuntime.Procs {
public interface IOpenDreamGameTiming {
public GameTick CurTick { get; }

public TimeSpan LastTick { get; }

public TimeSpan RealTime { get; }

public TimeSpan TickPeriod { get; }

public byte TickRate { get; set; }
}
}
20 changes: 20 additions & 0 deletions OpenDreamRuntime/Procs/OpenDreamGameTiming.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Robust.Shared.Timing;

namespace OpenDreamRuntime.Procs {
sealed class OpenDreamGameTiming : IOpenDreamGameTiming {
[Dependency] private readonly IGameTiming _gameTiming = default!;

public GameTick CurTick => _gameTiming.CurTick;

public TimeSpan LastTick => _gameTiming.LastTick;

public TimeSpan RealTime => _gameTiming.RealTime;

public TimeSpan TickPeriod => _gameTiming.TickPeriod;

public byte TickRate {
get => _gameTiming.TickRate;
set => _gameTiming.TickRate = value;
}
}
}
2 changes: 1 addition & 1 deletion OpenDreamRuntime/Procs/ProcScheduler.Delays.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace OpenDreamRuntime.Procs;
// Handles delay processing for sleep() and spawn().

public sealed partial class ProcScheduler {
[Dependency] private readonly IGameTiming _gameTiming = default!;
[Dependency] private readonly IOpenDreamGameTiming _gameTiming = default!;

private PriorityQueue<DelayTicker, uint> _tickers = new();

Expand Down
1 change: 1 addition & 0 deletions OpenDreamRuntime/ServerContentIoC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public static void Register(bool unitTests = false) {
if (!unitTests) {
// Unit tests use their own version
IoCManager.Register<IDreamMapManager, DreamMapManager>();
IoCManager.Register<IOpenDreamGameTiming, OpenDreamGameTiming>();
}
}
}
Expand Down

0 comments on commit 66dddbc

Please sign in to comment.