コード例 #1
0
ファイル: WebRequest2_Lua.cs プロジェクト: xiaobin83/fraxy_m
        public static void Post_Lua(string url, string function, lua.LuaTable parameter, lua.LuaFunction complete, WebRequest2.Context context = null, string parametersStr = "")
        {
            Dictionary <string, object> param = new Dictionary <string, object>();

            if (parameter != null)
            {
                var L = luaVm;
                parameter.Push();
                Api.lua_pushnil(L);
                while (Api.lua_next(L, -2) != 0)
                {
                    var key   = Api.lua_tostring(L, -2);
                    var value = L.ValueAt(-1);
                    param.Add(key, value);
                    Api.lua_pop(L, 1);             // pop value
                }
                Api.lua_pop(L, 1);                 // pop table
            }

            var localComplete = complete.Retain();

            WebRequest2.Post(new System.Uri(url), function, param,
                             (s, resCode, payload, cookies, headers, localContext) =>
            {
                if (s == WebExceptionStatus.Success && resCode == HttpStatusCode.OK)
                {
                    localComplete.Invoke(true, payload);
                }
                else
                {
                    localComplete.Invoke(false);
                }
                localComplete.Dispose();
            }, context, parametersStr);
        }
コード例 #2
0
        object InvokeInternal(LuaTable target, int nrets, params object[] args)
        {
            var L   = CheckValid();
            var top = Api.lua_gettop(L);

            try
            {
                Push();
                int self = 0;
                if (target != null)
                {
                    target.Push();
                    self = 1;
                }
                for (int i = 0; i < args.Length; ++i)
                {
                    L.PushValue(args[i]);
                }
                L.Call(self + args.Length, nrets);
                if (nrets == 0)
                {
                    return(null);
                }
                else if (nrets == 1)
                {
                    var ret = L.ValueAt(-1);
                    Api.lua_settop(L, top);
                    return(ret);
                }
                else
                {
                    nrets = Api.lua_gettop(L) - top;
                    LuaTable ret = null;

                    Api.lua_createtable(L, nrets, 0);
                    for (int i = 0; i < nrets; ++i)
                    {
                        Api.lua_pushvalue(L, top + i + 1);
                        Api.lua_seti(L, -2, i + 1);
                    }
                    ret = LuaTable.MakeRefTo(L, -1);
                    Api.lua_settop(L, top);
                    return(ret);
                }
            }
            catch (Exception e)
            {
                Api.lua_settop(L, top);
                throw e;
            }
        }
コード例 #3
0
ファイル: LuaBehaviour.cs プロジェクト: xiaobin83/fraxy_m
        public void SendLuaMessage(string message, LuaTable tbl)
        {
            if (!scriptLoaded)
            {
                return;
            }

            Api.lua_rawgeti(L, Api.LUA_REGISTRYINDEX, luaBehaviourRef);
            if (Api.lua_getfield(L, -1, message) == Api.LUA_TFUNCTION)
            {
                Api.lua_pushvalue(L, -2);
                tbl.Push();
                try
                {
                    L.Call(2, 0);
                }
                catch (Exception e)
                {
                    Debug.LogErrorFormat("Invoke {0}.{1} failed: {2}", scriptName, message, e.Message);
                }
            }
            Api.lua_pop(L, 1);             // pop behaviour table
        }