database.vala 39 KB

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