Encapsulate userdata checks

This commit is contained in:
Tatsuru 2020-09-14 17:33:15 -03:00
parent 8d65ce81c7
commit 013714a26f

View file

@ -2586,10 +2586,36 @@ static int lib_rGetNameByColor(lua_State *L)
// S_SOUND
////////////
static int GetValidSoundOrigin(lua_State *L, void **origin)
{
const char *type;
lua_settop(L, 1);
type = GetUserdataUType(L);
if (fasticmp(type, "mobj_t"))
{
*origin = *((mobj_t **)luaL_checkudata(L, 1, META_MOBJ));
if (!(*origin))
return LUA_ErrInvalid(L, "mobj_t");
return 1;
}
else if (fasticmp(type, "sector_t"))
{
*origin = *((sector_t **)luaL_checkudata(L, 1, META_SECTOR));
if (!(*origin))
return LUA_ErrInvalid(L, "sector_t");
*origin = &((sector_t *)(*origin))->soundorg;
return 1;
}
return LUA_ErrInvalid(L, "mobj_t/sector_t");
}
static int lib_sStartSound(lua_State *L)
{
void *origin = NULL;
const char *origtype;
sfxenum_t sound_id = luaL_checkinteger(L, 2);
player_t *player = NULL;
//NOHUD
@ -2604,28 +2630,8 @@ static int lib_sStartSound(lua_State *L)
return LUA_ErrInvalid(L, "player_t");
}
if (!lua_isnil(L, 1))
{
lua_settop(L, 1);
origtype = GetUserdataUType(L);
if (fasticmp(origtype, "mobj_t"))
{
origin = *((mobj_t **)luaL_checkudata(L, 1, META_MOBJ));
if (!origin)
return LUA_ErrInvalid(L, "mobj_t");
}
else if (fasticmp(origtype, "sector_t"))
{
origin = *((sector_t **)luaL_checkudata(L, 1, META_SECTOR));
if (!origin)
return LUA_ErrInvalid(L, "sector_t");
origin = &((sector_t *)origin)->soundorg;
}
else
return LUA_ErrInvalid(L, "mobj_t/sector_t");
}
if (!GetValidSoundOrigin(L, &origin))
return 0;
if (!player || P_IsLocalPlayer(player))
{
if (hud_running)
@ -2639,7 +2645,6 @@ static int lib_sStartSound(lua_State *L)
static int lib_sStartSoundAtVolume(lua_State *L)
{
void *origin = NULL;
const char *origtype;
sfxenum_t sound_id = luaL_checkinteger(L, 2);
INT32 volume = (INT32)luaL_checkinteger(L, 3);
player_t *player = NULL;
@ -2654,56 +2659,19 @@ static int lib_sStartSoundAtVolume(lua_State *L)
return LUA_ErrInvalid(L, "player_t");
}
if (!lua_isnil(L, 1))
{
lua_settop(L, 1);
origtype = GetUserdataUType(L);
if (fasticmp(origtype, "mobj_t"))
{
origin = *((mobj_t **)luaL_checkudata(L, 1, META_MOBJ));
if (!origin)
return LUA_ErrInvalid(L, "mobj_t");
}
else if (fasticmp(origtype, "sector_t"))
{
origin = *((sector_t **)luaL_checkudata(L, 1, META_SECTOR));
if (!origin)
return LUA_ErrInvalid(L, "sector_t");
origin = &((sector_t *)origin)->soundorg;
}
else
if (!GetValidSoundOrigin(L, &origin))
return LUA_ErrInvalid(L, "mobj_t/sector_t");
}
if (!player || P_IsLocalPlayer(player))
S_StartSoundAtVolume(origin, sound_id, volume);
S_StartSoundAtVolume(origin, sound_id, volume);
return 0;
}
static int lib_sStopSound(lua_State *L)
{
void *origin = NULL;
const char *origtype;
//NOHUD
lua_settop(L, 1);
origtype = GetUserdataUType(L);
if (fasticmp(origtype, "mobj_t"))
{
origin = *((mobj_t **)luaL_checkudata(L, 1, META_MOBJ));
if (!origin)
return LUA_ErrInvalid(L, "mobj_t");
}
else if (fasticmp(origtype, "sector_t"))
{
origin = *((sector_t **)luaL_checkudata(L, 1, META_SECTOR));
if (!origin)
return LUA_ErrInvalid(L, "sector_t");
origin = &((sector_t *)origin)->soundorg;
}
else
if (!GetValidSoundOrigin(L, &origin))
return LUA_ErrInvalid(L, "mobj_t/sector_t");
S_StopSound(origin);
@ -2713,35 +2681,14 @@ static int lib_sStopSound(lua_State *L)
static int lib_sStopSoundByID(lua_State *L)
{
void *origin = NULL;
const char *origtype;
sfxenum_t sound_id = luaL_checkinteger(L, 2);
//NOHUD
if (sound_id >= NUMSFX)
return luaL_error(L, "sfx %d out of range (0 - %d)", sound_id, NUMSFX-1);
if (!lua_isnil(L, 1))
{
lua_settop(L, 1);
origtype = GetUserdataUType(L);
if (fasticmp(origtype, "mobj_t"))
{
origin = *((mobj_t **)luaL_checkudata(L, 1, META_MOBJ));
if (!origin)
return LUA_ErrInvalid(L, "mobj_t");
}
else if (fasticmp(origtype, "sector_t"))
{
origin = *((sector_t **)luaL_checkudata(L, 1, META_SECTOR));
if (!origin)
return LUA_ErrInvalid(L, "sector_t");
origin = &((sector_t *)origin)->soundorg;
}
else
if (!GetValidSoundOrigin(L, &origin))
return LUA_ErrInvalid(L, "mobj_t/sector_t");
}
S_StopSoundByID(origin, sound_id);
return 0;
@ -2968,27 +2915,9 @@ static int lib_sSetMusicPosition(lua_State *L)
static int lib_sOriginPlaying(lua_State *L)
{
void *origin = NULL;
const char *origtype;
//NOHUD
INLEVEL
lua_settop(L, 1);
origtype = GetUserdataUType(L);
if (fasticmp(origtype, "mobj_t"))
{
origin = *((mobj_t **)luaL_checkudata(L, 1, META_MOBJ));
if (!origin)
return LUA_ErrInvalid(L, "mobj_t");
}
else if (fasticmp(origtype, "sector_t"))
{
origin = *((sector_t **)luaL_checkudata(L, 1, META_SECTOR));
if (!origin)
return LUA_ErrInvalid(L, "sector_t");
origin = &((sector_t *)origin)->soundorg;
}
else
if (!GetValidSoundOrigin(L, &origin))
return LUA_ErrInvalid(L, "mobj_t/sector_t");
lua_pushboolean(L, S_OriginPlaying(origin));
@ -3008,35 +2937,13 @@ static int lib_sIdPlaying(lua_State *L)
static int lib_sSoundPlaying(lua_State *L)
{
void *origin = NULL;
const char *origtype;
sfxenum_t id = luaL_checkinteger(L, 2);
//NOHUD
INLEVEL
if (id >= NUMSFX)
return luaL_error(L, "sfx %d out of range (0 - %d)", id, NUMSFX-1);
if (!lua_isnil(L, 1))
{
lua_settop(L, 1);
origtype = GetUserdataUType(L);
if (fasticmp(origtype, "mobj_t"))
{
origin = *((mobj_t **)luaL_checkudata(L, 1, META_MOBJ));
if (!origin)
return LUA_ErrInvalid(L, "mobj_t");
}
else if (fasticmp(origtype, "sector_t"))
{
origin = *((sector_t **)luaL_checkudata(L, 1, META_SECTOR));
if (!origin)
return LUA_ErrInvalid(L, "sector_t");
origin = &((sector_t *)origin)->soundorg;
}
else
return LUA_ErrInvalid(L, "mobj_t/sector_t");
}
if (!GetValidSoundOrigin(L, &origin))
return LUA_ErrInvalid(L, "mobj_t/sector_t");
lua_pushboolean(L, S_SoundPlaying(origin, id));
return 1;