database.vala 39 KB

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