Editor/Embedded/ValveResourceFormat/Blocks/RawBinary.cs

An abstract Block subclass representing an opaque region of raw binary in a Valve resource file. It skips reading data into memory on Read, can serialize by copying bytes from the resource reader stream into a supplied stream, and writes a placeholder text for textual output.

File Access
#nullable enable
using System;
using System.Collections.Generic;
using System.Linq;
using HumanoidRetargeter.EditorTools.Embedded.ValveResourceFormat.Utils;
using System.IO;

namespace HumanoidRetargeter.EditorTools.Embedded.ValveResourceFormat.Blocks;

/// <summary>
/// An opaque block of raw binary data.
/// </summary>
public abstract class RawBinary : Block
{
    /// <inheritdoc/>
    public override void Read(BinaryReader reader)
    {
        // Binary data is not read into memory upon load
    }

    /// <inheritdoc/>
    public override void Serialize(Stream stream)
    {
        if (Resource?.Reader == null)
        {
            throw new NotImplementedException("Serializing this block currently only works when modifying an existing resource.");
        }

        // The dumbest implementation.
        var data = new byte[Size];
        Resource.Reader.BaseStream.Position = Offset;
        Resource.Reader.Read(data);
        stream.Write(data);
    }

    /// <inheritdoc/>
    public override void WriteText(IndentedTextWriter writer)
    {
        writer.WriteLine("Not yet.");
    }
}