Skip to content

Commit 9a09bae

Browse files
authored
fix(script): Improve string handling of 'Fundamentalist' code hacks (#1797)
1 parent 08807eb commit 9a09bae

File tree

4 files changed

+23
-23
lines changed

4 files changed

+23
-23
lines changed

Generals/Code/GameEngine/Source/GameLogic/ScriptEngine/Scripts.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2059,13 +2059,13 @@ Parameter *Parameter::ReadParameter(DataChunkInput &file)
20592059
// quick hack to make loading models with "Fundamentalist" switch to "GLA"
20602060
if (pParm->m_string.startsWith("Fundamentalist"))
20612061
{
2062-
char oldName[256];
2062+
const char* replacePrefix = "Fundamentalist";
2063+
const size_t offset = pParm->m_string.startsWith(replacePrefix) ? strlen(replacePrefix) : 0u;
20632064
char newName[256];
2064-
strcpy(oldName, pParm->m_string.str());
20652065
strcpy(newName, "GLA");
2066-
strlcat(newName, oldName+strlen("Fundamentalist"), ARRAY_SIZE(newName));
2066+
strlcat(newName, pParm->m_string.str() + offset, ARRAY_SIZE(newName));
2067+
DEBUG_LOG(("Changing Script Ref from %s to %s", pParm->m_string.str(), newName));
20672068
pParm->m_string.set(newName);
2068-
DEBUG_LOG(("Changing Script Ref from %s to %s", oldName, newName));
20692069
}
20702070
}
20712071

Generals/Code/Tools/WorldBuilder/src/WorldBuilderDoc.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -422,11 +422,11 @@ void CWorldBuilderDoc::Serialize(CArchive& ar)
422422

423423
AsciiString ConvertName(AsciiString name)
424424
{
425-
char oldName[256];
425+
const char* replacePrefix = "Fundamentalist";
426+
const size_t offset = name.startsWith(replacePrefix) ? strlen(replacePrefix) : 0u;
426427
char newName[256];
427-
strcpy(oldName, name.str());
428428
strcpy(newName, "GLA");
429-
strlcat(newName, oldName+strlen("Fundamentalist"), ARRAY_SIZE(newName));
429+
strlcat(newName, name.str() + offset, ARRAY_SIZE(newName));
430430
AsciiString swapName;
431431
swapName.set(newName);
432432
const ThingTemplate *tt = TheThingFactory->findTemplate(swapName);
@@ -438,11 +438,11 @@ AsciiString ConvertName(AsciiString name)
438438

439439
AsciiString ConvertFaction(AsciiString name)
440440
{
441-
char oldName[256];
441+
const char* replacePrefix = "FactionFundamentalist";
442+
const size_t offset = name.startsWith(replacePrefix) ? strlen(replacePrefix) : 0u;
442443
char newName[256];
443-
strcpy(oldName, name.str());
444444
strcpy(newName, "FactionGLA");
445-
strlcat(newName, oldName+strlen("FactionFundamentalist"), ARRAY_SIZE(newName));
445+
strlcat(newName, name.str() + offset, ARRAY_SIZE(newName));
446446
AsciiString swapName;
447447
swapName.set(newName);
448448
const PlayerTemplate* pt = ThePlayerTemplateStore->findPlayerTemplate(NAMEKEY(swapName));

GeneralsMD/Code/GameEngine/Source/GameLogic/ScriptEngine/Scripts.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2134,13 +2134,13 @@ Parameter *Parameter::ReadParameter(DataChunkInput &file)
21342134
// quick hack to make loading models with "Fundamentalist" switch to "GLA"
21352135
if (pParm->m_string.startsWith("Fundamentalist"))
21362136
{
2137-
char oldName[256];
2137+
const char* replacePrefix = "Fundamentalist";
2138+
const size_t offset = pParm->m_string.startsWith(replacePrefix) ? strlen(replacePrefix) : 0u;
21382139
char newName[256];
2139-
strcpy(oldName, pParm->m_string.str());
21402140
strcpy(newName, "GLA");
2141-
strlcat(newName, oldName+strlen("Fundamentalist"), ARRAY_SIZE(newName));
2141+
strlcat(newName, pParm->m_string.str() + offset, ARRAY_SIZE(newName));
2142+
DEBUG_LOG(("Changing Script Ref from %s to %s", pParm->m_string.str(), newName));
21422143
pParm->m_string.set(newName);
2143-
DEBUG_LOG(("Changing Script Ref from %s to %s", oldName, newName));
21442144
}
21452145
}
21462146

GeneralsMD/Code/Tools/WorldBuilder/src/WorldBuilderDoc.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -434,10 +434,10 @@ void CWorldBuilderDoc::Serialize(CArchive& ar)
434434

435435
AsciiString ConvertToNonGCName(AsciiString name, Bool checkTemplate=true)
436436
{
437-
char oldName[256];
437+
const char* replacePrefix = "GC_";
438+
const size_t offset = name.startsWith(replacePrefix) ? strlen(replacePrefix) : 0u;
438439
char newName[256];
439-
strcpy(oldName, name.str());
440-
strcpy(newName, oldName+strlen("GC_"));
440+
strlcpy(newName, name.str() + offset, ARRAY_SIZE(newName));
441441
AsciiString swapName;
442442
swapName.set(newName);
443443
if (checkTemplate)
@@ -453,11 +453,11 @@ AsciiString ConvertToNonGCName(AsciiString name, Bool checkTemplate=true)
453453

454454
AsciiString ConvertName(AsciiString name)
455455
{
456-
char oldName[256];
456+
const char* replacePrefix = "Fundamentalist";
457+
const size_t offset = name.startsWith(replacePrefix) ? strlen(replacePrefix) : 0u;
457458
char newName[256];
458-
strcpy(oldName, name.str());
459459
strcpy(newName, "GLA");
460-
strlcat(newName, oldName+strlen("Fundamentalist"), ARRAY_SIZE(newName));
460+
strlcat(newName, name.str() + offset, ARRAY_SIZE(newName));
461461
AsciiString swapName;
462462
swapName.set(newName);
463463
const ThingTemplate *tt = TheThingFactory->findTemplate(swapName);
@@ -469,11 +469,11 @@ AsciiString ConvertName(AsciiString name)
469469

470470
AsciiString ConvertFaction(AsciiString name)
471471
{
472-
char oldName[256];
472+
const char* replacePrefix = "FactionFundamentalist";
473+
const size_t offset = name.startsWith(replacePrefix) ? strlen(replacePrefix) : 0u;
473474
char newName[256];
474-
strcpy(oldName, name.str());
475475
strcpy(newName, "FactionGLA");
476-
strlcat(newName, oldName+strlen("FactionFundamentalist"), ARRAY_SIZE(newName));
476+
strlcat(newName, name.str() + offset, ARRAY_SIZE(newName));
477477
AsciiString swapName;
478478
swapName.set(newName);
479479
const PlayerTemplate* pt = ThePlayerTemplateStore->findPlayerTemplate(NAMEKEY(swapName));

0 commit comments

Comments
 (0)