From c9c23296514ee883d215af0acf34173cb057248e Mon Sep 17 00:00:00 2001 From: adelikat Date: Tue, 20 May 2014 15:39:43 +0000 Subject: [PATCH] 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 --- BizHawk.Client.EmuHawk/LogWindow.cs | 6 ++- BizHawk.Client.EmuHawk/movie/RecordMovie.cs | 49 ++++++--------------- BizHawk.Common/ReflectionUtil.cs | 29 ++++++++++-- 3 files changed, 43 insertions(+), 41 deletions(-) diff --git a/BizHawk.Client.EmuHawk/LogWindow.cs b/BizHawk.Client.EmuHawk/LogWindow.cs index 67093d5034..c1d726ec35 100644 --- a/BizHawk.Client.EmuHawk/LogWindow.cs +++ b/BizHawk.Client.EmuHawk/LogWindow.cs @@ -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); diff --git a/BizHawk.Client.EmuHawk/movie/RecordMovie.cs b/BizHawk.Client.EmuHawk/movie/RecordMovie.cs index b7b3187063..8d91872771 100644 --- a/BizHawk.Client.EmuHawk/movie/RecordMovie.cs +++ b/BizHawk.Client.EmuHawk/movie/RecordMovie.cs @@ -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 diff --git a/BizHawk.Common/ReflectionUtil.cs b/BizHawk.Common/ReflectionUtil.cs index 834025b6e0..88f3521f41 100644 --- a/BizHawk.Common/ReflectionUtil.cs +++ b/BizHawk.Common/ReflectionUtil.cs @@ -4,7 +4,7 @@ using System.Linq; using System.Reflection; using System.Text; -namespace BizHawk.Common +namespace BizHawk.Common.ReflectionExtensions { /// /// Reflection based helper methods @@ -15,7 +15,7 @@ namespace BizHawk.Common /// Takes an object and determines if it has methodName as a public method /// /// Returns whether or not the obj both contains the method name and the method is public - 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 /// - 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; + } } }