Exemple #1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="BayatGames.SaveGameFree.SaveGameWeb"/> class.
 /// </summary>
 /// <param name="username">Username.</param>
 /// <param name="password">Password.</param>
 /// <param name="url">URL.</param>
 /// <param name="encode">If set to <c>true</c> encode.</param>
 /// <param name="encodePassword">Encode password.</param>
 /// <param name="serializer">Serializer.</param>
 /// <param name="encoder">Encoder.</param>
 /// <param name="encoding">Encoding.</param>
 public SaveGameWeb(string username, string password, string url, bool encode, string encodePassword, ISaveGameSerializer serializer, ISaveGameEncoder encoder, Encoding encoding)
 {
     m_Username       = username;
     m_Password       = password;
     m_URL            = url;
     m_Encode         = encode;
     m_EncodePassword = encodePassword;
     m_Serializer     = serializer;
     m_Encoder        = encoder;
     m_Encoding       = encoding;
 }
Exemple #2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="BayatGames.SaveGameFree.SaveGameWeb"/> class.
 /// </summary>
 /// <param name="username">Username.</param>
 /// <param name="password">Password.</param>
 /// <param name="url">URL.</param>
 /// <param name="encode">If set to <c>true</c> encode.</param>
 /// <param name="encodePassword">Encode password.</param>
 /// <param name="serializer">Serializer.</param>
 /// <param name="encoder">Encoder.</param>
 public SaveGameWeb(string username,
                    string password,
                    string url,
                    bool encode,
                    string encodePassword,
                    ISaveGameSerializer serializer,
                    ISaveGameEncoder encoder) : this(username,
                                                     password,
                                                     url,
                                                     encode,
                                                     encodePassword,
                                                     serializer,
                                                     encoder,
                                                     DefaultEncoding)
 {
 }
Exemple #3
0
        protected virtual void Awake()
        {
            Debug.Log(Application.persistentDataPath);
            Debug.Log(Application.dataPath);
            if (resetBlanks)
            {
                if (string.IsNullOrEmpty(encodePassword))
                {
                    encodePassword = SaveGame.EncodePassword;
                }
                if (serializer == null)
                {
                    serializer = SaveGame.Serializer;
                }
                if (encoder == null)
                {
                    encoder = SaveGame.Encoder;
                }
                if (encoding == null)
                {
                    encoding = SaveGame.DefaultEncoding;
                }
            }
            switch (format)
            {
            case SaveFormat.Binary:
                serializer = new SaveGameBinarySerializer();
                break;

            case SaveFormat.JSON:
                serializer = new SaveGameJsonSerializer();
                break;

            case SaveFormat.XML:
                serializer = new SaveGameXmlSerializer();
                break;
            }
            if (loadOnAwake)
            {
                Load();
            }
            if (saveOnAwake)
            {
                Save();
            }
        }
    protected virtual void Awake()
    {
        if (string.IsNullOrEmpty(encodePassword))
        {
            encodePassword = SaveGame.EncodePassword;
        }
        if (serializer == null)
        {
            serializer = SaveGame.Serializer;
        }
        if (encoder == null)
        {
            encoder = SaveGame.Encoder;
        }
        if (encoding == null)
        {
            encoding = SaveGame.DefaultEncoding;
        }

        serializer = new SaveGameJsonSerializer();
    }
Exemple #5
0
        /// <summary>
        ///     Loads data using identifier.
        /// </summary>
        /// <param name="identifier">Identifier.</param>
        /// <param name="defaultValue">Default Value.</param>
        /// <param name="encode">Load encrypted data? (set it to true if you have used encryption in save)</param>
        /// <param name="password">Encryption Password.</param>
        /// <param name="serializer">Serializer.</param>
        /// <param name="encoder">Encoder.</param>
        /// <param name="encoding">Encoding.</param>
        /// <param name="path">Path.</param>
        /// <typeparam name="T">The 1st type parameter.</typeparam>
        public static T Load <T>(string identifier, T defaultValue, bool encode, string password,
                                 ISaveGameSerializer serializer, ISaveGameEncoder encoder, Encoding encoding, SaveGamePath path)
        {
            if (string.IsNullOrEmpty(identifier))
            {
                throw new ArgumentNullException("identifier");
            }
            if (serializer == null)
            {
                serializer = Serializer;
            }
            if (encoding == null)
            {
                encoding = DefaultEncoding;
            }
            if (defaultValue == null)
            {
                defaultValue = default(T);
            }
            var result   = defaultValue;
            var filePath = "";

            if (!IsFilePath(identifier))
            {
                switch (path)
                {
                default:
                case SaveGamePath.PersistentDataPath:
                    filePath = string.Format("{0}/{1}", Application.persistentDataPath, identifier);
                    break;

                case SaveGamePath.DataPath:
                    filePath = string.Format("{0}/{1}", Application.dataPath, identifier);
                    break;
                }
            }
            else
            {
                filePath = identifier;
            }
#if !UNITY_SAMSUNGTV && !UNITY_TVOS && !UNITY_WEBGL
            if (!Exists(filePath, path))
#else
            if (!Exists(filePath, path))
#endif
            {
                Debug.LogWarningFormat(
                    "The specified identifier ({1}) does not exists. please use Exists () to check for existent before calling Load.\n" +
                    "returning the default(T) instance.",
                    filePath,
                    identifier);
                return(result);
            }

            Stream stream = null;
            if (encode)
            {
                var data = "";
#if !UNITY_SAMSUNGTV && !UNITY_TVOS && !UNITY_WEBGL
                if (IOSupported())
                {
#if UNITY_WSA || UNITY_WINRT
                    data = encoding.GetString(UnityEngine.Windows.File.ReadAllBytes(filePath));
#else
                    data = File.ReadAllText(filePath, encoding);
#endif
                }
                else
                {
                    data = PlayerPrefs.GetString(filePath);
                }
#else
                data = PlayerPrefs.GetString(filePath);
#endif
                var decoded = encoder.Decode(data, password);
                stream = new MemoryStream(encoding.GetBytes(decoded), true);
            }
            else
            {
#if !UNITY_SAMSUNGTV && !UNITY_TVOS && !UNITY_WEBGL
                if (IOSupported())
                {
#if UNITY_WSA || UNITY_WINRT
                    stream = new MemoryStream(UnityEngine.Windows.File.ReadAllBytes(filePath));
#else
                    stream = File.OpenRead(filePath);
#endif
                }
                else
                {
                    var data = PlayerPrefs.GetString(filePath);
                    stream = new MemoryStream(encoding.GetBytes(data));
                }
#else
                string data = PlayerPrefs.GetString(filePath);
                stream = new MemoryStream(encoding.GetBytes(data));
#endif
            }

            result = serializer.Deserialize <T>(stream, encoding);
            stream.Dispose();
            if (result == null)
            {
                result = defaultValue;
            }
            if (LoadCallback != null)
            {
                LoadCallback.Invoke(
                    result,
                    identifier,
                    encode,
                    password,
                    serializer,
                    encoder,
                    encoding,
                    path);
            }
            if (OnLoaded != null)
            {
                OnLoaded(
                    result,
                    identifier,
                    encode,
                    password,
                    serializer,
                    encoder,
                    encoding,
                    path);
            }
            return(result);
        }
Exemple #6
0
 /// <summary>
 ///     Load the specified identifier, defaultValue and encoder.
 /// </summary>
 /// <param name="identifier">Identifier.</param>
 /// <param name="defaultValue">Default value.</param>
 /// <param name="encoder">Encoder.</param>
 /// <typeparam name="T">The 1st type parameter.</typeparam>
 public static T Load <T>(string identifier, T defaultValue, ISaveGameEncoder encoder)
 {
     return(Load(identifier, defaultValue, Encode, EncodePassword, Serializer, encoder, DefaultEncoding,
                 SavePath));
 }
Exemple #7
0
        /// <summary>
        ///     Saves data using the identifier.
        /// </summary>
        /// <param name="identifier">Identifier.</param>
        /// <param name="obj">Object to save.</param>
        /// <param name="encode">Encrypt the data?</param>
        /// <param name="password">Encryption Password.</param>
        /// <param name="serializer">Serializer.</param>
        /// <param name="encoder">Encoder.</param>
        /// <param name="encoding">Encoding.</param>
        /// <param name="path">Path.</param>
        /// <typeparam name="T">The 1st type parameter.</typeparam>
        public static void Save <T>(string identifier, T obj, bool encode, string password,
                                    ISaveGameSerializer serializer, ISaveGameEncoder encoder, Encoding encoding, SaveGamePath path)
        {
            if (string.IsNullOrEmpty(identifier))
            {
                throw new ArgumentNullException("identifier");
            }
            if (serializer == null)
            {
                serializer = Serializer;
            }
            if (encoding == null)
            {
                encoding = DefaultEncoding;
            }
            var filePath = "";

            if (!IsFilePath(identifier))
            {
                switch (path)
                {
                default:
                case SaveGamePath.PersistentDataPath:
                    filePath = string.Format("{0}/{1}", Application.persistentDataPath, identifier);
                    break;

                case SaveGamePath.DataPath:
                    filePath = string.Format("{0}/{1}", Application.dataPath, identifier);
                    break;
                }
            }
            else
            {
                filePath = identifier;
            }
            if (obj == null)
            {
                obj = default(T);
            }
            Stream stream = null;

#if !UNITY_SAMSUNGTV && !UNITY_TVOS && !UNITY_WEBGL
#if UNITY_WSA || UNITY_WINRT
            UnityEngine.Windows.Directory.CreateDirectory(filePath);
#else
            Directory.CreateDirectory(Path.GetDirectoryName(filePath));
#endif
#endif
            if (encode)
            {
                stream = new MemoryStream();
            }
            else
            {
#if !UNITY_SAMSUNGTV && !UNITY_TVOS && !UNITY_WEBGL
                if (IOSupported())
                {
#if UNITY_WSA || UNITY_WINRT
                    stream = new MemoryStream();
#else
                    stream = File.Create(filePath);
#endif
                }
                else
                {
                    stream = new MemoryStream();
                }
#else
                stream = new MemoryStream();
#endif
            }

            serializer.Serialize(obj, stream, encoding);
            if (encode)
            {
                var data    = encoding.GetString(((MemoryStream)stream).ToArray());
                var encoded = encoder.Encode(data, password);
#if !UNITY_SAMSUNGTV && !UNITY_TVOS && !UNITY_WEBGL
                if (IOSupported())
                {
#if UNITY_WSA || UNITY_WINRT
                    UnityEngine.Windows.File.WriteAllBytes(filePath, encoding.GetBytes(encoded));
#else
                    File.WriteAllText(filePath, encoded, encoding);
#endif
                }
                else
                {
                    PlayerPrefs.SetString(filePath, encoded);
                    PlayerPrefs.Save();
                }
#else
                PlayerPrefs.SetString(filePath, encoded);
                PlayerPrefs.Save();
#endif
            }
            else if (!IOSupported())
            {
                var data = encoding.GetString(((MemoryStream)stream).ToArray());
                PlayerPrefs.SetString(filePath, data);
                PlayerPrefs.Save();
            }

            stream.Dispose();
            if (SaveCallback != null)
            {
                SaveCallback.Invoke(
                    obj,
                    identifier,
                    encode,
                    password,
                    serializer,
                    encoder,
                    encoding,
                    path);
            }
            if (OnSaved != null)
            {
                OnSaved(
                    obj,
                    identifier,
                    encode,
                    password,
                    serializer,
                    encoder,
                    encoding,
                    path);
            }
        }
Exemple #8
0
 /// <summary>
 ///     Save the specified identifier, obj and encoder.
 /// </summary>
 /// <param name="identifier">Identifier.</param>
 /// <param name="obj">Object.</param>
 /// <param name="encoder">Encoder.</param>
 /// <typeparam name="T">The 1st type parameter.</typeparam>
 public static void Save <T>(string identifier, T obj, ISaveGameEncoder encoder)
 {
     Save(identifier, obj, Encode, EncodePassword, Serializer, encoder, DefaultEncoding, SavePath);
 }