Skip to content

Commit 8473f76

Browse files
committed
Improve setttings.Get*List(), fixes #28
1 parent 7aae39e commit 8473f76

File tree

4 files changed

+92
-11
lines changed

4 files changed

+92
-11
lines changed

Assets/UXF/Scripts/Etc/Settings.cs

Lines changed: 64 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,65 +116,121 @@ public void SetParent(ISettingsContainer parent)
116116

117117
/// <summary>
118118
/// Get a boolean list setting value. If it is not found, the request will cascade upwards in each parent setting until one is found.
119+
/// If the setting references a setting stored in the settings json file, a copy of the list will be returned. If it is a setting created with settings.SetValue(...), the original reference will be returned.
119120
/// </summary>
120121
/// <param name="key">The key (name) of the setting.</param>
121122
public List<bool> GetBoolList(string key)
122123
{
123-
return GetObjectList(key).Select(v => Convert.ToBoolean(v)).ToList();
124+
try
125+
{
126+
return GetObjectList(key).Select(v => Convert.ToBoolean(v)).ToList();
127+
}
128+
catch (InvalidCastException)
129+
{
130+
return (List<bool>) Get(key);
131+
}
124132
}
125133

126134
/// <summary>
127135
/// Get a integer list setting value. If it is not found, the request will cascade upwards in each parent setting until one is found.
136+
/// If the setting references a setting stored in the settings json file, a copy of the list will be returned. If it is a setting created with settings.SetValue(...), the original reference will be returned.
128137
/// </summary>
129138
/// <param name="key">The key (name) of the setting.</param>
130139
public List<int> GetIntList(string key)
131140
{
132-
return GetObjectList(key).Select(v => Convert.ToInt32(v)).ToList();
141+
try
142+
{
143+
return GetObjectList(key).Select(v => Convert.ToInt32(v)).ToList();
144+
}
145+
catch (InvalidCastException)
146+
{
147+
return (List<int>) Get(key);
148+
}
133149
}
134150

135151
/// <summary>
136152
/// Get a float list setting value. If it is not found, the request will cascade upwards in each parent setting until one is found.
153+
/// If the setting references a setting stored in the settings json file, a copy of the list will be returned. If it is a setting created with settings.SetValue(...), the original reference will be returned.
137154
/// </summary>
138155
/// <param name="key">The key (name) of the setting.</param>
139156
public List<float> GetFloatList(string key)
140157
{
141-
return GetObjectList(key).Select(v => Convert.ToSingle(v)).ToList();
158+
try
159+
{
160+
return GetObjectList(key).Select(v => Convert.ToSingle(v)).ToList();
161+
}
162+
catch (InvalidCastException)
163+
{
164+
return (List<float>) Get(key);
165+
}
142166
}
143167

144168
/// <summary>
145169
/// Get a long list setting value. If it is not found, the request will cascade upwards in each parent setting until one is found.
170+
/// If the setting references a setting stored in the settings json file, a copy of the list will be returned. If it is a setting created with settings.SetValue(...), the original reference will be returned.
146171
/// </summary>
147172
/// <param name="key">The key (name) of the setting.</param>
148173
public List<long> GetLongList(string key)
149174
{
150-
return GetObjectList(key).Select(v => Convert.ToInt64(v)).ToList();
175+
try
176+
{
177+
return GetObjectList(key).Select(v => Convert.ToInt64(v)).ToList();
178+
}
179+
catch (InvalidCastException)
180+
{
181+
return (List<long>) Get(key);
182+
}
151183
}
152184

153185
/// <summary>
154186
/// Get a double list setting value. If it is not found, the request will cascade upwards in each parent setting until one is found.
187+
/// If the setting references a setting stored in the settings json file, a copy of the list will be returned. If it is a setting created with settings.SetValue(...), the original reference will be returned.
155188
/// </summary>
156189
/// <param name="key">The key (name) of the setting.</param>
157190
public List<double> GetDoubleList(string key)
158191
{
159-
return GetObjectList(key).Select(v => Convert.ToDouble(v)).ToList();
192+
try
193+
{
194+
return GetObjectList(key).Select(v => Convert.ToDouble(v)).ToList();
195+
}
196+
catch (InvalidCastException)
197+
{
198+
return (List<double>) Get(key);
199+
}
160200
}
161201

162202
/// <summary>
163203
/// Get a string list setting value. If it is not found, the request will cascade upwards in each parent setting until one is found.
204+
/// If the setting references a setting stored in the settings json file, a copy of the list will be returned. If it is a setting created with settings.SetValue(...), the original reference will be returned.
164205
/// </summary>
165206
/// <param name="key">The key (name) of the setting.</param>
166207
public List<string> GetStringList(string key)
167208
{
168-
return GetObjectList(key).Select(v => Convert.ToString(v)).ToList();
209+
try
210+
{
211+
return GetObjectList(key).Select(v => Convert.ToString(v)).ToList();
212+
}
213+
catch (InvalidCastException)
214+
{
215+
return (List<string>) Get(key);
216+
}
169217
}
170218

171219
/// <summary>
172-
/// Get a dictionary list setting value. If it is not found, the request will cascade upwards in each parent setting until one is found.
220+
/// Get a Dictionary<string, object> list setting value. If it is not found, the request will cascade upwards in each parent setting until one is found.
221+
/// If the setting references a setting stored in the settings json file, a copy of the list will be returned. If it is a setting created with settings.SetValue(...), the original reference will be returned.
173222
/// </summary>
174223
/// <param name="key">The key (name) of the setting.</param>
175224
public List<Dictionary<string, object>> GetDictList(string key)
176225
{
177-
return GetObjectList(key).Select(v => (Dictionary<string, object>) v).ToList();
226+
try
227+
{
228+
return GetObjectList(key).Select(v => (Dictionary<string, object>) v).ToList();
229+
}
230+
catch (InvalidCastException)
231+
{
232+
return (List<Dictionary<string, object>>) Get(key);
233+
}
178234
}
179235

180236
/// <summary>

Assets/UXF/Scripts/Tests/Editor/TestSettings.cs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public void DictToSettings()
5454
var deserializedList = new List<double>() { 1.44, 2, 3 };
5555
var deserializedIntList = new List<double>() { 1, 2, 3 };
5656
var deserializedDoubleList = new List<double>() { 1.44, 2.44, 3.44 };
57-
var deserializedFloatList = new List<double>() { 1.44f, 2.44f, 3.44f };
57+
var deserializedFloatList = new List<float>() { 1.44f, 2.44f, 3.44f };
5858
var deserializedObject = new Dictionary<string, object>()
5959
{
6060
{ "key1", "value1" },
@@ -94,6 +94,31 @@ public void GetSetSettings()
9494
Assert.Throws<KeyNotFoundException>(() => settings.GetObject("key3"));
9595
}
9696

97+
[Test]
98+
public void CastListSettings()
99+
{
100+
var createdObjectList = new List<object>() { null, 1, "hello" };
101+
var createdIntList = new List<int>() { 1, 2, 3 };
102+
var createdDoubleList = new List<double>() { 1.44, 2.44, 3.44 };
103+
var createdFloatList = new List<float>() { 1.44f, 2.44f, 3.44f };
104+
var createdBoolList = new List<bool>() { false, true, false };
105+
106+
Settings settings = new Settings(new Dictionary<string, object>()
107+
{
108+
{ "object_list", createdObjectList },
109+
{ "int_list", createdIntList },
110+
{ "double_list", createdDoubleList },
111+
{ "float_list", createdFloatList },
112+
{ "bool_list", createdBoolList }
113+
});
114+
115+
Assert.AreEqual(settings.GetObjectList("object_list"), createdObjectList);
116+
Assert.AreEqual(settings.GetIntList("int_list"), createdIntList);
117+
Assert.AreEqual(settings.GetDoubleList("double_list"), createdDoubleList);
118+
Assert.AreEqual(settings.GetFloatList("float_list"), createdFloatList);
119+
Assert.AreEqual(settings.GetBoolList("bool_list"), createdBoolList);
120+
}
121+
97122
[Test]
98123
public void CascadeSettings()
99124
{

ProjectSettings/ProjectVersion.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
m_EditorVersion: 2017.4.36f1
1+
m_EditorVersion: 2017.4.40f1

docs/wiki

Submodule wiki updated from 0be4a3b to ac3b13e

0 commit comments

Comments
 (0)