# The offline catalogue

The offline catalogue is the list of every item, with its name, its slot, the game it came
from and who can wear it, as one data file, `sona-catalogue.bin`, that the core reads with no
web call. Item pickers, searches and the makers are built on it. It holds every item id the
editor knows (`sona_catalogue_count` says how many) and nothing else: no models, no textures, no pictures.

You get the file only if you ask for it. Tick **Offline catalogue** on your app page when you
download a native SDK, or add `&catalogue=1` to the download address. It lands at
`data/sona-catalogue.bin` in the zip. Without the box ticked the download is the same as
before.

The file and the library go together: use the catalogue from the same download.

## A short example

A shorter form of `samples/c/catalogue_picker.c`, which is built with the samples:

```c
#include <stdio.h>
#include <string.h>
#include "sona.h"

int main(void) {
  sona_catalogue *cat = NULL;
  sona_catalogue_query q;
  sona_item_info it;
  uint32_t hits[10], total = 0, i;
  if (sona_catalogue_load(NULL, "data/sona-catalogue.bin", &cat) != SONA_OK) return 1;

  memset(&q, 0, sizeof q);
  q.text = "hoodie";
  q.gender = 1;                       /* what the editor offers a male body */
  q.require = SONA_ITEM_OFFERED;      /* only what the editor lists */
  sona_catalogue_search(cat, &q, hits, 10, &total);
  for (i = 0; i < total && i < 10; i++) {
    sona_catalogue_get(cat, hits[i], &it);
    printf("%s  %s  %s\n", it.id, it.name, it.game);
  }
  sona_catalogue_free(cat);
  return 0;
}
```

```cpp
#include <cstdio>
#include "sona.hpp"

int main() {
  sona::Catalogue cat("data/sona-catalogue.bin");
  sona_catalogue_query q = sona::query_defaults();
  q.text = "hoodie";
  q.gender = 1;                       // what the editor offers a male body
  q.require = SONA_ITEM_OFFERED;      // only what the editor lists
  std::vector<uint32_t> hits;
  cat.search(&q, hits, 10);
  for (uint32_t i : hits) {
    sona_item_info it;
    cat.get(i, it);
    std::printf("%s  %s  %s\n", it.id, it.name, it.game);
  }
  return 0;
}
```

```csharp
using var cat = SonaCatalogue.Load("data/sona-catalogue.bin");
var q = new SonaCatalogueQuery { Text = "hoodie", Gender = 1, Require = SonaItemFlags.Offered };   // what the editor lists for a male body
foreach (var i in cat.Search(q, 10, out int total))
{
    var it = cat.Get(i);
    Console.WriteLine($"{it.Id}  {it.Name}  {it.Game}");
}
```

```rust
let bytes = std::fs::read("data/sona-catalogue.bin").unwrap_or_default();
let cat = Catalogue::from_bytes(&bytes)?;
let q = Query {
    text: Some("hoodie".into()),
    gender: 1,                                // what the editor offers a male body
    require: sona_sys::SONA_ITEM_OFFERED,     // only what the editor lists
    ..Default::default()
};
let (hits, _total) = cat.search(&q, 10)?;
for i in hits {
    if let Some(it) = cat.get(i) {
        println!("{}  {}  {}", it.id, it.name, it.game);
    }
}
```

```python
cat = sona.Catalogue("data/sona-catalogue.bin")
hits, total = cat.search(text="hoodie", gender=1, require=1, limit=10)   # require 1: SONA_ITEM_OFFERED
for i in hits:
    it = cat.get(i)
    print(it.id, it.name, it.game)
```

With today's catalogue, 631 items match and it prints the first ten, such as
`Haunt Hoodie`. To put a picked item on an avatar, pass `it.guid` to
`sona_manifest_wear`.

## Rules

- **Threads.** A loaded catalogue never changes. Every call that reads it is safe from any
  number of threads at once. Do not call `sona_catalogue_free` while another thread uses it.
- **Lifetimes.** Every string an item gives you lives until `sona_catalogue_free`. The
  catalogue keeps its own copy of the file: free your bytes after `sona_catalogue_load_memory`
  returns.
- **The ctx is optional.** Pass `NULL` and the catalogue uses `malloc` and `free`, and errors
  come back only as result codes. Pass a ctx and it uses the ctx's allocator (the host's own,
  not the metered one), and `sona_last_error(ctx)` gives the message and the fix. The
  catalogue may outlive the ctx. Its memory is not counted in `sona_stats`.
- **Memory.** The file's size plus a fixed 8 bytes per game.
- **No web, no licence.** Loading needs neither. The catalogue tells you about items; it does
  not give you their bytes. Drawing an item still goes through your sources.
- **Text.** Every string is UTF-8, never `NULL`, and `""` when the data has none.
- **Order.** Items are sorted by id. An index is valid for that catalogue only.

## Functions

### sona_catalogue_load

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

Reads the file at `path` (UTF-8, also on Windows) and checks every byte of it.
`*out` is set to `NULL` first, and to the catalogue on `SONA_OK`.

| Result | When |
|---|---|
| `SONA_OK` | loaded |
| `SONA_E_ARGUMENT` | `path` or `out` is `NULL` |
| `SONA_E_IO` | the file could not be opened or read |
| `SONA_E_CORRUPT` | empty, truncated, damaged, or bigger than the 256 MB limit |
| `SONA_E_UNSUPPORTED` | a newer major format than this library reads |
| `SONA_E_MEMORY` | the allocator returned `NULL` |

The file is checked as it loads.

### sona_catalogue_load_memory

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

The same from bytes you hold: an asset in your package, a file you read yourself. The bytes
are copied. Results as `sona_catalogue_load`, without `SONA_E_IO`. `bytes` may be `NULL` only
when `len` is 0 (which is `SONA_E_CORRUPT`).

### sona_catalogue_free

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

Frees it and every string it gave out. `NULL` does nothing.

### sona_catalogue_count

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

How many items. Indexes run from 0 to count - 1. 0 for `NULL`.

### sona_catalogue_get

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

Fills `*out` with one item. `SONA_E_NOT_FOUND` past the last index, `SONA_E_ARGUMENT` for a
`NULL` argument.

### sona_catalogue_find

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

The index of an item, or -1 when it is not there. `id` is 32 hex digits in any case, with or
without dashes, braces, spaces or line breaks: `00000008-0003-0003-...`,
`{00000008-...}` and `000000080003...` all work. Any other character, or more or fewer
digits, gives -1. A binary search.

### sona_catalogue_find_guid

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

The same for a `sona_guid` in wire order, as `sona_manifest` holds them
(`m.worn[i].id`, `m.figure_a.id`).

### 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);
```

Writes the indexes of matching items, in id order, into `out_indexes`: at most `max` of them,
after skipping `q->offset` matches. `*total` (when not `NULL`) is the count of every match, the
skipped ones included, so you can size a list or page through it. `q` `NULL` matches every
item. `out_indexes` may be `NULL` when `max` is 0, to count only.

`SONA_E_ARGUMENT`: `cat` is `NULL`, `out_indexes` is `NULL` with `max` above 0, `q->text` is
over 1024 bytes, or it holds more than 16 words. A search walks every item.

### sona_catalogue_game_count, sona_catalogue_game

```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);
```

The games items come from (895 today), sorted by name with ASCII case folded, and how many
items each has. For a game filter: put a name in `q->game`. `SONA_E_NOT_FOUND` past the last
one. `name` and `items` may be `NULL`.

### sona_catalogue_generated

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

When the catalogue's data was made, ISO 8601 (`"2026-09-14T05:49:58.153Z"`), or `""`.
`NULL` for a `NULL` catalogue.

### sona_catalogue_section_name

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

The editor's name for a section: `"shirt"`, `"facialHair"`, `"carryable"`. `NULL` past
`SONA_SECTION_OTHER`. Show your own words to players; these are keys.

## Structs

### sona_item_info

| Field | What |
|---|---|
| `sona_guid guid` | the id, wire order. Pass it to `sona_manifest_wear` |
| `char id[37]` | the id as lowercase text |
| `uint8_t kind` | `SONA_ITEM_KIND_MODEL` 1, `_TEXTURE` 2, `_SHAPE_OVERRIDE` 3, `_ANIMATION` 4, `_UNKNOWN` 0 |
| `uint8_t gender` | 1 male, 2 female, 3 both, 0 not stated |
| `uint8_t section` | a `sona_section`, never 0 |
| `uint32_t mask` | the AssetMask: which slots it covers (the id's first group) |
| `uint32_t flags` | `SONA_ITEM_*`, below |
| `sona_route route` | where the editor's router puts it, the same as `sona_slot_of(&guid)` |
| `uint32_t colour_set` | the palette it tints from when `SONA_ITEM_COLOURABLE`; else 0 |
| `const char *name` | its name; `""` for the few with none |
| `const char *game` | the game or shop it comes from; `""` for built-in items |
| `const char *type` | the type, `"Shirt/Hoodie"`, `"Shoes/Trainers"` |
| `const char *family` | the type's first part, `"Shirt"` |
| `const char *description` | how an award is earned; `""` for most items |
| `uint16_t subcategory` | the type as a number; 0 when none |
| `uint8_t colour_choices` | colourways in its own colour table; 0 when not stated |
| `uint8_t texture_type`, `shape_type` | from the item's data; 255 when not stated |
| `uint8_t pad[3]` | zero |

`sona_struct_size("sona_item_info")` and `sona_struct_offset("sona_item_info.<field>")`
give the layout for bindings.

### sona_catalogue_query

All zero matches everything. Each field set narrows it; an item must pass all of them.

| Field | Matches |
|---|---|
| `const char *text` | items whose name, game or type holds every word (split on spaces, ASCII letters folded; other bytes must match exactly). In double quotes, `"Wave Tee"`, it is the whole name instead. `NULL` or `""`: any |
| `const char *game` | that game's name, ASCII case folded. `""` matches items with no game |
| `uint32_t slots` | items whose mask has any of these bits |
| `uint32_t section` | that `sona_section` |
| `uint32_t gender` | 1 or 2: what the editor offers that body. A model locked to the other body is left out; textures, shapes and animations never are. 0: any body. Over 2: `SONA_E_ARGUMENT` |
| `uint32_t require` | items with every one of these `SONA_ITEM_*` flags |
| `uint32_t exclude` | items with none of these flags |
| `uint32_t offset` | skip this many matches, for paging |

The editor also widens a misspelt word to catalogue words one edit away. The catalogue does
not: words match as typed.

## Flags

| Flag | Set when |
|---|---|
| `SONA_ITEM_OFFERED` | the editor lists it: it has a name, it is not a garment's body shape, it is not a duplicate, and the data does not mark it not pickable (unless that would empty its whole section, as with eye shadow). The body rule is separate: `q.gender`. This is 360sona.com's editor's own rule; the native editor lists these and whatever the avatar wears ([Player editor](editor.md#what-a-list-holds)) |
| `SONA_ITEM_NAMED` | it has a name a person can read |
| `SONA_ITEM_STOCK` | built in: every avatar has it |
| `SONA_ITEM_AWARD` | earned in a game; `description` says how |
| `SONA_ITEM_MARKETPLACE` | sold: neither stock nor an award |
| `SONA_ITEM_HIDDEN` | a duplicate of another item. Not listed; an avatar already wearing it still draws |
| `SONA_ITEM_NOT_PICKABLE` | the data marks it not pickable |
| `SONA_ITEM_HAS_MODEL` | it has a mesh |
| `SONA_ITEM_GLB` | a glTF export of it exists on 360sona |
| `SONA_ITEM_COLOURABLE` | it takes a colour from the manifest's palette (`colour_set`) |
| `SONA_ITEM_COLOURWAYS` | its own colour table offers more than one colourway |
| `SONA_ITEM_MULTI_SLOT` | its mask covers more than one slot |

Exactly one of `STOCK`, `AWARD` and `MARKETPLACE` is set on every item.

## Sections

The editor's sections, from the item's mask by the editor's own rules: props first, then
gloves (a glove sets two bits), then anything covering two or more garment slots is a
costume, then the item's first slot.

`SONA_SECTION_COSTUME` 1, `SHIRT` 2, `TROUSERS` 3, `SHOES` 4, `HEADWEAR` 5, `GLASSES` 6,
`GLOVES` 7, `WRISTWEAR` 8, `RING` 9, `EARRINGS` 10, `CARRYABLE` 11, `HAIR` 12, `EYES` 13,
`EYEBROWS` 14, `MOUTH` 15, `FACIAL_HAIR` 16, `EYE_SHADOW` 17, `FACIAL_FEATURE` 18, `CHIN` 19,
`NOSE` 20, `EARS` 21, `BODY` 22, `HEAD` 23, `ANIMATION` 24, `SHAPE` 25, `OTHER` 26.
`SONA_SECTION_ANY` (0) appears only in queries.

## The file format

Little-endian. Version 1.0. A reader accepts any 1.x: later minor versions may make the
header or the records longer and add flags, and a 1.0 reader skips what it does not know.

The header, 64 bytes or more:

| Offset | Size | Field |
|---|---|---|
| 0 | 8 | magic `"SONACAT"` then `0x1a` |
| 8 | 2 | major version, 1 |
| 10 | 2 | minor version, 0 |
| 12 | 4 | header bytes (64) |
| 16 | 4 | record bytes (64; at most 4096) |
| 20 | 4 | item count |
| 24 | 4 | where the records start |
| 28 | 4 | where the string table starts |
| 32 | 4 | string table bytes |
| 36 | 4 | file bytes |
| 40 | 4 | the generated date (a string offset) |
| 44 | 4 | CRC-32 (IEEE) of the whole file, these four bytes read as zero |
| 48 | 16 | zero |

Each record, sorted by id with no id twice:

| Offset | Size | Field |
|---|---|---|
| 0 | 16 | id, wire order |
| 16 | 4 | mask |
| 20 | 4 | flags |
| 24 | 4 each | name, game, type, family, description (string offsets) |
| 44 | 4 | colour set |
| 48 | 2 | subcategory |
| 50 | 1 each | kind, gender, section, route, colour choices, texture type, shape type |
| 57 | 7 | zero |

The string table is UTF-8 strings, each ending in a zero byte, each stored once. It starts
with the empty string, so offset 0 is `""`. A string offset must point at the start of a
string.

What the loader refuses, all as `SONA_E_CORRUPT` unless said: a short file; a wrong magic;
another major version (`SONA_E_UNSUPPORTED`); a file size that is not the one in the header;
a CRC that does not match; sections that do not fit or overlap; a string table that does not
start and end with a zero byte or is not valid UTF-8; a string offset outside the table or in
the middle of a string; ids out of order or repeated; a kind, gender, section or route out of
range. A truncated or damaged file is refused, never read past its end. The games list is
sorted in n log n.
