当前位置: 首页>>代码示例>>C++>>正文


C++ ObjectRef::import方法代码示例

本文整理汇总了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");
    }
}
开发者ID:spriest487,项目名称:dy-engine,代码行数:95,代码来源:AssetLibrary.cpp


注:本文中的ObjectRef::import方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。