tecs.files

Where a game reads its shipped content from.

nupp.io.files owns every filesystem operation and every writable root: reading, writing, atomic commits, directory streams, temporaries, metadata, and the platform's own configuration, data and cache directories for one named application. This module keeps the one thing Nupp cannot supply, because it is a property of the product rather than of the platform: where this build's content was installed.

const disk = require("nupp.io.files")

disk.setApplicationIdentity("Ex Nihilo", "Starfarer")
const saves = assert(disk.dataPath()):join("saves")
disk.writeAtomic(saves:join("slot1.json"), bytes)

const level = disk.read(tecs.files.assetPath("levels/one.json"))

The asset root#

assetRoot is where shipped content lives and is never a place a build may write. TECS_ASSETS wins, which is how a development run reads straight out of a build tree instead of a staged copy. Then the __tecsContent global, since a host knows the layout it installed and this is not something to sniff for. Then the directory the running executable was loaded from, which is where a single-directory build puts everything. assetPath joins against it without checking existence, because a path that is about to be read is as valid a request as one that is already there.

Writable roots#

A game names itself through nupp.io.files.setApplicationIdentity and then asks nupp.io.files for configurationPath, dataPath or cachePath, each of which is a distinct directory that is created before it is answered. Saves and settings belong under dataPath, and anything the game can rebuild belongs under cachePath, which the operating system may empty between any two runs. tecs.application.newApplication names the identity "tecs", "tecs" when a game named none, so an engine test and an unnamed tool write somewhere stable.

Module contents

Functions

FunctionKindDescription
assetPathfunctionResolves relative against the asset root.
assetRootfunctionReturns the root the engine reads shipped content from.
resetfunctionForgets the cached asset root so the next call resolves again.
setAssetRootfunctionOverrides the asset root, for a test or a tool.

Values

ValueKindDescription
ASSET_ROOT_VARIABLEvariableRead-only.
CONTENT_GLOBALvariableRead-only.

Functions#

assetPathfunction#

function assetPath(relative: string): string

Resolves relative against the asset root.

Arguments

NameTypeDescription
relativestring

a path under the content root, with / separators

Returns

TypeDescription
string

the joined path, without checking that anything is there

Raises

TypeCondition
string

when relative is empty

assetRootfunction#

function assetRoot(): string

Returns the root the engine reads shipped content from.

The root is resolved once and remembered, and reset forgets it. An empty answer is not a failure: it means no override named a root and the platform declined to say where the executable is, and a caller falls back to a path relative to the working directory.

Returns

TypeDescription
string

the root with a trailing separator, or an empty string

resetfunction#

function reset(): nil

Forgets the cached asset root so the next call resolves again.

A test that moves TECS_ASSETS between cases needs this. A game does not: nothing under a running process changes where content was installed. The writable roots are nupp.io.files', and resetApplicationPaths forgets those.

Returns

TypeDescription
nil

setAssetRootfunction#

function setAssetRoot(root: string): nil

Overrides the asset root, for a test or a tool.

Arguments

NameTypeDescription
rootstring

the content directory to read from, with or without a trailing separator

Returns

TypeDescription
nil

Raises

TypeCondition
string

when root is empty

Values#

ASSET_ROOT_VARIABLEvariable#

Read-only. Names the environment variable that overrides the asset root.

CONTENT_GLOBALvariable#

Read-only. Names the global a host sets to the content root it installed.