Add deprecation warning for setting TurnTimeLeft/ReadyTimeLeft directly, fix SetTurnTimeLeft/SetReadyTimeLeft not setting vars reliably
authorWuzzy <Wuzzy2@mail.ru>
Sun, 02 Sep 2018 22:02:15 +0200
changeset 13741 e8f26bc793d9
parent 13740 2bb7141496a9
child 13742 cb2857f55130
Add deprecation warning for setting TurnTimeLeft/ReadyTimeLeft directly, fix SetTurnTimeLeft/SetReadyTimeLeft not setting vars reliably
ChangeLog.txt
hedgewars/uScript.pas
--- a/ChangeLog.txt	Sun Sep 02 21:26:44 2018 +0200
+++ b/ChangeLog.txt	Sun Sep 02 22:02:15 2018 +0200
@@ -104,6 +104,9 @@
  * Fix many server messages being not translated
 
 Lua API:
+ * Deprecation: Setting TurnTimeLeft/ReadyTimeLeft directly is deprecated and will become useless in future. Use the setter functions below
+ + New call: SetTurnTimeLeft(TurnTimeLeft): Set remaining turn time
+ + New call: SetReadyTimeLeft(ReadyTimeLeft): Set remaining ready time
  + New call: Retreat(time [, respectGetAwayTimeFactor): Force current turn into retreating mode
  + New call: GetAmmoTimer(gearUid, ammoType): Returns current set timer for given ammoType and hog gear in ms. Returns nil for non-timerable ammo
  + New call: EnableSwitchHog(): Enable hog switching
@@ -111,9 +114,6 @@
  + New call: GetVampiric(): Returns true if vampirism is currently active
  + 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
- + New call: SetTurnTimeLeft(TurnTimeLeft): Set remaining turn time
- + New call: SetReadyTimeLeft(ReadyTimeLeft): Set remaining ready time
- * Deprecation: Setting TurnTimeLeft/ReadyTimeLeft directly is now deprecated and will become useless in a future version. Use the setter functions instead
  + 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
--- a/hedgewars/uScript.pas	Sun Sep 02 21:26:44 2018 +0200
+++ b/hedgewars/uScript.pas	Sun Sep 02 22:02:15 2018 +0200
@@ -102,6 +102,8 @@
     mapDims : boolean;
     PointsBuffer: shortstring;
     PrevCursorX, PrevCursorY: LongInt;
+    PendingTurnTimeLeft, PendingReadyTimeLeft: LongWord;
+    isPendingTurnTimeLeft, isPendingReadyTimeLeft: boolean;
 
 {$IFDEF USE_LUA_SCRIPT}
 procedure ScriptPrepareAmmoStore; forward;
@@ -136,26 +138,40 @@
     exit(true);
 end;
 
-procedure LuaError(s: shortstring);
-var src: shortstring;
+procedure LuaErrorOrWarning(s: shortstring; isWarning: boolean);
+var src, intro: shortstring;
 const
     maxsrclen = 20;
 begin
+    if isWarning then
+        intro:= 'LUA WARNING'
+    else
+        intro:= 'LUA ERROR';
     if LuaUpdateDebugInfo() then
         begin
         src:= StrPas(LuaDebugInfo.source);
-        s:= 'LUA ERROR [ ... '
+        s:= intro + ': [ ... '
             + copy(src, Length(src) - maxsrclen, maxsrclen - 3) + ':'
             + inttostr(LuaDebugInfo.currentLine) + ']: ' + s;
         end
     else
-        s:= 'LUA ERROR: ' + s;
+        s:= intro + ': ' + s;
     WriteLnToConsole(s);
     AddChatString(#5 + s);
-    if cTestLua then
+    if cTestLua and (not isWarning) then
         halt(HaltTestLuaError);
 end;
 
+procedure LuaError(s: shortstring);
+begin
+    LuaErrorOrWarning(s, false);
+end;
+
+procedure LuaWarning(s: shortstring);
+begin
+    LuaErrorOrWarning(s, true);
+end;
+
 procedure LuaCallError(error, call, paramsyntax: shortstring);
 begin
     LuaError(call + ': ' + error);
@@ -3187,16 +3203,36 @@
 end;
 
 function lc_setturntimeleft(L : Plua_State) : LongInt; Cdecl;
+var number: Int64;
 begin
     if CheckLuaParamCount(L, 1, 'SetTurnTimeLeft', 'TurnTimeLeft') then
-        TurnTimeLeft:= Trunc(lua_tonumber(L, 1));
+        begin
+        number:= Trunc(lua_tonumber(L, 1));
+        if number < 0 then
+            number:= 0;
+        if number > cMaxTurnTime then
+            number:= cMaxTurnTime;
+        // The real TurnTimeLeft will be set in SetGlobals
+        PendingTurnTimeLeft:= number;
+        isPendingTurnTimeLeft:= true;
+        end;
     lc_setturntimeleft:= 0;
 end;
 
 function lc_setreadytimeleft(L : Plua_State) : LongInt; Cdecl;
+var number: Int64;
 begin
     if CheckLuaParamCount(L, 1, 'SetReadyTimeLeft', 'ReadyTimeLeft') then
-        ReadyTimeLeft:= Trunc(lua_tonumber(L, 1));
+        begin
+        number:= Trunc(lua_tonumber(L, 1));
+        if number < 0 then
+            number:= 0;
+        if number > cMaxTurnTime then
+            number:= cMaxTurnTime;
+        // The real ReadyTimeLeft will be set in SetGlobals
+        PendingReadyTimeLeft:= number;
+        isPendingReadyTimeLeft:= true;
+        end;
     lc_setreadytimeleft:= 0;
 end;
 
@@ -3682,14 +3718,41 @@
 end;
 
 procedure GetGlobals;
+var currentTTL, currentRTL, newTTL, newRTL: LongInt;
 begin
-// This function is deprecated.
-// TODO: Remove this function.
-// Setting TurnTimeLeft and ReadyTimeLeft is now done in the setter functions
+// Setting TurnTimeLeft and ReadyTimeLeft should now be done in the setter functions.
 // SetTurnTimeLeft and SetReadTimeLeft.
 // GetGloals should be removed in a future release.
-TurnTimeLeft:= ScriptGetInteger('TurnTimeLeft');
-ReadyTimeLeft:= ScriptGetInteger('ReadyTimeLeft');
+
+// DEPRECATED: Read TurnTimeLeft and ReadyTimeLeft from script directly.
+// TODO: Remove this behaviour in a future version.
+currentTTL:= TurnTimeLeft;
+currentRTL:= ReadyTimeLeft;
+newTTL:= ScriptGetInteger('TurnTimeLeft');
+newRTL:= ScriptGetInteger('ReadyTimeLeft');
+if currentTTL <> newTTL then
+    begin
+    TurnTimeLeft:= newTTL;
+    LuaWarning('Writing to TurnTimeLeft directly is deprecated! Use SetTurnTimeLeft instead!');
+    end;
+
+if currentRTL <> newRTL then
+    begin
+    ReadyTimeLeft:= newRTL;
+    LuaWarning('Writing to ReadyTimeLeft directly is deprecated! Use SetReadyTimeLeft instead!');
+    end;
+
+// Set TurnTimeLeft and ReadyTimeLeft if activated by SetTurnTimeLeft and SetReadyTimeLeft before
+if isPendingTurnTimeLeft then
+    begin
+    TurnTimeLeft:= PendingTurnTimeLeft;
+    isPendingTurnTimeLeft:= false;
+    end;
+if isPendingReadyTimeLeft then
+    begin
+    ReadyTimeLeft:= PendingReadyTimeLeft;
+    isPendingReadyTimeLeft:= false;
+    end;
 end;
 
 procedure ScriptCall(fname : shortstring);
@@ -4316,6 +4379,8 @@
 PointsBuffer:= '';
 PrevCursorX:= NoPointX;
 PrevCursorY:= NoPointX;
+isPendingTurnTimeLeft:= false;
+isPendingReadyTimeLeft:= false;
 end;
 
 procedure freeModule;