Skip to content

Commit

Permalink
Merge pull request #326 from NoiseStudio/fix/255/entity-schedule-test…
Browse files Browse the repository at this point in the history
…-execution-sometimes-throws

Fix EntityScheduleTest.Execution sometimes throws
  • Loading branch information
Vixenka committed Aug 19, 2023
2 parents 1a0801c + 8bc00e4 commit 0f7ba25
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
5 changes: 5 additions & 0 deletions NoiseEngine/Jobs/AffectiveSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,24 @@ public abstract class AffectiveSystem : IDisposable {
if (!archetypes.Add(archetype))
return;

bool created = false;
if (!systems.TryGetValue(hashCode, out EntitySystem? system)) {
lock (systems) {
if (!systems.TryGetValue(hashCode, out system)) {
system = InternalCreateFromComponents(components);
if (system is null)
return;

created = true;
systems.Add(hashCode, system);
}
}
}

system.RegisterArchetype(archetype);

if (created)
system.TryEnableAfterInitialization();
}

internal EntitySystem? InternalCreateFromComponents(Dictionary<Type, IComponent> components) {
Expand Down
15 changes: 14 additions & 1 deletion NoiseEngine/Jobs/EntitySystemT0.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ protected struct NoiseEngineInternal_DoNotUse {
private AtomicBool isWorking;
private AtomicBool isDisposed;
private bool enabled;
private bool isEnabledUsed;
private EntitySchedule? schedule;
private double? cycleTime;
private bool isDoneInitialize;
Expand Down Expand Up @@ -261,6 +262,8 @@ protected struct NoiseEngineInternal_DoNotUse {
AssertIsNotDisposed();

enabled = value;
isEnabledUsed = true;

if (value) {
started = true;
OnStart();
Expand Down Expand Up @@ -439,14 +442,24 @@ protected struct NoiseEngineInternal_DoNotUse {
#pragma warning restore CS0618

OnInitialize();
Enabled = true;

lock (scheduleLocker) {
isDoneInitialize = true;
Schedule ??= world.DefaultSchedule;
}
}

internal void TryEnableAfterInitialization() {
if (isEnabledUsed)
return;

lock (enabledLocker) {
if (isEnabledUsed)
return;
Enabled = true;
}
}

#pragma warning disable CS0618
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void SystemExecutionInternal(
Expand Down
1 change: 1 addition & 0 deletions NoiseEngine/Jobs/EntityWorld.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public partial class EntityWorld : IDisposable {

systems.Add(system);
RegisterArchetypesToSystem(system);
system.TryEnableAfterInitialization();

if (IsDisposed) {
system.Dispose();
Expand Down

0 comments on commit 0f7ba25

Please sign in to comment.