예제 #1
0
        private void RebuildFile()
        {
            if (File.Exists(path))
            {
                File.Delete(path);
            }

            if (!Directory.Exists(root + Path.DirectorySeparatorChar + profileName))
            {
                Directory.CreateDirectory(root + Path.DirectorySeparatorChar + profileName);
            }

            IFormatter formatter = new BinaryFormatter();

            using (Stream stream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None))
            {
                try
                {
                    formatter.Serialize(stream, dict);
                }
                catch (SerializationException e)
                {
                    Debug.LogException(e);
                    stream.Close();
                }
                catch (Exception e)
                {
                    Debug.LogException(e);
                    stream.Close();
                }
            }
        }
예제 #2
0
        public void Save()
        {
            IFormatter formatter = new BinaryFormatter();

            try
            {
                Stream stream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None);

                try
                {
                    formatter.Serialize(stream, dict);
                }
                catch (SerializationException e)
                {
                    Debug.LogException(e);
                }
                catch (Exception e)
                {
                    Debug.LogException(e);
                }
                finally
                {
                    stream.Close();
                }
            }
            catch (Exception)
            {
                RebuildFile();
                Save();
            }
        }
예제 #3
0
        public DataContainer(string filename, string profile = "default")
        {
            profileName = profile;
            path        = root + Path.DirectorySeparatorChar + profile + Path.DirectorySeparatorChar + filename;

            if (File.Exists(path))
            {
                IFormatter formatter = new BinaryFormatter();
                using (Stream stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
                    try
                    {
                        dict = (Dictionary <string, T>)formatter.Deserialize(stream);
                    }
                    catch (SerializationException e)
                    {
                        Debug.LogException(e);
                        RebuildFile();
                    }
            }
            else
            {
                RebuildFile();
            }

            if (Application.isPlaying && SaveOnQuit.Instances < 1)
            {
                QuitObject = new GameObject("FilePrefs_QuitObject", typeof(SaveOnQuit));
            }
        }