Skip to content

Commit 12adacb

Browse files
committed
fix enableRest
1 parent 7e220bd commit 12adacb

1 file changed

Lines changed: 24 additions & 14 deletions

File tree

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

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -840,22 +840,33 @@ public void enableRest() {
840840

841841
public void enableRest(short port) {
842842
this.checkDestroyed();
843+
try (Arena arena = Arena.ofConfined()) {
844+
importModule(arena, "FlecsDoc", flecs_h.FlecsDocImport$address());
845+
importModule(arena, "FlecsRest", flecs_h.FlecsRestImport$address());
846+
importModule(arena, "FlecsAlerts", flecs_h.FlecsAlertsImport$address());
847+
importModule(arena, "FlecsStats", flecs_h.FlecsStatsImport$address());
848+
importModule(arena, "FlecsMetrics", flecs_h.FlecsMetricsImport$address());
849+
850+
long restCompId = flecs_h.FLECS_IDEcsRestID_();
851+
if (restCompId == 0) {
852+
throw new IllegalStateException("Failed to find EcsRest component.");
853+
}
843854

844-
flecs_h.FlecsDocImport(this.worldSeg);
845-
flecs_h.FlecsRestImport(this.worldSeg);
846-
flecs_h.FlecsAlertsImport(this.worldSeg);
847-
flecs_h.FlecsStatsImport(this.worldSeg);
848-
flecs_h.FlecsMetricsImport(this.worldSeg);
855+
MemorySegment restDataSeg = EcsRest.allocate(arena);
856+
restDataSeg.fill((byte) 0);
857+
EcsRest.port(restDataSeg, port);
858+
EcsRest.ipaddr(restDataSeg, MemorySegment.NULL);
859+
EcsRest.impl(restDataSeg, MemorySegment.NULL);
849860

850-
long restCompId = flecs_h.FLECS_IDEcsRestID_();
851-
if( restCompId == 0) {
852-
throw new IllegalStateException("Failed to find EcsRest component.");
861+
flecs_h.ecs_set_id(this.worldSeg, FlecsConstants.EcsWorld, restCompId, EcsRest.sizeof(), restDataSeg);
853862
}
863+
}
854864

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);
865+
private void importModule(Arena arena, String moduleName, MemorySegment moduleImportFunction) {
866+
MemorySegment moduleNameSeg = arena.allocateFrom(moduleName);
867+
long moduleId = flecs_h.ecs_import_c(this.worldSeg, moduleImportFunction, moduleNameSeg);
868+
if (moduleId == 0) {
869+
throw new IllegalStateException("Failed to import module: " + moduleName);
859870
}
860871
}
861872

@@ -867,7 +878,7 @@ public void disableRest() {
867878
throw new IllegalStateException("Failed to find EcsRest component.");
868879
}
869880

870-
flecs_h.ecs_remove_id(this.worldSeg, restCompId, restCompId);
881+
flecs_h.ecs_remove_id(this.worldSeg, FlecsConstants.EcsWorld, restCompId);
871882
}
872883

873884
public void destroy() {
@@ -899,4 +910,3 @@ public String toString() {
899910
}
900911
}
901912

902-

0 commit comments

Comments
 (0)