database.vala 31 KB

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