2
0

database.vala 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455
  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, resource_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.resource_path_to_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 = (Hashtable)json[i];
  516. // Decode object ID.
  517. Guid obj_id;
  518. if (obj.has_key("id"))
  519. obj_id = Guid.parse((string)obj["id"]);
  520. else if (obj.has_key("_guid"))
  521. obj_id = Guid.parse((string)obj["_guid"]);
  522. else
  523. obj_id = Guid.new_guid();
  524. create_internal(0, obj_id);
  525. // Determine the object's type based on the type of its
  526. // parent and other heuristics.
  527. string owner_type = object_type(owner_id);
  528. if (owner_type == "level") {
  529. if (key == "units")
  530. set_object_type(obj_id, "unit");
  531. else if (key == "sounds")
  532. set_object_type(obj_id, OBJECT_TYPE_SOUND_SOURCE);
  533. else
  534. set_object_type(obj_id, "undefined");
  535. } else if (owner_type == "state_machine") {
  536. if (key == "states")
  537. set_object_type(obj_id, "state_machine_node");
  538. else if (key == "variables")
  539. set_object_type(obj_id, "state_machine_variable");
  540. else
  541. set_object_type(obj_id, "undefined");
  542. } else if (owner_type == "state_machine_node") {
  543. if (key == "animations")
  544. set_object_type(obj_id, "node_animation");
  545. else if (key == "transitions")
  546. set_object_type(obj_id, "node_transition");
  547. } else if (owner_type == "sprite") {
  548. if (key == "frames") {
  549. set_object_type(obj_id, "sprite_frame");
  550. set_property_internal(0, obj_id, "index", (double)i);
  551. }
  552. }
  553. decode_object(obj_id, owner_id, "", obj);
  554. assert(has_property(obj_id, "_type"));
  555. add_to_set_internal(0, owner_id, key, obj_id);
  556. }
  557. }
  558. private Value? decode_value(Value? value)
  559. {
  560. if (value.holds(typeof(ArrayList))) {
  561. ArrayList<Value?> al = (ArrayList<Value?>)value;
  562. if (al.size == 1)
  563. return Vector3((double)al[0], 0.0, 0.0);
  564. else if (al.size == 2)
  565. return Vector3((double)al[0], (double)al[1], 0.0);
  566. else if (al.size == 3)
  567. return Vector3((double)al[0], (double)al[1], (double)al[2]);
  568. else if (al.size == 4)
  569. return Quaternion((double)al[0], (double)al[1], (double)al[2], (double)al[3]);
  570. else
  571. return Vector3(0.0, 0.0, 0.0);
  572. } else if (value.holds(typeof(string))) {
  573. Guid id;
  574. if (Guid.try_parse((string)value, out id))
  575. return id;
  576. return value;
  577. } else if (value == null
  578. || value.holds(typeof(bool))
  579. || value.holds(typeof(double))) {
  580. return value;
  581. } else {
  582. return null;
  583. }
  584. }
  585. private Hashtable encode_object(Guid id, HashMap<string, Value?> db)
  586. {
  587. Hashtable obj = new Hashtable();
  588. if (id != GUID_ZERO)
  589. obj["_guid"] = id.to_string();
  590. string[] keys = db.keys.to_array();
  591. foreach (string key in keys) {
  592. // Since null-key is equivalent to non-existent key, skip serialization.
  593. if (db[key] == null)
  594. continue;
  595. string[] foo = key.split(".");
  596. Hashtable x = obj;
  597. if (foo.length > 1) {
  598. for (int i = 0; i < foo.length - 1; ++i) {
  599. string f = foo[i];
  600. if (x.has_key(f)) {
  601. x = (Hashtable)x[f];
  602. continue;
  603. }
  604. Hashtable y = new Hashtable();
  605. x.set(f, y);
  606. x = y;
  607. }
  608. }
  609. x.set(foo[foo.length - 1], encode_value(db[key]));
  610. }
  611. return obj;
  612. }
  613. private Value? encode_value(Value? value)
  614. {
  615. assert(is_valid_value(value) || value.holds(typeof(HashSet)));
  616. if (value.holds(typeof(Vector3))) {
  617. Vector3 v = (Vector3)value;
  618. ArrayList<Value?> arr = new Gee.ArrayList<Value?>();
  619. arr.add(v.x);
  620. arr.add(v.y);
  621. arr.add(v.z);
  622. return arr;
  623. } else if (value.holds(typeof(Quaternion))) {
  624. Quaternion q = (Quaternion)value;
  625. ArrayList<Value?> arr = new Gee.ArrayList<Value?>();
  626. arr.add(q.x);
  627. arr.add(q.y);
  628. arr.add(q.z);
  629. arr.add(q.w);
  630. return arr;
  631. } else if (value.holds(typeof(Guid))) {
  632. Guid id = (Guid)value;
  633. return id.to_string();
  634. } else if (value.holds(typeof(HashSet))) {
  635. HashSet<Guid?> hs = (HashSet<Guid?>)value;
  636. ArrayList<Value?> arr = new Gee.ArrayList<Value?>();
  637. foreach (Guid id in hs) {
  638. arr.add(encode_object(id, get_data(id)));
  639. }
  640. return arr;
  641. } else {
  642. return value;
  643. }
  644. }
  645. private HashMap<string, Value?> get_data(Guid id)
  646. {
  647. assert(has_object(id));
  648. return _data[id];
  649. }
  650. private void create_internal(int dir, Guid id)
  651. {
  652. assert(id != GUID_ZERO);
  653. if (_debug)
  654. logi("create %s".printf(id.to_string()));
  655. _data[id] = new HashMap<string, Value?>();
  656. _distance_from_last_sync += dir;
  657. object_created(id);
  658. }
  659. private void destroy_internal(int dir, Guid id)
  660. {
  661. assert(id != GUID_ZERO);
  662. assert(has_object(id));
  663. if (_debug)
  664. logi("destroy %s".printf(id.to_string()));
  665. object_destroyed(id);
  666. _distance_from_last_sync += dir;
  667. _data.unset(id);
  668. }
  669. private void set_property_internal(int dir, Guid id, string key, Value? value)
  670. {
  671. assert(has_object(id));
  672. assert(is_valid_key(key));
  673. assert(is_valid_value(value));
  674. if (_debug)
  675. logi("set_property %s %s %s".printf(id.to_string(), key, value_to_string(value)));
  676. HashMap<string, Value?> ob = get_data(id);
  677. ob[key] = value;
  678. _distance_from_last_sync += dir;
  679. key_changed(id, key);
  680. }
  681. private void create_empty_set(int dir, Guid id, string key)
  682. {
  683. assert(has_object(id));
  684. assert(is_valid_key(key));
  685. HashMap<string, Value?> ob = get_data(id);
  686. assert(!ob.has_key(key));
  687. ob[key] = new HashSet<Guid?>(Guid.hash_func, Guid.equal_func);
  688. }
  689. private void add_to_set_internal(int dir, Guid id, string key, Guid item_id)
  690. {
  691. assert(has_object(id));
  692. assert(is_valid_key(key));
  693. assert(item_id != GUID_ZERO);
  694. assert(has_object(item_id));
  695. if (_debug)
  696. logi("add_to_set %s %s %s".printf(id.to_string(), key, item_id.to_string()));
  697. HashMap<string, Value?> ob = get_data(id);
  698. if (!ob.has_key(key)) {
  699. HashSet<Guid?> hs = new HashSet<Guid?>(Guid.hash_func, Guid.equal_func);
  700. hs.add(item_id);
  701. ob[key] = hs;
  702. } else {
  703. ((HashSet<Guid?>)ob[key]).add(item_id);
  704. }
  705. _distance_from_last_sync += dir;
  706. key_changed(id, key);
  707. }
  708. private void remove_from_set_internal(int dir, Guid id, string key, Guid item_id)
  709. {
  710. assert(has_object(id));
  711. assert(is_valid_key(key));
  712. assert(item_id != GUID_ZERO);
  713. if (_debug)
  714. logi("remove_from_set %s %s %s".printf(id.to_string(), key, item_id.to_string()));
  715. HashMap<string, Value?> ob = get_data(id);
  716. ((HashSet<Guid?>)ob[key]).remove(item_id);
  717. _distance_from_last_sync += dir;
  718. key_changed(id, key);
  719. }
  720. // Returns the type of the object @a id.
  721. public string object_type(Guid id)
  722. {
  723. assert(has_object(id));
  724. return (string)get_data(id)["_type"];
  725. }
  726. // Sets the @a type of the object @a id.
  727. // This is called automatically when loading data or when new objects are created via create().
  728. // It can occasionally be called manually after loading legacy data with no type information
  729. // stored inside objects.
  730. public void set_object_type(Guid id, string type)
  731. {
  732. assert(has_object(id));
  733. get_data(id)["_type"] = type;
  734. }
  735. public void create(Guid id, string type)
  736. {
  737. assert(id != GUID_ZERO);
  738. assert(!has_object(id));
  739. if (_undo_redo != null) {
  740. _undo_redo._undo.write_destroy_action(Action.DESTROY, id, type);
  741. _undo_redo._redo.clear();
  742. }
  743. create_internal(1, id);
  744. set_object_type(id, type);
  745. object_created(id);
  746. }
  747. public void destroy(Guid id)
  748. {
  749. assert(id != GUID_ZERO);
  750. assert(has_object(id));
  751. string obj_type = object_type(id);
  752. HashMap<string, Value?> o = get_data(id);
  753. string[] keys = o.keys.to_array();
  754. foreach (string key in keys) {
  755. Value? value = o[key];
  756. if (value.holds(typeof(HashSet))) {
  757. HashSet<Guid?> hs = (HashSet<Guid?>)value;
  758. Guid?[] ids = hs.to_array();
  759. foreach (Guid item_id in ids) {
  760. remove_from_set(id, key, item_id);
  761. destroy(item_id);
  762. }
  763. } else {
  764. set_property_null(id, key);
  765. }
  766. }
  767. if (_undo_redo != null) {
  768. _undo_redo._undo.write_create_action(Action.CREATE, id, obj_type);
  769. _undo_redo._redo.clear();
  770. }
  771. destroy_internal(1, id);
  772. }
  773. public void set_property_null(Guid id, string key)
  774. {
  775. assert(has_object(id));
  776. assert(is_valid_key(key));
  777. assert(is_valid_value(null));
  778. if (_undo_redo != null) {
  779. HashMap<string, Value?> ob = get_data(id);
  780. if (ob.has_key(key) && ob[key] != null) {
  781. if (ob[key].holds(typeof(bool)))
  782. _undo_redo._undo.write_set_property_bool_action(Action.SET_PROPERTY_BOOL, id, key, (bool)ob[key]);
  783. if (ob[key].holds(typeof(double)))
  784. _undo_redo._undo.write_set_property_double_action(Action.SET_PROPERTY_DOUBLE, id, key, (double)ob[key]);
  785. if (ob[key].holds(typeof(string)))
  786. _undo_redo._undo.write_set_property_string_action(Action.SET_PROPERTY_STRING, id, key, (string)ob[key]);
  787. if (ob[key].holds(typeof(Guid)))
  788. _undo_redo._undo.write_set_property_guid_action(Action.SET_PROPERTY_GUID, id, key, (Guid)ob[key]);
  789. if (ob[key].holds(typeof(Vector3)))
  790. _undo_redo._undo.write_set_property_vector3_action(Action.SET_PROPERTY_VECTOR3, id, key, (Vector3)ob[key]);
  791. if (ob[key].holds(typeof(Quaternion)))
  792. _undo_redo._undo.write_set_property_quaternion_action(Action.SET_PROPERTY_QUATERNION, id, key, (Quaternion)ob[key]);
  793. } else {
  794. _undo_redo._undo.write_set_property_null_action(Action.SET_PROPERTY_NULL, id, key);
  795. }
  796. _undo_redo._redo.clear();
  797. }
  798. set_property_internal(1, id, key, null);
  799. }
  800. public void set_property_bool(Guid id, string key, bool val)
  801. {
  802. assert(has_object(id));
  803. assert(is_valid_key(key));
  804. assert(is_valid_value(val));
  805. if (_undo_redo != null) {
  806. HashMap<string, Value?> ob = get_data(id);
  807. if (ob.has_key(key) && ob[key] != null)
  808. _undo_redo._undo.write_set_property_bool_action(Action.SET_PROPERTY_BOOL, id, key, (bool)ob[key]);
  809. else
  810. _undo_redo._undo.write_set_property_null_action(Action.SET_PROPERTY_NULL, id, key);
  811. _undo_redo._redo.clear();
  812. }
  813. set_property_internal(1, id, key, val);
  814. }
  815. public void set_property_double(Guid id, string key, double val)
  816. {
  817. assert(has_object(id));
  818. assert(is_valid_key(key));
  819. assert(is_valid_value(val));
  820. if (_undo_redo != null) {
  821. HashMap<string, Value?> ob = get_data(id);
  822. if (ob.has_key(key) && ob[key] != null)
  823. _undo_redo._undo.write_set_property_double_action(Action.SET_PROPERTY_DOUBLE, id, key, (double)ob[key]);
  824. else
  825. _undo_redo._undo.write_set_property_null_action(Action.SET_PROPERTY_NULL, id, key);
  826. _undo_redo._redo.clear();
  827. }
  828. set_property_internal(1, id, key, val);
  829. }
  830. public void set_property_string(Guid id, string key, string val)
  831. {
  832. assert(has_object(id));
  833. assert(is_valid_key(key));
  834. assert(is_valid_value(val));
  835. if (_undo_redo != null) {
  836. HashMap<string, Value?> ob = get_data(id);
  837. if (ob.has_key(key) && ob[key] != null)
  838. _undo_redo._undo.write_set_property_string_action(Action.SET_PROPERTY_STRING, id, key, (string)ob[key]);
  839. else
  840. _undo_redo._undo.write_set_property_null_action(Action.SET_PROPERTY_NULL, id, key);
  841. _undo_redo._redo.clear();
  842. }
  843. set_property_internal(1, id, key, val);
  844. }
  845. public void set_property_guid(Guid id, string key, Guid val)
  846. {
  847. assert(has_object(id));
  848. assert(is_valid_key(key));
  849. assert(is_valid_value(val));
  850. if (_undo_redo != null) {
  851. HashMap<string, Value?> ob = get_data(id);
  852. if (ob.has_key(key) && ob[key] != null)
  853. _undo_redo._undo.write_set_property_guid_action(Action.SET_PROPERTY_GUID, id, key, (Guid)ob[key]);
  854. else
  855. _undo_redo._undo.write_set_property_null_action(Action.SET_PROPERTY_NULL, id, key);
  856. _undo_redo._redo.clear();
  857. }
  858. set_property_internal(1, id, key, val);
  859. }
  860. public void set_property_vector3(Guid id, string key, Vector3 val)
  861. {
  862. assert(has_object(id));
  863. assert(is_valid_key(key));
  864. assert(is_valid_value(val));
  865. if (_undo_redo != null) {
  866. HashMap<string, Value?> ob = get_data(id);
  867. if (ob.has_key(key) && ob[key] != null)
  868. _undo_redo._undo.write_set_property_vector3_action(Action.SET_PROPERTY_VECTOR3, id, key, (Vector3)ob[key]);
  869. else
  870. _undo_redo._undo.write_set_property_null_action(Action.SET_PROPERTY_NULL, id, key);
  871. _undo_redo._redo.clear();
  872. }
  873. set_property_internal(1, id, key, val);
  874. }
  875. public void set_property_quaternion(Guid id, string key, Quaternion val)
  876. {
  877. assert(has_object(id));
  878. assert(is_valid_key(key));
  879. assert(is_valid_value(val));
  880. if (_undo_redo != null) {
  881. HashMap<string, Value?> ob = get_data(id);
  882. if (ob.has_key(key) && ob[key] != null)
  883. _undo_redo._undo.write_set_property_quaternion_action(Action.SET_PROPERTY_QUATERNION, id, key, (Quaternion)ob[key]);
  884. else
  885. _undo_redo._undo.write_set_property_null_action(Action.SET_PROPERTY_NULL, id, key);
  886. _undo_redo._redo.clear();
  887. }
  888. set_property_internal(1, id, key, val);
  889. }
  890. public void add_to_set(Guid id, string key, Guid item_id)
  891. {
  892. assert(has_object(id));
  893. assert(is_valid_key(key));
  894. assert(item_id != GUID_ZERO);
  895. assert(has_object(item_id));
  896. if (_undo_redo != null) {
  897. _undo_redo._undo.write_remove_from_set_action(Action.REMOVE_FROM_SET, id, key, item_id);
  898. _undo_redo._redo.clear();
  899. }
  900. add_to_set_internal(1, id, key, item_id);
  901. }
  902. public void remove_from_set(Guid id, string key, Guid item_id)
  903. {
  904. assert(has_object(id));
  905. assert(is_valid_key(key));
  906. assert(item_id != GUID_ZERO);
  907. if (_undo_redo != null) {
  908. _undo_redo._undo.write_add_to_set_action(Action.ADD_TO_SET, id, key, item_id);
  909. _undo_redo._redo.clear();
  910. }
  911. remove_from_set_internal(1, id, key, item_id);
  912. }
  913. public bool has_object(Guid id)
  914. {
  915. return id == GUID_ZERO || _data.has_key(id);
  916. }
  917. public bool has_property(Guid id, string key)
  918. {
  919. return get_property(id, key) != null;
  920. }
  921. public Value? get_property(Guid id, string key)
  922. {
  923. assert(has_object(id));
  924. assert(is_valid_key(key));
  925. HashMap<string, Value?> ob = get_data(id);
  926. Value? value = (ob.has_key(key) ? ob[key] : null);
  927. if (_debug_getters)
  928. logi("get_property %s %s %s".printf(id.to_string(), key, value_to_string(value)));
  929. return value;
  930. }
  931. public bool get_property_bool(Guid id, string key)
  932. {
  933. return (bool)get_property(id, key);
  934. }
  935. public double get_property_double(Guid id, string key)
  936. {
  937. return (double)get_property(id, key);
  938. }
  939. public string get_property_string(Guid id, string key)
  940. {
  941. return (string)get_property(id, key);
  942. }
  943. public Guid get_property_guid(Guid id, string key)
  944. {
  945. return (Guid)get_property(id, key);
  946. }
  947. public Vector3 get_property_vector3(Guid id, string key)
  948. {
  949. return (Vector3)get_property(id, key);
  950. }
  951. public Quaternion get_property_quaternion(Guid id, string key)
  952. {
  953. return (Quaternion)get_property(id, key);
  954. }
  955. public HashSet<Guid?> get_property_set(Guid id, string key, HashSet<Guid?> deffault)
  956. {
  957. assert(has_object(id));
  958. assert(is_valid_key(key));
  959. HashMap<string, Value?> ob = get_data(id);
  960. HashSet<Guid?> value;
  961. if (ob.has_key(key))
  962. value = ob[key] as HashSet<Guid?>;
  963. else
  964. value = deffault;
  965. if (_debug_getters)
  966. logi("get_property %s %s %s".printf(id.to_string(), key, value_to_string(value)));
  967. return value;
  968. }
  969. public HashMap<string, Value?> get_object(Guid id)
  970. {
  971. return (HashMap<string, Value?>)get_data(GUID_ZERO)[id.to_string()];
  972. }
  973. public string[] get_keys(Guid id)
  974. {
  975. HashMap<string, Value?> data = get_data(id);
  976. return data.keys.to_array();
  977. }
  978. public void add_restore_point(int id, Guid?[] data)
  979. {
  980. if (_debug)
  981. logi("add_restore_point %d, undo size = %u".printf(id, _undo_redo._undo.size()));
  982. if (_undo_redo != null) {
  983. _undo_redo._undo.write_restore_point(id, data);
  984. _undo_redo._redo.clear();
  985. }
  986. }
  987. /// Duplicates the object specified by id and assign new_id to the duplicated object.
  988. public void duplicate(Guid id, Guid new_id)
  989. {
  990. assert(id != GUID_ZERO);
  991. assert(new_id != GUID_ZERO);
  992. assert(id != new_id);
  993. assert(has_object(id));
  994. create(new_id, object_type(id));
  995. HashMap<string, Value?> ob = get_data(id);
  996. string[] keys = ob.keys.to_array();
  997. foreach (string key in keys) {
  998. Value? val = ob[key];
  999. if (val.holds(typeof(HashSet))) {
  1000. HashSet<Guid?> hs = (HashSet<Guid?>)val;
  1001. foreach (Guid j in hs) {
  1002. Guid x = Guid.new_guid();
  1003. duplicate(j, x);
  1004. add_to_set(new_id, key, x);
  1005. }
  1006. } else {
  1007. if (ob[key] == null)
  1008. set_property_null(new_id, key);
  1009. if (ob[key].holds(typeof(bool)))
  1010. set_property_bool(new_id, key, (bool)ob[key]);
  1011. if (ob[key].holds(typeof(double)))
  1012. set_property_double(new_id, key, (double)ob[key]);
  1013. if (ob[key].holds(typeof(string)))
  1014. set_property_string(new_id, key, (string)ob[key]);
  1015. if (ob[key].holds(typeof(Guid)))
  1016. set_property_guid(new_id, key, (Guid)ob[key]);
  1017. if (ob[key].holds(typeof(Vector3)))
  1018. set_property_vector3(new_id, key, (Vector3)ob[key]);
  1019. if (ob[key].holds(typeof(Quaternion)))
  1020. set_property_quaternion(new_id, key, (Quaternion)ob[key]);
  1021. }
  1022. }
  1023. }
  1024. /// Copies the database to db under the given new_key.
  1025. public void copy_to(Database db, string new_key)
  1026. {
  1027. assert(db != null);
  1028. assert(is_valid_key(new_key));
  1029. copy_deep(db, GUID_ZERO, new_key);
  1030. }
  1031. public void copy_deep(Database db, Guid id, string new_key)
  1032. {
  1033. HashMap<string, Value?> ob = get_data(id);
  1034. string[] keys = ob.keys.to_array();
  1035. foreach (string key in keys) {
  1036. Value? value = ob[key];
  1037. if (value.holds(typeof(HashSet))) {
  1038. HashSet<Guid?> hs = (HashSet<Guid?>)value;
  1039. foreach (Guid j in hs) {
  1040. db.create(j, object_type(j));
  1041. copy_deep(db, j, "");
  1042. db.add_to_set(id, new_key + (new_key == "" ? "" : ".") + key, j);
  1043. }
  1044. } else {
  1045. string kk = new_key + (new_key == "" ? "" : ".") + key;
  1046. if (ob[key] == null)
  1047. db.set_property_null(id, kk);
  1048. if (ob[key].holds(typeof(bool)))
  1049. db.set_property_bool(id, kk, (bool)ob[key]);
  1050. if (ob[key].holds(typeof(double)))
  1051. db.set_property_double(id, kk, (double)ob[key]);
  1052. if (ob[key].holds(typeof(string)))
  1053. db.set_property_string(id, kk, (string)ob[key]);
  1054. if (ob[key].holds(typeof(Guid)))
  1055. db.set_property_guid(id, kk, (Guid)ob[key]);
  1056. if (ob[key].holds(typeof(Vector3)))
  1057. db.set_property_vector3(id, kk, (Vector3)ob[key]);
  1058. if (ob[key].holds(typeof(Quaternion)))
  1059. db.set_property_quaternion(id, kk, (Quaternion)ob[key]);
  1060. }
  1061. }
  1062. }
  1063. // Tries to read a restore point @a rp from the @a stack and returns
  1064. // 0 if successful.
  1065. private int try_read_restore_point(ref RestorePoint rp, Stack stack)
  1066. {
  1067. if (stack.size() < sizeof(Action) + sizeof(RestorePointHeader))
  1068. return -1;
  1069. rp = stack.read_restore_point();
  1070. if (stack.size() < rp.header.size) {
  1071. // The restore point has been overwritten.
  1072. stack.clear();
  1073. return -1;
  1074. }
  1075. return 0;
  1076. }
  1077. // Un-does the last action and returns its ID, or -1 if there is no
  1078. // action to undo.
  1079. public int undo()
  1080. {
  1081. if (_undo_redo == null)
  1082. return -1;
  1083. RestorePoint rp = {};
  1084. if (try_read_restore_point(ref rp, _undo_redo._undo) != 0)
  1085. return -1;
  1086. undo_or_redo(_undo_redo._undo, _undo_redo._redo, rp.header.size);
  1087. undo_redo(true, rp.header.id, rp.data);
  1088. _undo_redo._redo.write_restore_point(rp.header.id, rp.data);
  1089. return (int)rp.header.id;
  1090. }
  1091. // Re-does the last action and returns its ID, or -1 if there is no
  1092. // action to redo.
  1093. public int redo()
  1094. {
  1095. if (_undo_redo == null)
  1096. return -1;
  1097. RestorePoint rp = {};
  1098. if (try_read_restore_point(ref rp, _undo_redo._redo) != 0)
  1099. return -1;
  1100. undo_or_redo(_undo_redo._redo, _undo_redo._undo, rp.header.size);
  1101. undo_redo(false, rp.header.id, rp.data);
  1102. _undo_redo._undo.write_restore_point(rp.header.id, rp.data);
  1103. return (int)rp.header.id;
  1104. }
  1105. private void undo_or_redo(Stack undo, Stack redo, uint32 restore_point_size)
  1106. {
  1107. assert(undo.size() >= restore_point_size);
  1108. int dir = undo == _undo_redo._undo ? -1 : 1;
  1109. // Read up to restore_point_size bytes.
  1110. uint32 undo_size_start = undo.size();
  1111. while (undo_size_start - undo.size() < restore_point_size) {
  1112. Action action = (Action)undo.read_uint32();
  1113. if (action == Action.CREATE) {
  1114. Guid id = undo.read_guid();
  1115. string obj_type = undo.read_string();
  1116. redo.write_destroy_action(Action.DESTROY, id, obj_type);
  1117. create_internal(dir, id);
  1118. set_object_type(id, obj_type);
  1119. } else if (action == Action.DESTROY) {
  1120. Guid id = undo.read_guid();
  1121. string obj_type = undo.read_string();
  1122. redo.write_create_action(Action.CREATE, id, obj_type);
  1123. destroy_internal(dir, id);
  1124. } else if (action == Action.SET_PROPERTY_NULL) {
  1125. Guid id = undo.read_guid();
  1126. string key = undo.read_string();
  1127. if (has_property(id, key)) {
  1128. if (get_data(id)[key].holds(typeof(bool)))
  1129. redo.write_set_property_bool_action(Action.SET_PROPERTY_BOOL, id, key, get_property_bool(id, key));
  1130. if (get_data(id)[key].holds(typeof(double)))
  1131. redo.write_set_property_double_action(Action.SET_PROPERTY_DOUBLE, id, key, get_property_double(id, key));
  1132. if (get_data(id)[key].holds(typeof(string)))
  1133. redo.write_set_property_string_action(Action.SET_PROPERTY_STRING, id, key, get_property_string(id, key));
  1134. if (get_data(id)[key].holds(typeof(Guid)))
  1135. redo.write_set_property_guid_action(Action.SET_PROPERTY_GUID, id, key, get_property_guid(id, key));
  1136. if (get_data(id)[key].holds(typeof(Vector3)))
  1137. redo.write_set_property_vector3_action(Action.SET_PROPERTY_VECTOR3, id, key, get_property_vector3(id, key));
  1138. if (get_data(id)[key].holds(typeof(Quaternion)))
  1139. redo.write_set_property_quaternion_action(Action.SET_PROPERTY_QUATERNION, id, key, get_property_quaternion(id, key));
  1140. } else {
  1141. redo.write_set_property_null_action(Action.SET_PROPERTY_NULL, id, key);
  1142. }
  1143. set_property_internal(dir, id, key, null);
  1144. } else if (action == Action.SET_PROPERTY_BOOL) {
  1145. Guid id = undo.read_guid();
  1146. string key = undo.read_string();
  1147. bool val = undo.read_bool();
  1148. if (has_property(id, key))
  1149. redo.write_set_property_bool_action(Action.SET_PROPERTY_BOOL, id, key, get_property_bool(id, key));
  1150. else
  1151. redo.write_set_property_null_action(Action.SET_PROPERTY_NULL, id, key);
  1152. set_property_internal(dir, id, key, val);
  1153. } else if (action == Action.SET_PROPERTY_DOUBLE) {
  1154. Guid id = undo.read_guid();
  1155. string key = undo.read_string();
  1156. double val = undo.read_double();
  1157. if (has_property(id, key))
  1158. redo.write_set_property_double_action(Action.SET_PROPERTY_DOUBLE, id, key, get_property_double(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_STRING) {
  1163. Guid id = undo.read_guid();
  1164. string key = undo.read_string();
  1165. string val = undo.read_string();
  1166. if (has_property(id, key))
  1167. redo.write_set_property_string_action(Action.SET_PROPERTY_STRING, id, key, get_property_string(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_GUID) {
  1172. Guid id = undo.read_guid();
  1173. string key = undo.read_string();
  1174. Guid val = undo.read_guid();
  1175. if (has_property(id, key))
  1176. redo.write_set_property_guid_action(Action.SET_PROPERTY_GUID, id, key, get_property_guid(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_VECTOR3) {
  1181. Guid id = undo.read_guid();
  1182. string key = undo.read_string();
  1183. Vector3 val = undo.read_vector3();
  1184. if (has_property(id, key))
  1185. redo.write_set_property_vector3_action(Action.SET_PROPERTY_VECTOR3, id, key, get_property_vector3(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_QUATERNION) {
  1190. Guid id = undo.read_guid();
  1191. string key = undo.read_string();
  1192. Quaternion val = undo.read_quaternion();
  1193. if (has_property(id, key))
  1194. redo.write_set_property_quaternion_action(Action.SET_PROPERTY_QUATERNION, id, key, get_property_quaternion(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.ADD_TO_SET) {
  1199. Guid id = undo.read_guid();
  1200. string key = undo.read_string();
  1201. Guid item_id = undo.read_guid();
  1202. redo.write_remove_from_set_action(Action.REMOVE_FROM_SET, id, key, item_id);
  1203. add_to_set_internal(dir, id, key, item_id);
  1204. } else if (action == Action.REMOVE_FROM_SET) {
  1205. Guid id = undo.read_guid();
  1206. string key = undo.read_string();
  1207. Guid item_id = undo.read_guid();
  1208. redo.write_add_to_set_action(Action.ADD_TO_SET, id, key, item_id);
  1209. remove_from_set_internal(dir, id, key, item_id);
  1210. }
  1211. }
  1212. }
  1213. }
  1214. } /* namespace Crown */