database.vala 41 KB

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