Skip to content

Commit e404977

Browse files
authored
refactor: Simplify some if-else condition chains (#1683)
1 parent 5f6a948 commit e404977

File tree

6 files changed

+344
-286
lines changed

6 files changed

+344
-286
lines changed

Generals/Code/GameEngine/Source/Common/Recorder.cpp

Lines changed: 114 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1480,117 +1480,142 @@ void RecorderClass::appendNextCommand() {
14801480
}
14811481

14821482
void RecorderClass::readArgument(GameMessageArgumentDataType type, GameMessage *msg) {
1483-
if (type == ARGUMENTDATATYPE_INTEGER) {
1484-
Int theint;
1485-
m_file->read(&theint, sizeof(theint));
1486-
msg->appendIntegerArgument(theint);
1483+
switch (type) {
1484+
case ARGUMENTDATATYPE_INTEGER: {
1485+
Int theint;
1486+
m_file->read(&theint, sizeof(theint));
1487+
msg->appendIntegerArgument(theint);
14871488
#ifdef DEBUG_LOGGING
1488-
if (m_doingAnalysis)
1489-
{
1490-
DEBUG_LOG(("Integer argument: %d (%8.8X)", theint, theint));
1491-
}
1489+
if (m_doingAnalysis)
1490+
{
1491+
DEBUG_LOG(("Integer argument: %d (%8.8X)", theint, theint));
1492+
}
14921493
#endif
1493-
} else if (type == ARGUMENTDATATYPE_REAL) {
1494-
Real thereal;
1495-
m_file->read(&thereal, sizeof(thereal));
1496-
msg->appendRealArgument(thereal);
1497-
#ifdef DEBUG_LOGGING
1498-
if (m_doingAnalysis)
1499-
{
1500-
DEBUG_LOG(("Real argument: %g (%8.8X)", thereal, *(int *)&thereal));
1494+
break;
15011495
}
1502-
#endif
1503-
} else if (type == ARGUMENTDATATYPE_BOOLEAN) {
1504-
Bool thebool;
1505-
m_file->read(&thebool, sizeof(thebool));
1506-
msg->appendBooleanArgument(thebool);
1496+
case ARGUMENTDATATYPE_REAL: {
1497+
Real thereal;
1498+
m_file->read(&thereal, sizeof(thereal));
1499+
msg->appendRealArgument(thereal);
15071500
#ifdef DEBUG_LOGGING
1508-
if (m_doingAnalysis)
1509-
{
1510-
DEBUG_LOG(("Bool argument: %d", thebool));
1511-
}
1501+
if (m_doingAnalysis)
1502+
{
1503+
DEBUG_LOG(("Real argument: %g (%8.8X)", thereal, *(int *)&thereal));
1504+
}
15121505
#endif
1513-
} else if (type == ARGUMENTDATATYPE_OBJECTID) {
1514-
ObjectID theid;
1515-
m_file->read(&theid, sizeof(theid));
1516-
msg->appendObjectIDArgument(theid);
1517-
#ifdef DEBUG_LOGGING
1518-
if (m_doingAnalysis)
1519-
{
1520-
DEBUG_LOG(("Object ID argument: %d", theid));
1506+
break;
15211507
}
1522-
#endif
1523-
} else if (type == ARGUMENTDATATYPE_DRAWABLEID) {
1524-
DrawableID theid;
1525-
m_file->read(&theid, sizeof(theid));
1526-
msg->appendDrawableIDArgument(theid);
1508+
case ARGUMENTDATATYPE_BOOLEAN: {
1509+
Bool thebool;
1510+
m_file->read(&thebool, sizeof(thebool));
1511+
msg->appendBooleanArgument(thebool);
15271512
#ifdef DEBUG_LOGGING
1528-
if (m_doingAnalysis)
1529-
{
1530-
DEBUG_LOG(("Drawable ID argument: %d", theid));
1531-
}
1513+
if (m_doingAnalysis)
1514+
{
1515+
DEBUG_LOG(("Bool argument: %d", thebool));
1516+
}
15321517
#endif
1533-
} else if (type == ARGUMENTDATATYPE_TEAMID) {
1534-
UnsignedInt theid;
1535-
m_file->read(&theid, sizeof(theid));
1536-
msg->appendTeamIDArgument(theid);
1537-
#ifdef DEBUG_LOGGING
1538-
if (m_doingAnalysis)
1539-
{
1540-
DEBUG_LOG(("Team ID argument: %d", theid));
1518+
break;
15411519
}
1542-
#endif
1543-
} else if (type == ARGUMENTDATATYPE_LOCATION) {
1544-
Coord3D loc;
1545-
m_file->read(&loc, sizeof(loc));
1546-
msg->appendLocationArgument(loc);
1520+
case ARGUMENTDATATYPE_OBJECTID: {
1521+
ObjectID theid;
1522+
m_file->read(&theid, sizeof(theid));
1523+
msg->appendObjectIDArgument(theid);
15471524
#ifdef DEBUG_LOGGING
1548-
if (m_doingAnalysis)
1549-
{
1550-
DEBUG_LOG(("Coord3D argument: %g %g %g (%8.8X %8.8X %8.8X)", loc.x, loc.y, loc.z,
1551-
*(int *)&loc.x, *(int *)&loc.y, *(int *)&loc.z));
1552-
}
1525+
if (m_doingAnalysis)
1526+
{
1527+
DEBUG_LOG(("Object ID argument: %d", theid));
1528+
}
15531529
#endif
1554-
} else if (type == ARGUMENTDATATYPE_PIXEL) {
1555-
ICoord2D pixel;
1556-
m_file->read(&pixel, sizeof(pixel));
1557-
msg->appendPixelArgument(pixel);
1558-
#ifdef DEBUG_LOGGING
1559-
if (m_doingAnalysis)
1560-
{
1561-
DEBUG_LOG(("Pixel argument: %d,%d", pixel.x, pixel.y));
1530+
break;
15621531
}
1532+
case ARGUMENTDATATYPE_DRAWABLEID: {
1533+
DrawableID theid;
1534+
m_file->read(&theid, sizeof(theid));
1535+
msg->appendDrawableIDArgument(theid);
1536+
#ifdef DEBUG_LOGGING
1537+
if (m_doingAnalysis)
1538+
{
1539+
DEBUG_LOG(("Drawable ID argument: %d", theid));
1540+
}
15631541
#endif
1564-
} else if (type == ARGUMENTDATATYPE_PIXELREGION) {
1565-
IRegion2D reg;
1566-
m_file->read(&reg, sizeof(reg));
1567-
msg->appendPixelRegionArgument(reg);
1542+
break;
1543+
}
1544+
case ARGUMENTDATATYPE_TEAMID: {
1545+
UnsignedInt theid;
1546+
m_file->read(&theid, sizeof(theid));
1547+
msg->appendTeamIDArgument(theid);
15681548
#ifdef DEBUG_LOGGING
1569-
if (m_doingAnalysis)
1570-
{
1571-
DEBUG_LOG(("Pixel Region argument: %d,%d -> %d,%d", reg.lo.x, reg.lo.y, reg.hi.x, reg.hi.y));
1549+
if (m_doingAnalysis)
1550+
{
1551+
DEBUG_LOG(("Team ID argument: %d", theid));
1552+
}
1553+
#endif
1554+
break;
15721555
}
1556+
case ARGUMENTDATATYPE_LOCATION: {
1557+
Coord3D loc;
1558+
m_file->read(&loc, sizeof(loc));
1559+
msg->appendLocationArgument(loc);
1560+
#ifdef DEBUG_LOGGING
1561+
if (m_doingAnalysis)
1562+
{
1563+
DEBUG_LOG(("Coord3D argument: %g %g %g (%8.8X %8.8X %8.8X)", loc.x, loc.y, loc.z,
1564+
*(int *)&loc.x, *(int *)&loc.y, *(int *)&loc.z));
1565+
}
15731566
#endif
1574-
} else if (type == ARGUMENTDATATYPE_TIMESTAMP) { // Not to be confused with Terrance Stamp... Kneel before Zod!!!
1575-
UnsignedInt stamp;
1576-
m_file->read(&stamp, sizeof(stamp));
1577-
msg->appendTimestampArgument(stamp);
1567+
break;
1568+
}
1569+
case ARGUMENTDATATYPE_PIXEL: {
1570+
ICoord2D pixel;
1571+
m_file->read(&pixel, sizeof(pixel));
1572+
msg->appendPixelArgument(pixel);
15781573
#ifdef DEBUG_LOGGING
1579-
if (m_doingAnalysis)
1580-
{
1581-
DEBUG_LOG(("Timestamp argument: %d", stamp));
1574+
if (m_doingAnalysis)
1575+
{
1576+
DEBUG_LOG(("Pixel argument: %d,%d", pixel.x, pixel.y));
1577+
}
1578+
#endif
1579+
break;
15821580
}
1581+
case ARGUMENTDATATYPE_PIXELREGION: {
1582+
IRegion2D reg;
1583+
m_file->read(&reg, sizeof(reg));
1584+
msg->appendPixelRegionArgument(reg);
1585+
#ifdef DEBUG_LOGGING
1586+
if (m_doingAnalysis)
1587+
{
1588+
DEBUG_LOG(("Pixel Region argument: %d,%d -> %d,%d", reg.lo.x, reg.lo.y, reg.hi.x, reg.hi.y));
1589+
}
15831590
#endif
1584-
} else if (type == ARGUMENTDATATYPE_WIDECHAR) {
1585-
WideChar theid;
1586-
m_file->read(&theid, sizeof(theid));
1587-
msg->appendWideCharArgument(theid);
1591+
break;
1592+
}
1593+
case ARGUMENTDATATYPE_TIMESTAMP: { // Not to be confused with Terrance Stamp... Kneel before Zod!!!
1594+
UnsignedInt stamp;
1595+
m_file->read(&stamp, sizeof(stamp));
1596+
msg->appendTimestampArgument(stamp);
15881597
#ifdef DEBUG_LOGGING
1589-
if (m_doingAnalysis)
1590-
{
1591-
DEBUG_LOG(("WideChar argument: %d (%lc)", theid, theid));
1598+
if (m_doingAnalysis)
1599+
{
1600+
DEBUG_LOG(("Timestamp argument: %d", stamp));
1601+
}
1602+
#endif
1603+
break;
15921604
}
1605+
case ARGUMENTDATATYPE_WIDECHAR: {
1606+
WideChar theid;
1607+
m_file->read(&theid, sizeof(theid));
1608+
msg->appendWideCharArgument(theid);
1609+
#ifdef DEBUG_LOGGING
1610+
if (m_doingAnalysis)
1611+
{
1612+
DEBUG_LOG(("WideChar argument: %d (%lc)", theid, theid));
1613+
}
15931614
#endif
1615+
break;
1616+
}
1617+
default:
1618+
break;
15941619
}
15951620
}
15961621

Generals/Code/GameEngine/Source/GameNetwork/NAT.cpp

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -907,36 +907,11 @@ void NAT::processManglerResponse(UnsignedShort mangledPort) {
907907
// check to see if we've completed all the rounds
908908
// this is kind of a cheesy way to check, but it works.
909909
Bool NAT::allConnectionsDone() {
910-
if (m_numNodes == 2) {
911-
if (m_connectionRound >= 1) {
912-
return TRUE;
913-
}
914-
} else if (m_numNodes == 3) {
915-
if (m_connectionRound >= 3) {
916-
return TRUE;
917-
}
918-
} else if (m_numNodes == 4) {
919-
if (m_connectionRound >= 3) {
920-
return TRUE;
921-
}
922-
} else if (m_numNodes == 5) {
923-
if (m_connectionRound >= 5) {
924-
return TRUE;
925-
}
926-
} else if (m_numNodes == 6) {
927-
if (m_connectionRound >= 5) {
928-
return TRUE;
929-
}
930-
} else if (m_numNodes == 7) {
931-
if (m_connectionRound >= 7) {
932-
return TRUE;
933-
}
934-
} else if (m_numNodes == 8) {
935-
if (m_connectionRound >= 7) {
936-
return TRUE;
937-
}
910+
if (m_numNodes < 2) {
911+
return FALSE;
938912
}
939-
return FALSE;
913+
const Int requiredRounds = (m_numNodes & 1) ? m_numNodes : m_numNodes - 1;
914+
return m_connectionRound >= requiredRounds;
940915
}
941916

942917
Bool NAT::allConnectionsDoneThisRound() {

Generals/Code/GameEngine/Source/GameNetwork/udp.cpp

Lines changed: 54 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -324,33 +324,62 @@ UDP::sockStat UDP::GetStatus(void)
324324
Int status = m_lastError;
325325
#ifdef _WINDOWS
326326
//int status=WSAGetLastError();
327-
if (status==0) return(OK);
328-
else if (status==WSAEINTR) return(INTR);
329-
else if (status==WSAEINPROGRESS) return(INPROGRESS);
330-
else if (status==WSAECONNREFUSED) return(CONNREFUSED);
331-
else if (status==WSAEINVAL) return(INVAL);
332-
else if (status==WSAEISCONN) return(ISCONN);
333-
else if (status==WSAENOTSOCK) return(NOTSOCK);
334-
else if (status==WSAETIMEDOUT) return(TIMEDOUT);
335-
else if (status==WSAEALREADY) return(ALREADY);
336-
else if (status==WSAEWOULDBLOCK) return(WOULDBLOCK);
337-
else if (status==WSAEBADF) return(BADF);
338-
else return((UDP::sockStat)status);
327+
switch (status) {
328+
case NO_ERROR:
329+
return OK;
330+
case WSAEINTR:
331+
return INTR;
332+
case WSAEINPROGRESS:
333+
return INPROGRESS;
334+
case WSAECONNREFUSED:
335+
return CONNREFUSED;
336+
case WSAEINVAL:
337+
return INVAL;
338+
case WSAEISCONN:
339+
return ISCONN;
340+
case WSAENOTSOCK:
341+
return NOTSOCK;
342+
case WSAETIMEDOUT:
343+
return TIMEDOUT;
344+
case WSAEALREADY:
345+
return ALREADY;
346+
case WSAEWOULDBLOCK:
347+
return WOULDBLOCK;
348+
case WSAEBADF:
349+
return BADF;
350+
default:
351+
return (UDP::sockStat)status;
352+
}
339353
#else
340354
//int status=errno;
341-
if (status==0) return(OK);
342-
else if (status==EINTR) return(INTR);
343-
else if (status==EINPROGRESS) return(INPROGRESS);
344-
else if (status==ECONNREFUSED) return(CONNREFUSED);
345-
else if (status==EINVAL) return(INVAL);
346-
else if (status==EISCONN) return(ISCONN);
347-
else if (status==ENOTSOCK) return(NOTSOCK);
348-
else if (status==ETIMEDOUT) return(TIMEDOUT);
349-
else if (status==EALREADY) return(ALREADY);
350-
else if (status==EAGAIN) return(AGAIN);
351-
else if (status==EWOULDBLOCK) return(WOULDBLOCK);
352-
else if (status==EBADF) return(BADF);
353-
else return(UNKNOWN);
355+
switch (status) {
356+
case 0:
357+
return OK;
358+
case EINTR:
359+
return INTR;
360+
case EINPROGRESS:
361+
return INPROGRESS;
362+
case ECONNREFUSED:
363+
return CONNREFUSED;
364+
case EINVAL:
365+
return INVAL;
366+
case EISCONN:
367+
return ISCONN;
368+
case ENOTSOCK:
369+
return NOTSOCK;
370+
case ETIMEDOUT:
371+
return TIMEDOUT;
372+
case EALREADY:
373+
return ALREADY;
374+
case EAGAIN:
375+
return AGAIN;
376+
case EWOULDBLOCK:
377+
return WOULDBLOCK;
378+
case EBADF:
379+
return BADF;
380+
default:
381+
return UNKNOWN;
382+
}
354383
#endif
355384
}
356385

0 commit comments

Comments
 (0)