database.vala 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441
  1. /*
  2. * Copyright (c) 2012-2022 Daniele Bartolini et al.
  3. * License: https://github.com/crownengine/crown/blob/master/LICENSE
  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[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. // The number of changes to the database since the last successful state
  337. // synchronization (load(), save() etc.). If it is less than 0, the changes
  338. // came from undo(), otherwise they came from redo() or from regular calls to
  339. // create(), destroy(), set_*() etc. A value of 0 means there were no changes.
  340. public int _distance_from_last_sync;
  341. // Signals
  342. public signal void key_changed(Guid id, string key);
  343. public signal void object_created(Guid id);
  344. public signal void object_destroyed(Guid id);
  345. public signal void undo_redo(bool undo, uint32 id, Guid[] data);
  346. public Database(UndoRedo? undo_redo = null)
  347. {
  348. _data = new HashMap<Guid?, HashMap<string, Value?>>(Guid.hash_func, Guid.equal_func);
  349. _undo_redo = undo_redo;
  350. reset();
  351. }
  352. /// Resets database to clean state.
  353. public void reset()
  354. {
  355. _data.clear();
  356. if (_undo_redo != null)
  357. _undo_redo.reset();
  358. _distance_from_last_sync = 0;
  359. // This is a special field which stores all objects
  360. _data[GUID_ZERO] = new HashMap<string, Value?>();
  361. }
  362. /// Returns whether the database has been changed since last call to Save().
  363. public bool changed()
  364. {
  365. return _distance_from_last_sync != 0;
  366. }
  367. /// Saves database to path without marking it as not changed.
  368. public void dump(string path, Guid id)
  369. {
  370. Hashtable json = encode(id);
  371. SJSON.save(json, path);
  372. }
  373. /// Saves database to path.
  374. public void save(string path, Guid id)
  375. {
  376. dump(path, id);
  377. _distance_from_last_sync = 0;
  378. }
  379. // See: load_more_from_path().
  380. public int load_more_from_file(out Guid object_id, FileStream? fs, string resource_path)
  381. {
  382. Hashtable json = SJSON.load_from_file(fs);
  383. // Parse the object's ID or generate a new one if none is found.
  384. if (json.has_key("id"))
  385. object_id = Guid.parse((string)json["id"]);
  386. else if (json.has_key("_guid"))
  387. object_id = Guid.parse((string)json["_guid"]);
  388. else
  389. object_id = Guid.new_guid();
  390. create_internal(0, object_id);
  391. set_object_type(object_id, resource_type(resource_path));
  392. decode_object(object_id, GUID_ZERO, "", json);
  393. // Create a mapping between the path and the object it has been loaded into.
  394. set_property_internal(0, GUID_ZERO, resource_path, object_id);
  395. return 0;
  396. }
  397. // Loads the database with the object stored at @a path, without resetting the
  398. // database. This makes it possible to load multiple objects from distinct
  399. // paths in the same database. @a resource_path is used as a key in the
  400. // database to refer to the object that has been loaded. This is useful when
  401. // you do not have the object ID but only its path, as it is often the case
  402. // since resources use paths and not IDs to reference each other.
  403. public int load_more_from_path(out Guid object_id, string path, string resource_path)
  404. {
  405. object_id = GUID_ZERO;
  406. FileStream fs = FileStream.open(path, "rb");
  407. if (fs == null)
  408. return 1;
  409. return load_more_from_file(out object_id, fs, resource_path);
  410. }
  411. /// Loads the database with the object stored at @a path.
  412. public int load_from_file(out Guid object_id, FileStream fs, string resource_path)
  413. {
  414. reset();
  415. return load_more_from_file(out object_id, fs, resource_path);
  416. }
  417. /// Loads the database with the object stored at @a path.
  418. public int load_from_path(out Guid object_id, string path, string resource_path)
  419. {
  420. reset();
  421. return load_more_from_path(out object_id, path, resource_path);
  422. }
  423. /// Encodes the object @a id into SJSON object.
  424. private Hashtable encode(Guid id)
  425. {
  426. return encode_object(id, get_data(id));
  427. }
  428. private static bool is_valid_value(Value? value)
  429. {
  430. return value == null
  431. || value.holds(typeof(bool))
  432. || value.holds(typeof(double))
  433. || value.holds(typeof(string))
  434. || value.holds(typeof(Guid))
  435. || value.holds(typeof(Vector3))
  436. || value.holds(typeof(Quaternion))
  437. ;
  438. }
  439. private static bool is_valid_key(string key)
  440. {
  441. return key.length > 0
  442. && !key.has_prefix(".")
  443. && !key.has_suffix(".")
  444. ;
  445. }
  446. private static string value_to_string(Value? value)
  447. {
  448. if (value == null)
  449. return "null";
  450. if (value.holds(typeof(bool)))
  451. return ((bool)value).to_string();
  452. if (value.holds(typeof(double)))
  453. return ((double)value).to_string();
  454. if (value.holds(typeof(string)))
  455. return ((string)value).to_string();
  456. if (value.holds(typeof(Guid)))
  457. return ((Guid)value).to_string();
  458. if (value.holds(typeof(Vector3)))
  459. return ((Vector3)value).to_string();
  460. if (value.holds(typeof(Quaternion)))
  461. return ((Quaternion)value).to_string();
  462. if (value.holds(typeof(HashSet)))
  463. return "Set<Guid>";
  464. return "<invalid>";
  465. }
  466. private void decode_object(Guid id, Guid owner_id, string db_key, Hashtable json)
  467. {
  468. string old_db = db_key;
  469. string k = db_key;
  470. string[] keys = json.keys.to_array();
  471. foreach (string key in keys) {
  472. // ID is filled by decode_set().
  473. if (key == "id" || key == "_guid")
  474. continue;
  475. // The "type" key defines object type only if it appears
  476. // in the root of a JSON object (k == "").
  477. if (k == "") {
  478. if (key == "type" || key == "_type")
  479. set_object_type(id, (string)json[key]);
  480. }
  481. Value? val = json[key];
  482. k += k == "" ? key : ("." + key);
  483. if (val.holds(typeof(Hashtable))) {
  484. Hashtable ht = (Hashtable)val;
  485. decode_object(id, owner_id, k, ht);
  486. } else if (val.holds(typeof(ArrayList))) {
  487. ArrayList<Value?> arr = (ArrayList<Value?>)val;
  488. if (arr.size > 0 && arr[0].holds(typeof(double)))
  489. set_property_internal(0, id, k, decode_value(val));
  490. else
  491. decode_set(id, key, arr);
  492. } else {
  493. set_property_internal(0, id, k, decode_value(val));
  494. }
  495. k = old_db;
  496. }
  497. }
  498. private void decode_set(Guid owner_id, string key, ArrayList<Value?> json)
  499. {
  500. // Set should be created even if it is empty.
  501. create_empty_set(0, owner_id, key);
  502. for (int i = 0; i < json.size; ++i) {
  503. Hashtable obj = (Hashtable)json[i];
  504. // Decode object ID.
  505. Guid obj_id;
  506. if (obj.has_key("id"))
  507. obj_id = Guid.parse((string)obj["id"]);
  508. else if (obj.has_key("_guid"))
  509. obj_id = Guid.parse((string)obj["_guid"]);
  510. else
  511. obj_id = Guid.new_guid();
  512. create_internal(0, obj_id);
  513. // Determine the object's type based on the type of its
  514. // parent and other heuristics.
  515. string owner_type = object_type(owner_id);
  516. if (owner_type == "level") {
  517. if (key == "units")
  518. set_object_type(obj_id, "unit");
  519. else if (key == "sounds")
  520. set_object_type(obj_id, "level_sound");
  521. else
  522. set_object_type(obj_id, "undefined");
  523. } else if (owner_type == "state_machine") {
  524. if (key == "states")
  525. set_object_type(obj_id, "state_machine_node");
  526. else if (key == "variables")
  527. set_object_type(obj_id, "state_machine_variable");
  528. else
  529. set_object_type(obj_id, "undefined");
  530. } else if (owner_type == "state_machine_node") {
  531. if (key == "animations")
  532. set_object_type(obj_id, "node_animation");
  533. else if (key == "transitions")
  534. set_object_type(obj_id, "node_transition");
  535. } else if (owner_type == "sprite") {
  536. if (key == "frames") {
  537. set_object_type(obj_id, "sprite_frame");
  538. set_property_internal(0, obj_id, "index", (double)i);
  539. }
  540. }
  541. decode_object(obj_id, owner_id, "", obj);
  542. assert(has_property(obj_id, "_type"));
  543. add_to_set_internal(0, owner_id, key, obj_id);
  544. }
  545. }
  546. private Value? decode_value(Value? value)
  547. {
  548. if (value.holds(typeof(ArrayList))) {
  549. ArrayList<Value?> al = (ArrayList<Value?>)value;
  550. if (al.size == 1)
  551. return Vector3((double)al[0], 0.0, 0.0);
  552. else if (al.size == 2)
  553. return Vector3((double)al[0], (double)al[1], 0.0);
  554. else if (al.size == 3)
  555. return Vector3((double)al[0], (double)al[1], (double)al[2]);
  556. else if (al.size == 4)
  557. return Quaternion((double)al[0], (double)al[1], (double)al[2], (double)al[3]);
  558. else
  559. return Vector3(0.0, 0.0, 0.0);
  560. } else if (value.holds(typeof(string))) {
  561. Guid id;
  562. if (Guid.try_parse((string)value, out id))
  563. return id;
  564. return value;
  565. } else if (value == null
  566. || value.holds(typeof(bool))
  567. || value.holds(typeof(double))) {
  568. return value;
  569. } else {
  570. return null;
  571. }
  572. }
  573. private Hashtable encode_object(Guid id, HashMap<string, Value?> db)
  574. {
  575. Hashtable obj = new Hashtable();
  576. if (id != GUID_ZERO)
  577. obj["_guid"] = id.to_string();
  578. string[] keys = db.keys.to_array();
  579. foreach (string key in keys) {
  580. // Since null-key is equivalent to non-existent key, skip serialization.
  581. if (db[key] == null)
  582. continue;
  583. string[] foo = key.split(".");
  584. Hashtable x = obj;
  585. if (foo.length > 1) {
  586. for (int i = 0; i < foo.length - 1; ++i) {
  587. string f = foo[i];
  588. if (x.has_key(f)) {
  589. x = (Hashtable)x[f];
  590. continue;
  591. }
  592. Hashtable y = new Hashtable();
  593. x.set(f, y);
  594. x = y;
  595. }
  596. }
  597. x.set(foo[foo.length - 1], encode_value(db[key]));
  598. }
  599. return obj;
  600. }
  601. private Value? encode_value(Value? value)
  602. {
  603. assert(is_valid_value(value) || value.holds(typeof(HashSet)));
  604. if (value.holds(typeof(Vector3))) {
  605. Vector3 v = (Vector3)value;
  606. ArrayList<Value?> arr = new Gee.ArrayList<Value?>();
  607. arr.add(v.x);
  608. arr.add(v.y);
  609. arr.add(v.z);
  610. return arr;
  611. } else if (value.holds(typeof(Quaternion))) {
  612. Quaternion q = (Quaternion)value;
  613. ArrayList<Value?> arr = new Gee.ArrayList<Value?>();
  614. arr.add(q.x);
  615. arr.add(q.y);
  616. arr.add(q.z);
  617. arr.add(q.w);
  618. return arr;
  619. } else if (value.holds(typeof(Guid))) {
  620. Guid id = (Guid)value;
  621. return id.to_string();
  622. } else if (value.holds(typeof(HashSet))) {
  623. HashSet<Guid?> hs = (HashSet<Guid?>)value;
  624. ArrayList<Value?> arr = new Gee.ArrayList<Value?>();
  625. foreach (Guid id in hs) {
  626. arr.add(encode_object(id, get_data(id)));
  627. }
  628. return arr;
  629. } else {
  630. return value;
  631. }
  632. }
  633. private HashMap<string, Value?> get_data(Guid id)
  634. {
  635. assert(has_object(id));
  636. return _data[id];
  637. }
  638. private void create_internal(int dir, Guid id)
  639. {
  640. assert(id != GUID_ZERO);
  641. if (_debug)
  642. logi("create %s".printf(id.to_string()));
  643. _data[id] = new HashMap<string, Value?>();
  644. _distance_from_last_sync += dir;
  645. object_created(id);
  646. }
  647. private void destroy_internal(int dir, Guid id)
  648. {
  649. assert(id != GUID_ZERO);
  650. assert(has_object(id));
  651. if (_debug)
  652. logi("destroy %s".printf(id.to_string()));
  653. object_destroyed(id);
  654. _distance_from_last_sync += dir;
  655. _data.unset(id);
  656. }
  657. private void set_property_internal(int dir, Guid id, string key, Value? value)
  658. {
  659. assert(has_object(id));
  660. assert(is_valid_key(key));
  661. assert(is_valid_value(value));
  662. if (_debug)
  663. logi("set_property %s %s %s".printf(id.to_string(), key, value_to_string(value)));
  664. HashMap<string, Value?> ob = get_data(id);
  665. ob[key] = value;
  666. _distance_from_last_sync += dir;
  667. key_changed(id, key);
  668. }
  669. private void create_empty_set(int dir, Guid id, string key)
  670. {
  671. assert(has_object(id));
  672. assert(is_valid_key(key));
  673. HashMap<string, Value?> ob = get_data(id);
  674. assert(!ob.has_key(key));
  675. ob[key] = new HashSet<Guid?>(Guid.hash_func, Guid.equal_func);
  676. }
  677. private void add_to_set_internal(int dir, Guid id, string key, Guid item_id)
  678. {
  679. assert(has_object(id));
  680. assert(is_valid_key(key));
  681. assert(item_id != GUID_ZERO);
  682. assert(has_object(item_id));
  683. if (_debug)
  684. logi("add_to_set %s %s %s".printf(id.to_string(), key, item_id.to_string()));
  685. HashMap<string, Value?> ob = get_data(id);
  686. if (!ob.has_key(key)) {
  687. HashSet<Guid?> hs = new HashSet<Guid?>(Guid.hash_func, Guid.equal_func);
  688. hs.add(item_id);
  689. ob[key] = hs;
  690. } else {
  691. ((HashSet<Guid?>)ob[key]).add(item_id);
  692. }
  693. _distance_from_last_sync += dir;
  694. key_changed(id, key);
  695. }
  696. private void remove_from_set_internal(int dir, Guid id, string key, Guid item_id)
  697. {
  698. assert(has_object(id));
  699. assert(is_valid_key(key));
  700. assert(item_id != GUID_ZERO);
  701. if (_debug)
  702. logi("remove_from_set %s %s %s".printf(id.to_string(), key, item_id.to_string()));
  703. HashMap<string, Value?> ob = get_data(id);
  704. ((HashSet<Guid?>)ob[key]).remove(item_id);
  705. _distance_from_last_sync += dir;
  706. key_changed(id, key);
  707. }
  708. // Returns the type of the object @a id.
  709. public string object_type(Guid id)
  710. {
  711. assert(has_object(id));
  712. return (string)get_data(id)["_type"];
  713. }
  714. // Sets the @a type of the object @a id.
  715. // This is called automatically when loading data or when new objects are created via create().
  716. // It can occasionally be called manually after loading legacy data with no type information
  717. // stored inside objects.
  718. public void set_object_type(Guid id, string type)
  719. {
  720. assert(has_object(id));
  721. get_data(id)["_type"] = type;
  722. }
  723. public void create(Guid id, string type)
  724. {
  725. assert(id != GUID_ZERO);
  726. assert(!has_object(id));
  727. if (_undo_redo != null) {
  728. _undo_redo._undo.write_destroy_action(Action.DESTROY, id, type);
  729. _undo_redo._redo.clear();
  730. }
  731. create_internal(1, id);
  732. set_object_type(id, type);
  733. object_created(id);
  734. }
  735. public void destroy(Guid id)
  736. {
  737. assert(id != GUID_ZERO);
  738. assert(has_object(id));
  739. string obj_type = object_type(id);
  740. HashMap<string, Value?> o = get_data(id);
  741. string[] keys = o.keys.to_array();
  742. foreach (string key in keys) {
  743. Value? value = o[key];
  744. if (value.holds(typeof(HashSet))) {
  745. HashSet<Guid?> hs = (HashSet<Guid?>)value;
  746. Guid?[] ids = hs.to_array();
  747. foreach (Guid item_id in ids) {
  748. remove_from_set(id, key, item_id);
  749. destroy(item_id);
  750. }
  751. } else {
  752. set_property_null(id, key);
  753. }
  754. }
  755. if (_undo_redo != null) {
  756. _undo_redo._undo.write_create_action(Action.CREATE, id, obj_type);
  757. _undo_redo._redo.clear();
  758. }
  759. destroy_internal(1, id);
  760. }
  761. public void set_property_null(Guid id, string key)
  762. {
  763. assert(has_object(id));
  764. assert(is_valid_key(key));
  765. assert(is_valid_value(null));
  766. if (_undo_redo != null) {
  767. HashMap<string, Value?> ob = get_data(id);
  768. if (ob.has_key(key) && ob[key] != null) {
  769. if (ob[key].holds(typeof(bool)))
  770. _undo_redo._undo.write_set_property_bool_action(Action.SET_PROPERTY_BOOL, id, key, (bool)ob[key]);
  771. if (ob[key].holds(typeof(double)))
  772. _undo_redo._undo.write_set_property_double_action(Action.SET_PROPERTY_DOUBLE, id, key, (double)ob[key]);
  773. if (ob[key].holds(typeof(string)))
  774. _undo_redo._undo.write_set_property_string_action(Action.SET_PROPERTY_STRING, id, key, (string)ob[key]);
  775. if (ob[key].holds(typeof(Guid)))
  776. _undo_redo._undo.write_set_property_guid_action(Action.SET_PROPERTY_GUID, id, key, (Guid)ob[key]);
  777. if (ob[key].holds(typeof(Vector3)))
  778. _undo_redo._undo.write_set_property_vector3_action(Action.SET_PROPERTY_VECTOR3, id, key, (Vector3)ob[key]);
  779. if (ob[key].holds(typeof(Quaternion)))
  780. _undo_redo._undo.write_set_property_quaternion_action(Action.SET_PROPERTY_QUATERNION, id, key, (Quaternion)ob[key]);
  781. } else {
  782. _undo_redo._undo.write_set_property_null_action(Action.SET_PROPERTY_NULL, id, key);
  783. }
  784. _undo_redo._redo.clear();
  785. }
  786. set_property_internal(1, id, key, null);
  787. }
  788. public void set_property_bool(Guid id, string key, bool val)
  789. {
  790. assert(has_object(id));
  791. assert(is_valid_key(key));
  792. assert(is_valid_value(val));
  793. if (_undo_redo != null) {
  794. HashMap<string, Value?> ob = get_data(id);
  795. if (ob.has_key(key) && ob[key] != null)
  796. _undo_redo._undo.write_set_property_bool_action(Action.SET_PROPERTY_BOOL, id, key, (bool)ob[key]);
  797. else
  798. _undo_redo._undo.write_set_property_null_action(Action.SET_PROPERTY_NULL, id, key);
  799. _undo_redo._redo.clear();
  800. }
  801. set_property_internal(1, id, key, val);
  802. }
  803. public void set_property_double(Guid id, string key, double val)
  804. {
  805. assert(has_object(id));
  806. assert(is_valid_key(key));
  807. assert(is_valid_value(val));
  808. if (_undo_redo != null) {
  809. HashMap<string, Value?> ob = get_data(id);
  810. if (ob.has_key(key) && ob[key] != null)
  811. _undo_redo._undo.write_set_property_double_action(Action.SET_PROPERTY_DOUBLE, id, key, (double)ob[key]);
  812. else
  813. _undo_redo._undo.write_set_property_null_action(Action.SET_PROPERTY_NULL, id, key);
  814. _undo_redo._redo.clear();
  815. }
  816. set_property_internal(1, id, key, val);
  817. }
  818. public void set_property_string(Guid id, string key, string val)
  819. {
  820. assert(has_object(id));
  821. assert(is_valid_key(key));
  822. assert(is_valid_value(val));
  823. if (_undo_redo != null) {
  824. HashMap<string, Value?> ob = get_data(id);
  825. if (ob.has_key(key) && ob[key] != null)
  826. _undo_redo._undo.write_set_property_string_action(Action.SET_PROPERTY_STRING, id, key, (string)ob[key]);
  827. else
  828. _undo_redo._undo.write_set_property_null_action(Action.SET_PROPERTY_NULL, id, key);
  829. _undo_redo._redo.clear();
  830. }
  831. set_property_internal(1, id, key, val);
  832. }
  833. public void set_property_guid(Guid id, string key, Guid val)
  834. {
  835. assert(has_object(id));
  836. assert(is_valid_key(key));
  837. assert(is_valid_value(val));
  838. if (_undo_redo != null) {
  839. HashMap<string, Value?> ob = get_data(id);
  840. if (ob.has_key(key) && ob[key] != null)
  841. _undo_redo._undo.write_set_property_guid_action(Action.SET_PROPERTY_GUID, id, key, (Guid)ob[key]);
  842. else
  843. _undo_redo._undo.write_set_property_null_action(Action.SET_PROPERTY_NULL, id, key);
  844. _undo_redo._redo.clear();
  845. }
  846. set_property_internal(1, id, key, val);
  847. }
  848. public void set_property_vector3(Guid id, string key, Vector3 val)
  849. {
  850. assert(has_object(id));
  851. assert(is_valid_key(key));
  852. assert(is_valid_value(val));
  853. if (_undo_redo != null) {
  854. HashMap<string, Value?> ob = get_data(id);
  855. if (ob.has_key(key) && ob[key] != null)
  856. _undo_redo._undo.write_set_property_vector3_action(Action.SET_PROPERTY_VECTOR3, id, key, (Vector3)ob[key]);
  857. else
  858. _undo_redo._undo.write_set_property_null_action(Action.SET_PROPERTY_NULL, id, key);
  859. _undo_redo._redo.clear();
  860. }
  861. set_property_internal(1, id, key, val);
  862. }
  863. public void set_property_quaternion(Guid id, string key, Quaternion val)
  864. {
  865. assert(has_object(id));
  866. assert(is_valid_key(key));
  867. assert(is_valid_value(val));
  868. if (_undo_redo != null) {
  869. HashMap<string, Value?> ob = get_data(id);
  870. if (ob.has_key(key) && ob[key] != null)
  871. _undo_redo._undo.write_set_property_quaternion_action(Action.SET_PROPERTY_QUATERNION, id, key, (Quaternion)ob[key]);
  872. else
  873. _undo_redo._undo.write_set_property_null_action(Action.SET_PROPERTY_NULL, id, key);
  874. _undo_redo._redo.clear();
  875. }
  876. set_property_internal(1, id, key, val);
  877. }
  878. public void add_to_set(Guid id, string key, Guid item_id)
  879. {
  880. assert(has_object(id));
  881. assert(is_valid_key(key));
  882. assert(item_id != GUID_ZERO);
  883. assert(has_object(item_id));
  884. if (_undo_redo != null) {
  885. _undo_redo._undo.write_remove_from_set_action(Action.REMOVE_FROM_SET, id, key, item_id);
  886. _undo_redo._redo.clear();
  887. }
  888. add_to_set_internal(1, id, key, item_id);
  889. }
  890. public void remove_from_set(Guid id, string key, Guid item_id)
  891. {
  892. assert(has_object(id));
  893. assert(is_valid_key(key));
  894. assert(item_id != GUID_ZERO);
  895. if (_undo_redo != null) {
  896. _undo_redo._undo.write_add_to_set_action(Action.ADD_TO_SET, id, key, item_id);
  897. _undo_redo._redo.clear();
  898. }
  899. remove_from_set_internal(1, id, key, item_id);
  900. }
  901. public bool has_object(Guid id)
  902. {
  903. return id == GUID_ZERO || _data.has_key(id);
  904. }
  905. public bool has_property(Guid id, string key)
  906. {
  907. return get_property(id, key) != null;
  908. }
  909. public Value? get_property(Guid id, string key)
  910. {
  911. assert(has_object(id));
  912. assert(is_valid_key(key));
  913. HashMap<string, Value?> ob = get_data(id);
  914. Value? value = (ob.has_key(key) ? ob[key] : null);
  915. if (_debug_getters)
  916. logi("get_property %s %s %s".printf(id.to_string(), key, value_to_string(value)));
  917. return value;
  918. }
  919. public bool get_property_bool(Guid id, string key)
  920. {
  921. return (bool)get_property(id, key);
  922. }
  923. public double get_property_double(Guid id, string key)
  924. {
  925. return (double)get_property(id, key);
  926. }
  927. public string get_property_string(Guid id, string key)
  928. {
  929. return (string)get_property(id, key);
  930. }
  931. public Guid get_property_guid(Guid id, string key)
  932. {
  933. return (Guid)get_property(id, key);
  934. }
  935. public Vector3 get_property_vector3(Guid id, string key)
  936. {
  937. return (Vector3)get_property(id, key);
  938. }
  939. public Quaternion get_property_quaternion(Guid id, string key)
  940. {
  941. return (Quaternion)get_property(id, key);
  942. }
  943. public HashSet<Guid?> get_property_set(Guid id, string key, HashSet<Guid?> deffault)
  944. {
  945. assert(has_object(id));
  946. assert(is_valid_key(key));
  947. HashMap<string, Value?> ob = get_data(id);
  948. HashSet<Guid?> value;
  949. if (ob.has_key(key))
  950. value = ob[key] as HashSet<Guid?>;
  951. else
  952. value = deffault;
  953. if (_debug_getters)
  954. logi("get_property %s %s %s".printf(id.to_string(), key, value_to_string(value)));
  955. return value;
  956. }
  957. public HashMap<string, Value?> get_object(Guid id)
  958. {
  959. return (HashMap<string, Value?>)get_data(GUID_ZERO)[id.to_string()];
  960. }
  961. public string[] get_keys(Guid id)
  962. {
  963. HashMap<string, Value?> data = get_data(id);
  964. return data.keys.to_array();
  965. }
  966. public void add_restore_point(int id, Guid[] data)
  967. {
  968. if (_debug)
  969. logi("add_restore_point %d, undo size = %u".printf(id, _undo_redo._undo.size()));
  970. if (_undo_redo != null) {
  971. _undo_redo._undo.write_restore_point(id, data);
  972. _undo_redo._redo.clear();
  973. }
  974. }
  975. /// Duplicates the object specified by id and assign new_id to the duplicated object.
  976. public void duplicate(Guid id, Guid new_id)
  977. {
  978. assert(id != GUID_ZERO);
  979. assert(new_id != GUID_ZERO);
  980. assert(id != new_id);
  981. assert(has_object(id));
  982. create(new_id, object_type(id));
  983. HashMap<string, Value?> ob = get_data(id);
  984. string[] keys = ob.keys.to_array();
  985. foreach (string key in keys) {
  986. Value? val = ob[key];
  987. if (val.holds(typeof(HashSet))) {
  988. HashSet<Guid?> hs = (HashSet<Guid?>)val;
  989. foreach (Guid j in hs) {
  990. Guid x = Guid.new_guid();
  991. duplicate(j, x);
  992. add_to_set(new_id, key, x);
  993. }
  994. } else {
  995. if (ob[key] == null)
  996. set_property_null(new_id, key);
  997. if (ob[key].holds(typeof(bool)))
  998. set_property_bool(new_id, key, (bool)ob[key]);
  999. if (ob[key].holds(typeof(double)))
  1000. set_property_double(new_id, key, (double)ob[key]);
  1001. if (ob[key].holds(typeof(string)))
  1002. set_property_string(new_id, key, (string)ob[key]);
  1003. if (ob[key].holds(typeof(Guid)))
  1004. set_property_guid(new_id, key, (Guid)ob[key]);
  1005. if (ob[key].holds(typeof(Vector3)))
  1006. set_property_vector3(new_id, key, (Vector3)ob[key]);
  1007. if (ob[key].holds(typeof(Quaternion)))
  1008. set_property_quaternion(new_id, key, (Quaternion)ob[key]);
  1009. }
  1010. }
  1011. }
  1012. /// Copies the database to db under the given new_key.
  1013. public void copy_to(Database db, string new_key)
  1014. {
  1015. assert(db != null);
  1016. assert(is_valid_key(new_key));
  1017. copy_deep(db, GUID_ZERO, new_key);
  1018. }
  1019. public void copy_deep(Database db, Guid id, string new_key)
  1020. {
  1021. HashMap<string, Value?> ob = get_data(id);
  1022. string[] keys = ob.keys.to_array();
  1023. foreach (string key in keys) {
  1024. Value? value = ob[key];
  1025. if (value.holds(typeof(HashSet))) {
  1026. HashSet<Guid?> hs = (HashSet<Guid?>)value;
  1027. foreach (Guid j in hs) {
  1028. db.create(j, object_type(j));
  1029. copy_deep(db, j, "");
  1030. db.add_to_set(id, new_key + (new_key == "" ? "" : ".") + key, j);
  1031. }
  1032. } else {
  1033. string kk = new_key + (new_key == "" ? "" : ".") + key;
  1034. if (ob[key] == null)
  1035. db.set_property_null(id, kk);
  1036. if (ob[key].holds(typeof(bool)))
  1037. db.set_property_bool(id, kk, (bool)ob[key]);
  1038. if (ob[key].holds(typeof(double)))
  1039. db.set_property_double(id, kk, (double)ob[key]);
  1040. if (ob[key].holds(typeof(string)))
  1041. db.set_property_string(id, kk, (string)ob[key]);
  1042. if (ob[key].holds(typeof(Guid)))
  1043. db.set_property_guid(id, kk, (Guid)ob[key]);
  1044. if (ob[key].holds(typeof(Vector3)))
  1045. db.set_property_vector3(id, kk, (Vector3)ob[key]);
  1046. if (ob[key].holds(typeof(Quaternion)))
  1047. db.set_property_quaternion(id, kk, (Quaternion)ob[key]);
  1048. }
  1049. }
  1050. }
  1051. // Tries to read a restore point @a rp from the @a stack and returns
  1052. // 0 if successful.
  1053. private int try_read_restore_point(ref RestorePoint rp, Stack stack)
  1054. {
  1055. if (stack.size() < sizeof(Action) + sizeof(RestorePointHeader))
  1056. return -1;
  1057. rp = stack.read_restore_point();
  1058. if (stack.size() < rp.header.size) {
  1059. // The restore point has been overwritten.
  1060. stack.clear();
  1061. return -1;
  1062. }
  1063. return 0;
  1064. }
  1065. // Un-does the last action and returns its ID, or -1 if there is no
  1066. // action to undo.
  1067. public int undo()
  1068. {
  1069. if (_undo_redo == null)
  1070. return -1;
  1071. RestorePoint rp = {};
  1072. if (try_read_restore_point(ref rp, _undo_redo._undo) != 0)
  1073. return -1;
  1074. undo_or_redo(_undo_redo._undo, _undo_redo._redo, rp.header.size);
  1075. undo_redo(true, rp.header.id, rp.data);
  1076. _undo_redo._redo.write_restore_point(rp.header.id, rp.data);
  1077. return (int)rp.header.id;
  1078. }
  1079. // Re-does the last action and returns its ID, or -1 if there is no
  1080. // action to redo.
  1081. public int redo()
  1082. {
  1083. if (_undo_redo == null)
  1084. return -1;
  1085. RestorePoint rp = {};
  1086. if (try_read_restore_point(ref rp, _undo_redo._redo) != 0)
  1087. return -1;
  1088. undo_or_redo(_undo_redo._redo, _undo_redo._undo, rp.header.size);
  1089. undo_redo(false, rp.header.id, rp.data);
  1090. _undo_redo._undo.write_restore_point(rp.header.id, rp.data);
  1091. return (int)rp.header.id;
  1092. }
  1093. private void undo_or_redo(Stack undo, Stack redo, uint32 restore_point_size)
  1094. {
  1095. assert(undo.size() >= restore_point_size);
  1096. int dir = undo == _undo_redo._undo ? -1 : 1;
  1097. // Read up to restore_point_size bytes.
  1098. uint32 undo_size_start = undo.size();
  1099. while (undo_size_start - undo.size() < restore_point_size) {
  1100. Action action = (Action)undo.read_uint32();
  1101. if (action == Action.CREATE) {
  1102. Guid id = undo.read_guid();
  1103. string obj_type = undo.read_string();
  1104. redo.write_destroy_action(Action.DESTROY, id, obj_type);
  1105. create_internal(dir, id);
  1106. set_object_type(id, obj_type);
  1107. } else if (action == Action.DESTROY) {
  1108. Guid id = undo.read_guid();
  1109. string obj_type = undo.read_string();
  1110. redo.write_create_action(Action.CREATE, id, obj_type);
  1111. destroy_internal(dir, id);
  1112. } else if (action == Action.SET_PROPERTY_NULL) {
  1113. Guid id = undo.read_guid();
  1114. string key = undo.read_string();
  1115. if (has_property(id, key)) {
  1116. if (get_data(id)[key].holds(typeof(bool)))
  1117. redo.write_set_property_bool_action(Action.SET_PROPERTY_BOOL, id, key, get_property_bool(id, key));
  1118. if (get_data(id)[key].holds(typeof(double)))
  1119. redo.write_set_property_double_action(Action.SET_PROPERTY_DOUBLE, id, key, get_property_double(id, key));
  1120. if (get_data(id)[key].holds(typeof(string)))
  1121. redo.write_set_property_string_action(Action.SET_PROPERTY_STRING, id, key, get_property_string(id, key));
  1122. if (get_data(id)[key].holds(typeof(Guid)))
  1123. redo.write_set_property_guid_action(Action.SET_PROPERTY_GUID, id, key, get_property_guid(id, key));
  1124. if (get_data(id)[key].holds(typeof(Vector3)))
  1125. redo.write_set_property_vector3_action(Action.SET_PROPERTY_VECTOR3, id, key, get_property_vector3(id, key));
  1126. if (get_data(id)[key].holds(typeof(Quaternion)))
  1127. redo.write_set_property_quaternion_action(Action.SET_PROPERTY_QUATERNION, id, key, get_property_quaternion(id, key));
  1128. } else {
  1129. redo.write_set_property_null_action(Action.SET_PROPERTY_NULL, id, key);
  1130. }
  1131. set_property_internal(dir, id, key, null);
  1132. } else if (action == Action.SET_PROPERTY_BOOL) {
  1133. Guid id = undo.read_guid();
  1134. string key = undo.read_string();
  1135. bool val = undo.read_bool();
  1136. if (has_property(id, key))
  1137. redo.write_set_property_bool_action(Action.SET_PROPERTY_BOOL, id, key, get_property_bool(id, key));
  1138. else
  1139. redo.write_set_property_null_action(Action.SET_PROPERTY_NULL, id, key);
  1140. set_property_internal(dir, id, key, val);
  1141. } else if (action == Action.SET_PROPERTY_DOUBLE) {
  1142. Guid id = undo.read_guid();
  1143. string key = undo.read_string();
  1144. double val = undo.read_double();
  1145. if (has_property(id, key))
  1146. redo.write_set_property_double_action(Action.SET_PROPERTY_DOUBLE, id, key, get_property_double(id, key));
  1147. else
  1148. redo.write_set_property_null_action(Action.SET_PROPERTY_NULL, id, key);
  1149. set_property_internal(dir, id, key, val);
  1150. } else if (action == Action.SET_PROPERTY_STRING) {
  1151. Guid id = undo.read_guid();
  1152. string key = undo.read_string();
  1153. string val = undo.read_string();
  1154. if (has_property(id, key))
  1155. redo.write_set_property_string_action(Action.SET_PROPERTY_STRING, id, key, get_property_string(id, key));
  1156. else
  1157. redo.write_set_property_null_action(Action.SET_PROPERTY_NULL, id, key);
  1158. set_property_internal(dir, id, key, val);
  1159. } else if (action == Action.SET_PROPERTY_GUID) {
  1160. Guid id = undo.read_guid();
  1161. string key = undo.read_string();
  1162. Guid val = undo.read_guid();
  1163. if (has_property(id, key))
  1164. redo.write_set_property_guid_action(Action.SET_PROPERTY_GUID, id, key, get_property_guid(id, key));
  1165. else
  1166. redo.write_set_property_null_action(Action.SET_PROPERTY_NULL, id, key);
  1167. set_property_internal(dir, id, key, val);
  1168. } else if (action == Action.SET_PROPERTY_VECTOR3) {
  1169. Guid id = undo.read_guid();
  1170. string key = undo.read_string();
  1171. Vector3 val = undo.read_vector3();
  1172. if (has_property(id, key))
  1173. redo.write_set_property_vector3_action(Action.SET_PROPERTY_VECTOR3, id, key, get_property_vector3(id, key));
  1174. else
  1175. redo.write_set_property_null_action(Action.SET_PROPERTY_NULL, id, key);
  1176. set_property_internal(dir, id, key, val);
  1177. } else if (action == Action.SET_PROPERTY_QUATERNION) {
  1178. Guid id = undo.read_guid();
  1179. string key = undo.read_string();
  1180. Quaternion val = undo.read_quaternion();
  1181. if (has_property(id, key))
  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. set_property_internal(dir, id, key, val);
  1186. } else if (action == Action.ADD_TO_SET) {
  1187. Guid id = undo.read_guid();
  1188. string key = undo.read_string();
  1189. Guid item_id = undo.read_guid();
  1190. redo.write_remove_from_set_action(Action.REMOVE_FROM_SET, id, key, item_id);
  1191. add_to_set_internal(dir, id, key, item_id);
  1192. } else if (action == Action.REMOVE_FROM_SET) {
  1193. Guid id = undo.read_guid();
  1194. string key = undo.read_string();
  1195. Guid item_id = undo.read_guid();
  1196. redo.write_add_to_set_action(Action.ADD_TO_SET, id, key, item_id);
  1197. remove_from_set_internal(dir, id, key, item_id);
  1198. }
  1199. }
  1200. }
  1201. }
  1202. } /* namespace Crown */