private static Dictionary <uint, ISaveable> buildObjectTable(ISaveable root, SaveType saveType) { Dictionary <uint, ISaveable> objects = new Dictionary <uint, ISaveable>(); objects.Add(root.GetId(), root); Stack <ISaveable> toVisit = new Stack <ISaveable>(); addUnvisitedSaveablesToStack(objects, root.GetSaveableRefs(saveType), toVisit); while (toVisit.Count != 0) { ISaveable next = toVisit.Pop(); if (objects.ContainsKey(next.GetId())) { throw new InvalidOperationException("Cannot add same object again"); } if (next.GetId() != VALUE_TYPE_ID) { objects.Add(next.GetId(), next); } List <ISaveable> refs = next.GetSaveableRefs(saveType); addUnvisitedSaveablesToStack(objects, refs, toVisit); } return(objects); }
public static LoadResponse Load(BinaryReader reader, SaveType saveType) { /* 0 - Depersist object table. */ uint rootId = 0; bool rootFound = false; Dictionary <uint, ISaveable> objectTable = new Dictionary <uint, ISaveable>(); int count = reader.ReadInt32(); for (int i = 0; i < count; i++) { uint objectId = reader.ReadUInt32(); String assemblyQualifiedTypeName = reader.ReadString(); bool isRoot = reader.ReadBoolean(); if (isRoot && rootFound) { throw new NotSupportedException("More than one root object defined in the save file!"); } else if (isRoot) { rootFound = true; rootId = objectId; } ISaveable loadedObject = constructType(assemblyQualifiedTypeName, objectId); loadedObject.Load(reader, saveType); objectTable.Add(loadedObject.GetId(), loadedObject); } if (rootFound == false) { throw new NotSupportedException("No root object defined in the save file!"); } /* 1 - Inflate all objects */ inflateObjects(objectTable, saveType); /* 2 - Construct the result object */ List <ISaveable> values = new List <ISaveable>(); foreach (ISaveable saveable in objectTable.Values) { values.Add(saveable); } LoadResponse response = new LoadResponse(); response.root = objectTable[rootId]; response.objects = values; /* 3 - Return the response object */ return(response); }