public SimpleDataObject(object o, ObjectGraphContext ctx = null) { Context = ctx ?? new ObjectGraphContext(); if (o != null) { Type = o.GetType(); if (Context.GetID(o) == null) ID = Context.Add(o); Data = o.GetData(ctx); } else { Type = null; Data = new SafeDictionary<string, object>(); ID = -1; } }
public SimpleDataObject(SafeDictionary<string, IData> simpleData, ObjectGraphContext ctx = null) { SimpleData = simpleData; Context = ctx ?? new ObjectGraphContext(); }
public DynamicDictionary() { dict = new SafeDictionary <object, object>(); }
private static object DeserializeObjectWithProperties(TextReader r, Type type, ObjectGraphContext context, StringBuilder log) { // create object and add it to our context var o = type.Instantiate(); context.Add(o); // properties count - need to create object and populate properties int count; string s = r.ReadTo(':', log); if (!int.TryParse(s, out count)) { throw new SerializationException("Expected integer, got \"" + s + "\" when parsing property count."); } var dict = new SafeDictionary <string, object>(); // deserialize the properties var props = ObjectGraphContext.GetKnownProperties(type, true); for (int i = 0; i < count; i++) { var pname = r.ReadTo(':', log).Trim(); if (type == typeof(Game.Objects.Civilization.Empire) && pname == "StoredResources") { } if (props.ContainsKey(pname)) { // TODO - get base class recursively, not just derived class and declaring type var prop = type.GetProperty(pname, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance) ?? props[pname]; // get concrete type property in case it has DoNotSerialize and the abstract type doesn't if (prop != null) { if (prop.Name == "StarSystemNames") { } var data = Deserialize(r, prop.PropertyType, false, context, log); if (prop.HasAttribute <SerializationPriorityAttribute>()) { prop.SetValue(o, data); // TODO - use cached reflection lambdas } if (!prop.HasAttribute <DoNotSerializeAttribute>()) { dict[pname] = data; } } else { r.ReadToEndOfLine(';', log); // throw away this property, we don't need it } // if p is null or has do not serialize attribute, it must be data from an old version with different property names, so don't crash } } //propertySetterTasks.Add(Task.Factory.StartNew(() => //{ o.SetData(dict, context); //})); // clean up ReadSemicolon(r, type, log); return(o); }
public static IEnumerable <PathfinderNode <T> > CreateDijkstraMap <T>(T start, T end, ConnectivityGraph <T> graph) { // pathfind! // step 1: empty priority queue with cost to reach each node var queue = new List <PathfinderNode <T> >(); // step 2: empty set of previously visited nodes, along with costs and previous-node references var visited = new SafeDictionary <T, PathfinderNode <T> >(); // step 3: add start node and cost queue.Add(new PathfinderNode <T>(start, 0, null)); // step 4: quit if there are no nodes (all paths exhausted without finding goal) bool success = false; while (queue.Any() && !success) { // step 5: take lowest cost node out of queue // also prefer straight line movement to diagonal var minCost = queue.Min(n => n.Cost); var node = queue.Where(n => n.Cost == minCost).First(); queue.Remove(node); // step 6: if node is the goal, stop - success! if (node.Location.Equals(end)) { success = true; } // step 7: check possible moves var moves = graph.GetExits(node.Location); // step 7a: remove blocked points (aka calculate cost) // nothing to do here // step 7b: update priority queue foreach (var move in moves) { if (!visited.ContainsKey(move)) { // didn't visit yet var newnode = new PathfinderNode <T>(move, node.Cost + 1, node); queue.Add(newnode); visited.Add(move, newnode); } else { // already visited - but is it by a longer path? var items = queue.Where(n => n.Location.Equals(move) && n.Cost > node.Cost + 1); if (items.Any()) { foreach (var old in items.ToArray()) { queue.Remove(old); } var newnode = new PathfinderNode <T>(move, node.Cost + 1, node); queue.Add(newnode); visited.Add(move, newnode); } } } } return(visited.Values); }