Resource system: Difference between revisions

From ZeldaMods (Breath of the Wild)
Jump to navigation Jump to search
imported>Leoetlino
(→‎Memory allocation: parent heap)
imported>Leoetlino
Line 48: Line 48:
If the file device is set to the AoC file device, <code>Aoc/0010/</code> 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.
If the file device is set to the AoC file device, <code>Aoc/0010/</code> 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.


==== AoC with the Aoc/0010 prefix ====
==== Cases where the AoC file device is used ====
{{Empty section}}
If AoC version 0x200 or higher is installed and if the canonical resource name (without the <code>Aoc/0010/</code> prefix) matches any of the below patterns:
* Terrain/A/AocField*
* Pack/DungeonNNN.pack with NNN > 119
* UI/StaffRollDLC/*
* Map/MainField/* (if Pack/AocMainField.pack was loaded successfully)
* Map/MainFieldDungeon/*
* Map/AocField/*
* Map/CDungeon/* (for DungeonNNN with NNN > 119)
* Physics/StaticCompound/AocField/*
* Physics/StaticCompound/MainFieldDungeon/*
* Physics/StaticCompound/CDungeon/* (for DungeonNNN with NNN > 119)
* Movie/Demo6*
* Game/AocField/*
* NavMesh/AocField/*
* NavMesh/MainFieldDungeon/*
* NavMesh/CDungeon/* (for DungeonNNN with NNN > 119)
* Physics/TeraMeshRigidBody/AocField/*
* Voice/{language}/Stream_Demo600_0/Demo600_0_000.bfstm


== Memory allocation ==
== Memory allocation ==

Revision as of 14:43, 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 composed of 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.

Cases where the AoC file device is used

If AoC version 0x200 or higher is installed and if the canonical resource name (without the Aoc/0010/ prefix) matches any of the below patterns:

  • Terrain/A/AocField*
  • Pack/DungeonNNN.pack with NNN > 119
  • UI/StaffRollDLC/*
  • Map/MainField/* (if Pack/AocMainField.pack was loaded successfully)
  • Map/MainFieldDungeon/*
  • Map/AocField/*
  • Map/CDungeon/* (for DungeonNNN with NNN > 119)
  • Physics/StaticCompound/AocField/*
  • Physics/StaticCompound/MainFieldDungeon/*
  • Physics/StaticCompound/CDungeon/* (for DungeonNNN with NNN > 119)
  • Movie/Demo6*
  • Game/AocField/*
  • NavMesh/AocField/*
  • NavMesh/MainFieldDungeon/*
  • NavMesh/CDungeon/* (for DungeonNNN with NNN > 119)
  • Physics/TeraMeshRigidBody/AocField/*
  • Voice/{language}/Stream_Demo600_0/Demo600_0_000.bfstm

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.

Parent heap

If the heap size is smaller than 0x80000 bytes, the heap is created with the "arena for resource S" (small) heap as parent.

Otherwise, the parent heap is the "arena for resource L" (large).

Resource size table

The Resource Size Table (RSTB) contains a list of canonical resource names and their sizes. It is loaded from ResourceSizeTable.product.rsizetable and 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 most factories, only used to allocate the file loading buffer (which is as large as the file to load), the C++ resource class, extra allocations depending on the resource class and some extra bytes to ensure data is aligned correctly in memory.

Therefore, the following formula should give a correct size value for all resource types:

(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 amount of dynamically allocated memory depends on the resource.