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
feat(w3ddevice): play the Bink movies through the shipped BINKW32.DLL
Olcay Seygan1ea60dc33a8f
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex b00aa9c4b..a1fa9569c 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -209,6 +209,7 @@ found and fixed — EA's own, not port damage.**- Widescreen resolutions are back in the options menu.- Zoom further out, with the whole map drawn instead of black corners.+- The zoom-out ceiling is the same for everyone; no setting buys you a wider view.- Zoom toward the cursor works; the spot under your pointer stays there.- The camera turns in whole 45-degree steps, instantly, while you hold the key.- Edge scrolling works windowed, and scroll speed no longer follows your frame rate.@@ -280,6 +281,7 @@ found and fixed — EA's own, not port damage.**## Sound, video, and getting it to start at all- Audio is real, through the audio library the retail game ships with.+- The videos play: the intro, the sizzle reel, the mission briefings, the general portraits.- About 5,600 graphics calls are translated to a modern path, none of them touched.- No disc, no registry keys, no retail installer — a normal install works.- The 1.04 patch content is reachable again.@@ -307,7 +309,6 @@ found and fixed — EA's own, not port damage.**## Not there yet-- The intro and mission videos do not play; the decoder was never released.- Random maps generate and can be played — a number picks the map and the match starts on it —but there is still no menu entry and no reroll button.- Online and LAN play are untested.diff --git a/GeneralsMD/Code/CMakeLists.txt b/GeneralsMD/Code/CMakeLists.txtindex e11bea443..e7f5753ad 100644--- a/GeneralsMD/Code/CMakeLists.txt+++ b/GeneralsMD/Code/CMakeLists.txt@@ -409,11 +409,6 @@ file(GLOB_RECURSE GAMEENGINEDEVICE_SOURCES ${GED}/Source/*.cpp)# shipping build uses the Win32 message mouse instead).list(REMOVE_ITEM GAMEENGINEDEVICE_SOURCES${GED}/Source/Win32Device/GameClient/Win32DIMouse.cpp)-# Bink is a binary-only SDK that is not in the release, so its device is-# replaced by the null device in Stubs/ (PLAN.md Phase 4: skipped movies).-list(REMOVE_ITEM GAMEENGINEDEVICE_SOURCES- ${GED}/Source/VideoDevice/Bink/BinkVideoPlayer.cpp)-# Miles is binary-only too, but the game ships mss32.dll and it exports# decorated stdcall names (_AIL_startup@0, ...), so MilesAudioManager can bind# to the real DLL through an import library - real audio, no SDK. Thyme's@@ -422,6 +417,16 @@ list(REMOVE_ITEM GAMEENGINEDEVICE_SOURCES# lib makes the exe resolve those names from whatever mss32.dll sits next to# it at runtime - the game's real one. (`lib /def` alone can't produce this# import lib: it prepends a second underscore to the already-decorated names.)+# Bink is binary-only too, and the same trick works: the game ships BINKW32.DLL+# and it exports decorated stdcall names (_BinkOpen@8, ...), so BinkVideoPlayer+# binds to the real DLL through the import library of a no-op stub built as+# binkw32.dll. Real movies, no SDK. The stub dll in build/Release/ must never+# be copied to Run/ - the loader has to find the game's own BINKW32.DLL there.+add_library(binkstub SHARED+ ${WW}/Bink/stub/bink.c ${WW}/Bink/stub/bink.def)+set_target_properties(binkstub PROPERTIES OUTPUT_NAME binkw32)+target_include_directories(binkstub PRIVATE ${WW}/Bink/include)+add_library(milesstub SHARED${WW}/Miles6/stub/miles.c ${WW}/Miles6/stub/miles.def)set_target_properties(milesstub PROPERTIES OUTPUT_NAME mss32)@@ -431,9 +436,9 @@ add_library(gameenginedevice STATIC ${GAMEENGINEDEVICE_SOURCES})target_include_directories(gameenginedevice PUBLIC${GED}/Include ${STUBS} ${CMAKE_CURRENT_SOURCE_DIR}/Main- ${WW}/Miles6/include)+ ${WW}/Miles6/include ${WW}/Bink/include)set_target_properties(gameenginedevice PROPERTIES CXX_STANDARD 17)-target_link_libraries(gameenginedevice PUBLIC gameengine ww3d2 milesstub)+target_link_libraries(gameenginedevice PUBLIC gameengine ww3d2 milesstub binkstub)# ---- generals.exe ---------------------------------------------------------# BuildVersion.h and GeneratedVersion.h are normally written by Tools/@@ -558,6 +563,14 @@ add_custom_command(TARGET dx8_smoke POST_BUILDadd_dependencies(dx8_smoke d3d8to9)add_test(NAME dx8_smoke COMMAND dx8_smoke)add_test(NAME dx8_smoke_d3d12 COMMAND dx8_smoke -d3d12)++# bink_smoke validates the hand-reconstructed Bink ABI against the real+# BINKW32.DLL the game ships - it loads that DLL by path, so it does not link+# binkstub and it has to run from Run/. Without game data it prints "skip".+add_executable(bink_smoke ${TESTS}/bink_smoke.c)+target_include_directories(bink_smoke PRIVATE ${WW}/Bink/include)+add_test(NAME bink_smoke COMMAND bink_smoke+ WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../Run)# "-msaa": a multisampled back buffer, and the plain render target + non-multisampled depth# buffer pairing that DX8Wrapper::_Get_Non_MultiSampled_Depth_Buffer hands the screen filters.add_test(NAME dx8_smoke_msaa COMMAND dx8_smoke -msaa 4)diff --git a/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/W3DGameClient.h b/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/W3DGameClient.hindex c5b7e3af8..af08378eb 100644--- a/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/W3DGameClient.h+++ b/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/W3DGameClient.h@@ -48,10 +48,7 @@#include "W3DDevice/GameClient/W3DGameWindowManager.h"#include "W3DDevice/GameClient/W3DGameFont.h"#include "W3DDevice/GameClient/W3DDisplayStringManager.h"-// Bink is a binary-only SDK and is not in the source release, so the movies run-// through VideoPlayer itself: it parses Video.ini and then opens nothing, which-// makes every movie a zero-frame one that the callers skip.-#include "GameClient/VideoPlayer.h"+#include "VideoDevice/Bink/BinkVideoPlayer.h"#include "Win32Device/GameClient/Win32DIKeyboard.h"#include "Win32Device/GameClient/Win32DIMouse.h"#include "Win32Device/GameClient/Win32Mouse.h"@@ -115,7 +112,7 @@ protected:/// Manager for display stringsvirtual DisplayStringManager *createDisplayStringManager( void ) { return NEW W3DDisplayStringManager; }- virtual VideoPlayerInterface *createVideoPlayer( void ) { return NEW VideoPlayer; }+ virtual VideoPlayerInterface *createVideoPlayer( void ) { return NEW BinkVideoPlayer; }/// factory for creating the TerrainVisualvirtual TerrainVisual *createTerrainVisual( void ) { return NEW W3DTerrainVisual; }diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/Bink/include/bink.h b/GeneralsMD/Code/Libraries/Source/WWVegas/Bink/include/bink.hnew file mode 100644index 000000000..e31686c98--- /dev/null+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/Bink/include/bink.h@@ -0,0 +1,100 @@+/*+ * bink.h - reconstructed subset of RAD Game Tools' Bink 1.x API.+ *+ * The Bink SDK is binary-only and was stripped from the source release, but the+ * game ships BINKW32.DLL and that DLL exports decorated stdcall names+ * (_BinkOpen@8, ...). So BinkVideoPlayer only needs declarations, and the+ * stub in ../stub/ supplies an import library that binds to the real DLL at+ * runtime - the same recipe as Miles6/ and mss32.dll.+ *+ * Provenance of every declaration below:+ * - the @N byte counts in BINKW32.DLL's export table fix the argument count+ * of each function (dumpbin /exports), and the call sites in+ * GameEngineDevice/Source/VideoDevice/Bink/BinkVideoPlayer.cpp fix the+ * argument types.+ * - the BINK struct's leading fields are Bink 1's documented public layout;+ * the port reads only the seven declared here and Tests/bink_smoke.c+ * checks all of them against the .bik file header.+ * - the surface and open flags are Bink 1's published constants.+ */++#ifndef __BINK_H__+#define __BINK_H__++#ifdef __cplusplus+extern "C" {+#endif++/*+ * RAD's radbase.h shorthand. BinkVideoPlayer.cpp declares its surface flag as+ * a "u32", so the missing SDK header supplied these - without them that file+ * does not compile.+ */+typedef unsigned char u8;+typedef unsigned short u16;+typedef unsigned int u32;+typedef signed int s32;++/* Open flags. */+#define BINKPRELOADALL 0x00002000L /* read the whole movie into memory */++/* Surface types for BinkCopyToBuffer. */+#define BINKSURFACE8P 0+#define BINKSURFACE24 1+#define BINKSURFACE24R 2+#define BINKSURFACE32 3+#define BINKSURFACE32R 4+#define BINKSURFACE32A 5+#define BINKSURFACE32RA 6+#define BINKSURFACE4444 7+#define BINKSURFACE5551 8+#define BINKSURFACE555 9+#define BINKSURFACE565 10+#define BINKSURFACE655 11+#define BINKSURFACE664 12+#define BINKSURFACEYUY2 13+#define BINKSURFACEUYVY 14+#define BINKSURFACEYV12 15++#define BINKCOPYALL 0x80000000L /* copy the whole frame, not the dirty rects */++/*+ * The real BINK struct continues well past FrameRateDiv; it is allocated by+ * BinkOpen inside the DLL and only ever read here, so declaring the prefix the+ * port uses is enough - the trailing fields' offsets never come into play.+ */+typedef struct BINK+{+ unsigned int Width; /* frame width, 1 based */+ unsigned int Height; /* frame height, 1 based */+ unsigned int Frames; /* number of frames, 1 based */+ unsigned int FrameNum; /* frame about to be displayed, 1 based */+ unsigned int LastFrameNum; /* last displayed frame, 1 based, 0 = none */+ unsigned int FrameRate; /* frame rate numerator */+ unsigned int FrameRateDiv; /* frame rate denominator */+} BINK, *HBINK;++typedef int (__stdcall *BINKSNDSYSOPEN)(unsigned int param);++HBINK __stdcall BinkOpen(const char *name, unsigned int flags);+void __stdcall BinkClose(HBINK bnk);+int __stdcall BinkWait(HBINK bnk);+int __stdcall BinkDoFrame(HBINK bnk);+void __stdcall BinkNextFrame(HBINK bnk);+int __stdcall BinkGoto(HBINK bnk, unsigned int frame, int flags);+int __stdcall BinkCopyToBuffer(HBINK bnk, void *dest, int destpitch,+ unsigned int destheight, unsigned int destx,+ unsigned int desty, unsigned int flags);+int __stdcall BinkSetVolume(HBINK bnk, unsigned int trackid, int volume);+void __stdcall BinkSetSoundTrack(unsigned int total_tracks, unsigned int *tracks);+int __stdcall BinkSetSoundSystem(BINKSNDSYSOPEN open, unsigned int param);+int __stdcall BinkOpenDirectSound(unsigned int param);++#define BinkSoundUseDirectSound(lpDS) \+ BinkSetSoundSystem((BINKSNDSYSOPEN)BinkOpenDirectSound, (unsigned int)(lpDS))++#ifdef __cplusplus+}+#endif++#endif /* __BINK_H__ */diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/Bink/stub/bink.c b/GeneralsMD/Code/Libraries/Source/WWVegas/Bink/stub/bink.cnew file mode 100644index 000000000..7f5edd983--- /dev/null+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/Bink/stub/bink.c@@ -0,0 +1,24 @@+/*+ * No-op stand-in for binkw32.dll. Nothing here ever runs: it exists only so+ * the linker can produce an import library whose symbols carry the decorated+ * names the shipped BINKW32.DLL exports, which is what generals.exe binds to+ * at run time. (`lib /def` alone cannot make that import library - it prefixes+ * a second underscore onto the already-decorated names.)+ *+ * The stub DLL built from this file must never be copied next to generals.exe.+ */+#include "bink.h"++HBINK __stdcall BinkOpen(const char *name, unsigned int flags) { return 0; }+void __stdcall BinkClose(HBINK bnk) {}+int __stdcall BinkWait(HBINK bnk) { return 0; }+int __stdcall BinkDoFrame(HBINK bnk) { return 0; }+void __stdcall BinkNextFrame(HBINK bnk) {}+int __stdcall BinkGoto(HBINK bnk, unsigned int frame, int flags) { return 0; }+int __stdcall BinkCopyToBuffer(HBINK bnk, void *dest, int destpitch,+ unsigned int destheight, unsigned int destx,+ unsigned int desty, unsigned int flags) { return 0; }+int __stdcall BinkSetVolume(HBINK bnk, unsigned int trackid, int volume) { return 0; }+void __stdcall BinkSetSoundTrack(unsigned int total_tracks, unsigned int *tracks) {}+int __stdcall BinkSetSoundSystem(BINKSNDSYSOPEN open, unsigned int param) { return 0; }+int __stdcall BinkOpenDirectSound(unsigned int param) { return 0; }diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/Bink/stub/bink.def b/GeneralsMD/Code/Libraries/Source/WWVegas/Bink/stub/bink.defnew file mode 100644index 000000000..de285f6e2--- /dev/null+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/Bink/stub/bink.def@@ -0,0 +1,12 @@+EXPORTS+_BinkOpen@8+_BinkClose@4+_BinkWait@4+_BinkDoFrame@4+_BinkNextFrame@4+_BinkGoto@12+_BinkCopyToBuffer@28+_BinkSetVolume@12+_BinkSetSoundTrack@8+_BinkSetSoundSystem@8+_BinkOpenDirectSound@4diff --git a/GeneralsMD/Code/Tests/bink_smoke.c b/GeneralsMD/Code/Tests/bink_smoke.cnew file mode 100644index 000000000..ce9dbcefe--- /dev/null+++ b/GeneralsMD/Code/Tests/bink_smoke.c@@ -0,0 +1,156 @@+/*+ * bink_smoke - checks the reconstructed Bink ABI in Libraries/Source/WWVegas/+ * Bink/include/bink.h against the real BINKW32.DLL the game ships.+ *+ * Nothing in that header could be taken from an SDK, so the two things it had+ * to guess are exactly what this proves:+ *+ * 1. the BINK struct's leading fields - Width/Height/Frames/FrameRate/+ * FrameRateDiv are compared against the same values read straight out of+ * the .bik file header, so a shifted offset shows up as a mismatch;+ * 2. the BINKSURFACE* constants - one frame is decoded twice, once as 32 bit+ * and once as 24 bit, and the two must agree pixel for pixel.+ *+ * It loads ".\\BINKW32.DLL" by an explicit path on purpose (a backslash: LoadLibrary+ * does not take forward slashes in a path, it would fall back to the search+ * order and find the stub next to this exe): the plain name would+ * find the no-op stub sitting next to this exe in the build tree. Run it with+ * GeneralsMD/Run as the working directory (CTest does). With no game data it+ * reports "skip" and passes - Run/ is not in the repo.+ */+#include <windows.h>+#include <stdio.h>+#include <stdlib.h>++#include "bink.h"++static int failures = 0;++#define CHECK_EQ(a, b, what) \+ do { \+ unsigned int _a = (unsigned int)(a), _b = (unsigned int)(b); \+ if (_a != _b) { \+ printf("FAIL %s: %u != %u\n", (what), _a, _b); \+ ++failures; \+ } \+ } while (0)++static unsigned int rd32(const unsigned char *p) {+ return p[0] | (p[1] << 8) | (p[2] << 16) | ((unsigned int)p[3] << 24);+}++int main(int argc, char **argv) {+ const char *path = (argc > 1) ? argv[1] : "Data/English/Movies/EA_LOGO.BIK";+ unsigned char hdr[40];+ char dllPath[MAX_PATH];+ unsigned int fileFrames, fileWidth, fileHeight, fileRate, fileRateDiv;+ unsigned char *buf32, *buf24;+ unsigned int w, h, i, frame;+ int nonBlack = 0;+ HMODULE dll;+ HBINK bnk;++ HBINK (__stdcall *pOpen)(const char *, unsigned int);+ void (__stdcall *pClose)(HBINK);+ int (__stdcall *pDoFrame)(HBINK);+ void (__stdcall *pNextFrame)(HBINK);+ int (__stdcall *pCopy)(HBINK, void *, int, unsigned int, unsigned int,+ unsigned int, unsigned int);+ void (__stdcall *pSetSoundTrack)(unsigned int, unsigned int *);+ const char *(__stdcall *pGetError)(void);++ FILE *f = fopen(path, "rb");+ if (!f || fread(hdr, 1, sizeof hdr, f) != sizeof hdr) {+ if (f) fclose(f);+ printf("skip: no %s (game data is not in the repo)\n", path);+ return 0;+ }+ fclose(f);+ if (hdr[0] != 'B' || hdr[1] != 'I' || hdr[2] != 'K') {+ printf("FAIL %s is not a Bink file\n", path);+ return 1;+ }+ /* Bink file header: frames at 8, width at 20, height at 24, fps at 28/32. */+ fileFrames = rd32(hdr + 8);+ fileWidth = rd32(hdr + 20);+ fileHeight = rd32(hdr + 24);+ fileRate = rd32(hdr + 28);+ fileRateDiv = rd32(hdr + 32);++ /* An absolute path, because a relative one still goes through the DLL search+ order - and that starts at this exe's own directory, where the build puts+ the no-op stub also called binkw32.dll. */+ if (!GetFullPathNameA("BINKW32.DLL", sizeof dllPath, dllPath, NULL)) return 1;+ dll = LoadLibraryA(dllPath);+ if (!dll) {+ printf("skip: no %s\n", dllPath);+ return 0;+ }+ pOpen = (void *)GetProcAddress(dll, "_BinkOpen@8");+ pClose = (void *)GetProcAddress(dll, "_BinkClose@4");+ pDoFrame = (void *)GetProcAddress(dll, "_BinkDoFrame@4");+ pNextFrame = (void *)GetProcAddress(dll, "_BinkNextFrame@4");+ pCopy = (void *)GetProcAddress(dll, "_BinkCopyToBuffer@28");+ pSetSoundTrack = (void *)GetProcAddress(dll, "_BinkSetSoundTrack@8");+ pGetError = (void *)GetProcAddress(dll, "_BinkGetError@0");+ if (!pOpen || !pClose || !pDoFrame || !pNextFrame || !pCopy || !pSetSoundTrack) {+ printf("FAIL BINKW32.DLL does not export the decorated names bink.def lists\n");+ return 1;+ }++ pSetSoundTrack(0, 0); /* decode silently, no audio device needed */+ bnk = pOpen(path, BINKPRELOADALL);+ if (!bnk) {+ printf("FAIL BinkOpen(%s) returned NULL: %s\n", path,+ pGetError ? pGetError() : "(no BinkGetError)");+ return 1;+ }++ CHECK_EQ(bnk->Width, fileWidth, "BINK.Width");+ CHECK_EQ(bnk->Height, fileHeight, "BINK.Height");+ CHECK_EQ(bnk->Frames, fileFrames, "BINK.Frames");+ CHECK_EQ(bnk->FrameNum, 1, "BINK.FrameNum starts at 1");+ CHECK_EQ(bnk->FrameRate, fileRate, "BINK.FrameRate");+ CHECK_EQ(bnk->FrameRateDiv, fileRateDiv, "BINK.FrameRateDiv");++ w = fileWidth;+ h = fileHeight;+ buf32 = (unsigned char *)malloc(w * h * 4);+ buf24 = (unsigned char *)malloc(w * h * 3);++ /* Skip over any black lead-in, else the pixel comparison proves nothing. */+ for (frame = 0; frame < 60 && !nonBlack; ++frame) {+ pDoFrame(bnk);+ memset(buf32, 0xCD, w * h * 4);+ pCopy(bnk, buf32, (int)(w * 4), h, 0, 0, BINKSURFACE32 | BINKCOPYALL);+ for (i = 0; i < w * h * 4; ++i) {+ if (buf32[i] != 0 && buf32[i] != 0xCD) { nonBlack = 1; break; }+ }+ if (!nonBlack && frame + 1 < bnk->Frames) pNextFrame(bnk);+ }+ if (!nonBlack) {+ printf("FAIL first %u frames decoded to nothing but black\n", frame);+ ++failures;+ } else {+ memset(buf24, 0xCD, w * h * 3);+ pCopy(bnk, buf24, (int)(w * 3), h, 0, 0, BINKSURFACE24 | BINKCOPYALL);+ for (i = 0; i < w * h; ++i) {+ if (buf32[i * 4 + 0] != buf24[i * 3 + 0] ||+ buf32[i * 4 + 1] != buf24[i * 3 + 1] ||+ buf32[i * 4 + 2] != buf24[i * 3 + 2]) {+ printf("FAIL pixel %u differs between BINKSURFACE32 and BINKSURFACE24\n", i);+ ++failures;+ break;+ }+ }+ }++ free(buf32);+ free(buf24);+ pClose(bnk);+ FreeLibrary(dll);++ printf("%s: %ux%u, %u frames, %u/%u fps - %d failure(s)\n", path, w, h,+ fileFrames, fileRate, fileRateDiv, failures);+ return failures;+}