# IContainerAdapter interface

> Opens one archive or disc image format, such as id's PAK, Valve's VPK, ZIP or an ISO 9660 image, as a read-only IFileSource. An adapter registers one with AddContainer.

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

```csharp
public interface IContainerAdapter
```

Opens one archive or disc image format, such as id's PAK, Valve's VPK, ZIP or an ISO 9660 image, as a read-only [IFileSource](/docs/api/ifilesource). An adapter registers one with [AddContainer](/docs/api/adaptercontext#addcontainer).

The mount core uses containers in three places. A pointer to a file, like a disc image, opens through the first container that recognises it. When no game recognises a folder, each file at its top level that a container recognises is opened and probed in turn, up to three levels deep. And a game's mount plan can name a container inside its tree as a layer, which opens the first time something reads from it.

A container can also implement `IIndexedContainer`, which adds `ReadIndex` for the archive's own directory bytes, so a mount can fingerprint the archive by its index instead of its data.

The `pak` adapter's container:

```csharp
public sealed class PakContainer : IIndexedContainer
{
    public string Id => "pak";

    public bool Recognises(IFileSource parent, VirtualPath path)
    {
        if (!parent.TryOpen(path, out var stream))
            return false;
        using (stream)
        {
            Span<byte> head = stackalloc byte[4];
            return stream.ReadAtLeast(head, 4, false) == 4 && head.SequenceEqual(PakArchive.Signature);
        }
    }

    public byte[] ReadIndex(IFileSource parent, VirtualPath path)
    {
        using var access = parent.OpenAccess(path);
        return PakArchive.ReadIndex(access, $"{parent.Location}/{path}");
    }

    public IFileSource Open(IFileSource parent, VirtualPath path)
    {
        return OpenPak(parent, path);
    }
}
```

## Members

### Id (property)

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

The container's name, such as `pak`. It's recorded in the chain of containers a probe went through to find a game, so a mount found inside nested archives can say how it got there.

### Recognises (method)

```csharp
bool Recognises(IFileSource parent, VirtualPath path)
```

Returns whether the file at `path` in `parent` is this format. Containers are asked in the order they were registered and the first `true` wins, so check something specific to the format, like its magic bytes, not its extension.

It's asked about many files that aren't yours, every file at the top of a folder no game recognised among them, so read a few bytes at most and return `false` for anything that doesn't open.

### Open (method)

```csharp
IFileSource Open(IFileSource parent, VirtualPath path)
```

Opens the file at `path` in `parent` and returns its contents as an [IFileSource](/docs/api/ifilesource). Read the archive's directory here and read file data only when a file is opened. Give the source a `Location` that says where it came from; `pak` uses `$"{parent.Location}/{path}"`.

If the source you return implements `IDisposable`, the mount core disposes it once nothing holds it any more, so that's where to close the archive's handle. Throw `MountException` when the file is this format but can't be read, saying why.
