void CComponentInstanceManager::LoadFile(uint32_t uFileId, std::vector<CComponentBase*>* pLoadComponents)
{
    uint32_t uFileStartPos = 0;
    uint32_t uFileDataLength = 0;
    bool bRet = m_pProject->QueryFileLayoutInfo(uFileId, uFileStartPos, uFileDataLength);
    BEATS_ASSERT(bRet, _T("Query file layout info failed! file id %d"), uFileId);
    if (bRet)
    {
        // Use a serializer proxy to avoid multi-thread load the same serializer
        CSerializer dataProxy;
        dataProxy.SetBuffer(m_pSerializer->GetBuffer(), m_pSerializer->GetWritePos(), false);
        std::vector<CComponentInstance*> loadComponents;
        dataProxy.SetReadPos(uFileStartPos);
        uint32_t uFileStartPosRead, uFileDataLengthRead;
        dataProxy >> uFileStartPosRead >> uFileDataLengthRead;
        BEATS_ASSERT(uFileStartPosRead == uFileStartPos && uFileDataLengthRead == uFileDataLength);
        uint32_t uComponentCount = 0;
        dataProxy >> uComponentCount;
        bool bOriginalLoadPhase = IsInLoadingPhase();
        BEATS_ASSERT(!bOriginalLoadPhase, "I don't think it will nest!");
        SetLoadPhaseFlag(true);
        for (uint32_t j = 0; j < uComponentCount; ++j)
        {
            uint32_t uComponentDataSize, uGuid, uId;
            uint32_t uComponentStartPos = dataProxy.GetReadPos();
            dataProxy >> uComponentDataSize >> uGuid >> uId;
            CComponentInstance* pComponent = down_cast<CComponentInstance*>(CComponentInstanceManager::GetInstance()->CreateComponent(uGuid, false, false, uId, true, &dataProxy, false));
            pComponent->SetDataPos(uComponentStartPos);
            pComponent->SetDataSize(uComponentDataSize);
            BEATS_ASSERT(pComponent != NULL);
            if (pLoadComponents != nullptr)
            {
                pLoadComponents->push_back(pComponent);
            }
            loadComponents.push_back(pComponent);
            BEATS_ASSERT(uComponentStartPos + uComponentDataSize == dataProxy.GetReadPos(), _T("Component Data Not Match!\nGot an error when import data for component %x %s instance id %d\nRequired size: %d, Actual size: %d"), uGuid, pComponent->GetClassStr(), uId, uComponentDataSize, dataProxy.GetReadPos() - uComponentStartPos);
            dataProxy.SetReadPos(uComponentStartPos + uComponentDataSize);
        }
        BEATS_ASSERT(dataProxy.GetReadPos() - uFileStartPos == uFileDataLength, _T("File Data NOT Match!\nGot an error when import data for file %d Required size:%d Actual size %d"), uFileId, uFileDataLength, dataProxy.GetReadPos() - uFileStartPos);
        dataProxy.SetBuffer(nullptr, 0, false);
        ResolveDependency();
        SetLoadPhaseFlag(bOriginalLoadPhase);
        // After all component instance's value is read, we call each's load function.
        for (size_t i = 0; i < loadComponents.size(); ++i)
        {
            loadComponents[i]->Load();
        }
        m_loadedFiles.push_back(uFileId);
        BEYONDENGINE_CHECK_HEAP;
    }
}
Ejemplo n.º 2
0
bool CTextureAtlas::Load()
{
    BEATS_ASSERT(!IsLoaded());

    TString fileext = CFilePathTool::GetInstance()->Extension(GetFilePath().c_str());
    if(fileext == _T(".xml"))
    {
        TiXmlDocument doc;
        CSerializer serializer;
        CFilePathTool::GetInstance()->LoadFile(&serializer, GetFilePath().c_str(), _T("rb"));
        if (serializer.GetWritePos() != serializer.GetReadPos())
        {
            doc.Parse((char*)serializer.GetReadPtr());
        }
        TiXmlElement *root = doc.RootElement();
        BEATS_ASSERT(root && strcmp(root->Value(), "Imageset") == 0, 
            _T("TextureAtlas file %s not found or incorrect!"), GetFilePath().c_str());

        const char *textureFile = root->Attribute("Imagefile");
        BEATS_ASSERT(textureFile);
        TCHAR szNameBuffer[MAX_PATH];
        CStringHelper::GetInstance()->ConvertToTCHAR(textureFile, szNameBuffer, MAX_PATH);
        m_texture = CResourceManager::GetInstance()->GetResource<CTexture>(szNameBuffer);
        kmVec2 size;
        kmVec2 point;
        TString strName;
        for(TiXmlElement *elemImage = root->FirstChildElement("Image");
            elemImage != nullptr; elemImage = elemImage->NextSiblingElement("Image"))
        {
            const char *name = elemImage->Attribute("Name");
            BEATS_ASSERT(name);
            kmVec2Fill(&point, 0.f, 0.f);
            kmVec2Fill(&size, 0.f, 0.f);
            elemImage->QueryFloatAttribute("XPos", &point.x);
            elemImage->QueryFloatAttribute("YPos", &point.y);
            elemImage->QueryFloatAttribute("Width", &size.x);
            elemImage->QueryFloatAttribute("Height", &size.y);

            CStringHelper::GetInstance()->ConvertToTCHAR(name, szNameBuffer, MAX_PATH);
            strName.assign(szNameBuffer);
            CreateTextureFrag(strName, point, size);
        }
    }
    else
    {
        m_texture = CResourceManager::GetInstance()->GetResource<CTexture>(GetFilePath());
    }

    m_name = CFilePathTool::GetInstance()->FileName(GetFilePath().c_str());
    super::Load();
    return true;
}