From e36098becf9165bc5d470aded5f11a317e329850 Mon Sep 17 00:00:00 2001 From: YoshiRulz Date: Fri, 24 Jul 2020 13:33:12 +1000 Subject: [PATCH] A little dedup --- .../lua/LuaLibraryBase.cs | 15 +-------- .../tools/Lua/Win32LuaLibraries.cs | 32 ++++++++++++------- .../Extensions/ReflectionExtensions.cs | 16 ---------- 3 files changed, 21 insertions(+), 42 deletions(-) diff --git a/src/BizHawk.Client.Common/lua/LuaLibraryBase.cs b/src/BizHawk.Client.Common/lua/LuaLibraryBase.cs index b3a360e09d..6127fc4a8c 100644 --- a/src/BizHawk.Client.Common/lua/LuaLibraryBase.cs +++ b/src/BizHawk.Client.Common/lua/LuaLibraryBase.cs @@ -3,7 +3,6 @@ using System.Drawing; using System.Threading; using NLua; -using BizHawk.Common.ReflectionExtensions; namespace BizHawk.Client.Common { @@ -27,7 +26,7 @@ namespace BizHawk.Client.Common public abstract string Name { get; } public Action LogOutputCallback { protected get; set; } - protected Lua Lua { get; } + public Lua Lua { get; } public static void ClearCurrentThread() { @@ -73,17 +72,5 @@ namespace BizHawk.Client.Common { LogOutputCallback?.Invoke(message.ToString()); } - - public void LuaRegister(Type callingLibrary, LuaDocumentation docs = null) - { - Lua.NewTable(Name); - foreach (var method in GetType().GetMethods()) - { - var foundAttrs = method.GetCustomAttributes(typeof(LuaMethodAttribute), false); - if (foundAttrs.Length == 0) continue; - Lua.RegisterFunction($"{Name}.{((LuaMethodAttribute) foundAttrs[0]).Name}", this, method); - docs?.Add(new LibraryFunction(Name, callingLibrary.Description(), method)); - } - } } } diff --git a/src/BizHawk.Client.EmuHawk/tools/Lua/Win32LuaLibraries.cs b/src/BizHawk.Client.EmuHawk/tools/Lua/Win32LuaLibraries.cs index 5e3a28d03d..a8b01afdd0 100644 --- a/src/BizHawk.Client.EmuHawk/tools/Lua/Win32LuaLibraries.cs +++ b/src/BizHawk.Client.EmuHawk/tools/Lua/Win32LuaLibraries.cs @@ -1,4 +1,5 @@ using System; +using System.ComponentModel; using System.IO; using System.Linq; using System.Reflection; @@ -6,7 +7,6 @@ using System.Threading; using NLua; -using BizHawk.Common.ReflectionExtensions; using BizHawk.Emulation.Common; using BizHawk.Client.Common; @@ -23,6 +23,23 @@ namespace BizHawk.Client.EmuHawk public Win32LuaLibraries(IMainFormForApi mainForm, IEmulatorServiceProvider serviceProvider) : this() { + void EnumerateLuaFunctions(string name, Type type, LuaLibraryBase instance) + { + instance?.Lua?.NewTable(name); + foreach (var method in type.GetMethods()) + { + var foundAttrs = method.GetCustomAttributes(typeof(LuaMethodAttribute), false); + if (foundAttrs.Length == 0) continue; + instance?.Lua?.RegisterFunction($"{name}.{((LuaMethodAttribute) foundAttrs[0]).Name}", instance, method); + Docs.Add(new LibraryFunction( + name, + type.GetCustomAttributes(typeof(DescriptionAttribute), false).Cast() + .Select(descAttr => descAttr.Description).FirstOrDefault() ?? string.Empty, + method + )); + } + } + LuaWait = new AutoResetEvent(false); Docs.Clear(); @@ -41,7 +58,6 @@ namespace BizHawk.Client.EmuHawk if (addLibrary) { var instance = (LuaLibraryBase)Activator.CreateInstance(lib, _lua); - instance.LuaRegister(lib, Docs); instance.LogOutputCallback = ConsoleLuaLibrary.LogOutput; ServiceInjector.UpdateServices(serviceProvider, instance); @@ -56,6 +72,7 @@ namespace BizHawk.Client.EmuHawk if (instance is DelegatingLuaLibraryEmu dlgInstanceEmu) dlgInstanceEmu.APIs = ApiContainerInstance; // this is necessary as the property has the `new` modifier else if (instance is DelegatingLuaLibrary dlgInstance) dlgInstance.APIs = ApiContainerInstance; + EnumerateLuaFunctions(instance.Name, lib, instance); Libraries.Add(lib, instance); } } @@ -65,16 +82,7 @@ namespace BizHawk.Client.EmuHawk EmulationLuaLibrary.FrameAdvanceCallback = Frameadvance; EmulationLuaLibrary.YieldCallback = EmuYield; - // Add LuaCanvas to Docs - Type luaCanvas = typeof(LuaCanvas); - - foreach (var method in luaCanvas.GetMethods()) - { - if (method.GetCustomAttributes(typeof(LuaMethodAttribute), false).Length != 0) - { - Docs.Add(new LibraryFunction(nameof(LuaCanvas), luaCanvas.Description(), method)); - } - } + EnumerateLuaFunctions(nameof(LuaCanvas), typeof(LuaCanvas), null); // add LuaCanvas to Lua function reference table } /// lazily instantiated diff --git a/src/BizHawk.Common/Extensions/ReflectionExtensions.cs b/src/BizHawk.Common/Extensions/ReflectionExtensions.cs index fa935b4eab..897f96b926 100644 --- a/src/BizHawk.Common/Extensions/ReflectionExtensions.cs +++ b/src/BizHawk.Common/Extensions/ReflectionExtensions.cs @@ -64,22 +64,6 @@ namespace BizHawk.Common.ReflectionExtensions return type.Name; } - /// - /// Gets the description attribute from a type - /// - public static string Description(this Type type) - { - var descriptions = (DescriptionAttribute[]) - type.GetCustomAttributes(typeof(DescriptionAttribute), false); - - if (descriptions.Length == 0) - { - return ""; - } - - return descriptions[0].Description; - } - /// /// Gets an enum from a description attribute ///