# ConversionContext class

> What a converter reads through, in Accepts and Convert: the content asked for, the namespace's files, and the places to note what the result points at and what went wrong.

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

```csharp
public sealed class ConversionContext
```

What a converter reads through, in [Accepts](/docs/api/iconverter#accepts) and [Convert](/docs/api/iconverter#convert): the content asked for, the namespace's files, and the places to note what the result points at and what went wrong.

Every read is recorded with a hash of what it found, a missing file and a folder listing included. A cached result is used again only while every one of those still matches, so read through here and nowhere else. The pipeline makes the context. You never make one yourself.

## Members

### Kind (property)

```csharp
public ContentKind Kind { get; }
```

The [ContentKind](/docs/api/contentkind) being asked for, always the converter's [Produces](/docs/api/iconverter#produces).

### Id (property)

```csharp
public ContentId Id { get; }
```

The [ContentId](/docs/api/contentid) being asked for. Its namespace is the one the converter is bound to, and its name is what the converter turns into paths to read.

### LastRead (property)

```csharp
public VirtualPath? LastRead { get; }
```

The last path read, or `null` before anything is. A failed conversion is reported with it.

### Exists (method)

```csharp
public bool Exists(VirtualPath path)
```

Returns whether a file is at `path` in the namespace. A file that isn't there is recorded too, so the result converts again if one appears.

### List (method)

```csharp
public IReadOnlyList<FileEntry> List(VirtualPath directory, bool recursive = false)
```

Lists the files under `directory`, sorted by path, directly in it or at any depth with `recursive`. The listing is recorded by its paths, sizes and the layers they come from, so adding, removing or moving a file between layers converts the result again.

### LayerOf (method)

```csharp
public FileLayer? LayerOf(VirtualPath path)
```

Returns which layer the file at `path` comes from, its name and role, or `null` when the namespace isn't a layered mount. It records a listing of the file's folder if nothing already covers it, so the result converts again if another layer's copy takes over. The map converters tag a map's origin by its layer's role: community for an addon, download, workshop or mod, expansion for DLC, official otherwise.

### TryRead (method)

```csharp
public bool TryRead(VirtualPath path, out ReadOnlyMemory<byte> data)
```

Reads the whole file at `path`, or returns `false` if it isn't there. The data is the file's bytes from the layer that wins.

### Copies (method)

```csharp
public IReadOnlyList<string> Copies(VirtualPath path)
```

Names every layer that holds a file at `path`, the winning one first. Read one of them with [TryReadCopy](#tryreadcopy).

### TryReadCopy (method)

```csharp
public bool TryReadCopy(VirtualPath path, string layer, out ReadOnlyMemory<byte> data)
```

Reads the copy of `path` in the layer named `layer`, rather than the one that wins, or returns `false` if that layer has none. It's recorded apart from the winning copy. It's for when the winning copy can't be used, as when a Garry's Mod addon's copy of a model is a stub its Lua fills in at runtime and the one beneath stands in.

### TryOpen (method)

```csharp
public bool TryOpen(VirtualPath path, out IRandomAccess? access)
```

Opens the file at `path` for reading at any offset, without reading it whole, or returns `false` if it isn't there. It's the one for a big archive inside the mount, like a map's BSP, when you only need some of it.

### Open (method)

```csharp
public IRandomAccess Open(VirtualPath path)
```

[TryOpen](#tryopen), throwing `FileNotFoundException` naming the content and the path when the file isn't there.

### Read (method)

```csharp
public ReadOnlyMemory<byte> Read(VirtualPath path)
```

[TryRead](#tryread), throwing `FileNotFoundException` naming the content and the path when the file isn't there.

### Reference (method)

```csharp
public ContentId Reference(ContentKind kind, string name)
```

```csharp
public ContentId Reference(ContentKind kind, ContentId id)
```

Names a piece of content the result points at, by kind and id, and returns the id. Give a bare name for content in the same namespace, or a whole [ContentId](/docs/api/contentid) for another. Starting a map follows every reference out from its manifest, so everything the map reaches converts as it loads.

### Warn (method)

```csharp
public void Warn(string message)
```

Notes a problem that didn't stop the conversion, kept with the result and shown with it. Say what was left out and why.

### Shared (method)

```csharp
public T Shared<T>(string key, Func<ConversionContext, T> build) where T : class
```

Parses something once for every conversion that needs it, under a `key`, and hands each the same object. A Source map's BSP is read once for its manifest, cells, models and lightmaps this way. The parse runs on a context of its own, and everything it read, listed, referenced and warned about counts as this conversion's too, so each result still converts again when the parse's inputs change. The object is held in memory and parsed again once any of those inputs change.

Start the key with the converter's own prefix and the path, as `source.bsp:<path>`. Throws `InvalidOperationException` if the key is already shared as a different type.
