예제 #1
0
        public FsPrefsProvider()
        {
#if !UNITY_EDITOR
            if (!Directory.Exists(Application.persistentDataPath))
            {
                Directory.CreateDirectory(Application.persistentDataPath);
            }

            var prefsFilePath = Application.persistentDataPath + "/preferences.ini";

            if (string.IsNullOrEmpty(prefsFilePath))
            {
                Dbg.Error("Can't get preferences.json path");
                _data = new Dictionary <string, object>();
                return;
            }

            try
            {
                if (!File.Exists(prefsFilePath))
                {
                    _data = new Dictionary <string, object>();
                }
                else
                {
                    using (var sr = File.OpenText(prefsFilePath))
                    {
                        _data = GetDataFromText(sr);
                    }
                }
            }
            catch (Exception ex)
            {
                Dbg.Error("Can't open preferences.json for read: {0}", ex);
                _data = new Dictionary <string, object>();
            }

            try
            {
                _stream = File.Open(prefsFilePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read);
                _writer = new StreamWriter(_stream);

                Dbg.Log("FS Prefs Initialized at path \"{0}\"", prefsFilePath);
            }
            catch (Exception ex)
            {
                Dbg.Error("Can't open preferences.json for write: {0}", ex);
            }
#endif
        }
예제 #2
0
        public override void Flush()
        {
            if (_stream == null)
            {
                Dbg.Error("Can't flush, stream is null");
                return;
            }

            string str;

            _stream.Seek(0, SeekOrigin.Begin);
            foreach (var pair in _data)
            {
                _writer.Write(pair.Key);
                _writer.Write('=');
                if (pair.Value is int || pair.Value is bool)
                {
                    str = pair.Value.ToString();
                }
                else if (pair.Value is float)
                {
                    str = (( float )pair.Value).ToString(CultureInfo.InvariantCulture);
                    if (str.IndexOf('.') < 0)
                    {
                        str += ".0";
                    }
                }
                else
                {
                    str = EscapeString(pair.Value.ToString());
                }
                _writer.WriteLine(str);
            }

            _writer.Flush();
            _stream.SetLength(_stream.Position);
        }