Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
Preferences.cs
Go to the documentation of this file.
1using System;
3using System.IO;
4using System.Linq;
5using Newtonsoft.Json;
6using Newtonsoft.Json.Bson;
7using Newtonsoft.Json.Linq;
9
10namespace Terraria.IO;
11
12public class Preferences
13{
14 public delegate void TextProcessAction(ref string text);
15
17
18 private readonly string _path;
19
21
22 public readonly bool UseBson;
23
24 private readonly object _lock = new object();
25
26 public bool AutoSave;
27
29
31
33
34 public Preferences(string path, bool parseAllTypes = false, bool useBson = false)
35 {
36 //IL_004f: Unknown result type (might be due to invalid IL or missing references)
37 //IL_0054: Unknown result type (might be due to invalid IL or missing references)
38 //IL_0060: Expected O, but got Unknown
39 //IL_002e: Unknown result type (might be due to invalid IL or missing references)
40 //IL_0033: Unknown result type (might be due to invalid IL or missing references)
41 //IL_003a: Unknown result type (might be due to invalid IL or missing references)
42 //IL_0041: Unknown result type (might be due to invalid IL or missing references)
43 //IL_004d: Expected O, but got Unknown
44 _path = path;
46 if (parseAllTypes)
47 {
49 {
52 Formatting = (Formatting)1
53 };
54 }
55 else
56 {
58 {
59 Formatting = (Formatting)1
60 };
61 }
62 }
63
64 public bool Load()
65 {
66 //IL_005d: Unknown result type (might be due to invalid IL or missing references)
67 //IL_0064: Expected O, but got Unknown
68 lock (_lock)
69 {
70 if (!File.Exists(_path))
71 {
72 return false;
73 }
74 try
75 {
76 if (!UseBson)
77 {
78 string text = File.ReadAllText(_path);
80 }
81 else
82 {
85 try
86 {
87 JsonSerializer val2 = JsonSerializer.Create(_serializerSettings);
88 _data = val2.Deserialize<Dictionary<string, object>>((JsonReader)(object)val);
89 }
90 finally
91 {
92 ((IDisposable)val)?.Dispose();
93 }
94 }
95 if (_data == null)
96 {
98 }
99 if (this.OnLoad != null)
100 {
101 this.OnLoad(this);
102 }
103 return true;
104 }
105 catch (Exception)
106 {
107 return false;
108 }
109 }
110 }
111
112 public bool Save(bool canCreateFile = true)
113 {
114 //IL_00c5: Unknown result type (might be due to invalid IL or missing references)
115 //IL_00cc: Expected O, but got Unknown
116 lock (_lock)
117 {
118 try
119 {
120 if (this.OnSave != null)
121 {
122 this.OnSave(this);
123 }
124 if (!canCreateFile && !File.Exists(_path))
125 {
126 return false;
127 }
128 Directory.GetParent(_path).Create();
129 if (File.Exists(_path))
130 {
132 }
133 if (!UseBson)
134 {
135 string text = JsonConvert.SerializeObject((object)_data, _serializerSettings);
136 if (this.OnProcessText != null)
137 {
138 this.OnProcessText(ref text);
139 }
142 }
143 else
144 {
147 try
148 {
150 JsonSerializer.Create(_serializerSettings).Serialize((JsonWriter)(object)val, (object)_data);
151 }
152 finally
153 {
154 ((IDisposable)val)?.Dispose();
155 }
156 }
157 }
158 catch (Exception ex)
159 {
160 Console.WriteLine(Language.GetTextValue("Error.UnableToWritePreferences", _path));
161 Console.WriteLine(ex.ToString());
162 return false;
163 }
164 return true;
165 }
166 }
167
168 public void Clear()
169 {
170 _data.Clear();
171 }
172
173 public void Put(string name, object value)
174 {
175 lock (_lock)
176 {
177 _data[name] = value;
178 if (AutoSave)
179 {
180 Save();
181 }
182 }
183 }
184
185 public bool Contains(string name)
186 {
187 lock (_lock)
188 {
189 return _data.ContainsKey(name);
190 }
191 }
192
193 public T Get<T>(string name, T defaultValue)
194 {
195 //IL_003b: Unknown result type (might be due to invalid IL or missing references)
196 //IL_0045: Expected O, but got Unknown
197 lock (_lock)
198 {
199 try
200 {
201 if (_data.TryGetValue(name, out var value))
202 {
203 if (!(value is T result))
204 {
205 if (value is JObject)
206 {
207 return JsonConvert.DeserializeObject<T>(((object)(JObject)value).ToString());
208 }
209 return (T)Convert.ChangeType(value, typeof(T));
210 }
211 return result;
212 }
213 return defaultValue;
214 }
215 catch
216 {
217 return defaultValue;
218 }
219 }
220 }
221
222 public void Get<T>(string name, ref T currentValue)
223 {
224 currentValue = Get(name, currentValue);
225 }
226
228 {
229 return _data.Keys.ToList();
230 }
231}
bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value)
static void WriteLine()
Definition Console.cs:733
static ? object ChangeType(object? value, TypeCode typeCode)
Definition Convert.cs:229
static ? DirectoryInfo GetParent(string path)
Definition Directory.cs:9
static void SetAttributes(string path, FileAttributes fileAttributes)
Definition File.cs:230
static FileStream Create(string path)
Definition File.cs:73
static bool Exists([NotNullWhen(true)] string? path)
Definition File.cs:97
static FileStream OpenRead(string path)
Definition File.cs:236
static void WriteAllText(string path, string? contents)
Definition File.cs:282
static string ReadAllText(string path)
Definition File.cs:246
delegate void TextProcessAction(ref string text)
Preferences(string path, bool parseAllTypes=false, bool useBson=false)
Dictionary< string, object > _data
bool Contains(string name)
readonly object _lock
Action< Preferences > OnLoad
T Get< T >(string name, T defaultValue)
readonly string _path
Action< Preferences > OnSave
List< string > GetAllKeys()
void Put(string name, object value)
bool Save(bool canCreateFile=true)
readonly JsonSerializerSettings _serializerSettings
readonly bool UseBson
TextProcessAction OnProcessText
static string GetTextValue(string key)
Definition Language.cs:15