unit_compiler.cpp 27 KB

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