Add 2nd optional parameter to HedgewarsScriptLoad. If false, its OK if file does not exist. Also, return true/false on success
authorWuzzy <Wuzzy2@mail.ru>
Fri, 31 Aug 2018 21:34:33 +0200
changeset 13728 247d1bcf3c5e
parent 13727 eeeea1cfaf08
child 13729 69a913e064e3
Add 2nd optional parameter to HedgewarsScriptLoad. If false, its OK if file does not exist. Also, return true/false on success
ChangeLog.txt
hedgewars/uScript.pas
share/hedgewars/Data/Scripts/Locale.lua
--- a/ChangeLog.txt	Fri Aug 31 21:22:12 2018 +0200
+++ b/ChangeLog.txt	Fri Aug 31 21:34:33 2018 +0200
@@ -107,6 +107,8 @@
  + New call: GetLaserSight(): Returns true if laser sight (as utility) is currenctly active (ignoring sniper rifle)
  + New call: IsHogHidden(gear): Returns true if hog is hidden
  + Changed call: AddTeam: 2nd param. color: Accepts negative value to use a default clan color from player settings
+ + Changed call: HedgewarsScriptLoad: 2nd param. mustExist. If false, it's allowed for the script to not exist
+ + Changed call: HedgewarsScriptLoad: Return true on success and false on failure
  + Change callback: onGearResurrect: 2nd parameter for visual gear spawned at resurrect position (might be nil)
  + New parameter: SetAmmoTexts: 5th param. showExtra: Set to false to hide texts like “Not yet available”
  + New parameter: ShowMission: 6th param. forceDisplay: Set to true to prevent this particular mission panel to be hidden manually by player
--- a/hedgewars/uScript.pas	Fri Aug 31 21:22:12 2018 +0200
+++ b/hedgewars/uScript.pas	Fri Aug 31 21:34:33 2018 +0200
@@ -34,7 +34,7 @@
 procedure ScriptPrintStack;
 procedure ScriptClearStack;
 
-procedure ScriptLoad(name : shortstring; mustExist : boolean);
+function ScriptLoad(name : shortstring; mustExist : boolean): boolean;
 procedure ScriptOnPreviewInit;
 procedure ScriptOnGameInit;
 procedure ScriptOnScreenResize;
@@ -3066,12 +3066,20 @@
 
 
 function lc_hedgewarsscriptload(L : Plua_State) : LongInt; Cdecl;
+var success : boolean;
+    n : LongInt;
 begin
-    if CheckLuaParamCount(L, 1, 'HedgewarsScriptLoad', 'scriptPath') then
-        ScriptLoad(lua_tostring(L, 1), true)
+    if CheckAndFetchParamCount(L, 1, 2, 'HedgewarsScriptLoad', 'scriptPath [, mustExist]', n) then
+        begin
+        if n = 1 then
+            success:= ScriptLoad(lua_tostring(L, 1), true)
+        else
+            success:= ScriptLoad(lua_tostring(L, 1), lua_toboolean(L, 2));
+        end
     else
-        lua_pushnil(L);
-    lc_hedgewarsscriptload:= 0;
+        success:= false;
+    lua_pushboolean(L, success);
+    lc_hedgewarsscriptload:= 1;
 end;
 
 
@@ -3560,7 +3568,7 @@
 end;
 // ⭒⭐⭒✨⭐⭒✨⭐☆✨⭐✨✧✨☆✨✧✨☆⭒✨☆⭐⭒☆✧✨⭒✨⭐✧⭒☆⭒✧☆✨✧⭐☆✨☆✧⭒✨✧⭒☆⭐☆✧
 
-procedure ScriptLoad(name : shortstring; mustExist : boolean);
+function ScriptLoad(name : shortstring; mustExist : boolean): boolean;
 var ret : LongInt;
       s : shortstring;
       f : PFSFile;
@@ -3579,6 +3587,7 @@
         OutError('Script not found: ' + name, true)
     else
         AddFileLog('[LUA] Script not found: ' + name);
+    ScriptLoad:= false;
     exit;
     end;
 
@@ -3602,13 +3611,15 @@
     begin
     LuaError('Failed to load ' + name + '(error ' + IntToStr(ret) + ')');
     LuaError(lua_tostring(luaState, -1));
+    ScriptLoad:= false;
     end
 else
     begin
     WriteLnToConsole('Lua: ' + name + ' loaded');
     // call the script file
     lua_pcall(luaState, 0, 0, 0);
-    ScriptLoaded:= true
+    ScriptLoaded:= true;
+    ScriptLoad:= true;
     end;
 end;
 
--- a/share/hedgewars/Data/Scripts/Locale.lua	Fri Aug 31 21:22:12 2018 +0200
+++ b/share/hedgewars/Data/Scripts/Locale.lua	Fri Aug 31 21:34:33 2018 +0200
@@ -1,7 +1,7 @@
 -- Library for localizing strings in lua scripts
 
 if LOCALE ~= "en" then
-    HedgewarsScriptLoad("Locale/" .. tostring(LOCALE) .. ".lua")
+    HedgewarsScriptLoad("Locale/" .. tostring(LOCALE) .. ".lua", false)
 end
 
 function loc(text)