Skip to content

Commit 881b84f

Browse files
committed
lint
1 parent 3a636bd commit 881b84f

File tree

9 files changed

+70
-60
lines changed

9 files changed

+70
-60
lines changed

DMCompiler/Bytecode/DreamProcOpcode.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ public enum DreamProcOpcode : byte {
309309
Animate = 0x9C,
310310
[OpcodeMetadata(-1)]
311311
Sleep = 0x9D,
312-
[OpcodeMetadata(0)]
312+
[OpcodeMetadata]
313313
BackgroundSleep = 0x9E,
314314
}
315315
// ReSharper restore MissingBlankLines

DMCompiler/Compiler/DM/DMParser.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,7 +1333,7 @@ private DMASTProcStatement For() {
13331333
DMASTExpression? expr3 = Expression();
13341334
DMASTProcStatement? statement3 = null;
13351335
if (expr3 == null) {
1336-
CheckForStatementIncrementor(ref expr3, ref statement3);
1336+
CheckForStatementIncrementor();
13371337

13381338
if (Current().Type != TokenType.DM_RightParenthesis) {
13391339
Emit(WarningCode.BadExpression, "Expected 3nd expression in for");
@@ -1345,7 +1345,7 @@ private DMASTProcStatement For() {
13451345

13461346
return new DMASTProcStatementFor(loc, expr1, expr2, expr3, statement3, dmTypes, GetForBody());
13471347

1348-
void CheckForStatementIncrementor(ref DMASTExpression? expr3, ref DMASTProcStatement? statement3) {
1348+
void CheckForStatementIncrementor() {
13491349
DMASTProcStatementSleep? sleep = Sleep();
13501350
if (sleep == null) {
13511351
expr3 = new DMASTConstantNull(loc);

DMCompiler/DM/Builders/DMProcBuilder.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ private void ProcessStatementDel(DMASTProcStatementDel statementDel) {
146146
private void ProcessStatementSleep(DMASTProcStatementSleep statementSleep) {
147147

148148
var expr = _exprBuilder.Create(statementSleep.Delay);
149+
149150
if (expr.TryAsConstant(compiler, out var constant)) {
150151
if (constant is Number constantNumber) {
151152
proc.Sleep(constantNumber.Value);
@@ -325,17 +326,11 @@ private void ProcessStatementFor(DMASTProcStatementFor statementFor) {
325326
}
326327

327328
ProcessStatementForList(list, keyVar, valueVar, statementFor.DMTypes, statementFor.Body);
328-
} else if (statementFor.Expression2 != null || statementFor.Expression3 != null && statementFor.Statement3 == null) {
329-
var initializer = statementFor.Expression1 != null ? _exprBuilder.Create(statementFor.Expression1) : null;
330-
var comparator = statementFor.Expression2 != null ? _exprBuilder.Create(statementFor.Expression2) : null;
331-
var incrementor = statementFor.Expression3 != null ? _exprBuilder.Create(statementFor.Expression3) : null;
332-
333-
ProcessStatementForStandard(initializer, comparator, incrementor, statementFor.Body);
334-
} else if (statementFor.Expression2 != null || statementFor.Expression3 == null && statementFor.Statement3 != null) {
329+
} else if (statementFor.Statement3 != null) {
335330
var initializer = statementFor.Expression1 != null ? _exprBuilder.Create(statementFor.Expression1) : null;
336331
var comparator = statementFor.Expression2 != null ? _exprBuilder.Create(statementFor.Expression2) : null;
337332

338-
Action statementHandler = null;
333+
Action? statementHandler = null;
339334
switch (statementFor.Statement3) {
340335
case DMASTProcStatementSleep sleepStatement: {
341336
statementHandler = () => {
@@ -350,6 +345,12 @@ private void ProcessStatementFor(DMASTProcStatementFor statementFor) {
350345
}
351346

352347
ProcessStatementForWithStatementInc(initializer, comparator, statementHandler, statementFor.Body);
348+
} else if (statementFor.Expression2 != null || statementFor.Expression3 != null) {
349+
var initializer = statementFor.Expression1 != null ? _exprBuilder.Create(statementFor.Expression1) : null;
350+
var comparator = statementFor.Expression2 != null ? _exprBuilder.Create(statementFor.Expression2) : null;
351+
var incrementor = statementFor.Expression3 != null ? _exprBuilder.Create(statementFor.Expression3) : null;
352+
353+
ProcessStatementForStandard(initializer, comparator, incrementor, statementFor.Body);
353354
} else {
354355
switch (statementFor.Expression1) {
355356
case DMASTAssign {LHS: DMASTVarDeclExpression decl, RHS: DMASTExpressionInRange range}: {
Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
namespace OpenDreamRuntime.Procs {
2-
public abstract class AsyncProcState : ProcState {
3-
#if TOOLS
4-
public override (string SourceFile, int Line) TracyLocationId => ("Async Native Proc", 0);
5-
#endif
6-
public abstract void SafeResume();
7-
}
1+
namespace OpenDreamRuntime.Procs;
2+
public abstract class AsyncProcState : ProcState {
3+
#if TOOLS
4+
public override (string SourceFile, int Line) TracyLocationId => ("Async Native Proc", 0);
5+
#endif
6+
public abstract void SafeResume();
87
}

OpenDreamRuntime/Procs/DMOpcodeHandlers.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1865,12 +1865,12 @@ public static ProcStatus Spawn(DMProcState state) {
18651865
// and have state.Spawn return a ProcState instead
18661866
DreamThread newContext = state.Spawn();
18671867

1868-
async void Wait() {
1868+
async Task Wait() {
18691869
await state.ProcScheduler.CreateDelay(delay, state.Proc.Id, state.Thread.Id);
18701870
newContext.Resume();
18711871
}
18721872

1873-
Wait();
1873+
_ = Wait();
18741874
state.Jump(jumpTo);
18751875
return ProcStatus.Continue;
18761876
}
@@ -1901,11 +1901,10 @@ static ProcStatus SleepCore(DMProcState state, Task delay) {
19011901
sealed class SleepState : AsyncProcState {
19021902
public static readonly Stack<SleepState> Pool = new();
19031903

1904-
[Dependency] public readonly ProcScheduler ProcScheduler = null!;
1904+
[Dependency] private readonly ProcScheduler ProcScheduler = null!;
19051905

19061906
DreamProc? _proc;
19071907
Task? _task;
1908-
bool inResume;
19091908

19101909
public SleepState() {
19111910
IoCManager.InjectDependencies(this);
Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
using Robust.Shared.Timing;
22

3-
namespace OpenDreamRuntime.Procs {
4-
public interface IOpenDreamGameTiming {
5-
public GameTick CurTick { get; }
3+
namespace OpenDreamRuntime.Procs;
4+
public interface IOpenDreamGameTiming {
5+
public GameTick CurTick { get; }
66

7-
public TimeSpan LastTick { get; }
7+
public TimeSpan LastTick { get; }
88

9-
public TimeSpan RealTime { get; }
9+
public TimeSpan RealTime { get; }
1010

11-
public TimeSpan TickPeriod { get; }
11+
public TimeSpan TickPeriod { get; }
1212

13-
public ushort TickRate { get; set; }
14-
}
13+
public ushort TickRate { get; set; }
1514
}
Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
using Robust.Shared.Timing;
22

3-
namespace OpenDreamRuntime.Procs {
4-
sealed class OpenDreamGameTiming : IOpenDreamGameTiming {
5-
[Dependency] private readonly IGameTiming _gameTiming = default!;
3+
namespace OpenDreamRuntime.Procs;
4+
sealed internal class OpenDreamGameTiming : IOpenDreamGameTiming {
5+
[Dependency] private readonly IGameTiming _gameTiming = default!;
66

7-
public GameTick CurTick => _gameTiming.CurTick;
7+
public GameTick CurTick => _gameTiming.CurTick;
88

9-
public TimeSpan LastTick => _gameTiming.LastTick;
9+
public TimeSpan LastTick => _gameTiming.LastTick;
1010

11-
public TimeSpan RealTime => _gameTiming.RealTime;
11+
public TimeSpan RealTime => _gameTiming.RealTime;
1212

13-
public TimeSpan TickPeriod => _gameTiming.TickPeriod;
13+
public TimeSpan TickPeriod => _gameTiming.TickPeriod;
1414

15-
public ushort TickRate {
16-
get => _gameTiming.TickRate;
17-
set => _gameTiming.TickRate = value;
18-
}
15+
public ushort TickRate {
16+
get => _gameTiming.TickRate;
17+
set => _gameTiming.TickRate = value;
1918
}
2019
}

OpenDreamRuntime/Procs/ProcScheduler.Delays.cs

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,18 @@ namespace OpenDreamRuntime.Procs;
77

88
public sealed partial class ProcScheduler {
99
private sealed class ThreadYieldTracker {
10-
public uint threadYieldCounter = 1;
11-
public Dictionary<int, uint> procYieldCounter = new();
10+
public uint ThreadYieldCounter = 1;
11+
public readonly Dictionary<int, uint> ProcYieldCounter = new();
1212
}
13+
1314
[Dependency] private readonly IOpenDreamGameTiming _gameTiming = default!;
1415

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

17-
private Dictionary<int, ThreadYieldTracker> YieldTrackersByThread = new();
18+
private readonly Dictionary<int, ThreadYieldTracker> _yieldTrackersByThread = new();
1819

19-
private readonly ushort YieldProcThreshold = 10;
20-
private readonly ushort YieldThreadThreshold = 20;
20+
private readonly ushort _yieldProcThreshold = 10;
21+
private readonly ushort _yieldThreadThreshold = 20;
2122

2223
// This is for deferred tasks that need to fire in the current tick.
2324
private readonly Queue<TaskCompletionSource> _deferredTasks = new();
@@ -29,6 +30,12 @@ private sealed class ThreadYieldTracker {
2930
/// <param name="deciseconds">
3031
/// The amount of time, in deciseconds, to sleep. Gets rounded down to a number of ticks.
3132
/// </param>
33+
/// <param name="procId">
34+
/// ID of active process to track sharing
35+
/// </param>
36+
/// <param name="threadId">
37+
/// ID of active thread to track sharing
38+
/// </param>
3239
public Task CreateDelay(float deciseconds, int procId, int threadId) {
3340
// BYOND stores sleep/spawn delays with an exact amount of ticks.
3441
// Yes, this means that if you change world.fps/tick_lag while sleeping,
@@ -48,6 +55,12 @@ public Task CreateDelay(float deciseconds, int procId, int threadId) {
4855
/// <param name="ticks">
4956
/// The amount of ticks to sleep.
5057
/// </param>
58+
/// <param name="procId">
59+
/// ID of active process to track sharing
60+
/// </param>
61+
/// <param name="threadId">
62+
/// ID of active thread to track sharing
63+
/// </param>
5164
public Task CreateDelayTicks(int ticks, int procId, int threadId) {
5265
// When the delay is <= zero, we should run again in the current tick.
5366
// Now, BYOND apparently does have a difference between 0 and -1. See https://github.com/OpenDreamProject/OpenDream/issues/1262#issuecomment-1563663041
@@ -57,27 +70,27 @@ public Task CreateDelayTicks(int ticks, int procId, int threadId) {
5770
// In simple testing, this tends to amount to the same as one thread sleeping with -1 twenty times in one tick or a single proc ten times.
5871

5972
if (ticks < 0) {
60-
var yieldTracker = YieldTrackersByThread.GetValueOrDefault(threadId, new());
61-
uint sleepCountByThread = yieldTracker.threadYieldCounter;
62-
uint sleepCountByProc = yieldTracker.procYieldCounter.GetValueOrDefault(procId, 1u);
73+
var yieldTracker = _yieldTrackersByThread.GetValueOrDefault(threadId, new());
74+
uint sleepCountByThread = yieldTracker.ThreadYieldCounter;
75+
uint sleepCountByProc = yieldTracker.ProcYieldCounter.GetValueOrDefault(procId, 1u);
6376

6477
bool exceeded = false;
65-
if (sleepCountByThread < YieldThreadThreshold) {
66-
yieldTracker.threadYieldCounter = sleepCountByThread + 1u;
78+
if (sleepCountByThread < _yieldThreadThreshold) {
79+
yieldTracker.ThreadYieldCounter = sleepCountByThread + 1u;
6780
} else {
6881
exceeded = true;
6982
}
7083

71-
if (sleepCountByProc < YieldProcThreshold) {
72-
yieldTracker.procYieldCounter[procId] = sleepCountByProc + 1u;
84+
if (sleepCountByProc < _yieldProcThreshold) {
85+
yieldTracker.ProcYieldCounter[procId] = sleepCountByProc + 1u;
7386
} else {
7487
exceeded = true;
7588
}
7689

7790
if (exceeded) {
78-
YieldTrackersByThread.Remove(threadId);
91+
_yieldTrackersByThread.Remove(threadId);
7992
} else {
80-
YieldTrackersByThread[threadId] = yieldTracker;
93+
_yieldTrackersByThread[threadId] = yieldTracker;
8194
return Task.CompletedTask;
8295
}
8396
}
@@ -104,7 +117,7 @@ private void InsertTask(DelayTicker ticker) {
104117
}
105118

106119
private void UpdateDelays() {
107-
YieldTrackersByThread.Clear();
120+
_yieldTrackersByThread.Clear();
108121

109122
while (_tickers.Count > 0) {
110123
var ticker = _tickers.Peek();

OpenDreamRuntime/WalkManager.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public void StartWalk(DreamObjectMovable movable, int dir, int lag, int speed) {
4343
var moveProc = movable.GetProc("Move");
4444

4545
while (true) {
46-
await _scheduler.CreateDelayTicks(lag, state.Proc.Id, state.Thread.Id);
46+
await _scheduler.CreateDelayTicks(lag, state.Proc!.Id, state.Thread.Id);
4747
if (cancelSource.IsCancellationRequested)
4848
break;
4949

@@ -70,7 +70,7 @@ public void StartWalkRand(DreamObjectMovable movable, int lag, int speed) { // T
7070
var moveProc = movable.GetProc("Move");
7171

7272
while (true) {
73-
await _scheduler.CreateDelayTicks(lag, state.Proc.Id, state.Thread.Id);
73+
await _scheduler.CreateDelayTicks(lag, state.Proc!.Id, state.Thread.Id);
7474
if (cancelSource.IsCancellationRequested)
7575
break;
7676
var dir = DreamProcNativeHelpers.GetRandomDirection(_dreamManager);
@@ -97,7 +97,7 @@ public void StartWalkTowards(DreamObjectMovable movable, DreamObjectAtom target,
9797
var moveProc = movable.GetProc("Move");
9898

9999
while (true) {
100-
await _scheduler.CreateDelayTicks(lag, state.Proc.Id, state.Thread.Id);
100+
await _scheduler.CreateDelayTicks(lag, state.Proc!.Id, state.Thread.Id);
101101
if (cancelSource.IsCancellationRequested)
102102
break;
103103

@@ -128,7 +128,7 @@ public void StartWalkTo(DreamObjectMovable movable, DreamObjectAtom target, int
128128
var moveProc = movable.GetProc("Move");
129129

130130
while (true) {
131-
await _scheduler.CreateDelayTicks(lag, state.Proc.Id, state.Thread.Id);
131+
await _scheduler.CreateDelayTicks(lag, state.Proc!.Id, state.Thread.Id);
132132
if (cancelSource.IsCancellationRequested)
133133
break;
134134

0 commit comments

Comments
 (0)