A file system.

Call init to generate data files and get an instance of this class.

When the target is javascript or has threads, and the file system isn't in embed mode, make sure you load the resources before trying to access them because resources will load asynchronously.

If you don't want to micromanage, use the filesystem in embed mode by using the init function, or load every resources from the start by calling loadDataFiles with no arguments then unload the data files used to load the resource by calling unloadDataFiles.

Hot-reloading will automatically load files that it needs to load but won't automatically unload them.

Available since

0.1.0

.

Static methods

staticinit(dataPath:Dynamic, maxSizePerFile:Dynamic, ?convInsts:Dynamic, resPath:Dynamic, ?buildPath:Dynamic, ?resPathsToSkip:Dynamic):Dynamic

Builds the data files then returns a new nb.fs.NFileSystem instance all set up.

Set dataPath to "EMBED" to enable embed mode.

Parameters:

dataPath

Data files directory path, relative to the executable.

resPath

Resources directory path, relative to the project's directory.

buildPath

Executable path relative to the project's directory.

resPathsToSkip

Paths relative to the project's resource directory that will be ignored when looking for new or updated resources.

maxSizePerFile

Maximum size of a data file. A number <= 0 means there's no limit.

statics_convert(path:String, bytes:Bytes, convDefs:Array<ConvDefinition>):Bytes

Converts a file data to another type if it has a converter for it.

Parameters:

path

The path of the resource, relative to the project's resource folder.

bytes

The resource's data as bytes.

convDefs

An array of nb.fs.NFileSystem.ConvDefinition defining the converters that could be used.

Returns:

The possibly converted bytes.

Constructor

new(resPath:String, ?resPathsToSkip:Array<String>)

Creates a new NFileSystem instance.

Could be useful if you are trying to use multiple file systems, otherwise you shouldn't be calling it and you're propably looking for the init function.

Also, this file system wasn't made with other file systems in mind at all.

Parameters:

resPath

Resources directory path, relative to the project's directory.

resPathsToSkip

Paths relative to the project's resource directory that will be ignored when looking for new or updated resources.

Variables

@:value(true)autoLoadFiles:Bool = true

If true, files will be loaded automatically in memory when their data are being accessed.

@:value(true)autoWatchFiles:Bool = true

Adds files to watchedFiles automatically when they are loaded.

@:value("")read onlybuildPath:String = ""

Executable path relative to the project's directory.

@:value(null)convInsts:Array<ConvDefinition> = null

Contains instructions about which file type should be converted.

@:value("")read onlydataPath:String = ""

Data files directory path, relative to the executable.

@:value(false)read onlyembedMode:Bool = false

If true, the file system assumes the resources data are embedded in the executable.

@:value([])read onlyfileEntries:Array<NFileEntry> = []

Contains all created NFileEntrys.

@:value(new MapArray())read onlyfreeAddresses:MapArray<Array<Float>> = new MapArray()

Contains freed addresses. A free Address refers to data in the data file that is allowed to be overwritten.

@:value([])read onlyloadedDataFiles:Array<DataFile> = []

Contains all loaded data files.

@:value(-1)maxSizePerFile:Int = -1

Maximum size of a data file. A number <= 0 means there's no limit.

read onlyresInfos:ResInfos

Contains all the Addresses and DataFileInfos.

@:value("")read onlyresPath:String = ""

Resources directory path, relative to the project's directory.

@:value([])resPathsToSkip:Array<String> = []

Paths relative to the project's resource directory that will be ignored when looking for new or updated resources.

@:value("")read onlyrootPath:String = ""

String added at the beginning of requests sent to the server. The true server's root will always be at the project's directory path.

@:value([])read onlywatchedFiles:Array<NFileEntry> = []

Files to check for hot-reloading.

Methods

addFileToWatch(entry:NFileEntry):Void

Watches a file for any change.

By default, this is done automatically when a resource gets loaded.

checkWatchedFiles():Void

Hot-reloads resources.

Checks for any file that was modified recently by looking at their metadata and updates their associated data file and Address.

This is done asynchronously if the target is javascript or supports threads.

convert(path:String, bytes:Bytes):Bytes

Converts a file data to another type.

It checks if there's any instruction in convInsts that says if the data should be converted.

Parameters:

path

The path of the resource, relative to the project's resource folder.

bytes

The resource data as bytes.

Returns:

bytes converted.

dir(path:String):Array<FileEntry>

Returns a list of entries that are in a directory.

Parameters:

path

A path relative to the project's resource directory to deduce the directory.

Returns:

Entries with their associated resources being in path's directory.

dispose():Void

Does nothing yet.

exists(resPath:String):Bool

Returns true if an Address is associated to the path given, false otherwise.

get(resPath:String):FileEntry

Returns an entry associated to a given resource path. If it doesn't have its entry cached and its a valid resource path, it creates the entry.

Throws an error if it's not a valid resource path. (It doesn't have an associated Address.)

Parameters:

resPath

A resource path, relative to the resource directory.

Returns:

The associated nb.fs.NFileSystem.NFileEntry downcasted to hxd.fs.FileEntry.

getRoot():FileEntry

Returns the resource directory's associated nb.fs.NFileSystem.NFileEntry downcasted to hxd.fs.FileEntry.

@:value({ loadResources : true })loadDataFiles(?paths:Array<String>, loadResources:Bool = true, ?onLoaded:DataFile ‑> Void, ?onAllLoaded:Array<DataFile> ‑> Void):Array<Progress>

Loads data files.

This is done asynchronously if the target is javascript or supports threads.

Parameters:

paths

Paths of data files that are to be loaded, relative to the executable.

loadResources

If true, when a data file finishes loading, loads all its associated resources.

onLoaded

Called whenever a data file finishes loading.

onAllLoaded

Called when all the data files finished loading.

Returns:

An nb.fs.NFileSystem.Progress for each data file to check the loading progresses.

loadResFromDataFile(dfRelPath:String, ?onAllLoaded:() ‑> Void):Progress

Loads all resources associated with a datafile.

Parameters:

dfRelPath

Path of a data file, relative to the executable.

onAllLoaded

Called when all data files finishes loading.

Returns:

An nb.fs.NFileSystem.Progress to check the loading progress.

loadResFromPaths(relPaths:Array<String>, ?onLoaded:NFileEntry ‑> Void, ?onAllLoaded:Array<NFileEntry> ‑> Void):Progress

Loads resources from given paths.

Loads their associated data file if they are not loaded.

Parameters:

relPaths

Paths of resources to load.

onLoaded

Called when a resource finishes loading.

onAllLoaded

Called when all resources finished loading.

Returns:

An nb.fs.NFileSystem.Progress to check the loading progress.

inlineunloadDataFile(path:String):Bool

Unloads a data file.

Keep in mind that as long as the datafile is referenced somewhere, it's still in memory. This function only tells the file system to stop referencing it so that it gets garbage collected.

Parameters:

path

Path of the data file, relative to the executable.

Returns:

true if a loaded data file was just unloaded, false otherwise.

update(dt:Float):Void

The function called every frame by nb.Manager.

Parameters:

dt

Elapsed time in seconds.