forked from SanderMertens/flecs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentity_name.c
671 lines (564 loc) · 15.4 KB
/
entity_name.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
/**
* @file entity_name.c
* @brief Functions for working with named entities.
*/
#include "private_api.h"
#include <ctype.h>
#define ECS_NAME_BUFFER_LENGTH (64)
static
bool flecs_path_append(
const ecs_world_t *world,
ecs_entity_t parent,
ecs_entity_t child,
const char *sep,
const char *prefix,
ecs_strbuf_t *buf)
{
ecs_poly_assert(world, ecs_world_t);
ecs_assert(sep[0] != 0, ECS_INVALID_PARAMETER, NULL);
ecs_entity_t cur = 0;
const char *name = NULL;
ecs_size_t name_len = 0;
if (child && ecs_is_alive(world, child)) {
cur = ecs_get_target(world, child, EcsChildOf, 0);
if (cur) {
ecs_assert(cur != child, ECS_CYCLE_DETECTED, NULL);
if (cur != parent && (cur != EcsFlecsCore || prefix != NULL)) {
flecs_path_append(world, parent, cur, sep, prefix, buf);
if (!sep[1]) {
ecs_strbuf_appendch(buf, sep[0]);
} else {
ecs_strbuf_appendstr(buf, sep);
}
}
} else if (prefix && prefix[0]) {
if (!prefix[1]) {
ecs_strbuf_appendch(buf, prefix[0]);
} else {
ecs_strbuf_appendstr(buf, prefix);
}
}
const EcsIdentifier *id = ecs_get_pair(
world, child, EcsIdentifier, EcsName);
if (id) {
name = id->value;
name_len = id->length;
}
}
if (name) {
ecs_strbuf_appendstrn(buf, name, name_len);
} else {
ecs_strbuf_appendint(buf, flecs_uto(int64_t, (uint32_t)child));
}
return cur != 0;
}
bool flecs_name_is_id(
const char *name)
{
ecs_assert(name != NULL, ECS_INTERNAL_ERROR, NULL);
if (!isdigit(name[0])) {
return false;
}
ecs_size_t i, length = ecs_os_strlen(name);
for (i = 1; i < length; i ++) {
char ch = name[i];
if (!isdigit(ch)) {
break;
}
}
return i >= length;
}
ecs_entity_t flecs_name_to_id(
const ecs_world_t *world,
const char *name)
{
int64_t result = atoll(name);
ecs_assert(result >= 0, ECS_INTERNAL_ERROR, NULL);
ecs_entity_t alive = ecs_get_alive(world, (ecs_entity_t)result);
if (alive) {
return alive;
} else {
if ((uint32_t)result == (uint64_t)result) {
return (ecs_entity_t)result;
} else {
return 0;
}
}
}
static
ecs_entity_t flecs_get_builtin(
const char *name)
{
if (name[0] == '.' && name[1] == '\0') {
return EcsThis;
} else if (name[0] == '*' && name[1] == '\0') {
return EcsWildcard;
} else if (name[0] == '_' && name[1] == '\0') {
return EcsAny;
} else if (name[0] == '$' && name[1] == '\0') {
return EcsVariable;
}
return 0;
}
static
bool flecs_is_sep(
const char **ptr,
const char *sep)
{
ecs_size_t len = ecs_os_strlen(sep);
if (!ecs_os_strncmp(*ptr, sep, len)) {
*ptr += len;
return true;
} else {
return false;
}
}
static
const char* flecs_path_elem(
const char *path,
const char *sep,
int32_t *len)
{
const char *ptr;
char ch;
int32_t template_nesting = 0;
int32_t count = 0;
for (ptr = path; (ch = *ptr); ptr ++) {
if (ch == '<') {
template_nesting ++;
} else if (ch == '>') {
template_nesting --;
}
ecs_check(template_nesting >= 0, ECS_INVALID_PARAMETER, path);
if (!template_nesting && flecs_is_sep(&ptr, sep)) {
break;
}
count ++;
}
if (len) {
*len = count;
}
if (count) {
return ptr;
} else {
return NULL;
}
error:
return NULL;
}
static
bool flecs_is_root_path(
const char *path,
const char *prefix)
{
if (prefix) {
return !ecs_os_strncmp(path, prefix, ecs_os_strlen(prefix));
} else {
return false;
}
}
static
ecs_entity_t flecs_get_parent_from_path(
const ecs_world_t *world,
ecs_entity_t parent,
const char **path_ptr,
const char *prefix,
bool new_entity)
{
bool start_from_root = false;
const char *path = *path_ptr;
if (flecs_is_root_path(path, prefix)) {
path += ecs_os_strlen(prefix);
parent = 0;
start_from_root = true;
}
if (!start_from_root && !parent && new_entity) {
parent = ecs_get_scope(world);
}
*path_ptr = path;
return parent;
}
static
void flecs_on_set_symbol(ecs_iter_t *it) {
EcsIdentifier *n = ecs_field(it, EcsIdentifier, 1);
ecs_world_t *world = it->world;
int i;
for (i = 0; i < it->count; i ++) {
ecs_entity_t e = it->entities[i];
flecs_name_index_ensure(
&world->symbols, e, n[i].value, n[i].length, n[i].hash);
}
}
void flecs_bootstrap_hierarchy(ecs_world_t *world) {
ecs_observer(world, {
.entity = ecs_entity(world, {.add = {ecs_childof(EcsFlecsInternals)}}),
.filter.terms[0] = {
.id = ecs_pair(ecs_id(EcsIdentifier), EcsSymbol),
.src.flags = EcsSelf
},
.callback = flecs_on_set_symbol,
.events = {EcsOnSet},
.yield_existing = true
});
}
/* Public functions */
void ecs_get_path_w_sep_buf(
const ecs_world_t *world,
ecs_entity_t parent,
ecs_entity_t child,
const char *sep,
const char *prefix,
ecs_strbuf_t *buf)
{
ecs_check(world != NULL, ECS_INVALID_PARAMETER, NULL);
ecs_check(buf != NULL, ECS_INVALID_PARAMETER, NULL);
world = ecs_get_world(world);
if (child == EcsWildcard) {
ecs_strbuf_appendch(buf, '*');
return;
}
if (child == EcsAny) {
ecs_strbuf_appendch(buf, '_');
return;
}
if (!sep) {
sep = ".";
}
if (!child || parent != child) {
flecs_path_append(world, parent, child, sep, prefix, buf);
} else {
ecs_strbuf_appendstrn(buf, "", 0);
}
error:
return;
}
char* ecs_get_path_w_sep(
const ecs_world_t *world,
ecs_entity_t parent,
ecs_entity_t child,
const char *sep,
const char *prefix)
{
ecs_strbuf_t buf = ECS_STRBUF_INIT;
ecs_get_path_w_sep_buf(world, parent, child, sep, prefix, &buf);
return ecs_strbuf_get(&buf);
}
ecs_entity_t ecs_lookup_child(
const ecs_world_t *world,
ecs_entity_t parent,
const char *name)
{
ecs_check(world != NULL, ECS_INTERNAL_ERROR, NULL);
world = ecs_get_world(world);
if (flecs_name_is_id(name)) {
ecs_entity_t result = flecs_name_to_id(world, name);
if (result && ecs_is_alive(world, result)) {
if (parent && !ecs_has_pair(world, result, EcsChildOf, parent)) {
return 0;
}
return result;
}
}
ecs_id_t pair = ecs_childof(parent);
ecs_hashmap_t *index = flecs_id_name_index_get(world, pair);
if (index) {
return flecs_name_index_find(index, name, 0, 0);
} else {
return 0;
}
error:
return 0;
}
ecs_entity_t ecs_lookup(
const ecs_world_t *world,
const char *path)
{
return ecs_lookup_path_w_sep(world, 0, path, ".", NULL, true);
}
ecs_entity_t ecs_lookup_symbol(
const ecs_world_t *world,
const char *name,
bool lookup_as_path,
bool recursive)
{
if (!name) {
return 0;
}
ecs_check(world != NULL, ECS_INTERNAL_ERROR, NULL);
world = ecs_get_world(world);
ecs_entity_t e = 0;
if (lookup_as_path) {
e = ecs_lookup_path_w_sep(world, 0, name, ".", NULL, recursive);
}
if (!e) {
e = flecs_name_index_find(&world->symbols, name, 0, 0);
}
return e;
error:
return 0;
}
ecs_entity_t ecs_lookup_path_w_sep(
const ecs_world_t *world,
ecs_entity_t parent,
const char *path,
const char *sep,
const char *prefix,
bool recursive)
{
if (!path) {
return 0;
}
ecs_check(world != NULL, ECS_INTERNAL_ERROR, NULL);
const ecs_world_t *stage = world;
world = ecs_get_world(world);
ecs_entity_t e = flecs_get_builtin(path);
if (e) {
return e;
}
e = flecs_name_index_find(&world->aliases, path, 0, 0);
if (e) {
return e;
}
char buff[ECS_NAME_BUFFER_LENGTH];
const char *ptr, *ptr_start;
char *elem = buff;
int32_t len, size = ECS_NAME_BUFFER_LENGTH;
ecs_entity_t cur;
bool lookup_path_search = false;
const ecs_entity_t *lookup_path = ecs_get_lookup_path(stage);
const ecs_entity_t *lookup_path_cur = lookup_path;
while (lookup_path_cur && *lookup_path_cur) {
lookup_path_cur ++;
}
if (!sep) {
sep = ".";
}
parent = flecs_get_parent_from_path(stage, parent, &path, prefix, true);
if (!sep[0]) {
return ecs_lookup_child(world, parent, path);
}
retry:
cur = parent;
ptr_start = ptr = path;
while ((ptr = flecs_path_elem(ptr, sep, &len))) {
if (len < size) {
ecs_os_memcpy(elem, ptr_start, len);
} else {
if (size == ECS_NAME_BUFFER_LENGTH) {
elem = NULL;
}
elem = ecs_os_realloc(elem, len + 1);
ecs_os_memcpy(elem, ptr_start, len);
size = len + 1;
}
elem[len] = '\0';
ptr_start = ptr;
cur = ecs_lookup_child(world, cur, elem);
if (!cur) {
goto tail;
}
}
tail:
if (!cur && recursive) {
if (!lookup_path_search) {
if (parent) {
parent = ecs_get_target(world, parent, EcsChildOf, 0);
goto retry;
} else {
lookup_path_search = true;
}
}
if (lookup_path_search) {
if (lookup_path_cur != lookup_path) {
lookup_path_cur --;
parent = lookup_path_cur[0];
goto retry;
}
}
}
if (elem != buff) {
ecs_os_free(elem);
}
return cur;
error:
return 0;
}
ecs_entity_t ecs_set_scope(
ecs_world_t *world,
ecs_entity_t scope)
{
ecs_check(world != NULL, ECS_INVALID_PARAMETER, NULL);
ecs_stage_t *stage = flecs_stage_from_world(&world);
ecs_entity_t cur = stage->scope;
stage->scope = scope;
return cur;
error:
return 0;
}
ecs_entity_t ecs_get_scope(
const ecs_world_t *world)
{
ecs_check(world != NULL, ECS_INVALID_PARAMETER, NULL);
const ecs_stage_t *stage = flecs_stage_from_readonly_world(world);
return stage->scope;
error:
return 0;
}
ecs_entity_t* ecs_set_lookup_path(
ecs_world_t *world,
const ecs_entity_t *lookup_path)
{
ecs_check(world != NULL, ECS_INVALID_PARAMETER, NULL);
ecs_stage_t *stage = flecs_stage_from_world(&world);
/* Safe: application owns lookup path */
ecs_entity_t *cur = ECS_CONST_CAST(ecs_entity_t*, stage->lookup_path);
stage->lookup_path = lookup_path;
return cur;
error:
return NULL;
}
ecs_entity_t* ecs_get_lookup_path(
const ecs_world_t *world)
{
ecs_check(world != NULL, ECS_INVALID_PARAMETER, NULL);
const ecs_stage_t *stage = flecs_stage_from_readonly_world(world);
/* Safe: application owns lookup path */
return ECS_CONST_CAST(ecs_entity_t*, stage->lookup_path);
error:
return NULL;
}
const char* ecs_set_name_prefix(
ecs_world_t *world,
const char *prefix)
{
ecs_poly_assert(world, ecs_world_t);
const char *old_prefix = world->info.name_prefix;
world->info.name_prefix = prefix;
return old_prefix;
}
static
void flecs_add_path(
ecs_world_t *world,
bool defer_suspend,
ecs_entity_t parent,
ecs_entity_t entity,
const char *name)
{
ecs_suspend_readonly_state_t srs;
ecs_world_t *real_world = NULL;
if (defer_suspend) {
real_world = flecs_suspend_readonly(world, &srs);
ecs_assert(real_world != NULL, ECS_INTERNAL_ERROR, NULL);
}
if (parent) {
ecs_add_pair(world, entity, EcsChildOf, parent);
}
ecs_set_name(world, entity, name);
if (defer_suspend) {
ecs_stage_t *stage = flecs_stage_from_world(&world);
flecs_resume_readonly(real_world, &srs);
flecs_defer_path(stage, parent, entity, name);
}
}
ecs_entity_t ecs_add_path_w_sep(
ecs_world_t *world,
ecs_entity_t entity,
ecs_entity_t parent,
const char *path,
const char *sep,
const char *prefix)
{
ecs_check(world != NULL, ECS_INVALID_PARAMETER, NULL);
if (!sep) {
sep = ".";
}
if (!path) {
if (!entity) {
entity = ecs_new_id(world);
}
if (parent) {
ecs_add_pair(world, entity, EcsChildOf, entity);
}
return entity;
}
bool root_path = flecs_is_root_path(path, prefix);
parent = flecs_get_parent_from_path(world, parent, &path, prefix, !entity);
char buff[ECS_NAME_BUFFER_LENGTH];
const char *ptr = path;
const char *ptr_start = path;
char *elem = buff;
int32_t len, size = ECS_NAME_BUFFER_LENGTH;
/* If we're in deferred/readonly mode suspend it, so that the name index is
* immediately updated. Without this, we could create multiple entities for
* the same name in a single command queue. */
bool suspend_defer = ecs_is_deferred(world) &&
(ecs_get_stage_count(world) <= 1);
ecs_entity_t cur = parent;
char *name = NULL;
if (sep[0]) {
while ((ptr = flecs_path_elem(ptr, sep, &len))) {
if (len < size) {
ecs_os_memcpy(elem, ptr_start, len);
} else {
if (size == ECS_NAME_BUFFER_LENGTH) {
elem = NULL;
}
elem = ecs_os_realloc(elem, len + 1);
ecs_os_memcpy(elem, ptr_start, len);
size = len + 1;
}
elem[len] = '\0';
ptr_start = ptr;
ecs_entity_t e = ecs_lookup_child(world, cur, elem);
if (!e) {
if (name) {
ecs_os_free(name);
}
name = ecs_os_strdup(elem);
/* If this is the last entity in the path, use the provided id */
bool last_elem = false;
if (!flecs_path_elem(ptr, sep, NULL)) {
e = entity;
last_elem = true;
}
if (!e) {
if (last_elem) {
ecs_entity_t prev = ecs_set_scope(world, 0);
e = ecs_new(world, 0);
ecs_set_scope(world, prev);
} else {
e = ecs_new_id(world);
}
}
if (!cur && last_elem && root_path) {
ecs_remove_pair(world, e, EcsChildOf, EcsWildcard);
}
flecs_add_path(world, suspend_defer, cur, e, name);
}
cur = e;
}
if (entity && (cur != entity)) {
ecs_throw(ECS_ALREADY_DEFINED, name);
}
if (name) {
ecs_os_free(name);
}
if (elem != buff) {
ecs_os_free(elem);
}
} else {
flecs_add_path(world, suspend_defer, parent, entity, path);
}
return cur;
error:
return 0;
}
ecs_entity_t ecs_new_from_path_w_sep(
ecs_world_t *world,
ecs_entity_t parent,
const char *path,
const char *sep,
const char *prefix)
{
return ecs_add_path_w_sep(world, 0, parent, path, sep, prefix);
}