Beispiel #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);
    }
    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);
    }
Beispiel #3
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;
        }
Beispiel #4
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);
            }
        }
    }
        public static BinaryFormatter GetBinaryFormatter()
        {
            var formatter = new BinaryFormatter();
            var selector  = new SurrogateSelector();

            var vector3Surrogate    = new Vector3SerializationSurrogate();
            var quaternionSurrogate = new QuaternionSerializationSurrogate();

            selector.AddSurrogate(typeof(Vector3), new StreamingContext(StreamingContextStates.All), vector3Surrogate);
            selector.AddSurrogate(typeof(Quaternion), new StreamingContext(StreamingContextStates.All), quaternionSurrogate);

            formatter.SurrogateSelector = selector;
            return(formatter);
        }
Beispiel #6
0
    public static BinaryFormatter GetBinaryFormatter()
    {
        BinaryFormatter formatter = new BinaryFormatter();

        SurrogateSelector selector = new SurrogateSelector();

        Vector3SerializationSurrogate    vector3Surrogate    = new Vector3SerializationSurrogate();
        QuaternionSerializationSurrogate quaternionSurrogate = new QuaternionSerializationSurrogate();
        ColorSerializationSurrogate      colorSurrogate      = new ColorSerializationSurrogate();

        selector.AddSurrogate(typeof(Vector3), new StreamingContext(StreamingContextStates.All), vector3Surrogate);
        selector.AddSurrogate(typeof(Quaternion), new StreamingContext(StreamingContextStates.All), quaternionSurrogate);
        selector.AddSurrogate(typeof(Color), new StreamingContext(StreamingContextStates.All), colorSurrogate);

        formatter.SurrogateSelector = selector;

        return(formatter);
    }
    public static BinaryFormatter GetBinaryFormatter()
    {
        BinaryFormatter formatter = new BinaryFormatter();

        SurrogateSelector selector = new SurrogateSelector();

        Vector3SerializationSurrogate    vector3Surrogate    = new Vector3SerializationSurrogate();
        QuaternionSerializationSurrogate quaternionSurrogate = new QuaternionSerializationSurrogate();
        LinkedListSerializationSurrogate llSurrogate         = new LinkedListSerializationSurrogate();

        selector.AddSurrogate(typeof(Vector3), new StreamingContext(StreamingContextStates.All), vector3Surrogate);
        selector.AddSurrogate(typeof(Quaternion), new StreamingContext(StreamingContextStates.All), quaternionSurrogate);
        selector.AddSurrogate(typeof(LinkedList <KeyValuePair <Cards, bool> >), new StreamingContext(StreamingContextStates.All), llSurrogate);

        formatter.SurrogateSelector = selector;

        return(formatter);
    }
    public static void AddAllUnitySurrogate(this SurrogateSelector surrogateSelector)
    {
        var colorSS      = new ColorSerializationSurrogate();
        var quaternionSS = new QuaternionSerializationSurrogate();
        var vector2IntSS = new Vector2IntSerializationSurrogate();
        var vector2SS    = new Vector2SerializationSurrogate();
        var vector3IntSS = new Vector3IntSerializationSurrogate();
        var vector3SS    = new Vector3SerializationSurrogate();
        var vector4SS    = new Vector4SerializationSurrogate();

        surrogateSelector.AddSurrogate(typeof(Color), new StreamingContext(StreamingContextStates.All), colorSS);
        surrogateSelector.AddSurrogate(typeof(Quaternion), new StreamingContext(StreamingContextStates.All), quaternionSS);
        surrogateSelector.AddSurrogate(typeof(Vector2Int), new StreamingContext(StreamingContextStates.All), vector2IntSS);
        surrogateSelector.AddSurrogate(typeof(Vector2), new StreamingContext(StreamingContextStates.All), vector2SS);
        surrogateSelector.AddSurrogate(typeof(Vector3Int), new StreamingContext(StreamingContextStates.All), vector3IntSS);
        surrogateSelector.AddSurrogate(typeof(Vector3), new StreamingContext(StreamingContextStates.All), vector3SS);
        surrogateSelector.AddSurrogate(typeof(Vector4), new StreamingContext(StreamingContextStates.All), vector4SS);
    }
Beispiel #9
0
    //Creates a binary formatter
    public static BinaryFormatter GetBinaryFormatter()
    {
        BinaryFormatter formatter = new BinaryFormatter();

        //Surrogates
        SurrogateSelector selector = new SurrogateSelector();

        //Init surrogates
        Vector3SerializationSurrogate    vector3Surrogate    = new Vector3SerializationSurrogate();
        QuaternionSerializationSurrogate quaternionSurrogate = new QuaternionSerializationSurrogate();

        //Add surrogates
        selector.AddSurrogate(typeof(Vector3), new StreamingContext(StreamingContextStates.All), vector3Surrogate);
        selector.AddSurrogate(typeof(Quaternion), new StreamingContext(StreamingContextStates.All), quaternionSurrogate);

        //Apply surrogates
        formatter.SurrogateSelector = selector;

        return(formatter);
    }
    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;
    }