database.vala 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155
  1. /*
  2. * Copyright (c) 2012-2026 Daniele Bartolini et al.
  3. * SPDX-License-Identifier: GPL-3.0-or-later
  4. */
  5. namespace Crown
  6. {
  7. public enum PropertyType
  8. {
  9. BOOL,
  10. DOUBLE,
  11. STRING,
  12. VECTOR3,
  13. QUATERNION,
  14. RESOURCE,
  15. REFERENCE,
  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. public bool not_serialized;
  46. public string? tooltip;
  47. }
  48. public struct Resource
  49. {
  50. public string? name;
  51. }
  52. public enum UndoRedoAction
  53. {
  54. RESTORE_POINT = int.MAX;
  55. }
  56. public struct RestorePointHeader
  57. {
  58. public uint32 id;
  59. public uint32 flags;
  60. public uint32 size;
  61. public uint32 num_guids;
  62. }
  63. public struct RestorePoint
  64. {
  65. public RestorePointHeader header;
  66. public Guid?[] data;
  67. }
  68. public class Stack
  69. {
  70. public uint32 _capacity;
  71. public uint8[] _data;
  72. public uint32 _read; // Position of the read/write head.
  73. public uint32 _size; // Size of the data written in the stack.
  74. public uint32 _last_write_restore_point_size; // Size when write_restore_point() was last called.
  75. ///
  76. public Stack(uint32 capacity)
  77. {
  78. assert(capacity > 0);
  79. _capacity = capacity;
  80. _data = new uint8[_capacity];
  81. clear();
  82. }
  83. ///
  84. public uint32 size()
  85. {
  86. return _size;
  87. }
  88. ///
  89. public void clear()
  90. {
  91. _read = 0;
  92. _size = 0;
  93. _last_write_restore_point_size = 0;
  94. }
  95. // Copies @a data into @a destination.
  96. public void copy_data(uint8* destination, void* data, ulong len)
  97. {
  98. uint8* source = (uint8*)data;
  99. for (ulong ii = 0; ii < len; ++ii)
  100. destination[ii] = source[ii];
  101. }
  102. // Writes @a data into the current page.
  103. public void write_data_internal(uint8* data, uint32 len)
  104. {
  105. assert(data != null);
  106. uint32 bytes_left = len;
  107. uint32 bytes_avail;
  108. // Write the data that wraps around.
  109. while (bytes_left > (bytes_avail = _capacity - _read)) {
  110. copy_data(&_data[_read]
  111. , ((uint8*)data) + (len - bytes_left)
  112. , bytes_avail
  113. );
  114. _read = (_read + bytes_avail) % _capacity;
  115. _size = uint32.min(_capacity, _size + bytes_avail);
  116. bytes_left -= bytes_avail;
  117. }
  118. // Write the remaining data.
  119. copy_data(&_data[_read]
  120. , ((uint8*)data) + (len - bytes_left)
  121. , bytes_left
  122. );
  123. _read += bytes_left;
  124. _size = uint32.min(_capacity, _size + bytes_left);
  125. _last_write_restore_point_size += len;
  126. }
  127. // Wrapper to avoid casting sizeof() manually.
  128. public void write_data(void* data, ulong len)
  129. {
  130. write_data_internal((uint8*)data, (uint32)len);
  131. }
  132. public void read_data_internal(uint8* data, uint32 len)
  133. {
  134. assert(data != null);
  135. uint32 bytes_left = len;
  136. // Read the data that wraps around.
  137. while (bytes_left > _read) {
  138. copy_data(data + bytes_left - _read
  139. , &_data[0]
  140. , _read
  141. );
  142. bytes_left -= _read;
  143. _size -= _read;
  144. assert(_size <= _capacity);
  145. _read = _capacity;
  146. }
  147. // Read the remaining data.
  148. copy_data(data
  149. , &_data[_read - bytes_left]
  150. , bytes_left
  151. );
  152. _read -= bytes_left;
  153. _size -= bytes_left;
  154. assert(_size <= _capacity);
  155. }
  156. // Wrapper to avoid casting sizeof() manually.
  157. public void read_data(void* data, ulong size)
  158. {
  159. read_data_internal((uint8*)data, (uint32)size);
  160. }
  161. public void write_bool(bool a)
  162. {
  163. write_data(&a, sizeof(bool));
  164. }
  165. public void write_uint32(uint32 a)
  166. {
  167. write_data(&a, sizeof(uint32));
  168. }
  169. public void write_double(double a)
  170. {
  171. write_data(&a, sizeof(double));
  172. }
  173. public void write_string(string str)
  174. {
  175. uint32 len = str.length;
  176. write_data(&str.data[0], len);
  177. write_data(&len, sizeof(uint32));
  178. }
  179. public void write_guid(Guid a)
  180. {
  181. write_data(&a, sizeof(Guid));
  182. }
  183. public void write_vector3(Vector3 a)
  184. {
  185. write_data(&a, sizeof(Vector3));
  186. }
  187. public void write_quaternion(Quaternion a)
  188. {
  189. write_data(&a, sizeof(Quaternion));
  190. }
  191. public bool read_bool()
  192. {
  193. bool a = false;
  194. read_data(&a, sizeof(bool));
  195. return a;
  196. }
  197. public int read_int()
  198. {
  199. int a = 0;
  200. read_data(&a, sizeof(int));
  201. return a;
  202. }
  203. public uint32 read_uint32()
  204. {
  205. uint32 a = 0;
  206. read_data(&a, sizeof(uint32));
  207. return a;
  208. }
  209. public double read_double()
  210. {
  211. double a = 0;
  212. read_data(&a, sizeof(double));
  213. return a;
  214. }
  215. public Guid read_guid()
  216. {
  217. Guid a = GUID_ZERO;
  218. read_data(&a, sizeof(Guid));
  219. return a;
  220. }
  221. public Vector3 read_vector3()
  222. {
  223. Vector3 a = VECTOR3_ZERO;
  224. read_data(&a, sizeof(Vector3));
  225. return a;
  226. }
  227. public Quaternion read_quaternion()
  228. {
  229. Quaternion a = QUATERNION_IDENTITY;
  230. read_data(&a, sizeof(Quaternion));
  231. return a;
  232. }
  233. public string read_string()
  234. {
  235. uint32 len = 0;
  236. read_data(&len, sizeof(uint32));
  237. uint8[] str = new uint8[len + 1];
  238. read_data(str, len);
  239. str[len] = '\0';
  240. return (string)str;
  241. }
  242. public Resource read_resource()
  243. {
  244. string name = read_string();
  245. Resource resource = { name == "" ? null : name };
  246. return resource;
  247. }
  248. public void write_create_action(uint32 action, Guid id, string type)
  249. {
  250. write_string(type);
  251. write_guid(id);
  252. write_uint32(action);
  253. }
  254. public void write_destroy_action(uint32 action, Guid id, string type)
  255. {
  256. write_string(type);
  257. write_guid(id);
  258. write_uint32(action);
  259. }
  260. public void write_set_null_action(uint32 action, Guid id, string key)
  261. {
  262. // No value to push
  263. write_string(key);
  264. write_guid(id);
  265. write_uint32(action);
  266. }
  267. public void write_set_bool_action(uint32 action, Guid id, string key, bool val)
  268. {
  269. write_bool(val);
  270. write_string(key);
  271. write_guid(id);
  272. write_uint32(action);
  273. }
  274. public void write_set_double_action(uint32 action, Guid id, string key, double val)
  275. {
  276. write_double(val);
  277. write_string(key);
  278. write_guid(id);
  279. write_uint32(action);
  280. }
  281. public void write_set_string_action(uint32 action, Guid id, string key, string val)
  282. {
  283. write_string(val);
  284. write_string(key);
  285. write_guid(id);
  286. write_uint32(action);
  287. }
  288. public void write_set_vector3_action(uint32 action, Guid id, string key, Vector3 val)
  289. {
  290. write_vector3(val);
  291. write_string(key);
  292. write_guid(id);
  293. write_uint32(action);
  294. }
  295. public void write_set_quaternion_action(uint32 action, Guid id, string key, Quaternion val)
  296. {
  297. write_quaternion(val);
  298. write_string(key);
  299. write_guid(id);
  300. write_uint32(action);
  301. }
  302. public void write_set_resource_action(uint32 action, Guid id, string key, Resource val)
  303. {
  304. write_string(val.name == null ? "" : val.name);
  305. write_string(key);
  306. write_guid(id);
  307. write_uint32(action);
  308. }
  309. public void write_set_reference_action(uint32 action, Guid id, string key, Guid val)
  310. {
  311. write_guid(val);
  312. write_string(key);
  313. write_guid(id);
  314. write_uint32(action);
  315. }
  316. public void write_add_to_set_action(uint32 action, Guid id, string key, Guid item_id)
  317. {
  318. write_guid(item_id);
  319. write_string(key);
  320. write_guid(id);
  321. write_uint32(action);
  322. }
  323. public void write_remove_from_set_action(uint32 action, Guid id, string key, Guid item_id)
  324. {
  325. write_guid(item_id);
  326. write_string(key);
  327. write_guid(id);
  328. write_uint32(action);
  329. }
  330. public void write_restore_point(uint32 id, uint32 flags, Guid?[] data)
  331. {
  332. uint32 size = _last_write_restore_point_size;
  333. uint32 num_guids = data.length;
  334. for (uint32 i = 0; i < num_guids; ++i)
  335. write_guid(data[num_guids - 1 - i]);
  336. write_uint32(num_guids);
  337. write_uint32(size);
  338. write_uint32(flags);
  339. write_uint32(id);
  340. write_uint32(UndoRedoAction.RESTORE_POINT);
  341. _last_write_restore_point_size = 0;
  342. }
  343. public RestorePoint read_restore_point()
  344. {
  345. uint32 t = read_uint32();
  346. assert(t == UndoRedoAction.RESTORE_POINT);
  347. uint32 id = read_uint32();
  348. uint32 flags = read_uint32();
  349. uint32 size = read_uint32();
  350. uint32 num_guids = read_uint32();
  351. Guid?[] ids = new Guid?[num_guids];
  352. for (uint32 i = 0; i < num_guids; ++i)
  353. ids[i] = read_guid();
  354. RestorePointHeader rph = { id, flags, size, num_guids };
  355. return { rph, ids };
  356. }
  357. }
  358. public class UndoRedo
  359. {
  360. public Stack _undo;
  361. public Stack _redo;
  362. public int _distance_from_last_sync;
  363. ///
  364. public UndoRedo(uint32 undo_redo_size = 0)
  365. {
  366. uint32 size = uint32.max(1024, undo_redo_size);
  367. _undo = new Stack(size);
  368. _redo = new Stack(size);
  369. reset();
  370. }
  371. public void reset()
  372. {
  373. _undo.clear();
  374. _redo.clear();
  375. _distance_from_last_sync = 0;
  376. }
  377. }
  378. const string OBJECT_NAME_UNNAMED = "Unnamed";
  379. public enum ObjectTypeFlags
  380. {
  381. NONE = 0,
  382. UNIT_COMPONENT = 1 << 0,
  383. }
  384. public struct PropertiesSlice
  385. {
  386. int start; // Index of first property.
  387. int end; // Index of last property + 1.
  388. }
  389. public delegate void Aspect(out string name, Database database, Guid id);
  390. [Compact]
  391. public struct AspectData
  392. {
  393. public unowned Aspect callback;
  394. }
  395. public struct ObjectTypeInfo
  396. {
  397. PropertiesSlice properties;
  398. string name;
  399. string ui_name;
  400. double ui_order;
  401. ObjectTypeFlags flags;
  402. string? user_data;
  403. Gee.HashMap<StringId64?, AspectData?> aspects;
  404. }
  405. public class Database
  406. {
  407. public static bool _debug = false;
  408. public static bool _debug_getters = false;
  409. public enum Action
  410. {
  411. CREATE,
  412. DESTROY,
  413. SET_NULL,
  414. SET_BOOL,
  415. SET_DOUBLE,
  416. SET_STRING,
  417. SET_VECTOR3,
  418. SET_QUATERNION,
  419. SET_RESOURCE,
  420. SET_REFERENCE,
  421. ADD_TO_SET,
  422. REMOVE_FROM_SET
  423. }
  424. // Data
  425. private PropertyDefinition[] _property_definitions;
  426. public Gee.HashMap<StringId64?, ObjectTypeInfo?> _object_definitions;
  427. public Gee.HashMap<Guid?, Gee.HashMap<string, Value?>> _data;
  428. public UndoRedo? _undo_redo;
  429. public Project _project;
  430. // The number of changes to the database since the last successful state
  431. // synchronization (load(), save() etc.). If it is less than 0, the changes
  432. // came from undo(), otherwise they came from redo() or from regular calls to
  433. // create(), destroy(), set_*() etc. A value of 0 means there were no changes.
  434. // Signals
  435. public signal void object_type_added(ObjectTypeInfo info);
  436. public signal void objects_created(Guid?[] object_ids, uint32 flags);
  437. public signal void objects_destroyed(Guid?[] object_ids, uint32 flags);
  438. public signal void objects_changed(Guid?[] object_ids, uint32 flags);
  439. public Database(Project project, UndoRedo? undo_redo = null)
  440. {
  441. _property_definitions = new PropertyDefinition[0];
  442. _object_definitions = new Gee.HashMap<StringId64?, ObjectTypeInfo?>(StringId64.hash_func, StringId64.equal_func);
  443. _data = new Gee.HashMap<Guid?, Gee.HashMap<string, Value?>>(Guid.hash_func, Guid.equal_func);
  444. _project = project;
  445. _undo_redo = undo_redo;
  446. reset();
  447. }
  448. /// Resets database to clean state.
  449. public void reset()
  450. {
  451. _data.clear();
  452. if (_undo_redo != null)
  453. _undo_redo.reset();
  454. // This is a special field which stores all objects
  455. _data[GUID_ZERO] = new Gee.HashMap<string, Value?>();
  456. }
  457. /// Returns whether the database has been changed since last call to Save().
  458. public bool changed()
  459. {
  460. return _undo_redo != null
  461. ? _undo_redo._distance_from_last_sync != 0
  462. : false
  463. ;
  464. }
  465. /// Saves database to path without marking it as not changed.
  466. public int dump(string path, Guid id)
  467. {
  468. try {
  469. Hashtable json = encode(id);
  470. SJSON.save(json, path);
  471. return 0;
  472. } catch (JsonWriteError e) {
  473. return -1;
  474. }
  475. }
  476. /// Saves database to path.
  477. public int save(string path, Guid id)
  478. {
  479. int err = dump(path, id);
  480. if (err == 0) {
  481. if (_undo_redo != null)
  482. _undo_redo._distance_from_last_sync = 0;
  483. }
  484. return err;
  485. }
  486. public UndoRedo disable_undo()
  487. {
  488. var undo = _undo_redo;
  489. _undo_redo = null;
  490. return undo;
  491. }
  492. public void restore_undo(UndoRedo undo_redo)
  493. {
  494. _undo_redo = undo_redo;
  495. }
  496. // See: add_from_path().
  497. public int add_from_file(out Guid object_id, FileStream? fs, string resource_path)
  498. {
  499. UndoRedo undo_redo = disable_undo();
  500. try {
  501. Hashtable json = SJSON.load_from_file(fs);
  502. // Parse the object's ID or generate a new one if none is found.
  503. if (json.has_key("id"))
  504. object_id = Guid.parse((string)json["id"]);
  505. else if (json.has_key("_guid"))
  506. object_id = Guid.parse((string)json["_guid"]);
  507. else
  508. object_id = Guid.new_guid();
  509. string type = ResourceId.type(resource_path);
  510. StringId64 type_hash = StringId64(type);
  511. _data[object_id] = new Gee.HashMap<string, Value?>();
  512. set_type(object_id, type);
  513. set_owner(object_id, GUID_ZERO);
  514. set_alive(object_id, true);
  515. if (has_type(type_hash))
  516. _init_object(object_id, object_definition(type_hash));
  517. decode_object(object_id, GUID_ZERO, "", json);
  518. // Create a mapping between the path and the object it has been loaded into.
  519. set(0, GUID_ZERO, resource_path, object_id);
  520. restore_undo(undo_redo);
  521. return 0;
  522. } catch (JsonSyntaxError e) {
  523. object_id = GUID_ZERO;
  524. restore_undo(undo_redo);
  525. return -1;
  526. }
  527. }
  528. // Adds the object stored at @a path to the database.
  529. // This makes it possible to load multiple objects from distinct
  530. // paths in the same database. @a resource_path is used as a key in the
  531. // database to refer to the object that has been loaded. This is useful when
  532. // you do not have the object ID but only its path, as it is often the case
  533. // since resources use paths and not IDs to reference each other.
  534. public int add_from_path(out Guid object_id, string path, string resource_path)
  535. {
  536. object_id = GUID_ZERO;
  537. FileStream fs = FileStream.open(path, "rb");
  538. if (fs == null)
  539. return 1;
  540. return add_from_file(out object_id, fs, resource_path);
  541. }
  542. public int add_from_resource_path(out Guid object_id, string resource_path)
  543. {
  544. // If the resource is already loaded.
  545. if (has_property(GUID_ZERO, resource_path)) {
  546. object_id = get_reference(GUID_ZERO, resource_path);
  547. return 0;
  548. }
  549. string path = _project.absolute_path(resource_path);
  550. return add_from_path(out object_id, path, resource_path);
  551. }
  552. /// Loads the database with the object stored at @a path.
  553. public int load_from_file(out Guid object_id, FileStream fs, string resource_path)
  554. {
  555. reset();
  556. return add_from_file(out object_id, fs, resource_path);
  557. }
  558. /// Loads the database with the object stored at @a path.
  559. public int load_from_path(out Guid object_id, string path, string resource_path)
  560. {
  561. reset();
  562. return add_from_path(out object_id, path, resource_path);
  563. }
  564. /// Encodes the object @a id into SJSON object.
  565. public Hashtable encode(Guid id)
  566. {
  567. return encode_object(id, get_data(id));
  568. }
  569. public static bool is_valid_value(Value? value)
  570. {
  571. return value == null
  572. || value.holds(typeof(bool))
  573. || value.holds(typeof(double))
  574. || value.holds(typeof(string))
  575. || value.holds(typeof(Vector3))
  576. || value.holds(typeof(Quaternion))
  577. || value.holds(typeof(Resource))
  578. || value.holds(typeof(Guid))
  579. ;
  580. }
  581. public static bool is_valid_key(string key)
  582. {
  583. return key.length > 0
  584. && !key.has_prefix(".")
  585. && !key.has_suffix(".")
  586. ;
  587. }
  588. public static string debug_string(Value? value)
  589. {
  590. if (value == null)
  591. return "null";
  592. if (value.holds(typeof(bool)))
  593. return ((bool)value).to_string();
  594. if (value.holds(typeof(double)))
  595. return ((double)value).to_string();
  596. if (value.holds(typeof(string)))
  597. return ((string)value).to_string();
  598. if (value.holds(typeof(Vector3)))
  599. return ((Vector3)value).to_string();
  600. if (value.holds(typeof(Quaternion)))
  601. return ((Quaternion)value).to_string();
  602. if (value.holds(typeof(Resource)))
  603. return ((Resource)value).name == null ? "(None)" : ((Resource)value).name;
  604. if (value.holds(typeof(Guid)))
  605. return ((Guid)value).to_debug_string();
  606. if (value.holds(typeof(Gee.HashSet)))
  607. return "Set<Guid>";
  608. return "<invalid>";
  609. }
  610. public void decode_object_compat(Guid id, Guid owner_id, string db_key, Hashtable json)
  611. {
  612. string old_db = db_key;
  613. string k = db_key;
  614. string[] keys = json.keys.to_array();
  615. foreach (string key in keys) {
  616. // ID is filled by decode_set().
  617. if (key == "id"
  618. || key == "_guid"
  619. || key == "_alive"
  620. || key == "prefab"
  621. )
  622. continue;
  623. Value? val = json[key];
  624. k += k == "" ? key : ("." + key);
  625. if (val.holds(typeof(Hashtable))) {
  626. Hashtable ht = (Hashtable)val;
  627. decode_object(id, owner_id, k, ht);
  628. } else if (val.holds(typeof(Gee.ArrayList))) {
  629. Gee.ArrayList<Value?> arr = (Gee.ArrayList<Value?>)val;
  630. if (arr.size > 0
  631. && arr[0].holds(typeof(double))
  632. && k != "frames" // sprite_animation
  633. )
  634. set(0, id, k, decode_value(val));
  635. else
  636. decode_set(id, key, arr);
  637. } else {
  638. set(0, id, k, decode_value(val));
  639. }
  640. k = old_db;
  641. }
  642. }
  643. public void decode_object_from_properties(Guid id, Guid owner_id, PropertyDefinition[]? properties, Hashtable json)
  644. {
  645. foreach (PropertyDefinition def in properties) {
  646. // Find table and key to read from.
  647. string[] keys = def.name.split(".");
  648. string key = keys[keys.length - 1];
  649. Hashtable input = json;
  650. if (keys.length > 1) {
  651. for (int i = 0; i < keys.length - 1; ++i) {
  652. string f = keys[i];
  653. if (input.has_key(f)) {
  654. input = (Hashtable)input[f];
  655. continue;
  656. }
  657. }
  658. }
  659. if (!input.has_key(key))
  660. continue;
  661. // Read property.
  662. if (def.type == PropertyType.OBJECTS_SET) {
  663. decode_set(id, def.name, (Gee.ArrayList<Value?>)input[key]);
  664. } else if (def.type == PropertyType.RESOURCE) {
  665. Resource res = { null };
  666. if (input.has_key(key)) {
  667. Value? val = input[key];
  668. if (val.holds(typeof(string)))
  669. res.name = (string)input[key];
  670. }
  671. set(0, id, def.name, res);
  672. } else {
  673. set(0, id, def.name, decode_value(input[key]));
  674. }
  675. }
  676. }
  677. public void decode_object(Guid id, Guid owner_id, string db_key, Hashtable json)
  678. {
  679. string? type = null;
  680. // The "type" key defines object type only if it appears
  681. // in the root of a JSON object (k == "").
  682. if (db_key == "" && !has_property(id, "_type")) {
  683. if (json.has_key("_type"))
  684. type = (string)json["_type"];
  685. else if (json.has_key("type"))
  686. type = (string)json["type"];
  687. assert(type != null);
  688. set_type(id, type);
  689. StringId64 type_hash = StringId64(type);
  690. if (has_type(type_hash))
  691. _init_object(id, object_definition(type_hash));
  692. } else {
  693. type = object_type(id);
  694. }
  695. assert(type != null);
  696. PropertyDefinition[]? properties = object_definition(StringId64(type));
  697. decode_object_from_properties(id, owner_id, properties, json);
  698. if (type == OBJECT_TYPE_UNIT)
  699. decode_object_compat(id, owner_id, db_key, json);
  700. }
  701. public void decode_set(Guid owner_id, string key, Gee.ArrayList<Value?> json)
  702. {
  703. // Set should be created even if it is empty.
  704. create_empty_set(owner_id, key);
  705. for (int i = 0; i < json.size; ++i) {
  706. Hashtable obj;
  707. string owner_type = object_type(owner_id);
  708. obj = (Hashtable)json[i];
  709. // Decode object ID.
  710. Guid obj_id;
  711. if (obj.has_key("id") && owner_type != OBJECT_TYPE_FONT)
  712. obj_id = Guid.parse((string)obj["id"]);
  713. else if (obj.has_key("_guid"))
  714. obj_id = Guid.parse((string)obj["_guid"]);
  715. else
  716. obj_id = Guid.new_guid();
  717. _data[obj_id] = new Gee.HashMap<string, Value?>();
  718. set_owner(obj_id, owner_id);
  719. set_alive(obj_id, true);
  720. decode_object(obj_id, owner_id, "", obj);
  721. assert(has_property(obj_id, "_type"));
  722. add_to_set_internal(0, owner_id, key, obj_id);
  723. }
  724. }
  725. public Value? decode_value(Value? value)
  726. {
  727. if (value.holds(typeof(Gee.ArrayList))) {
  728. Gee.ArrayList<Value?> al = (Gee.ArrayList<Value?>)value;
  729. if (al.size == 1)
  730. return Vector3((double)al[0], 0.0, 0.0);
  731. else if (al.size == 2)
  732. return Vector3((double)al[0], (double)al[1], 0.0);
  733. else if (al.size == 3)
  734. return Vector3((double)al[0], (double)al[1], (double)al[2]);
  735. else if (al.size == 4)
  736. return Quaternion((double)al[0], (double)al[1], (double)al[2], (double)al[3]);
  737. else
  738. return Vector3(0.0, 0.0, 0.0);
  739. } else if (value.holds(typeof(string))) {
  740. Guid id;
  741. if (Guid.try_parse((string)value, out id))
  742. return id;
  743. return value;
  744. } else if (value == null
  745. || value.holds(typeof(bool))
  746. || value.holds(typeof(double))) {
  747. return value;
  748. } else {
  749. return null;
  750. }
  751. }
  752. public Hashtable encode_object_compat(Guid id, Gee.HashMap<string, Value?> db)
  753. {
  754. Hashtable obj = new Hashtable();
  755. if (id != GUID_ZERO)
  756. obj["_guid"] = id.to_string();
  757. string[] keys = db.keys.to_array();
  758. foreach (string key in keys) {
  759. // Since null-key is equivalent to non-existent key, skip serialization.
  760. if (db[key] == null || key == "_owner" || key == "_alive")
  761. continue;
  762. string[] foo = key.split(".");
  763. Hashtable x = obj;
  764. if (foo.length > 1) {
  765. for (int i = 0; i < foo.length - 1; ++i) {
  766. string f = foo[i];
  767. if (x.has_key(f)) {
  768. x = (Hashtable)x[f];
  769. continue;
  770. }
  771. Hashtable y = new Hashtable();
  772. x.set(f, y);
  773. x = y;
  774. }
  775. }
  776. x.set(foo[foo.length - 1], encode_value(db[key]));
  777. }
  778. return obj;
  779. }
  780. public Hashtable encode_object(Guid id, Gee.HashMap<string, Value?> db)
  781. {
  782. assert(is_alive(id));
  783. string type = object_type(id);
  784. PropertyDefinition[]? properties = object_definition(StringId64(type));
  785. if (type == OBJECT_TYPE_UNIT || properties == null)
  786. return encode_object_compat(id, db);
  787. Hashtable obj = new Hashtable();
  788. if (id != GUID_ZERO) {
  789. obj["_guid"] = id.to_string();
  790. obj["_type"] = type;
  791. }
  792. foreach (PropertyDefinition def in properties) {
  793. if (def.not_serialized)
  794. continue;
  795. // Since null-key is equivalent to non-existent key, skip serialization.
  796. if (db[def.name] == null)
  797. continue;
  798. string[] foo = def.name.split(".");
  799. Hashtable x = obj;
  800. if (foo.length > 1) {
  801. for (int i = 0; i < foo.length - 1; ++i) {
  802. string f = foo[i];
  803. if (x.has_key(f)) {
  804. x = (Hashtable)x[f];
  805. continue;
  806. }
  807. Hashtable y = new Hashtable();
  808. x.set(f, y);
  809. x = y;
  810. }
  811. }
  812. x.set(foo[foo.length - 1], encode_value(db[def.name]));
  813. }
  814. return obj;
  815. }
  816. public Value? encode_value(Value? value)
  817. {
  818. assert(is_valid_value(value) || value.holds(typeof(Gee.HashSet)));
  819. if (value.holds(typeof(Vector3))) {
  820. Vector3 v = (Vector3)value;
  821. Gee.ArrayList<Value?> arr = new Gee.ArrayList<Value?>();
  822. arr.add(v.x);
  823. arr.add(v.y);
  824. arr.add(v.z);
  825. return arr;
  826. } else if (value.holds(typeof(Quaternion))) {
  827. Quaternion q = (Quaternion)value;
  828. Gee.ArrayList<Value?> arr = new Gee.ArrayList<Value?>();
  829. arr.add(q.x);
  830. arr.add(q.y);
  831. arr.add(q.z);
  832. arr.add(q.w);
  833. return arr;
  834. } else if (value.holds(typeof(Resource))) {
  835. Resource res = (Resource)value;
  836. if (res.name == null)
  837. return null;
  838. else
  839. return res.name;
  840. } else if (value.holds(typeof(Guid))) {
  841. Guid id = (Guid)value;
  842. return id.to_string();
  843. } else if (value.holds(typeof(Gee.HashSet))) {
  844. Gee.HashSet<Guid?> hs = (Gee.HashSet<Guid?>)value;
  845. Gee.ArrayList<Value?> arr = new Gee.ArrayList<Value?>();
  846. foreach (Guid id in hs) {
  847. if (!is_alive(id))
  848. continue;
  849. arr.add(encode_object(id, get_data(id)));
  850. }
  851. return arr;
  852. } else {
  853. return value;
  854. }
  855. }
  856. public Gee.HashMap<string, Value?> get_data(Guid id)
  857. {
  858. assert(has_object(id));
  859. return _data[id];
  860. }
  861. public void set(int dir, Guid id, string key, Value? value)
  862. {
  863. assert(has_object(id));
  864. assert(is_valid_key(key));
  865. assert(is_valid_value(value));
  866. if (_debug)
  867. logi("set_property %s %s %s".printf(debug_string(id), key, debug_string(value)));
  868. Gee.HashMap<string, Value?> ob = get_data(id);
  869. ob[key] = value;
  870. if (_undo_redo != null)
  871. _undo_redo._distance_from_last_sync += dir;
  872. }
  873. public void create_empty_set(Guid id, string key)
  874. {
  875. assert(has_object(id));
  876. assert(is_valid_key(key));
  877. Gee.HashMap<string, Value?> ob = get_data(id);
  878. ob[key] = new Gee.HashSet<Guid?>(Guid.hash_func, Guid.equal_func);
  879. }
  880. public void add_to_set_internal(int dir, Guid id, string key, Guid item_id)
  881. {
  882. assert(has_object(id));
  883. assert(is_valid_key(key));
  884. assert(item_id != GUID_ZERO);
  885. assert(has_object(item_id));
  886. if (_debug)
  887. logi("add_to_set %s %s %s".printf(debug_string(id), key, debug_string(item_id)));
  888. Gee.HashMap<string, Value?> ob = get_data(id);
  889. if (!ob.has_key(key)) {
  890. Gee.HashSet<Guid?> hs = new Gee.HashSet<Guid?>(Guid.hash_func, Guid.equal_func);
  891. hs.add(item_id);
  892. ob[key] = hs;
  893. } else {
  894. ((Gee.HashSet<Guid?>)ob[key]).add(item_id);
  895. }
  896. get_data(item_id)["_owner"] = id;
  897. if (_undo_redo != null)
  898. _undo_redo._distance_from_last_sync += dir;
  899. }
  900. public void remove_from_set_internal(int dir, Guid id, string key, Guid item_id)
  901. {
  902. assert(has_object(id));
  903. assert(is_valid_key(key));
  904. assert(item_id != GUID_ZERO);
  905. if (_debug)
  906. logi("remove_from_set %s %s %s".printf(debug_string(id), key, debug_string(item_id)));
  907. Gee.HashMap<string, Value?> ob = get_data(id);
  908. ((Gee.HashSet<Guid?>)ob[key]).remove(item_id);
  909. set_owner(id, GUID_ZERO);
  910. if (_undo_redo != null)
  911. _undo_redo._distance_from_last_sync += dir;
  912. }
  913. // Returns the type of the object @a id.
  914. public string object_type(Guid id)
  915. {
  916. assert(has_object(id));
  917. if (id == GUID_ZERO)
  918. return "database";
  919. else
  920. return (string)get_data(id)["_type"];
  921. }
  922. // Returns the owner of @a id.
  923. public Guid owner(Guid id)
  924. {
  925. assert(has_object(id));
  926. return (Guid)get_data(id)["_owner"];
  927. }
  928. // Sets the @a type of the object @a id.
  929. // This is called automatically when loading data or when new objects are created via create().
  930. // It can occasionally be called manually after loading legacy data with no type information
  931. // stored inside objects.
  932. public void set_type(Guid id, string type)
  933. {
  934. assert(has_object(id));
  935. get_data(id)["_type"] = type;
  936. }
  937. public void set_owner(Guid id, Guid owner_id)
  938. {
  939. assert(has_object(id));
  940. assert(has_object(owner_id));
  941. get_data(id)["_owner"] = owner_id;
  942. }
  943. public void set_alive(Guid id, bool alive)
  944. {
  945. assert(has_object(id));
  946. get_data(id)["_alive"] = alive;
  947. }
  948. public bool is_alive(Guid id)
  949. {
  950. assert(has_object(id));
  951. return id == GUID_ZERO || (bool)get_data(id)["_alive"];
  952. }
  953. public void _init_object(Guid id, PropertyDefinition[] properties)
  954. {
  955. foreach (PropertyDefinition def in properties) {
  956. switch (def.type) {
  957. case PropertyType.BOOL:
  958. set_bool(id, def.name, (bool)def.deffault);
  959. break;
  960. case PropertyType.DOUBLE:
  961. set_double(id, def.name, (double)def.deffault);
  962. break;
  963. case PropertyType.STRING:
  964. set_string(id, def.name, (string)def.deffault);
  965. break;
  966. case PropertyType.VECTOR3:
  967. set_vector3(id, def.name, (Vector3)def.deffault);
  968. break;
  969. case PropertyType.QUATERNION:
  970. set_quaternion(id, def.name, (Quaternion)def.deffault);
  971. break;
  972. case PropertyType.RESOURCE:
  973. set_resource(id, def.name, (string?)def.deffault);
  974. break;
  975. case PropertyType.REFERENCE:
  976. set_reference(id, def.name, (Guid)def.deffault);
  977. break;
  978. case PropertyType.OBJECTS_SET:
  979. create_empty_set(id, def.name);
  980. break;
  981. default:
  982. assert(false);
  983. break;
  984. }
  985. }
  986. }
  987. public void create(Guid id, string type)
  988. {
  989. assert(id != GUID_ZERO);
  990. assert(!has_object(id));
  991. if (_debug)
  992. logi("create %s".printf(debug_string(id)));
  993. if (_undo_redo != null) {
  994. _undo_redo._distance_from_last_sync += 1;
  995. _undo_redo._undo.write_destroy_action(Action.DESTROY, id, type);
  996. _undo_redo._redo.clear();
  997. }
  998. _data[id] = new Gee.HashMap<string, Value?>();
  999. set_alive(id, true);
  1000. set_type(id, type);
  1001. StringId64 type_hash = StringId64(type);
  1002. if (has_type(type_hash))
  1003. _init_object(id, object_definition(type_hash));
  1004. }
  1005. public void destroy(Guid id)
  1006. {
  1007. assert(id != GUID_ZERO);
  1008. assert(has_object(id));
  1009. string obj_type = object_type(id);
  1010. Gee.HashMap<string, Value?> o = get_data(id);
  1011. string[] keys = o.keys.to_array();
  1012. foreach (string key in keys) {
  1013. Value? value = o[key];
  1014. if (value.holds(typeof(Gee.HashSet))) {
  1015. Gee.HashSet<Guid?> hs = (Gee.HashSet<Guid?>)value;
  1016. Guid?[] ids = hs.to_array();
  1017. foreach (Guid item_id in ids) {
  1018. if (is_alive(item_id))
  1019. destroy(item_id);
  1020. }
  1021. }
  1022. }
  1023. set_alive(id, false);
  1024. if (_undo_redo != null) {
  1025. _undo_redo._distance_from_last_sync += 1;
  1026. _undo_redo._undo.write_create_action(Action.CREATE, id, obj_type);
  1027. _undo_redo._redo.clear();
  1028. }
  1029. if (_debug)
  1030. logi("destroy %s".printf(debug_string(id)));
  1031. }
  1032. public void set_null(Guid id, string key)
  1033. {
  1034. assert(has_object(id));
  1035. assert(is_valid_key(key));
  1036. assert(is_valid_value(null));
  1037. if (_undo_redo != null) {
  1038. Gee.HashMap<string, Value?> ob = get_data(id);
  1039. if (ob.has_key(key) && ob[key] != null) {
  1040. if (ob[key].holds(typeof(bool)))
  1041. _undo_redo._undo.write_set_bool_action(Action.SET_BOOL, id, key, (bool)ob[key]);
  1042. if (ob[key].holds(typeof(double)))
  1043. _undo_redo._undo.write_set_double_action(Action.SET_DOUBLE, id, key, (double)ob[key]);
  1044. if (ob[key].holds(typeof(string)))
  1045. _undo_redo._undo.write_set_string_action(Action.SET_STRING, id, key, (string)ob[key]);
  1046. if (ob[key].holds(typeof(Vector3)))
  1047. _undo_redo._undo.write_set_vector3_action(Action.SET_VECTOR3, id, key, (Vector3)ob[key]);
  1048. if (ob[key].holds(typeof(Quaternion)))
  1049. _undo_redo._undo.write_set_quaternion_action(Action.SET_QUATERNION, id, key, (Quaternion)ob[key]);
  1050. if (ob[key].holds(typeof(Resource)))
  1051. _undo_redo._undo.write_set_resource_action(Action.SET_RESOURCE, id, key, (Resource)ob[key]);
  1052. if (ob[key].holds(typeof(Guid)))
  1053. _undo_redo._undo.write_set_reference_action(Action.SET_REFERENCE, id, key, (Guid)ob[key]);
  1054. } else {
  1055. _undo_redo._undo.write_set_null_action(Action.SET_NULL, id, key);
  1056. }
  1057. _undo_redo._redo.clear();
  1058. }
  1059. set(1, id, key, null);
  1060. }
  1061. public void set_bool(Guid id, string key, bool val)
  1062. {
  1063. assert(has_object(id));
  1064. assert(is_valid_key(key));
  1065. assert(is_valid_value(val));
  1066. if (_undo_redo != null) {
  1067. Gee.HashMap<string, Value?> ob = get_data(id);
  1068. if (ob.has_key(key) && ob[key] != null)
  1069. _undo_redo._undo.write_set_bool_action(Action.SET_BOOL, id, key, (bool)ob[key]);
  1070. else
  1071. _undo_redo._undo.write_set_null_action(Action.SET_NULL, id, key);
  1072. _undo_redo._redo.clear();
  1073. }
  1074. set(1, id, key, val);
  1075. }
  1076. public void set_double(Guid id, string key, double val)
  1077. {
  1078. assert(has_object(id));
  1079. assert(is_valid_key(key));
  1080. assert(is_valid_value(val));
  1081. if (_undo_redo != null) {
  1082. Gee.HashMap<string, Value?> ob = get_data(id);
  1083. if (ob.has_key(key) && ob[key] != null)
  1084. _undo_redo._undo.write_set_double_action(Action.SET_DOUBLE, id, key, (double)ob[key]);
  1085. else
  1086. _undo_redo._undo.write_set_null_action(Action.SET_NULL, id, key);
  1087. _undo_redo._redo.clear();
  1088. }
  1089. set(1, id, key, val);
  1090. }
  1091. public void set_string(Guid id, string key, string val)
  1092. {
  1093. assert(has_object(id));
  1094. assert(is_valid_key(key));
  1095. assert(is_valid_value(val));
  1096. if (_undo_redo != null) {
  1097. Gee.HashMap<string, Value?> ob = get_data(id);
  1098. if (ob.has_key(key) && ob[key] != null)
  1099. _undo_redo._undo.write_set_string_action(Action.SET_STRING, id, key, (string)ob[key]);
  1100. else
  1101. _undo_redo._undo.write_set_null_action(Action.SET_NULL, id, key);
  1102. _undo_redo._redo.clear();
  1103. }
  1104. set(1, id, key, val);
  1105. }
  1106. public void set_vector3(Guid id, string key, Vector3 val)
  1107. {
  1108. assert(has_object(id));
  1109. assert(is_valid_key(key));
  1110. assert(is_valid_value(val));
  1111. if (_undo_redo != null) {
  1112. Gee.HashMap<string, Value?> ob = get_data(id);
  1113. if (ob.has_key(key) && ob[key] != null)
  1114. _undo_redo._undo.write_set_vector3_action(Action.SET_VECTOR3, id, key, (Vector3)ob[key]);
  1115. else
  1116. _undo_redo._undo.write_set_null_action(Action.SET_NULL, id, key);
  1117. _undo_redo._redo.clear();
  1118. }
  1119. set(1, id, key, val);
  1120. }
  1121. public void set_quaternion(Guid id, string key, Quaternion val)
  1122. {
  1123. assert(has_object(id));
  1124. assert(is_valid_key(key));
  1125. assert(is_valid_value(val));
  1126. if (_undo_redo != null) {
  1127. Gee.HashMap<string, Value?> ob = get_data(id);
  1128. if (ob.has_key(key) && ob[key] != null)
  1129. _undo_redo._undo.write_set_quaternion_action(Action.SET_QUATERNION, id, key, (Quaternion)ob[key]);
  1130. else
  1131. _undo_redo._undo.write_set_null_action(Action.SET_NULL, id, key);
  1132. _undo_redo._redo.clear();
  1133. }
  1134. set(1, id, key, val);
  1135. }
  1136. public void set_resource(Guid id, string key, string? val)
  1137. {
  1138. assert(has_object(id));
  1139. assert(is_valid_key(key));
  1140. assert(is_valid_value(val));
  1141. if (_undo_redo != null) {
  1142. Gee.HashMap<string, Value?> ob = get_data(id);
  1143. if (ob.has_key(key) && ob[key] != null)
  1144. _undo_redo._undo.write_set_resource_action(Action.SET_RESOURCE, id, key, { ((Resource)ob[key]).name });
  1145. else
  1146. _undo_redo._undo.write_set_null_action(Action.SET_NULL, id, key);
  1147. _undo_redo._redo.clear();
  1148. }
  1149. Resource res = { val };
  1150. set(1, id, key, res);
  1151. }
  1152. public void set_reference(Guid id, string key, Guid val)
  1153. {
  1154. assert(has_object(id));
  1155. assert(is_valid_key(key));
  1156. assert(is_valid_value(val));
  1157. if (_undo_redo != null) {
  1158. Gee.HashMap<string, Value?> ob = get_data(id);
  1159. if (ob.has_key(key) && ob[key] != null)
  1160. _undo_redo._undo.write_set_reference_action(Action.SET_REFERENCE, id, key, (Guid)ob[key]);
  1161. else
  1162. _undo_redo._undo.write_set_null_action(Action.SET_NULL, id, key);
  1163. _undo_redo._redo.clear();
  1164. }
  1165. set(1, id, key, val);
  1166. }
  1167. public void set_property(Guid id, string key, Value? val)
  1168. {
  1169. if (val == null)
  1170. set_null(id, key);
  1171. if (val.holds(typeof(bool)))
  1172. set_bool(id, key, (bool)val);
  1173. else if (val.holds(typeof(double)))
  1174. set_double(id, key, (double)val);
  1175. else if (val.holds(typeof(string)))
  1176. set_string(id, key, (string)val);
  1177. else if (val.holds(typeof(Vector3)))
  1178. set_vector3(id, key, (Vector3)val);
  1179. else if (val.holds(typeof(Quaternion)))
  1180. set_quaternion(id, key, (Quaternion)val);
  1181. else if (val.holds(typeof(Resource)))
  1182. set_resource(id, key, ((Resource)val).name);
  1183. else if (val.holds(typeof(Guid)))
  1184. set_reference(id, key, (Guid)val);
  1185. else
  1186. assert(false);
  1187. }
  1188. public void add_to_set(Guid id, string key, Guid item_id)
  1189. {
  1190. assert(has_object(id));
  1191. assert(is_valid_key(key));
  1192. assert(item_id != GUID_ZERO);
  1193. assert(has_object(item_id));
  1194. if (_undo_redo != null) {
  1195. _undo_redo._undo.write_remove_from_set_action(Action.REMOVE_FROM_SET, id, key, item_id);
  1196. _undo_redo._redo.clear();
  1197. }
  1198. add_to_set_internal(1, id, key, item_id);
  1199. }
  1200. public void remove_from_set(Guid id, string key, Guid item_id)
  1201. {
  1202. assert(has_object(id));
  1203. assert(is_valid_key(key));
  1204. assert(item_id != GUID_ZERO);
  1205. if (_undo_redo != null) {
  1206. _undo_redo._undo.write_add_to_set_action(Action.ADD_TO_SET, id, key, item_id);
  1207. _undo_redo._redo.clear();
  1208. }
  1209. remove_from_set_internal(1, id, key, item_id);
  1210. }
  1211. public bool has_object(Guid id)
  1212. {
  1213. return id == GUID_ZERO || _data.has_key(id);
  1214. }
  1215. public bool has_property(Guid id, string key)
  1216. {
  1217. return get_property(id, key) != null;
  1218. }
  1219. public Value? get_property(Guid id, string key, Value? val = null)
  1220. {
  1221. assert(has_object(id));
  1222. assert(is_valid_key(key));
  1223. Gee.HashMap<string, Value?> ob = get_data(id);
  1224. Value? value = (ob.has_key(key) ? ob[key] : val);
  1225. if (_debug_getters)
  1226. logi("get_property %s %s %s".printf(debug_string(id), key, debug_string(value)));
  1227. return value;
  1228. }
  1229. public bool get_bool(Guid id, string key, bool deffault = false)
  1230. {
  1231. return (bool)get_property(id, key, deffault);
  1232. }
  1233. public double get_double(Guid id, string key, double deffault = 0.0)
  1234. {
  1235. return (double)get_property(id, key, deffault);
  1236. }
  1237. public string get_string(Guid id, string key, string deffault = "")
  1238. {
  1239. return (string)get_property(id, key, deffault);
  1240. }
  1241. public Vector3 get_vector3(Guid id, string key, Vector3 deffault = VECTOR3_ZERO)
  1242. {
  1243. return (Vector3)get_property(id, key, deffault);
  1244. }
  1245. public Quaternion get_quaternion(Guid id, string key, Quaternion deffault = QUATERNION_IDENTITY)
  1246. {
  1247. return (Quaternion)get_property(id, key, deffault);
  1248. }
  1249. public string? get_resource(Guid id, string key, string? deffault = null)
  1250. {
  1251. Resource deffault_res = { deffault };
  1252. Value? val = get_property(id, key, deffault_res);
  1253. assert(val == null || val.holds(typeof(Crown.Resource)));
  1254. return ((Resource)val).name;
  1255. }
  1256. public Guid get_reference(Guid id, string key, Guid deffault = GUID_ZERO)
  1257. {
  1258. return (Guid)get_property(id, key, deffault);
  1259. }
  1260. public Gee.HashSet<Guid?> get_set(Guid id, string key, Gee.HashSet<Guid?> deffault = new Gee.HashSet<Guid?>(Guid.hash_func, Guid.equal_func))
  1261. {
  1262. assert(has_object(id));
  1263. assert(is_valid_key(key));
  1264. Gee.HashMap<string, Value?> ob = get_data(id);
  1265. Gee.HashSet<Guid?> value;
  1266. if (ob.has_key(key)) {
  1267. Gee.HashSet<Guid?> objects = (Gee.HashSet<Guid?>)ob[key];
  1268. value = new Gee.HashSet<Guid?>(Guid.hash_func, Guid.equal_func);
  1269. foreach (var obj in objects) {
  1270. if (is_alive(obj))
  1271. value.add(obj);
  1272. }
  1273. } else {
  1274. value = deffault;
  1275. }
  1276. if (_debug_getters)
  1277. logi("get_property %s %s %s".printf(debug_string(id), key, debug_string(value)));
  1278. return value;
  1279. }
  1280. public Gee.HashMap<string, Value?> get_object(Guid id)
  1281. {
  1282. return (Gee.HashMap<string, Value?>)get_data(GUID_ZERO)[id.to_string()];
  1283. }
  1284. public string[] get_keys(Guid id)
  1285. {
  1286. Gee.HashMap<string, Value?> data = get_data(id);
  1287. return data.keys.to_array();
  1288. }
  1289. public void add_restore_point(int id, Guid?[] data, uint32 flags = 0u)
  1290. {
  1291. if (_debug)
  1292. logi("add_restore_point %d, undo size = %u".printf(id, _undo_redo != null ? _undo_redo._undo.size() : 0));
  1293. if (_undo_redo != null) {
  1294. _undo_redo._undo.write_restore_point(id, flags, data);
  1295. _undo_redo._redo.clear();
  1296. }
  1297. switch (id) {
  1298. case ActionType.CREATE_OBJECTS:
  1299. objects_created(data, flags);
  1300. break;
  1301. case ActionType.DESTROY_OBJECTS:
  1302. objects_destroyed(data, flags);
  1303. break;
  1304. case ActionType.CHANGE_OBJECTS:
  1305. objects_changed(data, flags);
  1306. break;
  1307. default:
  1308. logw("Unknown action type %d".printf(id));
  1309. break;
  1310. }
  1311. }
  1312. /// Duplicates the object specified by id and assign new_id to the duplicated object.
  1313. public void duplicate(Guid id, Guid new_id, Database? dest = null)
  1314. {
  1315. assert(id != GUID_ZERO);
  1316. assert(new_id != GUID_ZERO);
  1317. assert(id != new_id);
  1318. assert(has_object(id));
  1319. if (dest == null)
  1320. dest = this;
  1321. dest.create(new_id, object_type(id));
  1322. Gee.HashMap<string, Value?> ob = get_data(id);
  1323. string[] keys = ob.keys.to_array();
  1324. foreach (string key in keys) {
  1325. Value? val = ob[key];
  1326. if (val.holds(typeof(Gee.HashSet))) {
  1327. Gee.HashSet<Guid?> hs = (Gee.HashSet<Guid?>)val;
  1328. foreach (Guid j in hs) {
  1329. Guid x = Guid.new_guid();
  1330. duplicate(j, x, dest);
  1331. dest.add_to_set(new_id, key, x);
  1332. }
  1333. } else {
  1334. if (ob[key] == null)
  1335. dest.set_null(new_id, key);
  1336. else if (ob[key].holds(typeof(bool)))
  1337. dest.set_bool(new_id, key, (bool)ob[key]);
  1338. else if (ob[key].holds(typeof(double)))
  1339. dest.set_double(new_id, key, (double)ob[key]);
  1340. else if (ob[key].holds(typeof(string)))
  1341. dest.set_string(new_id, key, (string)ob[key]);
  1342. else if (ob[key].holds(typeof(Vector3)))
  1343. dest.set_vector3(new_id, key, (Vector3)ob[key]);
  1344. else if (ob[key].holds(typeof(Quaternion)))
  1345. dest.set_quaternion(new_id, key, (Quaternion)ob[key]);
  1346. else if (ob[key].holds(typeof(Resource)))
  1347. dest.set_resource(new_id, key, ((Resource)ob[key]).name);
  1348. else if (ob[key].holds(typeof(Guid)))
  1349. dest.set_reference(new_id, key, (Guid)ob[key]);
  1350. else
  1351. assert(false);
  1352. }
  1353. }
  1354. }
  1355. public void duplicate_and_add_to_set(Guid id, Guid new_id)
  1356. {
  1357. duplicate(id, new_id);
  1358. Guid owner_id = owner(id);
  1359. if (owner_id == GUID_ZERO)
  1360. return;
  1361. PropertyDefinition[]? properties = object_definition(StringId64(object_type(owner_id)));
  1362. foreach (PropertyDefinition def in properties) {
  1363. if (def.type != PropertyType.OBJECTS_SET)
  1364. continue;
  1365. Gee.HashSet<Guid?> objects = get_set(owner_id, def.name);
  1366. if (objects.contains(id)) {
  1367. add_to_set(owner_id, def.name, new_id);
  1368. break;
  1369. }
  1370. }
  1371. }
  1372. /// Copies the database to db under the given new_key.
  1373. public void copy_to(Database db, string new_key)
  1374. {
  1375. assert(db != null);
  1376. assert(is_valid_key(new_key));
  1377. copy_deep(db, GUID_ZERO, new_key);
  1378. }
  1379. public void copy_deep(Database db, Guid id, string new_key)
  1380. {
  1381. Gee.HashMap<string, Value?> ob = get_data(id);
  1382. string[] keys = ob.keys.to_array();
  1383. foreach (string key in keys) {
  1384. Value? value = ob[key];
  1385. if (value.holds(typeof(Gee.HashSet))) {
  1386. Gee.HashSet<Guid?> hs = (Gee.HashSet<Guid?>)value;
  1387. foreach (Guid j in hs) {
  1388. db.create(j, object_type(j));
  1389. copy_deep(db, j, "");
  1390. db.add_to_set(id, new_key + (new_key == "" ? "" : ".") + key, j);
  1391. }
  1392. } else {
  1393. if (!db.has_object(id))
  1394. db.create(id, object_type(id));
  1395. string kk = new_key + (new_key == "" ? "" : ".") + key;
  1396. if (ob[key] == null)
  1397. db.set_null(id, kk);
  1398. if (ob[key].holds(typeof(bool)))
  1399. db.set_bool(id, kk, (bool)ob[key]);
  1400. if (ob[key].holds(typeof(double)))
  1401. db.set_double(id, kk, (double)ob[key]);
  1402. if (ob[key].holds(typeof(string)))
  1403. db.set_string(id, kk, (string)ob[key]);
  1404. if (ob[key].holds(typeof(Vector3)))
  1405. db.set_vector3(id, kk, (Vector3)ob[key]);
  1406. if (ob[key].holds(typeof(Quaternion)))
  1407. db.set_quaternion(id, kk, (Quaternion)ob[key]);
  1408. if (ob[key].holds(typeof(Resource)))
  1409. db.set_resource(id, kk, ((Resource)ob[key]).name);
  1410. if (ob[key].holds(typeof(Guid)))
  1411. db.set_reference(id, kk, (Guid)ob[key]);
  1412. }
  1413. }
  1414. }
  1415. // Tries to read a restore point @a rp from the @a stack and returns
  1416. // 0 if successful.
  1417. public int try_read_restore_point(ref RestorePoint rp, Stack stack)
  1418. {
  1419. if (stack.size() < sizeof(Action) + sizeof(RestorePointHeader))
  1420. return -1;
  1421. rp = stack.read_restore_point();
  1422. if (stack.size() < rp.header.size) {
  1423. // The restore point has been overwritten.
  1424. stack.clear();
  1425. return -1;
  1426. }
  1427. return 0;
  1428. }
  1429. // Un-does the last action and returns its ID, or -1 if there is no
  1430. // action to undo.
  1431. public int undo()
  1432. {
  1433. if (_undo_redo == null)
  1434. return -1;
  1435. RestorePoint rp = {};
  1436. if (try_read_restore_point(ref rp, _undo_redo._undo) != 0)
  1437. return -1;
  1438. undo_or_redo(_undo_redo._undo, _undo_redo._redo, rp.header.size);
  1439. switch (rp.header.id) {
  1440. case ActionType.CREATE_OBJECTS:
  1441. objects_destroyed(rp.data, 0u);
  1442. break;
  1443. case ActionType.DESTROY_OBJECTS:
  1444. objects_created(rp.data, 0u);
  1445. break;
  1446. case ActionType.CHANGE_OBJECTS:
  1447. objects_changed(rp.data, 0u);
  1448. break;
  1449. default:
  1450. logw("Unknown action type %u".printf(rp.header.id));
  1451. break;
  1452. }
  1453. _undo_redo._redo.write_restore_point(rp.header.id, rp.header.flags, rp.data);
  1454. return (int)rp.header.id;
  1455. }
  1456. // Re-does the last action and returns its ID, or -1 if there is no
  1457. // action to redo.
  1458. public int redo()
  1459. {
  1460. if (_undo_redo == null)
  1461. return -1;
  1462. RestorePoint rp = {};
  1463. if (try_read_restore_point(ref rp, _undo_redo._redo) != 0)
  1464. return -1;
  1465. undo_or_redo(_undo_redo._redo, _undo_redo._undo, rp.header.size);
  1466. switch (rp.header.id) {
  1467. case ActionType.CREATE_OBJECTS:
  1468. objects_created(rp.data, 0u);
  1469. break;
  1470. case ActionType.DESTROY_OBJECTS:
  1471. objects_destroyed(rp.data, 0u);
  1472. break;
  1473. case ActionType.CHANGE_OBJECTS:
  1474. objects_changed(rp.data, 0u);
  1475. break;
  1476. default:
  1477. logw("Unknown action type %u".printf(rp.header.id));
  1478. break;
  1479. }
  1480. _undo_redo._undo.write_restore_point(rp.header.id, rp.header.flags, rp.data);
  1481. return (int)rp.header.id;
  1482. }
  1483. public void undo_or_redo(Stack undo, Stack redo, uint32 restore_point_size)
  1484. {
  1485. assert(undo.size() >= restore_point_size);
  1486. int dir = undo == _undo_redo._undo ? -1 : 1;
  1487. // Read up to restore_point_size bytes.
  1488. uint32 undo_size_start = undo.size();
  1489. while (undo_size_start - undo.size() < restore_point_size) {
  1490. Action action = (Action)undo.read_uint32();
  1491. if (action == Action.CREATE) {
  1492. Guid id = undo.read_guid();
  1493. string obj_type = undo.read_string();
  1494. set_alive(id, true);
  1495. _undo_redo._distance_from_last_sync += dir;
  1496. redo.write_destroy_action(Action.DESTROY, id, obj_type);
  1497. } else if (action == Action.DESTROY) {
  1498. Guid id = undo.read_guid();
  1499. string obj_type = undo.read_string();
  1500. set_alive(id, false);
  1501. _undo_redo._distance_from_last_sync += dir;
  1502. redo.write_create_action(Action.CREATE, id, obj_type);
  1503. } else if (action == Action.SET_NULL) {
  1504. Guid id = undo.read_guid();
  1505. string key = undo.read_string();
  1506. if (has_property(id, key)) {
  1507. if (get_data(id)[key].holds(typeof(bool)))
  1508. redo.write_set_bool_action(Action.SET_BOOL, id, key, get_bool(id, key));
  1509. if (get_data(id)[key].holds(typeof(double)))
  1510. redo.write_set_double_action(Action.SET_DOUBLE, id, key, get_double(id, key));
  1511. if (get_data(id)[key].holds(typeof(string)))
  1512. redo.write_set_string_action(Action.SET_STRING, id, key, get_string(id, key));
  1513. if (get_data(id)[key].holds(typeof(Vector3)))
  1514. redo.write_set_vector3_action(Action.SET_VECTOR3, id, key, get_vector3(id, key));
  1515. if (get_data(id)[key].holds(typeof(Quaternion)))
  1516. redo.write_set_quaternion_action(Action.SET_QUATERNION, id, key, get_quaternion(id, key));
  1517. if (get_data(id)[key].holds(typeof(Resource)))
  1518. redo.write_set_resource_action(Action.SET_RESOURCE, id, key, { get_resource(id, key) });
  1519. if (get_data(id)[key].holds(typeof(Guid)))
  1520. redo.write_set_reference_action(Action.SET_REFERENCE, id, key, get_reference(id, key));
  1521. } else {
  1522. redo.write_set_null_action(Action.SET_NULL, id, key);
  1523. }
  1524. set(dir, id, key, null);
  1525. } else if (action == Action.SET_BOOL) {
  1526. Guid id = undo.read_guid();
  1527. string key = undo.read_string();
  1528. bool val = undo.read_bool();
  1529. if (has_property(id, key))
  1530. redo.write_set_bool_action(Action.SET_BOOL, id, key, get_bool(id, key));
  1531. else
  1532. redo.write_set_null_action(Action.SET_NULL, id, key);
  1533. set(dir, id, key, val);
  1534. } else if (action == Action.SET_DOUBLE) {
  1535. Guid id = undo.read_guid();
  1536. string key = undo.read_string();
  1537. double val = undo.read_double();
  1538. if (has_property(id, key))
  1539. redo.write_set_double_action(Action.SET_DOUBLE, id, key, get_double(id, key));
  1540. else
  1541. redo.write_set_null_action(Action.SET_NULL, id, key);
  1542. set(dir, id, key, val);
  1543. } else if (action == Action.SET_STRING) {
  1544. Guid id = undo.read_guid();
  1545. string key = undo.read_string();
  1546. string val = undo.read_string();
  1547. if (has_property(id, key))
  1548. redo.write_set_string_action(Action.SET_STRING, id, key, get_string(id, key));
  1549. else
  1550. redo.write_set_null_action(Action.SET_NULL, id, key);
  1551. set(dir, id, key, val);
  1552. } else if (action == Action.SET_VECTOR3) {
  1553. Guid id = undo.read_guid();
  1554. string key = undo.read_string();
  1555. Vector3 val = undo.read_vector3();
  1556. if (has_property(id, key))
  1557. redo.write_set_vector3_action(Action.SET_VECTOR3, id, key, get_vector3(id, key));
  1558. else
  1559. redo.write_set_null_action(Action.SET_NULL, id, key);
  1560. set(dir, id, key, val);
  1561. } else if (action == Action.SET_QUATERNION) {
  1562. Guid id = undo.read_guid();
  1563. string key = undo.read_string();
  1564. Quaternion val = undo.read_quaternion();
  1565. if (has_property(id, key))
  1566. redo.write_set_quaternion_action(Action.SET_QUATERNION, id, key, get_quaternion(id, key));
  1567. else
  1568. redo.write_set_null_action(Action.SET_NULL, id, key);
  1569. set(dir, id, key, val);
  1570. } else if (action == Action.SET_RESOURCE) {
  1571. Guid id = undo.read_guid();
  1572. string key = undo.read_string();
  1573. Resource val = undo.read_resource();
  1574. if (has_property(id, key))
  1575. redo.write_set_resource_action(Action.SET_RESOURCE, id, key, { get_resource(id, key) });
  1576. else
  1577. redo.write_set_null_action(Action.SET_NULL, id, key);
  1578. set(dir, id, key, val);
  1579. } else if (action == Action.SET_REFERENCE) {
  1580. Guid id = undo.read_guid();
  1581. string key = undo.read_string();
  1582. Guid val = undo.read_guid();
  1583. if (has_property(id, key))
  1584. redo.write_set_reference_action(Action.SET_REFERENCE, id, key, get_reference(id, key));
  1585. else
  1586. redo.write_set_null_action(Action.SET_NULL, id, key);
  1587. set(dir, id, key, val);
  1588. } else if (action == Action.ADD_TO_SET) {
  1589. Guid id = undo.read_guid();
  1590. string key = undo.read_string();
  1591. Guid item_id = undo.read_guid();
  1592. redo.write_remove_from_set_action(Action.REMOVE_FROM_SET, id, key, item_id);
  1593. add_to_set_internal(dir, id, key, item_id);
  1594. } else if (action == Action.REMOVE_FROM_SET) {
  1595. Guid id = undo.read_guid();
  1596. string key = undo.read_string();
  1597. Guid item_id = undo.read_guid();
  1598. redo.write_add_to_set_action(Action.ADD_TO_SET, id, key, item_id);
  1599. remove_from_set_internal(dir, id, key, item_id);
  1600. }
  1601. }
  1602. }
  1603. public void add_properties(PropertyDefinition[] properties)
  1604. {
  1605. foreach (PropertyDefinition def in properties) {
  1606. // Generate labels if missing.
  1607. if (def.label == null) {
  1608. int ld = def.name.last_index_of_char('.');
  1609. string label = ld == -1 ? def.name : def.name.substring(ld + 1);
  1610. def.label = camel_case(label);
  1611. }
  1612. if (def.enum_labels.length == 0) {
  1613. string[] labels = new string[def.enum_values.length];
  1614. for (int i = 0; i < def.enum_values.length; ++i)
  1615. labels[i] = camel_case(def.enum_values[i]);
  1616. def.enum_labels = labels;
  1617. }
  1618. // Assign default/min/max values.
  1619. switch (def.type) {
  1620. case PropertyType.BOOL:
  1621. if (def.deffault == null)
  1622. def.deffault = false;
  1623. assert(def.deffault.holds(typeof(bool)));
  1624. break;
  1625. case PropertyType.DOUBLE:
  1626. if (def.deffault == null)
  1627. def.deffault = 0.0;
  1628. if (def.min == null)
  1629. def.min = -double.MAX;
  1630. if (def.max == null)
  1631. def.max = double.MAX;
  1632. assert(def.deffault.holds(typeof(double)));
  1633. assert(def.min.holds(typeof(double)));
  1634. assert(def.max.holds(typeof(double)));
  1635. break;
  1636. case PropertyType.STRING:
  1637. if (def.deffault == null) {
  1638. if (def.enum_values.length > 0)
  1639. def.deffault = def.enum_values[0];
  1640. else
  1641. def.deffault = "";
  1642. }
  1643. assert(def.enum_property == null && def.enum_callback == null || def.enum_property != null && def.enum_callback != null && def.enum_values.length == 0);
  1644. assert(def.deffault.holds(typeof(string)));
  1645. break;
  1646. case PropertyType.VECTOR3:
  1647. if (def.deffault == null)
  1648. def.deffault = VECTOR3_ZERO;
  1649. if (def.min == null)
  1650. def.min = VECTOR3_MIN;
  1651. if (def.max == null)
  1652. def.max = VECTOR3_MAX;
  1653. assert(def.deffault.holds(typeof(Vector3)));
  1654. assert(def.min.holds(typeof(Vector3)));
  1655. assert(def.max.holds(typeof(Vector3)));
  1656. break;
  1657. case PropertyType.QUATERNION:
  1658. if (def.deffault == null)
  1659. def.deffault = QUATERNION_IDENTITY;
  1660. assert(def.deffault.holds(typeof(Quaternion)));
  1661. break;
  1662. case PropertyType.RESOURCE:
  1663. if (def.deffault == null)
  1664. def.deffault = (string?)null;
  1665. assert(def.resource_type != null);
  1666. assert(def.deffault.holds(typeof(string?)));
  1667. break;
  1668. case PropertyType.REFERENCE:
  1669. def.deffault = GUID_ZERO;
  1670. assert(def.deffault.holds(typeof(Guid)));
  1671. assert(def.object_type._id != 0);
  1672. break;
  1673. case PropertyType.OBJECTS_SET:
  1674. assert(def.object_type._id != 0);
  1675. break;
  1676. default:
  1677. assert(false);
  1678. break;
  1679. }
  1680. _property_definitions += def;
  1681. }
  1682. }
  1683. // Creates a new object @a type with the specified @a properties and returns its ID.
  1684. public StringId64 create_object_type(string type
  1685. , PropertyDefinition[] properties
  1686. , double ui_order = 0.0
  1687. , ObjectTypeFlags flags = ObjectTypeFlags.NONE
  1688. , string? user_data = null
  1689. )
  1690. {
  1691. StringId64 type_hash = StringId64(type);
  1692. assert(!_object_definitions.has_key(type_hash));
  1693. assert(properties.length > 0);
  1694. int first_property = _property_definitions.length;
  1695. int num_properties = first_property + properties.length;
  1696. add_properties(properties);
  1697. ObjectTypeInfo info = {};
  1698. info.properties = { first_property, num_properties };
  1699. info.name = type;
  1700. info.ui_name = camel_case(type);
  1701. info.ui_order = ui_order;
  1702. info.flags = flags;
  1703. info.user_data = user_data;
  1704. info.aspects = new Gee.HashMap<StringId64?, AspectData?>(StringId64.hash_func, StringId64.equal_func);
  1705. _object_definitions[type_hash] = info;
  1706. object_type_added(info);
  1707. return type_hash;
  1708. }
  1709. // Returns the array of properties (i.e. its definition) of the object @a type.
  1710. public unowned PropertyDefinition[]? object_definition(StringId64 type)
  1711. {
  1712. if (!_object_definitions.has_key(type))
  1713. return null;
  1714. PropertiesSlice ps = _object_definitions[type].properties;
  1715. return _property_definitions[ps.start : ps.end];
  1716. }
  1717. // Returns the name of the object @id. If the object has no name set, it returns
  1718. // OBJECT_NAME_UNNAMED.
  1719. public string name(Guid id)
  1720. {
  1721. string name = get_string(id, "editor.name", OBJECT_NAME_UNNAMED);
  1722. if (name == OBJECT_NAME_UNNAMED)
  1723. return get_string(id, "name", OBJECT_NAME_UNNAMED);
  1724. return name;
  1725. }
  1726. // Sets the @a name of the object @a id.
  1727. public void set_name(Guid id, string name)
  1728. {
  1729. set_string(id, "editor.name", name);
  1730. }
  1731. // Returns whether the object @a type exists (i.e. has been created with create_object_type()).
  1732. public bool has_type(StringId64 type)
  1733. {
  1734. return _object_definitions.has_key(type);
  1735. }
  1736. public string type_name(StringId64 type)
  1737. {
  1738. return _object_definitions[type].name;
  1739. }
  1740. public uint type_flags(StringId64 type)
  1741. {
  1742. return _object_definitions[type].flags;
  1743. }
  1744. public ObjectTypeInfo type_info(StringId64 type)
  1745. {
  1746. return _object_definitions[type];
  1747. }
  1748. public Guid?[] all_objects_of_type(StringId64 type)
  1749. {
  1750. Gee.ArrayList<Guid?> all = new Gee.ArrayList<Guid?>();
  1751. foreach (var item in _data) {
  1752. Guid id = item.key;
  1753. if (id != GUID_ZERO
  1754. && StringId64(object_type(id)) == type
  1755. && is_alive(id)) {
  1756. all.add(id);
  1757. }
  1758. }
  1759. return all.to_array();
  1760. }
  1761. public bool is_subobject_of(Guid subobject_id, Guid object_id, string set_name)
  1762. {
  1763. assert(has_object(subobject_id));
  1764. assert(has_object(object_id));
  1765. if (!has_property(object_id, set_name))
  1766. return false;
  1767. return get_set(object_id, set_name).contains(subobject_id);
  1768. }
  1769. public bool find_property(ref uint32 property_index, StringId64 object_type, PropertyType type, string name)
  1770. {
  1771. PropertyDefinition[]? properties = object_definition(object_type);
  1772. if (properties == null)
  1773. return false;
  1774. for (int i = 0; i < properties.length; ++i) {
  1775. PropertyDefinition def = properties[i];
  1776. if (def.type == type && def.name == name) {
  1777. property_index = i;
  1778. return true;
  1779. }
  1780. }
  1781. return false;
  1782. }
  1783. public void set_aspect(StringId64 object_type, StringId64 aspect, Aspect callback)
  1784. {
  1785. ObjectTypeInfo info = type_info(object_type);
  1786. AspectData data = AspectData();
  1787. data.callback = callback;
  1788. info.aspects[aspect] = data;
  1789. assert(info.aspects.has_key(aspect));
  1790. assert(get_aspect(object_type, aspect) == callback);
  1791. }
  1792. public unowned Aspect? get_aspect(StringId64 object_type, StringId64 aspect)
  1793. {
  1794. ObjectTypeInfo info = type_info(object_type);
  1795. if (info.aspects.has_key(aspect))
  1796. return info.aspects[aspect].callback;
  1797. return null;
  1798. }
  1799. }
  1800. public void default_name_aspect(out string name, Database database, Guid id)
  1801. {
  1802. name = database.name(id);
  1803. StringId64 object_type = StringId64(database.object_type(id));
  1804. PropertyDefinition[]? properties = database.object_definition(object_type);
  1805. uint32 name_index = 0;
  1806. if (database.find_property(ref name_index, object_type, PropertyType.STRING, "name"))
  1807. name = database.get_string(id, properties[name_index].name);
  1808. else if (database.find_property(ref name_index, object_type, PropertyType.STRING, "editor.name"))
  1809. name = database.get_string(id, properties[name_index].name);
  1810. else
  1811. name = OBJECT_NAME_UNNAMED;
  1812. }
  1813. } /* namespace Crown */