metaforce/DataSpec/AssetNameMap.cpp

76 lines
2.1 KiB
C++
Raw Normal View History

2017-07-13 11:39:52 +00:00
#include "AssetNameMap.hpp"
#include "athena/MemoryReader.hpp"
2018-01-01 10:15:26 +00:00
extern "C" const uint8_t ASSET_NAME_MP32[];
extern "C" const size_t ASSET_NAME_MP32_SZ;
extern "C" const uint8_t ASSET_NAME_MP64[];
extern "C" const size_t ASSET_NAME_MP64_SZ;
2017-07-13 11:39:52 +00:00
2018-12-08 05:30:43 +00:00
namespace DataSpec::AssetNameMap {
2017-07-13 11:39:52 +00:00
logvisor::Module Log("AssetNameMap");
2018-12-08 05:30:43 +00:00
struct SAsset {
std::string name;
std::string directory;
hecl::FourCC type;
SAsset() = default;
SAsset(const hecl::FourCC& typeIn, athena::io::IStreamReader& in) : type(typeIn) {
uint32_t nameLen = in.readUint32Big();
name = in.readString(nameLen);
uint32_t dirLen = in.readUint32Big();
directory = in.readString(dirLen);
}
2017-07-13 11:39:52 +00:00
};
static std::unordered_map<uint64_t, SAsset> g_AssetNameMap;
static bool g_AssetNameMapInit = false;
2018-12-08 05:30:43 +00:00
void LoadAssetMap(athena::io::MemoryReader& ar) {
if (!ar.hasError()) {
hecl::FourCC magic;
if (ar.length() >= 4)
ar.readBytesToBuf(&magic, 4);
if (magic != FOURCC('AIDM'))
Log.report(logvisor::Warning,
_SYS_STR("Unable to load asset map; Assets will not have proper filenames for most files."));
else {
uint32_t assetCount = ar.readUint32Big();
g_AssetNameMap.reserve(assetCount);
for (uint32_t i = 0; i < assetCount; ++i) {
hecl::FourCC type;
ar.readBytesToBuf(&type, 4);
uint64_t id = ar.readUint64Big();
g_AssetNameMap[id] = SAsset(type, ar);
}
2017-07-13 11:39:52 +00:00
}
2018-12-08 05:30:43 +00:00
}
2017-07-13 11:39:52 +00:00
}
2018-12-08 05:30:43 +00:00
void InitAssetNameMap() {
if (g_AssetNameMapInit)
return;
2017-07-13 11:39:52 +00:00
2018-12-08 05:30:43 +00:00
Log.report(logvisor::Info, "Initializing asset name database...");
2017-07-23 09:21:18 +00:00
2018-12-08 05:30:43 +00:00
/* First load the 32bit map for MP1/2 */
{
athena::io::MemoryReader ar(ASSET_NAME_MP32, ASSET_NAME_MP32_SZ);
LoadAssetMap(ar);
}
/* Now load the 64bit map for MP3 */
{
athena::io::MemoryReader ar(ASSET_NAME_MP64, ASSET_NAME_MP64_SZ);
LoadAssetMap(ar);
}
g_AssetNameMapInit = true;
2017-07-13 11:39:52 +00:00
}
2018-12-08 05:30:43 +00:00
const std::string* TranslateIdToName(const UniqueID32& id) {
if (g_AssetNameMap.find(id.toUint64()) == g_AssetNameMap.end())
return nullptr;
2017-07-13 11:39:52 +00:00
2018-12-08 05:30:43 +00:00
return &g_AssetNameMap[id.toUint64()].name;
2017-07-13 11:39:52 +00:00
}
2018-12-08 05:30:43 +00:00
} // namespace DataSpec::AssetNameMap