Recursive AI

Detect an invalid replay file in C#

OneScope is a free, open-source viewer for simulation recordings, with a desktop window or command-line control, from Recursive AI. MIT or Apache-2.0.

A replay file here is a recording in a OneScope module’s format, and that module decides which bytes it accepts. If you have your own format, see Write a custom replay viewer module.

Use CounterModule.cs and broken.replay from the zip.

Terminal
dotnet add package RecursiveAI.OneScope.Sdk

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using OneScope.Sdk;
try { CounterModule.OpenFile("broken.replay"); }
catch (ScopeFormatException error)
{
    Console.WriteLine(error.Message);
}

Unzip the example, then dotnet run.

Text
broken.replay: Expected 'COUNT ' at offset 0.

Parser and inputs

broken.replay contains COUNT followed by a newline. The parser expects COUNT (including the space), then a step count. The good file, counter.replay, contains:

Text
COUNT 4

OpenFile is a helper in CounterModule.cs that reads the file and calls Open; the full program calls Open directly.

The program reports the broken header, then opens the good file with the same parser:

C#
using OneScope.Sdk;
 
const string path = "broken.replay";
var bytes = File.ReadAllBytes(path);
try
{
    new CounterModule().Open(bytes, path);
    Console.WriteLine("Header accepted.");
}
catch (ScopeFormatException error)
{
    Console.WriteLine(error.Message);
}
 
const string good = "counter.replay";
var source = new CounterModule().Open(File.ReadAllBytes(good), good);
Console.WriteLine($"{good}: {source.Recording.StepCount} steps");
Text
broken.replay: Expected 'COUNT ' at offset 0.
counter.replay: 4 steps

A missing file is a read error from File.ReadAllBytes, not a ScopeFormatException from the parser.

Variations

To test a recognized header with a bad count, replace the call inside try in Program.cs:

C#
new CounterModule().Open("COUNT -1"u8.ToArray(), "bad-count.replay");
Text
bad-count.replay: Expected 0 to 1000 steps after 'COUNT '.

The short answer

Pass the replay bytes and the file name to the matching module’s Open method. With a module that follows the OneScope authoring contract, catch ScopeFormatException to report that the module rejected the data. The exception’s Input property identifies what the parser was reading, and Message includes the parser’s reason. OneScope defines the exception; the module decides which bytes it accepts.

How do you check a file’s format from its header in C#?

Check the contents, not the .replay extension. The counter example starts with a fixed byte prefix, COUNT; a replay format can use a fixed identifier like this to distinguish its files from other formats. The example then checks the step count before allocating the array of inputs. A file with the right prefix but a negative count still fails, as the variation shows. In a binary format with variable-length records, check that each declared length fits in the bytes that remain before slicing or allocating. Include the offending field or byte offset in the exception so the reader knows what to fix.

A rejection means the selected module cannot open these bytes. It does not distinguish a damaged replay from a valid replay for a different module. If you work with several formats, use the module for the recording you expect; a name or extension is not a substitute for parsing. Also decide whether you want to check just whether a file opens, or whether it replays as recorded. With recorded state hashes, History.Scan() can expose a playback mismatch through FirstHashMismatch. That check happens during playback and is separate from a parser rejection.

All onescope recipes