forked from Elfocrash/L2dotNET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStatsSet.cs
More file actions
247 lines (203 loc) · 9.62 KB
/
Copy pathStatsSet.cs
File metadata and controls
247 lines (203 loc) · 9.62 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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using NLog;
namespace L2dotNET.Templates
{
public class StatsSet : Dictionary<string, object>
{
private static readonly Logger Log = LogManager.GetCurrentClassLogger();
public StatsSet() { }
public StatsSet(int size) : base(size) { }
public StatsSet(StatsSet set) : base(set) { }
public void Set(string key, object value)
{
Add(key, value);
}
public void Set(string key, string value)
{
Add(key, value);
}
public void Set(string key, bool value)
{
Add(key, value);
}
public void Set(string key, int value)
{
Add(key, value);
}
public void Set(string key, int[] value)
{
Add(key, value);
}
public void Set(string key, long value)
{
Add(key, value);
}
public void Set(string key, double value)
{
Add(key, value);
}
public void Set(string key, Enum value)
{
Add(key, value);
}
public void Set<T>(string key, T value)
{
Add(key, value);
}
public void Unset(string key)
{
Remove(key);
}
public bool GetBool(string key, bool defaultValue = default(bool))
{
//check if the dictionary contains the key
if (!ContainsKey(key))
return defaultValue;
string value = base[key].ToString();
bool toReturn;
return bool.TryParse(value, out toReturn) ? toReturn : defaultValue;
// else
// Log.Error($"Conversion of key '{ key }' failed! Cannot convert value '{ value }' to '{ typeof(bool).FullName }'! The function will return the 'defaultValue' parameter.");
// Log.Warn($"Key '{ key }' was not found in the dictionary! The function will return the 'defaultValue' parameter.");
//if key doesn't exists,
//returns the defaultValue var
}
public byte GetByte(string key, byte defaultValue = default(byte))
{
//check if the dictionary contains the key
if (!ContainsKey(key))
return defaultValue;
string value = base[key].ToString();
byte toReturn;
return byte.TryParse(value, out toReturn) ? toReturn : defaultValue;
//else
// Log.Error($"Conversion of key '{ key }' failed! Cannot convert value '{ value }' to '{ typeof(byte).FullName }'! The function will return the 'defaultValue' parameter.");
//Log.Warn($"Key '{ key }' was not found in the dictionary! The function will return the 'defaultValue' parameter.");
//if key doesn't exists,
//returns the defaultValue var
}
public int[] GetIntegerArray(string key)
{
object val = this[key];
if (val is int[])
return (int[])val;
if (val is int)
return new[] { int.Parse(val.ToString()) };
if (!(val is string))
return null;
string[] vals = ((string)val).Split(';');
int[] result = new int[vals.Length];
int i = 0;
foreach (string v in vals)
result[i++] = int.Parse(v);
return result;
}
public int GetInt(string key, int defaultValue = default(int))
{
//check if the dictionary contains the key
if (!ContainsKey(key))
return defaultValue;
string value = base[key].ToString();
int toReturn;
return int.TryParse(value, out toReturn) ? toReturn : defaultValue;
//else
// Log.Error($"Conversion of key '{ key }' failed! Cannot convert value '{ value }' to '{ typeof(int).FullName }'! The function return the 'defaultValue' parameter.");
//Log.Warn($"Key '{ key }' was not found in the dictionary! The function will return the 'defaultValue' parameter.");
//if key doesn't exists,
//returns the defaultValue var
}
public float GetFloat(string key, float defaultValue = default(float))
{
//check if the dictionary contains the key
if (!ContainsKey(key))
return defaultValue;
string value = base[key].ToString();
float toReturn;
return float.TryParse(value, out toReturn) ? toReturn : defaultValue;
// else
// Log.Error($"Conversion of key '{ key }' failed! Cannot convert value '{ value }' to '{ typeof(float).FullName }'! The function return the 'defaultValue' parameter.");
// Log.Warn($"Key '{ key }' was not found in the dictionary! The function will return the 'defaultValue' parameter.");
//if key doesn't exists,
//returns the defaultValue var
}
public double GetDouble(string key, double defaultValue = default(double))
{
//check if the dictionary contains the key
if (!ContainsKey(key))
return defaultValue;
string value = base[key].ToString();
double toReturn;
return double.TryParse(value, out toReturn) ? toReturn : defaultValue;
//else
// Log.Error($"Conversion of key '{ key }' failed! Cannot convert value '{ value }' to '{ typeof(double).FullName }'! The function will return the 'defaultValue' parameter.");
//Log.Warn($"Key '{ key }' was not found in the dictionary! The function will return the 'defaultValue' parameter.");
//if key doesn't exists,
//returns the defaultValue var
}
public string GetString(string key, string defaultValue = default(string))
{
//check if the dictionary contains the key
if (!ContainsKey(key))
return defaultValue;
string value = base[key].ToString();
return value;
//Log.Warn($"Key '{ key }' was not found in the dictionary! The function will return the 'defaultValue' parameter.");
//if key doesn't exists,
//returns the defaultValue var
}
///<summary>Gets the 'value' from dictionary based on 'key' parameter and converts it to the type of T.</summary>
///<typeparam name="T"></typeparam>
///<param name="key">Specific 'key' to get the 'value' from dictionary.</param>
///<param name="defaultValue">Optional. When not specified, has a value of 'default(T)'.</param>
///<returns>Converted value when Parse not fails, if it fails then returns 'defaultValue'.</returns>
public T Get<T>(string key, T defaultValue = default(T)) where T : struct, IConvertible
{
if (string.IsNullOrWhiteSpace(key))
return defaultValue;
//check if the dictionary contains the key
if (!ContainsKey(key))
return defaultValue;
string value = base[key].ToString();
if (typeof(T).IsEnum) //block for Enum handling
{
T result;
if (Enum.TryParse(value, out result))
return Enum.IsDefined(typeof(T), result) ? result : Enum.GetValues(typeof(T)).Cast<T>().FirstOrDefault();
// Log.Error($"Conversion of key '{ key }' failed! Cannot convert value '{ value }' to '{ typeof(T).FullName }'! The function will return the first enum element.");
return Enum.GetValues(typeof(T)).Cast<T>().FirstOrDefault(); //if it returned false, returns the first element from enum. Default value is always 0, but not every enum has it.
}
//find the TryParse method.
MethodInfo parseMethod = typeof(T).GetMethod("TryParse",
//We want the public static one
BindingFlags.Public | BindingFlags.Static, Type.DefaultBinder,
//where the arguments are (string, out T)
new[] { typeof(string), typeof(T).MakeByRefType() }, null);
if (parseMethod == null)
return defaultValue;
//create the parameter list for the function call
object[] args = { value, default(T) };
//and then call the function.
if ((bool)parseMethod.Invoke(null, args))
return (T)args[1]; //if it returned true, returns converted value
// Log.Error($"Conversion of key '{ key }' failed! Cannot convert value '{ value }' to '{ typeof(T).FullName }'! The function will return the 'defaultValue' parameter.");
return defaultValue; //if it returned false, returns defaultValue' parameter."
//if key doesn't exists,
//returns the defaultValue var,
//when not specified returns the default value of 'T'
// Log.Warn($"Key '{ key }' was not found in the dictionary! The function will return the 'defaultValue' parameter.");
}
public string[] GetStringArray(string key)
{
object val = this[key];
if (val is string[])
return (string[])val;
if (val is string)
return ((string)val).Split(';');
return null;
//throw new IllegalArgumentException($"StatsSet : String array required, but found: {val} for key: {key}.");
}
}
}