database.vala 38 KB

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