Zero Hour rebuilt from EA's source: x64, Direct3D 11, VS2022/CMake, ~60 original bugs fixed, an AI that builds a base. Bring your own game.
C++ cmakecnccommand-and-conquercppdirectx11game-enginegame-preservationgeneralsrtssage-enginex64zero-hour
fix(gameengine): cap the debug log, network traces only in debug builds
Olcay Seygan27934c47e0d8
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 87da0c645..123bb168e 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -268,6 +268,7 @@ found and fixed — EA's own, not port damage.**- Every moving unit's skeleton was worked out three times a picture, once for its shadow, once for the scene and once more for the outline drawn when it stands behind a building. It is worked out once now, and late in the same match the animation costs 60% less. With the rules step finding the next unit due for an update a little faster, minutes 24 to 26 went from 119.8 to 123.7 frames a second, and the match still ends on the same checksum.- Late in a match the picture caught less often. Every ten seconds each computer player looked over its economy, its spending and the enemy army all in one step, 8 to 10ms of work landing on a single picture. Those three now run a fifth of a second apart. With four Hard computer players on Twilight Flame at minutes 24 and 25, pictures slower than 16.7ms went from 29 a minute to 22, and the slowest one in a hundred from 14.7ms to 14.0ms. Two more hitches got smaller in the same match. The map of where units can drive is rebuilt whenever a building goes up or comes down, and that rebuild went from 9.9ms to 7.4ms, with the ones over 8ms halved (90 to 43). A Hard computer steering its Particle Cannon beam now weighs the ground around it in 0.9ms instead of 2.6ms. Neither changes a single move: the match ends on the same checksum.- The game no longer freezes for seconds when your hard drive is busy. It writes a log of the match as it plays, and once a second the battle itself stopped to push that log to disk. On a drive already busy with something else, Windows can hold such a write back for seconds, and the whole game stood still behind it: a 2v2 against the computer froze for 7.8 seconds on a single step. The log is written on the side now and the battle never waits for it. Three matches played while another program kept the drive busy froze for 6.2 seconds in two of them before; with the change, the slowest step in all three took 48ms.+- The game's log no longer grows without limit. Players sent us logs of half a gigabyte up to 19 GB, enough to fill a disk, and the launcher could not open them to report a crash. Most of it was the network code writing a line for every order and every frame, which only a developer's build writes now: a 100-second two-player network match went from 10.9 MB of log to 175 KB, and a skirmish from 600 KB to 166 KB. Past 64 MB the log keeps its first 8 MB, which say how the game started and on what machine, and its most recent lines, so it stays under 64 MB however long you play.- Every animation runs on a clock instead of counting frames.- Tanks and trucks move the same at any frame rate. Their treads scrolled a fixed step every picture, so at 120 frames a second they ran four times too fast, and a tank turning on the spot rolled both treads forward as if it were driving. Each tread now covers exactly the ground under it. In a tight turn the two run against each other, and backing up runs them backwards. Wheels spin with the distance driven the same way. The hull's springs used to settle four times too fast as well, and every start or stop threw the chassis to its full tilt in a single picture. Now a tank squats as it pulls away and dips its nose when it brakes, easing in over a tenth of a second and settling within half a second. In a corner it leans out, a Crusader up to two and a half degrees. The Humvee, which never moved on its springs at all, rocks too. A run drawing one picture per game tick and one drawing fourteen put every tank in the same pose, to the last digit, for as long as neither skipped a tick.- A scroll that starts after the camera has stood still no longer lurches. The first frame of the move was timed from the last time the camera moved, so after a few seconds of looking at one spot it was worth three frames of scrolling at once. The clock runs while the camera is still now, and the first frame moves one frame's worth.diff --git a/GeneralsMD/Code/GameEngine/Include/Common/Debug.h b/GeneralsMD/Code/GameEngine/Include/Common/Debug.hindex c097a7534..f91243147 100644--- a/GeneralsMD/Code/GameEngine/Include/Common/Debug.h+++ b/GeneralsMD/Code/GameEngine/Include/Common/Debug.h@@ -182,6 +182,12 @@ class AsciiString;#define DEBUG_LOG_DEV(m) ((void)0)#endif+/// The debug log's cap (Debug.cpp): cut the open, flushed log at path down to its first headEnd+/// bytes, a line saying what went, and the whole lines in its last keep bytes; it stays open at+/// its new end, which is returned. Here rather than static so test_gameengine can call it.+#include <stdio.h>+DEBUG_EXTERN_C long DebugCutLogFile(FILE *file, const char *path, long headEnd, long keep);+#ifdef DEBUG_CRASHINGDEBUG_EXTERN_C void DebugCrash(const char *format, ...);diff --git a/GeneralsMD/Code/GameEngine/Source/Common/System/Debug.cpp b/GeneralsMD/Code/GameEngine/Source/Common/System/Debug.cppindex 32a15ace5..11d09d37b 100644--- a/GeneralsMD/Code/GameEngine/Source/Common/System/Debug.cpp+++ b/GeneralsMD/Code/GameEngine/Source/Common/System/Debug.cpp@@ -87,8 +87,11 @@ extern HWND ApplicationHWnd;#include <condition_variable>#include <mutex>#include <new>-#if !defined(_WIN32)+#if defined(_WIN32)+#include <io.h> // _chsize_s, DebugCutLogFile+#else#include <pthread.h>+#include <unistd.h> // ftruncate, DebugCutLogFile#endifextern char *gAppPrefix; /// So WB can have a different log file name.@@ -124,9 +127,59 @@ extern char *gAppPrefix; /// So WB can have a different log file name.// ----------------------------------------------------------------------------// PRIVATE DATA// ----------------------------------------------------------------------------+long DebugCutLogFile(FILE *file, const char *path, long headEnd, long keep)+{+ fflush(file);+ const long size = ftell(file);+ // A second handle reads the tail: the log is opened "w", and in text mode on Windows.+ FILE *reader = zh_fopen(path, "rb");+ if (reader == NULL)+ return size; // something else holds the file shut; it grows until the next try+ char *tail = (char *)malloc(keep);+ fseek(reader, size - keep, SEEK_SET);+ const size_t got = fread(tail, 1, keep, reader);+ fclose(reader);++ // From the first whole line, with each \r\n back to \n, which the text-mode write expands again.+ const char *newline = (const char *)memchr(tail, '\n', got);+ const size_t start = (newline != NULL) ? (size_t)(newline - tail) + 1 : got;+ size_t length = 0;+ for (size_t i = start; i < got; ++i)+ {+ if (tail[i] == '\r' && i + 1 < got && tail[i + 1] == '\n')+ continue;+ tail[length++] = tail[i];+ }++ fseek(file, headEnd, SEEK_SET);+ fprintf(file, "\n==== The log passed %ld MB and %ld bytes of it were cut here: the start of the run is above, its latest lines below ====\n\n",+ size / (1024 * 1024), size - keep + (long)start - headEnd);+ fwrite(tail, 1, length, file);+ free(tail);+ fflush(file);+ const long end = ftell(file);+#if defined(_WIN32)+ _chsize_s(_fileno(file), end);+#else+ ftruncate(fileno(file), end);+#endif+ return end;+}+#ifdef DEBUG_LOGGINGstatic FILE *theLogFile = NULL;+/* The log has a cap. It used to grow for as long as the game ran, and players sent reports from+ logs of 0.5 to 19 GB, which fill a disk and which the launcher could not read. Past+ LOG_CAP_BYTES the writer cuts it back to its first LOG_HEAD_BYTES (the launch: versions,+ hardware, settings, the first load) and its latest LOG_KEEP_BYTES, in place, so the name, the+ open file and the tail the launcher and a crash report read all stay as they were. */+static const long LOG_CAP_BYTES = 64 * 1024 * 1024;+static const long LOG_HEAD_BYTES = 8 * 1024 * 1024;+static const long LOG_KEEP_BYTES = 24 * 1024 * 1024;+static char theLogPath[ _MAX_PATH ];+static long theLogHeadEnd = 0; // where the head ends: the end of the batch that took the file past LOG_HEAD_BYTES+/* The log file is written by a thread of its own, never by the thread that logs. Flushing once asecond from the logging thread still put a disk write inside whichever logic frame came due, andD: here is a spinning disk: with other copies of the game, a build and a video dump writing to it,@@ -191,6 +244,11 @@ static void flushLogQueue(void)return;fwrite(batch, 1, batchUsed, theLogFile);fflush(theLogFile);+ const long size = ftell(theLogFile);+ if (theLogHeadEnd == 0 && size >= LOG_HEAD_BYTES)+ theLogHeadEnd = size;+ if (size > LOG_CAP_BYTES)+ DebugCutLogFile(theLogFile, theLogPath, theLogHeadEnd, LOG_KEEP_BYTES);}static void logWriterMain(void)@@ -652,6 +710,7 @@ void DebugInit(int flags)const int rotated = zh_rename(curbuf, prevbuf);theLogFile = zh_fopen(curbuf, "w");+ strlcpy(theLogPath, curbuf, ARRAY_SIZE(theLogPath));#if !defined(_WIN32)// The crash handler writes its report into the log too, and may only use the descriptor.setCrashLogDescriptor(theLogFile != NULL ? fileno(theLogFile) : -1);diff --git a/GeneralsMD/Code/GameEngine/Source/GameNetwork/ConnectionManager.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/ConnectionManager.cppindex e994befdf..3e2a7a016 100644--- a/GeneralsMD/Code/GameEngine/Source/GameNetwork/ConnectionManager.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/ConnectionManager.cpp@@ -1203,7 +1203,10 @@ void ConnectionManager::sendRemoteCommand(NetCommandRef *msg) {return;}- DEBUG_LOG(("ConnectionManager::sendRemoteCommand - sending net command %d of type %s from player %d, relay is 0x%x\n",+ /* The per-command and per-frame lines of the network code are DEBUG_LOG_DEV: in a Release log+ they were nearly all of it, 60 MB in a 100 s three-player match, and most of the 0.5-19 GB logs+ players sent. The Debug build still writes them. */+ DEBUG_LOG_DEV(("ConnectionManager::sendRemoteCommand - sending net command %d of type %s from player %d, relay is 0x%x\n",msg->getCommand()->getID(), GetAsciiNetCommandType(msg->getCommand()->getNetCommandType()).str(), msg->getCommand()->getPlayerID(), msg->getRelay()));UnsignedByte relay = msg->getRelay();@@ -1212,14 +1215,14 @@ void ConnectionManager::sendRemoteCommand(NetCommandRef *msg) {FrameDataManager *senderFrameData = (senderID < MAX_SLOTS) ? m_frameData[senderID] : NULL;if ((relay & (1 << m_localSlot)) && (senderFrameData != NULL)) {if (IsCommandSynchronized(msg->getCommand()->getNetCommandType())) {- DEBUG_LOG(("ConnectionManager::sendRemoteCommand - adding net command of type %s to player %d for frame %d\n", GetAsciiNetCommandType(msg->getCommand()->getNetCommandType()).str(), msg->getCommand()->getPlayerID(), msg->getCommand()->getExecutionFrame()));+ DEBUG_LOG_DEV(("ConnectionManager::sendRemoteCommand - adding net command of type %s to player %d for frame %d\n", GetAsciiNetCommandType(msg->getCommand()->getNetCommandType()).str(), msg->getCommand()->getPlayerID(), msg->getCommand()->getExecutionFrame()));senderFrameData->addNetCommandMsg(msg->getCommand());}}for (Int i = 0; i < MAX_SLOTS; ++i) {if ((relay & (1 << i)) && ((m_connections[i] != NULL) && (m_connections[i]->isQuitting() == FALSE))) {- DEBUG_LOG(("ConnectionManager::sendRemoteCommand - relaying command %d to player %d\n", msg->getCommand()->getID(), i));+ DEBUG_LOG_DEV(("ConnectionManager::sendRemoteCommand - relaying command %d to player %d\n", msg->getCommand()->getID(), i));m_connections[i]->sendNetCommandMsg(msg->getCommand(), 1 << i);actualRelay = actualRelay | (1 << i);}@@ -1519,7 +1522,7 @@ void ConnectionManager::processFrameTick(UnsignedInt frame) {if ((Int)frame > m_lastFrameTickSent)m_lastFrameTickSent = (Int)frame;- DEBUG_LOG(("ConnectionManager::processFrameTick - sending frame info for frame %d, ID %d, command count %d\n", frame, msg->getID(), commandCount));+ DEBUG_LOG_DEV(("ConnectionManager::processFrameTick - sending frame info for frame %d, ID %d, command count %d\n", frame, msg->getID(), commandCount));sendLocalCommand(msg, 0xff & ~(1 << m_localSlot));@@ -1582,11 +1585,11 @@ void ConnectionManager::sendLocalCommand(NetCommandMsg *msg, UnsignedByte relay}msg->attach();- DEBUG_LOG(("ConnectionManager::sendLocalCommand - sending net command %d of type %s\n", msg->getID(),+ DEBUG_LOG_DEV(("ConnectionManager::sendLocalCommand - sending net command %d of type %s\n", msg->getID(),GetAsciiNetCommandType(msg->getNetCommandType()).str()));if (relay & (1 << m_localSlot)) {- DEBUG_LOG(("ConnectionManager::sendLocalCommand - adding net command of type %s to player %d for frame %d\n", GetAsciiNetCommandType(msg->getNetCommandType()).str(), msg->getPlayerID(), msg->getExecutionFrame()));+ DEBUG_LOG_DEV(("ConnectionManager::sendLocalCommand - adding net command of type %s to player %d for frame %d\n", GetAsciiNetCommandType(msg->getNetCommandType()).str(), msg->getPlayerID(), msg->getExecutionFrame()));m_frameData[m_localSlot]->addNetCommandMsg(msg);}@@ -1630,7 +1633,7 @@ void ConnectionManager::sendLocalCommandDirect(NetCommandMsg *msg, UnsignedByteif (((relay & (1 << m_localSlot)) != 0) && (m_frameData[m_localSlot] != NULL)) {if (IsCommandSynchronized(msg->getNetCommandType()) == TRUE) {- DEBUG_LOG(("ConnectionManager::sendLocalCommandDirect - adding net command of type %s to player %d for frame %d\n", GetAsciiNetCommandType(msg->getNetCommandType()).str(), msg->getPlayerID(), msg->getExecutionFrame()));+ DEBUG_LOG_DEV(("ConnectionManager::sendLocalCommandDirect - adding net command of type %s to player %d for frame %d\n", GetAsciiNetCommandType(msg->getNetCommandType()).str(), msg->getPlayerID(), msg->getExecutionFrame()));m_frameData[m_localSlot]->addNetCommandMsg(msg);}}@@ -1640,7 +1643,7 @@ void ConnectionManager::sendLocalCommandDirect(NetCommandMsg *msg, UnsignedByteif ((m_connections[i] != NULL) && (m_connections[i]->isQuitting() == FALSE)) {UnsignedByte temprelay = 1 << i;m_connections[i]->sendNetCommandMsg(msg, temprelay);- DEBUG_LOG(("ConnectionManager::sendLocalCommandDirect - Sending direct command %d of type %s to player %d\n", msg->getID(), GetAsciiNetCommandType(msg->getNetCommandType()).str(), i));+ DEBUG_LOG_DEV(("ConnectionManager::sendLocalCommandDirect - Sending direct command %d of type %s to player %d\n", msg->getID(), GetAsciiNetCommandType(msg->getNetCommandType()).str(), i));}}}@@ -1782,7 +1785,7 @@ NetCommandList *ConnectionManager::getFrameCommandList(UnsignedInt frame)m_frameData[i]->resetFrame(frame - FRAMES_TO_KEEP); // After getting the commands for that frame from this// FrameDataManager object, we need to tell it that we're// done with the messages for that frame.- DEBUG_LOG(("getFrameCommandList - called reset frame on player %d for frame %d\n", i, frame - FRAMES_TO_KEEP));+ DEBUG_LOG_DEV(("getFrameCommandList - called reset frame on player %d for frame %d\n", i, frame - FRAMES_TO_KEEP));}}}diff --git a/GeneralsMD/Code/GameEngine/Source/GameNetwork/FrameDataManager.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/FrameDataManager.cppindex b807f7e9c..20e726995 100644--- a/GeneralsMD/Code/GameEngine/Source/GameNetwork/FrameDataManager.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/FrameDataManager.cpp@@ -90,7 +90,7 @@ void FrameDataManager::update() {void FrameDataManager::addNetCommandMsg(NetCommandMsg *msg) {UnsignedInt frame = msg->getExecutionFrame();UnsignedInt frameindex = frame % FRAME_DATA_LENGTH;- DEBUG_LOG(("FrameDataManager::addNetCommandMsg - about to add a command of type %s for frame %d, frame index %d\n", GetAsciiNetCommandType(msg->getNetCommandType()).str(), frame, frameindex));+ DEBUG_LOG_DEV(("FrameDataManager::addNetCommandMsg - about to add a command of type %s for frame %d, frame index %d\n", GetAsciiNetCommandType(msg->getNetCommandType()).str(), frame, frameindex));m_frameData[frameindex].addCommand(msg);if (m_isLocal) {diff --git a/GeneralsMD/Code/GameEngine/Source/GameNetwork/NetPacket.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/NetPacket.cppindex b1edc5781..d1ad88ab5 100644--- a/GeneralsMD/Code/GameEngine/Source/GameNetwork/NetPacket.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/NetPacket.cpp@@ -5111,7 +5111,7 @@ NetCommandList * NetPacket::getCommandList() {case NETCOMMANDTYPE_FRAMEINFO:msg = readFrameMessage(m_packet, i);// frameinfodebug- DEBUG_LOG(("read frame %d from player %d, command count = %d, relay = 0x%X\n", frame, playerID, ((NetFrameCommandMsg *)msg)->getCommandCount(), relay));+ DEBUG_LOG_DEV(("read frame %d from player %d, command count = %d, relay = 0x%X\n", frame, playerID, ((NetFrameCommandMsg *)msg)->getCommandCount(), relay));break;case NETCOMMANDTYPE_PLAYERLEAVE:msg = readPlayerLeaveMessage(m_packet, i);diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11texture.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11texture.cppindex 0a8a05436..432f95542 100644--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11texture.cpp+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11texture.cpp@@ -244,6 +244,12 @@ static bool upload_levels(ID3D11DeviceContext * context, ID3D11Texture2D * destistatic void note_average_colour(IDirect3DTexture9 * texture, const D3DSURFACE_DESC & description,unsigned level_count){+ // A 64x64 16-bit texture is mirrored again some 150 times a second for as long as a match runs,+ // and every one used to add a note: 15,000 in a 100 s network match, all of them in the debug log+ // at shutdown. The first 64 answer the question.+ if (Notes.size() >= 64) {+ return;+ }D3DLOCKED_RECT locked;if (FAILED(texture->LockRect(0, &locked, NULL, D3DLOCK_READONLY))) {return;diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cppindex e34ad0089..98c31aa0c 100644--- a/GeneralsMD/Code/Tests/test_gameengine.cpp+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp@@ -18123,3 +18123,41 @@ TEST(ai_persona_rides_the_slot_entry_and_normal_leaves_it_unchanged)delete TheWritableGlobalData;TheWritableGlobalData = saved;}++// The debug log's cap: the head and the last whole lines survive, in one file still open for writing.+TEST(debug_log_cut_keeps_the_head_and_the_last_whole_lines)+{+ const std::string path = (std::filesystem::temp_directory_path() / "zh_logcut_test.txt").string();+ FILE *log = fopen( path.c_str(), "w" ); // text mode, as the game opens it+ fputs( "head one\nhead two\n", log );+ fflush( log );+ const long headEnd = ftell( log );+ char line[ 32 ];+ for( Int i = 0; i < 1000; ++i )+ {+ snprintf( line, sizeof( line ), "line %04d\n", i );+ fputs( line, log );+ }+ const long before = ftell( log );+ const long after = DebugCutLogFile( log, path.c_str(), headEnd, 100 );+ fputs( "written after the cut\n", log );+ fclose( log );++ FILE *in = fopen( path.c_str(), "r" );+ std::string text;+ char buffer[ 4096 ];+ size_t got;+ while( (got = fread( buffer, 1, sizeof( buffer ), in )) > 0 )+ text.append( buffer, got );+ fclose( in );+ std::filesystem::remove( path );++ CHECK( after < before );+ CHECK( text.compare( 0, 18, "head one\nhead two\n" ) == 0 );+ CHECK( text.find( "cut here" ) != std::string::npos );+ CHECK( text.find( "line 0989" ) == std::string::npos );+ CHECK( text.find( "\nline 0991\n" ) != std::string::npos ); // a whole line, never half of one+ CHECK( text.find( '\r' ) == std::string::npos ); // no \r\r\n from the text-mode rewrite+ const std::string ending = "line 0999\nwritten after the cut\n";+ CHECK( text.compare( text.size() - ending.size(), ending.size(), ending ) == 0 );+}