Skip to content

Commit 9e454bd

Browse files
committed
Move some watch class logic from instance methods to static
1 parent 3cd9318 commit 9e454bd

File tree

5 files changed

+93
-122
lines changed

5 files changed

+93
-122
lines changed

src/BizHawk.Client.Common/tools/Cheat.cs

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -91,35 +91,20 @@ public MemoryDomain Domain
9191

9292
public string AddressStr => _watch.AddressString;
9393

94-
public string ValueStr =>
95-
_watch.Size switch
96-
{
97-
WatchSize.Byte => ((ByteWatch) _watch).FormatValue((byte)_val),
98-
WatchSize.Word => ((WordWatch) _watch).FormatValue((ushort)_val),
99-
WatchSize.DWord => ((DWordWatch) _watch).FormatValue((uint)_val),
100-
WatchSize.Separator => "",
101-
_ => string.Empty,
102-
};
94+
public string ValueStr
95+
=> _watch.Size is WatchSize.Byte or WatchSize.Word or WatchSize.DWord
96+
? _watch.IsValid
97+
? Watch.FormatValue(unchecked((uint) _val), _watch.Size, _watch.Type)
98+
: "-"
99+
: string.Empty;
103100

104101
public string CompareStr
105-
{
106-
get
107-
{
108-
if (_compare.HasValue)
109-
{
110-
return _watch.Size switch
111-
{
112-
WatchSize.Byte => ((ByteWatch) _watch).FormatValue((byte)_compare.Value),
113-
WatchSize.Word => ((WordWatch) _watch).FormatValue((ushort)_compare.Value),
114-
WatchSize.DWord => ((DWordWatch) _watch).FormatValue((uint)_compare.Value),
115-
WatchSize.Separator => "",
116-
_ => string.Empty,
117-
};
118-
}
119-
120-
return "";
121-
}
122-
}
102+
=> _compare.Value is int compareValue
103+
&& _watch.Size is WatchSize.Byte or WatchSize.Word or WatchSize.DWord
104+
? _watch.IsValid
105+
? Watch.FormatValue(unchecked((uint) compareValue), _watch.Size, _watch.Type)
106+
: "-"
107+
: string.Empty;
123108

124109
public CompareType ComparisonType { get; }
125110

src/BizHawk.Client.Common/tools/Watch/ByteWatch.cs

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using System.Collections.Generic;
2-
using System.Globalization;
2+
33
using BizHawk.Emulation.Common;
44

55
namespace BizHawk.Client.Common
@@ -56,16 +56,7 @@ public override bool Poke(string value)
5656
{
5757
try
5858
{
59-
byte val = Type switch
60-
{
61-
WatchDisplayType.Unsigned => byte.Parse(value),
62-
WatchDisplayType.Signed => (byte)sbyte.Parse(value),
63-
WatchDisplayType.Hex => byte.Parse(value, NumberStyles.HexNumber),
64-
WatchDisplayType.Binary => Convert.ToByte(value, 2),
65-
_ => 0,
66-
};
67-
68-
PokeByte(val);
59+
PokeByte(unchecked((byte) Watch.ParseValue(value, Size, Type)));
6960
return true;
7061
}
7162
catch
@@ -104,17 +95,7 @@ public override void Update(PreviousType previousType)
10495

10596
// TODO: Implements IFormattable
10697
public string FormatValue(byte val)
107-
{
108-
return Type switch
109-
{
110-
_ when !IsValid => "-",
111-
WatchDisplayType.Unsigned => val.ToString(),
112-
WatchDisplayType.Signed => ((sbyte) val).ToString(),
113-
WatchDisplayType.Hex => $"{val:X2}",
114-
WatchDisplayType.Binary => Convert.ToString(val, 2).PadLeft(8, '0').Insert(4, " "),
115-
_ => val.ToString(),
116-
};
117-
}
98+
=> IsValid ? Watch.FormatValue(val, Size, Type) : "-";
11899

119100
public override string Diff => $"{_value - _previous:+#;-#;0}";
120101

src/BizHawk.Client.Common/tools/Watch/DWordWatch.cs

Lines changed: 3 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System.Collections.Generic;
2-
using System.Globalization;
3-
using BizHawk.Common.NumberExtensions;
2+
43
using BizHawk.Emulation.Common;
54

65
namespace BizHawk.Client.Common
@@ -58,19 +57,7 @@ public override bool Poke(string value)
5857
{
5958
try
6059
{
61-
uint val = Type switch
62-
{
63-
WatchDisplayType.Unsigned => uint.Parse(value),
64-
WatchDisplayType.Signed => (uint)int.Parse(value),
65-
WatchDisplayType.Hex => uint.Parse(value, NumberStyles.HexNumber),
66-
WatchDisplayType.FixedPoint_20_12 => (uint)(double.Parse(value, NumberFormatInfo.InvariantInfo) * 4096.0),
67-
WatchDisplayType.FixedPoint_16_16 => (uint)(double.Parse(value, NumberFormatInfo.InvariantInfo) * 65536.0),
68-
WatchDisplayType.Float => NumberExtensions.ReinterpretAsUInt32(float.Parse(value, NumberFormatInfo.InvariantInfo)),
69-
WatchDisplayType.Binary => Convert.ToUInt32(value, 2),
70-
_ => 0,
71-
};
72-
73-
PokeDWord(val);
60+
PokeDWord(Watch.ParseValue(value, Size, Type));
7461
return true;
7562
}
7663
catch
@@ -109,36 +96,7 @@ public override void Update(PreviousType previousType)
10996

11097
// TODO: Implements IFormattable
11198
public string FormatValue(uint val)
112-
{
113-
string FormatFloat()
114-
{
115-
var _float = NumberExtensions.ReinterpretAsF32(val);
116-
return _float.ToString(NumberFormatInfo.InvariantInfo);
117-
}
118-
119-
string FormatBinary()
120-
{
121-
var str = Convert.ToString(val, 2).PadLeft(32, '0');
122-
for (var i = 28; i > 0; i -= 4)
123-
{
124-
str = str.Insert(i, " ");
125-
}
126-
return str;
127-
}
128-
129-
return Type switch
130-
{
131-
_ when !IsValid => "-",
132-
WatchDisplayType.Unsigned => val.ToString(),
133-
WatchDisplayType.Signed => ((int)val).ToString(),
134-
WatchDisplayType.Hex => $"{val:X8}",
135-
WatchDisplayType.FixedPoint_20_12 => ((int)val / 4096.0).ToString("0.######", NumberFormatInfo.InvariantInfo),
136-
WatchDisplayType.FixedPoint_16_16 => ((int)val / 65536.0).ToString("0.######", NumberFormatInfo.InvariantInfo),
137-
WatchDisplayType.Float => FormatFloat(),
138-
WatchDisplayType.Binary => FormatBinary(),
139-
_ => val.ToString(),
140-
};
141-
}
99+
=> IsValid ? Watch.FormatValue(val, Size, Type) : "-";
142100

143101
public override string Diff => $"{_value - (long)_previous:+#;-#;0}";
144102

src/BizHawk.Client.Common/tools/Watch/Watch.cs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,78 @@ public abstract class Watch
1919
IEquatable<Cheat>,
2020
IComparable<Watch>
2121
{
22+
private const string ERR_MSG_INVALID_WIDTH = "can only parse numeric strings for 1-, 2-, or 4-octet watches";
23+
24+
public static string FormatValue(uint value, WatchSize width, WatchDisplayType dispType)
25+
=> width switch
26+
{
27+
WatchSize.Byte => dispType switch
28+
{
29+
WatchDisplayType.Signed => unchecked((sbyte) value).ToString(),
30+
WatchDisplayType.Unsigned => value.ToString(),
31+
WatchDisplayType.Hex => $"{value:X2}",
32+
WatchDisplayType.Binary => Convert.ToString(unchecked((byte) value), toBase: 2).PadLeft(8, '0').Insert(4, " "),
33+
_ => value.ToString(), //TODO throw instead?
34+
},
35+
WatchSize.Word => dispType switch
36+
{
37+
WatchDisplayType.Signed => unchecked((short) value).ToString(),
38+
WatchDisplayType.Unsigned => value.ToString(),
39+
WatchDisplayType.Hex => $"{value:X4}",
40+
WatchDisplayType.Binary => Convert.ToString(unchecked((ushort) value), toBase: 2).PadLeft(16, '0')
41+
.Insert(8, " ").Insert(4, " ").Insert(14, " "),
42+
WatchDisplayType.FixedPoint_12_4 => $"{unchecked((short) value) / 16.0:F4}",
43+
_ => value.ToString(), //TODO throw instead?
44+
},
45+
WatchSize.DWord => dispType switch
46+
{
47+
WatchDisplayType.Signed => unchecked((int) value).ToString(),
48+
WatchDisplayType.Unsigned => value.ToString(),
49+
WatchDisplayType.Hex => $"{value:X8}",
50+
WatchDisplayType.Binary => Convert.ToString(value, toBase: 2).PadLeft(32, '0')
51+
.Insert(28, " ").Insert(24, " ").Insert(20, " ").Insert(16, " ").Insert(12, " ").Insert(8, " ").Insert(4, " "),
52+
WatchDisplayType.FixedPoint_20_12 => $"{unchecked((int) value) / 4096.0:0.######}",
53+
WatchDisplayType.FixedPoint_16_16 => $"{unchecked((int) value) / 65536.0:0.######}",
54+
WatchDisplayType.Float => NumberExtensions.ReinterpretAsF32(value).ToString(NumberFormatInfo.InvariantInfo),
55+
_ => value.ToString(), //TODO throw instead?
56+
},
57+
_ => throw new ArgumentOutOfRangeException(paramName: nameof(width), width, message: ERR_MSG_INVALID_WIDTH),
58+
};
59+
60+
public static uint ParseValue(string value, WatchSize width, WatchDisplayType dispType)
61+
=> width switch
62+
{
63+
WatchSize.Byte => dispType switch
64+
{
65+
WatchDisplayType.Signed => unchecked((byte) sbyte.Parse(value)),
66+
WatchDisplayType.Unsigned => byte.Parse(value),
67+
WatchDisplayType.Hex => byte.Parse(value, NumberStyles.HexNumber),
68+
WatchDisplayType.Binary => Convert.ToByte(value, fromBase: 2),
69+
_ => 0, //TODO throw instead?
70+
},
71+
WatchSize.Word => dispType switch
72+
{
73+
WatchDisplayType.Signed => unchecked((ushort) short.Parse(value)),
74+
WatchDisplayType.Unsigned => ushort.Parse(value),
75+
WatchDisplayType.Hex => ushort.Parse(value, NumberStyles.HexNumber),
76+
WatchDisplayType.Binary => Convert.ToUInt16(value, fromBase: 2),
77+
WatchDisplayType.FixedPoint_12_4 => unchecked((ushort) (16.0 * double.Parse(value, NumberFormatInfo.InvariantInfo))),
78+
_ => 0, //TODO throw instead?
79+
},
80+
WatchSize.DWord => dispType switch
81+
{
82+
WatchDisplayType.Signed => unchecked((uint) int.Parse(value)),
83+
WatchDisplayType.Unsigned => uint.Parse(value),
84+
WatchDisplayType.Hex => uint.Parse(value, NumberStyles.HexNumber),
85+
WatchDisplayType.Binary => Convert.ToUInt32(value, fromBase: 2),
86+
WatchDisplayType.FixedPoint_20_12 => unchecked((uint) (4096.0 * double.Parse(value, NumberFormatInfo.InvariantInfo))),
87+
WatchDisplayType.FixedPoint_16_16 => unchecked((uint) (65536.0 * double.Parse(value, NumberFormatInfo.InvariantInfo))),
88+
WatchDisplayType.Float => NumberExtensions.ReinterpretAsUInt32(float.Parse(value, NumberFormatInfo.InvariantInfo)),
89+
_ => 0, //TODO throw instead?
90+
},
91+
_ => throw new ArgumentOutOfRangeException(paramName: nameof(width), width, message: ERR_MSG_INVALID_WIDTH),
92+
};
93+
2294
private MemoryDomain _domain;
2395
private WatchDisplayType _type;
2496

src/BizHawk.Client.Common/tools/Watch/WordWatch.cs

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using System.Collections.Generic;
2-
using System.Globalization;
2+
33
using BizHawk.Emulation.Common;
44

55
namespace BizHawk.Client.Common
@@ -54,17 +54,7 @@ public override bool Poke(string value)
5454
{
5555
try
5656
{
57-
ushort val = Type switch
58-
{
59-
WatchDisplayType.Unsigned => ushort.Parse(value),
60-
WatchDisplayType.Signed => (ushort)short.Parse(value),
61-
WatchDisplayType.Hex => ushort.Parse(value, NumberStyles.HexNumber),
62-
WatchDisplayType.Binary => Convert.ToUInt16(value, 2),
63-
WatchDisplayType.FixedPoint_12_4 => (ushort)(double.Parse(value, NumberFormatInfo.InvariantInfo) * 16.0),
64-
_ => 0,
65-
};
66-
67-
PokeWord(val);
57+
PokeWord(unchecked((ushort) Watch.ParseValue(value, Size, Type)));
6858
return true;
6959
}
7060
catch
@@ -104,22 +94,7 @@ public override void Update(PreviousType previousType)
10494

10595
// TODO: Implements IFormattable
10696
public string FormatValue(ushort val)
107-
{
108-
return Type switch
109-
{
110-
_ when !IsValid => "-",
111-
WatchDisplayType.Unsigned => val.ToString(),
112-
WatchDisplayType.Signed => ((short) val).ToString(), WatchDisplayType.Hex => $"{val:X4}",
113-
WatchDisplayType.FixedPoint_12_4 => ((short)val / 16.0).ToString("F4", NumberFormatInfo.InvariantInfo),
114-
WatchDisplayType.Binary => Convert
115-
.ToString(val, 2)
116-
.PadLeft(16, '0')
117-
.Insert(8, " ")
118-
.Insert(4, " ")
119-
.Insert(14, " "),
120-
_ => val.ToString(),
121-
};
122-
}
97+
=> IsValid ? Watch.FormatValue(val, Size, Type) : "-";
12398

12499
public override string Diff => $"{_value - _previous:+#;-#;0}";
125100

0 commit comments

Comments
 (0)