8#include <simplesquirrel/simplesquirrel.hpp>
20 if (sq_gettype(
vm,
idx) != OT_STRING)
return false;
21 const SQChar *
s =
nullptr;
22 if (SQ_FAILED(sq_getstring(
vm,
idx, &
s)) || !
s)
return false;
29 const SQInteger
top = sq_gettop(
vm);
30 sq_pushobject(
vm, instance);
31 sq_pushstring(
vm,
name.c_str(), -1);
32 if (SQ_FAILED(sq_get(
vm, -2))) {
36 sq_getstackobj(
vm, -1, &
out);
54bool getNumberField(
const ssq::Object& comp,
const std::string&
name,
float&
out) {
56 if (!
field.ok() || !
field.value().isNumeric())
return false;
62bool setNumberField(
const ssq::Object& comp,
const std::string&
name,
float value) {
66std::vector<std::string> collectFieldNames(ssq::Object fieldsObj) {
67 std::vector<std::string> names;
69 if (!
vm)
return names;
70 HSQOBJECT arr = fieldsObj.getRaw();
71 if (arr._type != OT_ARRAY)
return names;
73 const SQInteger
top = sq_gettop(
vm);
74 sq_pushobject(
vm, arr);
75 const SQInteger len = sq_getsize(
vm, -1);
76 names.reserve(
size_t(std::max<SQInteger>(0, len)));
77 for (SQInteger i = 0; i < len; ++i) {
78 sq_pushinteger(
vm, i);
79 if (SQ_FAILED(sq_get(
vm, -2)))
continue;
81 if (readStringField(
vm, -1,
name)) names.push_back(
name);
89 const SQInteger
top = sq_gettop(
vm);
90 sq_pushobject(
vm, arr);
91 const SQInteger len = sq_getsize(
vm, -1);
93 return int(std::max<SQInteger>(0, len));
104 int firstEntity,
int entityCount) {
105 if (!buf)
throw Exception(
"packScriptEntityFloats: buffer is null");
106 if (
slot.empty())
throw Exception(
"packScriptEntityFloats: empty slot");
110 HSQOBJECT ents = entitiesObj.getRaw();
111 if (ents._type != OT_ARRAY)
return 0;
113 const std::vector<std::string>
fields = collectFieldNames(fieldsObj);
114 if (
fields.empty())
return 0;
116 const int total = entityArrayLen(
vm, ents);
117 const int first = std::clamp(firstEntity, 0, total);
118 const int n = std::clamp(entityCount, 0, total -
first);
119 if (
n <= 0)
return 0;
121 const int floatsPer = int(
fields.size());
122 std::vector<float> tmp(
size_t(
n) *
size_t(floatsPer), 0.f);
124 const SQInteger
top = sq_gettop(
vm);
125 sq_pushobject(
vm, ents);
126 for (
int i = 0; i <
n; ++i) {
128 if (SQ_FAILED(sq_get(
vm, -2))) {
130 throw Exception(
"packScriptEntityFloats: bad entity index %d",
first + i);
132 if (sq_gettype(
vm, -1) != OT_INSTANCE) {
134 throw Exception(
"packScriptEntityFloats: entity[%d] is not an instance",
first + i);
140 sq_resetobject(&comp);
141 if (!getInstanceSlot(
vm,
entity,
slot, comp) || comp._type != OT_INSTANCE) {
142 if (comp._type != OT_NULL) sq_release(
vm, &comp);
144 throw Exception(
"packScriptEntityFloats: missing component slot '%s'",
slot.c_str());
148 ssq::Object compObj(
vm);
149 compObj.getRaw() = comp;
151 for (
int f = 0;
f < floatsPer; ++
f) {
153 if (!getNumberField(compObj,
fields[
size_t(
f)],
v)) {
155 throw Exception(
"packScriptEntityFloats: missing field '%s.%s'",
slot.c_str(),
158 tmp[size_t(i) * size_t(floatsPer) + size_t(
f)] =
v;
164 const int needBytes = (
first +
n) * floatsPer *
int(
sizeof(
float));
165 if (buf->
getSize() < needBytes)
166 throw Exception(
"packScriptEntityFloats: buffer too small (%d < %d)", buf->
getSize(),
173 ssq::Object fieldsObj,
GpuBuffer *buf,
int entityCount) {
178 GpuBuffer *buf,
int firstEntity,
int entityCount) {
179 if (!buf)
throw Exception(
"unpackScriptEntityFloats: buffer is null");
180 if (
slot.empty())
throw Exception(
"unpackScriptEntityFloats: empty slot");
181 if (entityCount <= 0)
return 0;
185 HSQOBJECT ents = entitiesObj.getRaw();
186 if (ents._type != OT_ARRAY)
return 0;
188 const std::vector<std::string>
fields = collectFieldNames(fieldsObj);
189 if (
fields.empty())
return 0;
191 const int nArr = entityArrayLen(
vm, ents);
192 const int first = std::clamp(firstEntity, 0, nArr);
193 const int n = std::clamp(entityCount, 0, nArr -
first);
194 if (
n <= 0)
return 0;
196 const int floatsPer = int(
fields.size());
197 std::vector<float> tmp(
size_t(
n) *
size_t(floatsPer), 0.f);
198 const int needBytes = (
first +
n) * floatsPer *
int(
sizeof(
float));
199 if (buf->
getSize() < needBytes)
200 throw Exception(
"unpackScriptEntityFloats: buffer too small (%d < %d)", buf->
getSize(), needBytes);
203 const SQInteger
top = sq_gettop(
vm);
204 sq_pushobject(
vm, ents);
205 for (
int i = 0; i <
n; ++i) {
207 if (SQ_FAILED(sq_get(
vm, -2))) {
209 throw Exception(
"unpackScriptEntityFloats: bad entity index %d",
first + i);
215 sq_resetobject(&comp);
216 if (!getInstanceSlot(
vm,
entity,
slot, comp) || comp._type != OT_INSTANCE) {
217 if (comp._type != OT_NULL) sq_release(
vm, &comp);
219 throw Exception(
"unpackScriptEntityFloats: missing component slot '%s'",
slot.c_str());
222 ssq::Object compObj(
vm);
223 compObj.getRaw() = comp;
225 for (
int f = 0;
f < floatsPer; ++
f) {
226 const float v = tmp[size_t(i) * size_t(floatsPer) + size_t(
f)];
227 if (!setNumberField(compObj,
fields[
size_t(
f)],
v)) {
229 throw Exception(
"unpackScriptEntityFloats: cannot set '%s.%s'",
slot.c_str(),
struct SQVM * HSQUIRRELVM
std::vector< std::string > fields
const EntityFact * entity
Owning, renderer-independent dynamic values.
EVENGINE_API_FOUNDATION public API.
bool ok() const noexcept
Whether this result represents a non-failure outcome.
static Value number(double value)
Compatibility factory for a Double value.
Backend-agnostic GPU buffer for compute (storage) or CPU staging transfers. Squirrel-owned; derived c...
virtual void writeFloat32s(const float *data, int count, int startIndex=0)=0
Bulk float upload/download (one transfer). startIndex is in floats.
virtual int getSize() const =0
Byte length of the owned buffer.
virtual void readFloat32s(float *out, int count, int startIndex=0) const =0
Reads float 32 s.
double asDouble(double fallback=0.0) const
As double.
const Value * field(const Value &value, const char *key)
Borrow an optional object field.
Fluids module — interactive surface fluid simulation.
int unpackScriptEntityFloatsRange(ssq::Object entitiesObj, const std::string &slot, ssq::Object fieldsObj, GpuBuffer *buf, int firstEntity, int entityCount)
Unpack a contiguous buffer range into the matching stable ECS view range.
int packScriptEntityFloats(ssq::Object entitiesObj, const std::string &slot, ssq::Object fieldsObj, GpuBuffer *buf)
Pack script ECS entities' component number fields into a GpuBuffer (AoS).
int unpackScriptEntityFloats(ssq::Object entitiesObj, const std::string &slot, ssq::Object fieldsObj, GpuBuffer *buf, int entityCount)
Inverse of packScriptEntityFloats: write buffer rows back into component fields.
int packScriptEntityFloatsRange(ssq::Object entitiesObj, const std::string &slot, ssq::Object fieldsObj, GpuBuffer *buf, int firstEntity, int entityCount)
Pack a contiguous entity range into the matching range of an existing buffer.
EVENGINE_API_FOUNDATION Result< Value > readScriptComponentField(const ssq::Object &instance, const std::string &field)
读一个脚本组件实例的字段,**不关心这个字段存在哪**。
EVENGINE_API_FOUNDATION Result< void > writeScriptComponentField(const ssq::Object &instance, const std::string &field, const Value &value)
写一个脚本组件实例的字段,归属规则见 readScriptComponentField。