Skip to content

Commit f68b40a

Browse files
committed
refactor(scriptcompiler): use registry lookup instead of hardcoded game directory path
1 parent 4929764 commit f68b40a

File tree

2 files changed

+76
-8
lines changed

2 files changed

+76
-8
lines changed

Generals/Code/Tools/DataChunkConverter/DataChunkConverter.cpp

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
#include "Common/LocalFileSystem.h"
4848
#include "Common/ArchiveFileSystem.h"
4949
#include "Common/INI.h"
50+
#include "Common/Registry.h"
5051
#include "Win32Device/Common/Win32LocalFileSystem.h"
5152
#include "Win32Device/Common/Win32BIGFileSystem.h"
5253

@@ -255,11 +256,25 @@ static void extractChunkIDsFromRawData(const unsigned char* data, size_t dataSiz
255256
}
256257
}
257258

259+
static bool getGameDirectoryFromRegistry(char* gameDir, size_t maxLen)
260+
{
261+
AsciiString installPath;
262+
if (GetStringFromRegistry("", "InstallPath", installPath)) {
263+
if (installPath.getLength() > 0 && installPath.getLength() < (Int)maxLen) {
264+
strncpy(gameDir, installPath.str(), maxLen - 1);
265+
gameDir[maxLen - 1] = '\0';
266+
return true;
267+
}
268+
}
269+
return false;
270+
}
271+
258272
static void printUsage(const char* exeName)
259273
{
260-
printf("Usage: %s -in <input_file> -out <output_file>\n", exeName);
274+
printf("Usage: %s -in <input_file> -out <output_file> [-gamedir <game_directory>]\n", exeName);
261275
printf("Converts SCB files to JSON and vice versa.\n");
262276
printf("File format is auto-detected based on extension (.scb or .json)\n");
277+
printf("If -gamedir is not specified, attempts to find game directory from registry or current directory.\n");
263278
}
264279

265280

@@ -659,8 +674,7 @@ int main(int argc, char* argv[])
659674
{
660675
initMemoryManager();
661676

662-
const char* gameDir = "C:\\Program Files\\EA Games\\Command and Conquer Generals Zero Hour\\Command and Conquer Generals";
663-
677+
char gameDir[MAX_PATH] = "";
664678
char currentDir[MAX_PATH];
665679
if (!GetCurrentDirectoryA(MAX_PATH, currentDir)) {
666680
printf("Error: Could not get current directory. Error code: %d\n", GetLastError());
@@ -671,6 +685,24 @@ int main(int argc, char* argv[])
671685
bool inGameDir = (dataAttrs != INVALID_FILE_ATTRIBUTES && (dataAttrs & FILE_ATTRIBUTE_DIRECTORY));
672686

673687
if (!inGameDir) {
688+
const char* specifiedGameDir = NULL;
689+
690+
for (int i = 1; i < argc; i++) {
691+
if (strcmp(argv[i], "-gamedir") == 0 && i + 1 < argc) {
692+
specifiedGameDir = argv[++i];
693+
break;
694+
}
695+
}
696+
697+
if (specifiedGameDir) {
698+
strncpy(gameDir, specifiedGameDir, MAX_PATH - 1);
699+
gameDir[MAX_PATH - 1] = '\0';
700+
} else if (!getGameDirectoryFromRegistry(gameDir, MAX_PATH)) {
701+
printf("Error: Could not find game directory.\n");
702+
printf("Please specify the game directory using -gamedir <path> or ensure the game is installed and registered.\n");
703+
return 1;
704+
}
705+
674706
if (!SetCurrentDirectoryA(gameDir)) {
675707
printf("Error: Could not set working directory to game directory: %s\n", gameDir);
676708
return 1;
@@ -684,7 +716,7 @@ int main(int argc, char* argv[])
684716
}
685717
}
686718

687-
if (argc != 5) {
719+
if (argc < 5) {
688720
printUsage(argv[0]);
689721
return 1;
690722
}
@@ -697,6 +729,8 @@ int main(int argc, char* argv[])
697729
inputPath = argv[++i];
698730
} else if (strcmp(argv[i], "-out") == 0 && i + 1 < argc) {
699731
outputPath = argv[++i];
732+
} else if (strcmp(argv[i], "-gamedir") == 0 && i + 1 < argc) {
733+
i++;
700734
}
701735
}
702736

GeneralsMD/Code/Tools/DataChunkConverter/DataChunkConverter.cpp

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
#include "Common/LocalFileSystem.h"
4848
#include "Common/ArchiveFileSystem.h"
4949
#include "Common/INI.h"
50+
#include "Common/Registry.h"
5051
#include "Win32Device/Common/Win32LocalFileSystem.h"
5152
#include "Win32Device/Common/Win32BIGFileSystem.h"
5253

@@ -257,11 +258,25 @@ static void extractChunkIDsFromRawData(const unsigned char* data, size_t dataSiz
257258
}
258259
}
259260

261+
static bool getGameDirectoryFromRegistry(char* gameDir, size_t maxLen)
262+
{
263+
AsciiString installPath;
264+
if (GetStringFromRegistry("", "InstallPath", installPath)) {
265+
if (installPath.getLength() > 0 && installPath.getLength() < (Int)maxLen) {
266+
strncpy(gameDir, installPath.str(), maxLen - 1);
267+
gameDir[maxLen - 1] = '\0';
268+
return true;
269+
}
270+
}
271+
return false;
272+
}
273+
260274
static void printUsage(const char* exeName)
261275
{
262-
printf("Usage: %s -in <input_file> -out <output_file>\n", exeName);
276+
printf("Usage: %s -in <input_file> -out <output_file> [-gamedir <game_directory>]\n", exeName);
263277
printf("Converts SCB files to JSON and vice versa.\n");
264278
printf("File format is auto-detected based on extension (.scb or .json)\n");
279+
printf("If -gamedir is not specified, attempts to find game directory from registry or current directory.\n");
265280
}
266281

267282

@@ -661,8 +676,7 @@ int main(int argc, char* argv[])
661676
{
662677
initMemoryManager();
663678

664-
const char* gameDir = "C:\\Program Files\\EA Games\\Command and Conquer Generals Zero Hour\\Command and Conquer Generals Zero Hour";
665-
679+
char gameDir[MAX_PATH] = "";
666680
char currentDir[MAX_PATH];
667681
if (!GetCurrentDirectoryA(MAX_PATH, currentDir)) {
668682
printf("Error: Could not get current directory. Error code: %d\n", GetLastError());
@@ -673,6 +687,24 @@ int main(int argc, char* argv[])
673687
bool inGameDir = (dataAttrs != INVALID_FILE_ATTRIBUTES && (dataAttrs & FILE_ATTRIBUTE_DIRECTORY));
674688

675689
if (!inGameDir) {
690+
const char* specifiedGameDir = NULL;
691+
692+
for (int i = 1; i < argc; i++) {
693+
if (strcmp(argv[i], "-gamedir") == 0 && i + 1 < argc) {
694+
specifiedGameDir = argv[++i];
695+
break;
696+
}
697+
}
698+
699+
if (specifiedGameDir) {
700+
strncpy(gameDir, specifiedGameDir, MAX_PATH - 1);
701+
gameDir[MAX_PATH - 1] = '\0';
702+
} else if (!getGameDirectoryFromRegistry(gameDir, MAX_PATH)) {
703+
printf("Error: Could not find game directory.\n");
704+
printf("Please specify the game directory using -gamedir <path> or ensure the game is installed and registered.\n");
705+
return 1;
706+
}
707+
676708
if (!SetCurrentDirectoryA(gameDir)) {
677709
printf("Error: Could not set working directory to game directory: %s\n", gameDir);
678710
return 1;
@@ -686,7 +718,7 @@ int main(int argc, char* argv[])
686718
}
687719
}
688720

689-
if (argc != 5) {
721+
if (argc < 5) {
690722
printUsage(argv[0]);
691723
return 1;
692724
}
@@ -699,6 +731,8 @@ int main(int argc, char* argv[])
699731
inputPath = argv[++i];
700732
} else if (strcmp(argv[i], "-out") == 0 && i + 1 < argc) {
701733
outputPath = argv[++i];
734+
} else if (strcmp(argv[i], "-gamedir") == 0 && i + 1 < argc) {
735+
i++;
702736
}
703737
}
704738

0 commit comments

Comments
 (0)