示例#1
0
    CFileSystem::Handle CFileSystem::OpenCreateFile(const char* szFullPath, EOpenMode openAccess) {
#if BEHAVIAC_CCDEFINE_ANDROID && (BEHAVIAC_CCDEFINE_ANDROID_VER > 8)
        AAssetManager* mgr = behaviac::CFileManager::GetInstance()->GetAssetManager();
        Handle hFile = FILE_SYSTEM_INVALID_HANDLE;

        if (openAccess != EOpenMode_Read) {
            BEHAVIAC_LOGERROR("Only read mode is supported for asset files");
            return hFile;
        }

        if (behaviac::StringUtils::StartsWith(szFullPath, "assets:/")) {
            //skip "assets:/"
            const char* fileName = szFullPath + 8;

            AAsset* asset = AAssetManager_open(mgr, fileName, AASSET_MODE_UNKNOWN);

            hFile = (Handle)asset;
        }

#else
        Handle hFile = fopen(szFullPath, openMode[openAccess]);

        if (!hFile) {
            BEHAVIAC_LOGERROR("Could not open file %s", szFullPath);
            return hFile;
        }

#endif
        return hFile;
    }
示例#2
0
void CPathID::SetContent(const char* path)
{
    BEHAVIAC_ASSERT(path);   // no full paths allowed
    char filename[ PATHID_MAX_PATH ];
    filename[0] = '\0';
    CFileManager::GetInstance()->FormatPath(path, filename);
#ifdef _DEBUG

    if (strchr(filename, ':'))
    {
        BEHAVIAC_LOGERROR("Building a path id with a full path is prohibited.\n%s", filename);
    }

#endif // #ifdef _DEBUG

    if (CExtensionConfig::GetInstance())
    {
        const CExtensionConfig::SConfigInfo* cfg =  CExtensionConfig::GetInstance()->GetInfoFromSource(filename);

        if (cfg)
        {
            //BEHAVIAC_LOGWARNING("Resource ID for %s uses wrong extension (%s instead of %s).\nSupported, but should be fixed.", s.c_str(), cfg->m_sourceExt[0].c_str(), cfg->m_targetExt.c_str());
            const char* ext = behaviac::StringUtils::FindFullExtension(filename);
            strcpy(const_cast<char*>(ext), cfg->m_targetExt.c_str() + 1);
        }
    }

    CPathID::SetContentPrivate(filename);
}
示例#3
0
    //! When exceptions are disabled by defining RAPIDXML_NO_EXCEPTIONS,
    //! this function is called to notify user about the error.
    //! It must be defined by the user.
    //! <br><br>
    //! This function cannot return. If it does, the results are undefined.
    //! <br><br>
    //! A very simple definition might look like that:
    //! <pre>
    //! void %rapidxml::%parse_error_handler(const char *what, void *where)
    //! {
    //!     std::cout << "Parse error: " << what << "\n";
    //!     std::abort();
    //! }
    //! </pre>
    //! \param what Human readable description of the error.
    //! \param where Pointer to character data where error was detected.
    void parse_error_handler(const char* what, void* where)
    {
        BEHAVIAC_UNUSED_VAR(where);

        BEHAVIAC_LOGERROR("rapidxml parse error: %s\n", what);
        BEHAVIAC_ASSERT(0);
    }
示例#4
0
const CExtensionConfig::SConfigInfo* CExtensionConfig::GetInfoFromSource(const char* source)
{
    const char* str = behaviac::StringUtils::FindFullExtension(source);

    if (str && str[0])
    {
        const char* ext = str - 1;
        uint32_t resSize = m_resInfos.size();

        for (uint32_t i = 0; i < resSize; ++i)
        {
            SConfigInfo& cfg = m_resInfos[i];
            uint32_t extSize = cfg.m_sourceExt.size();

            for (uint32_t j = 0; j < extSize; ++j)
            {
                if (strcmp(cfg.m_sourceExt[j].c_str(), ext) == 0)
                {
                    return &cfg;
                }
            }
        }

        if (m_resInfos.size() == 0)
        {
            BEHAVIAC_LOGERROR("Be sure that XML file \"engine/ResourceConfig.xml\" exist.\n");
        }
    }

    return NULL;
}
示例#5
0
const CExtensionConfig::SConfigInfo* CExtensionConfig::GetInfoFromDependency(const char* file)
{
    const char* str = behaviac::StringUtils::FindFullExtension(file);

    if (str && str[0])
    {
        const char* ext = str - 1;

        for (unsigned int i = 0; i < m_resInfos.size(); ++i)
        {
            for (unsigned int j = 0; j < m_resInfos[i].m_compileDependencies.size(); ++j)
            {
                if (m_resInfos[i].m_compileDependencies[j] == ext)
                {
                    return &m_resInfos[i];
                }
            }
        }

        if (m_resInfos.size() == 0)
        {
            BEHAVIAC_LOGERROR("Be sure that XML file \"engine/ResourceConfig.xml\" exist.\n");
        }
    }

    return NULL;
}
示例#6
0
	EBTStatus FSMTask::UpdateFSM(Agent* pAgent, EBTStatus childStatus)
	{
		BEHAVIAC_ASSERT(this->m_node != 0);
		BEHAVIAC_ASSERT(this->m_currentNodeId != -1);

		EBTStatus status = childStatus;
		bool bLoop = true;

#if !BEHAVIAC_RELEASE
		const int kMaxCount = 10;
		behaviac::map<int, int> state_update_count;
#endif//#if !BEHAVIAC_RELEASE

		while (bLoop)
		{
			BehaviorTask* currentState = this->GetChildById(this->m_currentNodeId);
            //BEHAVIAC_ASSERT(currentState->GetNextStateId() == -1, "m_nextStateId is not reset to -1 in onenter");
			currentState->exec(pAgent);

			if (StateTask::DynamicCast(currentState) != 0)
			{
				StateTask* pStateTask = (StateTask*)currentState;

				if (pStateTask->IsEndState())
				{
					return BT_SUCCESS;
				}
			}

			int nextStateId = currentState->GetNextStateId();

			if (nextStateId < 0) // don't know why, the nextStateID might be -2147483648, so change the condition
			{
				//if not transitioned, don't go on next state, to exit
				bLoop = false;
			}
			else
			{
#if !BEHAVIAC_RELEASE
				state_update_count[this->m_currentNodeId]++;
				if (state_update_count[this->m_currentNodeId] > kMaxCount) {
					behaviac::string treeName = GetParentTreeName(pAgent, this->GetNode());
					BEHAVIAC_LOGERROR("%s might be updating an FSM('%s') endlessly, possibly a dead loop, please redesign it!", pAgent->GetName().c_str(), treeName.c_str());
					BEHAVIAC_ASSERT(false);
				}
#endif

				//if transitioned, go on next state
				this->m_currentNodeId = nextStateId;
			}
		}

		return status;
	}
示例#7
0
const CExtensionConfig::SConfigInfo* CExtensionConfig::GetInfoFromClassID(CStringID classID)
{
    for (unsigned int i = 0; i < m_resInfos.size(); ++i)
    {
        if (m_resInfos[i].m_classID == classID)
        {
            return &m_resInfos[i];
        }
    }

    if (m_resInfos.size() == 0)
    {
        BEHAVIAC_LOGERROR("Be sure that XML file \"engine/ResourceConfig.xml\" exist.\n");
    }

    return NULL;
}
示例#8
0
    bool CFileSystem::ReadFile(Handle file, void* pBuffer, uint32_t nNumberOfBytesToRead, uint32_t* pNumberOfBytesRead) {
        if (!file) {
            BEHAVIAC_LOGERROR("File not open");
            return 0;
        }

#if BEHAVIAC_CCDEFINE_ANDROID && (BEHAVIAC_CCDEFINE_ANDROID_VER > 8)
        size_t ret = AAsset_read((AAsset*)file, pBuffer, nNumberOfBytesToRead);
#else
        size_t ret = fread(pBuffer, 1, nNumberOfBytesToRead, (FILE*)file);
#endif

        if (pNumberOfBytesRead) {
            *pNumberOfBytesRead = ret;
        }

        return true;
    }
示例#9
0
	Workspace* Workspace::GetInstance(const char* version_str)
    {
        if (ms_instance == NULL)
        {
			if (version_str && !StringUtils::StrEqual(version_str, BEHAVIAC_VERSION_STR))
			{
				BEHAVIAC_LOGERROR("lib is built with '%s', while the app is built with '%s'!\n", BEHAVIAC_VERSION_STR, version_str);
				BEHAVIAC_ASSERT(false);
				return 0;
			}

            //if new  an Workspace class ,then the staict variable will be set value
            Workspace* _workspace = BEHAVIAC_NEW Workspace();
            BEHAVIAC_UNUSED_VAR(_workspace);
            BEHAVIAC_ASSERT(ms_instance != NULL);
        }

        return ms_instance;
    }
示例#10
0
	Workspace* Workspace::GetInstance(const char* version_str)
    {
        if (ms_instance == NULL)
        {
			if (version_str && !StringUtils::StrEqual(version_str, BEHAVIAC_BUILD_CONFIG_STR))
			{
				BEHAVIAC_LOGERROR("lib is built with '%s', while the executable is built with '%s'! please use the same define for '_DEBUG' and 'BEHAVIAC_RELEASE' in both the lib and executable's make.\n", BEHAVIAC_BUILD_CONFIG_STR, version_str);
				BEHAVIAC_ASSERT(false);
				return 0;
			}

            //if new  an Workspace class ,then the staict variable will be set value
            Workspace* _workspace = BEHAVIAC_NEW Workspace();
            BEHAVIAC_UNUSED_VAR(_workspace);
            BEHAVIAC_ASSERT(ms_instance != NULL);
        }

        return ms_instance;
    }
示例#11
0
const CExtensionConfig::SConfigInfo* CExtensionConfig::GetInfoFromTarget(const char* target)
{
    const char* str = behaviac::StringUtils::FindFullExtension(target);

    if (str && str[0])
    {
        const char* ext = str - 1;

        for (unsigned int i = 0; i < m_resInfos.size(); ++i)
        {
            if (m_resInfos[i].m_targetExt == ext)
            {
                return &m_resInfos[i];
            }
        }

        if (m_resInfos.size() == 0)
        {
            BEHAVIAC_LOGERROR("Be sure that XML file \"engine/ResourceConfig.xml\" exist.\n");
        }
    }

    return NULL;
}
示例#12
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;
    }