.agg file format of Heroes of Might and Magic: JavaScript 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 JavaScript generated by Kaitai Struct depends on the JavaScript runtime library. You have to install it before you can parse data.

The JavaScript runtime library is available at npm:

npm install kaitai-struct

Code

See the usage examples in the JavaScript notes.

Parse structure from an ArrayBuffer:

var arrayBuffer = ...;
var data = new HeroesOfMightAndMagicAgg(new KaitaiStream(arrayBuffer));

After that, one can get various attributes from the structure by accessing fields or properties like:

data.numFiles // => get num files

JavaScript source code to parse .agg file format of Heroes of Might and Magic

HeroesOfMightAndMagicAgg.js

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

(function (root, factory) {
  if (typeof define === 'function' && define.amd) {
    define(['kaitai-struct/KaitaiStream'], factory);
  } else if (typeof module === 'object' && module.exports) {
    module.exports = factory(require('kaitai-struct/KaitaiStream'));
  } else {
    root.HeroesOfMightAndMagicAgg = factory(root.KaitaiStream);
  }
}(this, function (KaitaiStream) {
/**
 * @see {@link http://rewiki.regengedanken.de/wiki/.AGG_(Heroes_of_Might_and_Magic)|Source}
 */

var HeroesOfMightAndMagicAgg = (function() {
  function HeroesOfMightAndMagicAgg(_io, _parent, _root) {
    this._io = _io;
    this._parent = _parent;
    this._root = _root || this;

    this._read();
  }
  HeroesOfMightAndMagicAgg.prototype._read = function() {
    this.numFiles = this._io.readU2le();
    this.entries = new Array(this.numFiles);
    for (var i = 0; i < this.numFiles; i++) {
      this.entries[i] = new Entry(this._io, this, this._root);
    }
  }

  var Entry = HeroesOfMightAndMagicAgg.Entry = (function() {
    function Entry(_io, _parent, _root) {
      this._io = _io;
      this._parent = _parent;
      this._root = _root || this;

      this._read();
    }
    Entry.prototype._read = function() {
      this.hash = this._io.readU2le();
      this.offset = this._io.readU4le();
      this.size = this._io.readU4le();
      this.size2 = this._io.readU4le();
    }
    Object.defineProperty(Entry.prototype, 'body', {
      get: function() {
        if (this._m_body !== undefined)
          return this._m_body;
        var _pos = this._io.pos;
        this._io.seek(this.offset);
        this._m_body = this._io.readBytes(this.size);
        this._io.seek(_pos);
        return this._m_body;
      }
    });

    return Entry;
  })();

  var Filename = HeroesOfMightAndMagicAgg.Filename = (function() {
    function Filename(_io, _parent, _root) {
      this._io = _io;
      this._parent = _parent;
      this._root = _root || this;

      this._read();
    }
    Filename.prototype._read = function() {
      this.str = KaitaiStream.bytesToStr(this._io.readBytesTerm(0, false, true, true), "ASCII");
    }

    return Filename;
  })();
  Object.defineProperty(HeroesOfMightAndMagicAgg.prototype, 'filenames', {
    get: function() {
      if (this._m_filenames !== undefined)
        return this._m_filenames;
      var _pos = this._io.pos;
      this._io.seek((this.entries[this.entries.length - 1].offset + this.entries[this.entries.length - 1].size));
      this._raw__m_filenames = new Array(this.numFiles);
      this._m_filenames = new Array(this.numFiles);
      for (var i = 0; i < this.numFiles; i++) {
        this._raw__m_filenames[i] = this._io.readBytes(15);
        var _io__raw__m_filenames = new KaitaiStream(this._raw__m_filenames[i]);
        this._m_filenames[i] = new Filename(_io__raw__m_filenames, this, this._root);
      }
      this._io.seek(_pos);
      return this._m_filenames;
    }
  });

  return HeroesOfMightAndMagicAgg;
})();
return HeroesOfMightAndMagicAgg;
}));