# HG changeset patch # User sheepluva # Date 1461708277 -7200 # Node ID e9481c5a130b9a479b86ffe35b588e64a7fbbd68 # Parent a6d097f23138f18343cb6c5671990ec98060b45a Lua-API - GetClanColor: Verify argument and display error message on fail (instead of engine crash) diff -r a6d097f23138 -r e9481c5a130b hedgewars/uScript.pas --- a/hedgewars/uScript.pas Tue Apr 26 23:34:28 2016 +0200 +++ b/hedgewars/uScript.pas Wed Apr 27 00:04:37 2016 +0200 @@ -1210,9 +1210,24 @@ end; function lc_getclancolor(L : Plua_State) : LongInt; Cdecl; +var idx: integer; begin - if CheckLuaParamCount(L, 1, 'GetClanColor', 'clan') then - lua_pushinteger(L, ClansArray[lua_tointeger(L, 1)]^.Color shl 8 or $FF) + if CheckLuaParamCount(L, 1, 'GetClanColor', 'clanIdx') then + begin + idx:= lua_tointeger(L, 1); + if (not lua_isnumber(L, 1)) then + begin + LuaError('Argument ''clanIdx'' must be a number!'); + lua_pushnil(L); + end + else if (idx < 0) or (idx >= ClansCount) then + begin + LuaError('Argument ''clanIdx'' out of range! (There are currently ' + IntToStr(ClansCount) + ' clans, so valid range is: 0-' + IntToStr(ClansCount-1) + ')'); + lua_pushnil(L); + end + else + lua_pushinteger(L, ClansArray[idx]^.Color shl 8 or $FF); + end else lua_pushnil(L); // return value on stack (nil) lc_getclancolor:= 1