# Writing a mount adapter

> A walkthrough of Quake's adapter, from its folder and adapter.toml to recognising the game, planning its mount, containers and converters.

Part of [Adapters](/docs/adapters) in the Evil Engine docs.

An adapter teaches Evil Engine to read one game, one platform or one file format. Quake's is the example here: it recognises both editions of Quake, plans their mounts over id's PAK archives, and converts their maps, textures and palettes. It leans on three small adapters, `pc` for the platform, `steam` for Steam's files and `pak` for the archive format.

Adapters are built inside the repository today. There's no package to build one against from outside it, so start from [Building from source](/docs/source/building).

## The folder

Each adapter is a folder under `Libraries/Adapters/`, named by its id:

```
Libraries/Adapters/quake/
  adapter.toml
  EvilEngine.Adapters.Quake.csproj
  Quake.cs
  QuakeGame.cs
  QuakeRereleaseGame.cs
  Maps/
```

Add the project to `EvilEngine.slnx` under `/Libraries/Adapters/`, so `just build` builds it.

## adapter.toml

```toml
id = "quake"
name = "Quake"
version = "0.1.0"
api = 1
code = "Code/quake.dll"
games = ["quake", "quake-rerelease"]
platforms = ["pc"]

[titles]
quake = "Quake"
quake-rerelease = "Quake (2021)"

[needs]
"pc" = "^0.1"
"steam" = "^0.1"
"pak" = "^0.1"
```

| Key | What it is |
| --- | --- |
| `id` | Lowercase letters, digits, `.`, `_` and `-`. |
| `name` | The adapter's own name. It never names a game: `[titles]` does. |
| `version`, `api` | `major.minor.patch`, and the mount API major, which is `1`. |
| `code` | The DLL, a path inside the folder. |
| `games` | Every game this adapter registers. Registering one that isn't listed throws. |
| `platforms` | Shown in the boot transcript beside the games. |
| `[titles]` | Every game in `games`, titled as the game titles itself, the name the menus show for it, like Singleplayer's sections. Each game needs one, no two may be alike, and a title for a game not in `games` is an error. A row in Content Mounts uses the store's title instead when a finder has one. |
| `[needs]` | Other adapters this one uses, with version ranges. They load first. |

An adapter with no `games`, like `pak`, only needs `id`, `name`, `version`, `api` and `code`. A key the reader doesn't know is an error.

## The project

Quake's project file, with its shader resources left out:

```xml
<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <AssemblyName>quake</AssemblyName>
        <RootNamespace>EvilEngine.Adapters.Quake</RootNamespace>
    </PropertyGroup>
    <ItemGroup>
        <ProjectReference Include="../../EvilEngine.Conversion/EvilEngine.Conversion.csproj"/>
        <ProjectReference Include="../pc/EvilEngine.Adapters.Pc.csproj"/>
        <ProjectReference Include="../steam/EvilEngine.Adapters.Steam.csproj"/>
        <ProjectReference Include="../pak/EvilEngine.Adapters.Pak.csproj"/>
    </ItemGroup>
</Project>
```

Set `AssemblyName` to the adapter's id. The shared `Directory.Build.props` and `Directory.Build.targets` in `Libraries/Adapters/` do the rest: they turn on dynamic loading, stop referenced projects being copied, and after each build lay the adapter out in `.build/adapters/<id>/` as `adapter.toml` plus a `Code/` folder with your DLL and its own dependencies. That's the folder the engine loads in a run from the repository.

At boot every DLL in `Code/` has its references read before anything loads. An adapter is refused if one references Godot, `EvilEngine.Api` (Plugins' API, which adapters never reach), `EvilEngine.Host`, or an adapter it doesn't list in `[needs]`.

## Registering

The entry point is the one public class that implements [IAdapter](/docs/api/iadapter). The host calls [Register](/docs/api/iadapter#register) once with an [AdapterContext](/docs/api/adaptercontext). Quake's, cut down to three of its converters:

```csharp
public void Register(AdapterContext context)
{
    context.AddGame(new QuakeGame());
    context.AddGame(new QuakeRereleaseGame());
    foreach (var game in new[] { QuakeGame.Name, QuakeRereleaseGame.Name })
    {
        context.AddConverter(game, new QuakeMapRecords());
        context.AddConverter(game, new QuakeTextureConverter());
        context.AddConverter(game, new QuakeLumpConverter());
    }
}
```

Add games before binding converters to them: [AddConverter](/docs/api/adaptercontext#addconverter) only takes a game this adapter has already added. The `pak` adapter's `Register` is one line, `context.AddContainer(new PakContainer())`, and `steam`'s adds its finder.

## Recognising the game

An [IGameAdapter](/docs/api/igameadapter) has a [Game](/docs/api/igameadapter#game) name, its [Platforms](/docs/api/igameadapter#platforms), and two methods.

[Probe](/docs/api/igameadapter#probe) gets a read-only [IFileSource](/docs/api/ifilesource) for whatever the player pointed at and returns a [MountProbe](/docs/api/mountprobe), or `null` if it isn't this game. It runs on every location the engine reads, so keep it to a few reads. `QuakeGame` reads only the headers of `id1/PAK0.PAK` and `id1/PAK1.PAK`, and tells the build from where the two keep their directories:

```csharp
evidence.Add($"that pair is the registered {label}");
return new MountProbe(new MountId(Game, Pc.Id, label), ProbeStatus.Known, tree)
    { Evidence = evidence, Externals = SteamClaims.Externals };
```

That makes the mount `quake/pc/1.06`. A pair it has no version for still mounts, as `quake/pc/unknown` with [ProbeStatus.UnknownVariant](/docs/api/probestatus#unknownvariant). The evidence lines are printed in the boot transcript, so write them for a person. `Externals` names what the game reads from outside its folder, here Steam's `steamapps` and `depotcache` folders, found from where the install sits in its Steam library. The host resolves those before it opens the mount.

Return `null` rather than throwing: the host doesn't catch exceptions from a probe.

## Planning the mount

[Open](/docs/api/igameadapter#open) returns a [MountPlan](/docs/api/mountplan), and the mount core builds the mount from it. Quake's plan has, for each game folder it runs:

- a layer for each PAK, highest number first, each a [LayerSource.Container](/docs/api/layersource-container) placed at its folder so `PAK0.PAK`'s `maps/e1m1.bsp` reads as `id1/maps/e1m1.bsp`,
- then a layer for the folder's loose files.

The first layer wins when two hold the same path, which is the order Quake itself searches. The plan also lists the parts a Plugin can require (one per episode or mission pack, each with the base game's files under it), the build files, and `Records` naming `records/maps`, the record set Singleplayer reads its map list from. A folder Quake only runs when it's asked to, a mod, gets a part and a note but no layer.

Throw `MountException` when the game can't be mounted. The pointer shows as failed with your message.

## Containers

An archive format is an [IContainerAdapter](/docs/api/icontaineradapter), in its own adapter so any game can use it. [Recognises](/docs/api/icontaineradapter#recognises) checks the format's magic bytes, `PACK` for a PAK, and [Open](/docs/api/icontaineradapter#open) reads the directory and returns an [IFileSource](/docs/api/ifilesource) that reads file data only when a file is opened.

## Converters

An [IConverter](/docs/api/iconverter) turns files from the mount into Evil Engine content. Content is named `<game>:<name>`, so Quake's palette is `quake:id1/gfx/palette`. When something asks for a texture under `quake:`, the converters bound to `quake` for textures are asked in order, and the first whose [Accepts](/docs/api/iconverter#accepts) says yes runs [Convert](/docs/api/iconverter#convert).

Read through the [ConversionContext](/docs/api/conversioncontext) only. Every read is recorded, so a cached result is converted again when a file it read changes. Bump [Version](/docs/api/iconverter#version) when your output changes for the same input. Throw to fail, and the pipeline reports it with the converter, the content id and the last file read.

## Trying it

`just build` builds every adapter into `.build/adapters`. `just mounts` builds too, then loads them and probes a location, printing each mount's layers, parts and externals:

```sh
just mounts "$HOME/.local/share/Steam/steamapps/common/Quake"
just mounts "$HOME/.local/share/Steam/steamapps/common/Quake" --convert texture quake:id1/gfx/palette
```

`--file <path>` says which layer a file comes from, and `--convert <kind> <id>` converts one thing and prints its result, with `--out <file>` to save it.

Tests go in `Libraries/Tests/EvilEngine.Adapters.Tests`. Tests that read a real install skip unless an environment variable points at one, like `EVIL_QUAKE`.

## Behaviour is the spec

A game's formats and rules are written from what the game does, checked against the running game, never translated from its code or its SDK's. Each title's work is done in two roles that never share a session: one studies the game and writes the spec, the other builds from the spec alone.
