| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307 |
- /*
- * Copyright (c) 2012-2018 Daniele Bartolini and individual contributors.
- * License: https://github.com/dbartolini/crown/blob/master/LICENSE
- */
- using Gee;
- namespace Crown
- {
- public class Database
- {
- private enum Action
- {
- CREATE,
- DESTROY,
- SET_PROPERTY_NULL,
- SET_PROPERTY_BOOL,
- SET_PROPERTY_DOUBLE,
- SET_PROPERTY_STRING,
- SET_PROPERTY_GUID,
- SET_PROPERTY_VECTOR3,
- SET_PROPERTY_QUATERNION,
- ADD_TO_SET,
- REMOVE_FROM_SET,
- RESTORE_POINT
- }
- private struct RestorePoint
- {
- public int id;
- public uint32 size;
- public Guid[] data;
- }
- private class Stack
- {
- private uint8[] _data;
- private uint32 _read;
- public Stack()
- {
- _data = new uint8[1024*1024];
- _read = 0;
- }
- public void clear()
- {
- _read = 0;
- }
- public uint32 size()
- {
- return _read;
- }
- public void write_data(void* data, ulong len)
- {
- uint8* buf = (uint8*)data;
- for (ulong i = 0; i < len; ++i, ++_read)
- _data[_read] = buf[i];
- }
- public void write_bool(bool a)
- {
- write_data(&a, sizeof(bool));
- }
- public void write_int(int a)
- {
- write_data(&a, sizeof(int));
- }
- public void write_uint32(uint32 a)
- {
- write_data(&a, sizeof(uint32));
- }
- public void write_double(double a)
- {
- write_data(&a, sizeof(double));
- }
- public void write_string(string str)
- {
- uint32 len = str.length;
- write_data(&str.data[0], len);
- write_data(&len, sizeof(uint32));
- }
- public void write_guid(Guid a)
- {
- write_data(&a, sizeof(Guid));
- }
- public void write_vector3(Vector3 a)
- {
- write_data(&a, sizeof(Vector3));
- }
- public void write_quaternion(Quaternion a)
- {
- write_data(&a, sizeof(Quaternion));
- }
- public void write_action(Action t)
- {
- write_uint32((uint32)t);
- }
- public Action read_action()
- {
- _read -= (uint32)sizeof(uint32);
- uint32 a = *(uint32*)(&_data[_read]);
- return (Action)a;
- }
- public bool read_bool()
- {
- _read -= (uint32)sizeof(bool);
- bool a = *(bool*)(&_data[_read]);
- return a;
- }
- public int read_int()
- {
- _read -= (uint32)sizeof(int);
- int a = *(int*)(&_data[_read]);
- return a;
- }
- public uint32 read_uint32()
- {
- _read -= (uint32)sizeof(uint32);
- uint32 a = *(uint32*)(&_data[_read]);
- return a;
- }
- public double read_double()
- {
- _read -= (uint32)sizeof(double);
- double a = *(double*)(&_data[_read]);
- return a;
- }
- public Guid read_guid()
- {
- _read -= (uint32)sizeof(Guid);
- Guid a = *(Guid*)(&_data[_read]);
- return a;
- }
- public Vector3 read_vector3()
- {
- _read -= (uint32)sizeof(Vector3);
- Vector3 a = *(Vector3*)(&_data[_read]);
- return a;
- }
- public Quaternion read_quaternion()
- {
- _read -= (uint32)sizeof(Quaternion);
- Quaternion a = *(Quaternion*)(&_data[_read]);
- return a;
- }
- public string read_string()
- {
- _read -= (uint32)sizeof(uint32);
- uint32 len = *(uint32*)(&_data[_read]);
- _read -= len;
- uint8[] str = new uint8[len + 1];
- for (uint32 i = 0; i < len; ++i)
- str[i] = *(uint8*)(&_data[_read + i]);
- str[len] = '\0';
- return (string)str;
- }
- public void write_create_action(Guid id)
- {
- write_guid(id);
- write_action(Action.CREATE);
- }
- public void write_destroy_action(Guid id)
- {
- write_guid(id);
- write_action(Action.DESTROY);
- }
- public void write_set_property_null_action(Guid id, string key)
- {
- // No value to push
- write_string(key);
- write_guid(id);
- write_action(Action.SET_PROPERTY_NULL);
- }
- public void write_set_property_bool_action(Guid id, string key, bool val)
- {
- write_bool(val);
- write_string(key);
- write_guid(id);
- write_action(Action.SET_PROPERTY_BOOL);
- }
- public void write_set_property_double_action(Guid id, string key, double val)
- {
- write_double(val);
- write_string(key);
- write_guid(id);
- write_action(Action.SET_PROPERTY_DOUBLE);
- }
- public void write_set_property_string_action(Guid id, string key, string val)
- {
- write_string(val);
- write_string(key);
- write_guid(id);
- write_action(Action.SET_PROPERTY_STRING);
- }
- public void write_set_property_guid_action(Guid id, string key, Guid val)
- {
- write_guid(val);
- write_string(key);
- write_guid(id);
- write_action(Action.SET_PROPERTY_GUID);
- }
- public void write_set_property_vector3_action(Guid id, string key, Vector3 val)
- {
- write_vector3(val);
- write_string(key);
- write_guid(id);
- write_action(Action.SET_PROPERTY_VECTOR3);
- }
- public void write_set_property_quaternion_action(Guid id, string key, Quaternion val)
- {
- write_quaternion(val);
- write_string(key);
- write_guid(id);
- write_action(Action.SET_PROPERTY_QUATERNION);
- }
- public void write_add_to_set_action(Guid id, string key, Guid item_id)
- {
- write_guid(item_id);
- write_string(key);
- write_guid(id);
- write_action(Action.ADD_TO_SET);
- }
- public void write_remove_from_set_action(Guid id, string key, Guid item_id)
- {
- write_guid(item_id);
- write_string(key);
- write_guid(id);
- write_action(Action.REMOVE_FROM_SET);
- }
- public void write_restore_point(int id, uint32 size, Guid[] data)
- {
- uint32 num_guids = data.length;
- for (uint32 i = 0; i < num_guids; ++i)
- write_guid(data[i]);
- write_uint32(num_guids);
- write_uint32(size);
- write_int(id);
- write_action(Action.RESTORE_POINT);
- }
- public uint32 peek_type()
- {
- return *(uint32*)(&_data[_read - (uint32)sizeof(uint32)]);
- }
- public RestorePoint read_restore_point()
- {
- Action t = read_action();
- assert(t == Action.RESTORE_POINT);
- int id = read_int();
- uint32 size = read_uint32();
- uint32 num_guids = read_uint32();
- Guid[] ids = new Guid[num_guids];
- for (uint32 i = 0; i < num_guids; ++i)
- ids[i] = read_guid();
- return { id, size, ids };
- }
- }
- // Data
- private HashMap<string, Value?> _data;
- private Stack _undo;
- private Stack _redo;
- private Stack _undo_points;
- private Stack _redo_points;
- private bool _changed;
- // Signals
- public signal void key_changed(Guid id, string key);
- public signal void undo_redo(bool undo, int id, Guid[] data);
- public Database()
- {
- _data = new HashMap<string, Value?>();
- _undo = new Stack();
- _redo = new Stack();
- _undo_points = new Stack();
- _redo_points = new Stack();
- reset();
- }
- /// Resets database to clean state.
- public void reset()
- {
- _data.clear();
- _undo.clear();
- _redo.clear();
- _undo_points.clear();
- _redo_points.clear();
- _changed = false;
- // This is a special field which stores all objects
- _data.set("_objects", new HashMap<string, Value?>());
- }
- /// Returns whether the database has been changed since last call to Save().
- public bool changed()
- {
- return _changed;
- }
- /// Saves database to path without marking it as not changed.
- public void dump(string path)
- {
- Hashtable json = encode();
- SJSON.save(json, path);
- }
- /// Saves database to path.
- public void save(string path)
- {
- dump(path);
- _changed = false;
- }
- /// Loads database from path.
- public void load(string path)
- {
- Hashtable json = SJSON.load(path);
- decode(json);
- _changed = false;
- }
- private Hashtable encode()
- {
- return encode_object(GUID_ZERO, _data);
- }
- private static bool is_valid_value(Value? value)
- {
- return value == null
- || value.holds(typeof(bool))
- || value.holds(typeof(double))
- || value.holds(typeof(string))
- || value.holds(typeof(Guid))
- || value.holds(typeof(Vector3))
- || value.holds(typeof(Quaternion))
- ;
- }
- private static bool is_valid_key(string key)
- {
- return key.length > 0
- && key != "_objects"
- && !key.has_prefix(".")
- && !key.has_suffix(".")
- ;
- }
- #if 0
- private static string value_to_string(Value? value)
- {
- if (value == null)
- return "null";
- if (value.holds(typeof(bool)))
- return ((bool)value).to_string();
- if (value.holds(typeof(double)))
- return ((double)value).to_string();
- if (value.holds(typeof(string)))
- return ((string)value).to_string();
- if (value.holds(typeof(Guid)))
- return ((Guid)value).to_string();
- if (value.holds(typeof(Vector3)))
- return ((Vector3)value).to_string();
- if (value.holds(typeof(Quaternion)))
- return ((Quaternion)value).to_string();
- if (value.holds(typeof(HashSet)))
- return "Set<Guid>";
- return "<invalid>";
- }
- #endif // CROWN_DEBUG
- public void decode(Hashtable json)
- {
- reset();
- decode_object(GUID_ZERO, "", json);
- }
- private void decode_object(Guid id, string db_key, Hashtable json)
- {
- string old_db = db_key;
- string k = db_key;
- string[] keys = json.keys.to_array();
- foreach (string key in keys)
- {
- assert(key != "_objects");
- if (key == "id")
- continue;
- Value? val = json[key];
- k += k == "" ? key : ("." + key);
- if (val.holds(typeof(Hashtable)))
- {
- Hashtable ht = (Hashtable)val;
- decode_object(id, k, ht);
- }
- else if (val.holds(typeof(ArrayList)))
- {
- ArrayList<Value?> arr = (ArrayList<Value?>)val;
- if (arr.size > 0 && arr[0].holds(typeof(double)))
- set_property_internal(id, k, decode_value(val));
- else
- decode_set(id, key, arr);
- }
- else
- {
- set_property_internal(id, k, decode_value(val));
- }
- k = old_db;
- }
- }
- private void decode_set(Guid id, string key, ArrayList<Value?> json)
- {
- // Set should be created even if it is empty.
- create_empty_set(id, key);
- for (int i = 0; i < json.size; ++i)
- {
- Hashtable obj = (Hashtable)json[i];
- Guid item_id = Guid.parse((string)obj["id"]);
- create_internal(item_id);
- decode_object(item_id, "", obj);
- add_to_set_internal(id, key, item_id);
- }
- }
- private Value? decode_value(Value? value)
- {
- if (value.holds(typeof(ArrayList)))
- {
- ArrayList<Value?> al = (ArrayList<Value?>)value;
- if (al.size == 3)
- return Vector3((double)al[0], (double)al[1], (double)al[2]);
- else if (al.size == 4)
- return Quaternion((double)al[0], (double)al[1], (double)al[2], (double)al[3]);
- else
- assert(false);
- }
- else if (value.holds(typeof(string)))
- {
- Guid id;
- if (Guid.try_parse((string)value, out id))
- return id;
- return value;
- }
- else if (value == null || value.holds(typeof(bool)) || value.holds(typeof(double)))
- {
- return value;
- }
- else
- {
- assert(false);
- }
- return null;
- }
- private Hashtable encode_object(Guid id, HashMap<string, Value?> db)
- {
- Hashtable obj = new Hashtable();
- if (id != GUID_ZERO)
- obj["id"] = id.to_string();
- string[] keys = db.keys.to_array();
- foreach (string key in keys)
- {
- if (key == "_objects")
- continue;
- string[] foo = key.split(".");
- Hashtable x = obj;
- if (foo.length > 1)
- {
- for (int i = 0; i < foo.length - 1; ++i)
- {
- string f = foo[i];
- if (x.has_key(f))
- {
- x = (Hashtable)x[f];
- continue;
- }
- Hashtable y = new Hashtable();
- x.set(f, y);
- x = y;
- }
- }
- x.set(foo[foo.length-1], encode_value(db[key]));
- }
- return obj;
- }
- private Value? encode_value(Value? value)
- {
- assert(is_valid_value(value) || value.holds(typeof(HashSet)));
- if (value.holds(typeof(Vector3)))
- {
- Vector3 v = (Vector3)value;
- ArrayList<Value?> arr = new Gee.ArrayList<Value?>();
- arr.add(v.x);
- arr.add(v.y);
- arr.add(v.z);
- return arr;
- }
- else if (value.holds(typeof(Quaternion)))
- {
- Quaternion q = (Quaternion)value;
- ArrayList<Value?> arr = new Gee.ArrayList<Value?>();
- arr.add(q.x);
- arr.add(q.y);
- arr.add(q.z);
- arr.add(q.w);
- return arr;
- }
- else if (value.holds(typeof(Guid)))
- {
- Guid id = (Guid)value;
- return id.to_string();
- }
- else if (value.holds(typeof(HashSet)))
- {
- HashSet<Guid?> hs = (HashSet<Guid?>)value;
- ArrayList<Value?> arr = new Gee.ArrayList<Value?>();
- foreach (Guid id in hs)
- {
- HashMap<string, Value?> objs = (HashMap<string, Value?>)_data["_objects"];
- arr.add(encode_object(id, (HashMap<string, Value?>)objs[id.to_string()]));
- }
- return arr;
- }
- else
- {
- return value;
- }
- }
- private HashMap<string, Value?> get_data(Guid id)
- {
- assert(has_object(id));
- return (HashMap<string, Value?>)(id == GUID_ZERO ? _data : (_data["_objects"] as HashMap<string, Value?>)[id.to_string()]);
- }
- private void create_internal(Guid id)
- {
- assert(id != GUID_ZERO);
- #if 0
- stdout.printf("create %s\n", id.to_string());
- #endif // CROWN_DEBUG
- (_data["_objects"] as HashMap<string, Value?>).set(id.to_string(), new HashMap<string, Value?>());
- _changed = true;
- key_changed(id, "_objects");
- }
- private void destroy_internal(Guid id)
- {
- assert(id != GUID_ZERO);
- assert(has_object(id));
- #if 0
- stdout.printf("destroy %s\n", id.to_string());
- #endif // CROWN_DEBUG
- (_data["_objects"] as HashMap<string, Value?>).unset(id.to_string());
- _changed = true;
- key_changed(id, "_objects");
- }
- private void set_property_internal(Guid id, string key, Value? value)
- {
- assert(has_object(id));
- assert(is_valid_key(key));
- assert(is_valid_value(value));
- #if 0
- stdout.printf("set_property %s %s %s\n"
- , id.to_string()
- , key
- , (value == null) ? "null" : value_to_string(value)
- );
- #endif // CROWN_DEBUG
- HashMap<string, Value?> ob = get_data(id);
- ob[key] = value;
- _changed = true;
- key_changed(id, key);
- }
- private void create_empty_set(Guid id, string key)
- {
- assert(has_object(id));
- assert(is_valid_key(key));
- HashMap<string, Value?> ob = get_data(id);
- assert(!ob.has_key(key));
- ob[key] = new HashSet<Guid?>(Guid.hash_func, Guid.equal_func);
- }
- private void add_to_set_internal(Guid id, string key, Guid item_id)
- {
- assert(has_object(id));
- assert(is_valid_key(key));
- assert(item_id != GUID_ZERO);
- assert(has_object(item_id));
- #if 0
- stdout.printf("add_to_set %s %s %s\n"
- , id.to_string()
- , key
- , item_id.to_string()
- );
- #endif // CROWN_DEBUG
- HashMap<string, Value?> ob = get_data(id);
- if (!ob.has_key(key))
- {
- HashSet<Guid?> hs = new HashSet<Guid?>(Guid.hash_func, Guid.equal_func);
- hs.add(item_id);
- ob[key] = hs;
- }
- else
- {
- (ob[key] as HashSet<Guid?>).add(item_id);
- }
- _changed = true;
- key_changed(id, key);
- }
- private void remove_from_set_internal(Guid id, string key, Guid item_id)
- {
- assert(has_object(id));
- assert(is_valid_key(key));
- assert(item_id != GUID_ZERO);
- #if 0
- stdout.printf("remove_from_set %s %s %s\n"
- , id.to_string()
- , key
- , item_id.to_string()
- );
- #endif // CROWN_DEBUG
- HashMap<string, Value?> ob = get_data(id);
- (ob[key] as HashSet<Guid?>).remove(item_id);
- _changed = true;
- key_changed(id, key);
- }
- public void create(Guid id)
- {
- assert(id != GUID_ZERO);
- assert(!has_object(id));
- _undo.write_destroy_action(id);
- _redo.clear();
- _redo_points.clear();
- create_internal(id);
- }
- public void destroy(Guid id)
- {
- assert(id != GUID_ZERO);
- assert(has_object(id));
- HashMap<string, Value?> o = get_data(id);
- string[] keys = o.keys.to_array();
- foreach (string key in keys)
- {
- Value? value = o[key];
- if (value.holds(typeof(HashSet)))
- {
- HashSet<Guid?> hs = (HashSet<Guid?>)value;
- Guid?[] ids = hs.to_array();
- foreach (Guid item_id in ids)
- {
- remove_from_set(id, key, item_id);
- destroy(item_id);
- }
- }
- else
- {
- set_property_null(id, key);
- }
- }
- _undo.write_create_action(id);
- _redo.clear();
- _redo_points.clear();
- destroy_internal(id);
- }
- public void set_property_null(Guid id, string key)
- {
- assert(has_object(id));
- assert(is_valid_key(key));
- assert(is_valid_value(null));
- HashMap<string, Value?> ob = get_data(id);
- if (ob.has_key(key) && ob[key] != null)
- {
- if (ob[key].holds(typeof(bool)))
- _undo.write_set_property_bool_action(id, key, (bool)ob[key]);
- if (ob[key].holds(typeof(double)))
- _undo.write_set_property_double_action(id, key, (double)ob[key]);
- if (ob[key].holds(typeof(string)))
- _undo.write_set_property_string_action(id, key, (string)ob[key]);
- if (ob[key].holds(typeof(Guid)))
- _undo.write_set_property_guid_action(id, key, (Guid)ob[key]);
- if (ob[key].holds(typeof(Vector3)))
- _undo.write_set_property_vector3_action(id, key, (Vector3)ob[key]);
- if (ob[key].holds(typeof(Quaternion)))
- _undo.write_set_property_quaternion_action(id, key, (Quaternion)ob[key]);
- }
- else
- {
- _undo.write_set_property_null_action(id, key);
- }
- _redo.clear();
- _redo_points.clear();
- set_property_internal(id, key, null);
- }
- public void set_property_bool(Guid id, string key, bool val)
- {
- assert(has_object(id));
- assert(is_valid_key(key));
- assert(is_valid_value(val));
- HashMap<string, Value?> ob = get_data(id);
- if (ob.has_key(key) && ob[key] != null)
- _undo.write_set_property_bool_action(id, key, (bool)ob[key]);
- else
- _undo.write_set_property_null_action(id, key);
- _redo.clear();
- _redo_points.clear();
- set_property_internal(id, key, val);
- }
- public void set_property_double(Guid id, string key, double val)
- {
- assert(has_object(id));
- assert(is_valid_key(key));
- assert(is_valid_value(val));
- HashMap<string, Value?> ob = get_data(id);
- if (ob.has_key(key) && ob[key] != null)
- _undo.write_set_property_double_action(id, key, (double)ob[key]);
- else
- _undo.write_set_property_null_action(id, key);
- _redo.clear();
- _redo_points.clear();
- set_property_internal(id, key, val);
- }
- public void set_property_string(Guid id, string key, string val)
- {
- assert(has_object(id));
- assert(is_valid_key(key));
- assert(is_valid_value(val));
- HashMap<string, Value?> ob = get_data(id);
- if (ob.has_key(key) && ob[key] != null)
- _undo.write_set_property_string_action(id, key, (string)ob[key]);
- else
- _undo.write_set_property_null_action(id, key);
- _redo.clear();
- _redo_points.clear();
- set_property_internal(id, key, val);
- }
- public void set_property_guid(Guid id, string key, Guid val)
- {
- assert(has_object(id));
- assert(is_valid_key(key));
- assert(is_valid_value(val));
- HashMap<string, Value?> ob = get_data(id);
- if (ob.has_key(key) && ob[key] != null)
- _undo.write_set_property_guid_action(id, key, (Guid)ob[key]);
- else
- _undo.write_set_property_null_action(id, key);
- _redo.clear();
- _redo_points.clear();
- set_property_internal(id, key, val);
- }
- public void set_property_vector3(Guid id, string key, Vector3 val)
- {
- assert(has_object(id));
- assert(is_valid_key(key));
- assert(is_valid_value(val));
- HashMap<string, Value?> ob = get_data(id);
- if (ob.has_key(key) && ob[key] != null)
- _undo.write_set_property_vector3_action(id, key, (Vector3)ob[key]);
- else
- _undo.write_set_property_null_action(id, key);
- _redo.clear();
- _redo_points.clear();
- set_property_internal(id, key, val);
- }
- public void set_property_quaternion(Guid id, string key, Quaternion val)
- {
- assert(has_object(id));
- assert(is_valid_key(key));
- assert(is_valid_value(val));
- HashMap<string, Value?> ob = get_data(id);
- if (ob.has_key(key) && ob[key] != null)
- _undo.write_set_property_quaternion_action(id, key, (Quaternion)ob[key]);
- else
- _undo.write_set_property_null_action(id, key);
- _redo.clear();
- _redo_points.clear();
- set_property_internal(id, key, val);
- }
- public void add_to_set(Guid id, string key, Guid item_id)
- {
- assert(has_object(id));
- assert(is_valid_key(key));
- assert(item_id != GUID_ZERO);
- assert(has_object(item_id));
- _undo.write_remove_from_set_action(id, key, item_id);
- _redo.clear();
- _redo_points.clear();
- add_to_set_internal(id, key, item_id);
- }
- public void remove_from_set(Guid id, string key, Guid item_id)
- {
- assert(has_object(id));
- assert(is_valid_key(key));
- assert(item_id != GUID_ZERO);
- _undo.write_add_to_set_action(id, key, item_id);
- _redo.clear();
- _redo_points.clear();
- remove_from_set_internal(id, key, item_id);
- }
- public bool has_object(Guid id)
- {
- bool contains = (_data["_objects"] as HashMap<string, Value?>).has_key(id.to_string());
- return id == GUID_ZERO || contains;
- }
- public bool has_property(Guid id, string key)
- {
- assert(has_object(id));
- assert(is_valid_key(key));
- return get_data(id).has_key(key);
- }
- public Value? get_property(Guid id, string key)
- {
- assert(has_object(id));
- assert(is_valid_key(key));
- HashMap<string, Value?> ob = get_data(id);
- Value? value = (ob.has_key(key) ? ob[key] : null);
- #if 0
- stdout.printf("get_property %s %s %s\n"
- , id.to_string()
- , key
- , (value == null) ? "null" : value_to_string(value)
- );
- #endif // CROWN_DEBUG
- return value;
- }
- public bool get_property_bool(Guid id, string key)
- {
- return (bool)get_property(id, key);
- }
- public double get_property_double(Guid id, string key)
- {
- return (double)get_property(id, key);
- }
- public string get_property_string(Guid id, string key)
- {
- return (string)get_property(id, key);
- }
- public Guid get_property_guid(Guid id, string key)
- {
- return (Guid)get_property(id, key);
- }
- public Vector3 get_property_vector3(Guid id, string key)
- {
- return (Vector3)get_property(id, key);
- }
- public Quaternion get_property_quaternion(Guid id, string key)
- {
- return (Quaternion)get_property(id, key);
- }
- public HashSet<Guid?> get_property_set(Guid id, string key, HashSet<Guid?> deffault)
- {
- assert(has_object(id));
- assert(is_valid_key(key));
- HashMap<string, Value?> ob = get_data(id);
- if (ob.has_key(key))
- return ob[key] as HashSet<Guid?>;
- else
- return deffault;
- #if 0
- // stdout.printf("get_property %s %s %s\n"
- // , id.to_string()
- // , key
- // , (value == null) ? "null" : value_to_string(value)
- // );
- #endif // CROWN_DEBUG
- }
- public HashMap<string, Value?> get_object(Guid id)
- {
- return (HashMap<string, Value?>)get_data(GUID_ZERO)[id.to_string()];
- }
- public string[] get_keys(Guid id)
- {
- HashMap<string, Value?> data = get_data(id);
- return data.keys.to_array();
- }
- public void add_restore_point(int id, Guid[] data)
- {
- #if 0
- stdout.printf("add_restore_point %d, undo size = %u\n", id, _undo.size());
- #endif // CROWN_DEBUG
- _undo_points.write_restore_point(id, _undo.size(), data);
- _redo.clear();
- _redo_points.clear();
- }
- /// Duplicates the object specified by id and assign new_id to the duplicated object.
- public void duplicate(Guid id, Guid new_id)
- {
- assert(id != GUID_ZERO);
- assert(new_id != GUID_ZERO);
- assert(id != new_id);
- assert(has_object(id));
- create(new_id);
- HashMap<string, Value?> ob = get_data(id);
- string[] keys = ob.keys.to_array();
- foreach (string key in keys)
- {
- Value? val = ob[key];
- if (val.holds(typeof(HashSet)))
- {
- HashSet<Guid?> hs = (HashSet<Guid?>)val;
- foreach (Guid j in hs)
- {
- Guid x = Guid.new_guid();
- duplicate(j, x);
- add_to_set(new_id, key, x);
- }
- }
- else
- {
- if (ob[key] == null)
- set_property_null(new_id, key);
- if (ob[key].holds(typeof(bool)))
- set_property_bool(new_id, key, (bool)ob[key]);
- if (ob[key].holds(typeof(double)))
- set_property_double(new_id, key, (double)ob[key]);
- if (ob[key].holds(typeof(string)))
- set_property_string(new_id, key, (string)ob[key]);
- if (ob[key].holds(typeof(Guid)))
- set_property_guid(new_id, key, (Guid)ob[key]);
- if (ob[key].holds(typeof(Vector3)))
- set_property_vector3(new_id, key, (Vector3)ob[key]);
- if (ob[key].holds(typeof(Quaternion)))
- set_property_quaternion(new_id, key, (Quaternion)ob[key]);
- }
- }
- }
- /// Copies the database to db under the given new_key.
- public void copy_to(Database db, string new_key)
- {
- assert(db != null);
- assert(is_valid_key(new_key));
- copy_deep(db, GUID_ZERO, new_key);
- }
- public void copy_deep(Database db, Guid id, string new_key)
- {
- HashMap<string, Value?> ob = get_data(id);
- string[] keys = ob.keys.to_array();
- foreach (string key in keys)
- {
- if (key == "_objects")
- continue;
- Value? value = ob[key];
- if (value.holds(typeof(HashSet)))
- {
- HashSet<Guid?> hs = (HashSet<Guid?>)value;
- foreach (Guid j in hs)
- {
- db.create(j);
- copy_deep(db, j, "");
- db.add_to_set(id, new_key + (new_key == "" ? "" : ".") + key, j);
- }
- }
- else
- {
- string kk = new_key + (new_key == "" ? "" : ".") + key;
- if (ob[key] == null)
- db.set_property_null(id, kk);
- if (ob[key].holds(typeof(bool)))
- db.set_property_bool(id, kk, (bool)ob[key]);
- if (ob[key].holds(typeof(double)))
- db.set_property_double(id, kk, (double)ob[key]);
- if (ob[key].holds(typeof(string)))
- db.set_property_string(id, kk, (string)ob[key]);
- if (ob[key].holds(typeof(Guid)))
- db.set_property_guid(id, kk, (Guid)ob[key]);
- if (ob[key].holds(typeof(Vector3)))
- db.set_property_vector3(id, kk, (Vector3)ob[key]);
- if (ob[key].holds(typeof(Quaternion)))
- db.set_property_quaternion(id, kk, (Quaternion)ob[key]);
- }
- }
- }
- public void undo()
- {
- if (_undo_points.size() == 0)
- return;
- RestorePoint rp = _undo_points.read_restore_point();
- _redo_points.write_restore_point(rp.id, _redo.size(), rp.data);
- undo_until(rp.size);
- undo_redo(true, rp.id, rp.data);
- }
- public void redo()
- {
- if (_redo_points.size() == 0)
- return;
- RestorePoint rp = _redo_points.read_restore_point();
- _undo_points.write_restore_point(rp.id, _undo.size(), rp.data);
- redo_until(rp.size);
- undo_redo(false, rp.id, rp.data);
- }
- private void undo_until(uint32 size)
- {
- undo_redo_until(size, _undo, _redo);
- }
- private void redo_until(uint32 size)
- {
- undo_redo_until(size, _redo, _undo);
- }
- private void undo_redo_until(uint32 size, Stack undo, Stack redo)
- {
- while (undo.size() != size)
- {
- uint32 type = undo.peek_type();
- if (type == Action.CREATE)
- {
- Action t = undo.read_action();
- assert(t == Action.CREATE);
- Guid id = undo.read_guid();
- redo.write_destroy_action(id);
- create_internal(id);
- }
- else if (type == Action.DESTROY)
- {
- Action t = undo.read_action();
- assert(t == Action.DESTROY);
- Guid id = undo.read_guid();
- redo.write_create_action(id);
- destroy_internal(id);
- }
- else if (type == Action.SET_PROPERTY_NULL)
- {
- Action t = undo.read_action();
- assert(t == Action.SET_PROPERTY_NULL);
- Guid id = undo.read_guid();
- string key = undo.read_string();
- if (get_data(id).has_key(key))
- {
- if (get_data(id)[key].holds(typeof(bool)))
- redo.write_set_property_bool_action(id, key, (bool)get_data(id)[key]);
- if (get_data(id)[key].holds(typeof(double)))
- redo.write_set_property_double_action(id, key, (double)get_data(id)[key]);
- if (get_data(id)[key].holds(typeof(string)))
- redo.write_set_property_string_action(id, key, (string)get_data(id)[key]);
- if (get_data(id)[key].holds(typeof(Guid)))
- redo.write_set_property_guid_action(id, key, (Guid)get_data(id)[key]);
- if (get_data(id)[key].holds(typeof(Vector3)))
- redo.write_set_property_vector3_action(id, key, (Vector3)get_data(id)[key]);
- if (get_data(id)[key].holds(typeof(Quaternion)))
- redo.write_set_property_quaternion_action(id, key, (Quaternion)get_data(id)[key]);
- }
- else
- {
- redo.write_set_property_null_action(id, key);
- }
- set_property_internal(id, key, null);
- }
- else if (type == Action.SET_PROPERTY_BOOL)
- {
- Action t = undo.read_action();
- assert(t == Action.SET_PROPERTY_BOOL);
- Guid id = undo.read_guid();
- string key = undo.read_string();
- bool val = undo.read_bool();
- if (get_data(id).has_key(key))
- redo.write_set_property_bool_action(id, key, (bool)get_data(id)[key]);
- else
- redo.write_set_property_null_action(id, key);
- set_property_internal(id, key, val);
- }
- else if (type == Action.SET_PROPERTY_DOUBLE)
- {
- Action t = undo.read_action();
- assert(t == Action.SET_PROPERTY_DOUBLE);
- Guid id = undo.read_guid();
- string key = undo.read_string();
- double val = undo.read_double();
- if (get_data(id).has_key(key))
- redo.write_set_property_double_action(id, key, (double)get_data(id)[key]);
- else
- redo.write_set_property_null_action(id, key);
- set_property_internal(id, key, val);
- }
- else if (type == Action.SET_PROPERTY_STRING)
- {
- Action t = undo.read_action();
- assert(t == Action.SET_PROPERTY_STRING);
- Guid id = undo.read_guid();
- string key = undo.read_string();
- string val = undo.read_string();
- if (get_data(id).has_key(key))
- redo.write_set_property_string_action(id, key, (string)get_data(id)[key]);
- else
- redo.write_set_property_null_action(id, key);
- set_property_internal(id, key, val);
- }
- else if (type == Action.SET_PROPERTY_GUID)
- {
- Action t = undo.read_action();
- assert(t == Action.SET_PROPERTY_GUID);
- Guid id = undo.read_guid();
- string key = undo.read_string();
- Guid val = undo.read_guid();
- if (get_data(id).has_key(key))
- redo.write_set_property_guid_action(id, key, (Guid)get_data(id)[key]);
- else
- redo.write_set_property_null_action(id, key);
- set_property_internal(id, key, val);
- }
- else if (type == Action.SET_PROPERTY_VECTOR3)
- {
- Action t = undo.read_action();
- assert(t == Action.SET_PROPERTY_VECTOR3);
- Guid id = undo.read_guid();
- string key = undo.read_string();
- Vector3 val = undo.read_vector3();
- if (get_data(id).has_key(key))
- redo.write_set_property_vector3_action(id, key, (Vector3)get_data(id)[key]);
- else
- redo.write_set_property_null_action(id, key);
- set_property_internal(id, key, val);
- }
- else if (type == Action.SET_PROPERTY_QUATERNION)
- {
- Action t = undo.read_action();
- assert(t == Action.SET_PROPERTY_QUATERNION);
- Guid id = undo.read_guid();
- string key = undo.read_string();
- Quaternion val = undo.read_quaternion();
- if (get_data(id).has_key(key))
- redo.write_set_property_quaternion_action(id, key, (Quaternion)get_data(id)[key]);
- else
- redo.write_set_property_null_action(id, key);
- set_property_internal(id, key, val);
- }
- else if (type == Action.ADD_TO_SET)
- {
- Action t = undo.read_action();
- assert(t == Action.ADD_TO_SET);
- Guid id = undo.read_guid();
- string key = undo.read_string();
- Guid item_id = undo.read_guid();
- redo.write_remove_from_set_action(id, key, item_id);
- add_to_set_internal(id, key, item_id);
- }
- else if (type == Action.REMOVE_FROM_SET)
- {
- Action t = undo.read_action();
- assert(t == Action.REMOVE_FROM_SET);
- Guid id = undo.read_guid();
- string key = undo.read_string();
- Guid item_id = undo.read_guid();
- redo.write_add_to_set_action(id, key, item_id);
- remove_from_set_internal(id, key, item_id);
- }
- }
- }
- }
- }
|