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
This commit is contained in:
parent
ef37bfd0c7
commit
ee1cea5d08
|
@ -118,6 +118,7 @@
|
|||
<Compile Include="lua\EmuLuaLibrary.NES.cs" />
|
||||
<Compile Include="lua\EmuLuaLibrary.SNES.cs" />
|
||||
<Compile Include="lua\EmuLuaLibrary.String.cs" />
|
||||
<Compile Include="lua\LuaAttributes.cs" />
|
||||
<Compile Include="lua\LuaDocumentation.cs" />
|
||||
<Compile Include="lua\LuaFile.cs" />
|
||||
<Compile Include="lua\LuaFileList.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);
|
||||
|
|
|
@ -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<LibraryFunction> FunctionList = new List<LibraryFunction>();
|
||||
|
||||
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()
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue