Game modules
A game module is an add-on for the game itself, not for a server: a native library that adds a tab of servers to the server browser, a way to reach servers (a transport), folders of content mounted while one of its connections lasts, and a page in the settings. Server plugins are something else (docs/plugins_api.md).
Without a modules/ folder in the user folder nothing of this runs: no file is read, no thread started, and the game is the same as without the module host.
Installing
A module travels as a .csmod, a package signed with its author's Ed25519 key. A player installs it in Settings > Modules > Install a module…, which enables it (it loads at once when no module ran yet, else at the next start). Official modules also install from the WORKSHOP page, for an account that owns the game (docs/workshop_plan.md). Authors make and install them with cs_plugin (docs/plugins_tools.md):
cs_plugin check <folder> # module.toml and its libraries
cs_plugin pack <folder> [--key FILE] # <id>-<version>.csmod
cs_plugin install <file.csmod> [--user DIR]
install checks the package's signatures and every file's hash, keeps the author's key for the module's updates (modules/authors.toml), and puts the module in <user folder>/modules/<id>/. With it go the package's manifest (.manifest.toml) and its signature (.signature), which the game checks each time it loads the module.
The game loads a module at start-up when:
-
it is enabled:
modules/modules.toml,enabled = ["<id>", ...](Settings > Modules has a switch for each); -
its author is trusted: the game's list (
data/module_sdk/trusted_authors.toml) or the player's (modules/trusted.toml,keys = ["<hex>", ...]; Settings > Modules offers "Trust this author" for a module whose author is not); - its signature is good and no file the manifest lists was changed;
-
it was made for this game's module API (
api = 1).
development = true in modules.toml loads a module that is not signed (a module's author's own copy); the log says so.
A module's folder
<id>/
module.toml
lib/linux/<name>.so
lib/windows/<name>.dll
lang/en.toml (its texts: one file a language, optional)
lang/es.toml
lang/pt_BR.toml
... (anything else it needs: content/, data)
module.toml:
id = "hello" # the folder's name: a-z, 0-9, _
name = "Hello"
version = "1.0.0"
author = "Someone"
description = "What it does."
language = "en" # the language of its own texts (default en)
api = 1
# Optional: its servers' drop reasons that are passing (a service of
# theirs lost a moment): the game joins the same server again at once,
# twice in two minutes at most. Matched whole, also inside 'Kicked :"..."'.
passing_drops = ["Lost the link for a moment"]
[library]
linux = "lib/linux/hello.so"
windows = "lib/windows/hello.dll"
Languages
A module speaks the language the game shows, without asking the player: host->text(ctx, key) gives a key's text from lang/<language>.toml in its package. The file names are the game's languages (en, es, pt_BR, in any case); anyone can add one, a new file being a new language. A file holds flat keys or tables ([menu] and title is menu.title):
# lang/es.toml
greeting = "¡Hola, {1}!"
"Join a server" = "Entrar a un servidor" # an English text as its own key
[module]
description = "Lo que hace." # Settings > Modules shows it
The language's file is read first, then its base's (pt for pt_BR), then the module's own (language in module.toml), so a text missing in one shows in the module's language. A key in no file comes back as it is: a module may write its texts in English and use them as keys, with no en.toml. module.name and module.description replace the manifest's on Settings > Modules. The player may change the language while playing: read host->setting(ctx, "language") each frame and ask for the texts again when it changes (filling in {1}-like values is the module's own). text may be called from any thread.
A module runs native code without a sandbox: it can do whatever the game can. That is why it loads only when the player enabled it and trusts its author.
The ABI
data/module_sdk/cs_module.h, C. The library exports cs_game_module_load(const cs_module_host *) (non-zero: loaded), and optionally cs_game_module_frame(double now) (once a frame) and cs_game_module_unload(void). Every call either way is on the game's main thread; a module may run threads of its own and hand their results over in its frame or with post.
What the game gives (cs_module_host): a log; the module's data folder (<user folder>/module_data/<id>/) and its package folder; the game's settings by console name (name, rate, cl_updaterate, cl_cmdrate, language, cl_lw, cl_lc, model); the player's account certificate; a signature with this computer's key (sign_with_device: the key the certificate names, so a module's own service can tell that the player holds it; what is signed is "CS-MODULE:" + id + "\0" + message, which serves for nothing else); opening an http or https page in the player's browser; and the registrations:
-
Transport (
register_transport(scheme, class)): an address<scheme>://<rest>goes through the module instead of CSNet (the connect command, the server browser, retry and Reconnect all take it). The module speaks to its server however it likes; to the game it hands the server's messages in the game's wire format (docs/protocol.md, "Layer 2: messages"), in batches that each count as one transmit, and it takes the game's own (the reliable ones and the unreliable ones, also in that format). The client, the prediction and the HUD do not know the difference. A transport may say how many transmits a second its connection takes at most (max_send_rate); the client sends its moves no faster. It may also say how many commands a second its server should see at most (command_rate): the game keeps its commands under it (99.5: 99 a second; the frames between add their time and buttons to the next one), as the engine's fps_max, and a command never takes more time than its frames (a server holds a client whose commands run ahead of its clock); a transport never changes a command's time itself, as the prediction would not match the server. The game tells it the player's frame rate limit (frame_limit, fps_max, 0 for none) after it opens and whenever it changes, so its server can be told the frame rate the player really plays at. A player's info may carry*picture, a picture of the connection's own files (pictures/<name>.jpgor.png, at most 512 pixels a side, read through the mounts below): the scoreboard shows it as that player's profile picture. -
Server source (
register_server_source, thenserver_rows(json)): a tab in the server browser. Its rows are a JSON array of objects:address,name,map,players,bots,max,ping(ms, -1 unknown),password,mode,noteandlocked(why Connect is off), and optionallylist, its players shown beside the list (a JSON array of{name, frags, bot}, and optionallyseconds, the time played, andcsid, an account's CSID for its profile picture). The game does not ask those servers anything: the module does. Optional:views, the tab's own sub-tabs (a JSON array of{id, title}; a row'sviewsnames the ids it shows in), andset_favorite, the chosen server's star (a row'sfavoritesays whether it is on); on a view whose id isfavoritesthe browser offers "Add a server", which callsset_favoritewith the address as the player typed it (no scheme) and thenrefresh; andrefresh_server, the chosen server's Refresh button, also called when a row is chosen (that one server asked again, its row sent with the others when it answers, so a module may ask a server's players only then; without it the button is not shown). The browser reads a tab's rows only when they change, and draws only the rows in view, so a module may list thousands. -
Settings page (
register_settings_page, thenpage_fields(json)): a section of Settings > Modules, or of another settings page when itsplacementnames one ("Spray": the section follows the game's own spray). Its fields are a JSON array of{id, type, label, value, help}, typetext,folder(with a folder picker),button,toggle,status,info,choice(options:{value, label}objects, shown as a list; with animageeach, a picture file, its thumbnail beside its name; with acoloreach,#rrggbb, as swatches),file(a file picker;filterslike["*.png, *.jpg ; Pictures"]) orimage(the picture filevalue, at mostsizepixels a side, 128 by default); a change or a press calls the page's action with the field and its value. Abuttonwithconfirm,{title, text, ok}, asks in a popup first ("ok" the yes button's text) and calls the action only on yes. A page's fields are shown again whenever the module sends new ones. -
Mounts for a connection (
mount_for_connection(folders, downloads), called from a transport'sopen): folders the game may read files from while that connection lasts, after the game's own content (they never replace a game file), and the folder the server's downloads go to (in the module's data folder, made if missing, after the folders; a file downloaded during the connection is found at once, as a map the server changes to). They are taken away when the connection closes. -
Files read from those folders first (
prefer_for_connection(names)): the files the connection's server checks, named as the server names them; while the connection lasts the game reads them from its mounted folders before its own content, so it draws the very files the server was told about. A list replaces the one before; the connection's close clears it. (Optional at the end ofcs_module_host: a module checks the host'ssizebefore calling it.) -
Demo source (
register_demo_source, thendemo_rows(json)anddemo_status(text)): a tab of the Demos page, for demos the game cannot read itself. Its rows are a JSON array of objects:file(the demo's path),name,map,recorded(seconds since 1970),size(bytes) andnote. Itsextensionis what the page's "Open a file" offers on that tab (a demo anywhere: one downloaded, say), and itsfolderwhat "Open the folder" opens. Watch (or a file opened) callswatch(file): the module makes it a demo in the game's own format,.csdem(records: the seconds since the recording began as an f64, a kind as an i32, a length as a u32 and its bytes; a kind of 0 and up is a batch of the server's messages in the wire format with the transmit it came in, below 0 what the player did: -2 is the view angles as three f32 and the buttons as an i32; native/client/client.cpp, "Demos"), and callsplay_demo(path, folders): the game plays it in its demo player, with those folders read after its own content while it plays. A status line says what is happening in the meantime ("Preparing...", or why a demo cannot play). (Optional at the end ofcs_module_host, as above.) -
Pictures (
image_load,image_png; any thread): a picture file (PNG, JPEG, WebP, BMP, TGA, SVG) read as RGBA pixels, and RGBA pixels written as a PNG, with the game's own image code, so a module needs no image library of its own. (Optional at the end ofcs_module_host, as above.) -
Player decals (
player_decal_for_connection; main thread): the picture a player's decal draws while the connection lasts, a pixel a unit, laid along the surface and kept as the server's other decals (until the world's limit of decals takes the oldest). The game's own sprays stay on the game's servers: over a module's connection the game neither sends the player's spray nor fetches the others', and a player decal with no picture from the module draws nothing. (Optional at the end ofcs_module_host, as above.)
The sample
samples/module/hello (C) has a tab with one server, a page with buttons (one signs with the computer's key) and a text field, and a transport, hello://<demo>, that plays a recorded game (.csdem) back as if a server sent it, with the module's content/ mounted meanwhile. tools/module_test.sh packs, signs, installs and loads it, and checks that a changed package does not install and that a changed library, an author not trusted and a module not enabled do not load.