database.vala 41 KB

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