how can i deserialize entt meta correctly? [code]
- User
how can i deserialize entt meta correctly?
void AssetManager::RegisterAssetsMetadata() { entt::meta_factory<BaseAsset>() .type(entt::type_hash<BaseAsset>::value(), "Asset") .data<&BaseAsset::guid>("GUID"_hs, "GUID") .data<&BaseAsset::lastModified>("lastModified"_hs, "lastModified") .data<&BaseAsset::sourceFilePath>("sourceFilePath"_hs, "sourceFilePath"); entt::meta_factory<TextureAsset>() .type(entt::type_hash<TextureAsset>::value(), "Texture Asset") .base<BaseAsset>() // register base class .data<&TextureAsset::compressionFormats>("compressionFormats"_hs, "compressionFormats") .data<&TextureAsset::mipmapsLevel>("mipmapsLevel"_hs, "mipmapsLevel") .data<&TextureAsset::filterMode>("filterMode"_hs, "filterMode") .data<&TextureAsset::wrapMode>("wrapMode"_hs, "wrapMode"); } void AssetManager::SerializeAssetsMetadata(std::unique_ptr<BaseAsset> const& _asset, AssetType _type) { std::string filename = _asset->sourceFilePath.string() + m_AssetMetadataExt; std::ofstream metafile(filename); if (!metafile.is_open()) { return; } // wrap data in meta_any entt::meta_any assetmetadata{ *_asset }; for (auto&& [id, data] : assetmetadata.type().data()) { auto typeID = data.type().id(); if (entt::resolve<xresource::full_guid>().id() == typeID) { if (xresource::full_guid* ptr = data.get(assetmetadata).try_cast<xresource::full_guid>()) { metafile << data.name() << " " << "Type="; metafile << ptr->m_Type.m_Value << '\n'; metafile << data.name() << " " << "Instance="; metafile << ptr->m_Instance.m_Value << '\n'; } } else if (entt::resolve<fs::file_time_type>().id() == typeID) { if (fs::file_time_type* ptr = data.get(assetmetadata).try_cast<fs::file_time_type>()) { metafile << data.name() << "="; metafile << *ptr << '\n'; } } else if (entt::resolve<fs::path>().id() == typeID) { if (data.get(assetmetadata).try_cast<fs::path>()) { metafile << data.name() << "="; fs::path tmp = *data.get(assetmetadata).try_cast<fs::path>(); metafile << tmp.string() << '\n'; } } } switch (_type) { case TEXTURE: assetmetadata = dynamic_cast<TextureAsset&>(*_asset); break; }; for (auto&& [id, data] : assetmetadata.type().data()) { auto typeID = data.type().id(); if (entt::resolve<int>().id() == typeID) { if (int* ptr = data.get(assetmetadata).try_cast<int>()) { metafile << data.name() << "="; metafile << *ptr << '\n'; } } else if (entt::resolve<TextureAsset::TextureCompression>().id() == typeID) { if (TextureAsset::TextureCompression* ptr = data.get(assetmetadata).try_cast<TextureAsset::TextureCompression>()) { metafile << data.name() << "="; metafile << EnumToString(*ptr) << '\n'; } } else if (entt::resolve<TextureAsset::TextureFilter>().id() == typeID) { if (TextureAsset::TextureFilter* ptr = data.get(assetmetadata).try_cast<TextureAsset::TextureFilter>()) { metafile << data.name() << "="; metafile << EnumToString(*ptr) << '\n'; } } else if (entt::resolve<TextureAsset::TextureWrap>().id() == typeID) { if (TextureAsset::TextureWrap* ptr = data.get(assetmetadata).try_cast<TextureAsset::TextureWrap>()) { metafile << data.name() << "="; metafile << EnumToString(*ptr) << '\n'; } } } } AssetType AssetManager::DeserializeAssetsMetadata(std::filesystem::path _path, std::unique_ptr<BaseAsset> const& _asset) { // read .meta file std::ifstream metafile(_path); if (!metafile.is_open()) { return UNKNOWN; } entt::meta_type assetType = entt::resolve(entt::hashed_string{ "Asset" }); for (std::string line; std::getline(metafile, line, '='); ) { entt::meta_type type = entt::resolve(entt::hashed_string{ line.c_str() }); // to refactor .. same as inspector if (entt::resolve<xresource::full_guid>().name() == line) { xresource::type_guid type; xresource::instance_guid instance; std::getline(metafile, line, '='); std::getline(metafile, line, '='); std::istringstream iss{ line }; iss >> type.m_Value; std::getline(metafile, line, '='); std::getline(metafile, line, '='); iss.clear(); iss.str(line); iss >> instance.m_Value; _asset->guid = { instance,type }; } if (entt::resolve<fs::file_time_type>().name() == line) { std::getline(metafile, line, '='); std::istringstream iss{ line }; std::cout << line << std::endl; std::chrono::system_clock::time_point sys_tp; iss >> std::chrono::parse("%F %T", sys_tp); if (iss.fail()) { std::cerr << "Failed to parse time string." << std::endl; } _asset->lastModified = std::chrono::clock_cast<std::chrono::file_clock>(sys_tp); } std::cout << line << std::endl; } std::cout << _asset->lastModified << std::endl; if (TextureAsset::typeGUID.m_Value == _asset->guid.m_Type.m_Value) { return TEXTURE; } return UNKNOWN; } Assistant
Looking at your code, I see several issues with the deserialization approach. The main problems are:
- You're trying to resolve meta types using string names, but you registered them with hashed strings
- The file parsing logic is inconsistent and doesn't properly handle the key-value format
- You're not using the meta system to actually set the values on the object
Here's a corrected version of your
DeserializeAssetsMetadatafunction:AssetType AssetManager::DeserializeAssetsMetadata(std::filesystem::path _path, std::unique_ptr<BaseAsset>& _asset) { std::ifstream metafile(_path); if (!metafile.is_open()) { return UNKNOWN; } // First, determine the asset type to create the correct derived class AssetType assetType = UNKNOWN; // Parse file to determine type first (you might want to store this as first line) std::string line; std::map<std::string, std::string> metadata; // Read all key-value pairs into a map while (std::getline(metafile, line)) { size_t pos = line.find('='); if (pos != std::string::npos) { std::string key = line.substr(0, pos); std::string value = line.substr(pos + 1); metadata[key] = value; } } // Check GUID Type to determine asset type if (metadata.find("GUID Type") != metadata.end()) { xresource::type_guid typeGuid; std::istringstream(metadata["GUID Type"]) >> typeGuid.m_Value; if (TextureAsset::typeGUID.m_Value == typeGuid.m_Value) { _asset = std::make_unique<TextureAsset>(); assetType = TEXTURE; } // Add other asset types here } if (!_asset) { _asset = std::make_unique<BaseAsset>(); } // Now deserialize using meta system entt::meta_any assetMeta{ *_asset }; // Deserialize base class properties auto baseType = entt::resolve<BaseAsset>(); deserializeMetaData(baseType, assetMeta, metadata); // Deserialize derived class properties if applicable if (assetType == TEXTURE) { entt::meta_any textureAssetMeta{ dynamic_cast<TextureAsset&>(*_asset) }; auto textureType = entt::resolve<TextureAsset>(); deserializeMetaData(textureType, textureAssetMeta, metadata); } return assetType; } // Helper function to deserialize meta data void AssetManager::deserializeMetaData(entt::meta_type type, entt::meta_any& object, const std::map<std::string, std::string>& metadata) { for (auto&& [id, data] : type.data()) { std::string dataName(data.name()); // Handle GUID specially since it's stored as two fields if (dataName == "GUID") { if (metadata.find("GUID Type") != metadata.end() && metadata.find("GUID Instance") != metadata.end()) { xresource::full_guid guid; std::istringstream(metadata.at("GUID Type")) >> guid.m_Type.m_Value; std::istringstream(metadata.at("GUID Instance")) >> guid.m_Instance.m_Value; data.set(object, guid); } } else if (metadata.find(dataName) != metadata.end()) { const std::string& value = metadata.at(dataName); auto dataType = data.type(); if (dataType == entt::resolve<int>()) { int intValue; std::istringstream(value) >> intValue; data.set(object, intValue); } else if (dataType == entt::resolve<fs::path>()) { data.set(object, fs::path(value)); } else if (dataType == entt::resolve<fs::file_time_type>()) { std::chrono::system_clock::time_point sys_tp; std::istringstream iss(value); iss >> std::chrono::parse("%F %T", sys_tp); if (!iss.fail()) { auto file_tp = std::chrono::clock_cast<std::chrono::file_clock>(sys_tp); data.set(object, file_tp); } } else if (dataType == entt::resolve<TextureAsset::TextureCompression>()) { auto enumValue = StringToEnum<TextureAsset::TextureCompression>(value); data.set(object, enumValue); } else if (dataType == entt::resolve<TextureAsset::TextureFilter>()) { auto enumValue = StringToEnum<TextureAsset::TextureFilter>(value); data.set(object, enumValue); } else if (dataType == entt::resolve<TextureAsset::TextureWrap>()) { auto enumValue = StringToEnum<TextureAsset::TextureWrap>(value); data.set(object, enumValue); } } } }Key improvements:
- Proper file parsing: Read all key-value pairs into a map first
- Type determination: Check the GUID type to create the correct derived class instance
- Use meta system for setting values: Use
data.set()to properly set values through the meta system - Separate base and derived deserialization: Handle inheritance properly
- Helper function: Extract the deserialization logic into a reusable helper
You'll also need to register your enum types with the meta system:
// Add this to RegisterAssetsMetadata() entt::meta<TextureAsset::TextureCompression>() .type(entt::type_hash<TextureAsset::TextureCompression>::value()); entt::meta<TextureAsset::TextureFilter>() .type(entt::type_hash<TextureAsset::TextureFilter>::value()); entt::meta<TextureAsset::TextureWrap>() .type(entt::type_hash<TextureAsset::TextureWrap>::value());This approach properly leverages EnTT's meta system for deserialization and handles the inheritance hierarchy correctly.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.