Lua-API - GetClanColor: Verify argument and display error message on fail (instead of engine crash)
authorsheepluva
Wed, 27 Apr 2016 00:04:37 +0200
changeset 11741 e9481c5a130b
parent 11740 a6d097f23138
child 11742 571d06a86bb6
Lua-API - GetClanColor: Verify argument and display error message on fail (instead of engine crash)
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