Skip to content

Commit ea5546e

Browse files
committed
better enum parsing
1 parent dd52df9 commit ea5546e

File tree

5 files changed

+33
-189
lines changed

5 files changed

+33
-189
lines changed

RasterPropMonitor/Auxiliary modules/JSILabel.cs

Lines changed: 4 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -139,41 +139,9 @@ public void Start()
139139

140140
if (!string.IsNullOrEmpty(anchor))
141141
{
142-
if (anchor == TextAnchor.LowerCenter.ToString())
142+
if (Enum.TryParse(anchor, out TextAnchor textAnchor))
143143
{
144-
textObj.anchor = TextAnchor.LowerCenter;
145-
}
146-
else if (anchor == TextAnchor.LowerLeft.ToString())
147-
{
148-
textObj.anchor = TextAnchor.LowerLeft;
149-
}
150-
else if (anchor == TextAnchor.LowerRight.ToString())
151-
{
152-
textObj.anchor = TextAnchor.LowerRight;
153-
}
154-
else if (anchor == TextAnchor.MiddleCenter.ToString())
155-
{
156-
textObj.anchor = TextAnchor.MiddleCenter;
157-
}
158-
else if (anchor == TextAnchor.MiddleLeft.ToString())
159-
{
160-
textObj.anchor = TextAnchor.MiddleLeft;
161-
}
162-
else if (anchor == TextAnchor.MiddleRight.ToString())
163-
{
164-
textObj.anchor = TextAnchor.MiddleRight;
165-
}
166-
else if (anchor == TextAnchor.UpperCenter.ToString())
167-
{
168-
textObj.anchor = TextAnchor.UpperCenter;
169-
}
170-
else if (anchor == TextAnchor.UpperLeft.ToString())
171-
{
172-
textObj.anchor = TextAnchor.UpperLeft;
173-
}
174-
else if (anchor == TextAnchor.UpperRight.ToString())
175-
{
176-
textObj.anchor = TextAnchor.UpperRight;
144+
textObj.anchor = textAnchor;
177145
}
178146
else
179147
{
@@ -183,17 +151,9 @@ public void Start()
183151

184152
if (!string.IsNullOrEmpty(alignment))
185153
{
186-
if (alignment == TextAlignment.Center.ToString())
187-
{
188-
textObj.alignment = TextAlignment.Center;
189-
}
190-
else if (alignment == TextAlignment.Left.ToString())
191-
{
192-
textObj.alignment = TextAlignment.Left;
193-
}
194-
else if (alignment == TextAlignment.Right.ToString())
154+
if (Enum.TryParse(alignment, out TextAlignment textAlignment))
195155
{
196-
textObj.alignment = TextAlignment.Right;
156+
textObj.alignment = textAlignment;
197157
}
198158
else
199159
{

RasterPropMonitor/Auxiliary modules/JSISetInternalCameraFOV.cs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -66,20 +66,7 @@ public void Start()
6666
seatData.maxRot = moduleConfig.GetFloat("maxRot") ?? defaultMaxRot;
6767
seatData.maxPitch = moduleConfig.GetFloat("maxPitch") ?? defaultMaxPitch;
6868
seatData.minPitch = moduleConfig.GetFloat("minPitch") ?? defaultMinPitch;
69-
seatData.hideKerbal = HideKerbal.none;
70-
71-
if (moduleConfig.HasValue("hideKerbal"))
72-
{
73-
string hideKerbalVal = moduleConfig.GetValue("hideKerbal");
74-
if (hideKerbalVal == HideKerbal.head.ToString())
75-
{
76-
seatData.hideKerbal = HideKerbal.head;
77-
}
78-
else if (hideKerbalVal == HideKerbal.all.ToString())
79-
{
80-
seatData.hideKerbal = HideKerbal.all;
81-
}
82-
}
69+
moduleConfig.TryGetEnum("hideKerbal", ref seatData.hideKerbal, default);
8370

8471
seats.Add(seatData);
8572
JUtil.LogMessage(this, "Setting per-seat camera parameters for seat {0}: fov {1}, maxRot {2}, maxPitch {3}, minPitch {4}, hideKerbal {5}",

RasterPropMonitor/Core/CustomVariable.cs

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -83,38 +83,9 @@ internal CustomVariable(ConfigNode node, RasterPropMonitorComputer rpmComp)
8383
throw new ArgumentException("Did not find any SOURCE_VARIABLE nodes in RPM_CUSTOM_VARIABLE", name);
8484
}
8585

86-
string oper = node.GetValue("operator");
87-
if (oper == Operator.NONE.ToString())
86+
if (!node.TryGetEnum("operator", ref op, default))
8887
{
89-
op = Operator.NONE;
90-
}
91-
else if (oper == Operator.AND.ToString())
92-
{
93-
op = Operator.AND;
94-
}
95-
else if (oper == Operator.OR.ToString())
96-
{
97-
op = Operator.OR;
98-
}
99-
else if (oper == Operator.NAND.ToString())
100-
{
101-
op = Operator.NAND;
102-
}
103-
else if (oper == Operator.NOR.ToString())
104-
{
105-
op = Operator.NOR;
106-
}
107-
else if (oper == Operator.XOR.ToString())
108-
{
109-
op = Operator.XOR;
110-
}
111-
else if (oper == Operator.ISNANORINF.ToString())
112-
{
113-
op = Operator.ISNANORINF;
114-
}
115-
else
116-
{
117-
throw new ArgumentException("Found an invalid operator type in RPM_CUSTOM_VARIABLE", oper);
88+
throw new ArgumentException("Found an invalid operator type in RPM_CUSTOM_VARIABLE", node.GetValue("operator"));
11889
}
11990
}
12091

RasterPropMonitor/Core/MathVariable.cs

Lines changed: 25 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -55,78 +55,33 @@ internal MathVariable(ConfigNode node, RasterPropMonitorComputer rpmComp)
5555

5656
int maxParameters = int.MaxValue;
5757

58-
string oper = node.GetValue("operator");
59-
if (oper == Operator.NONE.ToString())
58+
if (!node.TryGetEnum("operator", ref op, default))
6059
{
61-
op = Operator.NONE;
62-
indexOperator = false;
60+
throw new ArgumentException("Found an invalid operator type in RPM_CUSTOM_VARIABLE", node.GetValue("operator"));
6361
}
64-
else if (oper == Operator.ADD.ToString())
65-
{
66-
op = Operator.ADD;
67-
indexOperator = false;
68-
}
69-
else if (oper == Operator.SUBTRACT.ToString())
70-
{
71-
op = Operator.SUBTRACT;
72-
indexOperator = false;
73-
}
74-
else if (oper == Operator.MULTIPLY.ToString())
75-
{
76-
op = Operator.MULTIPLY;
77-
indexOperator = false;
78-
}
79-
else if (oper == Operator.DIVIDE.ToString())
80-
{
81-
op = Operator.DIVIDE;
82-
indexOperator = false;
83-
}
84-
else if (oper == Operator.MAX.ToString())
85-
{
86-
op = Operator.MAX;
87-
indexOperator = false;
88-
}
89-
else if (oper == Operator.MIN.ToString())
90-
{
91-
op = Operator.MIN;
92-
indexOperator = false;
93-
}
94-
else if (oper == Operator.POWER.ToString())
95-
{
96-
op = Operator.POWER;
97-
indexOperator = false;
98-
}
99-
else if (oper == Operator.ANGLEDELTA.ToString())
100-
{
101-
op = Operator.ANGLEDELTA;
102-
indexOperator = false;
103-
maxParameters = 2;
104-
}
105-
else if (oper == Operator.ATAN2.ToString())
106-
{
107-
op = Operator.ATAN2;
108-
indexOperator = false;
109-
maxParameters = 2;
110-
}
111-
else if (oper == Operator.MODULO.ToString())
112-
{
113-
op = Operator.MODULO;
114-
indexOperator = false;
115-
maxParameters = 2;
116-
}
117-
else if (oper == Operator.MAXINDEX.ToString())
118-
{
119-
op = Operator.MAXINDEX;
120-
indexOperator = true;
121-
}
122-
else if (oper == Operator.MININDEX.ToString())
123-
{
124-
op = Operator.MININDEX;
125-
indexOperator = true;
126-
}
127-
else
128-
{
129-
throw new ArgumentException("Found an invalid operator type in RPM_CUSTOM_VARIABLE", oper);
62+
63+
switch (op)
64+
{
65+
case Operator.NONE:
66+
case Operator.ADD:
67+
case Operator.SUBTRACT:
68+
case Operator.MULTIPLY:
69+
case Operator.DIVIDE:
70+
case Operator.MAX:
71+
case Operator.MIN:
72+
indexOperator = false;
73+
break;
74+
case Operator.POWER:
75+
case Operator.ANGLEDELTA:
76+
case Operator.ATAN2:
77+
case Operator.MODULO:
78+
indexOperator = false;
79+
maxParameters = 2;
80+
break;
81+
case Operator.MAXINDEX:
82+
case Operator.MININDEX:
83+
indexOperator = true;
84+
break;
13085
}
13186

13287
string[] sources = node.GetValues("sourceVariable");

RasterPropMonitor/Handlers/JSIGraphingBackground.cs

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -211,36 +211,7 @@ public DataSet(ConfigNode node, RasterPropMonitorComputer rpmComp)
211211
lineWidth = int.Parse(node.GetValue("borderWidth"));
212212
}
213213

214-
string graphTypeStr = node.GetValue("graphType").Trim();
215-
if (graphTypeStr == GraphType.VerticalUp.ToString())
216-
{
217-
graphType = GraphType.VerticalUp;
218-
}
219-
else if (graphTypeStr == GraphType.VerticalDown.ToString())
220-
{
221-
graphType = GraphType.VerticalDown;
222-
}
223-
else if (graphTypeStr == GraphType.VerticalSplit.ToString())
224-
{
225-
graphType = GraphType.VerticalSplit;
226-
}
227-
else if (graphTypeStr == GraphType.HorizontalRight.ToString())
228-
{
229-
graphType = GraphType.HorizontalRight;
230-
}
231-
else if (graphTypeStr == GraphType.HorizontalLeft.ToString())
232-
{
233-
graphType = GraphType.HorizontalLeft;
234-
}
235-
else if (graphTypeStr == GraphType.HorizontalSplit.ToString())
236-
{
237-
graphType = GraphType.HorizontalSplit;
238-
}
239-
else if (graphTypeStr == GraphType.Lamp.ToString())
240-
{
241-
graphType = GraphType.Lamp;
242-
}
243-
else
214+
if (!node.TryGetEnum("graphType", ref graphType, default))
244215
{
245216
throw new ArgumentException("Unknown 'graphType' in DATA_SET");
246217
}

0 commit comments

Comments
 (0)