unit_compiler.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. /*
  2. * Copyright (c) 2012-2020 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/dbartolini/crown/blob/master/LICENSE
  4. */
  5. #include "config.h"
  6. #if CROWN_CAN_COMPILE
  7. #include "core/containers/array.h"
  8. #include "core/containers/hash_map.h"
  9. #include "core/json/json_object.h"
  10. #include "core/json/sjson.h"
  11. #include "core/math/math.h"
  12. #include "core/memory/temp_allocator.h"
  13. #include "core/strings/dynamic_string.h"
  14. #include "resource/compile_options.h"
  15. #include "resource/physics_resource.h"
  16. #include "resource/unit_compiler.h"
  17. #include "resource/unit_resource.h"
  18. #include "world/types.h"
  19. #include <algorithm>
  20. namespace crown
  21. {
  22. struct ProjectionInfo
  23. {
  24. const char* name;
  25. ProjectionType::Enum type;
  26. };
  27. static const ProjectionInfo s_projection[] =
  28. {
  29. { "perspective", ProjectionType::PERSPECTIVE },
  30. { "orthographic", ProjectionType::ORTHOGRAPHIC }
  31. };
  32. CE_STATIC_ASSERT(countof(s_projection) == ProjectionType::COUNT);
  33. struct LightInfo
  34. {
  35. const char* name;
  36. LightType::Enum type;
  37. };
  38. static const LightInfo s_light[] =
  39. {
  40. { "directional", LightType::DIRECTIONAL },
  41. { "omni", LightType::OMNI },
  42. { "spot", LightType::SPOT }
  43. };
  44. CE_STATIC_ASSERT(countof(s_light) == LightType::COUNT);
  45. static ProjectionType::Enum projection_name_to_enum(const char* name)
  46. {
  47. for (u32 i = 0; i < countof(s_projection); ++i)
  48. {
  49. if (strcmp(name, s_projection[i].name) == 0)
  50. return s_projection[i].type;
  51. }
  52. return ProjectionType::COUNT;
  53. }
  54. static LightType::Enum light_name_to_enum(const char* name)
  55. {
  56. for (u32 i = 0; i < countof(s_light); ++i)
  57. {
  58. if (strcmp(name, s_light[i].name) == 0)
  59. return s_light[i].type;
  60. }
  61. return LightType::COUNT;
  62. }
  63. static s32 compile_transform(Buffer& output, const char* json, CompileOptions& /*opts*/)
  64. {
  65. TempAllocator4096 ta;
  66. JsonObject obj(ta);
  67. sjson::parse(json, obj);
  68. TransformDesc td;
  69. td.position = sjson::parse_vector3 (obj["position"]);
  70. td.rotation = sjson::parse_quaternion(obj["rotation"]);
  71. td.scale = sjson::parse_vector3 (obj["scale"]);
  72. array::push(output, (char*)&td, sizeof(td));
  73. return 0;
  74. }
  75. static s32 compile_camera(Buffer& output, const char* json, CompileOptions& opts)
  76. {
  77. TempAllocator4096 ta;
  78. JsonObject obj(ta);
  79. sjson::parse(json, obj);
  80. DynamicString type(ta);
  81. sjson::parse_string(obj["projection"], type);
  82. ProjectionType::Enum pt = projection_name_to_enum(type.c_str());
  83. DATA_COMPILER_ASSERT(pt != ProjectionType::COUNT
  84. , opts
  85. , "Unknown projection type: '%s'"
  86. , type.c_str()
  87. );
  88. CameraDesc cd;
  89. cd.type = pt;
  90. cd.fov = sjson::parse_float(obj["fov"]);
  91. cd.near_range = sjson::parse_float(obj["near_range"]);
  92. cd.far_range = sjson::parse_float(obj["far_range"]);
  93. array::push(output, (char*)&cd, sizeof(cd));
  94. return 0;
  95. }
  96. static s32 compile_mesh_renderer(Buffer& output, const char* json, CompileOptions& opts)
  97. {
  98. TempAllocator4096 ta;
  99. JsonObject obj(ta);
  100. sjson::parse(json, obj);
  101. DynamicString mesh_resource(ta);
  102. sjson::parse_string(obj["mesh_resource"], mesh_resource);
  103. DATA_COMPILER_ASSERT_RESOURCE_EXISTS("mesh"
  104. , mesh_resource.c_str()
  105. , opts
  106. );
  107. opts.add_requirement("mesh", mesh_resource.c_str());
  108. DynamicString material(ta);
  109. sjson::parse_string(obj["material"], material);
  110. DATA_COMPILER_ASSERT_RESOURCE_EXISTS("material"
  111. , material.c_str()
  112. , opts
  113. );
  114. opts.add_requirement("material", material.c_str());
  115. MeshRendererDesc mrd;
  116. mrd.mesh_resource = sjson::parse_resource_name(obj["mesh_resource"]);
  117. mrd.geometry_name = sjson::parse_string_id (obj["geometry_name"]);
  118. mrd.material_resource = sjson::parse_resource_name(obj["material"]);
  119. mrd.visible = sjson::parse_bool (obj["visible"]);
  120. mrd._pad0[0] = 0;
  121. mrd._pad0[1] = 0;
  122. mrd._pad0[2] = 0;
  123. array::push(output, (char*)&mrd, sizeof(mrd));
  124. return 0;
  125. }
  126. static s32 compile_sprite_renderer(Buffer& output, const char* json, CompileOptions& opts)
  127. {
  128. TempAllocator4096 ta;
  129. JsonObject obj(ta);
  130. sjson::parse(json, obj);
  131. DynamicString sprite_resource(ta);
  132. sjson::parse_string(obj["sprite_resource"], sprite_resource);
  133. DATA_COMPILER_ASSERT_RESOURCE_EXISTS("sprite"
  134. , sprite_resource.c_str()
  135. , opts
  136. );
  137. opts.add_requirement("sprite", sprite_resource.c_str());
  138. DynamicString material(ta);
  139. sjson::parse_string(obj["material"], material);
  140. DATA_COMPILER_ASSERT_RESOURCE_EXISTS("material"
  141. , material.c_str()
  142. , opts
  143. );
  144. opts.add_requirement("material", material.c_str());
  145. SpriteRendererDesc srd;
  146. srd.sprite_resource = sjson::parse_resource_name(obj["sprite_resource"]);
  147. srd.material_resource = sjson::parse_resource_name(obj["material"]);
  148. srd.layer = sjson::parse_int (obj["layer"]);
  149. srd.depth = sjson::parse_int (obj["depth"]);
  150. srd.visible = sjson::parse_bool (obj["visible"]);
  151. srd._pad0[0] = 0;
  152. srd._pad0[1] = 0;
  153. srd._pad0[2] = 0;
  154. srd._pad1[0] = 0;
  155. srd._pad1[1] = 0;
  156. srd._pad1[2] = 0;
  157. srd._pad1[3] = 0;
  158. array::push(output, (char*)&srd, sizeof(srd));
  159. return 0;
  160. }
  161. static s32 compile_light(Buffer& output, const char* json, CompileOptions& opts)
  162. {
  163. TempAllocator4096 ta;
  164. JsonObject obj(ta);
  165. sjson::parse(json, obj);
  166. DynamicString type(ta);
  167. sjson::parse_string(obj["type"], type);
  168. LightType::Enum lt = light_name_to_enum(type.c_str());
  169. DATA_COMPILER_ASSERT(lt != LightType::COUNT
  170. , opts
  171. , "Unknown light type: '%s'"
  172. , type.c_str()
  173. );
  174. LightDesc ld;
  175. ld.type = lt;
  176. ld.range = sjson::parse_float (obj["range"]);
  177. ld.intensity = sjson::parse_float (obj["intensity"]);
  178. ld.spot_angle = sjson::parse_float (obj["spot_angle"]);
  179. ld.color = sjson::parse_vector3(obj["color"]);
  180. array::push(output, (char*)&ld, sizeof(ld));
  181. return 0;
  182. }
  183. static s32 compile_script(Buffer& output, const char* json, CompileOptions& opts)
  184. {
  185. TempAllocator4096 ta;
  186. JsonObject obj(ta);
  187. sjson::parse(json, obj);
  188. DynamicString script_resource(ta);
  189. sjson::parse_string(obj["script_resource"], script_resource);
  190. DATA_COMPILER_ASSERT_RESOURCE_EXISTS("lua"
  191. , script_resource.c_str()
  192. , opts
  193. );
  194. opts.add_requirement("lua", script_resource.c_str());
  195. ScriptDesc sd;
  196. sd.script_resource = sjson::parse_resource_name(obj["script_resource"]);
  197. array::push(output, (char*)&sd, sizeof(sd));
  198. return 0;
  199. }
  200. static s32 compile_animation_state_machine(Buffer& output, const char* json, CompileOptions& opts)
  201. {
  202. TempAllocator4096 ta;
  203. JsonObject obj(ta);
  204. sjson::parse(json, obj);
  205. DynamicString state_machine_resource(ta);
  206. sjson::parse_string(obj["state_machine_resource"], state_machine_resource);
  207. DATA_COMPILER_ASSERT_RESOURCE_EXISTS("state_machine"
  208. , state_machine_resource.c_str()
  209. , opts
  210. );
  211. opts.add_requirement("state_machine", state_machine_resource.c_str());
  212. AnimationStateMachineDesc asmd;
  213. asmd.state_machine_resource = sjson::parse_resource_name(obj["state_machine_resource"]);
  214. array::push(output, (char*)&asmd, sizeof(asmd));
  215. return 0;
  216. }
  217. UnitCompiler::UnitCompiler(CompileOptions& opts)
  218. : _opts(opts)
  219. , _num_units(0)
  220. , _component_data(default_allocator())
  221. , _component_info(default_allocator())
  222. , _unit_names(default_allocator())
  223. {
  224. register_component_compiler("transform", &compile_transform, 0.0f);
  225. register_component_compiler("camera", &compile_camera, 1.0f);
  226. register_component_compiler("mesh_renderer", &compile_mesh_renderer, 1.0f);
  227. register_component_compiler("sprite_renderer", &compile_sprite_renderer, 1.0f);
  228. register_component_compiler("light", &compile_light, 1.0f);
  229. register_component_compiler("script", &compile_script, 1.0f);
  230. register_component_compiler("collider", &physics_resource_internal::compile_collider, 1.0f);
  231. register_component_compiler("actor", &physics_resource_internal::compile_actor, 2.0f);
  232. register_component_compiler("joint", &physics_resource_internal::compile_joint, 3.0f);
  233. register_component_compiler("animation_state_machine", &compile_animation_state_machine, 1.0f);
  234. }
  235. Buffer UnitCompiler::read_unit(const char* path)
  236. {
  237. Buffer buf = _opts.read(path);
  238. array::push_back(buf, '\0');
  239. return buf;
  240. }
  241. s32 UnitCompiler::compile_unit(const char* path)
  242. {
  243. return compile_unit_from_json(array::begin(read_unit(path)));
  244. }
  245. u32 component_index(const JsonArray& components, const StringView& id)
  246. {
  247. char guid[37];
  248. strncpy(guid, id.data(), sizeof(guid) - 1);
  249. guid[36] = '\0';
  250. Guid idd = guid::parse(guid);
  251. for (u32 i = 0; i < array::size(components); ++i)
  252. {
  253. TempAllocator512 ta;
  254. JsonObject obj(ta);
  255. sjson::parse(components[i], obj);
  256. if (sjson::parse_guid(obj["id"]) == idd)
  257. return i;
  258. }
  259. return UINT32_MAX;
  260. }
  261. s32 UnitCompiler::compile_unit_from_json(const char* json)
  262. {
  263. Buffer data(default_allocator());
  264. array::reserve(data, 1024*1024);
  265. u32 num_prefabs = 1;
  266. TempAllocator4096 ta;
  267. JsonObject prefabs[4] = { JsonObject(ta), JsonObject(ta), JsonObject(ta), JsonObject(ta) };
  268. sjson::parse(json, prefabs[0]);
  269. for (u32 i = 0; i < countof(prefabs); ++i, ++num_prefabs)
  270. {
  271. const JsonObject& prefab = prefabs[i];
  272. if (!json_object::has(prefab, "prefab"))
  273. break;
  274. TempAllocator512 ta;
  275. DynamicString path(ta);
  276. sjson::parse_string(prefab["prefab"], path);
  277. DATA_COMPILER_ASSERT_RESOURCE_EXISTS("unit"
  278. , path.c_str()
  279. , _opts
  280. );
  281. path += ".unit";
  282. Buffer buf = read_unit(path.c_str());
  283. const char* d = array::end(data);
  284. array::push(data, array::begin(buf), array::size(buf));
  285. sjson::parse(d, prefabs[i + 1]);
  286. }
  287. JsonObject& prefab_root = prefabs[num_prefabs - 1];
  288. JsonArray prefab_root_components_original(ta);
  289. sjson::parse_array(prefab_root["components"], prefab_root_components_original);
  290. JsonArray prefab_root_components(ta);
  291. sjson::parse_array(prefab_root["components"], prefab_root_components);
  292. if (num_prefabs > 1)
  293. {
  294. // Merge prefabs' components
  295. for (u32 i = 0; i < num_prefabs; ++i)
  296. {
  297. const JsonObject& prefab = prefabs[num_prefabs - i - 1];
  298. if (!json_object::has(prefab, "modified_components"))
  299. continue;
  300. JsonObject modified_components(ta);
  301. sjson::parse(prefab["modified_components"], modified_components);
  302. auto cur = json_object::begin(modified_components);
  303. auto end = json_object::end(modified_components);
  304. for (; cur != end; ++cur)
  305. {
  306. JSON_OBJECT_SKIP_HOLE(modified_components, cur);
  307. const StringView key = cur->first;
  308. const StringView id(&key.data()[1], key.length()-1);
  309. const char* value = cur->second;
  310. u32 comp_index = component_index(prefab_root_components_original, id);
  311. if (comp_index != UINT32_MAX)
  312. prefab_root_components[comp_index] = value;
  313. }
  314. }
  315. }
  316. if (array::size(prefab_root_components) > 0)
  317. {
  318. for (u32 i = 0; i < array::size(prefab_root_components); ++i)
  319. {
  320. const char* value = prefab_root_components[i];
  321. TempAllocator512 ta;
  322. JsonObject component(ta);
  323. sjson::parse(value, component);
  324. const StringId32 type = sjson::parse_string_id(component["type"]);
  325. Buffer output(default_allocator());
  326. if (compile_component(output, type, component["data"]) != 0)
  327. return -1;
  328. add_component_data(type, output, _num_units);
  329. }
  330. }
  331. // Unnamed objects have all-zero hash
  332. StringId32 name_hash;
  333. // Parse Level Editor data
  334. if (json_object::has(prefabs[0], "editor"))
  335. {
  336. JsonObject editor(ta);
  337. sjson::parse(prefabs[0]["editor"], editor);
  338. if (json_object::has(editor, "name"))
  339. name_hash = sjson::parse_string_id(editor["name"]);
  340. }
  341. array::push_back(_unit_names, name_hash);
  342. ++_num_units;
  343. return 0;
  344. }
  345. s32 UnitCompiler::compile_multiple_units(const char* json)
  346. {
  347. TempAllocator4096 ta;
  348. JsonArray units(ta);
  349. sjson::parse_array(json, units);
  350. for (u32 i = 0; i < array::size(units); ++i)
  351. {
  352. if (compile_unit_from_json(units[i]) != 0)
  353. return -1;
  354. }
  355. return 0;
  356. }
  357. Buffer UnitCompiler::blob()
  358. {
  359. UnitResource ur;
  360. ur.version = RESOURCE_HEADER(RESOURCE_VERSION_UNIT);
  361. ur.num_units = _num_units;
  362. ur.num_component_types = 0;
  363. auto cur = hash_map::begin(_component_data);
  364. auto end = hash_map::end(_component_data);
  365. for (; cur != end; ++cur)
  366. {
  367. HASH_MAP_SKIP_HOLE(_component_data, cur);
  368. const u32 num = cur->second._num;
  369. if (num > 0)
  370. ++ur.num_component_types;
  371. }
  372. Buffer buf(default_allocator());
  373. array::push(buf, (char*)&ur, sizeof(ur));
  374. for (u32 i = 0; i < array::size(_component_info); ++i)
  375. {
  376. const StringId32 type = _component_info[i]._type;
  377. const ComponentTypeData& ctd = hash_map::get(_component_data, type, ComponentTypeData(default_allocator()));
  378. const Buffer& data = ctd._data;
  379. const Array<u32>& unit_index = ctd._unit_index;
  380. const u32 num = ctd._num;
  381. if (num > 0)
  382. {
  383. ComponentData cd;
  384. cd.type = type;
  385. cd.num_instances = num;
  386. cd.size = array::size(data) + sizeof(u32)*array::size(unit_index);
  387. const u32 pad = cd.size % alignof(cd);
  388. cd.size += pad;
  389. array::push(buf, (char*)&cd, sizeof(cd));
  390. array::push(buf, (char*)array::begin(unit_index), sizeof(u32)*array::size(unit_index));
  391. array::push(buf, array::begin(data), array::size(data));
  392. // Insert proper padding
  393. for (u32 i = 0; i < pad; ++i)
  394. array::push_back(buf, (char)0);
  395. }
  396. }
  397. return buf;
  398. }
  399. void UnitCompiler::add_component_data(StringId32 type, const Buffer& data, u32 unit_index)
  400. {
  401. ComponentTypeData component_types_deffault(default_allocator());
  402. ComponentTypeData& ctd = const_cast<ComponentTypeData&>(hash_map::get(_component_data, type, component_types_deffault));
  403. array::push(ctd._data, array::begin(data), array::size(data));
  404. array::push_back(ctd._unit_index, unit_index);
  405. ++ctd._num;
  406. }
  407. void UnitCompiler::register_component_compiler(const char* type, CompileFunction fn, f32 spawn_order)
  408. {
  409. register_component_compiler(StringId32(type), fn, spawn_order);
  410. }
  411. void UnitCompiler::register_component_compiler(StringId32 type, CompileFunction fn, f32 spawn_order)
  412. {
  413. ComponentTypeData ctd(default_allocator());
  414. ctd._compiler = fn;
  415. ComponentTypeInfo cti;
  416. cti._type = type;
  417. cti._spawn_order = spawn_order;
  418. hash_map::set(_component_data, type, ctd);
  419. array::push_back(_component_info, cti);
  420. std::sort(array::begin(_component_info), array::end(_component_info));
  421. }
  422. s32 UnitCompiler::compile_component(Buffer& output, StringId32 type, const char* json)
  423. {
  424. DATA_COMPILER_ASSERT(hash_map::has(_component_data, type), _opts, "Unknown component");
  425. return hash_map::get(_component_data, type, ComponentTypeData(default_allocator()))._compiler(output, json, _opts);
  426. }
  427. } // namespace crown
  428. #endif // CROWN_CAN_COMPILE