Check argument sanity in w_ParticleSystem_setColors.

This commit is contained in:
vrld
2012-01-19 14:59:36 +01:00
parent 1a9eafb76a
commit 3904e49c75
@@ -204,27 +204,32 @@ namespace opengl
int w_ParticleSystem_setColors(lua_State * L)
{
ParticleSystem * t = luax_checkparticlesystem(L, 1);
size_t nColors = (lua_gettop(L) - 1) / 4;
int cargs = lua_gettop(L) - 1;
size_t nColors = (cargs + 3) / 4; // nColors = ceil(color_args / 4)
if (cargs % 4 != 0 || cargs == 0)
return luaL_error(L, "Expected red, green, blue, and alpha. Only got %d of 4 components.", cargs % 4);
if (nColors > 8)
return luaL_error(L, "At most eight (8) colors may be used.");
if (nColors == 1)
{
t->setColor(Color(luaL_checkint(L,2),
luaL_checkint(L,3),
luaL_checkint(L,4),
luaL_checkint(L,5)));
int r = luaL_checkint(L, 2);
int g = luaL_checkint(L, 3);
int b = luaL_checkint(L, 4);
int a = luaL_checkint(L, 5);
t->setColor(Color(r,g,b,a));
}
else
{
std::vector<Color> colors(nColors);
for (size_t i = 0; i < nColors; ++i)
{
colors[i] = Color(luaL_checkint(L, 1 + i*4 + 1),
luaL_checkint(L, 1 + i*4 + 2),
luaL_checkint(L, 1 + i*4 + 3),
luaL_checkint(L, 1 + i*4 + 4));
int r = luaL_checkint(L, 1 + i*4 + 1);
int g = luaL_checkint(L, 1 + i*4 + 2);
int b = luaL_checkint(L, 1 + i*4 + 3);
int a = luaL_checkint(L, 1 + i*4 + 4);
colors[i] = Color(r,g,b,a);
}
t->setColor(colors);
}