database.vala 45 KB

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