database.vala 36 KB

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