Examples - nosoop/stocksoup GitHub Wiki

Cross-game Color Literals

This is an example of how to do colored chat messages that work across CS:GO and Source "2009" games (Team Fortress 2, Counter-Strike: Source, Half-Life 2: Deathmatch, Day of Defeat: Source).

#pragma semicolon 1
#include <sourcemod>

#pragma newdecls required
#include <stocksoup/color_literals>

public void OnPluginStart() {
	RegAdminCmd("sm_printcolormessage", PrintColoredMessage, ADMFLAG_ROOT);
	
	LoadTranslations("common.phrases");
	
	char yellowText[96] = CSGO_COLOR_ORANGE ... "%t" ... COLOR_DEFAULT;
	
	if (GetEngineVersion() != Engine_CSGO) {
		MapCSGOColorTextToRGB(yellowText, yellowText, sizeof(yellowText));
	}
	
	PrintColoredChatAll(yellowText, "No matching client");
}

public Action PrintColoredMessage(int client, int argc) {
	// set up some text using the more limited color palette
	char ctext[] = "This is a " ... CSGO_COLOR_YELLOW ... "color text" ... COLOR_DEFAULT
			... ".  This was done using the " ... CSGO_COLOR_LIME ... "stocksoup library"
			... COLOR_DEFAULT ... ".  This is an " ... CSGO_COLOR_ORANGE ... "orange"
			... COLOR_DEFAULT ... ", and this is " ... CSGO_COLOR_BLUEGRAY
			... "gray" ... COLOR_DEFAULT ... ".  "
			... COLOR_TEAM ... "This is team-colored." ... COLOR_DEFAULT;
	
	char buffer[256];
	if (!client) {
		// called from server console, remove all colorization before dumping to server output
		StripColorChars(ctext, buffer, sizeof(buffer), false);
		PrintToServer("%s", buffer);
	} else {
		if (GetEngineVersion() != Engine_CSGO) {
			// non-CS:GO games use RGB colors, so map it accordingly
			MapCSGOColorTextToRGB(ctext, buffer, sizeof(buffer));
			PrintColoredChat(client, "%s", buffer);
		} else {
			PrintColoredChat(client, "%s", ctext);
		}
	}
	return Plugin_Handled;
}

Game Translation Parsing

Short demonstration of extracting a translation from a game file.

The API is designed to be similar to SourceMod's SMCParser.

#include <sourcemod>

#pragma semicolon 1
#include <stocksoup/textparse>

public void OnPluginStart() {
    // set up the parser and our callback
    TranslationFileParser tfp;
    tfp.Init();
    tfp.OnKeyValue = OnTranslationPair;
    
    // the file must be opened in binary mode
    // it's also recommended to set use_valve_fs to ensure it can be read even
    // if if is mounted from a different directory
    File f = OpenFile("resource/tf_english.txt", "rb", .use_valve_fs = true);
    tfp.ParseOpenUTF16File(f);
    delete f;
}

/**
 * Extract localizations.
 */
void OnTranslationPair(const char[] key, const char[] value) {
    if (StrEqual(key, "TF_SteelJaw_Desc")) {
        LogMessage("%s", value);
    }
}

Enumerating entities attached to a given one

Entities sharing the same parent act in a singly linked-list manner; one of them is the first child entity, and each child entity points to the next one up to the last.

int entity; // this is expected to contain a valid entity
for (int i = GetMoveChild(entity); IsValidEntity(i); (i = GetMovePeer(i))) {
    // 'i' contains an entity that is attached to 'entity'
}
⚠️ **GitHub.com Fallback** ⚠️