Replace dynamic type with Object in API

Using the dynamic type here is no better than object because the values are
primitive types -- more likely to be cast/typechecked than used as a receiver
param for a method call.
This commit is contained in:
YoshiRulz 2020-04-13 17:04:20 +10:00
parent 46a7e64911
commit e226ff102b
No known key found for this signature in database
GPG Key ID: C4DE31C245353FB7
14 changed files with 22 additions and 46 deletions

View File

@ -16,12 +16,12 @@ namespace BizHawk.Client.Common
private readonly Action<string> LogCallback; private readonly Action<string> LogCallback;
public IDictionary<string, dynamic> Get(int? controller = null) public IDictionary<string, object> Get(int? controller = null)
{ {
return Global.InputManager.AutofireStickyXorAdapter.ToDictionary(controller); return Global.InputManager.AutofireStickyXorAdapter.ToDictionary(controller);
} }
public IDictionary<string, dynamic> GetImmediate(int? controller = null) public IDictionary<string, object> GetImmediate(int? controller = null)
{ {
return Global.InputManager.ActiveController.ToDictionary(controller); return Global.InputManager.ActiveController.ToDictionary(controller);
} }

View File

@ -21,7 +21,7 @@ namespace BizHawk.Client.Common
public bool StartsFromSaveram() => Global.MovieSession.Movie.IsActive() && Global.MovieSession.Movie.StartsFromSaveRam; public bool StartsFromSaveram() => Global.MovieSession.Movie.IsActive() && Global.MovieSession.Movie.StartsFromSaveRam;
public IDictionary<string, dynamic> GetInput(int frame, int? controller = null) public IDictionary<string, object> GetInput(int frame, int? controller = null)
{ {
if (Global.MovieSession.Movie.NotActive()) if (Global.MovieSession.Movie.NotActive())
{ {

View File

@ -63,11 +63,11 @@ namespace BizHawk.Client.Common
return result; return result;
} }
public dynamic ReadCommand(string query = null) public object ReadCommand(string query = null)
{ {
if (string.IsNullOrWhiteSpace(query)) return "query is empty"; if (string.IsNullOrWhiteSpace(query)) return "query is empty";
if (_dbConnection == null) return "Database not open."; if (_dbConnection == null) return "Database not open.";
dynamic result; object result;
try try
{ {
_dbConnection.Open(); _dbConnection.Open();

View File

@ -5,6 +5,6 @@ namespace BizHawk.Client.Common
public interface IInput : IExternalApi public interface IInput : IExternalApi
{ {
Dictionary<string, bool> Get(); Dictionary<string, bool> Get();
Dictionary<string, dynamic> GetMouse(); Dictionary<string, object> GetMouse();
} }
} }

View File

@ -1,4 +1,5 @@
using System.Collections.Generic; using System.Collections.Generic;
namespace BizHawk.Client.Common namespace BizHawk.Client.Common
{ {
public interface IInputMovie : IExternalApi public interface IInputMovie : IExternalApi
@ -6,7 +7,7 @@ namespace BizHawk.Client.Common
bool StartsFromSavestate(); bool StartsFromSavestate();
bool StartsFromSaveram(); bool StartsFromSaveram();
string Filename(); string Filename();
IDictionary<string, dynamic> GetInput(int frame, int? controller = null); IDictionary<string, object> GetInput(int frame, int? controller = null);
string GetInputAsMnemonic(int frame); string GetInputAsMnemonic(int frame);
bool GetReadOnly(); bool GetReadOnly();
ulong GetRerecordCount(); ulong GetRerecordCount();

View File

@ -4,8 +4,8 @@ namespace BizHawk.Client.Common
{ {
public interface IJoypad : IExternalApi public interface IJoypad : IExternalApi
{ {
IDictionary<string, dynamic> Get(int? controller = null); IDictionary<string, object> Get(int? controller = null);
IDictionary<string, dynamic> GetImmediate(int? controller = null); IDictionary<string, object> GetImmediate(int? controller = null);
void SetFromMnemonicStr(string inputLogEntry); void SetFromMnemonicStr(string inputLogEntry);
void Set(Dictionary<string, bool> buttons, int? controller = null); void Set(Dictionary<string, bool> buttons, int? controller = null);
void Set(string button, bool? state = null, int? controller = null); void Set(string button, bool? state = null, int? controller = null);

View File

@ -5,6 +5,6 @@
string CreateDatabase(string name); string CreateDatabase(string name);
string OpenDatabase(string name); string OpenDatabase(string name);
string WriteCommand(string query = ""); string WriteCommand(string query = "");
dynamic ReadCommand(string query = ""); object ReadCommand(string query = "");
} }
} }

View File

@ -29,12 +29,7 @@ namespace BizHawk.Client.Common
[LuaMethodExample("local nljoyget = joypad.getimmediate( );")] [LuaMethodExample("local nljoyget = joypad.getimmediate( );")]
[LuaMethod("getimmediate", "returns a lua table of any controller buttons currently pressed by the user")] [LuaMethod("getimmediate", "returns a lua table of any controller buttons currently pressed by the user")]
public LuaTable GetImmediate(int? controller = null) public LuaTable GetImmediate(int? controller = null) => APIs.Joypad.GetImmediate(controller).ToLuaTable(Lua);
{
return APIs.Joypad
.GetImmediate(controller)
.ToLuaTable(Lua);
}
[LuaMethodExample("joypad.setfrommnemonicstr( \"| 0, 0, 0, 100,...R..B....|\" );")] [LuaMethodExample("joypad.setfrommnemonicstr( \"| 0, 0, 0, 100,...R..B....|\" );")]
[LuaMethod("setfrommnemonicstr", "sets the given buttons to their provided values for the current frame, string will be interpretted the same way an entry from a movie input log would be")] [LuaMethod("setfrommnemonicstr", "sets the given buttons to their provided values for the current frame, string will be interpretted the same way an entry from a movie input log would be")]

View File

@ -29,12 +29,7 @@ namespace BizHawk.Client.Common
[LuaMethodExample("local nlmovget = movie.getinput( 500 );")] [LuaMethodExample("local nlmovget = movie.getinput( 500 );")]
[LuaMethod("getinput", "Returns a table of buttons pressed on a given frame of the loaded movie")] [LuaMethod("getinput", "Returns a table of buttons pressed on a given frame of the loaded movie")]
public LuaTable GetInput(int frame, int? controller = null) public LuaTable GetInput(int frame, int? controller = null) => APIs.Movie.GetInput(frame, controller).ToLuaTable(Lua);
{
return APIs.Movie
.GetInput(frame, controller)
.ToLuaTable(Lua);
}
[LuaMethodExample("local stmovget = movie.getinputasmnemonic( 500 );")] [LuaMethodExample("local stmovget = movie.getinputasmnemonic( 500 );")]
[LuaMethod("getinputasmnemonic", "Returns the input of a given frame of the loaded movie in a raw inputlog string")] [LuaMethod("getinputasmnemonic", "Returns the input of a given frame of the loaded movie in a raw inputlog string")]

View File

@ -34,15 +34,10 @@ namespace BizHawk.Client.Common
[LuaMethodExample("local obSQLrea = SQL.readcommand( \"SELECT * FROM eg_tab WHERE eg_tab_id = 1;\" );")] [LuaMethodExample("local obSQLrea = SQL.readcommand( \"SELECT * FROM eg_tab WHERE eg_tab_id = 1;\" );")]
[LuaMethod("readcommand", "Run a SQLite read command which includes Select. Returns all rows into a LuaTable." + [LuaMethod("readcommand", "Run a SQLite read command which includes Select. Returns all rows into a LuaTable." +
"Ex: select * from rewards")] "Ex: select * from rewards")]
public dynamic ReadCommand(string query = "") public object ReadCommand(string query = "")
{ {
var result = APIs.Sql.ReadCommand(query); var result = APIs.Sql.ReadCommand(query);
if (result is Dictionary<string, object> dict) return result is Dictionary<string, object> dict ? dict.ToLuaTable(Lua) : result;
{
return dict.ToLuaTable(Lua);
}
return result;
} }
} }
} }

View File

@ -86,7 +86,7 @@ namespace BizHawk.Client.EmuHawk
#region Helpers #region Helpers
private static void InvokeMainFormMethod(string name, dynamic[] paramList = null) private static void InvokeMainFormMethod(string name, object[] paramList = null)
{ {
List<Type> typeList = new List<Type>(); List<Type> typeList = new List<Type>();
MethodInfo method; MethodInfo method;

View File

@ -15,9 +15,9 @@ namespace BizHawk.Client.EmuHawk
return buttons; return buttons;
} }
public Dictionary<string, dynamic> GetMouse() public Dictionary<string, object> GetMouse()
{ {
var buttons = new Dictionary<string, dynamic>(); var buttons = new Dictionary<string, object>();
// TODO - need to specify whether in "emu" or "native" coordinate space. // TODO - need to specify whether in "emu" or "native" coordinate space.
var p = GlobalWin.DisplayManager.UntransformPoint(Control.MousePosition); var p = GlobalWin.DisplayManager.UntransformPoint(Control.MousePosition);
buttons["X"] = p.X; buttons["X"] = p.X;

View File

@ -18,20 +18,10 @@ namespace BizHawk.Client.EmuHawk
[LuaMethodExample("local nlinpget = input.get( );")] [LuaMethodExample("local nlinpget = input.get( );")]
[LuaMethod("get", "Returns a lua table of all the buttons the user is currently pressing on their keyboard and gamepads\nAll buttons that are pressed have their key values set to true; all others remain nil.")] [LuaMethod("get", "Returns a lua table of all the buttons the user is currently pressing on their keyboard and gamepads\nAll buttons that are pressed have their key values set to true; all others remain nil.")]
public LuaTable Get() public LuaTable Get() => APIs.Input.Get().ToLuaTable(Lua);
{
return APIs.Input
.Get()
.ToLuaTable(Lua);
}
[LuaMethodExample("local nlinpget = input.getmouse( );")] [LuaMethodExample("local nlinpget = input.getmouse( );")]
[LuaMethod("getmouse", "Returns a lua table of the mouse X/Y coordinates and button states. Table keys are X, Y, Left, Middle, Right, XButton1, XButton2, Wheel.")] [LuaMethod("getmouse", "Returns a lua table of the mouse X/Y coordinates and button states. Table keys are X, Y, Left, Middle, Right, XButton1, XButton2, Wheel.")]
public LuaTable GetMouse() public LuaTable GetMouse() => APIs.Input.GetMouse().ToLuaTable(Lua);
{
return APIs.Input
.GetMouse()
.ToLuaTable(Lua);
}
} }
} }

View File

@ -355,9 +355,9 @@ namespace BizHawk.Emulation.Common
return buttons; return buttons;
} }
public static IDictionary<string, dynamic> ToDictionary(this IController controller, int? controllerNum = null) public static IDictionary<string, object> ToDictionary(this IController controller, int? controllerNum = null)
{ {
var buttons = new Dictionary<string, dynamic>(); var buttons = new Dictionary<string, object>();
foreach (var button in controller.Definition.BoolButtons) foreach (var button in controller.Definition.BoolButtons)
{ {