database.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  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/strings/string.h"
  6. #include "core/types.h"
  7. #include "core/guid.h"
  8. #include "core/memory/allocator.h"
  9. #include "core/math/types.h"
  10. #include "core/memory/memory.h"
  11. #include "core/containers/hash_map.h"
  12. #include "core/strings/string_stream.h"
  13. #include "core/memory/temp_allocator.h"
  14. #include "core/strings/dynamic_string.h"
  15. #include "core/math/quaternion.h"
  16. #include "core/math/vector3.h"
  17. #include "core/json/types.h"
  18. #include "core/json/sjson.h"
  19. #include "core/json/json_object.h"
  20. #include "core/containers/vector.h"
  21. #include "core/filesystem/filesystem_disk.h"
  22. #include "core/filesystem/file.h"
  23. #include "core/containers/hash_set.h"
  24. namespace crown
  25. {
  26. struct ActionType
  27. {
  28. enum Enum
  29. {
  30. CREATE,
  31. DESTROY,
  32. SET_PROPERTY_NULL,
  33. SET_PROPERTY_BOOL,
  34. SET_PROPERTY_DOUBLE,
  35. SET_PROPERTY_STRING,
  36. SET_PROPERTY_GUID,
  37. SET_PROPERTY_VECTOR3,
  38. SET_PROPERTY_QUATERNION,
  39. ADD_TO_SET,
  40. REMOVE_FROM_SET,
  41. RESTORE_POINT
  42. };
  43. };
  44. struct ValueType
  45. {
  46. enum Enum
  47. {
  48. NIL,
  49. BOOL,
  50. DOUBLE,
  51. STRING,
  52. GUID,
  53. VECTOR3,
  54. QUATERNION,
  55. SET,
  56. };
  57. };
  58. struct Ledger
  59. {
  60. Allocator* _allocator;
  61. u8* _data;
  62. u32 _read;
  63. Ledger(Allocator& a)
  64. : _allocator(&a)
  65. , _data(NULL)
  66. , _read(0)
  67. {
  68. _data = (u8*)a.allocate(1024*1024);
  69. _read = 0;
  70. }
  71. ~Ledger()
  72. {
  73. _allocator->deallocate(_data);
  74. }
  75. void clear()
  76. {
  77. _read = 0;
  78. }
  79. u32 size()
  80. {
  81. return _read;
  82. }
  83. void write(const void* data, u32 size)
  84. {
  85. memcpy(&_data[_read], data, size);
  86. _read += size;
  87. }
  88. template <typename T>
  89. void write(const T& data)
  90. {
  91. write(&data, sizeof(T));
  92. }
  93. void read(void* data, u32 size)
  94. {
  95. _read -= size;
  96. memcpy(data, &_data[_read], size);
  97. }
  98. template <typename T>
  99. void read(T& data)
  100. {
  101. read(&data, sizeof(T));
  102. }
  103. ValueType::Enum read_value_type()
  104. {
  105. _read -= (u32)sizeof(u32);
  106. u32 a = *(u32*)(&_data[_read]);
  107. return (ValueType::Enum)a;
  108. }
  109. void write_create_action(Guid id)
  110. {
  111. write(id);
  112. write(ActionType::CREATE);
  113. }
  114. void write_destroy_action(Guid id)
  115. {
  116. write(id);
  117. write(ActionType::DESTROY);
  118. }
  119. void write_set_property_null(Guid id, const char* key)
  120. {
  121. const u32 key_len = strlen32(key) + 1;
  122. write(key, key_len);
  123. write(key_len);
  124. write(id);
  125. write(ActionType::SET_PROPERTY_NULL);
  126. }
  127. void write_set_property_bool(Guid id, const char* key, bool value)
  128. {
  129. write(value);
  130. const u32 key_len = strlen32(key) + 1;
  131. write(key, key_len);
  132. write(key_len);
  133. write(id);
  134. write(ActionType::SET_PROPERTY_BOOL);
  135. }
  136. void write_set_property_double(Guid id, const char* key, double value)
  137. {
  138. write(value);
  139. const u32 key_len = strlen32(key) + 1;
  140. write(key, key_len);
  141. write(key_len);
  142. write(id);
  143. write(ActionType::SET_PROPERTY_DOUBLE);
  144. }
  145. void write_set_property_string(Guid id, const char* key, const char* value)
  146. {
  147. const u32 value_len = strlen32(value) + 1;
  148. write(value, value_len);
  149. write(value_len);
  150. const u32 key_len = strlen32(key) + 1;
  151. write(key, key_len);
  152. write(key_len);
  153. write(id);
  154. write(ActionType::SET_PROPERTY_STRING);
  155. }
  156. void write_set_property_guid(Guid id, const char* key, const Guid& value)
  157. {
  158. write(value);
  159. const u32 key_len = strlen32(key) + 1;
  160. write(key, key_len);
  161. write(key_len);
  162. write(id);
  163. write(ActionType::SET_PROPERTY_GUID);
  164. }
  165. void write_set_property_vector3(Guid id, const char* key, const Vector3& value)
  166. {
  167. write(value);
  168. const u32 key_len = strlen32(key) + 1;
  169. write(key, key_len);
  170. write(key_len);
  171. write(id);
  172. write(ActionType::SET_PROPERTY_VECTOR3);
  173. }
  174. void write_set_property_quaternion(Guid id, const char* key, const Quaternion& value)
  175. {
  176. write(value);
  177. const u32 key_len = strlen32(key) + 1;
  178. write(key, key_len);
  179. write(key_len);
  180. write(id);
  181. write(ActionType::SET_PROPERTY_QUATERNION);
  182. }
  183. void write_add_to_set_action(Guid id, const char* key, Guid item_id)
  184. {
  185. write(item_id);
  186. const u32 key_len = strlen32(key) + 1;
  187. write(key, key_len);
  188. write(key_len);
  189. write(id);
  190. write(ActionType::ADD_TO_SET);
  191. }
  192. void write_remove_from_set_action(Guid id, const char* key, Guid item_id)
  193. {
  194. write(item_id);
  195. const u32 key_len = strlen32(key) + 1;
  196. write(key, key_len);
  197. write(key_len);
  198. write(id);
  199. write(ActionType::REMOVE_FROM_SET);
  200. }
  201. void write_restore_point(int id, u32 size, Guid* begin, Guid* end)
  202. {
  203. const u32 num_guids = end - begin;
  204. for (u32 i = 0; i < num_guids; ++i)
  205. write(begin[i]);
  206. write(num_guids);
  207. write(size);
  208. write(id);
  209. write(ActionType::RESTORE_POINT);
  210. }
  211. u32 peek_type()
  212. {
  213. return *(u32*)(&_data[_read - (u32)sizeof(u32)]);
  214. }
  215. };
  216. struct Value
  217. {
  218. Value()
  219. : allocator(NULL)
  220. {
  221. }
  222. ~Value()
  223. {
  224. if (allocator != NULL)
  225. allocator->deallocate(value_string);
  226. }
  227. Value(const Value& other)
  228. {
  229. if (type == ValueType::STRING)
  230. allocator->deallocate(value_string);
  231. type = other.type;
  232. switch (type)
  233. {
  234. case ValueType::NIL:
  235. break;
  236. case ValueType::BOOL:
  237. value_bool = other.value_bool;
  238. break;
  239. case ValueType::DOUBLE:
  240. value_double = other.value_double;
  241. break;
  242. case ValueType::STRING:
  243. allocator = other.allocator;
  244. value_string = (char*)allocator->allocate(strlen32(other.value_string) + 1);
  245. strcpy(value_string, other.value_string);
  246. break;
  247. case ValueType::GUID:
  248. value_guid = other.value_guid;
  249. break;
  250. case ValueType::VECTOR3:
  251. value_vector3 = other.value_vector3;
  252. break;
  253. case ValueType::QUATERNION:
  254. value_quaternion = other.value_quaternion;
  255. break;
  256. default:
  257. break;
  258. }
  259. }
  260. Value& operator=(const Value& other)
  261. {
  262. if (type == ValueType::STRING)
  263. allocator->deallocate(value_string);
  264. type = other.type;
  265. switch (type)
  266. {
  267. case ValueType::NIL:
  268. break;
  269. case ValueType::BOOL:
  270. value_bool = other.value_bool;
  271. break;
  272. case ValueType::DOUBLE:
  273. value_double = other.value_double;
  274. break;
  275. case ValueType::STRING:
  276. allocator = other.allocator;
  277. value_string = (char*)allocator->allocate(strlen32(other.value_string) + 1);
  278. strcpy(value_string, other.value_string);
  279. break;
  280. case ValueType::GUID:
  281. value_guid = other.value_guid;
  282. break;
  283. case ValueType::VECTOR3:
  284. value_vector3 = other.value_vector3;
  285. break;
  286. case ValueType::QUATERNION:
  287. value_quaternion = other.value_quaternion;
  288. break;
  289. default:
  290. break;
  291. }
  292. return *this;
  293. }
  294. Allocator* allocator;
  295. ValueType::Enum type;
  296. union
  297. {
  298. bool value_bool;
  299. f64 value_double;
  300. char* value_string;
  301. Guid value_guid;
  302. Vector3 value_vector3;
  303. Quaternion value_quaternion;
  304. };
  305. };
  306. struct Object
  307. {
  308. ALLOCATOR_AWARE;
  309. HashMap<const char*, Value> _data;
  310. Object(Allocator& a)
  311. : _data(a)
  312. {
  313. }
  314. };
  315. template<>
  316. struct hash<Guid>
  317. {
  318. u32 operator()(const Guid val) const
  319. {
  320. return val.data1 ^ val.data2 ^ val.data3 ^ val.data4;
  321. }
  322. };
  323. template<>
  324. struct hash<const char*>
  325. {
  326. u32 operator()(const char* val) const
  327. {
  328. return murmur32(val, strlen32(val), 0);
  329. }
  330. };
  331. struct Database
  332. {
  333. Allocator* _allocator;
  334. HashMap<Guid, Object*> _objects;
  335. const HashMap<StringId32, const char*>& _key_database;
  336. Database(Allocator& a, const HashMap<StringId32, const char*>& key_database)
  337. : _allocator(&a)
  338. , _objects(a)
  339. , _key_database(key_database)
  340. {
  341. // This is a special field which stores all objects
  342. hash_map::set(_objects, GUID_ZERO, CE_NEW(*_allocator, Object)(*_allocator));
  343. }
  344. ~Database()
  345. {
  346. auto cur = hash_map::begin(_objects);
  347. auto end = hash_map::end(_objects);
  348. for (; cur != end; ++cur)
  349. {
  350. HASH_MAP_SKIP_HOLE(_objects, cur);
  351. CE_DELETE(*_allocator, cur->second);
  352. }
  353. }
  354. void create_internal(Guid id)
  355. {
  356. Object* obj = CE_NEW(*_allocator, Object)(*_allocator);
  357. hash_map::set(_objects, id, obj);
  358. }
  359. void destroy_internal(Guid id)
  360. {
  361. Object* obj = hash_map::get(_objects, id, (Object*)NULL);
  362. CE_DELETE(*_allocator, obj);
  363. }
  364. void set_property_bool_internal(Guid guid, const char* key, bool value)
  365. {
  366. Object* obj = hash_map::get(_objects, guid, (Object*)NULL);
  367. Value val;
  368. val.type = ValueType::BOOL;
  369. val.value_bool = value;
  370. hash_map::set(obj->_data, key, val);
  371. }
  372. void set_property_double_internal(Guid guid, const char* key, f64 value)
  373. {
  374. Object* obj = hash_map::get(_objects, guid, (Object*)NULL);
  375. Value val;
  376. val.type = ValueType::DOUBLE;
  377. val.value_double = value;
  378. hash_map::set(obj->_data, key, val);
  379. }
  380. void set_property_string_internal(Guid guid, const char* key, const char* value)
  381. {
  382. Object* obj = hash_map::get(_objects, guid, (Object*)NULL);
  383. Value val;
  384. val.type = ValueType::STRING;
  385. val.allocator = &default_allocator();
  386. val.value_string = (char*)val.allocator->allocate(strlen32(value) + 1);
  387. strcpy(val.value_string, value);
  388. hash_map::set(obj->_data, key, val);
  389. }
  390. void set_property_guid_internal(Guid guid, const char* key, Guid value)
  391. {
  392. Object* obj = hash_map::get(_objects, guid, (Object*)NULL);
  393. Value val;
  394. val.type = ValueType::GUID;
  395. val.value_guid = value;
  396. hash_map::set(obj->_data, key, val);
  397. }
  398. void set_property_vector3_internal(Guid guid, const char* key, Vector3 value)
  399. {
  400. Object* obj = hash_map::get(_objects, guid, (Object*)NULL);
  401. Value val;
  402. val.type = ValueType::VECTOR3;
  403. val.value_vector3 = value;
  404. hash_map::set(obj->_data, key, val);
  405. }
  406. void set_property_quaternion_internal(Guid guid, const char* key, Quaternion value)
  407. {
  408. Object* obj = hash_map::get(_objects, guid, (Object*)NULL);
  409. Value val;
  410. val.type = ValueType::QUATERNION;
  411. val.value_quaternion = value;
  412. hash_map::set(obj->_data, key, val);
  413. }
  414. Vector3 get_property_vector3(Guid guid, const char* key)
  415. {
  416. Object* obj = hash_map::get(_objects, guid, (Object*)NULL);
  417. Value val;
  418. val.type = ValueType::NIL;
  419. val = hash_map::get(obj->_data, key, val);
  420. return val.value_vector3;
  421. }
  422. void load(const char* json)
  423. {
  424. TempAllocator4096 ta;
  425. JsonObject object(ta);
  426. sjson::parse(json, object);
  427. decode(object);
  428. }
  429. void decode(JsonObject& object)
  430. {
  431. // FIXME: reset();
  432. decode_object(GUID_ZERO, "", object);
  433. }
  434. void decode_object(Guid id, const char* key, JsonObject& object)
  435. {
  436. auto cur = json_object::begin(object);
  437. auto end = json_object::end(object);
  438. for (; cur != end; ++cur)
  439. {
  440. JSON_OBJECT_SKIP_HOLE(object, cur);
  441. if (cur->first == "id")
  442. continue;
  443. if (cur->first._data[0] == '#')
  444. continue;
  445. const char* key_null = NULL;
  446. const char* key = hash_map::get(_key_database, StringId32(cur->first.data(), cur->first.length()), key_null);
  447. if (key == key_null)
  448. {
  449. printf("KEY NOT IN DATABASE: %.*s\n", cur->first.length(), cur->first.data());
  450. return;
  451. }
  452. const char* value = cur->second;
  453. // Decode value
  454. switch (sjson::type(cur->second))
  455. {
  456. case JsonValueType::NIL:
  457. break;
  458. case JsonValueType::BOOL:
  459. set_property_bool_internal(id, key, sjson::parse_bool(value));
  460. break;
  461. case JsonValueType::NUMBER:
  462. set_property_double_internal(id, key, sjson::parse_float(value));
  463. break;
  464. case JsonValueType::STRING:
  465. {
  466. TempAllocator256 ta;
  467. DynamicString str(ta);
  468. sjson::parse_string(value, str);
  469. set_property_string_internal(id, key, str.c_str());
  470. }
  471. break;
  472. case JsonValueType::ARRAY:
  473. {
  474. TempAllocator256 ta;
  475. JsonArray arr(ta);
  476. sjson::parse_array(value, arr);
  477. if (array::size(arr) == 3 && sjson::type(arr[0]) == JsonValueType::NUMBER)
  478. {
  479. set_property_vector3_internal(id, key, sjson::parse_vector3(value));
  480. }
  481. else if (array::size(arr) == 4 && sjson::type(arr[0]) == JsonValueType::NUMBER)
  482. set_property_quaternion_internal(id, key, sjson::parse_quaternion(value));
  483. else
  484. decode_set(id, key, arr);
  485. }
  486. break;
  487. case JsonValueType::OBJECT:
  488. {
  489. const char* k = key; // FIXME, define k
  490. TempAllocator256 ta;
  491. JsonObject sub_object(ta);
  492. sjson::parse(cur->second, sub_object);
  493. decode_object(id, k, sub_object);
  494. }
  495. break;
  496. default:
  497. CE_FATAL("Unknown key type");
  498. break;
  499. }
  500. }
  501. }
  502. void decode_set(Guid id, const char* key, const JsonArray& json)
  503. {
  504. // Set should be created even if it is empty.
  505. create_empty_set(id, key);
  506. for (u32 i = 0; i < array::size(json); ++i)
  507. {
  508. TempAllocator128 ta;
  509. JsonObject obj(ta);
  510. sjson::parse_object(json[i], obj);
  511. Guid item_id = sjson::parse_guid(obj["id"]);
  512. create_internal(item_id);
  513. decode_object(item_id, "", obj);
  514. // add_to_set_internal(id, key, item_id);
  515. }
  516. }
  517. void create_empty_set(Guid id, const char* key)
  518. {
  519. }
  520. void dump()
  521. {
  522. TempAllocator4096 ta;
  523. StringStream ss(ta);
  524. auto cur = hash_map::begin(_objects);
  525. auto end = hash_map::end(_objects);
  526. for (; cur != end; ++cur)
  527. {
  528. HASH_MAP_SKIP_HOLE(_objects, cur);
  529. {
  530. ss << "{\n";
  531. ss << " id = ";
  532. {
  533. TempAllocator128 ta;
  534. DynamicString str(ta);
  535. str.from_guid(cur->first);
  536. ss << '"' << str.c_str() << '"';
  537. }
  538. ss << "\n";
  539. encode_object(ss, cur->second);
  540. ss << "}\n";
  541. }
  542. }
  543. printf("%s\n", string_stream::c_str(ss));
  544. }
  545. void encode_object(StringStream& ss, Object* object)
  546. {
  547. auto cur = hash_map::begin(object->_data);
  548. auto end = hash_map::end(object->_data);
  549. for (; cur != end; ++cur)
  550. {
  551. HASH_MAP_SKIP_HOLE(object->_data, cur);
  552. ss << " ";
  553. ss << cur->first;
  554. ss << " = ";
  555. encode_value(ss, cur->second);
  556. ss << "\n";
  557. }
  558. }
  559. void encode_value(StringStream& ss, const Value& value)
  560. {
  561. switch (value.type)
  562. {
  563. case ValueType::NIL:
  564. ss << "null";
  565. break;
  566. case ValueType::BOOL:
  567. ss << (value.value_bool ? "true" : "false");
  568. break;
  569. case ValueType::DOUBLE:
  570. ss << value.value_double;
  571. break;
  572. case ValueType::STRING:
  573. ss << '"' << value.value_string << '"';
  574. break;
  575. case ValueType::GUID:
  576. {
  577. TempAllocator128 ta;
  578. DynamicString str(ta);
  579. str.from_guid(value.value_guid);
  580. ss << '"' << str.c_str() << '"';
  581. }
  582. break;
  583. case ValueType::VECTOR3:
  584. ss << "[ " << value.value_vector3.x << " " << value.value_vector3.y << " " << value.value_vector3.z << " ]";
  585. break;
  586. case ValueType::QUATERNION:
  587. ss << "[ " << value.value_quaternion.x << " " << value.value_quaternion.y << " " << value.value_quaternion.z << " " << value.value_quaternion.w << " ]";
  588. break;
  589. default:
  590. break;
  591. }
  592. }
  593. };
  594. template<>
  595. struct equal_to<const char*>
  596. {
  597. bool operator()(const char* a, const char* b) const
  598. {
  599. return strcmp(a, b) == 0;
  600. };
  601. };
  602. void test_database()
  603. {
  604. memory_globals::init();
  605. {
  606. #define DATABASE_KEY_SOUNDS "sounds"
  607. #define DATABASE_KEY_CLASS "class"
  608. #define DATABASE_KEY_COLLISION_FILTER "collision_filter"
  609. #define DATABASE_KEY_COMPONENTS "components"
  610. #define DATABASE_KEY_DATA "data"
  611. #define DATABASE_KEY_EDITOR "editor"
  612. #define DATABASE_KEY_GEOMETRY_NAME "geometry_name"
  613. #define DATABASE_KEY_ID "id"
  614. #define DATABASE_KEY_MASS "mass"
  615. #define DATABASE_KEY_MATERIAL "material"
  616. #define DATABASE_KEY_MESH_RESOURCE "mesh_resource"
  617. #define DATABASE_KEY_MODIFIED_COMPONENTS "modified_components"
  618. #define DATABASE_KEY_NAME "name"
  619. #define DATABASE_KEY_POSITION "position"
  620. #define DATABASE_KEY_PREFAB "prefab"
  621. #define DATABASE_KEY_ROTATION "rotation"
  622. #define DATABASE_KEY_SCALE "scale"
  623. #define DATABASE_KEY_SCENE "scene"
  624. #define DATABASE_KEY_SHAPE "shape"
  625. #define DATABASE_KEY_TYPE "type"
  626. #define DATABASE_KEY_UNITS "units"
  627. #define DATABASE_KEY_VISIBLE "visible"
  628. HashMap<StringId32, const char*> key_database(default_allocator());
  629. hash_map::set(key_database, StringId32(DATABASE_KEY_SOUNDS), (const char*)DATABASE_KEY_SOUNDS);
  630. hash_map::set(key_database, StringId32(DATABASE_KEY_CLASS), (const char*)DATABASE_KEY_CLASS);
  631. hash_map::set(key_database, StringId32(DATABASE_KEY_COLLISION_FILTER), (const char*)DATABASE_KEY_COLLISION_FILTER);
  632. hash_map::set(key_database, StringId32(DATABASE_KEY_COMPONENTS), (const char*)DATABASE_KEY_COMPONENTS);
  633. hash_map::set(key_database, StringId32(DATABASE_KEY_DATA), (const char*)DATABASE_KEY_DATA);
  634. hash_map::set(key_database, StringId32(DATABASE_KEY_EDITOR), (const char*)DATABASE_KEY_EDITOR);
  635. hash_map::set(key_database, StringId32(DATABASE_KEY_GEOMETRY_NAME), (const char*)DATABASE_KEY_GEOMETRY_NAME);
  636. hash_map::set(key_database, StringId32(DATABASE_KEY_ID), (const char*)DATABASE_KEY_ID);
  637. hash_map::set(key_database, StringId32(DATABASE_KEY_MASS), (const char*)DATABASE_KEY_MASS);
  638. hash_map::set(key_database, StringId32(DATABASE_KEY_MATERIAL), (const char*)DATABASE_KEY_MATERIAL);
  639. hash_map::set(key_database, StringId32(DATABASE_KEY_MESH_RESOURCE), (const char*)DATABASE_KEY_MESH_RESOURCE);
  640. hash_map::set(key_database, StringId32(DATABASE_KEY_MODIFIED_COMPONENTS), (const char*)DATABASE_KEY_MODIFIED_COMPONENTS);
  641. hash_map::set(key_database, StringId32(DATABASE_KEY_NAME), (const char*)DATABASE_KEY_NAME);
  642. hash_map::set(key_database, StringId32(DATABASE_KEY_POSITION), (const char*)DATABASE_KEY_POSITION);
  643. hash_map::set(key_database, StringId32(DATABASE_KEY_PREFAB), (const char*)DATABASE_KEY_PREFAB);
  644. hash_map::set(key_database, StringId32(DATABASE_KEY_ROTATION), (const char*)DATABASE_KEY_ROTATION);
  645. hash_map::set(key_database, StringId32(DATABASE_KEY_SCALE), (const char*)DATABASE_KEY_SCALE);
  646. hash_map::set(key_database, StringId32(DATABASE_KEY_SCENE), (const char*)DATABASE_KEY_SCENE);
  647. hash_map::set(key_database, StringId32(DATABASE_KEY_SHAPE), (const char*)DATABASE_KEY_SHAPE);
  648. hash_map::set(key_database, StringId32(DATABASE_KEY_TYPE), (const char*)DATABASE_KEY_TYPE);
  649. hash_map::set(key_database, StringId32(DATABASE_KEY_UNITS), (const char*)DATABASE_KEY_UNITS);
  650. hash_map::set(key_database, StringId32(DATABASE_KEY_VISIBLE), (const char*)DATABASE_KEY_VISIBLE);
  651. Ledger ledger(default_allocator());
  652. #if 1
  653. char* level_json = NULL;
  654. FilesystemDisk disk(default_allocator());
  655. File* file = disk.open("/home/dani/git/crown/samples/01-physics/test.level", FileOpenMode::READ);
  656. if (file)
  657. {
  658. u32 size = file->size();
  659. level_json = (char*)default_allocator().allocate(size + 1);
  660. file->read(level_json, size);
  661. level_json[size] = '\0';
  662. disk.close(*file);
  663. }
  664. Database db(default_allocator(), key_database);
  665. db.load(level_json);
  666. // db.set_property_vector3_internal(guid::parse("f56420ad-7f9c-4cca-aca5-350f366e0dc0"), "position", vector3(1, 2, 3));
  667. // db.set_property_vector3_internal(guid::parse("f56420ad-7f9c-4cca-aca5-350f366e0dc0"), "position", vector3(4, 5, 6));
  668. // db.set_property_vector3_internal(guid::parse("f56420ad-7f9c-4cca-aca5-350f366e0dc0"), "position", vector3(7, 8, 9));
  669. db.dump();
  670. default_allocator().deallocate(level_json);
  671. #else
  672. Database db(default_allocator(), key_database);
  673. Guid id = guid::new_guid();
  674. db.create_internal(id);
  675. db.set_property_vector3_internal(id, DATABASE_KEY_POSITION, vector3(1, 2, 3));
  676. db.set_property_vector3_internal(id, DATABASE_KEY_POSITION, vector3(4, 5, 6));
  677. db.set_property_quaternion_internal(id, DATABASE_KEY_ROTATION, quaternion(1, 2, 3, 0));
  678. db.set_property_string_internal(id, DATABASE_KEY_MASS, "apple");
  679. db.set_property_string_internal(id, DATABASE_KEY_MASS, "banana");
  680. db.dump();
  681. db.destroy_internal(id);
  682. #endif
  683. }
  684. memory_globals::shutdown();
  685. }
  686. } // namespace crown