SettingsReader.cs
3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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);
}
}