Exemplo n.º 1
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 System.ArgumentNullException("identifier");
            }
            if (serializer == null)
            {
                serializer = SaveGame.Serializer;
            }
            if (encoding == null)
            {
                encoding = SaveGame.DefaultEncoding;
            }
            string 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)
            {
                string data    = encoding.GetString(((MemoryStream)stream).ToArray());
                string 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.SavePlayer();
#endif
            }
            else if (!IOSupported())
            {
                string 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);
            }
        }