This repository has been archived by the owner on Sep 19, 2022. It is now read-only.
forked from arves100/ffloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DPMsg.cpp
232 lines (193 loc) · 6.1 KB
/
DPMsg.cpp
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
/*!
@author Arves100
@brief DirectPlay message reimplementation
@date 08/02/2022
@file DPMsg.cpp
*/
#include "stdafx.h"
#include "DPMsg.h"
#include "Globals.h"
struct DPNameNet
{
char shortName[30];
char longName[100];
};
ENetPacket* DPMsg::DestroyPlayer(const std::shared_ptr<DPPlayer>& player)
{
DPMSG_DESTROYPLAYERORGROUP msg;
msg.dwType = DPSYS_DESTROYPLAYERORGROUP;
msg.dwPlayerType = DPPLAYERTYPE_PLAYER;
msg.dpId = player->GetId();
msg.lpLocalData = nullptr; // See above
msg.dwLocalDataSize = player->GetLocalDataSize();
msg.dwRemoteDataSize = player->GetRemoteDataSize();
msg.lpRemoteData = nullptr; // See avobe
msg.dpIdParent = 0;
msg.dwFlags = player->IsSpecator() ? DPPLAYER_SPECTATOR : 0;
msg.dwFlags |= player->IsMadeByHost() ? DPPLAYER_SERVERPLAYER : 0;
DPMsg dpMsg(player->GetId(), DPID_SYSMSG, DPMSG_TYPE_SYSTEM);
dpMsg.AddToSerialize(msg);
return dpMsg.Serialize();
}
ENetPacket* DPMsg::NewPlayer(const std::shared_ptr<DPPlayer>& player, DWORD oldPlayer)
{
DPMSG_CREATEPLAYERORGROUP msg;
msg.dwType = DPSYS_CREATEPLAYERORGROUP;
msg.dwPlayerType = DPPLAYERTYPE_PLAYER;
msg.dpId = player->GetId();
msg.dwCurrentPlayers = oldPlayer;
msg.lpData = nullptr;
msg.dwDataSize = player->GetLocalDataSize();
DPNAME name;
name.dwSize = sizeof(name);
name.dwFlags = 0;
name.lpszLongNameA = nullptr;
name.lpszShortNameA = nullptr;
msg.dpnName = name;
msg.dpIdParent = 0;
msg.dwFlags = 0;
DPMsg dpMsg(DPID_SYSMSG, DPID_SYSMSG, DPMSG_TYPE_SYSTEM);
dpMsg.AddToSerialize(msg);
DPNameNet netName;
strcpy_s(netName.shortName, _countof(netName.shortName), player->GetShortName());
strcpy_s(netName.longName, _countof(netName.longName), player->GetLongName());
dpMsg.AddToSerialize(netName);
if (msg.dwDataSize)
dpMsg.AddToSerialize(player->GetLocalData(), msg.dwDataSize);
return dpMsg.Serialize();
}
ENetPacket* DPMsg::CallNewId(LPDPNAME lpData)
{
DPMsg msg(0, 0, DPMSG_TYPE_CALL_NEWID);
DPPlayerInfo nfo = { 0 };
if (lpData->lpszLongNameA)
strncpy_s(nfo.longName, 100, lpData->lpszLongNameA, 100);
if (lpData->lpszShortNameA)
strncpy_s(nfo.name, 40, lpData->lpszShortNameA, 40);
msg.AddToSerialize(nfo);
return msg.Serialize();
}
ENetPacket* DPMsg::NewId(DPID id)
{
DPMsg msg(DPID_SYSMSG, DPID_SYSMSG, DPMSG_TYPE_NEWID);
msg.AddToSerialize(id);
return msg.Serialize();
}
ENetPacket* DPMsg::CreateRoomInfo(GUID roomId, DWORD maxPlayers, DWORD currPlayers, const char* sessionName, DWORD user[4], DWORD dwFlags)
{
DPGameInfo info;
info.session = roomId;
info.maxPlayers = maxPlayers;
info.currPlayers = currPlayers;
strncpy_s(info.sessionName, _countof(info.sessionName), sessionName, 100);
memcpy_s(info.user, sizeof(info.user), user, sizeof(info.user));
info.flags = dwFlags;
DPMsg msg(DPID_SYSMSG, DPID_SYSMSG, DPMSG_TYPE_GAME_INFO);
msg.AddToSerialize(info);
return msg.Serialize();
}
ENetPacket* DPMsg::ChatPacket(DPID from, DPID to, bool reliable, LPDPCHAT chatMsg)
{
DPMSG_CHAT chat;
chat.dwType = DPSYS_CHAT;
chat.dwFlags = 0;
chat.idFromPlayer = from;
chat.idToGroup = 0;
chat.idToPlayer = to;
chat.lpChat = chatMsg;
DPMsg msg(from, to, DPMSG_TYPE_SYSTEM);
msg.AddToSerialize(chat);
msg.AddToSerialize(chatMsg, sizeof(DPCHAT));
size_t len = strlen(chatMsg->lpszMessageA);
msg.AddToSerialize(len);
msg.AddToSerialize(chatMsg->lpszMessageA, len + 1);
return msg.Serialize(reliable ? ENET_PACKET_FLAG_RELIABLE : 0);
}
ENetPacket* DPMsg::CreatePlayerRemote(const std::shared_ptr<DPPlayer>& player, bool reliable)
{
DPMsg msg(player->GetId(), 0, DPMSG_TYPE_REMOTEINFO);
DWORD m = player->GetRemoteDataSize();
msg.AddToSerialize(m);
if (m > 0)
msg.AddToSerialize(player->GetRemoteData(), m);
return msg.Serialize(reliable ? ENET_PACKET_FLAG_RELIABLE : 0);
}
ENetPacket* DPMsg::CreateSendComplete(DPID idFrom, DPID idTo, DWORD dwFlags, DWORD dwPriority, DWORD dwTimeout, LPVOID lpContext, DWORD lpdwMsgID, HRESULT hr, DWORD dwSendTime)
{
DPMsg msg(idFrom, idTo, DPMSG_TYPE_SYSTEM);
DPMSG_SENDCOMPLETE msg2;
msg2.dwType = DPSYS_SENDCOMPLETE;
msg2.dwTimeout = dwTimeout;
msg2.idFrom = idFrom;
msg2.idTo = idTo;
msg2.dwFlags = dwFlags;
msg2.dwPriority = dwPriority;
msg2.dwMsgID = lpdwMsgID;
msg2.lpvContext = lpContext;
msg2.hr = hr;
msg2.dwSendTime = dwSendTime;
msg.AddToSerialize(msg2);
return msg.Serialize();
}
/*!
* @brief Translates internal network messages to DirectPlay messages
* @return HResult error code or DP_OK in case of success
* @param lpData Pointer of the data to store
* @param lpDataSize Pointer of the size of the data to store
*/
HRESULT_INT DPMsg::FixSysMessage(LPVOID lpData, LPDWORD lpDataSize)
{
ResetRead();
auto p = (DPMSG_GENERIC*)Read2(sizeof(DPMSG_GENERIC));
DWORD reqSize = 0;
ResetRead();
if (!p)
return DPERR_GENERIC;
auto arena = Globals::Get()->TheArena;
switch (p->dwType)
{
case DPSYS_SENDCOMPLETE:
{
DPMSG_SENDCOMPLETE* msg = (DPMSG_SENDCOMPLETE*)Read2(sizeof(DPMSG_SENDCOMPLETE));
reqSize = sizeof(DPMSG_SENDCOMPLETE);
break;
}
case DPSYS_CREATEPLAYERORGROUP:
{
DPMSG_CREATEPLAYERORGROUP* msg = (DPMSG_CREATEPLAYERORGROUP*)Read2(sizeof(DPMSG_CREATEPLAYERORGROUP));
DPNameNet* nm = (DPNameNet*)arena->Store(Read2(sizeof(DPNameNet)), sizeof(DPNameNet));
if (msg->dwDataSize)
msg->lpData = arena->Store(Read2(msg->dwDataSize), msg->dwDataSize);
msg->dpnName.lpszLongNameA = nm->longName;
msg->dpnName.lpszShortNameA = nm->shortName;
reqSize = sizeof(DPMSG_CREATEPLAYERORGROUP);
break;
}
case DPSYS_DESTROYPLAYERORGROUP:
{
DPMSG_DESTROYPLAYERORGROUP* msg = (DPMSG_DESTROYPLAYERORGROUP*)Read2(sizeof(DPMSG_DESTROYPLAYERORGROUP));
reqSize = sizeof(DPMSG_DESTROYPLAYERORGROUP);
break;
}
case DPSYS_CHAT:
{
DPMSG_CHAT* msg = (DPMSG_CHAT*)Read2(sizeof(DPMSG_CHAT));
LPDPCHAT lpc = (LPDPCHAT)arena->Store(Read2(sizeof(DPCHAT)), sizeof(DPCHAT));
msg->lpChat = lpc;
size_t len;
Read(len);
len += 1;
lpc->lpszMessageA = (LPSTR)arena->Store(Read2(len), len);
reqSize = sizeof(DPMSG_CHAT);
break;
}
default:
return DPERR_INVALIDOBJECT;
}
if (lpData && *lpDataSize < reqSize)
return DPERR_BUFFERTOOSMALL;
if (lpData)
memcpy_s(lpData, *lpDataSize, p, reqSize);
*lpDataSize = reqSize;
return DP_OK;
}