From ebe001b52655a18cf5dedd39b4aaa58e3350fc25 Mon Sep 17 00:00:00 2001 From: adelikat Date: Thu, 27 Apr 2017 10:55:22 -0500 Subject: [PATCH] Cleanup FirmwareManager, and some other Emulation.Common nitpicks --- BizHawk.Client.Common/FirmwareManager.cs | 10 +- .../config/FirmwaresConfig.cs | 44 +++---- .../BizInvoke/BizInvoker.cs | 43 +++++-- .../BizInvoke/DynamicLibraryImportResolver.cs | 6 +- .../Database/FirmwareDatabase.cs | 120 +++++++++--------- 5 files changed, 120 insertions(+), 103 deletions(-) diff --git a/BizHawk.Client.Common/FirmwareManager.cs b/BizHawk.Client.Common/FirmwareManager.cs index c052126e7a..87dee28277 100644 --- a/BizHawk.Client.Common/FirmwareManager.cs +++ b/BizHawk.Client.Common/FirmwareManager.cs @@ -129,7 +129,7 @@ namespace BizHawk.Client.Common HashSet sizes = new HashSet(); foreach (var ff in FirmwareDatabase.FirmwareFiles) { - sizes.Add(ff.size); + sizes.Add(ff.Size); } using (var reader = new RealFirmwareReader()) @@ -172,13 +172,13 @@ namespace BizHawk.Client.Common var fr1 = fr; var options = from fo in FirmwareDatabase.FirmwareOptions - where fo.systemId == fr1.systemId && fo.firmwareId == fr1.firmwareId && fo.IsAcceptableOrIdeal + where fo.SystemId == fr1.SystemId && fo.FirmwareId == fr1.FirmwareId && fo.IsAcceptableOrIdeal select fo; // try each option foreach (var fo in options) { - var hash = fo.hash; + var hash = fo.Hash; // did we find this firmware? if (reader.dict.ContainsKey(hash)) @@ -189,7 +189,7 @@ namespace BizHawk.Client.Common FilePath = reader.dict[hash].FileInfo.FullName, KnownFirmwareFile = FirmwareDatabase.FirmwareFilesByHash[hash], Hash = hash, - Size = fo.size + Size = fo.Size }; _resolutionDictionary[fr] = ri; goto DONE_FIRMWARE; @@ -243,7 +243,7 @@ namespace BizHawk.Client.Common // if the known firmware file is for a different firmware, flag it so we can show a warning var option = (from fo in FirmwareDatabase.FirmwareOptions - where fo.hash == rff.Hash && fo.ConfigKey != fr.ConfigKey + where fo.Hash == rff.Hash && fo.ConfigKey != fr.ConfigKey select fr).FirstOrDefault(); if (option != null) diff --git a/BizHawk.Client.EmuHawk/config/FirmwaresConfig.cs b/BizHawk.Client.EmuHawk/config/FirmwaresConfig.cs index b49bffb2e8..af6f56d3f6 100644 --- a/BizHawk.Client.EmuHawk/config/FirmwaresConfig.cs +++ b/BizHawk.Client.EmuHawk/config/FirmwaresConfig.cs @@ -139,9 +139,9 @@ namespace BizHawk.Client.EmuHawk lvi.UseItemStyleForSubItems = false; lvi.ImageIndex = idUnsure; lvi.ToolTipText = null; - lvi.SubItems.Add(fr.systemId); - lvi.SubItems.Add(fr.firmwareId); - lvi.SubItems.Add(fr.descr); + lvi.SubItems.Add(fr.SystemId); + lvi.SubItems.Add(fr.FirmwareId); + lvi.SubItems.Add(fr.Descr); lvi.SubItems.Add(""); //resolved with lvi.SubItems.Add(""); //location lvi.SubItems.Add(""); //size @@ -151,16 +151,16 @@ namespace BizHawk.Client.EmuHawk lvFirmwares.Items.Add(lvi); //build the groups in the listview as we go: - if (!groups.ContainsKey(fr.systemId)) + if (!groups.ContainsKey(fr.SystemId)) { string name; - if (!SystemGroupNames.TryGetValue(fr.systemId, out name)) + if (!SystemGroupNames.TryGetValue(fr.SystemId, out name)) name = "FIX ME (FirmwaresConfig.cs)"; - lvFirmwares.Groups.Add(fr.systemId, name); + lvFirmwares.Groups.Add(fr.SystemId, name); var lvg = lvFirmwares.Groups[lvFirmwares.Groups.Count - 1]; - groups[fr.systemId] = lvg; + groups[fr.SystemId] = lvg; } - lvi.Group = groups[fr.systemId]; + lvi.Group = groups[fr.SystemId]; } //now that we have some items in the listview, we can size some columns to sensible widths @@ -262,7 +262,7 @@ namespace BizHawk.Client.EmuHawk { lvi.ImageIndex = idOk; lvi.ToolTipText = "Good!"; - lvi.SubItems[4].Text = ri.KnownFirmwareFile.descr; + lvi.SubItems[4].Text = ri.KnownFirmwareFile.Description; } //bolden the item if necessary @@ -314,7 +314,7 @@ namespace BizHawk.Client.EmuHawk if (ri.KnownFirmwareFile == null) continue; if (ri.UserSpecified) continue; - string fpTarget = PathManager.StandardFirmwareName(ri.KnownFirmwareFile.recommendedName); + string fpTarget = PathManager.StandardFirmwareName(ri.KnownFirmwareFile.RecommendedName); string fpSource = ri.FilePath; try @@ -395,11 +395,11 @@ namespace BizHawk.Client.EmuHawk //get all options for this firmware (in order) var options = from fo in FirmwareDatabase.FirmwareOptions - where fo.systemId == fr.systemId && fo.firmwareId == fr.firmwareId + where fo.SystemId == fr.SystemId && fo.FirmwareId == fr.FirmwareId select fo; FirmwaresConfigInfo fciDialog = new FirmwaresConfigInfo(); - fciDialog.lblFirmware.Text = string.Format("{0} : {1} ({2})", fr.systemId, fr.firmwareId, fr.descr); + fciDialog.lblFirmware.Text = string.Format("{0} : {1} ({2})", fr.SystemId, fr.FirmwareId, fr.Descr); foreach (var o in options) { ListViewItem olvi = new ListViewItem(); @@ -407,24 +407,24 @@ namespace BizHawk.Client.EmuHawk olvi.SubItems.Add(new ListViewItem.ListViewSubItem()); olvi.SubItems.Add(new ListViewItem.ListViewSubItem()); olvi.SubItems.Add(new ListViewItem.ListViewSubItem()); - var ff = FirmwareDatabase.FirmwareFilesByHash[o.hash]; - if (o.status == FirmwareDatabase.FirmwareOptionStatus.Ideal) + var ff = FirmwareDatabase.FirmwareFilesByHash[o.Hash]; + if (o.Status == FirmwareDatabase.FirmwareOptionStatus.Ideal) olvi.ImageIndex = FirmwaresConfigInfo.idIdeal; - if (o.status == FirmwareDatabase.FirmwareOptionStatus.Acceptable) + if (o.Status == FirmwareDatabase.FirmwareOptionStatus.Acceptable) olvi.ImageIndex = FirmwaresConfigInfo.idAcceptable; - if (o.status == FirmwareDatabase.FirmwareOptionStatus.Unacceptable) + if (o.Status == FirmwareDatabase.FirmwareOptionStatus.Unacceptable) olvi.ImageIndex = FirmwaresConfigInfo.idUnacceptable; - if (o.status == FirmwareDatabase.FirmwareOptionStatus.Bad) + if (o.Status == FirmwareDatabase.FirmwareOptionStatus.Bad) olvi.ImageIndex = FirmwaresConfigInfo.idBad; - olvi.SubItems[0].Text = ff.size.ToString(); + olvi.SubItems[0].Text = ff.Size.ToString(); olvi.SubItems[0].Font = this.Font; //why doesnt this work? - olvi.SubItems[1].Text = "sha1:" + o.hash; + olvi.SubItems[1].Text = "sha1:" + o.Hash; olvi.SubItems[1].Font = fixedFont; - olvi.SubItems[2].Text = ff.recommendedName; + olvi.SubItems[2].Text = ff.RecommendedName; olvi.SubItems[2].Font = this.Font; //why doesnt this work? - olvi.SubItems[3].Text = ff.descr; + olvi.SubItems[3].Text = ff.Description; olvi.SubItems[3].Font = this.Font; //why doesnt this work? - olvi.SubItems[4].Text = ff.info; + olvi.SubItems[4].Text = ff.Info; olvi.SubItems[4].Font = this.Font; //why doesnt this work? fciDialog.lvOptions.Items.Add(olvi); } diff --git a/BizHawk.Emulation.Common/BizInvoke/BizInvoker.cs b/BizHawk.Emulation.Common/BizInvoke/BizInvoker.cs index d2bf032b6c..25054d8e96 100644 --- a/BizHawk.Emulation.Common/BizInvoke/BizInvoker.cs +++ b/BizHawk.Emulation.Common/BizInvoke/BizInvoker.cs @@ -192,40 +192,61 @@ namespace BizHawk.Emulation.Common.BizInvoke var returnType = baseMethod.ReturnType; // create the delegate type - var delegateType = type.DefineNestedType("DelegateType" + baseMethod.Name, - TypeAttributes.Class | TypeAttributes.NestedPrivate | TypeAttributes.Sealed, typeof(MulticastDelegate)); + var delegateType = type.DefineNestedType( + "DelegateType" + baseMethod.Name, + TypeAttributes.Class | TypeAttributes.NestedPrivate | TypeAttributes.Sealed, + typeof(MulticastDelegate)); + var delegateCtor = delegateType.DefineConstructor( MethodAttributes.RTSpecialName | MethodAttributes.HideBySig | MethodAttributes.Public, - CallingConventions.Standard, new Type[] { typeof(object), typeof(IntPtr) }); + CallingConventions.Standard, + new[] { typeof(object), typeof(IntPtr) }); + delegateCtor.SetImplementationFlags(MethodImplAttributes.Runtime | MethodImplAttributes.Managed); - var delegateInvoke = delegateType.DefineMethod("Invoke", - MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.NewSlot | MethodAttributes.Virtual, returnType, paramTypes); + + var delegateInvoke = delegateType.DefineMethod( + "Invoke", + MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.NewSlot | MethodAttributes.Virtual, + returnType, + paramTypes); + // we have to project all of the attributes from the baseMethod to the delegateInvoke // so for something like [Out], the interop engine will see it and use it for (int i = 0; i < paramInfos.Length; i++) { var p = delegateInvoke.DefineParameter(i + 1, ParameterAttributes.None, paramInfos[i].Name); foreach (var a in paramInfos[i].GetCustomAttributes(false)) + { p.SetCustomAttribute(GetAttributeBuilder(a)); + } } { var p = delegateInvoke.DefineParameter(0, ParameterAttributes.Retval, baseMethod.ReturnParameter.Name); foreach (var a in baseMethod.ReturnParameter.GetCustomAttributes(false)) + { p.SetCustomAttribute(GetAttributeBuilder(a)); + } } - delegateInvoke.SetImplementationFlags(MethodImplAttributes.Runtime | MethodImplAttributes.Managed); + // add the [UnmanagedFunctionPointer] to the delegate so interop will know how to call it var attr = new CustomAttributeBuilder(typeof(UnmanagedFunctionPointerAttribute).GetConstructor(new[] { typeof(CallingConvention) }), new object[] { nativeCall }); delegateType.SetCustomAttribute(attr); // define a field on the class to hold the delegate - var field = type.DefineField("DelegateField" + baseMethod.Name, delegateType, + var field = type.DefineField( + "DelegateField" + baseMethod.Name, + delegateType, FieldAttributes.Public); - var method = type.DefineMethod(baseMethod.Name, MethodAttributes.Virtual | MethodAttributes.Public, - CallingConventions.HasThis, returnType, paramTypes); + var method = type.DefineMethod( + baseMethod.Name, + MethodAttributes.Virtual | MethodAttributes.Public, + CallingConventions.HasThis, + returnType, + paramTypes); + var il = method.GetILGenerator(); Label exc = new Label(); @@ -357,7 +378,7 @@ namespace BizHawk.Emulation.Common.BizInvoke } /// - /// load an IntPtr constant in an il stream + /// load an IntPtr constant in an IL stream /// private static void LoadConstant(ILGenerator il, IntPtr p) { @@ -378,7 +399,7 @@ namespace BizHawk.Emulation.Common.BizInvoke } /// - /// load a UIntPtr constant in an il stream + /// load a UIntPtr constant in an IL stream /// private static void LoadConstant(ILGenerator il, UIntPtr p) { diff --git a/BizHawk.Emulation.Common/BizInvoke/DynamicLibraryImportResolver.cs b/BizHawk.Emulation.Common/BizInvoke/DynamicLibraryImportResolver.cs index 2acf6e8d3a..8e528b79f1 100644 --- a/BizHawk.Emulation.Common/BizInvoke/DynamicLibraryImportResolver.cs +++ b/BizHawk.Emulation.Common/BizInvoke/DynamicLibraryImportResolver.cs @@ -1,8 +1,6 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; using System.Runtime.InteropServices; + using BizHawk.Common; namespace BizHawk.Emulation.Common.BizInvoke @@ -61,8 +59,10 @@ namespace BizHawk.Emulation.Common.BizInvoke { [DllImport("kernel32.dll")] public static extern IntPtr LoadLibrary(string dllToLoad); + [DllImport("kernel32.dll")] public static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName); + [DllImport("kernel32.dll")] public static extern bool FreeLibrary(IntPtr hModule); } diff --git a/BizHawk.Emulation.Common/Database/FirmwareDatabase.cs b/BizHawk.Emulation.Common/Database/FirmwareDatabase.cs index ac1f60e3a2..c297b8c175 100644 --- a/BizHawk.Emulation.Common/Database/FirmwareDatabase.cs +++ b/BizHawk.Emulation.Common/Database/FirmwareDatabase.cs @@ -1,5 +1,4 @@ -using System; -using System.Linq; +using System.Linq; using System.Collections.Generic; namespace BizHawk.Emulation.Common @@ -9,11 +8,11 @@ namespace BizHawk.Emulation.Common static FirmwareDatabase() { // FDS has two OK variants (http://tcrf.net/Family_Computer_Disk_System) - var fds_nintendo = File("57FE1BDEE955BB48D357E463CCBF129496930B62", 8192, "disksys-nintendo.rom", "Bios (Nintendo)"); - var fds_twinfc = File("E4E41472C454F928E53EB10E0509BF7D1146ECC1", 8192, "disksys-nintendo.rom", "Bios (TwinFC)"); + var fdsNintendo = File("57FE1BDEE955BB48D357E463CCBF129496930B62", 8192, "disksys-nintendo.rom", "Bios (Nintendo)"); + var fdsTwinfc = File("E4E41472C454F928E53EB10E0509BF7D1146ECC1", 8192, "disksys-nintendo.rom", "Bios (TwinFC)"); Firmware("NES", "Bios_FDS", "Bios"); - Option("NES", "Bios_FDS", fds_nintendo); - Option("NES", "Bios_FDS", fds_twinfc); + Option("NES", "Bios_FDS", fdsNintendo); + Option("NES", "Bios_FDS", fdsTwinfc); FirmwareAndOption("973E10840DB683CF3FAF61BD443090786B3A9F04", 262144, "SNES", "Rom_SGB", "sgb.sfc", "Super GameBoy Rom"); // World (Rev B) ? FirmwareAndOption("A002F4EFBA42775A31185D443F3ED1790B0E949A", 3072, "SNES", "CX4", "cx4.rom", "CX4 Rom"); @@ -31,19 +30,16 @@ namespace BizHawk.Emulation.Common FirmwareAndOption("A3AF676991391A6DD716C79022D4947206B78164", 4096, "A78", "Bios_HSC", "7800highscore.bin", "Highscore Bios"); FirmwareAndOption("45BEDC4CBDEAC66C7DF59E9E599195C778D86A92", 8192, "Coleco", "Bios", "ColecoBios.bin", "Bios"); - { - var GBA_JDebug = File("AA98A2AD32B86106340665D1222D7D973A1361C7", 16384, "gbabios.rom", "Bios (J Debug)"); - var GBA_Normal = File("300C20DF6731A33952DED8C436F7F186D25D3492", 16384, "gbabios.rom", "Bios (World)"); - Firmware("GBA", "Bios", "Bios"); - Option("GBA", "Bios", GBA_Normal); - Option("GBA", "Bios", GBA_JDebug); - } + var gbaNormal = File("300C20DF6731A33952DED8C436F7F186D25D3492", 16384, "gbabios.rom", "Bios (World)"); + var gbaJDebug = File("AA98A2AD32B86106340665D1222D7D973A1361C7", 16384, "gbabios.rom", "Bios (J Debug)"); + Firmware("GBA", "Bios", "Bios"); + Option("GBA", "Bios", gbaNormal); + Option("GBA", "Bios", gbaJDebug); FirmwareAndOption("E4ED47FAE31693E016B081C6BDA48DA5B70D7CCB", 512, "Lynx", "Boot", "lynxboot.img", "Boot Rom"); - - //FirmwareAndOption("24F67BDEA115A2C847C8813A262502EE1607B7DF", "NDS", "Bios_Arm7", "biosnds7.rom", "ARM7 Bios"); - //FirmwareAndOption("BFAAC75F101C135E32E2AAF541DE6B1BE4C8C62D", "NDS", "Bios_Arm9", "biosnds9.rom", "ARM9 Bios"); + ////FirmwareAndOption("24F67BDEA115A2C847C8813A262502EE1607B7DF", "NDS", "Bios_Arm7", "biosnds7.rom", "ARM7 Bios"); + ////FirmwareAndOption("BFAAC75F101C135E32E2AAF541DE6B1BE4C8C62D", "NDS", "Bios_Arm9", "biosnds9.rom", "ARM9 Bios"); FirmwareAndOption("5A65B922B562CB1F57DAB51B73151283F0E20C7A", 8192, "INTV", "EROM", "erom.bin", "Executive Rom"); FirmwareAndOption("F9608BB4AD1CFE3640D02844C7AD8E0BCD974917", 2048, "INTV", "GROM", "grom.bin", "Graphics Rom"); @@ -159,7 +155,7 @@ namespace BizHawk.Emulation.Common var psone_r5e = File("DBC7339E5D85827C095764FC077B41F78FD2ECAE", 524288, "psone-45e.bin", "PSX BIOS (Version 4.5 05/25/00 E)", "Used on PSone SCPH-102 [g]. This is for Rev C PSone hardware [w]."); var ps2_50j = File("D7D6BE084F51354BC951D8FA2D8D912AA70ABC5E", 4194304, "ps2-50j.bin", "PSX BIOS (Version 5.0 10/27/00 J)", "Found on a PS2 [p]."); - ps_22j_bad.bad = ps_22j_bad2.bad = ps_30e_bad.bad = true; + ps_22j_bad.Bad = ps_22j_bad2.Bad = ps_30e_bad.Bad = true; Firmware("PSX", "U", "BIOS (U)"); Firmware("PSX", "J", "BIOS (J)"); @@ -197,11 +193,11 @@ namespace BizHawk.Emulation.Common Firmware("AppleII", "AppleIIe", "AppleIIe.rom"); var appleII_AppleIIe = File("B8EA90ABE135A0031065E01697C4A3A20D51198B", 16384, "AppleIIe.rom", "Apple II e"); - Option("AppleII", "AppleIIe", appleII_AppleIIe, FirmwareOptionStatus.Acceptable); + Option("AppleII", "AppleIIe", appleII_AppleIIe); Firmware("AppleII", "DiskII", "DiskII.rom"); var appleII_DiskII = File("D4181C9F046AAFC3FB326B381BAAC809D9E38D16", 256, "DiskII.rom", "Disk II"); - Option("AppleII", "DiskII", appleII_DiskII, FirmwareOptionStatus.Acceptable); + Option("AppleII", "DiskII", appleII_DiskII); } // adds a defined firmware ID to the database @@ -209,9 +205,9 @@ namespace BizHawk.Emulation.Common { var fr = new FirmwareRecord { - systemId = systemId, - firmwareId = id, - descr = descr + SystemId = systemId, + FirmwareId = id, + Descr = descr }; FirmwareRecords.Add(fr); @@ -222,19 +218,19 @@ namespace BizHawk.Emulation.Common { var fo = new FirmwareOption { - systemId = systemId, - firmwareId = id, - hash = hash, - status = status, - size = size + SystemId = systemId, + FirmwareId = id, + Hash = hash, + Status = status, + Size = size }; FirmwareOptions.Add(fo); // first option is automatically ideal - if (FirmwareOptions.Count == 1 && fo.status == FirmwareOptionStatus.Acceptable) + if (FirmwareOptions.Count == 1 && fo.Status == FirmwareOptionStatus.Acceptable) { - fo.status = FirmwareOptionStatus.Ideal; + fo.Status = FirmwareOptionStatus.Ideal; } return fo; @@ -243,12 +239,12 @@ namespace BizHawk.Emulation.Common // adds an acceptable option for a firmware ID to the database private static FirmwareOption Option(string systemId, string id, FirmwareFile ff, FirmwareOptionStatus status = FirmwareOptionStatus.Acceptable) { - var fo = Option(ff.hash, ff.size, systemId, id, status); + var fo = Option(ff.Hash, ff.Size, systemId, id, status); // make sure this goes in as bad - if (ff.bad) + if (ff.Bad) { - fo.status = FirmwareOptionStatus.Bad; + fo.Status = FirmwareOptionStatus.Bad; } return fo; @@ -261,11 +257,11 @@ namespace BizHawk.Emulation.Common var ff = new FirmwareFile { - hash = hashfix, - size = size, - recommendedName = recommendedName, - descr = descr, - info = additionalInfo + Hash = hashfix, + Size = size, + RecommendedName = recommendedName, + Description = descr, + Info = additionalInfo }; FirmwareFiles.Add(ff); FirmwareFilesByHash[hashfix] = ff; @@ -276,33 +272,33 @@ namespace BizHawk.Emulation.Common private static void FirmwareAndOption(string hash, long size, string systemId, string id, string name, string descr) { Firmware(systemId, id, descr); - File(hash, size, name, descr, ""); + File(hash, size, name, descr, string.Empty); Option(hash, size, systemId, id); } - public static readonly List FirmwareRecords = new List(); public static readonly List FirmwareOptions = new List(); public static readonly List FirmwareFiles = new List(); - public static Dictionary FirmwareFilesByHash = new Dictionary(); + public static readonly Dictionary FirmwareFilesByHash = new Dictionary(); public class FirmwareFile { - public string hash; - public long size; - public string recommendedName; - public string descr; - public string info; - public bool bad; + public string Hash { get; set; } + public long Size { get; set; } + public string RecommendedName { get; set; } + public string Description { get; set; } + public string Info { get; set; } + public bool Bad { get; set; } } public class FirmwareRecord { - public string systemId; - public string firmwareId; - public string descr; - public string ConfigKey => $"{systemId}+{firmwareId}"; + public string SystemId { get; set; } + public string FirmwareId { get; set; } + public string Descr { get; set; } + + public string ConfigKey => $"{SystemId}+{FirmwareId}"; } public enum FirmwareOptionStatus @@ -312,23 +308,23 @@ namespace BizHawk.Emulation.Common public class FirmwareOption { - public string systemId; - public string firmwareId; - public string hash; - public long size; - public FirmwareOptionStatus status; - public bool IsAcceptableOrIdeal { get { return status == FirmwareOptionStatus.Ideal || status == FirmwareOptionStatus.Acceptable; } } - public string ConfigKey => $"{systemId}+{firmwareId}"; - } + public string SystemId { get; set; } + public string FirmwareId { get; set; } + public string Hash { get; set; } + public long Size { get; set; } + public FirmwareOptionStatus Status { get; set; } + public bool IsAcceptableOrIdeal => Status == FirmwareOptionStatus.Ideal || Status == FirmwareOptionStatus.Acceptable; + public string ConfigKey => $"{SystemId}+{FirmwareId}"; + } public static FirmwareRecord LookupFirmwareRecord(string sysId, string firmwareId) { var found = - (from fr in FirmwareRecords - where fr.firmwareId == firmwareId - && fr.systemId == sysId - select fr); + from fr in FirmwareRecords + where fr.FirmwareId == firmwareId + && fr.SystemId == sysId + select fr; return found.FirstOrDefault(); }