database.vala 32 KB

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