Encapsulate userdata checks
This commit is contained in:
parent
8d65ce81c7
commit
013714a26f
|
@ -2586,10 +2586,36 @@ static int lib_rGetNameByColor(lua_State *L)
|
||||||
|
|
||||||
// S_SOUND
|
// 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)
|
static int lib_sStartSound(lua_State *L)
|
||||||
{
|
{
|
||||||
void *origin = NULL;
|
void *origin = NULL;
|
||||||
const char *origtype;
|
|
||||||
sfxenum_t sound_id = luaL_checkinteger(L, 2);
|
sfxenum_t sound_id = luaL_checkinteger(L, 2);
|
||||||
player_t *player = NULL;
|
player_t *player = NULL;
|
||||||
//NOHUD
|
//NOHUD
|
||||||
|
@ -2604,28 +2630,8 @@ static int lib_sStartSound(lua_State *L)
|
||||||
return LUA_ErrInvalid(L, "player_t");
|
return LUA_ErrInvalid(L, "player_t");
|
||||||
}
|
}
|
||||||
if (!lua_isnil(L, 1))
|
if (!lua_isnil(L, 1))
|
||||||
{
|
if (!GetValidSoundOrigin(L, &origin))
|
||||||
lua_settop(L, 1);
|
return 0;
|
||||||
|
|
||||||
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 (!player || P_IsLocalPlayer(player))
|
if (!player || P_IsLocalPlayer(player))
|
||||||
{
|
{
|
||||||
if (hud_running)
|
if (hud_running)
|
||||||
|
@ -2639,7 +2645,6 @@ static int lib_sStartSound(lua_State *L)
|
||||||
static int lib_sStartSoundAtVolume(lua_State *L)
|
static int lib_sStartSoundAtVolume(lua_State *L)
|
||||||
{
|
{
|
||||||
void *origin = NULL;
|
void *origin = NULL;
|
||||||
const char *origtype;
|
|
||||||
sfxenum_t sound_id = luaL_checkinteger(L, 2);
|
sfxenum_t sound_id = luaL_checkinteger(L, 2);
|
||||||
INT32 volume = (INT32)luaL_checkinteger(L, 3);
|
INT32 volume = (INT32)luaL_checkinteger(L, 3);
|
||||||
player_t *player = NULL;
|
player_t *player = NULL;
|
||||||
|
@ -2654,56 +2659,19 @@ static int lib_sStartSoundAtVolume(lua_State *L)
|
||||||
return LUA_ErrInvalid(L, "player_t");
|
return LUA_ErrInvalid(L, "player_t");
|
||||||
}
|
}
|
||||||
if (!lua_isnil(L, 1))
|
if (!lua_isnil(L, 1))
|
||||||
{
|
if (!GetValidSoundOrigin(L, &origin))
|
||||||
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");
|
return LUA_ErrInvalid(L, "mobj_t/sector_t");
|
||||||
}
|
|
||||||
if (!player || P_IsLocalPlayer(player))
|
if (!player || P_IsLocalPlayer(player))
|
||||||
S_StartSoundAtVolume(origin, sound_id, volume);
|
S_StartSoundAtVolume(origin, sound_id, volume);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int lib_sStopSound(lua_State *L)
|
static int lib_sStopSound(lua_State *L)
|
||||||
{
|
{
|
||||||
void *origin = NULL;
|
void *origin = NULL;
|
||||||
const char *origtype;
|
|
||||||
//NOHUD
|
//NOHUD
|
||||||
lua_settop(L, 1);
|
if (!GetValidSoundOrigin(L, &origin))
|
||||||
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");
|
return LUA_ErrInvalid(L, "mobj_t/sector_t");
|
||||||
|
|
||||||
S_StopSound(origin);
|
S_StopSound(origin);
|
||||||
|
@ -2713,35 +2681,14 @@ static int lib_sStopSound(lua_State *L)
|
||||||
static int lib_sStopSoundByID(lua_State *L)
|
static int lib_sStopSoundByID(lua_State *L)
|
||||||
{
|
{
|
||||||
void *origin = NULL;
|
void *origin = NULL;
|
||||||
const char *origtype;
|
|
||||||
sfxenum_t sound_id = luaL_checkinteger(L, 2);
|
sfxenum_t sound_id = luaL_checkinteger(L, 2);
|
||||||
//NOHUD
|
//NOHUD
|
||||||
|
|
||||||
if (sound_id >= NUMSFX)
|
if (sound_id >= NUMSFX)
|
||||||
return luaL_error(L, "sfx %d out of range (0 - %d)", sound_id, NUMSFX-1);
|
return luaL_error(L, "sfx %d out of range (0 - %d)", sound_id, NUMSFX-1);
|
||||||
if (!lua_isnil(L, 1))
|
if (!lua_isnil(L, 1))
|
||||||
{
|
if (!GetValidSoundOrigin(L, &origin))
|
||||||
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");
|
return LUA_ErrInvalid(L, "mobj_t/sector_t");
|
||||||
}
|
|
||||||
|
|
||||||
S_StopSoundByID(origin, sound_id);
|
S_StopSoundByID(origin, sound_id);
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -2968,27 +2915,9 @@ static int lib_sSetMusicPosition(lua_State *L)
|
||||||
static int lib_sOriginPlaying(lua_State *L)
|
static int lib_sOriginPlaying(lua_State *L)
|
||||||
{
|
{
|
||||||
void *origin = NULL;
|
void *origin = NULL;
|
||||||
const char *origtype;
|
|
||||||
//NOHUD
|
//NOHUD
|
||||||
INLEVEL
|
INLEVEL
|
||||||
lua_settop(L, 1);
|
if (!GetValidSoundOrigin(L, &origin))
|
||||||
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");
|
return LUA_ErrInvalid(L, "mobj_t/sector_t");
|
||||||
|
|
||||||
lua_pushboolean(L, S_OriginPlaying(origin));
|
lua_pushboolean(L, S_OriginPlaying(origin));
|
||||||
|
@ -3008,35 +2937,13 @@ static int lib_sIdPlaying(lua_State *L)
|
||||||
static int lib_sSoundPlaying(lua_State *L)
|
static int lib_sSoundPlaying(lua_State *L)
|
||||||
{
|
{
|
||||||
void *origin = NULL;
|
void *origin = NULL;
|
||||||
const char *origtype;
|
|
||||||
sfxenum_t id = luaL_checkinteger(L, 2);
|
sfxenum_t id = luaL_checkinteger(L, 2);
|
||||||
//NOHUD
|
//NOHUD
|
||||||
INLEVEL
|
INLEVEL
|
||||||
if (id >= NUMSFX)
|
if (id >= NUMSFX)
|
||||||
return luaL_error(L, "sfx %d out of range (0 - %d)", id, NUMSFX-1);
|
return luaL_error(L, "sfx %d out of range (0 - %d)", id, NUMSFX-1);
|
||||||
if (!lua_isnil(L, 1))
|
if (!GetValidSoundOrigin(L, &origin))
|
||||||
{
|
return LUA_ErrInvalid(L, "mobj_t/sector_t");
|
||||||
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");
|
|
||||||
}
|
|
||||||
|
|
||||||
lua_pushboolean(L, S_SoundPlaying(origin, id));
|
lua_pushboolean(L, S_SoundPlaying(origin, id));
|
||||||
return 1;
|
return 1;
|
||||||
|
|
Loading…
Reference in a new issue