Update CGameConfig members to use std::string instead of fixed-size char arrays (#1495)

This commit is contained in:
nosoop 2021-06-03 09:27:16 -07:00 committed by GitHub
parent 8f73e5e5a1
commit d01c72f79b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 45 deletions

View File

@ -225,7 +225,7 @@ SMCResult CGameConfig::ReadSMC_NewSection(const SMCStates *states, const char *n
{
bShouldBeReadingDefault = true;
m_ParseState = PSTATE_GAMEDEFS;
strncopy(m_Game, name, sizeof(m_Game));
m_Game = name;
} else {
m_IgnoreLevel++;
}
@ -241,7 +241,7 @@ SMCResult CGameConfig::ReadSMC_NewSection(const SMCStates *states, const char *n
{
m_ParseState = PSTATE_GAMEDEFS_KEYS;
}
else if ((strcmp(name, "#supported") == 0) && (strcmp(m_Game, "#default") == 0))
else if ((strcmp(name, "#supported") == 0) && (m_Game == "#default"))
{
m_ParseState = PSTATE_GAMEDEFS_SUPPORTED;
/* Ignore this section unless we get a game. */
@ -280,23 +280,23 @@ SMCResult CGameConfig::ReadSMC_NewSection(const SMCStates *states, const char *n
}
case PSTATE_GAMEDEFS_KEYS:
{
strncopy(m_Key, name, sizeof(m_Key));
m_Key = name;
m_ParseState = PSTATE_GAMEDEFS_KEYS_PLATFORM;
matched_platform = false;
break;
}
case PSTATE_GAMEDEFS_OFFSETS:
{
m_Prop[0] = '\0';
m_Class[0] = '\0';
strncopy(m_offset, name, sizeof(m_offset));
m_Prop.clear();
m_Class.clear();
m_offset = name;
m_ParseState = PSTATE_GAMEDEFS_OFFSETS_OFFSET;
matched_platform = false;
break;
}
case PSTATE_GAMEDEFS_SIGNATURES:
{
strncopy(m_offset, name, sizeof(m_offset));
m_offset = name;
s_TempSig.Reset();
m_ParseState = PSTATE_GAMEDEFS_SIGNATURES_SIG;
matched_platform = false;
@ -338,7 +338,7 @@ SMCResult CGameConfig::ReadSMC_NewSection(const SMCStates *states, const char *n
if (error[0] != '\0')
{
m_IgnoreLevel = 1;
logger->LogError("[SM] Error while parsing CRC section for \"%s\" (%s):", m_Game, m_CurFile);
logger->LogError("[SM] Error while parsing CRC section for \"%s\" (%s):", m_Game.c_str(), m_CurFile);
logger->LogError("[SM] %s", error);
} else {
m_ParseState = PSTATE_GAMEDEFS_CRC_BINARY;
@ -353,12 +353,12 @@ SMCResult CGameConfig::ReadSMC_NewSection(const SMCStates *states, const char *n
}
case PSTATE_GAMEDEFS_ADDRESSES:
{
m_Address[0] = '\0';
m_AddressSignature[0] = '\0';
m_Address.clear();
m_AddressSignature.clear();
m_AddressReadCount = 0;
m_AddressLastIsOffset = false;
strncopy(m_Address, name, sizeof(m_Address));
m_Address = name;
m_ParseState = PSTATE_GAMEDEFS_ADDRESSES_ADDRESS;
break;
@ -374,7 +374,7 @@ SMCResult CGameConfig::ReadSMC_NewSection(const SMCStates *states, const char *n
if (strcmp(name, "linux") != 0 && strcmp(name, "windows") != 0 && strcmp(name, "mac") != 0 &&
strcmp(name, "linux64") != 0 && strcmp(name, "windows64") != 0 && strcmp(name, "mac64") != 0)
{
logger->LogError("[SM] Error while parsing Address section for \"%s\" (%s):", m_Address, m_CurFile);
logger->LogError("[SM] Error while parsing Address section for \"%s\" (%s):", m_Address.c_str(), m_CurFile);
logger->LogError("[SM] Unrecognized platform \"%s\"", name);
}
m_IgnoreLevel = 1;
@ -411,11 +411,11 @@ SMCResult CGameConfig::ReadSMC_KeyValue(const SMCStates *states, const char *key
{
if (strcmp(key, "class") == 0)
{
strncopy(m_Class, value, sizeof(m_Class));
m_Class = value;
} else if (strcmp(key, "prop") == 0) {
strncopy(m_Prop, value, sizeof(m_Prop));
m_Prop = value;
} else if (IsPlatformCompatible(key, &matched_platform)) {
m_Offsets.replace(m_offset, static_cast<int>(strtol(value, NULL, 0)));
m_Offsets.replace(m_offset.c_str(), static_cast<int>(strtol(value, NULL, 0)));
}
} else if (m_ParseState == PSTATE_GAMEDEFS_KEYS) {
std::string vstr(value);
@ -425,7 +425,7 @@ SMCResult CGameConfig::ReadSMC_KeyValue(const SMCStates *states, const char *key
if (IsPlatformCompatible(key, &matched_platform))
{
std::string vstr(value);
m_Keys.replace(m_Key, std::move(vstr));
m_Keys.replace(m_Key.c_str(), std::move(vstr));
}
} else if (m_ParseState == PSTATE_GAMEDEFS_SUPPORTED) {
if (strcmp(key, "game") == 0)
@ -476,7 +476,7 @@ SMCResult CGameConfig::ReadSMC_KeyValue(const SMCStates *states, const char *key
int limit = sizeof(m_AddressRead)/sizeof(m_AddressRead[0]);
if (m_AddressLastIsOffset)
{
logger->LogError("[SM] Error parsing Address \"%s\", 'offset' entry must be the last entry (gameconf \"%s\")", m_Address, m_CurFile);
logger->LogError("[SM] Error parsing Address \"%s\", 'offset' entry must be the last entry (gameconf \"%s\")", m_Address.c_str(), m_CurFile);
}
else if (m_AddressReadCount < limit)
{
@ -489,10 +489,10 @@ SMCResult CGameConfig::ReadSMC_KeyValue(const SMCStates *states, const char *key
}
else
{
logger->LogError("[SM] Error parsing Address \"%s\", does not support more than %d read offsets (gameconf \"%s\")", m_Address, limit, m_CurFile);
logger->LogError("[SM] Error parsing Address \"%s\", does not support more than %d read offsets (gameconf \"%s\")", m_Address.c_str(), limit, m_CurFile);
}
} else if (strcmp(key, "signature") == 0) {
strncopy(m_AddressSignature, value, sizeof(m_AddressSignature));
m_AddressSignature = value;
}
} else if (m_ParseState == PSTATE_GAMEDEFS_CUSTOM) {
return m_CustomHandler->ReadSMC_KeyValue(states, key, value);
@ -548,25 +548,24 @@ SMCResult CGameConfig::ReadSMC_LeavingSection(const SMCStates *states)
case PSTATE_GAMEDEFS_OFFSETS_OFFSET:
{
/* Parse the offset... */
if (m_Class[0] != '\0'
&& m_Prop[0] != '\0')
if (!m_Class.empty() && !m_Prop.empty())
{
SendProp *pProp = gamehelpers->FindInSendTable(m_Class, m_Prop);
SendProp *pProp = gamehelpers->FindInSendTable(m_Class.c_str(), m_Prop.c_str());
if (pProp)
{
int val = gamehelpers->GetSendPropOffset(pProp);
m_Offsets.replace(m_offset, val);
m_Props.replace(m_offset, pProp);
m_Offsets.replace(m_offset.c_str(), val);
m_Props.replace(m_offset.c_str(), pProp);
} else {
/* Check if it's a non-default game and no offsets exist */
if (((strcmp(m_Game, "*") != 0) && strcmp(m_Game, "#default") != 0)
&& (!m_Offsets.retrieve(m_offset)))
if ((m_Game != "*" && m_Game != "#default")
&& (!m_Offsets.retrieve(m_offset.c_str())))
{
logger->LogError("[SM] Unable to find property %s.%s (file \"%s\") (mod \"%s\")",
m_Class,
m_Prop,
m_Class.c_str(),
m_Prop.c_str(),
m_CurFile,
m_Game);
m_Game.c_str());
}
}
}
@ -674,7 +673,7 @@ SMCResult CGameConfig::ReadSMC_LeavingSection(const SMCStates *states)
}
}
m_Sigs.replace(m_offset, final_addr);
m_Sigs.replace(m_offset.c_str(), final_addr);
}
m_ParseState = PSTATE_GAMEDEFS_SIGNATURES;
@ -690,10 +689,10 @@ SMCResult CGameConfig::ReadSMC_LeavingSection(const SMCStates *states)
{
m_ParseState = PSTATE_GAMEDEFS_ADDRESSES;
if (m_Address[0] != '\0' && m_AddressSignature[0] != '\0')
if (!m_Address.empty() && !m_AddressSignature.empty())
{
AddressConf addrConf(m_AddressSignature, sizeof(m_AddressSignature), m_AddressReadCount, m_AddressRead, m_AddressLastIsOffset);
m_Addresses.replace(m_Address, addrConf);
AddressConf addrConf(std::move(m_AddressSignature), m_AddressReadCount, m_AddressRead, m_AddressLastIsOffset);
m_Addresses.replace(m_Address.c_str(), addrConf);
}
break;
@ -1041,7 +1040,7 @@ bool CGameConfig::GetAddress(const char *key, void **retaddr)
AddressConf &addrConf = r->value;
void *addr;
if (!GetMemSig(addrConf.signatureName, &addr))
if (!GetMemSig(addrConf.signatureName.c_str(), &addr))
{
*retaddr = NULL;
return false;
@ -1074,11 +1073,11 @@ static inline unsigned minOf(unsigned a, unsigned b)
return a <= b ? a : b;
}
CGameConfig::AddressConf::AddressConf(char *sigName, unsigned sigLength, unsigned readCount, int *read, bool lastIsOffset)
CGameConfig::AddressConf::AddressConf(std::string&& sigName, unsigned readCount, int *read, bool lastIsOffset)
{
unsigned readLimit = minOf(readCount, sizeof(this->read) / sizeof(this->read[0]));
strncopy(signatureName, sigName, sizeof(signatureName) / sizeof(signatureName[0]));
this->signatureName = std::move(sigName);
this->readCount = readLimit;
memcpy(&this->read[0], read, sizeof(this->read[0])*readLimit);

View File

@ -86,11 +86,11 @@ private:
/* Parse states */
int m_ParseState;
unsigned int m_IgnoreLevel;
char m_Class[64];
char m_Prop[64];
char m_offset[64];
char m_Game[256];
char m_Key[64];
std::string m_Class;
std::string m_Prop;
std::string m_offset;
std::string m_Game;
std::string m_Key;
bool bShouldBeReadingDefault;
bool had_game;
bool matched_game;
@ -105,18 +105,18 @@ private:
/* Support for reading Addresses */
struct AddressConf
{
char signatureName[64];
std::string signatureName;
int readCount;
int read[8];
bool lastIsOffset;
AddressConf(char *sigName, unsigned sigLength, unsigned readCount, int *read, bool lastIsOffset);
AddressConf(std::string&& sigName, unsigned readCount, int *read, bool lastIsOffset);
AddressConf() {}
};
char m_Address[64];
char m_AddressSignature[64];
std::string m_Address;
std::string m_AddressSignature;
int m_AddressReadCount;
int m_AddressRead[8];
bool m_AddressLastIsOffset;