SettingsReader.cs 3.31 KB
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

public class SettingsReader
{
    public SettingsReader() { }

    public SettingsReader(string settingsFile)
    {
        Initialize(settingsFile);
    }

    private Dictionary<string, object> _raw = new Dictionary<string, object>();

    public Dictionary<string, object> rawSettings { get { return _raw; } }

    public bool isInitialized { get; private set; }

    private string _path;

    public void Initialize(string settingsFile)
    {
        _path = string.Format("{0}/{1}", Application.streamingAssetsPath, settingsFile);
        if (!File.Exists(_path))
        {
            Debug.LogError("Settings: No settings file found: " + _path);
            return;
        }
        
        var tempDict = JsonConvert.DeserializeObject<Dictionary<string,object>>(File.ReadAllText(_path));
        _raw = new Dictionary<string, object>();

        var enumerator = tempDict.GetEnumerator();
        while (enumerator.MoveNext())
            _raw.Add(enumerator.Current.Key.ToLower(), enumerator.Current.Value);

        isInitialized = true;
    }

    public void CreateEntry(string key, object value)
    {
        var thisDict = new Dictionary<string, object>()
        {
            { key, value }
        };

        CreateEntry(thisDict);
    }

    public void CreateEntry(Dictionary<string, object> entry)
    {
        foreach (KeyValuePair<string, object> obj in entry)
        {
            if (_raw.ContainsKey(obj.Key))
                _raw[obj.Key] = obj.Value;
            else
                _raw.Add(obj.Key, obj.Value);
        }

        string write = JsonConvert.SerializeObject(_raw, Formatting.Indented);
        File.WriteAllText(_path, write);
    }

    public string GetString(string key)
    {
        string val = string.Empty;
        key = key.ToLower();

        if (_raw != null && _raw.ContainsKey(key))
            val = _raw[key].ToString();
        else
            Debug.LogError(string.Format("Settings: \"{0}\" does not exist in settings file", key));

        return val;
    }

    public int GetInt(string key)
    {
        int val = 0;
        key = key.ToLower();

        if (_raw != null && _raw.ContainsKey(key))
            System.Int32.TryParse(_raw[key].ToString(), out val);
        else
            Debug.LogError(string.Format("Settings: \"{0}\" does not exist in settings file", key));

        return val;
    }

    public float GetFloat(string key)
    {
        float val = 0;
        key = key.ToLower();

        if (_raw != null && _raw.ContainsKey(key))
            float.TryParse(_raw[key].ToString(), out val);
        else
            Debug.LogError(string.Format("Settings: \"{0}\" does not exist in settings file", key));

        return val;
    }

    public bool GetBool(string key)
    {
        bool val = false;
        key = key.ToLower();

        if (_raw != null && _raw.ContainsKey(key))
            val = _raw[key].ToString().ToLower() == "true" ? true : false;
        else
            Debug.LogError(string.Format("Settings: \"{0}\" does not exist in settings file", key));

        return val;
    }

    public T GetObject<T>(string key)
    {
        string obj = JsonConvert.SerializeObject(_raw[key]);
        return JsonConvert.DeserializeObject<T>(obj);
    }
}