database.vala 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300
  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. // The "type" key defines object type only if it appears
  399. // in the root of a JSON object (k == "").
  400. if (!has_property(id, "type"))
  401. set_object_type(id, (string)json["type"]);
  402. }
  403. string[] keys = json.keys.to_array();
  404. foreach (string key in keys) {
  405. if (key == "id")
  406. continue;
  407. Value? val = json[key];
  408. k += k == "" ? key : ("." + key);
  409. if (val.holds(typeof(Hashtable))) {
  410. Hashtable ht = (Hashtable)val;
  411. decode_object(id, k, ht);
  412. } else if (val.holds(typeof(ArrayList))) {
  413. ArrayList<Value?> arr = (ArrayList<Value?>)val;
  414. if (arr.size > 0 && arr[0].holds(typeof(double)))
  415. set_property_internal(0, id, k, decode_value(val));
  416. else
  417. decode_set(id, key, arr);
  418. } else {
  419. set_property_internal(0, id, k, decode_value(val));
  420. }
  421. k = old_db;
  422. }
  423. }
  424. private void decode_set(Guid owner_id, string key, ArrayList<Value?> json)
  425. {
  426. // Set should be created even if it is empty.
  427. create_empty_set(0, owner_id, key);
  428. for (int i = 0; i < json.size; ++i) {
  429. Hashtable obj = (Hashtable)json[i];
  430. Guid obj_id = Guid.parse((string)obj["id"]);
  431. create_internal(0, obj_id);
  432. decode_object(obj_id, "", obj);
  433. add_to_set_internal(0, owner_id, key, obj_id);
  434. }
  435. }
  436. private Value? decode_value(Value? value)
  437. {
  438. if (value.holds(typeof(ArrayList))) {
  439. ArrayList<Value?> al = (ArrayList<Value?>)value;
  440. if (al.size == 3)
  441. return Vector3((double)al[0], (double)al[1], (double)al[2]);
  442. else if (al.size == 4)
  443. return Quaternion((double)al[0], (double)al[1], (double)al[2], (double)al[3]);
  444. else
  445. assert(false);
  446. } else if (value.holds(typeof(string))) {
  447. Guid id;
  448. if (Guid.try_parse((string)value, out id))
  449. return id;
  450. return value;
  451. } else if (value == null || value.holds(typeof(bool)) || value.holds(typeof(double))) {
  452. return value;
  453. } else {
  454. assert(false);
  455. }
  456. return null;
  457. }
  458. private Hashtable encode_object(Guid id, HashMap<string, Value?> db)
  459. {
  460. Hashtable obj = new Hashtable();
  461. if (id != GUID_ZERO)
  462. obj["id"] = id.to_string();
  463. string[] keys = db.keys.to_array();
  464. foreach (string key in keys) {
  465. // Since null-key is equivalent to non-existent key, skip serialization.
  466. if (db[key] == null)
  467. continue;
  468. string[] foo = key.split(".");
  469. Hashtable x = obj;
  470. if (foo.length > 1) {
  471. for (int i = 0; i < foo.length - 1; ++i) {
  472. string f = foo[i];
  473. if (x.has_key(f)) {
  474. x = (Hashtable)x[f];
  475. continue;
  476. }
  477. Hashtable y = new Hashtable();
  478. x.set(f, y);
  479. x = y;
  480. }
  481. }
  482. x.set(foo[foo.length - 1], encode_value(db[key]));
  483. }
  484. return obj;
  485. }
  486. private Value? encode_value(Value? value)
  487. {
  488. assert(is_valid_value(value) || value.holds(typeof(HashSet)));
  489. if (value.holds(typeof(Vector3))) {
  490. Vector3 v = (Vector3)value;
  491. ArrayList<Value?> arr = new Gee.ArrayList<Value?>();
  492. arr.add(v.x);
  493. arr.add(v.y);
  494. arr.add(v.z);
  495. return arr;
  496. } else if (value.holds(typeof(Quaternion))) {
  497. Quaternion q = (Quaternion)value;
  498. ArrayList<Value?> arr = new Gee.ArrayList<Value?>();
  499. arr.add(q.x);
  500. arr.add(q.y);
  501. arr.add(q.z);
  502. arr.add(q.w);
  503. return arr;
  504. } else if (value.holds(typeof(Guid))) {
  505. Guid id = (Guid)value;
  506. return id.to_string();
  507. } else if (value.holds(typeof(HashSet))) {
  508. HashSet<Guid?> hs = (HashSet<Guid?>)value;
  509. ArrayList<Value?> arr = new Gee.ArrayList<Value?>();
  510. foreach (Guid id in hs) {
  511. arr.add(encode_object(id, get_data(id)));
  512. }
  513. return arr;
  514. } else {
  515. return value;
  516. }
  517. }
  518. private HashMap<string, Value?> get_data(Guid id)
  519. {
  520. assert(has_object(id));
  521. return _data[id];
  522. }
  523. private void create_internal(int dir, Guid id)
  524. {
  525. assert(id != GUID_ZERO);
  526. if (_debug)
  527. logi("create %s".printf(id.to_string()));
  528. _data[id] = new HashMap<string, Value?>();
  529. _distance_from_last_sync += dir;
  530. object_created(id);
  531. }
  532. private void destroy_internal(int dir, Guid id)
  533. {
  534. assert(id != GUID_ZERO);
  535. assert(has_object(id));
  536. if (_debug)
  537. logi("destroy %s".printf(id.to_string()));
  538. object_destroyed(id);
  539. _distance_from_last_sync += dir;
  540. _data.unset(id);
  541. }
  542. private void set_property_internal(int dir, Guid id, string key, Value? value)
  543. {
  544. assert(has_object(id));
  545. assert(is_valid_key(key));
  546. assert(is_valid_value(value));
  547. if (_debug)
  548. logi("set_property %s %s %s".printf(id.to_string(), key, (value == null) ? "null" : value_to_string(value)));
  549. HashMap<string, Value?> ob = get_data(id);
  550. ob[key] = value;
  551. _distance_from_last_sync += dir;
  552. key_changed(id, key);
  553. }
  554. private void create_empty_set(int dir, Guid id, string key)
  555. {
  556. assert(has_object(id));
  557. assert(is_valid_key(key));
  558. HashMap<string, Value?> ob = get_data(id);
  559. assert(!ob.has_key(key));
  560. ob[key] = new HashSet<Guid?>(Guid.hash_func, Guid.equal_func);
  561. }
  562. private void add_to_set_internal(int dir, Guid id, string key, Guid item_id)
  563. {
  564. assert(has_object(id));
  565. assert(is_valid_key(key));
  566. assert(item_id != GUID_ZERO);
  567. assert(has_object(item_id));
  568. if (_debug)
  569. logi("add_to_set %s %s %s".printf(id.to_string(), key, item_id.to_string()));
  570. HashMap<string, Value?> ob = get_data(id);
  571. if (!ob.has_key(key)) {
  572. HashSet<Guid?> hs = new HashSet<Guid?>(Guid.hash_func, Guid.equal_func);
  573. hs.add(item_id);
  574. ob[key] = hs;
  575. } else {
  576. ((HashSet<Guid?>)ob[key]).add(item_id);
  577. }
  578. _distance_from_last_sync += dir;
  579. key_changed(id, key);
  580. }
  581. private void remove_from_set_internal(int dir, Guid id, string key, Guid item_id)
  582. {
  583. assert(has_object(id));
  584. assert(is_valid_key(key));
  585. assert(item_id != GUID_ZERO);
  586. if (_debug)
  587. logi("remove_from_set %s %s %s".printf(id.to_string(), key, item_id.to_string()));
  588. HashMap<string, Value?> ob = get_data(id);
  589. ((HashSet<Guid?>)ob[key]).remove(item_id);
  590. _distance_from_last_sync += dir;
  591. key_changed(id, key);
  592. }
  593. // Returns the type of the object @a id.
  594. public string object_type(Guid id)
  595. {
  596. assert(has_object(id));
  597. return (string)get_data(id)["type"];
  598. }
  599. // Sets the @a type of the object @a id.
  600. // This is called automatically when loading data or when new objects are created via create().
  601. // It can occasionally be called manually after loading legacy data with no type information
  602. // stored inside objects.
  603. public void set_object_type(Guid id, string type)
  604. {
  605. assert(has_object(id));
  606. get_data(id)["type"] = type;
  607. }
  608. public void create(Guid id, string type)
  609. {
  610. assert(id != GUID_ZERO);
  611. assert(!has_object(id));
  612. _undo.write_destroy_action(id, type);
  613. _redo.clear();
  614. _redo_points.clear();
  615. create_internal(1, id);
  616. set_object_type(id, type);
  617. object_created(id);
  618. }
  619. public void destroy(Guid id)
  620. {
  621. assert(id != GUID_ZERO);
  622. assert(has_object(id));
  623. string obj_type = object_type(id);
  624. HashMap<string, Value?> o = get_data(id);
  625. string[] keys = o.keys.to_array();
  626. foreach (string key in keys) {
  627. Value? value = o[key];
  628. if (value.holds(typeof(HashSet))) {
  629. HashSet<Guid?> hs = (HashSet<Guid?>)value;
  630. Guid?[] ids = hs.to_array();
  631. foreach (Guid item_id in ids) {
  632. remove_from_set(id, key, item_id);
  633. destroy(item_id);
  634. }
  635. } else {
  636. set_property_null(id, key);
  637. }
  638. }
  639. _undo.write_create_action(id, obj_type);
  640. _redo.clear();
  641. _redo_points.clear();
  642. destroy_internal(1, id);
  643. }
  644. public void set_property_null(Guid id, string key)
  645. {
  646. assert(has_object(id));
  647. assert(is_valid_key(key));
  648. assert(is_valid_value(null));
  649. HashMap<string, Value?> ob = get_data(id);
  650. if (ob.has_key(key) && ob[key] != null) {
  651. if (ob[key].holds(typeof(bool)))
  652. _undo.write_set_property_bool_action(id, key, (bool)ob[key]);
  653. if (ob[key].holds(typeof(double)))
  654. _undo.write_set_property_double_action(id, key, (double)ob[key]);
  655. if (ob[key].holds(typeof(string)))
  656. _undo.write_set_property_string_action(id, key, (string)ob[key]);
  657. if (ob[key].holds(typeof(Guid)))
  658. _undo.write_set_property_guid_action(id, key, (Guid)ob[key]);
  659. if (ob[key].holds(typeof(Vector3)))
  660. _undo.write_set_property_vector3_action(id, key, (Vector3)ob[key]);
  661. if (ob[key].holds(typeof(Quaternion)))
  662. _undo.write_set_property_quaternion_action(id, key, (Quaternion)ob[key]);
  663. } else {
  664. _undo.write_set_property_null_action(id, key);
  665. }
  666. _redo.clear();
  667. _redo_points.clear();
  668. set_property_internal(1, id, key, null);
  669. }
  670. public void set_property_bool(Guid id, string key, bool val)
  671. {
  672. assert(has_object(id));
  673. assert(is_valid_key(key));
  674. assert(is_valid_value(val));
  675. HashMap<string, Value?> ob = get_data(id);
  676. if (ob.has_key(key) && ob[key] != null)
  677. _undo.write_set_property_bool_action(id, key, (bool)ob[key]);
  678. else
  679. _undo.write_set_property_null_action(id, key);
  680. _redo.clear();
  681. _redo_points.clear();
  682. set_property_internal(1, id, key, val);
  683. }
  684. public void set_property_double(Guid id, string key, double val)
  685. {
  686. assert(has_object(id));
  687. assert(is_valid_key(key));
  688. assert(is_valid_value(val));
  689. HashMap<string, Value?> ob = get_data(id);
  690. if (ob.has_key(key) && ob[key] != null)
  691. _undo.write_set_property_double_action(id, key, (double)ob[key]);
  692. else
  693. _undo.write_set_property_null_action(id, key);
  694. _redo.clear();
  695. _redo_points.clear();
  696. set_property_internal(1, id, key, val);
  697. }
  698. public void set_property_string(Guid id, string key, string val)
  699. {
  700. assert(has_object(id));
  701. assert(is_valid_key(key));
  702. assert(is_valid_value(val));
  703. HashMap<string, Value?> ob = get_data(id);
  704. if (ob.has_key(key) && ob[key] != null)
  705. _undo.write_set_property_string_action(id, key, (string)ob[key]);
  706. else
  707. _undo.write_set_property_null_action(id, key);
  708. _redo.clear();
  709. _redo_points.clear();
  710. set_property_internal(1, id, key, val);
  711. }
  712. public void set_property_guid(Guid id, string key, Guid val)
  713. {
  714. assert(has_object(id));
  715. assert(is_valid_key(key));
  716. assert(is_valid_value(val));
  717. HashMap<string, Value?> ob = get_data(id);
  718. if (ob.has_key(key) && ob[key] != null)
  719. _undo.write_set_property_guid_action(id, key, (Guid)ob[key]);
  720. else
  721. _undo.write_set_property_null_action(id, key);
  722. _redo.clear();
  723. _redo_points.clear();
  724. set_property_internal(1, id, key, val);
  725. }
  726. public void set_property_vector3(Guid id, string key, Vector3 val)
  727. {
  728. assert(has_object(id));
  729. assert(is_valid_key(key));
  730. assert(is_valid_value(val));
  731. HashMap<string, Value?> ob = get_data(id);
  732. if (ob.has_key(key) && ob[key] != null)
  733. _undo.write_set_property_vector3_action(id, key, (Vector3)ob[key]);
  734. else
  735. _undo.write_set_property_null_action(id, key);
  736. _redo.clear();
  737. _redo_points.clear();
  738. set_property_internal(1, id, key, val);
  739. }
  740. public void set_property_quaternion(Guid id, string key, Quaternion val)
  741. {
  742. assert(has_object(id));
  743. assert(is_valid_key(key));
  744. assert(is_valid_value(val));
  745. HashMap<string, Value?> ob = get_data(id);
  746. if (ob.has_key(key) && ob[key] != null)
  747. _undo.write_set_property_quaternion_action(id, key, (Quaternion)ob[key]);
  748. else
  749. _undo.write_set_property_null_action(id, key);
  750. _redo.clear();
  751. _redo_points.clear();
  752. set_property_internal(1, id, key, val);
  753. }
  754. public void add_to_set(Guid id, string key, Guid item_id)
  755. {
  756. assert(has_object(id));
  757. assert(is_valid_key(key));
  758. assert(item_id != GUID_ZERO);
  759. assert(has_object(item_id));
  760. _undo.write_remove_from_set_action(id, key, item_id);
  761. _redo.clear();
  762. _redo_points.clear();
  763. add_to_set_internal(1, id, key, item_id);
  764. }
  765. public void remove_from_set(Guid id, string key, Guid item_id)
  766. {
  767. assert(has_object(id));
  768. assert(is_valid_key(key));
  769. assert(item_id != GUID_ZERO);
  770. _undo.write_add_to_set_action(id, key, item_id);
  771. _redo.clear();
  772. _redo_points.clear();
  773. remove_from_set_internal(1, id, key, item_id);
  774. }
  775. public bool has_object(Guid id)
  776. {
  777. return id == GUID_ZERO || _data.has_key(id);
  778. }
  779. public bool has_property(Guid id, string key)
  780. {
  781. return get_property(id, key) != null;
  782. }
  783. public Value? get_property(Guid id, string key)
  784. {
  785. assert(has_object(id));
  786. assert(is_valid_key(key));
  787. HashMap<string, Value?> ob = get_data(id);
  788. Value? value = (ob.has_key(key) ? ob[key] : null);
  789. if (_debug_getters)
  790. logi("get_property %s %s %s".printf(id.to_string(), key, (value == null) ? "null" : value_to_string(value)));
  791. return value;
  792. }
  793. public bool get_property_bool(Guid id, string key)
  794. {
  795. return (bool)get_property(id, key);
  796. }
  797. public double get_property_double(Guid id, string key)
  798. {
  799. return (double)get_property(id, key);
  800. }
  801. public string get_property_string(Guid id, string key)
  802. {
  803. return (string)get_property(id, key);
  804. }
  805. public Guid get_property_guid(Guid id, string key)
  806. {
  807. return (Guid)get_property(id, key);
  808. }
  809. public Vector3 get_property_vector3(Guid id, string key)
  810. {
  811. return (Vector3)get_property(id, key);
  812. }
  813. public Quaternion get_property_quaternion(Guid id, string key)
  814. {
  815. return (Quaternion)get_property(id, key);
  816. }
  817. public HashSet<Guid?> get_property_set(Guid id, string key, HashSet<Guid?> deffault)
  818. {
  819. assert(has_object(id));
  820. assert(is_valid_key(key));
  821. HashMap<string, Value?> ob = get_data(id);
  822. HashSet<Guid?> value;
  823. if (ob.has_key(key))
  824. value = ob[key] as HashSet<Guid?>;
  825. else
  826. value = deffault;
  827. if (_debug_getters)
  828. logi("get_property %s %s %s".printf(id.to_string(), key, (value == null) ? "null" : value_to_string(value)));
  829. return value;
  830. }
  831. public HashMap<string, Value?> get_object(Guid id)
  832. {
  833. return (HashMap<string, Value?>)get_data(GUID_ZERO)[id.to_string()];
  834. }
  835. public string[] get_keys(Guid id)
  836. {
  837. HashMap<string, Value?> data = get_data(id);
  838. return data.keys.to_array();
  839. }
  840. public void add_restore_point(int id, Guid[] data)
  841. {
  842. if (_debug)
  843. logi("add_restore_point %d, undo size = %u".printf(id, _undo.size()));
  844. _undo_points.write_restore_point(id, _undo.size(), data);
  845. _redo.clear();
  846. _redo_points.clear();
  847. }
  848. /// Duplicates the object specified by id and assign new_id to the duplicated object.
  849. public void duplicate(Guid id, Guid new_id)
  850. {
  851. assert(id != GUID_ZERO);
  852. assert(new_id != GUID_ZERO);
  853. assert(id != new_id);
  854. assert(has_object(id));
  855. create(new_id, object_type(id));
  856. HashMap<string, Value?> ob = get_data(id);
  857. string[] keys = ob.keys.to_array();
  858. foreach (string key in keys) {
  859. Value? val = ob[key];
  860. if (val.holds(typeof(HashSet))) {
  861. HashSet<Guid?> hs = (HashSet<Guid?>)val;
  862. foreach (Guid j in hs) {
  863. Guid x = Guid.new_guid();
  864. duplicate(j, x);
  865. add_to_set(new_id, key, x);
  866. }
  867. } else {
  868. if (ob[key] == null)
  869. set_property_null(new_id, key);
  870. if (ob[key].holds(typeof(bool)))
  871. set_property_bool(new_id, key, (bool)ob[key]);
  872. if (ob[key].holds(typeof(double)))
  873. set_property_double(new_id, key, (double)ob[key]);
  874. if (ob[key].holds(typeof(string)))
  875. set_property_string(new_id, key, (string)ob[key]);
  876. if (ob[key].holds(typeof(Guid)))
  877. set_property_guid(new_id, key, (Guid)ob[key]);
  878. if (ob[key].holds(typeof(Vector3)))
  879. set_property_vector3(new_id, key, (Vector3)ob[key]);
  880. if (ob[key].holds(typeof(Quaternion)))
  881. set_property_quaternion(new_id, key, (Quaternion)ob[key]);
  882. }
  883. }
  884. }
  885. /// Copies the database to db under the given new_key.
  886. public void copy_to(Database db, string new_key)
  887. {
  888. assert(db != null);
  889. assert(is_valid_key(new_key));
  890. copy_deep(db, GUID_ZERO, new_key);
  891. }
  892. public void copy_deep(Database db, Guid id, string new_key)
  893. {
  894. HashMap<string, Value?> ob = get_data(id);
  895. string[] keys = ob.keys.to_array();
  896. foreach (string key in keys) {
  897. Value? value = ob[key];
  898. if (value.holds(typeof(HashSet))) {
  899. HashSet<Guid?> hs = (HashSet<Guid?>)value;
  900. foreach (Guid j in hs) {
  901. db.create(j, object_type(j));
  902. copy_deep(db, j, "");
  903. db.add_to_set(id, new_key + (new_key == "" ? "" : ".") + key, j);
  904. }
  905. } else {
  906. string kk = new_key + (new_key == "" ? "" : ".") + key;
  907. if (ob[key] == null)
  908. db.set_property_null(id, kk);
  909. if (ob[key].holds(typeof(bool)))
  910. db.set_property_bool(id, kk, (bool)ob[key]);
  911. if (ob[key].holds(typeof(double)))
  912. db.set_property_double(id, kk, (double)ob[key]);
  913. if (ob[key].holds(typeof(string)))
  914. db.set_property_string(id, kk, (string)ob[key]);
  915. if (ob[key].holds(typeof(Guid)))
  916. db.set_property_guid(id, kk, (Guid)ob[key]);
  917. if (ob[key].holds(typeof(Vector3)))
  918. db.set_property_vector3(id, kk, (Vector3)ob[key]);
  919. if (ob[key].holds(typeof(Quaternion)))
  920. db.set_property_quaternion(id, kk, (Quaternion)ob[key]);
  921. }
  922. }
  923. }
  924. // Un-does the last action and returns its ID, or -1 if there is no
  925. // action to undo.
  926. public int undo()
  927. {
  928. if (_undo_points.size() == 0)
  929. return -1;
  930. RestorePoint rp = _undo_points.read_restore_point();
  931. _redo_points.write_restore_point(rp.id, _redo.size(), rp.data);
  932. undo_until(rp.size);
  933. undo_redo(true, rp.id, rp.data);
  934. return rp.id;
  935. }
  936. // Re-does the last action and returns its ID, or -1 if there is no
  937. // action to redo.
  938. public int redo()
  939. {
  940. if (_redo_points.size() == 0)
  941. return -1;
  942. RestorePoint rp = _redo_points.read_restore_point();
  943. _undo_points.write_restore_point(rp.id, _undo.size(), rp.data);
  944. redo_until(rp.size);
  945. undo_redo(false, rp.id, rp.data);
  946. return rp.id;
  947. }
  948. private void undo_until(uint32 size)
  949. {
  950. undo_redo_until(size, _undo, _redo);
  951. }
  952. private void redo_until(uint32 size)
  953. {
  954. undo_redo_until(size, _redo, _undo);
  955. }
  956. private void undo_redo_until(uint32 size, Stack undo, Stack redo)
  957. {
  958. int dir = undo == _undo ? -1 : 1;
  959. while (undo.size() != size) {
  960. uint32 type = undo.peek_type();
  961. if (type == Action.CREATE) {
  962. Action t = undo.read_action();
  963. assert(t == Action.CREATE);
  964. Guid id = undo.read_guid();
  965. string obj_type = undo.read_string();
  966. redo.write_destroy_action(id, obj_type);
  967. create_internal(dir, id);
  968. set_object_type(id, obj_type);
  969. } else if (type == Action.DESTROY) {
  970. Action t = undo.read_action();
  971. assert(t == Action.DESTROY);
  972. Guid id = undo.read_guid();
  973. string obj_type = undo.read_string();
  974. redo.write_create_action(id, obj_type);
  975. destroy_internal(dir, id);
  976. } else if (type == Action.SET_PROPERTY_NULL) {
  977. Action t = undo.read_action();
  978. assert(t == Action.SET_PROPERTY_NULL);
  979. Guid id = undo.read_guid();
  980. string key = undo.read_string();
  981. if (has_property(id, key)) {
  982. if (get_data(id)[key].holds(typeof(bool)))
  983. redo.write_set_property_bool_action(id, key, get_property_bool(id, key));
  984. if (get_data(id)[key].holds(typeof(double)))
  985. redo.write_set_property_double_action(id, key, get_property_double(id, key));
  986. if (get_data(id)[key].holds(typeof(string)))
  987. redo.write_set_property_string_action(id, key, get_property_string(id, key));
  988. if (get_data(id)[key].holds(typeof(Guid)))
  989. redo.write_set_property_guid_action(id, key, get_property_guid(id, key));
  990. if (get_data(id)[key].holds(typeof(Vector3)))
  991. redo.write_set_property_vector3_action(id, key, get_property_vector3(id, key));
  992. if (get_data(id)[key].holds(typeof(Quaternion)))
  993. redo.write_set_property_quaternion_action(id, key, get_property_quaternion(id, key));
  994. } else {
  995. redo.write_set_property_null_action(id, key);
  996. }
  997. set_property_internal(dir, id, key, null);
  998. } else if (type == Action.SET_PROPERTY_BOOL) {
  999. Action t = undo.read_action();
  1000. assert(t == Action.SET_PROPERTY_BOOL);
  1001. Guid id = undo.read_guid();
  1002. string key = undo.read_string();
  1003. bool val = undo.read_bool();
  1004. if (has_property(id, key))
  1005. redo.write_set_property_bool_action(id, key, get_property_bool(id, key));
  1006. else
  1007. redo.write_set_property_null_action(id, key);
  1008. set_property_internal(dir, id, key, val);
  1009. } else if (type == Action.SET_PROPERTY_DOUBLE) {
  1010. Action t = undo.read_action();
  1011. assert(t == Action.SET_PROPERTY_DOUBLE);
  1012. Guid id = undo.read_guid();
  1013. string key = undo.read_string();
  1014. double val = undo.read_double();
  1015. if (has_property(id, key))
  1016. redo.write_set_property_double_action(id, key, get_property_double(id, key));
  1017. else
  1018. redo.write_set_property_null_action(id, key);
  1019. set_property_internal(dir, id, key, val);
  1020. } else if (type == Action.SET_PROPERTY_STRING) {
  1021. Action t = undo.read_action();
  1022. assert(t == Action.SET_PROPERTY_STRING);
  1023. Guid id = undo.read_guid();
  1024. string key = undo.read_string();
  1025. string val = undo.read_string();
  1026. if (has_property(id, key))
  1027. redo.write_set_property_string_action(id, key, get_property_string(id, key));
  1028. else
  1029. redo.write_set_property_null_action(id, key);
  1030. set_property_internal(dir, id, key, val);
  1031. } else if (type == Action.SET_PROPERTY_GUID) {
  1032. Action t = undo.read_action();
  1033. assert(t == Action.SET_PROPERTY_GUID);
  1034. Guid id = undo.read_guid();
  1035. string key = undo.read_string();
  1036. Guid val = undo.read_guid();
  1037. if (has_property(id, key))
  1038. redo.write_set_property_guid_action(id, key, get_property_guid(id, key));
  1039. else
  1040. redo.write_set_property_null_action(id, key);
  1041. set_property_internal(dir, id, key, val);
  1042. } else if (type == Action.SET_PROPERTY_VECTOR3) {
  1043. Action t = undo.read_action();
  1044. assert(t == Action.SET_PROPERTY_VECTOR3);
  1045. Guid id = undo.read_guid();
  1046. string key = undo.read_string();
  1047. Vector3 val = undo.read_vector3();
  1048. if (has_property(id, key))
  1049. redo.write_set_property_vector3_action(id, key, get_property_vector3(id, key));
  1050. else
  1051. redo.write_set_property_null_action(id, key);
  1052. set_property_internal(dir, id, key, val);
  1053. } else if (type == Action.SET_PROPERTY_QUATERNION) {
  1054. Action t = undo.read_action();
  1055. assert(t == Action.SET_PROPERTY_QUATERNION);
  1056. Guid id = undo.read_guid();
  1057. string key = undo.read_string();
  1058. Quaternion val = undo.read_quaternion();
  1059. if (has_property(id, key))
  1060. redo.write_set_property_quaternion_action(id, key, get_property_quaternion(id, key));
  1061. else
  1062. redo.write_set_property_null_action(id, key);
  1063. set_property_internal(dir, id, key, val);
  1064. } else if (type == Action.ADD_TO_SET) {
  1065. Action t = undo.read_action();
  1066. assert(t == Action.ADD_TO_SET);
  1067. Guid id = undo.read_guid();
  1068. string key = undo.read_string();
  1069. Guid item_id = undo.read_guid();
  1070. redo.write_remove_from_set_action(id, key, item_id);
  1071. add_to_set_internal(dir, id, key, item_id);
  1072. } else if (type == Action.REMOVE_FROM_SET) {
  1073. Action t = undo.read_action();
  1074. assert(t == Action.REMOVE_FROM_SET);
  1075. Guid id = undo.read_guid();
  1076. string key = undo.read_string();
  1077. Guid item_id = undo.read_guid();
  1078. redo.write_add_to_set_action(id, key, item_id);
  1079. remove_from_set_internal(dir, id, key, item_id);
  1080. }
  1081. }
  1082. }
  1083. }
  1084. } /* namespace Crown */