2015-09-25 21:07:16 +08:00
|
|
|
/*
|
|
|
|
|
* scriptlib.cpp
|
|
|
|
|
*
|
|
|
|
|
* Created on: 2010-7-29
|
|
|
|
|
* Author: Argon
|
|
|
|
|
*/
|
|
|
|
|
#include "scriptlib.h"
|
|
|
|
|
#include "duel.h"
|
2020-07-20 20:50:15 +02:00
|
|
|
#include "lua_obj.h"
|
2015-09-25 21:07:16 +08:00
|
|
|
|
2020-07-21 15:03:03 +02:00
|
|
|
int32 scriptlib::check_param(lua_State* L, int32 param_type, int32 index, int32 retfalse, void* retobj) {
|
2020-07-20 20:58:34 +02:00
|
|
|
const char* type = nullptr;
|
2020-07-21 15:03:03 +02:00
|
|
|
lua_obj* obj = nullptr;
|
2015-09-25 21:07:16 +08:00
|
|
|
switch (param_type) {
|
2020-07-20 20:50:15 +02:00
|
|
|
case PARAM_TYPE_CARD:
|
|
|
|
|
case PARAM_TYPE_GROUP:
|
|
|
|
|
case PARAM_TYPE_EFFECT:
|
2020-08-23 17:56:55 +02:00
|
|
|
if((obj = lua_get<lua_obj*>(L, index)) != nullptr && obj->lua_type == param_type) {
|
2020-07-21 15:03:03 +02:00
|
|
|
if(retobj)
|
|
|
|
|
*(lua_obj**)retobj = obj;
|
2018-08-12 11:37:00 +08:00
|
|
|
return TRUE;
|
2021-01-03 23:26:33 +01:00
|
|
|
} else if(obj && obj->lua_type == PARAM_TYPE_DELETED) {
|
|
|
|
|
luaL_error(L, "Attempting to access deleted object.");
|
2020-07-21 15:03:03 +02:00
|
|
|
}
|
2020-07-20 20:50:15 +02:00
|
|
|
type = param_type == PARAM_TYPE_CARD ? "Card" : param_type == PARAM_TYPE_GROUP ? "Group" : "Effect";
|
2015-09-25 21:07:16 +08:00
|
|
|
break;
|
2020-07-20 20:50:15 +02:00
|
|
|
case PARAM_TYPE_FUNCTION:
|
2018-08-12 11:37:00 +08:00
|
|
|
if(lua_isfunction(L, index))
|
2015-09-25 21:07:16 +08:00
|
|
|
return TRUE;
|
2020-07-20 20:50:15 +02:00
|
|
|
type = "Function";
|
2015-09-25 21:07:16 +08:00
|
|
|
break;
|
2020-07-20 20:50:15 +02:00
|
|
|
case PARAM_TYPE_STRING:
|
2018-08-12 11:37:00 +08:00
|
|
|
if(lua_isstring(L, index))
|
2015-09-25 21:07:16 +08:00
|
|
|
return TRUE;
|
2020-07-20 20:50:15 +02:00
|
|
|
type = "String";
|
2015-09-25 21:07:16 +08:00
|
|
|
break;
|
2020-07-20 20:50:15 +02:00
|
|
|
case PARAM_TYPE_INT:
|
2020-03-14 18:40:09 +01:00
|
|
|
if(lua_isinteger(L, index) || lua_isnumber(L, index))
|
|
|
|
|
return TRUE;
|
2020-07-20 20:50:15 +02:00
|
|
|
type = "Int";
|
2020-03-14 18:40:09 +01:00
|
|
|
break;
|
2020-07-20 20:50:15 +02:00
|
|
|
case PARAM_TYPE_BOOLEAN:
|
2020-07-23 23:49:06 +02:00
|
|
|
if(lua_gettop(L) >= index)
|
2020-03-14 18:40:09 +01:00
|
|
|
return TRUE;
|
2020-07-20 20:50:15 +02:00
|
|
|
type = "boolean";
|
2020-03-14 18:40:09 +01:00
|
|
|
break;
|
|
|
|
|
}
|
2020-07-20 20:50:15 +02:00
|
|
|
if(retfalse)
|
|
|
|
|
return FALSE;
|
2020-08-23 17:56:55 +02:00
|
|
|
if(param_type == PARAM_TYPE_INT) {
|
|
|
|
|
interpreter::print_stacktrace(L);
|
2020-10-30 17:05:28 +01:00
|
|
|
const auto pduel = lua_get<duel*>(L);
|
2021-01-07 19:42:30 +01:00
|
|
|
pduel->handle_message(pduel->lua->format(R"(Parameter %d should be "%s".)", index, type), OCG_LOG_TYPE_ERROR);
|
2020-08-23 17:56:55 +02:00
|
|
|
} else {
|
2021-01-07 19:42:30 +01:00
|
|
|
luaL_error(L, R"(Parameter %d should be "%s".)", index, type);
|
2020-08-23 17:56:55 +02:00
|
|
|
}
|
2020-07-21 00:08:06 +02:00
|
|
|
return FALSE;
|
2015-09-25 21:07:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int32 scriptlib::check_param_count(lua_State* L, int32 count) {
|
|
|
|
|
if (lua_gettop(L) < count)
|
|
|
|
|
luaL_error(L, "%d Parameters are needed.", count);
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
int32 scriptlib::check_action_permission(lua_State* L) {
|
2020-10-30 17:05:28 +01:00
|
|
|
const auto pduel = lua_get<duel*>(L);
|
2015-09-25 21:07:16 +08:00
|
|
|
if(pduel->lua->no_action)
|
|
|
|
|
luaL_error(L, "Action is not allowed here.");
|
|
|
|
|
return TRUE;
|
2021-03-24 19:47:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Function get_lua_function(lua_State * L, int index) {
|
|
|
|
|
lua_pushvalue(L, index);
|
|
|
|
|
int32 ref = luaL_ref(L, LUA_REGISTRYINDEX);
|
|
|
|
|
return std::make_shared<Lua_Function_s>(lua_get<duel*>(L), ref);
|
|
|
|
|
}
|