Skip to content

Commit e09c51c

Browse files
committed
refactor: mirror Zero Hour changes to generals (#2630)
Mirrors the MessageArgument linked list to vector refactors from Zero Hour to Generals.
1 parent 6c8bf63 commit e09c51c

3 files changed

Lines changed: 19 additions & 54 deletions

File tree

Generals/Code/GameEngine/Include/Common/MessageStream.h

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ class GameMessageArgument : public MemoryPoolObject
8282
{
8383
MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(GameMessageArgument, "GameMessageArgument")
8484
public:
85-
GameMessageArgument* m_next; ///< The next argument
8685
GameMessageArgumentType m_data; ///< The data storage of an argument
8786
GameMessageArgumentDataType m_type; ///< The type of the argument.
8887
};
@@ -637,7 +636,6 @@ class GameMessage : public MemoryPoolObject
637636
GameMessage *prev() { return m_prev; } ///< Return prev message in the stream
638637

639638
Type getType() const { return m_type; } ///< Return the message type
640-
UnsignedByte getArgumentCount() const { return m_argCount; } ///< Return the number of arguments for this msg
641639

642640
const char *getCommandAsString() const; ///< returns a string representation of the command type.
643641
static const char *getCommandTypeAsString(GameMessage::Type t);
@@ -660,8 +658,8 @@ class GameMessage : public MemoryPoolObject
660658

661659
/**
662660
* Return the given argument union.
663-
* @todo This should be a more list-like interface. Very inefficient.
664661
*/
662+
UnsignedByte getArgumentCount() const { return static_cast<UnsignedByte>(m_argList.size()); }
665663
const GameMessageArgumentType *getArgument( Int argIndex ) const;
666664
GameMessageArgumentDataType getArgumentDataType( Int argIndex ) const;
667665

@@ -681,10 +679,7 @@ class GameMessage : public MemoryPoolObject
681679

682680
Int m_playerIndex; ///< The Player who issued the command
683681

684-
/// @todo If a GameMessage needs more than 255 arguments, it needs to be split up into multiple GameMessage's.
685-
UnsignedByte m_argCount; ///< The number of arguments of this message
686-
687-
GameMessageArgument *m_argList, *m_argTail; ///< This message's arguments
682+
std::vector<GameMessageArgument*> m_argList; ///< This message's arguments
688683

689684
/// allocate a new argument, add it to list, return pointer to its data
690685
GameMessageArgument *allocArg();

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

Lines changed: 11 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,6 @@ GameMessage::GameMessage( GameMessage::Type type )
5656
{
5757
m_playerIndex = ThePlayerList->getLocalPlayer()->getPlayerIndex();
5858
m_type = type;
59-
m_argList = nullptr;
60-
m_argTail = nullptr;
61-
m_argCount = 0;
6259
m_list = nullptr;
6360
}
6461

@@ -69,12 +66,8 @@ GameMessage::GameMessage( GameMessage::Type type )
6966
GameMessage::~GameMessage()
7067
{
7168
// free all arguments
72-
GameMessageArgument *arg, *nextArg;
73-
74-
for( arg = m_argList; arg; arg=nextArg )
75-
{
76-
nextArg = arg->m_next;
77-
deleteInstance(arg);
69+
for( size_t i = 0; i < m_argList.size(); ++i ) {
70+
deleteInstance(m_argList[i]);
7871
}
7972

8073
// detach message from list
@@ -84,14 +77,11 @@ GameMessage::~GameMessage()
8477

8578
/**
8679
* Return the given argument union.
87-
* @todo This should be a more list-like interface. Very inefficient.
8880
*/
8981
const GameMessageArgumentType *GameMessage::getArgument( Int argIndex ) const
9082
{
91-
int i=0;
92-
for( GameMessageArgument *a = m_argList; a; a=a->m_next, i++ )
93-
if (i == argIndex)
94-
return &a->m_data;
83+
if (static_cast<size_t>(argIndex) < m_argList.size())
84+
return &m_argList[argIndex]->m_data;
9585

9686
DEBUG_CRASH(("argument not found"));
9787
static const GameMessageArgumentType zero = { 0 };
@@ -103,17 +93,9 @@ const GameMessageArgumentType *GameMessage::getArgument( Int argIndex ) const
10393
*/
10494
GameMessageArgumentDataType GameMessage::getArgumentDataType( Int argIndex ) const
10595
{
106-
if (argIndex >= m_argCount) {
107-
return ARGUMENTDATATYPE_UNKNOWN;
108-
}
109-
int i=0;
110-
GameMessageArgument *a = m_argList;
111-
for (; a && (i < argIndex); a=a->m_next, ++i );
96+
if (static_cast<size_t>(argIndex) < m_argList.size())
97+
return m_argList[argIndex]->m_type;
11298

113-
if (a != nullptr)
114-
{
115-
return a->m_type;
116-
}
11799
return ARGUMENTDATATYPE_UNKNOWN;
118100
}
119101

@@ -124,21 +106,12 @@ GameMessageArgument *GameMessage::allocArg()
124106
{
125107
// allocate a new argument
126108
GameMessageArgument *arg = newInstance(GameMessageArgument);
109+
m_argList.push_back(arg);
127110

128-
// add to end of argument list
129-
if (m_argTail)
130-
m_argTail->m_next = arg;
131-
else
132-
{
133-
m_argList = arg;
134-
m_argTail = arg;
135-
}
136-
137-
arg->m_next = nullptr;
138-
m_argTail = arg;
139-
140-
m_argCount++;
141-
111+
DEBUG_ASSERTCRASH(
112+
m_argList.size() <= 255,
113+
("If a GameMessage needs more than 255 arguments, it needs to be split up into multiple GameMessage's.")
114+
);
142115
return arg;
143116
}
144117

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

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -785,15 +785,12 @@ void RecorderClass::writeToFile(GameMessage * msg) {
785785
argType = argType->getNext();
786786
}
787787

788-
// UnsignedByte lasttype = (UnsignedByte)ARGUMENTDATATYPE_UNKNOWN;
789-
Int numArgs = msg->getArgumentCount();
790-
for (Int i = 0; i < numArgs; ++i) {
791-
// UnsignedByte type = (UnsignedByte)(msg->getArgumentDataType(i));
792-
// if (lasttype != type) {
793-
// fwrite(&type, sizeof(type), 1, m_file);
794-
// lasttype = type;
795-
// }
796-
writeArgument(msg->getArgumentDataType(i), *(msg->getArgument(i)));
788+
const size_t argsCount = msg->getArgumentCount();
789+
790+
for (size_t i = 0; i < argsCount; ++i) {
791+
GameMessageArgumentDataType argType = msg->getArgumentDataType(i);
792+
const GameMessageArgumentType* arg = msg->getArgument(i);
793+
writeArgument(argType, *arg);
797794
}
798795

799796
deleteInstance(parser);

0 commit comments

Comments
 (0)