# Manifests

A manifest is one avatar: 1,000 bytes that hold its body, its shape, its nine colours, its face and the item worn in
each slot. It is the same description an Xbox 360 keeps for a profile, so a manifest from a console, from the
gamertag lookup or from 360sona.com's editor draws the same avatar in every SDK. Text APIs carry it as base64
(1,336 characters).

```c
sona_manifest m;
sona_guid tee;
const uint32_t colours[3] = { 0xff2a5caa, 0xffffffff, 0xffffffff };   /* 0xAARRGGBB */
if (sona_manifest_parse(manifest, &m) == SONA_OK &&
    sona_guid_parse("00000008-006e-0001-c1c8-f109a19cb2e0", &tee) == SONA_OK &&
    sona_manifest_wear(&m, &tee, colours, 0) == SONA_OK &&
    sona_manifest_write(&m, manifest) == SONA_OK)
  sona_avatar_add(ctx, manifest, NULL, &avatar);
```

```cpp
sona::Manifest m;
sona_guid tee;
const uint32_t colours[3] = { 0xff2a5caa, 0xffffffff, 0xffffffff };   // 0xAARRGGBB
sona::Manifest::parse(manifest, m);
sona::parse_guid("00000008-006e-0001-c1c8-f109a19cb2e0", tee);
m.wear(tee, colours);
sona::Avatar dressed = ctx.add(m);
```

```csharp
var m = SonaManifests.Parse(manifest);
var tee = SonaManifests.ParseGuid("00000008-006e-0001-c1c8-f109a19cb2e0");
SonaManifests.Wear(ref m, tee, 0xff2a5caa, 0xffffffff, 0xffffffff);
var dressed = ctx.Add(SonaManifests.Write(m));
```

```rust
let mut m = Manifest::parse(manifest)?;
m.wear(&guid_parse("00000008-006e-0001-c1c8-f109a19cb2e0")?, [0xff2a5caa, 0xffffffff, 0xffffffff], 0)?;
let dressed = ctx.add(&m.to_bytes()?, &AddOptions::default())?;
```

```python
m = sona.Manifest(manifest)
m.wear("00000008-006e-0001-c1c8-f109a19cb2e0", colours=(0xff2a5caa, 0xffffffff, 0xffffffff))
dressed = ctx.add(m.to_bytes())
```

The parse and write are exact: `sona_manifest_write(sona_manifest_parse(x))` gives back `x` byte for byte, so a
manifest you edit and save loses nothing you did not change. None of these calls needs a context, a licence or the
network.

## Where a manifest comes from

| Source | How | Page |
|---|---|---|
| A player's Xbox profile | the gamertag lookup, `GET /sona/v1/gamertag/{gamertag}` | [An avatar from a gamertag](../answers/avatar-from-a-gamertag.md) |
| A player who signs in | Sign in with 360sona, then `GET /slots` with the `avatar:read` scope | [Saved avatars](../web-api.md#slots) |
| A player, while playing | the player editor | [Player editor](../guides/editor.md) |
| Code | `sona_make_random`, `sona_make_variant`, `sona_make_dress_like` | [Making avatars in code](../guides/makers.md) |
| The command line | `sona avatar random`, `sona avatar new` | [CLI reference](../reference/cli.md) |
| 360sona.com | the editor's share box | [Avatar manifests](../web-api.md#manifest) |

A manifest you keep is a value like a name: store it, send it over the network and compare it byte for byte. It does
not change unless the player changes it.

## What is in it

`sona_manifest_parse` reads the bytes into a `sona_manifest`:

| Field | What it holds |
|---|---|
| `weight`, `height` | The two body sliders, -1 to 1 (stored as given; beyond 1 saturates when drawn) |
| `figure_a` | The body. Its id says the gender: `gender` is 1 for the man's body, 2 for the woman's |
| `figure_b` | The head |
| `chin`, `nose`, `ears` | The face's shapes, as item ids |
| `face[6]` | Mouth, eyes, eyebrows, facial hair, eye shadow and a facial feature, each a part and its placement |
| `colours[9]` | Skin, hair, mouth, eyes, eyebrows, eye shadow, facial hair, facial feature, facial feature 2 (0xAARRGGBB) |
| `worn[13]` | The item in each of the 13 slots, with its three colours; an empty slot has a zero id |
| `required[4]` | The last shoes, trousers, shirt and hair worn, so taking an item off puts them back |
| `valid` | Whether the manifest passes the console's own check |

The [struct fields](../reference/fields.md#sona_manifest) page has every field's type; [items and
slots](items.md) says what an item id is and which slot it fills.

## Changing one

The edit calls follow 360sona.com's editor, so an avatar edited in your game is one the editor and a console take:

- `sona_manifest_wear` puts an item on and takes off what shares its slots, then refills the required clothes (a
  shirt, trousers and shoes) from the last ones worn. `SONA_EDIT_NO_REQUIRED` leaves them empty;
  `SONA_EDIT_STACK_PROPS` keeps the props already held.
- `sona_manifest_unwear` takes off what a slot mask holds; `sona_manifest_undress_all` takes everything off.
- `sona_manifest_set_colour`, `sona_manifest_set_shape` and `sona_manifest_set_gender` set a colour, the two sliders
  and the body. A new body swaps the clothes made for the other one, as the editor does.
- `sona_manifest_ready_for_xbox` says whether a console would take it as it is: one prop, the required clothes, no
  custom item.

An avatar's look is fixed when it is added. To change what an avatar on screen wears, add the edited manifest as a
new avatar and swap the two when the new one is READY, so the old look stays on screen while the new one loads:

```c
sona_manifest m;
uint8_t edited[SONA_MANIFEST_BYTES];
sona_avatar *next = NULL;
sona_placement here = { { 0.0f, 0.0f, 0.0f }, 0.0f, 1.0f };   /* where the old one stands */

sona_manifest_parse(manifest, &m);
if (sona_manifest_wear(&m, &item, NULL, 0) == SONA_OK && sona_manifest_write(&m, edited) == SONA_OK)
  sona_avatar_add(ctx, edited, NULL, &next);

/* each frame, after sona_update */
if (next && sona_avatar_state_of(next) == SONA_AVATAR_READY) {
  sona_avatar_move(next, &here, 0);
  sona_avatar_remove(avatar);
  avatar = next;
  next = NULL;
}
```

`sona_avatar_set_manifest` is for one case only: an avatar added with `sona_avatar_add_waiting` before its manifest
was known takes it once ([loading placeholders](../guides/placeholder.md)). On the web, the avatar changes an
item in place ([`avatar.wear()`](../reference/web.md#avatar-wear)).

## Base64

Base64 is the form for text: JSON, URLs, the web SDK, a save file. `sona_avatar_add_base64` takes it directly (at
most 4,096 characters, so whitespace and line breaks are allowed); `sona_base64_decode` gives you the bytes.

```sh
sona avatar random --seed 42 --catalogue sona-catalogue.bin -o me.txt
sona manifest unpack me.txt -o me.json
```

`sona manifest unpack` writes every field as JSON to read or edit, and `sona manifest pack` turns it back into the
same 1,000 bytes.

## Related

- [Items and slots](items.md): what goes in `worn[13]`.
- [Making avatars in code](../guides/makers.md): random avatars and outfits.
- [Struct fields](../reference/fields.md#sona_manifest): `sona_manifest` field by field.
