Skip to content

Commit 6ef58bd

Browse files
committed
Extract superclass from Mini*Watch and dedup
1 parent 850e5c4 commit 6ef58bd

File tree

3 files changed

+53
-124
lines changed

3 files changed

+53
-124
lines changed

src/BizHawk.Client.Common/tools/RamSearchEngine/IMiniWatch.cs

Lines changed: 37 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -10,124 +10,72 @@ internal interface IMiniWatch
1010
{
1111
long Address { get; }
1212
uint Previous { get; }
13+
14+
uint GetValue(MemoryDomain domain, bool bigEndian);
15+
1316
void SetPreviousToCurrent(MemoryDomain domain, bool bigEndian);
1417
bool IsValid(MemoryDomain domain);
1518
}
1619

17-
internal class MiniByteWatch : IMiniWatch
20+
internal abstract class MiniWatchBase : IMiniWatch
1821
{
1922
public long Address { get; }
20-
private protected byte _previous;
2123

22-
public MiniByteWatch(MemoryDomain domain, long addr)
24+
public uint Previous { get; protected set; }
25+
26+
protected MiniWatchBase(long addr, uint prevValue)
2327
{
2428
Address = addr;
25-
_previous = GetByte(Address, domain);
29+
Previous = prevValue;
2630
}
2731

28-
public uint Previous => _previous;
32+
public uint GetValue(MemoryDomain domain, bool bigEndian)
33+
=> IsValid(domain) ? GetValueInner(Address, domain, bigEndian: bigEndian) : default;
34+
35+
protected abstract uint GetValueInner(long address, MemoryDomain domain, bool bigEndian);
2936

3037
public bool IsValid(MemoryDomain domain)
31-
{
32-
return IsValid(Address, domain);
33-
}
38+
=> IsValid(Address, domain);
39+
40+
protected abstract bool IsValid(long address, MemoryDomain domain);
3441

3542
public virtual void SetPreviousToCurrent(MemoryDomain domain, bool bigEndian)
36-
{
37-
_previous = GetByte(Address, domain);
38-
}
43+
=> Previous = GetValueInner(Address, domain, bigEndian: bigEndian);
44+
}
3945

40-
public static bool IsValid(long address, MemoryDomain domain)
41-
{
42-
return address < domain.Size;
43-
}
46+
internal class MiniByteWatch : MiniWatchBase
47+
{
48+
public MiniByteWatch(MemoryDomain domain, long addr)
49+
: base(addr: addr, prevValue: domain.PeekByte(addr)) {}
4450

45-
public static byte GetByte(long address, MemoryDomain domain)
46-
{
47-
if (!IsValid(address, domain))
48-
{
49-
return 0;
50-
}
51+
protected override uint GetValueInner(long address, MemoryDomain domain, bool bigEndian)
52+
=> domain.PeekByte(address);
5153

52-
return domain.PeekByte(address);
53-
}
54+
protected override bool IsValid(long address, MemoryDomain domain)
55+
=> 0L <= address && address < domain.Size;
5456
}
5557

56-
internal class MiniWordWatch : IMiniWatch
58+
internal class MiniWordWatch : MiniWatchBase
5759
{
58-
public long Address { get; }
59-
private protected ushort _previous;
60-
6160
public MiniWordWatch(MemoryDomain domain, long addr, bool bigEndian)
62-
{
63-
Address = addr;
64-
_previous = GetUshort(Address, domain, bigEndian);
65-
}
61+
: base(addr: addr, prevValue: domain.PeekUshort(addr, bigEndian: bigEndian)) {}
6662

67-
public uint Previous => _previous;
63+
protected override uint GetValueInner(long address, MemoryDomain domain, bool bigEndian)
64+
=> domain.PeekUshort(address, bigEndian);
6865

69-
public virtual void SetPreviousToCurrent(MemoryDomain domain, bool bigEndian)
70-
{
71-
_previous = GetUshort(Address, domain, bigEndian);
72-
}
73-
74-
public bool IsValid(MemoryDomain domain)
75-
{
76-
return IsValid(Address, domain);
77-
}
78-
79-
public static bool IsValid(long address, MemoryDomain domain)
80-
{
81-
return address < (domain.Size - 1);
82-
}
83-
84-
public static ushort GetUshort(long address, MemoryDomain domain, bool bigEndian)
85-
{
86-
if (!IsValid(address, domain))
87-
{
88-
return 0;
89-
}
90-
91-
return domain.PeekUshort(address, bigEndian);
92-
}
66+
protected override bool IsValid(long address, MemoryDomain domain)
67+
=> 0L <= address && address <= domain.Size - sizeof(ushort);
9368
}
9469

95-
internal class MiniDWordWatch : IMiniWatch
70+
internal class MiniDWordWatch : MiniWatchBase
9671
{
97-
public long Address { get; }
98-
private protected uint _previous;
99-
10072
public MiniDWordWatch(MemoryDomain domain, long addr, bool bigEndian)
101-
{
102-
Address = addr;
103-
_previous = GetUint(Address, domain, bigEndian);
104-
}
73+
: base(addr: addr, prevValue: domain.PeekUint(addr, bigEndian: bigEndian)) {}
10574

106-
public uint Previous => _previous;
75+
protected override uint GetValueInner(long address, MemoryDomain domain, bool bigEndian)
76+
=> domain.PeekUint(address, bigEndian);
10777

108-
public virtual void SetPreviousToCurrent(MemoryDomain domain, bool bigEndian)
109-
{
110-
_previous = GetUint(Address, domain, bigEndian);
111-
}
112-
113-
public bool IsValid(MemoryDomain domain)
114-
{
115-
return IsValid(Address, domain);
116-
}
117-
118-
public static bool IsValid(long address, MemoryDomain domain)
119-
{
120-
return address < (domain.Size - 3);
121-
}
122-
123-
public static uint GetUint(long address, MemoryDomain domain, bool bigEndian)
124-
{
125-
if (!IsValid(address, domain))
126-
{
127-
return 0;
128-
}
129-
130-
return domain.PeekUint(address, bigEndian);
131-
}
78+
protected override bool IsValid(long address, MemoryDomain domain)
79+
=> 0L <= address && address <= domain.Size - sizeof(uint);
13280
}
13381
}

src/BizHawk.Client.Common/tools/RamSearchEngine/IMiniWatchDetails.cs

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,34 +20,30 @@ internal sealed class MiniByteWatchDetailed : MiniByteWatch, IMiniWatchDetails
2020
private byte _current;
2121

2222
public MiniByteWatchDetailed(MemoryDomain domain, long addr) : base(domain, addr)
23-
{
24-
_previous = _current = GetByte(Address, domain);
25-
}
23+
=> Previous = _current = unchecked((byte) GetValueInner(Address, domain, bigEndian: default));
2624

2725
public override void SetPreviousToCurrent(MemoryDomain domain, bool bigEndian)
28-
{
29-
_previous = _current;
30-
}
26+
=> Previous = _current;
3127

3228
public uint Current => _current;
3329

3430
public int ChangeCount { get; private set; }
3531

3632
public void Update(PreviousType type, MemoryDomain domain, bool bigEndian)
3733
{
38-
var newValue = GetByte(Address, domain);
34+
var newValue = unchecked((byte) GetValueInner(Address, domain, bigEndian: default));
3935
if (newValue != _current)
4036
{
4137
ChangeCount++;
4238
if (type is PreviousType.LastChange)
4339
{
44-
_previous = _current;
40+
Previous = _current;
4541
}
4642
}
4743

4844
if (type is PreviousType.LastFrame)
4945
{
50-
_previous = _current;
46+
Previous = _current;
5147
}
5248

5349
_current = newValue;
@@ -61,34 +57,30 @@ internal sealed class MiniWordWatchDetailed : MiniWordWatch, IMiniWatchDetails
6157
private ushort _current;
6258

6359
public MiniWordWatchDetailed(MemoryDomain domain, long addr, bool bigEndian) : base(domain, addr, bigEndian)
64-
{
65-
_previous = _current = GetUshort(Address, domain, bigEndian);
66-
}
60+
=> Previous = _current = unchecked((ushort) GetValueInner(Address, domain, bigEndian: bigEndian));
6761

6862
public override void SetPreviousToCurrent(MemoryDomain domain, bool bigEndian)
69-
{
70-
_previous = _current;
71-
}
63+
=> Previous = _current;
7264

7365
public uint Current => _current;
7466

7567
public int ChangeCount { get; private set; }
7668

7769
public void Update(PreviousType type, MemoryDomain domain, bool bigEndian)
7870
{
79-
var newValue = GetUshort(Address, domain, bigEndian);
71+
var newValue = unchecked((ushort) GetValueInner(Address, domain, bigEndian: bigEndian));
8072
if (newValue != _current)
8173
{
8274
ChangeCount++;
8375
if (type is PreviousType.LastChange)
8476
{
85-
_previous = _current;
77+
Previous = _current;
8678
}
8779
}
8880

8981
if (type is PreviousType.LastFrame)
9082
{
91-
_previous = _current;
83+
Previous = _current;
9284
}
9385

9486
_current = newValue;
@@ -102,34 +94,30 @@ internal sealed class MiniDWordWatchDetailed : MiniDWordWatch, IMiniWatchDetails
10294
private uint _current;
10395

10496
public MiniDWordWatchDetailed(MemoryDomain domain, long addr, bool bigEndian) : base(domain, addr, bigEndian)
105-
{
106-
_previous = _current = GetUint(Address, domain, bigEndian);
107-
}
97+
=> Previous = _current = GetValueInner(Address, domain, bigEndian: bigEndian);
10898

10999
public override void SetPreviousToCurrent(MemoryDomain domain, bool bigEndian)
110-
{
111-
_previous = _current;
112-
}
100+
=> Previous = _current;
113101

114102
public uint Current => _current;
115103

116104
public int ChangeCount { get; private set; }
117105

118106
public void Update(PreviousType type, MemoryDomain domain, bool bigEndian)
119107
{
120-
var newValue = GetUint(Address, domain, bigEndian);
108+
var newValue = GetValueInner(Address, domain, bigEndian: bigEndian);
121109
if (newValue != _current)
122110
{
123111
ChangeCount++;
124112
if (type is PreviousType.LastChange)
125113
{
126-
_previous = _current;
114+
Previous = _current;
127115
}
128116
}
129117

130118
if (type is PreviousType.LastFrame)
131119
{
132-
_previous = _current;
120+
Previous = _current;
133121
}
134122

135123
_current = newValue;

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -655,14 +655,7 @@ private uint GetValue(IMiniWatch watch)
655655
{
656656
return detailedWatch.Current;
657657
}
658-
659-
return _settings.Size switch
660-
{
661-
WatchSize.Byte => MiniByteWatch.GetByte(watch.Address, Domain),
662-
WatchSize.Word => MiniWordWatch.GetUshort(watch.Address, Domain, _settings.BigEndian),
663-
WatchSize.DWord => MiniDWordWatch.GetUint(watch.Address, Domain, _settings.BigEndian),
664-
_ => MiniByteWatch.GetByte(watch.Address, Domain),
665-
};
658+
return watch.GetValue(Domain, bigEndian: _settings.BigEndian);
666659
}
667660

668661
private bool CanDoCompareType(Compare compareType)

0 commit comments

Comments
 (0)