예제 #1
0
bool StringMgr::Init()
{
	bool result = false;
	XmlWrapper xmlWrapper;
	XmlDocument& doc = xmlWrapper.GetDoc();
	m_FilterList.clear();
	m_StringMap.clear();
	do
	{
		if (!xmlWrapper.LoadFile("res/Data/String/StringList.xml"))
			break;
		XmlElement* rootNode = doc.FirstChildElement("StringList");
		if (rootNode == NULL)
			break;
		bool hasError = false;
		for (XmlElement* fileNode = rootNode->FirstChildElement("String"); fileNode; fileNode = fileNode->NextSiblingElement("String"))
		{
			const char* fileName = fileNode->Attribute("File");
			if (!LoadString(fileName))
			{
				GameLog("LoadString error =%s", fileName ? fileName : "");
				hasError = true;
			}
		}
		result = !hasError;
	} while (0);

	return result;
}
예제 #2
0
bool StringMgr::LoadStringWithPath(const char* dpath, const char* fileName, bool isFilter)
{
	if (!fileName)
		return false;
	bool result = false;
	XmlWrapper xmlWrapper;
	XmlDocument& doc = xmlWrapper.GetDoc();

	do
	{
		char path[256] = {};
		sprintf(path, "%s/%s", dpath, fileName);
		if (!xmlWrapper.LoadFile(path))
			break;
		XmlElement* rootNode = doc.FirstChildElement("Strings");
		if (rootNode == NULL)
			break;
		if (isFilter)
		{
			for (XmlElement* stringNode = rootNode->FirstChildElement("s"); stringNode; stringNode = stringNode->NextSiblingElement("s"))
			{
				const char* val = stringNode->Attribute("v");
				m_FilterList.push_back(val);
			}
		}
		else
		{
			for (XmlElement* stringNode = rootNode->FirstChildElement("String"); stringNode; stringNode = stringNode->NextSiblingElement("String"))
			{
				const char* key = stringNode->Attribute("Key");
				const char* value = stringNode->Attribute("Value");
				if (key && value && strlen(key) > 0 && strlen(value) > 0)
				{
					if (m_StringMap.find(key) != m_StringMap.end())
					{
						//Assert(false);
						GameLog("string key exist:%s file=%s", key, fileName);
					}
					m_StringMap[key] = value;
				}
			}
		}
		result = true;
	} while (0);

	return result;
}
예제 #3
0
bool StringMgr::Parse(const char* key, const string& text, vector<int>& indexs, vector<ETypeId>& typeIds, vector<string>& strings)
{
	int i = 0;
	int j = 0;
	int k = 0;
	bool hasError = false;
	while (true)
	{
		// ÕÒµ½{%µÄλÖÃ
		for (int x = i; x < (int)text.length(); ++x)
		{
			if (text[x] == '{' && x < (int)text.length() - 1 && text[x + 1] == '%')
			{
				i = x;
				break;
			}
		}
		if (i < 0)
			break;
		// 0123{%s1}
		// 012345678
		if ((int)text.length() < i + 5)
			break;
		// ¼ì²é¸ñʽ»¯ÀàÐÍ
		char typeChar = text.at(i + 2);
		// 0123{%{%s1}
		if (typeChar != 's'
			&& typeChar != 'S'
			&& typeChar != 'd'
			&& typeChar != 'D'
			&& typeChar != 'i'
			&& typeChar != 'I'
			&& typeChar != 'u'
			&& typeChar != 'U'
			&& typeChar != 'f'
			&& typeChar != 'F')
		{
			i += 2;
			continue;
		}
		j = text.find_first_of("}", i + 4);
		if (j < 0)
		{
			// 0123{%s1{%d2}
			i += 4;
			continue;
		}
		int indexLen = j - i - 3;
		/// 0123{%s}8
		if (indexLen < 1)
		{
			i = j + 1;
			continue;
		}
		// Ö»Ö§³Ö1-99µÄÐòºÅ
		// 0123{%s100}
		if (indexLen > 2)
		{
			i = j + 1;
			continue;
		}
		string indexStr = text.substr(i + 3, indexLen);
		/// ÅжÏÊÇ·ñÊÇÊý×Ö
		if (indexStr < "0" || indexStr > "99")
		{
			// 0123{%sab}
			//Assert(false);
			i = j + 1;
			continue;
		}
		int index = TypeConvert::ToInt(indexStr.c_str());
		if (index <= 0)
		{
			GameLog("StringMgr::Parse Error : %s", key);
			hasError = true;
			break;
		}
		if (typeChar == 's' || typeChar == 'S')
			typeIds.push_back(ETypeId_string);
		else if (typeChar == 'd' || typeChar == 'D' || typeChar == 'i' || typeChar == 'I')
			typeIds.push_back(ETypeId_int);
		else if (typeChar == 'u' || typeChar == 'U')
			typeIds.push_back(ETypeId_uint);
		else if (typeChar == 'f' || typeChar == 'F')
			typeIds.push_back(ETypeId_double);
		indexs.push_back(index);
		strings.push_back(text.substr(k, i - k));
		k = j + 1;
		i = j + 1;
	}
	strings.push_back(text.substr(k, text.length() - k));
	/// ¼ì²éÐòºÅÊÇ·ñÓÐÎó
	for (size_t i = 0; i<indexs.size(); ++i)
	{
		/// ÐòºÅ³¬³ö·¶Î§
		if (indexs[i] >(int)indexs.size())
		{
			GameLog("StringParser::Parse Error :%s", key);
			hasError = true;
			break;
		}
		/// ÐòºÅÖظ´
		for (size_t j = i + 1; j < indexs.size(); ++j)
		{
			if (indexs[i] == indexs[j])
			{
				GameLog("StringParser::Parse Error : %s", key);
				hasError = true;
				break;
			}
		}
	}
	if (hasError)
	{
		indexs.clear();
		strings.clear();
	}
	return !hasError;
}
예제 #4
0
void GameMapDescription::Load(core::stringc fn)
{
	GameLog("Map description %s loading started...", fn.c_str());
	io::IXMLReader *xml = Game.FS->createXMLReader(fn.c_str());

	for (int i=0; i < GAMEMAPDESCRIPTION_MAXDESCCOUNT; i++)
	{
		Items[i].keycount = 0;
		for (int k=0; k < GAMEMAPDESCRIPTION_MAXKEYCOUNT; k++) Items[i].key[k] = "";
		Items[i].title = "";
		Items[i].desc = "";
	}

	ItemsCount = 0;

	while( xml && xml->read() )
	{
		core::stringc node = xml->getNodeName();

		if (node == L"item")
		{
			switch (xml->getNodeType())
			{
				case io::EXN_ELEMENT_END:
					ItemsCount++;
					break;

				case io::EXN_ELEMENT:
					Items[ItemsCount].keycount = 0;
					while(xml && xml->read())
					{
						core::stringc node2 = xml->getNodeName();

						if (xml->getNodeType() == io::EXN_ELEMENT_END &&
							node == L"item")
						{
							break;
						}

						if (xml->getNodeType() == io::EXN_TEXT)
						{
							Items[ItemsCount].desc = xml->getNodeData();
						}

						if (node2 == L"title")
						{
							Items[ItemsCount].title =
								xml->getAttributeValue(L"value");
						}

						if (node2 == L"key")
						{
							Items[ItemsCount].key[Items[ItemsCount].keycount++] =
								xml->getAttributeValue(L"value");
						}

						assert( Items[ItemsCount].keycount < GAMEMAPDESCRIPTION_MAXKEYCOUNT );
					}
					break;
			}
		}
	}

	xml->drop();
	GameLog("Map description loaded.");
}