本文整理汇总了C++中ObjectRef::import方法的典型用法代码示例。如果您正苦于以下问题:C++ ObjectRef::import方法的具体用法?C++ ObjectRef::import怎么用?C++ ObjectRef::import使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ObjectRef
的用法示例。
在下文中一共展示了ObjectRef::import方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: loadPackage
void AssetLibrary::loadPackage(string name) {
ifstream file(name);
if (!file) {
throw IOException("file could not be opened");
}
auto baseName = path(name).stem();
Value package;
Json::Reader jsonReader;
if (!jsonReader.parse(file, package)) {
throw IOException("package file could not be read: " + jsonReader.getFormattedErrorMessages());
}
string prefix(baseName.string() + ".");
if (package.type() == ValueType::arrayValue) {
struct AssetInPackage {
string uid;
string objectName;
Value* objectVal;
};
vector<AssetInPackage> assetsInPackage;
assetsInPackage.reserve(package.size());
for (auto&& asset : package) {
if (asset.type() != ValueType::objectValue) {
throw IOException("items in objects array must be objects");
}
auto uid = asset.removeMember("uid");
if (uid.type() != ValueType::stringValue) {
throw IOException("resource uid must be a string");
}
auto& objectType = asset["objectName"];
if (objectType.type() != ValueType::stringValue) {
throw IOException("resource has missing or invalid type");
}
//normalize
auto uidStr = Object::uidToString(Object::uidFromString(uid.asString()));
assetsInPackage.emplace_back(AssetInPackage {
uidStr,
objectType.asString(),
&asset
});
}
JsonSerializer serializer;
/* simple ref resolver: each new item is mapped by uid, then subsequent items
can look it up. the main limitation is the objects must appear after all of their
dependencies within a file */
serializer.setRefResolver([this](Object::Uid uid, ObjectRefResolver resolve) {
auto uidStr = Object::uidToString(uid);
auto&& lookupNewObj = impl->assets.find(uidStr);
if (lookupNewObj == impl->assets.end()) {
logger.warnStream() << "failed to resolve reference for object " << uidStr;
resolve(nullptr);
}
else {
resolve(lookupNewObj->second);
}
});
for (auto&& asset : assetsInPackage) {
Value propertiesVal = *asset.objectVal;
propertiesVal.removeMember("type");
ObjectRef<> obj;
ObjectRef<AssetImporter> importer;
if (impl->tryGetImporter(asset.objectName, importer)) {
auto importerMeta = Metadata::createMetaObject(*importer);
serializer.deserializeProperties(propertiesVal, *importerMeta);
obj = importer->import();
}
else {
obj = impl->application.instantiate(asset.objectName);
auto meta = Metadata::createMetaObject(asset.objectName, *obj);
serializer.deserializeProperties(propertiesVal, *meta);
}
impl->assets[asset.uid] = obj;
}
}
else if (package.type() != ValueType::nullValue) {
throw IOException("package data must be an array");
}
}