database.vala 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320
  1. /*
  2. * Copyright (c) 2012-2021 Daniele Bartolini et al.
  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. // The number of changes to the database since the last successful state
  258. // synchronization (load(), save() etc.). If it is less than 0, the changes
  259. // came from undo(), otherwise they came from redo() or from regular calls to
  260. // create(), destroy(), set_*() etc. A value of 0 means there were no changes.
  261. public int _distance_from_last_sync;
  262. // Signals
  263. public signal void key_changed(Guid id, string key);
  264. public signal void undo_redo(bool undo, int id, Guid[] data);
  265. public Database()
  266. {
  267. _data = new HashMap<string, Value?>();
  268. _undo = new Stack();
  269. _redo = new Stack();
  270. _undo_points = new Stack();
  271. _redo_points = new Stack();
  272. reset();
  273. }
  274. /// Resets database to clean state.
  275. public void reset()
  276. {
  277. _data.clear();
  278. _undo.clear();
  279. _redo.clear();
  280. _undo_points.clear();
  281. _redo_points.clear();
  282. _distance_from_last_sync = 0;
  283. // This is a special field which stores all objects
  284. _data.set("_objects", new HashMap<string, Value?>());
  285. }
  286. /// Returns whether the database has been changed since last call to Save().
  287. public bool changed()
  288. {
  289. return _distance_from_last_sync != 0;
  290. }
  291. /// Saves database to path without marking it as not changed.
  292. public void dump(string path)
  293. {
  294. Hashtable json = encode();
  295. SJSON.save(json, path);
  296. }
  297. /// Saves database to path.
  298. public void save(string path)
  299. {
  300. dump(path);
  301. _distance_from_last_sync = 0;
  302. }
  303. /// Loads database from path.
  304. public void load(string path)
  305. {
  306. Hashtable json = SJSON.load(path);
  307. decode(json);
  308. _distance_from_last_sync = 0;
  309. }
  310. private Hashtable encode()
  311. {
  312. return encode_object(GUID_ZERO, _data);
  313. }
  314. private static bool is_valid_value(Value? value)
  315. {
  316. return value == null
  317. || value.holds(typeof(bool))
  318. || value.holds(typeof(double))
  319. || value.holds(typeof(string))
  320. || value.holds(typeof(Guid))
  321. || value.holds(typeof(Vector3))
  322. || value.holds(typeof(Quaternion))
  323. ;
  324. }
  325. private static bool is_valid_key(string key)
  326. {
  327. return key.length > 0
  328. && key != "_objects"
  329. && !key.has_prefix(".")
  330. && !key.has_suffix(".")
  331. ;
  332. }
  333. #if 0
  334. private static string value_to_string(Value? value)
  335. {
  336. if (value == null)
  337. return "null";
  338. if (value.holds(typeof(bool)))
  339. return ((bool)value).to_string();
  340. if (value.holds(typeof(double)))
  341. return ((double)value).to_string();
  342. if (value.holds(typeof(string)))
  343. return ((string)value).to_string();
  344. if (value.holds(typeof(Guid)))
  345. return ((Guid)value).to_string();
  346. if (value.holds(typeof(Vector3)))
  347. return ((Vector3)value).to_string();
  348. if (value.holds(typeof(Quaternion)))
  349. return ((Quaternion)value).to_string();
  350. if (value.holds(typeof(HashSet)))
  351. return "Set<Guid>";
  352. return "<invalid>";
  353. }
  354. #endif // CROWN_DEBUG
  355. public void decode(Hashtable json)
  356. {
  357. reset();
  358. decode_object(GUID_ZERO, "", json);
  359. }
  360. private void decode_object(Guid id, string db_key, Hashtable json)
  361. {
  362. string old_db = db_key;
  363. string k = db_key;
  364. string[] keys = json.keys.to_array();
  365. foreach (string key in keys)
  366. {
  367. assert(key != "_objects");
  368. if (key == "id")
  369. continue;
  370. Value? val = json[key];
  371. k += k == "" ? key : ("." + key);
  372. if (val.holds(typeof(Hashtable)))
  373. {
  374. Hashtable ht = (Hashtable)val;
  375. decode_object(id, k, ht);
  376. }
  377. else if (val.holds(typeof(ArrayList)))
  378. {
  379. ArrayList<Value?> arr = (ArrayList<Value?>)val;
  380. if (arr.size > 0 && arr[0].holds(typeof(double)))
  381. set_property_internal(1, id, k, decode_value(val));
  382. else
  383. decode_set(id, key, arr);
  384. }
  385. else
  386. {
  387. set_property_internal(1, id, k, decode_value(val));
  388. }
  389. k = old_db;
  390. }
  391. }
  392. private void decode_set(Guid id, string key, ArrayList<Value?> json)
  393. {
  394. // Set should be created even if it is empty.
  395. create_empty_set(1, id, key);
  396. for (int i = 0; i < json.size; ++i)
  397. {
  398. Hashtable obj = (Hashtable)json[i];
  399. Guid item_id = Guid.parse((string)obj["id"]);
  400. create_internal(1, item_id);
  401. decode_object(item_id, "", obj);
  402. add_to_set_internal(1, id, key, item_id);
  403. }
  404. }
  405. private Value? decode_value(Value? value)
  406. {
  407. if (value.holds(typeof(ArrayList)))
  408. {
  409. ArrayList<Value?> al = (ArrayList<Value?>)value;
  410. if (al.size == 3)
  411. return Vector3((double)al[0], (double)al[1], (double)al[2]);
  412. else if (al.size == 4)
  413. return Quaternion((double)al[0], (double)al[1], (double)al[2], (double)al[3]);
  414. else
  415. assert(false);
  416. }
  417. else if (value.holds(typeof(string)))
  418. {
  419. Guid id;
  420. if (Guid.try_parse((string)value, out id))
  421. return id;
  422. return value;
  423. }
  424. else if (value == null || value.holds(typeof(bool)) || value.holds(typeof(double)))
  425. {
  426. return value;
  427. }
  428. else
  429. {
  430. assert(false);
  431. }
  432. return null;
  433. }
  434. private Hashtable encode_object(Guid id, HashMap<string, Value?> db)
  435. {
  436. Hashtable obj = new Hashtable();
  437. if (id != GUID_ZERO)
  438. obj["id"] = id.to_string();
  439. string[] keys = db.keys.to_array();
  440. foreach (string key in keys)
  441. {
  442. if (key == "_objects")
  443. continue;
  444. // Since null-key is equivalent to non-existent key, skip serialization.
  445. if (db[key] == null)
  446. continue;
  447. string[] foo = key.split(".");
  448. Hashtable x = obj;
  449. if (foo.length > 1)
  450. {
  451. for (int i = 0; i < foo.length - 1; ++i)
  452. {
  453. string f = foo[i];
  454. if (x.has_key(f))
  455. {
  456. x = (Hashtable)x[f];
  457. continue;
  458. }
  459. Hashtable y = new Hashtable();
  460. x.set(f, y);
  461. x = y;
  462. }
  463. }
  464. x.set(foo[foo.length-1], encode_value(db[key]));
  465. }
  466. return obj;
  467. }
  468. private Value? encode_value(Value? value)
  469. {
  470. assert(is_valid_value(value) || value.holds(typeof(HashSet)));
  471. if (value.holds(typeof(Vector3)))
  472. {
  473. Vector3 v = (Vector3)value;
  474. ArrayList<Value?> arr = new Gee.ArrayList<Value?>();
  475. arr.add(v.x);
  476. arr.add(v.y);
  477. arr.add(v.z);
  478. return arr;
  479. }
  480. else if (value.holds(typeof(Quaternion)))
  481. {
  482. Quaternion q = (Quaternion)value;
  483. ArrayList<Value?> arr = new Gee.ArrayList<Value?>();
  484. arr.add(q.x);
  485. arr.add(q.y);
  486. arr.add(q.z);
  487. arr.add(q.w);
  488. return arr;
  489. }
  490. else if (value.holds(typeof(Guid)))
  491. {
  492. Guid id = (Guid)value;
  493. return id.to_string();
  494. }
  495. else if (value.holds(typeof(HashSet)))
  496. {
  497. HashSet<Guid?> hs = (HashSet<Guid?>)value;
  498. ArrayList<Value?> arr = new Gee.ArrayList<Value?>();
  499. foreach (Guid id in hs)
  500. {
  501. HashMap<string, Value?> objs = (HashMap<string, Value?>)_data["_objects"];
  502. arr.add(encode_object(id, (HashMap<string, Value?>)objs[id.to_string()]));
  503. }
  504. return arr;
  505. }
  506. else
  507. {
  508. return value;
  509. }
  510. }
  511. private HashMap<string, Value?> get_data(Guid id)
  512. {
  513. assert(has_object(id));
  514. return (HashMap<string, Value?>)(id == GUID_ZERO ? _data : ((HashMap<string, Value?>)_data["_objects"])[id.to_string()]);
  515. }
  516. private void create_internal(int dir, Guid id)
  517. {
  518. assert(id != GUID_ZERO);
  519. #if 0
  520. stdout.printf("create %s\n", id.to_string());
  521. #endif // CROWN_DEBUG
  522. ((HashMap<string, Value?>)_data["_objects"]).set(id.to_string(), new HashMap<string, Value?>());
  523. _distance_from_last_sync += dir;
  524. key_changed(id, "_objects");
  525. }
  526. private void destroy_internal(int dir, Guid id)
  527. {
  528. assert(id != GUID_ZERO);
  529. assert(has_object(id));
  530. #if 0
  531. stdout.printf("destroy %s\n", id.to_string());
  532. #endif // CROWN_DEBUG
  533. ((HashMap<string, Value?>)_data["_objects"]).unset(id.to_string());
  534. _distance_from_last_sync += dir;
  535. key_changed(id, "_objects");
  536. }
  537. private void set_property_internal(int dir, Guid id, string key, Value? value)
  538. {
  539. assert(has_object(id));
  540. assert(is_valid_key(key));
  541. assert(is_valid_value(value));
  542. #if 0
  543. stdout.printf("set_property %s %s %s\n"
  544. , id.to_string()
  545. , key
  546. , (value == null) ? "null" : value_to_string(value)
  547. );
  548. #endif // CROWN_DEBUG
  549. HashMap<string, Value?> ob = get_data(id);
  550. ob[key] = value;
  551. _distance_from_last_sync += dir;
  552. key_changed(id, key);
  553. }
  554. private void create_empty_set(int dir, Guid id, string key)
  555. {
  556. assert(has_object(id));
  557. assert(is_valid_key(key));
  558. HashMap<string, Value?> ob = get_data(id);
  559. assert(!ob.has_key(key));
  560. ob[key] = new HashSet<Guid?>(Guid.hash_func, Guid.equal_func);
  561. }
  562. private void add_to_set_internal(int dir, Guid id, string key, Guid item_id)
  563. {
  564. assert(has_object(id));
  565. assert(is_valid_key(key));
  566. assert(item_id != GUID_ZERO);
  567. assert(has_object(item_id));
  568. #if 0
  569. stdout.printf("add_to_set %s %s %s\n"
  570. , id.to_string()
  571. , key
  572. , item_id.to_string()
  573. );
  574. #endif // CROWN_DEBUG
  575. HashMap<string, Value?> ob = get_data(id);
  576. if (!ob.has_key(key))
  577. {
  578. HashSet<Guid?> hs = new HashSet<Guid?>(Guid.hash_func, Guid.equal_func);
  579. hs.add(item_id);
  580. ob[key] = hs;
  581. }
  582. else
  583. {
  584. ((HashSet<Guid?>)ob[key]).add(item_id);
  585. }
  586. _distance_from_last_sync += dir;
  587. key_changed(id, key);
  588. }
  589. private void remove_from_set_internal(int dir, Guid id, string key, Guid item_id)
  590. {
  591. assert(has_object(id));
  592. assert(is_valid_key(key));
  593. assert(item_id != GUID_ZERO);
  594. #if 0
  595. stdout.printf("remove_from_set %s %s %s\n"
  596. , id.to_string()
  597. , key
  598. , item_id.to_string()
  599. );
  600. #endif // CROWN_DEBUG
  601. HashMap<string, Value?> ob = get_data(id);
  602. ((HashSet<Guid?>)ob[key]).remove(item_id);
  603. _distance_from_last_sync += dir;
  604. key_changed(id, key);
  605. }
  606. public void create(Guid id)
  607. {
  608. assert(id != GUID_ZERO);
  609. assert(!has_object(id));
  610. _undo.write_destroy_action(id);
  611. _redo.clear();
  612. _redo_points.clear();
  613. create_internal(1, id);
  614. }
  615. public void destroy(Guid id)
  616. {
  617. assert(id != GUID_ZERO);
  618. assert(has_object(id));
  619. HashMap<string, Value?> o = get_data(id);
  620. string[] keys = o.keys.to_array();
  621. foreach (string key in keys)
  622. {
  623. Value? value = o[key];
  624. if (value.holds(typeof(HashSet)))
  625. {
  626. HashSet<Guid?> hs = (HashSet<Guid?>)value;
  627. Guid?[] ids = hs.to_array();
  628. foreach (Guid item_id in ids)
  629. {
  630. remove_from_set(id, key, item_id);
  631. destroy(item_id);
  632. }
  633. }
  634. else
  635. {
  636. set_property_null(id, key);
  637. }
  638. }
  639. _undo.write_create_action(id);
  640. _redo.clear();
  641. _redo_points.clear();
  642. destroy_internal(1, id);
  643. }
  644. public void set_property_null(Guid id, string key)
  645. {
  646. assert(has_object(id));
  647. assert(is_valid_key(key));
  648. assert(is_valid_value(null));
  649. HashMap<string, Value?> ob = get_data(id);
  650. if (ob.has_key(key) && ob[key] != null)
  651. {
  652. if (ob[key].holds(typeof(bool)))
  653. _undo.write_set_property_bool_action(id, key, (bool)ob[key]);
  654. if (ob[key].holds(typeof(double)))
  655. _undo.write_set_property_double_action(id, key, (double)ob[key]);
  656. if (ob[key].holds(typeof(string)))
  657. _undo.write_set_property_string_action(id, key, (string)ob[key]);
  658. if (ob[key].holds(typeof(Guid)))
  659. _undo.write_set_property_guid_action(id, key, (Guid)ob[key]);
  660. if (ob[key].holds(typeof(Vector3)))
  661. _undo.write_set_property_vector3_action(id, key, (Vector3)ob[key]);
  662. if (ob[key].holds(typeof(Quaternion)))
  663. _undo.write_set_property_quaternion_action(id, key, (Quaternion)ob[key]);
  664. }
  665. else
  666. {
  667. _undo.write_set_property_null_action(id, key);
  668. }
  669. _redo.clear();
  670. _redo_points.clear();
  671. set_property_internal(1, id, key, null);
  672. }
  673. public void set_property_bool(Guid id, string key, bool val)
  674. {
  675. assert(has_object(id));
  676. assert(is_valid_key(key));
  677. assert(is_valid_value(val));
  678. HashMap<string, Value?> ob = get_data(id);
  679. if (ob.has_key(key) && ob[key] != null)
  680. _undo.write_set_property_bool_action(id, key, (bool)ob[key]);
  681. else
  682. _undo.write_set_property_null_action(id, key);
  683. _redo.clear();
  684. _redo_points.clear();
  685. set_property_internal(1, id, key, val);
  686. }
  687. public void set_property_double(Guid id, string key, double val)
  688. {
  689. assert(has_object(id));
  690. assert(is_valid_key(key));
  691. assert(is_valid_value(val));
  692. HashMap<string, Value?> ob = get_data(id);
  693. if (ob.has_key(key) && ob[key] != null)
  694. _undo.write_set_property_double_action(id, key, (double)ob[key]);
  695. else
  696. _undo.write_set_property_null_action(id, key);
  697. _redo.clear();
  698. _redo_points.clear();
  699. set_property_internal(1, id, key, val);
  700. }
  701. public void set_property_string(Guid id, string key, string val)
  702. {
  703. assert(has_object(id));
  704. assert(is_valid_key(key));
  705. assert(is_valid_value(val));
  706. HashMap<string, Value?> ob = get_data(id);
  707. if (ob.has_key(key) && ob[key] != null)
  708. _undo.write_set_property_string_action(id, key, (string)ob[key]);
  709. else
  710. _undo.write_set_property_null_action(id, key);
  711. _redo.clear();
  712. _redo_points.clear();
  713. set_property_internal(1, id, key, val);
  714. }
  715. public void set_property_guid(Guid id, string key, Guid val)
  716. {
  717. assert(has_object(id));
  718. assert(is_valid_key(key));
  719. assert(is_valid_value(val));
  720. HashMap<string, Value?> ob = get_data(id);
  721. if (ob.has_key(key) && ob[key] != null)
  722. _undo.write_set_property_guid_action(id, key, (Guid)ob[key]);
  723. else
  724. _undo.write_set_property_null_action(id, key);
  725. _redo.clear();
  726. _redo_points.clear();
  727. set_property_internal(1, id, key, val);
  728. }
  729. public void set_property_vector3(Guid id, string key, Vector3 val)
  730. {
  731. assert(has_object(id));
  732. assert(is_valid_key(key));
  733. assert(is_valid_value(val));
  734. HashMap<string, Value?> ob = get_data(id);
  735. if (ob.has_key(key) && ob[key] != null)
  736. _undo.write_set_property_vector3_action(id, key, (Vector3)ob[key]);
  737. else
  738. _undo.write_set_property_null_action(id, key);
  739. _redo.clear();
  740. _redo_points.clear();
  741. set_property_internal(1, id, key, val);
  742. }
  743. public void set_property_quaternion(Guid id, string key, Quaternion val)
  744. {
  745. assert(has_object(id));
  746. assert(is_valid_key(key));
  747. assert(is_valid_value(val));
  748. HashMap<string, Value?> ob = get_data(id);
  749. if (ob.has_key(key) && ob[key] != null)
  750. _undo.write_set_property_quaternion_action(id, key, (Quaternion)ob[key]);
  751. else
  752. _undo.write_set_property_null_action(id, key);
  753. _redo.clear();
  754. _redo_points.clear();
  755. set_property_internal(1, id, key, val);
  756. }
  757. public void add_to_set(Guid id, string key, Guid item_id)
  758. {
  759. assert(has_object(id));
  760. assert(is_valid_key(key));
  761. assert(item_id != GUID_ZERO);
  762. assert(has_object(item_id));
  763. _undo.write_remove_from_set_action(id, key, item_id);
  764. _redo.clear();
  765. _redo_points.clear();
  766. add_to_set_internal(1, id, key, item_id);
  767. }
  768. public void remove_from_set(Guid id, string key, Guid item_id)
  769. {
  770. assert(has_object(id));
  771. assert(is_valid_key(key));
  772. assert(item_id != GUID_ZERO);
  773. _undo.write_add_to_set_action(id, key, item_id);
  774. _redo.clear();
  775. _redo_points.clear();
  776. remove_from_set_internal(1, id, key, item_id);
  777. }
  778. public bool has_object(Guid id)
  779. {
  780. return id == GUID_ZERO || ((HashMap<string, Value?>)_data["_objects"]).has_key(id.to_string());
  781. }
  782. public bool has_property(Guid id, string key)
  783. {
  784. return get_property(id, key) != null;
  785. }
  786. public Value? get_property(Guid id, string key)
  787. {
  788. assert(has_object(id));
  789. assert(is_valid_key(key));
  790. HashMap<string, Value?> ob = get_data(id);
  791. Value? value = (ob.has_key(key) ? ob[key] : null);
  792. #if 0
  793. stdout.printf("get_property %s %s %s\n"
  794. , id.to_string()
  795. , key
  796. , (value == null) ? "null" : value_to_string(value)
  797. );
  798. #endif // CROWN_DEBUG
  799. return value;
  800. }
  801. public bool get_property_bool(Guid id, string key)
  802. {
  803. return (bool)get_property(id, key);
  804. }
  805. public double get_property_double(Guid id, string key)
  806. {
  807. return (double)get_property(id, key);
  808. }
  809. public string get_property_string(Guid id, string key)
  810. {
  811. return (string)get_property(id, key);
  812. }
  813. public Guid get_property_guid(Guid id, string key)
  814. {
  815. return (Guid)get_property(id, key);
  816. }
  817. public Vector3 get_property_vector3(Guid id, string key)
  818. {
  819. return (Vector3)get_property(id, key);
  820. }
  821. public Quaternion get_property_quaternion(Guid id, string key)
  822. {
  823. return (Quaternion)get_property(id, key);
  824. }
  825. public HashSet<Guid?> get_property_set(Guid id, string key, HashSet<Guid?> deffault)
  826. {
  827. assert(has_object(id));
  828. assert(is_valid_key(key));
  829. HashMap<string, Value?> ob = get_data(id);
  830. if (ob.has_key(key))
  831. return ob[key] as HashSet<Guid?>;
  832. else
  833. return deffault;
  834. #if 0
  835. // stdout.printf("get_property %s %s %s\n"
  836. // , id.to_string()
  837. // , key
  838. // , (value == null) ? "null" : value_to_string(value)
  839. // );
  840. #endif // CROWN_DEBUG
  841. }
  842. public HashMap<string, Value?> get_object(Guid id)
  843. {
  844. return (HashMap<string, Value?>)get_data(GUID_ZERO)[id.to_string()];
  845. }
  846. public string[] get_keys(Guid id)
  847. {
  848. HashMap<string, Value?> data = get_data(id);
  849. return data.keys.to_array();
  850. }
  851. public void add_restore_point(int id, Guid[] data)
  852. {
  853. #if 0
  854. stdout.printf("add_restore_point %d, undo size = %u\n", id, _undo.size());
  855. #endif // CROWN_DEBUG
  856. _undo_points.write_restore_point(id, _undo.size(), data);
  857. _redo.clear();
  858. _redo_points.clear();
  859. }
  860. /// Duplicates the object specified by id and assign new_id to the duplicated object.
  861. public void duplicate(Guid id, Guid new_id)
  862. {
  863. assert(id != GUID_ZERO);
  864. assert(new_id != GUID_ZERO);
  865. assert(id != new_id);
  866. assert(has_object(id));
  867. create(new_id);
  868. HashMap<string, Value?> ob = get_data(id);
  869. string[] keys = ob.keys.to_array();
  870. foreach (string key in keys)
  871. {
  872. Value? val = ob[key];
  873. if (val.holds(typeof(HashSet)))
  874. {
  875. HashSet<Guid?> hs = (HashSet<Guid?>)val;
  876. foreach (Guid j in hs)
  877. {
  878. Guid x = Guid.new_guid();
  879. duplicate(j, x);
  880. add_to_set(new_id, key, x);
  881. }
  882. }
  883. else
  884. {
  885. if (ob[key] == null)
  886. set_property_null(new_id, key);
  887. if (ob[key].holds(typeof(bool)))
  888. set_property_bool(new_id, key, (bool)ob[key]);
  889. if (ob[key].holds(typeof(double)))
  890. set_property_double(new_id, key, (double)ob[key]);
  891. if (ob[key].holds(typeof(string)))
  892. set_property_string(new_id, key, (string)ob[key]);
  893. if (ob[key].holds(typeof(Guid)))
  894. set_property_guid(new_id, key, (Guid)ob[key]);
  895. if (ob[key].holds(typeof(Vector3)))
  896. set_property_vector3(new_id, key, (Vector3)ob[key]);
  897. if (ob[key].holds(typeof(Quaternion)))
  898. set_property_quaternion(new_id, key, (Quaternion)ob[key]);
  899. }
  900. }
  901. }
  902. /// Copies the database to db under the given new_key.
  903. public void copy_to(Database db, string new_key)
  904. {
  905. assert(db != null);
  906. assert(is_valid_key(new_key));
  907. copy_deep(db, GUID_ZERO, new_key);
  908. }
  909. public void copy_deep(Database db, Guid id, string new_key)
  910. {
  911. HashMap<string, Value?> ob = get_data(id);
  912. string[] keys = ob.keys.to_array();
  913. foreach (string key in keys)
  914. {
  915. if (key == "_objects")
  916. continue;
  917. Value? value = ob[key];
  918. if (value.holds(typeof(HashSet)))
  919. {
  920. HashSet<Guid?> hs = (HashSet<Guid?>)value;
  921. foreach (Guid j in hs)
  922. {
  923. db.create(j);
  924. copy_deep(db, j, "");
  925. db.add_to_set(id, new_key + (new_key == "" ? "" : ".") + key, j);
  926. }
  927. }
  928. else
  929. {
  930. string kk = new_key + (new_key == "" ? "" : ".") + key;
  931. if (ob[key] == null)
  932. db.set_property_null(id, kk);
  933. if (ob[key].holds(typeof(bool)))
  934. db.set_property_bool(id, kk, (bool)ob[key]);
  935. if (ob[key].holds(typeof(double)))
  936. db.set_property_double(id, kk, (double)ob[key]);
  937. if (ob[key].holds(typeof(string)))
  938. db.set_property_string(id, kk, (string)ob[key]);
  939. if (ob[key].holds(typeof(Guid)))
  940. db.set_property_guid(id, kk, (Guid)ob[key]);
  941. if (ob[key].holds(typeof(Vector3)))
  942. db.set_property_vector3(id, kk, (Vector3)ob[key]);
  943. if (ob[key].holds(typeof(Quaternion)))
  944. db.set_property_quaternion(id, kk, (Quaternion)ob[key]);
  945. }
  946. }
  947. }
  948. // Un-does the last action and returns its ID, or -1 if there is no
  949. // action to undo.
  950. public int undo()
  951. {
  952. if (_undo_points.size() == 0)
  953. return -1;
  954. RestorePoint rp = _undo_points.read_restore_point();
  955. _redo_points.write_restore_point(rp.id, _redo.size(), rp.data);
  956. undo_until(rp.size);
  957. undo_redo(true, rp.id, rp.data);
  958. return rp.id;
  959. }
  960. // Re-does the last action and returns its ID, or -1 if there is no
  961. // action to redo.
  962. public int redo()
  963. {
  964. if (_redo_points.size() == 0)
  965. return -1;
  966. RestorePoint rp = _redo_points.read_restore_point();
  967. _undo_points.write_restore_point(rp.id, _undo.size(), rp.data);
  968. redo_until(rp.size);
  969. undo_redo(false, rp.id, rp.data);
  970. return rp.id;
  971. }
  972. private void undo_until(uint32 size)
  973. {
  974. undo_redo_until(size, _undo, _redo);
  975. }
  976. private void redo_until(uint32 size)
  977. {
  978. undo_redo_until(size, _redo, _undo);
  979. }
  980. private void undo_redo_until(uint32 size, Stack undo, Stack redo)
  981. {
  982. int dir = undo == _undo ? -1 : 1;
  983. while (undo.size() != size)
  984. {
  985. uint32 type = undo.peek_type();
  986. if (type == Action.CREATE)
  987. {
  988. Action t = undo.read_action();
  989. assert(t == Action.CREATE);
  990. Guid id = undo.read_guid();
  991. redo.write_destroy_action(id);
  992. create_internal(dir, id);
  993. }
  994. else if (type == Action.DESTROY)
  995. {
  996. Action t = undo.read_action();
  997. assert(t == Action.DESTROY);
  998. Guid id = undo.read_guid();
  999. redo.write_create_action(id);
  1000. destroy_internal(dir, id);
  1001. }
  1002. else if (type == Action.SET_PROPERTY_NULL)
  1003. {
  1004. Action t = undo.read_action();
  1005. assert(t == Action.SET_PROPERTY_NULL);
  1006. Guid id = undo.read_guid();
  1007. string key = undo.read_string();
  1008. if (has_property(id, key))
  1009. {
  1010. if (get_data(id)[key].holds(typeof(bool)))
  1011. redo.write_set_property_bool_action(id, key, get_property_bool(id, key));
  1012. if (get_data(id)[key].holds(typeof(double)))
  1013. redo.write_set_property_double_action(id, key, get_property_double(id, key));
  1014. if (get_data(id)[key].holds(typeof(string)))
  1015. redo.write_set_property_string_action(id, key, get_property_string(id, key));
  1016. if (get_data(id)[key].holds(typeof(Guid)))
  1017. redo.write_set_property_guid_action(id, key, get_property_guid(id, key));
  1018. if (get_data(id)[key].holds(typeof(Vector3)))
  1019. redo.write_set_property_vector3_action(id, key, get_property_vector3(id, key));
  1020. if (get_data(id)[key].holds(typeof(Quaternion)))
  1021. redo.write_set_property_quaternion_action(id, key, get_property_quaternion(id, key));
  1022. }
  1023. else
  1024. {
  1025. redo.write_set_property_null_action(id, key);
  1026. }
  1027. set_property_internal(dir, id, key, null);
  1028. }
  1029. else if (type == Action.SET_PROPERTY_BOOL)
  1030. {
  1031. Action t = undo.read_action();
  1032. assert(t == Action.SET_PROPERTY_BOOL);
  1033. Guid id = undo.read_guid();
  1034. string key = undo.read_string();
  1035. bool val = undo.read_bool();
  1036. if (has_property(id, key))
  1037. redo.write_set_property_bool_action(id, key, get_property_bool(id, key));
  1038. else
  1039. redo.write_set_property_null_action(id, key);
  1040. set_property_internal(dir, id, key, val);
  1041. }
  1042. else if (type == Action.SET_PROPERTY_DOUBLE)
  1043. {
  1044. Action t = undo.read_action();
  1045. assert(t == Action.SET_PROPERTY_DOUBLE);
  1046. Guid id = undo.read_guid();
  1047. string key = undo.read_string();
  1048. double val = undo.read_double();
  1049. if (has_property(id, key))
  1050. redo.write_set_property_double_action(id, key, get_property_double(id, key));
  1051. else
  1052. redo.write_set_property_null_action(id, key);
  1053. set_property_internal(dir, id, key, val);
  1054. }
  1055. else if (type == Action.SET_PROPERTY_STRING)
  1056. {
  1057. Action t = undo.read_action();
  1058. assert(t == Action.SET_PROPERTY_STRING);
  1059. Guid id = undo.read_guid();
  1060. string key = undo.read_string();
  1061. string val = undo.read_string();
  1062. if (has_property(id, key))
  1063. redo.write_set_property_string_action(id, key, get_property_string(id, key));
  1064. else
  1065. redo.write_set_property_null_action(id, key);
  1066. set_property_internal(dir, id, key, val);
  1067. }
  1068. else if (type == Action.SET_PROPERTY_GUID)
  1069. {
  1070. Action t = undo.read_action();
  1071. assert(t == Action.SET_PROPERTY_GUID);
  1072. Guid id = undo.read_guid();
  1073. string key = undo.read_string();
  1074. Guid val = undo.read_guid();
  1075. if (has_property(id, key))
  1076. redo.write_set_property_guid_action(id, key, get_property_guid(id, key));
  1077. else
  1078. redo.write_set_property_null_action(id, key);
  1079. set_property_internal(dir, id, key, val);
  1080. }
  1081. else if (type == Action.SET_PROPERTY_VECTOR3)
  1082. {
  1083. Action t = undo.read_action();
  1084. assert(t == Action.SET_PROPERTY_VECTOR3);
  1085. Guid id = undo.read_guid();
  1086. string key = undo.read_string();
  1087. Vector3 val = undo.read_vector3();
  1088. if (has_property(id, key))
  1089. redo.write_set_property_vector3_action(id, key, get_property_vector3(id, key));
  1090. else
  1091. redo.write_set_property_null_action(id, key);
  1092. set_property_internal(dir, id, key, val);
  1093. }
  1094. else if (type == Action.SET_PROPERTY_QUATERNION)
  1095. {
  1096. Action t = undo.read_action();
  1097. assert(t == Action.SET_PROPERTY_QUATERNION);
  1098. Guid id = undo.read_guid();
  1099. string key = undo.read_string();
  1100. Quaternion val = undo.read_quaternion();
  1101. if (has_property(id, key))
  1102. redo.write_set_property_quaternion_action(id, key, get_property_quaternion(id, key));
  1103. else
  1104. redo.write_set_property_null_action(id, key);
  1105. set_property_internal(dir, id, key, val);
  1106. }
  1107. else if (type == Action.ADD_TO_SET)
  1108. {
  1109. Action t = undo.read_action();
  1110. assert(t == Action.ADD_TO_SET);
  1111. Guid id = undo.read_guid();
  1112. string key = undo.read_string();
  1113. Guid item_id = undo.read_guid();
  1114. redo.write_remove_from_set_action(id, key, item_id);
  1115. add_to_set_internal(dir, id, key, item_id);
  1116. }
  1117. else if (type == Action.REMOVE_FROM_SET)
  1118. {
  1119. Action t = undo.read_action();
  1120. assert(t == Action.REMOVE_FROM_SET);
  1121. Guid id = undo.read_guid();
  1122. string key = undo.read_string();
  1123. Guid item_id = undo.read_guid();
  1124. redo.write_add_to_set_action(id, key, item_id);
  1125. remove_from_set_internal(dir, id, key, item_id);
  1126. }
  1127. }
  1128. }
  1129. }
  1130. }