unit_compiler.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  1. /*
  2. * Copyright (c) 2012-2024 Daniele Bartolini et al.
  3. * SPDX-License-Identifier: MIT
  4. */
  5. #include "config.h"
  6. #if CROWN_CAN_COMPILE
  7. #include "core/containers/array.inl"
  8. #include "core/containers/hash_map.inl"
  9. #include "core/filesystem/file_buffer.inl"
  10. #include "core/guid.inl"
  11. #include "core/json/json_object.inl"
  12. #include "core/json/sjson.h"
  13. #include "core/math/math.h"
  14. #include "core/memory/temp_allocator.inl"
  15. #include "core/strings/dynamic_string.inl"
  16. #include "core/strings/string_id.inl"
  17. #include "resource/compile_options.inl"
  18. #include "resource/physics_resource.h"
  19. #include "resource/unit_compiler.h"
  20. #include "resource/unit_resource.h"
  21. #include "world/types.h"
  22. #include <algorithm>
  23. namespace crown
  24. {
  25. struct ProjectionInfo
  26. {
  27. const char *name;
  28. ProjectionType::Enum type;
  29. };
  30. static const ProjectionInfo s_projection[] =
  31. {
  32. { "perspective", ProjectionType::PERSPECTIVE },
  33. { "orthographic", ProjectionType::ORTHOGRAPHIC }
  34. };
  35. CE_STATIC_ASSERT(countof(s_projection) == ProjectionType::COUNT);
  36. struct LightInfo
  37. {
  38. const char *name;
  39. LightType::Enum type;
  40. };
  41. static const LightInfo s_light[] =
  42. {
  43. { "directional", LightType::DIRECTIONAL },
  44. { "omni", LightType::OMNI },
  45. { "spot", LightType::SPOT }
  46. };
  47. CE_STATIC_ASSERT(countof(s_light) == LightType::COUNT);
  48. static ProjectionType::Enum projection_name_to_enum(const char *name)
  49. {
  50. for (u32 i = 0; i < countof(s_projection); ++i) {
  51. if (strcmp(name, s_projection[i].name) == 0)
  52. return s_projection[i].type;
  53. }
  54. return ProjectionType::COUNT;
  55. }
  56. static LightType::Enum light_name_to_enum(const char *name)
  57. {
  58. for (u32 i = 0; i < countof(s_light); ++i) {
  59. if (strcmp(name, s_light[i].name) == 0)
  60. return s_light[i].type;
  61. }
  62. return LightType::COUNT;
  63. }
  64. static s32 compile_transform(Buffer &output, const char *json, CompileOptions & /*opts*/)
  65. {
  66. TempAllocator4096 ta;
  67. JsonObject obj(ta);
  68. sjson::parse(obj, json);
  69. TransformDesc td;
  70. td.position = sjson::parse_vector3 (obj["position"]);
  71. td.rotation = sjson::parse_quaternion(obj["rotation"]);
  72. td.scale = sjson::parse_vector3 (obj["scale"]);
  73. FileBuffer fb(output);
  74. BinaryWriter bw(fb);
  75. bw.write(td.position);
  76. bw.write(td.rotation);
  77. bw.write(td.scale);
  78. return 0;
  79. }
  80. static s32 compile_camera(Buffer &output, const char *json, CompileOptions &opts)
  81. {
  82. TempAllocator4096 ta;
  83. JsonObject obj(ta);
  84. sjson::parse(obj, json);
  85. DynamicString type(ta);
  86. sjson::parse_string(type, obj["projection"]);
  87. ProjectionType::Enum pt = projection_name_to_enum(type.c_str());
  88. DATA_COMPILER_ASSERT(pt != ProjectionType::COUNT
  89. , opts
  90. , "Unknown projection type: '%s'"
  91. , type.c_str()
  92. );
  93. CameraDesc cd;
  94. cd.type = pt;
  95. cd.fov = sjson::parse_float(obj["fov"]);
  96. cd.near_range = sjson::parse_float(obj["near_range"]);
  97. cd.far_range = sjson::parse_float(obj["far_range"]);
  98. FileBuffer fb(output);
  99. BinaryWriter bw(fb);
  100. bw.write(cd.type);
  101. bw.write(cd.fov);
  102. bw.write(cd.near_range);
  103. bw.write(cd.far_range);
  104. return 0;
  105. }
  106. static s32 compile_mesh_renderer(Buffer &output, const char *json, CompileOptions &opts)
  107. {
  108. TempAllocator4096 ta;
  109. JsonObject obj(ta);
  110. sjson::parse(obj, json);
  111. DynamicString mesh_resource(ta);
  112. sjson::parse_string(mesh_resource, obj["mesh_resource"]);
  113. DATA_COMPILER_ASSERT_RESOURCE_EXISTS("mesh"
  114. , mesh_resource.c_str()
  115. , opts
  116. );
  117. opts.add_requirement("mesh", mesh_resource.c_str());
  118. DynamicString material(ta);
  119. sjson::parse_string(material, obj["material"]);
  120. DATA_COMPILER_ASSERT_RESOURCE_EXISTS("material"
  121. , material.c_str()
  122. , opts
  123. );
  124. opts.add_requirement("material", material.c_str());
  125. MeshRendererDesc mrd;
  126. mrd.mesh_resource = sjson::parse_resource_name(obj["mesh_resource"]);
  127. mrd.material_resource = sjson::parse_resource_name(obj["material"]);
  128. mrd.geometry_name = sjson::parse_string_id (obj["geometry_name"]);
  129. mrd.visible = sjson::parse_bool (obj["visible"]);
  130. mrd._pad0[0] = 0;
  131. mrd._pad0[1] = 0;
  132. mrd._pad0[2] = 0;
  133. FileBuffer fb(output);
  134. BinaryWriter bw(fb);
  135. bw.write(mrd.mesh_resource);
  136. bw.write(mrd.material_resource);
  137. bw.write(mrd.geometry_name);
  138. bw.write(mrd.visible);
  139. bw.write(mrd._pad0[0]);
  140. bw.write(mrd._pad0[1]);
  141. bw.write(mrd._pad0[2]);
  142. return 0;
  143. }
  144. static s32 compile_sprite_renderer(Buffer &output, const char *json, CompileOptions &opts)
  145. {
  146. TempAllocator4096 ta;
  147. JsonObject obj(ta);
  148. sjson::parse(obj, json);
  149. DynamicString sprite_resource(ta);
  150. sjson::parse_string(sprite_resource, obj["sprite_resource"]);
  151. DATA_COMPILER_ASSERT_RESOURCE_EXISTS("sprite"
  152. , sprite_resource.c_str()
  153. , opts
  154. );
  155. opts.add_requirement("sprite", sprite_resource.c_str());
  156. DynamicString material(ta);
  157. sjson::parse_string(material, obj["material"]);
  158. DATA_COMPILER_ASSERT_RESOURCE_EXISTS("material"
  159. , material.c_str()
  160. , opts
  161. );
  162. opts.add_requirement("material", material.c_str());
  163. SpriteRendererDesc srd;
  164. srd.sprite_resource = sjson::parse_resource_name(obj["sprite_resource"]);
  165. srd.material_resource = sjson::parse_resource_name(obj["material"]);
  166. srd.layer = sjson::parse_int (obj["layer"]);
  167. srd.depth = sjson::parse_int (obj["depth"]);
  168. srd.visible = sjson::parse_bool (obj["visible"]);
  169. srd._pad0[0] = 0;
  170. srd._pad0[1] = 0;
  171. srd._pad0[2] = 0;
  172. srd._pad1[0] = 0;
  173. srd._pad1[1] = 0;
  174. srd._pad1[2] = 0;
  175. srd._pad1[3] = 0;
  176. FileBuffer fb(output);
  177. BinaryWriter bw(fb);
  178. bw.write(srd.sprite_resource);
  179. bw.write(srd.material_resource);
  180. bw.write(srd.layer);
  181. bw.write(srd.depth);
  182. bw.write(srd.visible);
  183. bw.write(srd._pad0[0]);
  184. bw.write(srd._pad0[1]);
  185. bw.write(srd._pad0[2]);
  186. bw.write(srd._pad1[0]);
  187. bw.write(srd._pad1[1]);
  188. bw.write(srd._pad1[2]);
  189. bw.write(srd._pad1[3]);
  190. return 0;
  191. }
  192. static s32 compile_light(Buffer &output, const char *json, CompileOptions &opts)
  193. {
  194. TempAllocator4096 ta;
  195. JsonObject obj(ta);
  196. sjson::parse(obj, json);
  197. DynamicString type(ta);
  198. sjson::parse_string(type, obj["type"]);
  199. LightType::Enum lt = light_name_to_enum(type.c_str());
  200. DATA_COMPILER_ASSERT(lt != LightType::COUNT
  201. , opts
  202. , "Unknown light type: '%s'"
  203. , type.c_str()
  204. );
  205. LightDesc ld;
  206. ld.type = lt;
  207. ld.range = sjson::parse_float (obj["range"]);
  208. ld.intensity = sjson::parse_float (obj["intensity"]);
  209. ld.spot_angle = sjson::parse_float (obj["spot_angle"]);
  210. ld.color = sjson::parse_vector3(obj["color"]);
  211. FileBuffer fb(output);
  212. BinaryWriter bw(fb);
  213. bw.write(ld.type);
  214. bw.write(ld.range);
  215. bw.write(ld.intensity);
  216. bw.write(ld.spot_angle);
  217. bw.write(ld.color);
  218. return 0;
  219. }
  220. static s32 compile_script(Buffer &output, const char *json, CompileOptions &opts)
  221. {
  222. TempAllocator4096 ta;
  223. JsonObject obj(ta);
  224. sjson::parse(obj, json);
  225. DynamicString script_resource(ta);
  226. sjson::parse_string(script_resource, obj["script_resource"]);
  227. DATA_COMPILER_ASSERT_RESOURCE_EXISTS("lua"
  228. , script_resource.c_str()
  229. , opts
  230. );
  231. opts.add_requirement("lua", script_resource.c_str());
  232. ScriptDesc sd;
  233. sd.script_resource = sjson::parse_resource_name(obj["script_resource"]);
  234. FileBuffer fb(output);
  235. BinaryWriter bw(fb);
  236. bw.write(sd.script_resource);
  237. return 0;
  238. }
  239. static s32 compile_animation_state_machine(Buffer &output, const char *json, CompileOptions &opts)
  240. {
  241. TempAllocator4096 ta;
  242. JsonObject obj(ta);
  243. sjson::parse(obj, json);
  244. DynamicString state_machine_resource(ta);
  245. sjson::parse_string(state_machine_resource, obj["state_machine_resource"]);
  246. DATA_COMPILER_ASSERT_RESOURCE_EXISTS("state_machine"
  247. , state_machine_resource.c_str()
  248. , opts
  249. );
  250. opts.add_requirement("state_machine", state_machine_resource.c_str());
  251. AnimationStateMachineDesc asmd;
  252. asmd.state_machine_resource = sjson::parse_resource_name(obj["state_machine_resource"]);
  253. FileBuffer fb(output);
  254. BinaryWriter bw(fb);
  255. bw.write(asmd.state_machine_resource);
  256. return 0;
  257. }
  258. namespace unit_compiler
  259. {
  260. Buffer read_unit(UnitCompiler &c, const char *path)
  261. {
  262. Buffer buf = c._opts.read(path);
  263. array::push_back(buf, '\0');
  264. return buf;
  265. }
  266. // Collects the prefabs data of @a unit_json and its children recursively.
  267. // Prefab of deeper child is collected first.
  268. //
  269. // Consider the following hierarchy:
  270. //
  271. // Unit Prefab
  272. // A P
  273. // +-B
  274. // +-C Q
  275. // +-D +-R
  276. //
  277. // collect_prefab(A) will collect the data needed to compile
  278. // the unit 'A' and all its children:
  279. //
  280. // prefab_offsets[ 0] = P = { prefab = nil } <- Prefab of A
  281. // prefab_offsets[ 1] = Q = { prefab = nil } <- Prefab of C
  282. // prefab_offsets[n-1] = A = { prefab = P } <- A itself
  283. s32 collect_prefabs(UnitCompiler &c, StringId64 unit_name, const char *unit_json, bool append_data)
  284. {
  285. TempAllocator4096 ta;
  286. JsonObject prefab(ta);
  287. sjson::parse(prefab, unit_json);
  288. if (json_object::has(prefab, "children")) {
  289. JsonArray children(ta);
  290. sjson::parse_array(children, prefab["children"]);
  291. for (u32 i = 0; i < array::size(children); ++i) {
  292. s32 err = collect_prefabs(c, unit_name, children[i], false);
  293. DATA_COMPILER_ENSURE(err == 0, c._opts);
  294. }
  295. }
  296. if (json_object::has(prefab, "prefab")) {
  297. TempAllocator512 ta;
  298. DynamicString path(ta);
  299. sjson::parse_string(path, prefab["prefab"]);
  300. DATA_COMPILER_ASSERT_RESOURCE_EXISTS("unit"
  301. , path.c_str()
  302. , c._opts
  303. );
  304. StringId64 name(path.c_str());
  305. path += ".unit";
  306. Buffer buf = read_unit(c, path.c_str());
  307. s32 err = collect_prefabs(c, name, array::begin(buf), true);
  308. DATA_COMPILER_ENSURE(err == 0, c._opts);
  309. }
  310. if (append_data) {
  311. u32 prefab_offset = array::size(c._prefab_data);
  312. array::push(c._prefab_data, unit_json, strlen32(unit_json));
  313. array::push_back(c._prefab_data, '\0');
  314. array::push_back(c._prefab_offsets, prefab_offset);
  315. array::push_back(c._prefab_names, unit_name);
  316. }
  317. return 0;
  318. }
  319. const char *prefab_json(UnitCompiler &c, const char *prefab_name)
  320. {
  321. StringId64 name(prefab_name);
  322. for (u32 i = 0; i < array::size(c._prefab_names); ++i) {
  323. if (c._prefab_names[i] == name)
  324. return &c._prefab_data[c._prefab_offsets[i]];
  325. }
  326. return NULL;
  327. }
  328. u32 object_index(const JsonArray &objects, const Guid &object_id)
  329. {
  330. for (u32 i = 0; i < array::size(objects); ++i) {
  331. TempAllocator512 ta;
  332. JsonObject obj(ta);
  333. sjson::parse(obj, objects[i]);
  334. if (json_object::has(obj, "id")) {
  335. if (sjson::parse_guid(obj["id"]) == object_id)
  336. return i;
  337. } else {
  338. if (sjson::parse_guid(obj["_guid"]) == object_id)
  339. return i;
  340. }
  341. }
  342. return UINT32_MAX;
  343. }
  344. Unit *find_children(Unit *unit, Guid id)
  345. {
  346. CE_ENSURE(unit != NULL);
  347. auto cur = hash_map::begin(unit->_children);
  348. auto end = hash_map::end(unit->_children);
  349. for (; cur != end; ++cur) {
  350. HASH_MAP_SKIP_HOLE(unit->_children, cur);
  351. if (cur->first == id)
  352. return cur->second;
  353. Unit *child = find_children(cur->second, id);
  354. if (child != NULL)
  355. return child;
  356. }
  357. return NULL;
  358. }
  359. void delete_unit(Unit *unit)
  360. {
  361. auto cur = hash_map::begin(unit->_children);
  362. auto end = hash_map::end(unit->_children);
  363. for (; cur != end; ++cur) {
  364. HASH_MAP_SKIP_HOLE(unit->_children, cur);
  365. delete_unit(cur->second);
  366. }
  367. CE_DELETE(default_allocator(), unit);
  368. }
  369. s32 modify_unit_components(UnitCompiler &c, Unit *unit, const char *unit_json)
  370. {
  371. TempAllocator4096 ta;
  372. JsonObject obj(ta);
  373. sjson::parse(obj, unit_json);
  374. if (json_object::has(obj, "components")) {
  375. JsonArray components(ta);
  376. sjson::parse_array(components, obj["components"]);
  377. // Add components.
  378. for (u32 cc = 0; cc < array::size(components); ++cc) {
  379. JsonObject component(ta);
  380. sjson::parse_object(component, components[cc]);
  381. array::push_back(unit->_merged_components, components[cc]);
  382. array::push_back(unit->_merged_components_data, component["data"]);
  383. }
  384. }
  385. if (json_object::has(obj, "deleted_components")) {
  386. JsonObject deleted_components(ta);
  387. sjson::parse_object(deleted_components, obj["deleted_components"]);
  388. // Delete components.
  389. auto cur = json_object::begin(deleted_components);
  390. auto end = json_object::end(deleted_components);
  391. for (; cur != end; ++cur) {
  392. JSON_OBJECT_SKIP_HOLE(deleted_components, cur);
  393. auto key = cur->first;
  394. // Extract GUID from key "#xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx".
  395. char guid[37];
  396. strncpy(guid, key.data() + 1, sizeof(guid) - 1);
  397. guid[36] = '\0';
  398. Guid component_id = guid::parse(guid);
  399. u32 comp_idx = object_index(unit->_merged_components, component_id);
  400. if (comp_idx != UINT32_MAX) {
  401. u32 comp_last = array::size(unit->_merged_components) - 1;
  402. unit->_merged_components[comp_idx] = unit->_merged_components[comp_last];
  403. array::pop_back(unit->_merged_components);
  404. unit->_merged_components_data[comp_idx] = unit->_merged_components_data[comp_last];
  405. array::pop_back(unit->_merged_components_data);
  406. } else {
  407. char buf[GUID_BUF_LEN];
  408. DATA_COMPILER_ASSERT(false
  409. , c._opts
  410. , "Deletion of unexisting component ID: %s"
  411. , guid::to_string(buf, sizeof(buf), component_id)
  412. );
  413. }
  414. }
  415. }
  416. if (json_object::has(obj, "modified_components")) {
  417. JsonObject modified_components(ta);
  418. sjson::parse(modified_components, obj["modified_components"]);
  419. // Modify components.
  420. auto cur = json_object::begin(modified_components);
  421. auto end = json_object::end(modified_components);
  422. for (; cur != end; ++cur) {
  423. JSON_OBJECT_SKIP_HOLE(modified_components, cur);
  424. auto key = cur->first;
  425. auto val = cur->second;
  426. // Extract GUID from key "#xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx".
  427. char guid[37];
  428. strncpy(guid, key.data() + 1, sizeof(guid) - 1);
  429. guid[36] = '\0';
  430. Guid component_id = guid::parse(guid);
  431. // Patch component "data" key.
  432. u32 comp_idx = object_index(unit->_merged_components, component_id);
  433. if (comp_idx != UINT32_MAX) {
  434. JsonObject modified_component(ta);
  435. sjson::parse_object(modified_component, val);
  436. unit->_merged_components_data[comp_idx] = modified_component["data"];
  437. } else {
  438. char buf[GUID_BUF_LEN];
  439. DATA_COMPILER_ASSERT(false
  440. , c._opts
  441. , "Modification of unexisting component ID: %s"
  442. , guid::to_string(buf, sizeof(buf), component_id)
  443. );
  444. }
  445. }
  446. }
  447. return 0;
  448. }
  449. s32 parse_unit_internal(UnitCompiler &c
  450. , const char *unit_json
  451. , Unit *instance_unit
  452. , Unit *parent_unit
  453. )
  454. {
  455. TempAllocator4096 ta;
  456. JsonObject obj(ta);
  457. sjson::parse(obj, unit_json);
  458. Guid id = sjson::parse_guid(obj["_guid"]);
  459. Unit *unit = instance_unit;
  460. if (unit == NULL) {
  461. unit = CE_NEW(default_allocator(), Unit)(default_allocator());
  462. }
  463. if (instance_unit == NULL) {
  464. if (parent_unit == NULL) {
  465. hash_map::set(c._units, id, unit);
  466. } else {
  467. unit->_parent = parent_unit;
  468. hash_map::set(parent_unit->_children, id, unit);
  469. }
  470. }
  471. if (json_object::has(obj, "prefab")) {
  472. TempAllocator512 ta;
  473. DynamicString prefab(ta);
  474. sjson::parse_string(prefab, obj["prefab"]);
  475. const char *prefab_json_data = prefab_json(c, prefab.c_str());
  476. DATA_COMPILER_ASSERT(prefab_json_data != NULL
  477. , c._opts
  478. , "Unknown prefab: '%s'"
  479. , prefab.c_str()
  480. );
  481. s32 err = parse_unit_internal(c
  482. , prefab_json_data
  483. , unit
  484. , NULL
  485. );
  486. DATA_COMPILER_ENSURE(err == 0, c._opts);
  487. }
  488. s32 err = modify_unit_components(c, unit, unit_json);
  489. DATA_COMPILER_ENSURE(err == 0, c._opts);
  490. if (json_object::has(obj, "children")) {
  491. JsonArray children(ta);
  492. sjson::parse_array(children, obj["children"]);
  493. for (u32 cc = 0; cc < array::size(children); ++cc) {
  494. s32 err = parse_unit_internal(c
  495. , children[cc]
  496. , NULL
  497. , unit
  498. );
  499. DATA_COMPILER_ENSURE(err == 0, c._opts);
  500. }
  501. }
  502. if (json_object::has(obj, "deleted_children")) {
  503. JsonArray deleted_children(ta);
  504. sjson::parse_array(deleted_children, obj["deleted_children"]);
  505. // Delete children.
  506. for (u32 ii = 0; ii < array::size(deleted_children); ++ii) {
  507. JsonObject obj(ta);
  508. sjson::parse_object(obj, deleted_children[ii]);
  509. Guid id = sjson::parse_guid(obj["id"]);
  510. Unit *child = find_children(unit, id);
  511. char buf[GUID_BUF_LEN];
  512. DATA_COMPILER_ASSERT(child != NULL
  513. , c._opts
  514. , "Deletion of unexisting child ID: %s"
  515. , guid::to_string(buf, sizeof(buf), id)
  516. );
  517. Unit *child_parent = child->_parent;
  518. delete_unit(child);
  519. hash_map::remove(child_parent->_children, id);
  520. }
  521. }
  522. if (json_object::has(obj, "modified_children")) {
  523. JsonArray modified_children(ta);
  524. sjson::parse_array(modified_children, obj["modified_children"]);
  525. for (u32 ii = 0; ii < array::size(modified_children); ++ii) {
  526. JsonObject obj(ta);
  527. sjson::parse_object(obj, modified_children[ii]);
  528. Guid id = sjson::parse_guid(obj["id"]);
  529. Unit *child = find_children(unit, id);
  530. char buf[GUID_BUF_LEN];
  531. DATA_COMPILER_ASSERT(child != NULL
  532. , c._opts
  533. , "Modification of unexisting child ID: %s"
  534. , guid::to_string(buf, sizeof(buf), id)
  535. );
  536. s32 err = modify_unit_components(c, child, modified_children[ii]);
  537. DATA_COMPILER_ENSURE(err == 0, c._opts);
  538. }
  539. }
  540. // Parse unit's editor name.
  541. if (json_object::has(obj, "editor")) {
  542. JsonObject editor(ta);
  543. sjson::parse(editor, obj["editor"]);
  544. if (json_object::has(editor, "name"))
  545. unit->_editor_name = sjson::parse_string_id(editor["name"]);
  546. }
  547. return 0;
  548. }
  549. s32 parse_unit_from_json(UnitCompiler &c, const char *unit_json)
  550. {
  551. s32 err = collect_prefabs(c, StringId64(), unit_json, true);
  552. DATA_COMPILER_ENSURE(err == 0, c._opts);
  553. u32 original_unit = c._prefab_offsets[array::size(c._prefab_offsets) - 1];
  554. return parse_unit_internal(c, &c._prefab_data[original_unit], NULL, NULL);
  555. }
  556. s32 parse_unit(UnitCompiler &c, const char *path)
  557. {
  558. return parse_unit_from_json(c, array::begin(read_unit(c, path)));
  559. }
  560. s32 parse_unit_array_from_json(UnitCompiler &c, const char *units_array_json)
  561. {
  562. TempAllocator4096 ta;
  563. JsonArray units(ta);
  564. sjson::parse_array(units, units_array_json);
  565. Array<u32> original_units(default_allocator());
  566. for (u32 i = 0; i < array::size(units); ++i) {
  567. s32 err = collect_prefabs(c, StringId64(), units[i], true);
  568. DATA_COMPILER_ENSURE(err == 0, c._opts);
  569. u32 original_unit = c._prefab_offsets[array::size(c._prefab_offsets) - 1];
  570. array::push_back(original_units, original_unit);
  571. }
  572. for (u32 i = 0; i < array::size(units); ++i) {
  573. s32 err = parse_unit_internal(c, &c._prefab_data[original_units[i]], NULL, NULL);
  574. DATA_COMPILER_ENSURE(err == 0, c._opts);
  575. }
  576. return 0;
  577. }
  578. s32 flatten_unit(UnitCompiler &c, Unit *unit, u32 parent_unit_index)
  579. {
  580. const u32 unit_index = c._num_units;
  581. // Compile component data for each component type found
  582. // in the tree of units.
  583. for (u32 cc = 0; cc < array::size(unit->_merged_components); ++cc) {
  584. const char *component_json = unit->_merged_components[cc];
  585. TempAllocator512 ta;
  586. JsonObject component(ta);
  587. sjson::parse(component, component_json);
  588. StringId32 comp_type;
  589. if (json_object::has(component, "type")) {
  590. comp_type = sjson::parse_string_id(component["type"]);
  591. } else {
  592. comp_type = sjson::parse_string_id(component["_type"]);
  593. }
  594. // Append data to the component data for the given type.
  595. ComponentTypeData ctd_deffault(default_allocator());
  596. ComponentTypeData &ctd = const_cast<ComponentTypeData &>(hash_map::get(c._component_data, comp_type, ctd_deffault));
  597. DATA_COMPILER_ASSERT(&ctd != &ctd_deffault, c._opts, "Unknown component type");
  598. // Compile component.
  599. Buffer comp_data(default_allocator());
  600. s32 err = ctd._compiler(comp_data, unit->_merged_components_data[cc], c._opts);
  601. DATA_COMPILER_ENSURE(err == 0, c._opts);
  602. // One component per unit max.
  603. auto cur = array::begin(ctd._unit_index);
  604. auto end = array::end(ctd._unit_index);
  605. if (std::find(cur, end, unit_index) != end) {
  606. char buf[STRING_ID32_BUF_LEN];
  607. DATA_COMPILER_ASSERT(false
  608. , c._opts
  609. , "Unit already has a component of type: %s"
  610. , comp_type.to_string(buf, sizeof(buf))
  611. );
  612. }
  613. array::push(ctd._data, array::begin(comp_data), array::size(comp_data));
  614. array::push_back(ctd._unit_index, unit_index);
  615. ++ctd._num;
  616. }
  617. array::push_back(c._unit_parents, parent_unit_index);
  618. array::push_back(c._unit_names, unit->_editor_name);
  619. ++c._num_units;
  620. // Flatten children tree.
  621. auto cur = hash_map::begin(unit->_children);
  622. auto end = hash_map::end(unit->_children);
  623. for (; cur != end; ++cur) {
  624. HASH_MAP_SKIP_HOLE(unit->_children, cur);
  625. s32 err = flatten_unit(c, cur->second, unit_index);
  626. DATA_COMPILER_ENSURE(err == 0, c._opts);
  627. }
  628. return 0;
  629. }
  630. void register_component_compiler(UnitCompiler &c, StringId32 type, CompileFunction fn, f32 spawn_order)
  631. {
  632. ComponentTypeData ctd(default_allocator());
  633. ctd._compiler = fn;
  634. ComponentTypeInfo cti;
  635. cti._type = type;
  636. cti._spawn_order = spawn_order;
  637. hash_map::set(c._component_data, type, ctd);
  638. array::push_back(c._component_info, cti);
  639. std::sort(array::begin(c._component_info), array::end(c._component_info));
  640. }
  641. void register_component_compiler(UnitCompiler &c, const char *type, CompileFunction fn, f32 spawn_order)
  642. {
  643. register_component_compiler(c, StringId32(type), fn, spawn_order);
  644. }
  645. s32 flatten(UnitCompiler &c)
  646. {
  647. auto cur = hash_map::begin(c._units);
  648. auto end = hash_map::end(c._units);
  649. for (; cur != end; ++cur) {
  650. HASH_MAP_SKIP_HOLE(c._units, cur);
  651. s32 err = flatten_unit(c, cur->second, UINT32_MAX);
  652. DATA_COMPILER_ENSURE(err == 0, c._opts);
  653. }
  654. return 0;
  655. }
  656. Buffer blob(UnitCompiler &c)
  657. {
  658. Buffer output(default_allocator());
  659. FileBuffer fb(output);
  660. BinaryWriter bw(fb);
  661. flatten(c);
  662. // Count component types.
  663. u32 num_component_types = 0;
  664. auto cur = hash_map::begin(c._component_data);
  665. auto end = hash_map::end(c._component_data);
  666. for (; cur != end; ++cur) {
  667. HASH_MAP_SKIP_HOLE(c._component_data, cur);
  668. if (cur->second._num > 0)
  669. ++num_component_types;
  670. }
  671. // Write header.
  672. UnitResource ur;
  673. ur.version = RESOURCE_HEADER(RESOURCE_VERSION_UNIT);
  674. ur.num_units = c._num_units;
  675. ur.num_component_types = num_component_types;
  676. bw.write(ur.version);
  677. bw.write(ur.num_units);
  678. bw.write(ur.num_component_types);
  679. CE_ENSURE(c._num_units > 0);
  680. CE_ENSURE(array::size(c._unit_parents) > 0);
  681. CE_ENSURE(array::size(c._unit_parents) == c._num_units);
  682. // Write parents.
  683. for (u32 ii = 0; ii < c._num_units; ++ii)
  684. bw.write(c._unit_parents[ii]);
  685. for (u32 ii = 0; ii < array::size(c._component_info); ++ii) {
  686. const StringId32 comp_type = c._component_info[ii]._type;
  687. const ComponentTypeData ctd_deffault(default_allocator());
  688. const ComponentTypeData &ctd = hash_map::get(c._component_data, comp_type, ctd_deffault);
  689. if (ctd._num == 0)
  690. continue;
  691. // Write component data.
  692. ComponentData cd;
  693. cd.type = comp_type;
  694. cd.num_instances = ctd._num;
  695. cd.data_size = array::size(ctd._data);
  696. bw.align(alignof(cd));
  697. bw.write(cd.type);
  698. bw.write(cd.num_instances);
  699. bw.write(cd.data_size);
  700. for (u32 jj = 0; jj < array::size(ctd._unit_index); ++jj)
  701. bw.write(ctd._unit_index[jj]);
  702. bw.align(16);
  703. bw.write(array::begin(ctd._data), array::size(ctd._data));
  704. }
  705. return output;
  706. }
  707. } // namespace unit_compiler
  708. Unit::Unit(Allocator &a)
  709. : _merged_components(a)
  710. , _merged_components_data(a)
  711. , _children(a)
  712. , _parent(NULL)
  713. {
  714. }
  715. UnitCompiler::UnitCompiler(Allocator &a, CompileOptions &opts)
  716. : _units(a)
  717. , _opts(opts)
  718. , _prefab_data(a)
  719. , _prefab_offsets(a)
  720. , _prefab_names(a)
  721. , _component_data(a)
  722. , _component_info(a)
  723. , _unit_names(a)
  724. , _unit_parents(a)
  725. , _num_units(0)
  726. {
  727. unit_compiler::register_component_compiler(*this, "transform", &compile_transform, 0.0f);
  728. unit_compiler::register_component_compiler(*this, "camera", &compile_camera, 1.0f);
  729. unit_compiler::register_component_compiler(*this, "mesh_renderer", &compile_mesh_renderer, 1.0f);
  730. unit_compiler::register_component_compiler(*this, "sprite_renderer", &compile_sprite_renderer, 1.0f);
  731. unit_compiler::register_component_compiler(*this, "light", &compile_light, 1.0f);
  732. unit_compiler::register_component_compiler(*this, "script", &compile_script, 1.0f);
  733. unit_compiler::register_component_compiler(*this, "collider", &physics_resource_internal::compile_collider, 1.0f);
  734. unit_compiler::register_component_compiler(*this, "actor", &physics_resource_internal::compile_actor, 2.0f);
  735. unit_compiler::register_component_compiler(*this, "joint", &physics_resource_internal::compile_joint, 3.0f);
  736. unit_compiler::register_component_compiler(*this, "animation_state_machine", &compile_animation_state_machine, 1.0f);
  737. }
  738. UnitCompiler::~UnitCompiler()
  739. {
  740. auto cur = hash_map::begin(_units);
  741. auto end = hash_map::end(_units);
  742. for (; cur != end; ++cur) {
  743. HASH_MAP_SKIP_HOLE(_units, cur);
  744. unit_compiler::delete_unit(cur->second);
  745. }
  746. hash_map::clear(_units);
  747. }
  748. } // namespace crown
  749. #endif // if CROWN_CAN_COMPILE