示例#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();
        }
示例#2
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);
        }
示例#3
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();
        }