Ejemplo n.º 1
0
 /*
  * Registers the indexing function of CLR objects
  * passed to Lua
  */
 private void createIndexingMetaFunction(SharpLua.Lua.LuaState luaState)
 {
     LuaDLL.lua_pushstring(luaState, "luaNet_indexfunction");
     LuaDLL.luaL_dostring(luaState, MetaFunctions.luaIndexFunction);	// steffenj: lua_dostring renamed to luaL_dostring
     //LuaDLL.lua_pushstdcallcfunction(luaState,indexFunction);
     LuaDLL.lua_rawset(luaState, (int)LuaIndexes.LUA_REGISTRYINDEX);
 }
Ejemplo n.º 2
0
 /*
  * Pushes the function into the Lua stack
  */
 internal void push(SharpLua.Lua.LuaState luaState)
 {
     if (_Reference != 0)
         LuaDLL.lua_getref(luaState, _Reference);
     else
         _Interpreter.pushCSFunction(function);
 }
Ejemplo n.º 3
0
 public void RegisterModule(SharpLua.LuaTypes.LuaTable environment)
 {
     LuaTable mod = new LuaTable();
     RegisterFunctions(mod);
     
     LuaTable mt = new LuaTable();
     mt.Register("__newindex", (LuaValue[] args) =>
                 {
                     string key = args[1].ToString();
                     if (key.ToLower() == "key")
                     {
                         CryptoLib.key = IExtendFramework.Encryption.SampleObjects.CreateRijndaelKeyWithSHA512(args[2].ToString());
                         Console.WriteLine("Encryption Key is now " + ByteToString(CryptoLib.key));
                     }
                     else if (key.ToLower() == "iv")
                     {
                         iv = IExtendFramework.Encryption.SampleObjects.CreateRijndaelIVWithSHA512(args[2].ToString());
                         Console.WriteLine("Encryption IV is now " + ByteToString(CryptoLib.iv));
                     }
                     
                     return LuaNil.Nil;
                 });
     mod.MetaTable = mt;
     environment.SetNameValue(ModuleName, mod);
 }
Ejemplo n.º 4
0
    static string LuaCaller(int n, SharpLua.var[] argv)
    {
        string type = SharpLua.VarToString(argv[1]);

        //TODO NO Reflection
        Type       MessageMgr = Assembly.GetExecutingAssembly().GetType("luamessage");
        MethodInfo theMethod  = MessageMgr.GetMethod(type);

        if (theMethod == null)
        {
            Debug.LogError("LuaCaller failed, no handler found:" + type);
            return(null);
        }

        object[] param = null;
        if (n > 2)
        {
            param = new object[n - 2];
            for (int i = 2; i < n; i++)
            {
                param[i - 2] = SharpLua.VarValue(argv[i]);
            }
        }

        try
        {
            theMethod.Invoke(null, param);
        } catch (Exception ex)
        {
            Debug.LogError("LuaCaller invoke failed:" + ex.ToString());
            return(null);
        }

        return(null);
    }
Ejemplo n.º 5
0
        /// <summary>
        /// CAUTION: LuaInterface's can't share the same lua state!
        /// </summary>
        /// <param name="lState"></param>
        public LuaInterface(SharpLua.Lua.LuaState lState)
        {
            lState.initializing = true;
            LuaDLL.lua_pushstring(lState, "LUAINTERFACE LOADED");
            LuaDLL.lua_gettable(lState, (int)LuaIndexes.LUA_REGISTRYINDEX);

            if (LuaDLL.lua_toboolean(lState, -1))
            {
                LuaDLL.lua_settop(lState, -2);
                throw new LuaException("There is already a LuaInterface associated with this LuaState");
            }
            else
            {
                LuaDLL.lua_settop(lState, -2);
                LuaDLL.lua_pushstring(lState, "LUAINTERFACE LOADED");
                LuaDLL.lua_pushboolean(lState, true);
                LuaDLL.lua_settable(lState, (int)LuaIndexes.LUA_REGISTRYINDEX);
                this.luaState = lState;
                LuaDLL.lua_pushvalue(lState, (int)LuaIndexes.LUA_GLOBALSINDEX);
                LuaDLL.lua_getglobal(lState, "luanet");
                LuaDLL.lua_pushstring(lState, "getmetatable");
                LuaDLL.lua_getglobal(lState, "getmetatable");
                LuaDLL.lua_settable(lState, -3);
                LuaDLL.lua_replace(lState, (int)LuaIndexes.LUA_GLOBALSINDEX);
                translator = new ObjectTranslator(this, this.luaState);
                LuaDLL.lua_replace(lState, (int)LuaIndexes.LUA_GLOBALSINDEX);
                DoString(ScriptStrings.InitLuaNet, "LuaNet");
                DoString(ScriptStrings.InitClrLib, "ClrLib");
                DoString(ScriptStrings.InitExtLib, "ExtLib");
            }
            _StatePassed = true;
            lState.initializing = false;
        }
Ejemplo n.º 6
0
 public static void Register(SharpLua L)
 {
     //L.BeginClass(typeof(UnityEngine.Animator), typeof(UnityEngine.Behaviour));
     //L.RegFunction("GetFloat", GetFloat);
     //L.RegFunction("SetFloat", SetFloat);
     //L.RegFunction("GetBool", GetBool);
     //L.EndClass();
 }
Ejemplo n.º 7
0
    public void Close()
    {
        Started = false;
        SharpLua.LuaObject close = L.GetFunction("close");
        L.CallFunction(close);

        L.Close();
        L = null;
    }
Ejemplo n.º 8
0
 /*
  * Sets up the list of objects in the Lua side
  */
 private void createLuaObjectList(SharpLua.Lua.LuaState luaState)
 {
     LuaDLL.lua_pushstring(luaState, "luaNet_objects");
     LuaDLL.lua_newtable(luaState);
     LuaDLL.lua_newtable(luaState);
     LuaDLL.lua_pushstring(luaState, "__mode");
     LuaDLL.lua_pushstring(luaState, "v");
     LuaDLL.lua_settable(luaState, -3);
     LuaDLL.lua_setmetatable(luaState, -2);
     LuaDLL.lua_settable(luaState, (int)LuaIndexes.LUA_REGISTRYINDEX);
 }
Ejemplo n.º 9
0
    static string LuaPrint(int n, SharpLua.var[] argv)
    {
        string info = "";

        for (int i = 1; i < n; i++)
        {
            info += SharpLua.VarToString(argv[i]) + "\t";
        }
        Debug.Log(info);
        return(null);
    }
Ejemplo n.º 10
0
    public LuaVM(string main, string init)
    {
        L = new SharpLua(main, init);

        SharpLua.LuaObject     set_print  = L.GetFunction("set_print");
        SharpLua.SharpFunction sharpPrint = LuaPrint;
        L.CallFunction(set_print, sharpPrint);

        SharpLua.LuaObject     loader    = L.GetFunction("unity_res_load");
        SharpLua.SharpFunction luaLoader = LuaLoader;
        L.CallFunction(loader, luaLoader);
    }
Ejemplo n.º 11
0
 /*
  * __gc metafunction of CLR objects.
  */
 private int collectObject(SharpLua.Lua.LuaState luaState)
 {
     int udata = LuaDLL.luanet_rawnetobj(luaState, 1);
     if (udata != -1)
     {
         translator.collectObject(udata);
     }
     else
     {
         // Debug.WriteLine("not found: " + udata);
     }
     return 0;
 }
Ejemplo n.º 12
0
 /*
  * Creates the metatable for superclasses (the base
  * field of registered tables)
  */
 private void createBaseClassMetatable(SharpLua.Lua.LuaState luaState)
 {
     LuaDLL.luaL_newmetatable(luaState, "luaNet_searchbase");
     LuaDLL.lua_pushstring(luaState, "__gc");
     LuaDLL.lua_pushstdcallcfunction(luaState, metaFunctions.gcFunction);
     LuaDLL.lua_settable(luaState, -3);
     LuaDLL.lua_pushstring(luaState, "__tostring");
     LuaDLL.lua_pushstdcallcfunction(luaState, metaFunctions.toStringFunction);
     LuaDLL.lua_settable(luaState, -3);
     LuaDLL.lua_pushstring(luaState, "__index");
     LuaDLL.lua_pushstdcallcfunction(luaState, metaFunctions.baseIndexFunction);
     LuaDLL.lua_settable(luaState, -3);
     LuaDLL.lua_pushstring(luaState, "__newindex");
     LuaDLL.lua_pushstdcallcfunction(luaState, metaFunctions.newindexFunction);
     LuaDLL.lua_settable(luaState, -3);
     LuaDLL.lua_settop(luaState, -2);
 }
Ejemplo n.º 13
0
    static public void Main()
    {
        SharpLua l = new SharpLua("main.lua");

        SharpLua.LuaObject load   = l.GetFunction("load");
        SharpLua.LuaObject f      = (SharpLua.LuaObject)l.CallFunction(load, "return ...")[0];
        object[]           result = l.CallFunction(f, "Hello World", l);
        Console.WriteLine((string)result[0]);
        Console.WriteLine(result[1].GetType());
        SharpLua.LuaObject gc = l.GetFunction("collectgarbage");
        l.CallFunction(gc, "collect");
        l.CollectGarbage();

        SharpLua.LuaObject     init = l.GetFunction("init");
        SharpLua.SharpFunction func = FuncCallByLua;
        l.CallFunction(init, func);
        SharpLua.LuaObject callback = l.GetFunction("callback");
        l.CallFunction(callback, 1, null, "string");
    }
Ejemplo n.º 14
0
        public ObjectTranslator(LuaInterface interpreter, SharpLua.Lua.LuaState luaState)
        {
            this.interpreter = interpreter;
            typeChecker = new CheckType(this);
            metaFunctions = new MetaFunctions(this);

            importTypeFunction = new SharpLua.Lua.lua_CFunction(this.importType);
            loadAssemblyFunction = new SharpLua.Lua.lua_CFunction(this.loadAssembly);
            registerTableFunction = new SharpLua.Lua.lua_CFunction(this.registerTable);
            unregisterTableFunction = new SharpLua.Lua.lua_CFunction(this.unregisterTable);
            getMethodSigFunction = new SharpLua.Lua.lua_CFunction(this.getMethodSignature);
            getConstructorSigFunction = new SharpLua.Lua.lua_CFunction(this.getConstructorSignature);

            ctypeFunction = new SharpLua.Lua.lua_CFunction(this.ctype);
            enumFromIntFunction = new SharpLua.Lua.lua_CFunction(this.enumFromInt);

            createLuaObjectList(luaState);
            createIndexingMetaFunction(luaState);
            createBaseClassMetatable(luaState);
            createClassMetatable(luaState);
            createFunctionMetatable(luaState);
            setGlobalFunctions(luaState);
        }
Ejemplo n.º 15
0
    static string LuaLoader(int n, SharpLua.var[] argv)
    {
        if (argv.Length < 2)
        {
            return(null);
        }
        string path = SharpLua.VarToString(argv[1]);

        if (string.IsNullOrEmpty(path))
        {
            return(null);
        }

        TextAsset script = Resources.Load(path) as TextAsset;

        if (script == null)
        {
            return(null);
        }

        argv[0].type = SharpLua.var_type.STRING;
        return(script.ToString());
    }
Ejemplo n.º 16
0
 public object extractGenerated(SharpLua.Lua.LuaState luaState, int stackPos)
 {
     return CodeGeneration.Instance.GetDelegate(delegateType, translator.getFunction(luaState, stackPos));
 }
Ejemplo n.º 17
0
 /// <summary>
 /// Extract zone points from a ZonePoint(lat,lon,alt) expression. 
 /// </summary>
 /// <param name="expr">Expression, which is found in the chunk.</param>
 /// <param name="lat">Retrived latitude.</param>
 /// <param name="lon">Retrived longitude.</param>
 /// <param name="alt">Retrived altitude.</param>
 private void getZonePoints ( SharpLua.Ast.Expression.CallExpr expr, ref double lat, ref double lon, ref double alt )
 {
     if (expr.Arguments[0] is SharpLua.Ast.Expression.UnOpExpr)
         lat = -double.Parse(((SharpLua.Ast.Expression.NumberExpr)((SharpLua.Ast.Expression.UnOpExpr)expr.Arguments[0]).Rhs).Value, System.Globalization.CultureInfo.InvariantCulture);
     else
         lat = double.Parse(((SharpLua.Ast.Expression.NumberExpr)expr.Arguments[0]).Value, System.Globalization.CultureInfo.InvariantCulture);
     if (expr.Arguments[1] is SharpLua.Ast.Expression.UnOpExpr)
         lon = -double.Parse(((SharpLua.Ast.Expression.NumberExpr)((SharpLua.Ast.Expression.UnOpExpr)expr.Arguments[1]).Rhs).Value, System.Globalization.CultureInfo.InvariantCulture);
     else
         lon = double.Parse(((SharpLua.Ast.Expression.NumberExpr)expr.Arguments[1]).Value, System.Globalization.CultureInfo.InvariantCulture);
     if (expr.Arguments[2] is SharpLua.Ast.Expression.UnOpExpr)
         alt = -double.Parse(((SharpLua.Ast.Expression.NumberExpr)((SharpLua.Ast.Expression.UnOpExpr)expr.Arguments[2]).Rhs).Value, System.Globalization.CultureInfo.InvariantCulture);
     else
         alt = double.Parse(((SharpLua.Ast.Expression.NumberExpr)expr.Arguments[2]).Value, System.Globalization.CultureInfo.InvariantCulture);
 }
Ejemplo n.º 18
0
        private static object checkudata_raw(SharpLua.Lua.LuaState L, int ud, string tname)
        {
            object p = SharpLua.Lua.lua_touserdata(L, ud);

            if (p != null)
            {  /* value is a userdata? */
                if (SharpLua.Lua.lua_getmetatable(L, ud) != 0)
                {
                    bool isEqual;

                    /* does it have a metatable? */
                    SharpLua.Lua.lua_getfield(L, (int)LuaIndexes.LUA_REGISTRYINDEX, tname);  /* get correct metatable */

                    isEqual = SharpLua.Lua.lua_rawequal(L, -1, -2) != 0;

                    // NASTY - we need our own version of the lua_pop macro
                    // lua_pop(L, 2);  /* remove both metatables */
                    SharpLua.Lua.lua_settop(L, -(2) - 1);

                    if (isEqual)   /* does it have the correct mt? */
                        return p;
                }
            }

            return null;
        }
Ejemplo n.º 19
0
 public LuaFunction(SharpLua.Lua.lua_CFunction function, LuaInterface interpreter)
 {
     _Reference = 0;
     this.function = function;
     _Interpreter = interpreter;
 }
Ejemplo n.º 20
0
 private object getAsByte(SharpLua.Lua.LuaState luaState, int stackPos)
 {
     byte retVal = (byte)LuaDLL.lua_tonumber(luaState, stackPos);
     if (retVal == 0 && !LuaDLL.lua_isnumber(luaState, stackPos)) return null;
     return retVal;
 }
Ejemplo n.º 21
0
 private object getAsFloat(SharpLua.Lua.LuaState luaState, int stackPos)
 {
     float retVal = (float)LuaDLL.lua_tonumber(luaState, stackPos);
     if (retVal == 0 && !LuaDLL.lua_isnumber(luaState, stackPos)) return null;
     return retVal;
 }
Ejemplo n.º 22
0
 //[DllImport(LUADLL,CallingConvention=CallingConvention.Cdecl)]
 public static void luaL_where(SharpLua.Lua.LuaState luaState, int level)
 {
     SharpLua.Lua.luaL_where(luaState, level);
 }
Ejemplo n.º 23
0
 //[DllImport(LUADLL,CallingConvention=CallingConvention.Cdecl)]
 public static void lua_pushlightuserdata(SharpLua.Lua.LuaState luaState, Object udata)
 {
     SharpLua.Lua.lua_pushlightuserdata(luaState, udata);
 }
Ejemplo n.º 24
0
 private object getAsUlong(SharpLua.Lua.LuaState luaState, int stackPos)
 {
     ulong retVal = (ulong)LuaDLL.lua_tonumber(luaState, stackPos);
     if (retVal == 0 && !LuaDLL.lua_isnumber(luaState, stackPos)) return null;
     return retVal;
 }
Ejemplo n.º 25
0
 private object getAsDouble(SharpLua.Lua.LuaState luaState, int stackPos)
 {
     double retVal = LuaDLL.lua_tonumber(luaState, stackPos);
     if (retVal == 0 && !LuaDLL.lua_isnumber(luaState, stackPos)) return null;
     return retVal;
 }
Ejemplo n.º 26
0
 private object getAsBoolean(SharpLua.Lua.LuaState luaState, int stackPos)
 {
     return LuaDLL.lua_toboolean(luaState, stackPos);
 }
Ejemplo n.º 27
0
 private object getAsDecimal(SharpLua.Lua.LuaState luaState, int stackPos)
 {
     decimal retVal = (decimal)LuaDLL.lua_tonumber(luaState, stackPos);
     if (retVal == 0 && !LuaDLL.lua_isnumber(luaState, stackPos)) return null;
     return retVal;
 }
Ejemplo n.º 28
0
 public object extractGenerated(SharpLua.Lua.LuaState luaState, int stackPos)
 {
     return CodeGeneration.Instance.GetClassInstance(klass, translator.getTable(luaState, stackPos));
 }
Ejemplo n.º 29
0
 public object getAsNetObject(SharpLua.Lua.LuaState luaState, int stackPos)
 {
     object obj = translator.getNetObject(luaState, stackPos);
     if (obj == null && LuaDLL.lua_type(luaState, stackPos) == LuaTypes.LUA_TTABLE)
     {
         if (LuaDLL.luaL_getmetafield(luaState, stackPos, "__index"))
         {
             if (LuaDLL.luaL_checkmetatable(luaState, -1))
             {
                 LuaDLL.lua_insert(luaState, stackPos);
                 LuaDLL.lua_remove(luaState, stackPos + 1);
                 obj = translator.getNetObject(luaState, stackPos);
             }
             else
             {
                 LuaDLL.lua_settop(luaState, -2);
             }
         }
     }
     return obj;
 }
Ejemplo n.º 30
0
 /*
  * Pushes this table into the Lua stack
  */
 internal void push(SharpLua.Lua.LuaState luaState)
 {
     LuaDLL.lua_getref(luaState, _Reference);
 }
Ejemplo n.º 31
0
        internal ExtractValue checkType(SharpLua.Lua.LuaState luaState, int stackPos, Type paramType)
        {
            LuaTypes luatype = LuaDLL.lua_type(luaState, stackPos);

            if (paramType.IsByRef) paramType = paramType.GetElementType();

            Type underlyingType = Nullable.GetUnderlyingType(paramType);
            if (underlyingType != null)
            {
                paramType = underlyingType;     // Silently convert nullable types to their non null requics
            }

            long runtimeHandleValue = paramType.TypeHandle.Value.ToInt64();

            if (paramType.Equals(typeof(object)))
                return extractValues[runtimeHandleValue];

            //CP: Added support for generic parameters
            if (paramType.IsGenericParameter)
            {
                if (luatype == LuaTypes.LUA_TBOOLEAN)
                    return extractValues[typeof(bool).TypeHandle.Value.ToInt64()];
                else if (luatype == LuaTypes.LUA_TSTRING)
                    return extractValues[typeof(string).TypeHandle.Value.ToInt64()];
                else if (luatype == LuaTypes.LUA_TTABLE)
                    return extractValues[typeof(LuaTable).TypeHandle.Value.ToInt64()];
                else if (luatype == LuaTypes.LUA_TUSERDATA)
                    return extractValues[typeof(object).TypeHandle.Value.ToInt64()];
                else if (luatype == LuaTypes.LUA_TFUNCTION)
                    return extractValues[typeof(LuaFunction).TypeHandle.Value.ToInt64()];
                else if (luatype == LuaTypes.LUA_TNUMBER)
                    return extractValues[typeof(double).TypeHandle.Value.ToInt64()];
                //else // suppress CS0642
                ;//an unsupported type was encountered
            }
            
            if (LuaDLL.lua_isnil(luaState, stackPos))
            {
                return extractNull;
            }

            if (LuaDLL.lua_isnumber(luaState, stackPos))
                return extractValues[runtimeHandleValue];

            if (paramType == typeof(bool))
            {
                if (LuaDLL.lua_isboolean(luaState, stackPos))
                    return extractValues[runtimeHandleValue];
            }
            else if (paramType == typeof(string))
            {
                if (LuaDLL.lua_isstring(luaState, stackPos))
                    return extractValues[runtimeHandleValue];
                else if (luatype == LuaTypes.LUA_TNIL)
                    return extractNetObject; // kevinh - silently convert nil to a null string pointer
            }
            else if (paramType == typeof(char))
            // string -> char support
            {
                if (LuaDLL.lua_isstring(luaState, stackPos))
                {
                    string str = LuaDLL.lua_tostring(luaState, stackPos);
                    if (str.Length == 1) // must be char length (Length == 1)
                        return extractValues[runtimeHandleValue];
                }
                else if (luatype == LuaTypes.LUA_TNIL)
                    return extractNetObject;
            }
            else if (paramType == typeof(LuaTable))
            {
                if (luatype == LuaTypes.LUA_TTABLE)
                    return extractValues[runtimeHandleValue];
            }
            else if (paramType == typeof(LuaUserData))
            {
                if (luatype == LuaTypes.LUA_TUSERDATA)
                    return extractValues[runtimeHandleValue];
            }
            else if (paramType == typeof(LuaFunction))
            {
                if (luatype == LuaTypes.LUA_TFUNCTION)
                    return extractValues[runtimeHandleValue];
            }
            else if (typeof(Delegate).IsAssignableFrom(paramType) && luatype == LuaTypes.LUA_TFUNCTION)
            {
                return new ExtractValue(new DelegateGenerator(translator, paramType).extractGenerated);
            }
            else if (paramType.IsInterface && luatype == LuaTypes.LUA_TTABLE)
            {
                return new ExtractValue(new ClassGenerator(translator, paramType).extractGenerated);
            }
            else if ((paramType.IsInterface || paramType.IsClass) && luatype == LuaTypes.LUA_TNIL)
            {
                // kevinh - allow nil to be silently converted to null - extractNetObject will return null when the item ain't found
                return extractNetObject;
            }
            else if (LuaDLL.lua_type(luaState, stackPos) == LuaTypes.LUA_TTABLE)
            {
                if (LuaDLL.luaL_getmetafield(luaState, stackPos, "__index"))
                {
                    object obj = translator.getNetObject(luaState, -1);
                    LuaDLL.lua_settop(luaState, -2);
                    if (obj != null && paramType.IsAssignableFrom(obj.GetType()))
                        return extractNetObject;
                }
                else
                    return null;
            }
            else
            {
                object obj = translator.getNetObject(luaState, stackPos);
                if (obj != null && paramType.IsAssignableFrom(obj.GetType()))
                    return extractNetObject;
            }

            return null;
        }
Ejemplo n.º 32
0
 private object getAsTable(SharpLua.Lua.LuaState luaState, int stackPos)
 {
     return translator.getTable(luaState, stackPos);
 }
Ejemplo n.º 33
0
 private object getAsChar(SharpLua.Lua.LuaState luaState, int stackPos)
 {
     if (LuaDLL.lua_isstring(luaState, stackPos))
     {
         string s = LuaDLL.lua_tostring(luaState, stackPos);
         if (s.Length == 0) // return a null char
             return '\0';
         else
         {
             if (s.Length > 1)
                 System.Diagnostics.Debug.WriteLine("String Length was greater than 1! Truncating...");
             return s[0];
         }
     }
     char retVal = (char)LuaDLL.lua_tonumber(luaState, stackPos);
     if (retVal == 0 && !LuaDLL.lua_isnumber(luaState, stackPos)) return null;
     return retVal;
 }
Ejemplo n.º 34
0
 private object getAsFunction(SharpLua.Lua.LuaState luaState, int stackPos)
 {
     return translator.getFunction(luaState, stackPos);
 }
Ejemplo n.º 35
0
 private object getAsUserdata(SharpLua.Lua.LuaState luaState, int stackPos)
 {
     return translator.getUserData(luaState, stackPos);
 }
Ejemplo n.º 36
0
 private object getAsString(SharpLua.Lua.LuaState luaState, int stackPos)
 {
     string retVal = LuaDLL.lua_tostring(luaState, stackPos);
     if (retVal == "" && !LuaDLL.lua_isstring(luaState, stackPos)) 
         return null;
     return retVal;
 }
Ejemplo n.º 37
0
 //[DllImport(LUADLL,CallingConvention=CallingConvention.Cdecl)]
 public static int lua_next(SharpLua.Lua.LuaState luaState, int index)
 {
     return SharpLua.Lua.lua_next(luaState, index);
 }