unit_compiler.cpp 14 KB

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