arz

package module
v2.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 29, 2026 License: MIT Imports: 9 Imported by: 0

README

ARZ

The arz library contains tools to parse content of ARZ-files, which have been used in Grim Dawn game as archives of parameters. It is named after .arz extension of the files. The project also provides a description of the format of the files.

Usage

The usage is mainly about calls to function Decode and methods of instances of type Db, pointers at which are returned by the function:

import "codeberg.org/alex-ichukov/arz"

func main() {
	// Get content of an ARZ-database in form of slice of bytes
	data := …

	// Decode the data
	db, err := arz.Decode(data)
	if err != nil {
		// Process the error
	}

	for _, r := range db.Records {
		// Doesn't allocate a string
		if string(db.Tag(r)) == "WeaponHunting_Ranged2h" {
			// Process a record of a two-handed ranged weapon
			…
		}
	}
}

Instances of type Db hold inside provided slices of data. Methods of the type return subslices of the slice for the maximum efficiency. The instances serve as a kind of god-objects, as instances of the types Record and Field provide only minimal data. Even fields of a record are accessed through an intermediate object of type Fields, which is managed as a part of an instance of Db. Such an approach allows to unpack compressed content of the fields on-fly, transparently for a user.

Accordingly to the format of the records, every record can have many fields, and every field can have many values of one single type: 32-bit signed integer, single-precision floating-point number, string or boolean. String values are represented by indices in the array of strings of the database. This makes processing of the values tricky. An example of such a processing can be found within an application in examples directory of the project. Any database of the game sports dozens of thousands of records and millions of fields in total, and most of the fields are single-valued. To process all the situations efficiently, method Seq of type Fields uses an optimization: it yields a field with a possible sub-iterator over values of the field. If the field has only one value, nil is yielded instead of the sub-iterator, and the value is stored within the field directly. Thus, the following situation arises frequently:

import "codeberg.org/alex-ichukov/arz"

func main() {
	…
	for _, r := range db.Records {
		for f, values := range db.Fields(r).Seq {
			if values == nil {
				// Process f.Value()
			} else {
				for v := range values {
					// Process v
				}
			}
		}
	}
}

Original databases of the game are pretty big (and, to mention, proprietary), and sometimes a small generated ARZ-database is required for, say, testing purposes. For that, function Encode of the library could be used. It takes a slice of records of type EncRec (which are completely distinct from instances of Record), which represent the database of interest. Its usage can be depicted in the following way:

import "codeberg.org/alex-ichukov/arz"

func main() {
	r := []arz.EncRec{
		{
			Name: "record1",
			Tag:  "tag1",

			Ints: map[string][]int32{
				"field1": {0},
				"field5": {9},
				"field9": {1, 2, 3},
			},

			Floats: map[string][]float32{
				"field2":  {0.0},
				"field6":  {3.7},
				"field10": {3.7, 3.14159, 2.71828},
			},

			Strings: map[string][]string{
				"field3":  {""},
				"field7":  {"singlestring"},
				"field11": {"arraystring1", "arraystring2"},
			},

			Bools: map[string][]bool{
				"field4":  {false},
				"field8":  {true},
				"field12": {false, true},
			},
		},
		{
			Name:  "record2",
			Tag:   "tag2",
			Bools: map[string][]bool{"field1": {false}},
		},
	}

	data := arz.Encode(r)
	// Process data
	…
}

The function assumes, that the input slice is not extreme in amount of records or fields, so it doesn't even return an error value.

License

The library is licensed under MIT license.

Documentation

Overview

Package arz provides tools to work with ARZ-databases, which have been used in Grim Dawn game. Main workhorses of the library are functions Decode and Encode, which allow to parse content of an ARZ-database and, vice versa, compile it from description of records.

Index

Constants

View Source
const (
	// FieldTypeInt32 marks, that values of the field should be
	// interpretted as 32-bit signed integers
	FieldTypeInt32 uint16 = iota

	// FiledTypeFloat32 marks, that values of the field should be
	// interpretted as single-precision floating-point numbers
	FieldTypeFloat32

	// FieldTypeString marks, that values of the field should be
	// interpretted as indices in global database table of strings
	FieldTypeString

	// FieldTypeBool marks, that values of the field should be interpretted
	// as boolean values: zero would mean boolean false and any non-zero
	// value would mean boolean true
	FieldTypeBool
)
View Source
const Version = "v2.1.0"

Version of the library

Variables

View Source
var ErrFieldsDataOutOfRange error = &errFieldsDataOutOfRange{}

The singleton instance of [errFieldsDataOutOfRange] as a signal, that database can't access fields of a record due to invalid data in the record

View Source
var ErrInvalidHeader error = &errInvalidHeader{}

The singleton instance of [errInvalidHeader] as a signal, that header of a database is invalid

View Source
var ErrInvalidString error = &errInvalidString{}

The singleton instance of [errInvalidString] as a signal, that database string is invalid

View Source
var ErrInvalidStringSection error = &errInvalidStringSection{}

The singleton instance of [errInvalidStringSection] as a signal, that string section of a database is invalid

View Source
var ErrNotEnoughData error = &errNotEnoughData{}

The singleton instance of [errNotEnoughData] as a signal, that there is not enough data to decode a header

Functions

func Encode added in v2.1.0

func Encode(r []EncRec) []byte

Encode takes a slice of instances of EncRec and encodes the records into a slice of bytes accordingly to the format of ARZ-databases. It is supposed to encode _small_ amount of data, so it assumes, that no input record sports a field with more than 65535 values and that the positions and offsets fit into unsigned 32-bit integers well.

Types

type Db

type Db struct {
	// contains filtered or unexported fields
}

Db is a type of depictions of ARZ-databases

func Decode

func Decode(data []byte) (*Db, error)

Decode takes a slice of bytes with data of an ARZ-database, parses it and returns a pointer to Db instance with the parsed information. It checks the data on consistency and returns a corresponding error in case of found disrepancies.

func (*Db) Fields

func (db *Db) Fields(r Record) (Fields, error)

Fields takes a record's decription and returns a prepared instance of Fields with the data decompressed from the database. The instance is valid until the next call of the method. If the description is malformed or an error has happened during the decompression, the method returns a corresponding error.

func (*Db) Records

func (db *Db) Records(yield func(Record) bool)

Records takes a function, which collects instances of Record, and iterates over the records data from the database, yielding the records to the function until it returns boolean false. It is supposed to be used in 'for range' constructions. The method does nothing, if the function is nil. It doesn't panic, if the records data is malformed.

func (*Db) String

func (db *Db) String(n uint32) []byte

String takes an index of a string within the database and returns a slice of bytes with the string. If the index is out of range, the method returns nil slice.

func (*Db) Tag

func (db *Db) Tag(r Record) []byte

Tag takes a record's description and returns a slice of bytes from within the database with the tag of the record. If the description is malformed, the method returns nil slice.

type EncRec added in v2.1.0

type EncRec struct {
	// Name is the name of the record
	Name string

	// Tag is the tag of the record
	Tag string

	// Ints is a collection of fields of the record with integer values.
	// Nil collection should be treated as empty.
	Ints map[string][]int32

	// Floats is a collection of fields of the record with single-precision
	// floating point values. Nil collection should be treated as empty.
	Floats map[string][]float32

	// Strings is a collection of fields of the record with string values.
	// Nil collection should be treated as empty.
	Strings map[string][]string

	// Bools is a collection of fields of the record with boolean values.
	// Nil collection should be treated as empty.
	Bools map[string][]bool
}

EncRec represents records of ARZ-database for the process of encoding

type ErrNotSupported added in v2.1.0

type ErrNotSupported struct {
	// contains filtered or unexported fields
}

ErrNotSupported represents errors, which signal, that the database's version is not supported

func (*ErrNotSupported) Error added in v2.1.0

func (e *ErrNotSupported) Error() string

Error returns a description of the error. It implements the corresponding method of the interface error.

func (*ErrNotSupported) Version added in v2.1.0

func (e *ErrNotSupported) Version() uint32

Version returns version of the database

type ErrStringNotSupported added in v2.1.0

type ErrStringNotSupported struct {
	// contains filtered or unexported fields
}

ErrStringNotSupported represents errors, which signal, that decoding of a database string is not supported

func (*ErrStringNotSupported) Error added in v2.1.0

func (e *ErrStringNotSupported) Error() string

Error returns a description of the error. It implements the corresponding method of the interface error.

func (*ErrStringNotSupported) Len added in v2.1.0

func (e *ErrStringNotSupported) Len() uint32

Len returns length of the database string

func (*ErrStringNotSupported) Pos added in v2.1.0

func (e *ErrStringNotSupported) Pos() int

Pos returns position of the database string within the data

type Field

type Field struct {
	// contains filtered or unexported fields
}

Field is a type of fields of records of ARZ-archives

func (Field) Name

func (f Field) Name() uint32

Name returns index of the field's name in the strings array of its ARZ-archive

func (Field) Type

func (f Field) Type() uint16

Type returns type of the field's values

func (Field) Value

func (f Field) Value() uint32

Value returns value of the field, if the field has exactly one value. It has no sense in other cases. The value must be interpreted with help of [Type] of the field.

func (Field) ValuesAmount

func (f Field) ValuesAmount() uint16

ValuesAmount returns amount of field's values

type Fields

type Fields struct {
	// contains filtered or unexported fields
}

Fields is a type of intermediate holders of decompressed data of fields, which main purpose is to provide iterators over fields and their values

func (Fields) Seq

func (f Fields) Seq(yield func(Field, iter.Seq[uint32]) bool)

Seq takes a yielding function, which collects fields and iterators of their values, and yields the fields and values from the inner data to the function. It does nothing, if the function is nil. If a current field has exactly one value, the method yields nil iterator over values, as it fills the only value directly in the field. If the inner data is malformed or inconsistent, the method doesn't produce excess fuss, yielding the fields and values, while it's possible. So, its behavior allows to get less values for a field, if the data is invalid.

type Record

type Record struct {
	// contains filtered or unexported fields
}

Record is a type of packed descriptions of records from ARZ-archives. The descriptions don't include depiction of fields of the records, as the fields should be queried directly from the archives.

func (Record) Name

func (r Record) Name() uint32

Name returns index of the record's name in the strings array of its ARZ-archive

Directories

Path Synopsis
examples
arzlist command
Package internal is a container of subpackages, which should be protected from import to other projects
Package internal is a container of subpackages, which should be protected from import to other projects
strpac
Package strpac provides tools to pack descriptions of strings in ARZ-archives of Grim Dawn game.
Package strpac provides tools to pack descriptions of strings in ARZ-archives of Grim Dawn game.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL