.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 JavaScript code generated by Kaitai Struct depends on the Kaitai Struct runtime library for JavaScript. You must add this dependency to your project before you can parse or serialize any 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(['exports', 'kaitai-struct/KaitaiStream'], factory);
  } else if (typeof exports === 'object' && exports !== null && typeof exports.nodeType !== 'number') {
    factory(exports, require('kaitai-struct/KaitaiStream'));
  } else {
    factory(root.HeroesOfMightAndMagicAgg || (root.HeroesOfMightAndMagicAgg = {}), root.KaitaiStream);
  }
})(typeof self !== 'undefined' ? self : this, function (HeroesOfMightAndMagicAgg_, KaitaiStream) {
/**
 * @see {@link https://web.archive.org/web/20170215190034/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 = [];
    for (var i = 0; i < this.numFiles; i++) {
      this.entries.push(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._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._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 = [];
      this._m_filenames = [];
      for (var i = 0; i < this.numFiles; i++) {
        this._raw__m_filenames.push(this._io.readBytes(15));
        var _io__raw__m_filenames = new KaitaiStream(this._raw__m_filenames[i]);
        this._m_filenames.push(new Filename(_io__raw__m_filenames, this, this._root));
      }
      this._io.seek(_pos);
      return this._m_filenames;
    }
  });

  return HeroesOfMightAndMagicAgg;
})();
HeroesOfMightAndMagicAgg_.HeroesOfMightAndMagicAgg = HeroesOfMightAndMagicAgg;
});