-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStoredVariable.cs
More file actions
241 lines (213 loc) · 7.79 KB
/
StoredVariable.cs
File metadata and controls
241 lines (213 loc) · 7.79 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
using System;
using System.Collections.Generic;
using Terraria;
namespace FKTModSettings
{
public class StoredVariable
{
internal double storedValue;
internal bool storedBool
{
get { return storedValue == 0d ? false : true; }
set { storedValue = value ? 1d : 0d; }
}
internal Type storedType;
public bool IsBoolean { get { return storedType == typeof(bool); } }
public bool IsComment { get { return storedType == typeof(string); } }
public bool IsWholeNumbers
{
get {
return
storedType == typeof(int) ||
storedType == typeof(uint) ||
storedType == typeof(short) ||
storedType == typeof(ushort) ||
storedType == typeof(long) ||
storedType == typeof(byte);
}
}
public bool IsDecimalNumbers
{
get
{
return
storedType == typeof(float) ||
storedType == typeof(double);
}
}
internal double valMin = double.MinValue;
internal double valMax = double.MaxValue;
private bool HasChanged;
public readonly string DisplayName;
internal bool Multiplayer;
internal bool InMultiplayer
{
get { return Multiplayer && Main.netMode != 0; }
}
#region Constructors
/// <summary>
/// Store mod settings for comments
/// </summary>
/// <param name="displayName">The name shown in the menu</param>
public StoredVariable(string comment, float scale)
{
DisplayName = comment;
storedType = typeof(string);
storedValue = scale;
}
/// <summary>
/// Store mod settings for bool
/// </summary>
/// <param name="displayName">The name shown in the menu</param>
/// <param name="multiplayer">Use multiplayer behaviour eg. hide from clients</param>
/// <param name="Value">Initial value to set to</param>
public StoredVariable(string displayName, bool multiplayer, Type variableType)
{
DisplayName = displayName;
Multiplayer = multiplayer;
storedType = variableType;
if (!IsBoolean &&
!IsComment &&
!IsWholeNumbers &&
!IsDecimalNumbers)
{
throw new NotSupportedException("Variable type " + variableType.ToString() + " for " + displayName + " is not supported. ");
}
}
#endregion
public bool SetMinMax(double Min, double Max)
{
if (IsBoolean || IsComment) return false;
if (Min > Max) // RECURSION CAN'T GO WRONG - famous last words
{
return SetMinMax(Max, Min);
}
else
{
valMin = Min;
valMax = Max;
}
return true;
}
private void CheckType(Type t)
{
if (t != storedType) Main.NewTextMultiline("Wrong type provided for " + DisplayName + ", value given is " + t.ToString() + " but was expecting " + storedType.ToString(), false, Microsoft.Xna.Framework.Color.Red);
}
#region Updates
public void Update(ref bool Value)
{
CheckType(Value.GetType());
if (HasChanged) { Value = storedBool; HasChanged = false; }
else { storedBool = Value; }
}
public void Update(ref int Value)
{
CheckType(Value.GetType());
if (HasChanged) { Value = (int)storedValue; HasChanged = false; }
else { storedValue = Value; }
}
public void Update(ref uint Value)
{
CheckType(Value.GetType());
if (HasChanged) { Value = (uint)storedValue; HasChanged = false; }
else { storedValue = Value; }
}
public void Update(ref short Value)
{
CheckType(Value.GetType());
if (HasChanged) { Value = (short)storedValue; HasChanged = false; }
else { storedValue = Value; }
}
public void Update(ref ushort Value)
{
CheckType(Value.GetType());
if (HasChanged) { Value = (ushort)storedValue; HasChanged = false; }
else { storedValue = Value; }
}
public void Update(ref long Value)
{
CheckType(Value.GetType());
if (HasChanged) { Value = (long)storedValue; HasChanged = false; }
else { storedValue = Value; }
}
public void Update(ref byte Value)
{
CheckType(Value.GetType());
if (HasChanged) { Value = (byte)storedValue; HasChanged = false; }
else { storedValue = Value; }
}
public void Update(ref float Value)
{
CheckType(Value.GetType());
if (HasChanged) { Value = (float)storedValue; HasChanged = false; }
else { storedValue = Value; }
}
public void Update(ref double Value)
{
CheckType(Value.GetType());
if (HasChanged) { Value = (double)storedValue; HasChanged = false; }
else { storedValue = Value; }
}
#endregion
#region Set
public void Set(object Value)
{
try
{
Value = Convert.ChangeType(Value, storedType);
CheckType(Value.GetType());
if (InMultiplayer) return;
HasChanged = true;
if (storedType == typeof(bool))
{
storedBool = (bool)Value;
return;
}
else if (storedType == typeof(int))
{
storedValue = Math.Max(Math.Min((int)Value, (int)valMax), (int)valMin);
return;
}
else if (storedType == typeof(uint))
{
storedValue = Math.Max(Math.Min((uint)Value, (uint)valMax), (uint)valMin);
return;
}
else if (storedType == typeof(short))
{
storedValue = Math.Max(Math.Min((short)Value, (short)valMax), (short)valMin);
return;
}
else if (storedType == typeof(ushort))
{
storedValue = Math.Max(Math.Min((ushort)Value, (ushort)valMax), (ushort)valMin);
return;
}
else if (storedType == typeof(long))
{
storedValue = Math.Max(Math.Min((long)Value, (long)valMax), (long)valMin);
return;
}
else if (storedType == typeof(byte))
{
storedValue = Math.Max(Math.Min((byte)Value, (byte)valMax), (byte)valMin);
return;
}
else if (storedType == typeof(float))
{
storedValue = Math.Max(Math.Min((float)Value, (float)valMax), (float)valMin);
return;
}
else if (storedType == typeof(double))
{
storedValue = Math.Max(Math.Min((double)Value, (double)valMax), (double)valMin);
return;
}
}
catch { }
// Don't reach here unless it can't find anything
throw new InvalidCastException("Wrong type provided for " + DisplayName + ", value given is " + Value.GetType().ToString() + " but was expecting " + storedType.ToString());
}
#endregion
}
}