core/UserMessages.cpp
changeset 3778 4c210a5b3824
parent 3759 bf45d8b36818
child 3779 b269413e1258
     1.1 --- a/core/UserMessages.cpp	Sun Feb 10 09:44:33 2013 -0500
     1.2 +++ b/core/UserMessages.cpp	Sun Feb 10 23:06:26 2013 -0500
     1.3 @@ -493,116 +493,16 @@
     1.4  	}
     1.5  }
     1.6  
     1.7 -#ifdef USE_PROTOBUF_USERMESSAGES
     1.8 -// Unlike protobuf's Message::CopyFrom:
     1.9 -// - Does not require to/from message descriptors to be identical.
    1.10 -// - Fields are copied by name, rather than index.
    1.11 -// - String fields are retrieved for copy with GetStringReference, to avoid destucting libprotobuf std::string in sm.
    1.12 -// - Unknown fields in |from| message are not copied to |to| message.
    1.13 -// - Copying Message fields recursively calls this function, rather than Message::MergeFrom.
    1.14 -static void _CopyProtobufMessage(const protobuf::Message &from, protobuf::Message *to)
    1.15 -{
    1.16 -	const protobuf::Descriptor* descriptor = from.GetDescriptor();
    1.17 -
    1.18 -	const protobuf::Reflection* from_reflection = from.GetReflection();
    1.19 -	const protobuf::Reflection* to_reflection = to->GetReflection();
    1.20 -
    1.21 -	protobuf::UnknownFieldSet toUnknownFields;
    1.22 -
    1.23 -	int fromFieldCount = descriptor->field_count();
    1.24 -	for (int i = 0; i < fromFieldCount; i++)
    1.25 -	{
    1.26 -		const protobuf::FieldDescriptor* fromField = descriptor->field(i);
    1.27 -		const protobuf::FieldDescriptor* toField = to->GetDescriptor()->FindFieldByName(fromField->name());
    1.28 -
    1.29 -		// This will stripped new fields off of intercepted usermessages.
    1.30 -		// TODO: fix that.
    1.31 -		if (!toField)
    1.32 -		{
    1.33 -			continue;
    1.34 -		}
    1.35 -
    1.36 -		if (fromField->is_repeated())
    1.37 -		{
    1.38 -			int count = from_reflection->FieldSize(from, fromField);
    1.39 -			for (int j = 0; j < count; j++)
    1.40 -			{
    1.41 -				switch (fromField->cpp_type())
    1.42 -				{
    1.43 -#define HANDLE_TYPE(CPPTYPE, METHOD)                                           \
    1.44 -				case protobuf::FieldDescriptor::CPPTYPE_##CPPTYPE:             \
    1.45 -					to_reflection->Add##METHOD(to, toField,                    \
    1.46 -					from_reflection->GetRepeated##METHOD(from, fromField, j)); \
    1.47 -					break;
    1.48 -				HANDLE_TYPE(INT32 , Int32 );
    1.49 -				HANDLE_TYPE(INT64 , Int64 );
    1.50 -				HANDLE_TYPE(UINT32, UInt32);
    1.51 -				HANDLE_TYPE(UINT64, UInt64);
    1.52 -				HANDLE_TYPE(FLOAT , Float );
    1.53 -				HANDLE_TYPE(DOUBLE, Double);
    1.54 -				HANDLE_TYPE(BOOL  , Bool  );
    1.55 -				HANDLE_TYPE(ENUM  , Enum  );
    1.56 -#undef HANDLE_TYPE
    1.57 -				case protobuf::FieldDescriptor::CPPTYPE_STRING:
    1.58 -					{
    1.59 -						std::string buffer;
    1.60 -						to_reflection->AddString(to, toField,
    1.61 -							from_reflection->GetRepeatedStringReference(from, fromField, j, &buffer));
    1.62 -					}
    1.63 -					break;
    1.64 -				case protobuf::FieldDescriptor::CPPTYPE_MESSAGE:
    1.65 -					_CopyProtobufMessage(
    1.66 -						from_reflection->GetRepeatedMessage(from, fromField, j),
    1.67 -						to_reflection->AddMessage(to, toField)
    1.68 -						);
    1.69 -					break;
    1.70 -				}
    1.71 -			}
    1.72 -		}
    1.73 -		else
    1.74 -		{
    1.75 -			switch (fromField->cpp_type())
    1.76 -			{
    1.77 -#define HANDLE_TYPE(CPPTYPE, METHOD)                            \
    1.78 -			case protobuf::FieldDescriptor::CPPTYPE_##CPPTYPE:  \
    1.79 -				to_reflection->Set##METHOD(to, toField,         \
    1.80 -				from_reflection->Get##METHOD(from, fromField)); \
    1.81 -				break;
    1.82 -			HANDLE_TYPE(INT32 , Int32 );
    1.83 -			HANDLE_TYPE(INT64 , Int64 );
    1.84 -			HANDLE_TYPE(UINT32, UInt32);
    1.85 -			HANDLE_TYPE(UINT64, UInt64);
    1.86 -			HANDLE_TYPE(FLOAT , Float );
    1.87 -			HANDLE_TYPE(DOUBLE, Double);
    1.88 -			HANDLE_TYPE(BOOL  , Bool  );
    1.89 -			HANDLE_TYPE(ENUM  , Enum  );
    1.90 -#undef HANDLE_TYPE
    1.91 -			case protobuf::FieldDescriptor::CPPTYPE_STRING:
    1.92 -				{
    1.93 -					std::string buffer;
    1.94 -					to_reflection->SetString(to, toField,
    1.95 -						from_reflection->GetStringReference(from, fromField, &buffer));
    1.96 -				}
    1.97 -				break;
    1.98 -			case protobuf::FieldDescriptor::CPPTYPE_MESSAGE:
    1.99 -				_CopyProtobufMessage(
   1.100 -					from_reflection->GetMessage(from, fromField),
   1.101 -					to_reflection->MutableMessage(to, toField)
   1.102 -					);
   1.103 -				break;
   1.104 -			}
   1.105 -		}
   1.106 -	}
   1.107 -}
   1.108 -#endif
   1.109 -
   1.110  #if SOURCE_ENGINE == SE_CSGO
   1.111  void UserMessages::OnSendUserMessage_Pre(IRecipientFilter &filter, int msg_type, const protobuf::Message &msg)
   1.112  {
   1.113  	OnStartMessage_Pre(&filter, msg_type, g_Cstrike15UsermessageHelpers.GetName(msg_type));
   1.114  	if (m_FakeMetaRes == MRES_SUPERCEDE)
   1.115  	{
   1.116 -		_CopyProtobufMessage(msg, m_InterceptBuffer);
   1.117 +		int size = msg.ByteSize();
   1.118 +		uint8 *data = (uint8 *)stackalloc(size);
   1.119 +		msg.SerializePartialToArray(data, size);
   1.120 +		m_InterceptBuffer->ParseFromArray(data, size);
   1.121  	}
   1.122  	else
   1.123  	{
   1.124 @@ -858,23 +758,33 @@
   1.125  #endif // SE_CSGO
   1.126  	}
   1.127  
   1.128 -	pList = &m_msgHooks[m_CurId];
   1.129 -	for (iter=pList->begin(); iter!=pList->end(); )
   1.130  	{
   1.131 -		pInfo = (*iter);
   1.132 -		pInfo->IsHooked = true;
   1.133 -		pInfo->Callback->OnUserMessage(m_CurId, m_OrigBuffer, m_CurRecFilter);
   1.134 +		int size = m_OrigBuffer->ByteSize();
   1.135 +		uint8 *data = (uint8 *)stackalloc(size);
   1.136 +		m_OrigBuffer->SerializePartialToArray(data, size);
   1.137 +		protobuf::Message *pTempMsg = g_Cstrike15UsermessageHelpers.GetPrototype(m_CurId)->New();
   1.138 +		pTempMsg->ParseFromArray(data, size);
   1.139  
   1.140 -		if (pInfo->KillMe)
   1.141 +		pList = &m_msgHooks[m_CurId];
   1.142 +		for (iter=pList->begin(); iter!=pList->end(); )
   1.143  		{
   1.144 -			iter = pList->erase(iter);
   1.145 -			m_FreeListeners.push(pInfo);
   1.146 -			_DecRefCounter();
   1.147 -			continue;
   1.148 +			pInfo = (*iter);
   1.149 +			pInfo->IsHooked = true;
   1.150 +			pInfo->Callback->OnUserMessage(m_CurId, pTempMsg, m_CurRecFilter);
   1.151 +
   1.152 +			if (pInfo->KillMe)
   1.153 +			{
   1.154 +				iter = pList->erase(iter);
   1.155 +				m_FreeListeners.push(pInfo);
   1.156 +				_DecRefCounter();
   1.157 +				continue;
   1.158 +			}
   1.159 +
   1.160 +			pInfo->IsHooked = false;
   1.161 +			iter++;
   1.162  		}
   1.163  
   1.164 -		pInfo->IsHooked = false;
   1.165 -		iter++;
   1.166 +		delete pTempMsg;
   1.167  	}
   1.168  
   1.169  	UM_RETURN_META((intercepted) ? MRES_SUPERCEDE : MRES_IGNORED);