Commit 8a76b646 authored by Megax's avatar Megax
Browse files

* LuaInterface szerkezeti felepitese lasacskan at lesz formalva. Amikor lesz...

* LuaInterface szerkezeti felepitese lasacskan at lesz formalva. Amikor lesz idom es kedvem folytatom az atalakitast.
parent 02869e90
/*
* This file is part of LuaInterface.
*
* Copyright (C) 2003-2005 Fabio Mascarenhas de Queiroz.
* Copyright (C) 2012 Megax <http://megax.yeahunter.hu/>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
using System;
namespace LuaInterface
{
/*
* Class used for generating delegates that get a table from the Lua
* stack as a an object of a specific type.
*
* Author: Fabio Mascarenhas
* Version: 1.0
*/
class ClassGenerator
{
private ObjectTranslator translator;
private Type klass;
public ClassGenerator(ObjectTranslator translator, Type klass)
{
this.translator = translator;
this.klass = klass;
}
public object extractGenerated(KopiLua.Lua.lua_State luaState, int stackPos)
{
return CodeGeneration.Instance.GetClassInstance(klass, translator.getTable(luaState, stackPos));
}
}
}
\ No newline at end of file
/*
* This file is part of LuaInterface.
*
* Copyright (C) 2003-2005 Fabio Mascarenhas de Queiroz.
* Copyright (C) 2012 Megax <http://megax.yeahunter.hu/>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
using System; using System;
using System.Collections; using System.Threading;
using System.Collections.Generic;
using System.Reflection; using System.Reflection;
using System.Reflection.Emit; using System.Reflection.Emit;
using System.Threading; using System.Collections;
using System.Collections.Generic;
namespace LuaInterface namespace LuaInterface
{ {
/*
* Structure to store a type and the return types of
* its methods (the type of the returned value and out/ref
* parameters).
*/
struct LuaClassType
{
public Type klass;
public Type[][] returnTypes;
}
/*
* Common interface for types generated from tables. The method
* returns the table that overrides some or all of the type's methods.
*/
public interface ILuaGeneratedType
{
LuaTable __luaInterface_getLuaTable();
}
/*
* Class used for generating delegates that get a function from the Lua
* stack as a delegate of a specific type.
*
* Author: Fabio Mascarenhas
* Version: 1.0
*/
class DelegateGenerator
{
private ObjectTranslator translator;
private Type delegateType;
public DelegateGenerator(ObjectTranslator translator,Type delegateType)
{
this.translator=translator;
this.delegateType=delegateType;
}
public object extractGenerated(KopiLua.Lua.lua_State luaState,int stackPos)
{
return CodeGeneration.Instance.GetDelegate(delegateType,translator.getFunction(luaState,stackPos));
}
}
/*
* Class used for generating delegates that get a table from the Lua
* stack as a an object of a specific type.
*
* Author: Fabio Mascarenhas
* Version: 1.0
*/
class ClassGenerator
{
private ObjectTranslator translator;
private Type klass;
public ClassGenerator(ObjectTranslator translator,Type klass)
{
this.translator=translator;
this.klass=klass;
}
public object extractGenerated(KopiLua.Lua.lua_State luaState,int stackPos)
{
return CodeGeneration.Instance.GetClassInstance(klass,translator.getTable(luaState,stackPos));
}
}
/* /*
* Dynamically generates new types from existing types and * Dynamically generates new types from existing types and
* Lua function and table values. Generated types are event handlers, * Lua function and table values. Generated types are event handlers,
* delegates, interface implementations and subclasses. * delegates, interface implementations and subclasses.
* *
* Author: Fabio Mascarenhas * Author: Fabio Mascarenhas
...@@ -83,34 +42,30 @@ namespace LuaInterface ...@@ -83,34 +42,30 @@ namespace LuaInterface
*/ */
class CodeGeneration class CodeGeneration
{ {
private Type eventHandlerParent=typeof(LuaEventHandler); private Dictionary<Type, LuaClassType> classCollection = new Dictionary<Type, LuaClassType>();
private Dictionary<Type, Type> eventHandlerCollection=new Dictionary<Type, Type>(); private Dictionary<Type, Type> eventHandlerCollection = new Dictionary<Type, Type>();
private Dictionary<Type, Type> delegateCollection = new Dictionary<Type, Type>();
private Type delegateParent=typeof(LuaDelegate); private static readonly CodeGeneration instance = new CodeGeneration();
private Dictionary<Type, Type> delegateCollection=new Dictionary<Type, Type>(); private Type eventHandlerParent = typeof(LuaEventHandler);
private Type delegateParent = typeof(LuaDelegate);
private Type classHelper=typeof(LuaClassHelper); private Type classHelper = typeof(LuaClassHelper);
private Dictionary<Type, LuaClassType> classCollection=new Dictionary<Type, LuaClassType>();
private AssemblyName assemblyName; private AssemblyName assemblyName;
private AssemblyBuilder newAssembly; private AssemblyBuilder newAssembly;
private ModuleBuilder newModule; private ModuleBuilder newModule;
private int luaClassNumber=1; private int luaClassNumber = 1;
private static readonly CodeGeneration instance = new CodeGeneration();
static CodeGeneration() static CodeGeneration()
{ {
} }
private CodeGeneration() private CodeGeneration()
{ {
// Create an assembly name // Create an assembly name
assemblyName=new AssemblyName( ); assemblyName = new AssemblyName();
assemblyName.Name="LuaInterface_generatedcode"; assemblyName.Name = "LuaInterface_generatedcode";
// Create a new assembly with one module. // Create a new assembly with one module.
newAssembly=Thread.GetDomain().DefineDynamicAssembly( newAssembly = Thread.GetDomain().DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
assemblyName, AssemblyBuilderAccess.Run); newModule = newAssembly.DefineDynamicModule("LuaInterface_generatedcode");
newModule=newAssembly.DefineDynamicModule("LuaInterface_generatedcode");
} }
/* /*
...@@ -118,10 +73,7 @@ namespace LuaInterface ...@@ -118,10 +73,7 @@ namespace LuaInterface
*/ */
public static CodeGeneration Instance public static CodeGeneration Instance
{ {
get get { return instance; }
{
return instance;
}
} }
/* /*
...@@ -135,30 +87,27 @@ namespace LuaInterface ...@@ -135,30 +87,27 @@ namespace LuaInterface
typeName = "LuaGeneratedClass" + luaClassNumber; typeName = "LuaGeneratedClass" + luaClassNumber;
luaClassNumber++; luaClassNumber++;
} }
// Define a public class in the assembly, called typeName // Define a public class in the assembly, called typeName
TypeBuilder myType=newModule.DefineType(typeName,TypeAttributes.Public,eventHandlerParent); var myType = newModule.DefineType(typeName, TypeAttributes.Public, eventHandlerParent);
// Defines the handler method. Its signature is void(object,<subclassofEventArgs>) // Defines the handler method. Its signature is void(object, <subclassofEventArgs>)
Type[] paramTypes = new Type[2]; var paramTypes = new Type[2];
paramTypes[0]=typeof(object); paramTypes[0] = typeof(object);
paramTypes[1]=eventHandlerType; paramTypes[1] = eventHandlerType;
Type returnType=typeof(void); var returnType = typeof(void);
MethodBuilder handleMethod=myType.DefineMethod("HandleEvent", var handleMethod = myType.DefineMethod("HandleEvent", MethodAttributes.Public | MethodAttributes.HideBySig, returnType, paramTypes);
MethodAttributes.Public|MethodAttributes.HideBySig,
returnType,paramTypes);
// Emits the IL for the method. It loads the arguments // Emits the IL for the method. It loads the arguments
// and calls the handleEvent method of the base class // 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_0);
generator.Emit(OpCodes.Ldarg_1); generator.Emit(OpCodes.Ldarg_1);
generator.Emit(OpCodes.Ldarg_2); generator.Emit(OpCodes.Ldarg_2);
MethodInfo miGenericEventHandler; var miGenericEventHandler = eventHandlerParent.GetMethod("handleEvent");
miGenericEventHandler=eventHandlerParent.GetMethod("handleEvent"); generator.Emit(OpCodes.Call, miGenericEventHandler);
generator.Emit(OpCodes.Call,miGenericEventHandler);
// returns // returns
generator.Emit(OpCodes.Ret); generator.Emit(OpCodes.Ret);
// creates the new type // creates the new type
return myType.CreateType(); return myType.CreateType();
} }
...@@ -175,112 +124,126 @@ namespace LuaInterface ...@@ -175,112 +124,126 @@ namespace LuaInterface
typeName = "LuaGeneratedClass" + luaClassNumber; typeName = "LuaGeneratedClass" + luaClassNumber;
luaClassNumber++; luaClassNumber++;
} }
// Define a public class in the assembly, called typeName // Define a public class in the assembly, called typeName
TypeBuilder myType=newModule.DefineType(typeName,TypeAttributes.Public,delegateParent); var myType = newModule.DefineType(typeName, TypeAttributes.Public, delegateParent);
// Defines the delegate method with the same signature as the // Defines the delegate method with the same signature as the
// Invoke method of delegateType // Invoke method of delegateType
MethodInfo invokeMethod=delegateType.GetMethod("Invoke"); var invokeMethod = delegateType.GetMethod("Invoke");
ParameterInfo[] paramInfo=invokeMethod.GetParameters(); var paramInfo = invokeMethod.GetParameters();
Type[] paramTypes=new Type[paramInfo.Length]; var paramTypes = new Type[paramInfo.Length];
Type returnType=invokeMethod.ReturnType; var returnType = invokeMethod.ReturnType;
// Counts out and ref params, for use later // Counts out and ref params, for use later
int nOutParams=0; int nOutAndRefParams=0; int nOutParams = 0; int nOutAndRefParams = 0;
for(int i=0;i<paramTypes.Length;i++)
for(int i = 0; i < paramTypes.Length; i++)
{ {
paramTypes[i]=paramInfo[i].ParameterType; paramTypes[i] = paramInfo[i].ParameterType;
if((!paramInfo[i].IsIn) && paramInfo[i].IsOut) if((!paramInfo[i].IsIn) && paramInfo[i].IsOut)
nOutParams++; nOutParams++;
if(paramTypes[i].IsByRef) if(paramTypes[i].IsByRef)
nOutAndRefParams++; nOutAndRefParams++;
} }
int[] refArgs=new int[nOutAndRefParams];
MethodBuilder delegateMethod=myType.DefineMethod("CallFunction", int[] refArgs = new int[nOutAndRefParams];
invokeMethod.Attributes, var delegateMethod = myType.DefineMethod("CallFunction", invokeMethod.Attributes, returnType, paramTypes);
returnType,paramTypes);
// Generates the IL for the method // Generates the IL for the method
ILGenerator generator=delegateMethod.GetILGenerator( ); ILGenerator generator = delegateMethod.GetILGenerator( );
generator.DeclareLocal(typeof(object[])); // original arguments generator.DeclareLocal(typeof(object[])); // original arguments
generator.DeclareLocal(typeof(object[])); // with out-only arguments removed generator.DeclareLocal(typeof(object[])); // with out-only arguments removed
generator.DeclareLocal(typeof(int[])); // indexes of out and ref arguments generator.DeclareLocal(typeof(int[])); // indexes of out and ref arguments
if(!(returnType == typeof(void))) // return value if(!(returnType == typeof(void))) // return value
generator.DeclareLocal(returnType); generator.DeclareLocal(returnType);
else else
generator.DeclareLocal(typeof(object)); generator.DeclareLocal(typeof(object));
// Initializes local variables // Initializes local variables
generator.Emit(OpCodes.Ldc_I4,paramTypes.Length); generator.Emit(OpCodes.Ldc_I4, paramTypes.Length);
generator.Emit(OpCodes.Newarr,typeof(object)); generator.Emit(OpCodes.Newarr, typeof(object));
generator.Emit(OpCodes.Stloc_0); generator.Emit(OpCodes.Stloc_0);
generator.Emit(OpCodes.Ldc_I4,paramTypes.Length-nOutParams); generator.Emit(OpCodes.Ldc_I4, paramTypes.Length-nOutParams);
generator.Emit(OpCodes.Newarr,typeof(object)); generator.Emit(OpCodes.Newarr, typeof(object));
generator.Emit(OpCodes.Stloc_1); generator.Emit(OpCodes.Stloc_1);
generator.Emit(OpCodes.Ldc_I4,nOutAndRefParams); generator.Emit(OpCodes.Ldc_I4, nOutAndRefParams);
generator.Emit(OpCodes.Newarr,typeof(int)); generator.Emit(OpCodes.Newarr, typeof(int));
generator.Emit(OpCodes.Stloc_2); generator.Emit(OpCodes.Stloc_2);
// Stores the arguments in the local variables // Stores the arguments in the local variables
for(int iArgs=0,iInArgs=0,iOutArgs=0;iArgs<paramTypes.Length;iArgs++) for(int iArgs = 0, iInArgs = 0, iOutArgs = 0; iArgs < paramTypes.Length; iArgs++)
{ {
generator.Emit(OpCodes.Ldloc_0); generator.Emit(OpCodes.Ldloc_0);
generator.Emit(OpCodes.Ldc_I4,iArgs); generator.Emit(OpCodes.Ldc_I4, iArgs);
generator.Emit(OpCodes.Ldarg,iArgs+1); generator.Emit(OpCodes.Ldarg, iArgs+1);
if(paramTypes[iArgs].IsByRef) if(paramTypes[iArgs].IsByRef)
{ {
if(paramTypes[iArgs].GetElementType().IsValueType) if(paramTypes[iArgs].GetElementType().IsValueType)
{ {
generator.Emit(OpCodes.Ldobj,paramTypes[iArgs].GetElementType()); generator.Emit(OpCodes.Ldobj, paramTypes[iArgs].GetElementType());
generator.Emit(OpCodes.Box,paramTypes[iArgs].GetElementType()); generator.Emit(OpCodes.Box, paramTypes[iArgs].GetElementType());
} else generator.Emit(OpCodes.Ldind_Ref); }
else
generator.Emit(OpCodes.Ldind_Ref);
} }
else else
{ {
if(paramTypes[iArgs].IsValueType) if(paramTypes[iArgs].IsValueType)
generator.Emit(OpCodes.Box,paramTypes[iArgs]); generator.Emit(OpCodes.Box, paramTypes[iArgs]);
} }
generator.Emit(OpCodes.Stelem_Ref); generator.Emit(OpCodes.Stelem_Ref);
if(paramTypes[iArgs].IsByRef) if(paramTypes[iArgs].IsByRef)
{ {
generator.Emit(OpCodes.Ldloc_2); generator.Emit(OpCodes.Ldloc_2);
generator.Emit(OpCodes.Ldc_I4,iOutArgs); generator.Emit(OpCodes.Ldc_I4, iOutArgs);
generator.Emit(OpCodes.Ldc_I4,iArgs); generator.Emit(OpCodes.Ldc_I4, iArgs);
generator.Emit(OpCodes.Stelem_I4); generator.Emit(OpCodes.Stelem_I4);
refArgs[iOutArgs]=iArgs; refArgs[iOutArgs] = iArgs;
iOutArgs++; iOutArgs++;
} }
if(paramInfo[iArgs].IsIn || (!paramInfo[iArgs].IsOut)) if(paramInfo[iArgs].IsIn || (!paramInfo[iArgs].IsOut))
{ {
generator.Emit(OpCodes.Ldloc_1); generator.Emit(OpCodes.Ldloc_1);
generator.Emit(OpCodes.Ldc_I4,iInArgs); generator.Emit(OpCodes.Ldc_I4, iInArgs);
generator.Emit(OpCodes.Ldarg,iArgs+1); generator.Emit(OpCodes.Ldarg, iArgs+1);
if(paramTypes[iArgs].IsByRef) if(paramTypes[iArgs].IsByRef)
{ {
if(paramTypes[iArgs].GetElementType().IsValueType) if(paramTypes[iArgs].GetElementType().IsValueType)
{ {
generator.Emit(OpCodes.Ldobj,paramTypes[iArgs].GetElementType()); generator.Emit(OpCodes.Ldobj, paramTypes[iArgs].GetElementType());
generator.Emit(OpCodes.Box,paramTypes[iArgs].GetElementType()); generator.Emit(OpCodes.Box, paramTypes[iArgs].GetElementType());
} }
else generator.Emit(OpCodes.Ldind_Ref); else
generator.Emit(OpCodes.Ldind_Ref);
} }
else else
{ {
if(paramTypes[iArgs].IsValueType) if(paramTypes[iArgs].IsValueType)
generator.Emit(OpCodes.Box,paramTypes[iArgs]); generator.Emit(OpCodes.Box, paramTypes[iArgs]);
} }
generator.Emit(OpCodes.Stelem_Ref); generator.Emit(OpCodes.Stelem_Ref);
iInArgs++; iInArgs++;
} }
} }
// Calls the callFunction method of the base class // Calls the callFunction method of the base class
generator.Emit(OpCodes.Ldarg_0); generator.Emit(OpCodes.Ldarg_0);
generator.Emit(OpCodes.Ldloc_0); generator.Emit(OpCodes.Ldloc_0);
generator.Emit(OpCodes.Ldloc_1); generator.Emit(OpCodes.Ldloc_1);
generator.Emit(OpCodes.Ldloc_2); generator.Emit(OpCodes.Ldloc_2);
MethodInfo miGenericEventHandler; var miGenericEventHandler = delegateParent.GetMethod("callFunction");
miGenericEventHandler=delegateParent.GetMethod("callFunction"); generator.Emit(OpCodes.Call, miGenericEventHandler);
generator.Emit(OpCodes.Call,miGenericEventHandler);
// Stores return value // Stores return value
if(returnType == typeof(void)) if(returnType == typeof(void))
{ {
...@@ -289,43 +252,48 @@ namespace LuaInterface ...@@ -289,43 +252,48 @@ namespace LuaInterface
} }
else if(returnType.IsValueType) else if(returnType.IsValueType)
{ {
generator.Emit(OpCodes.Unbox,returnType); generator.Emit(OpCodes.Unbox, returnType);
generator.Emit(OpCodes.Ldobj,returnType); generator.Emit(OpCodes.Ldobj, returnType);
} else generator.Emit(OpCodes.Castclass,returnType); }
else
generator.Emit(OpCodes.Castclass, returnType);
generator.Emit(OpCodes.Stloc_3); generator.Emit(OpCodes.Stloc_3);
// Stores new value of out and ref params // Stores new value of out and ref params
for(int i=0;i<refArgs.Length;i++) for(int i = 0; i < refArgs.Length; i++)
{ {
generator.Emit(OpCodes.Ldarg,refArgs[i]+1); generator.Emit(OpCodes.Ldarg, refArgs[i]+1);
generator.Emit(OpCodes.Ldloc_0); generator.Emit(OpCodes.Ldloc_0);
generator.Emit(OpCodes.Ldc_I4,refArgs[i]); generator.Emit(OpCodes.Ldc_I4, refArgs[i]);
generator.Emit(OpCodes.Ldelem_Ref); generator.Emit(OpCodes.Ldelem_Ref);
if(paramTypes[refArgs[i]].GetElementType().IsValueType) if(paramTypes[refArgs[i]].GetElementType().IsValueType)
{ {
generator.Emit(OpCodes.Unbox,paramTypes[refArgs[i]].GetElementType()); generator.Emit(OpCodes.Unbox, paramTypes[refArgs[i]].GetElementType());
generator.Emit(OpCodes.Ldobj,paramTypes[refArgs[i]].GetElementType()); generator.Emit(OpCodes.Ldobj, paramTypes[refArgs[i]].GetElementType());
generator.Emit(OpCodes.Stobj,paramTypes[refArgs[i]].GetElementType()); generator.Emit(OpCodes.Stobj, paramTypes[refArgs[i]].GetElementType());
} }
else else
{ {
generator.Emit(OpCodes.Castclass,paramTypes[refArgs[i]].GetElementType()); generator.Emit(OpCodes.Castclass, paramTypes[refArgs[i]].GetElementType());
generator.Emit(OpCodes.Stind_Ref); generator.Emit(OpCodes.Stind_Ref);
} }
} }
// Returns // Returns
if(!(returnType == typeof(void))) if(!(returnType == typeof(void)))
generator.Emit(OpCodes.Ldloc_3); generator.Emit(OpCodes.Ldloc_3);
generator.Emit(OpCodes.Ret); generator.Emit(OpCodes.Ret);
return myType.CreateType(); // creates the new type
// creates the new type
return myType.CreateType();
} }
/* /*
* Generates an implementation of klass, if it is an interface, or * Generates an implementation of klass, if it is an interface, or
* a subclass of klass that delegates its virtual methods to a Lua table. * a subclass of klass that delegates its virtual methods to a Lua table.
*/ */
public void GenerateClass(Type klass,out Type newType,out Type[][] returnTypes) public void GenerateClass(Type klass, out Type newType, out Type[][] returnTypes)
{ {
string typeName; string typeName;
lock(this) lock(this)
...@@ -333,66 +301,68 @@ namespace LuaInterface ...@@ -333,66 +301,68 @@ namespace LuaInterface
typeName = "LuaGeneratedClass" + luaClassNumber; typeName = "LuaGeneratedClass" + luaClassNumber;
luaClassNumber++; luaClassNumber++;
} }
TypeBuilder myType; TypeBuilder myType;
// Define a public class in the assembly, called typeName // Define a public class in the assembly, called typeName
if(klass.IsInterface) if(klass.IsInterface)
myType=newModule.DefineType(typeName,TypeAttributes.Public,typeof(object),new Type[] { klass,typeof(ILuaGeneratedType) }); myType = newModule.DefineType(typeName, TypeAttributes.Public, typeof(object), new Type[] { klass, typeof(ILuaGeneratedType) });
else else
myType=newModule.DefineType(typeName,TypeAttributes.Public,klass,new Type[] { typeof(ILuaGeneratedType) }); myType = newModule.DefineType(typeName, TypeAttributes.Public, klass, new Type[] { typeof(ILuaGeneratedType) });
// Field that stores the Lua table // Field that stores the Lua table
FieldBuilder luaTableField=myType.DefineField("__luaInterface_luaTable",typeof(LuaTable),FieldAttributes.Public); var luaTableField = myType.DefineField("__luaInterface_luaTable", typeof(LuaTable), FieldAttributes.Public);
// Field that stores the return types array // Field that stores the return types array
FieldBuilder returnTypesField=myType.DefineField("__luaInterface_returnTypes",typeof(Type[][]),FieldAttributes.Public); var returnTypesField = myType.DefineField("__luaInterface_returnTypes", typeof(Type[][]), FieldAttributes.Public);
// Generates the constructor for the new type, it takes a Lua table and an array // Generates the constructor for the new type, it takes a Lua table and an array
// of return types and stores them in the respective fields // of return types and stores them in the respective fields
ConstructorBuilder constructor= var constructor = myType.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new Type[] { typeof(LuaTable), typeof(Type[][]) });
myType.DefineConstructor(MethodAttributes.Public,CallingConventions.Standard,new Type[] { typeof(LuaTable),typeof(Type[][]) }); ILGenerator generator = constructor.GetILGenerator();
ILGenerator generator=constructor.GetILGenerator();
generator.Emit(OpCodes.Ldarg_0); generator.Emit(OpCodes.Ldarg_0);
if(klass.IsInterface) if(klass.IsInterface)
generator.Emit(OpCodes.Call,typeof(object).GetConstructor(Type.EmptyTypes)); generator.Emit(OpCodes.Call, typeof(object).GetConstructor(Type.EmptyTypes));
else else
generator.Emit(OpCodes.Call,klass.GetConstructor(Type.EmptyTypes)); generator.Emit(OpCodes.Call, klass.GetConstructor(Type.EmptyTypes));
generator.Emit(OpCodes.Ldarg_0); generator.Emit(OpCodes.Ldarg_0);
generator.Emit(OpCodes.Ldarg_1); generator.Emit(OpCodes.Ldarg_1);
generator.Emit(OpCodes.Stfld,luaTableField); generator.Emit(OpCodes.Stfld, luaTableField);
generator.Emit(OpCodes.Ldarg_0); generator.Emit(OpCodes.Ldarg_0);
generator.Emit(OpCodes.Ldarg_2); generator.Emit(OpCodes.Ldarg_2);
generator.Emit(OpCodes.Stfld,returnTypesField); generator.Emit(OpCodes.Stfld, returnTypesField);
generator.Emit(OpCodes.Ret); generator.Emit(OpCodes.Ret);
// Generates overriden versions of the klass' public virtual methods // Generates overriden versions of the klass' public virtual methods
MethodInfo[] classMethods=klass.GetMethods(); var classMethods = klass.GetMethods();
returnTypes=new Type[classMethods.Length][]; returnTypes = new Type[classMethods.Length][];
int i=0; int i = 0;
foreach(MethodInfo method in classMethods)
foreach(var method in classMethods)
{ {
if(klass.IsInterface) if(klass.IsInterface)
{ {
GenerateMethod(myType,method, GenerateMethod(myType, method, MethodAttributes.HideBySig|MethodAttributes.Virtual|MethodAttributes.NewSlot,
MethodAttributes.HideBySig|MethodAttributes.Virtual|MethodAttributes.NewSlot, i, luaTableField, returnTypesField, false, out returnTypes[i]);
i,luaTableField,returnTypesField,false,out returnTypes[i]);
i++; i++;
} }
else else
{ {
if(!method.IsPrivate && !method.IsFinal && method.IsVirtual) if(!method.IsPrivate && !method.IsFinal && method.IsVirtual)
{ {
GenerateMethod(myType,method,(method.Attributes|MethodAttributes.NewSlot)^MethodAttributes.NewSlot,i, GenerateMethod(myType, method, (method.Attributes|MethodAttributes.NewSlot)^MethodAttributes.NewSlot, i,
luaTableField,returnTypesField,true,out returnTypes[i]); luaTableField, returnTypesField, true, out returnTypes[i]);
i++; i++;
} }
} }
} }
// Generates an implementation of the __luaInterface_getLuaTable method // Generates an implementation of the __luaInterface_getLuaTable method
MethodBuilder returnTableMethod=myType.DefineMethod("__luaInterface_getLuaTable", var returnTableMethod = myType.DefineMethod("__luaInterface_getLuaTable",
MethodAttributes.Public|MethodAttributes.HideBySig|MethodAttributes.Virtual, MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.Virtual, typeof(LuaTable), new Type[0]);
typeof(LuaTable),new Type[0]); myType.DefineMethodOverride(returnTableMethod, typeof(ILuaGeneratedType).GetMethod("__luaInterface_getLuaTable"));
myType.DefineMethodOverride(returnTableMethod,typeof(ILuaGeneratedType).GetMethod("__luaInterface_getLuaTable")); generator = returnTableMethod.GetILGenerator();
generator=returnTableMethod.GetILGenerator(); generator.Emit(OpCodes.Ldfld, luaTableField);
generator.Emit(OpCodes.Ldfld,luaTableField);
generator.Emit(OpCodes.Ret); generator.Emit(OpCodes.Ret);
// Creates the type newType = myType.CreateType(); // Creates the type
newType=myType.CreateType();
} }
/* /*
...@@ -401,170 +371,197 @@ namespace LuaInterface ...@@ -401,170 +371,197 @@ namespace LuaInterface
* doesn't the method calls the base method (or does nothing, in case of interface * doesn't the method calls the base method (or does nothing, in case of interface
* implementations). * implementations).
*/ */
private void GenerateMethod(TypeBuilder myType,MethodInfo method,MethodAttributes attributes, private void GenerateMethod(TypeBuilder myType, MethodInfo method, MethodAttributes attributes, int methodIndex,
int methodIndex,FieldInfo luaTableField,FieldInfo returnTypesField,bool generateBase,out Type[] returnTypes) FieldInfo luaTableField, FieldInfo returnTypesField, bool generateBase, out Type[] returnTypes)
{ {
ParameterInfo[] paramInfo=method.GetParameters(); var paramInfo = method.GetParameters();
Type[] paramTypes=new Type[paramInfo.Length]; var paramTypes = new Type[paramInfo.Length];
List<Type> returnTypesList=new List<Type>(); var returnTypesList = new List<Type>();
// Counts out and ref parameters, for later use, // Counts out and ref parameters, for later use,
// and creates the list of return types // and creates the list of return types
int nOutParams=0; int nOutAndRefParams=0; int nOutParams = 0;
Type returnType=method.ReturnType; int nOutAndRefParams = 0;
var returnType = method.ReturnType;
returnTypesList.Add(returnType); returnTypesList.Add(returnType);
for(int i=0;i<paramTypes.Length;i++)
for(int i = 0; i < paramTypes.Length; i++)
{ {
paramTypes[i]=paramInfo[i].ParameterType; paramTypes[i] = paramInfo[i].ParameterType;
if((!paramInfo[i].IsIn) && paramInfo[i].IsOut) if((!paramInfo[i].IsIn) && paramInfo[i].IsOut)
nOutParams++; nOutParams++;
if(paramTypes[i].IsByRef) if(paramTypes[i].IsByRef)
{ {
returnTypesList.Add(paramTypes[i].GetElementType()); returnTypesList.Add(paramTypes[i].GetElementType());
nOutAndRefParams++; nOutAndRefParams++;
} }
} }
int[] refArgs=new int[nOutAndRefParams];
returnTypes=returnTypesList.ToArray(); int[] refArgs = new int[nOutAndRefParams];
returnTypes = returnTypesList.ToArray();
// Generates a version of the method that calls the base implementation // Generates a version of the method that calls the base implementation
// directly, for use by the base field of the table // directly, for use by the base field of the table
if(generateBase) if(generateBase)
{ {
MethodBuilder baseMethod=myType.DefineMethod("__luaInterface_base_"+method.Name, var baseMethod = myType.DefineMethod("__luaInterface_base_"+method.Name,
MethodAttributes.Private|MethodAttributes.NewSlot|MethodAttributes.HideBySig, MethodAttributes.Private|MethodAttributes.NewSlot|MethodAttributes.HideBySig,
returnType,paramTypes); returnType, paramTypes);
ILGenerator generatorBase=baseMethod.GetILGenerator(); ILGenerator generatorBase = baseMethod.GetILGenerator();
generatorBase.Emit(OpCodes.Ldarg_0); generatorBase.Emit(OpCodes.Ldarg_0);
for(int i=0;i<paramTypes.Length;i++)
generatorBase.Emit(OpCodes.Ldarg,i+1); for(int i = 0; i < paramTypes.Length; i++)
generatorBase.Emit(OpCodes.Call,method); generatorBase.Emit(OpCodes.Ldarg, i+1);
generatorBase.Emit(OpCodes.Call, method);
if(returnType == typeof(void)) if(returnType == typeof(void))
generatorBase.Emit(OpCodes.Pop); generatorBase.Emit(OpCodes.Pop);
generatorBase.Emit(OpCodes.Ret); generatorBase.Emit(OpCodes.Ret);
} }
// Defines the method // Defines the method
MethodBuilder methodImpl=myType.DefineMethod(method.Name,attributes, var methodImpl = myType.DefineMethod(method.Name, attributes, returnType, paramTypes);
returnType,paramTypes);
// If it's an implementation of an interface tells what method it // If it's an implementation of an interface tells what method it
// is overriding // is overriding
if(myType.BaseType.Equals(typeof(object))) if(myType.BaseType.Equals(typeof(object)))
myType.DefineMethodOverride(methodImpl,method); myType.DefineMethodOverride(methodImpl, method);
ILGenerator generator=methodImpl.GetILGenerator( );
ILGenerator generator = methodImpl.GetILGenerator( );
generator.DeclareLocal(typeof(object[])); // original arguments generator.DeclareLocal(typeof(object[])); // original arguments
generator.DeclareLocal(typeof(object[])); // with out-only arguments removed generator.DeclareLocal(typeof(object[])); // with out-only arguments removed
generator.DeclareLocal(typeof(int[])); // indexes of out and ref arguments generator.DeclareLocal(typeof(int[])); // indexes of out and ref arguments
if(!(returnType == typeof(void))) // return value if(!(returnType == typeof(void))) // return value
generator.DeclareLocal(returnType); generator.DeclareLocal(returnType);
else else
generator.DeclareLocal(typeof(object)); generator.DeclareLocal(typeof(object));
// Initializes local variables // Initializes local variables
generator.Emit(OpCodes.Ldc_I4,paramTypes.Length); generator.Emit(OpCodes.Ldc_I4, paramTypes.Length);
generator.Emit(OpCodes.Newarr,typeof(object)); generator.Emit(OpCodes.Newarr, typeof(object));
generator.Emit(OpCodes.Stloc_0); generator.Emit(OpCodes.Stloc_0);
generator.Emit(OpCodes.Ldc_I4,paramTypes.Length-nOutParams+1); generator.Emit(OpCodes.Ldc_I4, paramTypes.Length-nOutParams+1);
generator.Emit(OpCodes.Newarr,typeof(object)); generator.Emit(OpCodes.Newarr, typeof(object));
generator.Emit(OpCodes.Stloc_1); generator.Emit(OpCodes.Stloc_1);
generator.Emit(OpCodes.Ldc_I4,nOutAndRefParams); generator.Emit(OpCodes.Ldc_I4, nOutAndRefParams);
generator.Emit(OpCodes.Newarr,typeof(int)); generator.Emit(OpCodes.Newarr, typeof(int));
generator.Emit(OpCodes.Stloc_2); generator.Emit(OpCodes.Stloc_2);
generator.Emit(OpCodes.Ldloc_1); generator.Emit(OpCodes.Ldloc_1);
generator.Emit(OpCodes.Ldc_I4_0); generator.Emit(OpCodes.Ldc_I4_0);
generator.Emit(OpCodes.Ldarg_0); generator.Emit(OpCodes.Ldarg_0);
generator.Emit(OpCodes.Ldfld,luaTableField); generator.Emit(OpCodes.Ldfld, luaTableField);
generator.Emit(OpCodes.Stelem_Ref); generator.Emit(OpCodes.Stelem_Ref);
// Stores the arguments into the local variables, as needed // Stores the arguments into the local variables, as needed
for(int iArgs=0,iInArgs=1,iOutArgs=0;iArgs<paramTypes.Length;iArgs++) for(int iArgs = 0, iInArgs = 1, iOutArgs = 0; iArgs < paramTypes.Length; iArgs++)
{ {
generator.Emit(OpCodes.Ldloc_0); generator.Emit(OpCodes.Ldloc_0);
generator.Emit(OpCodes.Ldc_I4,iArgs); generator.Emit(OpCodes.Ldc_I4, iArgs);
generator.Emit(OpCodes.Ldarg,iArgs+1); generator.Emit(OpCodes.Ldarg, iArgs+1);
if(paramTypes[iArgs].IsByRef) if(paramTypes[iArgs].IsByRef)
{ {
if(paramTypes[iArgs].GetElementType().IsValueType) if(paramTypes[iArgs].GetElementType().IsValueType)
{ {
generator.Emit(OpCodes.Ldobj,paramTypes[iArgs].GetElementType()); generator.Emit(OpCodes.Ldobj, paramTypes[iArgs].GetElementType());
generator.Emit(OpCodes.Box,paramTypes[iArgs].GetElementType()); generator.Emit(OpCodes.Box, paramTypes[iArgs].GetElementType());
} }
else generator.Emit(OpCodes.Ldind_Ref); else
generator.Emit(OpCodes.Ldind_Ref);
} }
else else
{ {
if(paramTypes[iArgs].IsValueType) if(paramTypes[iArgs].IsValueType)
generator.Emit(OpCodes.Box,paramTypes[iArgs]); generator.Emit(OpCodes.Box, paramTypes[iArgs]);
} }
generator.Emit(OpCodes.Stelem_Ref); generator.Emit(OpCodes.Stelem_Ref);
if(paramTypes[iArgs].IsByRef) if(paramTypes[iArgs].IsByRef)
{ {
generator.Emit(OpCodes.Ldloc_2); generator.Emit(OpCodes.Ldloc_2);
generator.Emit(OpCodes.Ldc_I4,iOutArgs); generator.Emit(OpCodes.Ldc_I4, iOutArgs);
generator.Emit(OpCodes.Ldc_I4,iArgs); generator.Emit(OpCodes.Ldc_I4, iArgs);
generator.Emit(OpCodes.Stelem_I4); generator.Emit(OpCodes.Stelem_I4);
refArgs[iOutArgs]=iArgs; refArgs[iOutArgs] = iArgs;
iOutArgs++; iOutArgs++;
} }
if(paramInfo[iArgs].IsIn || (!paramInfo[iArgs].IsOut)) if(paramInfo[iArgs].IsIn || (!paramInfo[iArgs].IsOut))
{ {
generator.Emit(OpCodes.Ldloc_1); generator.Emit(OpCodes.Ldloc_1);
generator.Emit(OpCodes.Ldc_I4,iInArgs); generator.Emit(OpCodes.Ldc_I4, iInArgs);
generator.Emit(OpCodes.Ldarg,iArgs+1); generator.Emit(OpCodes.Ldarg, iArgs+1);
if(paramTypes[iArgs].IsByRef) if(paramTypes[iArgs].IsByRef)
{ {
if(paramTypes[iArgs].GetElementType().IsValueType) if(paramTypes[iArgs].GetElementType().IsValueType)
{ {
generator.Emit(OpCodes.Ldobj,paramTypes[iArgs].GetElementType()); generator.Emit(OpCodes.Ldobj, paramTypes[iArgs].GetElementType());
generator.Emit(OpCodes.Box,paramTypes[iArgs].GetElementType()); generator.Emit(OpCodes.Box, paramTypes[iArgs].GetElementType());
} }
else generator.Emit(OpCodes.Ldind_Ref); else
generator.Emit(OpCodes.Ldind_Ref);
} }
else else
{ {
if(paramTypes[iArgs].IsValueType) if(paramTypes[iArgs].IsValueType)
generator.Emit(OpCodes.Box,paramTypes[iArgs]); generator.Emit(OpCodes.Box, paramTypes[iArgs]);
} }
generator.Emit(OpCodes.Stelem_Ref); generator.Emit(OpCodes.Stelem_Ref);
iInArgs++; iInArgs++;
} }
} }
// Gets the function the method will delegate to by calling // Gets the function the method will delegate to by calling
// the getTableFunction method of class LuaClassHelper // the getTableFunction method of class LuaClassHelper
generator.Emit(OpCodes.Ldarg_0); generator.Emit(OpCodes.Ldarg_0);
generator.Emit(OpCodes.Ldfld,luaTableField); generator.Emit(OpCodes.Ldfld, luaTableField);
generator.Emit(OpCodes.Ldstr,method.Name); generator.Emit(OpCodes.Ldstr, method.Name);
generator.Emit(OpCodes.Call,classHelper.GetMethod("getTableFunction")); generator.Emit(OpCodes.Call, classHelper.GetMethod("getTableFunction"));
Label lab1=generator.DefineLabel(); var lab1 = generator.DefineLabel();
generator.Emit(OpCodes.Dup); generator.Emit(OpCodes.Dup);
generator.Emit(OpCodes.Brtrue_S,lab1); generator.Emit(OpCodes.Brtrue_S, lab1);
// Function does not exist, call base method // Function does not exist, call base method
generator.Emit(OpCodes.Pop); generator.Emit(OpCodes.Pop);
if(!method.IsAbstract) if(!method.IsAbstract)
{ {
generator.Emit(OpCodes.Ldarg_0); generator.Emit(OpCodes.Ldarg_0);
for(int i=0;i<paramTypes.Length;i++)
generator.Emit(OpCodes.Ldarg,i+1); for(int i = 0; i < paramTypes.Length; i++)
generator.Emit(OpCodes.Call,method); generator.Emit(OpCodes.Ldarg, i+1);
generator.Emit(OpCodes.Call, method);
if(returnType == typeof(void)) if(returnType == typeof(void))
generator.Emit(OpCodes.Pop); generator.Emit(OpCodes.Pop);
generator.Emit(OpCodes.Ret); generator.Emit(OpCodes.Ret);
generator.Emit(OpCodes.Ldnull); generator.Emit(OpCodes.Ldnull);
} else }
else
generator.Emit(OpCodes.Ldnull); generator.Emit(OpCodes.Ldnull);
Label lab2=generator.DefineLabel();
generator.Emit(OpCodes.Br_S,lab2); var lab2 = generator.DefineLabel();
generator.Emit(OpCodes.Br_S, lab2);
generator.MarkLabel(lab1); generator.MarkLabel(lab1);
// Function exists, call using method callFunction of LuaClassHelper // Function exists, call using method callFunction of LuaClassHelper
generator.Emit(OpCodes.Ldloc_0); generator.Emit(OpCodes.Ldloc_0);
generator.Emit(OpCodes.Ldarg_0); generator.Emit(OpCodes.Ldarg_0);
generator.Emit(OpCodes.Ldfld,returnTypesField); generator.Emit(OpCodes.Ldfld, returnTypesField);
generator.Emit(OpCodes.Ldc_I4,methodIndex); generator.Emit(OpCodes.Ldc_I4, methodIndex);
generator.Emit(OpCodes.Ldelem_Ref); generator.Emit(OpCodes.Ldelem_Ref);
generator.Emit(OpCodes.Ldloc_1); generator.Emit(OpCodes.Ldloc_1);
generator.Emit(OpCodes.Ldloc_2); generator.Emit(OpCodes.Ldloc_2);
generator.Emit(OpCodes.Call,classHelper.GetMethod("callFunction")); generator.Emit(OpCodes.Call, classHelper.GetMethod("callFunction"));
generator.MarkLabel(lab2); generator.MarkLabel(lab2);
// Stores the function return value // Stores the function return value
if(returnType == typeof(void)) if(returnType == typeof(void))
{ {
...@@ -573,27 +570,31 @@ namespace LuaInterface ...@@ -573,27 +570,31 @@ namespace LuaInterface
} }
else if(returnType.IsValueType) else if(returnType.IsValueType)
{ {
generator.Emit(OpCodes.Unbox,returnType); generator.Emit(OpCodes.Unbox, returnType);
generator.Emit(OpCodes.Ldobj,returnType); generator.Emit(OpCodes.Ldobj, returnType);
} }
else generator.Emit(OpCodes.Castclass,returnType); else
generator.Emit(OpCodes.Castclass, returnType);
generator.Emit(OpCodes.Stloc_3); generator.Emit(OpCodes.Stloc_3);
// Sets return values of out and ref parameters // Sets return values of out and ref parameters
for(int i=0;i<refArgs.Length;i++) for(int i = 0; i < refArgs.Length; i++)
{ {
generator.Emit(OpCodes.Ldarg,refArgs[i]+1); generator.Emit(OpCodes.Ldarg, refArgs[i]+1);
generator.Emit(OpCodes.Ldloc_0); generator.Emit(OpCodes.Ldloc_0);
generator.Emit(OpCodes.Ldc_I4,refArgs[i]); generator.Emit(OpCodes.Ldc_I4, refArgs[i]);
generator.Emit(OpCodes.Ldelem_Ref); generator.Emit(OpCodes.Ldelem_Ref);
if(paramTypes[refArgs[i]].GetElementType().IsValueType) if(paramTypes[refArgs[i]].GetElementType().IsValueType)
{ {
generator.Emit(OpCodes.Unbox,paramTypes[refArgs[i]].GetElementType()); generator.Emit(OpCodes.Unbox, paramTypes[refArgs[i]].GetElementType());
generator.Emit(OpCodes.Ldobj,paramTypes[refArgs[i]].GetElementType()); generator.Emit(OpCodes.Ldobj, paramTypes[refArgs[i]].GetElementType());
generator.Emit(OpCodes.Stobj,paramTypes[refArgs[i]].GetElementType()); generator.Emit(OpCodes.Stobj, paramTypes[refArgs[i]].GetElementType());
} }
else else
{ {
generator.Emit(OpCodes.Castclass,paramTypes[refArgs[i]].GetElementType()); generator.Emit(OpCodes.Castclass, paramTypes[refArgs[i]].GetElementType());
generator.Emit(OpCodes.Stind_Ref); generator.Emit(OpCodes.Stind_Ref);
} }
} }
...@@ -601,6 +602,7 @@ namespace LuaInterface ...@@ -601,6 +602,7 @@ namespace LuaInterface
// Returns // Returns
if(!(returnType == typeof(void))) if(!(returnType == typeof(void)))
generator.Emit(OpCodes.Ldloc_3); generator.Emit(OpCodes.Ldloc_3);
generator.Emit(OpCodes.Ret); generator.Emit(OpCodes.Ret);
} }
...@@ -611,17 +613,17 @@ namespace LuaInterface ...@@ -611,17 +613,17 @@ namespace LuaInterface
public LuaEventHandler GetEvent(Type eventHandlerType, LuaFunction eventHandler) public LuaEventHandler GetEvent(Type eventHandlerType, LuaFunction eventHandler)
{ {
Type eventConsumerType; Type eventConsumerType;
if (eventHandlerCollection.ContainsKey(eventHandlerType))
{ if(eventHandlerCollection.ContainsKey(eventHandlerType))
eventConsumerType=eventHandlerCollection[eventHandlerType]; eventConsumerType = eventHandlerCollection[eventHandlerType];
} else
else
{ {
eventConsumerType=GenerateEvent(eventHandlerType); eventConsumerType = GenerateEvent(eventHandlerType);
eventHandlerCollection[eventHandlerType] = eventConsumerType; eventHandlerCollection[eventHandlerType] = eventConsumerType;
} }
LuaEventHandler luaEventHandler=(LuaEventHandler)Activator.CreateInstance(eventConsumerType);
luaEventHandler.handler=eventHandler; var luaEventHandler = (LuaEventHandler)Activator.CreateInstance(eventConsumerType);
luaEventHandler.handler = eventHandler;
return luaEventHandler; return luaEventHandler;
} }
...@@ -631,26 +633,30 @@ namespace LuaInterface ...@@ -631,26 +633,30 @@ namespace LuaInterface
*/ */
public Delegate GetDelegate(Type delegateType, LuaFunction luaFunc) public Delegate GetDelegate(Type delegateType, LuaFunction luaFunc)
{ {
List<Type> returnTypes=new List<Type>(); var returnTypes = new List<Type>();
Type luaDelegateType; Type luaDelegateType;
if (delegateCollection.ContainsKey(delegateType))
{ if(delegateCollection.ContainsKey(delegateType))
luaDelegateType=delegateCollection[delegateType]; luaDelegateType = delegateCollection[delegateType];
} else
else
{ {
luaDelegateType=GenerateDelegate(delegateType); luaDelegateType = GenerateDelegate(delegateType);
delegateCollection[delegateType] = luaDelegateType; delegateCollection[delegateType] = luaDelegateType;
} }
MethodInfo methodInfo=delegateType.GetMethod("Invoke");
var methodInfo = delegateType.GetMethod("Invoke");
returnTypes.Add(methodInfo.ReturnType); returnTypes.Add(methodInfo.ReturnType);
foreach(ParameterInfo paramInfo in methodInfo.GetParameters())
foreach(ParameterInfo paramInfo in methodInfo.GetParameters())
{
if(paramInfo.ParameterType.IsByRef) if(paramInfo.ParameterType.IsByRef)
returnTypes.Add(paramInfo.ParameterType); returnTypes.Add(paramInfo.ParameterType);
LuaDelegate luaDelegate=(LuaDelegate)Activator.CreateInstance(luaDelegateType); }
luaDelegate.function=luaFunc;
luaDelegate.returnTypes=returnTypes.ToArray(); var luaDelegate = (LuaDelegate)Activator.CreateInstance(luaDelegateType);
return Delegate.CreateDelegate(delegateType,luaDelegate,"CallFunction"); luaDelegate.function = luaFunc;
luaDelegate.returnTypes = returnTypes.ToArray();
return Delegate.CreateDelegate(delegateType, luaDelegate, "CallFunction");
} }
/* /*
...@@ -662,17 +668,17 @@ namespace LuaInterface ...@@ -662,17 +668,17 @@ namespace LuaInterface
public object GetClassInstance(Type klass, LuaTable luaTable) public object GetClassInstance(Type klass, LuaTable luaTable)
{ {
LuaClassType luaClassType; LuaClassType luaClassType;
if (classCollection.ContainsKey(klass))
{ if(classCollection.ContainsKey(klass))
luaClassType=classCollection[klass]; luaClassType = classCollection[klass];
} else
else
{ {
luaClassType=new LuaClassType(); luaClassType = new LuaClassType();
GenerateClass(klass,out luaClassType.klass,out luaClassType.returnTypes); GenerateClass(klass, out luaClassType.klass, out luaClassType.returnTypes);
classCollection[klass] = luaClassType; classCollection[klass] = luaClassType;
} }
return Activator.CreateInstance(luaClassType.klass,new object[] {luaTable,luaClassType.returnTypes});
return Activator.CreateInstance(luaClassType.klass, new object[] {luaTable, luaClassType.returnTypes});
} }
} }
} }
\ No newline at end of file
/*
* This file is part of LuaInterface.
*
* Copyright (C) 2003-2005 Fabio Mascarenhas de Queiroz.
* Copyright (C) 2012 Megax <http://megax.yeahunter.hu/>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
using System;
namespace LuaInterface
{
/*
* Class used for generating delegates that get a function from the Lua
* stack as a delegate of a specific type.
*
* Author: Fabio Mascarenhas
* Version: 1.0
*/
class DelegateGenerator
{
private ObjectTranslator translator;
private Type delegateType;
public DelegateGenerator(ObjectTranslator translator, Type delegateType)
{
this.translator = translator;
this.delegateType = delegateType;
}
public object extractGenerated(KopiLua.Lua.lua_State luaState, int stackPos)
{
return CodeGeneration.Instance.GetDelegate(delegateType, translator.getFunction(luaState, stackPos));
}
}
}
\ No newline at end of file
/*
* This file is part of LuaInterface.
*
* Copyright (C) 2003-2005 Fabio Mascarenhas de Queiroz.
* Copyright (C) 2012 Megax <http://megax.yeahunter.hu/>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
using System;
namespace LuaInterface
{
/*
* Common interface for types generated from tables. The method
* returns the table that overrides some or all of the type's methods.
*/
public interface ILuaGeneratedType
{
LuaTable __luaInterface_getLuaTable();
}
}
\ No newline at end of file
/*
* This file is part of LuaInterface.
*
* Copyright (C) 2003-2005 Fabio Mascarenhas de Queiroz.
* Copyright (C) 2012 Megax <http://megax.yeahunter.hu/>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
using System;
namespace LuaInterface
{
/*
* Structure to store a type and the return types of
* its methods (the type of the returned value and out/ref
* parameters).
*/
struct LuaClassType
{
public Type klass;
public Type[][] returnTypes;
}
}
\ No newline at end of file
/*
namespace LuaInterface * This file is part of LuaInterface.
{ *
using System; * Copyright (C) 2003-2005 Fabio Mascarenhas de Queiroz.
using System.IO; * Copyright (C) 2012 Megax <http://megax.yeahunter.hu/>
using System.Collections; *
using System.Collections.Generic; * Permission is hereby granted, free of charge, to any person obtaining a copy
using System.Collections.Specialized; * of this software and associated documentation files (the "Software"), to deal
using System.Reflection; * in the Software without restriction, including without limitation the rights
using System.Threading; * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
using LuaWrap; * copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
using System;
using System.IO;
using System.Threading;
using System.Reflection;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using LuaInterface.Event;
namespace LuaInterface
{
/* /*
* Main class of LuaInterface * Main class of LuaInterface
* Object-oriented wrapper to Lua API * Object-oriented wrapper to Lua API
...@@ -64,15 +88,15 @@ namespace LuaInterface ...@@ -64,15 +88,15 @@ namespace LuaInterface
"-- Preload the mscorlib assembly \n"+ "-- Preload the mscorlib assembly \n"+
"luanet.load_assembly(\"mscorlib\") \n"; "luanet.load_assembly(\"mscorlib\") \n";
/*readonly */ KopiLua.Lua.lua_State luaState; private /*readonly */ KopiLua.Lua.lua_State luaState;
ObjectTranslator translator; private ObjectTranslator translator;
KopiLua.Lua.lua_CFunction panicCallback/*, lockCallback, unlockCallback*/; private KopiLua.Lua.lua_CFunction panicCallback;
/// <summary> /// <summary>
/// Used to ensure multiple .net threads all get serialized by this single lock for access to the lua stack/objects /// Used to ensure multiple .net threads all get serialized by this single lock for access to the lua stack/objects
/// </summary> /// </summary>
object luaLock = new object(); private object luaLock = new object();
public Lua() public Lua()
{ {
...@@ -561,8 +585,8 @@ namespace LuaInterface ...@@ -561,8 +585,8 @@ namespace LuaInterface
public LuaFunction GetFunction(string fullPath) public LuaFunction GetFunction(string fullPath)
{ {
object obj=this[fullPath]; object obj=this[fullPath];
//return (obj is KopiLua.Lua.lua_CFunction ? new LuaFunction((KopiLua.Lua.lua_CFunction)obj,this) : (LuaFunction)obj); return (obj is KopiLua.Lua.lua_CFunction ? new LuaFunction((KopiLua.Lua.lua_CFunction)obj,this) : (LuaFunction)obj);
return (obj is KopiLua.Lua.lua_CFunction ? new LuaFunction((KopiLua.Lua.lua_CFunction)obj,this) : /*(LuaFunction)*/new LuaFunction(obj.GetHashCode(), this)); //return (obj is KopiLua.Lua.lua_CFunction ? new LuaFunction((KopiLua.Lua.lua_CFunction)obj,this) : /*(LuaFunction)*/new LuaFunction(obj.GetHashCode(), this));
} }
/* /*
* Gets a function global variable as a delegate of * Gets a function global variable as a delegate of
...@@ -1041,98 +1065,4 @@ namespace LuaInterface ...@@ -1041,98 +1065,4 @@ namespace LuaInterface
#endregion #endregion
} }
/// <summary>
/// Event codes for lua hook function
/// </summary>
/// <remarks>
/// Do not change any of the values because they must match the lua values
/// </remarks>
/// <author>Reinhard Ostermeier</author>
public enum EventCodes
{
LUA_HOOKCALL = 0,
LUA_HOOKRET = 1,
LUA_HOOKLINE = 2,
LUA_HOOKCOUNT = 3,
LUA_HOOKTAILRET = 4,
}
/// <summary>
/// Event masks for lua hook callback
/// </summary>
/// <remarks>
/// Do not change any of the values because they must match the lua values
/// </remarks>
/// <author>Reinhard Ostermeier</author>
[Flags]
public enum EventMasks
{
LUA_MASKCALL = (1 << EventCodes.LUA_HOOKCALL),
LUA_MASKRET = (1 << EventCodes.LUA_HOOKRET),
LUA_MASKLINE = (1 << EventCodes.LUA_HOOKLINE),
LUA_MASKCOUNT = (1 << EventCodes.LUA_HOOKCOUNT),
LUA_MASKALL = Int32.MaxValue,
}
/// <summary>
/// Structure for lua debug information
/// </summary>
/// <remarks>
/// Do not change this struct because it must match the lua structure lua_debug
/// </remarks>
/// <author>Reinhard Ostermeier</author>
[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct LuaDebug
{
public EventCodes eventCode;
[System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPStr)]
public String name;
[System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPStr)]
public String namewhat;
[System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPStr)]
public String what;
[System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPStr)]
public String source;
public int currentline;
public int nups;
public int linedefined;
public int lastlinedefined;
[System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst = 60/*LUA_IDSIZE*/)]
public String shortsrc;
public int i_ci;
}
/// <summary>
/// Event args for hook callback event
/// </summary>
/// <author>Reinhard Ostermeier</author>
public class DebugHookEventArgs : EventArgs
{
private readonly LuaDebug luaDebug;
public DebugHookEventArgs(LuaDebug luaDebug)
{
this.luaDebug = luaDebug;
}
public LuaDebug LuaDebug
{
get { return luaDebug; }
}
}
public class HookExceptionEventArgs : EventArgs
{
private readonly Exception m_Exception;
public Exception Exception
{
get { return m_Exception; }
}
public HookExceptionEventArgs(Exception ex)
{
m_Exception = ex;
}
}
} }
\ No newline at end of file
using System; /*
* This file is part of LuaInterface.
*
* Copyright (C) 2003-2005 Fabio Mascarenhas de Queiroz.
* Copyright (C) 2012 Megax <http://megax.yeahunter.hu/>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text; using System.Text;
......
/*
* This file is part of LuaInterface.
*
* Copyright (C) 2003-2005 Fabio Mascarenhas de Queiroz.
* Copyright (C) 2012 Megax <http://megax.yeahunter.hu/>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
using System; using System;
using System.Runtime.Serialization; using System.Runtime.Serialization;
......
using System; /*
using System.Collections.Generic; * This file is part of LuaInterface.
*
* Copyright (C) 2003-2005 Fabio Mascarenhas de Queiroz.
* Copyright (C) 2012 Megax <http://megax.yeahunter.hu/>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
using System;
using System.Text; using System.Text;
using LuaWrap; using System.Collections.Generic;
namespace LuaInterface namespace LuaInterface
{ {
public class LuaFunction : LuaBase public class LuaFunction : LuaBase
{ {
//private Lua interpreter;
internal KopiLua.Lua.lua_CFunction function; internal KopiLua.Lua.lua_CFunction function;
//internal int reference;
public LuaFunction(int reference, Lua interpreter) public LuaFunction(int reference, Lua interpreter)
{ {
...@@ -25,39 +47,6 @@ namespace LuaInterface ...@@ -25,39 +47,6 @@ namespace LuaInterface
_Interpreter = interpreter; _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 * Calls the function casting return values to the types
* in returnTypes * in returnTypes
......
/*
* This file is part of LuaInterface.
*
* Copyright (C) 2003-2005 Fabio Mascarenhas de Queiroz.
* Copyright (C) 2012 Megax <http://megax.yeahunter.hu/>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
using System; using System;
namespace LuaInterface namespace LuaInterface
......
/*
* This file is part of LuaInterface.
*
* Copyright (C) 2003-2005 Fabio Mascarenhas de Queiroz.
* Copyright (C) 2012 Megax <http://megax.yeahunter.hu/>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
using System; using System;
namespace LuaInterface namespace LuaInterface
......
...@@ -36,7 +36,6 @@ ...@@ -36,7 +36,6 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="CheckType.cs" /> <Compile Include="CheckType.cs" />
<Compile Include="GenerateEventAssembly.cs" />
<Compile Include="Lua.cs" /> <Compile Include="Lua.cs" />
<Compile Include="LuaException.cs" /> <Compile Include="LuaException.cs" />
<Compile Include="Metatables.cs" /> <Compile Include="Metatables.cs" />
...@@ -53,6 +52,17 @@ ...@@ -53,6 +52,17 @@
<Compile Include="LuaScriptException.cs" /> <Compile Include="LuaScriptException.cs" />
<Compile Include="LuaTable.cs" /> <Compile Include="LuaTable.cs" />
<Compile Include="LuaUserData.cs" /> <Compile Include="LuaUserData.cs" />
<Compile Include="Extensions\GeneralExtensions.cs" />
<Compile Include="GenerateEventAssembly\LuaClassType.cs" />
<Compile Include="GenerateEventAssembly\ILuaGeneratedType.cs" />
<Compile Include="GenerateEventAssembly\DelegateGenerator.cs" />
<Compile Include="GenerateEventAssembly\ClassGenerator.cs" />
<Compile Include="GenerateEventAssembly\CodeGeneration.cs" />
<Compile Include="Event\EventCodes.cs" />
<Compile Include="Event\EventMasks.cs" />
<Compile Include="Event\LuaDebug.cs" />
<Compile Include="Event\DebugHookEventArgs.cs" />
<Compile Include="Event\HookExceptionEventArgs.cs" />
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
...@@ -68,4 +78,9 @@ ...@@ -68,4 +78,9 @@
<Name>KopiLua</Name> <Name>KopiLua</Name>
</ProjectReference> </ProjectReference>
</ItemGroup> </ItemGroup>
<ItemGroup>
<Folder Include="Extensions\" />
<Folder Include="GenerateEventAssembly\" />
<Folder Include="Event\" />
</ItemGroup>
</Project> </Project>
// /*
// LuaDLL.cs * This file is part of LuaInterface.
// *
// Author: * Copyright (C) 2003-2005 Fabio Mascarenhas de Queiroz.
// Joshua Simmons <simmons.44@gmail.com> * Copyright (C) 2009 Joshua Simmons <simmons.44@gmail.com>
// * Copyright (C) 2012 Megax <http://megax.yeahunter.hu/>
// Copyright (c) 2009 Joshua Simmons *
// * Permission is hereby granted, free of charge, to any person obtaining a copy
// Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal
// of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights
// in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is
// copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions:
// furnished to do so, subject to the following conditions: *
// * The above copyright notice and this permission notice shall be included in
// The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software.
// all copies or substantial portions of the Software. *
// * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE.
// THE SOFTWARE. */
using System; using System;
using System.IO;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using KopiLua; using System.Runtime.Serialization.Formatters.Binary;
using LuaInterface.Extensions;
namespace LuaWrap namespace LuaInterface
{ {
/// <summary> /// <summary>
/// Enumeration of basic lua globals. /// Enumeration of basic lua globals.
...@@ -38,42 +39,42 @@ namespace LuaWrap ...@@ -38,42 +39,42 @@ namespace LuaWrap
/// <summary> /// <summary>
/// Option for multiple returns in `lua_pcall' and `lua_call' /// Option for multiple returns in `lua_pcall' and `lua_call'
/// </summary> /// </summary>
MultiRet = -1, MultiRet = -1,
/// <summary> /// <summary>
/// Everything is OK. /// Everything is OK.
/// </summary> /// </summary>
Ok = 0, Ok = 0,
/// <summary> /// <summary>
/// Thread status, Ok or Yield /// Thread status, Ok or Yield
/// </summary> /// </summary>
Yield = 1, Yield = 1,
/// <summary> /// <summary>
/// A Runtime error. /// A Runtime error.
/// </summary> /// </summary>
ErrorRun = 2, ErrorRun = 2,
/// <summary> /// <summary>
/// A syntax error. /// A syntax error.
/// </summary> /// </summary>
ErrorSyntax = 3, ErrorSyntax = 3,
/// <summary> /// <summary>
/// A memory allocation error. For such errors, Lua does not call the error handler function. /// A memory allocation error. For such errors, Lua does not call the error handler function.
/// </summary> /// </summary>
ErrorMemory = 4, ErrorMemory = 4,
/// <summary> /// <summary>
/// An error in the error handling function. /// An error in the error handling function.
/// </summary> /// </summary>
ErrorError = 5, ErrorError = 5,
/// <summary> /// <summary>
/// An extra error for file load errors when using luaL_loadfile. /// An extra error for file load errors when using luaL_loadfile.
/// </summary> /// </summary>
ErrorFile = 6, ErrorFile = 6,
} }
public enum References : int public enum References : int
...@@ -114,7 +115,7 @@ namespace LuaWrap ...@@ -114,7 +115,7 @@ namespace LuaWrap
Collect = 2, Collect = 2,
/// <summary> /// <summary>
/// Returns the current amount of memory (in Kbytes) in use by Lua. /// Returns the current amount of memory (in Kbytes) in use by KopiLua.Lua.
/// </summary> /// </summary>
Count = 3, Count = 3,
...@@ -150,6 +151,22 @@ namespace LuaWrap ...@@ -150,6 +151,22 @@ namespace LuaWrap
{ {
private static int tag = 0; private static int tag = 0;
/// <summary>
/// Function to get byte array from a object
/// </summary>
/// <param name="_Object">object to get byte array</param>
/// <returns>Byte Array</returns>
public static byte[] ObjectToByteArray(object obj)
{
if(obj.IsNull())
return null;
var bf = new BinaryFormatter();
var ms = new MemoryStream();
bf.Serialize(ms, obj);
return ms.ToArray();
}
public static LuaTypes ToLuaTypes(this int type) public static LuaTypes ToLuaTypes(this int type)
{ {
return (LuaTypes)type; return (LuaTypes)type;
...@@ -164,7 +181,7 @@ namespace LuaWrap ...@@ -164,7 +181,7 @@ namespace LuaWrap
{ {
return number == 1; return number == 1;
} }
#region Core Library #region Core Library
/// <summary> /// <summary>
/// Pushes a C function onto the stack. This function receives a pointer to a C function and pushes onto the stack a Lua value of type function that, when called, invokes the corresponding C function. /// Pushes a C function onto the stack. This function receives a pointer to a C function and pushes onto the stack a Lua value of type function that, when called, invokes the corresponding C function.
...@@ -175,12 +192,12 @@ namespace LuaWrap ...@@ -175,12 +192,12 @@ namespace LuaWrap
/// <param name="fn"> /// <param name="fn">
/// A <see cref="CallbackFunction"/> /// A <see cref="CallbackFunction"/>
/// </param> /// </param>
public static void lua_pushcfunction(Lua.lua_State state, Lua.lua_CFunction fn) public static void lua_pushcfunction(KopiLua.Lua.lua_State state, KopiLua.Lua.lua_CFunction fn)
{ {
Lua.lua_pushcclosure(state, fn, 0); KopiLua.Lua.lua_pushcclosure(state, fn, 0);
} }
#endregion #endregion
#region Auxiliary Library #region Auxiliary Library
/// <summary> /// <summary>
/// Loads and runs the given file. /// Loads and runs the given file.
...@@ -194,11 +211,11 @@ namespace LuaWrap ...@@ -194,11 +211,11 @@ namespace LuaWrap
/// <returns> /// <returns>
/// A <see cref="System.Boolean"/> /// A <see cref="System.Boolean"/>
/// </returns> /// </returns>
public static bool luaL_dofile(Lua.lua_State state, string filename) public static bool luaL_dofile(KopiLua.Lua.lua_State state, string filename)
{ {
return (Lua.luaL_loadfile(state, filename).ToLuaEnum() == LuaEnum.Ok) && (Lua.lua_pcall(state, 0, (int)LuaEnum.MultiRet, 0).ToLuaEnum() == LuaEnum.Ok); return (KopiLua.Lua.luaL_loadfile(state, filename).ToLuaEnum() == LuaEnum.Ok) && (KopiLua.Lua.lua_pcall(state, 0, (int)LuaEnum.MultiRet, 0).ToLuaEnum() == LuaEnum.Ok);
} }
/// <summary> /// <summary>
/// Loads and runs the given string. /// Loads and runs the given string.
/// </summary> /// </summary>
...@@ -211,14 +228,14 @@ namespace LuaWrap ...@@ -211,14 +228,14 @@ namespace LuaWrap
/// <returns> /// <returns>
/// A <see cref="System.Boolean"/> /// A <see cref="System.Boolean"/>
/// </returns> /// </returns>
public static bool luaL_dostring(Lua.lua_State state, string chunk) public static bool luaL_dostring(KopiLua.Lua.lua_State state, string chunk)
{ {
return (Lua.luaL_loadstring(state, chunk).ToLuaEnum() == LuaEnum.Ok) && (Lua.lua_pcall(state, 0, (int)LuaEnum.MultiRet, 0).ToLuaEnum() == LuaEnum.Ok); return (KopiLua.Lua.luaL_loadstring(state, chunk).ToLuaEnum() == LuaEnum.Ok) && (KopiLua.Lua.lua_pcall(state, 0, (int)LuaEnum.MultiRet, 0).ToLuaEnum() == LuaEnum.Ok);
} }
public static LuaEnum luaL_loadbuffer(Lua.lua_State luaState, string buff, string name) public static LuaEnum luaL_loadbuffer(KopiLua.Lua.lua_State luaState, string buff, string name)
{ {
LuaEnum result = Lua.luaL_loadbuffer(luaState, buff, (uint)buff.Length, name).ToLuaEnum(); var result = KopiLua.Lua.luaL_loadbuffer(luaState, buff, (uint)buff.Length, name).ToLuaEnum();
return result; return result;
} }
...@@ -234,22 +251,24 @@ namespace LuaWrap ...@@ -234,22 +251,24 @@ namespace LuaWrap
/// <param name="r"> /// <param name="r">
/// A <see cref="System.Int32"/> /// A <see cref="System.Int32"/>
/// </param> /// </param>
public static void luaL_getref(Lua.lua_State state, int t, int r) public static void luaL_getref(KopiLua.Lua.lua_State state, int t, int r)
{ {
Lua.lua_rawgeti(state, t, r); KopiLua.Lua.lua_rawgeti(state, t, r);
} }
public static bool luaL_checkmetatable(Lua.lua_State luaState,int index) public static bool luaL_checkmetatable(KopiLua.Lua.lua_State luaState,int index)
{ {
bool retVal = false; bool retVal = false;
Console.WriteLine("v: " + luaState.tt.ToString());
if(Lua.lua_getmetatable(luaState,index)!=0) if(KopiLua.Lua.lua_getmetatable(luaState,index)!=0)
{ {
Lua.lua_pushlightuserdata(luaState, tag); KopiLua.Lua.lua_pushlightuserdata(luaState, tag);
Lua.lua_rawget(luaState, -2); KopiLua.Lua.lua_rawget(luaState, -2);
retVal = !Lua.lua_isnil(luaState, -1); retVal = !KopiLua.Lua.lua_isnil(luaState, -1);
Lua.lua_settop(luaState, -3); KopiLua.Lua.lua_settop(luaState, -3);
} }
return retVal; return retVal;
} }
...@@ -258,44 +277,43 @@ namespace LuaWrap ...@@ -258,44 +277,43 @@ namespace LuaWrap
return tag; return tag;
} }
public static void lua_getref(Lua.lua_State luaState, int reference) public static void lua_getref(KopiLua.Lua.lua_State luaState, int reference)
{ {
Lua.lua_rawgeti(luaState, (int)PseudoIndex.Registry, reference); KopiLua.Lua.lua_rawgeti(luaState, (int)PseudoIndex.Registry, reference);
} }
public static void lua_unref(Lua.lua_State luaState, int reference) public static void lua_unref(KopiLua.Lua.lua_State luaState, int reference)
{ {
Lua.luaL_unref(luaState, (int)PseudoIndex.Registry, reference); KopiLua.Lua.luaL_unref(luaState, (int)PseudoIndex.Registry, reference);
} }
public static int luanet_rawnetobj(Lua.lua_State luaState,int obj) public static int luanet_rawnetobj(KopiLua.Lua.lua_State luaState,int obj)
{ {
int udata= (int)Lua.lua_touserdata2(luaState, obj); int udata = (int)KopiLua.Lua.lua_touserdata2(luaState, obj);
if(udata!=0) return udata != 0 ? udata : -1;
return udata;
return -1;
} }
public static void lua_pushstdcallcfunction(Lua.lua_State luaState, KopiLua.Lua.lua_CFunction function) public static void lua_pushstdcallcfunction(KopiLua.Lua.lua_State luaState,KopiLua.Lua.lua_CFunction function)
{ {
lua_pushcfunction(luaState, function); lua_pushcfunction(luaState, function);
} }
public static int checkudata_raw(Lua.lua_State luaState, int ud, string tname) public static int checkudata_raw(KopiLua.Lua.lua_State luaState, int ud, string tname)
{ {
int p = (int)Lua.lua_touserdata2(luaState, ud); int p = (int)KopiLua.Lua.lua_touserdata2(luaState, ud);
//Console.WriteLine(BitConverter.ToInt32(ObjectToByteArray(KopiLua.Lua.lua_touserdata(luaState, ud)), 0));
if(p != 0) if(p != 0)
{ {
/* value is a userdata? */ /* value is a userdata? */
if(Lua.lua_getmetatable(luaState, ud)!=0) if(KopiLua.Lua.lua_getmetatable(luaState, ud)!=0)
{ {
/* does it have a metatable? */ /* does it have a metatable? */
Lua.lua_getfield(luaState, (int)PseudoIndex.Registry, tname); /* get correct metatable */ KopiLua.Lua.lua_getfield(luaState, (int)PseudoIndex.Registry, tname); /* get correct metatable */
bool isEqual = Lua.lua_rawequal(luaState, -1, -2).ToBoolean(); bool isEqual = KopiLua.Lua.lua_rawequal(luaState, -1, -2).ToBoolean();
// NASTY - we need our own version of the lua_pop macro // NASTY - we need our own version of the lua_pop macro
// lua_pop(L, 2); /* remove both metatables */ // lua_pop(L, 2); /* remove both metatables */
Lua.lua_settop(luaState, -(2) - 1); KopiLua.Lua.lua_settop(luaState, -(2) - 1);
if(isEqual) /* does it have the correct mt? */ if(isEqual) /* does it have the correct mt? */
return p; return p;
...@@ -305,33 +323,31 @@ namespace LuaWrap ...@@ -305,33 +323,31 @@ namespace LuaWrap
return 0; return 0;
} }
public static int luanet_checkudata(Lua.lua_State luaState, int ud, string tname) public static int luanet_checkudata(KopiLua.Lua.lua_State luaState, int ud, string tname)
{ {
int udata = checkudata_raw(luaState, ud, tname); int udata = checkudata_raw(luaState, ud, tname);
return udata != 0 ? udata : -1;
if(udata != 0)
return udata;
return -1;
} }
public static void luanet_newudata(Lua.lua_State luaState, int val) public static void luanet_newudata(KopiLua.Lua.lua_State luaState, int val)
{ {
Lua.lua_newuserdata(luaState, (uint)val); KopiLua.Lua.lua_newuserdata(luaState, (uint)val);
} }
public static int luanet_tonetobject(Lua.lua_State luaState, int index) public static int luanet_tonetobject(KopiLua.Lua.lua_State luaState, int index)
{ {
int udata; int udata;
Console.WriteLine("x" + KopiLua.Lua.lua_type(luaState, index).ToString());
if(Lua.lua_type(luaState, index).ToLuaTypes() == LuaTypes.UserData) if(KopiLua.Lua.lua_type(luaState, index).ToLuaTypes() == LuaTypes.UserData)
{ {
if(luaL_checkmetatable(luaState, index)) if(luaL_checkmetatable(luaState, index))
{ {
udata = (int)Lua.lua_touserdata2(luaState, index); udata = (int)KopiLua.Lua.lua_touserdata2(luaState, index);
if(udata != 0) if(udata != 0)
return udata; return udata;
} }
udata = checkudata_raw(luaState, index, "luaNet_class"); udata = checkudata_raw(luaState, index, "luaNet_class");
if(udata != 0) if(udata != 0)
return udata; return udata;
...@@ -344,17 +360,14 @@ namespace LuaWrap ...@@ -344,17 +360,14 @@ namespace LuaWrap
if(udata != 0) if(udata != 0)
return udata; return udata;
} }
return -1; return -1;
} }
public static int lua_ref(Lua.lua_State luaState, int lockRef) public static int lua_ref(KopiLua.Lua.lua_State luaState, int lockRef)
{ {
if(lockRef != 0) return lockRef != 0 ? KopiLua.Lua.luaL_ref(luaState, (int)PseudoIndex.Registry) : 0;
return Lua.luaL_ref(luaState, (int)PseudoIndex.Registry);
else
return 0;
} }
#endregion #endregion
} }
} }
\ No newline at end of file
using System; /*
* This file is part of LuaInterface.
*
* Copyright (C) 2003-2005 Fabio Mascarenhas de Queiroz.
* Copyright (C) 2012 Megax <http://megax.yeahunter.hu/>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
using System;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using System.Reflection; using System.Reflection;
......
using System; /*
* This file is part of LuaInterface.
*
* Copyright (C) 2003-2005 Fabio Mascarenhas de Queiroz.
* Copyright (C) 2012 Megax <http://megax.yeahunter.hu/>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
using System;
namespace LuaInterface namespace LuaInterface
{ {
...@@ -35,7 +60,7 @@ namespace LuaInterface ...@@ -35,7 +60,7 @@ namespace LuaInterface
/// <param name="innerException">The .NET exception triggered by user-code.</param> /// <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> /// <param name="source">The position in the script where the exception was triggered.</param>
public LuaScriptException(Exception innerException, string source) public LuaScriptException(Exception innerException, string source)
: base("A .NET exception occured in user-code", innerException) : base("A .NET exception occured in user-code", innerException)
{ {
this.source = source; this.source = source;
this.IsNetException = true; this.IsNetException = true;
......
using System; /*
using System.Collections.Generic; * This file is part of LuaInterface.
*
* Copyright (C) 2003-2005 Fabio Mascarenhas de Queiroz.
* Copyright (C) 2012 Megax <http://megax.yeahunter.hu/>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
using System;
using System.Text; using System.Text;
using System.Collections; using System.Collections;
using LuaWrap; using System.Collections.Generic;
namespace LuaInterface namespace LuaInterface
{ {
...@@ -14,43 +38,12 @@ namespace LuaInterface ...@@ -14,43 +38,12 @@ namespace LuaInterface
*/ */
public class LuaTable : LuaBase public class LuaTable : LuaBase
{ {
//internal int _Reference;
//private Lua _Interpreter;
public LuaTable(int reference, Lua interpreter) public LuaTable(int reference, Lua interpreter)
{ {
_Reference = reference; _Reference = reference;
_Interpreter = interpreter; _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 * Indexer for string fields of the table
*/ */
...@@ -126,18 +119,5 @@ namespace LuaInterface ...@@ -126,18 +119,5 @@ namespace LuaInterface
{ {
return "table"; 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; * This file is part of LuaInterface.
*
* Copyright (C) 2003-2005 Fabio Mascarenhas de Queiroz.
* Copyright (C) 2012 Megax <http://megax.yeahunter.hu/>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
using System;
using System.Text; using System.Text;
using LuaWrap; using System.Collections.Generic;
namespace LuaInterface namespace LuaInterface
{ {
public class LuaUserData : LuaBase public class LuaUserData : LuaBase
{ {
//internal int _Reference;
//private Lua _Interpreter;
public LuaUserData(int reference, Lua interpreter) public LuaUserData(int reference, Lua interpreter)
{ {
_Reference = reference; _Reference = reference;
_Interpreter = interpreter; _Interpreter = interpreter;
} }
//~LuaUserData()
//{
// if (_Reference != 0)
// _Interpreter.dispose(_Reference);
//}
/* /*
* Indexer for string fields of the userdata * Indexer for string fields of the userdata
*/ */
...@@ -66,18 +84,5 @@ namespace LuaInterface ...@@ -66,18 +84,5 @@ namespace LuaInterface
{ {
return "userdata"; 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;
//}
} }
} }
\ No newline at end of file
/*
* This file is part of LuaInterface.
*
* Copyright (C) 2003-2005 Fabio Mascarenhas de Queiroz.
* Copyright (C) 2012 Megax <http://megax.yeahunter.hu/>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
using System;
using System.IO;
using System.Collections;
using System.Reflection;
using System.Diagnostics;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace LuaInterface namespace LuaInterface
{ {
using System;
using System.IO;
using System.Collections;
using System.Reflection;
using System.Diagnostics;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using LuaWrap;
/* /*
* Functions used in the metatables of userdata representing * Functions used in the metatables of userdata representing
* CLR objects * CLR objects
...@@ -110,7 +134,7 @@ namespace LuaInterface ...@@ -110,7 +134,7 @@ namespace LuaInterface
{ {
LuaTypes type = KopiLua.Lua.lua_type(luaState, i).ToLuaTypes(); LuaTypes type = KopiLua.Lua.lua_type(luaState, i).ToLuaTypes();
// we dump stacks when deep in calls, calling typename while the stack is in flux can fail sometimes, so manually check for key types // we dump stacks when deep in calls, calling typename while the stack is in flux can fail sometimes, so manually check for key types
string typestr = (type == LuaTypes.Table) ? "table" : KopiLua.Lua.lua_typename(luaState, Convert.ToInt32(type)).ToString(); string typestr = (type == LuaTypes.Table) ? "table" : KopiLua.Lua.lua_typename(luaState, (int)type).ToString();
string strrep = KopiLua.Lua.lua_tostring(luaState, i).ToString(); string strrep = KopiLua.Lua.lua_tostring(luaState, i).ToString();
if (type == LuaTypes.UserData) if (type == LuaTypes.UserData)
......
/*
* This file is part of LuaInterface.
*
* Copyright (C) 2003-2005 Fabio Mascarenhas de Queiroz.
* Copyright (C) 2012 Megax <http://megax.yeahunter.hu/>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
using System;
using System.IO;
using System.Collections;
using System.Reflection;
using System.Collections.Generic;
using System.Diagnostics;
namespace LuaInterface namespace LuaInterface
{ {
using System;
using System.IO;
using System.Collections;
using System.Reflection;
using System.Collections.Generic;
using System.Diagnostics;
using LuaWrap;
/* /*
* Cached method * Cached method
*/ */
......
/*
* This file is part of LuaInterface.
*
* Copyright (C) 2003-2005 Fabio Mascarenhas de Queiroz.
* Copyright (C) 2012 Megax <http://megax.yeahunter.hu/>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
using System;
using System.IO;
using System.Collections;
using System.Reflection;
using System.Collections.Generic;
using System.Diagnostics;
namespace LuaInterface namespace LuaInterface
{ {
using System;
using System.IO;
using System.Collections;
using System.Reflection;
using System.Collections.Generic;
using System.Diagnostics;
using LuaWrap;
/* /*
* Passes objects from the CLR to Lua and vice-versa * Passes objects from the CLR to Lua and vice-versa
* *
...@@ -206,7 +230,7 @@ namespace LuaInterface ...@@ -206,7 +230,7 @@ namespace LuaInterface
try try
{ {
assembly = Assembly.LoadWithPartialName(assemblyName); assembly = Assembly.Load(assemblyName);
} }
catch (BadImageFormatException) catch (BadImageFormatException)
{ {
...@@ -614,6 +638,7 @@ namespace LuaInterface ...@@ -614,6 +638,7 @@ namespace LuaInterface
case LuaTypes.UserData: case LuaTypes.UserData:
{ {
int udata=LuaLib.luanet_tonetobject(luaState,index); int udata=LuaLib.luanet_tonetobject(luaState,index);
Console.WriteLine(udata);
if(udata!=-1) if(udata!=-1)
return objects[udata]; return objects[udata];
else else
......
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