Modernize lua read (part 1): C++ templating insurance (#7394)
* Modernize lua read (part 1): C++ templating assurance Implement the float readermaster
parent
86b19f2849
commit
180e551c56
|
@ -90,3 +90,15 @@ bool ModApiBase::isNaN(lua_State *L, int idx)
|
||||||
{
|
{
|
||||||
return lua_type(L, idx) == LUA_TNUMBER && std::isnan(lua_tonumber(L, idx));
|
return lua_type(L, idx) == LUA_TNUMBER && std::isnan(lua_tonumber(L, idx));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Read template functions
|
||||||
|
*/
|
||||||
|
template<>
|
||||||
|
float ModApiBase::readParam(lua_State *L, int index)
|
||||||
|
{
|
||||||
|
if (isNaN(L, index))
|
||||||
|
throw LuaError("NaN value is not allowed.");
|
||||||
|
|
||||||
|
return (float) luaL_checknumber(L, index);
|
||||||
|
}
|
||||||
|
|
|
@ -71,4 +71,15 @@ public:
|
||||||
int top);
|
int top);
|
||||||
|
|
||||||
static bool isNaN(lua_State *L, int idx);
|
static bool isNaN(lua_State *L, int idx);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Read a value using a template type T from Lua State L and index
|
||||||
|
*
|
||||||
|
* @tparam T type to read from Lua
|
||||||
|
* @param L Lua state
|
||||||
|
* @param index Lua Index to read
|
||||||
|
* @return read value from Lua
|
||||||
|
*/
|
||||||
|
template<typename T>
|
||||||
|
static T readParam(lua_State *L, int index);
|
||||||
};
|
};
|
||||||
|
|
|
@ -666,7 +666,7 @@ int ModApiEnvMod::l_get_objects_inside_radius(lua_State *L)
|
||||||
|
|
||||||
// Do it
|
// Do it
|
||||||
v3f pos = checkFloatPos(L, 1);
|
v3f pos = checkFloatPos(L, 1);
|
||||||
float radius = luaL_checknumber(L, 2) * BS;
|
float radius = readParam<float>(L, 2) * BS;
|
||||||
std::vector<u16> ids;
|
std::vector<u16> ids;
|
||||||
env->getObjectsInsideRadius(ids, pos, radius);
|
env->getObjectsInsideRadius(ids, pos, radius);
|
||||||
ScriptApiBase *script = getScriptApiBase(L);
|
ScriptApiBase *script = getScriptApiBase(L);
|
||||||
|
@ -690,7 +690,7 @@ int ModApiEnvMod::l_set_timeofday(lua_State *L)
|
||||||
GET_ENV_PTR;
|
GET_ENV_PTR;
|
||||||
|
|
||||||
// Do it
|
// Do it
|
||||||
float timeofday_f = luaL_checknumber(L, 1);
|
float timeofday_f = readParam<float>(L, 1);
|
||||||
sanity_check(timeofday_f >= 0.0 && timeofday_f <= 1.0);
|
sanity_check(timeofday_f >= 0.0 && timeofday_f <= 1.0);
|
||||||
int timeofday_mh = (int)(timeofday_f * 24000.0);
|
int timeofday_mh = (int)(timeofday_f * 24000.0);
|
||||||
// This should be set directly in the environment but currently
|
// This should be set directly in the environment but currently
|
||||||
|
@ -925,8 +925,8 @@ int ModApiEnvMod::l_get_perlin(lua_State *L)
|
||||||
} else {
|
} else {
|
||||||
params.seed = luaL_checkint(L, 1);
|
params.seed = luaL_checkint(L, 1);
|
||||||
params.octaves = luaL_checkint(L, 2);
|
params.octaves = luaL_checkint(L, 2);
|
||||||
params.persist = luaL_checknumber(L, 3);
|
params.persist = readParam<float>(L, 3);
|
||||||
params.spread = v3f(1, 1, 1) * luaL_checknumber(L, 4);
|
params.spread = v3f(1, 1, 1) * readParam<float>(L, 4);
|
||||||
}
|
}
|
||||||
|
|
||||||
params.seed += (int)env->getServerMap().getSeed();
|
params.seed += (int)env->getServerMap().getSeed();
|
||||||
|
|
|
@ -190,7 +190,7 @@ int MetaDataRef::l_set_float(lua_State *L)
|
||||||
|
|
||||||
MetaDataRef *ref = checkobject(L, 1);
|
MetaDataRef *ref = checkobject(L, 1);
|
||||||
std::string name = luaL_checkstring(L, 2);
|
std::string name = luaL_checkstring(L, 2);
|
||||||
float a = luaL_checknumber(L, 3);
|
float a = readParam<float>(L, 3);
|
||||||
std::string str = ftos(a);
|
std::string str = ftos(a);
|
||||||
|
|
||||||
Metadata *meta = ref->getmeta(true);
|
Metadata *meta = ref->getmeta(true);
|
||||||
|
|
|
@ -43,8 +43,8 @@ int NodeTimerRef::l_set(lua_State *L)
|
||||||
NodeTimerRef *o = checkobject(L, 1);
|
NodeTimerRef *o = checkobject(L, 1);
|
||||||
ServerEnvironment *env = o->m_env;
|
ServerEnvironment *env = o->m_env;
|
||||||
if(env == NULL) return 0;
|
if(env == NULL) return 0;
|
||||||
f32 t = luaL_checknumber(L,2);
|
f32 t = readParam<float>(L,2);
|
||||||
f32 e = luaL_checknumber(L,3);
|
f32 e = readParam<float>(L,3);
|
||||||
env->getMap().setNodeTimer(NodeTimer(t, e, o->m_p));
|
env->getMap().setNodeTimer(NodeTimer(t, e, o->m_p));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -55,7 +55,7 @@ int NodeTimerRef::l_start(lua_State *L)
|
||||||
NodeTimerRef *o = checkobject(L, 1);
|
NodeTimerRef *o = checkobject(L, 1);
|
||||||
ServerEnvironment *env = o->m_env;
|
ServerEnvironment *env = o->m_env;
|
||||||
if(env == NULL) return 0;
|
if(env == NULL) return 0;
|
||||||
f32 t = luaL_checknumber(L,2);
|
f32 t = readParam<float>(L,2);
|
||||||
env->getMap().setNodeTimer(NodeTimer(t, 0, o->m_p));
|
env->getMap().setNodeTimer(NodeTimer(t, 0, o->m_p));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -69,8 +69,8 @@ int LuaPerlinNoise::create_object(lua_State *L)
|
||||||
} else {
|
} else {
|
||||||
params.seed = luaL_checkint(L, 1);
|
params.seed = luaL_checkint(L, 1);
|
||||||
params.octaves = luaL_checkint(L, 2);
|
params.octaves = luaL_checkint(L, 2);
|
||||||
params.persist = luaL_checknumber(L, 3);
|
params.persist = readParam<float>(L, 3);
|
||||||
params.spread = v3f(1, 1, 1) * luaL_checknumber(L, 4);
|
params.spread = v3f(1, 1, 1) * readParam<float>(L, 4);
|
||||||
}
|
}
|
||||||
|
|
||||||
LuaPerlinNoise *o = new LuaPerlinNoise(¶ms);
|
LuaPerlinNoise *o = new LuaPerlinNoise(¶ms);
|
||||||
|
|
|
@ -898,7 +898,7 @@ int ObjectRef::l_set_yaw(lua_State *L)
|
||||||
if (isNaN(L, 2))
|
if (isNaN(L, 2))
|
||||||
throw LuaError("ObjectRef::set_yaw: NaN value is not allowed.");
|
throw LuaError("ObjectRef::set_yaw: NaN value is not allowed.");
|
||||||
|
|
||||||
float yaw = luaL_checknumber(L, 2) * core::RADTODEG;
|
float yaw = readParam<float>(L, 2) * core::RADTODEG;
|
||||||
// Do it
|
// Do it
|
||||||
co->setYaw(yaw);
|
co->setYaw(yaw);
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -1118,7 +1118,7 @@ int ObjectRef::l_set_look_vertical(lua_State *L)
|
||||||
ObjectRef *ref = checkobject(L, 1);
|
ObjectRef *ref = checkobject(L, 1);
|
||||||
PlayerSAO* co = getplayersao(ref);
|
PlayerSAO* co = getplayersao(ref);
|
||||||
if (co == NULL) return 0;
|
if (co == NULL) return 0;
|
||||||
float pitch = luaL_checknumber(L, 2) * core::RADTODEG;
|
float pitch = readParam<float>(L, 2) * core::RADTODEG;
|
||||||
// Do it
|
// Do it
|
||||||
co->setPitchAndSend(pitch);
|
co->setPitchAndSend(pitch);
|
||||||
return 1;
|
return 1;
|
||||||
|
@ -1131,7 +1131,7 @@ int ObjectRef::l_set_look_horizontal(lua_State *L)
|
||||||
ObjectRef *ref = checkobject(L, 1);
|
ObjectRef *ref = checkobject(L, 1);
|
||||||
PlayerSAO* co = getplayersao(ref);
|
PlayerSAO* co = getplayersao(ref);
|
||||||
if (co == NULL) return 0;
|
if (co == NULL) return 0;
|
||||||
float yaw = luaL_checknumber(L, 2) * core::RADTODEG;
|
float yaw = readParam<float>(L, 2) * core::RADTODEG;
|
||||||
// Do it
|
// Do it
|
||||||
co->setYawAndSend(yaw);
|
co->setYawAndSend(yaw);
|
||||||
return 1;
|
return 1;
|
||||||
|
@ -1149,7 +1149,7 @@ int ObjectRef::l_set_look_pitch(lua_State *L)
|
||||||
ObjectRef *ref = checkobject(L, 1);
|
ObjectRef *ref = checkobject(L, 1);
|
||||||
PlayerSAO* co = getplayersao(ref);
|
PlayerSAO* co = getplayersao(ref);
|
||||||
if (co == NULL) return 0;
|
if (co == NULL) return 0;
|
||||||
float pitch = luaL_checknumber(L, 2) * core::RADTODEG;
|
float pitch = readParam<float>(L, 2) * core::RADTODEG;
|
||||||
// Do it
|
// Do it
|
||||||
co->setPitchAndSend(pitch);
|
co->setPitchAndSend(pitch);
|
||||||
return 1;
|
return 1;
|
||||||
|
@ -1167,7 +1167,7 @@ int ObjectRef::l_set_look_yaw(lua_State *L)
|
||||||
ObjectRef *ref = checkobject(L, 1);
|
ObjectRef *ref = checkobject(L, 1);
|
||||||
PlayerSAO* co = getplayersao(ref);
|
PlayerSAO* co = getplayersao(ref);
|
||||||
if (co == NULL) return 0;
|
if (co == NULL) return 0;
|
||||||
float yaw = luaL_checknumber(L, 2) * core::RADTODEG;
|
float yaw = readParam<float>(L, 2) * core::RADTODEG;
|
||||||
// Do it
|
// Do it
|
||||||
co->setYawAndSend(yaw);
|
co->setYawAndSend(yaw);
|
||||||
return 1;
|
return 1;
|
||||||
|
@ -1739,7 +1739,7 @@ int ObjectRef::l_override_day_night_ratio(lua_State *L)
|
||||||
float ratio = 0.0f;
|
float ratio = 0.0f;
|
||||||
if (!lua_isnil(L, 2)) {
|
if (!lua_isnil(L, 2)) {
|
||||||
do_override = true;
|
do_override = true;
|
||||||
ratio = luaL_checknumber(L, 2);
|
ratio = readParam<float>(L, 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!getServer(L)->overrideDayNightRatio(player, do_override, ratio))
|
if (!getServer(L)->overrideDayNightRatio(player, do_override, ratio))
|
||||||
|
|
|
@ -455,8 +455,8 @@ int ModApiServer::l_sound_fade(lua_State *L)
|
||||||
{
|
{
|
||||||
NO_MAP_LOCK_REQUIRED;
|
NO_MAP_LOCK_REQUIRED;
|
||||||
s32 handle = luaL_checkinteger(L, 1);
|
s32 handle = luaL_checkinteger(L, 1);
|
||||||
float step = luaL_checknumber(L, 2);
|
float step = readParam<float>(L, 2);
|
||||||
float gain = luaL_checknumber(L, 3);
|
float gain = readParam<float>(L, 3);
|
||||||
getServer(L)->fadeSound(handle, step, gain);
|
getServer(L)->fadeSound(handle, step, gain);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -179,8 +179,7 @@ int ModApiUtil::l_get_hit_params(lua_State *L)
|
||||||
if(lua_isnoneornil(L, 3))
|
if(lua_isnoneornil(L, 3))
|
||||||
push_hit_params(L, getHitParams(groups, &tp));
|
push_hit_params(L, getHitParams(groups, &tp));
|
||||||
else
|
else
|
||||||
push_hit_params(L, getHitParams(groups, &tp,
|
push_hit_params(L, getHitParams(groups, &tp, readParam<float>(L, 3)));
|
||||||
luaL_checknumber(L, 3)));
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -270,7 +269,7 @@ int ModApiUtil::l_compress(lua_State *L)
|
||||||
|
|
||||||
int level = -1;
|
int level = -1;
|
||||||
if (!lua_isnone(L, 3) && !lua_isnil(L, 3))
|
if (!lua_isnone(L, 3) && !lua_isnil(L, 3))
|
||||||
level = luaL_checknumber(L, 3);
|
level = readParam<float>(L, 3);
|
||||||
|
|
||||||
std::ostringstream os;
|
std::ostringstream os;
|
||||||
compressZlib(std::string(data, size), os, level);
|
compressZlib(std::string(data, size), os, level);
|
||||||
|
|
Loading…
Reference in New Issue