database.vala 33 KB

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