Difference between revisions of "Protobuf"

From AlliedModders Wiki
Jump to: navigation, search
(Added example showing both embedded messages and repeated fields)
(Updated example code with code that should work on SourceMod 1.4)
Line 23: Line 23:
 
From funcommands' drug.sp, using the [http://hg.alliedmods.net/hl2sdks/hl2sdk-csgo/file/9a3c7f5049b6/public/game/shared/csgo/protobuf/cstrike15_usermessages.proto#l204 Fade] usermessage:
 
From funcommands' drug.sp, using the [http://hg.alliedmods.net/hl2sdks/hl2sdk-csgo/file/9a3c7f5049b6/public/game/shared/csgo/protobuf/cstrike15_usermessages.proto#l204 Fade] usermessage:
 
<pawn>
 
<pawn>
 +
// Merge this with your existing OnPluginStart
 +
OnPluginStart()
 +
{
 +
MarkNativeAsOptional(GetUserMessageType);
 +
}
 +
 +
// Elsewhere in the code
 
new clients[2];
 
new clients[2];
 
clients[0] = client;
 
clients[0] = client;
Line 36: Line 43:
 
new Handle:message = StartMessageEx(g_FadeUserMsgId, clients, 1);
 
new Handle:message = StartMessageEx(g_FadeUserMsgId, clients, 1);
 
 
if (GetUserMessageType() == UM_Protobuf)
+
if (GetFeatureStatus(FeatureType_Native, "GetUserMessageType") == FeatureStatus_Available && GetUserMessageType() == UM_Protobuf)
 
{
 
{
 
PbSetInt(message, "duration", duration);
 
PbSetInt(message, "duration", duration);

Revision as of 17:25, 7 March 2013

Intro

Unlike the serial bitbuffer-backed usermessages in older games, newer games such as CS:GO (and DOTA 2) now use Google's Protocol Buffers or "protobuf" to back net messages and usermessages.

Differences

Instead of having to be read or written in order, the protobuf usermessages use defined fields, accessible by name, in any order.

Starting, ending, and hooking usermessages remains unchanged. Reading and writing values to/from them is done using the Pb* set of natives in protobuf.inc instead of the Bf* natives in bitbuffer.inc.

You can tell which usermessage system is in use for the current game by checking GetUserMessageType(). Possible returns are UM_BitBuf and UM_Protobuf.


Basic fields ("optional" or "required") are single values and use the PbRead*/PbSet* natives.

Repeated fields are arrays of values, accessible by their 0-based index with the PbReadRepeated* natives or added with the PbAdd* natives. You can get the count of values in a repeated field with PbGetRepeatedFieldCount.


For message and field names, see the CS:GO Usermessages as defined in protobuf's proto format.

Protobuf natives

See protobuf.inc

Multi-game usermessages example

From funcommands' drug.sp, using the Fade usermessage:

// Merge this with your existing OnPluginStart
OnPluginStart()
{
	MarkNativeAsOptional(GetUserMessageType);
}
 
// Elsewhere in the code
	new clients[2];
	clients[0] = client;	
 
	new duration = 255;
	new holdtime = 255;
	new flags = 0x0002;
	new color[4] = { 0, 0, 0, 128 };
	color[0] = GetRandomInt(0,255);
	color[1] = GetRandomInt(0,255);
	color[2] = GetRandomInt(0,255);
 
	new Handle:message = StartMessageEx(g_FadeUserMsgId, clients, 1);
 
	if (GetFeatureStatus(FeatureType_Native, "GetUserMessageType") == FeatureStatus_Available && GetUserMessageType() == UM_Protobuf)
	{
		PbSetInt(message, "duration", duration);
		PbSetInt(message, "hold_time", holdtime);
		PbSetInt(message, "flags", flags);
		PbSetColor(message, "clr", color);
	}
	else
	{
		BfWriteShort(message, duration);
		BfWriteShort(message, holdtime);
		BfWriteShort(message, flags);
		BfWriteByte(message, color[0]);
		BfWriteByte(message, color[1]);
		BfWriteByte(message, color[2]);
		BfWriteByte(message, color[3]);
	}
 
	EndMessage();

Embedded message example

This example sends a VGUIMenu usermessage, adding values to a repeated field "subkeys" that uses the Subkey message type defined in the VGUIMenu message.

Once an embedded message handle is retrieved with ReadMessage, you can read or write to its fields with the normal PbRead/Set natives. GetRepeatedMessage or AddMessage will retrieve the handle for reading or writing respectively if it is a repeated field.

SendSourceModMOTD(client)
{	
	new Handle:pb = StartMessageOne("VGUIMenu", client);
 
	PbSetString(pb, "name", "info");
	PbSetBool(pb, "show", true);
 
	new Handle:subkey;
 
	subkey = PbAddMessage(pb, "subkeys");
	PbSetString(subkey, "name", "type");
	PbSetString(subkey, "str", "2"); // MOTDPANEL_TYPE_URL
 
	subkey = PbAddMessage(pb, "subkeys");
	PbSetString(subkey, "name", "title");
	PbSetString(subkey, "str", "TESTING");
 
	subkey = PbAddMessage(pb, "subkeys");
	PbSetString(subkey, "name", "msg");
	PbSetString(subkey, "str", "http://www.sourcemod.net");
 
	EndMessage();
}