Commit d7bb0c93 authored by Megax's avatar Megax
Browse files

* Mar csak egy falj nincs atalakitva. Ha az is meglesz (vagy kozben) akkor a...

* Mar csak egy falj nincs atalakitva. Ha az is meglesz (vagy kozben) akkor a fajlok kimeneti helyet is modositom + hozzadom azokat az exeket amik a luainterface-hez voltak.
parent bbf63a7e
...@@ -26,10 +26,13 @@ ...@@ -26,10 +26,13 @@
using System; using System;
using System.Reflection; using System.Reflection;
using System.Collections.Generic; using System.Collections.Generic;
using LuaInterface.Method;
using LuaInterface.Extensions; using LuaInterface.Extensions;
namespace LuaInterface namespace LuaInterface
{ {
using LuaCore = KopiLua.Lua;
/* /*
* Type checking and conversion functions. * Type checking and conversion functions.
* *
...@@ -84,9 +87,9 @@ namespace LuaInterface ...@@ -84,9 +87,9 @@ namespace LuaInterface
return extractValues.ContainsKey(runtimeHandleValue) ? extractValues[runtimeHandleValue] : extractNetObject; return extractValues.ContainsKey(runtimeHandleValue) ? extractValues[runtimeHandleValue] : extractNetObject;
} }
internal ExtractValue checkType(KopiLua.Lua.lua_State luaState, int stackPos, Type paramType) internal ExtractValue checkType(LuaCore.lua_State luaState, int stackPos, Type paramType)
{ {
var luatype = KopiLua.Lua.lua_type(luaState, stackPos).ToLuaTypes(); var luatype = LuaCore.lua_type(luaState, stackPos).ToLuaTypes();
if(paramType.IsByRef) if(paramType.IsByRef)
paramType = paramType.GetElementType(); paramType = paramType.GetElementType();
...@@ -120,17 +123,17 @@ namespace LuaInterface ...@@ -120,17 +123,17 @@ namespace LuaInterface
//;//an unsupported type was encountered //;//an unsupported type was encountered
} }
if(KopiLua.Lua.lua_isnumber(luaState, stackPos).ToBoolean()) if(LuaCore.lua_isnumber(luaState, stackPos).ToBoolean())
return extractValues[runtimeHandleValue]; return extractValues[runtimeHandleValue];
if(paramType == typeof(bool)) if(paramType == typeof(bool))
{ {
if(KopiLua.Lua.lua_isboolean(luaState, stackPos)) if(LuaCore.lua_isboolean(luaState, stackPos))
return extractValues[runtimeHandleValue]; return extractValues[runtimeHandleValue];
} }
else if(paramType == typeof(string)) else if(paramType == typeof(string))
{ {
if(KopiLua.Lua.lua_isstring(luaState, stackPos).ToBoolean()) if(LuaCore.lua_isstring(luaState, stackPos).ToBoolean())
return extractValues[runtimeHandleValue]; return extractValues[runtimeHandleValue];
else if(luatype == LuaTypes.Nil) else if(luatype == LuaTypes.Nil)
return extractNetObject; // kevinh - silently convert nil to a null string pointer return extractNetObject; // kevinh - silently convert nil to a null string pointer
...@@ -159,12 +162,12 @@ namespace LuaInterface ...@@ -159,12 +162,12 @@ namespace LuaInterface
// kevinh - allow nil to be silently converted to null - extractNetObject will return null when the item ain't found // kevinh - allow nil to be silently converted to null - extractNetObject will return null when the item ain't found
return extractNetObject; return extractNetObject;
} }
else if(KopiLua.Lua.lua_type(luaState, stackPos).ToLuaTypes() == LuaTypes.Table) else if(LuaCore.lua_type(luaState, stackPos).ToLuaTypes() == LuaTypes.Table)
{ {
if(KopiLua.Lua.luaL_getmetafield(luaState, stackPos, "__index").ToBoolean()) if(LuaCore.luaL_getmetafield(luaState, stackPos, "__index").ToBoolean())
{ {
object obj = translator.getNetObject(luaState, -1); object obj = translator.getNetObject(luaState, -1);
KopiLua.Lua.lua_settop(luaState, -2); LuaCore.lua_settop(luaState, -2);
if(!obj.IsNull() && paramType.IsAssignableFrom(obj.GetType())) if(!obj.IsNull() && paramType.IsAssignableFrom(obj.GetType()))
return extractNetObject; return extractNetObject;
} }
...@@ -186,156 +189,156 @@ namespace LuaInterface ...@@ -186,156 +189,156 @@ namespace LuaInterface
* index stackPos as the desired type if it can, or null * index stackPos as the desired type if it can, or null
* otherwise. * otherwise.
*/ */
private object getAsSbyte(KopiLua.Lua.lua_State luaState, int stackPos) private object getAsSbyte(LuaCore.lua_State luaState, int stackPos)
{ {
sbyte retVal = (sbyte)KopiLua.Lua.lua_tonumber(luaState, stackPos); sbyte retVal = (sbyte)LuaCore.lua_tonumber(luaState, stackPos);
if(retVal == 0 && !KopiLua.Lua.lua_isnumber(luaState, stackPos).ToBoolean()) if(retVal == 0 && !LuaCore.lua_isnumber(luaState, stackPos).ToBoolean())
return null; return null;
return retVal; return retVal;
} }
private object getAsByte(KopiLua.Lua.lua_State luaState, int stackPos) private object getAsByte(LuaCore.lua_State luaState, int stackPos)
{ {
byte retVal = (byte)KopiLua.Lua.lua_tonumber(luaState, stackPos); byte retVal = (byte)LuaCore.lua_tonumber(luaState, stackPos);
if(retVal == 0 && !KopiLua.Lua.lua_isnumber(luaState, stackPos).ToBoolean()) if(retVal == 0 && !LuaCore.lua_isnumber(luaState, stackPos).ToBoolean())
return null; return null;
return retVal; return retVal;
} }
private object getAsShort(KopiLua.Lua.lua_State luaState, int stackPos) private object getAsShort(LuaCore.lua_State luaState, int stackPos)
{ {
short retVal = (short)KopiLua.Lua.lua_tonumber(luaState, stackPos); short retVal = (short)LuaCore.lua_tonumber(luaState, stackPos);
if(retVal == 0 && !KopiLua.Lua.lua_isnumber(luaState, stackPos).ToBoolean()) if(retVal == 0 && !LuaCore.lua_isnumber(luaState, stackPos).ToBoolean())
return null; return null;
return retVal; return retVal;
} }
private object getAsUshort(KopiLua.Lua.lua_State luaState, int stackPos) private object getAsUshort(LuaCore.lua_State luaState, int stackPos)
{ {
ushort retVal = (ushort)KopiLua.Lua.lua_tonumber(luaState, stackPos); ushort retVal = (ushort)LuaCore.lua_tonumber(luaState, stackPos);
if(retVal == 0 && !KopiLua.Lua.lua_isnumber(luaState, stackPos).ToBoolean()) if(retVal == 0 && !LuaCore.lua_isnumber(luaState, stackPos).ToBoolean())
return null; return null;
return retVal; return retVal;
} }
private object getAsInt(KopiLua.Lua.lua_State luaState, int stackPos) private object getAsInt(LuaCore.lua_State luaState, int stackPos)
{ {
int retVal = (int)KopiLua.Lua.lua_tonumber(luaState, stackPos); int retVal = (int)LuaCore.lua_tonumber(luaState, stackPos);
if(retVal == 0 && !KopiLua.Lua.lua_isnumber(luaState, stackPos).ToBoolean()) if(retVal == 0 && !LuaCore.lua_isnumber(luaState, stackPos).ToBoolean())
return null; return null;
return retVal; return retVal;
} }
private object getAsUint(KopiLua.Lua.lua_State luaState, int stackPos) private object getAsUint(LuaCore.lua_State luaState, int stackPos)
{ {
uint retVal = (uint)KopiLua.Lua.lua_tonumber(luaState, stackPos); uint retVal = (uint)LuaCore.lua_tonumber(luaState, stackPos);
if(retVal == 0 && !KopiLua.Lua.lua_isnumber(luaState, stackPos).ToBoolean()) if(retVal == 0 && !LuaCore.lua_isnumber(luaState, stackPos).ToBoolean())
return null; return null;
return retVal; return retVal;
} }
private object getAsLong(KopiLua.Lua.lua_State luaState, int stackPos) private object getAsLong(LuaCore.lua_State luaState, int stackPos)
{ {
long retVal = (long)KopiLua.Lua.lua_tonumber(luaState, stackPos); long retVal = (long)LuaCore.lua_tonumber(luaState, stackPos);
if(retVal == 0 && !KopiLua.Lua.lua_isnumber(luaState, stackPos).ToBoolean()) if(retVal == 0 && !LuaCore.lua_isnumber(luaState, stackPos).ToBoolean())
return null; return null;
return retVal; return retVal;
} }
private object getAsUlong(KopiLua.Lua.lua_State luaState, int stackPos) private object getAsUlong(LuaCore.lua_State luaState, int stackPos)
{ {
ulong retVal = (ulong)KopiLua.Lua.lua_tonumber(luaState, stackPos); ulong retVal = (ulong)LuaCore.lua_tonumber(luaState, stackPos);
if(retVal == 0 && !KopiLua.Lua.lua_isnumber(luaState, stackPos).ToBoolean()) if(retVal == 0 && !LuaCore.lua_isnumber(luaState, stackPos).ToBoolean())
return null; return null;
return retVal; return retVal;
} }
private object getAsDouble(KopiLua.Lua.lua_State luaState, int stackPos) private object getAsDouble(LuaCore.lua_State luaState, int stackPos)
{ {
double retVal = KopiLua.Lua.lua_tonumber(luaState, stackPos); double retVal = LuaCore.lua_tonumber(luaState, stackPos);
if(retVal == 0 && !KopiLua.Lua.lua_isnumber(luaState, stackPos).ToBoolean()) if(retVal == 0 && !LuaCore.lua_isnumber(luaState, stackPos).ToBoolean())
return null; return null;
return retVal; return retVal;
} }
private object getAsChar(KopiLua.Lua.lua_State luaState, int stackPos) private object getAsChar(LuaCore.lua_State luaState, int stackPos)
{ {
char retVal = (char)KopiLua.Lua.lua_tonumber(luaState, stackPos); char retVal = (char)LuaCore.lua_tonumber(luaState, stackPos);
if(retVal == 0 && !KopiLua.Lua.lua_isnumber(luaState, stackPos).ToBoolean()) if(retVal == 0 && !LuaCore.lua_isnumber(luaState, stackPos).ToBoolean())
return null; return null;
return retVal; return retVal;
} }
private object getAsFloat(KopiLua.Lua.lua_State luaState, int stackPos) private object getAsFloat(LuaCore.lua_State luaState, int stackPos)
{ {
float retVal = (float)KopiLua.Lua.lua_tonumber(luaState, stackPos); float retVal = (float)LuaCore.lua_tonumber(luaState, stackPos);
if(retVal == 0 && !KopiLua.Lua.lua_isnumber(luaState, stackPos).ToBoolean()) if(retVal == 0 && !LuaCore.lua_isnumber(luaState, stackPos).ToBoolean())
return null; return null;
return retVal; return retVal;
} }
private object getAsDecimal(KopiLua.Lua.lua_State luaState, int stackPos) private object getAsDecimal(LuaCore.lua_State luaState, int stackPos)
{ {
decimal retVal = (decimal)KopiLua.Lua.lua_tonumber(luaState, stackPos); decimal retVal = (decimal)LuaCore.lua_tonumber(luaState, stackPos);
if(retVal == 0 && !KopiLua.Lua.lua_isnumber(luaState, stackPos).ToBoolean()) if(retVal == 0 && !LuaCore.lua_isnumber(luaState, stackPos).ToBoolean())
return null; return null;
return retVal; return retVal;
} }
private object getAsBoolean(KopiLua.Lua.lua_State luaState, int stackPos) private object getAsBoolean(LuaCore.lua_State luaState, int stackPos)
{ {
return KopiLua.Lua.lua_toboolean(luaState, stackPos); return LuaCore.lua_toboolean(luaState, stackPos);
} }
private object getAsString(KopiLua.Lua.lua_State luaState, int stackPos) private object getAsString(LuaCore.lua_State luaState, int stackPos)
{ {
string retVal = KopiLua.Lua.lua_tostring(luaState, stackPos).ToString(); string retVal = LuaCore.lua_tostring(luaState, stackPos).ToString();
if(retVal == string.Empty && !KopiLua.Lua.lua_isstring(luaState, stackPos).ToBoolean()) if(retVal == string.Empty && !LuaCore.lua_isstring(luaState, stackPos).ToBoolean())
return null; return null;
return retVal; return retVal;
} }
private object getAsTable(KopiLua.Lua.lua_State luaState, int stackPos) private object getAsTable(LuaCore.lua_State luaState, int stackPos)
{ {
return translator.getTable(luaState, stackPos); return translator.getTable(luaState, stackPos);
} }
private object getAsFunction(KopiLua.Lua.lua_State luaState, int stackPos) private object getAsFunction(LuaCore.lua_State luaState, int stackPos)
{ {
return translator.getFunction(luaState, stackPos); return translator.getFunction(luaState, stackPos);
} }
private object getAsUserdata(KopiLua.Lua.lua_State luaState, int stackPos) private object getAsUserdata(LuaCore.lua_State luaState, int stackPos)
{ {
return translator.getUserData(luaState, stackPos); return translator.getUserData(luaState, stackPos);
} }
public object getAsObject(KopiLua.Lua.lua_State luaState, int stackPos) public object getAsObject(LuaCore.lua_State luaState, int stackPos)
{ {
if(KopiLua.Lua.lua_type(luaState, stackPos).ToLuaTypes() == LuaTypes.Table) if(LuaCore.lua_type(luaState, stackPos).ToLuaTypes() == LuaTypes.Table)
{ {
if(KopiLua.Lua.luaL_getmetafield(luaState, stackPos, "__index").ToBoolean()) if(LuaCore.luaL_getmetafield(luaState, stackPos, "__index").ToBoolean())
{ {
if(LuaLib.luaL_checkmetatable(luaState, -1)) if(LuaLib.luaL_checkmetatable(luaState, -1))
{ {
KopiLua.Lua.lua_insert(luaState, stackPos); LuaCore.lua_insert(luaState, stackPos);
KopiLua.Lua.lua_remove(luaState, stackPos+1); LuaCore.lua_remove(luaState, stackPos+1);
} }
else else
KopiLua.Lua.lua_settop(luaState, -2); LuaCore.lua_settop(luaState, -2);
} }
} }
...@@ -343,22 +346,22 @@ namespace LuaInterface ...@@ -343,22 +346,22 @@ namespace LuaInterface
return obj; return obj;
} }
public object getAsNetObject(KopiLua.Lua.lua_State luaState, int stackPos) public object getAsNetObject(LuaCore.lua_State luaState, int stackPos)
{ {
object obj = translator.getNetObject(luaState, stackPos); object obj = translator.getNetObject(luaState, stackPos);
if(obj.IsNull() && KopiLua.Lua.lua_type(luaState, stackPos).ToLuaTypes() == LuaTypes.Table) if(obj.IsNull() && LuaCore.lua_type(luaState, stackPos).ToLuaTypes() == LuaTypes.Table)
{ {
if(KopiLua.Lua.luaL_getmetafield(luaState, stackPos, "__index").ToBoolean()) if(LuaCore.luaL_getmetafield(luaState, stackPos, "__index").ToBoolean())
{ {
if(LuaLib.luaL_checkmetatable(luaState, -1)) if(LuaLib.luaL_checkmetatable(luaState, -1))
{ {
KopiLua.Lua.lua_insert(luaState, stackPos); LuaCore.lua_insert(luaState, stackPos);
KopiLua.Lua.lua_remove(luaState, stackPos+1); LuaCore.lua_remove(luaState, stackPos+1);
obj = translator.getNetObject(luaState, stackPos); obj = translator.getNetObject(luaState, stackPos);
} }
else else
KopiLua.Lua.lua_settop(luaState, -2); LuaCore.lua_settop(luaState, -2);
} }
} }
......
...@@ -29,6 +29,7 @@ using System.Reflection; ...@@ -29,6 +29,7 @@ using System.Reflection;
using System.Reflection.Emit; using System.Reflection.Emit;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using LuaInterface.Method;
namespace LuaInterface namespace LuaInterface
{ {
......
This diff is collapsed.
...@@ -24,8 +24,8 @@ ...@@ -24,8 +24,8 @@
*/ */
using System; using System;
using System.Collections.Generic;
using System.Text; using System.Text;
using System.Collections.Generic;
namespace LuaInterface namespace LuaInterface
{ {
...@@ -51,13 +51,14 @@ namespace LuaInterface ...@@ -51,13 +51,14 @@ namespace LuaInterface
public virtual void Dispose(bool disposeManagedResources) public virtual void Dispose(bool disposeManagedResources)
{ {
if (!_Disposed) if(!_Disposed)
{ {
if (disposeManagedResources) if(disposeManagedResources)
{ {
if (_Reference != 0) if(_Reference != 0)
_Interpreter.dispose(_Reference); _Interpreter.dispose(_Reference);
} }
_Interpreter = null; _Interpreter = null;
_Disposed = true; _Disposed = true;
} }
...@@ -65,12 +66,13 @@ namespace LuaInterface ...@@ -65,12 +66,13 @@ namespace LuaInterface
public override bool Equals(object o) public override bool Equals(object o)
{ {
if (o is LuaBase) if(o is LuaBase)
{ {
LuaBase l = (LuaBase)o; var l = (LuaBase)o;
return _Interpreter.compareRef(l._Reference, _Reference); return _Interpreter.compareRef(l._Reference, _Reference);
} }
else return false; else
return false;
} }
public override int GetHashCode() public override int GetHashCode()
......
...@@ -29,9 +29,11 @@ using System.Collections.Generic; ...@@ -29,9 +29,11 @@ using System.Collections.Generic;
namespace LuaInterface namespace LuaInterface
{ {
using LuaCore = KopiLua.Lua;
public class LuaFunction : LuaBase public class LuaFunction : LuaBase
{ {
internal KopiLua.Lua.lua_CFunction function; internal LuaCore.lua_CFunction function;
public LuaFunction(int reference, Lua interpreter) public LuaFunction(int reference, Lua interpreter)
{ {
...@@ -40,7 +42,7 @@ namespace LuaInterface ...@@ -40,7 +42,7 @@ namespace LuaInterface
_Interpreter = interpreter; _Interpreter = interpreter;
} }
public LuaFunction(KopiLua.Lua.lua_CFunction function, Lua interpreter) public LuaFunction(LuaCore.lua_CFunction function, Lua interpreter)
{ {
_Reference = 0; _Reference = 0;
this.function = function; this.function = function;
...@@ -55,6 +57,7 @@ namespace LuaInterface ...@@ -55,6 +57,7 @@ namespace LuaInterface
{ {
return _Interpreter.callFunction(this, args, returnTypes); return _Interpreter.callFunction(this, args, returnTypes);
} }
/* /*
* Calls the function and returns its return values inside * Calls the function and returns its return values inside
* an array * an array
...@@ -63,39 +66,41 @@ namespace LuaInterface ...@@ -63,39 +66,41 @@ namespace LuaInterface
{ {
return _Interpreter.callFunction(this, args); return _Interpreter.callFunction(this, args);
} }
/* /*
* Pushes the function into the Lua stack * Pushes the function into the Lua stack
*/ */
internal void push(KopiLua.Lua.lua_State luaState) internal void push(LuaCore.lua_State luaState)
{ {
if (_Reference != 0) if(_Reference != 0)
LuaLib.lua_getref(luaState, _Reference); LuaLib.lua_getref(luaState, _Reference);
else else
_Interpreter.pushCSFunction(function); _Interpreter.pushCSFunction(function);
} }
public override string ToString() public override string ToString()
{ {
return "function"; return "function";
} }
public override bool Equals(object o) public override bool Equals(object o)
{ {
if (o is LuaFunction) if(o is LuaFunction)
{ {
LuaFunction l = (LuaFunction)o; var l = (LuaFunction)o;
if (this._Reference != 0 && l._Reference != 0)
if(this._Reference != 0 && l._Reference != 0)
return _Interpreter.compareRef(l._Reference, this._Reference); return _Interpreter.compareRef(l._Reference, this._Reference);
else else
return this.function == l.function; return this.function == l.function;
} }
else return false; else
return false;
} }
public override int GetHashCode() public override int GetHashCode()
{ {
if (_Reference != 0) return _Reference != 0 ? _Reference : function.GetHashCode();
return _Reference;
else
return function.GetHashCode();
} }
} }
} }
\ No newline at end of file
...@@ -38,7 +38,6 @@ ...@@ -38,7 +38,6 @@
<Compile Include="CheckType.cs" /> <Compile Include="CheckType.cs" />
<Compile Include="Lua.cs" /> <Compile Include="Lua.cs" />
<Compile Include="Metatables.cs" /> <Compile Include="Metatables.cs" />
<Compile Include="MethodWrapper.cs" />
<Compile Include="ObjectTranslator.cs" /> <Compile Include="ObjectTranslator.cs" />
<Compile Include="ProxyType.cs" /> <Compile Include="ProxyType.cs" />
<Compile Include="LuaLib\LuaLib.cs" /> <Compile Include="LuaLib\LuaLib.cs" />
...@@ -68,6 +67,14 @@ ...@@ -68,6 +67,14 @@
<Compile Include="LuaLib\LuaTypes.cs" /> <Compile Include="LuaLib\LuaTypes.cs" />
<Compile Include="LuaLib\GCOption.cs" /> <Compile Include="LuaLib\GCOption.cs" />
<Compile Include="LuaLib\PseudoIndex.cs" /> <Compile Include="LuaLib\PseudoIndex.cs" />
<Compile Include="Method\MethodCache.cs" />
<Compile Include="Method\MethodArgs.cs" />
<Compile Include="Method\LuaMethodWrapper.cs" />
<Compile Include="Method\EventHandlerContainer.cs" />
<Compile Include="Method\RegisterEventHandler.cs" />
<Compile Include="Method\LuaEventHandler.cs" />
<Compile Include="Method\LuaDelegate.cs" />
<Compile Include="Method\LuaClassHelper.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.
...@@ -88,5 +95,6 @@ ...@@ -88,5 +95,6 @@
<Folder Include="GenerateEventAssembly\" /> <Folder Include="GenerateEventAssembly\" />
<Folder Include="Event\" /> <Folder Include="Event\" />
<Folder Include="Exceptions\" /> <Folder Include="Exceptions\" />
<Folder Include="Method\" />
</ItemGroup> </ItemGroup>
</Project> </Project>
...@@ -32,6 +32,8 @@ using LuaInterface.Extensions; ...@@ -32,6 +32,8 @@ using LuaInterface.Extensions;
namespace LuaInterface namespace LuaInterface
{ {
using LuaCore = KopiLua.Lua;
public static class LuaLib public static class LuaLib
{ {
private static int tag = 0; private static int tag = 0;
...@@ -77,9 +79,9 @@ namespace LuaInterface ...@@ -77,9 +79,9 @@ namespace LuaInterface
/// <param name="fn"> /// <param name="fn">
/// A <see cref="CallbackFunction"/> /// A <see cref="CallbackFunction"/>
/// </param> /// </param>
public static void lua_pushcfunction(KopiLua.Lua.lua_State state, KopiLua.Lua.lua_CFunction fn) public static void lua_pushcfunction(LuaCore.lua_State state, LuaCore.lua_CFunction fn)
{ {
KopiLua.Lua.lua_pushcclosure(state, fn, 0); LuaCore.lua_pushcclosure(state, fn, 0);
} }
#endregion #endregion
...@@ -96,9 +98,9 @@ namespace LuaInterface ...@@ -96,9 +98,9 @@ namespace LuaInterface
/// <returns> /// <returns>
/// A <see cref="System.Boolean"/> /// A <see cref="System.Boolean"/>
/// </returns> /// </returns>
public static bool luaL_dofile(KopiLua.Lua.lua_State state, string filename) public static bool luaL_dofile(LuaCore.lua_State state, string filename)
{ {
return (KopiLua.Lua.luaL_loadfile(state, filename).ToLuaEnums() == LuaEnums.Ok) && (KopiLua.Lua.lua_pcall(state, 0, (int)LuaEnums.MultiRet, 0).ToLuaEnums() == LuaEnums.Ok); return (LuaCore.luaL_loadfile(state, filename).ToLuaEnums() == LuaEnums.Ok) && (LuaCore.lua_pcall(state, 0, (int)LuaEnums.MultiRet, 0).ToLuaEnums() == LuaEnums.Ok);
} }
/// <summary> /// <summary>
...@@ -113,14 +115,14 @@ namespace LuaInterface ...@@ -113,14 +115,14 @@ namespace LuaInterface
/// <returns> /// <returns>
/// A <see cref="System.Boolean"/> /// A <see cref="System.Boolean"/>
/// </returns> /// </returns>
public static bool luaL_dostring(KopiLua.Lua.lua_State state, string chunk) public static bool luaL_dostring(LuaCore.lua_State state, string chunk)
{ {
return (KopiLua.Lua.luaL_loadstring(state, chunk).ToLuaEnums() == LuaEnums.Ok) && (KopiLua.Lua.lua_pcall(state, 0, (int)LuaEnums.MultiRet, 0).ToLuaEnums() == LuaEnums.Ok); return (LuaCore.luaL_loadstring(state, chunk).ToLuaEnums() == LuaEnums.Ok) && (LuaCore.lua_pcall(state, 0, (int)LuaEnums.MultiRet, 0).ToLuaEnums() == LuaEnums.Ok);
} }
public static LuaEnums luaL_loadbuffer(KopiLua.Lua.lua_State luaState, string buff, string name) public static LuaEnums luaL_loadbuffer(LuaCore.lua_State luaState, string buff, string name)
{ {
var result = KopiLua.Lua.luaL_loadbuffer(luaState, buff, (uint)buff.Length, name).ToLuaEnums(); var result = LuaCore.luaL_loadbuffer(luaState, buff, (uint)buff.Length, name).ToLuaEnums();
return result; return result;
} }
...@@ -136,22 +138,22 @@ namespace LuaInterface ...@@ -136,22 +138,22 @@ namespace LuaInterface
/// <param name="r"> /// <param name="r">
/// A <see cref="System.Int32"/> /// A <see cref="System.Int32"/>
/// </param> /// </param>
public static void luaL_getref(KopiLua.Lua.lua_State state, int t, int r) public static void luaL_getref(LuaCore.lua_State state, int t, int r)
{ {
KopiLua.Lua.lua_rawgeti(state, t, r); LuaCore.lua_rawgeti(state, t, r);
} }
public static bool luaL_checkmetatable(KopiLua.Lua.lua_State luaState,int index) public static bool luaL_checkmetatable(LuaCore.lua_State luaState,int index)
{ {
bool retVal = false; bool retVal = false;
Console.WriteLine("v: " + luaState.tt.ToString()); Console.WriteLine("v: " + luaState.tt.ToString());
if(KopiLua.Lua.lua_getmetatable(luaState,index)!=0) if(LuaCore.lua_getmetatable(luaState,index)!=0)
{ {
KopiLua.Lua.lua_pushlightuserdata(luaState, tag); LuaCore.lua_pushlightuserdata(luaState, tag);
KopiLua.Lua.lua_rawget(luaState, -2); LuaCore.lua_rawget(luaState, -2);
retVal = !KopiLua.Lua.lua_isnil(luaState, -1); retVal = !LuaCore.lua_isnil(luaState, -1);
KopiLua.Lua.lua_settop(luaState, -3); LuaCore.lua_settop(luaState, -3);
} }
return retVal; return retVal;
...@@ -162,43 +164,43 @@ namespace LuaInterface ...@@ -162,43 +164,43 @@ namespace LuaInterface
return tag; return tag;
} }
public static void lua_getref(KopiLua.Lua.lua_State luaState, int reference) public static void lua_getref(LuaCore.lua_State luaState, int reference)
{ {
KopiLua.Lua.lua_rawgeti(luaState, (int)PseudoIndex.Registry, reference); LuaCore.lua_rawgeti(luaState, (int)PseudoIndex.Registry, reference);
} }
public static void lua_unref(KopiLua.Lua.lua_State luaState, int reference) public static void lua_unref(LuaCore.lua_State luaState, int reference)
{ {
KopiLua.Lua.luaL_unref(luaState, (int)PseudoIndex.Registry, reference); LuaCore.luaL_unref(luaState, (int)PseudoIndex.Registry, reference);
} }
public static int luanet_rawnetobj(KopiLua.Lua.lua_State luaState,int obj) public static int luanet_rawnetobj(LuaCore.lua_State luaState,int obj)
{ {
int udata = (int)KopiLua.Lua.lua_touserdata2(luaState, obj); int udata = (int)LuaCore.lua_touserdata2(luaState, obj);
return udata != 0 ? udata : -1; return udata != 0 ? udata : -1;
} }
public static void lua_pushstdcallcfunction(KopiLua.Lua.lua_State luaState,KopiLua.Lua.lua_CFunction function) public static void lua_pushstdcallcfunction(LuaCore.lua_State luaState,LuaCore.lua_CFunction function)
{ {
lua_pushcfunction(luaState, function); lua_pushcfunction(luaState, function);
} }
public static int checkudata_raw(KopiLua.Lua.lua_State luaState, int ud, string tname) public static int checkudata_raw(LuaCore.lua_State luaState, int ud, string tname)
{ {
int p = (int)KopiLua.Lua.lua_touserdata2(luaState, ud); int p = (int)LuaCore.lua_touserdata2(luaState, ud);
//Console.WriteLine(BitConverter.ToInt32(ObjectToByteArray(KopiLua.Lua.lua_touserdata(luaState, ud)), 0)); //Console.WriteLine(BitConverter.ToInt32(ObjectToByteArray(LuaCore.lua_touserdata(luaState, ud)), 0));
if(p != 0) if(p != 0)
{ {
/* value is a userdata? */ /* value is a userdata? */
if(KopiLua.Lua.lua_getmetatable(luaState, ud)!=0) if(LuaCore.lua_getmetatable(luaState, ud)!=0)
{ {
/* does it have a metatable? */ /* does it have a metatable? */
KopiLua.Lua.lua_getfield(luaState, (int)PseudoIndex.Registry, tname); /* get correct metatable */ LuaCore.lua_getfield(luaState, (int)PseudoIndex.Registry, tname); /* get correct metatable */
bool isEqual = KopiLua.Lua.lua_rawequal(luaState, -1, -2).ToBoolean(); bool isEqual = LuaCore.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 */
KopiLua.Lua.lua_settop(luaState, -(2) - 1); LuaCore.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;
...@@ -208,27 +210,27 @@ namespace LuaInterface ...@@ -208,27 +210,27 @@ namespace LuaInterface
return 0; return 0;
} }
public static int luanet_checkudata(KopiLua.Lua.lua_State luaState, int ud, string tname) public static int luanet_checkudata(LuaCore.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; return udata != 0 ? udata : -1;
} }
public static void luanet_newudata(KopiLua.Lua.lua_State luaState, int val) public static void luanet_newudata(LuaCore.lua_State luaState, int val)
{ {
KopiLua.Lua.lua_newuserdata(luaState, (uint)val); LuaCore.lua_newuserdata(luaState, (uint)val);
} }
public static int luanet_tonetobject(KopiLua.Lua.lua_State luaState, int index) public static int luanet_tonetobject(LuaCore.lua_State luaState, int index)
{ {
int udata; int udata;
Console.WriteLine("x" + KopiLua.Lua.lua_type(luaState, index).ToString()); Console.WriteLine("x" + LuaCore.lua_type(luaState, index).ToString());
if(KopiLua.Lua.lua_type(luaState, index).ToLuaTypes() == LuaTypes.UserData) if(LuaCore.lua_type(luaState, index).ToLuaTypes() == LuaTypes.UserData)
{ {
if(luaL_checkmetatable(luaState, index)) if(luaL_checkmetatable(luaState, index))
{ {
udata = (int)KopiLua.Lua.lua_touserdata2(luaState, index); udata = (int)LuaCore.lua_touserdata2(luaState, index);
if(udata != 0) if(udata != 0)
return udata; return udata;
} }
...@@ -249,9 +251,9 @@ namespace LuaInterface ...@@ -249,9 +251,9 @@ namespace LuaInterface
return -1; return -1;
} }
public static int lua_ref(KopiLua.Lua.lua_State luaState, int lockRef) public static int lua_ref(LuaCore.lua_State luaState, int lockRef)
{ {
return lockRef != 0 ? KopiLua.Lua.luaL_ref(luaState, (int)PseudoIndex.Registry) : 0; return lockRef != 0 ? LuaCore.luaL_ref(luaState, (int)PseudoIndex.Registry) : 0;
} }
#endregion #endregion
} }
......
...@@ -24,8 +24,9 @@ ...@@ -24,8 +24,9 @@
*/ */
using System; using System;
using System.Diagnostics.CodeAnalysis;
using System.Reflection; using System.Reflection;
using System.Diagnostics.CodeAnalysis;
using LuaInterface.Extensions;
namespace LuaInterface namespace LuaInterface
{ {
...@@ -40,15 +41,18 @@ namespace LuaInterface ...@@ -40,15 +41,18 @@ namespace LuaInterface
public static void TaggedInstanceMethods(Lua lua, object o) public static void TaggedInstanceMethods(Lua lua, object o)
{ {
#region Sanity checks #region Sanity checks
if (lua == null) throw new ArgumentNullException("lua"); if(lua.IsNull())
if (o == null) throw new ArgumentNullException("o"); throw new ArgumentNullException("lua");
if(o.IsNull())
throw new ArgumentNullException("o");
#endregion #endregion
foreach (MethodInfo method in o.GetType().GetMethods(BindingFlags.Instance | BindingFlags.Public)) foreach(var method in o.GetType().GetMethods(BindingFlags.Instance | BindingFlags.Public))
{ {
foreach (LuaGlobalAttribute attribute in method.GetCustomAttributes(typeof(LuaGlobalAttribute), true)) foreach(LuaGlobalAttribute attribute in method.GetCustomAttributes(typeof(LuaGlobalAttribute), true))
{ {
if (string.IsNullOrEmpty(attribute.Name)) if(string.IsNullOrEmpty(attribute.Name))
lua.RegisterFunction(method.Name, o, method); // CLR name lua.RegisterFunction(method.Name, o, method); // CLR name
else else
lua.RegisterFunction(attribute.Name, o, method); // Custom name lua.RegisterFunction(attribute.Name, o, method); // Custom name
...@@ -66,16 +70,21 @@ namespace LuaInterface ...@@ -66,16 +70,21 @@ namespace LuaInterface
public static void TaggedStaticMethods(Lua lua, Type type) public static void TaggedStaticMethods(Lua lua, Type type)
{ {
#region Sanity checks #region Sanity checks
if (lua == null) throw new ArgumentNullException("lua"); if(lua.IsNull())
if (type == null) throw new ArgumentNullException("type"); throw new ArgumentNullException("lua");
if (!type.IsClass) throw new ArgumentException("The type must be a class!", "type");
if(type.IsNull())
throw new ArgumentNullException("type");
if(!type.IsClass)
throw new ArgumentException("The type must be a class!", "type");
#endregion #endregion
foreach (MethodInfo method in type.GetMethods(BindingFlags.Static | BindingFlags.Public)) foreach(var method in type.GetMethods(BindingFlags.Static | BindingFlags.Public))
{ {
foreach (LuaGlobalAttribute attribute in method.GetCustomAttributes(typeof(LuaGlobalAttribute), false)) foreach(LuaGlobalAttribute attribute in method.GetCustomAttributes(typeof(LuaGlobalAttribute), false))
{ {
if (string.IsNullOrEmpty(attribute.Name)) if(string.IsNullOrEmpty(attribute.Name))
lua.RegisterFunction(method.Name, null, method); // CLR name lua.RegisterFunction(method.Name, null, method); // CLR name
else else
lua.RegisterFunction(attribute.Name, null, method); // Custom name lua.RegisterFunction(attribute.Name, null, method); // Custom name
...@@ -94,17 +103,20 @@ namespace LuaInterface ...@@ -94,17 +103,20 @@ namespace LuaInterface
public static void Enumeration<T>(Lua lua) public static void Enumeration<T>(Lua lua)
{ {
#region Sanity checks #region Sanity checks
if (lua == null) throw new ArgumentNullException("lua"); if(lua.IsNull())
throw new ArgumentNullException("lua");
#endregion #endregion
Type type = typeof(T); var type = typeof(T);
if (!type.IsEnum) throw new ArgumentException("The type must be an enumeration!");
if(!type.IsEnum)
throw new ArgumentException("The type must be an enumeration!");
string[] names = Enum.GetNames(type); string[] names = Enum.GetNames(type);
var values = (T[])Enum.GetValues(type); var values = (T[])Enum.GetValues(type);
lua.NewTable(type.Name); lua.NewTable(type.Name);
for (int i = 0; i < names.Length; i++)
for(int i = 0; i < names.Length; i++)
{ {
string path = type.Name + "." + names[i]; string path = type.Name + "." + names[i];
lua[path] = values[i]; lua[path] = values[i];
......
...@@ -30,6 +30,8 @@ using System.Collections.Generic; ...@@ -30,6 +30,8 @@ using System.Collections.Generic;
namespace LuaInterface namespace LuaInterface
{ {
using LuaCore = KopiLua.Lua;
/* /*
* Wrapper class for Lua tables * Wrapper class for Lua tables
* *
...@@ -58,6 +60,7 @@ namespace LuaInterface ...@@ -58,6 +60,7 @@ namespace LuaInterface
_Interpreter.setObject(_Reference, field, value); _Interpreter.setObject(_Reference, field, value);
} }
} }
/* /*
* Indexer for numeric fields of the table * Indexer for numeric fields of the table
*/ */
...@@ -73,7 +76,6 @@ namespace LuaInterface ...@@ -73,7 +76,6 @@ namespace LuaInterface
} }
} }
public System.Collections.IDictionaryEnumerator GetEnumerator() public System.Collections.IDictionaryEnumerator GetEnumerator()
{ {
return _Interpreter.GetTableDict(this).GetEnumerator(); return _Interpreter.GetTableDict(this).GetEnumerator();
...@@ -102,8 +104,8 @@ namespace LuaInterface ...@@ -102,8 +104,8 @@ namespace LuaInterface
{ {
object obj = _Interpreter.rawGetObject(_Reference, field); object obj = _Interpreter.rawGetObject(_Reference, field);
if (obj is KopiLua.Lua.lua_CFunction) if(obj is LuaCore.lua_CFunction)
return new LuaFunction((KopiLua.Lua.lua_CFunction)obj, _Interpreter); return new LuaFunction((LuaCore.lua_CFunction)obj, _Interpreter);
else else
return obj; return obj;
} }
...@@ -111,10 +113,11 @@ namespace LuaInterface ...@@ -111,10 +113,11 @@ namespace LuaInterface
/* /*
* Pushes this table into the Lua stack * Pushes this table into the Lua stack
*/ */
internal void push(KopiLua.Lua.lua_State luaState) internal void push(LuaCore.lua_State luaState)
{ {
LuaLib.lua_getref(luaState, _Reference); LuaLib.lua_getref(luaState, _Reference);
} }
public override string ToString() public override string ToString()
{ {
return "table"; return "table";
......
...@@ -29,6 +29,8 @@ using System.Collections.Generic; ...@@ -29,6 +29,8 @@ using System.Collections.Generic;
namespace LuaInterface namespace LuaInterface
{ {
using LuaCore = KopiLua.Lua;
public class LuaUserData : LuaBase public class LuaUserData : LuaBase
{ {
public LuaUserData(int reference, Lua interpreter) public LuaUserData(int reference, Lua interpreter)
...@@ -51,6 +53,7 @@ namespace LuaInterface ...@@ -51,6 +53,7 @@ namespace LuaInterface
_Interpreter.setObject(_Reference, field, value); _Interpreter.setObject(_Reference, field, value);
} }
} }
/* /*
* Indexer for numeric fields of the userdata * Indexer for numeric fields of the userdata
*/ */
...@@ -65,6 +68,7 @@ namespace LuaInterface ...@@ -65,6 +68,7 @@ namespace LuaInterface
_Interpreter.setObject(_Reference, field, value); _Interpreter.setObject(_Reference, field, value);
} }
} }
/* /*
* Calls the userdata and returns its return values inside * Calls the userdata and returns its return values inside
* an array * an array
...@@ -73,13 +77,15 @@ namespace LuaInterface ...@@ -73,13 +77,15 @@ namespace LuaInterface
{ {
return _Interpreter.callFunction(this, args); return _Interpreter.callFunction(this, args);
} }
/* /*
* Pushes the userdata into the Lua stack * Pushes the userdata into the Lua stack
*/ */
internal void push(KopiLua.Lua.lua_State luaState) internal void push(LuaCore.lua_State luaState)
{ {
LuaLib.lua_getref(luaState, _Reference); LuaLib.lua_getref(luaState, _Reference);
} }
public override string ToString() public override string ToString()
{ {
return "userdata"; return "userdata";
......
This diff is collapsed.
/*
* 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;
using System.Collections.Generic;
namespace LuaInterface.Method
{
/// <summary>
/// We keep track of what delegates we have auto attached to an event - to allow us to cleanly exit a LuaInterface session
/// </summary>
class EventHandlerContainer : IDisposable
{
private Dictionary<Delegate, RegisterEventHandler> dict = new Dictionary<Delegate, RegisterEventHandler>();
public void Add(Delegate handler, RegisterEventHandler eventInfo)
{
dict.Add(handler, eventInfo);
}
public void Remove(Delegate handler)
{
bool found = dict.Remove(handler);
Debug.Assert(found);
}
/// <summary>
/// Remove any still registered handlers
/// </summary>
public void Dispose()
{
foreach(KeyValuePair<Delegate, RegisterEventHandler> pair in dict)
pair.Value.RemovePending(pair.Key);
dict.Clear();
}
}
}
\ 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.Method
{
/*
* Static helper methods for Lua tables acting as CLR objects.
*
* Author: Fabio Mascarenhas
* Version: 1.0
*/
public class LuaClassHelper
{
/*
* Gets the function called name from the provided table,
* returning null if it does not exist
*/
public static LuaFunction getTableFunction(LuaTable luaTable, string name)
{
object funcObj = luaTable.rawget(name);
if(funcObj is LuaFunction)
return (LuaFunction)funcObj;
else
return null;
}
/*
* Calls the provided function with the provided parameters
*/
public static object callFunction(LuaFunction function, object[] args, Type[] returnTypes, object[] inArgs, int[] outArgs)
{
// args is the return array of arguments, inArgs is the actual array
// of arguments passed to the function (with in parameters only), outArgs
// has the positions of out parameters
object returnValue;
int iRefArgs;
object[] returnValues = function.call(inArgs, returnTypes);
if(returnTypes[0] == typeof(void))
{
returnValue = null;
iRefArgs = 0;
}
else
{
returnValue = returnValues[0];
iRefArgs = 1;
}
for(int i = 0; i < outArgs.Length; i++)
{
args[outArgs[i]] = returnValues[iRefArgs];
iRefArgs++;
}
return returnValue;
}
}
}
\ 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.Method
{
/*
* Wrapper class for Lua functions as delegates
* Subclasses with correct signatures are created
* at runtime.
*
* Author: Fabio Mascarenhas
* Version: 1.0
*/
public class LuaDelegate
{
public LuaFunction function;
public Type[] returnTypes;
public LuaDelegate()
{
function = null;
returnTypes = null;
}
public object callFunction(object[] args, object[] inArgs, int[] outArgs)
{
// args is the return array of arguments, inArgs is the actual array
// of arguments passed to the function (with in parameters only), outArgs
// has the positions of out parameters
object returnValue;
int iRefArgs;
object[] returnValues = function.call(inArgs, returnTypes);
if(returnTypes[0] == typeof(void))
{
returnValue = null;
iRefArgs = 0;
}
else
{
returnValue = returnValues[0];
iRefArgs = 1;
}
// Sets the value of out and ref parameters (from
// the values returned by the Lua function).
for(int i = 0; i < outArgs.Length; i++)
{
args[outArgs[i]] = returnValues[iRefArgs];
iRefArgs++;
}
return returnValue;
}
}
}
\ 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.Method
{
/*
* Base wrapper class for Lua function event handlers.
* Subclasses that do actual event handling are created
* at runtime.
*
* Author: Fabio Mascarenhas
* Version: 1.0
*/
public class LuaEventHandler
{
public LuaFunction handler = null;
// CP: Fix provided by Ben Bryant for delegates with one param
// link: http://luaforge.net/forum/message.php?msg_id=9318
public void handleEvent(object[] args)
{
handler.Call(args);
}
//public void handleEvent(object sender,object data)
//{
// handler.call(new object[] { sender,data },new Type[0]);
//}
}
}
\ 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.Reflection;
using System.Collections.Generic;
using LuaInterface.Exceptions;
using LuaInterface.Extensions;
namespace LuaInterface.Method
{
using LuaCore = KopiLua.Lua;
/*
* Argument extraction with type-conversion function
*/
delegate object ExtractValue(LuaCore.lua_State luaState, int stackPos);
/*
* Wrapper class for methods/constructors accessed from Lua.
*
* Author: Fabio Mascarenhas
* Version: 1.0
*/
class LuaMethodWrapper
{
private ObjectTranslator _Translator;
private MethodBase _Method;
private MethodCache _LastCalledMethod = new MethodCache();
private string _MethodName;
private MemberInfo[] _Members;
private IReflect _TargetType;
private ExtractValue _ExtractTarget;
private object _Target;
private BindingFlags _BindingType;
/*
* Constructs the wrapper for a known MethodBase instance
*/
public LuaMethodWrapper(ObjectTranslator translator, object target, IReflect targetType, MethodBase method)
{
_Translator = translator;
_Target = target;
_TargetType = targetType;
if(!targetType.IsNull())
_ExtractTarget = translator.typeChecker.getExtractor(targetType);
_Method = method;
_MethodName = method.Name;
if(method.IsStatic)
_BindingType = BindingFlags.Static;
else
_BindingType = BindingFlags.Instance;
}
/*
* Constructs the wrapper for a known method name
*/
public LuaMethodWrapper(ObjectTranslator translator, IReflect targetType, string methodName, BindingFlags bindingType)
{
_Translator = translator;
_MethodName = methodName;
_TargetType = targetType;
if(!targetType.IsNull())
_ExtractTarget = translator.typeChecker.getExtractor(targetType);
_BindingType = bindingType;
//CP: Removed NonPublic binding search and added IgnoreCase
_Members = targetType.UnderlyingSystemType.GetMember(methodName, MemberTypes.Method, bindingType | BindingFlags.Public | BindingFlags.IgnoreCase/*|BindingFlags.NonPublic*/);
}
/// <summary>
/// Convert C# exceptions into Lua errors
/// </summary>
/// <returns>num of things on stack</returns>
/// <param name="e">null for no pending exception</param>
int SetPendingException(Exception e)
{
return _Translator.interpreter.SetPendingException(e);
}
/*
* Calls the method. Receives the arguments from the Lua stack
* and returns values in it.
*/
public int call(LuaCore.lua_State luaState)
{
var methodToCall = _Method;
object targetObject = _Target;
bool failedCall = true;
int nReturnValues = 0;
if(!LuaCore.lua_checkstack(luaState, 5).ToBoolean())
throw new LuaException("Lua stack overflow");
bool isStatic = (_BindingType & BindingFlags.Static) == BindingFlags.Static;
SetPendingException(null);
if(methodToCall.IsNull()) // Method from name
{
if(isStatic)
targetObject = null;
else
targetObject = _ExtractTarget(luaState, 1);
//LuaCore.lua_remove(luaState,1); // Pops the receiver
if(!_LastCalledMethod.cachedMethod.IsNull()) // Cached?
{
int numStackToSkip = isStatic ? 0 : 1; // If this is an instance invoe we will have an extra arg on the stack for the targetObject
int numArgsPassed = LuaCore.lua_gettop(luaState) - numStackToSkip;
if(numArgsPassed == _LastCalledMethod.argTypes.Length) // No. of args match?
{
if(!LuaCore.lua_checkstack(luaState, _LastCalledMethod.outList.Length + 6).ToBoolean())
throw new LuaException("Lua stack overflow");
try
{
for(int i = 0; i < _LastCalledMethod.argTypes.Length; i++)
{
if(_LastCalledMethod.argTypes[i].isParamsArray)
{
object luaParamValue = _LastCalledMethod.argTypes[i].extractValue(luaState, i + 1 + numStackToSkip);
var paramArrayType = _LastCalledMethod.argTypes[i].paramsArrayType;
Array paramArray;
if(luaParamValue is LuaTable)
{
var table = (LuaTable)luaParamValue;
paramArray = Array.CreateInstance(paramArrayType, table.Values.Count);
for(int x = 1; x <= table.Values.Count; x++)
paramArray.SetValue(Convert.ChangeType(table[x], paramArrayType), x - 1);
}
else
{
paramArray = Array.CreateInstance(paramArrayType, 1);
paramArray.SetValue(luaParamValue, 0);
}
_LastCalledMethod.args[_LastCalledMethod.argTypes[i].index] = paramArray;
}
else
{
_LastCalledMethod.args[_LastCalledMethod.argTypes[i].index] =
_LastCalledMethod.argTypes[i].extractValue(luaState, i + 1 + numStackToSkip);
}
if(_LastCalledMethod.args[_LastCalledMethod.argTypes[i].index] == null &&
!LuaCore.lua_isnil(luaState, i + 1 + numStackToSkip))
throw new LuaException("argument number " + (i + 1) + " is invalid");
}
if((_BindingType & BindingFlags.Static) == BindingFlags.Static)
_Translator.push(luaState, _LastCalledMethod.cachedMethod.Invoke(null, _LastCalledMethod.args));
else
{
if(_LastCalledMethod.cachedMethod.IsConstructor)
_Translator.push(luaState, ((ConstructorInfo)_LastCalledMethod.cachedMethod).Invoke(_LastCalledMethod.args));
else
_Translator.push(luaState, _LastCalledMethod.cachedMethod.Invoke(targetObject, _LastCalledMethod.args));
}
failedCall = false;
}
catch(TargetInvocationException e)
{
// Failure of method invocation
return SetPendingException(e.GetBaseException());
}
catch(Exception e)
{
if(_Members.Length == 1) // Is the method overloaded?
// No, throw error
return SetPendingException(e);
}
}
}
// Cache miss
if(failedCall)
{
// System.Diagnostics.Debug.WriteLine("cache miss on " + methodName);
// If we are running an instance variable, we can now pop the targetObject from the stack
if(!isStatic)
{
if(targetObject.IsNull())
{
_Translator.throwError(luaState, String.Format("instance method '{0}' requires a non null target object", _MethodName));
LuaCore.lua_pushnil(luaState);
return 1;
}
LuaCore.lua_remove(luaState, 1); // Pops the receiver
}
bool hasMatch = false;
string candidateName = null;
foreach(var member in _Members)
{
candidateName = member.ReflectedType.Name + "." + member.Name;
var m = (MethodInfo)member;
bool isMethod = _Translator.matchParameters(luaState, m, ref _LastCalledMethod);
if(isMethod)
{
hasMatch = true;
break;
}
}
if(!hasMatch)
{
string msg = (candidateName == null) ? "invalid arguments to method call" : ("invalid arguments to method: " + candidateName);
_Translator.throwError(luaState, msg);
LuaCore.lua_pushnil(luaState);
return 1;
}
}
}
else // Method from MethodBase instance
{
if(methodToCall.ContainsGenericParameters)
{
bool isMethod = _Translator.matchParameters(luaState, methodToCall, ref _LastCalledMethod);
if(methodToCall.IsGenericMethodDefinition)
{
//need to make a concrete type of the generic method definition
var typeArgs = new List<Type>();
foreach(object arg in _LastCalledMethod.args)
typeArgs.Add(arg.GetType());
var concreteMethod = (methodToCall as MethodInfo).MakeGenericMethod(typeArgs.ToArray());
_Translator.push(luaState, concreteMethod.Invoke(targetObject, _LastCalledMethod.args));
failedCall = false;
}
else if(methodToCall.ContainsGenericParameters)
{
_Translator.throwError(luaState, "unable to invoke method on generic class as the current method is an open generic method");
LuaCore.lua_pushnil(luaState);
return 1;
}
}
else
{
if(!methodToCall.IsStatic && !methodToCall.IsConstructor && targetObject == null)
{
targetObject = _ExtractTarget(luaState, 1);
LuaCore.lua_remove(luaState, 1); // Pops the receiver
}
if(!_Translator.matchParameters(luaState, methodToCall, ref _LastCalledMethod))
{
_Translator.throwError(luaState, "invalid arguments to method call");
LuaCore.lua_pushnil(luaState);
return 1;
}
}
}
if(failedCall)
{
if(!LuaCore.lua_checkstack(luaState, _LastCalledMethod.outList.Length + 6).ToBoolean())
throw new LuaException("Lua stack overflow");
try
{
if(isStatic)
_Translator.push(luaState, _LastCalledMethod.cachedMethod.Invoke(null, _LastCalledMethod.args));
else
{
if(_LastCalledMethod.cachedMethod.IsConstructor)
_Translator.push(luaState, ((ConstructorInfo)_LastCalledMethod.cachedMethod).Invoke(_LastCalledMethod.args));
else
_Translator.push(luaState, _LastCalledMethod.cachedMethod.Invoke(targetObject, _LastCalledMethod.args));
}
}
catch(TargetInvocationException e)
{
return SetPendingException(e.GetBaseException());
}
catch(Exception e)
{
return SetPendingException(e);
}
}
// Pushes out and ref return values
for(int index = 0; index < _LastCalledMethod.outList.Length; index++)
{
nReturnValues++;
//for(int i=0;i<lastCalledMethod.outList.Length;i++)
_Translator.push(luaState, _LastCalledMethod.args[_LastCalledMethod.outList[index]]);
}
//by isSingle 2010-09-10 11:26:31
//Desc:
// if not return void,we need add 1,
// or we will lost the function's return value
// when call dotnet function like "int foo(arg1,out arg2,out arg3)" in lua code
if(!_LastCalledMethod.IsReturnVoid && nReturnValues > 0)
nReturnValues++;
return nReturnValues < 1 ? 1 : nReturnValues;
}
}
}
\ 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.Method
{
/*
* Parameter information
*/
struct MethodArgs
{
// Position of parameter
public int index;
// Type-conversion function
public ExtractValue extractValue;
public bool isParamsArray;
public Type paramsArrayType;
}
}
\ 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.Reflection;
using LuaInterface.Extensions;
namespace LuaInterface.Method
{
/*
* Cached method
*/
struct MethodCache
{
private MethodBase _cachedMethod;
public MethodBase cachedMethod
{
get
{
return _cachedMethod;
}
set
{
_cachedMethod = value;
var mi = value as MethodInfo;
if(!mi.IsNull())
IsReturnVoid = string.Compare(mi.ReturnType.Name, "System.Void", true) == 0;
}
}
public bool IsReturnVoid;
// List or arguments
public object[] args;
// Positions of out parameters
public int[] outList;
// Types of parameters
public MethodArgs[] argTypes;
}
}
\ No newline at end of file
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