예제 #1
0
        public static void DeserializeState(RecallableState S, Boolean ReinstantiatePast)
        {
            if (ReinstantiatePast)
            {
                ScriptProcessor.PastStates.Clear();
                ScriptProcessor.PastStates.Push(S);
            }
            ArrayList         ReconstructEnts = new ArrayList();
            IFormatter        SerFormatter    = new BinaryFormatter();
            SurrogateSelector SS = new SurrogateSelector();

            Surrogates.Vector2SS    V2SS  = new Surrogates.Vector2SS();
            Surrogates.PointSS      PSS   = new Surrogates.PointSS();
            Surrogates.RectangleSS  RSS   = new Surrogates.RectangleSS();
            Surrogates.Texture2DSS  T2DSS = new Surrogates.Texture2DSS();
            Surrogates.ColorSS      CSS   = new Surrogates.ColorSS();
            Surrogates.SpriteFontSS SFSS  = new Surrogates.SpriteFontSS();
            SS.AddSurrogate(typeof(Vector2), new StreamingContext(StreamingContextStates.All), V2SS);
            SS.AddSurrogate(typeof(Point), new StreamingContext(StreamingContextStates.All), PSS);
            SS.AddSurrogate(typeof(Rectangle), new StreamingContext(StreamingContextStates.All), RSS);
            SS.AddSurrogate(typeof(Texture2D), new StreamingContext(StreamingContextStates.All), T2DSS);
            SS.AddSurrogate(typeof(Color), new StreamingContext(StreamingContextStates.All), CSS);
            SS.AddSurrogate(typeof(SpriteFont), new StreamingContext(StreamingContextStates.All), SFSS);
            SerFormatter.SurrogateSelector = SS;
            foreach (byte[] Br in S.SerializedEnts)
            {
                MemoryStream DecodeStream = new MemoryStream(Br);
                WorldEntity  WE           = (WorldEntity)SerFormatter.Deserialize(DecodeStream);
                DecodeStream.Close();
                ReconstructEnts.Add(WE);
            }
            ArrayList NewUEnts = new ArrayList();
            ArrayList NewREnts = new ArrayList();

            foreach (VoidDel V in MouseLeftClick.GetInvocationList())
            {
                MouseLeftClick -= V;
            }
            RunQueue = new ArrayList();
            foreach (WorldEntity W in NonSerializables)
            {
                W.AddEventTriggers();
            }
            ArrayList TempList = new ArrayList();

            foreach (WorldEntity W in DeleteQueue)
            {
                if (NonSerializables.Contains(W))
                {
                    TempList.Add(W);
                }
            }
            DeleteQueue = TempList;
            foreach (WorldEntity W in ReconstructEnts)
            {
                if (S.UpdateIDs.Contains(W.EntityID))
                {
                    NewUEnts.Add(W);
                }
                if (S.RenderIDs.Contains(W.EntityID))
                {
                    NewREnts.Add(W);
                }
                W.ReissueID();
                W.OnDeserializeDo();
            }
            ScriptProcessor.LabelEntity = S.LabelEntity;
            if (ScriptProcessor.SongCom != S.SongCom && S.SongCom != null && S.SongCom.Split('|').Length > 1)
            {
                ScriptProcessor.ActivateScriptElement(S.SongCom);
            }
            if (S.Flags != null)
            {
                Flags = (Hashtable)S.Flags.Clone();
            }
            foreach (WorldEntity W in UpdateQueue)
            {
                if (NonSerializables.Contains(W))
                {
                    NewUEnts.Add(W);
                }
            }
            foreach (WorldEntity W in RenderQueue)
            {
                if (NonSerializables.Contains(W))
                {
                    NewREnts.Add(W);
                }
            }
            UpdateQueue = new ArrayList(NewUEnts);
            RenderQueue = new ArrayList(NewREnts);
            foreach (WorldEntity W in ReconstructEnts)
            {
                W.ResubscribeEvents();
            }
            ButtonScripts.UnHideUI();
        }
예제 #2
0
        public static RecallableState?SerializeState(List <WorldEntity> Skip)
        {
            ArrayList         UpdateIDs     = new ArrayList();
            ArrayList         RenderIDs     = new ArrayList();
            ArrayList         SerializedIDs = new ArrayList();
            IFormatter        SerFormatter  = new BinaryFormatter();
            ArrayList         Streams       = new ArrayList();
            SurrogateSelector SS            = new SurrogateSelector();

            Surrogates.Vector2SS    V2SS  = new Surrogates.Vector2SS();
            Surrogates.PointSS      PSS   = new Surrogates.PointSS();
            Surrogates.RectangleSS  RSS   = new Surrogates.RectangleSS();
            Surrogates.Texture2DSS  T2DSS = new Surrogates.Texture2DSS();
            Surrogates.ColorSS      CSS   = new Surrogates.ColorSS();
            Surrogates.SpriteFontSS SFSS  = new Surrogates.SpriteFontSS();
            SS.AddSurrogate(typeof(Vector2), new StreamingContext(StreamingContextStates.All), V2SS);
            SS.AddSurrogate(typeof(Point), new StreamingContext(StreamingContextStates.All), PSS);
            SS.AddSurrogate(typeof(Rectangle), new StreamingContext(StreamingContextStates.All), RSS);
            SS.AddSurrogate(typeof(Texture2D), new StreamingContext(StreamingContextStates.All), T2DSS);
            SS.AddSurrogate(typeof(Color), new StreamingContext(StreamingContextStates.All), CSS);
            SS.AddSurrogate(typeof(SpriteFont), new StreamingContext(StreamingContextStates.All), SFSS);
            SerFormatter.SurrogateSelector = SS;
            try
            {
                foreach (WorldEntity W in UpdateQueue)
                {
                    if (Skip.Contains(W))
                    {
                        continue;
                    }
                    UpdateIDs.Add(W.EntityID);
                    W.OnSerializeDo();
                    MemoryStream EntityStream = new MemoryStream();
                    SerFormatter.Serialize(EntityStream, W);
                    EntityStream.Close();
                    Streams.Add(EntityStream.ToArray());
                    SerializedIDs.Add(W.EntityID);
                }
                foreach (WorldEntity W in RenderQueue)
                {
                    if (Skip.Contains(W))
                    {
                        continue;
                    }
                    RenderIDs.Add(W.EntityID);
                    if (!SerializedIDs.Contains(W.EntityID))
                    {
                        W.OnSerializeDo();
                        MemoryStream EntityStream = new MemoryStream();
                        SerFormatter.Serialize(EntityStream, W);
                        EntityStream.Close();
                        Streams.Add(EntityStream.ToArray());
                        SerializedIDs.Add(W.EntityID);
                    }
                }
            }
            catch (Exception e)
            {
                WriteLine("Failed to serialize state due to " + e.GetType().Name + ": " + e.Message);
                return(null);
            }
            RecallableState Out = new RecallableState();

            Out.RenderIDs      = RenderIDs.ToArray().Select(x => (ulong)x).ToArray();
            Out.UpdateIDs      = UpdateIDs.ToArray().Select(x => (ulong)x).ToArray();
            Out.SerializedEnts = Streams.ToArray().Select(x => (byte[])x).ToArray();
            Out.LabelEntity    = ScriptProcessor.LabelEntity;
            Out.SongCom        = ScriptProcessor.SongCom;
            Out.Flags          = (Hashtable)Flags.Clone();
            return(Out);
        }