Difference between revisions of "User:Nosoop/Guide/How to X"

From AlliedModders Wiki
Jump to: navigation, search
(Pass enum structs through natives: Use API reference template for types)
(Add mention of CS:GO-specific chat line breaks)
Line 38: Line 38:
  
 
The safest approach is to serialize / deserialize the data through a standard container ({{SourceMod API|file=adt_trie|function=StringMap}}, {{SourceMod API|file=datapack|function=DataPack}}).  That is then exposed to other plugins as an [https://en.wikipedia.org/wiki/Opaque_pointer opaque handle] (the methodmap definition would inherit from <tt>Handle</tt>, and read / write operations are handled indirectly through natives registered by the originating plugin).
 
The safest approach is to serialize / deserialize the data through a standard container ({{SourceMod API|file=adt_trie|function=StringMap}}, {{SourceMod API|file=datapack|function=DataPack}}).  That is then exposed to other plugins as an [https://en.wikipedia.org/wiki/Opaque_pointer opaque handle] (the methodmap definition would inherit from <tt>Handle</tt>, and read / write operations are handled indirectly through natives registered by the originating plugin).
 +
 +
= Add linebreaks in CS:GO chat messages =
 +
 +
CS:GO uses the newline character <code>\n</code> as a text color.  Use the paragraph separator character instead: <code>\xE2\x80\xA9</code>

Revision as of 22:38, 18 December 2022

This is a page on how to do things that are common in plugins. These are intended to be best practices.

Determine if a client has access to something

Use the admin overrides system with the CheckCommandAccess function. For example, if you want to determine if a client has access to the sm_ban command:

if (CheckCommandAccess(client, "sm_ban", ADMFLAG_BAN)) {
    // client has access to the "sm_ban" command
}

The command parameter can be an arbitrary string; it does not have to point to a valid command:

if (CheckCommandAccess(client, "has_fancy_color_trails", ADMFLAG_CUSTOM1, true)) {
    // client has access to the "has_fancy_color_trails" override;
    // if an override entry isn't set it falls back to ADMFLAG_CUSTOM1.
    // ignores the privileges set on a command of that name if it exists
}

Under no circumstance should you leave the command parameter blank; even if you want to check access based on existing admin flags, you should provide a unique "command" string to allow server operators to override access themselves.

To assign access levels for specific strings, modify configs/admin_overrides.cfg.

For more detailed information, see this page on overriding command access.

Declare methodmap natives

Methodmap natives are the same as any other native, except that the this value is passed in as the first parameter, and any additional parameters follow it.

Pass enum structs through natives

In short, you don't. Enum structs do not (at the time of writing) have a standardized in-memory representation, so they should not be passed across plugin boundaries. There's also no way to guarantee that two plugins were compiled with the same definition, let alone implementation (though checking the offset / sizes of each member would lower the risk).

The safest approach is to serialize / deserialize the data through a standard container (StringMap, DataPack). That is then exposed to other plugins as an opaque handle (the methodmap definition would inherit from Handle, and read / write operations are handled indirectly through natives registered by the originating plugin).

Add linebreaks in CS:GO chat messages

CS:GO uses the newline character \n as a text color. Use the paragraph separator character instead: \xE2\x80\xA9