mirror of
https://github.com/vcmi/vcmi.git
synced 2025-11-25 22:42:04 +02:00
Renamed MetaString methods to more logical names
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* CGameState.cpp, part of VCMI engine
|
||||
* MetaString.cpp, part of VCMI engine
|
||||
*
|
||||
* Authors: listed in file AUTHORS in main folder
|
||||
*
|
||||
@@ -22,176 +22,202 @@
|
||||
|
||||
VCMI_LIB_NAMESPACE_BEGIN
|
||||
|
||||
void MetaString::getLocalString(const std::pair<ui8, ui32> & txt, std::string & dst) const
|
||||
void MetaString::appendLocalString(EMetaText type, ui32 serial)
|
||||
{
|
||||
int type = txt.first;
|
||||
message.push_back(EMessage::APPEND_LOCAL_STRING);
|
||||
localStrings.emplace_back(type, serial);
|
||||
}
|
||||
|
||||
void MetaString::appendRawString(std::string value)
|
||||
{
|
||||
message.push_back(EMessage::APPEND_RAW_STRING);
|
||||
exactStrings.push_back(value);
|
||||
}
|
||||
|
||||
void MetaString::appendNumber(int64_t value)
|
||||
{
|
||||
message.push_back(EMessage::APPEND_NUMBER);
|
||||
numbers.push_back(value);
|
||||
}
|
||||
|
||||
void MetaString::replaceLocalString(EMetaText type, ui32 serial)
|
||||
{
|
||||
message.push_back(EMessage::REPLACE_LOCAL_STRING);
|
||||
localStrings.emplace_back(type, serial);
|
||||
}
|
||||
|
||||
void MetaString::replaceRawString(const std::string &txt)
|
||||
{
|
||||
message.push_back(EMessage::REPLACE_RAW_STRING);
|
||||
exactStrings.push_back(txt);
|
||||
}
|
||||
|
||||
void MetaString::replaceNumber(int64_t txt)
|
||||
{
|
||||
message.push_back(EMessage::REPLACE_NUMBER);
|
||||
numbers.push_back(txt);
|
||||
}
|
||||
|
||||
void MetaString::replacePositiveNumber(int64_t txt)
|
||||
{
|
||||
message.push_back(EMessage::REPLACE_POSITIVE_NUMBER);
|
||||
numbers.push_back(txt);
|
||||
}
|
||||
|
||||
void MetaString::clear()
|
||||
{
|
||||
exactStrings.clear();
|
||||
localStrings.clear();
|
||||
message.clear();
|
||||
numbers.clear();
|
||||
}
|
||||
|
||||
bool MetaString::empty() const
|
||||
{
|
||||
return message.empty();
|
||||
}
|
||||
|
||||
std::string MetaString::getLocalString(const std::pair<EMetaText, ui32> & txt) const
|
||||
{
|
||||
EMetaText type = txt.first;
|
||||
int ser = txt.second;
|
||||
|
||||
if(type == ART_NAMES)
|
||||
switch(type)
|
||||
{
|
||||
const auto * art = ArtifactID(ser).toArtifact(VLC->artifacts());
|
||||
if(art)
|
||||
dst = art->getNameTranslated();
|
||||
else
|
||||
dst = "#!#";
|
||||
}
|
||||
else if(type == ART_DESCR)
|
||||
{
|
||||
const auto * art = ArtifactID(ser).toArtifact(VLC->artifacts());
|
||||
if(art)
|
||||
dst = art->getDescriptionTranslated();
|
||||
else
|
||||
dst = "#!#";
|
||||
}
|
||||
else if (type == ART_EVNTS)
|
||||
{
|
||||
const auto * art = ArtifactID(ser).toArtifact(VLC->artifacts());
|
||||
if(art)
|
||||
dst = art->getEventTranslated();
|
||||
else
|
||||
dst = "#!#";
|
||||
}
|
||||
else if(type == CRE_PL_NAMES)
|
||||
{
|
||||
const auto * cre = CreatureID(ser).toCreature(VLC->creatures());
|
||||
if(cre)
|
||||
dst = cre->getNamePluralTranslated();
|
||||
else
|
||||
dst = "#!#";
|
||||
}
|
||||
else if(type == CRE_SING_NAMES)
|
||||
{
|
||||
const auto * cre = CreatureID(ser).toCreature(VLC->creatures());
|
||||
if(cre)
|
||||
dst = cre->getNameSingularTranslated();
|
||||
else
|
||||
dst = "#!#";
|
||||
}
|
||||
else if(type == MINE_NAMES)
|
||||
{
|
||||
dst = VLC->generaltexth->translate("core.minename", ser);
|
||||
}
|
||||
else if(type == MINE_EVNTS)
|
||||
{
|
||||
dst = VLC->generaltexth->translate("core.mineevnt", ser);
|
||||
}
|
||||
else if(type == SPELL_NAME)
|
||||
{
|
||||
const auto * spell = SpellID(ser).toSpell(VLC->spells());
|
||||
if(spell)
|
||||
dst = spell->getNameTranslated();
|
||||
else
|
||||
dst = "#!#";
|
||||
}
|
||||
else if(type == OBJ_NAMES)
|
||||
{
|
||||
dst = VLC->objtypeh->getObjectName(ser, 0);
|
||||
}
|
||||
else if(type == SEC_SKILL_NAME)
|
||||
{
|
||||
dst = VLC->skillh->getByIndex(ser)->getNameTranslated();
|
||||
}
|
||||
else
|
||||
{
|
||||
switch(type)
|
||||
case EMetaText::ART_NAMES:
|
||||
{
|
||||
case GENERAL_TXT:
|
||||
dst = VLC->generaltexth->translate("core.genrltxt", ser);
|
||||
break;
|
||||
case RES_NAMES:
|
||||
dst = VLC->generaltexth->translate("core.restypes", ser);
|
||||
break;
|
||||
case ARRAY_TXT:
|
||||
dst = VLC->generaltexth->translate("core.arraytxt", ser);
|
||||
break;
|
||||
case CREGENS:
|
||||
dst = VLC->objtypeh->getObjectName(Obj::CREATURE_GENERATOR1, ser);
|
||||
break;
|
||||
case CREGENS4:
|
||||
dst = VLC->objtypeh->getObjectName(Obj::CREATURE_GENERATOR4, ser);
|
||||
break;
|
||||
case ADVOB_TXT:
|
||||
dst = VLC->generaltexth->translate("core.advevent", ser);
|
||||
break;
|
||||
case COLOR:
|
||||
dst = VLC->generaltexth->translate("vcmi.capitalColors", ser);
|
||||
break;
|
||||
case JK_TXT:
|
||||
dst = VLC->generaltexth->translate("core.jktext", ser);
|
||||
break;
|
||||
default:
|
||||
logGlobal->error("Failed string substitution because type is %d", type);
|
||||
dst = "#@#";
|
||||
return;
|
||||
const auto * art = ArtifactID(ser).toArtifact(VLC->artifacts());
|
||||
if(art)
|
||||
return art->getNameTranslated();
|
||||
return "#!#";
|
||||
}
|
||||
case EMetaText::ART_DESCR:
|
||||
{
|
||||
const auto * art = ArtifactID(ser).toArtifact(VLC->artifacts());
|
||||
if(art)
|
||||
return art->getDescriptionTranslated();
|
||||
return "#!#";
|
||||
}
|
||||
case EMetaText::ART_EVNTS:
|
||||
{
|
||||
const auto * art = ArtifactID(ser).toArtifact(VLC->artifacts());
|
||||
if(art)
|
||||
return art->getEventTranslated();
|
||||
return "#!#";
|
||||
}
|
||||
case EMetaText::CRE_PL_NAMES:
|
||||
{
|
||||
const auto * cre = CreatureID(ser).toCreature(VLC->creatures());
|
||||
if(cre)
|
||||
return cre->getNamePluralTranslated();
|
||||
return "#!#";
|
||||
}
|
||||
case EMetaText::CRE_SING_NAMES:
|
||||
{
|
||||
const auto * cre = CreatureID(ser).toCreature(VLC->creatures());
|
||||
if(cre)
|
||||
return cre->getNameSingularTranslated();
|
||||
return "#!#";
|
||||
}
|
||||
case EMetaText::MINE_NAMES:
|
||||
{
|
||||
return VLC->generaltexth->translate("core.minename", ser);
|
||||
}
|
||||
case EMetaText::MINE_EVNTS:
|
||||
{
|
||||
return VLC->generaltexth->translate("core.mineevnt", ser);
|
||||
}
|
||||
case EMetaText::SPELL_NAME:
|
||||
{
|
||||
const auto * spell = SpellID(ser).toSpell(VLC->spells());
|
||||
if(spell)
|
||||
return spell->getNameTranslated();
|
||||
return "#!#";
|
||||
}
|
||||
case EMetaText::OBJ_NAMES:
|
||||
return VLC->objtypeh->getObjectName(ser, 0);
|
||||
case EMetaText::SEC_SKILL_NAME:
|
||||
return VLC->skillh->getByIndex(ser)->getNameTranslated();
|
||||
case EMetaText::GENERAL_TXT:
|
||||
return VLC->generaltexth->translate("core.genrltxt", ser);
|
||||
case EMetaText::RES_NAMES:
|
||||
return VLC->generaltexth->translate("core.restypes", ser);
|
||||
case EMetaText::ARRAY_TXT:
|
||||
return VLC->generaltexth->translate("core.arraytxt", ser);
|
||||
case EMetaText::CREGENS:
|
||||
return VLC->objtypeh->getObjectName(Obj::CREATURE_GENERATOR1, ser);
|
||||
case EMetaText::CREGENS4:
|
||||
return VLC->objtypeh->getObjectName(Obj::CREATURE_GENERATOR4, ser);
|
||||
case EMetaText::ADVOB_TXT:
|
||||
return VLC->generaltexth->translate("core.advevent", ser);
|
||||
case EMetaText::COLOR:
|
||||
return VLC->generaltexth->translate("vcmi.capitalColors", ser);
|
||||
case EMetaText::JK_TXT:
|
||||
return VLC->generaltexth->translate("core.jktext", ser);
|
||||
default:
|
||||
logGlobal->error("Failed string substitution because type is %d", static_cast<int>(type));
|
||||
return "#@#";
|
||||
}
|
||||
}
|
||||
|
||||
DLL_LINKAGE void MetaString::toString(std::string &dst) const
|
||||
DLL_LINKAGE std::string MetaString::toString() const
|
||||
{
|
||||
std::string dst;
|
||||
|
||||
size_t exSt = 0;
|
||||
size_t loSt = 0;
|
||||
size_t nums = 0;
|
||||
dst.clear();
|
||||
|
||||
for(const auto & elem : message)
|
||||
{//TEXACT_STRING, TLOCAL_STRING, TNUMBER, TREPLACE_ESTRING, TREPLACE_LSTRING, TREPLACE_NUMBER
|
||||
{
|
||||
switch(elem)
|
||||
{
|
||||
case TEXACT_STRING:
|
||||
case EMessage::APPEND_RAW_STRING:
|
||||
dst += exactStrings[exSt++];
|
||||
break;
|
||||
case TLOCAL_STRING:
|
||||
case EMessage::APPEND_LOCAL_STRING:
|
||||
{
|
||||
std::string hlp;
|
||||
getLocalString(localStrings[loSt++], hlp);
|
||||
std::string hlp = getLocalString(localStrings[loSt++]);
|
||||
dst += hlp;
|
||||
}
|
||||
break;
|
||||
case TNUMBER:
|
||||
case EMessage::APPEND_NUMBER:
|
||||
dst += std::to_string(numbers[nums++]);
|
||||
break;
|
||||
case TREPLACE_ESTRING:
|
||||
case EMessage::REPLACE_RAW_STRING:
|
||||
boost::replace_first(dst, "%s", exactStrings[exSt++]);
|
||||
break;
|
||||
case TREPLACE_LSTRING:
|
||||
case EMessage::REPLACE_LOCAL_STRING:
|
||||
{
|
||||
std::string hlp;
|
||||
getLocalString(localStrings[loSt++], hlp);
|
||||
std::string hlp = getLocalString(localStrings[loSt++]);
|
||||
boost::replace_first(dst, "%s", hlp);
|
||||
}
|
||||
break;
|
||||
case TREPLACE_NUMBER:
|
||||
case EMessage::REPLACE_NUMBER:
|
||||
boost::replace_first(dst, "%d", std::to_string(numbers[nums++]));
|
||||
break;
|
||||
case TREPLACE_PLUSNUMBER:
|
||||
case EMessage::REPLACE_POSITIVE_NUMBER:
|
||||
boost::replace_first(dst, "%+d", '+' + std::to_string(numbers[nums++]));
|
||||
break;
|
||||
default:
|
||||
logGlobal->error("MetaString processing error! Received message of type %d", int(elem));
|
||||
logGlobal->error("MetaString processing error! Received message of type %d", static_cast<int>(elem));
|
||||
assert(0);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
DLL_LINKAGE std::string MetaString::toString() const
|
||||
{
|
||||
std::string ret;
|
||||
toString(ret);
|
||||
return ret;
|
||||
return dst;
|
||||
}
|
||||
|
||||
DLL_LINKAGE std::string MetaString::buildList () const
|
||||
///used to handle loot from creature bank
|
||||
{
|
||||
|
||||
size_t exSt = 0;
|
||||
size_t loSt = 0;
|
||||
size_t nums = 0;
|
||||
std::string lista;
|
||||
for (int i = 0; i < message.size(); ++i)
|
||||
{
|
||||
if (i > 0 && (message[i] == TEXACT_STRING || message[i] == TLOCAL_STRING))
|
||||
if (i > 0 && (message[i] == EMessage::APPEND_RAW_STRING || message[i] == EMessage::APPEND_LOCAL_STRING))
|
||||
{
|
||||
if (exSt == exactStrings.size() - 1)
|
||||
lista += VLC->generaltexth->allTexts[141]; //" and "
|
||||
@@ -200,30 +226,28 @@ DLL_LINKAGE std::string MetaString::buildList () const
|
||||
}
|
||||
switch (message[i])
|
||||
{
|
||||
case TEXACT_STRING:
|
||||
case EMessage::APPEND_RAW_STRING:
|
||||
lista += exactStrings[exSt++];
|
||||
break;
|
||||
case TLOCAL_STRING:
|
||||
case EMessage::APPEND_LOCAL_STRING:
|
||||
{
|
||||
std::string hlp;
|
||||
getLocalString (localStrings[loSt++], hlp);
|
||||
std::string hlp = getLocalString (localStrings[loSt++]);
|
||||
lista += hlp;
|
||||
}
|
||||
break;
|
||||
case TNUMBER:
|
||||
case EMessage::APPEND_NUMBER:
|
||||
lista += std::to_string(numbers[nums++]);
|
||||
break;
|
||||
case TREPLACE_ESTRING:
|
||||
case EMessage::REPLACE_RAW_STRING:
|
||||
lista.replace (lista.find("%s"), 2, exactStrings[exSt++]);
|
||||
break;
|
||||
case TREPLACE_LSTRING:
|
||||
case EMessage::REPLACE_LOCAL_STRING:
|
||||
{
|
||||
std::string hlp;
|
||||
getLocalString (localStrings[loSt++], hlp);
|
||||
std::string hlp = getLocalString (localStrings[loSt++]);
|
||||
lista.replace (lista.find("%s"), 2, hlp);
|
||||
}
|
||||
break;
|
||||
case TREPLACE_NUMBER:
|
||||
case EMessage::REPLACE_NUMBER:
|
||||
lista.replace (lista.find("%d"), 2, std::to_string(numbers[nums++]));
|
||||
break;
|
||||
default:
|
||||
@@ -234,20 +258,18 @@ DLL_LINKAGE std::string MetaString::buildList () const
|
||||
return lista;
|
||||
}
|
||||
|
||||
void MetaString::addCreReplacement(const CreatureID & id, TQuantity count) //adds sing or plural name;
|
||||
void MetaString::replaceCreatureName(const CreatureID & id, TQuantity count) //adds sing or plural name;
|
||||
{
|
||||
if (!count)
|
||||
addReplacement (CRE_PL_NAMES, id); //no creatures - just empty name (eg. defeat Angels)
|
||||
else if (count == 1)
|
||||
addReplacement (CRE_SING_NAMES, id);
|
||||
if (count == 1)
|
||||
replaceLocalString (EMetaText::CRE_SING_NAMES, id);
|
||||
else
|
||||
addReplacement (CRE_PL_NAMES, id);
|
||||
replaceLocalString (EMetaText::CRE_PL_NAMES, id);
|
||||
}
|
||||
|
||||
void MetaString::addReplacement(const CStackBasicDescriptor & stack)
|
||||
void MetaString::replaceCreatureName(const CStackBasicDescriptor & stack)
|
||||
{
|
||||
assert(stack.type); //valid type
|
||||
addCreReplacement(stack.type->getId(), stack.count);
|
||||
replaceCreatureName(stack.type->getId(), stack.count);
|
||||
}
|
||||
|
||||
VCMI_LIB_NAMESPACE_END
|
||||
|
||||
Reference in New Issue
Block a user