forked from electronicarts/CnC_Generals_Zero_Hour
-
Notifications
You must be signed in to change notification settings - Fork 202
Expand file tree
/
Copy pathLANAPI.h
More file actions
460 lines (400 loc) · 21.4 KB
/
LANAPI.h
File metadata and controls
460 lines (400 loc) · 21.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/*
** Command & Conquer Generals Zero Hour(tm)
** Copyright 2025 Electronic Arts Inc.
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
////////////////////////////////////////////////////////////////////////////////
// //
// (c) 2001-2003 Electronic Arts Inc. //
// //
////////////////////////////////////////////////////////////////////////////////
// LANAPI.h ///////////////////////////////////////////////////////////////
// LANAPI singleton class - defines interface to LAN broadcast communications
// Author: Matthew D. Campbell, October 2001
#pragma once
#include "GameNetwork/Transport.h"
#include "GameNetwork/NetworkInterface.h"
#include "GameNetwork/NetworkDefs.h"
#include "GameNetwork/LANPlayer.h"
#include "GameNetwork/LANGameInfo.h"
//static const Int g_lanPlayerNameLength = 20;
static const Int g_lanPlayerNameLength = 12; // reduced length because of game option length
//static const Int g_lanLoginNameLength = 16;
//static const Int g_lanHostNameLength = 16;
static const Int g_lanLoginNameLength = 1;
static const Int g_lanHostNameLength = 1;
//static const Int g_lanGameNameLength = 32;
static const Int g_lanGameNameLength = 16; // reduced length because of game option length
static const Int g_lanGameNameReservedLength = 16; // save N wchars for ID info
static const Int g_lanMaxChatLength = 100;
static const Int m_lanMaxOptionsLength = MAX_LANAPI_PACKET_SIZE - ( 8 + (g_lanGameNameLength+1)*2 + 4 + (g_lanPlayerNameLength+1)*2
+ (g_lanLoginNameLength+1) + (g_lanHostNameLength+1) );
static const Int g_maxSerialLength = 23; // including the trailing '\0'
struct LANMessage;
/**
* The LANAPI class is used to instantiate a singleton which
* implements the interface to all LAN broadcast communications.
*/
class LANAPIInterface : public SubsystemInterface
{
public:
virtual ~LANAPIInterface() { };
virtual void init() = 0; ///< Initialize or re-initialize the instance
virtual void reset() = 0; ///< reset the logic system
virtual void update() = 0; ///< update the world
virtual void setIsActive(Bool isActive ) = 0; ///< Tell TheLAN whether or not the app is active.
// Possible types of chat messages
enum ChatType
{
LANCHAT_NORMAL = 0,
LANCHAT_EMOTE,
LANCHAT_SYSTEM,
};
// Request functions generate network traffic
virtual void RequestLocations() = 0; ///< Request everybody to respond with where they are
virtual void RequestGameJoin( LANGameInfo *game, UnsignedInt ip = 0 ) = 0; ///< Request to join a game
virtual void RequestGameJoinDirectConnect( UnsignedInt ipaddress ) = 0; ///< Request to join a game at an IP address
virtual void RequestGameLeave() = 0; ///< Tell everyone we're leaving
virtual void RequestAccept() = 0; ///< Indicate we're OK with the game options
virtual void RequestHasMap() = 0; ///< Send our map status
virtual void RequestChat( UnicodeString message, ChatType format ) = 0; ///< Send a chat message
virtual void RequestGameStart() = 0; ///< Tell everyone the game is starting
virtual void RequestGameStartTimer( Int seconds ) = 0;
virtual void RequestGameOptions( AsciiString gameOptions, Bool isPublic, UnsignedInt ip = 0 ) = 0; ///< Change the game options
virtual void RequestGameCreate( UnicodeString gameName, Bool isDirectConnect ) = 0; ///< Try to host a game
virtual void RequestGameAnnounce() = 0; ///< Sound out current game info if host
// virtual void RequestSlotList() = 0; ///< Pump out the Slot info.
virtual void RequestSetName( UnicodeString newName ) = 0; ///< Pick a new name
virtual void RequestLobbyLeave( Bool forced ) = 0; ///< Announce that we're leaving the lobby
virtual void ResetGameStartTimer() = 0;
// Possible result codes passed to On functions
enum ReturnType
{
RET_OK, // Any function
RET_TIMEOUT, // OnGameJoin/Leave/Start, etc
RET_GAME_FULL, // OnGameJoin
RET_DUPLICATE_NAME, // OnGameJoin
RET_CRC_MISMATCH, // OnGameJoin
RET_SERIAL_DUPE, // OnGameJoin
RET_GAME_STARTED, // OnGameJoin
RET_GAME_EXISTS, // OnGameCreate
RET_GAME_GONE, // OnGameJoin
RET_BUSY, // OnGameCreate/Join/etc if another action is in progress
RET_UNKNOWN, // Default message for oddity
};
UnicodeString getErrorStringFromReturnType( ReturnType ret );
// On functions are (generally) the result of network traffic
virtual void OnGameList( LANGameInfo *gameList ) = 0; ///< List of games
virtual void OnPlayerList( LANPlayer *playerList ) = 0; ///< List of players in the Lobby
virtual void OnGameJoin( ReturnType ret, LANGameInfo *theGame ) = 0; ///< Did we get in the game?
virtual void OnPlayerJoin( Int slot, UnicodeString playerName ) = 0; ///< Someone else joined our game (host only; joiners get a slotlist)
virtual void OnHostLeave() = 0; ///< Host left the game
virtual void OnPlayerLeave( UnicodeString player ) = 0; ///< Someone left the game
virtual void OnAccept( UnsignedInt playerIP, Bool status ) = 0; ///< Someone's accept status changed
virtual void OnHasMap( UnsignedInt playerIP, Bool status ) = 0; ///< Someone's map status changed
virtual void OnChat( UnicodeString player, UnsignedInt ip,
UnicodeString message, ChatType format ) = 0; ///< Chat message from someone
virtual void OnGameStart() = 0; ///< The game is starting
virtual void OnGameStartTimer( Int seconds ) = 0;
virtual void OnGameOptions( UnsignedInt playerIP, Int playerSlot, AsciiString options ) = 0; ///< Someone sent game options
virtual void OnGameCreate( ReturnType ret ) = 0; ///< Your game is created
// virtual void OnSlotList( ReturnType ret, LANGameInfo *theGame ) = 0; ///< Slotlist for a game in setup
virtual void OnNameChange( UnsignedInt IP, UnicodeString newName ) = 0; ///< Someone has morphed
// Misc utility functions
virtual LANGameInfo * LookupGame( UnicodeString gameName ) = 0; ///< return a pointer to a game we know about
virtual LANGameInfo * LookupGameByListOffset( Int offset ) = 0; ///< return a pointer to a game we know about
virtual LANGameInfo * LookupGameByHost( UnsignedInt hostIP ) = 0; ///< return a pointer to the most recent game associated to the host IP address
virtual Bool SetLocalIP( UnsignedInt localIP ) = 0; ///< For multiple NIC machines
virtual void SetLocalIP( AsciiString localIP ) = 0; ///< For multiple NIC machines
virtual Bool AmIHost() = 0; ///< Am I hosting a game?
virtual inline UnicodeString GetMyName() = 0; ///< What's my name?
virtual inline LANGameInfo *GetMyGame() = 0; ///< What's my Game?
virtual void fillInLANMessage( LANMessage *msg ) = 0; ///< Fill in default params
virtual void checkMOTD() = 0;
};
/**
* LAN message class
*/
#pragma pack(push, 1)
struct LANMessage
{
enum Type ///< What kind of message are we?
{
// Locating everybody
MSG_REQUEST_LOCATIONS, ///< Hey, where is everybody?
MSG_GAME_ANNOUNCE, ///< Here I am, and here's my game info!
MSG_LOBBY_ANNOUNCE, ///< Hey, I'm in the lobby!
// Joining games
MSG_REQUEST_JOIN, ///< Let me in! Let me in!
MSG_JOIN_ACCEPT, ///< Okay, you can join.
MSG_JOIN_DENY, ///< Go away! We don't want any!
// Leaving games
MSG_REQUEST_GAME_LEAVE, ///< I want to leave the game
MSG_REQUEST_LOBBY_LEAVE,///< I'm leaving the lobby
// Game options, chat, etc
MSG_SET_ACCEPT, ///< I'm cool with everything as is.
MSG_MAP_AVAILABILITY, ///< I do (not) have the map.
MSG_CHAT, ///< Just spouting my mouth off.
MSG_GAME_START, ///< Hold on; we're starting!
MSG_GAME_START_TIMER, ///< The game will start in N seconds
MSG_GAME_OPTIONS, ///< Here's some info about the game.
MSG_INACTIVE, ///< I've alt-tabbed out. Unaccept me cause I'm a poo-flinging monkey.
MSG_REQUEST_GAME_INFO, ///< For direct connect, get the game info from a specific IP Address
// TheSuperHackers @feature Caball009 05/02/2026 Product information is exchanged on demand and never broadcast.
// A client is considered 'patched' if it responds to a product info request (or, in pre-match, if it sends one).
// The implementation consists of three parts.
// 1. player - player in lobby:
// - When a player detects a new player in the lobby, it sends a product info request to that player.
// - If the other player responds with an acknowledgement, they are considered patched.
// 2. player - host in lobby:
// - When a player detects a new game host in the lobby, it sends a product info request to that host.
// - If the host responds with an acknowledgement, it is considered patched.
// 3. players in pre-match (game room):
// - When a player joins a match, it sends a product info request to all players in that match.
// - Existing players treat this request as confirmation that the joining player is patched (no explicit acknowledgement required).
MSG_GAME_REQUEST_PRODUCT_INFO = 1000,
MSG_GAME_RESPONSE_PRODUCT_INFO,
MSG_LOBBY_REQUEST_PRODUCT_INFO,
MSG_LOBBY_RESPONSE_PRODUCT_INFO,
MSG_MATCH_REQUEST_PRODUCT_INFO,
MSG_MATCH_RESPONSE_PRODUCT_INFO,
} messageType;
WideChar name[g_lanPlayerNameLength+1]; ///< My name, for convenience
char userName[g_lanLoginNameLength+1]; ///< login name, for convenience
char hostName[g_lanHostNameLength+1]; ///< machine name, for convenience
// No additional data is required for REQUEST_LOCATIONS, LOBBY_ANNOUNCE,
// REQUEST_LOBBY_LEAVE, GAME_START.
union
{
// StartTimer is sent with GAME_START_TIMER
struct
{
Int seconds;
} StartTimer;
// GameJoined is sent with REQUEST_GAME_LEAVE
struct
{
WideChar gameName[g_lanGameNameLength+1];
} GameToLeave;
// GameInfo if sent with GAME_ANNOUNCE
struct
{
WideChar gameName[g_lanGameNameLength+1];
Bool inProgress;
char options[m_lanMaxOptionsLength+1];
Bool isDirectConnect;
} GameInfo;
// PlayerInfo is sent with REQUEST_GAME_INFO for direct connect games.
struct
{
UnsignedInt ip;
WideChar playerName[g_lanPlayerNameLength+1];
} PlayerInfo;
// GameToJoin is sent with REQUEST_JOIN
struct
{
UnsignedInt gameIP;
UnsignedInt exeCRC;
UnsignedInt iniCRC;
char serial[g_maxSerialLength];
} GameToJoin;
// GameJoined is sent with JOIN_ACCEPT
struct
{
WideChar gameName[g_lanGameNameLength+1];
UnsignedInt gameIP;
UnsignedInt playerIP;
Int slotPosition;
} GameJoined;
// GameNotJoined is sent with JOIN_DENY
struct
{
WideChar gameName[g_lanGameNameLength+1];
UnsignedInt gameIP;
UnsignedInt playerIP;
LANAPIInterface::ReturnType reason;
} GameNotJoined;
// Accept is sent with SET_ACCEPT
struct
{
WideChar gameName[g_lanGameNameLength+1];
Bool isAccepted;
} Accept;
// Accept is sent with MAP_AVAILABILITY
struct
{
WideChar gameName[g_lanGameNameLength+1];
UnsignedInt mapCRC; // to make sure we're talking about the same map
Bool hasMap;
} MapStatus;
// Chat is sent with CHAT
struct
{
WideChar gameName[g_lanGameNameLength+1];
LANAPIInterface::ChatType chatType;
WideChar message[g_lanMaxChatLength+1];
} Chat;
// GameOptions is sent with GAME_OPTIONS
struct
{
char options[m_lanMaxOptionsLength+1];
} GameOptions;
// ProductInfo is sent with REQUEST_PRODUCT_INFO and RESPONSE_PRODUCT_INFO
struct
{
UnsignedInt flags;
UnsignedInt upTime;
UnsignedInt exeCRC;
UnsignedInt iniCRC;
UnsignedInt fpMathCRC;
WideChar data[201];
} ProductInfo;
};
};
#pragma pack(pop)
static_assert(sizeof(LANMessage) <= MAX_LANAPI_PACKET_SIZE, "LANMessage struct cannot be larger than the max packet size");
/**
* The LANAPI class is used to instantiate a singleton which
* implements the interface to all LAN broadcast communications.
*/
class LANAPI : public LANAPIInterface
{
public:
LANAPI();
virtual ~LANAPI();
virtual void init(); ///< Initialize or re-initialize the instance
virtual void reset(); ///< reset the logic system
virtual void update(); ///< update the world
virtual void setIsActive(Bool isActive); ///< tell TheLAN whether or not
// Request functions generate network traffic
virtual void RequestLocations(); ///< Request everybody to respond with where they are
virtual void RequestGameJoin( LANGameInfo *game, UnsignedInt ip = 0 ); ///< Request to join a game
virtual void RequestGameJoinDirectConnect( UnsignedInt ipaddress ); ///< Request to join a game at an IP address
virtual void RequestGameLeave(); ///< Tell everyone we're leaving
virtual void RequestAccept(); ///< Indicate we're OK with the game options
virtual void RequestHasMap(); ///< Send our map status
virtual void RequestChat( UnicodeString message, ChatType format ); ///< Send a chat message
virtual void RequestGameStart(); ///< Tell everyone the game is starting
virtual void RequestGameStartTimer( Int seconds );
virtual void RequestGameOptions( AsciiString gameOptions, Bool isPublic, UnsignedInt ip = 0 ); ///< Change the game options
virtual void RequestGameCreate( UnicodeString gameName, Bool isDirectConnect ); ///< Try to host a game
virtual void RequestGameAnnounce(); ///< Send out game info if host
virtual void RequestSetName( UnicodeString newName ); ///< Pick a new name
// virtual void RequestSlotList(); ///< Pump out the Slot info.
virtual void RequestLobbyLeave( Bool forced ); ///< Announce that we're leaving the lobby
virtual void ResetGameStartTimer();
// On functions are (generally) the result of network traffic
virtual void OnGameList( LANGameInfo *gameList ); ///< List of games
virtual void OnPlayerList( LANPlayer *playerList ); ///< List of players in the Lobby
virtual void OnGameJoin( ReturnType ret, LANGameInfo *theGame ); ///< Did we get in the game?
virtual void OnPlayerJoin( Int slot, UnicodeString playerName ); ///< Someone else joined our game (host only; joiners get a slotlist)
virtual void OnHostLeave(); ///< Host left the game
virtual void OnPlayerLeave( UnicodeString player ); ///< Someone left the game
virtual void OnAccept( UnsignedInt playerIP, Bool status ); ///< Someone's accept status changed
virtual void OnHasMap( UnsignedInt playerIP, Bool status ); ///< Someone's map status changed
virtual void OnChat( UnicodeString player, UnsignedInt ip,
UnicodeString message, ChatType format ); ///< Chat message from someone
virtual void OnGameStart(); ///< The game is starting
virtual void OnGameStartTimer( Int seconds );
virtual void OnGameOptions( UnsignedInt playerIP, Int playerSlot, AsciiString options ); ///< Someone sent game options
virtual void OnGameCreate( ReturnType ret ); ///< Your game is created
//virtual void OnSlotList( ReturnType ret, LANGameInfo *theGame ); ///< Slotlist for a game in setup
virtual void OnNameChange( UnsignedInt IP, UnicodeString newName ); ///< Someone has morphed
virtual void OnInActive( UnsignedInt IP ); ///< Someone has alt-tabbed out.
// Misc utility functions
virtual LANGameInfo * LookupGame( UnicodeString gameName ); ///< return a pointer to a game we know about
virtual LANGameInfo * LookupGameByListOffset( Int offset ); ///< return a pointer to a game we know about
virtual LANGameInfo * LookupGameByHost( UnsignedInt hostIP ); ///< return a pointer to the most recent game associated to the host IP address
virtual LANPlayer * LookupPlayer( UnsignedInt playerIP ); ///< return a pointer to a player we know about
virtual Bool SetLocalIP( UnsignedInt localIP ); ///< For multiple NIC machines
virtual void SetLocalIP( AsciiString localIP ); ///< For multiple NIC machines
virtual Bool AmIHost(); ///< Am I hosting a game?
virtual UnicodeString GetMyName() { return m_name; } ///< What's my name?
virtual LANGameInfo* GetMyGame() { return m_currentGame; } ///< What's my Game?
virtual UnsignedInt GetLocalIP() { return m_localIP; } ///< What's my IP?
virtual void fillInLANMessage( LANMessage *msg ); ///< Fill in default params
virtual void checkMOTD();
protected:
enum PendingActionType
{
ACT_NONE = 0,
ACT_JOIN,
ACT_JOINDIRECTCONNECT,
ACT_LEAVE,
};
static const UnsignedInt s_resendDelta; // in ms
protected:
LANPlayer * m_lobbyPlayers; ///< List of players in the lobby
LANGameInfo * m_games; ///< List of games
UnicodeString m_name; ///< Who do we think we are?
AsciiString m_userName; ///< login name
AsciiString m_hostName; ///< machine name
UnsignedInt m_gameStartTime;
Int m_gameStartSeconds;
PendingActionType m_pendingAction; ///< What action are we performing?
UnsignedInt m_expiration; ///< When should we give up on our action?
UnsignedInt m_actionTimeout;
UnsignedInt m_directConnectRemoteIP;///< The IP address of the game we are direct connecting to.
// Resend timer ---------------------------------------------------------------------------
UnsignedInt m_lastResendTime; // in ms
Bool m_isInLANMenu; ///< true while we are in a LAN menu (lobby, game options, direct connect)
Bool m_inLobby; ///< Are we in the lobby (not in a game)?
LANGameInfo * m_currentGame; ///< Pointer to game (setup screen) we are currently in (null for lobby)
//LANGameInfo *m_currentGameInfo; ///< Pointer to game setup info we are currently in.
UnsignedInt m_localIP;
Transport* m_transport;
UnsignedInt m_broadcastAddr;
UnsignedInt m_lastUpdate;
AsciiString m_lastGameopt; /// @todo: hack for demo - remove this
Bool m_isActive; ///< is the game currently active?
LANMessage m_productInfoMessage; ///< store product info message to avoid having to recreate it multiple times
protected:
void sendMessage(LANMessage *msg, UnsignedInt ip = 0, Bool broadcast = TRUE); // Convenience function
void removePlayer(LANPlayer *player);
void removeGame(LANGameInfo *game);
void addPlayer(LANPlayer *player);
void addGame(LANGameInfo *game);
AsciiString createSlotString();
static UnsignedInt buildProductInfoFlags();
static void setProductInfoFromLocalData(GameSlot &slot);
static void setProductInfoFromMessage(GameSlot &slot, LANMessage *msg);
static Bool setProductInfoStrings(const UnicodeString(&input)[4], WideChar(&output)[201]);
static Bool getProductInfoStrings(WideChar(&input)[201], UnicodeString*(&output)[4]);
// Functions to handle incoming messages -----------------------------------
void handleRequestLocations( LANMessage *msg, UnsignedInt senderIP );
void handleGameAnnounce( LANMessage *msg, UnsignedInt senderIP );
void handleLobbyAnnounce( LANMessage *msg, UnsignedInt senderIP );
void handleRequestGameInfo( LANMessage *msg, UnsignedInt senderIP );
void handleRequestJoin( LANMessage *msg, UnsignedInt senderIP );
void handleJoinAccept( LANMessage *msg, UnsignedInt senderIP );
void handleJoinDeny( LANMessage *msg, UnsignedInt senderIP );
void handleRequestGameLeave( LANMessage *msg, UnsignedInt senderIP );
void handleRequestLobbyLeave( LANMessage *msg, UnsignedInt senderIP );
void handleSetAccept( LANMessage *msg, UnsignedInt senderIP );
void handleHasMap( LANMessage *msg, UnsignedInt senderIP );
void handleChat( LANMessage *msg, UnsignedInt senderIP );
void handleGameStart( LANMessage *msg, UnsignedInt senderIP );
void handleGameStartTimer( LANMessage *msg, UnsignedInt senderIP );
void handleGameOptions( LANMessage *msg, UnsignedInt senderIP );
void handleInActive( LANMessage *msg, UnsignedInt senderIP );
static LANMessage buildProductInfoMessage();
void sendProductInfoMessage(LANMessage::Type messageType, UnsignedInt senderIP);
void handleGameProductInfoRequest(LANMessage *msg, UnsignedInt senderIP);
void handleGameProductInfoResponse(LANMessage *msg, UnsignedInt senderIP);
void handleLobbyProductInfoRequest(LANMessage *msg, UnsignedInt senderIP);
void handleLobbyProductInfoResponse(LANMessage *msg, UnsignedInt senderIP);
void handleMatchProductInfoRequest(LANMessage *msg, UnsignedInt senderIP);
void handleMatchProductInfoResponse(LANMessage *msg, UnsignedInt senderIP);
};