1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-06-27 16:36:03 +00:00

Consolidate API object code (#12728)

Co-authored-by: sfan5 <sfan5@live.de>
This commit is contained in:
Jude Melton-Houghton 2022-10-04 08:31:36 -04:00 committed by GitHub
parent b21fb18379
commit 7632af3c73
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
42 changed files with 463 additions and 1079 deletions

View file

@ -40,7 +40,7 @@ LuaPerlinNoise::LuaPerlinNoise(const NoiseParams *params) :
int LuaPerlinNoise::l_get_2d(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
LuaPerlinNoise *o = checkobject(L, 1);
LuaPerlinNoise *o = checkObject<LuaPerlinNoise>(L, 1);
v2f p = readParam<v2f>(L, 2);
lua_Number val = NoisePerlin2D(&o->np, p.X, p.Y, 0);
lua_pushnumber(L, val);
@ -51,7 +51,7 @@ int LuaPerlinNoise::l_get_2d(lua_State *L)
int LuaPerlinNoise::l_get_3d(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
LuaPerlinNoise *o = checkobject(L, 1);
LuaPerlinNoise *o = checkObject<LuaPerlinNoise>(L, 1);
v3f p = check_v3f(L, 2);
lua_Number val = NoisePerlin3D(&o->np, p.X, p.Y, p.Z, 0);
lua_pushnumber(L, val);
@ -91,20 +91,9 @@ int LuaPerlinNoise::gc_object(lua_State *L)
}
LuaPerlinNoise *LuaPerlinNoise::checkobject(lua_State *L, int narg)
{
NO_MAP_LOCK_REQUIRED;
luaL_checktype(L, narg, LUA_TUSERDATA);
void *ud = luaL_checkudata(L, narg, className);
if (!ud)
luaL_typerror(L, narg, className);
return *(LuaPerlinNoise **)ud;
}
void *LuaPerlinNoise::packIn(lua_State *L, int idx)
{
LuaPerlinNoise *o = checkobject(L, idx);
LuaPerlinNoise *o = checkObject<LuaPerlinNoise>(L, idx);
return new NoiseParams(o->np);
}
@ -123,27 +112,11 @@ void LuaPerlinNoise::packOut(lua_State *L, void *ptr)
void LuaPerlinNoise::Register(lua_State *L)
{
lua_newtable(L);
int methodtable = lua_gettop(L);
luaL_newmetatable(L, className);
int metatable = lua_gettop(L);
lua_pushliteral(L, "__metatable");
lua_pushvalue(L, methodtable);
lua_settable(L, metatable);
lua_pushliteral(L, "__index");
lua_pushvalue(L, methodtable);
lua_settable(L, metatable);
lua_pushliteral(L, "__gc");
lua_pushcfunction(L, gc_object);
lua_settable(L, metatable);
lua_pop(L, 1);
luaL_register(L, nullptr, methods);
lua_pop(L, 1);
static const luaL_Reg metamethods[] = {
{"__gc", gc_object},
{0, 0}
};
registerClass(L, className, methods, metamethods);
lua_register(L, className, create_object);
@ -184,7 +157,7 @@ int LuaPerlinNoiseMap::l_get_2d_map(lua_State *L)
NO_MAP_LOCK_REQUIRED;
size_t i = 0;
LuaPerlinNoiseMap *o = checkobject(L, 1);
LuaPerlinNoiseMap *o = checkObject<LuaPerlinNoiseMap>(L, 1);
v2f p = readParam<v2f>(L, 2);
Noise *n = o->noise;
@ -207,7 +180,7 @@ int LuaPerlinNoiseMap::l_get_2d_map_flat(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
LuaPerlinNoiseMap *o = checkobject(L, 1);
LuaPerlinNoiseMap *o = checkObject<LuaPerlinNoiseMap>(L, 1);
v2f p = readParam<v2f>(L, 2);
bool use_buffer = lua_istable(L, 3);
@ -234,7 +207,7 @@ int LuaPerlinNoiseMap::l_get_3d_map(lua_State *L)
NO_MAP_LOCK_REQUIRED;
size_t i = 0;
LuaPerlinNoiseMap *o = checkobject(L, 1);
LuaPerlinNoiseMap *o = checkObject<LuaPerlinNoiseMap>(L, 1);
v3f p = check_v3f(L, 2);
if (!o->is3D())
@ -264,7 +237,7 @@ int LuaPerlinNoiseMap::l_get_3d_map_flat(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
LuaPerlinNoiseMap *o = checkobject(L, 1);
LuaPerlinNoiseMap *o = checkObject<LuaPerlinNoiseMap>(L, 1);
v3f p = check_v3f(L, 2);
bool use_buffer = lua_istable(L, 3);
@ -293,7 +266,7 @@ int LuaPerlinNoiseMap::l_calc_2d_map(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
LuaPerlinNoiseMap *o = checkobject(L, 1);
LuaPerlinNoiseMap *o = checkObject<LuaPerlinNoiseMap>(L, 1);
v2f p = readParam<v2f>(L, 2);
Noise *n = o->noise;
@ -306,7 +279,7 @@ int LuaPerlinNoiseMap::l_calc_3d_map(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
LuaPerlinNoiseMap *o = checkobject(L, 1);
LuaPerlinNoiseMap *o = checkObject<LuaPerlinNoiseMap>(L, 1);
v3f p = check_v3f(L, 2);
if (!o->is3D())
@ -323,7 +296,7 @@ int LuaPerlinNoiseMap::l_get_map_slice(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
LuaPerlinNoiseMap *o = checkobject(L, 1);
LuaPerlinNoiseMap *o = checkObject<LuaPerlinNoiseMap>(L, 1);
v3s16 slice_offset = read_v3s16(L, 2);
v3s16 slice_size = read_v3s16(L, 3);
bool use_buffer = lua_istable(L, 4);
@ -367,18 +340,6 @@ int LuaPerlinNoiseMap::gc_object(lua_State *L)
}
LuaPerlinNoiseMap *LuaPerlinNoiseMap::checkobject(lua_State *L, int narg)
{
luaL_checktype(L, narg, LUA_TUSERDATA);
void *ud = luaL_checkudata(L, narg, className);
if (!ud)
luaL_typerror(L, narg, className);
return *(LuaPerlinNoiseMap **)ud;
}
struct NoiseMapParams {
NoiseParams np;
s32 seed;
@ -387,7 +348,7 @@ struct NoiseMapParams {
void *LuaPerlinNoiseMap::packIn(lua_State *L, int idx)
{
LuaPerlinNoiseMap *o = checkobject(L, idx);
LuaPerlinNoiseMap *o = checkObject<LuaPerlinNoiseMap>(L, idx);
NoiseMapParams *ret = new NoiseMapParams();
ret->np = o->noise->np;
ret->seed = o->noise->seed;
@ -410,27 +371,11 @@ void LuaPerlinNoiseMap::packOut(lua_State *L, void *ptr)
void LuaPerlinNoiseMap::Register(lua_State *L)
{
lua_newtable(L);
int methodtable = lua_gettop(L);
luaL_newmetatable(L, className);
int metatable = lua_gettop(L);
lua_pushliteral(L, "__metatable");
lua_pushvalue(L, methodtable);
lua_settable(L, metatable);
lua_pushliteral(L, "__index");
lua_pushvalue(L, methodtable);
lua_settable(L, metatable);
lua_pushliteral(L, "__gc");
lua_pushcfunction(L, gc_object);
lua_settable(L, metatable);
lua_pop(L, 1);
luaL_register(L, nullptr, methods);
lua_pop(L, 1);
static const luaL_Reg metamethods[] = {
{"__gc", gc_object},
{0, 0}
};
registerClass(L, className, methods, metamethods);
lua_register(L, className, create_object);
@ -459,7 +404,7 @@ int LuaPseudoRandom::l_next(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
LuaPseudoRandom *o = checkobject(L, 1);
LuaPseudoRandom *o = checkObject<LuaPseudoRandom>(L, 1);
int min = 0;
int max = 32767;
lua_settop(L, 3);
@ -505,39 +450,13 @@ int LuaPseudoRandom::gc_object(lua_State *L)
}
LuaPseudoRandom *LuaPseudoRandom::checkobject(lua_State *L, int narg)
{
luaL_checktype(L, narg, LUA_TUSERDATA);
void *ud = luaL_checkudata(L, narg, className);
if (!ud)
luaL_typerror(L, narg, className);
return *(LuaPseudoRandom **)ud;
}
void LuaPseudoRandom::Register(lua_State *L)
{
lua_newtable(L);
int methodtable = lua_gettop(L);
luaL_newmetatable(L, className);
int metatable = lua_gettop(L);
lua_pushliteral(L, "__metatable");
lua_pushvalue(L, methodtable);
lua_settable(L, metatable);
lua_pushliteral(L, "__index");
lua_pushvalue(L, methodtable);
lua_settable(L, metatable);
lua_pushliteral(L, "__gc");
lua_pushcfunction(L, gc_object);
lua_settable(L, metatable);
lua_pop(L, 1);
luaL_register(L, nullptr, methods);
lua_pop(L, 1);
static const luaL_Reg metamethods[] = {
{"__gc", gc_object},
{0, 0}
};
registerClass(L, className, methods, metamethods);
lua_register(L, className, create_object);
}
@ -558,7 +477,7 @@ int LuaPcgRandom::l_next(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
LuaPcgRandom *o = checkobject(L, 1);
LuaPcgRandom *o = checkObject<LuaPcgRandom>(L, 1);
u32 min = lua_isnumber(L, 2) ? lua_tointeger(L, 2) : o->m_rnd.RANDOM_MIN;
u32 max = lua_isnumber(L, 3) ? lua_tointeger(L, 3) : o->m_rnd.RANDOM_MAX;
@ -571,7 +490,7 @@ int LuaPcgRandom::l_rand_normal_dist(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
LuaPcgRandom *o = checkobject(L, 1);
LuaPcgRandom *o = checkObject<LuaPcgRandom>(L, 1);
u32 min = lua_isnumber(L, 2) ? lua_tointeger(L, 2) : o->m_rnd.RANDOM_MIN;
u32 max = lua_isnumber(L, 3) ? lua_tointeger(L, 3) : o->m_rnd.RANDOM_MAX;
int num_trials = lua_isnumber(L, 4) ? lua_tointeger(L, 4) : 6;
@ -604,39 +523,13 @@ int LuaPcgRandom::gc_object(lua_State *L)
}
LuaPcgRandom *LuaPcgRandom::checkobject(lua_State *L, int narg)
{
luaL_checktype(L, narg, LUA_TUSERDATA);
void *ud = luaL_checkudata(L, narg, className);
if (!ud)
luaL_typerror(L, narg, className);
return *(LuaPcgRandom **)ud;
}
void LuaPcgRandom::Register(lua_State *L)
{
lua_newtable(L);
int methodtable = lua_gettop(L);
luaL_newmetatable(L, className);
int metatable = lua_gettop(L);
lua_pushliteral(L, "__metatable");
lua_pushvalue(L, methodtable);
lua_settable(L, metatable);
lua_pushliteral(L, "__index");
lua_pushvalue(L, methodtable);
lua_settable(L, metatable);
lua_pushliteral(L, "__gc");
lua_pushcfunction(L, gc_object);
lua_settable(L, metatable);
lua_pop(L, 1);
luaL_register(L, nullptr, methods);
lua_pop(L, 1);
static const luaL_Reg metamethods[] = {
{"__gc", gc_object},
{0, 0}
};
registerClass(L, className, methods, metamethods);
lua_register(L, className, create_object);
}
@ -663,7 +556,7 @@ int LuaSecureRandom::l_next_bytes(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
LuaSecureRandom *o = checkobject(L, 1);
LuaSecureRandom *o = checkObject<LuaSecureRandom>(L, 1);
u32 count = lua_isnumber(L, 2) ? lua_tointeger(L, 2) : 1;
// Limit count
@ -719,39 +612,13 @@ int LuaSecureRandom::gc_object(lua_State *L)
}
LuaSecureRandom *LuaSecureRandom::checkobject(lua_State *L, int narg)
{
luaL_checktype(L, narg, LUA_TUSERDATA);
void *ud = luaL_checkudata(L, narg, className);
if (!ud)
luaL_typerror(L, narg, className);
return *(LuaSecureRandom **)ud;
}
void LuaSecureRandom::Register(lua_State *L)
{
lua_newtable(L);
int methodtable = lua_gettop(L);
luaL_newmetatable(L, className);
int metatable = lua_gettop(L);
lua_pushliteral(L, "__metatable");
lua_pushvalue(L, methodtable);
lua_settable(L, metatable);
lua_pushliteral(L, "__index");
lua_pushvalue(L, methodtable);
lua_settable(L, metatable);
lua_pushliteral(L, "__gc");
lua_pushcfunction(L, gc_object);
lua_settable(L, metatable);
lua_pop(L, 1);
luaL_register(L, nullptr, methods);
lua_pop(L, 1);
static const luaL_Reg metamethods[] = {
{"__gc", gc_object},
{0, 0}
};
registerClass(L, className, methods, metamethods);
lua_register(L, className, create_object);
}