Exemple #1
0
        public void Load(Stream stream)
        {
            objects.Clear();
            Dictionary <int, int> localToGlobal = new Dictionary <int, int>();

            StateFormatter bf     = new StateFormatter();
            int            rootId = (int)bf.Deserialize(stream);

            while (stream.Position < stream.Length - 1)
            {
                StateObject obj = (StateObject)bf.Deserialize(stream);
                StateContainer.SetManager(obj, this);
                obj.AfterDeserialization();

                if (obj.ID == -1)
                {
                    localToGlobal.Add(obj.localID, objects.Add(obj));
                }
                else
                {
                    objects.Add(obj);
                }
            }

            root = objects[rootId] as WorldState;
            objects.Resolve(localToGlobal);

            UpdateDebugInfo();
        }
Exemple #2
0
        public Stream GetChanges(int turns)
        {
            List <SyncObject> changed;

            if (manageGlobalAddressing)
            {
                changed = changeRecord.GetSyncObjects(turns + 1);
            }
            else
            {
                changed = new List <SyncObject>();
                changed.AddRange(objects.Compare(syncCopy));
                foreach (int key in added.Keys)
                {
                    changed.Add(new SyncObject(key, SyncCode.NewLocal));
                }
            }

            Stream         s  = new MemoryStream();
            StateFormatter bf = new StateFormatter();

            foreach (SyncObject syncObj in changed)
            {
                bf.Serialize(s, syncObj.Code);

                switch (syncObj.Code)
                {
                case SyncCode.NewLocal:
                    bf.Serialize(s, added[syncObj.Key]);
                    break;

                case SyncCode.New:
                case SyncCode.Update:
                    bf.Serialize(s, objects[syncObj.Key]);
                    break;

                case SyncCode.Delete:
                    bf.Serialize(s, syncObj.Key);
                    break;

                default:
                    throw new BurntimeLogicException();
                }
            }

            bf.Serialize(s, SyncCode.End);

            s.Seek(0, SeekOrigin.Begin);

            return(s);
        }
Exemple #3
0
        public void Save(Stream stream)
        {
            StateFormatter bf = new StateFormatter();

            bf.Serialize(stream, root.ID);
            foreach (StateObject state in objects.Values)
            {
                bf.Serialize(stream, state);
            }
            foreach (StateObject state in added.Values)
            {
                bf.Serialize(stream, state);
            }

            stream.Seek(0, SeekOrigin.Begin);
        }
Exemple #4
0
        public Stream GetAllStates()
        {
            Stream         s  = new MemoryStream();
            StateFormatter bf = new StateFormatter();

            bf.Serialize(s, SyncCode.RootId);
            bf.Serialize(s, root.ID);

            foreach (StateObject obj in objects.Values)
            {
                bf.Serialize(s, SyncCode.New);
                bf.Serialize(s, obj);
            }

            bf.Serialize(s, SyncCode.End);

            s.Seek(0, SeekOrigin.Begin);
            return(s);
        }
Exemple #5
0
        public Stream UpdateMain(Stream Sync)
        {
            MemoryStream localToGlobalIdResult = new MemoryStream();

            StateFormatter bf = new StateFormatter();

            int rootId = -1;

            SyncCode code = (SyncCode)bf.Deserialize(Sync);

            while (code != SyncCode.End)
            {
                switch (code)
                {
                case SyncCode.NewLocal:
                {
                    object      obj   = bf.Deserialize(Sync);
                    StateObject state = (StateObject)obj;
                    state.ID = -1;
                    SetContainerLinks(state);
                    state.AfterDeserialization();
                    added.Add(state);
                }
                break;

                case SyncCode.Update:
                {
                    object      obj   = bf.Deserialize(Sync);
                    StateObject state = (StateObject)obj;
                    SetContainerLinks(state);
                    state.AfterDeserialization();
                    if (objects.ContainsKey(state.ID))
                    {
                        objects[state.ID] = state;
                    }
                    else
                    {
                        added.Remove(state.localID);
                        objects.Add(state);
                    }
                }
                break;

                case SyncCode.RootId:
                    rootId = (int)bf.Deserialize(Sync);
                    objects.Clear();
                    added.Clear();
                    syncCopy.Clear();
                    break;

                default:
                    throw new InvalidOperationException();
                }

                code = (SyncCode)bf.Deserialize(Sync);
            }

            if (rootId != -1)
            {
                root = objects[rootId] as WorldState;
            }

            UpdateDebugInfo();

            ApplyIDs(localToGlobalIdResult);
            localToGlobalIdResult.Seek(0, SeekOrigin.Begin);
            return(localToGlobalIdResult);
        }
Exemple #6
0
        public void Update(Stream sync)
        {
            StateFormatter bf = new StateFormatter();

            List <object> list = new List <object>();

            int rootId = -1;

            Burntime.Platform.Log.Debug("begin sync");

            int countNew    = 0;
            int countUpdate = 0;
            int countDelete = 0;

            SyncCode code = (SyncCode)bf.Deserialize(sync);

            while (code != SyncCode.End)
            {
                switch (code)
                {
                case SyncCode.New:
                {
                    object obj = bf.Deserialize(sync);
                    list.Add(obj);
                    StateObject state = (StateObject)obj;
                    if (state.ID == -1)
                    {
                        throw new Exception("unvalid ID");
                    }

                    SetContainerLinks(state);
                    state.AfterDeserialization();

                    if (objects.ContainsKey(state.ID))
                    {
                        objects[state.ID] = state;
                    }
                    else
                    {
                        objects.Add(state);
                    }

                    countNew++;
                    // Burntime.Platform.Log.Debug("update new: " + state);
                }
                break;

                case SyncCode.Update:
                {
                    object obj = bf.Deserialize(sync);
                    list.Add(obj);
                    StateObject state = (StateObject)obj;
                    if (state.ID == -1)
                    {
                        throw new Exception("unvalid ID");
                    }

                    SetContainerLinks(state);
                    state.AfterDeserialization();
                    if (objects.ContainsKey(state.ID))
                    {
                        objects[state.ID] = state;
                    }
                    else
                    {
                        added.Remove(state.localID);
                        objects.Add(state);
                    }

                    countUpdate++;
                    // Burntime.Platform.Log.Debug("update update: " + state);
                }
                break;

                case SyncCode.Delete:
                {
                    int key = (int)bf.Deserialize(sync);
                    if (objects.ContainsKey(key))
                    {
                        //     Burntime.Platform.Log.Debug("update delete: " + objects[key]);
                        objects.Remove(key);
                    }

                    countDelete++;
                }
                break;

                case SyncCode.RootId:
                    rootId = (int)bf.Deserialize(sync);
                    objects.Clear();
                    added.Clear();
                    syncCopy.Clear();
                    break;
                }

                code = (SyncCode)bf.Deserialize(sync);
            }

            if (rootId != -1)
            {
                root = objects[rootId] as WorldState;
            }

            // CheckConsistency(); // DEBUG
            UpdateDebugInfo();
        }