Skip to content

Commit 7e220bd

Browse files
committed
modify restEnable + add lookup test
1 parent b91557e commit 7e220bd

2 files changed

Lines changed: 43 additions & 20 deletions

File tree

src/main/java/com/github/elebras1/flecs/World.java

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -847,34 +847,27 @@ public void enableRest(short port) {
847847
flecs_h.FlecsStatsImport(this.worldSeg);
848848
flecs_h.FlecsMetricsImport(this.worldSeg);
849849

850-
try (Arena arena = Arena.ofConfined()) {
851-
MemorySegment restCompName = arena.allocateFrom("flecs.rest.Rest");
852-
long restCompId = flecs_h.ecs_lookup(this.worldSeg, restCompName);
853-
854-
if (restCompId == 0) {
855-
throw new IllegalStateException("Failed to find flecs.rest.Rest component.");
856-
}
857-
858-
MemorySegment restDataSeg = arena.allocate(32);
859-
restDataSeg.set(JAVA_SHORT, 0, port);
850+
long restCompId = flecs_h.FLECS_IDEcsRestID_();
851+
if( restCompId == 0) {
852+
throw new IllegalStateException("Failed to find EcsRest component.");
853+
}
860854

861-
flecs_h.ecs_set_id(this.worldSeg, restCompId, restCompId, 32, restDataSeg);
855+
try (Arena arena = Arena.ofConfined()) {
856+
MemorySegment restDataSeg = EcsRest.allocate(arena);
857+
EcsRest.port(restDataSeg, port);
858+
flecs_h.ecs_set_id(this.worldSeg, restCompId, restCompId, EcsRest.sizeof(), restDataSeg);
862859
}
863860
}
864861

865862
public void disableRest() {
866863
this.checkDestroyed();
867864

868-
try (Arena arena = Arena.ofConfined()) {
869-
MemorySegment restCompNameSeg = arena.allocateFrom("flecs.rest.Rest");
870-
long restCompId = flecs_h.ecs_lookup(this.worldSeg, restCompNameSeg);
871-
872-
if( restCompId == 0) {
873-
throw new IllegalStateException("Failed to find flecs.rest.Rest component. Make sure FlecsRest module is imported.");
874-
}
875-
876-
flecs_h.ecs_remove_id(this.worldSeg, restCompId, restCompId);
865+
long restCompId = flecs_h.FLECS_IDEcsRestID_();
866+
if( restCompId == 0) {
867+
throw new IllegalStateException("Failed to find EcsRest component.");
877868
}
869+
870+
flecs_h.ecs_remove_id(this.worldSeg, restCompId, restCompId);
878871
}
879872

880873
public void destroy() {
@@ -906,3 +899,4 @@ public String toString() {
906899
}
907900
}
908901

902+

src/test/java/com/github/elebras1/flecs/WorldTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.github.elebras1.flecs.component.Health;
44
import com.github.elebras1.flecs.component.Ideology;
5+
import com.github.elebras1.flecs.util.FlecsConstants;
56
import org.junit.jupiter.api.AfterEach;
67
import org.junit.jupiter.api.BeforeEach;
78
import org.junit.jupiter.api.Test;
@@ -97,6 +98,34 @@ void setEntityRangeTest() {
9798
}
9899
}
99100

101+
@Test
102+
void testLookup() {
103+
this.world.entity("test_entity");
104+
long entityId = this.world.lookup("test_entity");
105+
assertTrue(entityId > 0);
106+
}
107+
108+
@Test
109+
void testLookupWithSystemCreateBefore() {
110+
this.world.enableRest((short) 27750);
111+
this.world.component(Health.class);
112+
113+
this.world.system("system_test").kind(FlecsConstants.EcsOnUpdate).with(Health.class).iter(iter -> {
114+
long entityId = iter.world().lookup("test_entity");
115+
assertTrue(entityId > 0);
116+
assertEquals(10, iter.count());
117+
});
118+
119+
for(int i = 0; i < 10; i++) {
120+
long entityId = this.world.entity();
121+
EntityView entity = this.world.obtainEntityView(entityId);
122+
entity.set(new Health(100));
123+
}
124+
125+
this.world.entity("test_entity");
126+
this.world.progress(1);
127+
}
128+
100129
@AfterEach
101130
void tearDown() {
102131
this.world.destroy();

0 commit comments

Comments
 (0)