database.vala 46 KB

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