database.vala 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314
  1. /*
  2. * Copyright (c) 2012-2020 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/dbartolini/crown/blob/master/LICENSE
  4. */
  5. using Gee;
  6. namespace Crown
  7. {
  8. public class Database
  9. {
  10. private enum Action
  11. {
  12. CREATE,
  13. DESTROY,
  14. SET_PROPERTY_NULL,
  15. SET_PROPERTY_BOOL,
  16. SET_PROPERTY_DOUBLE,
  17. SET_PROPERTY_STRING,
  18. SET_PROPERTY_GUID,
  19. SET_PROPERTY_VECTOR3,
  20. SET_PROPERTY_QUATERNION,
  21. ADD_TO_SET,
  22. REMOVE_FROM_SET,
  23. RESTORE_POINT
  24. }
  25. private struct RestorePoint
  26. {
  27. public int id;
  28. public uint32 size;
  29. public Guid[] data;
  30. }
  31. private class Stack
  32. {
  33. private uint8[] _data;
  34. private uint32 _read;
  35. public Stack()
  36. {
  37. _data = new uint8[1024*1024];
  38. _read = 0;
  39. }
  40. public void clear()
  41. {
  42. _read = 0;
  43. }
  44. public uint32 size()
  45. {
  46. return _read;
  47. }
  48. public void write_data(void* data, ulong len)
  49. {
  50. uint8* buf = (uint8*)data;
  51. for (ulong i = 0; i < len; ++i, ++_read)
  52. _data[_read] = buf[i];
  53. }
  54. public void write_bool(bool a)
  55. {
  56. write_data(&a, sizeof(bool));
  57. }
  58. public void write_int(int a)
  59. {
  60. write_data(&a, sizeof(int));
  61. }
  62. public void write_uint32(uint32 a)
  63. {
  64. write_data(&a, sizeof(uint32));
  65. }
  66. public void write_double(double a)
  67. {
  68. write_data(&a, sizeof(double));
  69. }
  70. public void write_string(string str)
  71. {
  72. uint32 len = str.length;
  73. write_data(&str.data[0], len);
  74. write_data(&len, sizeof(uint32));
  75. }
  76. public void write_guid(Guid a)
  77. {
  78. write_data(&a, sizeof(Guid));
  79. }
  80. public void write_vector3(Vector3 a)
  81. {
  82. write_data(&a, sizeof(Vector3));
  83. }
  84. public void write_quaternion(Quaternion a)
  85. {
  86. write_data(&a, sizeof(Quaternion));
  87. }
  88. public void write_action(Action t)
  89. {
  90. write_uint32((uint32)t);
  91. }
  92. public Action read_action()
  93. {
  94. _read -= (uint32)sizeof(uint32);
  95. uint32 a = *(uint32*)(&_data[_read]);
  96. return (Action)a;
  97. }
  98. public bool read_bool()
  99. {
  100. _read -= (uint32)sizeof(bool);
  101. bool a = *(bool*)(&_data[_read]);
  102. return a;
  103. }
  104. public int read_int()
  105. {
  106. _read -= (uint32)sizeof(int);
  107. int a = *(int*)(&_data[_read]);
  108. return a;
  109. }
  110. public uint32 read_uint32()
  111. {
  112. _read -= (uint32)sizeof(uint32);
  113. uint32 a = *(uint32*)(&_data[_read]);
  114. return a;
  115. }
  116. public double read_double()
  117. {
  118. _read -= (uint32)sizeof(double);
  119. double a = *(double*)(&_data[_read]);
  120. return a;
  121. }
  122. public Guid read_guid()
  123. {
  124. _read -= (uint32)sizeof(Guid);
  125. Guid a = *(Guid*)(&_data[_read]);
  126. return a;
  127. }
  128. public Vector3 read_vector3()
  129. {
  130. _read -= (uint32)sizeof(Vector3);
  131. Vector3 a = *(Vector3*)(&_data[_read]);
  132. return a;
  133. }
  134. public Quaternion read_quaternion()
  135. {
  136. _read -= (uint32)sizeof(Quaternion);
  137. Quaternion a = *(Quaternion*)(&_data[_read]);
  138. return a;
  139. }
  140. public string read_string()
  141. {
  142. _read -= (uint32)sizeof(uint32);
  143. uint32 len = *(uint32*)(&_data[_read]);
  144. _read -= len;
  145. uint8[] str = new uint8[len + 1];
  146. for (uint32 i = 0; i < len; ++i)
  147. str[i] = *(uint8*)(&_data[_read + i]);
  148. str[len] = '\0';
  149. return (string)str;
  150. }
  151. public void write_create_action(Guid id)
  152. {
  153. write_guid(id);
  154. write_action(Action.CREATE);
  155. }
  156. public void write_destroy_action(Guid id)
  157. {
  158. write_guid(id);
  159. write_action(Action.DESTROY);
  160. }
  161. public void write_set_property_null_action(Guid id, string key)
  162. {
  163. // No value to push
  164. write_string(key);
  165. write_guid(id);
  166. write_action(Action.SET_PROPERTY_NULL);
  167. }
  168. public void write_set_property_bool_action(Guid id, string key, bool val)
  169. {
  170. write_bool(val);
  171. write_string(key);
  172. write_guid(id);
  173. write_action(Action.SET_PROPERTY_BOOL);
  174. }
  175. public void write_set_property_double_action(Guid id, string key, double val)
  176. {
  177. write_double(val);
  178. write_string(key);
  179. write_guid(id);
  180. write_action(Action.SET_PROPERTY_DOUBLE);
  181. }
  182. public void write_set_property_string_action(Guid id, string key, string val)
  183. {
  184. write_string(val);
  185. write_string(key);
  186. write_guid(id);
  187. write_action(Action.SET_PROPERTY_STRING);
  188. }
  189. public void write_set_property_guid_action(Guid id, string key, Guid val)
  190. {
  191. write_guid(val);
  192. write_string(key);
  193. write_guid(id);
  194. write_action(Action.SET_PROPERTY_GUID);
  195. }
  196. public void write_set_property_vector3_action(Guid id, string key, Vector3 val)
  197. {
  198. write_vector3(val);
  199. write_string(key);
  200. write_guid(id);
  201. write_action(Action.SET_PROPERTY_VECTOR3);
  202. }
  203. public void write_set_property_quaternion_action(Guid id, string key, Quaternion val)
  204. {
  205. write_quaternion(val);
  206. write_string(key);
  207. write_guid(id);
  208. write_action(Action.SET_PROPERTY_QUATERNION);
  209. }
  210. public void write_add_to_set_action(Guid id, string key, Guid item_id)
  211. {
  212. write_guid(item_id);
  213. write_string(key);
  214. write_guid(id);
  215. write_action(Action.ADD_TO_SET);
  216. }
  217. public void write_remove_from_set_action(Guid id, string key, Guid item_id)
  218. {
  219. write_guid(item_id);
  220. write_string(key);
  221. write_guid(id);
  222. write_action(Action.REMOVE_FROM_SET);
  223. }
  224. public void write_restore_point(int id, uint32 size, Guid[] data)
  225. {
  226. uint32 num_guids = data.length;
  227. for (uint32 i = 0; i < num_guids; ++i)
  228. write_guid(data[i]);
  229. write_uint32(num_guids);
  230. write_uint32(size);
  231. write_int(id);
  232. write_action(Action.RESTORE_POINT);
  233. }
  234. public uint32 peek_type()
  235. {
  236. return *(uint32*)(&_data[_read - (uint32)sizeof(uint32)]);
  237. }
  238. public RestorePoint read_restore_point()
  239. {
  240. Action t = read_action();
  241. assert(t == Action.RESTORE_POINT);
  242. int id = read_int();
  243. uint32 size = read_uint32();
  244. uint32 num_guids = read_uint32();
  245. Guid[] ids = new Guid[num_guids];
  246. for (uint32 i = 0; i < num_guids; ++i)
  247. ids[i] = read_guid();
  248. return { id, size, ids };
  249. }
  250. }
  251. // Data
  252. private HashMap<string, Value?> _data;
  253. private Stack _undo;
  254. private Stack _redo;
  255. private Stack _undo_points;
  256. private Stack _redo_points;
  257. private bool _changed;
  258. // Signals
  259. public signal void key_changed(Guid id, string key);
  260. public signal void undo_redo(bool undo, int id, Guid[] data);
  261. public Database()
  262. {
  263. _data = new HashMap<string, Value?>();
  264. _undo = new Stack();
  265. _redo = new Stack();
  266. _undo_points = new Stack();
  267. _redo_points = new Stack();
  268. reset();
  269. }
  270. /// Resets database to clean state.
  271. public void reset()
  272. {
  273. _data.clear();
  274. _undo.clear();
  275. _redo.clear();
  276. _undo_points.clear();
  277. _redo_points.clear();
  278. _changed = false;
  279. // This is a special field which stores all objects
  280. _data.set("_objects", new HashMap<string, Value?>());
  281. }
  282. /// Returns whether the database has been changed since last call to Save().
  283. public bool changed()
  284. {
  285. return _changed;
  286. }
  287. /// Saves database to path without marking it as not changed.
  288. public void dump(string path)
  289. {
  290. Hashtable json = encode();
  291. SJSON.save(json, path);
  292. }
  293. /// Saves database to path.
  294. public void save(string path)
  295. {
  296. dump(path);
  297. _changed = false;
  298. }
  299. /// Loads database from path.
  300. public void load(string path)
  301. {
  302. Hashtable json = SJSON.load(path);
  303. decode(json);
  304. _changed = false;
  305. }
  306. private Hashtable encode()
  307. {
  308. return encode_object(GUID_ZERO, _data);
  309. }
  310. private static bool is_valid_value(Value? value)
  311. {
  312. return value == null
  313. || value.holds(typeof(bool))
  314. || value.holds(typeof(double))
  315. || value.holds(typeof(string))
  316. || value.holds(typeof(Guid))
  317. || value.holds(typeof(Vector3))
  318. || value.holds(typeof(Quaternion))
  319. ;
  320. }
  321. private static bool is_valid_key(string key)
  322. {
  323. return key.length > 0
  324. && key != "_objects"
  325. && !key.has_prefix(".")
  326. && !key.has_suffix(".")
  327. ;
  328. }
  329. #if 0
  330. private static string value_to_string(Value? value)
  331. {
  332. if (value == null)
  333. return "null";
  334. if (value.holds(typeof(bool)))
  335. return ((bool)value).to_string();
  336. if (value.holds(typeof(double)))
  337. return ((double)value).to_string();
  338. if (value.holds(typeof(string)))
  339. return ((string)value).to_string();
  340. if (value.holds(typeof(Guid)))
  341. return ((Guid)value).to_string();
  342. if (value.holds(typeof(Vector3)))
  343. return ((Vector3)value).to_string();
  344. if (value.holds(typeof(Quaternion)))
  345. return ((Quaternion)value).to_string();
  346. if (value.holds(typeof(HashSet)))
  347. return "Set<Guid>";
  348. return "<invalid>";
  349. }
  350. #endif // CROWN_DEBUG
  351. public void decode(Hashtable json)
  352. {
  353. reset();
  354. decode_object(GUID_ZERO, "", json);
  355. }
  356. private void decode_object(Guid id, string db_key, Hashtable json)
  357. {
  358. string old_db = db_key;
  359. string k = db_key;
  360. string[] keys = json.keys.to_array();
  361. foreach (string key in keys)
  362. {
  363. assert(key != "_objects");
  364. if (key == "id")
  365. continue;
  366. Value? val = json[key];
  367. k += k == "" ? key : ("." + key);
  368. if (val.holds(typeof(Hashtable)))
  369. {
  370. Hashtable ht = (Hashtable)val;
  371. decode_object(id, k, ht);
  372. }
  373. else if (val.holds(typeof(ArrayList)))
  374. {
  375. ArrayList<Value?> arr = (ArrayList<Value?>)val;
  376. if (arr.size > 0 && arr[0].holds(typeof(double)))
  377. set_property_internal(id, k, decode_value(val));
  378. else
  379. decode_set(id, key, arr);
  380. }
  381. else
  382. {
  383. set_property_internal(id, k, decode_value(val));
  384. }
  385. k = old_db;
  386. }
  387. }
  388. private void decode_set(Guid id, string key, ArrayList<Value?> json)
  389. {
  390. // Set should be created even if it is empty.
  391. create_empty_set(id, key);
  392. for (int i = 0; i < json.size; ++i)
  393. {
  394. Hashtable obj = (Hashtable)json[i];
  395. Guid item_id = Guid.parse((string)obj["id"]);
  396. create_internal(item_id);
  397. decode_object(item_id, "", obj);
  398. add_to_set_internal(id, key, item_id);
  399. }
  400. }
  401. private Value? decode_value(Value? value)
  402. {
  403. if (value.holds(typeof(ArrayList)))
  404. {
  405. ArrayList<Value?> al = (ArrayList<Value?>)value;
  406. if (al.size == 3)
  407. return Vector3((double)al[0], (double)al[1], (double)al[2]);
  408. else if (al.size == 4)
  409. return Quaternion((double)al[0], (double)al[1], (double)al[2], (double)al[3]);
  410. else
  411. assert(false);
  412. }
  413. else if (value.holds(typeof(string)))
  414. {
  415. Guid id;
  416. if (Guid.try_parse((string)value, out id))
  417. return id;
  418. return value;
  419. }
  420. else if (value == null || value.holds(typeof(bool)) || value.holds(typeof(double)))
  421. {
  422. return value;
  423. }
  424. else
  425. {
  426. assert(false);
  427. }
  428. return null;
  429. }
  430. private Hashtable encode_object(Guid id, HashMap<string, Value?> db)
  431. {
  432. Hashtable obj = new Hashtable();
  433. if (id != GUID_ZERO)
  434. obj["id"] = id.to_string();
  435. string[] keys = db.keys.to_array();
  436. foreach (string key in keys)
  437. {
  438. if (key == "_objects")
  439. continue;
  440. // Since null-key is equivalent to non-existent key, skip serialization.
  441. if (db[key] == null)
  442. continue;
  443. string[] foo = key.split(".");
  444. Hashtable x = obj;
  445. if (foo.length > 1)
  446. {
  447. for (int i = 0; i < foo.length - 1; ++i)
  448. {
  449. string f = foo[i];
  450. if (x.has_key(f))
  451. {
  452. x = (Hashtable)x[f];
  453. continue;
  454. }
  455. Hashtable y = new Hashtable();
  456. x.set(f, y);
  457. x = y;
  458. }
  459. }
  460. x.set(foo[foo.length-1], encode_value(db[key]));
  461. }
  462. return obj;
  463. }
  464. private Value? encode_value(Value? value)
  465. {
  466. assert(is_valid_value(value) || value.holds(typeof(HashSet)));
  467. if (value.holds(typeof(Vector3)))
  468. {
  469. Vector3 v = (Vector3)value;
  470. ArrayList<Value?> arr = new Gee.ArrayList<Value?>();
  471. arr.add(v.x);
  472. arr.add(v.y);
  473. arr.add(v.z);
  474. return arr;
  475. }
  476. else if (value.holds(typeof(Quaternion)))
  477. {
  478. Quaternion q = (Quaternion)value;
  479. ArrayList<Value?> arr = new Gee.ArrayList<Value?>();
  480. arr.add(q.x);
  481. arr.add(q.y);
  482. arr.add(q.z);
  483. arr.add(q.w);
  484. return arr;
  485. }
  486. else if (value.holds(typeof(Guid)))
  487. {
  488. Guid id = (Guid)value;
  489. return id.to_string();
  490. }
  491. else if (value.holds(typeof(HashSet)))
  492. {
  493. HashSet<Guid?> hs = (HashSet<Guid?>)value;
  494. ArrayList<Value?> arr = new Gee.ArrayList<Value?>();
  495. foreach (Guid id in hs)
  496. {
  497. HashMap<string, Value?> objs = (HashMap<string, Value?>)_data["_objects"];
  498. arr.add(encode_object(id, (HashMap<string, Value?>)objs[id.to_string()]));
  499. }
  500. return arr;
  501. }
  502. else
  503. {
  504. return value;
  505. }
  506. }
  507. private HashMap<string, Value?> get_data(Guid id)
  508. {
  509. assert(has_object(id));
  510. return (HashMap<string, Value?>)(id == GUID_ZERO ? _data : ((HashMap<string, Value?>)_data["_objects"])[id.to_string()]);
  511. }
  512. private void create_internal(Guid id)
  513. {
  514. assert(id != GUID_ZERO);
  515. #if 0
  516. stdout.printf("create %s\n", id.to_string());
  517. #endif // CROWN_DEBUG
  518. ((HashMap<string, Value?>)_data["_objects"]).set(id.to_string(), new HashMap<string, Value?>());
  519. _changed = true;
  520. key_changed(id, "_objects");
  521. }
  522. private void destroy_internal(Guid id)
  523. {
  524. assert(id != GUID_ZERO);
  525. assert(has_object(id));
  526. #if 0
  527. stdout.printf("destroy %s\n", id.to_string());
  528. #endif // CROWN_DEBUG
  529. ((HashMap<string, Value?>)_data["_objects"]).unset(id.to_string());
  530. _changed = true;
  531. key_changed(id, "_objects");
  532. }
  533. private void set_property_internal(Guid id, string key, Value? value)
  534. {
  535. assert(has_object(id));
  536. assert(is_valid_key(key));
  537. assert(is_valid_value(value));
  538. #if 0
  539. stdout.printf("set_property %s %s %s\n"
  540. , id.to_string()
  541. , key
  542. , (value == null) ? "null" : value_to_string(value)
  543. );
  544. #endif // CROWN_DEBUG
  545. HashMap<string, Value?> ob = get_data(id);
  546. ob[key] = value;
  547. _changed = true;
  548. key_changed(id, key);
  549. }
  550. private void create_empty_set(Guid id, string key)
  551. {
  552. assert(has_object(id));
  553. assert(is_valid_key(key));
  554. HashMap<string, Value?> ob = get_data(id);
  555. assert(!ob.has_key(key));
  556. ob[key] = new HashSet<Guid?>(Guid.hash_func, Guid.equal_func);
  557. }
  558. private void add_to_set_internal(Guid id, string key, Guid item_id)
  559. {
  560. assert(has_object(id));
  561. assert(is_valid_key(key));
  562. assert(item_id != GUID_ZERO);
  563. assert(has_object(item_id));
  564. #if 0
  565. stdout.printf("add_to_set %s %s %s\n"
  566. , id.to_string()
  567. , key
  568. , item_id.to_string()
  569. );
  570. #endif // CROWN_DEBUG
  571. HashMap<string, Value?> ob = get_data(id);
  572. if (!ob.has_key(key))
  573. {
  574. HashSet<Guid?> hs = new HashSet<Guid?>(Guid.hash_func, Guid.equal_func);
  575. hs.add(item_id);
  576. ob[key] = hs;
  577. }
  578. else
  579. {
  580. ((HashSet<Guid?>)ob[key]).add(item_id);
  581. }
  582. _changed = true;
  583. key_changed(id, key);
  584. }
  585. private void remove_from_set_internal(Guid id, string key, Guid item_id)
  586. {
  587. assert(has_object(id));
  588. assert(is_valid_key(key));
  589. assert(item_id != GUID_ZERO);
  590. #if 0
  591. stdout.printf("remove_from_set %s %s %s\n"
  592. , id.to_string()
  593. , key
  594. , item_id.to_string()
  595. );
  596. #endif // CROWN_DEBUG
  597. HashMap<string, Value?> ob = get_data(id);
  598. ((HashSet<Guid?>)ob[key]).remove(item_id);
  599. _changed = true;
  600. key_changed(id, key);
  601. }
  602. public void create(Guid id)
  603. {
  604. assert(id != GUID_ZERO);
  605. assert(!has_object(id));
  606. _undo.write_destroy_action(id);
  607. _redo.clear();
  608. _redo_points.clear();
  609. create_internal(id);
  610. }
  611. public void destroy(Guid id)
  612. {
  613. assert(id != GUID_ZERO);
  614. assert(has_object(id));
  615. HashMap<string, Value?> o = get_data(id);
  616. string[] keys = o.keys.to_array();
  617. foreach (string key in keys)
  618. {
  619. Value? value = o[key];
  620. if (value.holds(typeof(HashSet)))
  621. {
  622. HashSet<Guid?> hs = (HashSet<Guid?>)value;
  623. Guid?[] ids = hs.to_array();
  624. foreach (Guid item_id in ids)
  625. {
  626. remove_from_set(id, key, item_id);
  627. destroy(item_id);
  628. }
  629. }
  630. else
  631. {
  632. set_property_null(id, key);
  633. }
  634. }
  635. _undo.write_create_action(id);
  636. _redo.clear();
  637. _redo_points.clear();
  638. destroy_internal(id);
  639. }
  640. public void set_property_null(Guid id, string key)
  641. {
  642. assert(has_object(id));
  643. assert(is_valid_key(key));
  644. assert(is_valid_value(null));
  645. HashMap<string, Value?> ob = get_data(id);
  646. if (ob.has_key(key) && ob[key] != null)
  647. {
  648. if (ob[key].holds(typeof(bool)))
  649. _undo.write_set_property_bool_action(id, key, (bool)ob[key]);
  650. if (ob[key].holds(typeof(double)))
  651. _undo.write_set_property_double_action(id, key, (double)ob[key]);
  652. if (ob[key].holds(typeof(string)))
  653. _undo.write_set_property_string_action(id, key, (string)ob[key]);
  654. if (ob[key].holds(typeof(Guid)))
  655. _undo.write_set_property_guid_action(id, key, (Guid)ob[key]);
  656. if (ob[key].holds(typeof(Vector3)))
  657. _undo.write_set_property_vector3_action(id, key, (Vector3)ob[key]);
  658. if (ob[key].holds(typeof(Quaternion)))
  659. _undo.write_set_property_quaternion_action(id, key, (Quaternion)ob[key]);
  660. }
  661. else
  662. {
  663. _undo.write_set_property_null_action(id, key);
  664. }
  665. _redo.clear();
  666. _redo_points.clear();
  667. set_property_internal(id, key, null);
  668. }
  669. public void set_property_bool(Guid id, string key, bool val)
  670. {
  671. assert(has_object(id));
  672. assert(is_valid_key(key));
  673. assert(is_valid_value(val));
  674. HashMap<string, Value?> ob = get_data(id);
  675. if (ob.has_key(key) && ob[key] != null)
  676. _undo.write_set_property_bool_action(id, key, (bool)ob[key]);
  677. else
  678. _undo.write_set_property_null_action(id, key);
  679. _redo.clear();
  680. _redo_points.clear();
  681. set_property_internal(id, key, val);
  682. }
  683. public void set_property_double(Guid id, string key, double val)
  684. {
  685. assert(has_object(id));
  686. assert(is_valid_key(key));
  687. assert(is_valid_value(val));
  688. HashMap<string, Value?> ob = get_data(id);
  689. if (ob.has_key(key) && ob[key] != null)
  690. _undo.write_set_property_double_action(id, key, (double)ob[key]);
  691. else
  692. _undo.write_set_property_null_action(id, key);
  693. _redo.clear();
  694. _redo_points.clear();
  695. set_property_internal(id, key, val);
  696. }
  697. public void set_property_string(Guid id, string key, string val)
  698. {
  699. assert(has_object(id));
  700. assert(is_valid_key(key));
  701. assert(is_valid_value(val));
  702. HashMap<string, Value?> ob = get_data(id);
  703. if (ob.has_key(key) && ob[key] != null)
  704. _undo.write_set_property_string_action(id, key, (string)ob[key]);
  705. else
  706. _undo.write_set_property_null_action(id, key);
  707. _redo.clear();
  708. _redo_points.clear();
  709. set_property_internal(id, key, val);
  710. }
  711. public void set_property_guid(Guid id, string key, Guid val)
  712. {
  713. assert(has_object(id));
  714. assert(is_valid_key(key));
  715. assert(is_valid_value(val));
  716. HashMap<string, Value?> ob = get_data(id);
  717. if (ob.has_key(key) && ob[key] != null)
  718. _undo.write_set_property_guid_action(id, key, (Guid)ob[key]);
  719. else
  720. _undo.write_set_property_null_action(id, key);
  721. _redo.clear();
  722. _redo_points.clear();
  723. set_property_internal(id, key, val);
  724. }
  725. public void set_property_vector3(Guid id, string key, Vector3 val)
  726. {
  727. assert(has_object(id));
  728. assert(is_valid_key(key));
  729. assert(is_valid_value(val));
  730. HashMap<string, Value?> ob = get_data(id);
  731. if (ob.has_key(key) && ob[key] != null)
  732. _undo.write_set_property_vector3_action(id, key, (Vector3)ob[key]);
  733. else
  734. _undo.write_set_property_null_action(id, key);
  735. _redo.clear();
  736. _redo_points.clear();
  737. set_property_internal(id, key, val);
  738. }
  739. public void set_property_quaternion(Guid id, string key, Quaternion val)
  740. {
  741. assert(has_object(id));
  742. assert(is_valid_key(key));
  743. assert(is_valid_value(val));
  744. HashMap<string, Value?> ob = get_data(id);
  745. if (ob.has_key(key) && ob[key] != null)
  746. _undo.write_set_property_quaternion_action(id, key, (Quaternion)ob[key]);
  747. else
  748. _undo.write_set_property_null_action(id, key);
  749. _redo.clear();
  750. _redo_points.clear();
  751. set_property_internal(id, key, val);
  752. }
  753. public void add_to_set(Guid id, string key, Guid item_id)
  754. {
  755. assert(has_object(id));
  756. assert(is_valid_key(key));
  757. assert(item_id != GUID_ZERO);
  758. assert(has_object(item_id));
  759. _undo.write_remove_from_set_action(id, key, item_id);
  760. _redo.clear();
  761. _redo_points.clear();
  762. add_to_set_internal(id, key, item_id);
  763. }
  764. public void remove_from_set(Guid id, string key, Guid item_id)
  765. {
  766. assert(has_object(id));
  767. assert(is_valid_key(key));
  768. assert(item_id != GUID_ZERO);
  769. _undo.write_add_to_set_action(id, key, item_id);
  770. _redo.clear();
  771. _redo_points.clear();
  772. remove_from_set_internal(id, key, item_id);
  773. }
  774. public bool has_object(Guid id)
  775. {
  776. return id == GUID_ZERO || ((HashMap<string, Value?>)_data["_objects"]).has_key(id.to_string());
  777. }
  778. public bool has_property(Guid id, string key)
  779. {
  780. return get_property(id, key) != null;
  781. }
  782. public Value? get_property(Guid id, string key)
  783. {
  784. assert(has_object(id));
  785. assert(is_valid_key(key));
  786. HashMap<string, Value?> ob = get_data(id);
  787. Value? value = (ob.has_key(key) ? ob[key] : null);
  788. #if 0
  789. stdout.printf("get_property %s %s %s\n"
  790. , id.to_string()
  791. , key
  792. , (value == null) ? "null" : value_to_string(value)
  793. );
  794. #endif // CROWN_DEBUG
  795. return value;
  796. }
  797. public bool get_property_bool(Guid id, string key)
  798. {
  799. return (bool)get_property(id, key);
  800. }
  801. public double get_property_double(Guid id, string key)
  802. {
  803. return (double)get_property(id, key);
  804. }
  805. public string get_property_string(Guid id, string key)
  806. {
  807. return (string)get_property(id, key);
  808. }
  809. public Guid get_property_guid(Guid id, string key)
  810. {
  811. return (Guid)get_property(id, key);
  812. }
  813. public Vector3 get_property_vector3(Guid id, string key)
  814. {
  815. return (Vector3)get_property(id, key);
  816. }
  817. public Quaternion get_property_quaternion(Guid id, string key)
  818. {
  819. return (Quaternion)get_property(id, key);
  820. }
  821. public HashSet<Guid?> get_property_set(Guid id, string key, HashSet<Guid?> deffault)
  822. {
  823. assert(has_object(id));
  824. assert(is_valid_key(key));
  825. HashMap<string, Value?> ob = get_data(id);
  826. if (ob.has_key(key))
  827. return ob[key] as HashSet<Guid?>;
  828. else
  829. return deffault;
  830. #if 0
  831. // stdout.printf("get_property %s %s %s\n"
  832. // , id.to_string()
  833. // , key
  834. // , (value == null) ? "null" : value_to_string(value)
  835. // );
  836. #endif // CROWN_DEBUG
  837. }
  838. public HashMap<string, Value?> get_object(Guid id)
  839. {
  840. return (HashMap<string, Value?>)get_data(GUID_ZERO)[id.to_string()];
  841. }
  842. public string[] get_keys(Guid id)
  843. {
  844. HashMap<string, Value?> data = get_data(id);
  845. return data.keys.to_array();
  846. }
  847. public void add_restore_point(int id, Guid[] data)
  848. {
  849. #if 0
  850. stdout.printf("add_restore_point %d, undo size = %u\n", id, _undo.size());
  851. #endif // CROWN_DEBUG
  852. _undo_points.write_restore_point(id, _undo.size(), data);
  853. _redo.clear();
  854. _redo_points.clear();
  855. }
  856. /// Duplicates the object specified by id and assign new_id to the duplicated object.
  857. public void duplicate(Guid id, Guid new_id)
  858. {
  859. assert(id != GUID_ZERO);
  860. assert(new_id != GUID_ZERO);
  861. assert(id != new_id);
  862. assert(has_object(id));
  863. create(new_id);
  864. HashMap<string, Value?> ob = get_data(id);
  865. string[] keys = ob.keys.to_array();
  866. foreach (string key in keys)
  867. {
  868. Value? val = ob[key];
  869. if (val.holds(typeof(HashSet)))
  870. {
  871. HashSet<Guid?> hs = (HashSet<Guid?>)val;
  872. foreach (Guid j in hs)
  873. {
  874. Guid x = Guid.new_guid();
  875. duplicate(j, x);
  876. add_to_set(new_id, key, x);
  877. }
  878. }
  879. else
  880. {
  881. if (ob[key] == null)
  882. set_property_null(new_id, key);
  883. if (ob[key].holds(typeof(bool)))
  884. set_property_bool(new_id, key, (bool)ob[key]);
  885. if (ob[key].holds(typeof(double)))
  886. set_property_double(new_id, key, (double)ob[key]);
  887. if (ob[key].holds(typeof(string)))
  888. set_property_string(new_id, key, (string)ob[key]);
  889. if (ob[key].holds(typeof(Guid)))
  890. set_property_guid(new_id, key, (Guid)ob[key]);
  891. if (ob[key].holds(typeof(Vector3)))
  892. set_property_vector3(new_id, key, (Vector3)ob[key]);
  893. if (ob[key].holds(typeof(Quaternion)))
  894. set_property_quaternion(new_id, key, (Quaternion)ob[key]);
  895. }
  896. }
  897. }
  898. /// Copies the database to db under the given new_key.
  899. public void copy_to(Database db, string new_key)
  900. {
  901. assert(db != null);
  902. assert(is_valid_key(new_key));
  903. copy_deep(db, GUID_ZERO, new_key);
  904. }
  905. public void copy_deep(Database db, Guid id, string new_key)
  906. {
  907. HashMap<string, Value?> ob = get_data(id);
  908. string[] keys = ob.keys.to_array();
  909. foreach (string key in keys)
  910. {
  911. if (key == "_objects")
  912. continue;
  913. Value? value = ob[key];
  914. if (value.holds(typeof(HashSet)))
  915. {
  916. HashSet<Guid?> hs = (HashSet<Guid?>)value;
  917. foreach (Guid j in hs)
  918. {
  919. db.create(j);
  920. copy_deep(db, j, "");
  921. db.add_to_set(id, new_key + (new_key == "" ? "" : ".") + key, j);
  922. }
  923. }
  924. else
  925. {
  926. string kk = new_key + (new_key == "" ? "" : ".") + key;
  927. if (ob[key] == null)
  928. db.set_property_null(id, kk);
  929. if (ob[key].holds(typeof(bool)))
  930. db.set_property_bool(id, kk, (bool)ob[key]);
  931. if (ob[key].holds(typeof(double)))
  932. db.set_property_double(id, kk, (double)ob[key]);
  933. if (ob[key].holds(typeof(string)))
  934. db.set_property_string(id, kk, (string)ob[key]);
  935. if (ob[key].holds(typeof(Guid)))
  936. db.set_property_guid(id, kk, (Guid)ob[key]);
  937. if (ob[key].holds(typeof(Vector3)))
  938. db.set_property_vector3(id, kk, (Vector3)ob[key]);
  939. if (ob[key].holds(typeof(Quaternion)))
  940. db.set_property_quaternion(id, kk, (Quaternion)ob[key]);
  941. }
  942. }
  943. }
  944. // Un-does the last action and returns its ID, or -1 if there is no
  945. // action to undo.
  946. public int undo()
  947. {
  948. if (_undo_points.size() == 0)
  949. return -1;
  950. RestorePoint rp = _undo_points.read_restore_point();
  951. _redo_points.write_restore_point(rp.id, _redo.size(), rp.data);
  952. undo_until(rp.size);
  953. undo_redo(true, rp.id, rp.data);
  954. return rp.id;
  955. }
  956. // Re-does the last action and returns its ID, or -1 if there is no
  957. // action to redo.
  958. public int redo()
  959. {
  960. if (_redo_points.size() == 0)
  961. return -1;
  962. RestorePoint rp = _redo_points.read_restore_point();
  963. _undo_points.write_restore_point(rp.id, _undo.size(), rp.data);
  964. redo_until(rp.size);
  965. undo_redo(false, rp.id, rp.data);
  966. return rp.id;
  967. }
  968. private void undo_until(uint32 size)
  969. {
  970. undo_redo_until(size, _undo, _redo);
  971. }
  972. private void redo_until(uint32 size)
  973. {
  974. undo_redo_until(size, _redo, _undo);
  975. }
  976. private void undo_redo_until(uint32 size, Stack undo, Stack redo)
  977. {
  978. while (undo.size() != size)
  979. {
  980. uint32 type = undo.peek_type();
  981. if (type == Action.CREATE)
  982. {
  983. Action t = undo.read_action();
  984. assert(t == Action.CREATE);
  985. Guid id = undo.read_guid();
  986. redo.write_destroy_action(id);
  987. create_internal(id);
  988. }
  989. else if (type == Action.DESTROY)
  990. {
  991. Action t = undo.read_action();
  992. assert(t == Action.DESTROY);
  993. Guid id = undo.read_guid();
  994. redo.write_create_action(id);
  995. destroy_internal(id);
  996. }
  997. else if (type == Action.SET_PROPERTY_NULL)
  998. {
  999. Action t = undo.read_action();
  1000. assert(t == Action.SET_PROPERTY_NULL);
  1001. Guid id = undo.read_guid();
  1002. string key = undo.read_string();
  1003. if (has_property(id, key))
  1004. {
  1005. if (get_data(id)[key].holds(typeof(bool)))
  1006. redo.write_set_property_bool_action(id, key, get_property_bool(id, key));
  1007. if (get_data(id)[key].holds(typeof(double)))
  1008. redo.write_set_property_double_action(id, key, get_property_double(id, key));
  1009. if (get_data(id)[key].holds(typeof(string)))
  1010. redo.write_set_property_string_action(id, key, get_property_string(id, key));
  1011. if (get_data(id)[key].holds(typeof(Guid)))
  1012. redo.write_set_property_guid_action(id, key, get_property_guid(id, key));
  1013. if (get_data(id)[key].holds(typeof(Vector3)))
  1014. redo.write_set_property_vector3_action(id, key, get_property_vector3(id, key));
  1015. if (get_data(id)[key].holds(typeof(Quaternion)))
  1016. redo.write_set_property_quaternion_action(id, key, get_property_quaternion(id, key));
  1017. }
  1018. else
  1019. {
  1020. redo.write_set_property_null_action(id, key);
  1021. }
  1022. set_property_internal(id, key, null);
  1023. }
  1024. else if (type == Action.SET_PROPERTY_BOOL)
  1025. {
  1026. Action t = undo.read_action();
  1027. assert(t == Action.SET_PROPERTY_BOOL);
  1028. Guid id = undo.read_guid();
  1029. string key = undo.read_string();
  1030. bool val = undo.read_bool();
  1031. if (has_property(id, key))
  1032. redo.write_set_property_bool_action(id, key, get_property_bool(id, key));
  1033. else
  1034. redo.write_set_property_null_action(id, key);
  1035. set_property_internal(id, key, val);
  1036. }
  1037. else if (type == Action.SET_PROPERTY_DOUBLE)
  1038. {
  1039. Action t = undo.read_action();
  1040. assert(t == Action.SET_PROPERTY_DOUBLE);
  1041. Guid id = undo.read_guid();
  1042. string key = undo.read_string();
  1043. double val = undo.read_double();
  1044. if (has_property(id, key))
  1045. redo.write_set_property_double_action(id, key, get_property_double(id, key));
  1046. else
  1047. redo.write_set_property_null_action(id, key);
  1048. set_property_internal(id, key, val);
  1049. }
  1050. else if (type == Action.SET_PROPERTY_STRING)
  1051. {
  1052. Action t = undo.read_action();
  1053. assert(t == Action.SET_PROPERTY_STRING);
  1054. Guid id = undo.read_guid();
  1055. string key = undo.read_string();
  1056. string val = undo.read_string();
  1057. if (has_property(id, key))
  1058. redo.write_set_property_string_action(id, key, get_property_string(id, key));
  1059. else
  1060. redo.write_set_property_null_action(id, key);
  1061. set_property_internal(id, key, val);
  1062. }
  1063. else if (type == Action.SET_PROPERTY_GUID)
  1064. {
  1065. Action t = undo.read_action();
  1066. assert(t == Action.SET_PROPERTY_GUID);
  1067. Guid id = undo.read_guid();
  1068. string key = undo.read_string();
  1069. Guid val = undo.read_guid();
  1070. if (has_property(id, key))
  1071. redo.write_set_property_guid_action(id, key, get_property_guid(id, key));
  1072. else
  1073. redo.write_set_property_null_action(id, key);
  1074. set_property_internal(id, key, val);
  1075. }
  1076. else if (type == Action.SET_PROPERTY_VECTOR3)
  1077. {
  1078. Action t = undo.read_action();
  1079. assert(t == Action.SET_PROPERTY_VECTOR3);
  1080. Guid id = undo.read_guid();
  1081. string key = undo.read_string();
  1082. Vector3 val = undo.read_vector3();
  1083. if (has_property(id, key))
  1084. redo.write_set_property_vector3_action(id, key, get_property_vector3(id, key));
  1085. else
  1086. redo.write_set_property_null_action(id, key);
  1087. set_property_internal(id, key, val);
  1088. }
  1089. else if (type == Action.SET_PROPERTY_QUATERNION)
  1090. {
  1091. Action t = undo.read_action();
  1092. assert(t == Action.SET_PROPERTY_QUATERNION);
  1093. Guid id = undo.read_guid();
  1094. string key = undo.read_string();
  1095. Quaternion val = undo.read_quaternion();
  1096. if (has_property(id, key))
  1097. redo.write_set_property_quaternion_action(id, key, get_property_quaternion(id, key));
  1098. else
  1099. redo.write_set_property_null_action(id, key);
  1100. set_property_internal(id, key, val);
  1101. }
  1102. else if (type == Action.ADD_TO_SET)
  1103. {
  1104. Action t = undo.read_action();
  1105. assert(t == Action.ADD_TO_SET);
  1106. Guid id = undo.read_guid();
  1107. string key = undo.read_string();
  1108. Guid item_id = undo.read_guid();
  1109. redo.write_remove_from_set_action(id, key, item_id);
  1110. add_to_set_internal(id, key, item_id);
  1111. }
  1112. else if (type == Action.REMOVE_FROM_SET)
  1113. {
  1114. Action t = undo.read_action();
  1115. assert(t == Action.REMOVE_FROM_SET);
  1116. Guid id = undo.read_guid();
  1117. string key = undo.read_string();
  1118. Guid item_id = undo.read_guid();
  1119. redo.write_add_to_set_action(id, key, item_id);
  1120. remove_from_set_internal(id, key, item_id);
  1121. }
  1122. }
  1123. }
  1124. }
  1125. }