Пример #1
0
static int lua_Pass_setParameterAutoBinding(lua_State* state)
{
    // Get the number of parameters.
    int paramCount = lua_gettop(state);

    // Attempt to match the parameters to a valid binding.
    switch (paramCount)
    {
        case 3:
        {
            do
            {
                if ((lua_type(state, 1) == LUA_TUSERDATA) &&
                    (lua_type(state, 2) == LUA_TSTRING || lua_type(state, 2) == LUA_TNIL) &&
                    lua_type(state, 3) == LUA_TNUMBER)
                {
                    // Get parameter 1 off the stack.
                    const char* param1 = gameplay::ScriptUtil::getString(2, false);

                    // Get parameter 2 off the stack.
                    RenderState::AutoBinding param2 = (RenderState::AutoBinding)luaL_checkint(state, 3);

                    Pass* instance = getInstance(state);
                    instance->setParameterAutoBinding(param1, param2);
                    
                    return 0;
                }
            } while (0);

            do
            {
                if ((lua_type(state, 1) == LUA_TUSERDATA) &&
                    (lua_type(state, 2) == LUA_TSTRING || lua_type(state, 2) == LUA_TNIL) &&
                    (lua_type(state, 3) == LUA_TSTRING || lua_type(state, 3) == LUA_TNIL))
                {
                    // Get parameter 1 off the stack.
                    const char* param1 = gameplay::ScriptUtil::getString(2, false);

                    // Get parameter 2 off the stack.
                    const char* param2 = gameplay::ScriptUtil::getString(3, false);

                    Pass* instance = getInstance(state);
                    instance->setParameterAutoBinding(param1, param2);
                    
                    return 0;
                }
            } while (0);

            lua_pushstring(state, "lua_Pass_setParameterAutoBinding - Failed to match the given parameters to a valid function signature.");
            lua_error(state);
            break;
        }
        default:
        {
            lua_pushstring(state, "Invalid number of parameters (expected 3).");
            lua_error(state);
            break;
        }
    }
    return 0;
}
Пример #2
0
int lua_Light_static_createSpot(lua_State* state)
{
    // Get the number of parameters.
    int paramCount = lua_gettop(state);

    // Attempt to match the parameters to a valid binding.
    switch (paramCount)
    {
        case 4:
        {
            do
            {
                if ((lua_type(state, 1) == LUA_TUSERDATA || lua_type(state, 1) == LUA_TNIL) &&
                    lua_type(state, 2) == LUA_TNUMBER &&
                    lua_type(state, 3) == LUA_TNUMBER &&
                    lua_type(state, 4) == LUA_TNUMBER)
                {
                    // Get parameter 1 off the stack.
                    bool param1Valid;
                    gameplay::ScriptUtil::LuaArray<Vector3> param1 = gameplay::ScriptUtil::getObjectPointer<Vector3>(1, "Vector3", true, &param1Valid);
                    if (!param1Valid)
                        break;

                    // Get parameter 2 off the stack.
                    float param2 = (float)luaL_checknumber(state, 2);

                    // Get parameter 3 off the stack.
                    float param3 = (float)luaL_checknumber(state, 3);

                    // Get parameter 4 off the stack.
                    float param4 = (float)luaL_checknumber(state, 4);

                    void* returnPtr = (void*)Light::createSpot(*param1, param2, param3, param4);
                    if (returnPtr)
                    {
                        gameplay::ScriptUtil::LuaObject* object = (gameplay::ScriptUtil::LuaObject*)lua_newuserdata(state, sizeof(gameplay::ScriptUtil::LuaObject));
                        object->instance = returnPtr;
                        object->owns = true;
                        luaL_getmetatable(state, "Light");
                        lua_setmetatable(state, -2);
                    }
                    else
                    {
                        lua_pushnil(state);
                    }

                    return 1;
                }
            } while (0);

            lua_pushstring(state, "lua_Light_static_createSpot - Failed to match the given parameters to a valid function signature.");
            lua_error(state);
            break;
        }
        case 6:
        {
            do
            {
                if (lua_type(state, 1) == LUA_TNUMBER &&
                    lua_type(state, 2) == LUA_TNUMBER &&
                    lua_type(state, 3) == LUA_TNUMBER &&
                    lua_type(state, 4) == LUA_TNUMBER &&
                    lua_type(state, 5) == LUA_TNUMBER &&
                    lua_type(state, 6) == LUA_TNUMBER)
                {
                    // Get parameter 1 off the stack.
                    float param1 = (float)luaL_checknumber(state, 1);

                    // Get parameter 2 off the stack.
                    float param2 = (float)luaL_checknumber(state, 2);

                    // Get parameter 3 off the stack.
                    float param3 = (float)luaL_checknumber(state, 3);

                    // Get parameter 4 off the stack.
                    float param4 = (float)luaL_checknumber(state, 4);

                    // Get parameter 5 off the stack.
                    float param5 = (float)luaL_checknumber(state, 5);

                    // Get parameter 6 off the stack.
                    float param6 = (float)luaL_checknumber(state, 6);

                    void* returnPtr = (void*)Light::createSpot(param1, param2, param3, param4, param5, param6);
                    if (returnPtr)
                    {
                        gameplay::ScriptUtil::LuaObject* object = (gameplay::ScriptUtil::LuaObject*)lua_newuserdata(state, sizeof(gameplay::ScriptUtil::LuaObject));
                        object->instance = returnPtr;
                        object->owns = true;
                        luaL_getmetatable(state, "Light");
                        lua_setmetatable(state, -2);
                    }
                    else
                    {
                        lua_pushnil(state);
                    }

                    return 1;
                }
            } while (0);

            lua_pushstring(state, "lua_Light_static_createSpot - Failed to match the given parameters to a valid function signature.");
            lua_error(state);
            break;
        }
        default:
        {
            lua_pushstring(state, "Invalid number of parameters (expected 4 or 6).");
            lua_error(state);
            break;
        }
    }
    return 0;
}
Пример #3
0
int lua_AudioSource_setVelocity(lua_State* state)
{
    // Get the number of parameters.
    int paramCount = lua_gettop(state);

    // Attempt to match the parameters to a valid binding.
    switch (paramCount)
    {
        case 2:
        {
            if ((lua_type(state, 1) == LUA_TUSERDATA) &&
                (lua_type(state, 2) == LUA_TUSERDATA || lua_type(state, 2) == LUA_TNIL))
            {
                // Get parameter 1 off the stack.
                ScriptUtil::LuaArray<Vector3> param1 = ScriptUtil::getObjectPointer<Vector3>(2, "Vector3", true);

                AudioSource* instance = getInstance(state);
                instance->setVelocity(*param1);
                
                return 0;
            }
            else
            {
                lua_pushstring(state, "lua_AudioSource_setVelocity - Failed to match the given parameters to a valid function signature.");
                lua_error(state);
            }
            break;
        }
        case 4:
        {
            if ((lua_type(state, 1) == LUA_TUSERDATA) &&
                lua_type(state, 2) == LUA_TNUMBER &&
                lua_type(state, 3) == LUA_TNUMBER &&
                lua_type(state, 4) == LUA_TNUMBER)
            {
                // Get parameter 1 off the stack.
                float param1 = (float)luaL_checknumber(state, 2);

                // Get parameter 2 off the stack.
                float param2 = (float)luaL_checknumber(state, 3);

                // Get parameter 3 off the stack.
                float param3 = (float)luaL_checknumber(state, 4);

                AudioSource* instance = getInstance(state);
                instance->setVelocity(param1, param2, param3);
                
                return 0;
            }
            else
            {
                lua_pushstring(state, "lua_AudioSource_setVelocity - Failed to match the given parameters to a valid function signature.");
                lua_error(state);
            }
            break;
        }
        default:
        {
            lua_pushstring(state, "Invalid number of parameters (expected 2 or 4).");
            lua_error(state);
            break;
        }
    }
    return 0;
}
Пример #4
0
void lua_beginblock() {
	if (lua_state->numCblocks >= MAX_C_BLOCKS)
		lua_error("too many nested blocks");
	lua_state->Cblocks[lua_state->numCblocks] = lua_state->Cstack;
	lua_state->numCblocks++;
}
Пример #5
0
int CLuaHandleSynced::CallAsTeam(lua_State* L)
{
	CLuaHandleSynced* lhs = GetActiveHandle();
	if (lhs->teamsLocked) {
		luaL_error(L, "CallAsTeam() called when teams are locked");
	}
	const int args = lua_gettop(L);
	if ((args < 2) || !lua_isfunction(L, 2)) {
		luaL_error(L, "Incorrect arguments to CallAsTeam()");
	}

	// save the current access
	const bool prevFullCtrl    = lhs->fullCtrl;
	const bool prevFullRead    = lhs->fullRead;
	const int prevCtrlTeam     = lhs->ctrlTeam;
	const int prevReadTeam     = lhs->readTeam;
	const int prevReadAllyTeam = lhs->readAllyTeam;
	const int prevSelectTeam   = lhs->selectTeam;

	// parse the new access
	if (lua_isnumber(L, 1)) {
		const int teamID = lua_toint(L, 1);
		if ((teamID < MinSpecialTeam) || (teamID >= teamHandler->ActiveTeams())) {
			luaL_error(L, "Bad teamID in SetCtrlTeam");
		}
		// ctrl
		lhs->ctrlTeam = teamID;
		lhs->fullCtrl = (lhs->ctrlTeam == CEventClient::AllAccessTeam);
		// read
		lhs->readTeam = teamID;
		lhs->readAllyTeam = (teamID < 0) ? teamID : teamHandler->AllyTeam(teamID);
		lhs->fullRead = (lhs->readAllyTeam == CEventClient::AllAccessTeam);
		activeFullRead     = lhs->fullRead;
		activeReadAllyTeam = lhs->readAllyTeam;
		// select
		lhs->selectTeam = teamID;
	}
	else if (lua_istable(L, 1)) {
		const int table = 1;
		for (lua_pushnil(L); lua_next(L, table) != 0; lua_pop(L, 1)) {
			if (!lua_israwstring(L, -2) || !lua_isnumber(L, -1)) {
				continue;
			}
			const string key = lua_tostring(L, -2);
			const int teamID = lua_toint(L, -1);
			if ((teamID < MinSpecialTeam) || (teamID >= teamHandler->ActiveTeams())) {
				luaL_error(L, "Bad teamID in SetCtrlTeam");
			}

			if (key == "ctrl") {
				lhs->ctrlTeam = teamID;
				lhs->fullCtrl = (lhs->ctrlTeam == CEventClient::AllAccessTeam);
			}
			else if (key == "read") {
				lhs->readTeam = teamID;
				lhs->readAllyTeam = (teamID < 0) ? teamID : teamHandler->AllyTeam(teamID);
				lhs->fullRead = (lhs->readAllyTeam == CEventClient::AllAccessTeam);
				activeFullRead     = lhs->fullRead;
				activeReadAllyTeam = lhs->readAllyTeam;
			}
			else if (key == "select") {
				lhs->selectTeam = teamID;
			}
		}
	}
	else {
		luaL_error(L, "Incorrect arguments to CallAsTeam()");
	}

	// call the function
	const int funcArgs = lua_gettop(L) - 2;

	// protected call so that the permissions are always reverted
	const int error = lua_pcall(lhs->L, funcArgs, LUA_MULTRET, 0);

	// revert the permissions
	lhs->fullCtrl      = prevFullCtrl;
	lhs->fullRead      = prevFullRead;
	lhs->ctrlTeam      = prevCtrlTeam;
	lhs->readTeam      = prevReadTeam;
	lhs->readAllyTeam  = prevReadAllyTeam;
	lhs->selectTeam    = prevSelectTeam;
	activeFullRead     = prevFullRead;
	activeReadAllyTeam = prevReadAllyTeam;

	if (error != 0) {
		logOutput.Print("error = %i, %s, %s\n",
		                error, "CallAsTeam", lua_tostring(L, -1));
		lua_error(L);
	}

	return lua_gettop(L) - 1;	// the teamID/table is still on the stack
}
static int CBaseCombatWeapon___index (lua_State *L) {
    CBaseCombatWeapon *pWeapon = lua_toweapon(L, 1);
    if (pWeapon == NULL) {  /* avoid extra test when d is not 0 */
        lua_Debug ar1;
        lua_getstack(L, 1, &ar1);
        lua_getinfo(L, "fl", &ar1);
        lua_Debug ar2;
        lua_getinfo(L, ">S", &ar2);
        lua_pushfstring(L, "%s:%d: attempt to index a NULL entity", ar2.short_src, ar1.currentline);
        return lua_error(L);
    }
    const char *field = luaL_checkstring(L, 2);
    if (Q_strcmp(field, "m_bAltFiresUnderwater") == 0)
        lua_pushboolean(L, pWeapon->m_bAltFiresUnderwater);
    else if (Q_strcmp(field, "m_bFireOnEmpty") == 0)
        lua_pushboolean(L, pWeapon->m_bFireOnEmpty);
    else if (Q_strcmp(field, "m_bFiresUnderwater") == 0)
        lua_pushboolean(L, pWeapon->m_bFiresUnderwater);
    else if (Q_strcmp(field, "m_bInReload") == 0)
        lua_pushboolean(L, pWeapon->m_bInReload);
    else if (Q_strcmp(field, "m_bReloadsSingly") == 0)
        lua_pushboolean(L, pWeapon->m_bReloadsSingly);
    else if (Q_strcmp(field, "m_fFireDuration") == 0)
        lua_pushnumber(L, pWeapon->m_fFireDuration);
    else if (Q_strcmp(field, "m_flNextEmptySoundTime") == 0)
        lua_pushnumber(L, pWeapon->m_flNextEmptySoundTime);
    else if (Q_strcmp(field, "m_flNextPrimaryAttack") == 0)
        lua_pushnumber(L, pWeapon->m_flNextPrimaryAttack);
    else if (Q_strcmp(field, "m_flNextSecondaryAttack") == 0)
        lua_pushnumber(L, pWeapon->m_flNextSecondaryAttack);
    else if (Q_strcmp(field, "m_flTimeWeaponIdle") == 0)
        lua_pushnumber(L, pWeapon->m_flTimeWeaponIdle);
    else if (Q_strcmp(field, "m_flUnlockTime") == 0)
        lua_pushnumber(L, pWeapon->m_flUnlockTime);
    else if (Q_strcmp(field, "m_fMaxRange1") == 0)
        lua_pushnumber(L, pWeapon->m_fMaxRange1);
    else if (Q_strcmp(field, "m_fMaxRange2") == 0)
        lua_pushnumber(L, pWeapon->m_fMaxRange2);
    else if (Q_strcmp(field, "m_fMinRange1") == 0)
        lua_pushnumber(L, pWeapon->m_fMinRange1);
    else if (Q_strcmp(field, "m_fMinRange2") == 0)
        lua_pushnumber(L, pWeapon->m_fMinRange2);
    else if (Q_strcmp(field, "m_iClip1") == 0)
        lua_pushinteger(L, pWeapon->m_iClip1);
    else if (Q_strcmp(field, "m_iClip2") == 0)
        lua_pushinteger(L, pWeapon->m_iClip2);
    else if (Q_strcmp(field, "m_iPrimaryAmmoType") == 0)
        lua_pushinteger(L, pWeapon->m_iPrimaryAmmoType);
    else if (Q_strcmp(field, "m_iSecondaryAmmoType") == 0)
        lua_pushinteger(L, pWeapon->m_iSecondaryAmmoType);
    else if (Q_strcmp(field, "m_iState") == 0)
        lua_pushinteger(L, pWeapon->m_iState);
    else if (Q_strcmp(field, "m_iSubType") == 0)
        lua_pushinteger(L, pWeapon->m_iSubType);
    else if (Q_strcmp(field, "m_iViewModelIndex") == 0)
        lua_pushinteger(L, pWeapon->m_iViewModelIndex);
    else if (Q_strcmp(field, "m_iWorldModelIndex") == 0)
        lua_pushinteger(L, pWeapon->m_iWorldModelIndex);
    else if (Q_strcmp(field, "m_nViewModelIndex") == 0)
        lua_pushinteger(L, pWeapon->m_nViewModelIndex);
    else if (pWeapon->m_nTableReference != LUA_NOREF) {
        lua_getref(L, pWeapon->m_nTableReference);
        lua_getfield(L, -1, field);
        if (lua_isnil(L, -1)) {
            lua_pop(L, 2);
            lua_getmetatable(L, 1);
            lua_getfield(L, -1, field);
            if (lua_isnil(L, -1)) {
                lua_pop(L, 2);
                luaL_getmetatable(L, "CBaseAnimating");
                lua_getfield(L, -1, field);
                if (lua_isnil(L, -1)) {
                    lua_pop(L, 2);
                    luaL_getmetatable(L, "CBaseEntity");
                    lua_getfield(L, -1, field);
                }
            }
        }
    }
    else {
        lua_getmetatable(L, 1);
        lua_getfield(L, -1, field);
        if (lua_isnil(L, -1)) {
            lua_pop(L, 2);
            luaL_getmetatable(L, "CBaseAnimating");
            lua_getfield(L, -1, field);
            if (lua_isnil(L, -1)) {
                lua_pop(L, 2);
                luaL_getmetatable(L, "CBaseEntity");
                lua_getfield(L, -1, field);
            }
        }
    }
    return 1;
}
Пример #7
0
/*
** Executes the given Lua function. Parameters are between [base,top).
** Returns n such that the the results are between [n,top).
*/
StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
  const Proto *const tf = cl->f.l;
  StkId top;  /* keep top local, for performance */
  const Instruction *pc = tf->code;
  TString **const kstr = tf->kstr;
  const lua_Hook linehook = L->linehook;
  infovalue(base-1)->pc = &pc;
  luaD_checkstack(L, tf->maxstacksize+EXTRA_STACK);
  if (tf->is_vararg)  /* varargs? */
    adjust_varargs(L, base, tf->numparams);
  else
    luaD_adjusttop(L, base, tf->numparams);
  top = L->top;
  /* main loop of interpreter */
  for (;;) {
    const Instruction i = *pc++;
    if (linehook)
      traceexec(L, base, top, linehook);
    switch (GET_OPCODE(i)) {
      case OP_END: {
        L->top = top;
        return top;
      }
      case OP_RETURN: {
        L->top = top;
        return base+GETARG_U(i);
      }
      case OP_CALL: {
        int nres = GETARG_B(i);
        if (nres == MULT_RET) nres = LUA_MULTRET;
        L->top = top;
        luaD_call(L, base+GETARG_A(i), nres);
        top = L->top;
        break;
      }
      case OP_TAILCALL: {
        L->top = top;
        luaD_call(L, base+GETARG_A(i), LUA_MULTRET);
        return base+GETARG_B(i);
      }
      case OP_PUSHNIL: {
        int n = GETARG_U(i);
        LUA_ASSERT(n>0, "invalid argument");
        do {
          ttype(top++) = LUA_TNIL;
        } while (--n > 0);
        break;
      }
      case OP_POP: {
        top -= GETARG_U(i);
        break;
      }
      case OP_PUSHINT: {
        ttype(top) = LUA_TNUMBER;
        nvalue(top) = (Number)GETARG_S(i);
        top++;
        break;
      }
      case OP_PUSHSTRING: {
        ttype(top) = LUA_TSTRING;
        tsvalue(top) = kstr[GETARG_U(i)];
        top++;
        break;
      }
      case OP_PUSHNUM: {
        ttype(top) = LUA_TNUMBER;
        nvalue(top) = tf->knum[GETARG_U(i)];
        top++;
        break;
      }
      case OP_PUSHNEGNUM: {
        ttype(top) = LUA_TNUMBER;
        nvalue(top) = -tf->knum[GETARG_U(i)];
        top++;
        break;
      }
      case OP_PUSHUPVALUE: {
        *top++ = cl->upvalue[GETARG_U(i)];
        break;
      }
      case OP_GETLOCAL: {
        *top++ = *(base+GETARG_U(i));
        break;
      }
      case OP_GETGLOBAL: {
        L->top = top;
        *top = *luaV_getglobal(L, kstr[GETARG_U(i)]);
        top++;
        break;
      }
      case OP_GETTABLE: {
        L->top = top;
        top--;
        *(top-1) = *luaV_gettable(L, top-1);
        break;
      }
      case OP_GETDOTTED: {
        ttype(top) = LUA_TSTRING;
        tsvalue(top) = kstr[GETARG_U(i)];
        L->top = top+1;
        *(top-1) = *luaV_gettable(L, top-1);
        break;
      }
      case OP_GETINDEXED: {
        *top = *(base+GETARG_U(i));
        L->top = top+1;
        *(top-1) = *luaV_gettable(L, top-1);
        break;
      }
      case OP_PUSHSELF: {
        TObject receiver;
        receiver = *(top-1);
        ttype(top) = LUA_TSTRING;
        tsvalue(top++) = kstr[GETARG_U(i)];
        L->top = top;
        *(top-2) = *luaV_gettable(L, top-2);
        *(top-1) = receiver;
        break;
      }
      case OP_CREATETABLE: {
        L->top = top;
        luaC_checkGC(L);
        hvalue(top) = luaH_new(L, GETARG_U(i));
        ttype(top) = LUA_TTABLE;
        top++;
        break;
      }
      case OP_SETLOCAL: {
        *(base+GETARG_U(i)) = *(--top);
        break;
      }
      case OP_SETGLOBAL: {
        L->top = top;
        luaV_setglobal(L, kstr[GETARG_U(i)]);
        top--;
        break;
      }
      case OP_SETTABLE: {
        StkId t = top-GETARG_A(i);
        L->top = top;
        luaV_settable(L, t, t+1);
        top -= GETARG_B(i);  /* pop values */
        break;
      }
      case OP_SETLIST: {
        int aux = GETARG_A(i) * LFIELDS_PER_FLUSH;
        int n = GETARG_B(i);
        Hash *arr = hvalue(top-n-1);
        L->top = top-n;  /* final value of `top' (in case of errors) */
        for (; n; n--)
          *luaH_setint(L, arr, n+aux) = *(--top);
        break;
      }
      case OP_SETMAP: {
        int n = GETARG_U(i);
        StkId finaltop = top-2*n;
        Hash *arr = hvalue(finaltop-1);
        L->top = finaltop;  /* final value of `top' (in case of errors) */
        for (; n; n--) {
          top-=2;
          *luaH_set(L, arr, top) = *(top+1);
        }
        break;
      }
      case OP_ADD: {
        if (tonumber(top-2) || tonumber(top-1))
          call_arith(L, top, TM_ADD);
        else
          nvalue(top-2) += nvalue(top-1);
        top--;
        break;
      }
      case OP_ADDI: {
        if (tonumber(top-1)) {
          ttype(top) = LUA_TNUMBER;
          nvalue(top) = (Number)GETARG_S(i);
          call_arith(L, top+1, TM_ADD);
        }
        else
          nvalue(top-1) += (Number)GETARG_S(i);
        break;
      }
      case OP_SUB: {
        if (tonumber(top-2) || tonumber(top-1))
          call_arith(L, top, TM_SUB);
        else
          nvalue(top-2) -= nvalue(top-1);
        top--;
        break;
      }
      case OP_MULT: {
        if (tonumber(top-2) || tonumber(top-1))
          call_arith(L, top, TM_MUL);
        else
          nvalue(top-2) *= nvalue(top-1);
        top--;
        break;
      }
      case OP_DIV: {
        if (tonumber(top-2) || tonumber(top-1))
          call_arith(L, top, TM_DIV);
        else
          nvalue(top-2) /= nvalue(top-1);
        top--;
        break;
      }
      case OP_POW: {
        if (!call_binTM(L, top, TM_POW))
          lua_error(L, "undefined operation");
        top--;
        break;
      }
      case OP_CONCAT: {
        int n = GETARG_U(i);
        luaV_strconc(L, n, top);
        top -= n-1;
        L->top = top;
        luaC_checkGC(L);
        break;
      }
      case OP_MINUS: {
        if (tonumber(top-1)) {
          ttype(top) = LUA_TNIL;
          call_arith(L, top+1, TM_UNM);
        }
        else
          nvalue(top-1) = -nvalue(top-1);
        break;
      }
      case OP_NOT: {
        ttype(top-1) =
           (ttype(top-1) == LUA_TNIL) ? LUA_TNUMBER : LUA_TNIL;
        nvalue(top-1) = 1;
        break;
      }
      case OP_JMPNE: {
        top -= 2;
        if (!luaO_equalObj(top, top+1)) dojump(pc, i);
        break;
      }
      case OP_JMPEQ: {
        top -= 2;
        if (luaO_equalObj(top, top+1)) dojump(pc, i);
        break;
      }
      case OP_JMPLT: {
        top -= 2;
        if (luaV_lessthan(L, top, top+1, top+2)) dojump(pc, i);
        break;
      }
      case OP_JMPLE: {  /* a <= b  ===  !(b<a) */
        top -= 2;
        if (!luaV_lessthan(L, top+1, top, top+2)) dojump(pc, i);
        break;
      }
      case OP_JMPGT: {  /* a > b  ===  (b<a) */
        top -= 2;
        if (luaV_lessthan(L, top+1, top, top+2)) dojump(pc, i);
        break;
      }
      case OP_JMPGE: {  /* a >= b  ===  !(a<b) */
        top -= 2;
        if (!luaV_lessthan(L, top, top+1, top+2)) dojump(pc, i);
        break;
      }
      case OP_JMPT: {
        if (ttype(--top) != LUA_TNIL) dojump(pc, i);
        break;
      }
      case OP_JMPF: {
        if (ttype(--top) == LUA_TNIL) dojump(pc, i);
        break;
      }
      case OP_JMPONT: {
        if (ttype(top-1) == LUA_TNIL) top--;
        else dojump(pc, i);
        break;
      }
      case OP_JMPONF: {
        if (ttype(top-1) != LUA_TNIL) top--;
        else dojump(pc, i);
        break;
      }
      case OP_JMP: {
        dojump(pc, i);
        break;
      }
      case OP_PUSHNILJMP: {
        ttype(top++) = LUA_TNIL;
        pc++;
        break;
      }
      case OP_FORPREP: {
        if (tonumber(top-1))
          lua_error(L, "`for' step must be a number");
        if (tonumber(top-2))
          lua_error(L, "`for' limit must be a number");
        if (tonumber(top-3))
          lua_error(L, "`for' initial value must be a number");
        if (nvalue(top-1) > 0 ?
            nvalue(top-3) > nvalue(top-2) :
            nvalue(top-3) < nvalue(top-2)) {  /* `empty' loop? */
          top -= 3;  /* remove control variables */
          dojump(pc, i);  /* jump to loop end */
        }
        break;
      }
      case OP_FORLOOP: {
        LUA_ASSERT(ttype(top-1) == LUA_TNUMBER, "invalid step");
        LUA_ASSERT(ttype(top-2) == LUA_TNUMBER, "invalid limit");
        if (ttype(top-3) != LUA_TNUMBER)
          lua_error(L, "`for' index must be a number");
        nvalue(top-3) += nvalue(top-1);  /* increment index */
        if (nvalue(top-1) > 0 ?
            nvalue(top-3) > nvalue(top-2) :
            nvalue(top-3) < nvalue(top-2))
          top -= 3;  /* end loop: remove control variables */
        else
          dojump(pc, i);  /* repeat loop */
        break;
      }
      case OP_LFORPREP: {
        Node *node;
        if (ttype(top-1) != LUA_TTABLE)
          lua_error(L, "`for' table must be a table");
        node = luaH_next(L, hvalue(top-1), &luaO_nilobject);
        if (node == NULL) {  /* `empty' loop? */
          top--;  /* remove table */
          dojump(pc, i);  /* jump to loop end */
        }
        else {
          top += 2;  /* index,value */
          *(top-2) = *key(node);
          *(top-1) = *val(node);
        }
        break;
      }
      case OP_LFORLOOP: {
        Node *node;
        LUA_ASSERT(ttype(top-3) == LUA_TTABLE, "invalid table");
        node = luaH_next(L, hvalue(top-3), top-2);
        if (node == NULL)  /* end loop? */
          top -= 3;  /* remove table, key, and value */
        else {
          *(top-2) = *key(node);
          *(top-1) = *val(node);
          dojump(pc, i);  /* repeat loop */
        }
        break;
      }
      case OP_CLOSURE: {
        L->top = top;
        luaV_Lclosure(L, tf->kproto[GETARG_A(i)], GETARG_B(i));
        top = L->top;
        luaC_checkGC(L);
        break;
      }
    }
  }
}
Пример #8
0
int lua_AnimationValue_setFloat(lua_State* state)
{
    // Get the number of parameters.
    int paramCount = lua_gettop(state);

    // Attempt to match the parameters to a valid binding.
    switch (paramCount)
    {
        case 3:
        {
            if ((lua_type(state, 1) == LUA_TUSERDATA) &&
                lua_type(state, 2) == LUA_TNUMBER &&
                lua_type(state, 3) == LUA_TNUMBER)
            {
                // Get parameter 1 off the stack.
                unsigned int param1 = (unsigned int)luaL_checkunsigned(state, 2);

                // Get parameter 2 off the stack.
                float param2 = (float)luaL_checknumber(state, 3);

                AnimationValue* instance = getInstance(state);
                instance->setFloat(param1, param2);
                
                return 0;
            }
            else
            {
                lua_pushstring(state, "lua_AnimationValue_setFloat - Failed to match the given parameters to a valid function signature.");
                lua_error(state);
            }
            break;
        }
        case 4:
        {
            if ((lua_type(state, 1) == LUA_TUSERDATA) &&
                (lua_type(state, 2) == LUA_TTABLE || lua_type(state, 2) == LUA_TLIGHTUSERDATA) &&
                lua_type(state, 3) == LUA_TNUMBER &&
                lua_type(state, 4) == LUA_TNUMBER)
            {
                // Get parameter 1 off the stack.
                float* param1 = ScriptUtil::getFloatPointer(2);

                // Get parameter 2 off the stack.
                unsigned int param2 = (unsigned int)luaL_checkunsigned(state, 3);

                // Get parameter 3 off the stack.
                unsigned int param3 = (unsigned int)luaL_checkunsigned(state, 4);

                AnimationValue* instance = getInstance(state);
                instance->setFloat(param1, param2, param3);
                
                return 0;
            }
            else
            {
                lua_pushstring(state, "lua_AnimationValue_setFloat - Failed to match the given parameters to a valid function signature.");
                lua_error(state);
            }
            break;
        }
        default:
        {
            lua_pushstring(state, "Invalid number of parameters (expected 3 or 4).");
            lua_error(state);
            break;
        }
    }
    return 0;
}
Пример #9
0
static int l_load_music_async_callback(lua_State *L)
{
    load_music_async_t *async = (load_music_async_t*)lua_touserdata(L, 1);

    // Replace light UD with full UD
    lua_pushvalue(L, 1);
    lua_gettable(L, LUA_REGISTRYINDEX);
    lua_insert(L, 1);
    lua_pushnil(L);
    lua_settable(L, LUA_REGISTRYINDEX);

    // Get CB state and function
    lua_pushvalue(L, 1);
    lua_gettable(L, LUA_REGISTRYINDEX);
    lua_rawgeti(L, -1, 1);
    lua_State *cbL = lua_tothread(L, -1);
    // NB: cbL may equal L, or it may not
    lua_pop(L, 1);
    lua_rawgeti(L, -1, 2);
    if(L != cbL)
        lua_xmove(L, cbL, 1);

    // Push CB arg
    int nargs = 1;
    if(async->music == NULL)
    {
        lua_pushnil(cbL);
        if(async->err)
        {
            if(*async->err)
            {
                lua_pushstring(cbL, async->err);
                nargs = 2;
            }
            free(async->err);
        }
    }
    else
    {
        lua_rawgeti(L, 2, 3);
        if(L != cbL)
            lua_xmove(L, cbL, 1);
        music_t* pLMusic = (music_t*)lua_touserdata(cbL, -1);
        pLMusic->pMusic = async->music;
        pLMusic->pRWop = async->rwop;
        async->music = NULL;
        async->rwop = NULL;
    }

    // Finish cleanup
    if(async->rwop)
        SDL_FreeRW(async->rwop);
    lua_pushvalue(L, 1);
    lua_pushnil(L);
    lua_settable(L, LUA_REGISTRYINDEX);

    // Callback
    if(cbL == L)
    {
        lua_call(cbL, nargs, 0);
        return 0;
    }
    if(lua_pcall(cbL, nargs, 0, 0) != 0)
    {
        lua_pushliteral(L, "Error in async music load callback: ");
        lua_xmove(cbL, L, 1);
        lua_tostring(L, -1);
        lua_concat(L, 2);
        lua_error(L);
    }
    return 0;
}
Пример #10
0
void cbLuaBreakHook(lua_State *L, lua_Debug *ar)
{
    // Need some other way to do this in Linux.
#ifdef __PRAXIS_WINDOWS__
    // Force GetAsyncKeyState to be called for both keys so that
    // their respective statuses are up to date.
    // Previously, only Ctrl was kept up to date. Q was checked only if Ctrl held down.
    // Press q, then other stuff, then when Ctrl pressed, a break would be triggered.
    bool bCtrlDown = ::GetAsyncKeyState(VK_LCONTROL) != 0;
    bool bQDown = ::GetAsyncKeyState(0x51) != 0;
    if(bCtrlDown && bQDown) // Ctrl-Q pressed
    {
        // Break detected. Check the time since last break.

        // If its too low, ignore.
        int nTime = ::timeGetTime();
        if(nTime < (g_nLastBreakTime + 200))
            return;

        // If its high enough, then break.
        g_nLastBreakTime = nTime;

        lua_pushstring(L, "User break.");
        lua_error(L);
        return;
    }
#endif

#ifdef __PRAXIS_LINUX__
    // SDL_GetKeyState
    // http://sdl.beuc.net/sdl.wiki/SDL_GetKeyState
    //
    // #include "SDL.h"
    // Uint8 *SDL_GetKeyState(int *numkeys);
    //
    // Example:
    //
    // Uint8 *keystate = SDL_GetKeyState(NULL);
    // if ( keystate[SDLK_RETURN] ) printf("Return Key Pressed.\n");
    // if ( keystate[SDLK_RIGHT] && keystate[SDLK_UP] ) printf("Right and Up Keys Pressed.\n");
    //
    // Note: Use SDL_PumpEvents to update the state array
    //
    // http://sdl.beuc.net/sdl.wiki/SDLKey
    // SDLK_LCTRL
    // SDLK_RCTRL
    // SDLK_q
    //
    // Calling SDL_PumpEvents shouldn't have side effects because SDL isn't being used for OpenGL

    char keys_return[32];

    //glutMainLoopEvent();
    XQueryKeymap(g_pAppDisplay, keys_return);

#if 0
    bool bNonZero = false;
    for (int i = 0; i < 32; i++)
    {
        if(keys_return[i] != 0)
            bNonZero = true;
    }

    if(bNonZero)
    {
        for (int i = 0; i < 32; i++)
        {
            printf("%d ", (unsigned int)keys_return[i]);
        }
        printf("\n");
    }
#endif

    if(keys_return[3] == 1 && keys_return[4] == 32)
    {
        // Break detected. Check the time since last break.

        // If its too low, ignore.
        int nTime = glutGet(GLUT_ELAPSED_TIME);
        if(nTime < (g_nLastBreakTime + 200))
            return;

        // If its high enough, then break.
        g_nLastBreakTime = nTime;

        lua_pushstring(L, "User break.");
        lua_error(L);
        return;
    }
#endif

    return;

#if 0
    HGLOBAL      hGlobal ;
    PTSTR        pGlobal ;

    std::string sText;

    OpenClipboard (g_AppHWND) ;
    if (hGlobal = GetClipboardData (CF_TEXT))
    {
         pGlobal = (PTSTR)GlobalLock (hGlobal) ;
         sText = pGlobal;
         GlobalUnlock(hGlobal);
    }
    CloseClipboard () ;


    if(sText == "praxis:STOP")
    {
        OpenClipboard (g_AppHWND) ;
        EmptyClipboard();
        CloseClipboard () ;

        lua_pushstring(L, "User break.");
        lua_error(L);
    }
#endif
}
Пример #11
0
void clua_hook_function(lua_State *L, lua_Debug *ar) {
  lua_checkstack(L, 2);
  lua_pushstring(L, "Lua execution quantum exceeded");
  lua_error(L);
}
Пример #12
0
void abort_hook(lua_State *L, lua_Debug *) {
	lua_pushstring(L, "You loose");
	lua_error(L);
}
Пример #13
0
int lua_TextureSampler_static_create(lua_State* state)
{
    // Get the number of parameters.
    int paramCount = lua_gettop(state);

    // Attempt to match the parameters to a valid binding.
    switch (paramCount)
    {
        case 1:
        {
            if ((lua_type(state, 1) == LUA_TUSERDATA || lua_type(state, 1) == LUA_TTABLE || lua_type(state, 1) == LUA_TNIL))
            {
                // Get parameter 1 off the stack.
                Texture* param1 = ScriptUtil::getObjectPointer<Texture>(1, "Texture", false);

                void* returnPtr = (void*)Texture::Sampler::create(param1);
                if (returnPtr)
                {
                    ScriptUtil::LuaObject* object = (ScriptUtil::LuaObject*)lua_newuserdata(state, sizeof(ScriptUtil::LuaObject));
                    object->instance = returnPtr;
                    object->owns = true;
                    luaL_getmetatable(state, "TextureSampler");
                    lua_setmetatable(state, -2);
                }
                else
                {
                    lua_pushnil(state);
                }

                return 1;
            }
            else if ((lua_type(state, 1) == LUA_TSTRING || lua_type(state, 1) == LUA_TNIL))
            {
                // Get parameter 1 off the stack.
                const char* param1 = ScriptUtil::getString(1, false);

                void* returnPtr = (void*)Texture::Sampler::create(param1);
                if (returnPtr)
                {
                    ScriptUtil::LuaObject* object = (ScriptUtil::LuaObject*)lua_newuserdata(state, sizeof(ScriptUtil::LuaObject));
                    object->instance = returnPtr;
                    object->owns = true;
                    luaL_getmetatable(state, "TextureSampler");
                    lua_setmetatable(state, -2);
                }
                else
                {
                    lua_pushnil(state);
                }

                return 1;
            }
            else
            {
                lua_pushstring(state, "lua_TextureSampler_static_create - Failed to match the given parameters to a valid function signature.");
                lua_error(state);
            }
            break;
        }
        case 2:
        {
            if ((lua_type(state, 1) == LUA_TSTRING || lua_type(state, 1) == LUA_TNIL) &&
                lua_type(state, 2) == LUA_TBOOLEAN)
            {
                // Get parameter 1 off the stack.
                const char* param1 = ScriptUtil::getString(1, false);

                // Get parameter 2 off the stack.
                bool param2 = ScriptUtil::luaCheckBool(state, 2);

                void* returnPtr = (void*)Texture::Sampler::create(param1, param2);
                if (returnPtr)
                {
                    ScriptUtil::LuaObject* object = (ScriptUtil::LuaObject*)lua_newuserdata(state, sizeof(ScriptUtil::LuaObject));
                    object->instance = returnPtr;
                    object->owns = true;
                    luaL_getmetatable(state, "TextureSampler");
                    lua_setmetatable(state, -2);
                }
                else
                {
                    lua_pushnil(state);
                }

                return 1;
            }
            else
            {
                lua_pushstring(state, "lua_TextureSampler_static_create - Failed to match the given parameters to a valid function signature.");
                lua_error(state);
            }
            break;
        }
        default:
        {
            lua_pushstring(state, "Invalid number of parameters (expected 1 or 2).");
            lua_error(state);
            break;
        }
    }
    return 0;
}
Пример #14
0
int luaRedisGenericCommand(lua_State *lua, int raise_error) {
    int j, argc = lua_gettop(lua);
    struct redisCommand *cmd;
    redisClient *c = server.lua_client;
    sds reply;

    /* Cached across calls. */
    static robj **argv = NULL;
    static int argv_size = 0;
    static robj *cached_objects[LUA_CMD_OBJCACHE_SIZE];
    static int cached_objects_len[LUA_CMD_OBJCACHE_SIZE];

    /* Require at least one argument */
    if (argc == 0) {
        luaPushError(lua,
            "Please specify at least one argument for redis.call()");
        return 1;
    }

    /* Build the arguments vector */
    if (!argv) {
        argv = zmalloc(sizeof(robj*)*argc);
    } else if (argv_size < argc) {
        argv = zrealloc(argv,sizeof(robj*)*argc);
        argv_size = argc;
    }

    for (j = 0; j < argc; j++) {
        char *obj_s;
        size_t obj_len;
        char dbuf[64];

        if (lua_type(lua,j+1) == LUA_TNUMBER) {
            /* We can't use lua_tolstring() for number -> string conversion
             * since Lua uses a format specifier that loses precision. */
            lua_Number num = lua_tonumber(lua,j+1);

            obj_len = snprintf(dbuf,sizeof(dbuf),"%.17g",(double)num);
            obj_s = dbuf;
        } else {
            obj_s = (char*)lua_tolstring(lua,j+1,&obj_len);
            if (obj_s == NULL) break; /* Not a string. */
        }

        /* Try to use a cached object. */
        if (j < LUA_CMD_OBJCACHE_SIZE && cached_objects[j] &&
            cached_objects_len[j] >= obj_len)
        {
            char *s = cached_objects[j]->ptr;
            struct sdshdr *sh = (void*)(s-(sizeof(struct sdshdr)));

            argv[j] = cached_objects[j];
            cached_objects[j] = NULL;
            memcpy(s,obj_s,obj_len+1);
            sh->free += (int)(sh->len - obj_len);
            sh->len = (int)obj_len;
        } else {
            argv[j] = createStringObject(obj_s, obj_len);
        }
    }

    /* Check if one of the arguments passed by the Lua script
     * is not a string or an integer (lua_isstring() return true for
     * integers as well). */
    if (j != argc) {
        j--;
        while (j >= 0) {
            decrRefCount(argv[j]);
            j--;
        }
        luaPushError(lua,
            "Lua redis() command arguments must be strings or integers");
        return 1;
    }

    /* Setup our fake client for command execution */
    c->argv = argv;
    c->argc = argc;

    /* Command lookup */
    cmd = lookupCommand(argv[0]->ptr);
    if (!cmd || ((cmd->arity > 0 && cmd->arity != argc) ||
                   (argc < -cmd->arity)))
    {
        if (cmd)
            luaPushError(lua,
                "Wrong number of args calling Redis command From Lua script");
        else
            luaPushError(lua,"Unknown Redis command called from Lua script");
        goto cleanup;
    }

    /* There are commands that are not allowed inside scripts. */
    if (cmd->flags & REDIS_CMD_NOSCRIPT) {
        luaPushError(lua, "This Redis command is not allowed from scripts");
        goto cleanup;
    }

    /* Write commands are forbidden against read-only slaves, or if a
     * command marked as non-deterministic was already called in the context
     * of this script. */
    if (cmd->flags & REDIS_CMD_WRITE) {
        if (server.lua_random_dirty) {
            luaPushError(lua,
                "Write commands not allowed after non deterministic commands");
            goto cleanup;
        } else if (server.masterhost && server.repl_slave_ro &&
                   !server.loading &&
                   !(server.lua_caller->flags & REDIS_MASTER))
        {
            luaPushError(lua, shared.roslaveerr->ptr);
            goto cleanup;
        } else if (server.stop_writes_on_bgsave_err &&
                   server.saveparamslen > 0 &&
                   server.lastbgsave_status == REDIS_ERR)
        {
            luaPushError(lua, shared.bgsaveerr->ptr);
            goto cleanup;
        }
    }

    /* If we reached the memory limit configured via maxmemory, commands that
     * could enlarge the memory usage are not allowed, but only if this is the
     * first write in the context of this script, otherwise we can't stop
     * in the middle. */
    if (server.maxmemory && server.lua_write_dirty == 0 &&
        (cmd->flags & REDIS_CMD_DENYOOM))
    {
        if (freeMemoryIfNeeded() == REDIS_ERR) {
            luaPushError(lua, shared.oomerr->ptr);
            goto cleanup;
        }
    }

    if (cmd->flags & REDIS_CMD_RANDOM) server.lua_random_dirty = 1;
    if (cmd->flags & REDIS_CMD_WRITE) server.lua_write_dirty = 1;

    /* Run the command */
    c->cmd = cmd;
    call(c,REDIS_CALL_SLOWLOG | REDIS_CALL_STATS);

    /* Convert the result of the Redis command into a suitable Lua type.
     * The first thing we need is to create a single string from the client
     * output buffers. */
    if (listLength(c->reply) == 0 && c->bufpos < REDIS_REPLY_CHUNK_BYTES) {
        /* This is a fast path for the common case of a reply inside the
         * client static buffer. Don't create an SDS string but just use
         * the client buffer directly. */
        c->buf[c->bufpos] = '\0';
        reply = c->buf;
        c->bufpos = 0;
    } else {
        reply = sdsnewlen(c->buf,c->bufpos);
        c->bufpos = 0;
        while(listLength(c->reply)) {
            robj *o = listNodeValue(listFirst(c->reply));

            reply = sdscatlen(reply,o->ptr,sdslen(o->ptr));
            listDelNode(c->reply,listFirst(c->reply));
        }
    }
    if (raise_error && reply[0] != '-') raise_error = 0;
    redisProtocolToLuaType(lua,reply);
    /* Sort the output array if needed, assuming it is a non-null multi bulk
     * reply as expected. */
    if ((cmd->flags & REDIS_CMD_SORT_FOR_SCRIPT) &&
        (reply[0] == '*' && reply[1] != '-')) {
            luaSortArray(lua);
    }
    if (reply != c->buf) sdsfree(reply);
    c->reply_bytes = 0;

cleanup:
    /* Clean up. Command code may have changed argv/argc so we use the
     * argv/argc of the client instead of the local variables. */
    for (j = 0; j < c->argc; j++) {
        robj *o = c->argv[j];

        /* Try to cache the object in the cached_objects array.
         * The object must be small, SDS-encoded, and with refcount = 1
         * (we must be the only owner) for us to cache it. */
        if (j < LUA_CMD_OBJCACHE_SIZE &&
            o->refcount == 1 &&
            o->encoding == REDIS_ENCODING_RAW &&
            sdslen(o->ptr) <= LUA_CMD_OBJCACHE_MAX_LEN)
        {
            struct sdshdr *sh = (void*)(((char*)(o->ptr))-(sizeof(struct sdshdr)));

            if (cached_objects[j]) decrRefCount(cached_objects[j]);
            cached_objects[j] = o;
            cached_objects_len[j] = sh->free + sh->len;
        } else {
            decrRefCount(o);
        }
    }

    if (c->argv != argv) {
        zfree(c->argv);
        argv = NULL;
    }

    if (raise_error) {
        /* If we are here we should have an error in the stack, in the
         * form of a table with an "err" field. Extract the string to
         * return the plain error. */
        lua_pushstring(lua,"err");
        lua_gettable(lua,-2);
        return lua_error(lua);
    }
    return 1;
}
Пример #15
0
static int quat_index(lua_State* L)
{
    Quatf* v = check_quat(L, 1);
    luaL_checkany(L, 2);

    int key_type = lua_type(L, 2);
    if (key_type == LUA_TNUMBER)
    {
        lua_Integer i = lua_tointeger(L, 2);
        if (i > 0 && i <= 4)
        {
            lua_pushnumber(L, (*v)[i-1]);
            return 1;
        }

        // error
        lua_pushstring(L, "quat: num index out of range, must be 1, 2, 3 or 4");
        lua_error(L);
    }
    else if (key_type == LUA_TSTRING)
    {
        size_t size;
        const char* s = lua_tolstring(L, 2, &size);
        if (size == 1)
        {
            switch (s[0])
            {
            case 'i':
                lua_pushnumber(L, v->i);
                return 1;
            case 'j':
                lua_pushnumber(L, v->j);
                return 1;
            case 'k':
                lua_pushnumber(L, v->k);
                return 1;
            case 'r':
                lua_pushnumber(L, v->r);
                return 1;
            }
        }
        else
        {
            // search for string key in quat_method_funcs array.
            int i = 0;
            while (quat_method_funcs[i].name)
            {
                if (!strcmp(s, quat_method_funcs[i].name))
                {
                    lua_pushcfunction(L, quat_method_funcs[i].func);
                    return 1;
                }
                i++;
            }
        }

        // error
        lua_pushstring(L, "quat: unknown string key");
        lua_error(L);
    }
    else
    {
        // error
        lua_pushstring(L, "vec4: unsupported key");
        lua_error(L);
    }

    return 0;
}
Пример #16
0
int lua_Model_getMaterial(lua_State* state)
{
    // Get the number of parameters.
    int paramCount = lua_gettop(state);

    // Attempt to match the parameters to a valid binding.
    switch (paramCount)
    {
        case 1:
        {
            if ((lua_type(state, 1) == LUA_TUSERDATA))
            {
                Model* instance = getInstance(state);
                void* returnPtr = ((void*)instance->getMaterial());
                if (returnPtr)
                {
                    gameplay::ScriptUtil::LuaObject* object = (gameplay::ScriptUtil::LuaObject*)lua_newuserdata(state, sizeof(gameplay::ScriptUtil::LuaObject));
                    object->instance = returnPtr;
                    object->owns = false;
                    luaL_getmetatable(state, "Material");
                    lua_setmetatable(state, -2);
                }
                else
                {
                    lua_pushnil(state);
                }

                return 1;
            }

            lua_pushstring(state, "lua_Model_getMaterial - Failed to match the given parameters to a valid function signature.");
            lua_error(state);
            break;
        }
        case 2:
        {
            if ((lua_type(state, 1) == LUA_TUSERDATA) &&
                lua_type(state, 2) == LUA_TNUMBER)
            {
                // Get parameter 1 off the stack.
                int param1 = (int)luaL_checkint(state, 2);

                Model* instance = getInstance(state);
                void* returnPtr = ((void*)instance->getMaterial(param1));
                if (returnPtr)
                {
                    gameplay::ScriptUtil::LuaObject* object = (gameplay::ScriptUtil::LuaObject*)lua_newuserdata(state, sizeof(gameplay::ScriptUtil::LuaObject));
                    object->instance = returnPtr;
                    object->owns = false;
                    luaL_getmetatable(state, "Material");
                    lua_setmetatable(state, -2);
                }
                else
                {
                    lua_pushnil(state);
                }

                return 1;
            }

            lua_pushstring(state, "lua_Model_getMaterial - Failed to match the given parameters to a valid function signature.");
            lua_error(state);
            break;
        }
        default:
        {
            lua_pushstring(state, "Invalid number of parameters (expected 1 or 2).");
            lua_error(state);
            break;
        }
    }
    return 0;
}
Пример #17
0
Файл: nlua.c Проект: pegue/naev
/**
 * @brief Loads specially modified basic stuff.
 *
 *    @param L Lua State to load the basic stuff into.
 *    @return 0 on success.
 */
int nlua_loadBasic( lua_State* L )
{
   int i;
   const char *override[] = { /* unsafe functions */
         "collectgarbage",
         "dofile",
         "getfenv",
         "getmetatable",
         "load",
         "loadfile",
         "loadstring",
         "rawequal",
         "rawget",
         "rawset",
         "setfenv",
         "setmetatable",
         "END"
   };


   nlua_load(L,luaopen_base); /* open base. */

   /* replace non-safe functions */
   for (i=0; strcmp(override[i],"END")!=0; i++) {
      lua_pushnil(L);
      lua_setglobal(L, override[i]);
   }

   nlua_load(L,luaopen_math); /* open math. */
   nlua_load(L,luaopen_table); /* open table. */
   nlua_load(L, luaopen_string); /* open string. */

   /* add our own */
   lua_register(L, "include", nlua_packfileLoader);

   return 0;
}


/**
 * @brief include( string module )
 *
 * Loads a module into the current Lua state from inside the data file.
 *
 *    @param module Name of the module to load.
 *    @return An error string on error.
 */
static int nlua_packfileLoader( lua_State* L )
{
   const char *filename;
   char *buf;
   uint32_t bufsize;

   filename = luaL_checkstring(L,1);

   /* try to locate the data */
   buf = ndata_read( filename, &bufsize );
   if (buf == NULL) {
      lua_pushfstring(L, "%s not found in ndata.", filename);
      return 1;
   }
   
   /* run the buffer */
   if (luaL_dobuffer(L, buf, bufsize, filename) != 0) {
      /* will push the current error from the dobuffer */
      lua_error(L);
      return 1;
   }

   /* cleanup, success */
   free(buf);
   return 0;
}


/**
 * @brief Loads the standard NAEV Lua API.
 *
 * Loads the modules:
 *  - naev
 *  - space
 *    - planet
 *    - system
 *  - var
 *  - pilot
 *  - time
 *  - player
 *  - diff
 *  - faction
 *  - vec2
 *
 * Only is missing:
 *  - misn
 *  - tk
 *  - hook
 *  - music
 *
 *    @param L Lua State to load modules into.
 *    @param readonly Load as readonly (good for sandboxing).
 *    @return 0 on success.
 */
int nlua_loadStandard( lua_State *L, int readonly )
{
   int r;

   r = 0;
   r |= lua_loadNaev(L);
   r |= lua_loadVar(L,readonly);
   r |= lua_loadSpace(L,readonly); /* planet, system */
   r |= lua_loadTime(L,readonly);
   r |= lua_loadPlayer(L,readonly);
   r |= lua_loadPilot(L,readonly);
   r |= lua_loadRnd(L);
   r |= lua_loadDiff(L,readonly);
   r |= lua_loadFaction(L,readonly);
   r |= lua_loadVector(L);

   return r;
}
Пример #18
0
int lua_Model_setMaterial(lua_State* state)
{
    // Get the number of parameters.
    int paramCount = lua_gettop(state);

    // Attempt to match the parameters to a valid binding.
    switch (paramCount)
    {
        case 2:
        {
            do
            {
                if ((lua_type(state, 1) == LUA_TUSERDATA) &&
                    (lua_type(state, 2) == LUA_TUSERDATA || lua_type(state, 2) == LUA_TTABLE || lua_type(state, 2) == LUA_TNIL))
                {
                    // Get parameter 1 off the stack.
                    bool param1Valid;
                    gameplay::ScriptUtil::LuaArray<Material> param1 = gameplay::ScriptUtil::getObjectPointer<Material>(2, "Material", false, &param1Valid);
                    if (!param1Valid)
                        break;

                    Model* instance = getInstance(state);
                    instance->setMaterial(param1);
                    
                    return 0;
                }
            } while (0);

            do
            {
                if ((lua_type(state, 1) == LUA_TUSERDATA) &&
                    (lua_type(state, 2) == LUA_TSTRING || lua_type(state, 2) == LUA_TNIL))
                {
                    // Get parameter 1 off the stack.
                    const char* param1 = gameplay::ScriptUtil::getString(2, false);

                    Model* instance = getInstance(state);
                    void* returnPtr = ((void*)instance->setMaterial(param1));
                    if (returnPtr)
                    {
                        gameplay::ScriptUtil::LuaObject* object = (gameplay::ScriptUtil::LuaObject*)lua_newuserdata(state, sizeof(gameplay::ScriptUtil::LuaObject));
                        object->instance = returnPtr;
                        object->owns = false;
                        luaL_getmetatable(state, "Material");
                        lua_setmetatable(state, -2);
                    }
                    else
                    {
                        lua_pushnil(state);
                    }

                    return 1;
                }
            } while (0);

            lua_pushstring(state, "lua_Model_setMaterial - Failed to match the given parameters to a valid function signature.");
            lua_error(state);
            break;
        }
        case 3:
        {
            do
            {
                if ((lua_type(state, 1) == LUA_TUSERDATA) &&
                    (lua_type(state, 2) == LUA_TUSERDATA || lua_type(state, 2) == LUA_TTABLE || lua_type(state, 2) == LUA_TNIL) &&
                    lua_type(state, 3) == LUA_TNUMBER)
                {
                    // Get parameter 1 off the stack.
                    bool param1Valid;
                    gameplay::ScriptUtil::LuaArray<Material> param1 = gameplay::ScriptUtil::getObjectPointer<Material>(2, "Material", false, &param1Valid);
                    if (!param1Valid)
                        break;

                    // Get parameter 2 off the stack.
                    int param2 = (int)luaL_checkint(state, 3);

                    Model* instance = getInstance(state);
                    instance->setMaterial(param1, param2);
                    
                    return 0;
                }
            } while (0);

            do
            {
                if ((lua_type(state, 1) == LUA_TUSERDATA) &&
                    (lua_type(state, 2) == LUA_TSTRING || lua_type(state, 2) == LUA_TNIL) &&
                    (lua_type(state, 3) == LUA_TSTRING || lua_type(state, 3) == LUA_TNIL))
                {
                    // Get parameter 1 off the stack.
                    const char* param1 = gameplay::ScriptUtil::getString(2, false);

                    // Get parameter 2 off the stack.
                    const char* param2 = gameplay::ScriptUtil::getString(3, false);

                    Model* instance = getInstance(state);
                    void* returnPtr = ((void*)instance->setMaterial(param1, param2));
                    if (returnPtr)
                    {
                        gameplay::ScriptUtil::LuaObject* object = (gameplay::ScriptUtil::LuaObject*)lua_newuserdata(state, sizeof(gameplay::ScriptUtil::LuaObject));
                        object->instance = returnPtr;
                        object->owns = false;
                        luaL_getmetatable(state, "Material");
                        lua_setmetatable(state, -2);
                    }
                    else
                    {
                        lua_pushnil(state);
                    }

                    return 1;
                }
            } while (0);

            do
            {
                if ((lua_type(state, 1) == LUA_TUSERDATA) &&
                    (lua_type(state, 2) == LUA_TSTRING || lua_type(state, 2) == LUA_TNIL) &&
                    lua_type(state, 3) == LUA_TNUMBER)
                {
                    // Get parameter 1 off the stack.
                    const char* param1 = gameplay::ScriptUtil::getString(2, false);

                    // Get parameter 2 off the stack.
                    int param2 = (int)luaL_checkint(state, 3);

                    Model* instance = getInstance(state);
                    void* returnPtr = ((void*)instance->setMaterial(param1, param2));
                    if (returnPtr)
                    {
                        gameplay::ScriptUtil::LuaObject* object = (gameplay::ScriptUtil::LuaObject*)lua_newuserdata(state, sizeof(gameplay::ScriptUtil::LuaObject));
                        object->instance = returnPtr;
                        object->owns = false;
                        luaL_getmetatable(state, "Material");
                        lua_setmetatable(state, -2);
                    }
                    else
                    {
                        lua_pushnil(state);
                    }

                    return 1;
                }
            } while (0);

            lua_pushstring(state, "lua_Model_setMaterial - Failed to match the given parameters to a valid function signature.");
            lua_error(state);
            break;
        }
        case 4:
        {
            do
            {
                if ((lua_type(state, 1) == LUA_TUSERDATA) &&
                    (lua_type(state, 2) == LUA_TSTRING || lua_type(state, 2) == LUA_TNIL) &&
                    (lua_type(state, 3) == LUA_TSTRING || lua_type(state, 3) == LUA_TNIL) &&
                    (lua_type(state, 4) == LUA_TSTRING || lua_type(state, 4) == LUA_TNIL))
                {
                    // Get parameter 1 off the stack.
                    const char* param1 = gameplay::ScriptUtil::getString(2, false);

                    // Get parameter 2 off the stack.
                    const char* param2 = gameplay::ScriptUtil::getString(3, false);

                    // Get parameter 3 off the stack.
                    const char* param3 = gameplay::ScriptUtil::getString(4, false);

                    Model* instance = getInstance(state);
                    void* returnPtr = ((void*)instance->setMaterial(param1, param2, param3));
                    if (returnPtr)
                    {
                        gameplay::ScriptUtil::LuaObject* object = (gameplay::ScriptUtil::LuaObject*)lua_newuserdata(state, sizeof(gameplay::ScriptUtil::LuaObject));
                        object->instance = returnPtr;
                        object->owns = false;
                        luaL_getmetatable(state, "Material");
                        lua_setmetatable(state, -2);
                    }
                    else
                    {
                        lua_pushnil(state);
                    }

                    return 1;
                }
            } while (0);

            lua_pushstring(state, "lua_Model_setMaterial - Failed to match the given parameters to a valid function signature.");
            lua_error(state);
            break;
        }
        case 5:
        {
            do
            {
                if ((lua_type(state, 1) == LUA_TUSERDATA) &&
                    (lua_type(state, 2) == LUA_TSTRING || lua_type(state, 2) == LUA_TNIL) &&
                    (lua_type(state, 3) == LUA_TSTRING || lua_type(state, 3) == LUA_TNIL) &&
                    (lua_type(state, 4) == LUA_TSTRING || lua_type(state, 4) == LUA_TNIL) &&
                    lua_type(state, 5) == LUA_TNUMBER)
                {
                    // Get parameter 1 off the stack.
                    const char* param1 = gameplay::ScriptUtil::getString(2, false);

                    // Get parameter 2 off the stack.
                    const char* param2 = gameplay::ScriptUtil::getString(3, false);

                    // Get parameter 3 off the stack.
                    const char* param3 = gameplay::ScriptUtil::getString(4, false);

                    // Get parameter 4 off the stack.
                    int param4 = (int)luaL_checkint(state, 5);

                    Model* instance = getInstance(state);
                    void* returnPtr = ((void*)instance->setMaterial(param1, param2, param3, param4));
                    if (returnPtr)
                    {
                        gameplay::ScriptUtil::LuaObject* object = (gameplay::ScriptUtil::LuaObject*)lua_newuserdata(state, sizeof(gameplay::ScriptUtil::LuaObject));
                        object->instance = returnPtr;
                        object->owns = false;
                        luaL_getmetatable(state, "Material");
                        lua_setmetatable(state, -2);
                    }
                    else
                    {
                        lua_pushnil(state);
                    }

                    return 1;
                }
            } while (0);

            lua_pushstring(state, "lua_Model_setMaterial - Failed to match the given parameters to a valid function signature.");
            lua_error(state);
            break;
        }
        default:
        {
            lua_pushstring(state, "Invalid number of parameters (expected 2, 3, 4 or 5).");
            lua_error(state);
            break;
        }
    }
    return 0;
}
static int CBaseCombatWeapon___newindex (lua_State *L) {
    CBaseCombatWeapon *pWeapon = lua_toweapon(L, 1);
    if (pWeapon == NULL) {  /* avoid extra test when d is not 0 */
        lua_Debug ar1;
        lua_getstack(L, 1, &ar1);
        lua_getinfo(L, "fl", &ar1);
        lua_Debug ar2;
        lua_getinfo(L, ">S", &ar2);
        lua_pushfstring(L, "%s:%d: attempt to index a NULL entity", ar2.short_src, ar1.currentline);
        return lua_error(L);
    }
    const char *field = luaL_checkstring(L, 2);
    if (Q_strcmp(field, "m_bAltFiresUnderwater") == 0)
        pWeapon->m_bAltFiresUnderwater = luaL_checkboolean(L, 3);
    else if (Q_strcmp(field, "m_bFireOnEmpty") == 0)
        pWeapon->m_bFireOnEmpty = luaL_checkboolean(L, 3);
    else if (Q_strcmp(field, "m_bFiresUnderwater") == 0)
        pWeapon->m_bFiresUnderwater = luaL_checkboolean(L, 3);
    else if (Q_strcmp(field, "m_bInReload") == 0)
        pWeapon->m_bInReload = luaL_checkboolean(L, 3);
    else if (Q_strcmp(field, "m_bReloadsSingly") == 0)
        pWeapon->m_bReloadsSingly = luaL_checkboolean(L, 3);
    else if (Q_strcmp(field, "m_fFireDuration") == 0)
        pWeapon->m_fFireDuration = luaL_checknumber(L, 3);
    else if (Q_strcmp(field, "m_flNextEmptySoundTime") == 0)
        pWeapon->m_flNextEmptySoundTime = luaL_checknumber(L, 3);
    else if (Q_strcmp(field, "m_flNextPrimaryAttack") == 0)
        pWeapon->m_flNextPrimaryAttack.GetForModify() = luaL_checknumber(L, 3);
    else if (Q_strcmp(field, "m_flNextSecondaryAttack") == 0)
        pWeapon->m_flNextSecondaryAttack.GetForModify() = luaL_checknumber(L, 3);
    else if (Q_strcmp(field, "m_flTimeWeaponIdle") == 0)
        pWeapon->m_flTimeWeaponIdle.GetForModify() = luaL_checknumber(L, 3);
    else if (Q_strcmp(field, "m_flUnlockTime") == 0)
        pWeapon->m_flUnlockTime = luaL_checknumber(L, 3);
    else if (Q_strcmp(field, "m_fMaxRange1") == 0)
        pWeapon->m_fMaxRange1 = luaL_checknumber(L, 3);
    else if (Q_strcmp(field, "m_fMaxRange2") == 0)
        pWeapon->m_fMaxRange2 = luaL_checknumber(L, 3);
    else if (Q_strcmp(field, "m_fMinRange1") == 0)
        pWeapon->m_fMinRange1 = luaL_checknumber(L, 3);
    else if (Q_strcmp(field, "m_fMinRange2") == 0)
        pWeapon->m_fMinRange2 = luaL_checknumber(L, 3);
    else if (Q_strcmp(field, "m_iClip1") == 0)
        pWeapon->m_iClip1.GetForModify() = luaL_checkint(L, 3);
    else if (Q_strcmp(field, "m_iClip2") == 0)
        pWeapon->m_iClip2.GetForModify() = luaL_checkint(L, 3);
    else if (Q_strcmp(field, "m_iPrimaryAmmoType") == 0)
        pWeapon->m_iPrimaryAmmoType.GetForModify() = luaL_checkint(L, 3);
    else if (Q_strcmp(field, "m_iSecondaryAmmoType") == 0)
        pWeapon->m_iSecondaryAmmoType.GetForModify() = luaL_checkint(L, 3);
    else if (Q_strcmp(field, "m_iState") == 0)
        pWeapon->m_iState.GetForModify() = luaL_checkint(L, 3);
    else if (Q_strcmp(field, "m_iSubType") == 0)
        pWeapon->m_iSubType = luaL_checkint(L, 3);
    else if (Q_strcmp(field, "m_iViewModelIndex") == 0)
        pWeapon->m_iViewModelIndex.GetForModify() = luaL_checkint(L, 3);
    else if (Q_strcmp(field, "m_iWorldModelIndex") == 0)
        pWeapon->m_iWorldModelIndex.GetForModify() = luaL_checkint(L, 3);
    else if (Q_strcmp(field, "m_nViewModelIndex") == 0)
        pWeapon->m_nViewModelIndex.GetForModify() = luaL_checkint(L, 3);
    else {
        if (pWeapon->m_nTableReference == LUA_NOREF) {
            lua_newtable(L);
            pWeapon->m_nTableReference = luaL_ref(L, LUA_REGISTRYINDEX);
        }
        lua_getref(L, pWeapon->m_nTableReference);
        lua_pushvalue(L, 3);
        lua_setfield(L, -2, field);
        lua_pop(L, 1);
    }
    return 0;
}
int lua_PhysicsCollisionShapeDefinition__init(lua_State* state)
{
    // Get the number of parameters.
    int paramCount = lua_gettop(state);

    // Attempt to match the parameters to a valid binding.
    switch (paramCount)
    {
        case 0:
        {
            void* returnPtr = (void*)new PhysicsCollisionShape::Definition();
            if (returnPtr)
            {
                gameplay::ScriptUtil::LuaObject* object = (gameplay::ScriptUtil::LuaObject*)lua_newuserdata(state, sizeof(gameplay::ScriptUtil::LuaObject));
                object->instance = returnPtr;
                object->owns = true;
                luaL_getmetatable(state, "PhysicsCollisionShapeDefinition");
                lua_setmetatable(state, -2);
            }
            else
            {
                lua_pushnil(state);
            }

            return 1;
            break;
        }
        case 1:
        {
            do
            {
                if ((lua_type(state, 1) == LUA_TUSERDATA || lua_type(state, 1) == LUA_TNIL))
                {
                    // Get parameter 1 off the stack.
                    bool param1Valid;
                    gameplay::ScriptUtil::LuaArray<PhysicsCollisionShape::Definition> param1 = gameplay::ScriptUtil::getObjectPointer<PhysicsCollisionShape::Definition>(1, "PhysicsCollisionShapeDefinition", true, &param1Valid);
                    if (!param1Valid)
                        break;

                    void* returnPtr = (void*)new PhysicsCollisionShape::Definition(*param1);
                    if (returnPtr)
                    {
                        gameplay::ScriptUtil::LuaObject* object = (gameplay::ScriptUtil::LuaObject*)lua_newuserdata(state, sizeof(gameplay::ScriptUtil::LuaObject));
                        object->instance = returnPtr;
                        object->owns = true;
                        luaL_getmetatable(state, "PhysicsCollisionShapeDefinition");
                        lua_setmetatable(state, -2);
                    }
                    else
                    {
                        lua_pushnil(state);
                    }

                    return 1;
                }
            } while (0);

            lua_pushstring(state, "lua_PhysicsCollisionShapeDefinition__init - Failed to match the given parameters to a valid function signature.");
            lua_error(state);
            break;
        }
        default:
        {
            lua_pushstring(state, "Invalid number of parameters (expected 0 or 1).");
            lua_error(state);
            break;
        }
    }
    return 0;
}
Пример #21
0
int32 luaD_call(StkId base, int32 nResults) {
	lua_Task *tmpTask = lua_state->task;
	if (!lua_state->task || lua_state->state_counter2) {
		lua_Task *t = luaM_new(lua_Task);
		lua_taskinit(t, lua_state->task, base, nResults);
		lua_state->task = t;
	} else {
		tmpTask = lua_state->some_task;
	}

	while (1) {
		lua_CFunction function = NULL;
		StkId firstResult = 0;
		TObject *funcObj = lua_state->stack.stack + base - 1;
		if (ttype(funcObj) == LUA_T_CLOSURE) {
			Closure *c = clvalue(funcObj);
			TObject *proto = &(c->consts[0]);
			ttype(funcObj) = LUA_T_CLMARK;
			if (ttype(proto) == LUA_T_CPROTO) {
				function = fvalue(funcObj);
				firstResult = callCclosure(c, fvalue(proto), base);
			} else {
				lua_taskresume(lua_state->task, c, tfvalue(proto), base);
				firstResult = luaV_execute(lua_state->task);
			}
		} else if (ttype(funcObj) == LUA_T_PMARK) {
			if (!lua_state->task->some_flag) {
				TObject *im = luaT_getimbyObj(funcObj, IM_FUNCTION);
				if (ttype(im) == LUA_T_NIL)
					lua_error("call expression not a function");
				luaD_callTM(im, (lua_state->stack.top - lua_state->stack.stack) - (base - 1), nResults);
				continue;
			}
			firstResult = luaV_execute(lua_state->task);
		} else if (ttype(funcObj) == LUA_T_CMARK) {
			if (!lua_state->task->some_flag) {
				TObject *im = luaT_getimbyObj(funcObj, IM_FUNCTION);
				if (ttype(im) == LUA_T_NIL)
					lua_error("call expression not a function");
				luaD_callTM(im, (lua_state->stack.top - lua_state->stack.stack) - (base - 1), nResults);
				continue;
			}
		} else if (ttype(funcObj) == LUA_T_CLMARK) {
			Closure *c = clvalue(funcObj);
			TObject *proto = &(c->consts[0]);
			if (!lua_state->task->some_flag) {
				TObject *im = luaT_getimbyObj(funcObj, IM_FUNCTION);
				if (ttype(im) == LUA_T_NIL)
					lua_error("call expression not a function");
				luaD_callTM(im, (lua_state->stack.top - lua_state->stack.stack) - (base - 1), nResults);
				continue;
			}
			if (ttype(proto) != LUA_T_CPROTO)
				firstResult = luaV_execute(lua_state->task);
		} else if (ttype(funcObj) == LUA_T_PROTO) {
			ttype(funcObj) = LUA_T_PMARK;
			lua_taskresume(lua_state->task, NULL, tfvalue(funcObj), base);
			firstResult = luaV_execute(lua_state->task);
		} else if (ttype(funcObj) == LUA_T_CPROTO) {
			ttype(funcObj) = LUA_T_CMARK;
			function = fvalue(funcObj);
			firstResult = callC(fvalue(funcObj), base);
		} else {
			TObject *im = luaT_getimbyObj(funcObj, IM_FUNCTION);
			if (ttype(im) == LUA_T_NIL) {
				// NOTE: Originally this throwed the lua_error. Anyway it is commented here because
				// when in year 4 bi.exit() calls bi.book.act:free(). But bi.book.act is nil,
				// hence it enters this branch and the error blocks the game.
				// Now we try instead to survive and go on with the function.
				lua_Task *t = lua_state->task;
				lua_state->task = t->next;
				lua_state->some_task = tmpTask;
				luaM_free(t);

				warning("Lua: call expression not a function");
				return 1;
// 				lua_error("call expression not a function");
			}
			luaD_callTM(im, (lua_state->stack.top - lua_state->stack.stack) - (base - 1), nResults);
			continue;
		}

		if (firstResult <= 0) {
			nResults = lua_state->task->aux;
			base = -firstResult;
			lua_Task *t = luaM_new(lua_Task);
			lua_taskinit(t, lua_state->task, base, nResults);
			lua_state->task = t;
		} else {
			nResults = lua_state->task->some_results;
			base = lua_state->task->some_base;
			if (nResults != 255)
				luaD_adjusttop(firstResult + nResults);
			base--;
			nResults = lua_state->stack.top - (lua_state->stack.stack + firstResult);
			for (int32 i = 0; i < nResults; i++)
				*(lua_state->stack.stack + base + i) = *(lua_state->stack.stack + firstResult + i);
			lua_state->stack.top -= firstResult - base;

			lua_Task *tmp = lua_state->task;
			lua_state->task = lua_state->task->next;
			luaM_free(tmp);
			if (lua_state->task) {
				nResults = lua_state->task->some_results;
				base = lua_state->task->some_base;
			}

			if (function == break_here) {
				if (!lua_state->state_counter1)  {
					lua_state->some_task = tmpTask;
					return 1;
				}
			}
		}

		if (lua_state->task == tmpTask)
			break;
	}

	return 0;
}
Пример #22
0
static gint glspi_pickfile(lua_State* L)
{
	gboolean save = FALSE;
	gchar *path = NULL;
	const gchar *mask = NULL;
	gchar *name = NULL;
	gchar*result;
	gint argc=lua_gettop(L);

	if (argc >= 1) {
		if  (lua_isstring(L,1))	{
			const gchar*tmp=lua_tostring(L,1);
			if (strcasecmp(tmp,"save")==0) {
				save=TRUE;
			} else
			if ( (*tmp != '\0') && (strcasecmp(tmp,"open")!=0) ) {
				lua_pushfstring(L, _("Error in module \"%s\" at function %s():\n"
							"expected string \"open\" or \"save\" for argument #1.\n"),
							LUA_MODULE_NAME, &__FUNCTION__[6]);
				lua_error(L);
				return 0;
			}
		} else if (!lua_isnil(L,1)) {
			FAIL_STRING_ARG(1);
			return 0;
		}
	}
	if (argc >= 2) {
		if (lua_isstring(L,2)) {
			path=g_strdup(lua_tostring(L,2));
		} else if (!lua_isnil(L,2)) {
			FAIL_STRING_ARG(2);
			return 0;
		}
	}
	if (argc >= 3 ) {
		if (lua_isstring(L,3)) {
			mask=lua_tostring(L,3);
		} else if (!lua_isnil(L,3)) {
			FAIL_STRING_ARG(3);
			return 0;
		}
	}

	if (path && *path && !g_file_test(path,G_FILE_TEST_IS_DIR) ){
		gchar*sep=strrchr(path, G_DIR_SEPARATOR);
		if (sep) {
			name=sep+1;
			*sep='\0';
		} else {
			name=path;
			path=NULL;
		}
	}

	result=file_dlg(L,save,path,mask,name);
	if (path) { g_free(path); } else if ( name ) { g_free(name); };
	if (result) {
		lua_pushstring(L,result);
		g_free(result);
	} else {
		lua_pushnil(L);
	}
	return 1;
}
Пример #23
0
static void checkCparams(int32 nParams) {
	if (lua_state->stack.top - lua_state->stack.stack < lua_state->Cstack.base + nParams)
		lua_error("API error - wrong number of arguments in C2lua stack");
}
Пример #24
0
int am_require(lua_State *L) {
    am_check_nargs(L, 1);
    char tmpbuf1[TMP_BUF_SZ];
    char tmpbuf2[TMP_BUF_SZ];
    size_t len;
    const char *modname = lua_tolstring(L, 1, &len);

    // check for string argument
    if (modname == NULL) {
        return luaL_error(L, "require expects a string as its single argument");
    }

    // check module name not too long
    if (len > TMP_BUF_SZ - 10) {
        return luaL_error(L, "module name '%s' too long", modname);
    }

    // check if module has already been loaded
    lua_rawgeti(L, LUA_REGISTRYINDEX, AM_MODULE_TABLE);
    lua_pushvalue(L, 1); // module name
    lua_rawget(L, -2);
    if (!lua_isnil(L, -1)) {
        // already loaded, or busy loading
        lua_remove(L, -2); // module table
        return 1;
    }
    lua_pop(L, 1); // nil

    // create exports table and add to module table:
    lua_newtable(L);
    lua_pushvalue(L, 1); // module name
    lua_pushvalue(L, -2); // export table
    lua_rawset(L, -4); // set it
    // export table now on top

    // read in the module
    int sz;
    strncpy(tmpbuf1, modname, TMP_BUF_SZ);
    am_replchr(tmpbuf1, '.', AM_PATH_SEP);
    snprintf(tmpbuf2, TMP_BUF_SZ, "%s.lua", tmpbuf1);
    char *errmsg;
    void *buf = am_read_resource(tmpbuf2, &sz, &errmsg);
    if (buf == NULL) {
        lua_pushfstring(L, "unable to load module '%s': %s", modname, errmsg);
        free(errmsg);
        return lua_error(L);
    }

    // replace "#!" at start with "--"
    char *cbuf = (char*)buf;
    if (sz >= 2 && cbuf[0] == '#' && cbuf[1] == '!') {
        cbuf[0] = '-';
        cbuf[1] = '-';
    }

    // parse and load the module
    snprintf(tmpbuf2, TMP_BUF_SZ, "@%s.lua", tmpbuf1);
    int res = luaL_loadbuffer(L, (const char*)buf, sz, tmpbuf2);
    free(buf);
    if (res != 0) return lua_error(L);

    // pass export table as arg
    lua_pushvalue(L, -2); // export table
    lua_call(L, 1, 1);

    if (!lua_isnil(L, -1)) {
        // replace export table with returned value in module table
        lua_pushvalue(L, 1); // module name
        lua_pushvalue(L, -2); // returned value
        lua_rawset(L, -5);
        lua_remove(L, -2); // export table
        lua_remove(L, -2); // module table
        // return value now on top
    } else {
        lua_pop(L, 1); // nil
        lua_remove(L, -2); // module table
        // export table now on top
    }
    return 1;
}
Пример #25
0
int lua_Light_setColor(lua_State* state)
{
    // Get the number of parameters.
    int paramCount = lua_gettop(state);

    // Attempt to match the parameters to a valid binding.
    switch (paramCount)
    {
        case 2:
        {
            do
            {
                if ((lua_type(state, 1) == LUA_TUSERDATA) &&
                    (lua_type(state, 2) == LUA_TUSERDATA || lua_type(state, 2) == LUA_TNIL))
                {
                    // Get parameter 1 off the stack.
                    bool param1Valid;
                    gameplay::ScriptUtil::LuaArray<Vector3> param1 = gameplay::ScriptUtil::getObjectPointer<Vector3>(2, "Vector3", true, &param1Valid);
                    if (!param1Valid)
                        break;

                    Light* instance = getInstance(state);
                    instance->setColor(*param1);
                    
                    return 0;
                }
            } while (0);

            lua_pushstring(state, "lua_Light_setColor - Failed to match the given parameters to a valid function signature.");
            lua_error(state);
            break;
        }
        case 4:
        {
            do
            {
                if ((lua_type(state, 1) == LUA_TUSERDATA) &&
                    lua_type(state, 2) == LUA_TNUMBER &&
                    lua_type(state, 3) == LUA_TNUMBER &&
                    lua_type(state, 4) == LUA_TNUMBER)
                {
                    // Get parameter 1 off the stack.
                    float param1 = (float)luaL_checknumber(state, 2);

                    // Get parameter 2 off the stack.
                    float param2 = (float)luaL_checknumber(state, 3);

                    // Get parameter 3 off the stack.
                    float param3 = (float)luaL_checknumber(state, 4);

                    Light* instance = getInstance(state);
                    instance->setColor(param1, param2, param3);
                    
                    return 0;
                }
            } while (0);

            lua_pushstring(state, "lua_Light_setColor - Failed to match the given parameters to a valid function signature.");
            lua_error(state);
            break;
        }
        default:
        {
            lua_pushstring(state, "Invalid number of parameters (expected 2 or 4).");
            lua_error(state);
            break;
        }
    }
    return 0;
}
Пример #26
0
static void lstop (void) {
  lua_setlinehook(L, old_linehook);
  lua_setcallhook(L, old_callhook);
  lreset();
  lua_error(L, "interrupted!");
}
Пример #27
0
/*-----------------------------for debuger-------------------------------*/
LUA_EXT_API const char* d_get_last_error(){
    //todo::get lua err;
    lua_error(L);
    return NULL;
}
Пример #28
0
void typeFB () {
	lua_error("unexpected type");
}
Пример #29
0
int lua_AudioSource_static_create(lua_State* state)
{
    // Get the number of parameters.
    int paramCount = lua_gettop(state);

    // Attempt to match the parameters to a valid binding.
    switch (paramCount)
    {
        case 1:
        {
            if ((lua_type(state, 1) == LUA_TSTRING || lua_type(state, 1) == LUA_TNIL))
            {
                // Get parameter 1 off the stack.
                ScriptUtil::LuaArray<const char> param1 = ScriptUtil::getString(1, false);

                void* returnPtr = (void*)AudioSource::create(param1);
                if (returnPtr)
                {
                    ScriptUtil::LuaObject* object = (ScriptUtil::LuaObject*)lua_newuserdata(state, sizeof(ScriptUtil::LuaObject));
                    object->instance = returnPtr;
                    object->owns = true;
                    luaL_getmetatable(state, "AudioSource");
                    lua_setmetatable(state, -2);
                }
                else
                {
                    lua_pushnil(state);
                }

                return 1;
            }
            else if ((lua_type(state, 1) == LUA_TUSERDATA || lua_type(state, 1) == LUA_TTABLE || lua_type(state, 1) == LUA_TNIL))
            {
                // Get parameter 1 off the stack.
                ScriptUtil::LuaArray<Properties> param1 = ScriptUtil::getObjectPointer<Properties>(1, "Properties", false);

                void* returnPtr = (void*)AudioSource::create(param1);
                if (returnPtr)
                {
                    ScriptUtil::LuaObject* object = (ScriptUtil::LuaObject*)lua_newuserdata(state, sizeof(ScriptUtil::LuaObject));
                    object->instance = returnPtr;
                    object->owns = true;
                    luaL_getmetatable(state, "AudioSource");
                    lua_setmetatable(state, -2);
                }
                else
                {
                    lua_pushnil(state);
                }

                return 1;
            }
            else
            {
                lua_pushstring(state, "lua_AudioSource_static_create - Failed to match the given parameters to a valid function signature.");
                lua_error(state);
            }
            break;
        }
        default:
        {
            lua_pushstring(state, "Invalid number of parameters (expected 1).");
            lua_error(state);
            break;
        }
    }
    return 0;
}
Пример #30
0
int read_message(lua_State* lua)
{
    void* luserdata = lua_touserdata(lua, lua_upvalueindex(1));
    if (NULL == luserdata) {
        lua_pushstring(lua, "read_message() invalid lightuserdata");
        lua_error(lua);
    }
    lua_sandbox* lsb = (lua_sandbox*)luserdata;

    const char* field;
    int fi = 0, ai = 0;
    switch (lua_gettop(lua)) {
    case 3:
        ai = luaL_checkint(lua, 3);
        if (ai < 0) {
            lua_pushstring(lua, "read_message() array index must be >= 0");
            lua_error(lua);
        }
        // fall-thru
    case 2:
        fi = luaL_checkint(lua, 2);
        if (fi < 0) {
            lua_pushstring(lua, "read_message() field index must be >= 0");
            lua_error(lua);
        }
        // fall-thru
    case 1:
        field = luaL_checkstring(lua, 1);
        break;
    default:
        lua_pushstring(lua, "read_message() incorrect number of arguments");
        lua_error(lua);
        break;
    }

    struct go_lua_read_message_return gr;
    // Cast away constness of the Lua string, the value is not modified
    // and it will save a copy.
    gr = go_lua_read_message(lsb->m_go, (char*)field, fi, ai);
    if (gr.r1 == NULL) {
        lua_pushnil(lua);
    } else {
        switch (gr.r0) {
        case 0:
            lua_pushlstring(lua, gr.r1, gr.r2);
            free(gr.r1);
            break;
        case 1:
            lua_pushlstring(lua, gr.r1, gr.r2);
            break;
        case 2:
            if (strncmp("Pid", field, 3) == 0
                || strncmp("Severity", field, 8) == 0) {
                lua_pushinteger(lua, *((GoInt32*)gr.r1));
            } else {
                lua_pushnumber(lua, *((GoInt*)gr.r1));
            }
            break;
        case 3:
            lua_pushnumber(lua, *((GoFloat64*)gr.r1));
            break;
        case 4:
            lua_pushboolean(lua, *((GoInt8*)gr.r1));
            break;
        }
    }
    return 1;
}