a round of code cleanup in Client.Common

This commit is contained in:
adelikat 2013-12-30 01:58:44 +00:00
parent 16f7c7fcdc
commit 7482cfdc5e
21 changed files with 406 additions and 309 deletions

View File

@ -1,7 +1,8 @@
using System; using System;
using ICSharpCode.SharpZipLib.Zip;
using System.IO;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
namespace BizHawk.Client.Common namespace BizHawk.Client.Common
{ {
@ -17,7 +18,7 @@ namespace BizHawk.Client.Common
public const string Movieheader = "Header"; public const string Movieheader = "Header";
*/ */
private static Dictionary<BinaryStateLump, string> LumpNames; private static readonly Dictionary<BinaryStateLump, string> LumpNames;
static BinaryStateFileNames() static BinaryStateFileNames()
{ {
@ -50,7 +51,7 @@ namespace BizHawk.Client.Common
public class BinaryStateLoader : IDisposable public class BinaryStateLoader : IDisposable
{ {
private bool isDisposed; private bool _isDisposed;
public void Dispose() public void Dispose()
{ {
Dispose(true); Dispose(true);
@ -59,9 +60,9 @@ namespace BizHawk.Client.Common
protected virtual void Dispose(bool disposing) protected virtual void Dispose(bool disposing)
{ {
if (!isDisposed) if (!_isDisposed)
{ {
isDisposed = true; _isDisposed = true;
if (disposing) if (disposing)
{ {
@ -70,8 +71,8 @@ namespace BizHawk.Client.Common
} }
} }
ZipFile zip; private ZipFile zip;
Version ver; private Version ver;
private BinaryStateLoader() private BinaryStateLoader()
{ {
@ -81,10 +82,12 @@ namespace BizHawk.Client.Common
{ {
// the "BizState 1.0" tag contains an integer in it describing the sub version. // the "BizState 1.0" tag contains an integer in it describing the sub version.
if (s.Length == 0) if (s.Length == 0)
{
ver = new Version(1, 0, 0); // except for the first release, which doesn't ver = new Version(1, 0, 0); // except for the first release, which doesn't
}
else else
{ {
StreamReader sr = new StreamReader(s); var sr = new StreamReader(s);
ver = new Version(1, 0, int.Parse(sr.ReadLine())); ver = new Version(1, 0, int.Parse(sr.ReadLine()));
} }
Console.WriteLine("Read a zipstate of version {0}", ver.ToString()); Console.WriteLine("Read a zipstate of version {0}", ver.ToString());
@ -92,17 +95,22 @@ namespace BizHawk.Client.Common
public static BinaryStateLoader LoadAndDetect(string Filename) public static BinaryStateLoader LoadAndDetect(string Filename)
{ {
BinaryStateLoader ret = new BinaryStateLoader(); var ret = new BinaryStateLoader();
//PORTABLE TODO - SKIP THIS.. FOR NOW // PORTABLE TODO - SKIP THIS.. FOR NOW
//check whether its an archive before we try opening it // check whether its an archive before we try opening it
int offset; int offset;
bool isExecutable; bool isExecutable;
bool isArchive; bool isArchive;
using(var archiveChecker = new SevenZipSharpArchiveHandler()) using (var archiveChecker = new SevenZipSharpArchiveHandler())
{
isArchive = archiveChecker.CheckSignature(Filename, out offset, out isExecutable); isArchive = archiveChecker.CheckSignature(Filename, out offset, out isExecutable);
if(!isArchive) }
if (!isArchive)
{
return null; return null;
}
try try
{ {
@ -112,6 +120,7 @@ namespace BizHawk.Client.Common
ret.zip.Close(); ret.zip.Close();
return null; return null;
} }
return ret; return ret;
} }
catch (ZipException) catch (ZipException)
@ -121,7 +130,7 @@ namespace BizHawk.Client.Common
} }
/// <summary> /// <summary>
/// /// Gets a lump
/// </summary> /// </summary>
/// <param name="Lump">lump to retriever</param> /// <param name="Lump">lump to retriever</param>
/// <param name="abort">true to throw exception on failure</param> /// <param name="abort">true to throw exception on failure</param>
@ -133,7 +142,7 @@ namespace BizHawk.Client.Common
var e = zip.GetEntry(Name); var e = zip.GetEntry(Name);
if (e != null) if (e != null)
{ {
using (Stream zs = zip.GetInputStream(e)) using (var zs = zip.GetInputStream(e))
{ {
callback(zs); callback(zs);
} }
@ -170,8 +179,6 @@ namespace BizHawk.Client.Common
/// <summary> /// <summary>
/// load binary state, or text state if binary state lump doesn't exist /// load binary state, or text state if binary state lump doesn't exist
/// </summary> /// </summary>
/// <param name="callbackBinary"></param>
/// <param name="callbackText"></param>
public void GetCoreState(Action<Stream> callbackBinary, Action<Stream> callbackText) public void GetCoreState(Action<Stream> callbackBinary, Action<Stream> callbackText)
{ {
if (!GetLump(BinaryStateLump.Corestate, false, callbackBinary) if (!GetLump(BinaryStateLump.Corestate, false, callbackBinary)
@ -210,7 +217,7 @@ namespace BizHawk.Client.Common
private void WriteVersion(Stream s) private void WriteVersion(Stream s)
{ {
StreamWriter sw = new StreamWriter(s); var sw = new StreamWriter(s);
sw.WriteLine("1"); // version 1.0.1 sw.WriteLine("1"); // version 1.0.1
sw.Flush(); sw.Flush();
} }
@ -244,7 +251,7 @@ namespace BizHawk.Client.Common
{ {
PutLump(Lump, delegate(Stream s) PutLump(Lump, delegate(Stream s)
{ {
BinaryWriter bw = new BinaryWriter(s); var bw = new BinaryWriter(s);
callback(bw); callback(bw);
bw.Flush(); bw.Flush();
}); });
@ -287,7 +294,8 @@ namespace BizHawk.Client.Common
} }
*/ */
private bool isDisposed; private bool _isDisposed;
public void Dispose() public void Dispose()
{ {
Dispose(true); Dispose(true);
@ -296,9 +304,9 @@ namespace BizHawk.Client.Common
protected virtual void Dispose(bool disposing) protected virtual void Dispose(bool disposing)
{ {
if (!isDisposed) if (!_isDisposed)
{ {
isDisposed = true; _isDisposed = true;
if (disposing) if (disposing)
{ {
@ -306,6 +314,5 @@ namespace BizHawk.Client.Common
} }
} }
} }
} }
} }

View File

@ -9,7 +9,7 @@ namespace BizHawk.Client.Common
{ {
public class Controller : IController public class Controller : IController
{ {
private ControllerDefinition type; private ControllerDefinition _type;
private readonly WorkingDictionary<string, List<string>> bindings = new WorkingDictionary<string, List<string>>(); private readonly WorkingDictionary<string, List<string>> bindings = new WorkingDictionary<string, List<string>>();
private readonly WorkingDictionary<string, bool> buttons = new WorkingDictionary<string, bool>(); private readonly WorkingDictionary<string, bool> buttons = new WorkingDictionary<string, bool>();
@ -21,17 +21,18 @@ namespace BizHawk.Client.Common
public Controller(ControllerDefinition definition) public Controller(ControllerDefinition definition)
{ {
type = definition; _type = definition;
for (int i = 0; i < type.FloatControls.Count; i++) for (int i = 0; i < _type.FloatControls.Count; i++)
{ {
FloatButtons[type.FloatControls[i]] = type.FloatRanges[i].Mid; FloatButtons[_type.FloatControls[i]] = _type.FloatRanges[i].Mid;
FloatRanges[type.FloatControls[i]] = type.FloatRanges[i]; FloatRanges[_type.FloatControls[i]] = _type.FloatRanges[i];
} }
} }
public ControllerDefinition Type { get { return type; } } public ControllerDefinition Type { get { return _type; } }
/// <summary>don't do this</summary> /// <summary>don't do this</summary>
public void ForceType(ControllerDefinition newtype) { type = newtype; } public void ForceType(ControllerDefinition newtype) { _type = newtype; }
public bool this[string button] { get { return IsPressed(button); } } public bool this[string button] { get { return IsPressed(button); } }
public bool IsPressed(string button) public bool IsPressed(string button)
{ {
@ -40,13 +41,13 @@ namespace BizHawk.Client.Common
public float GetFloat(string name) { return FloatButtons[name]; } public float GetFloat(string name) { return FloatButtons[name]; }
//look for bindings which are activated by the supplied physical button. // Looks for bindings which are activated by the supplied physical button.
public List<string> SearchBindings(string button) public List<string> SearchBindings(string button)
{ {
return (from kvp in bindings from bound_button in kvp.Value where bound_button == button select kvp.Key).ToList(); return (from kvp in bindings from bound_button in kvp.Value where bound_button == button select kvp.Key).ToList();
} }
//Searches bindings for the controller and returns true if this binding is mapped somewhere in this controller // Searches bindings for the controller and returns true if this binding is mapped somewhere in this controller
public bool HasBinding(string button) public bool HasBinding(string button)
{ {
return bindings.SelectMany(kvp => kvp.Value).Any(bound_button => bound_button == button); return bindings.SelectMany(kvp => kvp.Value).Any(bound_button => bound_button == button);
@ -66,7 +67,9 @@ namespace BizHawk.Client.Common
foreach (var bound_button in kvp.Value) foreach (var bound_button in kvp.Value)
{ {
if (controller[bound_button]) if (controller[bound_button])
{
buttons[kvp.Key] = true; buttons[kvp.Key] = true;
}
} }
} }
@ -110,15 +113,17 @@ namespace BizHawk.Client.Common
public void OR_FromLogical(IController controller) public void OR_FromLogical(IController controller)
{ {
// change: or from each button that the other input controller has // change: or from each button that the other input controller has
//foreach (string button in type.BoolButtons) // foreach (string button in type.BoolButtons)
if (controller.Type != null) if (controller.Type != null)
foreach (string button in controller.Type.BoolButtons) {
foreach (var button in controller.Type.BoolButtons)
{ {
if (controller.IsPressed(button)) if (controller.IsPressed(button))
{ {
buttons[button] = true; buttons[button] = true;
} }
} }
}
} }
public void BindButton(string button, string control) public void BindButton(string button, string control)
@ -129,10 +134,15 @@ namespace BizHawk.Client.Common
public void BindMulti(string button, string controlString) public void BindMulti(string button, string controlString)
{ {
if (string.IsNullOrEmpty(controlString)) if (string.IsNullOrEmpty(controlString))
{
return; return;
string[] controlbindings = controlString.Split(','); }
foreach (string control in controlbindings)
var controlbindings = controlString.Split(',');
foreach (var control in controlbindings)
{
bindings[button].Add(control.Trim()); bindings[button].Add(control.Trim());
}
} }
public void BindFloat(string button, Config.AnalogBind bind) public void BindFloat(string button, Config.AnalogBind bind)
@ -143,7 +153,6 @@ namespace BizHawk.Client.Common
/// <summary> /// <summary>
/// Returns a list of all keys mapped and the name of the button they are mapped to /// Returns a list of all keys mapped and the name of the button they are mapped to
/// </summary> /// </summary>
/// <returns></returns>
public List<KeyValuePair<string, string>> MappingList() public List<KeyValuePair<string, string>> MappingList()
{ {
return (from key in bindings from binding in key.Value select new KeyValuePair<string, string>(binding, key.Key)).ToList(); return (from key in bindings from binding in key.Value select new KeyValuePair<string, string>(binding, key.Key)).ToList();
@ -201,7 +210,7 @@ namespace BizHawk.Client.Common
public float GetFloat(string name) { throw new NotImplementedException(); } public float GetFloat(string name) { throw new NotImplementedException(); }
//look for bindings which are activated by the supplied physical button. // look for bindings which are activated by the supplied physical button.
public List<string> SearchBindings(string button) public List<string> SearchBindings(string button)
{ {
return (from kvp in bindings from bound_button in kvp.Value where bound_button == button select kvp.Key).ToList(); return (from kvp in bindings from bound_button in kvp.Value where bound_button == button select kvp.Key).ToList();
@ -218,7 +227,9 @@ namespace BizHawk.Client.Common
foreach (var bound_button in kvp.Value) foreach (var bound_button in kvp.Value)
{ {
if (buttons[kvp.Key] == false && controller[bound_button]) if (buttons[kvp.Key] == false && controller[bound_button])
{
buttonStarts[kvp.Key] = Global.Emulator.Frame; buttonStarts[kvp.Key] = Global.Emulator.Frame;
}
} }
} }
@ -241,13 +252,10 @@ namespace BizHawk.Client.Common
/// </summary> /// </summary>
public void OR_FromLogical(IController controller) public void OR_FromLogical(IController controller)
{ {
foreach (string button in type.BoolButtons) foreach (var button in type.BoolButtons.Where(controller.IsPressed))
{ {
if (controller.IsPressed(button)) buttons[button] = true;
{ Console.WriteLine(button);
buttons[button] = true;
Console.WriteLine(button);
}
} }
} }
@ -260,8 +268,8 @@ namespace BizHawk.Client.Common
{ {
if (!String.IsNullOrEmpty(controlString)) if (!String.IsNullOrEmpty(controlString))
{ {
string[] controlbindings = controlString.Split(','); var controlbindings = controlString.Split(',');
foreach (string control in controlbindings) foreach (var control in controlbindings)
{ {
bindings[button].Add(control.Trim()); bindings[button].Add(control.Trim());
} }
@ -270,7 +278,10 @@ namespace BizHawk.Client.Common
public void IncrementStarts() public void IncrementStarts()
{ {
foreach (var key in buttonStarts.Keys.ToArray()) buttonStarts[key]++; foreach (var key in buttonStarts.Keys.ToArray())
{
buttonStarts[key]++;
}
} }
public List<string> PressedButtons public List<string> PressedButtons

View File

@ -12,9 +12,9 @@ namespace BizHawk.Client.Common
Action<string> ShowWarning; Action<string> ShowWarning;
public CoreFileProvider(Action<string> ShowWarning) public CoreFileProvider(Action<string> showWarning)
{ {
this.ShowWarning = ShowWarning; ShowWarning = showWarning;
} }
public Stream OpenFirmware(string sysId, string key) public Stream OpenFirmware(string sysId, string key)
@ -39,6 +39,7 @@ namespace BizHawk.Client.Common
} }
#region EmuLoadHelper api #region EmuLoadHelper api
void FirmwareWarn(string sysID, string firmwareID, bool required, string msg = null) void FirmwareWarn(string sysID, string firmwareID, bool required, string msg = null)
{ {
if (required) if (required)

View File

@ -7,27 +7,33 @@ namespace BizHawk.Client.Common
/// </summary> /// </summary>
public static class InputValidate public static class InputValidate
{ {
public static bool IsValidUnsignedNumber(string Str) public static bool IsValidUnsignedNumber(string str)
{ {
char[] input = (Str.ToCharArray()); var input = str.ToCharArray();
ASCIIEncoding AE = new ASCIIEncoding(); var asciiEncoding = new ASCIIEncoding();
// Check each character in the new label to determine if it is a number. // Check each character in the new label to determine if it is a number.
for (int x = 0; x < input.Length; x++) for (int i = 0; i < input.Length; i++)
{ {
// Encode the character from the character array to its ASCII code. // Encode the character from the character array to its ASCII code.
byte[] bc = AE.GetBytes(input[x].ToString()); var bc = asciiEncoding.GetBytes(input[i].ToString());
// Determine if the ASCII code is within the valid range of numerical values. // Determine if the ASCII code is within the valid range of numerical values.
if (bc[0] < 47 || bc[0] > 58) if (bc[0] < 47 || bc[0] > 58)
{
return false; return false;
}
} }
return true; return true;
} }
public static bool IsValidUnsignedNumber(char c) public static bool IsValidUnsignedNumber(char c)
{ {
if (c < 47 || c > 58) if (c < 47 || c > 58)
{
return false; return false;
}
return true; return true;
} }
@ -35,40 +41,50 @@ namespace BizHawk.Client.Common
/// <summary> /// <summary>
/// Validates all chars are 0-9 or a dash as the first value /// Validates all chars are 0-9 or a dash as the first value
/// </summary> /// </summary>
/// <param name="Str"></param> public static bool IsValidSignedNumber(string str)
/// <returns></returns>
public static bool IsValidSignedNumber(string Str)
{ {
char[] input = (Str.Trim().ToCharArray()); var input = str.Trim().ToCharArray();
ASCIIEncoding AE = new ASCIIEncoding(); var asciiEncoding = new ASCIIEncoding();
// Check each character in the new label to determine if it is a number. // Check each character in the new label to determine if it is a number.
for (int x = 0; x < input.Length; x++) for (int i = 0; i < input.Length; i++)
{ {
// Encode the character from the character array to its ASCII code. // Encode the character from the character array to its ASCII code.
byte[] bc = AE.GetBytes(input[x].ToString()); var bc = asciiEncoding.GetBytes(input[i].ToString());
// Determine if the ASCII code is within the valid range of numerical values. // Determine if the ASCII code is within the valid range of numerical values.
if (bc[0] > 58) if (bc[0] > 58)
{
return false; return false;
}
if (bc[0] < 47) if (bc[0] < 47)
{ {
if (bc[0] == 45 && x == 0) if (bc[0] == 45 && i == 0)
{
continue; continue;
}
else else
{
return false; return false;
}
} }
} }
return true; return true;
} }
public static bool IsValidSignedNumber(char c) public static bool IsValidSignedNumber(char c)
{ {
if (c == 45) return true; if (c == 45)
{
return true;
}
if (c < 47 || c > 58) if (c < 47 || c > 58)
{
return false; return false;
}
return true; return true;
} }
@ -76,147 +92,174 @@ namespace BizHawk.Client.Common
/// <summary> /// <summary>
/// validates is a Hex number 0-9, A-F (must be capital letters) /// validates is a Hex number 0-9, A-F (must be capital letters)
/// </summary> /// </summary>
/// <returns></returns> public static bool IsValidHexNumber(string str)
public static bool IsValidHexNumber(string Str)
{ {
char[] input = (Str.ToCharArray()); var input = str.ToCharArray();
ASCIIEncoding AE = new ASCIIEncoding(); var asciiEncoding = new ASCIIEncoding();
// Check each character in the new label to determine if it is a number. // Check each character in the new label to determine if it is a number.
for (int x = 0; x < input.Length; x++) for (int i = 0; i < input.Length; i++)
{ {
// Encode the character from the character array to its ASCII code. // Encode the character from the character array to its ASCII code.
byte[] bc = AE.GetBytes(input[x].ToString()); var bc = asciiEncoding.GetBytes(input[i].ToString());
// Determine if the ASCII code is within the valid range of numerical values. // Determine if the ASCII code is within the valid range of numerical values.
if (bc[0] < 47) //0 if (bc[0] < 47) // 0
return false;
if (bc[0] > 58) //9
{ {
if (bc[0] < 65) //A return false;
return false; }
if (bc[0] > 70) //F if (bc[0] > 58) // 9
{
if (bc[0] < 65) // A
{ {
if (bc[0] < 97 || bc[0] > 102) //a-f return false;
}
if (bc[0] > 70) // F
{
if (bc[0] < 97 || bc[0] > 102) // a-f
{
return false; return false;
}
} }
} }
} }
return true; return true;
} }
public static bool IsValidHexNumber(char c) public static bool IsValidHexNumber(char c)
{ {
if (c < 47) return false; //0 if (c < 47)
if (c > 58) //9
{ {
if (c < 65) //A return false; // 0
return false; }
if (c > 70) //F if (c > 58) // 9
{
if (c < 65) // A
{ {
if (c < 97 || c > 102) //a-f return false;
}
if (c > 70) // F
{
if (c < 97 || c > 102) // a-f
{
return false; return false;
}
} }
} }
return true; return true;
} }
/// <summary> /// <summary>
/// Takes any string and removes any value that is not a valid hex value (0-9, a-f, A-F), returns the remaining characters in uppercase /// Takes any string and removes any value that is not a valid hex value (0-9, a-f, A-F), returns the remaining characters in uppercase
/// </summary> /// </summary>
/// <param name="raw"></param>
/// <returns></returns>
public static string DoHexString(string raw) public static string DoHexString(string raw)
{ {
raw = raw.ToUpper(); raw = raw.ToUpper();
StringBuilder output = new StringBuilder(); var output = new StringBuilder();
foreach (char x in raw) foreach (var chr in raw)
{ {
if (x >= 'A' && x <= 'F') if (chr >= 'A' && chr <= 'F')
{ {
output.Append(x); output.Append(chr);
} }
else if (x >= '0' && x <= '9') else if (chr >= '0' && chr <= '9')
{ {
output.Append(x); output.Append(chr);
} }
} }
return output.ToString(); return output.ToString();
} }
public static bool IsValidBinaryNumber(string str)
public static bool IsValidBinaryNumber(string s)
{ {
char[] input = (s.ToCharArray()); var input = str.ToCharArray();
ASCIIEncoding AE = new ASCIIEncoding(); var asciiEncoding = new ASCIIEncoding();
// Check each character in the new label to determine if it is a number. // Check each character in the new label to determine if it is a number.
for (int x = 0; x < input.Length; x++) for (int i = 0; i < input.Length; i++)
{ {
// Encode the character from the character array to its ASCII code. // Encode the character from the character array to its ASCII code.
byte[] bc = AE.GetBytes(input[x].ToString()); var bc = asciiEncoding.GetBytes(input[i].ToString());
// Determine if the ASCII code is within the valid range of numerical values. // Determine if the ASCII code is within the valid range of numerical values.
if (bc[0] != 48 && bc[0] != 49) //0 or 1 if (bc[0] != 48 && bc[0] != 49) // 0 or 1
{ {
return false; return false;
} }
} }
return true; return true;
} }
public static bool IsValidBinaryNumber(char c) public static bool IsValidBinaryNumber(char c)
{ {
return (c == 48 || c == 49); return c == 48 || c == 49;
} }
/// <summary> /// <summary>
/// Validates all chars are 0-9 or decimal /// Validates all chars are 0-9 or decimal
/// </summary> /// </summary>
/// <param name="Str"></param> public static bool IsValidFixedPointNumber(string str)
/// <returns></returns>
public static bool IsValidFixedPointNumber(string Str)
{ {
if (StringHelpers.HowMany(Str, '.') > 1) if (StringHelpers.HowMany(str, '.') > 1)
{ {
return false; return false;
} }
char[] input = (Str.Trim().ToCharArray()); var input = str.Trim().ToCharArray();
ASCIIEncoding AE = new ASCIIEncoding(); var asciiEncoding = new ASCIIEncoding();
// Check each character in the new label to determine if it is a number. // Check each character in the new label to determine if it is a number.
for (int i = 0; i < input.Length; i++)
for (int x = 0; x < input.Length; x++)
{ {
// Encode the character from the character array to its ASCII code. // Encode the character from the character array to its ASCII code.
byte[] bc = AE.GetBytes(input[x].ToString()); var bc = asciiEncoding.GetBytes(input[i].ToString());
// Determine if the ASCII code is within the valid range of numerical values. // Determine if the ASCII code is within the valid range of numerical values.
if (bc[0] > 58) if (bc[0] > 58)
{
return false; return false;
}
if (bc[0] == 46) if (bc[0] == 46)
{
continue; continue;
}
if (bc[0] < 48) if (bc[0] < 48)
{ {
if (bc[0] == 45 && x == 0) if (bc[0] == 45 && i == 0)
{
continue; continue;
}
else else
{
return false; return false;
}
} }
} }
return true; return true;
} }
public static bool IsValidFixedPointNumber(char c) public static bool IsValidFixedPointNumber(char c)
{ {
if (c == 46 || c == 45) return true; if (c == 46 || c == 45)
{
return true;
}
if (c < 48 || c > 58) if (c < 48 || c > 58)
{
return false; return false;
}
return true; return true;
} }
@ -224,45 +267,52 @@ namespace BizHawk.Client.Common
/// <summary> /// <summary>
/// Validates all chars are 0-9 or decimal or dash as the first character /// Validates all chars are 0-9 or decimal or dash as the first character
/// </summary> /// </summary>
/// <param name="Str"></param> public static bool IsValidDecimalNumber(string str)
/// <returns></returns>
public static bool IsValidDecimalNumber(string Str)
{ {
if (StringHelpers.HowMany(Str, '.') > 1) if (StringHelpers.HowMany(str, '.') > 1)
{ {
return false; return false;
} }
char[] input = (Str.Trim().ToCharArray()); var input = str.Trim().ToCharArray();
ASCIIEncoding AE = new ASCIIEncoding(); var asciiEncoding = new ASCIIEncoding();
// Check each character in the new label to determine if it is a number. // Check each character in the new label to determine if it is a number.
for (int x = 0; x < input.Length; x++) for (int i = 0; i < input.Length; i++)
{ {
// Encode the character from the character array to its ASCII code. // Encode the character from the character array to its ASCII code.
byte[] bc = AE.GetBytes(input[x].ToString()); var bc = asciiEncoding.GetBytes(input[i].ToString());
// Determine if the ASCII code is within the valid range of numerical values. // Determine if the ASCII code is within the valid range of numerical values.
if (bc[0] > 58) if (bc[0] > 58)
{
return false; return false;
}
if (bc[0] == 46) if (bc[0] == 46)
{
continue; continue;
}
if (bc[0] < 48) if (bc[0] < 48)
{ {
if (bc[0] == 45 && x == 0) if (bc[0] == 45 && i == 0)
{
continue; continue;
}
else else
{
return false; return false;
}
} }
} }
return true; return true;
} }
public static bool IsValidDecimalNumber(char c) public static bool IsValidDecimalNumber(char c)
{ {
if (c == 45 || c == 46) //45 = dash, 46 = dot if (c == 45 || c == 46) // 45 = dash, 46 = dot
{ {
return true; return true;
} }

View File

@ -7,40 +7,45 @@ namespace BizHawk.Client.Common
{ {
public static int HowMany(string str, char c) public static int HowMany(string str, char c)
{ {
if (!String.IsNullOrEmpty(str)) return !String.IsNullOrEmpty(str) ? str.Count(t => t == c) : 0;
{
return str.Count(t => t == c);
}
else
{
return 0;
}
} }
public static int HowMany(string str, string s) public static int HowMany(string str, string s)
{ {
int count = 0; var count = 0;
for (int x = 0; x < (str.Length - s.Length); x++) for (int i = 0; i < (str.Length - s.Length); i++)
{ {
if (str.Substring(x, s.Length) == s) if (str.Substring(i, s.Length) == s)
{
count++; count++;
}
} }
return count; return count;
} }
} }
//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(Int32 i)
{ {
//if (i == 0) return 0; if (i < 0x100)
//if (i < 0x10) return 1; {
if (i < 0x100) return 2; return 2;
//if (i < 0x1000) return 3; //adelikat: let's only do even numbers }
if (i < 0x10000) return 4; else if (i < 0x10000)
if (i < 0x1000000) return 6; {
else return 8; return 4;
}
else if (i < 0x1000000)
{
return 6;
}
else
{
return 8;
}
} }
public static uint MaxHexValueFromMaxDigits(Int32 i) public static uint MaxHexValueFromMaxDigits(Int32 i)

View File

@ -1,6 +1,4 @@
using System; namespace BizHawk.Client.Common
namespace BizHawk.Client.Common
{ {
public class BitLuaLibrary : LuaLibraryBase public class BitLuaLibrary : LuaLibraryBase
{ {

View File

@ -1,17 +1,17 @@
using System; using System;
using System.Linq; using System.Linq;
using LuaInterface; using BizHawk.Emulation.Cores.Nintendo.NES;
using BizHawk.Emulation.Cores.PCEngine; using BizHawk.Emulation.Cores.PCEngine;
using BizHawk.Emulation.Cores.Sega.MasterSystem; using BizHawk.Emulation.Cores.Sega.MasterSystem;
using BizHawk.Emulation.Cores.Nintendo.NES;
using LuaInterface;
namespace BizHawk.Client.Common namespace BizHawk.Client.Common
{ {
public partial class EmulatorLuaLibrary : LuaLibraryBase public class EmulatorLuaLibrary : LuaLibraryBase
{ {
public EmulatorLuaLibrary(Lua lua, Action frameAdvanceCallback, Action yieldCallback) public EmulatorLuaLibrary(Lua lua, Action frameAdvanceCallback, Action yieldCallback)
: base()
{ {
_lua = lua; _lua = lua;
_frameAdvanceCallback = frameAdvanceCallback; _frameAdvanceCallback = frameAdvanceCallback;
@ -41,9 +41,9 @@ namespace BizHawk.Client.Common
} }
} }
private Lua _lua; private readonly Lua _lua;
private Action _frameAdvanceCallback; private readonly Action _frameAdvanceCallback;
private Action _yieldCallback; private readonly Action _yieldCallback;
private static void emu_setrenderplanes_do(object[] lua_p) private static void emu_setrenderplanes_do(object[] lua_p)
{ {
@ -51,14 +51,14 @@ namespace BizHawk.Client.Common
{ {
// in the future, we could do something more arbitrary here. // in the future, we could do something more arbitrary here.
// but this isn't any worse than the old system // but this isn't any worse than the old system
NES.NESSettings s = (NES.NESSettings)Global.Emulator.GetSettings(); var s = (NES.NESSettings)Global.Emulator.GetSettings();
s.DispSprites = (bool)lua_p[0]; s.DispSprites = (bool)lua_p[0];
s.DispBackground = (bool)lua_p[1]; s.DispBackground = (bool)lua_p[1];
Global.Emulator.PutSettings(s); Global.Emulator.PutSettings(s);
} }
else if (Global.Emulator is PCEngine) else if (Global.Emulator is PCEngine)
{ {
PCEngine.PCESettings s = (PCEngine.PCESettings)Global.Emulator.GetSettings(); var s = (PCEngine.PCESettings)Global.Emulator.GetSettings();
s.ShowOBJ1 = (bool)lua_p[0]; s.ShowOBJ1 = (bool)lua_p[0];
s.ShowBG1 = (bool)lua_p[1]; s.ShowBG1 = (bool)lua_p[1];
if (lua_p.Length > 2) if (lua_p.Length > 2)
@ -66,11 +66,12 @@ namespace BizHawk.Client.Common
s.ShowOBJ2 = (bool)lua_p[2]; s.ShowOBJ2 = (bool)lua_p[2];
s.ShowBG2 = (bool)lua_p[3]; s.ShowBG2 = (bool)lua_p[3];
} }
Global.Emulator.PutSettings(s); Global.Emulator.PutSettings(s);
} }
else if (Global.Emulator is SMS) else if (Global.Emulator is SMS)
{ {
SMS.SMSSettings s = (SMS.SMSSettings)Global.Emulator.GetSettings(); var s = (SMS.SMSSettings)Global.Emulator.GetSettings();
s.DispOBJ = (bool)lua_p[0]; s.DispOBJ = (bool)lua_p[0];
s.DispBG = (bool)lua_p[1]; s.DispBG = (bool)lua_p[1];
Global.Emulator.PutSettings(s); Global.Emulator.PutSettings(s);
@ -79,7 +80,7 @@ namespace BizHawk.Client.Common
public static void emu_displayvsync(object boolean) public static void emu_displayvsync(object boolean)
{ {
string temp = boolean.ToString(); var temp = boolean.ToString();
if (!String.IsNullOrWhiteSpace(temp)) if (!String.IsNullOrWhiteSpace(temp))
{ {
if (temp == "0" || temp.ToLower() == "false") if (temp == "0" || temp.ToLower() == "false")
@ -110,11 +111,12 @@ namespace BizHawk.Client.Common
public LuaTable emu_getregisters() public LuaTable emu_getregisters()
{ {
LuaTable table = _lua.NewTable(); var table = _lua.NewTable();
foreach (var kvp in Global.Emulator.GetCpuFlagsAndRegisters()) foreach (var kvp in Global.Emulator.GetCpuFlagsAndRegisters())
{ {
table[kvp.Key] = kvp.Value; table[kvp.Key] = kvp.Value;
} }
return table; return table;
} }
@ -135,7 +137,7 @@ namespace BizHawk.Client.Common
public static void emu_limitframerate(object boolean) public static void emu_limitframerate(object boolean)
{ {
string temp = boolean.ToString(); var temp = boolean.ToString();
if (!String.IsNullOrWhiteSpace(temp)) if (!String.IsNullOrWhiteSpace(temp))
{ {
if (temp == "0" || temp.ToLower() == "false") if (temp == "0" || temp.ToLower() == "false")
@ -151,7 +153,7 @@ namespace BizHawk.Client.Common
public static void emu_minimizeframeskip(object boolean) public static void emu_minimizeframeskip(object boolean)
{ {
string temp = boolean.ToString(); var temp = boolean.ToString();
if (!String.IsNullOrWhiteSpace(temp)) if (!String.IsNullOrWhiteSpace(temp))
{ {
if (temp == "0" || temp.ToLower() == "false") if (temp == "0" || temp.ToLower() == "false")
@ -166,8 +168,11 @@ namespace BizHawk.Client.Common
} }
public static void emu_setrenderplanes( // For now, it accepts arguments up to 5. public static void emu_setrenderplanes( // For now, it accepts arguments up to 5.
object lua_p0, object lua_p1 = null, object lua_p2 = null, object lua_p0,
object lua_p3 = null, object lua_p4 = null) object lua_p1 = null,
object lua_p2 = null,
object lua_p3 = null,
object lua_p4 = null)
{ {
emu_setrenderplanes_do(LuaVarArgs(lua_p0, lua_p1, lua_p2, lua_p3, lua_p4)); emu_setrenderplanes_do(LuaVarArgs(lua_p0, lua_p1, lua_p2, lua_p3, lua_p4));
} }

View File

@ -1,5 +1,6 @@
using System; using System;
using System.Linq; using System.Linq;
using LuaInterface; using LuaInterface;
namespace BizHawk.Client.Common namespace BizHawk.Client.Common
@ -32,8 +33,8 @@ namespace BizHawk.Client.Common
} }
} }
public Action<string> LogOutputCallback = null; public Action<string> LogOutputCallback { get; set; }
public Lua CurrentThread; public Lua CurrentThread { get; set; }
#region Events Library Helpers #region Events Library Helpers

View File

@ -6,7 +6,6 @@ namespace BizHawk.Client.Common
public class JoypadLuaLibrary : LuaLibraryBase public class JoypadLuaLibrary : LuaLibraryBase
{ {
public JoypadLuaLibrary(Lua lua) public JoypadLuaLibrary(Lua lua)
: base()
{ {
_lua = lua; _lua = lua;
} }
@ -26,30 +25,30 @@ namespace BizHawk.Client.Common
} }
} }
private Lua _lua; private readonly Lua _lua;
public LuaTable joypad_get(object controller = null) public LuaTable joypad_get(object controller = null)
{ {
LuaTable buttons = _lua.NewTable(); var buttons = _lua.NewTable();
foreach (string button in Global.ControllerOutput.Source.Type.BoolButtons) foreach (var button in Global.ControllerOutput.Source.Type.BoolButtons)
{ {
if (controller == null) if (controller == null)
{ {
buttons[button] = Global.ControllerOutput[button]; buttons[button] = Global.ControllerOutput[button];
} }
else if (button.Length >= 3 && button.Substring(0, 2) == "P" + LuaInt(controller).ToString()) else if (button.Length >= 3 && button.Substring(0, 2) == "P" + LuaInt(controller))
{ {
buttons[button.Substring(3)] = Global.ControllerOutput["P" + LuaInt(controller) + " " + button.Substring(3)]; buttons[button.Substring(3)] = Global.ControllerOutput["P" + LuaInt(controller) + " " + button.Substring(3)];
} }
} }
foreach (string button in Global.ControllerOutput.Source.Type.FloatControls) foreach (var button in Global.ControllerOutput.Source.Type.FloatControls)
{ {
if (controller == null) if (controller == null)
{ {
buttons[button] = Global.ControllerOutput.GetFloat(button); buttons[button] = Global.ControllerOutput.GetFloat(button);
} }
else if (button.Length >= 3 && button.Substring(0, 2) == "P" + LuaInt(controller).ToString()) else if (button.Length >= 3 && button.Substring(0, 2) == "P" + LuaInt(controller))
{ {
buttons[button.Substring(3)] = Global.ControllerOutput.GetFloat("P" + LuaInt(controller) + " " + button.Substring(3)); buttons[button.Substring(3)] = Global.ControllerOutput.GetFloat("P" + LuaInt(controller) + " " + button.Substring(3));
} }
@ -64,11 +63,12 @@ namespace BizHawk.Client.Common
public LuaTable joypad_getimmediate() public LuaTable joypad_getimmediate()
{ {
LuaTable buttons = _lua.NewTable(); var buttons = _lua.NewTable();
foreach (string button in Global.ActiveController.Type.BoolButtons) foreach (var button in Global.ActiveController.Type.BoolButtons)
{ {
buttons[button] = Global.ActiveController[button]; buttons[button] = Global.ActiveController[button];
} }
return buttons; return buttons;
} }
@ -78,9 +78,9 @@ namespace BizHawk.Client.Common
{ {
foreach (var button in buttons.Keys) foreach (var button in buttons.Keys)
{ {
bool invert = false; var invert = false;
bool? theValue; bool? theValue;
string theValueStr = buttons[button].ToString(); var theValueStr = buttons[button].ToString();
if (!String.IsNullOrWhiteSpace(theValueStr)) if (!String.IsNullOrWhiteSpace(theValueStr))
{ {
@ -108,7 +108,7 @@ namespace BizHawk.Client.Common
{ {
if (theValue == true) if (theValue == true)
{ {
if (controller == null) //Force On if (controller == null) // Force On
{ {
Global.ClickyVirtualPadController.Click(button.ToString()); Global.ClickyVirtualPadController.Click(button.ToString());
Global.ForceOffAdaptor.SetSticky(button.ToString(), false); Global.ForceOffAdaptor.SetSticky(button.ToString(), false);
@ -119,7 +119,7 @@ namespace BizHawk.Client.Common
Global.ForceOffAdaptor.SetSticky("P" + controller + " " + button, false); Global.ForceOffAdaptor.SetSticky("P" + controller + " " + button, false);
} }
} }
else if (theValue == false) //Force off else if (theValue == false) // Force off
{ {
if (controller == null) if (controller == null)
{ {
@ -132,7 +132,7 @@ namespace BizHawk.Client.Common
} }
else else
{ {
//Turn everything off // Turn everything off
if (controller == null) if (controller == null)
{ {
Global.ForceOffAdaptor.SetSticky(button.ToString(), false); Global.ForceOffAdaptor.SetSticky(button.ToString(), false);
@ -143,7 +143,7 @@ namespace BizHawk.Client.Common
} }
} }
} }
else //Inverse else // Inverse
{ {
if (controller == null) if (controller == null)
{ {
@ -158,7 +158,10 @@ namespace BizHawk.Client.Common
} }
} }
} }
catch { /*Eat it*/ } catch
{
/*Eat it*/
}
} }
public void joypad_setanalog(LuaTable controls, object controller = null) public void joypad_setanalog(LuaTable controls, object controller = null)
@ -167,13 +170,13 @@ namespace BizHawk.Client.Common
{ {
foreach (var name in controls.Keys) foreach (var name in controls.Keys)
{ {
string theValueStr = controls[name].ToString(); var theValueStr = controls[name].ToString();
if (!String.IsNullOrWhiteSpace(theValueStr)) if (!String.IsNullOrWhiteSpace(theValueStr))
{ {
try try
{ {
float theValue = float.Parse(theValueStr); var theValue = float.Parse(theValueStr);
if (controller == null) if (controller == null)
{ {
Global.StickyXORAdapter.SetFloat(name.ToString(), theValue); Global.StickyXORAdapter.SetFloat(name.ToString(), theValue);

View File

@ -1,15 +1,13 @@
using System; using System;
using LuaInterface; using LuaInterface;
using BizHawk.Emulation.Common;
namespace BizHawk.Client.Common 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
{ {
public MainMemoryLuaLibrary(Lua lua) public MainMemoryLuaLibrary(Lua lua)
: base()
{ {
_lua = lua; _lua = lua;
} }
@ -56,12 +54,12 @@ namespace BizHawk.Client.Common
"write_s32_be", "write_s32_be",
"write_u16_be", "write_u16_be",
"write_u24_be", "write_u24_be",
"write_u32_be", "write_u32_be"
}; };
} }
} }
private Lua _lua; private readonly Lua _lua;
#region Main Memory Library Helpers #region Main Memory Library Helpers
@ -82,7 +80,10 @@ namespace BizHawk.Client.Common
{ {
uint v = 0; uint v = 0;
for (int i = 0; i < size; ++i) for (int i = 0; i < size; ++i)
{
v |= MM_R_U8(addr + i) << 8 * i; v |= MM_R_U8(addr + i) << 8 * i;
}
return v; return v;
} }
@ -95,7 +96,10 @@ namespace BizHawk.Client.Common
{ {
uint v = 0; uint v = 0;
for (int i = 0; i < size; ++i) for (int i = 0; i < size; ++i)
{
v |= MM_R_U8(addr + i) << 8 * (size - 1 - i); v |= MM_R_U8(addr + i) << 8 * (size - 1 - i);
}
return v; return v;
} }
@ -107,7 +111,9 @@ namespace BizHawk.Client.Common
private void MM_W_U_LE(int addr, uint v, int size) private void MM_W_U_LE(int addr, uint v, int size)
{ {
for (int i = 0; i < size; ++i) for (int i = 0; i < size; ++i)
{
MM_W_U8(addr + i, (v >> (8 * i)) & 0xFF); MM_W_U8(addr + i, (v >> (8 * i)) & 0xFF);
}
} }
private void MM_W_S_BE(int addr, int v, int size) private void MM_W_S_BE(int addr, int v, int size)
@ -118,7 +124,9 @@ namespace BizHawk.Client.Common
private void MM_W_U_BE(int addr, uint v, int size) private void MM_W_U_BE(int addr, uint v, int size)
{ {
for (int i = 0; i < size; ++i) for (int i = 0; i < size; ++i)
{
MM_W_U8(addr + i, (v >> (8 * (size - 1 - i))) & 0xFF); MM_W_U8(addr + i, (v >> (8 * (size - 1 - i))) & 0xFF);
}
} }
private uint MM_R_U8(int addr) private uint MM_R_U8(int addr)
@ -149,12 +157,12 @@ namespace BizHawk.Client.Common
int l = LuaInt(length); int l = LuaInt(length);
int addr = LuaInt(address); int addr = LuaInt(address);
int last_addr = l + addr; int last_addr = l + addr;
LuaTable table = _lua.NewTable(); var table = _lua.NewTable();
for (int i = addr; i <= last_addr; i++) for (int i = addr; i <= last_addr; i++)
{ {
string a = String.Format("{0:X2}", i); var a = String.Format("{0:X2}", i);
byte v = Global.Emulator.MemoryDomains.MainMemory.PeekByte(i); var v = Global.Emulator.MemoryDomains.MainMemory.PeekByte(i);
string vs = String.Format("{0:X2}", (int)v); var vs = String.Format("{0:X2}", (int)v);
table[a] = vs; table[a] = vs;
} }
return table; return table;
@ -162,18 +170,18 @@ namespace BizHawk.Client.Common
public float mainmemory_readfloat(object lua_addr, bool bigendian) public float mainmemory_readfloat(object lua_addr, bool bigendian)
{ {
int addr = LuaInt(lua_addr); var addr = LuaInt(lua_addr);
uint val = Global.Emulator.MemoryDomains.MainMemory.PeekDWord(addr, bigendian); uint val = Global.Emulator.MemoryDomains.MainMemory.PeekDWord(addr, bigendian);
byte[] bytes = BitConverter.GetBytes(val); var bytes = BitConverter.GetBytes(val);
float _float = BitConverter.ToSingle(bytes, 0); var _float = BitConverter.ToSingle(bytes, 0);
return _float; return _float;
} }
public void mainmemory_writebyte(object lua_addr, object lua_v) public void mainmemory_writebyte(object lua_addr, object lua_v)
{ {
int addr = LuaInt(lua_addr); var addr = LuaInt(lua_addr);
uint v = LuaUInt(lua_v); var v = LuaUInt(lua_v);
MM_W_U8(addr, v); MM_W_U8(addr, v);
} }
@ -190,9 +198,9 @@ namespace BizHawk.Client.Common
public void mainmemory_writefloat(object lua_addr, object lua_v, bool bigendian) public void mainmemory_writefloat(object lua_addr, object lua_v, bool bigendian)
{ {
int addr = LuaInt(lua_addr); var addr = LuaInt(lua_addr);
float dv = (float)(double)lua_v; var dv = (float)(double)lua_v;
byte[] bytes = BitConverter.GetBytes(dv); var bytes = BitConverter.GetBytes(dv);
uint v = BitConverter.ToUInt32(bytes, 0); uint v = BitConverter.ToUInt32(bytes, 0);
Global.Emulator.MemoryDomains.MainMemory.PokeDWord(addr, v, bigendian); Global.Emulator.MemoryDomains.MainMemory.PokeDWord(addr, v, bigendian);
} }
@ -200,183 +208,183 @@ namespace BizHawk.Client.Common
public int mainmemory_read_s8(object lua_addr) public int mainmemory_read_s8(object lua_addr)
{ {
int addr = LuaInt(lua_addr); var addr = LuaInt(lua_addr);
return (sbyte)MM_R_U8(addr); return (sbyte)MM_R_U8(addr);
} }
public uint mainmemory_read_u8(object lua_addr) public uint mainmemory_read_u8(object lua_addr)
{ {
int addr = LuaInt(lua_addr); var addr = LuaInt(lua_addr);
return MM_R_U8(addr); return MM_R_U8(addr);
} }
public int mainmemory_read_s16_le(object lua_addr) public int mainmemory_read_s16_le(object lua_addr)
{ {
int addr = LuaInt(lua_addr); var addr = LuaInt(lua_addr);
return MM_R_S_LE(addr, 2); return MM_R_S_LE(addr, 2);
} }
public int mainmemory_read_s24_le(object lua_addr) public int mainmemory_read_s24_le(object lua_addr)
{ {
int addr = LuaInt(lua_addr); var addr = LuaInt(lua_addr);
return MM_R_S_LE(addr, 3); return MM_R_S_LE(addr, 3);
} }
public int mainmemory_read_s32_le(object lua_addr) public int mainmemory_read_s32_le(object lua_addr)
{ {
int addr = LuaInt(lua_addr); var addr = LuaInt(lua_addr);
return MM_R_S_LE(addr, 4); return MM_R_S_LE(addr, 4);
} }
public uint mainmemory_read_u16_le(object lua_addr) public uint mainmemory_read_u16_le(object lua_addr)
{ {
int addr = LuaInt(lua_addr); var addr = LuaInt(lua_addr);
return MM_R_U_LE(addr, 2); return MM_R_U_LE(addr, 2);
} }
public uint mainmemory_read_u24_le(object lua_addr) public uint mainmemory_read_u24_le(object lua_addr)
{ {
int addr = LuaInt(lua_addr); var addr = LuaInt(lua_addr);
return MM_R_U_LE(addr, 3); return MM_R_U_LE(addr, 3);
} }
public uint mainmemory_read_u32_le(object lua_addr) public uint mainmemory_read_u32_le(object lua_addr)
{ {
int addr = LuaInt(lua_addr); var addr = LuaInt(lua_addr);
return MM_R_U_LE(addr, 4); return MM_R_U_LE(addr, 4);
} }
public int mainmemory_read_s16_be(object lua_addr) public int mainmemory_read_s16_be(object lua_addr)
{ {
int addr = LuaInt(lua_addr); var addr = LuaInt(lua_addr);
return MM_R_S_BE(addr, 2); return MM_R_S_BE(addr, 2);
} }
public int mainmemory_read_s24_be(object lua_addr) public int mainmemory_read_s24_be(object lua_addr)
{ {
int addr = LuaInt(lua_addr); var addr = LuaInt(lua_addr);
return MM_R_S_BE(addr, 3); return MM_R_S_BE(addr, 3);
} }
public int mainmemory_read_s32_be(object lua_addr) public int mainmemory_read_s32_be(object lua_addr)
{ {
int addr = LuaInt(lua_addr); var addr = LuaInt(lua_addr);
return MM_R_S_BE(addr, 4); return MM_R_S_BE(addr, 4);
} }
public uint mainmemory_read_u16_be(object lua_addr) public uint mainmemory_read_u16_be(object lua_addr)
{ {
int addr = LuaInt(lua_addr); var addr = LuaInt(lua_addr);
return MM_R_U_BE(addr, 2); return MM_R_U_BE(addr, 2);
} }
public uint mainmemory_read_u24_be(object lua_addr) public uint mainmemory_read_u24_be(object lua_addr)
{ {
int addr = LuaInt(lua_addr); var addr = LuaInt(lua_addr);
return MM_R_U_BE(addr, 3); return MM_R_U_BE(addr, 3);
} }
public uint mainmemory_read_u32_be(object lua_addr) public uint mainmemory_read_u32_be(object lua_addr)
{ {
int addr = LuaInt(lua_addr); var addr = LuaInt(lua_addr);
return MM_R_U_BE(addr, 4); return MM_R_U_BE(addr, 4);
} }
public void mainmemory_write_s8(object lua_addr, object lua_v) public void mainmemory_write_s8(object lua_addr, object lua_v)
{ {
int addr = LuaInt(lua_addr); var addr = LuaInt(lua_addr);
int v = LuaInt(lua_v); var v = LuaInt(lua_v);
MM_W_U8(addr, (uint)v); MM_W_U8(addr, (uint)v);
} }
public void mainmemory_write_u8(object lua_addr, object lua_v) public void mainmemory_write_u8(object lua_addr, object lua_v)
{ {
int addr = LuaInt(lua_addr); var addr = LuaInt(lua_addr);
uint v = LuaUInt(lua_v); var v = LuaUInt(lua_v);
MM_W_U8(addr, v); MM_W_U8(addr, v);
} }
public void mainmemory_write_s16_le(object lua_addr, object lua_v) public void mainmemory_write_s16_le(object lua_addr, object lua_v)
{ {
int addr = LuaInt(lua_addr); var addr = LuaInt(lua_addr);
int v = LuaInt(lua_v); var v = LuaInt(lua_v);
MM_W_S_LE(addr, v, 2); MM_W_S_LE(addr, v, 2);
} }
public void mainmemory_write_s24_le(object lua_addr, object lua_v) public void mainmemory_write_s24_le(object lua_addr, object lua_v)
{ {
int addr = LuaInt(lua_addr); var addr = LuaInt(lua_addr);
int v = LuaInt(lua_v); var v = LuaInt(lua_v);
MM_W_S_LE(addr, v, 3); MM_W_S_LE(addr, v, 3);
} }
public void mainmemory_write_s32_le(object lua_addr, object lua_v) public void mainmemory_write_s32_le(object lua_addr, object lua_v)
{ {
int addr = LuaInt(lua_addr); var addr = LuaInt(lua_addr);
int v = LuaInt(lua_v); var v = LuaInt(lua_v);
MM_W_S_LE(addr, v, 4); MM_W_S_LE(addr, v, 4);
} }
public void mainmemory_write_u16_le(object lua_addr, object lua_v) public void mainmemory_write_u16_le(object lua_addr, object lua_v)
{ {
int addr = LuaInt(lua_addr); var addr = LuaInt(lua_addr);
uint v = LuaUInt(lua_v); var v = LuaUInt(lua_v);
MM_W_U_LE(addr, v, 2); MM_W_U_LE(addr, v, 2);
} }
public void mainmemory_write_u24_le(object lua_addr, object lua_v) public void mainmemory_write_u24_le(object lua_addr, object lua_v)
{ {
int addr = LuaInt(lua_addr); var addr = LuaInt(lua_addr);
uint v = LuaUInt(lua_v); var v = LuaUInt(lua_v);
MM_W_U_LE(addr, v, 3); MM_W_U_LE(addr, v, 3);
} }
public void mainmemory_write_u32_le(object lua_addr, object lua_v) public void mainmemory_write_u32_le(object lua_addr, object lua_v)
{ {
int addr = LuaInt(lua_addr); var addr = LuaInt(lua_addr);
uint v = LuaUInt(lua_v); var v = LuaUInt(lua_v);
MM_W_U_LE(addr, v, 4); MM_W_U_LE(addr, v, 4);
} }
public void mainmemory_write_s16_be(object lua_addr, object lua_v) public void mainmemory_write_s16_be(object lua_addr, object lua_v)
{ {
int addr = LuaInt(lua_addr); var addr = LuaInt(lua_addr);
int v = LuaInt(lua_v); var v = LuaInt(lua_v);
MM_W_S_BE(addr, v, 2); MM_W_S_BE(addr, v, 2);
} }
public void mainmemory_write_s24_be(object lua_addr, object lua_v) public void mainmemory_write_s24_be(object lua_addr, object lua_v)
{ {
int addr = LuaInt(lua_addr); var addr = LuaInt(lua_addr);
int v = LuaInt(lua_v); var v = LuaInt(lua_v);
MM_W_S_BE(addr, v, 3); MM_W_S_BE(addr, v, 3);
} }
public void mainmemory_write_s32_be(object lua_addr, object lua_v) public void mainmemory_write_s32_be(object lua_addr, object lua_v)
{ {
int addr = LuaInt(lua_addr); var addr = LuaInt(lua_addr);
int v = LuaInt(lua_v); var v = LuaInt(lua_v);
MM_W_S_BE(addr, v, 4); MM_W_S_BE(addr, v, 4);
} }
public void mainmemory_write_u16_be(object lua_addr, object lua_v) public void mainmemory_write_u16_be(object lua_addr, object lua_v)
{ {
int addr = LuaInt(lua_addr); var addr = LuaInt(lua_addr);
uint v = LuaUInt(lua_v); var v = LuaUInt(lua_v);
MM_W_U_BE(addr, v, 2); MM_W_U_BE(addr, v, 2);
} }
public void mainmemory_write_u24_be(object lua_addr, object lua_v) public void mainmemory_write_u24_be(object lua_addr, object lua_v)
{ {
int addr = LuaInt(lua_addr); var addr = LuaInt(lua_addr);
uint v = LuaUInt(lua_v); var v = LuaUInt(lua_v);
MM_W_U_BE(addr, v, 3); MM_W_U_BE(addr, v, 3);
} }
public void mainmemory_write_u32_be(object lua_addr, object lua_v) public void mainmemory_write_u32_be(object lua_addr, object lua_v)
{ {
int addr = LuaInt(lua_addr); var addr = LuaInt(lua_addr);
uint v = LuaUInt(lua_v); var v = LuaUInt(lua_v);
MM_W_U_BE(addr, v, 4); MM_W_U_BE(addr, v, 4);
} }
} }

View File

@ -1,8 +1,6 @@
using System; using System;
using System.Linq; using System.Linq;
using BizHawk.Emulation.Common;
namespace BizHawk.Client.Common namespace BizHawk.Client.Common
{ {
public class MemoryLuaLibrary : LuaLibraryBase public class MemoryLuaLibrary : LuaLibraryBase
@ -50,12 +48,12 @@ namespace BizHawk.Client.Common
"write_s32_be", "write_s32_be",
"write_u16_be", "write_u16_be",
"write_u24_be", "write_u24_be",
"write_u32_be", "write_u32_be"
}; };
} }
} }
private int _current_memory_domain; //Main memory by default private int _currentMemoryDomain; // Main memory by default
#region Memory Library Helpers #region Memory Library Helpers
@ -121,12 +119,12 @@ namespace BizHawk.Client.Common
private uint M_R_U8(int addr) private uint M_R_U8(int addr)
{ {
return Global.Emulator.MemoryDomains[_current_memory_domain].PeekByte(addr); return Global.Emulator.MemoryDomains[_currentMemoryDomain].PeekByte(addr);
} }
private void M_W_U8(int addr, uint v) private void M_W_U8(int addr, uint v)
{ {
Global.Emulator.MemoryDomains[_current_memory_domain].PokeByte(addr, (byte)v); Global.Emulator.MemoryDomains[_currentMemoryDomain].PokeByte(addr, (byte)v);
} }
#endregion #endregion
@ -138,12 +136,12 @@ namespace BizHawk.Client.Common
public string memory_getcurrentmemorydomain() public string memory_getcurrentmemorydomain()
{ {
return Global.Emulator.MemoryDomains[_current_memory_domain].Name; return Global.Emulator.MemoryDomains[_currentMemoryDomain].Name;
} }
public int memory_getcurrentmemorydomainsize() public int memory_getcurrentmemorydomainsize()
{ {
return Global.Emulator.MemoryDomains[_current_memory_domain].Size; return Global.Emulator.MemoryDomains[_currentMemoryDomain].Size;
} }
public uint memory_readbyte(object lua_addr) public uint memory_readbyte(object lua_addr)
@ -155,7 +153,7 @@ namespace BizHawk.Client.Common
public float memory_readfloat(object lua_addr, bool bigendian) public float memory_readfloat(object lua_addr, bool bigendian)
{ {
int addr = LuaInt(lua_addr); int addr = LuaInt(lua_addr);
uint val = Global.Emulator.MemoryDomains[_current_memory_domain].PeekDWord(addr, bigendian); uint val = Global.Emulator.MemoryDomains[_currentMemoryDomain].PeekDWord(addr, bigendian);
byte[] bytes = BitConverter.GetBytes(val); byte[] bytes = BitConverter.GetBytes(val);
float _float = BitConverter.ToSingle(bytes, 0); float _float = BitConverter.ToSingle(bytes, 0);
@ -175,7 +173,7 @@ namespace BizHawk.Client.Common
float dv = (float)(double)lua_v; float dv = (float)(double)lua_v;
byte[] bytes = BitConverter.GetBytes(dv); byte[] bytes = BitConverter.GetBytes(dv);
uint v = BitConverter.ToUInt32(bytes, 0); uint v = BitConverter.ToUInt32(bytes, 0);
Global.Emulator.MemoryDomains[_current_memory_domain].PokeDWord(addr, v, bigendian); Global.Emulator.MemoryDomains[_currentMemoryDomain].PokeDWord(addr, v, bigendian);
} }
public bool memory_usememorydomain(object lua_input) public bool memory_usememorydomain(object lua_input)
@ -187,7 +185,7 @@ namespace BizHawk.Client.Common
{ {
if (Global.Emulator.MemoryDomains[x].Name == lua_input.ToString()) if (Global.Emulator.MemoryDomains[x].Name == lua_input.ToString())
{ {
_current_memory_domain = x; _currentMemoryDomain = x;
return true; return true;
} }
} }

View File

@ -5,7 +5,6 @@ namespace BizHawk.Client.Common
public class MovieLuaLibrary : LuaLibraryBase public class MovieLuaLibrary : LuaLibraryBase
{ {
public MovieLuaLibrary(Lua lua) public MovieLuaLibrary(Lua lua)
: base()
{ {
_lua = lua; _lua = lua;
} }
@ -27,12 +26,12 @@ namespace BizHawk.Client.Common
"rerecordcount", "rerecordcount",
"setreadonly", "setreadonly",
"setrerecordcounting", "setrerecordcounting",
"stop", "stop"
}; };
} }
} }
private Lua _lua; private readonly Lua _lua;
public static string movie_filename() public static string movie_filename()
{ {
@ -41,14 +40,14 @@ namespace BizHawk.Client.Common
public LuaTable movie_getinput(object frame) public LuaTable movie_getinput(object frame)
{ {
LuaTable input = _lua.NewTable(); var input = _lua.NewTable();
MovieControllerAdapter 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(LuaInt(frame)) Global.MovieSession.Movie.GetInput(LuaInt(frame))
); );
foreach (string button in m.Type.BoolButtons) foreach (var button in m.Type.BoolButtons)
{ {
input[button] = m[button]; input[button] = m[button];
} }
@ -103,14 +102,12 @@ namespace BizHawk.Client.Common
public static void movie_setreadonly(object lua_input) public static void movie_setreadonly(object lua_input)
{ {
Global.MovieSession.ReadOnly = (lua_input.ToString().ToUpper() == "TRUE" Global.MovieSession.ReadOnly = lua_input.ToString().ToUpper() == "TRUE" || lua_input.ToString() == "1";
|| lua_input.ToString() == "1");
} }
public static void movie_setrerecordcounting(object lua_input) public static void movie_setrerecordcounting(object lua_input)
{ {
Global.MovieSession.Movie.IsCountingRerecords Global.MovieSession.Movie.IsCountingRerecords = lua_input.ToString().ToUpper() == "TRUE" || lua_input.ToString() == "1";
= (lua_input.ToString().ToUpper() == "TRUE" || lua_input.ToString() == "1");
} }
public static void movie_stop() public static void movie_stop()

View File

@ -1,5 +1,4 @@
using System; using System.Linq;
using System.Linq;
using BizHawk.Emulation.Cores.Nintendo.NES; using BizHawk.Emulation.Cores.Nintendo.NES;
namespace BizHawk.Client.Common namespace BizHawk.Client.Common
@ -35,7 +34,7 @@ namespace BizHawk.Client.Common
if (Global.Emulator is NES) if (Global.Emulator is NES)
{ {
var decoder = new NESGameGenieDecoder(code); var decoder = new NESGameGenieDecoder(code);
Watch watch = Watch.GenerateWatch( var watch = Watch.GenerateWatch(
Global.Emulator.MemoryDomains[1], Global.Emulator.MemoryDomains[1],
decoder.Address, decoder.Address,
Watch.WatchSize.Byte, Watch.WatchSize.Byte,
@ -54,7 +53,6 @@ namespace BizHawk.Client.Common
// these methods are awkward. perhaps with the new core config system, one could // these methods are awkward. perhaps with the new core config system, one could
// automatically bring out all of the settings to a lua table, with names. that // automatically bring out all of the settings to a lua table, with names. that
// would be completely arbitrary and would remove the whole requirement for this mess // would be completely arbitrary and would remove the whole requirement for this mess
public static bool nes_getallowmorethaneightsprites() public static bool nes_getallowmorethaneightsprites()
{ {
return ((NES.NESSettings)Global.Emulator.GetSettings()).AllowMoreThanEightSprites; return ((NES.NESSettings)Global.Emulator.GetSettings()).AllowMoreThanEightSprites;
@ -155,8 +153,8 @@ namespace BizHawk.Client.Common
{ {
if (Global.Emulator is NES) if (Global.Emulator is NES)
{ {
int first = LuaInt(top); var first = LuaInt(top);
int last = LuaInt(bottom); var last = LuaInt(bottom);
if (first > 127) if (first > 127)
{ {
first = 127; first = 127;

View File

@ -26,7 +26,7 @@ namespace BizHawk.Client.Common
"setlayer_obj_1", "setlayer_obj_1",
"setlayer_obj_2", "setlayer_obj_2",
"setlayer_obj_3", "setlayer_obj_3",
"setlayer_obj_4", "setlayer_obj_4"
}; };
} }
} }

View File

@ -3,7 +3,6 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
namespace BizHawk.Client.Common namespace BizHawk.Client.Common
{ {
public interface ILuaDocumentation public interface ILuaDocumentation
@ -17,7 +16,7 @@ namespace BizHawk.Client.Common
public void Add(string method_lib, string method_name, System.Reflection.MethodInfo method) public void Add(string method_lib, string method_name, System.Reflection.MethodInfo method)
{ {
LibraryFunction f = new LibraryFunction(method_lib, method_name, method); var f = new LibraryFunction(method_lib, method_name, method);
FunctionList.Add(f); FunctionList.Add(f);
} }
@ -33,8 +32,8 @@ namespace BizHawk.Client.Common
public List<string> GetLibraryList() public List<string> GetLibraryList()
{ {
HashSet<string> libs = new HashSet<string>(); var libs = new HashSet<string>();
foreach (LibraryFunction function in FunctionList) foreach (var function in FunctionList)
{ {
libs.Add(function.Library); libs.Add(function.Library);
} }
@ -53,11 +52,12 @@ namespace BizHawk.Client.Common
{ {
Library = method_lib; Library = method_lib;
Name = method_name; Name = method_name;
System.Reflection.ParameterInfo[] info = method.GetParameters(); var info = method.GetParameters();
foreach (System.Reflection.ParameterInfo p in info) foreach (var p in info)
{ {
Parameters.Add(p.ToString()); Parameters.Add(p.ToString());
} }
return_type = method.ReturnType.ToString(); return_type = method.ReturnType.ToString();
} }
@ -70,17 +70,18 @@ namespace BizHawk.Client.Common
{ {
get get
{ {
StringBuilder list = new StringBuilder(); var list = new StringBuilder();
list.Append('('); list.Append('(');
for (int i = 0; i < Parameters.Count; i++) for (int i = 0; i < Parameters.Count; i++)
{ {
string param = Parameters[i].Replace("System", "").Replace("Object", "").Replace(" ", "").Replace(".", "").Replace("LuaInterface", ""); var param = Parameters[i].Replace("System", "").Replace("Object", "").Replace(" ", "").Replace(".", "").Replace("LuaInterface", "");
list.Append(param); list.Append(param);
if (i < Parameters.Count - 1) if (i < Parameters.Count - 1)
{ {
list.Append(','); list.Append(',');
} }
} }
list.Append(')'); list.Append(')');
return list.ToString(); return list.ToString();
} }

View File

@ -29,9 +29,12 @@ namespace BizHawk.Client.Common
Path = path; Path = path;
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 directory_info = new FileInfo(path).Directory;
if (directory_info != null) CurrentDirectory = directory_info.FullName; if (directory_info != null)
{
CurrentDirectory = directory_info.FullName;
}
} }
public LuaFile(bool isSeparator) public LuaFile(bool isSeparator)

View File

@ -1,20 +1,19 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using System.Text; using System.Text;
namespace BizHawk.Client.Common namespace BizHawk.Client.Common
{ {
public class LuaFileList : List<LuaFile> public class LuaFileList : List<LuaFile>
{ {
public LuaFileList() : base() { } internal LuaFileList() { }
private string _filename = String.Empty; private string _filename = String.Empty;
private bool _changes; private bool _changes;
public Action ChangedCallback; public Action ChangedCallback { get; set; }
public Action LoadCallback; public Action LoadCallback { get; set; }
public void StopAllScripts() public void StopAllScripts()
{ {
@ -27,6 +26,7 @@ namespace BizHawk.Client.Common
{ {
return _changes; return _changes;
} }
set set
{ {
_changes = value; _changes = value;
@ -34,17 +34,23 @@ namespace BizHawk.Client.Common
{ {
ChangedCallback(); ChangedCallback();
} }
} }
} }
public string Filename public string Filename
{ {
get { return _filename; } get
set { _filename = (value ?? String.Empty); } {
return _filename;
}
set
{
_filename = value ?? String.Empty;
}
} }
new public void Clear() public new void Clear()
{ {
StopAllScripts(); StopAllScripts();
_filename = String.Empty; _filename = String.Empty;
@ -52,25 +58,25 @@ namespace BizHawk.Client.Common
base.Clear(); base.Clear();
} }
new public void Add(LuaFile item) public new void Add(LuaFile item)
{ {
Changes = true; Changes = true;
base.Add(item); base.Add(item);
} }
new public void Insert(int index, LuaFile item) public new void Insert(int index, LuaFile item)
{ {
Changes = true; Changes = true;
base.Insert(index, item); base.Insert(index, item);
} }
new public bool Remove(LuaFile item) public new bool Remove(LuaFile item)
{ {
Changes = true; Changes = true;
return base.Remove(item); return base.Remove(item);
} }
new public int RemoveAll(Predicate<LuaFile> match) public new int RemoveAll(Predicate<LuaFile> match)
{ {
return base.RemoveAll(match); return base.RemoveAll(match);
} }
@ -109,6 +115,7 @@ namespace BizHawk.Client.Common
{ {
LoadCallback(); LoadCallback();
} }
return true; return true;
} }
else else
@ -138,6 +145,7 @@ namespace BizHawk.Client.Common
.Append(file.Path) .Append(file.Path)
.AppendLine(); .AppendLine();
} }
sw.Write(sb.ToString()); sw.Write(sb.ToString());
} }

View File

@ -9,7 +9,7 @@ namespace BizHawk.Client.Common
{ {
get get
{ {
return this.FirstOrDefault(x => x.Guid.ToString() == guid) ?? null; return this.FirstOrDefault(x => x.Guid.ToString() == guid);
} }
} }

View File

@ -12,9 +12,9 @@ namespace BizHawk.Client.Common
public virtual void LuaRegister(Lua lua, ILuaDocumentation docs = null) public virtual void LuaRegister(Lua lua, ILuaDocumentation docs = null)
{ {
lua.NewTable(Name); lua.NewTable(Name);
foreach (string methodName in Functions) foreach (var methodName in Functions)
{ {
string func = Name + "." + methodName; var func = Name + "." + methodName;
var method = GetType().GetMethod(Name + "_" + methodName); var method = GetType().GetMethod(Name + "_" + methodName);
lua.RegisterFunction(func, this, method); lua.RegisterFunction(func, this, method);
@ -25,14 +25,14 @@ namespace BizHawk.Client.Common
} }
} }
protected static int LuaInt(object lua_arg) protected static int LuaInt(object luaArg)
{ {
return Convert.ToInt32((double)lua_arg); return Convert.ToInt32((double)luaArg);
} }
protected static uint LuaUInt(object lua_arg) protected static uint LuaUInt(object luaArg)
{ {
return Convert.ToUInt32((double)lua_arg); return Convert.ToUInt32((double)luaArg);
} }
/// <summary> /// <summary>
@ -40,16 +40,20 @@ namespace BizHawk.Client.Common
/// So, if you want to support variable arguments, declare them as optional and pass /// So, if you want to support variable arguments, declare them as optional and pass
/// them to this method. /// them to this method.
/// </summary> /// </summary>
/// <param name="lua_args"></param> protected static object[] LuaVarArgs(params object[] luaArgs)
/// <returns></returns>
protected static object[] LuaVarArgs(params object[] lua_args)
{ {
int n = lua_args.Length; int n = luaArgs.Length;
int trim = 0; int trim = 0;
for (int i = n - 1; i >= 0; --i) for (int i = n - 1; i >= 0; --i)
if (lua_args[i] == null) ++trim; {
object[] lua_result = new object[n - trim]; if (luaArgs[i] == null)
Array.Copy(lua_args, lua_result, n - trim); {
++trim;
}
}
var lua_result = new object[n - trim];
Array.Copy(luaArgs, lua_result, n - trim);
return lua_result; return lua_result;
} }
} }

View File

@ -9,6 +9,7 @@ namespace BizHawk.Client.Common
private readonly string _name; private readonly string _name;
private readonly string _event; private readonly string _event;
private readonly Action _action; private readonly Action _action;
public NamedLuaFunction(LuaFunction function, string theevent, Action<string> logCallback, Lua lua, string name = null) public NamedLuaFunction(LuaFunction function, string theevent, Action<string> logCallback, Lua lua, string name = null)
{ {
_function = function; _function = function;
@ -32,7 +33,6 @@ namespace BizHawk.Client.Common
ex.Message ex.Message
); );
} }
}; };
} }

View File

@ -1,5 +1,4 @@
using System; using System.Linq;
using System.Linq;
namespace BizHawk.Client.Common namespace BizHawk.Client.Common
{ {