.agg file format of Heroes of Might and Magic: C++11/STL parsing library

Application

Heroes of Might and Magic

File extension

agg

KS implementation details

License: CC0-1.0

This page hosts a formal specification of .agg file format of Heroes of Might and Magic using Kaitai Struct. This specification can be automatically translated into a variety of programming languages to get a parsing library.

Usage

Runtime library

All parsing code for C++11/STL generated by Kaitai Struct depends on the C++/STL runtime library. You have to install it before you can parse data.

For C++, the easiest way is to clone the runtime library sources and build them along with your project.

Code

Using Kaitai Struct in C++/STL usually consists of 3 steps.

  1. We need to create an STL input stream (std::istream). One can open local file for that, or use existing std::string or char* buffer.
    #include <fstream>
    
    std::ifstream is("path/to/local/file.agg", std::ifstream::binary);
    
    #include <sstream>
    
    std::istringstream is(str);
    
    #include <sstream>
    
    const char buf[] = { ... };
    std::string str(buf, sizeof buf);
    std::istringstream is(str);
    
  2. We need to wrap our input stream into Kaitai stream:
    #include "kaitai/kaitaistream.h"
    
    kaitai::kstream ks(&is);
    
  3. And finally, we can invoke the parsing:
    heroes_of_might_and_magic_agg_t data(&ks);
    

After that, one can get various attributes from the structure by invoking getter methods like:

data.num_files() // => get num files

C++11/STL source code to parse .agg file format of Heroes of Might and Magic

heroes_of_might_and_magic_agg.h

#pragma once

// This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild

#include "kaitai/kaitaistruct.h"
#include <stdint.h>
#include <memory>
#include <vector>

#if KAITAI_STRUCT_VERSION < 9000L
#error "Incompatible Kaitai Struct C++/STL API: version 0.9 or later is required"
#endif

/**
 * \sa http://rewiki.regengedanken.de/wiki/.AGG_(Heroes_of_Might_and_Magic) Source
 */

class heroes_of_might_and_magic_agg_t : public kaitai::kstruct {

public:
    class entry_t;
    class filename_t;

    heroes_of_might_and_magic_agg_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent = nullptr, heroes_of_might_and_magic_agg_t* p__root = nullptr);

private:
    void _read();
    void _clean_up();

public:
    ~heroes_of_might_and_magic_agg_t();

    class entry_t : public kaitai::kstruct {

    public:

        entry_t(kaitai::kstream* p__io, heroes_of_might_and_magic_agg_t* p__parent = nullptr, heroes_of_might_and_magic_agg_t* p__root = nullptr);

    private:
        void _read();
        void _clean_up();

    public:
        ~entry_t();

    private:
        bool f_body;
        std::string m_body;

    public:
        std::string body();

    private:
        uint16_t m_hash;
        uint32_t m_offset;
        uint32_t m_size;
        uint32_t m_size2;
        heroes_of_might_and_magic_agg_t* m__root;
        heroes_of_might_and_magic_agg_t* m__parent;

    public:
        uint16_t hash() const { return m_hash; }
        uint32_t offset() const { return m_offset; }
        uint32_t size() const { return m_size; }
        uint32_t size2() const { return m_size2; }
        heroes_of_might_and_magic_agg_t* _root() const { return m__root; }
        heroes_of_might_and_magic_agg_t* _parent() const { return m__parent; }
    };

    class filename_t : public kaitai::kstruct {

    public:

        filename_t(kaitai::kstream* p__io, heroes_of_might_and_magic_agg_t* p__parent = nullptr, heroes_of_might_and_magic_agg_t* p__root = nullptr);

    private:
        void _read();
        void _clean_up();

    public:
        ~filename_t();

    private:
        std::string m_str;
        heroes_of_might_and_magic_agg_t* m__root;
        heroes_of_might_and_magic_agg_t* m__parent;

    public:
        std::string str() const { return m_str; }
        heroes_of_might_and_magic_agg_t* _root() const { return m__root; }
        heroes_of_might_and_magic_agg_t* _parent() const { return m__parent; }
    };

private:
    bool f_filenames;
    std::unique_ptr<std::vector<std::unique_ptr<filename_t>>> m_filenames;

public:
    std::vector<std::unique_ptr<filename_t>>* filenames();

private:
    uint16_t m_num_files;
    std::unique_ptr<std::vector<std::unique_ptr<entry_t>>> m_entries;
    heroes_of_might_and_magic_agg_t* m__root;
    kaitai::kstruct* m__parent;
    std::unique_ptr<std::vector<std::string>> m__raw_filenames;
    std::unique_ptr<std::vector<std::unique_ptr<kaitai::kstream>>> m__io__raw_filenames;

public:
    uint16_t num_files() const { return m_num_files; }
    std::vector<std::unique_ptr<entry_t>>* entries() const { return m_entries.get(); }
    heroes_of_might_and_magic_agg_t* _root() const { return m__root; }
    kaitai::kstruct* _parent() const { return m__parent; }
    std::vector<std::string>* _raw_filenames() const { return m__raw_filenames.get(); }
    std::vector<std::unique_ptr<kaitai::kstream>>* _io__raw_filenames() const { return m__io__raw_filenames.get(); }
};

heroes_of_might_and_magic_agg.cpp

// This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild

#include "heroes_of_might_and_magic_agg.h"

heroes_of_might_and_magic_agg_t::heroes_of_might_and_magic_agg_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent, heroes_of_might_and_magic_agg_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = this;
    m_entries = nullptr;
    m_filenames = nullptr;
    m__raw_filenames = nullptr;
    m__io__raw_filenames = nullptr;
    f_filenames = false;
    _read();
}

void heroes_of_might_and_magic_agg_t::_read() {
    m_num_files = m__io->read_u2le();
    int l_entries = num_files();
    m_entries = std::unique_ptr<std::vector<std::unique_ptr<entry_t>>>(new std::vector<std::unique_ptr<entry_t>>());
    m_entries->reserve(l_entries);
    for (int i = 0; i < l_entries; i++) {
        m_entries->push_back(std::move(std::unique_ptr<entry_t>(new entry_t(m__io, this, m__root))));
    }
}

heroes_of_might_and_magic_agg_t::~heroes_of_might_and_magic_agg_t() {
    _clean_up();
}

void heroes_of_might_and_magic_agg_t::_clean_up() {
    if (f_filenames) {
    }
}

heroes_of_might_and_magic_agg_t::entry_t::entry_t(kaitai::kstream* p__io, heroes_of_might_and_magic_agg_t* p__parent, heroes_of_might_and_magic_agg_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    f_body = false;
    _read();
}

void heroes_of_might_and_magic_agg_t::entry_t::_read() {
    m_hash = m__io->read_u2le();
    m_offset = m__io->read_u4le();
    m_size = m__io->read_u4le();
    m_size2 = m__io->read_u4le();
}

heroes_of_might_and_magic_agg_t::entry_t::~entry_t() {
    _clean_up();
}

void heroes_of_might_and_magic_agg_t::entry_t::_clean_up() {
    if (f_body) {
    }
}

std::string heroes_of_might_and_magic_agg_t::entry_t::body() {
    if (f_body)
        return m_body;
    std::streampos _pos = m__io->pos();
    m__io->seek(offset());
    m_body = m__io->read_bytes(size());
    m__io->seek(_pos);
    f_body = true;
    return m_body;
}

heroes_of_might_and_magic_agg_t::filename_t::filename_t(kaitai::kstream* p__io, heroes_of_might_and_magic_agg_t* p__parent, heroes_of_might_and_magic_agg_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    _read();
}

void heroes_of_might_and_magic_agg_t::filename_t::_read() {
    m_str = kaitai::kstream::bytes_to_str(m__io->read_bytes_term(0, false, true, true), std::string("ASCII"));
}

heroes_of_might_and_magic_agg_t::filename_t::~filename_t() {
    _clean_up();
}

void heroes_of_might_and_magic_agg_t::filename_t::_clean_up() {
}

std::vector<std::unique_ptr<heroes_of_might_and_magic_agg_t::filename_t>>* heroes_of_might_and_magic_agg_t::filenames() {
    if (f_filenames)
        return m_filenames.get();
    std::streampos _pos = m__io->pos();
    m__io->seek((entries()->back()->offset() + entries()->back()->size()));
    int l_filenames = num_files();
    m__raw_filenames = std::unique_ptr<std::vector<std::string>>(new std::vector<std::string>());
    m__raw_filenames->reserve(l_filenames);
    m__io__raw_filenames = std::unique_ptr<std::vector<std::unique_ptr<kaitai::kstream>>>(new std::vector<std::unique_ptr<kaitai::kstream>>());
    m__io__raw_filenames->reserve(l_filenames);
    m_filenames = std::unique_ptr<std::vector<std::unique_ptr<filename_t>>>(new std::vector<std::unique_ptr<filename_t>>());
    m_filenames->reserve(l_filenames);
    for (int i = 0; i < l_filenames; i++) {
        m__raw_filenames->push_back(std::move(m__io->read_bytes(15)));
        kaitai::kstream* io__raw_filenames = new kaitai::kstream(m__raw_filenames->at(m__raw_filenames->size() - 1));
        m__io__raw_filenames->emplace_back(io__raw_filenames);
        m_filenames->push_back(std::move(std::unique_ptr<filename_t>(new filename_t(io__raw_filenames, this, m__root))));
    }
    m__io->seek(_pos);
    f_filenames = true;
    return m_filenames.get();
}