Resource system: Difference between revisions

From ZeldaMods (Breath of the Wild)
Jump to navigation Jump to search
imported>Leoetlino
(Created page with "In ''Breath of the Wild'', the '''resource system''' is a subsystem that is responsible for managing resources, i.e. content files. It is implemented by ResourceMgrTask, [...")
(No difference)

Revision as of 10:09, 8 September 2018

In Breath of the Wild, the resource system is a subsystem that is responsible for managing resources, i.e. content files. It is implemented by ResourceMgrTask, ResourceSystem and a few ancillary classes (such as "resource binders" and "handles").

Concepts

Resource

Resources (in the context of the codebase) are C++ classes that are responsible for parsing raw data from content files and storing them in a more convenient data structure.

In general, there is one resource class per file type, not format. As an example, bgparamlist and bxml do not share the same class even though both are AAMP files.

In some cases, the resource is nothing more than a thin wrapper over the underlying file bytes. This is the case for BYMLs and other simple resource types that don't require any additional memory allocation. Such a wrapper is (unofficially) called ResourceBase.

Resource factory

Factories are C++ classes that create resource objects.

During application init, factory instances are created and registered with the resource system. When a resource is loaded, the system will look up the corresponding factory based on the file extension, load the entire file into memory, and pass the data to the factory, which then returns a Resource object on which parse() must be called.

List of resource factories

A complete list of resource factories can be found in the rstb project.

These values were extracted from the Switch and Wii U 1.5.0 executables.

Note that:

  • For ResourceLoadArg3 (used in model/bfres related code), the factory is hardcoded to be the ResourceBase factory.
  • For ResourceLoadArg2 (used for actor resources and physics stuff) and ResourceLoadArg (everything else), the factory is determined from the file extension.
  • Any file for which there isn't any specific factory will use the ResourceBase factory. 

Special cases

Compressed files

Compressed files can be automatically decompressed and loaded if their extension starts with an 's'. The 's' prefix indicates that a file is Yaz0 compressed.

When a resource load is requested, the resource system will in most cases automatically prepend 's' to the extension and use sead::ResourceMgr::tryLoadWithDecomp to try loading a compressed version first. This means that any code that interacts with the resource loading functions must not include the prefix in resource paths.

Exceptions: bfevfl, bcamanim and barslist are always loaded uncompressed (see 0x710120A9F4 in Switch 1.5.0).

Archives

The resource system can be configured to try loading from an SARC archive first.

To load from an archive, the global resource pack pointer (res::ResourceMgrTask::sInstance->packRes @ this+0x9c06f0 in Switch 1.5.0) must be set to an archive resource handle. Files in the archive can then be accessed as if they were at the root of the romfs/content partition. For example, if EventFlow/Test.bfevfl is a file in Pack/TitleBG.pack, the path that should be used for loading is EventFlow/Test.bfevfl, not Pack/TitleBG.pack/EventFlow/Test.bfevfl.

Calls to any of the resource loading functions will automatically check whether the specified resource exists in the archive. If it does exist, it will be loaded from the archive. If the file cannot be found in the archive, the game will ignore the active resource pack and load from the usual file device.

Add-on content

In order to load add-on content, the file device must be set to the AoC file device when calling a resource loading function.

This can be done by calling various aocManager that return the AoC file device if the resource path matches specific AoC path patterns and assigning the result to field 0x48 in the ResourceLoadArg class.

If no file device is explicitly set, the resource system will use the default file device (romfs on Switch or content directory on Wii U).

If the file device is set to the AoC file device, Aoc/0010 will be prepended to the canonical resource path. This is done to avoid conflicts between the base and AoC versions of a resource. Warning: the AoC file device is not always set when loading resources from AoC archives. This is the case for the model and ActorParam code. Conversely, the AoC device is sometimes set even when it is unnecessary.

Memory allocation

Heap size

The size of the resource loading heap the system allocates every time a resource is loaded depends on the value that is listed in the #Resource size table (RSTB). If lookup fails, the game will fall back to the following formula (Switch on 1.5.0):

alignedFileSize = (actualFileSize + 31) & -32;
factory->getResourceSize()
+ factory->constant
+ factory->getLoadDataAlignment()
+ (signed int)(float)(factory->sizeMultiplier * alignedFileSize)
+ (factory->sizeMultiplier * alignedFileSize >= 0.0 &&
   (float)(signed int)(float)(factory->sizeMultiplier * alignedFileSize) != (float)(sizeMultiplier * alignedFileSize))
+ 0x750

Warning: Failure to add entries to the RSTB for a large number of resources may result in system instability. The purpose of this fallback appears to be to allow loading resources during development even without an entry in the resource size table. As such, the system will often allocate way more memory than needed to account for any dynamic allocation that the resource class may do.

Resource size table

The Resource Size Table (RSTB) contains information about game resource file sizes. It is used to determine how much memory should be allocated when loading a resource.

It is currently unknown how Nintendo has determined the values in the RSTB. However by REing the resource system it was found that the resource loading heap is for simple factories only used to allocate the file loading buffer (which is as large as the file to load), the C++ resource class and some extra bytes to ensure data is aligned correctly in memory.

So a formula that should work for modifying the listed size is:

(size rounded up to multiple of 32) + CONSTANT + sizeof(ResourceClass) + PARSE_SIZE

CONSTANT is 0x168 in the Switch version and 0xe4 on Wii U.

PARSE_SIZE is the amount of memory allocated from the resource heap in the Resource::parse function. Determining the exact value of PARSE_SIZE requires reversing that function and tracking calls to operator new() because the total amount of memory is not listed anywhere.