2
0

database.vala 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515
  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 int save(string path, Guid id)
  386. {
  387. int err = dump(path, id);
  388. if (err == 0)
  389. _distance_from_last_sync = 0;
  390. return err;
  391. }
  392. // See: add_from_path().
  393. public int add_from_file(out Guid object_id, FileStream? fs, string resource_path)
  394. {
  395. try {
  396. Hashtable json = SJSON.load_from_file(fs);
  397. // Parse the object's ID or generate a new one if none is found.
  398. if (json.has_key("id"))
  399. object_id = Guid.parse((string)json["id"]);
  400. else if (json.has_key("_guid"))
  401. object_id = Guid.parse((string)json["_guid"]);
  402. else
  403. object_id = Guid.new_guid();
  404. create_internal(0, object_id);
  405. set_object_type(object_id, ResourceId.type(resource_path));
  406. decode_object(object_id, GUID_ZERO, "", json);
  407. // Create a mapping between the path and the object it has been loaded into.
  408. set_property_internal(0, GUID_ZERO, resource_path, object_id);
  409. return 0;
  410. } catch (JsonSyntaxError e) {
  411. object_id = GUID_ZERO;
  412. return -1;
  413. }
  414. }
  415. // Adds the object stored at @a path to the database.
  416. // This makes it possible to load multiple objects from distinct
  417. // paths in the same database. @a resource_path is used as a key in the
  418. // database to refer to the object that has been loaded. This is useful when
  419. // you do not have the object ID but only its path, as it is often the case
  420. // since resources use paths and not IDs to reference each other.
  421. public int add_from_path(out Guid object_id, string path, string resource_path)
  422. {
  423. object_id = GUID_ZERO;
  424. FileStream fs = FileStream.open(path, "rb");
  425. if (fs == null)
  426. return 1;
  427. return add_from_file(out object_id, fs, resource_path);
  428. }
  429. public int add_from_resource_path(out Guid object_id, string resource_path)
  430. {
  431. // If the resource is already loaded.
  432. if (has_property(GUID_ZERO, resource_path)) {
  433. object_id = get_property_guid(GUID_ZERO, resource_path);
  434. return 0;
  435. }
  436. string path = _project.absolute_path(resource_path);
  437. return add_from_path(out object_id, path, resource_path);
  438. }
  439. /// Loads the database with the object stored at @a path.
  440. public int load_from_file(out Guid object_id, FileStream fs, string resource_path)
  441. {
  442. reset();
  443. return add_from_file(out object_id, fs, resource_path);
  444. }
  445. /// Loads the database with the object stored at @a path.
  446. public int load_from_path(out Guid object_id, string path, string resource_path)
  447. {
  448. reset();
  449. return add_from_path(out object_id, path, resource_path);
  450. }
  451. /// Encodes the object @a id into SJSON object.
  452. private Hashtable encode(Guid id)
  453. {
  454. return encode_object(id, get_data(id));
  455. }
  456. private static bool is_valid_value(Value? value)
  457. {
  458. return value == null
  459. || value.holds(typeof(bool))
  460. || value.holds(typeof(double))
  461. || value.holds(typeof(string))
  462. || value.holds(typeof(Guid))
  463. || value.holds(typeof(Vector3))
  464. || value.holds(typeof(Quaternion))
  465. ;
  466. }
  467. private static bool is_valid_key(string key)
  468. {
  469. return key.length > 0
  470. && !key.has_prefix(".")
  471. && !key.has_suffix(".")
  472. ;
  473. }
  474. private static string value_to_string(Value? value)
  475. {
  476. if (value == null)
  477. return "null";
  478. if (value.holds(typeof(bool)))
  479. return ((bool)value).to_string();
  480. if (value.holds(typeof(double)))
  481. return ((double)value).to_string();
  482. if (value.holds(typeof(string)))
  483. return ((string)value).to_string();
  484. if (value.holds(typeof(Guid)))
  485. return ((Guid)value).to_string();
  486. if (value.holds(typeof(Vector3)))
  487. return ((Vector3)value).to_string();
  488. if (value.holds(typeof(Quaternion)))
  489. return ((Quaternion)value).to_string();
  490. if (value.holds(typeof(HashSet)))
  491. return "Set<Guid>";
  492. return "<invalid>";
  493. }
  494. private void decode_object(Guid id, Guid owner_id, string db_key, Hashtable json)
  495. {
  496. string old_db = db_key;
  497. string k = db_key;
  498. string[] keys = json.keys.to_array();
  499. foreach (string key in keys) {
  500. // ID is filled by decode_set().
  501. if (key == "id" || key == "_guid")
  502. continue;
  503. // The "type" key defines object type only if it appears
  504. // in the root of a JSON object (k == "").
  505. if (k == "") {
  506. if (key == "type" || key == "_type")
  507. set_object_type(id, (string)json[key]);
  508. }
  509. Value? val = json[key];
  510. k += k == "" ? key : ("." + key);
  511. if (val.holds(typeof(Hashtable))) {
  512. Hashtable ht = (Hashtable)val;
  513. decode_object(id, owner_id, k, ht);
  514. } else if (val.holds(typeof(ArrayList))) {
  515. ArrayList<Value?> arr = (ArrayList<Value?>)val;
  516. if (arr.size > 0
  517. && arr[0].holds(typeof(double))
  518. && k != "frames" // sprite_animation
  519. )
  520. set_property_internal(0, id, k, decode_value(val));
  521. else
  522. decode_set(id, key, arr);
  523. } else {
  524. set_property_internal(0, id, k, decode_value(val));
  525. }
  526. k = old_db;
  527. }
  528. }
  529. private void decode_set(Guid owner_id, string key, ArrayList<Value?> json)
  530. {
  531. // Set should be created even if it is empty.
  532. create_empty_set(0, owner_id, key);
  533. for (int i = 0; i < json.size; ++i) {
  534. Hashtable obj;
  535. string owner_type = object_type(owner_id);
  536. if (owner_type == "sprite_animation")
  537. obj = new Hashtable();
  538. else
  539. obj = (Hashtable)json[i];
  540. // Decode object ID.
  541. Guid obj_id;
  542. if (obj.has_key("id") && owner_type != "font")
  543. obj_id = Guid.parse((string)obj["id"]);
  544. else if (obj.has_key("_guid"))
  545. obj_id = Guid.parse((string)obj["_guid"]);
  546. else
  547. obj_id = Guid.new_guid();
  548. create_internal(0, obj_id);
  549. // Determine the object's type based on the type of its
  550. // parent and other heuristics.
  551. if (owner_type == OBJECT_TYPE_LEVEL) {
  552. if (key == "units")
  553. set_object_type(obj_id, OBJECT_TYPE_UNIT);
  554. else if (key == "sounds")
  555. set_object_type(obj_id, OBJECT_TYPE_SOUND_SOURCE);
  556. else
  557. set_object_type(obj_id, "undefined");
  558. } else if (owner_type == OBJECT_TYPE_STATE_MACHINE) {
  559. if (key == "states")
  560. set_object_type(obj_id, "state_machine_node");
  561. else if (key == "variables")
  562. set_object_type(obj_id, "state_machine_variable");
  563. else
  564. set_object_type(obj_id, "undefined");
  565. } else if (owner_type == "state_machine_node") {
  566. if (key == "animations")
  567. set_object_type(obj_id, "node_animation");
  568. else if (key == "transitions")
  569. set_object_type(obj_id, "node_transition");
  570. } else if (owner_type == OBJECT_TYPE_SPRITE) {
  571. if (key == "frames") {
  572. set_object_type(obj_id, "sprite_frame");
  573. set_property_internal(0, obj_id, "index", (double)i);
  574. }
  575. } else if (owner_type == "sprite_animation") {
  576. if (key == "frames") {
  577. set_object_type(obj_id, "animation_frame");
  578. set_property_internal(0, obj_id, "index", (double)json[i]);
  579. }
  580. } else if (owner_type == "font") {
  581. if (key == "glyphs") {
  582. set_object_type(obj_id, "font_glyph");
  583. set_property_internal(0, obj_id, "cp", (double)obj["id"]);
  584. }
  585. }
  586. decode_object(obj_id, owner_id, "", obj);
  587. assert(has_property(obj_id, "_type"));
  588. add_to_set_internal(0, owner_id, key, obj_id);
  589. }
  590. }
  591. private Value? decode_value(Value? value)
  592. {
  593. if (value.holds(typeof(ArrayList))) {
  594. ArrayList<Value?> al = (ArrayList<Value?>)value;
  595. if (al.size == 1)
  596. return Vector3((double)al[0], 0.0, 0.0);
  597. else if (al.size == 2)
  598. return Vector3((double)al[0], (double)al[1], 0.0);
  599. else if (al.size == 3)
  600. return Vector3((double)al[0], (double)al[1], (double)al[2]);
  601. else if (al.size == 4)
  602. return Quaternion((double)al[0], (double)al[1], (double)al[2], (double)al[3]);
  603. else
  604. return Vector3(0.0, 0.0, 0.0);
  605. } else if (value.holds(typeof(string))) {
  606. Guid id;
  607. if (Guid.try_parse((string)value, out id))
  608. return id;
  609. return value;
  610. } else if (value == null
  611. || value.holds(typeof(bool))
  612. || value.holds(typeof(double))) {
  613. return value;
  614. } else {
  615. return null;
  616. }
  617. }
  618. private Hashtable encode_object(Guid id, HashMap<string, Value?> db)
  619. {
  620. Hashtable obj = new Hashtable();
  621. if (id != GUID_ZERO)
  622. obj["_guid"] = id.to_string();
  623. string[] keys = db.keys.to_array();
  624. foreach (string key in keys) {
  625. // Since null-key is equivalent to non-existent key, skip serialization.
  626. if (db[key] == null)
  627. continue;
  628. string[] foo = key.split(".");
  629. Hashtable x = obj;
  630. if (foo.length > 1) {
  631. for (int i = 0; i < foo.length - 1; ++i) {
  632. string f = foo[i];
  633. if (x.has_key(f)) {
  634. x = (Hashtable)x[f];
  635. continue;
  636. }
  637. Hashtable y = new Hashtable();
  638. x.set(f, y);
  639. x = y;
  640. }
  641. }
  642. x.set(foo[foo.length - 1], encode_value(db[key]));
  643. }
  644. return obj;
  645. }
  646. private Value? encode_value(Value? value)
  647. {
  648. assert(is_valid_value(value) || value.holds(typeof(HashSet)));
  649. if (value.holds(typeof(Vector3))) {
  650. Vector3 v = (Vector3)value;
  651. ArrayList<Value?> arr = new Gee.ArrayList<Value?>();
  652. arr.add(v.x);
  653. arr.add(v.y);
  654. arr.add(v.z);
  655. return arr;
  656. } else if (value.holds(typeof(Quaternion))) {
  657. Quaternion q = (Quaternion)value;
  658. ArrayList<Value?> arr = new Gee.ArrayList<Value?>();
  659. arr.add(q.x);
  660. arr.add(q.y);
  661. arr.add(q.z);
  662. arr.add(q.w);
  663. return arr;
  664. } else if (value.holds(typeof(Guid))) {
  665. Guid id = (Guid)value;
  666. return id.to_string();
  667. } else if (value.holds(typeof(HashSet))) {
  668. HashSet<Guid?> hs = (HashSet<Guid?>)value;
  669. ArrayList<Value?> arr = new Gee.ArrayList<Value?>();
  670. foreach (Guid id in hs) {
  671. arr.add(encode_object(id, get_data(id)));
  672. }
  673. return arr;
  674. } else {
  675. return value;
  676. }
  677. }
  678. private HashMap<string, Value?> get_data(Guid id)
  679. {
  680. assert(has_object(id));
  681. return _data[id];
  682. }
  683. private void create_internal(int dir, Guid id)
  684. {
  685. assert(id != GUID_ZERO);
  686. if (_debug)
  687. logi("create %s".printf(id.to_string()));
  688. _data[id] = new HashMap<string, Value?>();
  689. _distance_from_last_sync += dir;
  690. object_created(id);
  691. }
  692. private void destroy_internal(int dir, Guid id)
  693. {
  694. assert(id != GUID_ZERO);
  695. assert(has_object(id));
  696. if (_debug)
  697. logi("destroy %s".printf(id.to_string()));
  698. object_destroyed(id);
  699. _distance_from_last_sync += dir;
  700. }
  701. public void set_property_internal(int dir, Guid id, string key, Value? value)
  702. {
  703. assert(has_object(id));
  704. assert(is_valid_key(key));
  705. assert(is_valid_value(value));
  706. if (_debug)
  707. logi("set_property %s %s %s".printf(id.to_string(), key, value_to_string(value)));
  708. HashMap<string, Value?> ob = get_data(id);
  709. ob[key] = value;
  710. _distance_from_last_sync += dir;
  711. key_changed(id, key);
  712. }
  713. public void create_empty_set(int dir, Guid id, string key)
  714. {
  715. assert(has_object(id));
  716. assert(is_valid_key(key));
  717. HashMap<string, Value?> ob = get_data(id);
  718. assert(!ob.has_key(key));
  719. ob[key] = new HashSet<Guid?>(Guid.hash_func, Guid.equal_func);
  720. }
  721. private void add_to_set_internal(int dir, Guid id, string key, Guid item_id)
  722. {
  723. assert(has_object(id));
  724. assert(is_valid_key(key));
  725. assert(item_id != GUID_ZERO);
  726. assert(has_object(item_id));
  727. if (_debug)
  728. logi("add_to_set %s %s %s".printf(id.to_string(), key, item_id.to_string()));
  729. HashMap<string, Value?> ob = get_data(id);
  730. if (!ob.has_key(key)) {
  731. HashSet<Guid?> hs = new HashSet<Guid?>(Guid.hash_func, Guid.equal_func);
  732. hs.add(item_id);
  733. ob[key] = hs;
  734. } else {
  735. ((HashSet<Guid?>)ob[key]).add(item_id);
  736. }
  737. _distance_from_last_sync += dir;
  738. key_changed(id, key);
  739. }
  740. private void remove_from_set_internal(int dir, Guid id, string key, Guid item_id)
  741. {
  742. assert(has_object(id));
  743. assert(is_valid_key(key));
  744. assert(item_id != GUID_ZERO);
  745. if (_debug)
  746. logi("remove_from_set %s %s %s".printf(id.to_string(), key, item_id.to_string()));
  747. HashMap<string, Value?> ob = get_data(id);
  748. ((HashSet<Guid?>)ob[key]).remove(item_id);
  749. _distance_from_last_sync += dir;
  750. key_changed(id, key);
  751. }
  752. // Returns the type of the object @a id.
  753. public string object_type(Guid id)
  754. {
  755. assert(has_object(id));
  756. if (id == GUID_ZERO)
  757. return "database";
  758. else
  759. return (string)get_data(id)["_type"];
  760. }
  761. // Sets the @a type of the object @a id.
  762. // This is called automatically when loading data or when new objects are created via create().
  763. // It can occasionally be called manually after loading legacy data with no type information
  764. // stored inside objects.
  765. public void set_object_type(Guid id, string type)
  766. {
  767. assert(has_object(id));
  768. get_data(id)["_type"] = type;
  769. }
  770. public void create(Guid id, string type)
  771. {
  772. assert(id != GUID_ZERO);
  773. assert(!has_object(id));
  774. if (_undo_redo != null) {
  775. _undo_redo._undo.write_destroy_action(Action.DESTROY, id, type);
  776. _undo_redo._redo.clear();
  777. }
  778. create_internal(1, id);
  779. set_object_type(id, type);
  780. object_created(id);
  781. }
  782. public void destroy(Guid id)
  783. {
  784. assert(id != GUID_ZERO);
  785. assert(has_object(id));
  786. string obj_type = object_type(id);
  787. HashMap<string, Value?> o = get_data(id);
  788. string[] keys = o.keys.to_array();
  789. foreach (string key in keys) {
  790. Value? value = o[key];
  791. if (value.holds(typeof(HashSet))) {
  792. HashSet<Guid?> hs = (HashSet<Guid?>)value;
  793. Guid?[] ids = hs.to_array();
  794. foreach (Guid item_id in ids) {
  795. remove_from_set(id, key, item_id);
  796. destroy(item_id);
  797. }
  798. } else {
  799. if (key != "type" && key != "_type")
  800. set_property_null(id, key);
  801. }
  802. }
  803. if (_undo_redo != null) {
  804. _undo_redo._undo.write_create_action(Action.CREATE, id, obj_type);
  805. _undo_redo._redo.clear();
  806. }
  807. destroy_internal(1, id);
  808. }
  809. public void set_property_null(Guid id, string key)
  810. {
  811. assert(has_object(id));
  812. assert(is_valid_key(key));
  813. assert(is_valid_value(null));
  814. if (_undo_redo != null) {
  815. HashMap<string, Value?> ob = get_data(id);
  816. if (ob.has_key(key) && ob[key] != null) {
  817. if (ob[key].holds(typeof(bool)))
  818. _undo_redo._undo.write_set_property_bool_action(Action.SET_PROPERTY_BOOL, id, key, (bool)ob[key]);
  819. if (ob[key].holds(typeof(double)))
  820. _undo_redo._undo.write_set_property_double_action(Action.SET_PROPERTY_DOUBLE, id, key, (double)ob[key]);
  821. if (ob[key].holds(typeof(string)))
  822. _undo_redo._undo.write_set_property_string_action(Action.SET_PROPERTY_STRING, id, key, (string)ob[key]);
  823. if (ob[key].holds(typeof(Guid)))
  824. _undo_redo._undo.write_set_property_guid_action(Action.SET_PROPERTY_GUID, id, key, (Guid)ob[key]);
  825. if (ob[key].holds(typeof(Vector3)))
  826. _undo_redo._undo.write_set_property_vector3_action(Action.SET_PROPERTY_VECTOR3, id, key, (Vector3)ob[key]);
  827. if (ob[key].holds(typeof(Quaternion)))
  828. _undo_redo._undo.write_set_property_quaternion_action(Action.SET_PROPERTY_QUATERNION, id, key, (Quaternion)ob[key]);
  829. } else {
  830. _undo_redo._undo.write_set_property_null_action(Action.SET_PROPERTY_NULL, id, key);
  831. }
  832. _undo_redo._redo.clear();
  833. }
  834. set_property_internal(1, id, key, null);
  835. }
  836. public void set_property_bool(Guid id, string key, bool val)
  837. {
  838. assert(has_object(id));
  839. assert(is_valid_key(key));
  840. assert(is_valid_value(val));
  841. if (_undo_redo != null) {
  842. HashMap<string, Value?> ob = get_data(id);
  843. if (ob.has_key(key) && ob[key] != null)
  844. _undo_redo._undo.write_set_property_bool_action(Action.SET_PROPERTY_BOOL, id, key, (bool)ob[key]);
  845. else
  846. _undo_redo._undo.write_set_property_null_action(Action.SET_PROPERTY_NULL, id, key);
  847. _undo_redo._redo.clear();
  848. }
  849. set_property_internal(1, id, key, val);
  850. }
  851. public void set_property_double(Guid id, string key, double val)
  852. {
  853. assert(has_object(id));
  854. assert(is_valid_key(key));
  855. assert(is_valid_value(val));
  856. if (_undo_redo != null) {
  857. HashMap<string, Value?> ob = get_data(id);
  858. if (ob.has_key(key) && ob[key] != null)
  859. _undo_redo._undo.write_set_property_double_action(Action.SET_PROPERTY_DOUBLE, id, key, (double)ob[key]);
  860. else
  861. _undo_redo._undo.write_set_property_null_action(Action.SET_PROPERTY_NULL, id, key);
  862. _undo_redo._redo.clear();
  863. }
  864. set_property_internal(1, id, key, val);
  865. }
  866. public void set_property_string(Guid id, string key, string val)
  867. {
  868. assert(has_object(id));
  869. assert(is_valid_key(key));
  870. assert(is_valid_value(val));
  871. if (_undo_redo != null) {
  872. HashMap<string, Value?> ob = get_data(id);
  873. if (ob.has_key(key) && ob[key] != null)
  874. _undo_redo._undo.write_set_property_string_action(Action.SET_PROPERTY_STRING, id, key, (string)ob[key]);
  875. else
  876. _undo_redo._undo.write_set_property_null_action(Action.SET_PROPERTY_NULL, id, key);
  877. _undo_redo._redo.clear();
  878. }
  879. set_property_internal(1, id, key, val);
  880. }
  881. public void set_property_guid(Guid id, string key, Guid val)
  882. {
  883. assert(has_object(id));
  884. assert(is_valid_key(key));
  885. assert(is_valid_value(val));
  886. if (_undo_redo != null) {
  887. HashMap<string, Value?> ob = get_data(id);
  888. if (ob.has_key(key) && ob[key] != null)
  889. _undo_redo._undo.write_set_property_guid_action(Action.SET_PROPERTY_GUID, id, key, (Guid)ob[key]);
  890. else
  891. _undo_redo._undo.write_set_property_null_action(Action.SET_PROPERTY_NULL, id, key);
  892. _undo_redo._redo.clear();
  893. }
  894. set_property_internal(1, id, key, val);
  895. }
  896. public void set_property_vector3(Guid id, string key, Vector3 val)
  897. {
  898. assert(has_object(id));
  899. assert(is_valid_key(key));
  900. assert(is_valid_value(val));
  901. if (_undo_redo != null) {
  902. HashMap<string, Value?> ob = get_data(id);
  903. if (ob.has_key(key) && ob[key] != null)
  904. _undo_redo._undo.write_set_property_vector3_action(Action.SET_PROPERTY_VECTOR3, id, key, (Vector3)ob[key]);
  905. else
  906. _undo_redo._undo.write_set_property_null_action(Action.SET_PROPERTY_NULL, id, key);
  907. _undo_redo._redo.clear();
  908. }
  909. set_property_internal(1, id, key, val);
  910. }
  911. public void set_property_quaternion(Guid id, string key, Quaternion val)
  912. {
  913. assert(has_object(id));
  914. assert(is_valid_key(key));
  915. assert(is_valid_value(val));
  916. if (_undo_redo != null) {
  917. HashMap<string, Value?> ob = get_data(id);
  918. if (ob.has_key(key) && ob[key] != null)
  919. _undo_redo._undo.write_set_property_quaternion_action(Action.SET_PROPERTY_QUATERNION, id, key, (Quaternion)ob[key]);
  920. else
  921. _undo_redo._undo.write_set_property_null_action(Action.SET_PROPERTY_NULL, id, key);
  922. _undo_redo._redo.clear();
  923. }
  924. set_property_internal(1, id, key, val);
  925. }
  926. public void set_property(Guid id, string key, Value? val)
  927. {
  928. if (val == null)
  929. set_property_null(id, key);
  930. if (val.holds(typeof(bool)))
  931. set_property_bool(id, key, (bool)val);
  932. else if (val.holds(typeof(double)))
  933. set_property_double(id, key, (double)val);
  934. else if (val.holds(typeof(string)))
  935. set_property_string(id, key, (string)val);
  936. else if (val.holds(typeof(Guid)))
  937. set_property_guid(id, key, (Guid)val);
  938. else if (val.holds(typeof(Vector3)))
  939. set_property_vector3(id, key, (Vector3)val);
  940. else if (val.holds(typeof(Quaternion)))
  941. set_property_quaternion(id, key, (Quaternion)val);
  942. else
  943. assert(false);
  944. }
  945. public void add_to_set(Guid id, string key, Guid item_id)
  946. {
  947. assert(has_object(id));
  948. assert(is_valid_key(key));
  949. assert(item_id != GUID_ZERO);
  950. assert(has_object(item_id));
  951. if (_undo_redo != null) {
  952. _undo_redo._undo.write_remove_from_set_action(Action.REMOVE_FROM_SET, id, key, item_id);
  953. _undo_redo._redo.clear();
  954. }
  955. add_to_set_internal(1, id, key, item_id);
  956. }
  957. public void remove_from_set(Guid id, string key, Guid item_id)
  958. {
  959. assert(has_object(id));
  960. assert(is_valid_key(key));
  961. assert(item_id != GUID_ZERO);
  962. if (_undo_redo != null) {
  963. _undo_redo._undo.write_add_to_set_action(Action.ADD_TO_SET, id, key, item_id);
  964. _undo_redo._redo.clear();
  965. }
  966. remove_from_set_internal(1, id, key, item_id);
  967. }
  968. public bool has_object(Guid id)
  969. {
  970. return id == GUID_ZERO || _data.has_key(id);
  971. }
  972. public bool has_property(Guid id, string key)
  973. {
  974. return get_property(id, key) != null;
  975. }
  976. public Value? get_property(Guid id, string key)
  977. {
  978. assert(has_object(id));
  979. assert(is_valid_key(key));
  980. HashMap<string, Value?> ob = get_data(id);
  981. Value? value = (ob.has_key(key) ? ob[key] : null);
  982. if (_debug_getters)
  983. logi("get_property %s %s %s".printf(id.to_string(), key, value_to_string(value)));
  984. return value;
  985. }
  986. public bool get_property_bool(Guid id, string key)
  987. {
  988. return (bool)get_property(id, key);
  989. }
  990. public double get_property_double(Guid id, string key)
  991. {
  992. return (double)get_property(id, key);
  993. }
  994. public string get_property_string(Guid id, string key)
  995. {
  996. return (string)get_property(id, key);
  997. }
  998. public Guid get_property_guid(Guid id, string key)
  999. {
  1000. return (Guid)get_property(id, key);
  1001. }
  1002. public Vector3 get_property_vector3(Guid id, string key)
  1003. {
  1004. return (Vector3)get_property(id, key);
  1005. }
  1006. public Quaternion get_property_quaternion(Guid id, string key)
  1007. {
  1008. return (Quaternion)get_property(id, key);
  1009. }
  1010. public HashSet<Guid?> get_property_set(Guid id, string key, HashSet<Guid?> deffault)
  1011. {
  1012. assert(has_object(id));
  1013. assert(is_valid_key(key));
  1014. HashMap<string, Value?> ob = get_data(id);
  1015. HashSet<Guid?> value;
  1016. if (ob.has_key(key))
  1017. value = ob[key] as HashSet<Guid?>;
  1018. else
  1019. value = deffault;
  1020. if (_debug_getters)
  1021. logi("get_property %s %s %s".printf(id.to_string(), key, value_to_string(value)));
  1022. return value;
  1023. }
  1024. public HashMap<string, Value?> get_object(Guid id)
  1025. {
  1026. return (HashMap<string, Value?>)get_data(GUID_ZERO)[id.to_string()];
  1027. }
  1028. public string[] get_keys(Guid id)
  1029. {
  1030. HashMap<string, Value?> data = get_data(id);
  1031. return data.keys.to_array();
  1032. }
  1033. public void add_restore_point(int id, Guid?[] data, uint32 flags = 0u)
  1034. {
  1035. if (_debug)
  1036. logi("add_restore_point %d, undo size = %u".printf(id, _undo_redo._undo.size()));
  1037. if (_undo_redo != null) {
  1038. _undo_redo._undo.write_restore_point(id, flags, data);
  1039. _undo_redo._redo.clear();
  1040. restore_point_added(id, data, flags);
  1041. }
  1042. }
  1043. /// Duplicates the object specified by id and assign new_id to the duplicated object.
  1044. public void duplicate(Guid id, Guid new_id)
  1045. {
  1046. assert(id != GUID_ZERO);
  1047. assert(new_id != GUID_ZERO);
  1048. assert(id != new_id);
  1049. assert(has_object(id));
  1050. create(new_id, object_type(id));
  1051. HashMap<string, Value?> ob = get_data(id);
  1052. string[] keys = ob.keys.to_array();
  1053. foreach (string key in keys) {
  1054. Value? val = ob[key];
  1055. if (val.holds(typeof(HashSet))) {
  1056. HashSet<Guid?> hs = (HashSet<Guid?>)val;
  1057. foreach (Guid j in hs) {
  1058. Guid x = Guid.new_guid();
  1059. duplicate(j, x);
  1060. add_to_set(new_id, key, x);
  1061. }
  1062. } else {
  1063. if (ob[key] == null)
  1064. set_property_null(new_id, key);
  1065. if (ob[key].holds(typeof(bool)))
  1066. set_property_bool(new_id, key, (bool)ob[key]);
  1067. if (ob[key].holds(typeof(double)))
  1068. set_property_double(new_id, key, (double)ob[key]);
  1069. if (ob[key].holds(typeof(string)))
  1070. set_property_string(new_id, key, (string)ob[key]);
  1071. if (ob[key].holds(typeof(Guid)))
  1072. set_property_guid(new_id, key, (Guid)ob[key]);
  1073. if (ob[key].holds(typeof(Vector3)))
  1074. set_property_vector3(new_id, key, (Vector3)ob[key]);
  1075. if (ob[key].holds(typeof(Quaternion)))
  1076. set_property_quaternion(new_id, key, (Quaternion)ob[key]);
  1077. }
  1078. }
  1079. }
  1080. /// Copies the database to db under the given new_key.
  1081. public void copy_to(Database db, string new_key)
  1082. {
  1083. assert(db != null);
  1084. assert(is_valid_key(new_key));
  1085. copy_deep(db, GUID_ZERO, new_key);
  1086. }
  1087. public void copy_deep(Database db, Guid id, string new_key)
  1088. {
  1089. HashMap<string, Value?> ob = get_data(id);
  1090. string[] keys = ob.keys.to_array();
  1091. foreach (string key in keys) {
  1092. Value? value = ob[key];
  1093. if (value.holds(typeof(HashSet))) {
  1094. HashSet<Guid?> hs = (HashSet<Guid?>)value;
  1095. foreach (Guid j in hs) {
  1096. db.create(j, object_type(j));
  1097. copy_deep(db, j, "");
  1098. db.add_to_set(id, new_key + (new_key == "" ? "" : ".") + key, j);
  1099. }
  1100. } else {
  1101. if (!db.has_object(id))
  1102. db.create(id, object_type(id));
  1103. string kk = new_key + (new_key == "" ? "" : ".") + key;
  1104. if (ob[key] == null)
  1105. db.set_property_null(id, kk);
  1106. if (ob[key].holds(typeof(bool)))
  1107. db.set_property_bool(id, kk, (bool)ob[key]);
  1108. if (ob[key].holds(typeof(double)))
  1109. db.set_property_double(id, kk, (double)ob[key]);
  1110. if (ob[key].holds(typeof(string)))
  1111. db.set_property_string(id, kk, (string)ob[key]);
  1112. if (ob[key].holds(typeof(Guid)))
  1113. db.set_property_guid(id, kk, (Guid)ob[key]);
  1114. if (ob[key].holds(typeof(Vector3)))
  1115. db.set_property_vector3(id, kk, (Vector3)ob[key]);
  1116. if (ob[key].holds(typeof(Quaternion)))
  1117. db.set_property_quaternion(id, kk, (Quaternion)ob[key]);
  1118. }
  1119. }
  1120. }
  1121. // Tries to read a restore point @a rp from the @a stack and returns
  1122. // 0 if successful.
  1123. private int try_read_restore_point(ref RestorePoint rp, Stack stack)
  1124. {
  1125. if (stack.size() < sizeof(Action) + sizeof(RestorePointHeader))
  1126. return -1;
  1127. rp = stack.read_restore_point();
  1128. if (stack.size() < rp.header.size) {
  1129. // The restore point has been overwritten.
  1130. stack.clear();
  1131. return -1;
  1132. }
  1133. return 0;
  1134. }
  1135. // Un-does the last action and returns its ID, or -1 if there is no
  1136. // action to undo.
  1137. public int undo()
  1138. {
  1139. if (_undo_redo == null)
  1140. return -1;
  1141. RestorePoint rp = {};
  1142. if (try_read_restore_point(ref rp, _undo_redo._undo) != 0)
  1143. return -1;
  1144. undo_or_redo(_undo_redo._undo, _undo_redo._redo, rp.header.size);
  1145. undo_redo(true, rp.header.id, rp.data);
  1146. _undo_redo._redo.write_restore_point(rp.header.id, rp.header.flags, rp.data);
  1147. return (int)rp.header.id;
  1148. }
  1149. // Re-does the last action and returns its ID, or -1 if there is no
  1150. // action to redo.
  1151. public int redo()
  1152. {
  1153. if (_undo_redo == null)
  1154. return -1;
  1155. RestorePoint rp = {};
  1156. if (try_read_restore_point(ref rp, _undo_redo._redo) != 0)
  1157. return -1;
  1158. undo_or_redo(_undo_redo._redo, _undo_redo._undo, rp.header.size);
  1159. undo_redo(false, rp.header.id, rp.data);
  1160. _undo_redo._undo.write_restore_point(rp.header.id, rp.header.flags, rp.data);
  1161. return (int)rp.header.id;
  1162. }
  1163. private void undo_or_redo(Stack undo, Stack redo, uint32 restore_point_size)
  1164. {
  1165. assert(undo.size() >= restore_point_size);
  1166. int dir = undo == _undo_redo._undo ? -1 : 1;
  1167. // Read up to restore_point_size bytes.
  1168. uint32 undo_size_start = undo.size();
  1169. while (undo_size_start - undo.size() < restore_point_size) {
  1170. Action action = (Action)undo.read_uint32();
  1171. if (action == Action.CREATE) {
  1172. Guid id = undo.read_guid();
  1173. string obj_type = undo.read_string();
  1174. redo.write_destroy_action(Action.DESTROY, id, obj_type);
  1175. create_internal(dir, id);
  1176. set_object_type(id, obj_type);
  1177. } else if (action == Action.DESTROY) {
  1178. Guid id = undo.read_guid();
  1179. string obj_type = undo.read_string();
  1180. redo.write_create_action(Action.CREATE, id, obj_type);
  1181. destroy_internal(dir, id);
  1182. } else if (action == Action.SET_PROPERTY_NULL) {
  1183. Guid id = undo.read_guid();
  1184. string key = undo.read_string();
  1185. if (has_property(id, key)) {
  1186. if (get_data(id)[key].holds(typeof(bool)))
  1187. redo.write_set_property_bool_action(Action.SET_PROPERTY_BOOL, id, key, get_property_bool(id, key));
  1188. if (get_data(id)[key].holds(typeof(double)))
  1189. redo.write_set_property_double_action(Action.SET_PROPERTY_DOUBLE, id, key, get_property_double(id, key));
  1190. if (get_data(id)[key].holds(typeof(string)))
  1191. redo.write_set_property_string_action(Action.SET_PROPERTY_STRING, id, key, get_property_string(id, key));
  1192. if (get_data(id)[key].holds(typeof(Guid)))
  1193. redo.write_set_property_guid_action(Action.SET_PROPERTY_GUID, id, key, get_property_guid(id, key));
  1194. if (get_data(id)[key].holds(typeof(Vector3)))
  1195. redo.write_set_property_vector3_action(Action.SET_PROPERTY_VECTOR3, id, key, get_property_vector3(id, key));
  1196. if (get_data(id)[key].holds(typeof(Quaternion)))
  1197. redo.write_set_property_quaternion_action(Action.SET_PROPERTY_QUATERNION, id, key, get_property_quaternion(id, key));
  1198. } else {
  1199. redo.write_set_property_null_action(Action.SET_PROPERTY_NULL, id, key);
  1200. }
  1201. set_property_internal(dir, id, key, null);
  1202. } else if (action == Action.SET_PROPERTY_BOOL) {
  1203. Guid id = undo.read_guid();
  1204. string key = undo.read_string();
  1205. bool val = undo.read_bool();
  1206. if (has_property(id, key))
  1207. redo.write_set_property_bool_action(Action.SET_PROPERTY_BOOL, id, key, get_property_bool(id, key));
  1208. else
  1209. redo.write_set_property_null_action(Action.SET_PROPERTY_NULL, id, key);
  1210. set_property_internal(dir, id, key, val);
  1211. } else if (action == Action.SET_PROPERTY_DOUBLE) {
  1212. Guid id = undo.read_guid();
  1213. string key = undo.read_string();
  1214. double val = undo.read_double();
  1215. if (has_property(id, key))
  1216. redo.write_set_property_double_action(Action.SET_PROPERTY_DOUBLE, id, key, get_property_double(id, key));
  1217. else
  1218. redo.write_set_property_null_action(Action.SET_PROPERTY_NULL, id, key);
  1219. set_property_internal(dir, id, key, val);
  1220. } else if (action == Action.SET_PROPERTY_STRING) {
  1221. Guid id = undo.read_guid();
  1222. string key = undo.read_string();
  1223. string val = undo.read_string();
  1224. if (has_property(id, key))
  1225. redo.write_set_property_string_action(Action.SET_PROPERTY_STRING, id, key, get_property_string(id, key));
  1226. else
  1227. redo.write_set_property_null_action(Action.SET_PROPERTY_NULL, id, key);
  1228. set_property_internal(dir, id, key, val);
  1229. } else if (action == Action.SET_PROPERTY_GUID) {
  1230. Guid id = undo.read_guid();
  1231. string key = undo.read_string();
  1232. Guid val = undo.read_guid();
  1233. if (has_property(id, key))
  1234. redo.write_set_property_guid_action(Action.SET_PROPERTY_GUID, id, key, get_property_guid(id, key));
  1235. else
  1236. redo.write_set_property_null_action(Action.SET_PROPERTY_NULL, id, key);
  1237. set_property_internal(dir, id, key, val);
  1238. } else if (action == Action.SET_PROPERTY_VECTOR3) {
  1239. Guid id = undo.read_guid();
  1240. string key = undo.read_string();
  1241. Vector3 val = undo.read_vector3();
  1242. if (has_property(id, key))
  1243. redo.write_set_property_vector3_action(Action.SET_PROPERTY_VECTOR3, id, key, get_property_vector3(id, key));
  1244. else
  1245. redo.write_set_property_null_action(Action.SET_PROPERTY_NULL, id, key);
  1246. set_property_internal(dir, id, key, val);
  1247. } else if (action == Action.SET_PROPERTY_QUATERNION) {
  1248. Guid id = undo.read_guid();
  1249. string key = undo.read_string();
  1250. Quaternion val = undo.read_quaternion();
  1251. if (has_property(id, key))
  1252. redo.write_set_property_quaternion_action(Action.SET_PROPERTY_QUATERNION, id, key, get_property_quaternion(id, key));
  1253. else
  1254. redo.write_set_property_null_action(Action.SET_PROPERTY_NULL, id, key);
  1255. set_property_internal(dir, id, key, val);
  1256. } else if (action == Action.ADD_TO_SET) {
  1257. Guid id = undo.read_guid();
  1258. string key = undo.read_string();
  1259. Guid item_id = undo.read_guid();
  1260. redo.write_remove_from_set_action(Action.REMOVE_FROM_SET, id, key, item_id);
  1261. add_to_set_internal(dir, id, key, item_id);
  1262. } else if (action == Action.REMOVE_FROM_SET) {
  1263. Guid id = undo.read_guid();
  1264. string key = undo.read_string();
  1265. Guid item_id = undo.read_guid();
  1266. redo.write_add_to_set_action(Action.ADD_TO_SET, id, key, item_id);
  1267. remove_from_set_internal(dir, id, key, item_id);
  1268. }
  1269. }
  1270. }
  1271. }
  1272. } /* namespace Crown */