From ee1cea5d08b96d27f15ecd288acaf3853771dbe8 Mon Sep 17 00:00:00 2001 From: adelikat Date: Sat, 25 Jan 2014 19:49:29 +0000 Subject: [PATCH] Lua - even better way to build lua libraries, now methods will have attributes for the name, and description of each method. Someone needs to put in descriptions now. Bit library is the only one done as of this commit --- .../BizHawk.Client.Common.csproj | 1 + .../lua/EmuLuaLibrary.Bit.cs | 59 ++++++++++++++++++- BizHawk.Client.Common/lua/LuaDocumentation.cs | 6 +- BizHawk.Client.Common/lua/LuaLibraryBase.cs | 20 +++++-- 4 files changed, 77 insertions(+), 9 deletions(-) diff --git a/BizHawk.Client.Common/BizHawk.Client.Common.csproj b/BizHawk.Client.Common/BizHawk.Client.Common.csproj index e4302a372d..bfecd134be 100644 --- a/BizHawk.Client.Common/BizHawk.Client.Common.csproj +++ b/BizHawk.Client.Common/BizHawk.Client.Common.csproj @@ -118,6 +118,7 @@ + diff --git a/BizHawk.Client.Common/lua/EmuLuaLibrary.Bit.cs b/BizHawk.Client.Common/lua/EmuLuaLibrary.Bit.cs index 17170bb10e..698ce2c53e 100644 --- a/BizHawk.Client.Common/lua/EmuLuaLibrary.Bit.cs +++ b/BizHawk.Client.Common/lua/EmuLuaLibrary.Bit.cs @@ -1,4 +1,5 @@ using System; + namespace BizHawk.Client.Common { public class BitLuaLibrary : LuaLibraryBase @@ -28,74 +29,130 @@ namespace BizHawk.Client.Common } } + [LuaMethodAttributes( + "band", + "Bitwise AND of 'val' against 'amt'" + )] public static uint Band(object val, object amt) { return (uint)(LuaInt(val) & LuaInt(amt)); } + [LuaMethodAttributes( + "bnot", + "Bitwise NOT of 'val' against 'amt'" + )] public static uint Bnot(object val) { return (uint)(~LuaInt(val)); } + [LuaMethodAttributes( + "bor", + "Bitwise OR of 'val' against 'amt'" + )] public static uint Bor(object val, object amt) { return (uint)(LuaInt(val) | LuaInt(amt)); } + [LuaMethodAttributes( + "bxor", + "Bitwise XOR of 'val' against 'amt'" + )] public static uint Bxor(object val, object amt) { return (uint)(LuaInt(val) ^ LuaInt(amt)); } + [LuaMethodAttributes( + "lshift", + "Logical shift left of 'val' by 'amt' bits" + )] public static uint Lshift(object val, object amt) { return (uint)(LuaInt(val) << LuaInt(amt)); } + [LuaMethodAttributes( + "rol", + "Left rotate 'val' by 'amt' bits" + )] public static uint Rol(object val, object amt) { return (uint)((LuaInt(val) << LuaInt(amt)) | (LuaInt(val) >> (32 - LuaInt(amt)))); } + [LuaMethodAttributes( + "ror", + "Right rotate 'val' by 'amt' bits" + )] public static uint Ror(object val, object amt) { return (uint)((LuaInt(val) >> LuaInt(amt)) | (LuaInt(val) << (32 - LuaInt(amt)))); } + [LuaMethodAttributes( + "rshift", + "Logical shift right of 'val' by 'amt' bits" + )] public static uint Rshift(object val, object amt) { return (uint)(LuaInt(val) >> LuaInt(amt)); } + [LuaMethodAttributes( + "check", + "Returns result of bit 'pos' being set in 'num'" + )] public static bool Check(object num, object pos) { return (LuaLong(num) & (1 << LuaInt(pos))) != 0; } - + + [LuaMethodAttributes( + "set", + "TODO" + )] public static uint Set(object num, object pos) { return (uint)(LuaInt(num) | 1 << LuaInt(pos)); } + [LuaMethodAttributes( + "clear", + "TODO" + )] public static uint Clear(object num, object pos) { return (uint)(LuaInt(num) & ~(1 << LuaInt(pos))); } + [LuaMethodAttributes( + "byteswap_16", + "Byte swaps 'short', i.e. bit.byteswap_16(0xFF00) would return 0x00FF" + )] public static uint Byteswap_16(object _short) { return (UInt16)((LuaInt(_short) & 0xFFU) << 8 | (LuaInt(_short) & 0xFF00U) >> 8); } + [LuaMethodAttributes( + "byteswap_32", + "Byte swaps 'dword'" + )] public static uint Byteswap_32(object _dword) { return (LuaUInt(_dword) & 0x000000FFU) << 24 | (LuaUInt(_dword) & 0x0000FF00U) << 8 | (LuaUInt(_dword) & 0x00FF0000U) >> 8 | (LuaUInt(_dword) & 0xFF000000U) >> 24; } + [LuaMethodAttributes( + "byteswap_64", + "Byte swaps 'long'" + )] public static UInt64 Byteswap_64(object _long) { UInt64 value = (UInt64)LuaLong(_long); diff --git a/BizHawk.Client.Common/lua/LuaDocumentation.cs b/BizHawk.Client.Common/lua/LuaDocumentation.cs index e6d7c3a206..71f77aaef3 100644 --- a/BizHawk.Client.Common/lua/LuaDocumentation.cs +++ b/BizHawk.Client.Common/lua/LuaDocumentation.cs @@ -7,17 +7,19 @@ namespace BizHawk.Client.Common { public interface ILuaDocumentation { - void Add(string method_lib, string method_name, System.Reflection.MethodInfo method); + void Add(string method_lib, string method_name, System.Reflection.MethodInfo method, string description); } public class LuaDocumentation : ILuaDocumentation { public List FunctionList = new List(); - public void Add(string method_lib, string method_name, System.Reflection.MethodInfo method) + public void Add(string method_lib, string method_name, System.Reflection.MethodInfo method, string description) { var f = new LibraryFunction(method_lib, method_name, method); FunctionList.Add(f); + + // TODO: use description; } public void Clear() diff --git a/BizHawk.Client.Common/lua/LuaLibraryBase.cs b/BizHawk.Client.Common/lua/LuaLibraryBase.cs index a56cc2bc52..91654caddf 100644 --- a/BizHawk.Client.Common/lua/LuaLibraryBase.cs +++ b/BizHawk.Client.Common/lua/LuaLibraryBase.cs @@ -1,4 +1,6 @@ using System; +using System.Collections.Generic; +using System.Linq; using System.Reflection; using BizHawk.Common; @@ -22,7 +24,7 @@ namespace BizHawk.Client.Common if (docs != null) { - docs.Add(Name, methodName, method); + docs.Add(Name, methodName, method, String.Empty); } } } @@ -32,15 +34,21 @@ namespace BizHawk.Client.Common { lua.NewTable(Name); - foreach (var nameLookup in Functions) + var luaAttr = typeof(LuaMethodAttributes); + + var methods = GetType() + .GetMethods() + .Where(m => m.GetCustomAttributes(luaAttr, false).Any()); + + foreach (var method in methods) { - var luaMethodName = Name + "." + nameLookup.ToLower(); - var actualMethodName = GetType().GetMethod(nameLookup); - lua.RegisterFunction(luaMethodName, this, actualMethodName); + var luaMethodAttr = method.GetCustomAttributes(luaAttr, false).First() as LuaMethodAttributes; + var luaName = Name + "." + luaMethodAttr.Name; + lua.RegisterFunction(luaName, this, method); if (docs != null) { - docs.Add(Name, nameLookup, actualMethodName); + docs.Add(Name, luaName, method, luaMethodAttr.Description); } } }