Commit 19e9e632 authored by Megax's avatar Megax
Browse files

* Tudomasom szerinti legutolso LuaInterfacekerul feltoltesre majd atalakitasra.

parent 00eddcac
using System;
using System.Collections.Generic;
using System.Reflection;
using LuaWrap;
using Lua511;
namespace LuaInterface
{
......@@ -64,9 +64,9 @@ namespace LuaInterface
return extractNetObject;
}
internal ExtractValue checkType(KopiLua.Lua.lua_State luaState,int stackPos,Type paramType)
internal ExtractValue checkType(IntPtr luaState,int stackPos,Type paramType)
{
LuaType luatype = KopiLua.Lua.lua_type(luaState, stackPos).ToLuaType();
LuaTypes luatype = LuaDLL.lua_type(luaState, stackPos);
if(paramType.IsByRef) paramType=paramType.GetElementType();
......@@ -81,55 +81,74 @@ namespace LuaInterface
if (paramType.Equals(typeof(object)))
return extractValues[runtimeHandleValue];
if (KopiLua.Lua.lua_isnumber(luaState, stackPos).ToBoolean())
//CP: Added support for generic parameters
if (paramType.IsGenericParameter)
{
if (luatype == LuaTypes.LUA_TBOOLEAN)
return extractValues[typeof(bool).TypeHandle.Value.ToInt64()];
else if (luatype == LuaTypes.LUA_TSTRING)
return extractValues[typeof(string).TypeHandle.Value.ToInt64()];
else if (luatype == LuaTypes.LUA_TTABLE)
return extractValues[typeof(LuaTable).TypeHandle.Value.ToInt64()];
else if (luatype == LuaTypes.LUA_TUSERDATA)
return extractValues[typeof(object).TypeHandle.Value.ToInt64()];
else if (luatype == LuaTypes.LUA_TFUNCTION)
return extractValues[typeof(LuaFunction).TypeHandle.Value.ToInt64()];
else if (luatype == LuaTypes.LUA_TNUMBER)
return extractValues[typeof(double).TypeHandle.Value.ToInt64()];
else
;//an unsupported type was encountered
}
if (LuaDLL.lua_isnumber(luaState, stackPos))
return extractValues[runtimeHandleValue];
if (paramType == typeof(bool))
{
if (KopiLua.Lua.lua_isboolean(luaState, stackPos))
if (LuaDLL.lua_isboolean(luaState, stackPos))
return extractValues[runtimeHandleValue];
}
else if (paramType == typeof(string))
{
if (KopiLua.Lua.lua_isstring(luaState, stackPos).ToBoolean())
if (LuaDLL.lua_isstring(luaState, stackPos))
return extractValues[runtimeHandleValue];
else if (luatype == LuaType.Nil)
else if (luatype == LuaTypes.LUA_TNIL)
return extractNetObject; // kevinh - silently convert nil to a null string pointer
}
else if (paramType == typeof(LuaTable))
{
if (luatype == LuaType.Table)
if (luatype == LuaTypes.LUA_TTABLE)
return extractValues[runtimeHandleValue];
}
else if (paramType == typeof(LuaUserData))
{
if (luatype == LuaType.UserData)
if (luatype == LuaTypes.LUA_TUSERDATA)
return extractValues[runtimeHandleValue];
}
else if (paramType == typeof(LuaFunction))
{
if (luatype == LuaType.Function)
if (luatype == LuaTypes.LUA_TFUNCTION)
return extractValues[runtimeHandleValue];
}
else if (typeof(Delegate).IsAssignableFrom(paramType) && luatype == LuaType.Function)
else if (typeof(Delegate).IsAssignableFrom(paramType) && luatype == LuaTypes.LUA_TFUNCTION)
{
return new ExtractValue(new DelegateGenerator(translator, paramType).extractGenerated);
}
else if (paramType.IsInterface && luatype == LuaType.Table)
else if (paramType.IsInterface && luatype == LuaTypes.LUA_TTABLE)
{
return new ExtractValue(new ClassGenerator(translator, paramType).extractGenerated);
}
else if ((paramType.IsInterface || paramType.IsClass) && luatype == LuaType.Nil)
else if ((paramType.IsInterface || paramType.IsClass) && luatype == LuaTypes.LUA_TNIL)
{
// kevinh - allow nil to be silently converted to null - extractNetObject will return null when the item ain't found
return extractNetObject;
}
else if (KopiLua.Lua.lua_type(luaState, stackPos).ToLuaType() == LuaType.Table)
else if (LuaDLL.lua_type(luaState, stackPos) == LuaTypes.LUA_TTABLE)
{
if (KopiLua.Lua.luaL_getmetafield(luaState, stackPos, "__index").ToBoolean())
if (LuaDLL.luaL_getmetafield(luaState, stackPos, "__index"))
{
object obj = translator.getNetObject(luaState, -1);
KopiLua.Lua.lua_settop(luaState, -2);
LuaDLL.lua_settop(luaState, -2);
if (obj != null && paramType.IsAssignableFrom(obj.GetType()))
return extractNetObject;
}
......@@ -151,136 +170,136 @@ namespace LuaInterface
* index stackPos as the desired type if it can, or null
* otherwise.
*/
private object getAsSbyte(KopiLua.Lua.lua_State luaState,int stackPos)
private object getAsSbyte(IntPtr luaState,int stackPos)
{
sbyte retVal=(sbyte)KopiLua.Lua.lua_tonumber(luaState,stackPos);
if(retVal==0 && !KopiLua.Lua.lua_isnumber(luaState,stackPos).ToBoolean()) return null;
sbyte retVal=(sbyte)LuaDLL.lua_tonumber(luaState,stackPos);
if(retVal==0 && !LuaDLL.lua_isnumber(luaState,stackPos)) return null;
return retVal;
}
private object getAsByte(KopiLua.Lua.lua_State luaState,int stackPos)
private object getAsByte(IntPtr luaState,int stackPos)
{
byte retVal=(byte)KopiLua.Lua.lua_tonumber(luaState,stackPos);
if(retVal==0 && !KopiLua.Lua.lua_isnumber(luaState,stackPos).ToBoolean()) return null;
byte retVal=(byte)LuaDLL.lua_tonumber(luaState,stackPos);
if(retVal==0 && !LuaDLL.lua_isnumber(luaState,stackPos)) return null;
return retVal;
}
private object getAsShort(KopiLua.Lua.lua_State luaState,int stackPos)
private object getAsShort(IntPtr luaState,int stackPos)
{
short retVal=(short)KopiLua.Lua.lua_tonumber(luaState,stackPos);
if(retVal==0 && !KopiLua.Lua.lua_isnumber(luaState,stackPos).ToBoolean()) return null;
short retVal=(short)LuaDLL.lua_tonumber(luaState,stackPos);
if(retVal==0 && !LuaDLL.lua_isnumber(luaState,stackPos)) return null;
return retVal;
}
private object getAsUshort(KopiLua.Lua.lua_State luaState,int stackPos)
private object getAsUshort(IntPtr luaState,int stackPos)
{
ushort retVal=(ushort)KopiLua.Lua.lua_tonumber(luaState,stackPos);
if(retVal==0 && !KopiLua.Lua.lua_isnumber(luaState,stackPos).ToBoolean()) return null;
ushort retVal=(ushort)LuaDLL.lua_tonumber(luaState,stackPos);
if(retVal==0 && !LuaDLL.lua_isnumber(luaState,stackPos)) return null;
return retVal;
}
private object getAsInt(KopiLua.Lua.lua_State luaState,int stackPos)
private object getAsInt(IntPtr luaState,int stackPos)
{
int retVal=(int)KopiLua.Lua.lua_tonumber(luaState,stackPos);
if(retVal==0 && !KopiLua.Lua.lua_isnumber(luaState,stackPos).ToBoolean()) return null;
int retVal=(int)LuaDLL.lua_tonumber(luaState,stackPos);
if(retVal==0 && !LuaDLL.lua_isnumber(luaState,stackPos)) return null;
return retVal;
}
private object getAsUint(KopiLua.Lua.lua_State luaState,int stackPos)
private object getAsUint(IntPtr luaState,int stackPos)
{
uint retVal=(uint)KopiLua.Lua.lua_tonumber(luaState,stackPos);
if(retVal==0 && !KopiLua.Lua.lua_isnumber(luaState,stackPos).ToBoolean()) return null;
uint retVal=(uint)LuaDLL.lua_tonumber(luaState,stackPos);
if(retVal==0 && !LuaDLL.lua_isnumber(luaState,stackPos)) return null;
return retVal;
}
private object getAsLong(KopiLua.Lua.lua_State luaState,int stackPos)
private object getAsLong(IntPtr luaState,int stackPos)
{
long retVal=(long)KopiLua.Lua.lua_tonumber(luaState,stackPos);
if(retVal==0 && !KopiLua.Lua.lua_isnumber(luaState,stackPos).ToBoolean()) return null;
long retVal=(long)LuaDLL.lua_tonumber(luaState,stackPos);
if(retVal==0 && !LuaDLL.lua_isnumber(luaState,stackPos)) return null;
return retVal;
}
private object getAsUlong(KopiLua.Lua.lua_State luaState,int stackPos)
private object getAsUlong(IntPtr luaState,int stackPos)
{
ulong retVal=(ulong)KopiLua.Lua.lua_tonumber(luaState,stackPos);
if(retVal==0 && !KopiLua.Lua.lua_isnumber(luaState,stackPos).ToBoolean()) return null;
ulong retVal=(ulong)LuaDLL.lua_tonumber(luaState,stackPos);
if(retVal==0 && !LuaDLL.lua_isnumber(luaState,stackPos)) return null;
return retVal;
}
private object getAsDouble(KopiLua.Lua.lua_State luaState,int stackPos)
private object getAsDouble(IntPtr luaState,int stackPos)
{
double retVal=KopiLua.Lua.lua_tonumber(luaState,stackPos);
if(retVal==0 && !KopiLua.Lua.lua_isnumber(luaState,stackPos).ToBoolean()) return null;
double retVal=LuaDLL.lua_tonumber(luaState,stackPos);
if(retVal==0 && !LuaDLL.lua_isnumber(luaState,stackPos)) return null;
return retVal;
}
private object getAsChar(KopiLua.Lua.lua_State luaState,int stackPos)
private object getAsChar(IntPtr luaState,int stackPos)
{
char retVal=(char)KopiLua.Lua.lua_tonumber(luaState,stackPos);
if(retVal==0 && !KopiLua.Lua.lua_isnumber(luaState,stackPos).ToBoolean()) return null;
char retVal=(char)LuaDLL.lua_tonumber(luaState,stackPos);
if(retVal==0 && !LuaDLL.lua_isnumber(luaState,stackPos)) return null;
return retVal;
}
private object getAsFloat(KopiLua.Lua.lua_State luaState,int stackPos)
private object getAsFloat(IntPtr luaState,int stackPos)
{
float retVal=(float)KopiLua.Lua.lua_tonumber(luaState,stackPos);
if(retVal==0 && !KopiLua.Lua.lua_isnumber(luaState,stackPos).ToBoolean()) return null;
float retVal=(float)LuaDLL.lua_tonumber(luaState,stackPos);
if(retVal==0 && !LuaDLL.lua_isnumber(luaState,stackPos)) return null;
return retVal;
}
private object getAsDecimal(KopiLua.Lua.lua_State luaState,int stackPos)
private object getAsDecimal(IntPtr luaState,int stackPos)
{
decimal retVal=(decimal)KopiLua.Lua.lua_tonumber(luaState,stackPos);
if(retVal==0 && !KopiLua.Lua.lua_isnumber(luaState,stackPos).ToBoolean()) return null;
decimal retVal=(decimal)LuaDLL.lua_tonumber(luaState,stackPos);
if(retVal==0 && !LuaDLL.lua_isnumber(luaState,stackPos)) return null;
return retVal;
}
private object getAsBoolean(KopiLua.Lua.lua_State luaState,int stackPos)
private object getAsBoolean(IntPtr luaState,int stackPos)
{
return KopiLua.Lua.lua_toboolean(luaState,stackPos);
return LuaDLL.lua_toboolean(luaState,stackPos);
}
private object getAsString(KopiLua.Lua.lua_State luaState,int stackPos)
private object getAsString(IntPtr luaState,int stackPos)
{
string retVal=KopiLua.Lua.lua_tostring(luaState,stackPos).ToString();
if(retVal==string.Empty && !KopiLua.Lua.lua_isstring(luaState,stackPos).ToBoolean()) return null;
string retVal=LuaDLL.lua_tostring(luaState,stackPos);
if(retVal=="" && !LuaDLL.lua_isstring(luaState,stackPos)) return null;
return retVal;
}
private object getAsTable(KopiLua.Lua.lua_State luaState,int stackPos)
private object getAsTable(IntPtr luaState,int stackPos)
{
return translator.getTable(luaState,stackPos);
}
private object getAsFunction(KopiLua.Lua.lua_State luaState,int stackPos)
private object getAsFunction(IntPtr luaState,int stackPos)
{
return translator.getFunction(luaState,stackPos);
}
private object getAsUserdata(KopiLua.Lua.lua_State luaState,int stackPos)
private object getAsUserdata(IntPtr luaState,int stackPos)
{
return translator.getUserData(luaState,stackPos);
}
public object getAsObject(KopiLua.Lua.lua_State luaState,int stackPos)
public object getAsObject(IntPtr luaState,int stackPos)
{
if(KopiLua.Lua.lua_type(luaState,stackPos).ToLuaType()==LuaType.Table)
if(LuaDLL.lua_type(luaState,stackPos)==LuaTypes.LUA_TTABLE)
{
if(KopiLua.Lua.luaL_getmetafield(luaState,stackPos,"__index").ToBoolean())
if(LuaDLL.luaL_getmetafield(luaState,stackPos,"__index"))
{
if(LuaLib.luaL_checkmetatable(luaState,-1))
if(LuaDLL.luaL_checkmetatable(luaState,-1))
{
KopiLua.Lua.lua_insert(luaState,stackPos);
KopiLua.Lua.lua_remove(luaState,stackPos+1);
LuaDLL.lua_insert(luaState,stackPos);
LuaDLL.lua_remove(luaState,stackPos+1);
}
else
{
KopiLua.Lua.lua_settop(luaState,-2);
LuaDLL.lua_settop(luaState,-2);
}
}
}
object obj=translator.getObject(luaState,stackPos);
return obj;
}
public object getAsNetObject(KopiLua.Lua.lua_State luaState,int stackPos)
public object getAsNetObject(IntPtr luaState,int stackPos)
{
object obj=translator.getNetObject(luaState,stackPos);
if(obj==null && KopiLua.Lua.lua_type(luaState,stackPos).ToLuaType()==LuaType.Table)
if(obj==null && LuaDLL.lua_type(luaState,stackPos)==LuaTypes.LUA_TTABLE)
{
if(KopiLua.Lua.luaL_getmetafield(luaState,stackPos,"__index").ToBoolean())
if(LuaDLL.luaL_getmetafield(luaState,stackPos,"__index"))
{
if(LuaLib.luaL_checkmetatable(luaState,-1))
if(LuaDLL.luaL_checkmetatable(luaState,-1))
{
KopiLua.Lua.lua_insert(luaState,stackPos);
KopiLua.Lua.lua_remove(luaState,stackPos+1);
LuaDLL.lua_insert(luaState,stackPos);
LuaDLL.lua_remove(luaState,stackPos+1);
obj=translator.getNetObject(luaState,stackPos);
}
else
{
KopiLua.Lua.lua_settop(luaState,-2);
LuaDLL.lua_settop(luaState,-2);
}
}
}
......
......@@ -44,7 +44,7 @@ namespace LuaInterface
this.translator=translator;
this.delegateType=delegateType;
}
public object extractGenerated(KopiLua.Lua.lua_State luaState,int stackPos)
public object extractGenerated(IntPtr luaState,int stackPos)
{
return CodeGeneration.Instance.GetDelegate(delegateType,translator.getFunction(luaState,stackPos));
}
......@@ -67,7 +67,7 @@ namespace LuaInterface
this.translator=translator;
this.klass=klass;
}
public object extractGenerated(KopiLua.Lua.lua_State luaState,int stackPos)
public object extractGenerated(IntPtr luaState,int stackPos)
{
return CodeGeneration.Instance.GetClassInstance(klass,translator.getTable(luaState,stackPos));
}
......@@ -105,10 +105,11 @@ namespace LuaInterface
private CodeGeneration()
{
// Create an assembly name
assemblyName=new AssemblyName();
assemblyName=new AssemblyName( );
assemblyName.Name="LuaInterface_generatedcode";
// Create a new assembly with one module.
newAssembly=Thread.GetDomain().DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
newAssembly=Thread.GetDomain().DefineDynamicAssembly(
assemblyName, AssemblyBuilderAccess.Run);
newModule=newAssembly.DefineDynamicModule("LuaInterface_generatedcode");
}
......@@ -148,7 +149,7 @@ namespace LuaInterface
// Emits the IL for the method. It loads the arguments
// and calls the handleEvent method of the base class
ILGenerator generator=handleMethod.GetILGenerator();
ILGenerator generator=handleMethod.GetILGenerator( );
generator.Emit(OpCodes.Ldarg_0);
generator.Emit(OpCodes.Ldarg_1);
generator.Emit(OpCodes.Ldarg_2);
......@@ -201,7 +202,7 @@ namespace LuaInterface
returnType,paramTypes);
// Generates the IL for the method
ILGenerator generator=delegateMethod.GetILGenerator();
ILGenerator generator=delegateMethod.GetILGenerator( );
generator.DeclareLocal(typeof(object[])); // original arguments
generator.DeclareLocal(typeof(object[])); // with out-only arguments removed
......@@ -451,7 +452,7 @@ namespace LuaInterface
if(myType.BaseType.Equals(typeof(object)))
myType.DefineMethodOverride(methodImpl,method);
ILGenerator generator=methodImpl.GetILGenerator();
ILGenerator generator=methodImpl.GetILGenerator( );
generator.DeclareLocal(typeof(object[])); // original arguments
generator.DeclareLocal(typeof(object[])); // with out-only arguments removed
......
This diff is collapsed.
using System;
using System.Collections.Generic;
using System.Text;
namespace LuaInterface
{
/// <summary>
/// Base class to provide consistent disposal flow across lua objects. Uses code provided by Yves Duhoux and suggestions by Hans Schmeidenbacher and Qingrui Li
/// </summary>
public abstract class LuaBase : IDisposable
{
private bool _Disposed;
protected int _Reference;
protected Lua _Interpreter;
~LuaBase()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
public virtual void Dispose(bool disposeManagedResources)
{
if (!_Disposed)
{
if (disposeManagedResources)
{
if (_Reference != 0)
_Interpreter.dispose(_Reference);
}
_Interpreter = null;
_Disposed = true;
}
}
public override bool Equals(object o)
{
if (o is LuaBase)
{
LuaBase l = (LuaBase)o;
return _Interpreter.compareRef(l._Reference, _Reference);
}
else return false;
}
public override int GetHashCode()
{
return _Reference;
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;
namespace LuaInterface
{
/// <summary>
/// Add a specific type for Lua exceptions (kevinh)
/// Exceptions thrown by the Lua runtime
/// </summary>
public class LuaException : ApplicationException
[Serializable]
public class LuaException : Exception
{
public LuaException(string reason) : base(reason)
{
}
public LuaException()
{}
public LuaException(string message) : base(message)
{}
public LuaException(string message, Exception innerException) : base(message, innerException)
{}
protected LuaException(SerializationInfo info, StreamingContext context) : base(info, context)
{}
}
}
\ No newline at end of file
using System;
using System.Collections.Generic;
using System.Text;
using Lua511;
namespace LuaInterface
{
public class LuaFunction : LuaBase
{
//private Lua interpreter;
internal LuaCSFunction function;
//internal int reference;
public LuaFunction(int reference, Lua interpreter)
{
_Reference = reference;
this.function = null;
_Interpreter = interpreter;
}
public LuaFunction(LuaCSFunction function, Lua interpreter)
{
_Reference = 0;
this.function = function;
_Interpreter = interpreter;
}
//~LuaFunction()
//{
// if (reference != 0)
// interpreter.dispose(reference);
//}
//bool disposed = false;
//~LuaFunction()
//{
// Dispose(false);
//}
//public void Dispose()
//{
// Dispose(true);
// GC.SuppressFinalize(this);
//}
//public virtual void Dispose(bool disposeManagedResources)
//{
// if (!this.disposed)
// {
// if (disposeManagedResources)
// {
// if (_Reference != 0)
// _Interpreter.dispose(_Reference);
// }
// disposed = true;
// }
//}
/*
* Calls the function casting return values to the types
* in returnTypes
*/
internal object[] call(object[] args, Type[] returnTypes)
{
return _Interpreter.callFunction(this, args, returnTypes);
}
/*
* Calls the function and returns its return values inside
* an array
*/
public object[] Call(params object[] args)
{
return _Interpreter.callFunction(this, args);
}
/*
* Pushes the function into the Lua stack
*/
internal void push(IntPtr luaState)
{
if (_Reference != 0)
LuaDLL.lua_getref(luaState, _Reference);
else
_Interpreter.pushCSFunction(function);
}
public override string ToString()
{
return "function";
}
public override bool Equals(object o)
{
if (o is LuaFunction)
{
LuaFunction l = (LuaFunction)o;
if (this._Reference != 0 && l._Reference != 0)
return _Interpreter.compareRef(l._Reference, this._Reference);
else
return this.function == l.function;
}
else return false;
}
public override int GetHashCode()
{
if (_Reference != 0)
return _Reference;
else
return function.GetHashCode();
}
}
}
using System;
namespace LuaInterface
{
/// <summary>
/// Marks a method for global usage in Lua scripts
/// </summary>
/// <see cref="LuaRegistrationHelper.TaggedInstanceMethods"/>
/// <see cref="LuaRegistrationHelper.TaggedStaticMethods"/>
[AttributeUsage(AttributeTargets.Method)]
public sealed class LuaGlobalAttribute : Attribute
{
/// <summary>
/// An alternative name to use for calling the function in Lua - leave empty for CLR name
/// </summary>
public string Name { get; set; }
/// <summary>
/// A description of the function
/// </summary>
public string Description { get; set; }
}
}
using System;
namespace LuaInterface
{
/// <summary>
/// Marks a method, field or property to be hidden from Lua auto-completion
/// </summary>
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Field | AttributeTargets.Property)]
public sealed class LuaHideAttribute : Attribute
{}
}
......@@ -45,6 +45,14 @@
<Compile Include="ProxyType.cs" />
<Compile Include="LuaLib\LuaLib.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="LuaBase.cs" />
<Compile Include="LuaFunction.cs" />
<Compile Include="LuaGlobalAttribute.cs" />
<Compile Include="LuaHideAttribute.cs" />
<Compile Include="LuaRegistrationHelper.cs" />
<Compile Include="LuaScriptException.cs" />
<Compile Include="LuaTable.cs" />
<Compile Include="LuaUserData.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
......
using System;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
namespace LuaInterface
{
public static class LuaRegistrationHelper
{
#region Tagged instance methods
/// <summary>
/// Registers all public instance methods in an object tagged with <see cref="LuaGlobalAttribute"/> as Lua global functions
/// </summary>
/// <param name="lua">The Lua VM to add the methods to</param>
/// <param name="o">The object to get the methods from</param>
public static void TaggedInstanceMethods(Lua lua, object o)
{
#region Sanity checks
if (lua == null) throw new ArgumentNullException("lua");
if (o == null) throw new ArgumentNullException("o");
#endregion
foreach (MethodInfo method in o.GetType().GetMethods(BindingFlags.Instance | BindingFlags.Public))
{
foreach (LuaGlobalAttribute attribute in method.GetCustomAttributes(typeof(LuaGlobalAttribute), true))
{
if (string.IsNullOrEmpty(attribute.Name))
lua.RegisterFunction(method.Name, o, method); // CLR name
else
lua.RegisterFunction(attribute.Name, o, method); // Custom name
}
}
}
#endregion
#region Tagged static methods
/// <summary>
/// Registers all public static methods in a class tagged with <see cref="LuaGlobalAttribute"/> as Lua global functions
/// </summary>
/// <param name="lua">The Lua VM to add the methods to</param>
/// <param name="type">The class type to get the methods from</param>
public static void TaggedStaticMethods(Lua lua, Type type)
{
#region Sanity checks
if (lua == null) throw new ArgumentNullException("lua");
if (type == null) throw new ArgumentNullException("type");
if (!type.IsClass) throw new ArgumentException("The type must be a class!", "type");
#endregion
foreach (MethodInfo method in type.GetMethods(BindingFlags.Static | BindingFlags.Public))
{
foreach (LuaGlobalAttribute attribute in method.GetCustomAttributes(typeof(LuaGlobalAttribute), false))
{
if (string.IsNullOrEmpty(attribute.Name))
lua.RegisterFunction(method.Name, null, method); // CLR name
else
lua.RegisterFunction(attribute.Name, null, method); // Custom name
}
}
}
#endregion
#region Enumeration
/// <summary>
/// Registers an enumeration's values for usage as a Lua variable table
/// </summary>
/// <typeparam name="T">The enum type to register</typeparam>
/// <param name="lua">The Lua VM to add the enum to</param>
[SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter", Justification = "The type parameter is used to select an enum type")]
public static void Enumeration<T>(Lua lua)
{
#region Sanity checks
if (lua == null) throw new ArgumentNullException("lua");
#endregion
Type type = typeof(T);
if (!type.IsEnum) throw new ArgumentException("The type must be an enumeration!");
string[] names = Enum.GetNames(type);
var values = (T[])Enum.GetValues(type);
lua.NewTable(type.Name);
for (int i = 0; i < names.Length; i++)
{
string path = type.Name + "." + names[i];
lua[path] = values[i];
}
}
#endregion
}
}
using System;
namespace LuaInterface
{
/// <summary>
/// Exceptions thrown by the Lua runtime because of errors in the script
/// </summary>
public class LuaScriptException : LuaException
{
/// <summary>
/// Returns true if the exception has occured as the result of a .NET exception in user code
/// </summary>
public bool IsNetException { get; private set; }
private readonly string source;
/// <summary>
/// The position in the script where the exception was triggered.
/// </summary>
public override string Source { get { return source; } }
/// <summary>
/// Creates a new Lua-only exception.
/// </summary>
/// <param name="message">The message that describes the error.</param>
/// <param name="source">The position in the script where the exception was triggered.</param>
public LuaScriptException(string message, string source) : base(message)
{
this.source = source;
}
/// <summary>
/// Creates a new .NET wrapping exception.
/// </summary>
/// <param name="innerException">The .NET exception triggered by user-code.</param>
/// <param name="source">The position in the script where the exception was triggered.</param>
public LuaScriptException(Exception innerException, string source)
: base("A .NET exception occured in user-code", innerException)
{
this.source = source;
this.IsNetException = true;
}
public override string ToString()
{
// Prepend the error source
return GetType().FullName + ": " + source + Message;
}
}
}
\ No newline at end of file
using System;
using System.Collections.Generic;
using System.Text;
using Lua511;
using System.Collections;
namespace LuaInterface
{
/*
* Wrapper class for Lua tables
*
* Author: Fabio Mascarenhas
* Version: 1.0
*/
public class LuaTable : LuaBase
{
//internal int _Reference;
//private Lua _Interpreter;
public LuaTable(int reference, Lua interpreter)
{
_Reference = reference;
_Interpreter = interpreter;
}
//bool disposed = false;
//~LuaTable()
//{
// Dispose(false);
//}
//public void Dispose()
//{
// Dispose(true);
// GC.SuppressFinalize(this);
//}
//public virtual void Dispose(bool disposeManagedResources)
//{
// if (!this.disposed)
// {
// if (disposeManagedResources)
// {
// if (_Reference != 0)
// _Interpreter.dispose(_Reference);
// }
// disposed = true;
// }
//}
//~LuaTable()
//{
// _Interpreter.dispose(_Reference);
//}
/*
* Indexer for string fields of the table
*/
public object this[string field]
{
get
{
return _Interpreter.getObject(_Reference, field);
}
set
{
_Interpreter.setObject(_Reference, field, value);
}
}
/*
* Indexer for numeric fields of the table
*/
public object this[object field]
{
get
{
return _Interpreter.getObject(_Reference, field);
}
set
{
_Interpreter.setObject(_Reference, field, value);
}
}
public System.Collections.IDictionaryEnumerator GetEnumerator()
{
return _Interpreter.GetTableDict(this).GetEnumerator();
}
public ICollection Keys
{
get { return _Interpreter.GetTableDict(this).Keys; }
}
public ICollection Values
{
get { return _Interpreter.GetTableDict(this).Values; }
}
/*
* Gets an string fields of a table ignoring its metatable,
* if it exists
*/
internal object rawget(string field)
{
return _Interpreter.rawGetObject(_Reference, field);
}
internal object rawgetFunction(string field)
{
object obj = _Interpreter.rawGetObject(_Reference, field);
if (obj is LuaCSFunction)
return new LuaFunction((LuaCSFunction)obj, _Interpreter);
else
return obj;
}
/*
* Pushes this table into the Lua stack
*/
internal void push(IntPtr luaState)
{
LuaDLL.lua_getref(luaState, _Reference);
}
public override string ToString()
{
return "table";
}
//public override bool Equals(object o)
//{
// if (o is LuaTable)
// {
// LuaTable l = (LuaTable)o;
// return _Interpreter.compareRef(l._Reference, _Reference);
// }
// else return false;
//}
//public override int GetHashCode()
//{
// return _Reference;
//}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using Lua511;
namespace LuaInterface
{
public class LuaUserData : LuaBase
{
//internal int _Reference;
//private Lua _Interpreter;
public LuaUserData(int reference, Lua interpreter)
{
_Reference = reference;
_Interpreter = interpreter;
}
//~LuaUserData()
//{
// if (_Reference != 0)
// _Interpreter.dispose(_Reference);
//}
/*
* Indexer for string fields of the userdata
*/
public object this[string field]
{
get
{
return _Interpreter.getObject(_Reference, field);
}
set
{
_Interpreter.setObject(_Reference, field, value);
}
}
/*
* Indexer for numeric fields of the userdata
*/
public object this[object field]
{
get
{
return _Interpreter.getObject(_Reference, field);
}
set
{
_Interpreter.setObject(_Reference, field, value);
}
}
/*
* Calls the userdata and returns its return values inside
* an array
*/
public object[] Call(params object[] args)
{
return _Interpreter.callFunction(this, args);
}
/*
* Pushes the userdata into the Lua stack
*/
internal void push(IntPtr luaState)
{
LuaDLL.lua_getref(luaState, _Reference);
}
public override string ToString()
{
return "userdata";
}
//public override bool Equals(object o)
//{
// if (o is LuaUserData)
// {
// LuaUserData l = (LuaUserData)o;
// return _Interpreter.compareRef(l._Reference, _Reference);
// }
// else return false;
//}
//public override int GetHashCode()
//{
// return _Reference;
//}
}
}
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
......@@ -22,7 +22,7 @@ using System.Security.Permissions;
// You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("2.0.1.*")]
[assembly: AssemblyVersion("2.0.4.*")]
//
// In order to sign your assembly you must specify a key to use. Refer to the
......
......@@ -9,6 +9,7 @@ namespace LuaInterface
/// </summary>
public class ProxyType : IReflect
{
Type proxy;
public ProxyType(Type proxy)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment