Skip to content

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.

fix(gameengine): start when Documents refuses writes

Olcay Seyganf6f456caf38f
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 58d6effd6..8780f2dbe 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -834,7 +834,7 @@ The whole HUD is on the hardware. Four colours carry it, and they mean the same
a shadow back in. The game drew every shadow as one hard silhouette before this, whether it came
off a tank's hull or off something twenty metres up, and edges that sharp are the tell that a
picture was computed rather than photographed. This is what the game draws with now; a machine
- that cannot run the newer picture keeps the old shadows on its own, and `-noshadowmap` puts them
+ that cannot run the newer picture keeps the old shadows on its own, and Classic Graphics puts them
back on any machine.
- A hovering helicopter has its shadow back, rotor blades and all. The softening spread a
Comanche's thin body so wide that nothing of it was left on the ground, and the spinning blades
@@ -937,6 +937,8 @@ The whole HUD is on the hardware. Four colours carry it, and they mean the same
- A unit can no longer load into a vehicle that no longer exists.
- Selling or losing an airfield under a jet on its takeoff roll no longer takes the game down. The jet takes off.
- A looping sound coming round for its next pass no longer takes the game down. v2.0.0 lifted unit replies over the battle, and the check for whether a sound is a reply read a part of the sound that a looping one can have lost by then. A player's skirmish ended on "Technical Difficulties" twelve hundred frames in. A sound with nothing to say about itself now plays at the effects volume.
+- The game starts on a machine that will not let it write to Documents. Windows' ransomware protection refuses a program it does not know yet, and a new 64-bit generals.exe is exactly that, so for some players v2.0.0 showed "Technical Difficulties" five seconds after every start: the first file the game wrote there was a leftover debugging file EA's code saved at the main menu, and when that failed nothing else could be written either, the crash report included. The debugging file is gone, and when Documents refuses the game, it keeps its settings, replays and saves in the same folder under your local application data instead.
+- A network game with a computer player no longer falls apart because two players' processors do the maths slightly differently. v2.0.0 is the first 64-bit build, and the 64-bit Windows runtime picks between two versions of a logarithm depending on the processor, one bit apart. The computer player's unit choice used one, so two machines could see it buy different units. Every machine uses the same one now.
- When the game does go down, the reason and where it happened are written into the game log beside the game as well as into the crash file under Documents. Two players on v2.0.0 saw "Technical Difficulties" and their crash file never turned up, so the report that reached us said nothing about why.
- A Particle Uplink Cannon fired by a script along a path no longer crashes at the path's last waypoint.
- A damage effect whose attacker died first no longer crashes the game, and neither does a flash-bang into a building with fewer soldiers than it was meant to kill.
diff --git a/GeneralsMD/Code/GameEngine/Include/Common/EarlyOptions.h b/GeneralsMD/Code/GameEngine/Include/Common/EarlyOptions.h
index 7063984af..6f470763d 100644
--- a/GeneralsMD/Code/GameEngine/Include/Common/EarlyOptions.h
+++ b/GeneralsMD/Code/GameEngine/Include/Common/EarlyOptions.h
@@ -97,18 +97,44 @@ inline bool findDocumentsFolderA( char *out, size_t outSize )
return true;
}
+/** Whether a file can be created in this directory, which is made first if it is missing.
+ *
+ * Windows' Controlled Folder Access, the ransomware protection in Defender, refuses an unknown
+ * program any write under Documents, and a new generals.exe is an unknown program. The first
+ * write the game makes there is a debugging CRC file just after the shell map loads, and it
+ * threw; the crash report meant to explain that could not be written either, so v2.0.0 players
+ * got "Technical Difficulties" five seconds in with nothing to send. The probe deletes itself. */
+inline bool isDirectoryWritable( const char *directory )
+{
+ ::CreateDirectoryA( directory, NULL );
+
+ char probe[MAX_PATH * 2];
+ if (::_snprintf( probe, sizeof( probe ), "%szhr-write-test.tmp", directory ) < 0)
+ return false;
+ probe[sizeof( probe ) - 1] = 0;
+
+ HANDLE file = ::CreateFileA( probe, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
+ FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE, NULL );
+ if (file == INVALID_HANDLE_VALUE)
+ return false;
+ ::CloseHandle( file );
+ return true;
+}
+
/** The directory the game keeps Options.ini, replays and save games in.
*
- * GlobalData works this out the same way at startup (Documents plus a leaf name the installer
- * writes into the registry), but it does it far too late to help WinMain. Both defaults have to
- * match GlobalData.cpp's or the two read different files. */
+ * Documents plus a leaf name the installer writes into the registry. WinMain needs it before
+ * the engine exists and GlobalData sets m_userDataDir from it, so the two cannot disagree. When
+ * nothing can be written there the same leaf under %LOCALAPPDATA% is used instead, which no
+ * folder protection covers; the settings the player had in Documents are then left behind, but
+ * a game that cannot save settings could not have kept them anyway. */
inline bool findUserDataDirectory( char *out, size_t outSize )
{
if (outSize == 0)
return false;
out[0] = 0;
- char documents[MAX_PATH];
+ char documents[MAX_PATH * 2];
if (!findDocumentsFolderA( documents, sizeof( documents ) ))
return false;
@@ -149,6 +175,31 @@ inline bool findUserDataDirectory( char *out, size_t outSize )
return false;
}
out[outSize - 1] = 0;
+
+ // The answer is worked out once a process: WinMain and GlobalData both ask, and a probe that
+ // passed the first time and failed the second would split them between two folders.
+ static char s_chosen[MAX_PATH * 2] = "";
+ if (s_chosen[0] == 0)
+ {
+ ::strncpy( s_chosen, out, sizeof( s_chosen ) - 1 );
+ const char *localAppData = ::getenv( "LOCALAPPDATA" );
+ if (!isDirectoryWritable( out ) && localAppData != NULL && localAppData[0] != 0)
+ {
+ char fallback[MAX_PATH * 2];
+ if (::_snprintf( fallback, sizeof( fallback ), "%s\\%s\\", localAppData, leaf ) >= 0)
+ {
+ fallback[sizeof( fallback ) - 1] = 0;
+ if (isDirectoryWritable( fallback ))
+ ::strncpy( s_chosen, fallback, sizeof( s_chosen ) - 1 );
+ }
+ }
+ }
+ if (::strlen( s_chosen ) + 1 > outSize)
+ {
+ out[0] = 0;
+ return false;
+ }
+ ::strcpy( out, s_chosen );
return true;
}
diff --git a/GeneralsMD/Code/GameEngine/Source/Common/GameEngine.cpp b/GeneralsMD/Code/GameEngine/Source/Common/GameEngine.cpp
index 95a0044d1..443ca8810 100644
--- a/GeneralsMD/Code/GameEngine/Source/Common/GameEngine.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/Common/GameEngine.cpp
@@ -152,9 +152,11 @@ void DeepCRCSanityCheck::reset(void)
static Int timesThrough = 0;
static UnsignedInt lastCRC = 0;
- AsciiString fname;
- fname.format("%sCRCAfter%dMaps.dat", TheGlobalData->getPath_UserData().str(), timesThrough);
- UnsignedInt thisCRC = TheGameLogic->getCRC( CRC_RECALC, fname );
+ // EA wrote the whole deep CRC out to CRCAfter<n>Maps.dat in the user data folder here, on every
+ // player's machine, for a comparison only this function makes and only against the number. A
+ // folder that refused the write threw out of the shell map's first frame as "Uncaught exception
+ // in WinMain"; the number alone is all the check needs.
+ UnsignedInt thisCRC = TheGameLogic->getCRC( CRC_RECALC );
DEBUG_LOG(("DeepCRCSanityCheck: CRC is %X\n", thisCRC));
DEBUG_ASSERTCRASH(timesThrough == 0 || thisCRC == lastCRC,
diff --git a/GeneralsMD/Code/GameEngine/Source/Common/GlobalData.cpp b/GeneralsMD/Code/GameEngine/Source/Common/GlobalData.cpp
index 12dadfa7a..b5d588171 100644
--- a/GeneralsMD/Code/GameEngine/Source/Common/GlobalData.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/Common/GlobalData.cpp
@@ -39,7 +39,7 @@
#define DEFINE_PANNING_NAMES
#include "Common/CRC.h"
-#include "Common/EarlyOptions.h" // findDocumentsFolderA
+#include "Common/EarlyOptions.h" // findUserDataDirectory
#include "Common/File.h"
#include "Common/FileSystem.h"
#include "Common/GameAudio.h"
@@ -1227,34 +1227,14 @@ GlobalData::GlobalData()
m_keyboardCameraRotateSpeed = 0.1f;
- // Set user data directory based on registry settings instead of INI parameters. This allows us to
- // localize the leaf name.
- // A redirected Documents folder can sit at a path longer than MAX_PATH, and the shell call this
- // used to make simply fails there - see findDocumentsFolderA. The buffer is generous for the same
- // reason.
+ // Documents plus the registry's leaf name, or the same leaf under %LOCALAPPDATA% when nothing can
+ // be written under Documents - see findUserDataDirectory, which WinMain asks the same question of.
+ // A redirected Documents folder can sit at a path longer than MAX_PATH, so the buffer is generous.
char temp[1024];
- if (findDocumentsFolderA(temp, sizeof(temp)))
+ if (findUserDataDirectory(temp, sizeof(temp)))
{
- AsciiString myDocumentsDirectory = temp;
-
- if (myDocumentsDirectory.getCharAt(myDocumentsDirectory.getLength() -1) != '\\')
- myDocumentsDirectory.concat( '\\' );
-
- AsciiString leafName;
-
- if ( !GetStringFromRegistry( "", "UserDataLeafName", leafName ) )
- {
- // Use something, anything
- // [MH] had to remove this, otherwise mapcache build step won't run... DEBUG_CRASH( ( "Could not find registry key UserDataLeafName; defaulting to \"Command and Conquer Generals Zero Hour Data\" " ) );
- leafName = "Command and Conquer Generals Zero Hour Data";
- }
-
- myDocumentsDirectory.concat( leafName );
- if (myDocumentsDirectory.getCharAt( myDocumentsDirectory.getLength() - 1) != '\\')
- myDocumentsDirectory.concat( '\\' );
-
- CreateDirectory(myDocumentsDirectory.str(), NULL);
- m_userDataDir = myDocumentsDirectory;
+ m_userDataDir = temp;
+ DEBUG_LOG(("User data folder: %s\n", temp));
}
else
{