Skip to content

Commit 412d586

Browse files
committed
Revert "Revert "Revert "Fix #1258: Add mixed hex and RGB(A) parsing to XMLColorToInt (#1261)"""
This reverts commit febb856.
1 parent 47f0ab4 commit 412d586

File tree

3 files changed

+26
-125
lines changed

3 files changed

+26
-125
lines changed

Server/mods/deathmatch/logic/CVehicle.cpp

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -301,20 +301,28 @@ bool CVehicle::ReadSpecialData(const int iLine)
301301
char szTemp[256];
302302
if (GetCustomDataString("color", szTemp, 256, true))
303303
{
304-
std::vector<SColorRGBA> vecColors;
305-
unsigned char ucCount;
304+
uchar ucValues[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
305+
char* sz1 = strtok(szTemp, ", ");
306+
if (sz1)
307+
ucValues[0] = atoi(sz1);
306308

307-
if (ColorStringToRGB(szTemp, SColorRGBA(0, 0, 0, 0), vecColors, ucCount))
309+
int i;
310+
for (i = 1; i < 12; i++)
308311
{
309-
if (ucCount % 3 == 0)
310-
m_Color.SetRGBColors(vecColors[0], vecColors[1], vecColors[2], vecColors[3]);
311-
else
312-
m_Color.SetPaletteColors(vecColors[0].R, vecColors[0].G, vecColors[0].B, vecColors[1].R);
312+
char* szn = strtok(NULL, ", ");
313+
if (!szn)
314+
break;
315+
ucValues[i] = atoi(szn);
316+
}
317+
318+
if (i == 3 || i == 6 || i == 9 || i == 12)
319+
{
320+
m_Color.SetRGBColors(SColorRGBA(ucValues[0], ucValues[1], ucValues[2], 0), SColorRGBA(ucValues[3], ucValues[4], ucValues[5], 0),
321+
SColorRGBA(ucValues[6], ucValues[7], ucValues[8], 0), SColorRGBA(ucValues[9], ucValues[10], ucValues[11], 0));
313322
}
314323
else
315324
{
316-
CLogger::ErrorPrintf("Bad 'color' value specified in <vehicle> (line %u)\n", iLine);
317-
return false;
325+
m_Color.SetPaletteColors(ucValues[0], ucValues[1], ucValues[2], ucValues[3]);
318326
}
319327
}
320328

Shared/mods/deathmatch/logic/Utils.cpp

Lines changed: 9 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -689,120 +689,18 @@ bool XMLColorToInt(const char* szColor, unsigned long& ulColor)
689689

690690
bool XMLColorToInt(const char* szColor, unsigned char& ucRed, unsigned char& ucGreen, unsigned char& ucBlue, unsigned char& ucAlpha)
691691
{
692-
// If we're empty, let's just stop right away
693-
if (!szColor || strlen(szColor) == 0)
694-
return false;
695-
696-
std::vector<SColorRGBA> vecColors;
697-
unsigned char ucCount;
698-
699-
if (ColorStringToRGBA(szColor, SColorRGBA(ucRed, ucGreen, ucBlue, ucAlpha), vecColors, ucCount))
700-
{
701-
ucRed = vecColors[0].R;
702-
ucGreen = vecColors[0].G;
703-
ucBlue = vecColors[0].B;
704-
ucAlpha = vecColors[0].A;
705-
706-
return true;
707-
}
708-
709-
return false;
710-
}
711-
712-
bool ColorStringToRGBA(const char* szColor, SColorRGBA defaultColor, std::vector<SColorRGBA>& vecColors, unsigned char& ucCount, bool bIgnoreAlpha)
713-
{
714-
std::stringstream ss(szColor);
715-
SColorRGBA color = defaultColor;
716-
bool bPreviousWasHex = false;
717-
unsigned int uiRGBAIndex = 0;
718-
unsigned long ulColor;
719-
unsigned char ucValue;
720-
unsigned char ucLength = bIgnoreAlpha ? 3 : 4;
721-
722-
while (ss.good())
692+
// Convert it to an integer first
693+
unsigned long ulColor;
694+
if (!XMLColorToInt(szColor, ulColor))
723695
{
724-
// Ambiguous value before a comma
725-
SString strValue;
726-
getline(ss, strValue, ',');
727-
728-
// Remove spaces
729-
ReplaceOccurrencesInString(strValue, " ", "");
730-
731-
// Is the value looking like a hexadecimal?
732-
if (strValue[0] == '#')
733-
{
734-
// Try converting it to an integer
735-
if (XMLColorToInt(strValue.c_str(), ulColor))
736-
{
737-
// If a previous RGBA wasn't finished, let's finish it now
738-
if (!bPreviousWasHex && uiRGBAIndex != 0)
739-
{
740-
vecColors.push_back(color);
741-
ucCount += ucLength - uiRGBAIndex;
742-
color = defaultColor;
743-
}
744-
745-
color.R = static_cast<unsigned char>(ulColor);
746-
color.G = static_cast<unsigned char>(ulColor >> 8);
747-
color.B = static_cast<unsigned char>(ulColor >> 16);
748-
749-
if (!bIgnoreAlpha)
750-
color.A = static_cast<unsigned char>(ulColor >> 24);
751-
752-
bPreviousWasHex = true;
753-
ucCount += ucLength;
754-
}
755-
else
756-
return false;
757-
}
758-
// It looks like we have an empty value, let's skip the value but treat it as RGB
759-
else if (strValue.empty())
760-
{
761-
if (bPreviousWasHex || uiRGBAIndex % ucLength == 0)
762-
{
763-
bPreviousWasHex = false;
764-
uiRGBAIndex = 0;
765-
}
766-
767-
uiRGBAIndex++;
768-
ucCount++;
769-
770-
if (uiRGBAIndex % ucLength != 0 && ss.good())
771-
continue;
772-
}
773-
// It looks like a plain number so let's treat it as a RGBA value
774-
else if (strValue.find_first_not_of("0123456789") == std::string::npos)
775-
{
776-
ucValue = atoi(strValue.c_str());
777-
778-
if (bPreviousWasHex || uiRGBAIndex % ucLength == 0)
779-
{
780-
color.R = ucValue;
781-
bPreviousWasHex = false;
782-
uiRGBAIndex = 0;
783-
}
784-
else if (uiRGBAIndex % ucLength == 1)
785-
color.G = ucValue;
786-
else if (uiRGBAIndex % ucLength == 2)
787-
color.B = ucValue;
788-
else if (uiRGBAIndex % ucLength == 3)
789-
color.A = ucValue;
790-
791-
uiRGBAIndex++;
792-
ucCount++;
793-
794-
if (uiRGBAIndex % ucLength != 0 && ss.good())
795-
continue;
796-
}
797-
else
798-
return false;
799-
800-
// We have a color, so let's push it
801-
vecColors.push_back(color);
802-
color = defaultColor;
803-
uiRGBAIndex = 0;
696+
return false;
804697
}
805698

699+
// Convert it to red, green, blue and alpha
700+
ucRed = static_cast<unsigned char>(ulColor);
701+
ucGreen = static_cast<unsigned char>(ulColor >> 8);
702+
ucBlue = static_cast<unsigned char>(ulColor >> 16);
703+
ucAlpha = static_cast<unsigned char>(ulColor >> 24);
806704
return true;
807705
}
808706

Shared/mods/deathmatch/logic/Utils.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,6 @@ bool IsValidOrganizationPath(const char* szPath);
139139
unsigned int HexToInt(const char* szHex);
140140
bool XMLColorToInt(const char* szColor, unsigned long& ulColor);
141141
bool XMLColorToInt(const char* szColor, unsigned char& ucRed, unsigned char& ucGreen, unsigned char& ucBlue, unsigned char& ucAlpha);
142-
bool ColorStringToRGBA(const char* szColor, SColorRGBA defaultColor, std::vector<SColorRGBA>& vecColors, unsigned char& ucCount, bool bIgnoreAlpha = false);
143-
inline bool ColorStringToRGB(const char* szColor, SColorRGBA defaultColor, std::vector<SColorRGBA>& vecColors, unsigned char& ucCount)
144-
{
145-
return ColorStringToRGBA(szColor, defaultColor, vecColors, ucCount, true);
146-
}
147142

148143
inline float WrapAround(float fValue, float fHigh)
149144
{

0 commit comments

Comments
 (0)