|
1 | 1 | package com.github.elebras1.flecs; |
2 | 2 |
|
| 3 | +import java.lang.foreign.MemoryLayout; |
3 | 4 | import java.lang.foreign.MemorySegment; |
4 | 5 | import java.lang.foreign.ValueLayout; |
5 | 6 | import java.util.Arrays; |
@@ -174,13 +175,9 @@ public <T> byte fieldByte(Class<T> componentClass, int index, String fieldName, |
174 | 175 |
|
175 | 176 | public <T> String fieldString(Class<T> componentClass, int index, String fieldName, int i) { |
176 | 177 | long offset = fieldPtr(componentClass, index, fieldName, i); |
177 | | - MemorySegment stringPointer = this.cachedColumns[index].get(ValueLayout.ADDRESS.withByteAlignment(1), offset); |
178 | | - |
179 | | - if (stringPointer.address() == 0) { |
180 | | - return null; |
181 | | - } |
182 | | - |
183 | | - return stringPointer.reinterpret(Short.MAX_VALUE).getString(0); |
| 178 | + Component<T> component = this.world.componentRegistry().getComponent(componentClass); |
| 179 | + long capacity = component.layout().select(MemoryLayout.PathElement.groupElement(fieldName)).byteSize(); |
| 180 | + return this.cachedColumns[index].asSlice(offset, capacity).getString(0); |
184 | 181 | } |
185 | 182 |
|
186 | 183 | public <T> void setFieldInt(Class<T> componentClass, int index, String fieldName, int i, int value) { |
@@ -219,7 +216,18 @@ public <T> void setFieldByte(Class<T> componentClass, int index, String fieldNam |
219 | 216 | } |
220 | 217 |
|
221 | 218 | public <T> void setFieldString(Class<T> componentClass, int index, String fieldName, int i, String value) { |
222 | | - throw new UnsupportedOperationException("setFieldString not yet implemented"); |
| 219 | + long offset = fieldPtr(componentClass, index, fieldName, i); |
| 220 | + Component<T> component = this.world.componentRegistry().getComponent(componentClass); |
| 221 | + long capacity = component.layout().select(MemoryLayout.PathElement.groupElement(fieldName)).byteSize(); |
| 222 | + MemorySegment slice = this.cachedColumns[index].asSlice(offset, capacity); |
| 223 | + if (value == null || value.isEmpty()) { |
| 224 | + slice.set(ValueLayout.JAVA_BYTE, 0, (byte) 0); |
| 225 | + return; |
| 226 | + } |
| 227 | + byte[] bytes = value.getBytes(java.nio.charset.StandardCharsets.UTF_8); |
| 228 | + int len = Math.min(bytes.length, (int) capacity - 1); |
| 229 | + MemorySegment.copy(bytes, 0, slice, ValueLayout.JAVA_BYTE, 0, len); |
| 230 | + slice.set(ValueLayout.JAVA_BYTE, len, (byte) 0); |
223 | 231 | } |
224 | 232 |
|
225 | 233 | public long event() { |
|
0 commit comments