Gears

Share Code Format Reference

Every byte of a Gears share code - prefix and base64 text, header, compression, payload, hashes, value encodings and errors - with a worked example.

Use this page to read or write share codes in any language. A share code is one line of text, starting with Gears:, that holds the settings a player changed from their defaults. A code holds either global settings, which a player changes from the main menu or in game, or world settings, which belong to one world and can't change while it runs.

This page describes format version 1, the only version. The C# library GearsSharing and the documentation site's TypeScript port both follow it, and both are tested against the same test vectors. For what a code is for, see Share Codes.

How a code is built

A code is built in four layers. To write one, work from the top down. To read one, work from the bottom up.

  1. A payload of mods, setting hashes and values.
  2. Optional compression: the payload is raw-deflated when that makes it smaller.
  3. A header byte in front, giving the version, the scope, and whether the payload is compressed.
  4. Prefix and base64 text: the header and payload as base64, after the Gears: prefix.

Prefix and base64 text

RuleWritingReading
PrefixWrite Gears: exactly.Trim the ends of the text, then require Gears:. It is case-sensitive: gears: is not a code.
AlphabetURL-safe base64: - and _ in place of + and /.Accept both alphabets.
PaddingLeave out the trailing =.Accept it.
WhitespaceWrite none.Ignore whitespace after the prefix, so a code split across lines still reads.
LengthAt most 32,767 characters, prefix included. A writer must refuse to make a longer code.Don't enforce a length; the 1 MB payload limit below covers it.

Base64 text whose length leaves 1 character over a multiple of 4 can't be valid, and neither can padding in the middle. Both are InvalidBase64.

Header byte

The first decoded byte is the header. The rest are the payload.

BitsMeaning
0-3Format version. Always 1.
4Set for a World code, clear for a Global code.
5Set when the payload is compressed.
6-7Reserved. Always clear.

A reader must reject any other version, or a reserved bit that is set, as UnsupportedVersion.

Payload compression

When bit 5 is set, the payload is raw deflate, the compressed format defined in Request for Comments (RFC) 1951, with no zlib or gzip header. A writer compresses the payload and keeps the result only if it is shorter. An empty payload is never compressed.

A reader must stop inflating once the output passes 1 MB (1,048,576 bytes) and report TooLarge, so a tampered code can't expand without limit.

Payload layout

The payload has the same shape for both scopes:

payload  = [ count mod* ]               ; empty means no mods
mod      = modHash:u32 count setting*
setting  = settingHash:u32 value
  • An empty payload means no mods. It is a valid code that changes nothing from the defaults.
  • u32 is 4 bytes, little-endian.
  • A writer leaves out mods with no settings, and never writes the same mod hash twice, or the same setting hash twice within a mod.
  • After the last mod, the payload must end. Leftover bytes are TrailingData.

Number and text encodings

NameEncoding
varintUnsigned Little Endian Base 128 (LEB128): 7 bits per byte, low bits first, high bit set on every byte but the last. At most 10 bytes (64 bits); a longer one is MalformedData.
countA varint. Every counted item takes at least one byte, so a count larger than the bytes left is MalformedData.
signedZigzag, then a varint: (n << 1) ^ (n >> 63) on a 64-bit integer, so 0, -1, 1, -2 become 0, 1, 2, 3.
textA varint byte length, then that many bytes of UTF-8 (the 8-bit Unicode Transformation Format). Invalid UTF-8 is MalformedData.

Name hashes

Names are stored as 32-bit FNV-1a hashes of their UTF-8 bytes. FNV-1a is the Fowler–Noll–Vo hash, variant 1a:

hash = 0x811C9DC5
for each byte b of UTF-8(text):
    hash = (hash XOR b) * 0x01000193   ; modulo 2^32
HashText that is hashed
ModThe mod's name: the value of <Name> in its ModInfo.xml.
Global settingtab + "\0" + category + "\0" + name: the three names joined by a zero byte.
World settingThe setting name alone. World setting names are unique within a mod.

Hashes are case-sensitive and use the names exactly as the mod defines them. Check an implementation against these values:

TextHash
"" (empty)811c9dc5
"a"e40c292c
"ModA"6f4ea346
"Héllo ✓ 🎮"9fa0ce5c
Global setting General / Display / Int2fae2104

Value encodings

Each value starts with a one-byte tag. Several kinds have a compact form. A writer picks the compact form whenever it applies; a reader accepts either.

TagKindData after the tagWriter uses it when
0Bool falsenone
1Bool truenone
2Intsignedalways, for an int. A reader rejects a value outside the 32-bit range as MalformedData.
3Float, wholesignedthe float is a whole number greater than -2^20 and less than 2^20, and is not -0. A reader converts it to a single-precision float.
4Float4 bytes: single precision as defined by the Institute of Electrical and Electronics Engineers (IEEE) 754 standard, little-endianany other float. NaN (not a number) payloads and -0 survive unchanged.
5String, nullnonethe string is null.
6Stringtextthe string is not null. It may be empty.
7Enum by nametextthe value is a named enum member. The name must match exactly.
8Enum by numbersigned (64-bit)the value has no member name, such as a combination of flags.
9Color, compact4 bytes: R, G, B, A, each 0-255every channel is between 0 and 1 and equals exactly round(channel * 255) / 255 in single precision. A reader divides each byte by 255.
10Color4 floats as in tag 4: R, G, B, Aany other color.
11Serializedtextthe setting's type is a mod's own type, written by its registered serializer.

Any other tag is MalformedData.

Floats are single precision throughout. Round a double to single precision before you compare or write it, and compare floats by their bits, so that NaN equals NaN and -0 differs from 0.

Decoding errors

A reader stops at the first problem that prevents decoding. It reports repeated entries only after reading the whole code, so an editor can still show what is there.

ErrorWhen
EmptyThe text is empty or whitespace, is only the prefix, or decodes to no bytes.
MissingPrefixThe trimmed text does not start with Gears:.
InvalidBase64The text after the prefix is not base64.
UnsupportedVersionThe header's version is not 1, or a reserved bit is set.
CorruptCompressionThe compressed payload can't be inflated.
TooLargeThe payload inflates to more than 1 MB.
TruncatedThe payload ends part way through a value or text.
MalformedDataA count, number, tag or text is impossible: see Number and text encodings and Value encodings.
TrailingDataBytes are left after the last mod.
DuplicateSettingThe same mod hash appears twice, or the same setting hash twice within a mod.

Worked example: decoding a code

Gears:EQEY_QvIASITDNYB is a World code that turns one setting on. After the prefix, the base64 decodes to 12 bytes:

11 01 18 FD 0B C8 01 22 13 0C D6 01
BytesMeaning
11Header 0001 0001: version 1, World, not compressed.
011 mod.
18 FD 0B C8Mod hash 0xC80BFD18: FNV-1a of "M".
011 setting.
22 13 0C D6Setting hash 0xD60C1322: FNV-1a of "S".
01Tag 1: Bool true.

The payload is 11 bytes, too short for deflate to shrink it, so it is not compressed.

To write this code: build the payload, try deflating it and keep the shorter form, put the header byte in front, then base64 the result with the URL-safe alphabet, drop the padding, and add the prefix.

On this page