Cogs.Core
FieldFunctions.cpp
1#include "FieldFunctions.h"
2
3#include "Context.h"
4#include "Engine.h"
5#include "EntityStore.h"
6
7#include "FieldSetter.h"
8
9#include "Types.h"
10
11#include "Resources/ResourceBase.h"
12
13#include "Foundation/ComponentModel/Component.h"
14#include "Foundation/Logging/Logger.h"
15
16#include <algorithm>
17
18#include <glm/vec2.hpp>
19#include <glm/vec3.hpp>
20#include <glm/vec4.hpp>
21#include <glm/mat4x4.hpp>
22#include <glm/ext/quaternion_float.hpp>
23#include <glm/ext/quaternion_double.hpp>
24
25using namespace Cogs::Core;
26using namespace Cogs::ComponentModel;
27
28
29namespace {
31 size_t scanEncodedLength(const char* value) {
32 size_t length = 0;
33 while (*value != 0) {
34 if (*value == '\\') {
35 value++;
36 if (*value == 0) break; // Really error
37 length++;
38 }
39 else if (*value == '|') {
40 break;
41 }
42 else {
43 length++;
44 }
45
46 value++;
47 }
48
49 return length;
50 }
51
53 size_t scanEncodedVectorLength(const char* value) {
54 size_t length = 0;
55 while (*value != 0) {
56 if (*value == '\\') {
57 value++;
58 if (*value == 0) break; // Really error
59 }
60 else if (*value == '|') {
61 length++;
62 }
63
64 value++;
65 }
66
67 return length;
68 }
69
71 std::vector<std::string> decodeMultiString(const char* value)
72 {
73 std::vector<std::string> strings;
74 strings.reserve(scanEncodedVectorLength(value));
75
76 std::string str;
77 str.reserve(scanEncodedLength(value));
78 while (*value != 0) {
79 if (*value == '\\') {
80 value++;
81 str.push_back(*value);
82 if (*value == 0) {
83 assert(false && "Invalid EncodedMultiStringField encoding");
84 return strings;
85 }
86 }
87 else if (*value == '|') {
88 strings.emplace_back(str);
89 str.clear();
90 str.reserve(scanEncodedLength(value + 1));
91 }
92 else {
93 str.push_back(*value);
94 }
95
96 value++;
97 }
98
99 return strings;
100 }
101
103 std::string encodeMultiString(const std::vector<std::string>& value)
104 {
105 std::string output;
106 size_t estimate = 0;
107 for (const auto& str : value) {
108 estimate += str.length() + 1;
109 }
110 output.reserve(estimate);
111
112 for (const std::string& s : value) {
113 for (const char c : s) {
114 if (c == '\\' || c == '|') output.push_back('\\');
115 output.push_back(c);
116 }
117
118 // Add terminator/separator:
119 output.push_back('|');
120 }
121
122 return output;
123 }
124
125 template<typename Collection>
126 int getMultiValue(const void* field, void* value, int size)
127 {
128 auto collection = static_cast<const Collection*>(field);
129
130 if (value && collection->size()) {
131 std::memcpy(value, collection->data(), size * collection->size());
132 }
133
134 return static_cast<int>(collection->size());
135 }
136
137 const Cogs::Logging::Log logger = Cogs::Logging::getLogger("FieldFunctions");
138}
139
140void setEntityField(BridgeContext* ctx, EntityId entityId, const ComponentId componentId, const FieldId fieldId, EntityId targetId)
141{
142 Context* context = static_cast<Context *>(ctx);
143
144 const Cogs::Reflection::Type& componentType = Cogs::Reflection::TypeDatabase::getType(componentId);
145 const Cogs::Reflection::Field* field = componentType.getField(fieldId);
146
147 const Cogs::Reflection::Type& entityType = Cogs::Reflection::TypeDatabase::getType<EntityPtr>();
148 const Cogs::Reflection::Type& wEntityType = Cogs::Reflection::TypeDatabase::getType<WeakEntityPtr>();
149
150 const Cogs::Core::EntityPtr targetPtr = (targetId == NoEntity) ? EntityPtr() : context->store->getEntity(targetId);
151
152 if (field->getTypeId() == entityType.getTypeId()) {
153 Cogs::setField(context, entityId, componentId, fieldId, targetPtr);
154 } else if (field->getTypeId() == wEntityType.getTypeId()) {
155 Cogs::setField(context, entityId, componentId, fieldId, WeakEntityPtr(targetPtr));
156 }
157 else {
158 LOG_ERROR(logger, "setEntityField: Type match error for field '%s' in component '%s'.", field->getName().c_str(), componentType.getName().c_str());
159 }
160}
161
162EntityId getEntityField(BridgeContext* ctx, EntityId entityId, const ComponentId componentId, const FieldId fieldId)
163{
164 Context* context = static_cast<Context*>(ctx);
165
166 const Cogs::Reflection::Type& componentType = Cogs::Reflection::TypeDatabase::getType(componentId);
167 const Cogs::Reflection::Field* field = componentType.getField(fieldId);
168
169 const Cogs::Reflection::Type& entityType = Cogs::Reflection::TypeDatabase::getType<EntityPtr>();
170 const Cogs::Reflection::Type& wEntityType = Cogs::Reflection::TypeDatabase::getType<WeakEntityPtr>();
171
172 if (field->getTypeId() == entityType.getTypeId()) {
173 const EntityPtr* fieldPtr = Cogs::getField<EntityPtr>(context, entityId, componentId, fieldId);
174 return (*fieldPtr) ? (*fieldPtr)->getId() : NoEntity;
175 }
176 else if (field->getTypeId() == wEntityType.getTypeId()) {
177 const EntityPtr fieldPtr = Cogs::getField<WeakEntityPtr>(context, entityId, componentId, fieldId)->lock();
178 return fieldPtr ? fieldPtr->getId() : NoEntity;
179 }
180 else {
181 LOG_ERROR(logger, "getEntityField: Type match error for field '%s' in component '%s'.", field->getName().c_str(), componentType.getName().c_str());
182 return NoEntity;
183 }
184}
185
186void setMultiEntityField(BridgeContext* ctx, EntityId entityId, const ComponentId componentId, const FieldId fieldId, const EntityId * targetIds, int count)
187{
188 auto context = static_cast<Context *>(ctx);
189
190 std::vector<WeakEntityPtr> entities;
191
192 for (int i = 0; i < count; ++i) {
193 entities.push_back(context->store->getEntity(targetIds[i]));
194 }
195
196 Cogs::setField(context, entityId, componentId, fieldId, std::move(entities));
197}
198
199void setFloatField(BridgeContext* ctx, EntityId entityId, const ComponentId componentId, const FieldId fieldId, const float value)
200{
201 auto context = static_cast<Context *>(ctx);
202
203 Cogs::setField(context, entityId, componentId, fieldId, value);
204}
205
206void setDoubleField(BridgeContext* ctx, EntityId entityId, const ComponentId componentId, const FieldId fieldId, const double value)
207{
208 auto context = static_cast<Context *>(ctx);
209
210 Cogs::setField(context, entityId, componentId, fieldId, value);
211}
212
213void setIntegerField(BridgeContext* ctx, EntityId entityId, const ComponentId componentId, const FieldId fieldId, const int value)
214{
215 auto context = static_cast<Context *>(ctx);
216
217 Cogs::setField(context, entityId, componentId, fieldId, value);
218}
219
220void setUIntegerField(BridgeContext* ctx, EntityId entityId, const ComponentId componentId, const FieldId fieldId, const uint32_t value)
221{
222 auto context = static_cast<Context *>(ctx);
223
224 Cogs::setField(context, entityId, componentId, fieldId, value);
225}
226
227void setInt64Field(BridgeContext* ctx, EntityId entityId, const ComponentId componentId, const FieldId fieldId, const int64_t value)
228{
229 auto context = static_cast<Context *>(ctx);
230
231 Cogs::setField(context, entityId, componentId, fieldId, value);
232}
233
234void setUInt64Field(BridgeContext* ctx, EntityId entityId, const ComponentId componentId, const FieldId fieldId, const uint64_t value)
235{
236 auto context = static_cast<Context *>(ctx);
237
238 Cogs::setField(context, entityId, componentId, fieldId, value);
239}
240
241
242void setBoolField(BridgeContext* ctx, EntityId entityId, const ComponentId componentId, const FieldId fieldId, const CogsBool value)
243{
244 auto context = static_cast<Context *>(ctx);
245
246 Cogs::setField(context, entityId, componentId, fieldId, value != 0);
247}
248
249int getBoolField(BridgeContext* ctx, EntityId entityId, const ComponentId componentId, const FieldId fieldId)
250{
251 auto context = static_cast<Context*>(ctx);
252 auto fieldPtr = Cogs::getField<bool>(context, entityId, componentId, fieldId);
253 return *fieldPtr ? 1 : 0;
254}
255
256void setVector2Field(BridgeContext* ctx, EntityId entityId, const ComponentId componentId, const FieldId fieldId, const float * value)
257{
258 auto context = static_cast<Context *>(ctx);
259
260 Cogs::setField(context, entityId, componentId, fieldId, *reinterpret_cast<const glm::vec2 *>(value));
261}
262
263void setVector3Field(BridgeContext* ctx, EntityId entityId, const ComponentId componentId, const FieldId fieldId, const float * value)
264{
265 auto context = static_cast<Context *>(ctx);
266
267 Cogs::setField(context, entityId, componentId, fieldId, *reinterpret_cast<const glm::vec3 *>(value));
268}
269
270void setVector4Field(BridgeContext* ctx, EntityId entityId, const ComponentId componentId, const FieldId fieldId, const float * value)
271{
272 auto context = static_cast<Context *>(ctx);
273
274 Cogs::setField(context, entityId, componentId, fieldId, *reinterpret_cast<const glm::vec4 *>(value));
275}
276
277
278void setVector2iField(BridgeContext* ctx, EntityId entityId, const ComponentId componentId, const FieldId fieldId, const int32_t * value)
279{
280 auto context = static_cast<Context *>(ctx);
281
282 Cogs::setField(context, entityId, componentId, fieldId, *reinterpret_cast<const glm::ivec2 *>(value));
283}
284
285void setVector3iField(BridgeContext* ctx, EntityId entityId, const ComponentId componentId, const FieldId fieldId, const int32_t * value)
286{
287 auto context = static_cast<Context *>(ctx);
288
289 Cogs::setField(context, entityId, componentId, fieldId, *reinterpret_cast<const glm::ivec3 *>(value));
290}
291
292void setVector4iField(BridgeContext* ctx, EntityId entityId, const ComponentId componentId, const FieldId fieldId, const int32_t * value)
293{
294 auto context = static_cast<Context *>(ctx);
295
296 Cogs::setField(context, entityId, componentId, fieldId, *reinterpret_cast<const glm::ivec4 *>(value));
297}
298
299void setVector2uField(BridgeContext* ctx, EntityId entityId, const ComponentId componentId, const FieldId fieldId, const uint32_t * value)
300{
301 auto context = static_cast<Context *>(ctx);
302
303 Cogs::setField(context, entityId, componentId, fieldId, *reinterpret_cast<const glm::uvec2 *>(value));
304}
305
306void setVector3uField(BridgeContext* ctx, EntityId entityId, const ComponentId componentId, const FieldId fieldId, const uint32_t * value)
307{
308 auto context = static_cast<Context *>(ctx);
309
310 Cogs::setField(context, entityId, componentId, fieldId, *reinterpret_cast<const glm::uvec3 *>(value));
311}
312
313void setVector4uField(BridgeContext* ctx, EntityId entityId, const ComponentId componentId, const FieldId fieldId, const uint32_t * value)
314{
315 auto context = static_cast<Context *>(ctx);
316
317 Cogs::setField(context, entityId, componentId, fieldId, *reinterpret_cast<const glm::uvec4 *>(value));
318}
319
320
321
322void setVector2dField(BridgeContext* ctx, EntityId entityId, const ComponentId componentId, const FieldId fieldId, const double * value)
323{
324 auto context = static_cast<Context *>(ctx);
325
326 Cogs::setField(context, entityId, componentId, fieldId, *reinterpret_cast<const glm::dvec2 *>(value));
327}
328
329void setVector3dField(BridgeContext* ctx, EntityId entityId, const ComponentId componentId, const FieldId fieldId, const double * value)
330{
331 auto context = static_cast<Context *>(ctx);
332
333 Cogs::setField(context, entityId, componentId, fieldId, *reinterpret_cast<const glm::dvec3 *>(value));
334}
335
336void setVector4dField(BridgeContext* ctx, EntityId entityId, const ComponentId componentId, const FieldId fieldId, const double * value)
337{
338 auto context = static_cast<Context *>(ctx);
339
340 Cogs::setField(context, entityId, componentId, fieldId, *reinterpret_cast<const glm::dvec4 *>(value));
341}
342
343void setQuaternionField(BridgeContext* ctx, EntityId entityId, const ComponentId componentId, const FieldId fieldId, const float * value)
344{
345 auto context = static_cast<Context *>(ctx);
346
347 Cogs::setField(context, entityId, componentId, fieldId, *reinterpret_cast<const glm::quat *>(value));
348}
349
350void setQuaterniondField(BridgeContext* ctx, EntityId entityId, const ComponentId componentId, const FieldId fieldId, const double * value)
351{
352 auto context = static_cast<Context *>(ctx);
353
354 Cogs::setField(context, entityId, componentId, fieldId, *reinterpret_cast<const glm::dquat *>(value));
355}
356
357void setMatrixField(BridgeContext* ctx, EntityId entityId, const ComponentId componentId, const FieldId fieldId, const float * value)
358{
359 auto context = static_cast<Context *>(ctx);
360
361 Cogs::setField(context, entityId, componentId, fieldId, *reinterpret_cast<const glm::mat4 *>(value));
362}
363
364void setMultiMatrixField(BridgeContext* ctx, EntityId entityId, const ComponentId componentId, const FieldId fieldId, const float* values, const int count)
365{
366 Context* context = static_cast<Context*>(ctx);
367
368 Cogs::assignField(context, entityId, componentId, fieldId, reinterpret_cast<const glm::mat4*>(values), count);
369}
370
371void setMultiFloatField(BridgeContext* ctx, EntityId entityId, const ComponentId componentId, const FieldId fieldId, const float * values, const int count)
372{
373 auto context = static_cast<Context *>(ctx);
374
375 Cogs::assignField(context, entityId, componentId, fieldId, values, count);
376}
377
378void setMultiFloatFieldSubset(BridgeContext* ctx, EntityId entityId, const ComponentId componentId, const FieldId fieldId, const float * values, const int offset, const int count)
379{
380 auto context = static_cast<Context *>(ctx);
381
382 Cogs::assignFieldSubset(context, entityId, componentId, fieldId, values, offset, count);
383}
384
385void setMultiDoubleField(BridgeContext* ctx, EntityId entityId, const ComponentId componentId, const FieldId fieldId, const double * values, const int count)
386{
387 auto context = static_cast<Context *>(ctx);
388
389 Cogs::assignField(context, entityId, componentId, fieldId, values, count);
390}
391
392void setMultiIntegerField(BridgeContext* ctx, EntityId entityId, const ComponentId componentId, const FieldId fieldId, const int * values, const int count)
393{
394 auto context = static_cast<Context *>(ctx);
395
396 Cogs::assignField(context, entityId, componentId, fieldId, values, count);
397}
398
399void setMultiUInt64Field(BridgeContext* ctx, EntityId entityId, const ComponentId componentId, const FieldId fieldId, const uint64_t * values, const int count)
400{
401 auto context = static_cast<Context *>(ctx);
402
403 Cogs::assignField(context, entityId, componentId, fieldId, values, count);
404}
405
406void setMultiUInt64FieldSubset(BridgeContext* ctx, EntityId entityId, const ComponentId componentId, const FieldId fieldId, const uint64_t * values, const int offset, const int count)
407{
408 auto context = static_cast<Context *>(ctx);
409
410 Cogs::assignFieldSubset(context, entityId, componentId, fieldId, values, offset, count);
411}
412
413void setMultiVector2Field(BridgeContext* ctx, EntityId entityId, const ComponentId componentId, const FieldId fieldId, const float * values, const int count)
414{
415 auto context = static_cast<Context *>(ctx);
416
417 Cogs::assignField(context, entityId, componentId, fieldId, reinterpret_cast<const glm::vec2 *>(values), count);
418}
419
420void setMultiVector3Field(BridgeContext* ctx, EntityId entityId, const ComponentId componentId, const FieldId fieldId, const float * values, const int count)
421{
422 auto context = static_cast<Context *>(ctx);
423
424 Cogs::assignField(context, entityId, componentId, fieldId, reinterpret_cast<const glm::vec3 *>(values), count);
425}
426
427void setMultiVector3FieldSubset(BridgeContext* ctx, EntityId entityId, const ComponentId componentId, const FieldId fieldId, const float * values, const int offset, const int count)
428{
429 auto context = static_cast<Context *>(ctx);
430
431 Cogs::assignFieldSubset(context, entityId, componentId, fieldId, reinterpret_cast<const glm::vec3 *>(values), offset, count);
432}
433
434void setMultiVector4Field(BridgeContext* ctx, EntityId entityId, const ComponentId componentId, const FieldId fieldId, const float * values, const int count)
435{
436 auto context = static_cast<Context *>(ctx);
437
438 Cogs::assignField(context, entityId, componentId, fieldId, reinterpret_cast<const glm::vec4 *>(values), count);
439}
440
441void setMultiVector4FieldSubset(BridgeContext* ctx, EntityId entityId, const ComponentId componentId, const FieldId fieldId, const float * values, const int offset, const int count)
442{
443 auto context = static_cast<Context *>(ctx);
444
445 Cogs::assignFieldSubset(context, entityId, componentId, fieldId, reinterpret_cast<const glm::vec4 *>(values), offset, count);
446}
447
448void setMultiVector2dField(BridgeContext* ctx, EntityId entityId, const ComponentId componentId, const FieldId fieldId, const double * values, const int count)
449{
450 auto context = static_cast<Context *>(ctx);
451
452 Cogs::assignField(context, entityId, componentId, fieldId, reinterpret_cast<const glm::dvec2 *>(values), count);
453}
454
455void setMultiVector3dField(BridgeContext* ctx, EntityId entityId, const ComponentId componentId, const FieldId fieldId, const double * values, const int count)
456{
457 auto context = static_cast<Context *>(ctx);
458
459 Cogs::assignField(context, entityId, componentId, fieldId, reinterpret_cast<const glm::dvec3 *>(values), count);
460}
461
462void setMultiVector4dField(BridgeContext* ctx, EntityId entityId, const ComponentId componentId, const FieldId fieldId, const double * values, const int count)
463{
464 auto context = static_cast<Context *>(ctx);
465
466 Cogs::assignField(context, entityId, componentId, fieldId, reinterpret_cast<const glm::dvec4 *>(values), count);
467}
468
469void setStringField(BridgeContext* ctx, EntityId entityId, const ComponentId componentId, const FieldId fieldId, const char * value)
470{
471 auto context = static_cast<Context *>(ctx);
472
473 Cogs::setField(context, entityId, componentId, fieldId, std::string(value));
474}
475
476void setMultiStringField(BridgeContext* ctx, EntityId entityId, const ComponentId componentId, const FieldId fieldId, const char ** value, const int count)
477{
478 auto context = static_cast<Context *>(ctx);
479
480 std::vector<std::string> strings(count);
481
482 for (int i = 0; i < count; ++i) {
483 strings[i] = std::string(value[i]);
484 }
485
486 Cogs::setField(context, entityId, componentId, fieldId, std::move(strings));
487}
488
489void getFieldValue(BridgeContext* ctx, EntityId entityId, const ComponentId componentId, const FieldId fieldId, void * value, int typeSize)
490{
491 auto context = static_cast<Context *>(ctx);
492
493 auto fieldPtr = Cogs::getField<uint8_t>(context, entityId, componentId, fieldId);
494
495 auto & componentType = Cogs::Reflection::TypeDatabase::getType(componentId);
496 auto field = componentType.getField(fieldId);
497 if (fieldPtr == nullptr) {
498 LOG_ERROR(logger, "getFieldValue: Invalid field '%d' in component '%s'.", fieldId, componentType.getName().c_str());
499 return;
500 }
501
502 auto & fieldType = Cogs::Reflection::TypeDatabase::getType(field->getTypeId());
503
504 // Incompatible types/sizes.
505 if (Cogs::Reflection::TypeDatabase::getType<bool>() == fieldType)
506 assert((typeSize == 1 || typeSize == 4) && "Field size mismatch");
507 else
508 assert(static_cast<int>(fieldType.getSize()) == typeSize && "Field size mismatch");
509
510 std::memset(value, 0, typeSize);
511 std::memcpy(value, fieldPtr, typeSize);
512}
513
514const char * getStringFieldValue(BridgeContext* ctx, EntityId entityId, const ComponentId componentId, const FieldId fieldId)
515{
516 auto context = static_cast<Context *>(ctx);
517 auto fieldPtr = Cogs::getField<std::string>(context, entityId, componentId, fieldId);
518
519 return fieldPtr->c_str();
520}
521
522void getVector3FieldValue(BridgeContext* ctx, EntityId entityId, const ComponentId componentId, const FieldId fieldId, float * value)
523{
524 auto context = static_cast<Context *>(ctx);
525
526 auto fieldPtr = Cogs::getField<glm::vec3>(context, entityId, componentId, fieldId);
527
528 *reinterpret_cast<glm::vec3 *>(value) = *fieldPtr;
529}
530
531/*
532* Stores multi string encoded into one string.
533* "" => []
534* "|" => [""]
535* "Hello|World|" => ["Hello", "World"]
536* "Hello\|World|" => ["Hello|World"]
537* "Hello\\|\\||World||" => ["Hello||", "World", ""]
538*/
539void setEncodedMultiStringField(BridgeContext* ctx, EntityId entityId, const ComponentId componentId, const FieldId fieldId, const char* value)
540{
541 auto context = static_cast<Context*>(ctx);
542
543 std::vector<std::string> strings = ::decodeMultiString(value);
544 Cogs::setField(context, entityId, componentId, fieldId, std::move(strings));
545}
546
547const char* getEncodedMultiStringFieldValue(BridgeContext* ctx, EntityId entityId, const ComponentId componentId, const FieldId fieldId)
548{
549 auto context = static_cast<Context*>(ctx);
550
551 const auto fieldPtr = Cogs::getField<std::vector<std::string>>(context, entityId, componentId, fieldId);
552
553 static std::string output;
554 output = ::encodeMultiString(*fieldPtr);
555 return output.c_str();
556}
557
558// See setEncodedMultiStringField details.
560int getEncodedMultiStringField(BridgeContext* ctx, EntityId entityId, const ComponentId componentId, const FieldId fieldId, char* value, int size)
561{
562 auto context = static_cast<Context*>(ctx);
563
564 const auto fieldPtr = Cogs::getField<std::vector<std::string>>(context, entityId, componentId, fieldId);
565
566 std::string output = ::encodeMultiString(*fieldPtr);
567
568 int actualLength = static_cast<int>(output.size());
569 int toCopy = std::min(actualLength + 1, size);
570 std::memcpy(value, output.data(), static_cast<size_t>(toCopy));
571 return actualLength;
572}
573
574int getMultiFieldValue(BridgeContext* ctx, EntityId entityId, const ComponentId componentId, const FieldId fieldId, void * value, int size)
575{
576 auto context = static_cast<Context *>(ctx);
577
578 auto entity = context->store->getEntityPtr(entityId);
579
580 if (!entity) {
581 return 0;
582 }
583
584 auto & componentType = Cogs::Reflection::TypeDatabase::getType(componentId);
585
586 assert(fieldId != Cogs::Reflection::NoField && "Invalid field id.");
587
588 auto field = componentType.getField(fieldId);
589
591
592 auto component = entity->getComponent<Cogs::ComponentModel::Component>(componentType);
593 assert(component != nullptr && "Null component - Not added to entity?");
594 auto fieldPtr = field->getPtr<void *>(component);
595
596 if (type == Cogs::Reflection::TypeDatabase::getType<std::vector<int32_t>>()) {
597 return getMultiValue<std::vector<int32_t>>(fieldPtr, value, size);
598 } else if (type == Cogs::Reflection::TypeDatabase::getType<std::vector<uint32_t>>()) {
599 return getMultiValue<std::vector<uint32_t>>(fieldPtr, value, size);
600 } else if (type == Cogs::Reflection::TypeDatabase::getType<std::vector<float>>()) {
601 return getMultiValue<std::vector<float>>(fieldPtr, value, size);
602 } else if (type == Cogs::Reflection::TypeDatabase::getType<std::vector<glm::vec2>>()) {
603 return getMultiValue<std::vector<glm::vec2>>(fieldPtr, value, size);
604 } else if (type == Cogs::Reflection::TypeDatabase::getType<std::vector<glm::vec3>>()) {
605 return getMultiValue<std::vector<glm::vec3>>(fieldPtr, value, size);
606 } else if (type == Cogs::Reflection::TypeDatabase::getType<std::vector<glm::vec4>>()) {
607 return getMultiValue<std::vector<glm::vec4>>(fieldPtr, value, size);
608 } else if (type == Cogs::Reflection::TypeDatabase::getType<std::vector<glm::quat>>()) {
609 return getMultiValue<std::vector<glm::quat>>(fieldPtr, value, size);
610 } else {
611 LOG_ERROR(logger, "getMultiFieldValue: Unsupported. Field: '%s' Type: '%s'", field->getName().c_str(), type.getName().c_str());
612 }
613
614 return 0;
615}
616
617ResourceId getResourceFieldValue(BridgeContext* ctx, EntityId entityId, const ComponentId componentId, const FieldId fieldId)
618{
619 auto context = static_cast<Context *>(ctx);
620
621 const auto fieldPtr = Cogs::getField<ResourceHandleBase>(context, entityId, componentId, fieldId);
622
623 auto id = fieldPtr->getId();
624 auto resource = fieldPtr->get();
625
626 if (id == NoResourceId && resource != nullptr) {
627 auto manager = resource->getOwner();
628 id = manager->getNextResourceId();
629 manager->setResourceId(resource, id);
630 }
631
632 return id;
633
634}
Base class for Component instances.
Definition: Component.h:143
A Context instance contains all the services, systems and runtime components needed to use Cogs.
Definition: Context.h:83
class EntityStore * store
Entity store.
Definition: Context.h:231
EntityPtr getEntity(const StringView &name, bool logIfNotFound=true) const
Retrieve a reference to the shared entity pointer to the Entity with the given name.
ComponentModel::Entity * getEntityPtr(const EntityId entityId)
Get a raw pointer to the entity with the given id.
Log implementation class.
Definition: LogManager.h:139
Field definition describing a single data member of a data structure.
Definition: Field.h:68
const Name & getName() const
Get the name of the field.
Definition: Field.h:136
TypeId getTypeId() const
Get the type id of the field.
Definition: Field.h:140
FieldValueType * getPtr(void *container) const
Get a pointer to this field on the given container.
Definition: Field.h:112
static const Type & getType()
Get the Type of the given template argument.
Definition: TypeDatabase.h:168
Represents a discrete type definition, describing a native type class.
Definition: Type.h:89
constexpr const Name & getName() const
Get the unique name of the type.
Definition: Type.h:198
const Field * getField(const Name &name) const
Get a pointer to the field info of the field with the given name.
Definition: Type.cpp:53
constexpr TypeId getTypeId() const
Get the unique Reflection::TypeId of this instance.
Definition: Type.h:325
Contains code for composing and managing entities built from components.
Contains the Engine, Renderer, resource managers and other systems needed to run Cogs....
std::shared_ptr< ComponentModel::Entity > EntityPtr
Smart pointer for Entity access.
Definition: EntityPtr.h:12
std::weak_ptr< ComponentModel::Entity > WeakEntityPtr
Weak Smart pointer for Entity access.
Definition: EntityPtr.h:18
constexpr Log getLogger(const char(&name)[LEN]) noexcept
Definition: LogManager.h:180
constexpr FieldId NoField
No field id.
Definition: Name.h:60
void assignField(Cogs::Core::Context *context, EntityId entityId, const ComponentId componentId, const Cogs::Reflection::FieldId fieldId, const DataType *data, int count)
Assign value to a Component field.
Definition: FieldSetter.h:35
const char * c_str() const
Gets the name as a null-terminated string.
Definition: Name.h:116