Esempio n. 1
0
        private void BtnSave_Click(object sender, RoutedEventArgs e)
        {
            bool?result = OnSave?.Invoke();

            if (result.Value == true)
            {
                SetVisibility(Visibility.Collapsed);
            }
        }
Esempio n. 2
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);
            }
        }