1.1 --- a/core/smn_usermsgs.cpp Mon Jan 07 09:09:14 2013 -0500
1.2 +++ b/core/smn_usermsgs.cpp Tue Jan 22 21:43:12 2013 -0500
1.3 @@ -31,15 +31,22 @@
1.4
1.5 #include "HandleSys.h"
1.6 #include "PluginSys.h"
1.7 -#include "UserMessages.h"
1.8 #include "PlayerManager.h"
1.9 #include "smn_usermsgs.h"
1.10 +#ifdef USE_PROTOBUF_USERMESSAGES
1.11 +#include "UserMessagePBHelpers.h"
1.12 +#endif
1.13
1.14 -HandleType_t g_WrBitBufType;
1.15 -HandleType_t g_RdBitBufType;
1.16 +HandleType_t g_ProtobufType = NO_HANDLE_TYPE;
1.17 +HandleType_t g_WrBitBufType = NO_HANDLE_TYPE;
1.18 +HandleType_t g_RdBitBufType = NO_HANDLE_TYPE;
1.19 +
1.20 Handle_t g_CurMsgHandle;
1.21 +
1.22 +#ifndef USE_PROTOBUF_USERMESSAGES
1.23 Handle_t g_ReadBufHandle;
1.24 bf_read g_ReadBitBuf;
1.25 +#endif
1.26
1.27 int g_MsgPlayers[256];
1.28 bool g_IsMsgInExec = false;
1.29 @@ -85,10 +92,14 @@
1.30 g_HandleSys.InitAccessDefaults(NULL, &sec);
1.31 sec.access[HandleAccess_Delete] = HANDLE_RESTRICT_IDENTITY;
1.32
1.33 +#ifdef USE_PROTOBUF_USERMESSAGES
1.34 + g_ProtobufType = g_HandleSys.CreateType("ProtobufUM", this, 0, NULL, NULL, g_pCoreIdent, NULL);
1.35 +#else
1.36 g_WrBitBufType = g_HandleSys.CreateType("BitBufWriter", this, 0, NULL, NULL, g_pCoreIdent, NULL);
1.37 g_RdBitBufType = g_HandleSys.CreateType("BitBufReader", this, 0, NULL, &sec, g_pCoreIdent, NULL);
1.38
1.39 g_ReadBufHandle = g_HandleSys.CreateHandle(g_RdBitBufType, &g_ReadBitBuf, NULL, g_pCoreIdent, NULL);
1.40 +#endif
1.41
1.42 g_PluginSys.AddPluginsListener(this);
1.43 }
1.44 @@ -98,6 +109,11 @@
1.45 HandleSecurity sec;
1.46 sec.pIdentity = g_pCoreIdent;
1.47
1.48 +#ifdef USE_PROTOBUF_USERMESSAGES
1.49 + g_HandleSys.RemoveType(g_ProtobufType, g_pCoreIdent);
1.50 +
1.51 + g_ProtobufType = 0;
1.52 +#else
1.53 g_HandleSys.FreeHandle(g_ReadBufHandle, &sec);
1.54
1.55 g_HandleSys.RemoveType(g_WrBitBufType, g_pCoreIdent);
1.56 @@ -105,15 +121,24 @@
1.57
1.58 g_WrBitBufType = 0;
1.59 g_RdBitBufType = 0;
1.60 +#endif
1.61 }
1.62
1.63 void UsrMessageNatives::OnHandleDestroy(HandleType_t type, void *object)
1.64 {
1.65 +#ifdef USE_PROTOBUF_USERMESSAGES
1.66 + delete (SMProtobufMessage *)object;
1.67 +#endif
1.68 }
1.69
1.70 bool UsrMessageNatives::GetHandleApproxSize(HandleType_t type, void *object, unsigned int *pSize)
1.71 {
1.72 +#ifdef USE_PROTOBUF_USERMESSAGES
1.73 + // Different messages have different sizes, but this works as an approximate
1.74 + *pSize = sizeof(protobuf::Message) + sizeof(SMProtobufMessage);
1.75 +#else
1.76 *pSize = sizeof(bf_read);
1.77 +#endif
1.78
1.79 return true;
1.80 }
1.81 @@ -275,37 +300,69 @@
1.82 return m_Notify;
1.83 }
1.84
1.85 +#ifdef USE_PROTOBUF_USERMESSAGES
1.86 +void MsgListenerWrapper::OnUserMessage(int msg_id, protobuf::Message *msg, IRecipientFilter *pFilter)
1.87 +#else
1.88 void MsgListenerWrapper::OnUserMessage(int msg_id, bf_write *bf, IRecipientFilter *pFilter)
1.89 +#endif
1.90 {
1.91 cell_t res;
1.92 + Handle_t hndl;
1.93 size_t size = _FillInPlayers(g_MsgPlayers, pFilter);
1.94
1.95 +#ifdef USE_PROTOBUF_USERMESSAGES
1.96 + hndl = g_HandleSys.CreateHandle(g_ProtobufType, new SMProtobufMessage(msg), NULL, g_pCoreIdent, NULL);
1.97 +#else
1.98 g_ReadBitBuf.StartReading(bf->GetBasePointer(), bf->GetNumBytesWritten());
1.99 + hndl = g_ReadBufHandle;
1.100 +#endif
1.101
1.102 m_Hook->PushCell(msg_id);
1.103 - m_Hook->PushCell(g_ReadBufHandle);
1.104 + m_Hook->PushCell(hndl);
1.105 m_Hook->PushArray(g_MsgPlayers, size);
1.106 m_Hook->PushCell(size);
1.107 m_Hook->PushCell(pFilter->IsReliable());
1.108 m_Hook->PushCell(pFilter->IsInitMessage());
1.109 m_Hook->Execute(&res);
1.110 +
1.111 +#ifdef USE_PROTOBUF_USERMESSAGES
1.112 + HandleSecurity sec;
1.113 + sec.pIdentity = g_pCoreIdent;
1.114 + g_HandleSys.FreeHandle(hndl, &sec);
1.115 +#endif
1.116 }
1.117
1.118 +#ifdef USE_PROTOBUF_USERMESSAGES
1.119 +ResultType MsgListenerWrapper::InterceptUserMessage(int msg_id, protobuf::Message *msg, IRecipientFilter *pFilter)
1.120 +#else
1.121 ResultType MsgListenerWrapper::InterceptUserMessage(int msg_id, bf_write *bf, IRecipientFilter *pFilter)
1.122 +#endif
1.123 {
1.124 + Handle_t hndl;
1.125 cell_t res = static_cast<cell_t>(Pl_Continue);
1.126 size_t size = _FillInPlayers(g_MsgPlayers, pFilter);
1.127
1.128 +#ifdef USE_PROTOBUF_USERMESSAGES
1.129 + hndl = g_HandleSys.CreateHandle(g_ProtobufType, new SMProtobufMessage(msg), NULL, g_pCoreIdent, NULL);
1.130 +#else
1.131 g_ReadBitBuf.StartReading(bf->GetBasePointer(), bf->GetNumBytesWritten());
1.132 + hndl = g_ReadBufHandle;
1.133 +#endif
1.134
1.135 m_Intercept->PushCell(msg_id);
1.136 - m_Intercept->PushCell(g_ReadBufHandle);
1.137 + m_Intercept->PushCell(hndl);
1.138 m_Intercept->PushArray(g_MsgPlayers, size);
1.139 m_Intercept->PushCell(size);
1.140 m_Intercept->PushCell(pFilter->IsReliable());
1.141 m_Intercept->PushCell(pFilter->IsInitMessage());
1.142 m_Intercept->Execute(&res);
1.143
1.144 +#ifdef USE_PROTOBUF_USERMESSAGES
1.145 + HandleSecurity sec;
1.146 + sec.pIdentity = g_pCoreIdent;
1.147 + g_HandleSys.FreeHandle(hndl, &sec);
1.148 +#endif
1.149 +
1.150 return static_cast<ResultType>(res);
1.151 }
1.152
1.153 @@ -330,6 +387,11 @@
1.154
1.155 static UsrMessageNatives s_UsrMessageNatives;
1.156
1.157 +static cell_t smn_GetUserMessageType(IPluginContext *pCtx, const cell_t *params)
1.158 +{
1.159 + return g_UserMsgs.GetUserMessageType();
1.160 +}
1.161 +
1.162 static cell_t smn_GetUserMessageId(IPluginContext *pCtx, const cell_t *params)
1.163 {
1.164 char *msgname;
1.165 @@ -353,7 +415,6 @@
1.166 cell_t *cl_array;
1.167 unsigned int numClients;
1.168 int msgid;
1.169 - bf_write *pBitBuf;
1.170 int client;
1.171 CPlayer *pPlayer = NULL;
1.172
1.173 @@ -387,13 +448,24 @@
1.174 }
1.175 }
1.176
1.177 - pBitBuf = g_UserMsgs.StartMessage(msgid, cl_array, numClients, params[4]);
1.178 +#ifdef USE_PROTOBUF_USERMESSAGES
1.179 + protobuf::Message *msg = g_UserMsgs.StartProtobufMessage(msgid, cl_array, numClients, params[4]);
1.180 + if (!msg)
1.181 + {
1.182 + return pCtx->ThrowNativeError("Unable to execute a new message while in hook");
1.183 + }
1.184 +
1.185 + g_CurMsgHandle = g_HandleSys.CreateHandle(g_ProtobufType, new SMProtobufMessage(msg), pCtx->GetIdentity(), g_pCoreIdent, NULL);
1.186 +#else
1.187 + bf_write *pBitBuf = g_UserMsgs.StartBitBufMessage(msgid, cl_array, numClients, params[4]);
1.188 if (!pBitBuf)
1.189 {
1.190 return pCtx->ThrowNativeError("Unable to execute a new message while in hook");
1.191 }
1.192
1.193 g_CurMsgHandle = g_HandleSys.CreateHandle(g_WrBitBufType, pBitBuf, pCtx->GetIdentity(), g_pCoreIdent, NULL);
1.194 +#endif
1.195 +
1.196 g_IsMsgInExec = true;
1.197
1.198 return g_CurMsgHandle;
1.199 @@ -403,7 +475,6 @@
1.200 {
1.201 cell_t *cl_array;
1.202 unsigned int numClients;
1.203 - bf_write *pBitBuf;
1.204 int client;
1.205 CPlayer *pPlayer = NULL;
1.206 int msgid = params[1];
1.207 @@ -436,13 +507,23 @@
1.208 }
1.209 }
1.210
1.211 - pBitBuf = g_UserMsgs.StartMessage(msgid, cl_array, numClients, params[4]);
1.212 +#ifdef USE_PROTOBUF_USERMESSAGES
1.213 + protobuf::Message *msg = g_UserMsgs.StartProtobufMessage(msgid, cl_array, numClients, params[4]);
1.214 + if (!msg)
1.215 + {
1.216 + return pCtx->ThrowNativeError("Unable to execute a new message while in hook");
1.217 + }
1.218 +
1.219 + g_CurMsgHandle = g_HandleSys.CreateHandle(g_ProtobufType, new SMProtobufMessage(msg), pCtx->GetIdentity(), g_pCoreIdent, NULL);
1.220 +#else
1.221 + bf_write *pBitBuf = g_UserMsgs.StartBitBufMessage(msgid, cl_array, numClients, params[4]);
1.222 if (!pBitBuf)
1.223 {
1.224 return pCtx->ThrowNativeError("Unable to execute a new message while in hook");
1.225 }
1.226
1.227 g_CurMsgHandle = g_HandleSys.CreateHandle(g_WrBitBufType, pBitBuf, pCtx->GetIdentity(), g_pCoreIdent, NULL);
1.228 +#endif
1.229 g_IsMsgInExec = true;
1.230
1.231 return g_CurMsgHandle;
1.232 @@ -534,6 +615,7 @@
1.233
1.234 REGISTER_NATIVES(usrmsgnatives)
1.235 {
1.236 + {"GetUserMessageType", smn_GetUserMessageType},
1.237 {"GetUserMessageId", smn_GetUserMessageId},
1.238 {"GetUserMessageName", smn_GetUserMessageName},
1.239 {"StartMessage", smn_StartMessage},