.agg file format of Heroes of Might and Magic: C++98/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++98/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++98/STL source code to parse .agg file format of Heroes of Might and Magic

heroes_of_might_and_magic_agg.h

#ifndef HEROES_OF_MIGHT_AND_MAGIC_AGG_H_
#define HEROES_OF_MIGHT_AND_MAGIC_AGG_H_

// 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 <vector>

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

/**
 * \sa https://web.archive.org/web/20170215190034/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 = 0, heroes_of_might_and_magic_agg_t* p__root = 0);

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 = 0, heroes_of_might_and_magic_agg_t* p__root = 0);

    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 = 0, heroes_of_might_and_magic_agg_t* p__root = 0);

    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::vector<filename_t*>* m_filenames;

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

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

public:
    uint16_t num_files() const { return m_num_files; }
    std::vector<entry_t*>* entries() const { return m_entries; }
    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; }
    std::vector<kaitai::kstream*>* _io__raw_filenames() const { return m__io__raw_filenames; }
};

#endif  // HEROES_OF_MIGHT_AND_MAGIC_AGG_H_

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 = 0;
    m_filenames = 0;
    m__raw_filenames = 0;
    m__io__raw_filenames = 0;
    f_filenames = false;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void heroes_of_might_and_magic_agg_t::_read() {
    m_num_files = m__io->read_u2le();
    m_entries = new std::vector<entry_t*>();
    const int l_entries = num_files();
    for (int i = 0; i < l_entries; i++) {
        m_entries->push_back(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 (m_entries) {
        for (std::vector<entry_t*>::iterator it = m_entries->begin(); it != m_entries->end(); ++it) {
            delete *it;
        }
        delete m_entries; m_entries = 0;
    }
    if (f_filenames) {
        if (m__raw_filenames) {
            delete m__raw_filenames; m__raw_filenames = 0;
        }
        if (m__io__raw_filenames) {
            for (std::vector<kaitai::kstream*>::iterator it = m__io__raw_filenames->begin(); it != m__io__raw_filenames->end(); ++it) {
                delete *it;
            }
            delete m__io__raw_filenames; m__io__raw_filenames = 0;
        }
        if (m_filenames) {
            for (std::vector<filename_t*>::iterator it = m_filenames->begin(); it != m_filenames->end(); ++it) {
                delete *it;
            }
            delete m_filenames; m_filenames = 0;
        }
    }
}

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;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

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;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

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<heroes_of_might_and_magic_agg_t::filename_t*>* heroes_of_might_and_magic_agg_t::filenames() {
    if (f_filenames)
        return m_filenames;
    std::streampos _pos = m__io->pos();
    m__io->seek((entries()->back()->offset() + entries()->back()->size()));
    m__raw_filenames = new std::vector<std::string>();
    m__io__raw_filenames = new std::vector<kaitai::kstream*>();
    m_filenames = new std::vector<filename_t*>();
    const int l_filenames = num_files();
    for (int i = 0; i < l_filenames; i++) {
        m__raw_filenames->push_back(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->push_back(io__raw_filenames);
        m_filenames->push_back(new filename_t(io__raw_filenames, this, m__root));
    }
    m__io->seek(_pos);
    f_filenames = true;
    return m_filenames;
}