Record Movie - refactor to check if Global.Emulator has a DisplayType property defined, and if so, use that to populate the PAL header, rather than a bunch of ifs on core type. Refactored ReflectionUtils to be Extension methods in their own namespace, added methods for checking and getting values from a public property

This commit is contained in:
adelikat 2014-05-20 15:39:43 +00:00
parent 6e4af65b99
commit c9c2329651
3 changed files with 43 additions and 41 deletions

View File

@ -9,6 +9,8 @@ using BizHawk.Common;
using BizHawk.Client.Common;
using BizHawk.Emulation.Common;
using BizHawk.Common.ReflectionExtensions;
//todo - perks - pause, copy to clipboard, backlog length limiting
namespace BizHawk.Client.EmuHawk
@ -136,7 +138,7 @@ namespace BizHawk.Client.EmuHawk
private void HideShowGameDbButton()
{
AddToGameDbBtn.Visible = ReflectionUtil.HasExposedMethod(Global.Emulator, "GenerateGameDbEntry")
AddToGameDbBtn.Visible = Global.Emulator.HasExposedMethod("GenerateGameDbEntry")
&& (Global.Game.Status == RomStatus.Unknown || Global.Game.Status == RomStatus.NotInDatabase);
}
@ -146,7 +148,7 @@ namespace BizHawk.Client.EmuHawk
var result = picker.ShowDialog();
if (result == DialogResult.OK)
{
var entryObj = (CompactGameInfo)ReflectionUtil.InvokeMethod(Global.Emulator, "GenerateGameDbEntry", null);
var entryObj = (CompactGameInfo)Global.Emulator.InvokeMethod("GenerateGameDbEntry", null);
var userDb = Path.Combine(PathManager.GetExeDirectoryAbsolute(), "gamedb", "gamedb_user.txt");
Global.Game.Status = entryObj.Status = picker.PickedStatus;
Database.SaveDatabaseEntry(userDb, entryObj);

View File

@ -1,8 +1,12 @@
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
using Newtonsoft.Json;
using BizHawk.Common.ReflectionExtensions;
using BizHawk.Client.Common;
using BizHawk.Emulation.Common;
using BizHawk.Emulation.Cores.ColecoVision;
@ -13,10 +17,6 @@ using BizHawk.Emulation.Cores.Nintendo.SNES;
using BizHawk.Emulation.Cores.Sega.MasterSystem;
using BizHawk.Emulation.Cores.Consoles.Sega.gpgx;
using System.Reflection;
using Newtonsoft.Json;
namespace BizHawk.Client.EmuHawk
{
public partial class RecordMovie : Form
@ -124,41 +124,18 @@ namespace BizHawk.Client.EmuHawk
_movieToRecord.Header[HeaderKeys.BOARDNAME] = Global.Emulator.BoardName;
}
if (Global.Emulator.HasPublicProperty("DisplayType"))
{
var region = Global.Emulator.GetPropertyValue("DisplayType");
if ((DisplayType)region == DisplayType.PAL)
{
_movieToRecord.Header[HeaderKeys.PAL] = "1";
}
}
if (Global.Emulator is LibsnesCore)
{
_movieToRecord.Header[HeaderKeys.SGB] = (Global.Emulator as LibsnesCore).IsSGB.ToString();
if ((Global.Emulator as LibsnesCore).DisplayType == DisplayType.PAL)
{
_movieToRecord.Header[HeaderKeys.PAL] = "1";
}
}
else if (Global.Emulator is SMS)
{
if ((Global.Emulator as SMS).DisplayType == DisplayType.PAL)
{
_movieToRecord.Header[HeaderKeys.PAL] = "1";
}
}
else if (Global.Emulator is NES)
{
if ((Global.Emulator as NES).DisplayType == DisplayType.PAL)
{
_movieToRecord.Header[HeaderKeys.PAL] = "1";
}
}
else if (Global.Emulator is N64)
{
if ((Global.Emulator as N64).DisplayType == DisplayType.PAL)
{
_movieToRecord.Header[HeaderKeys.PAL] = "1";
}
}
else if (Global.Emulator is GPGX)
{
if ((Global.Emulator as GPGX).DisplayType == DisplayType.PAL)
{
_movieToRecord.Header[HeaderKeys.PAL] = "1";
}
}
_movieToRecord.Header[HeaderKeys.CORE] = ((CoreAttributes)Attribute

View File

@ -4,7 +4,7 @@ using System.Linq;
using System.Reflection;
using System.Text;
namespace BizHawk.Common
namespace BizHawk.Common.ReflectionExtensions
{
/// <summary>
/// Reflection based helper methods
@ -15,7 +15,7 @@ namespace BizHawk.Common
/// Takes an object and determines if it has methodName as a public method
/// </summary>
/// <returns>Returns whether or not the obj both contains the method name and the method is public</returns>
public static bool HasExposedMethod(object obj, string methodName)
public static bool HasExposedMethod(this object obj, string methodName)
{
var method = obj.GetType().GetMethod(methodName);
@ -35,7 +35,7 @@ namespace BizHawk.Common
/// If the method returns void, the return value is null
/// If the method does not exist or is not public, it returns null
/// </returns>
public static object InvokeMethod(object obj, string methodName, object[] args)
public static object InvokeMethod(this object obj, string methodName, object[] args)
{
var method = obj.GetType().GetMethod(methodName);
if (method != null && method.IsPublic)
@ -45,5 +45,28 @@ namespace BizHawk.Common
return null;
}
public static bool HasPublicProperty(this object obj, string propertyName)
{
var property = obj.GetType().GetProperty(propertyName);
if (property != null)
{
return property.CanRead;
}
return false;
}
public static object GetPropertyValue(this object obj, string propertyName)
{
var property = obj.GetType().GetProperty(propertyName);
if (property != null && property.CanRead)
{
return property.GetValue(obj, null);
}
return null;
}
}
}