# C API: The offline catalogue

The full contracts, the structs, the flags and the file format are in [The offline catalogue](../../guides/catalogue.md). The catalogue is a handle of its own: it needs no ctx after loading and any thread may read it.

## sona_catalogue_load

```c
sona_result sona_catalogue_load(sona_ctx *ctx, const char *path, sona_catalogue **out);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ctx` | `sona_ctx *` | The context | required |
| `path` | `const char *` | A file path | required |
| `out` | `sona_catalogue **` | Receives the new catalogue | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_UNSUPPORTED`, `SONA_E_MEMORY`, `SONA_E_IO`, `SONA_E_CORRUPT` ([what each means](../../troubleshooting.md#error-codes)).

**Example** (also in [The offline catalogue](../../guides/catalogue.md)):

```c
sona_result sona_catalogue_load(sona_ctx *ctx, const char *path, sona_catalogue **out);
```

Loads `sona-catalogue.bin` from a UTF-8 path and checks every byte. `ctx` may be `NULL`. `SONA_E_IO`, `SONA_E_CORRUPT`, `SONA_E_UNSUPPORTED`.

## sona_catalogue_load_memory

```c
sona_result sona_catalogue_load_memory(sona_ctx *ctx, const void *bytes, size_t len, sona_catalogue **out);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ctx` | `sona_ctx *` | The context | required |
| `bytes` | `const void *` | The file's bytes | required |
| `len` | `size_t` | How many bytes there are | - |
| `out` | `sona_catalogue **` | Receives the new catalogue | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_UNSUPPORTED`, `SONA_E_MEMORY`, `SONA_E_IO`, `SONA_E_CORRUPT` ([what each means](../../troubleshooting.md#error-codes)).

**Example** (also in [The offline catalogue](../../guides/catalogue.md)):

```c
sona_result sona_catalogue_load_memory(sona_ctx *ctx, const void *bytes, size_t len, sona_catalogue **out);
```

The same from bytes in memory. The catalogue keeps its own copy.

## sona_catalogue_free

```c
void sona_catalogue_free(sona_catalogue *cat);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `cat` | `sona_catalogue *` | The catalogue | required |

**Returns** Nothing.

**Example** (also in [The offline catalogue](../../guides/catalogue.md)):

```c
void sona_catalogue_free(sona_catalogue *cat);
```

Frees it and every string it handed out. `NULL` is fine.

## sona_catalogue_count

```c
uint32_t sona_catalogue_count(const sona_catalogue *cat);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `cat` | `const sona_catalogue *` | The catalogue | required |

**Returns** `uint32_t`.

**Example** (also in [The offline catalogue](../../guides/catalogue.md)):

```c
uint32_t sona_catalogue_count(const sona_catalogue *cat);
```

Items, sorted by id. 0 for `NULL`.

## sona_catalogue_get

```c
sona_result sona_catalogue_get(const sona_catalogue *cat, uint32_t index, sona_item_info *out);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `cat` | `const sona_catalogue *` | The catalogue | required |
| `index` | `uint32_t` | Which one, from 0 | - |
| `out` | `sona_item_info *` | Receives the [`sona_item_info`](../fields.md#sona_item_info) | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_NOT_FOUND` ([what each means](../../troubleshooting.md#error-codes)).

**Example** (also in [The offline catalogue](../../guides/catalogue.md)):

```c
sona_result sona_catalogue_get(const sona_catalogue *cat, uint32_t index, sona_item_info *out);
```

One item. Its strings live as long as the catalogue.

## sona_catalogue_find

```c
int32_t sona_catalogue_find(const sona_catalogue *cat, const char *id);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `cat` | `const sona_catalogue *` | The catalogue | required |
| `id` | `const char *` | An item id: 32 hex digits, with or without dashes, braces or spaces | required |

**Returns** `int32_t`.

**Example** (also in [The offline catalogue](../../guides/catalogue.md)):

```c
int32_t sona_catalogue_find(const sona_catalogue *cat, const char *id);
```

The index of an id in any spelling, or -1. A binary search.

## sona_catalogue_find_guid

```c
int32_t sona_catalogue_find_guid(const sona_catalogue *cat, const sona_guid *id);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `cat` | `const sona_catalogue *` | The catalogue | required |
| `id` | `const sona_guid *` | An item id | required |

**Returns** `int32_t`.

**Example** (also in [The offline catalogue](../../guides/catalogue.md)):

```c
int32_t sona_catalogue_find_guid(const sona_catalogue *cat, const sona_guid *id);
```

The same from a parsed GUID.

## sona_catalogue_search

```c
sona_result sona_catalogue_search(const sona_catalogue *cat, const sona_catalogue_query *q, uint32_t *out_indexes, uint32_t max, uint32_t *total);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `cat` | `const sona_catalogue *` | The catalogue | required |
| `q` | `const sona_catalogue_query *` | A [`sona_catalogue_query`](../fields.md#sona_catalogue_query) | required |
| `out_indexes` | `uint32_t *` | Receives catalogue indexes, up to `max` | required |
| `max` | `uint32_t` | How many entries `out` holds | - |
| `total` | `uint32_t *` | Receives how many there are in all; may be NULL | `NULL`: not written |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT` ([what each means](../../troubleshooting.md#error-codes)).

**Example** (also in [The offline catalogue](../../guides/catalogue.md)):

```c
sona_result sona_catalogue_search(const sona_catalogue *cat, const sona_catalogue_query *q,
                                  uint32_t *out_indexes, uint32_t max, uint32_t *total);
```

Indexes matching a query (words, game, slots, section, body, flags, paging), in id order.

## sona_catalogue_game_count

```c
uint32_t sona_catalogue_game_count(const sona_catalogue *cat);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `cat` | `const sona_catalogue *` | The catalogue | required |

**Returns** `uint32_t`.

**Example** (also in [The offline catalogue](../../guides/catalogue.md)):

```c
uint32_t sona_catalogue_game_count(const sona_catalogue *cat);
sona_result sona_catalogue_game(const sona_catalogue *cat, uint32_t index, const char **name, uint32_t *items);
```

How many distinct games items come from.

## sona_catalogue_game

```c
sona_result sona_catalogue_game(const sona_catalogue *cat, uint32_t index, const char **name, uint32_t *items);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `cat` | `const sona_catalogue *` | The catalogue | required |
| `index` | `uint32_t` | Which one, from 0 | - |
| `name` | `const char **` | Receives the name; lives as long as the catalogue | required |
| `items` | `uint32_t *` | Receives how many items the game has; may be NULL | `NULL`: not written |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_NOT_FOUND` ([what each means](../../troubleshooting.md#error-codes)).

**Example** (also in [The offline catalogue](../../guides/catalogue.md)):

```c
uint32_t sona_catalogue_game_count(const sona_catalogue *cat);
sona_result sona_catalogue_game(const sona_catalogue *cat, uint32_t index, const char **name, uint32_t *items);
```

One game, sorted by name, and its item count.

## sona_catalogue_generated

```c
const char *sona_catalogue_generated(const sona_catalogue *cat);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `cat` | `const sona_catalogue *` | The catalogue | required |

**Returns** `const char *`.

**Example** (also in [The offline catalogue](../../guides/catalogue.md)):

```c
const char *sona_catalogue_generated(const sona_catalogue *cat);
```

When its data was made, ISO 8601.

## sona_catalogue_section_name

```c
const char *sona_catalogue_section_name(uint32_t section);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `section` | `uint32_t` | A `SONA_SECTION_*` | - |

**Returns** `const char *`.

**Example** (also in [The offline catalogue](../../guides/catalogue.md)):

```c
const char *sona_catalogue_section_name(uint32_t section);
```

A `sona_section` as the editor spells it; `NULL` past the last.

## sona_catalogue_language_count

```c
uint32_t sona_catalogue_language_count(const sona_catalogue *cat);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `cat` | `const sona_catalogue *` | The catalogue | required |

**Returns** `uint32_t`.

**Example** (also in [Item names in other languages](../../guides/catalogue-languages.md)):

```c
uint32_t n = sona_catalogue_language_count(cat);          /* "en" among them */
const char *tag = sona_catalogue_language(cat, i);        /* "cs", "da", "de", "el", "en", "en-GB", "es", ... */
const char *name;
if (sona_catalogue_item_name(cat, index, "pt-PT", &name) == SONA_OK) show(name);
else show(info.name);                                     /* SONA_E_NOT_FOUND: that market used the English name */
```

How many languages the catalogue carries item names in. Each is a BCP 47 tag ("de", "ja", "pt-BR"), sorted; "en" is always one.

## sona_catalogue_language

```c
const char * sona_catalogue_language(const sona_catalogue *cat, uint32_t index);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `cat` | `const sona_catalogue *` | The catalogue | required |
| `index` | `uint32_t` | Which one, from 0 | - |

**Returns** `const char *`.

**Example** (also in [Item names in other languages](../../guides/catalogue-languages.md)):

```c
uint32_t n = sona_catalogue_language_count(cat);          /* "en" among them */
const char *tag = sona_catalogue_language(cat, i);        /* "cs", "da", "de", "el", "en", "en-GB", "es", ... */
const char *name;
if (sona_catalogue_item_name(cat, index, "pt-PT", &name) == SONA_OK) show(name);
else show(info.name);                                     /* SONA_E_NOT_FOUND: that market used the English name */
```

The language tag at `index` (see `sona_catalogue_language_count`); NULL past the last.

## sona_catalogue_item_name

```c
sona_result sona_catalogue_item_name(const sona_catalogue *cat, uint32_t index, const char *lang, const char **name);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `cat` | `const sona_catalogue *` | The catalogue | required |
| `index` | `uint32_t` | Which one, from 0 | - |
| `lang` | `const char *` | A language tag, such as `"fr"` or `"pt-BR"` | required |
| `name` | `const char **` | Receives the name; lives as long as the catalogue | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_NOT_FOUND` ([what each means](../../troubleshooting.md#error-codes)).

**Example** (also in [Item names in other languages](../../guides/catalogue-languages.md)):

```c
uint32_t n = sona_catalogue_language_count(cat);          /* "en" among them */
const char *tag = sona_catalogue_language(cat, i);        /* "cs", "da", "de", "el", "en", "en-GB", "es", ... */
const char *name;
if (sona_catalogue_item_name(cat, index, "pt-PT", &name) == SONA_OK) show(name);
else show(info.name);                                     /* SONA_E_NOT_FOUND: that market used the English name */
```

An item's name in lang (an exact tag first, then the market the Xbox 360 marketplace served it from, then its language: [catalogue languages guide](../../guides/catalogue-languages.md)); it lives as long as the catalogue. `SONA_E_NOT_FOUND` when the item has no name in that language: show `sona_item_info`.name.

## sona_item_colourways

```c
sona_result sona_item_colourways(sona_ctx *ctx, const char *item_id, uint32_t (*out)[3], uint32_t cap, uint32_t *count);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ctx` | `sona_ctx *` | The context | required |
| `item_id` | `const char *` | An item id as text | required |
| `out` | `uint32_t (*out)[3]` | Receives three colours per colourway | required |
| `cap` | `uint32_t` | How many entries `out` holds | - |
| `count` | `uint32_t *` | Receives how many entries there are; may be NULL | `NULL`: not written |

**Returns** `SONA_OK`, `SONA_PENDING` while it is still working. **Errors** `SONA_E_ARGUMENT`, `SONA_E_NOT_FOUND` ([what each means](../../troubleshooting.md#error-codes)).

An item's own colourways (its colour table): up to cap sets of primary, secondary and tertiary colours (0xFFRRGGBB) into out, and how many it has in *count (0 for an item without a table). Wearing the item with one set's three colours in its `sona_asset_ref`.colours (`sona_manifest_wear`) wears that colourway; colours that match no set wear the first. The item is read through the ctx's sources: `SONA_PENDING` until it has loaded (call again after `sona_update`), `SONA_E_NOT_FOUND` when no source has it, `SONA_E_ARGUMENT` for a bad id.
