# IConverter interface

> Turns a mounted game's files into one piece of Evil Engine content, such as a texture, a material, a map or a record set. Everything past a converter sees only the converted result, never the game's own format.

Namespace `EvilEngine.Conversion`, assembly `EvilEngine.Conversion`. Engine commit 4ff877b1.

```csharp
public interface IConverter
```

Turns a mounted game's files into one piece of Evil Engine content, such as a texture, a material, a map or a record set. Everything past a converter sees only the converted result, never the game's own format.

An adapter binds converters to a namespace with [AddConverter](/docs/api/adaptercontext#addconverter) or [AddOwnConverter](/docs/api/adaptercontext#addownconverter). When something asks for content of a kind under an id, the pipeline goes through the converters bound to that id's namespace for that kind, in the order they were bound, and the first whose [Accepts](#accepts) returns `true` runs [Convert](#convert). The result is cached against every file the converter read, and converted again when one of those files changes.

Quake's palette and colormap converter, whole:

```csharp
public sealed class QuakeLumpConverter : IConverter
{
    public string Id => "quake.lmp";

    public int Version => 1;

    public ContentKind Produces => ContentKind.Texture;

    public bool Accepts(ConversionContext context)
    {
        return Lump(context) is { } path && context.Exists(path);
    }

    public void Convert(ConversionContext context, Stream output)
    {
        var path = Lump(context) ?? throw new InvalidOperationException($"{context.Id} isn't a palette or colormap.");
        var data = context.Read(path).Span;
        var palette = path.Value.EndsWith(QuakeMap.Palette, StringComparison.Ordinal);
        var size = palette ? 256 * 3 : 256 * Shades;
        if (data.Length < size)
            throw new QuakeMapException(path.Value, $"is {data.Length} bytes, short of the {size} it needs.");
        var texture = Texture.Flat(palette ? TextureFormat.R8G8B8Unorm : TextureFormat.R8Unorm, 256,
            palette ? 1 : Shades, data[..size].ToArray()) with
        {
            Sampling = new TextureSampling(TextureWrap.Clamp, TextureWrap.Clamp, TextureWrap.Clamp, TextureFilter.Nearest,
                TextureMipFilter.None)
        };
        output.Adopt(TextureFile.Write(texture));
    }
}
```

`Lump` and `Shades` are the converter's own helpers, left out here.

## Members

### Id (property)

```csharp
string Id { get; }
```

A name for the converter, unique among the converters bound to one namespace for one kind. Binding a second converter with the same `Id` there throws. By convention it starts with the adapter's id, as in `quake.lmp` or `quake.maps`. It's shown in conversion errors and kept with each converted result.

### Version (property)

```csharp
int Version { get; }
```

A number to bump whenever the converter's output changes for the same input. Every bound converter's `Id` and `Version` for a namespace and kind go into the cache key, so a cached result made under a different version is converted again. The disk cache also keys on the converter's compiled code, but the version is what says the output changed on purpose.

### Produces (property)

```csharp
ContentKind Produces { get; }
```

The `ContentKind` this converter makes, such as `ContentKind.Texture` or `ContentKind.Record`. A converter is only ever asked for content of this kind.

### Accepts (method)

```csharp
bool Accepts(ConversionContext context)
```

Returns whether this converter makes the content the context asks for, by its `Kind` and `Id`. Return `false` to let the next bound converter try. Read only what you need to decide, through the context: `Exists`, `List` and the reads are all recorded, a missing file included, so a cached answer is checked again when they change.

If it throws, the conversion fails as if `Convert` had.

### Convert (method)

```csharp
void Convert(ConversionContext context, Stream output)
```

Reads the game's files through the context and writes the converted content to `output` in Evil Engine's own format for the kind. Write the bytes, or hand over a finished byte array with `output.Adopt(bytes)` to skip a copy.

Read files through the context only (`Read`, `TryRead`, `Open`, `TryOpen`, `List`), never from disk or from a mount directly, since what it reads is what the cache checks. Name the other content your result points at with `context.Reference(kind, name)`, and note a problem that didn't stop the conversion with `context.Warn(message)`.

To fail, throw. The pipeline catches it and reports the conversion as failed with your message, the converter's `Id`, the content id and the last path read, and nothing is cached.
