database.vala 39 KB

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