⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rpgtextparse.cc

📁 五行MMORPG引擎系统V1.0
💻 CC
📖 第 1 页 / 共 2 页
字号:
			}
			//普通模式 /entry
			else
			{
				bOperator	= dIsopr(*pSlash);

				//处理Entry标签
				if(bOperator)
				{
					for(; pSlash<pTail;pSlash++)
						if(!dIsopr(*pSlash) || dIsslash(*pSlash) )
							break;
					nNameLen = pSlash - pEntry;
				}
				else
				{
					for(; pSlash<pTail;pSlash++)
						if(!dIsvarchar(*pSlash)/* && *pSlash != '_'*/)
							break;

					nNameLen = pSlash - pEntry;
					//跳过收尾符
					if(*pSlash == '>')
						pSlash++;
				}
			}//else

			///////////////////////////////////////////////
			//判断是否有参数
			if(*pEntry && *pSlash == ':')
			{
				//如果格式   :<param> 或  :<param\n格式
				if(pSlash[1] == '<')
				{
					pSlash+=2;
					pParamPtr = pSlash;
					for(; pSlash<pTail;pSlash++)
						if(/*dIsslash(*pSlash) ||*/ *pSlash == '\n' || *pSlash == '>')
							break;

					nParamsLen = pSlash - pParamPtr;
					if(*pSlash == '>')
						pSlash++;
					pParams		= m_textPool.getStrBuffer(nParamsLen+2);
					pParams[0] = ':';
					dStrncpy(pParams+1, pParamPtr, nParamsLen);
					nParamsLen++;
				}
				//普通模式 /entry
				else
				{
					pParamPtr = pSlash;
					for(; pSlash<pTail;pSlash++)
					{
						if(!dIsalnum(*pSlash) && *pSlash != '_' && *pSlash != ':')
							break;
					}
					nParamsLen	= pSlash - pParamPtr;
					//跳过收尾符
					if(*pSlash == '>')
						pSlash++;
					pParams		= m_textPool.getStrBuffer(nParamsLen+1);
					dStrncpy(pParams, pParamPtr, nParamsLen);
				}//else
			}//if
		}//else
		

		if(nSecLen)
		{
			//保存%前的原字符串内容
			//m_textPool.advance();
			pSection = m_textPool.getStrBuffer(nSecLen+1);
			dStrncpy(pSection, pCurrent, nSecLen);
			m_nBufSize += nSecLen;

			m_arSections.increment();
			m_arSections.last().pSection	= pSection;
			m_arSections.last().nLen		= nSecLen;
			m_arSections.last().pParams	= NULL;
		}

		if(*pEntry != '/' && *pEntry)
		{
			sIDName	= StringTable->insertn(pEntry,nNameLen);
			m_nBufSize += ParseEntry(sIDName,pParams, nParamsLen,nLevel);

			//m_arSections.last().pParams	= pParams;
			//nBufSize += nParamsLen;

		}

		pCurrent = pSlash;
	}//for


	//直接返回原内容,没有变量替换的话
	if(pCurrent == sText)
		return sText;
	
	//保存最的一段内容
	nSecLen = pTail - pCurrent;
	if(nSecLen)
	{
		m_nBufSize += nSecLen;

		m_arSections.increment();
		m_arSections.last().pSection	= pCurrent;
		m_arSections.last().pParams	= NULL;
		m_arSections.last().nLen		= nSecLen;
	}

	if(nLevel < m_nLevelNum)
		return NULL;
	return ConcatSections(m_nBufSize);
}


BOOL RPGTextParse::ClearParse()
{
	m_textPool.clear();
	m_arSections.clear();
	m_arColorStack.clear();
	m_nBufSize = 0;
	return TRUE;
}


RPGTextParse& RPGTextParse::operator << (CSTR str)
{
	//char* pSection;
	int	nSecLen;

	nSecLen = dStrlen(str);
	m_nBufSize += nSecLen;
	//pSection = m_textPool.getStrBuffer(nSecLen+1);
	//dStrcpy(pSection, str);

	m_arSections.increment();
	m_arSections.last().pSection	= str;
	m_arSections.last().pParams	= NULL;
	m_arSections.last().nLen		= nSecLen;
	return *this;
}


CSTR RPGTextParse::ConcatSections(U32 nBufSize)
{
	char	*pContent,
			*pCurrent;
	U32	n;
	if(nBufSize == 0 || m_arSections.size()==0)
	{
		return "";
	}
	//consoleAlloc();
	///////////////////////////////////
	//内容连接
	//直接把内容写到STR中
	//m_textPool.advance();
	pContent = m_textPool.getStrBuffer(nBufSize+1);

	pCurrent = pContent;
	for(n=0; n<m_arSections.size(); n++)
	{	
		//switch(m_arSections[n].cType)
		//{
		//case 'b':
		//	dStrcpy(pCurrent,dAtob(pVar)?"true":"false");
		//	break;
		//case 'd':
		//case 'i':
		//	dSprintf(pCurrent,nBufSize - (pCurrent-pContent),"%d",dAtoi(pVar));
		//	break;
		//case 'f':
		//case 'g':
		//	dSprintf(pCurrent,nBufSize - (pCurrent-pContent),"%g",dAtof(pVar));
		//	break;
		//case 's':
		//default:
		//	dStrcpy(pCurrent,pVar);
		//	break;
		//}
		dStrncpy(pCurrent,m_arSections[n].pSection , m_arSections[n].nLen);
		pCurrent += m_arSections[n].nLen;
		if(m_arSections[n].pParams)
		{
			dStrncpy(pCurrent,m_arSections[n].pParams , m_arSections[n].nParamsLen);
			pCurrent += m_arSections[n].nParamsLen;
		}
	}
	pContent[nBufSize] = 0;
	return pContent;

}


U32 RPGTextParse::ParseEntry(StringTableEntry sIDName,CSTR pParams, U32 nParamsLen,S32 nLevel)
{
	TextEntry*	pEntry;
	CSTR			pValue;
	char*			pBuffer;
	U32			nLen;



	nLen = 0;
	pEntry = m_arEntrys.Get(sIDName);
	if(pEntry)
	{
		SimObject*	pGObject = NULL;
		pValue	= StringTable->getBlank();;

		switch(pEntry->dwType)
		{
		case TXTT_GAME:
			pGObject = static_cast<SimObject*>(g_pRPGSetting);
			break;
		case TXTT_PLAYER:
			pGObject = static_cast<SimObject*>(g_pGPlayer);
			break;
		case TXTT_NPC:
			pGObject = static_cast<SimObject*>(g_pGNpc);
			break;
		case TXTT_SCENE:
			pGObject = static_cast<SimObject*>(g_pGScene);
			break;
		case TXTT_DIALOG:
			pGObject = static_cast<SimObject*>(g_pRPGDialog);
			break;
		case TXTT_GOODS:
			pGObject = static_cast<SimObject*>(g_pGGoods);
			break;

		default:
			pValue	= 	NULL;
			break;
		}

		if(pValue)
		{	
			if(pGObject)
			{
				if(pEntry->pField[0] == '*')
					pValue = Con::executef(pGObject,1,pEntry->pField+1);
				else
					pValue = pGObject->getDataField(pEntry->pField, NULL);
			}
		}
		else
		{
			switch(pEntry->dwType)
			{
			case TXTT_TASK:
				if(dynamic_cast<GTaskState*>(g_pGTaskState))
					pValue = g_pGTaskState->GetDataBy(pEntry->pField);
				pParams		= NULL;
				nParamsLen	= 0;
				break;

			case TXTT_ICON:
				nLen		= dStrlen(m_pExpsPath) + dStrlen(pEntry->pFilename) + 10;
				pBuffer = m_textPool.getStrBuffer(nLen+1);
				pValue  = pBuffer;
				dSprintf(pBuffer,nLen,"<bitmap:%s/%s>", 
							m_pExpsPath, 
							pEntry->pFilename);
				break;

			case TXTT_ACTION:
			case TXTT_MOTION:
				{
					pValue	= NULL;
					GCharacter*	pChar;
					if(!pParams || !Sim::findObject(pParams+1,pChar) )
						pChar = static_cast<GCharacter*>(g_pGPlayer);
					if(pChar)
					{
						pChar->PlayMotion(pEntry->pData);
					}
				}
				break;

			case TXTT_COLORMODE:
				pValue		= NULL;
				m_sClrTag	= pEntry->pTag;
				m_nClrTagLen	= dStrlen(m_sClrTag);
				break;


			case TXTT_TAG2:
				if(pParams && pParams[0])
				{	
					pParams++;
					nParamsLen--;
				}
			case TXTT_TAG:

				if(pParams && pParams[0] == ':' && pParams[1] == '$')
				{	
					pParams = Con::getVariable(pParams+1);
					nLen		= dStrlen(pEntry->pTag) + dStrlen(pParams) + 3;
					pBuffer	= m_textPool.getStrBuffer(nLen+1);
					dSprintf(pBuffer,nLen,"<%s:%s>", pEntry->pTag, pParams);
				}
				else
				{
					nLen		= dStrlen(pEntry->pTag) + nParamsLen + 2;
					pBuffer	= m_textPool.getStrBuffer(nLen+1);
					dSprintf(pBuffer,nLen,"<%s%s>", pEntry->pTag, pParams?pParams:"");
				}
				pValue		= pBuffer;
				pParams		= NULL;
				nParamsLen	= 0;
				break;

			case TXTT_TAGEND:
				pParams		= NULL;
				nParamsLen  = 0; //标签结束不需要参数
				nLen		= dStrlen(pEntry->pTag) +  3;
				pBuffer	= m_textPool.getStrBuffer(nLen+1);
				pValue	= pBuffer;
				dSprintf(pBuffer,nLen,"</%s>", pEntry->pTag);
				break;


			case TXTT_COLOR:
				{
					if(pEntry->pColor[0] == '@')
					{	
						m_arColorStack.pop_back();
						if(m_arColorStack.size())
							pEntry = m_arColorStack.last();
						else
							pEntry = NULL;
					}
					else
						m_arColorStack.push_back(pEntry);

					if(pEntry)
					{
						nLen = m_nClrTagLen + 8 + 3;
						pBuffer = m_textPool.getStrBuffer(nLen+1);
						pValue  = pBuffer;

						if(pParams && pParams[0] == ':' && pParams[1] == '$')
							dSprintf(pBuffer,nLen,"<%s:%s>", m_sClrTag, Con::getVariable(pParams+1));
						else if(pEntry->pColor[0] && pEntry->pColor[0] != '.')
							dSprintf(pBuffer,nLen,"<%s:%8s>", m_sClrTag ,pEntry->pColor);
						else if(nParamsLen <= 7)
							dSprintf(pBuffer,nLen,"<%s:%6sff>", m_sClrTag ,pParams&&pParams[0]?pParams+1 : "");
						else
							dSprintf(pBuffer,nLen,"<%s:%8s>", m_sClrTag ,pParams+1);
						pParams		= NULL;
						nParamsLen	= 0;
						pBuffer += m_nClrTagLen+2;
						for(U32 n=0;n < 8; n++) 
							if(!dIshex(pBuffer[n]))
								pBuffer[n] = '0';
					}
					else
						pValue = NULL;
				}
				break;

			case TXTT_GVAR:		//全局变量
				pValue = Con::getVariable(pEntry->pVar);
				break;

			case TXTT_VAR:			//先局部后全局
			default:
				pValue = Con::getLocalVariable(pEntry->pVar);
				if(pValue == 0 || pValue[0] == 0)
					pValue = Con::getVariable(pEntry->pVar);
				break;
			}//switch
		}//else

	}//if
	else
	{
		pValue = sIDName;
	}

	if(pValue == NULL)
		nLen = 0;
	else
	{
		//内容格式为  =>内容
		if(pValue[0] == '=' && pValue[1] == '>')
		{
			if(ParseText(pValue+2,nLevel-1) == NULL)
				return 0;
		}

		if(nLen == 0)
			nLen = dStrlen(pValue);
	}

	if(pValue && nLen)
	{
		m_arSections.increment();
		m_arSections.last().pSection	= pValue;
		m_arSections.last().nLen		= nLen;
		m_arSections.last().pParams	= pParams;
		m_arSections.last().nParamsLen= nParamsLen;
	}

	return nLen + nParamsLen;
}

ConsoleMethod(RPGTextParse,ParseText,const char*, 3,3,"object.ParseText(sText)")
{
	return object->ParseText(argv[2]);
}


};//namespace RPG

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -