Skip to content

Commit f9e25b8

Browse files
committed
refactor system into a class
1 parent 91c6807 commit f9e25b8

4 files changed

Lines changed: 56 additions & 44 deletions

File tree

source/Systems/DataImportSystem.cs

Lines changed: 26 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@
22
using Data.Components;
33
using Data.Messages;
44
using Simulation;
5-
using Simulation.Functions;
65
using System;
76
using System.Diagnostics;
8-
using System.Runtime.InteropServices;
97
using Unmanaged;
108
using Worlds;
119

1210
namespace Data.Systems
1311
{
14-
public readonly partial struct DataImportSystem : ISystem
12+
public partial class DataImportSystem : ISystem, IDisposable, IListener<LoadData>
1513
{
1614
private readonly Dictionary<Entity, LoadingTask> tasks;
1715
private readonly Stack<Operation> operations;
@@ -22,7 +20,7 @@ public DataImportSystem()
2220
operations = new(4);
2321
}
2422

25-
public readonly void Dispose()
23+
public void Dispose()
2624
{
2725
while (operations.TryPop(out Operation operation))
2826
{
@@ -33,17 +31,9 @@ public readonly void Dispose()
3331
tasks.Dispose();
3432
}
3533

36-
unsafe readonly void ISystem.CollectMessageHandlers(MessageHandlerCollector collectors)
37-
{
38-
collectors.Add<LoadData>(&HandleDataRequest);
39-
}
40-
41-
void ISystem.Start(in SystemContext context, in World world)
42-
{
43-
}
44-
45-
void ISystem.Update(in SystemContext context, in World world, in TimeSpan delta)
34+
void ISystem.Update(Simulator simulator, double deltaTime)
4635
{
36+
World world = simulator.world;
4737
Schema schema = world.Schema;
4838
int dataComponent = schema.GetComponentType<IsDataRequest>();
4939
int sourceType = schema.GetComponentType<IsDataSource>();
@@ -79,7 +69,7 @@ void ISystem.Update(in SystemContext context, in World world, in TimeSpan delta)
7969
}
8070
else
8171
{
82-
task.duration += delta;
72+
task.duration += deltaTime;
8373
if (task.duration >= request.timeout)
8474
{
8575
request.status = RequestStatus.NotFound;
@@ -98,11 +88,28 @@ void ISystem.Update(in SystemContext context, in World world, in TimeSpan delta)
9888
PerformInstructions(world);
9989
}
10090

101-
void ISystem.Finish(in SystemContext context, in World world)
91+
void IListener<LoadData>.Receive(ref LoadData request)
10292
{
93+
if (request.status == RequestStatus.Uninitialized)
94+
{
95+
request.status = RequestStatus.Loading;
96+
}
97+
98+
if (request.status == RequestStatus.Loading)
99+
{
100+
int sourceType = request.world.Schema.GetComponentType<IsDataSource>();
101+
if (TryLoad(request.world, request.address, sourceType, out ByteReader newReader))
102+
{
103+
request.Load(newReader);
104+
}
105+
else
106+
{
107+
request.NotFound();
108+
}
109+
}
103110
}
104111

105-
private readonly void PerformInstructions(World world)
112+
private void PerformInstructions(World world)
106113
{
107114
while (operations.TryPop(out Operation operation))
108115
{
@@ -119,7 +126,7 @@ private static bool TryLoad(Entity entity, Address address, int sourceType, out
119126
Span<byte> readData = newReader.GetBytes();
120127
operation = new();
121128
operation.SelectEntity(entity);
122-
operation.CreateOrSetArray(readData.As<byte, BinaryData>());
129+
operation.CreateOrSetArray(readData.As<byte, DataByte>());
123130
newReader.Dispose();
124131
return true;
125132
}
@@ -167,7 +174,7 @@ private static bool TryLoadFromWorld(World world, Address address, int sourceTyp
167174
if (source.address.Matches(address))
168175
{
169176
uint entity = entities[i];
170-
Span<byte> fileData = world.GetArray<BinaryData>(entity).AsSpan<byte>();
177+
Span<byte> fileData = world.GetArray<DataByte>(entity).AsSpan<byte>();
171178
newReader = new(fileData);
172179
Trace.WriteLine($"Loaded data from entity `{entity}` for address `{source.address}`");
173180
return true;
@@ -196,27 +203,5 @@ private static bool TryLoadFromFileSystem(Address address, out ByteReader newRea
196203
return false;
197204
}
198205
}
199-
200-
[UnmanagedCallersOnly]
201-
private static StatusCode HandleDataRequest(HandleMessage.Input input)
202-
{
203-
ref LoadData message = ref input.ReadMessage<LoadData>();
204-
if (!message.IsLoaded)
205-
{
206-
int sourceType = message.world.Schema.GetComponentType<IsDataSource>();
207-
if (TryLoad(message.world, message.address, sourceType, out ByteReader newReader))
208-
{
209-
message.BecomeLoaded(newReader);
210-
return StatusCode.Success(0);
211-
}
212-
else
213-
{
214-
Trace.TraceError($"Failed to load data from address `{message.address}`, data not found");
215-
return StatusCode.Failure(0);
216-
}
217-
}
218-
219-
return StatusCode.Continue;
220-
}
221206
}
222207
}

source/Systems/LoadingTask.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace Data.Systems
55
internal struct LoadingTask
66
{
77
public readonly DateTime startTime;
8-
public TimeSpan duration;
8+
public double duration;
99

1010
[Obsolete("Default constructor not supported", true)]
1111
public LoadingTask()

tests/DataSystemTests.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@ static DataSystemTests()
1414
protected override void SetUp()
1515
{
1616
base.SetUp();
17-
simulator.AddSystem(new DataImportSystem());
17+
simulator.Add(new DataImportSystem());
18+
}
19+
20+
protected override void TearDown()
21+
{
22+
simulator.Remove<DataImportSystem>();
23+
base.TearDown();
1824
}
1925

2026
protected override Schema CreateSchema()

tests/ImportTests.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using Collections.Generic;
2-
using Data.Components;
2+
using Data.Messages;
33
using System;
44
using System.Threading;
55
using System.Threading.Tasks;
@@ -129,5 +129,26 @@ public async Task LoadEmbeddedResource(CancellationToken cancellation)
129129
Span<char> fileText = span.Slice(0, length);
130130
Assert.That(fileText.ToString(), Contains.Substring("i am an embedded resource"));
131131
}
132+
133+
[Test]
134+
public void LoadWithMessage()
135+
{
136+
const string FileName = "tomato";
137+
string randomStr = Guid.NewGuid().ToString();
138+
DataSource file = new(world, FileName);
139+
file.WriteUTF8(randomStr);
140+
141+
LoadData loadData = new(world, FileName);
142+
Broadcast(ref loadData);
143+
Assert.That(loadData.status == RequestStatus.Loaded);
144+
bool consumed = loadData.TryConsume(out ByteReader byteReader);
145+
Assert.That(consumed, Is.True);
146+
Assert.That(loadData.status == RequestStatus.Consumed);
147+
Span<char> buffer = stackalloc char[128];
148+
int length = byteReader.ReadUTF8(buffer);
149+
Span<char> text = buffer.Slice(0, length);
150+
Assert.That(text.ToString(), Is.EqualTo(randomStr));
151+
byteReader.Dispose();
152+
}
132153
}
133154
}

0 commit comments

Comments
 (0)