resource

package
v1.10.1 Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2024 License: BSD-3-Clause Imports: 8 Imported by: 0

Documentation

Overview

Package resource provides interfaces and primitives for low-level handling of resources.

Index

Constants

View Source
const (
	// Palette refers to color tables.
	Palette = ContentType(0x00)
	// Text refers to texts.
	Text = ContentType(0x01)
	// Bitmap refers to images.
	Bitmap = ContentType(0x02)
	// Font refers to font descriptions.
	Font = ContentType(0x03)
	// Animation refers to graphical animations.
	Animation = ContentType(0x04)
	// Sound refers to audio samples.
	Sound = ContentType(0x07)
	// Geometry refers to 3D models.
	Geometry = ContentType(0x0F)
	// Movie refers to audio logs and cutscenes.
	Movie = ContentType(0x11)
	// Archive refers to archive data.
	Archive = ContentType(0x30)
)

Variables

This section is empty.

Functions

func ErrBlockNotFound added in v1.8.0

func ErrBlockNotFound(index, available int) error

ErrBlockNotFound returns an error specifying the given index does not identify a valid block.

func ErrNotFound added in v1.8.0

func ErrNotFound(id ID) error

ErrNotFound returns an error specifying the given ID doesn't have an associated resource.

func ErrWrongType added in v1.8.0

func ErrWrongType(key Key, expected ContentType) error

ErrWrongType returns an error specifying a resource was of unexpected type.

Types

type Block added in v1.4.0

type Block []byte

Block is one set of bytes stored in a resource. The interpretation of the bytes is dependent on the content type of the resource.

type BlockNotFoundError added in v1.8.0

type BlockNotFoundError struct {
	Index     int
	Available int
}

BlockNotFoundError indicates the unknown block index.

func (BlockNotFoundError) Error added in v1.8.0

func (err BlockNotFoundError) Error() string

Error implements the error interface.

type BlockProvider

type BlockProvider interface {
	// BlockCount returns the number of available blocks in the resource.
	// Simple resources will always have exactly one block.
	BlockCount() int

	// Block returns the reader for the identified block.
	// Each call returns a new reader instance.
	// Data provided by this reader is always uncompressed.
	Block(index int) (io.Reader, error)
}

BlockProvider are capable of returning block data of a resource.

type Blocks added in v1.4.0

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

Blocks is a list of blocks in memory.

func BlocksFrom added in v1.4.0

func BlocksFrom(data [][]byte) Blocks

BlocksFrom returns a blocks instance from given data.

func (Blocks) Block added in v1.4.0

func (blocks Blocks) Block(index int) (io.Reader, error)

Block returns the reader for the identified block. Each call returns a new reader instance.

func (Blocks) BlockCount added in v1.4.0

func (blocks Blocks) BlockCount() int

BlockCount returns the number of available blocks.

func (Blocks) BlockRaw added in v1.4.0

func (blocks Blocks) BlockRaw(index int) ([]byte, error)

BlockRaw returns the raw byte slice stored in the identified block.

func (*Blocks) Set added in v1.4.0

func (blocks *Blocks) Set(data [][]byte)

Set the data of all the blocks.

func (*Blocks) SetBlock added in v1.4.0

func (blocks *Blocks) SetBlock(index int, data []byte)

SetBlock sets the data of the identified block.

type ChangeNotifier

type ChangeNotifier struct {
	Localizer Localizer
	Callback  ModificationCallback
}

ChangeNotifier is a utility that assists in detecting changes in modifying resources. A callback is called for any resource identifier that refers to resources that are different after a modification.

Use this utility in combination to resource lists where resources can be overwritten by other entries. Changes in order, or content, affects how a resource is resolved.

func (ChangeNotifier) ModifyAndNotify

func (notifier ChangeNotifier) ModifyAndNotify(modifier func(), affectedIDs ...[]ID)

ModifyAndNotify must be called with a range of affected IDs that will change during the call of the modifier. A hash snapshot is taken before and after the modifier, considering the affected IDs. Any change is then reported to the callback, listing all IDs that have different hashes.

Hashing the resources considers all languages, as well as the meta-information of the resources.

type ContentType

type ContentType byte

ContentType identifies how resource data shall be interpreted.

func (ContentType) String added in v1.2.0

func (t ContentType) String() string

String returns the textual representation of the type.

type Filter

type Filter interface {
	Filter(lang Language, id ID) List
}

Filter filters for language and id to produce a list of matching resources.

type ID

type ID uint16

ID represents an integer key of resources.

func (ID) Plus

func (id ID) Plus(offset int) ID

Plus adds the given offset and returns the resulting ID.

func (ID) String

func (id ID) String() string

String returns the textual representation.

func (ID) Value

func (id ID) Value() uint16

Value returns the numerical value of the identifier.

type IDMarkerMap

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

IDMarkerMap is used to collect IDs.

func (*IDMarkerMap) Add

func (marker *IDMarkerMap) Add(id ID)

Add adds the given ID to the map.

func (IDMarkerMap) ToList

func (marker IDMarkerMap) ToList() []ID

ToList converts the map to a de-duplicated list.

type Key

type Key struct {
	// ID identifies the resource.
	ID ID
	// Lang localizes the resource, should the actual content be localized.
	Lang Language
	// Index specifies the exact entry within a resource.
	Index int
}

Key identifies a particular resource out of a combination of a resource ID, a language, and a block index.

Keys are used for retrieving particular content that is stored in resources. Not all resources contain only one atomic entity.

func KeyOf

func KeyOf(id ID, lang Language, index int) Key

KeyOf is a factory function for creating a Key instance. It is done to avoid getting warnings when trying to do a similar construction via Key{id, lang, index}.

type Language

type Language byte

Language defines the human language of a resource.

const (
	// LangAny identifies language agnostic resources.
	LangAny Language = 0xFF
	// LangDefault identifies the default language, typically English - unless modded.
	LangDefault Language = 0
	// LangFrench identifies the French language.
	LangFrench Language = 1
	// LangGerman identifies the German language.
	LangGerman Language = 2

	// LanguageCount specifies how many languages are supported.
	LanguageCount = 3
)

func Languages

func Languages() []Language

Languages returns a slice of all human languages. Does not include "Any" selector.

func (Language) Includes

func (lang Language) Includes(other Language) bool

Includes returns true if the language includes the provided one. This is not symmetrical. While "Any" includes "German", "German" does not include "Any".

func (Language) String

func (lang Language) String() string

type List

type List []View

List is a helper type for slices of resources. Although the length of the list is immutable, the content and the fields are not. Meaning, modifying the cells of one list may affect the cells of others.

func (List) Joined

func (list List) Joined(other List) List

Joined returns a new list which is the combination of this list and the provided one.

func (List) With

func (list List) With(res View) List

With returns a new list with the provided resource appended at the end.

type LocalizedResources

type LocalizedResources struct {
	// ID is the identifier of the viewer. This could be a filename for instance.
	ID string
	// Language specifies for which language the viewer has resources.
	Language Language
	// Viewer is the actual container of the resources.
	Viewer Viewer
}

LocalizedResources associates a language with a resource viewer under a specific identifier.

type LocalizedResourcesList

type LocalizedResourcesList []LocalizedResources

LocalizedResourcesList is a collection of localized resources. it exists to provide a typical implementation of a Selector.

func (LocalizedResourcesList) Filter

func (list LocalizedResourcesList) Filter(lang Language, id ID) List

Filter returns all resources that match the given parameters.

type Localizer

type Localizer interface {
	LocalizedResources(lang Language) Selector
}

Localizer provides selectors to resources for specific languages.

type ModificationCallback

type ModificationCallback func(modifiedIDs []ID, failedIDs []ID)

ModificationCallback is a callback function to notify a change in resources.

type NotFoundError added in v1.8.0

type NotFoundError struct {
	ID ID
}

NotFoundError indicates the unknown ID.

func (NotFoundError) Error added in v1.8.0

func (err NotFoundError) Error() string

Error implements the error interface.

type Properties added in v1.4.0

type Properties struct {
	// Compound tells whether the resource should be serialized with a directory.
	// Compound resources can have zero, one, or more blocks.
	// Simple resources always have exactly one block.
	Compound bool

	// ContentType describes how the block data shall be interpreted.
	ContentType ContentType

	// Compressed tells whether the data shall be serialized in compressed form.
	Compressed bool
}

Properties describe the meta information about a resource.

type Resource

type Resource struct {
	Properties
	Blocks
}

Resource provides meta information as well as access to its contained blocks.

func (Resource) Compound

func (res Resource) Compound() bool

Compound tells whether the resource should be serialized with a directory. Compound resources can have zero, one, or more blocks. Simple resources always have exactly one block.

func (Resource) Compressed

func (res Resource) Compressed() bool

Compressed tells whether the data shall be serialized in compressed form.

func (Resource) ContentType

func (res Resource) ContentType() ContentType

ContentType describes how the block data shall be interpreted.

type Selector

type Selector struct {
	// Lang specifies the language to filter by.
	Lang Language

	// From specifies from where the resources shall be taken.
	From Filter

	// As defines how the found resources should be viewed in case more than one matches.
	// By default, the last resource will be used.
	As ViewStrategy
}

Selector provides a merged view of resources according to a language.

func (Selector) Select

func (merger Selector) Select(id ID) (view View, err error)

Select provides a collected view on one resource.

type Store

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

Store holds a set of resources. This set can be modified.

func (*Store) Del

func (store *Store) Del(id ID) bool

Del removes the resource with given identifier from the store.

func (Store) IDs

func (store Store) IDs() []ID

IDs returns a list of available IDs this store currently contains.

func (*Store) Put

func (store *Store) Put(id ID, view View) error

Put (re-)assigns an identifier with data. If no resource with given ID exists, then it is created. Existing resources are overwritten with the provided data.

func (Store) Resource

func (store Store) Resource(id ID) (*Resource, error)

Resource returns reference to the contained resource for given identifier.

func (Store) View added in v1.4.0

func (store Store) View(id ID) (View, error)

View returns a read-only view on the resource for given identifier.

type View

type View interface {

	// Compound tells whether the resource should be serialized with a directory.
	// Compound resources can have zero, one, or more blocks.
	// Simple resources always have exactly one block.
	Compound() bool

	// ContentType describes how the block data shall be interpreted.
	ContentType() ContentType

	// Compressed tells whether the data shall be serialized in compressed form.
	Compressed() bool

	// BlockCount returns the number of available blocks in the resource.
	// Simple resources will always have exactly one block.
	BlockCount() int

	// Block returns the reader for the identified block.
	// Each call returns a new reader instance.
	// Data provided by this reader is always uncompressed.
	Block(index int) (io.Reader, error)
}

View is a read-only view on a selected resource.

type ViewStrategy

type ViewStrategy interface {
	// IsCompoundList returns true for compound resources where each contained block is a separate entity.
	// Separate entities are those that can be replaced without affecting others.
	// Examples are the small game textures and the list of object names.
	IsCompoundList(id ID) bool
}

ViewStrategy defines how selected resources shall be viewed.

type Viewer added in v1.4.0

type Viewer interface {
	// IDs returns a list of available IDs this viewer can provide.
	IDs() []ID

	// View returns a read-only view to a resource for the given identifier.
	View(id ID) (View, error)
}

Viewer provides resources from some storage.

type WrongTypeError added in v1.8.0

type WrongTypeError struct {
	Key      Key
	Expected ContentType
}

WrongTypeError indicates a resource of unexpected type.

func (WrongTypeError) Error added in v1.8.0

func (err WrongTypeError) Error() string

Error implements the error interface.

Directories

Path Synopsis
Package lgres implements serialization of resources using the DOS format.
Package lgres implements serialization of resources using the DOS format.
internal/compression
Package compression implements the LG resource file compression algorithm.
Package compression implements the LG resource file compression algorithm.

Jump to

Keyboard shortcuts

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