database.vala 38 KB

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