database.vala 32 KB

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