Ejemplo n.º 1
0
    public static bool SaveGame(SaveGame saveGame, string name)
    {
        BinaryFormatter   formatter         = new BinaryFormatter();
        SurrogateSelector surrogateSelector = new SurrogateSelector();

        //Adiciona a serialização das classes Vector3 e Quaternion
        Vector3SerializationSurrogate vector3SS = new Vector3SerializationSurrogate();

        surrogateSelector.AddSurrogate(typeof(Vector3), new StreamingContext(StreamingContextStates.All), vector3SS);
        QuaternionSerializationSurrogate quatSS = new QuaternionSerializationSurrogate();

        surrogateSelector.AddSurrogate(typeof(Quaternion), new StreamingContext(StreamingContextStates.All), quatSS);

        formatter.SurrogateSelector = surrogateSelector;


        using (FileStream stream = new FileStream(GetSavePath(name), FileMode.Create))
        {
            try
            {
                formatter.Serialize(stream, saveGame);
            }
            catch (Exception e)
            {
                Debug.LogError("Object of type " + saveGame.GetType() + " could not be serialized");
                Debug.LogError(e);
                return(false);
            }
        }

        return(true);
    }
Ejemplo n.º 2
0
        public static object Deserialize(byte[] bytes)
        {
            var formatter = new BinaryFormatter();

            var selector = new SurrogateSelector();
            var context  = new StreamingContext(StreamingContextStates.All);

            selector.AddSurrogate(typeof(UnityEngine.Vector4), context, new Vector4_SerializationSurrogate());
            selector.AddSurrogate(typeof(UnityEngine.Vector3), context, new Vector3_SerializationSurrogate());
            formatter.SurrogateSelector = selector;

            var mem = new MemoryStream(bytes);

            object result;

            try
            {
                result = formatter.Deserialize(mem);
            }
            finally
            {
                mem.Close();
            }
            return(result);
        }
 static SerializationManager()
 {
     Selector.AddSurrogate(typeof(Color32), new StreamingContext(StreamingContextStates.All), new Color32Surrogate());
     Selector.AddSurrogate(typeof(Vector3), new StreamingContext(StreamingContextStates.All), new Vector3Surrogate());
     Selector.AddSurrogate(typeof(Vector2), new StreamingContext(StreamingContextStates.All), new Vector2Surrogate());
     Selector.AddSurrogate(typeof(Quaternion), new StreamingContext(StreamingContextStates.All), new QuaternionSurrogate());
 }
Ejemplo n.º 4
0
    public static BinaryFormatter GetBinaryFormatter()
    {
        BinaryFormatter bf = new BinaryFormatter();

        SurrogateSelector surrogateSelector = new SurrogateSelector();

        Vector3IntSerializationSurrogate vector3SS = new Vector3IntSerializationSurrogate();

        surrogateSelector.AddSurrogate(typeof(Vector3Int), new StreamingContext(StreamingContextStates.All), vector3SS);
        ColorSerializationSurrogate colour3SS = new ColorSerializationSurrogate();

        surrogateSelector.AddSurrogate(typeof(Color), new StreamingContext(StreamingContextStates.All), colour3SS);
        BuffSerializationSurrogate buffSerializationSurrogate = new BuffSerializationSurrogate();

        surrogateSelector.AddSurrogate(typeof(Buff), new StreamingContext(StreamingContextStates.All), buffSerializationSurrogate);
        CardSerializationSurrogate cardSerializationSurrogate = new CardSerializationSurrogate();

        surrogateSelector.AddSurrogate(typeof(Card), new StreamingContext(StreamingContextStates.All), cardSerializationSurrogate);
        EquipmentSerializationSurrogate equipmentSerializationSurrogate = new EquipmentSerializationSurrogate();

        surrogateSelector.AddSurrogate(typeof(Equipment), new StreamingContext(StreamingContextStates.All), equipmentSerializationSurrogate);

        bf.SurrogateSelector = surrogateSelector;

        return(bf);
    }
Ejemplo n.º 5
0
        public static T Clone <T> (T reference) where T : class, ICloneable
        {
            if (reference == null)
            {
                return(null);
            }

            T clone = default(T);
            BinaryFormatter   formater  = new BinaryFormatter();
            MemoryStream      stream    = new MemoryStream();
            SurrogateSelector surrogate = new SurrogateSelector();

            /* Using surrogates because Cairo structures aren't serializable */
            surrogate.AddSurrogate(typeof(Cairo.PointD),
                                   new StreamingContext(StreamingContextStates.All),
                                   new PointDSerializationSurrogate());
            surrogate.AddSurrogate(typeof(Cairo.Color),
                                   new StreamingContext(StreamingContextStates.All),
                                   new ColorSerializationSurrogate());

            formater.SurrogateSelector = surrogate;
            formater.Serialize(stream, reference);
            stream.Flush();
            stream.Position = 0;
            clone           = ((T)formater.Deserialize(stream));

            return((T)clone);
        }
Ejemplo n.º 6
0
    private static BinaryFormatter CreateBinaryFormatter()
    {
        BinaryFormatter   form = new BinaryFormatter();
        SurrogateSelector ss   = new SurrogateSelector();

        {
            Vector3IntSerializationSurrogate v3iss = new Vector3IntSerializationSurrogate();
            ss.AddSurrogate(typeof(Vector3Int),
                            new StreamingContext(StreamingContextStates.All),
                            v3iss);
        }
        {
            Vector3SerializationSurrogate v3ss = new Vector3SerializationSurrogate();
            ss.AddSurrogate(typeof(Vector3),
                            new StreamingContext(StreamingContextStates.All),
                            v3ss);
        }
        {
            QuaternionSerializationSurrogate qss = new QuaternionSerializationSurrogate();
            ss.AddSurrogate(typeof(Quaternion),
                            new StreamingContext(StreamingContextStates.All),
                            qss);
        }
        {
            Vector2SerializationSurrogate v2ss = new Vector2SerializationSurrogate();
            ss.AddSurrogate(typeof(Vector2),
                            new StreamingContext(StreamingContextStates.All),
                            v2ss);
        }

        form.SurrogateSelector = ss;
        return(form);
    }
        void ReadData()
        {
            StreamingContext  context = new StreamingContext(StreamingContextStates.Other);
            SurrogateSelector sel     = new SurrogateSelector();

            sel.AddSurrogate(typeof(Point), context, new PointSurrogate());
            sel.AddSurrogate(typeof(FalseISerializable), context, new FalseISerializableSurrogate());

            BinaryFormatter f = new BinaryFormatter(sel, context);

            object list = f.Deserialize(ms);

            object[][] originalMsgData = null;
            IMessage[] calls           = null;
            IMessage[] resps           = null;

            originalMsgData = ProcessMessages(null, null);

            calls = new IMessage[originalMsgData.Length];
            resps = new IMessage[originalMsgData.Length];


            for (int n = 0; n < originalMsgData.Length; n++)
            {
                calls[n] = (IMessage)f.Deserialize(ms);
                resps[n] = (IMessage)f.DeserializeMethodResponse(ms, null, (IMethodCallMessage)calls[n]);
            }

            f.Binder = new TestBinder();
            object btbob = f.Deserialize(ms);

            ms.Close();

            List expected = CreateTestData();
            List actual   = (List)list;

            expected.CheckEquals(actual, "List");

            for (int i = 0; i < actual.children.Length - 1; ++i)
            {
                if (actual.children [i].next != actual.children [i + 1])
                {
                    Assert.Fail("Deserialization did not restore pointer graph");
                }
            }

            BinderTester_A bta = CreateBinderTestData();

            Assert.AreEqual(btbob.GetType(), typeof(BinderTester_B), "BinderTest.class");
            BinderTester_B btb = btbob as BinderTester_B;

            if (btb != null)
            {
                Assert.AreEqual(btb.x, bta.x, "BinderTest.x");
                Assert.AreEqual(btb.y, bta.y, "BinderTest.y");
            }

            CheckMessages("MethodCall", originalMsgData, ProcessMessages(null, calls));
            CheckMessages("MethodResponse", originalMsgData, ProcessMessages(null, resps));
        }
Ejemplo n.º 8
0
    public static void Save(System.Object obj, string path)
    {
        try
        {
            IFormatter        formatter = new BinaryFormatter();
            SurrogateSelector ss        = new SurrogateSelector();
            ss.AddSurrogate(typeof(Vector3), new StreamingContext(StreamingContextStates.All), new Vector3SerializationSurrogate());
            ss.AddSurrogate(typeof(Quaternion), new StreamingContext(StreamingContextStates.All), new QuaternionSerializationSurrogate());
            ss.AddSurrogate(typeof(BigInteger), new StreamingContext(StreamingContextStates.All), new BigIntegerSerializationSurrogate());
            formatter.SurrogateSelector = ss;

            Stream stream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None);
            formatter.Serialize(stream, obj);
            stream.Close();
        }
        catch (System.Exception e)
        {
            Debug.Log(e.ToString());
            try
            {
                File.Delete(path);
            }
            catch (System.Exception f)
            {
            }
        }
    }
Ejemplo n.º 9
0
        public void AddSurrogate_InvalidArguments_ThrowExceptions()
        {
            var s = new SurrogateSelector();

            AssertExtensions.Throws <ArgumentNullException>("type", () => s.AddSurrogate(null, new StreamingContext(), new NonSerializablePairSurrogate()));
            AssertExtensions.Throws <ArgumentNullException>("surrogate", () => s.AddSurrogate(typeof(NonSerializablePair <int, string>), new StreamingContext(), null));
        }
Ejemplo n.º 10
0
    private static void AddSurrogates(ref SurrogateSelector ss)
    {
        Vector2Surrogate Vector2_SS = new Vector2Surrogate();

        ss.AddSurrogate(typeof(Vector2),
                        new StreamingContext(StreamingContextStates.All),
                        Vector2_SS);
        Vector3Surrogate Vector3_SS = new Vector3Surrogate();

        ss.AddSurrogate(typeof(Vector3),
                        new StreamingContext(StreamingContextStates.All),
                        Vector3_SS);
        Vector4Surrogate Vector4_SS = new Vector4Surrogate();

        ss.AddSurrogate(typeof(Vector4),
                        new StreamingContext(StreamingContextStates.All),
                        Vector4_SS);
        ColorSurrogate Color_SS = new ColorSurrogate();

        ss.AddSurrogate(typeof(Color),
                        new StreamingContext(StreamingContextStates.All),
                        Color_SS);
        QuaternionSurrogate Quaternion_SS = new QuaternionSurrogate();

        ss.AddSurrogate(typeof(Quaternion),
                        new StreamingContext(StreamingContextStates.All),
                        Quaternion_SS);
    }
Ejemplo n.º 11
0
        public void ISerializationSurrogate()
        {
            dbContext.Configuration.ProxyCreationEnabled = false;

            var streamingContext  = new StreamingContext(StreamingContextStates.All, dbContext);
            var surrogateSelector = new SurrogateSelector();

            surrogateSelector.AddSurrogate(
                typeof(Order_Detail),
                streamingContext,
                new OrderDetailSerializerSurrogate());
            surrogateSelector.AddSurrogate(
                typeof(Product),
                streamingContext,
                new ProductSerializerSurrogate());

            var tester = new XmlDataContractSerializerTester <IEnumerable <Order_Detail> >(new NetDataContractSerializer(
                                                                                               streamingContext,
                                                                                               int.MaxValue,
                                                                                               true,
                                                                                               FormatterAssemblyStyle.Simple,
                                                                                               surrogateSelector
                                                                                               ), true);

            var orderDetails = dbContext.Order_Details.ToList();

            tester.SerializeAndDeserialize(orderDetails);
        }
Ejemplo n.º 12
0
        public static T DeepCopy <T>(T target)
        {
            T   result;
            var formatter = new BinaryFormatter();
            var mem       = new MemoryStream();

            var selector = new SurrogateSelector();
            var context  = new StreamingContext(StreamingContextStates.All);

            selector.AddSurrogate(typeof(UnityEngine.LayerMask), context, new LayerMaskSurrogate());
            selector.AddSurrogate(typeof(UnityEngine.GameObject), context, new GameObjectSurrogate());

            formatter.SurrogateSelector = selector;

            try
            {
                formatter.Serialize(mem, target);
                mem.Position = 0;
                result       = (T)formatter.Deserialize(mem);
            }
            finally
            {
                mem.Close();
            }

            return(result);
        }
Ejemplo n.º 13
0
        public static void Save(object obj, string path)
        {
            BinaryFormatter bf   = new BinaryFormatter();
            FileStream      file = File.Create(path);

            if (ss == null)
            {
                CreateSurrogates();
            }

            if (Texture2DCompressionType_old != Texture2DCompressionType)
            {
                ss.RemoveSurrogate(typeof(Texture2D), new StreamingContext(StreamingContextStates.All));

                Texture2DSerializationSurrogate texture_ss = new Texture2DSerializationSurrogate();
                ss.AddSurrogate(typeof(Texture2D), new StreamingContext(StreamingContextStates.All), texture_ss);

                Texture2DCompressionType_old = Texture2DCompressionType;
            }

            bf.SurrogateSelector = ss;

            bf.Serialize(file, obj);
            file.Close();
        }
    /// <summary>
    /// Saves the Level state as completed.
    /// </summary>
    /// <param name="i_path">The World path.</param>
    /// <param name="i_levelPos">The Level position.</param>
    public void CompleteLevel(string i_path, Vector2 i_levelPos)
    {
        //Sets up the FileStream and BinaryFormatter
        FileStream                    file    = File.Open(i_path + Path.DirectorySeparatorChar + "Levels" + Path.DirectorySeparatorChar + (int)i_levelPos.x + "x" + (int)i_levelPos.y + Path.DirectorySeparatorChar + (int)i_levelPos.x + "x" + (int)i_levelPos.y + LEVEL_EXT, FileMode.Open);
        BinaryFormatter               bf      = new BinaryFormatter();
        SurrogateSelector             ss      = new SurrogateSelector();
        Vector2SerializationSurrogate v2Ss    = new Vector2SerializationSurrogate();
        LevelSerializationSurrogate   levelSs = new LevelSerializationSurrogate();
        StreamingContext              sc      = new StreamingContext(StreamingContextStates.All);

        ss.AddSurrogate(typeof(Vector2), sc, v2Ss);
        ss.AddSurrogate(typeof(Level), sc, levelSs);
        bf.SurrogateSelector = ss;

        //Loads the Level, updates completed value, and saves the Level.
        Level auxLevel;

        using (file)
        {
            auxLevel            = ((Level)(bf.Deserialize(file)));
            auxLevel._completed = true;
            file.Position       = 0;
            bf.Serialize(file, auxLevel);
        }
    }
Ejemplo n.º 15
0
        public override void SaveInfo(OAuth2Info infoToSave)
        {
            CheckOrCreateDirectory();

            using (MemoryStream memoryStream = new MemoryStream())
            {
                SurrogateSelector selector = new SurrogateSelector();

                selector.AddSurrogate(typeof(ClientSecrets),
                                      new StreamingContext(StreamingContextStates.All),
                                      new ClientSecretsSurrogate());

                selector.AddSurrogate(typeof(TokenResponse),
                                      new StreamingContext(StreamingContextStates.All),
                                      new TokenResponseSurrogate());

                IFormatter serializer = new BinaryFormatter();

                serializer.SurrogateSelector = selector;

                serializer.Serialize(memoryStream, infoToSave);

                byte[] byteArray      = memoryStream.ToArray();
                byte[] protectedArray = ProtectedData.Protect(byteArray, s_aditionalEntropy,
                                                              DataProtectionScope.CurrentUser);

                System.IO.File.WriteAllBytes(destFile, protectedArray);
            }
        }
Ejemplo n.º 16
0
    bool Load(String s)
    {
        if (!File.Exists(s))
        {
            return(false);
        }
        BinaryFormatter   BF = new BinaryFormatter();
        SurrogateSelector ss = new SurrogateSelector();

        ss.AddSurrogate(typeof(Vector3),
                        new StreamingContext(StreamingContextStates.All),
                        new Vector3SerializationSurrogate());
        ss.AddSurrogate(typeof(Quaternion),
                        new StreamingContext(StreamingContextStates.All),
                        new QuaternionSerializationSurrogate());
        ss.AddSurrogate(typeof(Color),
                        new StreamingContext(StreamingContextStates.All),
                        new ColorSerializationSurrogate());
        BF.SurrogateSelector = ss;
        using (FileStream stream = new FileStream(Application.persistentDataPath + "/" + FileName + ".save", FileMode.Open, FileAccess.Read))
        {
            IS = BF.Deserialize(stream) as InputSaver;
            IS.setParent(this.gameObject.transform.parent.transform.parent.gameObject);
            stream.Close();
        }
        return(true);
    }
Ejemplo n.º 17
0
    //Load spline
    public void LoadInfo(string path)
    {
        if (File.Exists(path))
        {
            BinaryFormatter bf = new BinaryFormatter();

            SurrogateSelector                    ss   = new SurrogateSelector();
            Vector3SerializationSurrogate        v3ss = new Vector3SerializationSurrogate();
            SplineSettingsSerializationSurrogate ssss = new SplineSettingsSerializationSurrogate();
            ss.AddSurrogate(typeof(Vector3),
                            new StreamingContext(StreamingContextStates.All),
                            v3ss);
            ss.AddSurrogate(typeof(SplineSettings),
                            new StreamingContext(StreamingContextStates.All),
                            ssss);
            bf.SurrogateSelector = ss;

            FileStream file = File.Open(path, FileMode.Open);
            splines         = (List <Spline>)bf.Deserialize(file);
            connectedPoints = (List <ControlPoint>)bf.Deserialize(file);
            file.Close();

            OnAfterDeserialize();
            ResetGeneratedContent();
        }
    }
Ejemplo n.º 18
0
        /// <summary>
        /// Create a <see cref="BinaryFormatter"/> instance with necessary binder and surrogates.
        /// </summary>
        /// <returns></returns>
        private static BinaryFormatter CreateBinaryFormatter()
        {
            BinaryFormatter formatter = new BinaryFormatter();

            formatter.Binder = new VersionDeserializationBinder();

            List <Type> types = new List <Type>()
            {
                typeof(EmployeeDefinition),
                typeof(SkillDefinition),
                typeof(ScriptableObject),
                typeof(NameLists),
                typeof(ItemDefinition),
                typeof(MissionDefinition),
                typeof(MissionHook)
            };

            // Create a SurrogateSelector
            SurrogateSelector ss = new SurrogateSelector();

            ss.AddSurrogate(typeof(Vector2Int), new StreamingContext(StreamingContextStates.All), new Vector2IntSerializationSurrogate());
            ss.AddSurrogate(typeof(Color32), new StreamingContext(StreamingContextStates.All), new ColorSerializationSurrogate());
            foreach (var type in types)
            {
                ss.AddSurrogate(type, new StreamingContext(StreamingContextStates.All), new ScriptableObjectSerializationSurrogate());
            }

            formatter.SurrogateSelector = ss;

            return(formatter);
        }
Ejemplo n.º 19
0
 public static void RegisterSurrogates(SurrogateSelector ss)
 {
     ss.AddSurrogate(typeof(global::Cairo.Rectangle), CloneSC, new CairoRectangleSerializationSurrogate());
     ss.AddSurrogate(typeof(global::Cairo.Matrix), CloneSC, new CairoMatrixSerializationSurrogate());
     ss.AddSurrogate(typeof(global::Cairo.SolidPattern), CloneSC, new CairoSolidPatternSerializationSurrogate());
     ss.AddSurrogate(typeof(global::Cairo.Color), CloneSC, new CairoColorSerializationSurrogate());
 }
Ejemplo n.º 20
0
    public static SaveGame LoadGame(string name)
    {
        if (!DoesSaveGameExist(name))
        {
            return(null);
        }

        BinaryFormatter   formatter         = new BinaryFormatter();
        SurrogateSelector surrogateSelector = new SurrogateSelector();

        //Adiciona a serialização das classes Vector3 e Quaternion
        Vector3SerializationSurrogate vector3SS = new Vector3SerializationSurrogate();

        surrogateSelector.AddSurrogate(typeof(Vector3), new StreamingContext(StreamingContextStates.All), vector3SS);
        QuaternionSerializationSurrogate quatSS = new QuaternionSerializationSurrogate();

        surrogateSelector.AddSurrogate(typeof(Quaternion), new StreamingContext(StreamingContextStates.All), quatSS);

        formatter.SurrogateSelector = surrogateSelector;

        using (FileStream stream = new FileStream(GetSavePath(name), FileMode.Open))
        {
            try
            {
                return(formatter.Deserialize(stream) as SaveGame);
            }
            catch (Exception e)
            {
                Debug.LogError(e);
                return(null);
            }
        }
    }
        private static void CreateSurrogateSelector()
        {
            SurrogateSelector = new SurrogateSelector();

            SurrogateSelector.AddSurrogate(typeof(Vector3), new StreamingContext(StreamingContextStates.All), new SerializationSurrogateVector3());
            SurrogateSelector.AddSurrogate(typeof(Vector2), new StreamingContext(StreamingContextStates.All), new SerializationSurrogateVector2());
            SurrogateSelector.AddSurrogate(typeof(Quaternion), new StreamingContext(StreamingContextStates.All), new SerializationSurrogateQuaternion());
        }
Ejemplo n.º 22
0
 public static void Reg(SurrogateSelector selector)
 {
     selector.AddSurrogate(typeof(Vector2), new StreamingContext(StreamingContextStates.All), new SerializationSurrogate_Vector2());
     selector.AddSurrogate(typeof(Vector3), new StreamingContext(StreamingContextStates.All), new SerializationSurrogate_Vector3());
     selector.AddSurrogate(typeof(Vector2Int), new StreamingContext(StreamingContextStates.All), new SerializationSurrogate_Vector2Int());
     selector.AddSurrogate(typeof(Vector3Int), new StreamingContext(StreamingContextStates.All), new SerializationSurrogate_Vector3Int());
     selector.AddSurrogate(typeof(Quaternion), new StreamingContext(StreamingContextStates.All), new SerializationSurrogate_Quaternion());
 }
Ejemplo n.º 23
0
        public BinaryMessageSerializer()
        {
            var surrogateSelector = new SurrogateSelector();

            surrogateSelector.AddSurrogate(typeof(XDocument), new StreamingContext(StreamingContextStates.All), new XContainerSurrogate());
            surrogateSelector.AddSurrogate(typeof(XElement), new StreamingContext(StreamingContextStates.All), new XElementSurrogate());

            binaryFormatter.SurrogateSelector = surrogateSelector;
        }
Ejemplo n.º 24
0
        public static void Save()
        {
            SurrogateSelector surrogateSelector = new SurrogateSelector();

            SerializationSurrogates.Vector2IntSerializationSurrogate vector2I = new SerializationSurrogates.Vector2IntSerializationSurrogate();
            SerializationSurrogates.Vector2SerializationSurrogate    vector2  = new SerializationSurrogates.Vector2SerializationSurrogate();
            SerializationSurrogates.Vector3SerializationSurrogate    vector3  = new SerializationSurrogates.Vector3SerializationSurrogate();
            SerializationSurrogates.Vector3IntSerializationSurrogate vector3I = new SerializationSurrogates.Vector3IntSerializationSurrogate();

            surrogateSelector.AddSurrogate(typeof(Vector2Int), new StreamingContext(StreamingContextStates.All), vector2I);
            surrogateSelector.AddSurrogate(typeof(Vector2), new StreamingContext(StreamingContextStates.All), vector2);
            surrogateSelector.AddSurrogate(typeof(Vector3Int), new StreamingContext(StreamingContextStates.All), vector3I);
            surrogateSelector.AddSurrogate(typeof(Vector3), new StreamingContext(StreamingContextStates.All), vector3);

            XmlDocument xml   = new XmlDocument();
            XmlElement  world = xml.CreateElement("World");

            world.SetAttribute("name", "Test");

            xml.AppendChild(world);

            XmlElement mods = xml.CreateElement("Mods");

            world.AppendChild(mods);

            foreach (var modInstance in ModManager.ModInstances)
            {
                XmlElement mod = xml.CreateElement("Mod");
                mod.SetAttribute("InternalName", modInstance.Value.InternalName);
                mod.SetAttribute("Version", modInstance.Value.Version);
                mods.AppendChild(mod);
            }

            XmlElement blocksXml = xml.CreateElement("Blocks");

            world.AppendChild(blocksXml);

            List <BaseClasses.Block> blocks = World.ReturnAllBlocks();

            foreach (var block in blocks)
            {
                XmlElement blockXml = xml.CreateElement("Block");
                blockXml.SetAttribute("InternalName", block.InternalName);
                blockXml.InnerText = ObjectToString(block, surrogateSelector);
                blocksXml.AppendChild(blockXml);
            }

            XmlElement player = xml.CreateElement("Player");

            player.SetAttribute("InternalName", PlayerRegister.Player.InternalName);
            player.InnerText = ObjectToString(PlayerRegister.Player, surrogateSelector);

            world.AppendChild(player);

            File.WriteAllText(Application.persistentDataPath + "/world.xml", xml.OuterXml);
        }
Ejemplo n.º 25
0
        public Serializer()
        {
            _surrogateSelector = new SurrogateSelector();
            Vector2SurrogateSelector   vector2SS   = new Vector2SurrogateSelector();
            RectangleSurrogateSelector rectangleSS = new RectangleSurrogateSelector();
            ColorSurrogateSelector     colorSS     = new ColorSurrogateSelector();

            _surrogateSelector.AddSurrogate(typeof(Vector2), new StreamingContext(StreamingContextStates.All), vector2SS);
            _surrogateSelector.AddSurrogate(typeof(Rectangle), new StreamingContext(StreamingContextStates.All), rectangleSS);
            _surrogateSelector.AddSurrogate(typeof(Color), new StreamingContext(StreamingContextStates.All), colorSS);
        }
Ejemplo n.º 26
0
 /// <summary>
 /// Gets the surrogate selecteds for System.Drawing.Pen and System.Drawing.Brush
 /// </summary>
 /// <returns>SurrogateSelector</returns>
 public static SurrogateSelector GetSurrogateSelectors()
 {
     SurrogateSelector ss = new SurrogateSelector();
     ss.AddSurrogate(typeof (Pen), new StreamingContext(StreamingContextStates.All), new PenSurrogate());
     ss.AddSurrogate(typeof (SolidBrush), new StreamingContext(StreamingContextStates.All),
                     new SolidBrushSurrogate());
     ss.AddSurrogate(typeof (TextureBrush), new StreamingContext(StreamingContextStates.All),
                     new TextureBrushSurrogate());
     ss.AddSurrogate(typeof (Matrix), new StreamingContext(StreamingContextStates.All), new MatrixSurrogate());
     return ss;
 }
Ejemplo n.º 27
0
        public static BinaryFormatter GetBinaryFormatter()
        {
            BinaryFormatter   binaryFormatter = new BinaryFormatter();
            SurrogateSelector selector        = new SurrogateSelector();

            selector.AddSurrogate(typeof(Vector3), new StreamingContext(StreamingContextStates.All), new Vector3SerializationSurrogates());
            selector.AddSurrogate(typeof(Quaternion), new StreamingContext(StreamingContextStates.All), new QuaternionSerializationSurrogates());

            binaryFormatter.SurrogateSelector = selector;
            return(binaryFormatter);
        }
Ejemplo n.º 28
0
 public static SurrogateSelector GetSurrogateSelector()
 {
     if (ss == null)
     {
         ss      = new SurrogateSelector();
         context = new StreamingContext(StreamingContextStates.All);
         ss.AddSurrogate(typeof(Vector3), context, new Vector3SerializationSurrogate());
         ss.AddSurrogate(typeof(Quaternion), context, new QuaternionSerializationSurrogate());
         ss.AddSurrogate(typeof(Color), context, new ColorSerializationSurrogate());
     }
     return(ss);
 }
Ejemplo n.º 29
0
        public static SurrogateSelector GetSurrogateSelector()
        { // Construct a SurrogateSelector object to serialize normally non serializable classes: Vector3, Vector3Int, Quaternion
            SurrogateSelector                ss    = new SurrogateSelector();
            Vector3SerializationSurrogate    v3ss  = new Vector3SerializationSurrogate();
            Vector3IntSerializationSurrogate v3iss = new Vector3IntSerializationSurrogate();
            QuaternionSerializationSurrogate qss   = new QuaternionSerializationSurrogate();

            ss.AddSurrogate(typeof(Vector3), new StreamingContext(StreamingContextStates.All), v3ss);
            ss.AddSurrogate(typeof(Vector3Int), new StreamingContext(StreamingContextStates.All), v3iss);
            ss.AddSurrogate(typeof(Quaternion), new StreamingContext(StreamingContextStates.All), qss);
            return(ss);
        }
Ejemplo n.º 30
0
    private BinaryFormatter GetFormatter()
    {
        var formatter         = new BinaryFormatter();
        var surrogateSelector = new SurrogateSelector();

        surrogateSelector.AddSurrogate(typeof(Vector3), new StreamingContext(
                                           StreamingContextStates.All), new Vector3Serializer());
        surrogateSelector.AddSurrogate(typeof(Quaternion), new StreamingContext(
                                           StreamingContextStates.All), new QuaternionSerializer());
        formatter.SurrogateSelector = surrogateSelector;
        return(formatter);
    }
Ejemplo n.º 31
0
 public static SaveGame Load()
 {
     BinaryFormatter bf = new BinaryFormatter();
     //surrogate serializer setup.
     SurrogateSelector ss = new SurrogateSelector();
     ColorSerializationSurrogate css = new ColorSerializationSurrogate();
     ss.AddSurrogate(typeof(Color),new StreamingContext(StreamingContextStates.All),css);
     //use surrogate selector.
     bf.SurrogateSelector = ss;
     FileStream file = File.Open(Application.persistentDataPath+"/saveGame.serious",FileMode.Open);
     SaveGame loadedGame = (SaveGame)bf.Deserialize(file);
     file.Close();
     return loadedGame;
 }
Ejemplo n.º 32
0
    public static void Save(SaveGame save)
    {
        BinaryFormatter bf = new BinaryFormatter();
        //surrogate serializer setup.
        SurrogateSelector ss = new SurrogateSelector();
        ColorSerializationSurrogate css = new ColorSerializationSurrogate();
        ss.AddSurrogate(typeof(Color),new StreamingContext(StreamingContextStates.All),css);
        //use surrogate selector.
        bf.SurrogateSelector = ss;

        FileStream file = File.Create(Application.persistentDataPath+"/saveGame.serious");
        bf.Serialize(file, save);

        file.Close ();
        Debug.Log(Application.persistentDataPath+"/saveGame.serious");
    }
Ejemplo n.º 33
0
    //Saving and Loading
    public void SaveData()
    {
        Debug.Log("PlayerInfo Saved");
        PlayerStats.SaveTemp ();
        BinaryFormatter bf = new BinaryFormatter();
        SurrogateSelector ss = new SurrogateSelector();
        FileStream file = File.Create(Application.persistentDataPath + "/" + FileName + ".dat");
        //
        Vector3SerializationSurrogate v3ss = new Vector3SerializationSurrogate();
        ss.AddSurrogate(typeof(Vector3),
                         new StreamingContext(StreamingContextStates.All),
                         v3ss);
        bf.SurrogateSelector = ss;
        //
        PlayerData data = new PlayerData();
        data.PickupCount = Pickups;
        data.ChestCount = ChestsOpened;
        data.SwitchCount = SwitchesUsed;
        data.DeathCount = Deaths;
        data.checkpoint = checkpoint;
        data.playerHealth = playerHealth;
        data.currentscene = currentscene;

        //
        bf.Serialize(file, data);
        file.Close();
    }
	public bool runTest()
	{
		Console.WriteLine(s_strTFPath + " " + s_strTFName + " , for " + s_strClassMethod + " , Source ver " + s_strDtTmVer);
		int iCountErrors = 0;
		int iCountTestcases = 0;
		String strLoc = "Loc_000oo";
		try {
			do
			{
				SurrogateSelector surrSelect;
				SurrogateSelector surrSelect_next;
				TestSerializationClass testclass;
				TestSerializationSurrogate testss, testss1, testss2;
				StreamingContext context = new StreamingContext(StreamingContextStates.CrossMachine);
				ISurrogateSelector ss;
				SurrogateSelector[] arrSelectors;
				Int32 iHowDeep;
				strLoc = "Loc_100aa";
				surrSelect = new SurrogateSelector();
				iCountTestcases++;
				try
				{
					surrSelect.AddSurrogate(this.GetType(), context, null);
					iCountErrors++;
					printerr("Error_100bb! Expected exception not thrown");
				}
				catch (ArgumentException){}
				catch (Exception exc)
				{
					iCountErrors++;
					printerr("Error_100dd! Incorrect Exception thrown: "+exc.ToString());
				}
				strLoc = "Loc_200aa";
				surrSelect = new SurrogateSelector();
				testss = new TestSerializationSurrogate();
				iCountTestcases++;
				try
				{
					surrSelect.AddSurrogate(null, context, testss);
					iCountErrors++;
					printerr("Error_200bb! Expected exception not thrown");
				}
				catch (ArgumentException){}
				catch (Exception exc)
				{
					iCountErrors++;
					printerr("Error_200dd! Incorrect exception thrown: "+exc.ToString());
				}
				strLoc = "Loc_300aa";
				surrSelect = new SurrogateSelector();
				testss = new TestSerializationSurrogate();
				testclass = new TestSerializationClass();
				surrSelect.AddSurrogate(testclass.GetType(), context, testss);
				iCountTestcases++;
				if(!surrSelect.GetSurrogate(testclass.GetType(), context, out ss).Equals(testss))
				{
					iCountErrors++;
					printerr("Error_300bb! Incorrect ISerializationSurrogate returned");
				}
				strLoc = "Loc_350aa";
				testss = new TestSerializationSurrogate();
				testss1 = new TestSerializationSurrogate();
				testss2 = new TestSerializationSurrogate();
				surrSelect = new SurrogateSelector();
				surrSelect.AddSurrogate(typeof(Decimal), context, testss);
				surrSelect.AddSurrogate(typeof(Int64),context, testss1);
				surrSelect.AddSurrogate(typeof(Int32),context, testss2);
				iCountTestcases++;
				if(!surrSelect.GetSurrogate(typeof(Decimal), context, out ss).Equals(testss))
				{
					iCountErrors++;
					printerr("Error_350bb! Incorrect ISerializationSurrogate returned");
				}
				iCountTestcases++;
				if(surrSelect != ss)
				{
					iCountErrors++;
					printerr("Error_0453cd! Incorrect ISurrogateSelector returned");
				}
				iCountTestcases++;
				if(!surrSelect.GetSurrogate(typeof(Int64), context, out ss).Equals(testss1))
				{
					iCountErrors++;
					printerr("Error_350cc! Incorrect ISerializationSurrgate returned");
				}
				iCountTestcases++;
				if(!surrSelect.GetSurrogate(typeof(Int32), context, out ss).Equals(testss2))
				{
					iCountErrors++;
					printerr("Error_350dd! Incorrect IserializationSurrogate returned");
				}
				strLoc = "Loc_370aa";
				testss = new TestSerializationSurrogate();
				testss1 = new TestSerializationSurrogate();
				testss2 = new TestSerializationSurrogate();
				surrSelect = new SurrogateSelector();
				surrSelect.AddSurrogate(typeof(Decimal), context,  testss);
				surrSelect.AddSurrogate(typeof(Int64),context,  testss1);
				surrSelect.AddSurrogate(typeof(Int32),context,  testss2);
				iCountTestcases++;
				if(surrSelect.GetSurrogate(typeof(Int16), context, out ss) != null)
				{
					iCountErrors++;
					printerr("Error_370bb! Type not in surrogateselector was actually found???");
				}
				strLoc = "Loc_400aa";
				surrSelect = new SurrogateSelector();
				testss = new TestSerializationSurrogate();
				testclass = new TestSerializationClass();
				surrSelect.AddSurrogate(testclass.GetType(),context, testss);
				try
				{
					surrSelect.AddSurrogate(testclass.GetType(),context, new TestSerializationSurrogate());
					iCountErrors++;
					printerr("Error_400bb! Expected Exception not thrown");
				}catch (ArgumentException){
				}catch (Exception exc){
					iCountErrors++;
					printerr("Error_400cc! Incorrect Exception thrown : "+exc.ToString());
				}
				surrSelect = new SurrogateSelector();
				surrSelect_next = new SurrogateSelector();
				surrSelect.ChainSelector(surrSelect_next);
				testss = new TestSerializationSurrogate();
				context = new StreamingContext(StreamingContextStates.All);
				surrSelect_next.AddSurrogate(typeof(Decimal), context, testss);
				iCountTestcases++;
				if(!surrSelect.GetSurrogate(typeof(Decimal), context, out ss).Equals(testss))
				{
					iCountErrors++;
					printerr("Error_04582fd! Incorrect ISerializationSurrogate returned");
				}
				iCountTestcases++;
				if(surrSelect == ss)
				{
					iCountErrors++;
					printerr("Error_0478523vdf! Incorrect ISurrogateSelector returned");
				}
				iCountTestcases++;
				if(surrSelect_next != ss)
				{
					iCountErrors++;
					printerr("Error_934721cd! Incorrect ISurrogateSelector returned");
				}
				iHowDeep = 100;
				arrSelectors = new SurrogateSelector[iHowDeep];
				arrSelectors[0] = new SurrogateSelector();
				for(int i=1;i<iHowDeep;i++){
					arrSelectors[i] = new SurrogateSelector();
					arrSelectors[0].ChainSelector(arrSelectors[i]);
				}
				testss = new TestSerializationSurrogate();
				context = new StreamingContext(StreamingContextStates.All);
				arrSelectors[1].AddSurrogate(typeof(Decimal), context, testss);
				iCountTestcases++;
				if(!arrSelectors[0].GetSurrogate(typeof(Decimal), context, out ss).Equals(testss))
				{
					iCountErrors++;
					printerr("Error_743cd! Incorrect ISerializationSurrogate returned");
				}
				iCountTestcases++;
				if(arrSelectors[1] != ss)
				{
					iCountErrors++;
					printerr("Error_10573cd! Incorrect ISurrogateSelector returned");
				}
				arrSelectors[0].AddSurrogate(typeof(Decimal), context, testss);
				iCountTestcases++;
				if(!arrSelectors[0].GetSurrogate(typeof(Decimal), context, out ss).Equals(testss))
				{
					iCountErrors++;
					printerr("Error_0472ds! Incorrect ISerializationSurrogate returned");
				}
				iCountTestcases++;
				if(arrSelectors[0] != ss)
				{
					iCountErrors++;
					printerr("Error_0342dfg! Incorrect ISurrogateSelector returned");
				}
			} while (false);
			} catch (Exception exc_general ) {
			++iCountErrors;
			Console.WriteLine (s_strTFAbbrev + " : Error Err_8888yyy!  strLoc=="+ strLoc +", exc_general=="+exc_general.ToString());
			Console.WriteLine(exc_general.StackTrace);
		}
		if ( iCountErrors == 0 )
		{
			Console.WriteLine( "paSs.   "+s_strTFPath +" "+s_strTFName+" ,iCountTestcases=="+iCountTestcases);
			return true;
		}
		else
		{
			Console.WriteLine("FAiL!   "+s_strTFPath+" "+s_strTFName+" ,iCountErrors=="+iCountErrors+" , BugNums?: "+s_strActiveBugNums );
			return false;
		}
	}
Ejemplo n.º 35
0
    static void CreateSurrogates()
    {
        ss = new SurrogateSelector();

        //VECTOR2
        Vector2SerializationSurrogate v2_ss = new Vector2SerializationSurrogate();
        ss.AddSurrogate(typeof(Vector2), new StreamingContext(StreamingContextStates.All), v2_ss);
        //VECTOR3
        Vector3SerializationSurrogate v3_ss = new Vector3SerializationSurrogate();
        ss.AddSurrogate(typeof(Vector3), new StreamingContext(StreamingContextStates.All), v3_ss);
        //VECTOR4
        Vector4SerializationSurrogate v4_ss = new Vector4SerializationSurrogate();
        ss.AddSurrogate(typeof(Vector4), new StreamingContext(StreamingContextStates.All), v4_ss);

        //QUATERNION
        QuaternionSerializationSurrogate q_ss = new QuaternionSerializationSurrogate();
        ss.AddSurrogate(typeof(Quaternion), new StreamingContext(StreamingContextStates.All), q_ss);

        //COLOR AND COLOR32
        ColorSerializationSurrogate color_ss = new ColorSerializationSurrogate();
        ss.AddSurrogate(typeof(Color), new StreamingContext(StreamingContextStates.All), color_ss);
        ss.AddSurrogate(typeof(Color32), new StreamingContext(StreamingContextStates.All), color_ss);

        //TEXTURE2D
        Texture2DSerializationSurrogate texture_ss = new Texture2DSerializationSurrogate();
        ss.AddSurrogate(typeof(Texture2D), new StreamingContext(StreamingContextStates.All), texture_ss);

        Texture2DCompressionType_old = Texture2DCompressionType;
    }
Ejemplo n.º 36
0
	public bool runTest()
	{
		Console.WriteLine(s_strTFPath + " " + s_strTFName + " , for " + s_strClassMethod + " , Source ver " + s_strDtTmVer);
		int iCountErrors = 0;
		int iCountTestcases = 0;
		String strLoc = "Loc_000oo";
		try {
			do
			{
				SurrogateSelector surrSelect1, surrSelect2, surrSelect3, surrSelect4, surrSelect5;
				TestSerializationClass testclass;
				TestSerializationSurrogate testss1, testss2, testss3, testss4, testss5;
				StreamingContext context = new StreamingContext(StreamingContextStates.CrossMachine);
				ISurrogateSelector ss;
				strLoc = "Loc_100aa";
				surrSelect1 = new SurrogateSelector();
				iCountTestcases++;
				try
				{
					surrSelect1.GetSurrogate(null, context, out ss);
					iCountErrors++;
					printerr("Error_100bb!! Expected ArgumentException not thrown");
				}
				catch (ArgumentException){}
				catch (Exception exc)
				{
					iCountErrors++;
					printerr("Error_100dd!! Incorrect Exception thrown : "+exc.ToString());
				}
				strLoc = "Loc_300aa";
				surrSelect1 = new SurrogateSelector();
				testss1= new TestSerializationSurrogate();
				testclass = new TestSerializationClass();
				surrSelect1.AddSurrogate(testclass.GetType(), context, testss1);
				iCountTestcases++;
				if(!surrSelect1.GetSurrogate(testclass.GetType(), context, out ss).Equals(testss1))
				{
					iCountErrors++;
					printerr("Error_300bb! Incorrect ISerializationSurrogate returned");
				}
				strLoc = "Loc_350aa";
				testss1= new TestSerializationSurrogate();
				testss2 = new TestSerializationSurrogate();
				testss3 = new TestSerializationSurrogate();
				surrSelect1 = new SurrogateSelector();
				surrSelect1.AddSurrogate(typeof(Int64), context, testss2);
				surrSelect1.AddSurrogate(typeof(Int32), context, testss3);
				iCountTestcases++;
				if(!surrSelect1.GetSurrogate(typeof(Int64), context, out ss).Equals(testss2))
				{
					iCountErrors++;
					printerr("Error_350cc! Incorrect ISerializationSurrgate returned");
				}
				iCountTestcases++;
				if(!surrSelect1.GetSurrogate(typeof(Int32), context, out ss).Equals(testss3))
				{
					iCountErrors++;
					printerr("Error_350dd! Incorrect IserializationSurrogate returned");
				}
				strLoc = "Loc_370aa";
				testss1= new TestSerializationSurrogate();
				testss2 = new TestSerializationSurrogate();
				testss3 = new TestSerializationSurrogate();
				surrSelect1 = new SurrogateSelector();
				surrSelect1.AddSurrogate(typeof(Int64), context,  testss2);
				surrSelect1.AddSurrogate(typeof(Int32), context,  testss3);
				strLoc = "Loc_400aa";
				surrSelect1 = new SurrogateSelector();
				surrSelect2 = new SurrogateSelector();
				surrSelect3 = new SurrogateSelector();
				surrSelect4 = new SurrogateSelector();
				surrSelect5 = new SurrogateSelector();
				testss1 = new TestSerializationSurrogate();
				testss2 = new TestSerializationSurrogate();
				testss3 = new TestSerializationSurrogate();
				testss4 = new TestSerializationSurrogate();
				testss5 = new TestSerializationSurrogate();
				surrSelect3.ChainSelector(surrSelect5);
				surrSelect3.ChainSelector(surrSelect4);
				surrSelect2.ChainSelector(surrSelect3);
				surrSelect1.ChainSelector(surrSelect2);
				surrSelect5.AddSurrogate(typeof(Single), context,  testss5);
				iCountTestcases++;
				if(!(surrSelect1.GetSurrogate(typeof(Single), context, out ss).Equals(testss5)))
				{
					iCountErrors++;
					printerr("Error_400bb!! Did not find the surrogate through the chain");
				}
				iCountTestcases++;
				if(!(surrSelect3.GetSurrogate(typeof(Single), context, out ss).Equals(testss5)))
				{
					iCountErrors++;
					printerr("Error_400cc!! Did not find the surrogate through the chain");
				}
				iCountTestcases++;
				if(!(surrSelect4.GetSurrogate(typeof(Single), context, out ss).Equals(testss5)))
				{
					iCountErrors++;
					printerr("Error_400dd!! Did not find the surrogate through the chain");
				}
				strLoc = "Loc_401aa";
				surrSelect1.AddSurrogate(typeof(Guid), context,  testss1);
				iCountTestcases++;
				if(!(surrSelect1.GetSurrogate(typeof(Guid), context, out ss).Equals(testss1)))
				{
					iCountErrors++;
					printerr("Error_401bb!! Did not find the surrogate through the chain");
				}
				iCountTestcases++;
				if(surrSelect2.GetSurrogate(typeof(Guid), context, out ss) != null)
				{
					iCountErrors++;
					printerr("Error_401cc!! Found surrogate that is above in the chain");
				}
				strLoc = "Loc_402aa";
				surrSelect2.AddSurrogate(typeof(Decimal), context,  testss2);
				iCountTestcases++;
				if(!(surrSelect1.GetSurrogate(typeof(Decimal), context, out ss).Equals(testss2)))
				{
					iCountErrors++;
					printerr("Error_402bb!! Didn't find surrogate of child");
				}
				strLoc = "Loc_403aa";
				surrSelect3.AddSurrogate(typeof(Int32), context,  testss3);
				iCountTestcases++;
				if(!(surrSelect1.GetSurrogate(typeof(Int32), context, out ss).Equals(testss3)))
				{
					iCountErrors++;
					printerr("Error_403bb!! Didn't find surrogate of child");
				}
				strLoc = "Loc_404aa";
				surrSelect4.AddSurrogate(typeof(Exception), context,  testss4);
				iCountTestcases++;
				if(!(surrSelect1.GetSurrogate(typeof(Exception), context, out ss).Equals(testss4)))
				{
					iCountErrors++;
					printerr("Error_404bb!! Didn't find surrogate of child");
				}
				strLoc = "Loc_405aa";
				if(surrSelect1.GetSurrogate(typeof(Int16), context, out ss) != null)
				{
					iCountErrors++;
					printerr("Error_405bb!! Found surrogate not in chain");
				}
			} while (false);
			} catch (Exception exc_general ) {
			++iCountErrors;
			Console.WriteLine (s_strTFAbbrev + " : Error Err_8888yyy!  strLoc=="+ strLoc +", exc_general=="+exc_general.ToString());
			Console.WriteLine(exc_general.StackTrace);
		}
		if ( iCountErrors == 0 )
		{
			Console.WriteLine( "paSs.   "+s_strTFPath +" "+s_strTFName+" ,iCountTestcases=="+iCountTestcases);
			return true;
		}
		else
		{
			Console.WriteLine("FAiL!   "+s_strTFPath+" "+s_strTFName+" ,iCountErrors=="+iCountErrors+" , BugNums?: "+s_strActiveBugNums );
			return false;
		}
	}
Ejemplo n.º 37
0
   public static void Go() {
      using (var stream = new MemoryStream()) {
         // 1. Construct the desired formatter
         IFormatter formatter = new SoapFormatter();

         // 2. Construct a SurrogateSelector object
         SurrogateSelector ss = new SurrogateSelector();

         // 3. Tell the surrogate selector to use our surrogate for DateTime objects
         ISerializationSurrogate utcToLocalTimeSurrogate = new UniversalToLocalTimeSerializationSurrogate();
#if GetSurrogateForCyclicalReference
         utcToLocalTimeSurrogate = FormatterServices.GetSurrogateForCyclicalReference(utcToLocalTimeSurrogate); 
#endif
         ss.AddSurrogate(typeof(DateTime), formatter.Context, utcToLocalTimeSurrogate);

         // NOTE: AddSurrogate can be called multiple times to register multiple surrogates

         // 4. Tell the formatter to use our surrogate selector
         formatter.SurrogateSelector = ss;

         // Create a DateTime that represents the local time on the machine & serialize it
         DateTime localTimeBeforeSerialize = DateTime.Now;
         formatter.Serialize(stream, localTimeBeforeSerialize);

         // The Soap-formatted stream displays the Universal time as a string to prove it worked
         stream.Position = 0;
         Console.WriteLine(new StreamReader(stream).ReadToEnd());

         // Deserialize the Universal time string & convert it to a local DateTime for this machine
         stream.Position = 0;
         DateTime localTimeAfterDeserialize = (DateTime)formatter.Deserialize(stream);

         // Prove it worked correctly:
         Console.WriteLine("LocalTimeBeforeSerialize ={0}", localTimeBeforeSerialize);
         Console.WriteLine("LocalTimeAfterDeserialize={0}", localTimeAfterDeserialize);
      }
   }
 public bool runTest()
   {
   Console.WriteLine(s_strTFPath + " " + s_strTFName + " , for " + s_strClassMethod + " , Source ver " + s_strDtTmVer);
   int iCountErrors = 0;
   int iCountTestcases = 0;
   String strLoc = "Loc_000oo";
   try {
   do
     {
     SurrogateSelector surrSelect1, surrSelect2, surrSelect3, surrSelect4, surrSelect5;
     TestSerializationSurrogate testss1, testss2, testss3, testss4, testss5;
     StreamingContext context = new StreamingContext(StreamingContextStates.CrossMachine);
     ISurrogateSelector ss;
     SurrogateSelector[] arrSelectors;
     Int32 iHowDeep;
     strLoc = "Loc_100aa";
     surrSelect1 = new SurrogateSelector();
     iCountTestcases++;
     try
       {
       surrSelect1.ChainSelector(null);
       iCountErrors++;
       printerr("Error_100bb!! Expected ArgumentException not thrown");
       }
     catch (ArgumentException){}
     catch (Exception exc)
       {
       iCountErrors++;
       printerr("Error_100dd!! Incorrect Exception thrown : "+exc.ToString());
       }
     strLoc = "Loc_300aa";
     iCountTestcases++;
     surrSelect1 = new SurrogateSelector();
     try
       {
       surrSelect1.ChainSelector(surrSelect1);
       }catch (SerializationException){
       }catch(Exception ex){
       iCountErrors++;
       Console.WriteLine("Err_432fs!! Incorrect Exception thrown : "+ex.ToString());
       }
     if(surrSelect1.GetSurrogate(typeof(Int64), context, out ss) != null)
       {
       iCountErrors++;
       printerr("Error_2139d!!!!");
       }
     strLoc = "Loc_350aa";
     testss1= new TestSerializationSurrogate();
     testss2 = new TestSerializationSurrogate();
     testss3 = new TestSerializationSurrogate();
     surrSelect1 = new SurrogateSelector();
     surrSelect1.AddSurrogate(typeof(Int64), context,  testss2);
     surrSelect1.AddSurrogate(typeof(Int32), context,  testss3);
     iCountTestcases++;
     if(!surrSelect1.GetSurrogate(typeof(Int64), context, out ss).Equals(testss2))
       {
       iCountErrors++;
       printerr("Error_350cc! Incorrect ISerializationSurrgate returned");
       }
     iCountTestcases++;
     if(!surrSelect1.GetSurrogate(typeof(Int32), context, out ss).Equals(testss3))
       {
       iCountErrors++;
       printerr("Error_350dd! Incorrect IserializationSurrogate returned");
       }
     strLoc = "Loc_370aa";
     testss1= new TestSerializationSurrogate();
     testss2 = new TestSerializationSurrogate();
     testss3 = new TestSerializationSurrogate();
     surrSelect1 = new SurrogateSelector();
     surrSelect1.AddSurrogate(typeof(Int64), context,  testss2);
     surrSelect1.AddSurrogate(typeof(Int32), context,  testss3);
     strLoc = "Loc_400aa";
     surrSelect1 = new SurrogateSelector();
     surrSelect2 = new SurrogateSelector();
     surrSelect3 = new SurrogateSelector();
     surrSelect4 = new SurrogateSelector();
     surrSelect5 = new SurrogateSelector();
     testss1 = new TestSerializationSurrogate();
     testss2 = new TestSerializationSurrogate();
     testss3 = new TestSerializationSurrogate();
     testss4 = new TestSerializationSurrogate();
     testss5 = new TestSerializationSurrogate();
     surrSelect3.ChainSelector(surrSelect5);
     surrSelect3.ChainSelector(surrSelect4);
     surrSelect2.ChainSelector(surrSelect3);
     surrSelect1.ChainSelector(surrSelect2);
     surrSelect5.AddSurrogate(typeof(Single), context,  testss5);
     iCountTestcases++;
     if(!(surrSelect1.GetSurrogate(typeof(Single), context, out ss).Equals(testss5)))
       {
       iCountErrors++;
       printerr("Error_400bb!! Did not find the surrogate through the chain");
       }
     iCountTestcases++;
     if(!(surrSelect3.GetSurrogate(typeof(Single), context, out ss).Equals(testss5)))
       {
       iCountErrors++;
       printerr("Error_400cc!! Did not find the surrogate through the chain");
       }
     iCountTestcases++;
     if(!(surrSelect4.GetSurrogate(typeof(Single), context, out ss).Equals(testss5)))
       {
       iCountErrors++;
       printerr("Error_400dd!! Did not find the surrogate through the chain");
       }
     strLoc = "Loc_401aa";
     surrSelect1.AddSurrogate(typeof(Guid), context,  testss1);
     iCountTestcases++;
     if(!(surrSelect1.GetSurrogate(typeof(Guid), context, out ss).Equals(testss1)))
       {
       iCountErrors++;
       printerr("Error_401bb!! Did not find the surrogate through the chain");
       }
     iCountTestcases++;
     if(surrSelect2.GetSurrogate(typeof(Guid), context, out ss) != null)
       {
       iCountErrors++;
       printerr("Error_401cc!! Found surrogate that is above in the chain");
       }
     strLoc = "Loc_402aa";
     surrSelect2.AddSurrogate(typeof(Decimal), context,  testss2);
     iCountTestcases++;
     if(!(surrSelect1.GetSurrogate(typeof(Decimal), context, out ss).Equals(testss2)))
       {
       iCountErrors++;
       printerr("Error_402bb!! Didn't find surrogate of child");
       }
     strLoc = "Loc_403aa";
     surrSelect3.AddSurrogate(typeof(Int32), context,  testss3);
     iCountTestcases++;
     if(!(surrSelect1.GetSurrogate(typeof(Int32), context, out ss).Equals(testss3)))
       {
       iCountErrors++;
       printerr("Error_403bb!! Didn't find surrogate of child");
       }
     strLoc = "Loc_404aa";
     surrSelect4.AddSurrogate(typeof(Exception), context,  testss4);
     iCountTestcases++;
     if(!(surrSelect1.GetSurrogate(typeof(Exception), context, out ss).Equals(testss4)))
       {
       iCountErrors++;
       printerr("Error_404bb!! Didn't find surrogate of child");
       }
     strLoc = "Loc_405aa";
     if(surrSelect1.GetSurrogate(typeof(Int16), context, out ss) != null)
       {
       iCountErrors++;
       printerr("Error_405bb!! Found surrogate not in chain");
       }
     iHowDeep = 10;
     arrSelectors = new SurrogateSelector[iHowDeep];
     arrSelectors[0] = new SurrogateSelector();
     for(int i=1;i<iHowDeep;i++){
     arrSelectors[i] = new SurrogateSelector();
     arrSelectors[0].ChainSelector(arrSelectors[i]);
     }
     iCountTestcases++;
     if(arrSelectors[0].GetNextSelector() != arrSelectors[iHowDeep-1])
       {
       iCountErrors++;
       printerr("Error_03842vdsd! Incorrect ISerializationSurrgate returned");
       }
     for(int i=iHowDeep-1;i>1;i--){
     iCountTestcases++;
     if(arrSelectors[i].GetNextSelector() != arrSelectors[i-1]){
     iCountErrors++;
     printerr("Err_74239cd_" + i + "! Incorrect ISerializationSurrgate returned");
     }
     }
     iCountTestcases++;
     if(arrSelectors[1].GetNextSelector() != null)
       {
       iCountErrors++;
       printerr("Error_0483vf! Incorrect ISerializationSurrgate returned");
       }
     try {
     iCountTestcases++;
     arrSelectors[2].ChainSelector(arrSelectors[8]);
     context = new StreamingContext(StreamingContextStates.All);
     testss1 = (TestSerializationSurrogate)arrSelectors[0].GetSurrogate(typeof(Int64), context, out ss);
     iCountErrors++;
     Console.WriteLine("Err_74239cd!! Expected Exception not thrown");
     }catch(ArgumentException){
     }catch(Exception ex){
     iCountErrors++;
     Console.WriteLine("Err_652df!! Incorrect Exception thrown : "+ex.ToString());
     }
     } while (false);
   } catch (Exception exc_general ) {
   ++iCountErrors;
   Console.WriteLine (s_strTFAbbrev + " : Error Err_8888yyy!  strLoc=="+ strLoc +", exc_general=="+exc_general.ToString());
   Console.WriteLine(exc_general.StackTrace);
   }
   if ( iCountErrors == 0 )
     {
     Console.WriteLine( "paSs.   "+s_strTFPath +" "+s_strTFName+" ,iCountTestcases=="+iCountTestcases);
     return true;
     }
   else
     {
     Console.WriteLine("FAiL!   "+s_strTFPath+" "+s_strTFName+" ,iCountErrors=="+iCountErrors+" , BugNums?: "+s_strActiveBugNums );
     return false;
     }
   }
Ejemplo n.º 39
0
	public bool runTest()
	{
		Console.WriteLine(s_strTFPath + " " + s_strTFName + " , for " + s_strClassMethod + " , Source ver : " + s_strDtTmVer);
		int iCountErrors = 0;
		int iCountTestcases = 0;
		String strLoc = "Loc_000oo";
		IFormatter formatter;
		SurrogateSelector selector;
		MySerializationSurrogate surrogate;
		StreamingContext context = new StreamingContext(StreamingContextStates.All);
		MemoryStream stream;
		Int32 iValue;
		A a;
		ObjectManager manager;
		ObjectIDGenerator generator;
		Int64 rootId;
		Int64 childId;
		Boolean firstTime;
		MemberInfo[] members;
		try {
			strLoc = "Loc_29457sdg";
			iCountTestcases++;
			selector = new SurrogateSelector();
			surrogate = new MySerializationSurrogate();
			selector.AddSurrogate(typeof(A), context, surrogate);
			formatter = new BinaryFormatter();
			formatter.SurrogateSelector = selector;
			stream = new MemoryStream();
			a = new A();
			a.I = 10;
			formatter.Serialize(stream, a);
			stream.Position = 0;
			A a1 = (A)formatter.Deserialize(stream);
			if(a1.I != 30){
				iCountErrors++;
				Console.WriteLine("Err_753ffd! Unexpected value returned, Value: <{0}>", a1.I);
			}
			strLoc = "Loc_8394tfsg";
			iCountTestcases++;
			generator = new ObjectIDGenerator();
			a = new A();
			iValue = 500;
			rootId = generator.GetId(a, out firstTime);
			childId = generator.GetId(iValue, out firstTime);
			selector = new SurrogateSelector();
			surrogate = new MySerializationSurrogate();
			selector.AddSurrogate(typeof(A), context, surrogate);
			manager = new ObjectManager(selector, context);
			members = FormatterServices.GetSerializableMembers(typeof(A));
			manager.RecordDelayedFixup(rootId, "Int32Twist", childId);
			try{
				SerializationInfo serInfo1 = new SerializationInfo(typeof(A), new FormatterConverter());
				manager.RegisterObject(a, rootId, serInfo1);
				manager.RegisterObject(iValue, childId);
				Console.WriteLine(a.I);
			}catch(Exception ex){
				iCountErrors++;
				Console.WriteLine(ex);
			}
			strLoc = "Loc_87sgsfd";
			iCountTestcases++;
			generator = new ObjectIDGenerator();
			a = new A();
			iValue = 500;
			rootId = generator.GetId(a, out firstTime);
			childId = generator.GetId(iValue, out firstTime);
			manager = new ObjectManager(null, context);
			members = FormatterServices.GetSerializableMembers(typeof(A));
			manager.RecordFixup(rootId, members[0], childId);
			manager.RegisterObject(a, rootId);
			manager.RegisterObject(iValue, childId);
			if(a.I != 500){
				iCountErrors++;
				Console.WriteLine("Err_93745sg! Unexpected value returned, Value: <{0}>", a1.I);
			}
		} catch (Exception exc_general ) {
			++iCountErrors;
			Console.WriteLine (s_strTFAbbrev + " : Error Err_8888yyy!  strLoc=="+ strLoc +", exc_general==\n"+exc_general.StackTrace);
		}
		if ( iCountErrors == 0 )
		{
			Console.WriteLine( "paSs.   "+s_strTFPath +" "+s_strTFName+" ,iCountTestcases=="+iCountTestcases);
			return true;
		}
		else
		{
			Console.WriteLine("FAiL!   "+s_strTFPath+" "+s_strTFName+" ,iCountErrors=="+iCountErrors+" , BugNums?: "+s_strActiveBugNums );
			return false;
		}
	}
Ejemplo n.º 40
0
	public bool runTest()
	{
		Console.WriteLine(s_strTFPath + "\\" + s_strTFName + " , for " + s_strClassMethod + " , Source ver " + s_strDtTmVer);
		String strLoc = "Loc_000oo";
		String strValue = String.Empty;
		int iCountErrors = 0;
		int iCountTestcases = 0;
		Int32 iValue;
		A a1;
		V v1;
		S s1;
		V_I vi1;
		Surrogate_1 surrogate;
		SurrogateSelector selector;
		SoapFormatter formatter;
		MemoryStream stream;
		StreamingContext context = new StreamingContext(StreamingContextStates.All);
		Object objRet;
		ReflectionPermission perm;
		try
		{
			strLoc = "Loc_9347sg";
			iCountTestcases++;
			iValue = 5;
			formatter = new SoapFormatter();
			stream = new MemoryStream();
			try{
				Serialize(formatter, stream, iValue); 
				iCountErrors++;
				Console.WriteLine("Err_7349sg! No exception thrown, ");
			}catch(SecurityException){
			}catch(Exception ex){
				iCountErrors++;
				Console.WriteLine("Err_348tdg! Wrong exception thrown, " + ex.GetType().Name);
			}
			strLoc = "Loc_2407sdg";
			iCountTestcases++;
			stream.Position = 0;
			formatter.Serialize(stream, iValue);
			stream.Position = 0;
			try{
				objRet = Deserialize(formatter, stream);
			}catch(Exception ex){
				iCountErrors++;
				Console.WriteLine("Err_207tdsg! Wrong exception thrown, " + ex.GetType().Name);
			}
			strLoc = "Loc_3947tfdg";
			iCountTestcases++;
			a1 = new A(5);
			formatter = new SoapFormatter();
			stream = new MemoryStream();
			try{
				Serialize(formatter, stream, a1); 
				iCountErrors++;
				Console.WriteLine("Err_39475sdg! No exception thrown, ");
			}catch(SecurityException){
			}catch(Exception ex){
				iCountErrors++;
				Console.WriteLine("Err_2047tsfgb! Wrong exception thrown, " + ex.GetType().Name);
			}
			stream.Position = 0;
			formatter.Serialize(stream, a1);
			stream.Position = 0;
			try{
				objRet = Deserialize(formatter, stream);
				iCountErrors++;
				Console.WriteLine("Err_9347tsfg! No exception thrown, ");
			}catch(SecurityException){
			}catch(Exception ex){
				iCountErrors++;
				Console.WriteLine("Err_0347tsfg! Wrong exception thrown, " + ex.GetType().Name);
			}
			strLoc = "Loc_34975sg";
			iCountTestcases++;
			v1 = new V(5);
			formatter = new SoapFormatter();
			stream = new MemoryStream();
			try{
				Serialize(formatter, stream, v1); 
				iCountErrors++;
				Console.WriteLine("Err_98324sdg! No exception thrown, ");
			}catch(SecurityException){
			}catch(Exception ex){
				iCountErrors++;
				Console.WriteLine("Err_3047tfxg! Wrong exception thrown, " + ex.GetType().Name);
			}
			stream.Position = 0;
			formatter.Serialize(stream, v1);
			stream.Position = 0;
			try{
				objRet = Deserialize(formatter, stream);
				iCountErrors++;
				Console.WriteLine("Err_7349sg! No exception thrown, ");
			}
			catch(SecurityException)
			{
			}
			catch(Exception ex)
			{
				iCountErrors++;
				Console.WriteLine("Err_29437tsg! Wrong exception thrown, " + ex.GetType().Name);
			}
			strLoc = "Loc_34975sg";
			iCountTestcases++;
			s1 = new S(5);
			formatter = new SoapFormatter();
			stream = new MemoryStream();
			try{
				Serialize(formatter, stream, s1); 
				iCountErrors++;
				Console.WriteLine("Err_3946tsdg! No exception thrown, ");
			}catch(SecurityException){
			}catch(Exception ex){
				iCountErrors++;
				Console.WriteLine("Err_9347tsdg! Wrong exception thrown, " + ex.GetType().Name);
			}
			stream.Position = 0;
			formatter.Serialize(stream, s1);
			stream.Position = 0;
			try{
				objRet = Deserialize(formatter, stream);
				iCountErrors++;
				Console.WriteLine("Err_98324sdg! No exception thrown, ");
			}
			catch(SecurityException)
			{
			}
			catch(Exception ex)
			{
				iCountErrors++;
				Console.WriteLine("Err_93745sdg! Exception thrown, " + ex.GetType().Name);
			}
			strLoc = "Loc_34975sg";
			iCountTestcases++;
			vi1 = new V_I(6);
			formatter = new SoapFormatter();
			stream = new MemoryStream();
			try{
				Serialize(formatter, stream, vi1); 
				iCountErrors++;
				Console.WriteLine("Err_2075sdg! No exception thrown, ");
			}catch(SecurityException){
			}catch(Exception ex){
				iCountErrors++;
				Console.WriteLine("Err_294375sdg! Wrong exception thrown, " + ex.GetType().Name);
			}
			stream.Position = 0;
			formatter.Serialize(stream, vi1);
			stream.Position = 0;
			try{
				objRet = Deserialize(formatter, stream);
				iCountErrors++;
				Console.WriteLine("Err_98324sdg! No exception thrown, ");
			}
			catch(SecurityException)
			{
			}
			catch(Exception ex)
			{
				iCountErrors++;
				Console.WriteLine("Err_24907tsdfg! Exception thrown, " + ex.GetType().Name);
			}			
			strLoc = "Loc_34975sg";
			iCountTestcases++;
			selector = new SurrogateSelector();
			surrogate = new Surrogate_1();
			selector.AddSurrogate(typeof(A), context, surrogate);
			selector.AddSurrogate(typeof(V), context, surrogate);
			formatter = new SoapFormatter();
			formatter.SurrogateSelector = selector;
			stream = new MemoryStream();
			a1 = new A(5);
			try{
				Serialize(formatter, stream, a1); 
				iCountErrors++;
				Console.WriteLine("Err_93475sdg! No exception thrown, ");
			}catch(SecurityException){
			}catch(Exception ex){
				iCountErrors++;
				Console.WriteLine("Err_3947tsg! Wrong exception thrown, " + ex.GetType().Name);
			}
			stream.Position = 0;
			formatter.Serialize(stream, a1);
			stream.Position = 0;
			try{
				objRet = Deserialize(formatter, stream);
				iCountErrors++;
				Console.WriteLine("Err_98324sdg! No exception thrown, ");
			}
			catch(SecurityException)
			{
			}
			catch(Exception ex)
			{
				iCountErrors++;
				Console.WriteLine("Err_39047tsg! Exception thrown, " + ex.GetType().Name);
			}
			strLoc = "Loc_3048sdg";
			iCountTestcases++;
			v1 = new V(7);
			formatter = new SoapFormatter();
			formatter.SurrogateSelector = selector;
			stream = new MemoryStream();
			try{
				Serialize(formatter, stream, v1); 
				iCountErrors++;
				Console.WriteLine("Err_9347tsgf! No exception thrown, ");
			}catch(SecurityException){
			}catch(Exception ex){
				iCountErrors++;
				Console.WriteLine("Err_2947tsgd! Wrong exception thrown, " + ex.GetType().Name);
			}
			stream.Position = 0;
			formatter.Serialize(stream, v1);
			stream.Position = 0;
			try{
				objRet = Deserialize(formatter, stream);
				iCountErrors++;
				Console.WriteLine("Err_98324sdg! No exception thrown, ");
			}
			catch(SecurityException)
			{
			}
			catch(Exception ex)
			{
				iCountErrors++;
				Console.WriteLine("Err_89734trfsg! Exception thrown, " + ex.GetType().Name);
			}			
			perm = new ReflectionPermission(PermissionState.Unrestricted);
			perm.Deny();
			strLoc = "Loc_83245tsg";
			iCountTestcases++;
			a1 = new A(5);
			formatter = new SoapFormatter();
			stream = new MemoryStream();
			try{
				formatter.Serialize(stream, a1);
				stream.Position = 0;
				objRet = formatter.Deserialize(stream);				
				if(((A)objRet).I!=5){
					iCountErrors++;
					Console.WriteLine("Err_3947tsdg! wrong value returned, " + (Int32)objRet);
				}
			}catch(Exception ex){
				iCountErrors++;
				Console.WriteLine("Err_29746tsfdg! Exception thrown, " + ex.GetType().Name);
			}
		} catch (Exception exc_general ) {
			++iCountErrors;
			Console.WriteLine (s_strTFAbbrev + " : Error Err_8888yyy!  strLoc=="+ strLoc +", exc_general=="+exc_general.ToString());
		}
		if ( iCountErrors == 0 )
		{
			Console.WriteLine( "paSs. "+s_strTFName+" ,iCountTestcases=="+iCountTestcases.ToString());
			return true;
		}
		else
		{
			Console.WriteLine("FAiL! "+s_strTFName+" ,inCountErrors=="+iCountErrors.ToString()+" , BugNums?: "+s_strActiveBugNums );
			return false;
		}
	}