Skip to content

Commit 66c40e1

Browse files
bruno-dasilvaMearman
authored andcommitted
address review comments
1 parent 3d1ea7d commit 66c40e1

6 files changed

Lines changed: 44 additions & 47 deletions

File tree

rts/Lua/LuaConstEngine.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
* @field noHandicapForReclaim boolean Whether handicap is applied to income from reclaim
2929
* @field groupAddDoesntSelect boolean Whether 'group add' also selects the group (does both if false)
3030
* @field deadTeamsKeepUnitLimit boolean Whether engine redistributes dead team unitlimit to allies (false) or keeps it as-is (true)
31-
* @field hasChecksums boolean Whether the engine was built with SYNCCHECK (i.e. Spring.GetPrevFrameChecksum() returns a value)
3231
*/
3332

3433
/***
@@ -47,6 +46,7 @@
4746
* @field gameSpeed number Number of simulation gameframes per second
4847
* @field textColorCodes TextColorCode Table containing keys that represent the color code operations during font rendering
4948
* @field isHeadless boolean? Whether this is a headless engine build. Not available in synced
49+
* @field hasSyncChecksums boolean? Whether the engine was built with sync-check support (i.e. Spring.GetPrevFrameSyncChecksum() returns a meaningful value). Not available in synced
5050
*/
5151

5252
bool LuaConstEngine::PushEntries(lua_State* L)
@@ -60,8 +60,14 @@ bool LuaConstEngine::PushEntries(lua_State* L)
6060
LuaPushNamedString(L, "buildFlags" , SpringVersion::GetAdditional());
6161
LuaPushNamedNumber(L, "wordSize", (!CLuaHandle::GetHandleSynced(L))? Platform::NativeWordSize() * 8: 0);
6262

63-
if (!CLuaHandle::GetHandleSynced(L))
63+
if (!CLuaHandle::GetHandleSynced(L)) {
6464
LuaPushNamedBool(L, "isHeadless", SpringVersion::IsHeadless());
65+
#ifdef SYNCCHECK
66+
LuaPushNamedBool(L, "hasSyncChecksums", true);
67+
#else
68+
LuaPushNamedBool(L, "hasSyncChecksums", false);
69+
#endif
70+
}
6571

6672
LuaPushNamedNumber(L, "gameSpeed", GAME_SPEED);
6773
LuaPushNamedNumber(L, "maxCustomPaletteID", MAX_CUSTOM_COLORS - 1);
@@ -74,7 +80,7 @@ bool LuaConstEngine::PushEntries(lua_State* L)
7480
*
7581
* will be compatible even on engines that don't yet know about the entry at all. */
7682
lua_pushliteral(L, "FeatureSupport");
77-
lua_createtable(L, 0, 12);
83+
lua_createtable(L, 0, 11);
7884
LuaPushNamedBool(L, "NegativeGetUnitCurrentCommand", true);
7985
LuaPushNamedBool(L, "hasExitOnlyYardmaps", true);
8086
LuaPushNamedNumber(L, "rmlUiApiVersion", 1);
@@ -88,11 +94,6 @@ bool LuaConstEngine::PushEntries(lua_State* L)
8894
LuaPushNamedBool(L, "noHandicapForReclaim", true);
8995
LuaPushNamedBool(L, "groupAddDoesntSelect", true);
9096
LuaPushNamedBool(L, "deadTeamsKeepUnitLimit", false);
91-
#ifdef SYNCCHECK
92-
LuaPushNamedBool(L, "hasChecksums", true);
93-
#else
94-
LuaPushNamedBool(L, "hasChecksums", false);
95-
#endif
9697
lua_rawset(L, -3);
9798

9899
lua_pushliteral(L, "textColorCodes");

rts/Lua/LuaSyncedRead.cpp

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@
7979
#include "System/FileSystem/FileHandler.h"
8080
#include "System/FileSystem/FileSystem.h"
8181
#include "System/StringUtil.h"
82-
#include "System/Sync/SyncChecker.h"
8382

8483
#include <cctype>
8584
#include <functional>
@@ -122,7 +121,6 @@ bool LuaSyncedRead::PushEntries(lua_State* L)
122121

123122
REGISTER_LUA_CFUNC(GetGameFrame);
124123
REGISTER_LUA_CFUNC(GetGameSeconds);
125-
REGISTER_LUA_CFUNC(GetPrevFrameChecksum);
126124

127125
REGISTER_LUA_CFUNC(GetGameRulesParam);
128126
REGISTER_LUA_CFUNC(GetGameRulesParams);
@@ -933,39 +931,6 @@ int LuaSyncedRead::GetGameSeconds(lua_State* L)
933931
}
934932

935933

936-
/***
937-
*
938-
* Returns the engine's final sync checksum for the previous simulation
939-
* frame as an 8-character lowercase hex string (e.g. `"fade1eaf"`).
940-
* The value is stable for the entire duration of the current frame
941-
* (independent of gadget/callin ordering) and is identical across all
942-
* in-sync clients, so it can be folded into a deterministic per-run
943-
* artifact for sync testing (e.g. fightertest).
944-
*
945-
* Returned as a string rather than a number because Spring's Lua is
946-
* built with `LUA_NUMBER = float`, which cannot represent a 32-bit
947-
* checksum losslessly.
948-
*
949-
* Returns `nil` on builds without `SYNCCHECK`. Check
950-
* `Engine.FeatureSupport.hasChecksums` to detect availability.
951-
*
952-
* @function Spring.GetPrevFrameChecksum
953-
*
954-
* @return string|nil checksum
955-
*/
956-
int LuaSyncedRead::GetPrevFrameChecksum(lua_State* L)
957-
{
958-
#ifdef SYNCCHECK
959-
char buf[16];
960-
snprintf(buf, sizeof(buf), "%08x", CSyncChecker::GetPrevChecksum());
961-
lua_pushstring(L, buf);
962-
#else
963-
lua_pushnil(L);
964-
#endif
965-
return 1;
966-
}
967-
968-
969934
/******************************************************************************
970935
* Environment
971936
* @section environment

rts/Lua/LuaSyncedRead.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ class LuaSyncedRead {
4545

4646
static int GetGameFrame(lua_State* L);
4747
static int GetGameSeconds(lua_State* L);
48-
static int GetPrevFrameChecksum(lua_State* L);
4948

5049
static int GetGameRulesParam(lua_State* L);
5150
static int GetGameRulesParams(lua_State* L);

rts/Lua/LuaUnsyncedRead.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
#include "System/Sound/ISound.h"
7676
#include "System/Sound/ISoundChannels.h"
7777
#include "System/StringUtil.h"
78+
#include "System/Sync/SyncChecker.h"
7879
#include "System/Misc/SpringTime.h"
7980
#include "System/ScopedResource.h"
8081
#include "System/Math/NURBS.h"
@@ -120,6 +121,7 @@ bool LuaUnsyncedRead::PushEntries(lua_State* L)
120121
REGISTER_LUA_CFUNC(GetGameSecondsInterpolated);
121122
REGISTER_LUA_CFUNC(GetLastUpdateSeconds);
122123
REGISTER_LUA_CFUNC(GetVideoCapturingMode);
124+
REGISTER_LUA_CFUNC(GetPrevFrameSyncChecksum);
123125

124126
REGISTER_LUA_CFUNC(GetNumDisplays);
125127
REGISTER_LUA_CFUNC(GetViewGeometry);
@@ -1236,6 +1238,35 @@ int LuaUnsyncedRead::GetVideoCapturingMode(lua_State* L)
12361238
}
12371239

12381240

1241+
/***
1242+
*
1243+
* Returns the engine's sync checksum for the previous simframe,
1244+
* useful for testing. The returned string is NOT convertible to
1245+
* a number within Lua.
1246+
*
1247+
* Returns a dummy value if `Engine.hasSyncChecksums` is false,
1248+
* or if no frames were processed yet.
1249+
*
1250+
* @function Spring.GetPrevFrameSyncChecksum
1251+
*
1252+
* @return string checksum
1253+
*/
1254+
int LuaUnsyncedRead::GetPrevFrameSyncChecksum(lua_State* L)
1255+
{
1256+
#ifdef SYNCCHECK
1257+
unsigned checksum = CSyncChecker::GetPrevChecksum();
1258+
#else
1259+
unsigned checksum = 0;
1260+
#endif
1261+
1262+
char buf[9];
1263+
snprintf(buf, sizeof(buf), "%08x", checksum);
1264+
lua_pushstring(L, buf);
1265+
1266+
return 1;
1267+
}
1268+
1269+
12391270
/******************************************************************************
12401271
* Unit attributes
12411272
* @section unitattributes

rts/Lua/LuaUnsyncedRead.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class LuaUnsyncedRead {
3232
static int GetGameSecondsInterpolated(lua_State* L);
3333
static int GetLastUpdateSeconds(lua_State* L);
3434
static int GetVideoCapturingMode(lua_State* L);
35+
static int GetPrevFrameSyncChecksum(lua_State* L);
3536

3637
static int GetNumDisplays(lua_State* L);
3738
static int GetViewGeometry(lua_State* L);

rts/Net/NetCommands.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -623,9 +623,9 @@ void CGame::ClientReadNet()
623623
ASSERT_SYNCED(CSyncChecker::GetChecksum());
624624
clientNet->Send(CBaseNetProtocol::Get().SendSyncResponse(gu->myPlayerNum, gs->frameNum, CSyncChecker::GetChecksum()));
625625

626-
// Cache the just-sent, frame-closed checksum so Lua gadgets
627-
// running in the NEXT sim frame can read a stable value via
628-
// Spring.GetPrevFrameChecksum(). Must happen before the
626+
// Cache the just-closed frame's checksum so Lua can read a
627+
// stable value during the next sim frame via
628+
// Spring.GetPrevFrameSyncChecksum(). Must run before the
629629
// 4096-frame reset below so we capture the pre-reset value.
630630
CSyncChecker::SetPrevChecksum(CSyncChecker::GetChecksum());
631631

0 commit comments

Comments
 (0)