2
0

database.vala 38 KB

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