This repository has been archived by the owner on Jul 13, 2020. It is now read-only.
forked from Slaynash/VRCModLoader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModPrefs.cs
232 lines (213 loc) · 9.86 KB
/
ModPrefs.cs
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Newtonsoft;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace VRCModLoader
{
public class Pref
{
public string section { get; set; } = "";
public string name { get; set; } = "";
public object value { get; set; } = null;
}
/// <summary>
/// Allows to get and set preferences for your mod.
/// </summary>
[Obsolete("Please use VRCTools.ModPrefs instead")]
public static class ModPrefs
{
private static List<Pref> _prefList;
private static List<Pref> prefList
{
get
{
if (_prefList == null)
{
string userDataDir = "";
if (UnityEngine.Application.platform == UnityEngine.RuntimePlatform.WindowsPlayer)
userDataDir = Path.Combine(Environment.CurrentDirectory, "UserData");
else if (UnityEngine.Application.platform == UnityEngine.RuntimePlatform.Android)
userDataDir = "/sdcard/VRCTools/UserData";
if (!Directory.Exists(userDataDir)) Directory.CreateDirectory(userDataDir);
if (!File.Exists(Path.Combine(userDataDir, "modPrefs.json")))
{
_prefList = new List<Pref>();
File.WriteAllText(Path.Combine(userDataDir, "modPrefs.json"), JsonConvert.SerializeObject(_prefList, Formatting.Indented));
}
var input = File.ReadAllText(Path.Combine(userDataDir, "modPrefs.json"));
_prefList = JsonConvert.DeserializeObject<List<Pref>>(input);
}
return _prefList;
}
}
private static void WriteJson()
{
string FilePath = "";
if (UnityEngine.Application.platform == UnityEngine.RuntimePlatform.WindowsPlayer)
FilePath = Path.Combine(Environment.CurrentDirectory, "UserData");
else if (UnityEngine.Application.platform == UnityEngine.RuntimePlatform.Android)
FilePath = "/sdcard/VRCTools/UserData";
if (!Directory.Exists(FilePath)) Directory.CreateDirectory(FilePath);
FilePath = Path.Combine(FilePath, "modPrefs.json");
FileInfo jsonFileInfo = new FileInfo(FilePath);
FileStream jsonFileStream = null;
if (!jsonFileInfo.Exists)
jsonFileStream = jsonFileInfo.Create();
else
jsonFileStream = new FileStream(FilePath, FileMode.Open, FileAccess.Write, FileShare.Read);
JsonSerializer jsonSerializer = new JsonSerializer();
using (StreamWriter jsonStreamWriter = new StreamWriter(jsonFileStream))
{
jsonStreamWriter.AutoFlush = true;
using (JsonWriter jsonWriter = new JsonTextWriter(jsonStreamWriter))
jsonSerializer.Serialize(jsonWriter, prefList);
}
}
/// <summary>
/// Gets a string from the JSON.
/// </summary>
/// <param name="section">Section of the key.</param>
/// <param name="name">Name of the key.</param>
/// <param name="defaultValue">Value that should be used when no value is found.</param>
/// <param name="autoSave">Whether or not the default value should be written if no value is found.</param>
/// <returns></returns>
public static string GetString(string section, string name, string defaultValue = "", bool autoSave = false)
{
//string value = Instance.IniReadValue(section, name);
string value = (string)prefList.Find(i => i.section == section && i.name == name).value;
if (value != null && value != "")
return value;
else if (autoSave)
SetString(section, name, defaultValue);
return defaultValue;
}
/// <summary>
/// Gets an int from the JSON.
/// </summary>
/// <param name="section">Section of the key.</param>
/// <param name="name">Name of the key.</param>
/// <param name="defaultValue">Value that should be used when no value is found.</param>
/// <param name="autoSave">Whether or not the default value should be written if no value is found.</param>
/// <returns></returns>
public static int GetInt(string section, string name, int defaultValue = 0, bool autoSave = false)
{
int value;
if (int.TryParse((string)prefList.Find(i => i.section == section && i.name == name).value, out value))
return value;
else if (autoSave)
SetInt(section, name, defaultValue);
return defaultValue;
}
/// <summary>
/// Gets a float from the JSON.
/// </summary>
/// <param name="section">Section of the key.</param>
/// <param name="name">Name of the key.</param>
/// <param name="defaultValue">Value that should be used when no value is found.</param>
/// <param name="autoSave">Whether or not the default value should be written if no value is found.</param>
/// <returns></returns>
public static float GetFloat(string section, string name, float defaultValue = 0f, bool autoSave = false)
{
float value;
if (float.TryParse((string)prefList.Find(i => i.section == section && i.name == name).value, out value))
return value;
else if (autoSave)
SetFloat(section, name, defaultValue);
return defaultValue;
}
/// <summary>
/// Gets a bool from the JSON.
/// </summary>
/// <param name="section">Section of the key.</param>
/// <param name="name">Name of the key.</param>
/// <param name="defaultValue">Value that should be used when no value is found.</param>
/// <param name="autoSave">Whether or not the default value should be written if no value is found.</param>
/// <returns></returns>
public static bool GetBool(string section, string name, bool defaultValue = false, bool autoSave = false)
{
if (prefList.Find(i => i.section == section && i.name == name) != null)
{
bool value = (bool)prefList.Find(i => i.section == section && i.name == name).value;
return value;
}
else if (autoSave)
{
SetBool(section, name, defaultValue);
}
return defaultValue;
}
/// <summary>
/// Checks whether or not a key exists in the JSON.
/// </summary>
/// <param name="section">Section of the key.</param>
/// <param name="name">Name of the key.</param>
/// <returns></returns>
public static bool HasKey(string section, string name)
{
if (prefList.Find(i => i.section == section && i.name == name) != null)
return true;
else
return false;
//return Instance.IniReadValue(section, name) != null;
}
/// <summary>
/// Sets a float in the JSON.
/// </summary>
/// <param name="section">Section of the key.</param>
/// <param name="name">Name of the key.</param>
/// <param name="value">Value that should be written.</param>
public static void SetFloat(string section, string name, float value)
{
if (prefList.Find(i => i.section == section && i.name == name) != null)
prefList.Find(i => i.section == section && i.name == name).value = value.ToString();
else
prefList.Add(new Pref(){ section = section, name = name, value = value });
WriteJson();
}
/// <summary>
/// Sets an int in the JSON.
/// </summary>
/// <param name="section">Section of the key.</param>
/// <param name="name">Name of the key.</param>
/// <param name="value">Value that should be written.</param>
public static void SetInt(string section, string name, int value)
{
if (prefList.Find(i => i.section == section && i.name == name) != null)
prefList.Find(i => i.section == section && i.name == name).value = value.ToString();
else
prefList.Add(new Pref() { section = section, name = name, value = value });
WriteJson();
}
/// <summary>
/// Sets a string in the JSON.
/// </summary>
/// <param name="section">Section of the key.</param>
/// <param name="name">Name of the key.</param>
/// <param name="value">Value that should be written.</param>
public static void SetString(string section, string name, string value)
{
if (prefList.Find(i => i.section == section && i.name == name) != null)
prefList.Find(i => i.section == section && i.name == name).value = value;
else
prefList.Add(new Pref() { section = section, name = name, value = value });
WriteJson();
}
/// <summary>
/// Sets a bool in the JSON.
/// </summary>
/// <param name="section">Section of the key.</param>
/// <param name="name">Name of the key.</param>
/// <param name="value">Value that should be written.</param>
public static void SetBool(string section, string name, bool value)
{
if (prefList.Find(i => i.section == section && i.name == name) != null)
prefList.Find(i => i.section == section && i.name == name).value = value;
else
prefList.Add(new Pref() { section = section, name = name, value = value });
WriteJson();
}
}
}