# C API: Custom items

Your own items: paint a stock item's template, or bring your own skinned mesh, and wear it like any other item. The format, the rules and the tools: [Custom items](../../guides/custom-items.md).

## sona_custom_item_check

```c
sona_result sona_custom_item_check(const void *bytes, size_t len, sona_custom_item_info *info, char *why, size_t why_cap);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `bytes` | `const void *` | The file's bytes | required |
| `len` | `size_t` | How many bytes there are | - |
| `info` | `sona_custom_item_info *` | Receives what the file holds; may be NULL | `NULL`: not written |
| `why` | `char *` | Receives what was refused, NUL-terminated; may be NULL | `NULL`: not written |
| `why_cap` | `size_t` | How many bytes `why` holds | - |

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

Pure, no ctx, no network: rules 1 to 11. `SONA_OK` with *info set, or `SONA_E_CORRUPT`, `SONA_E_LIMIT` or `SONA_E_UNSUPPORTED` (a newer format) with the rule in why ("rule 6: ..."), always NUL-terminated. info and why may be NULL.

## sona_custom_item_add

```c
sona_result sona_custom_item_add(sona_ctx *ctx, const void *bytes, size_t len, sona_guid *out_id);
```

| 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_id` | `sona_guid *` | Receives the item's id | required |

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

**Example** (also in [Custom items](../../guides/custom-items.md)):

```c
sona_guid id;
sona_manifest m;
uint8_t worn[SONA_MANIFEST_BYTES];
if (sona_custom_item_add(ctx, file, file_len, &id) == SONA_OK &&     /* the bytes of night-sky.sonaitem */
    sona_manifest_parse(manifest, &m) == SONA_OK &&                  /* an avatar's 1,000 bytes */
    sona_manifest_wear(&m, &id, NULL, 0) == SONA_OK &&
    sona_manifest_write(&m, worn) == SONA_OK)
  sona_avatar_add(ctx, worn, NULL, &avatar);                         /* an avatar wearing it */
```

Checks, copies and keeps the file in ctx. Avatars wearing the id rebuild (READY again). Same id: a higher version replaces, a lower one is kept out (`SONA_OK`, nothing changes); the same version with another hash is `SONA_E_ARGUMENT`. out_id may be NULL.

## sona_custom_item_remove

```c
sona_result sona_custom_item_remove(sona_ctx *ctx, const sona_guid *id);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ctx` | `sona_ctx *` | The context | required |
| `id` | `const sona_guid *` | An item id | required |

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

**Example**:

```c
sona_guid x_guid;
memset(&x_guid, 0, sizeof x_guid);
sona_result result = sona_custom_item_remove(ctx, &x_guid);
if (result != SONA_OK) printf("sona_custom_item_remove: %s\n", sona_result_name(result));
```

Forgets a custom item; avatars wearing it rebuild without it. `SONA_E_NOT_FOUND` for an id the ctx does not hold.

## sona_custom_item_count

```c
uint32_t sona_custom_item_count(const sona_ctx *ctx);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ctx` | `const sona_ctx *` | The context | required |

**Returns** `uint32_t`.

**Example**:

```c
uint32_t x_got = sona_custom_item_count(ctx);
printf("sona_custom_item_count: %lld\n", (long long)x_got);
```

How many custom items the ctx holds.

## sona_custom_item_get

```c
sona_result sona_custom_item_get(const sona_ctx *ctx, uint32_t index, sona_custom_item_info *out);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ctx` | `const sona_ctx *` | The context | required |
| `index` | `uint32_t` | Which one, from 0 | - |
| `out` | `sona_custom_item_info *` | Receives the [`sona_custom_item_info`](../fields.md#sona_custom_item_info) | required |

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

**Example**:

```c
sona_custom_item_info x_custom_item_info;
memset(&x_custom_item_info, 0, sizeof x_custom_item_info);
sona_result result = sona_custom_item_get(ctx, 0, &x_custom_item_info);
if (result != SONA_OK) printf("sona_custom_item_get: %s\n", sona_result_name(result));
```

The custom item at `index`, in the order `sona_custom_item_count` counts them.

## sona_custom_item_find

```c
sona_result sona_custom_item_find(const sona_ctx *ctx, const sona_guid *id, sona_custom_item_info *out);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ctx` | `const sona_ctx *` | The context | required |
| `id` | `const sona_guid *` | An item id | required |
| `out` | `sona_custom_item_info *` | Receives the [`sona_custom_item_info`](../fields.md#sona_custom_item_info) | required |

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

**Example**:

```c
sona_guid x_guid;
memset(&x_guid, 0, sizeof x_guid);
sona_custom_item_info x_custom_item_info2;
memset(&x_custom_item_info2, 0, sizeof x_custom_item_info2);
sona_result result = sona_custom_item_find(ctx, &x_guid, &x_custom_item_info2);
if (result != SONA_OK) printf("sona_custom_item_find: %s\n", sona_result_name(result));
```

The custom item with this id; `SONA_E_NOT_FOUND` when the ctx holds none.

## sona_guid_is_custom

```c
int sona_guid_is_custom(const sona_guid *id);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `id` | `const sona_guid *` | An item id | required |

**Returns** `int`.

**Example**:

```c
sona_guid x_guid;
memset(&x_guid, 0, sizeof x_guid);
int x_got = sona_guid_is_custom(&x_guid);
printf("sona_guid_is_custom: %lld\n", (long long)x_got);
```

1 when the id is a custom item's id, 0 for a stock item's.

## sona_guid_pair

```c
sona_result sona_guid_pair(const sona_guid *id, sona_guid *out);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `id` | `const sona_guid *` | An item id | required |
| `out` | `sona_guid *` | Receives the [`sona_guid`](../fields.md#sona_guid) | required |

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

**Example**:

```c
sona_guid x_guid;
memset(&x_guid, 0, sizeof x_guid);
sona_guid x_guid2;
memset(&x_guid2, 0, sizeof x_guid2);
sona_result result = sona_guid_pair(&x_guid, &x_guid2);
if (result != SONA_OK) printf("sona_guid_pair: %s\n", sona_result_name(result));
```

The same custom item's id for the other body. `SONA_E_ARGUMENT` for a body 3 item or a stock id.

## sona_custom_item_new_id

```c
sona_result sona_custom_item_new_id(sona_ctx *ctx, const sona_guid *template_id, sona_guid *out);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ctx` | `sona_ctx *` | The context | required |
| `template_id` | `const sona_guid *` | A [`sona_guid`](../fields.md#sona_guid) | required |
| `out` | `sona_guid *` | Receives the [`sona_guid`](../fields.md#sona_guid) | required |

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

**Example**:

```c
sona_guid x_guid;
memset(&x_guid, 0, sizeof x_guid);
sona_guid x_guid2;
memset(&x_guid2, 0, sizeof x_guid2);
sona_result result = sona_custom_item_new_id(ctx, &x_guid, &x_guid2);
if (result != SONA_OK) printf("sona_custom_item_new_id: %s\n", sona_result_name(result));
```

A fresh id for a template (a real worn id of a 1.0 slot, body 1, 2 or 3). ctx may be NULL.

## sona_custom_item_pack

```c
sona_result sona_custom_item_pack(sona_ctx *ctx, const sona_paint_spec *spec, uint8_t **out, size_t *out_len);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ctx` | `sona_ctx *` | The context | required |
| `spec` | `const sona_paint_spec *` | The options, a [`sona_paint_spec`](../fields.md#sona_paint_spec) | required |
| `out` | `uint8_t **` | Receives the bytes; free them with `sona_free` | required |
| `out_len` | `size_t *` | Receives how many bytes `*out` holds | required |

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

Pixels in, a `.sonaitem` out (free with `sona_free`): the same bytes as `scene.items.pack` gives for the same input. The template must be readable through the ctx's sources (surfaces and hash are checked): `SONA_PENDING` until it has been fetched, then call again after `sona_update`. Each surface takes the template's default layout. The file is not signed. [Custom items](../../guides/custom-items.md) has the format.

## sona_avatar_missing_items

```c
uint32_t sona_avatar_missing_items(const sona_avatar *a, sona_guid *out, uint32_t cap);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `a` | `const sona_avatar *` | The avatar | required |
| `out` | `sona_guid *` | Receives the [`sona_guid`](../fields.md#sona_guid) | required |
| `cap` | `uint32_t` | How many entries `out` holds | - |

**Returns** `uint32_t`.

**Example**:

```c
sona_guid x_guid;
memset(&x_guid, 0, sizeof x_guid);
uint32_t x_got = sona_avatar_missing_items(avatar, &x_guid, 1);
printf("sona_avatar_missing_items: %lld\n", (long long)x_got);
```

Custom ids this avatar wears that no registration supplied, as last built. Returns the count; writes up to cap.

## sona_mesh_item_spec_defaults

```c
void sona_mesh_item_spec_defaults(sona_mesh_item_spec *s);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `s` | `sona_mesh_item_spec *` | The [`sona_mesh_item_spec`](../fields.md#sona_mesh_item_spec) to fill with the defaults | required |

**Returns** Nothing.

**Example**:

```c
sona_mesh_item_spec x_mesh_item_spec;
sona_mesh_item_spec_defaults(&x_mesh_item_spec);
sona_mesh_item_spec_defaults(&x_mesh_item_spec);
```

`size` set and every pointer NULL.

## sona_custom_item_pack_mesh

```c
sona_result sona_custom_item_pack_mesh(sona_ctx *ctx, const sona_mesh_item_spec *spec, uint8_t **out, size_t *out_len, char *why, size_t why_cap);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ctx` | `sona_ctx *` | The context | required |
| `spec` | `const sona_mesh_item_spec *` | The options, a [`sona_mesh_item_spec`](../fields.md#sona_mesh_item_spec) | required |
| `out` | `uint8_t **` | Receives the bytes; free them with `sona_free` | required |
| `out_len` | `size_t *` | Receives how many bytes `*out` holds | required |
| `why` | `char *` | Receives what was refused, NUL-terminated; may be NULL | `NULL`: not written |
| `why_cap` | `size_t` | How many bytes `why` holds | - |

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

Free *out with `sona_free`. why (may be NULL) says what the import refused, always NUL-terminated. Not signed. Reads the template, the rig and the coverage check's assets through the ctx's sources: `SONA_PENDING` until they arrive. `SONA_E_ARGUMENT` when the glb is refused ([custom items](../../guides/custom-items.md), sections 5.2 and 5.3).
