22using Data . Components ;
33using Data . Messages ;
44using Simulation ;
5- using Simulation . Functions ;
65using System ;
76using System . Diagnostics ;
8- using System . Runtime . InteropServices ;
97using Unmanaged ;
108using Worlds ;
119
1210namespace 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}
0 commit comments