Beispiel #1
0
    bool Workspace::Load(const char* relativePath, bool bForce)
    {
        bool bOk = this->TryInit();
		if (!bOk)
		{
			//not init correctly
			return false;
		}

        //BEHAVIAC_ASSERT(behaviac::StringUtils::FindExtension(relativePath) == 0, "no extention to specify");
        BEHAVIAC_ASSERT(IsValidPath(relativePath));

        BehaviorTree* pBT = 0;
        BehaviorTrees_t::iterator it = m_behaviortrees.find(relativePath);

        if (it != m_behaviortrees.end())
        {
            if (!bForce)
            {
                return true;
            }

            pBT = it->second;
        }

        behaviac::string fullPath = StringUtils::CombineDir(this->GetFilePath(), relativePath);

        Workspace::EFileFormat f = this->GetFileFormat();

        switch (f)
        {
            case EFF_default:
            {
                // try to load the behavior in xml
                behaviac::string path = fullPath + ".xml";

                if (behaviac::CFileManager::GetInstance()->FileExists(path.c_str()))
                {
                    f = EFF_xml;
                    fullPath = path;
                }
                else
                {
                    // try to load the behavior in bson
                    path = fullPath + ".bson.bytes";

                    if (behaviac::CFileManager::GetInstance()->FileExists(path.c_str()))
                    {
                        f = EFF_bson;
                        fullPath = path;
                    }
                    // try to load the behavior in cpp
                    else
                    {
                        f = EFF_cpp;
                    }
                }
            }
            break;

            case EFF_xml:
                fullPath += ".xml";
                break;

            case EFF_bson:
                fullPath += ".bson.bytes";
                break;

            case EFF_cpp:
                break;

            default:
                BEHAVIAC_ASSERT(0);
                break;
        }

        bool bLoadResult = false;
        bool bNewly = false;

        if (!pBT)
        {
            //in case of circular referencebehavior
            bNewly = true;
            pBT = BEHAVIAC_NEW BehaviorTree();
            m_behaviortrees[relativePath] = pBT;
        }

        BEHAVIAC_ASSERT(pBT);

        bool bCleared = false;

        if (f == EFF_xml || f == EFF_bson)
        {
            char* pBuffer = ReadFileToBuffer(fullPath.c_str());

            if (pBuffer)
            {
                //if forced to reload
                if (!bNewly)
                {
                    bCleared = true;
                    pBT->Clear();
                }

                if (f == EFF_xml)
                {
                    bLoadResult = pBT->load_xml(pBuffer);
                }
                else
                {
                    bLoadResult = pBT->load_bson(pBuffer);
                }

                PopFileFromBuffer(pBuffer);
            }
            else
            {
                BEHAVIAC_LOGERROR("'%s' doesn't exist!, Please check the file name or override Workspace and its GetFilePath()\n", fullPath.c_str());
                BEHAVIAC_ASSERT(false);
            }
        }
        else if (f == EFF_cpp)
        {
            if (!bNewly)
            {
                bCleared = true;
                pBT->Clear();
            }

            if (m_behaviortreeCreators && m_behaviortreeCreators->find(relativePath) != m_behaviortreeCreators->end())
            {
                BehaviorTreeCreator_t btCreator = (*m_behaviortreeCreators)[relativePath];
                bLoadResult = (*btCreator)(pBT);
            }
            else
            {
                BEHAVIAC_ASSERT(0);
                BEHAVIAC_LOGWARNING("The behaviac_generated/behaviors/generated_behaviors.h should be included by one of your apps.");
            }
        }
        else
        {
            BEHAVIAC_ASSERT(0);
        }

        if (bLoadResult)
        {
            BEHAVIAC_ASSERT(pBT->GetName() == relativePath);

            if (!bNewly)
            {
                BEHAVIAC_ASSERT(m_behaviortrees[pBT->GetName()] == pBT);
            }
        }
        else
        {
            if (bNewly)
            {
                //if it is forced to reload
                m_behaviortrees.erase(relativePath);

                BEHAVIAC_DELETE(pBT);
            }
            else if (bCleared)
            {
                //it has been cleared but failed to load, to remove it
                m_behaviortrees.erase(relativePath);
            }

            BEHAVIAC_LOGWARNING("'%s' is not loaded!\n", fullPath.c_str());
        }

        return bLoadResult;
    }
Beispiel #2
0
	bool Workspace::Load(const char* relativePath, bool bForce)
	{
		BEHAVIAC_ASSERT(behaviac::StringUtils::FindExtension(relativePath) == 0, "no extention to specify");
		BEHAVIAC_ASSERT(IsValidPath(relativePath));

		BehaviorTree* pBT = 0;
		BehaviorTrees_t::iterator it = ms_behaviortrees.find(relativePath);
		if (it != ms_behaviortrees.end())
		{
			if (!bForce) {
				return true;
			}
			pBT = it->second;
		}

		behaviac::string fullPath = ms_workspace_export_path;
		fullPath += relativePath;

		Workspace::EFileFormat f = Workspace::GetFileFormat();
		switch (f)
		{
			case EFF_default:
				{
					// try to load the behavior in xml
					behaviac::string path = fullPath + ".xml";
					if (CFileManager::GetInstance()->FileExists(path.c_str()))
					{
						f = EFF_xml;
						fullPath = path;
					}
					else
					{
						// try to load the behavior in bson
						path = fullPath + ".bson.bytes";
						if (CFileManager::GetInstance()->FileExists(path.c_str()))
						{
							f = EFF_bson;
							fullPath = path;
						}
						// try to load the behavior in cpp
						else
						{
							f = EFF_cpp;
						}
					}
				}
				break;

			case EFF_xml:
				fullPath += ".xml";
				break;

			case EFF_bson:
				fullPath += ".bson.bytes";
				break;

			case EFF_cpp:
				break;

			default:
				BEHAVIAC_ASSERT(0);
				break;
		}

		bool bLoadResult = false;
		bool bNewly = false;
		if (!pBT) {
			//in case of circular referencebehavior
			bNewly = true;
			pBT = BEHAVIAC_NEW BehaviorTree();
			ms_behaviortrees[relativePath] = pBT;
		}

		BEHAVIAC_ASSERT(pBT);

		bool bCleared = false;

		if (f == EFF_xml || f == EFF_bson)
		{
			char* pBuffer = ReadFileToBuffer(fullPath.c_str());
			if (pBuffer) {
				//if forced to reload
				if (!bNewly) {
					bCleared = true;
					pBT->Clear();
				}

				if (f == EFF_xml) {
					bLoadResult = pBT->load_xml(pBuffer);
				}
				else {
					bLoadResult = pBT->load_bson(pBuffer);
				}

				PopFileFromBuffer(pBuffer);
			}
			else {
				BEHAVIAC_LOGWARNING("Workspace::Load:FileNotOpen %s", fullPath.c_str());
			}
		}
		else if (f == EFF_cpp)
		{
			if (!bNewly) {
				bCleared = true;
				pBT->Clear();
			}

			if (ms_behaviortreeCreators && ms_behaviortreeCreators->find(relativePath) != ms_behaviortreeCreators->end())
			{
				BehaviorTreeCreator_t btCreator = (*ms_behaviortreeCreators)[relativePath];
				bLoadResult = (*btCreator)(pBT);
			}
			else {
				BEHAVIAC_ASSERT(0);
				BEHAVIAC_LOGWARNING("The generated_behaviors.cpp file should be included by the app.");
			}
		}
		else
		{
			BEHAVIAC_ASSERT(0);
		}

		if (bLoadResult) {
			BEHAVIAC_ASSERT(pBT->GetName() == relativePath);
			if (!bNewly) {
				BEHAVIAC_ASSERT(ms_behaviortrees[pBT->GetName()] == pBT);
			}
		}
		else {
			if (bNewly) {
				//if it is forced to reload
				ms_behaviortrees.erase(relativePath);

				BEHAVIAC_DELETE(pBT);
			}
			else if (bCleared) {
				//it has been cleared but failed to load, to remove it
				ms_behaviortrees.erase(relativePath);
			}

			BEHAVIAC_LOGWARNING("BehaviorTree %s not loaded!\n", fullPath.c_str());
		}

		return bLoadResult;
	}