Comments on ByteWatch

Also moved nullable value type to non nullable (with exception
prevention).
This commit is contained in:
Hathor86 2015-11-29 17:13:32 +01:00
parent 01639c3e10
commit e7de250fb2
8 changed files with 259 additions and 127 deletions

View File

@ -1,13 +1,17 @@
using BizHawk.Common.NumberExtensions; using System;
using BizHawk.Common.StringExtensions;
using BizHawk.Emulation.Common;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.Linq; using System.Linq;
using BizHawk.Common.NumberExtensions;
using BizHawk.Common.StringExtensions;
using BizHawk.Emulation.Common;
namespace BizHawk.Client.Common namespace BizHawk.Client.Common
{ {
/// <summary>
/// This class holds a byte (8 bits) <see cref="Watch"/>
/// </summary>
public sealed class ByteWatch : Watch public sealed class ByteWatch : Watch
{ {
#region Fields #region Fields
@ -19,6 +23,18 @@ namespace BizHawk.Client.Common
#region cTor(s) #region cTor(s)
/// <summary>
/// Inialize a new instance of <see cref="ByteWatch"/>
/// </summary>
/// <param name="domain"><see cref="MemoryDomain"/> where you want to track</param>
/// <param name="address">The address you want to track</param>
/// <param name="type">How you you want to display the value See <see cref="DisplayType"/></param>
/// <param name="bigEndian">Specify the endianess. true for big endian</param>
/// <param name="note">A custom note about the <see cref="Watch"/></param>
/// <param name="value">Current value</param>
/// <param name="previous">Previous value</param>
/// <param name="changeCount">How many times value has changed</param>
/// <exception cref="ArgumentException">Occurs when a <see cref="DisplayType"/> is incompatible with <see cref="WatchSize.Byte"/></exception>
internal ByteWatch(MemoryDomain domain, long address, DisplayType type, bool bigEndian, string note, byte value, byte previous, int changeCount) internal ByteWatch(MemoryDomain domain, long address, DisplayType type, bool bigEndian, string note, byte value, byte previous, int changeCount)
: base(domain, address, WatchSize.Byte, type, bigEndian, note) : base(domain, address, WatchSize.Byte, type, bigEndian, note)
{ {
@ -27,15 +43,13 @@ namespace BizHawk.Client.Common
this._changecount = changeCount; this._changecount = changeCount;
} }
internal ByteWatch(MemoryDomain domain, long address, DisplayType type, bool bigEndian, string note)
:this(domain, address, type, bigEndian, note, 0, 0, 0)
{
_previous = GetByte();
_value = GetByte();
}
#endregion #endregion
#region Methods
/// <summary>
/// Enumerate wich <see cref="DisplayType"/> are valid for a <see cref="ByteWatch"/>
/// </summary>
public static IEnumerable<DisplayType> ValidTypes public static IEnumerable<DisplayType> ValidTypes
{ {
get get
@ -47,54 +61,59 @@ namespace BizHawk.Client.Common
} }
} }
#region Implements
/// <summary>
/// Get a list a <see cref="DisplayType"/> that can be used for this <see cref="ByteWatch"/>
/// </summary>
/// <returns>An enumartion that contains all valid <see cref="DisplayType"/></returns>
public override IEnumerable<DisplayType> AvailableTypes() public override IEnumerable<DisplayType> AvailableTypes()
{ {
yield return DisplayType.Unsigned; return ValidTypes;
yield return DisplayType.Signed;
yield return DisplayType.Hex;
yield return DisplayType.Binary;
}
public override int? Value
{
get { return GetByte(); }
}
public override int? ValueNoFreeze
{
get { return GetByte(true); }
}
public override string ValueString
{
get { return FormatValue(GetByte()); }
}
public override int? Previous
{
get { return _previous; }
}
public override string PreviousStr
{
get { return FormatValue(_previous); }
} }
/// <summary>
/// Reset the previous value; set it to the current one
/// </summary>
public override void ResetPrevious() public override void ResetPrevious()
{ {
_previous = GetByte(); _previous = GetByte();
} }
public override string ToString() /// <summary>
/// Update the Watch (read it from <see cref="MemoryDomain"/>
/// </summary>
public override void Update()
{ {
return Notes + ": " + ValueString; switch (Global.Config.RamWatchDefinePrevious)
{
case PreviousType.Original:
return;
case PreviousType.LastChange:
var temp = _value;
_value = GetByte();
if (_value != temp)
{
_previous = _value;
_changecount++;
}
break;
case PreviousType.LastFrame:
_previous = _value;
_value = GetByte();
if (_value != Previous)
{
_changecount++;
}
break;
}
} }
public override uint MaxValue #endregion
{
get { return byte.MaxValue; }
}
//TODO: Implements IFormattable
public string FormatValue(byte val) public string FormatValue(byte val)
{ {
switch (Type) switch (Type)
@ -111,6 +130,92 @@ namespace BizHawk.Client.Common
} }
} }
#endregion
public override string ToString()
{
return Notes + ": " + ValueString;
}
#region Properties
#region Implements
/// <summary>
/// Get a string representation of difference
/// between current value and the previous one
/// </summary>
public override string Diff
{
get
{
string diff = string.Empty;
byte diffVal = Convert.ToByte(_value - _previous);
if (diffVal > 0)
{
diff = "+";
}
else if (diffVal < 0)
{
diff = "-";
}
return string.Format("{0}{1}", diff, FormatValue(diffVal));
}
}
/// <summary>
/// Get the maximum possible value
/// </summary>
public override uint MaxValue
{
get
{
return byte.MaxValue;
}
}
/// <summary>
/// Get the current value
/// </summary>
public override int Value
{
get
{
return GetByte();
}
}
/// <summary>
/// Gets the current value
/// but with stuff I don't understand
/// </summary>
/// <remarks>zero 15-nov-2015 - bypass LIAR LOGIC, see fdc9ea2aa922876d20ba897fb76909bf75fa6c92 https://github.com/TASVideos/BizHawk/issues/326 </remarks>
public override int ValueNoFreeze
{
get
{
return GetByte(true);
}
}
/// <summary>
/// Get a string representation of the current value
/// </summary>
public override string ValueString
{
get
{
return FormatValue(_value);
}
}
/// <summary>
/// Try to sets the value into the <see cref="MemoryDomain"/>
/// at the current <see cref="Watch"/> address
/// </summary>
/// <param name="value">Value to set</param>
/// <returns>True if value successfully sets; othewise, false</returns>
public override bool Poke(string value) public override bool Poke(string value)
{ {
try try
@ -185,51 +290,30 @@ namespace BizHawk.Client.Common
} }
} }
public override string Diff /// <summary>
/// Get the previous value
/// </summary>
public override int Previous
{ {
get get
{ {
var diff = string.Empty; return _previous;
var diffVal = _value - _previous;
if (diffVal > 0)
{
diff = "+";
}
else if (diffVal < 0)
{
diff = "-";
}
return diff + FormatValue((byte)(_previous - _value));
} }
} }
public override void Update() /// <summary>
/// Get a string representation of the previous value
/// </summary>
public override string PreviousStr
{ {
switch (Global.Config.RamWatchDefinePrevious) get
{ {
case PreviousType.Original: return FormatValue(_previous);
return;
case PreviousType.LastChange:
var temp = _value;
_value = GetByte();
if (_value != temp)
{
_previous = _value;
_changecount++;
}
break;
case PreviousType.LastFrame:
_previous = _value;
_value = GetByte();
if (_value != Previous)
{
_changecount++;
}
break;
} }
} }
#endregion Implements
#endregion
} }
} }

View File

@ -62,17 +62,17 @@ namespace BizHawk.Client.Common
yield return DisplayType.Float; yield return DisplayType.Float;
} }
public override int? Value public override int Value
{ {
get { return (int)GetDWord(); } get { return (int)GetDWord(); }
} }
public override int? ValueNoFreeze public override int ValueNoFreeze
{ {
get { return (int)GetDWord(true); } get { return (int)GetDWord(true); }
} }
public override int? Previous public override int Previous
{ {
get { return (int)_previous; } get { return (int)_previous; }
} }

View File

@ -14,19 +14,19 @@ namespace BizHawk.Client.Common
get { return new SeparatorWatch(); } get { return new SeparatorWatch(); }
} }
public override int? Value public override int Value
{ {
get { return null; } get { return 0; }
} }
public override int? ValueNoFreeze public override int ValueNoFreeze
{ {
get { return null; } get { return 0; }
} }
public override int? Previous public override int Previous
{ {
get { return null; } get { return 0; }
} }
public override string ValueString public override string ValueString

View File

@ -1,13 +1,18 @@
using BizHawk.Common.NumberExtensions; using System;
using BizHawk.Emulation.Common;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.Linq; using System.Linq;
using BizHawk.Common.NumberExtensions;
using BizHawk.Emulation.Common;
namespace BizHawk.Client.Common namespace BizHawk.Client.Common
{ {
/// <summary>
/// This class holds a watch i.e. something inside a <see cref="MemoryDomain"/> identified by an address
/// with a specific size (8, 16 or 32bits).
/// This is an abstract class
/// </summary>
public abstract partial class Watch public abstract partial class Watch
: IEquatable<Watch>, : IEquatable<Watch>,
IEquatable<Cheat>, IEquatable<Cheat>,
@ -311,7 +316,7 @@ namespace BizHawk.Client.Common
public abstract IEnumerable<DisplayType> AvailableTypes(); public abstract IEnumerable<DisplayType> AvailableTypes();
/// <summary> /// <summary>
/// Reset the previous value /// Reset the previous value; set it to the current one
/// </summary> /// </summary>
public abstract void ResetPrevious(); public abstract void ResetPrevious();
@ -329,7 +334,7 @@ namespace BizHawk.Client.Common
if (!bypassFreeze && Global.CheatList.IsActive(_domain, _address)) if (!bypassFreeze && Global.CheatList.IsActive(_domain, _address))
{ {
//LIAR logic //LIAR logic
return Global.CheatList.GetByteValue(_domain, _address).Value; return Global.CheatList.GetByteValue(_domain, _address) ?? 0;
} }
else else
{ {
@ -349,7 +354,7 @@ namespace BizHawk.Client.Common
if (!bypassFreeze && Global.CheatList.IsActive(_domain, _address)) if (!bypassFreeze && Global.CheatList.IsActive(_domain, _address))
{ {
//LIAR logic //LIAR logic
return (ushort)Global.CheatList.GetCheatValue(_domain, _address, WatchSize.Word).Value; return (ushort)(Global.CheatList.GetCheatValue(_domain, _address, WatchSize.Word) ?? 0);
} }
else else
{ {
@ -369,7 +374,7 @@ namespace BizHawk.Client.Common
if (!bypassFreeze && Global.CheatList.IsActive(_domain, _address)) if (!bypassFreeze && Global.CheatList.IsActive(_domain, _address))
{ {
//LIAR logic //LIAR logic
return (uint)Global.CheatList.GetCheatValue(_domain, _address, WatchSize.DWord).Value; return (uint)(Global.CheatList.GetCheatValue(_domain, _address, WatchSize.DWord) ?? 0);
} }
else else
{ {
@ -424,7 +429,8 @@ namespace BizHawk.Client.Common
/// <returns>True if both object are equals; otherwise, false</returns> /// <returns>True if both object are equals; otherwise, false</returns>
public bool Equals(Watch other) public bool Equals(Watch other)
{ {
return this._domain == other._domain && return !object.ReferenceEquals(other, null) &&
this._domain == other._domain &&
this._address == other._address && this._address == other._address &&
this._size == other._size; this._size == other._size;
} }
@ -440,7 +446,8 @@ namespace BizHawk.Client.Common
/// <returns>True if both object are equals; otherwise, false</returns> /// <returns>True if both object are equals; otherwise, false</returns>
public bool Equals(Cheat other) public bool Equals(Cheat other)
{ {
return this._domain == other.Domain && return !object.ReferenceEquals(other, null) &&
this._domain == other.Domain &&
this._address == other.Address && this._address == other.Address &&
this._size == other.Size; this._size == other.Size;
} }
@ -467,7 +474,11 @@ namespace BizHawk.Client.Common
{ {
return 0; return 0;
} }
else if (_address.Equals(other._address)) else if (object.ReferenceEquals(other, null))
{
return 1;
}
else if (_address.Equals(other._address))
{ {
return ((int)_size).CompareTo((int)other._size); return ((int)_size).CompareTo((int)other._size);
} }
@ -543,14 +554,50 @@ namespace BizHawk.Client.Common
#region Abstracts #region Abstracts
/// <summary>
/// Get a string representation of difference
/// between current value and the previous one
/// </summary>
public abstract string Diff { get; } public abstract string Diff { get; }
/// <summary>
/// Get the maximum possible value
/// </summary>
public abstract uint MaxValue { get; } public abstract uint MaxValue { get; }
public abstract int? Value { get; }
//zero 15-nov-2015 - bypass LIAR LOGIC, see fdc9ea2aa922876d20ba897fb76909bf75fa6c92 https://github.com/TASVideos/BizHawk/issues/326 /// <summary>
public abstract int? ValueNoFreeze { get; } /// Get the current value
/// </summary>
public abstract int Value { get; }
/// <summary>
/// Gets the current value
/// but with stuff I don't understand
/// </summary>
/// <remarks>zero 15-nov-2015 - bypass LIAR LOGIC, see fdc9ea2aa922876d20ba897fb76909bf75fa6c92 https://github.com/TASVideos/BizHawk/issues/326 </remarks>
public abstract int ValueNoFreeze { get; }
/// <summary>
/// Get a string representation of the current value
/// </summary>
public abstract string ValueString { get; } public abstract string ValueString { get; }
/// <summary>
/// Try to sets the value into the <see cref="MemoryDomain"/>
/// at the current <see cref="Watch"/> address
/// </summary>
/// <param name="value">Value to set</param>
/// <returns>True if value successfully sets; othewise, false</returns>
public abstract bool Poke(string value); public abstract bool Poke(string value);
public abstract int? Previous { get; }
/// <summary>
/// Get the previous value
/// </summary>
public abstract int Previous { get; }
/// <summary>
/// Get a string representation of the previous value
/// </summary>
public abstract string PreviousStr { get; } public abstract string PreviousStr { get; }
#endregion Abstracts #endregion Abstracts
@ -728,6 +775,7 @@ namespace BizHawk.Client.Common
#endregion #endregion
//TODO: Replace all the following stuff by implementing ISerializable
public static string DisplayTypeToString(DisplayType type) public static string DisplayTypeToString(DisplayType type)
{ {
switch (type) switch (type)

View File

@ -118,7 +118,7 @@ namespace BizHawk.Client.Common
if (reverse) if (reverse)
{ {
_watchList = _watchList _watchList = _watchList
.OrderByDescending(x => x.Address ?? 0) .OrderByDescending(x => x.Address)
.ThenBy(x => x.Domain.Name) .ThenBy(x => x.Domain.Name)
.ThenBy(x => x.Size) .ThenBy(x => x.Size)
.ThenBy(x => x.Type) .ThenBy(x => x.Type)
@ -128,7 +128,7 @@ namespace BizHawk.Client.Common
else else
{ {
_watchList = _watchList _watchList = _watchList
.OrderBy(x => x.Address ?? 0) .OrderBy(x => x.Address)
.ThenBy(x => x.Domain.Name) .ThenBy(x => x.Domain.Name)
.ThenBy(x => x.Size) .ThenBy(x => x.Size)
.ThenBy(x => x.Type) .ThenBy(x => x.Type)
@ -141,8 +141,8 @@ namespace BizHawk.Client.Common
if (reverse) if (reverse)
{ {
_watchList = _watchList _watchList = _watchList
.OrderByDescending(x => x.Value ?? 0) .OrderByDescending(x => x.Value)
.ThenBy(x => x.Address ?? 0) .ThenBy(x => x.Address)
.ThenBy(x => x.Size) .ThenBy(x => x.Size)
.ThenBy(x => x.Type) .ThenBy(x => x.Type)
.ThenBy(x => x.BigEndian) .ThenBy(x => x.BigEndian)
@ -151,8 +151,8 @@ namespace BizHawk.Client.Common
else else
{ {
_watchList = _watchList _watchList = _watchList
.OrderBy(x => x.Value ?? 0) .OrderBy(x => x.Value)
.ThenBy(x => x.Address ?? 0) .ThenBy(x => x.Address)
.ThenBy(x => x.Size) .ThenBy(x => x.Size)
.ThenBy(x => x.Type) .ThenBy(x => x.Type)
.ThenBy(x => x.BigEndian) .ThenBy(x => x.BigEndian)

View File

@ -62,17 +62,17 @@ namespace BizHawk.Client.Common
get { return ushort.MaxValue; } get { return ushort.MaxValue; }
} }
public override int? Value public override int Value
{ {
get { return GetWord(); } get { return GetWord(); }
} }
public override int? ValueNoFreeze public override int ValueNoFreeze
{ {
get { return GetWord(true); } get { return GetWord(true); }
} }
public override int? Previous public override int Previous
{ {
get { return _previous; } get { return _previous; }
} }

View File

@ -234,7 +234,7 @@ namespace BizHawk.Client.EmuHawk.ToolExtensions
Global.CheatList.AddRange( Global.CheatList.AddRange(
watches watches
.Where(w => !w.IsSeparator) .Where(w => !w.IsSeparator)
.Select(w => new Cheat(w, w.Value ?? 0))); .Select(w => new Cheat(w, w.Value)));
} }
public static void UnfreezeAll(this IEnumerable<Watch> watches) public static void UnfreezeAll(this IEnumerable<Watch> watches)

View File

@ -762,7 +762,7 @@ namespace BizHawk.Client.EmuHawk
Global.CheatList.Add(new Cheat( Global.CheatList.Add(new Cheat(
watch, watch,
watch.Value ?? 0)); watch.Value));
} }
} }
@ -780,7 +780,7 @@ namespace BizHawk.Client.EmuHawk
cheats.Add(new Cheat( cheats.Add(new Cheat(
watch, watch,
watch.Value ?? 0)); watch.Value));
} }
Global.CheatList.AddRange(cheats); Global.CheatList.AddRange(cheats);