Some code cleanup tinkerings in Client.Common

This commit is contained in:
adelikat 2014-02-03 20:48:01 +00:00
parent 3dfcd8b22a
commit 23f7017651
20 changed files with 247 additions and 298 deletions

View File

@ -1,13 +1,13 @@
using System; using System.Linq;
using System.Linq;
namespace BizHawk.Client.Common namespace BizHawk.Client.Common
{ {
// TODO: these classes are worthless or need to be extensions, decide which
public static class StringHelpers public static class StringHelpers
{ {
public static int HowMany(string str, char c) public static int HowMany(string str, char c)
{ {
return !String.IsNullOrEmpty(str) ? str.Count(t => t == c) : 0; return !string.IsNullOrEmpty(str) ? str.Count(t => t == c) : 0;
} }
public static int HowMany(string str, string s) public static int HowMany(string str, string s)
@ -28,27 +28,27 @@ namespace BizHawk.Client.Common
// TODO: put it in its own file // TODO: put it in its own file
public static class IntHelpers // TODO: a less lame name public static class IntHelpers // TODO: a less lame name
{ {
public static int GetNumDigits(Int32 i) public static int GetNumDigits(int i)
{ {
if (i < 0x100) if (i < 0x100)
{ {
return 2; return 2;
} }
else if (i < 0x10000)
if (i < 0x10000)
{ {
return 4; return 4;
} }
else if (i < 0x1000000)
if (i < 0x1000000)
{ {
return 6; return 6;
} }
else
{ return 8;
return 8;
}
} }
public static uint MaxHexValueFromMaxDigits(Int32 i) public static uint MaxHexValueFromMaxDigits(int i)
{ {
switch (i) switch (i)
{ {
@ -74,5 +74,5 @@ namespace BizHawk.Client.Common
return int.MaxValue; return int.MaxValue;
} }
} }
} }

View File

@ -111,7 +111,7 @@ namespace BizHawk.Client.Common
"byteswap_16", "byteswap_16",
"Byte swaps 'short', i.e. bit.byteswap_16(0xFF00) would return 0x00FF" "Byte swaps 'short', i.e. bit.byteswap_16(0xFF00) would return 0x00FF"
)] )]
public static ushort Byteswap_16(ushort val) public static ushort Byteswap16(ushort val)
{ {
return (ushort)((val & 0xFFU) << 8 | (val & 0xFF00U) >> 8); return (ushort)((val & 0xFFU) << 8 | (val & 0xFF00U) >> 8);
} }
@ -120,7 +120,7 @@ namespace BizHawk.Client.Common
"byteswap_32", "byteswap_32",
"Byte swaps 'dword'" "Byte swaps 'dword'"
)] )]
public static uint Byteswap_32(uint val) public static uint Byteswap32(uint val)
{ {
return (val & 0x000000FFU) << 24 | (val & 0x0000FF00U) << 8 | return (val & 0x000000FFU) << 24 | (val & 0x0000FF00U) << 8 |
(val & 0x00FF0000U) >> 8 | (val & 0xFF000000U) >> 24; (val & 0x00FF0000U) >> 8 | (val & 0xFF000000U) >> 24;
@ -130,12 +130,12 @@ namespace BizHawk.Client.Common
"byteswap_64", "byteswap_64",
"Byte swaps 'long'" "Byte swaps 'long'"
)] )]
public static UInt64 Byteswap_64(ulong val) public static UInt64 Byteswap64(ulong val)
{ {
return (val & 0x00000000000000FFUL) << 56 | (val & 0x000000000000FF00UL) << 40 | return (val & 0x00000000000000FFUL) << 56 | (val & 0x000000000000FF00UL) << 40 |
(val & 0x0000000000FF0000UL) << 24 | (val & 0x00000000FF000000UL) << 8 | (val & 0x0000000000FF0000UL) << 24 | (val & 0x00000000FF000000UL) << 8 |
(val & 0x000000FF00000000UL) >> 8 | (val & 0x0000FF0000000000UL) >> 24 | (val & 0x000000FF00000000UL) >> 8 | (val & 0x0000FF0000000000UL) >> 24 |
(val & 0x00FF000000000000UL) >> 40 | (val & 0xFF00000000000000UL) >> 56; (val & 0x00FF000000000000UL) >> 40 | (val & 0xFF00000000000000UL) >> 56;
} }
} }
} }

View File

@ -13,6 +13,10 @@ namespace BizHawk.Client.Common
public class EmulatorLuaLibrary : LuaLibraryBase public class EmulatorLuaLibrary : LuaLibraryBase
{ {
private readonly Lua _lua;
private readonly Action _frameAdvanceCallback;
private readonly Action _yieldCallback;
public EmulatorLuaLibrary(Lua lua, Action frameAdvanceCallback, Action yieldCallback) public EmulatorLuaLibrary(Lua lua, Action frameAdvanceCallback, Action yieldCallback)
{ {
_lua = lua; _lua = lua;
@ -22,10 +26,6 @@ namespace BizHawk.Client.Common
public override string Name { get { return "emu"; } } public override string Name { get { return "emu"; } }
private readonly Lua _lua;
private readonly Action _frameAdvanceCallback;
private readonly Action _yieldCallback;
private static void SetrenderplanesDo(IList<object> luaParam) private static void SetrenderplanesDo(IList<object> luaParam)
{ {
if (Global.Emulator is NES) if (Global.Emulator is NES)

View File

@ -7,6 +7,8 @@ namespace BizHawk.Client.Common
{ {
public class EventLuaLibrary : LuaLibraryBase public class EventLuaLibrary : LuaLibraryBase
{ {
private readonly LuaFunctionList _luaFunctions = new LuaFunctionList();
public EventLuaLibrary(Action<string> logOutputCallback) public EventLuaLibrary(Action<string> logOutputCallback)
{ {
LogOutputCallback = logOutputCallback; LogOutputCallback = logOutputCallback;
@ -19,8 +21,6 @@ namespace BizHawk.Client.Common
#region Events Library Helpers #region Events Library Helpers
private readonly LuaFunctionList _luaFunctions = new LuaFunctionList();
public LuaFunctionList RegisteredFunctions { get { return _luaFunctions; } } public LuaFunctionList RegisteredFunctions { get { return _luaFunctions; } }
public void CallSaveStateEvent(string name) public void CallSaveStateEvent(string name)

View File

@ -7,6 +7,8 @@ namespace BizHawk.Client.Common
// TODO: this needs a major refactor, as well as MemoryLuaLibrary, and this shoudl inherit memorylua library and extend it // TODO: this needs a major refactor, as well as MemoryLuaLibrary, and this shoudl inherit memorylua library and extend it
public class MainMemoryLuaLibrary : LuaLibraryBase public class MainMemoryLuaLibrary : LuaLibraryBase
{ {
private readonly Lua _lua;
public MainMemoryLuaLibrary(Lua lua) public MainMemoryLuaLibrary(Lua lua)
{ {
_lua = lua; _lua = lua;
@ -14,8 +16,6 @@ namespace BizHawk.Client.Common
public override string Name { get { return "mainmemory"; } } public override string Name { get { return "mainmemory"; } }
private readonly Lua _lua;
#region Main Memory Library Helpers #region Main Memory Library Helpers
private static int U2S(uint u, int size) private static int U2S(uint u, int size)
@ -123,9 +123,9 @@ namespace BizHawk.Client.Common
var table = _lua.NewTable(); var table = _lua.NewTable();
for (var i = addr; i <= lastAddr; i++) for (var i = addr; i <= lastAddr; i++)
{ {
var a = String.Format("{0:X2}", i); var a = string.Format("{0:X2}", i);
var v = Global.Emulator.MemoryDomains.MainMemory.PeekByte(i); var v = Global.Emulator.MemoryDomains.MainMemory.PeekByte(i);
var vs = String.Format("{0:X2}", (int)v); var vs = string.Format("{0:X2}", (int)v);
table[a] = vs; table[a] = vs;
} }
@ -162,8 +162,7 @@ namespace BizHawk.Client.Common
{ {
Global.Emulator.MemoryDomains.MainMemory.PokeByte( Global.Emulator.MemoryDomains.MainMemory.PokeByte(
LuaInt(address), LuaInt(address),
(byte)LuaInt(memoryblock[address]) (byte)LuaInt(memoryblock[address]));
);
} }
} }

View File

@ -5,10 +5,10 @@ namespace BizHawk.Client.Common
{ {
public class MemoryLuaLibrary : LuaLibraryBase public class MemoryLuaLibrary : LuaLibraryBase
{ {
public override string Name { get { return "memory"; } }
private int _currentMemoryDomain; // Main memory by default private int _currentMemoryDomain; // Main memory by default
public override string Name { get { return "memory"; } }
#region Memory Library Helpers #region Memory Library Helpers
private static int U2S(uint u, int size) private static int U2S(uint u, int size)
@ -95,7 +95,7 @@ namespace BizHawk.Client.Common
)] )]
public string GetMemoryDomainList() public string GetMemoryDomainList()
{ {
return Global.Emulator.MemoryDomains.Aggregate(String.Empty, (current, t) => current + (t.Name + '\n')); return Global.Emulator.MemoryDomains.Aggregate(string.Empty, (current, t) => current + (t.Name + '\n'));
} }
[LuaMethodAttributes( [LuaMethodAttributes(

View File

@ -4,6 +4,8 @@ namespace BizHawk.Client.Common
{ {
public class MovieLuaLibrary : LuaLibraryBase public class MovieLuaLibrary : LuaLibraryBase
{ {
private readonly Lua _lua;
public MovieLuaLibrary(Lua lua) public MovieLuaLibrary(Lua lua)
{ {
_lua = lua; _lua = lua;
@ -11,8 +13,6 @@ namespace BizHawk.Client.Common
public override string Name { get { return "movie"; } } public override string Name { get { return "movie"; } }
private readonly Lua _lua;
[LuaMethodAttributes( [LuaMethodAttributes(
"filename", "filename",
"Returns the file name including path of the currently loaded movie" "Returns the file name including path of the currently loaded movie"
@ -32,8 +32,7 @@ namespace BizHawk.Client.Common
var m = new MovieControllerAdapter { Type = Global.MovieSession.MovieControllerAdapter.Type }; var m = new MovieControllerAdapter { Type = Global.MovieSession.MovieControllerAdapter.Type };
m.SetControllersAsMnemonic( m.SetControllersAsMnemonic(
Global.MovieSession.Movie.GetInput(frame) Global.MovieSession.Movie.GetInput(frame));
);
foreach (var button in m.Type.BoolButtons) foreach (var button in m.Type.BoolButtons)
{ {
@ -89,18 +88,18 @@ namespace BizHawk.Client.Common
{ {
return "FINISHED"; return "FINISHED";
} }
else if (Global.MovieSession.Movie.IsPlaying)
if (Global.MovieSession.Movie.IsPlaying)
{ {
return "PLAY"; return "PLAY";
} }
else if (Global.MovieSession.Movie.IsRecording)
if (Global.MovieSession.Movie.IsRecording)
{ {
return "RECORD"; return "RECORD";
} }
else
{ return "INACTIVE";
return "INACTIVE";
}
} }
[LuaMethodAttributes( [LuaMethodAttributes(

View File

@ -3,7 +3,7 @@ using BizHawk.Emulation.Cores.Nintendo.NES;
namespace BizHawk.Client.Common namespace BizHawk.Client.Common
{ {
public class NESLuaLibrary : LuaLibraryBase public class NesLuaLibrary : LuaLibraryBase
{ {
// TODO: // TODO:
// perhaps with the new core config system, one could // perhaps with the new core config system, one could
@ -26,13 +26,12 @@ namespace BizHawk.Client.Common
Watch.WatchSize.Byte, Watch.WatchSize.Byte,
Watch.DisplayType.Hex, Watch.DisplayType.Hex,
code, code,
false false);
);
Global.CheatList.Add(new Cheat( Global.CheatList.Add(new Cheat(
watch, watch,
decoder.Value, decoder.Value,
decoder.Compare decoder.Compare));
));
} }
} }
@ -55,10 +54,8 @@ namespace BizHawk.Client.Common
{ {
return ((NES.NESSettings)Global.Emulator.GetSettings()).PAL_BottomLine; return ((NES.NESSettings)Global.Emulator.GetSettings()).PAL_BottomLine;
} }
else
{ return ((NES.NESSettings)Global.Emulator.GetSettings()).NTSC_BottomLine;
return ((NES.NESSettings)Global.Emulator.GetSettings()).NTSC_BottomLine;
}
} }
[LuaMethodAttributes( [LuaMethodAttributes(
@ -98,10 +95,8 @@ namespace BizHawk.Client.Common
{ {
return ((NES.NESSettings)Global.Emulator.GetSettings()).PAL_TopLine; return ((NES.NESSettings)Global.Emulator.GetSettings()).PAL_TopLine;
} }
else
{ return ((NES.NESSettings)Global.Emulator.GetSettings()).NTSC_TopLine;
return ((NES.NESSettings)Global.Emulator.GetSettings()).NTSC_TopLine;
}
} }
[LuaMethodAttributes( [LuaMethodAttributes(
@ -114,8 +109,7 @@ namespace BizHawk.Client.Common
{ {
var decoder = new NESGameGenieDecoder(code); var decoder = new NESGameGenieDecoder(code);
Global.CheatList.RemoveRange( Global.CheatList.RemoveRange(
Global.CheatList.Where(x => x.Address == decoder.Address) Global.CheatList.Where(x => x.Address == decoder.Address));
);
} }
} }

View File

@ -2,7 +2,7 @@
namespace BizHawk.Client.Common namespace BizHawk.Client.Common
{ {
public class SNESLuaLibrary : LuaLibraryBase public class SnesLuaLibrary : LuaLibraryBase
{ {
public override string Name { get { return "snes"; } } public override string Name { get { return "snes"; } }

View File

@ -11,7 +11,7 @@ namespace BizHawk.Client.Common
)] )]
public static string Hex(long num) public static string Hex(long num)
{ {
var hex = String.Format("{0:X}", num); var hex = string.Format("{0:X}", num);
if (hex.Length == 1) if (hex.Length == 1)
{ {
hex = "0" + hex; hex = "0" + hex;

View File

@ -1,5 +1,4 @@
using System; using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using System.Text; using System.Text;
@ -13,18 +12,17 @@ namespace BizHawk.Client.Common
public class LuaDocumentation : ILuaDocumentation public class LuaDocumentation : ILuaDocumentation
{ {
public List<LibraryFunction> FunctionList { get; set; }
public LuaDocumentation() public LuaDocumentation()
{ {
FunctionList = new List<LibraryFunction>(); FunctionList = new List<LibraryFunction>();
} }
public List<LibraryFunction> FunctionList { get; set; }
public void Add(string methodLib, string methodName, MethodInfo method, string description) public void Add(string methodLib, string methodName, MethodInfo method, string description)
{ {
FunctionList.Add( FunctionList.Add(
new LibraryFunction(methodLib, methodName, method, description) new LibraryFunction(methodLib, methodName, method, description));
);
} }
public void Clear() public void Clear()
@ -51,6 +49,8 @@ namespace BizHawk.Client.Common
public class LibraryFunction public class LibraryFunction
{ {
private readonly string _returnType = string.Empty;
public LibraryFunction(string methodLib, string methodName, MethodInfo method, string description) public LibraryFunction(string methodLib, string methodName, MethodInfo method, string description)
{ {
Library = methodLib; Library = methodLib;
@ -71,7 +71,6 @@ namespace BizHawk.Client.Common
public string Library { get; set; } public string Library { get; set; }
public string Name { get; set; } public string Name { get; set; }
public List<string> Parameters { get; set; } public List<string> Parameters { get; set; }
private readonly string _returnType = string.Empty;
public string Description { get; set; } public string Description { get; set; }

View File

@ -5,18 +5,9 @@ namespace BizHawk.Client.Common
{ {
public class LuaFile public class LuaFile
{ {
public string Name { get; set; }
public string Path { get; set; }
public bool Enabled { get; set; }
public bool Paused { get; set; }
public bool IsSeparator { get; set; }
public LuaInterface.Lua Thread { get; set; }
public bool FrameWaiting { get; set; }
public string CurrentDirectory { get; set; }
public LuaFile(string path) public LuaFile(string path)
{ {
Name = String.Empty; Name = string.Empty;
Path = path; Path = path;
Enabled = true; Enabled = true;
Paused = false; Paused = false;
@ -30,18 +21,18 @@ namespace BizHawk.Client.Common
IsSeparator = false; IsSeparator = false;
// the current directory for the lua task will start off wherever the lua file is located // the current directory for the lua task will start off wherever the lua file is located
var directory_info = new FileInfo(path).Directory; var directoryInfo = new FileInfo(path).Directory;
if (directory_info != null) if (directoryInfo != null)
{ {
CurrentDirectory = directory_info.FullName; CurrentDirectory = directoryInfo.FullName;
} }
} }
public LuaFile(bool isSeparator) public LuaFile(bool isSeparator)
{ {
IsSeparator = isSeparator; IsSeparator = isSeparator;
Name = String.Empty; Name = string.Empty;
Path = String.Empty; Path = string.Empty;
Enabled = false; Enabled = false;
} }
@ -55,6 +46,15 @@ namespace BizHawk.Client.Common
CurrentDirectory = file.CurrentDirectory; CurrentDirectory = file.CurrentDirectory;
} }
public string Name { get; set; }
public string Path { get; set; }
public bool Enabled { get; set; }
public bool Paused { get; set; }
public bool IsSeparator { get; set; }
public LuaInterface.Lua Thread { get; set; }
public bool FrameWaiting { get; set; }
public string CurrentDirectory { get; set; }
public static LuaFile SeparatorInstance public static LuaFile SeparatorInstance
{ {
get { return new LuaFile(true); } get { return new LuaFile(true); }

View File

@ -7,19 +7,12 @@ namespace BizHawk.Client.Common
{ {
public class LuaFileList : List<LuaFile> public class LuaFileList : List<LuaFile>
{ {
public LuaFileList() { } private string _filename = string.Empty;
private string _filename = String.Empty;
private bool _changes; private bool _changes;
public Action ChangedCallback { get; set; } public Action ChangedCallback { get; set; }
public Action LoadCallback { get; set; } public Action LoadCallback { get; set; }
public void StopAllScripts()
{
ForEach(x => x.Enabled = false);
}
public bool Changes public bool Changes
{ {
get get
@ -46,14 +39,19 @@ namespace BizHawk.Client.Common
set set
{ {
_filename = value ?? String.Empty; _filename = value ?? string.Empty;
} }
} }
public void StopAllScripts()
{
ForEach(x => x.Enabled = false);
}
public new void Clear() public new void Clear()
{ {
StopAllScripts(); StopAllScripts();
_filename = String.Empty; _filename = string.Empty;
Changes = false; Changes = false;
base.Clear(); base.Clear();
} }
@ -118,15 +116,13 @@ namespace BizHawk.Client.Common
return true; return true;
} }
else
{ return false;
return false;
}
} }
public void SaveSession() public void SaveSession()
{ {
if (!String.IsNullOrWhiteSpace(_filename)) if (!string.IsNullOrWhiteSpace(_filename))
{ {
SaveSession(_filename); SaveSession(_filename);
} }

View File

@ -59,9 +59,9 @@ namespace BizHawk.Client.Common
} }
} }
var lua_result = new object[n - trim]; var luaResult = new object[n - trim];
Array.Copy(luaArgs, lua_result, n - trim); Array.Copy(luaArgs, luaResult, n - trim);
return lua_result; return luaResult;
} }
} }
} }

View File

@ -30,19 +30,13 @@ namespace BizHawk.Client.Common
"error running function attached by the event " + "error running function attached by the event " +
_event + _event +
"\nError message: " + "\nError message: " +
ex.Message ex.Message);
);
} }
}; };
} }
public Guid Guid { get; private set; } public Guid Guid { get; private set; }
public void Call(string name = null)
{
_function.Call(name);
}
public string Name public string Name
{ {
get { return _name; } get { return _name; }
@ -59,5 +53,10 @@ namespace BizHawk.Client.Common
{ {
get { return _action; } get { return _action; }
} }
public void Call(string name = null)
{
_function.Call(name);
}
} }
} }

View File

@ -5,7 +5,10 @@ namespace BizHawk.Client.Common
{ {
public class Cheat public class Cheat
{ {
#region Constructors private readonly Watch _watch;
private int? _compare;
private int _val;
private bool _enabled;
public Cheat(Watch watch, int value, int? compare = null, bool enabled = true) public Cheat(Watch watch, int value, int? compare = null, bool enabled = true)
{ {
@ -43,33 +46,13 @@ namespace BizHawk.Client.Common
} }
public delegate void CheatEventHandler(object sender); public delegate void CheatEventHandler(object sender);
public event CheatEventHandler Changed;
public static Cheat Separator public static Cheat Separator
{ {
get { return new Cheat(SeparatorWatch.Instance, 0, null, false); } get { return new Cheat(SeparatorWatch.Instance, 0, null, false); }
} }
#endregion
#region private parts
private readonly Watch _watch;
private int? _compare;
private int _val;
private bool _enabled;
private void Changes()
{
if (Changed != null)
{
Changed(this);
}
}
#endregion
#region Properties
public bool IsSeparator public bool IsSeparator
{ {
get { return _watch.IsSeparator; } get { return _watch.IsSeparator; }
@ -127,7 +110,7 @@ namespace BizHawk.Client.Common
public string Name public string Name
{ {
get { return IsSeparator ? String.Empty : _watch.Notes; } get { return IsSeparator ? string.Empty : _watch.Notes; }
} }
public string AddressStr public string AddressStr
@ -143,7 +126,7 @@ namespace BizHawk.Client.Common
{ {
default: default:
case Watch.WatchSize.Separator: case Watch.WatchSize.Separator:
return String.Empty; return string.Empty;
case Watch.WatchSize.Byte: case Watch.WatchSize.Byte:
return (_watch as ByteWatch).FormatValue((byte)_val); return (_watch as ByteWatch).FormatValue((byte)_val);
case Watch.WatchSize.Word: case Watch.WatchSize.Word:
@ -164,7 +147,7 @@ namespace BizHawk.Client.Common
{ {
default: default:
case Watch.WatchSize.Separator: case Watch.WatchSize.Separator:
return String.Empty; return string.Empty;
case Watch.WatchSize.Byte: case Watch.WatchSize.Byte:
return (_watch as ByteWatch).FormatValue((byte)_compare.Value); return (_watch as ByteWatch).FormatValue((byte)_compare.Value);
case Watch.WatchSize.Word: case Watch.WatchSize.Word:
@ -173,19 +156,11 @@ namespace BizHawk.Client.Common
return (_watch as DWordWatch).FormatValue((uint)_compare.Value); return (_watch as DWordWatch).FormatValue((uint)_compare.Value);
} }
} }
else
{ return string.Empty;
return String.Empty;
}
} }
} }
public event CheatEventHandler Changed;
#endregion
#region Actions
public void Enable() public void Enable()
{ {
if (!IsSeparator) if (!IsSeparator)
@ -285,6 +260,13 @@ namespace BizHawk.Client.Common
} }
} }
#endregion private void Changes()
{
if (Changed != null)
{
Changed(this);
}
}
} }
} }

View File

@ -11,7 +11,29 @@ namespace BizHawk.Client.Common
{ {
public class CheatCollection : ICollection<Cheat> public class CheatCollection : ICollection<Cheat>
{ {
private List<Cheat> _cheatList = new List<Cheat>();
private string _currentFileName = string.Empty;
private string _defaultFileName = string.Empty;
private bool _changes; private bool _changes;
public delegate void CheatListEventHandler(object sender, CheatListEventArgs e);
public event CheatListEventHandler Changed;
public int Count
{
get { return _cheatList.Count; }
}
public int CheatCount
{
get { return _cheatList.Count(x => !x.IsSeparator); }
}
public int ActiveCount
{
get { return _cheatList.Count(x => x.Enabled); }
}
public bool Changes public bool Changes
{ {
get get
@ -29,19 +51,12 @@ namespace BizHawk.Client.Common
} }
} }
private List<Cheat> _cheatList = new List<Cheat>(); public string CurrentFileName
private string _currentFileName = String.Empty;
private string _defaultFileName = String.Empty;
public IEnumerator<Cheat> GetEnumerator()
{ {
return _cheatList.GetEnumerator(); get { return _currentFileName; }
} }
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() public bool IsReadOnly { get { return false; } }
{
return GetEnumerator();
}
public Cheat this[int index] public Cheat this[int index]
{ {
@ -56,6 +71,16 @@ namespace BizHawk.Client.Common
} }
} }
public IEnumerator<Cheat> GetEnumerator()
{
return _cheatList.GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public void Pulse() public void Pulse()
{ {
_cheatList.ForEach(cheat => cheat.Pulse()); _cheatList.ForEach(cheat => cheat.Pulse());
@ -64,7 +89,6 @@ namespace BizHawk.Client.Common
/// <summary> /// <summary>
/// Looks for a .cht file that matches the ROM loaded based on the default filename for a given ROM /// Looks for a .cht file that matches the ROM loaded based on the default filename for a given ROM
/// </summary> /// </summary>
/// <returns></returns>
public bool AttemptToLoadCheatFile() public bool AttemptToLoadCheatFile()
{ {
var file = new FileInfo(_defaultFileName); var file = new FileInfo(_defaultFileName);
@ -73,32 +97,15 @@ namespace BizHawk.Client.Common
{ {
return Load(file.FullName, false); return Load(file.FullName, false);
} }
else
{ return false;
return false;
}
}
public int Count
{
get { return _cheatList.Count; }
}
public int CheatCount
{
get { return _cheatList.Count(x => !x.IsSeparator); }
}
public int ActiveCount
{
get { return _cheatList.Count(x => x.Enabled); }
} }
public void NewList(string defaultFileName) public void NewList(string defaultFileName)
{ {
_defaultFileName = defaultFileName; _defaultFileName = defaultFileName;
_cheatList.Clear(); _cheatList.Clear();
_currentFileName = String.Empty; _currentFileName = string.Empty;
Changes = false; Changes = false;
} }
@ -145,10 +152,8 @@ namespace BizHawk.Client.Common
Changes = true; Changes = true;
return true; return true;
} }
else
{ return false;
return false;
}
} }
public bool Remove(Watch w) public bool Remove(Watch w)
@ -160,10 +165,8 @@ namespace BizHawk.Client.Common
Changes = true; Changes = true;
return true; return true;
} }
else
{ return false;
return false;
}
} }
public bool Contains(Cheat cheat) public bool Contains(Cheat cheat)
@ -176,8 +179,6 @@ namespace BizHawk.Client.Common
_cheatList.CopyTo(array, arrayIndex); _cheatList.CopyTo(array, arrayIndex);
} }
public bool IsReadOnly { get { return false; } }
public void RemoveRange(IEnumerable<Cheat> cheats) public void RemoveRange(IEnumerable<Cheat> cheats)
{ {
foreach (var cheat in cheats) foreach (var cheat in cheats)
@ -206,19 +207,11 @@ namespace BizHawk.Client.Common
public bool IsActive(MemoryDomain domain, int address) public bool IsActive(MemoryDomain domain, int address)
{ {
foreach (var cheat in _cheatList) return _cheatList.Any(cheat =>
{ !cheat.IsSeparator &&
if (cheat.IsSeparator) cheat.Enabled &&
{ cheat.Domain == domain
continue; && cheat.Contains(address));
}
else if (cheat.Domain == domain && cheat.Contains(address) && cheat.Enabled)
{
return true;
}
}
return false;
} }
public void SaveOnClose() public void SaveOnClose()
@ -227,14 +220,14 @@ namespace BizHawk.Client.Common
{ {
if (Changes && _cheatList.Any()) if (Changes && _cheatList.Any())
{ {
if (String.IsNullOrWhiteSpace(_currentFileName)) if (string.IsNullOrWhiteSpace(_currentFileName))
{ {
_currentFileName = _defaultFileName; _currentFileName = _defaultFileName;
} }
SaveFile(_currentFileName); SaveFile(_currentFileName);
} }
else if (!_cheatList.Any() && !String.IsNullOrWhiteSpace(_currentFileName)) else if (!_cheatList.Any() && !string.IsNullOrWhiteSpace(_currentFileName))
{ {
new FileInfo(_currentFileName).Delete(); new FileInfo(_currentFileName).Delete();
} }
@ -243,7 +236,7 @@ namespace BizHawk.Client.Common
public bool Save() public bool Save()
{ {
if (String.IsNullOrWhiteSpace(_currentFileName)) if (string.IsNullOrWhiteSpace(_currentFileName))
{ {
_currentFileName = _defaultFileName; _currentFileName = _defaultFileName;
} }
@ -346,8 +339,8 @@ namespace BizHawk.Client.Common
} }
var vals = s.Split('\t'); var vals = s.Split('\t');
var address = Int32.Parse(vals[0], NumberStyles.HexNumber); var address = int.Parse(vals[0], NumberStyles.HexNumber);
var value = Int32.Parse(vals[1], NumberStyles.HexNumber); var value = int.Parse(vals[1], NumberStyles.HexNumber);
if (vals[2] == "N") if (vals[2] == "N")
{ {
@ -355,7 +348,7 @@ namespace BizHawk.Client.Common
} }
else else
{ {
compare = Int32.Parse(vals[2], NumberStyles.HexNumber); compare = int.Parse(vals[2], NumberStyles.HexNumber);
} }
var domain = Global.Emulator.MemoryDomains[vals[3]]; var domain = Global.Emulator.MemoryDomains[vals[3]];
@ -376,8 +369,7 @@ namespace BizHawk.Client.Common
size, size,
type, type,
name, name,
bigendian bigendian);
);
Add(new Cheat(watch, value, compare, !Global.Config.DisableCheatsOnLoad && enabled)); Add(new Cheat(watch, value, compare, !Global.Config.DisableCheatsOnLoad && enabled));
} }
@ -393,11 +385,6 @@ namespace BizHawk.Client.Common
return true; return true;
} }
public string CurrentFileName
{
get { return _currentFileName; }
}
public void Sort(string column, bool reverse) public void Sort(string column, bool reverse)
{ {
switch (column) switch (column)
@ -572,21 +559,6 @@ namespace BizHawk.Client.Common
} }
} }
public class CheatListEventArgs : EventArgs
{
public CheatListEventArgs(Cheat c)
{
Cheat = c;
}
public Cheat Cheat { get; private set; }
}
public delegate void CheatListEventHandler(object sender, CheatListEventArgs e);
public event CheatListEventHandler Changed;
#region Privates
private void CheatChanged(object sender) private void CheatChanged(object sender)
{ {
if (Changed != null) if (Changed != null)
@ -597,7 +569,15 @@ namespace BizHawk.Client.Common
_changes = true; _changes = true;
} }
#endregion public class CheatListEventArgs : EventArgs
{
public CheatListEventArgs(Cheat c)
{
Cheat = c;
}
public Cheat Cheat { get; private set; }
}
public const string NAME = "NamesColumn"; public const string NAME = "NamesColumn";
public const string ADDRESS = "AddressColumn"; public const string ADDRESS = "AddressColumn";

View File

@ -44,7 +44,7 @@ namespace BizHawk.Client.Common
protected DisplayType _type; protected DisplayType _type;
protected bool _bigEndian; protected bool _bigEndian;
protected int _changecount; protected int _changecount;
protected string _notes = String.Empty; protected string _notes = string.Empty;
public abstract int? Value { get; } public abstract int? Value { get; }
public abstract string ValueString { get; } public abstract string ValueString { get; }
@ -163,10 +163,8 @@ namespace BizHawk.Client.Common
{ {
return "X" + IntHelpers.GetNumDigits(this._domain.Size - 1); return "X" + IntHelpers.GetNumDigits(this._domain.Size - 1);
} }
else
{ return string.Empty;
return String.Empty;
}
} }
} }
@ -283,17 +281,17 @@ namespace BizHawk.Client.Common
public override string AddressString public override string AddressString
{ {
get { return String.Empty; } get { return string.Empty; }
} }
public override string ValueString public override string ValueString
{ {
get { return String.Empty; } get { return string.Empty; }
} }
public override string PreviousStr public override string PreviousStr
{ {
get { return String.Empty; } get { return string.Empty; }
} }
public override string ToString() public override string ToString()
@ -331,7 +329,7 @@ namespace BizHawk.Client.Common
return; return;
} }
public override string Diff { get { return String.Empty; } } public override string Diff { get { return string.Empty; } }
public override void Update() { return; } public override void Update() { return; }
} }
@ -503,7 +501,7 @@ namespace BizHawk.Client.Common
{ {
get get
{ {
var diff = String.Empty; var diff = string.Empty;
var diffVal = _value - _previous; var diffVal = _value - _previous;
if (diffVal > 0) if (diffVal > 0)
{ {
@ -636,7 +634,7 @@ namespace BizHawk.Client.Common
case DisplayType.Hex: case DisplayType.Hex:
return val.ToHexString(4); return val.ToHexString(4);
case DisplayType.FixedPoint_12_4: case DisplayType.FixedPoint_12_4:
return String.Format("{0:F4}", val / 16.0); return string.Format("{0:F4}", val / 16.0);
case DisplayType.Binary: case DisplayType.Binary:
return Convert.ToString(val, 2).PadLeft(16, '0').Insert(8, " ").Insert(4, " ").Insert(14, " "); return Convert.ToString(val, 2).PadLeft(16, '0').Insert(8, " ").Insert(4, " ").Insert(14, " ");
} }
@ -841,11 +839,11 @@ namespace BizHawk.Client.Common
case DisplayType.Hex: case DisplayType.Hex:
return val.ToHexString(8); return val.ToHexString(8);
case DisplayType.FixedPoint_20_12: case DisplayType.FixedPoint_20_12:
return String.Format("{0:0.######}", val / 4096.0); return string.Format("{0:0.######}", val / 4096.0);
case DisplayType.Float: case DisplayType.Float:
var bytes = BitConverter.GetBytes(val); var bytes = BitConverter.GetBytes(val);
var _float = BitConverter.ToSingle(bytes, 0); var _float = BitConverter.ToSingle(bytes, 0);
return String.Format("{0:0.######}", _float); return string.Format("{0:0.######}", _float);
} }
} }

View File

@ -1,5 +1,4 @@
using System; using System.Collections.Generic;
using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
@ -11,6 +10,10 @@ namespace BizHawk.Client.Common
{ {
public class WatchList : IList<Watch> public class WatchList : IList<Watch>
{ {
private List<Watch> _watchList = new List<Watch>();
private MemoryDomain _domain;
private string _currentFilename = string.Empty;
public const string ADDRESS = "AddressColumn"; public const string ADDRESS = "AddressColumn";
public const string VALUE = "ValueColumn"; public const string VALUE = "ValueColumn";
public const string PREV = "PrevColumn"; public const string PREV = "PrevColumn";
@ -19,25 +22,24 @@ namespace BizHawk.Client.Common
public const string DOMAIN = "DomainColumn"; public const string DOMAIN = "DomainColumn";
public const string NOTES = "NotesColumn"; public const string NOTES = "NotesColumn";
public enum WatchPrevDef { LastSearch, Original, LastFrame, LastChange }
private List<Watch> _watchList = new List<Watch>();
private MemoryDomain _domain;
private string _currentFilename = String.Empty;
public WatchList(MemoryDomain domain) public WatchList(MemoryDomain domain)
{ {
_domain = domain; _domain = domain;
} }
public enum WatchPrevDef { LastSearch, Original, LastFrame, LastChange }
public IEnumerator<Watch> GetEnumerator() public string AddressFormatStr // TODO: this is probably compensating for not using the ToHex string extension
{ {
return _watchList.GetEnumerator(); get
} {
if (_domain != null)
{
return "{0:X" + IntHelpers.GetNumDigits(this._domain.Size - 1) + "}";
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() return string.Empty;
{ }
return GetEnumerator();
} }
public int Count public int Count
@ -45,12 +47,6 @@ namespace BizHawk.Client.Common
get { return _watchList.Count; } get { return _watchList.Count; }
} }
public Watch this[int index]
{
get { return _watchList[index]; }
set { _watchList[index] = value; }
}
public int WatchCount public int WatchCount
{ {
get { return _watchList.Count(w => !w.IsSeparator); } get { return _watchList.Count(w => !w.IsSeparator); }
@ -61,6 +57,38 @@ namespace BizHawk.Client.Common
get { return _watchList.Count; } get { return _watchList.Count; }
} }
public MemoryDomain Domain
{
get { return _domain; }
set { _domain = value; }
}
public bool IsReadOnly { get { return false; } }
public string CurrentFileName
{
get { return _currentFilename; }
set { _currentFilename = value; }
}
public bool Changes { get; set; }
public Watch this[int index]
{
get { return _watchList[index]; }
set { _watchList[index] = value; }
}
public IEnumerator<Watch> GetEnumerator()
{
return _watchList.GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public void OrderWatches(string column, bool reverse) public void OrderWatches(string column, bool reverse)
{ {
switch (column) switch (column)
@ -221,30 +249,13 @@ namespace BizHawk.Client.Common
} }
} }
public string AddressFormatStr
{
get
{
if (_domain != null)
{
return "{0:X" + IntHelpers.GetNumDigits(this._domain.Size - 1) + "}";
}
else
{
return String.Empty;
}
}
}
public void Clear() public void Clear()
{ {
_watchList.Clear(); _watchList.Clear();
Changes = false; Changes = false;
_currentFilename = String.Empty; _currentFilename = string.Empty;
} }
public MemoryDomain Domain { get { return _domain; } set { _domain = value; } }
public void UpdateValues() public void UpdateValues()
{ {
foreach (var watch in _watchList) foreach (var watch in _watchList)
@ -289,8 +300,6 @@ namespace BizHawk.Client.Common
} }
} }
public bool IsReadOnly { get { return false; } }
public bool Contains(Watch watch) public bool Contains(Watch watch)
{ {
return _watchList.Any(w => return _watchList.Any(w =>
@ -298,8 +307,7 @@ namespace BizHawk.Client.Common
w.Type == watch.Type && w.Type == watch.Type &&
w.Domain == watch.Domain && w.Domain == watch.Domain &&
w.Address == watch.Address && w.Address == watch.Address &&
w.BigEndian == watch.BigEndian w.BigEndian == watch.BigEndian);
);
} }
public void CopyTo(Watch[] array, int arrayIndex) public void CopyTo(Watch[] array, int arrayIndex)
@ -320,9 +328,6 @@ namespace BizHawk.Client.Common
#region File handling logic - probably needs to be its own class #region File handling logic - probably needs to be its own class
public string CurrentFileName { get { return _currentFilename; } set { _currentFilename = value; } }
public bool Changes { get; set; }
public bool Load(string path, bool append) public bool Load(string path, bool append)
{ {
var result = LoadFile(path, append); var result = LoadFile(path, append);
@ -345,7 +350,7 @@ namespace BizHawk.Client.Common
public void Reload() public void Reload()
{ {
if (!String.IsNullOrWhiteSpace(CurrentFileName)) if (!string.IsNullOrWhiteSpace(CurrentFileName))
{ {
LoadFile(CurrentFileName, append: false); LoadFile(CurrentFileName, append: false);
Changes = false; Changes = false;
@ -354,7 +359,7 @@ namespace BizHawk.Client.Common
public bool Save() public bool Save()
{ {
if (String.IsNullOrWhiteSpace(CurrentFileName)) if (string.IsNullOrWhiteSpace(CurrentFileName))
{ {
return false; return false;
} }
@ -369,7 +374,7 @@ namespace BizHawk.Client.Common
foreach (var watch in _watchList) foreach (var watch in _watchList)
{ {
sb sb
.Append(String.Format(AddressFormatStr, watch.Address ?? 0)).Append('\t') .Append(string.Format(AddressFormatStr, watch.Address ?? 0)).Append('\t')
.Append(watch.SizeAsChar).Append('\t') .Append(watch.SizeAsChar).Append('\t')
.Append(watch.TypeAsChar).Append('\t') .Append(watch.TypeAsChar).Append('\t')
.Append(watch.BigEndian ? '1' : '0').Append('\t') .Append(watch.BigEndian ? '1' : '0').Append('\t')
@ -393,15 +398,13 @@ namespace BizHawk.Client.Common
CurrentFileName = file.FullName; CurrentFileName = file.FullName;
return Save(); return Save();
} }
else
{ return false;
return false;
}
} }
private bool LoadFile(string path, bool append) private bool LoadFile(string path, bool append)
{ {
var domain = String.Empty; var domain = string.Empty;
var file = new FileInfo(path); var file = new FileInfo(path);
if (file.Exists == false) if (file.Exists == false)
{ {
@ -457,9 +460,9 @@ namespace BizHawk.Client.Common
} }
else if (numColumns == 4) else if (numColumns == 4)
{ {
isOldBizHawkWatch = true; isOldBizHawkWatch = true; // This supports the legacy .wch format from 1.0.5 and earlier
} }
else // 4 is 1.0.5 and earlier else
{ {
continue; // If not 4, something is wrong with this line, ignore it continue; // If not 4, something is wrong with this line, ignore it
} }
@ -471,7 +474,7 @@ namespace BizHawk.Client.Common
var temp = line.Substring(0, line.IndexOf('\t')); var temp = line.Substring(0, line.IndexOf('\t'));
try try
{ {
addr = Int32.Parse(temp, NumberStyles.HexNumber); addr = int.Parse(temp, NumberStyles.HexNumber);
} }
catch catch
{ {
@ -490,7 +493,7 @@ namespace BizHawk.Client.Common
line = line.Substring(startIndex, line.Length - startIndex); // Endian line = line.Substring(startIndex, line.Length - startIndex); // Endian
try try
{ {
startIndex = Int16.Parse(line[0].ToString()); startIndex = short.Parse(line[0].ToString());
} }
catch catch
{ {

View File

@ -98,9 +98,9 @@ namespace BizHawk.Client.EmuHawk
new MemoryLuaLibrary().LuaRegister(lua, Docs); new MemoryLuaLibrary().LuaRegister(lua, Docs);
new MainMemoryLuaLibrary(_lua).LuaRegister(lua, Docs); new MainMemoryLuaLibrary(_lua).LuaRegister(lua, Docs);
new MovieLuaLibrary(_lua).LuaRegister(lua, Docs); new MovieLuaLibrary(_lua).LuaRegister(lua, Docs);
new NESLuaLibrary().LuaRegister(lua, Docs); new NesLuaLibrary().LuaRegister(lua, Docs);
new SavestateLuaLibrary().LuaRegister(lua, Docs); new SavestateLuaLibrary().LuaRegister(lua, Docs);
new SNESLuaLibrary().LuaRegister(lua, Docs); new SnesLuaLibrary().LuaRegister(lua, Docs);
new StringLuaLibrary().LuaRegister(lua, Docs); new StringLuaLibrary().LuaRegister(lua, Docs);
Docs.Sort(); Docs.Sort();