mirror of
https://github.com/AxioDL/PrimeWorldEditor.git
synced 2025-12-14 15:46:17 +00:00
CStringTable: Make use of size_t
Plays nicer with standard containers. While we're at it, we can use std::move where applicable.
This commit is contained in:
@@ -77,11 +77,11 @@ static std::pair<const ELanguage*, size_t> GetSupportedLanguages(EGame Game, ERe
|
||||
// Utility function - retrieve the index of a given language
|
||||
static int FindLanguageIndex(const CStringTable* pkInTable, ELanguage InLanguage)
|
||||
{
|
||||
for (uint LanguageIdx = 0; LanguageIdx < pkInTable->NumLanguages(); LanguageIdx++)
|
||||
for (size_t LanguageIdx = 0; LanguageIdx < pkInTable->NumLanguages(); LanguageIdx++)
|
||||
{
|
||||
if (pkInTable->LanguageByIndex(LanguageIdx) == InLanguage)
|
||||
{
|
||||
return LanguageIdx;
|
||||
return static_cast<int>(LanguageIdx);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -89,47 +89,46 @@ static int FindLanguageIndex(const CStringTable* pkInTable, ELanguage InLanguage
|
||||
}
|
||||
|
||||
/** Returns a string given a language/index pair */
|
||||
TString CStringTable::GetString(ELanguage Language, uint StringIndex) const
|
||||
TString CStringTable::GetString(ELanguage Language, size_t StringIndex) const
|
||||
{
|
||||
int LanguageIdx = FindLanguageIndex(this, Language);
|
||||
const int LanguageIdx = FindLanguageIndex(this, Language);
|
||||
|
||||
if (LanguageIdx >= 0 && mLanguages[LanguageIdx].Strings.size() > StringIndex)
|
||||
{
|
||||
return mLanguages[LanguageIdx].Strings[StringIndex].String;
|
||||
}
|
||||
else
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
/** Updates a string for a given language */
|
||||
void CStringTable::SetString(ELanguage Language, uint StringIndex, const TString& kNewString)
|
||||
void CStringTable::SetString(ELanguage Language, size_t StringIndex, TString kNewString)
|
||||
{
|
||||
int LanguageIdx = FindLanguageIndex(this, Language);
|
||||
int EnglishIdx = FindLanguageIndex(this, ELanguage::English);
|
||||
const int LanguageIdx = FindLanguageIndex(this, Language);
|
||||
const int EnglishIdx = FindLanguageIndex(this, ELanguage::English);
|
||||
|
||||
if (LanguageIdx >= 0 && mLanguages[LanguageIdx].Strings.size() > StringIndex)
|
||||
{
|
||||
mLanguages[LanguageIdx].Strings[StringIndex].String = kNewString;
|
||||
mLanguages[LanguageIdx].Strings[StringIndex].IsLocalized =
|
||||
(LanguageIdx == EnglishIdx || kNewString != mLanguages[EnglishIdx].Strings[StringIndex].String);
|
||||
auto& string = mLanguages[LanguageIdx].Strings[StringIndex];
|
||||
|
||||
string.IsLocalized = LanguageIdx == EnglishIdx || kNewString != mLanguages[EnglishIdx].Strings[StringIndex].String;
|
||||
string.String = std::move(kNewString);
|
||||
}
|
||||
}
|
||||
|
||||
/** Updates a string name */
|
||||
void CStringTable::SetStringName(uint StringIndex, const TString& kNewName)
|
||||
void CStringTable::SetStringName(size_t StringIndex, TString kNewName)
|
||||
{
|
||||
// Sanity check - make sure the string index is valid
|
||||
ASSERT( NumStrings() > StringIndex );
|
||||
ASSERT(NumStrings() > StringIndex);
|
||||
|
||||
// Expand the name listing if needed and assign the name
|
||||
if (mStringNames.size() <= StringIndex)
|
||||
{
|
||||
mStringNames.resize( StringIndex + 1 );
|
||||
mStringNames.resize(StringIndex + 1);
|
||||
}
|
||||
|
||||
mStringNames[StringIndex] = kNewName;
|
||||
mStringNames[StringIndex] = std::move(kNewName);
|
||||
|
||||
// Strip empty string names
|
||||
while (mStringNames.back().IsEmpty())
|
||||
@@ -137,37 +136,36 @@ void CStringTable::SetStringName(uint StringIndex, const TString& kNewName)
|
||||
}
|
||||
|
||||
/** Move string to another position in the table */
|
||||
void CStringTable::MoveString(uint StringIndex, uint NewIndex)
|
||||
void CStringTable::MoveString(size_t StringIndex, size_t NewIndex)
|
||||
{
|
||||
ASSERT( NumStrings() > StringIndex );
|
||||
ASSERT( NumStrings() > NewIndex );
|
||||
ASSERT(NumStrings() > StringIndex);
|
||||
ASSERT(NumStrings() > NewIndex);
|
||||
|
||||
if (NewIndex == StringIndex)
|
||||
return;
|
||||
|
||||
// Update string data
|
||||
for (uint LanguageIdx = 0; LanguageIdx < mLanguages.size(); LanguageIdx++)
|
||||
for (SLanguageData& Language : mLanguages)
|
||||
{
|
||||
SLanguageData& Language = mLanguages[LanguageIdx];
|
||||
SStringData String = Language.Strings[StringIndex];
|
||||
|
||||
if (NewIndex > StringIndex)
|
||||
{
|
||||
for (uint i=StringIndex; i<NewIndex; i++)
|
||||
Language.Strings[i] = Language.Strings[i+1];
|
||||
for (size_t i = StringIndex; i < NewIndex; i++)
|
||||
Language.Strings[i] = Language.Strings[i + 1];
|
||||
}
|
||||
else
|
||||
{
|
||||
for (uint i=StringIndex; i>NewIndex; i--)
|
||||
Language.Strings[i] = Language.Strings[i-1];
|
||||
for (size_t i = StringIndex; i > NewIndex; i--)
|
||||
Language.Strings[i] = Language.Strings[i - 1];
|
||||
}
|
||||
|
||||
Language.Strings[NewIndex] = String;
|
||||
Language.Strings[NewIndex] = std::move(String);
|
||||
}
|
||||
|
||||
// Update string name
|
||||
uint MinIndex = Math::Min(StringIndex, NewIndex);
|
||||
uint MaxIndex = Math::Max(StringIndex, NewIndex);
|
||||
const size_t MinIndex = std::min(StringIndex, NewIndex);
|
||||
const size_t MaxIndex = std::max(StringIndex, NewIndex);
|
||||
|
||||
if (MinIndex < mStringNames.size())
|
||||
{
|
||||
@@ -180,15 +178,15 @@ void CStringTable::MoveString(uint StringIndex, uint NewIndex)
|
||||
|
||||
if (NewIndex > StringIndex)
|
||||
{
|
||||
for (uint i=StringIndex; i<NewIndex; i++)
|
||||
mStringNames[i] = mStringNames[i+1];
|
||||
for (size_t i = StringIndex; i < NewIndex; i++)
|
||||
mStringNames[i] = mStringNames[i + 1];
|
||||
}
|
||||
else
|
||||
{
|
||||
for (uint i=StringIndex; i>NewIndex; i--)
|
||||
mStringNames[i] = mStringNames[i-1];
|
||||
for (size_t i = StringIndex; i > NewIndex; i--)
|
||||
mStringNames[i] = mStringNames[i - 1];
|
||||
}
|
||||
mStringNames[NewIndex] = Name;
|
||||
mStringNames[NewIndex] = std::move(Name);
|
||||
|
||||
// Strip empty string names
|
||||
while (mStringNames.back().IsEmpty())
|
||||
@@ -197,37 +195,37 @@ void CStringTable::MoveString(uint StringIndex, uint NewIndex)
|
||||
}
|
||||
|
||||
/** Add a new string to the table */
|
||||
void CStringTable::AddString(uint AtIndex)
|
||||
void CStringTable::AddString(size_t AtIndex)
|
||||
{
|
||||
if (AtIndex < NumStrings())
|
||||
{
|
||||
if (mStringNames.size() > AtIndex)
|
||||
{
|
||||
mStringNames.insert( mStringNames.begin() + AtIndex, 1, "" );
|
||||
mStringNames.insert(mStringNames.begin() + AtIndex, 1, "");
|
||||
}
|
||||
}
|
||||
else
|
||||
AtIndex = NumStrings();
|
||||
|
||||
for (uint LanguageIdx = 0; LanguageIdx < mLanguages.size(); LanguageIdx++)
|
||||
{
|
||||
SLanguageData& Language = mLanguages[LanguageIdx];
|
||||
Language.Strings.insert( Language.Strings.begin() + AtIndex, 1, SStringData() );
|
||||
AtIndex = NumStrings();
|
||||
}
|
||||
|
||||
for (SLanguageData& Language : mLanguages)
|
||||
{
|
||||
Language.Strings.insert(Language.Strings.begin() + AtIndex, 1, SStringData());
|
||||
}
|
||||
}
|
||||
|
||||
/** Remove a string from the table */
|
||||
void CStringTable::RemoveString(uint StringIndex)
|
||||
void CStringTable::RemoveString(size_t StringIndex)
|
||||
{
|
||||
ASSERT( StringIndex < NumStrings() );
|
||||
ASSERT(StringIndex < NumStrings());
|
||||
|
||||
if (mStringNames.size() > StringIndex)
|
||||
mStringNames.erase( mStringNames.begin() + StringIndex );
|
||||
mStringNames.erase(mStringNames.begin() + StringIndex);
|
||||
|
||||
for (uint LanguageIdx = 0; LanguageIdx < mLanguages.size(); LanguageIdx++)
|
||||
for (SLanguageData& Language : mLanguages)
|
||||
{
|
||||
SLanguageData& Language = mLanguages[LanguageIdx];
|
||||
Language.Strings.erase( Language.Strings.begin() + StringIndex );
|
||||
Language.Strings.erase(Language.Strings.begin() + StringIndex);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -235,7 +233,7 @@ void CStringTable::RemoveString(uint StringIndex)
|
||||
void CStringTable::InitializeNewResource()
|
||||
{
|
||||
// Initialize data for whatever languages are supported by our game/region
|
||||
ERegion Region = ( Entry() && Entry()->Project() ? Entry()->Project()->Region() : ERegion::NTSC );
|
||||
const ERegion Region = ( Entry() && Entry()->Project() ? Entry()->Project()->Region() : ERegion::NTSC );
|
||||
const auto [data, dataSize] = GetSupportedLanguages(Game(), Region);
|
||||
mLanguages.resize(dataSize);
|
||||
|
||||
@@ -258,7 +256,7 @@ std::unique_ptr<CDependencyTree> CStringTable::BuildDependencyTree() const
|
||||
{
|
||||
// STRGs can reference FONTs with the &font=; formatting tag and TXTRs with the &image=; tag
|
||||
auto pTree = std::make_unique<CDependencyTree>();
|
||||
EIDLength IDLength = CAssetID::GameIDLength( Game() );
|
||||
EIDLength IDLength = CAssetID::GameIDLength(Game());
|
||||
|
||||
for (const SLanguageData& language : mLanguages)
|
||||
{
|
||||
@@ -276,13 +274,15 @@ std::unique_ptr<CDependencyTree> CStringTable::BuildDependencyTree() const
|
||||
}
|
||||
|
||||
// Get tag name and parameters
|
||||
int NameEnd = kString.IndexOf('=', TagIdx);
|
||||
int TagEnd = kString.IndexOf(';', TagIdx);
|
||||
if (NameEnd == -1 || TagEnd == -1) continue;
|
||||
const int NameEnd = kString.IndexOf('=', TagIdx);
|
||||
const int TagEnd = kString.IndexOf(';', TagIdx);
|
||||
if (NameEnd == -1 || TagEnd == -1)
|
||||
continue;
|
||||
|
||||
TString TagName = kString.SubString(TagIdx + 1, NameEnd - TagIdx - 1);
|
||||
const TString TagName = kString.SubString(TagIdx + 1, NameEnd - TagIdx - 1);
|
||||
TString ParamString = kString.SubString(NameEnd + 1, TagEnd - NameEnd - 1);
|
||||
if (ParamString.IsEmpty()) continue;
|
||||
if (ParamString.IsEmpty())
|
||||
continue;
|
||||
|
||||
// Font
|
||||
if (TagName == "font")
|
||||
@@ -295,9 +295,8 @@ std::unique_ptr<CDependencyTree> CStringTable::BuildDependencyTree() const
|
||||
}
|
||||
|
||||
ASSERT(ParamString.Size() == IDLength * 2);
|
||||
pTree->AddDependency( CAssetID::FromString(ParamString) );
|
||||
pTree->AddDependency(CAssetID::FromString(ParamString));
|
||||
}
|
||||
|
||||
// Image
|
||||
else if (TagName == "image")
|
||||
{
|
||||
@@ -307,20 +306,25 @@ std::unique_ptr<CDependencyTree> CStringTable::BuildDependencyTree() const
|
||||
uint TexturesStart = 0;
|
||||
|
||||
if (ImageType == "A")
|
||||
{
|
||||
TexturesStart = 2;
|
||||
|
||||
}
|
||||
else if (ImageType == "SI")
|
||||
{
|
||||
TexturesStart = 3;
|
||||
|
||||
}
|
||||
else if (ImageType == "SA")
|
||||
{
|
||||
TexturesStart = 4;
|
||||
|
||||
}
|
||||
else if (ImageType == "B")
|
||||
{
|
||||
TexturesStart = 2;
|
||||
|
||||
}
|
||||
else if (ImageType.IsHexString(false, static_cast<int>(IDLength) * 2))
|
||||
{
|
||||
TexturesStart = 0;
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
errorf("Unrecognized image type: %s", *ImageType);
|
||||
@@ -330,7 +334,7 @@ std::unique_ptr<CDependencyTree> CStringTable::BuildDependencyTree() const
|
||||
// Load texture IDs
|
||||
TStringList::iterator Iter = Params.begin();
|
||||
|
||||
for (uint ParamIdx = 0; ParamIdx < Params.size(); ParamIdx++, Iter++)
|
||||
for (uint ParamIdx = 0; ParamIdx < Params.size(); ParamIdx++, ++Iter)
|
||||
{
|
||||
if (ParamIdx >= TexturesStart)
|
||||
{
|
||||
@@ -365,8 +369,9 @@ TString CStringTable::StripFormatting(const TString& kInString)
|
||||
if (Out[CharIdx] == '&')
|
||||
{
|
||||
if (TagStart == -1)
|
||||
{
|
||||
TagStart = CharIdx;
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
Out.Remove(TagStart, 1);
|
||||
@@ -374,11 +379,10 @@ TString CStringTable::StripFormatting(const TString& kInString)
|
||||
CharIdx--;
|
||||
}
|
||||
}
|
||||
|
||||
else if (TagStart != -1 && Out[CharIdx] == ';')
|
||||
{
|
||||
int TagEnd = CharIdx + 1;
|
||||
int TagLen = TagEnd - TagStart;
|
||||
const int TagEnd = CharIdx + 1;
|
||||
const int TagLen = TagEnd - TagStart;
|
||||
Out.Remove(TagStart, TagLen);
|
||||
CharIdx = TagStart - 1;
|
||||
TagStart = -1;
|
||||
|
||||
@@ -53,34 +53,34 @@ public:
|
||||
explicit CStringTable(CResourceEntry *pEntry = nullptr) : CResource(pEntry) {}
|
||||
|
||||
/** Returns the number of languages in the table */
|
||||
uint NumLanguages() const { return mLanguages.size(); }
|
||||
size_t NumLanguages() const { return mLanguages.size(); }
|
||||
|
||||
/** Returns the number of strings in the table */
|
||||
uint NumStrings() const { return mLanguages.empty() ? 0 : mLanguages[0].Strings.size(); }
|
||||
size_t NumStrings() const { return mLanguages.empty() ? 0 : mLanguages[0].Strings.size(); }
|
||||
|
||||
/** Returns languages used by index */
|
||||
ELanguage LanguageByIndex(uint Index) const { return mLanguages.size() > Index ? mLanguages[Index].Language : ELanguage::Invalid; }
|
||||
ELanguage LanguageByIndex(size_t Index) const { return mLanguages.size() > Index ? mLanguages[Index].Language : ELanguage::Invalid; }
|
||||
|
||||
/** Returns the string name by string index. May be blank if the string at the requested index is unnamed */
|
||||
TString StringNameByIndex(uint Index) const { return mStringNames.size() > Index ? mStringNames[Index] : ""; }
|
||||
TString StringNameByIndex(size_t Index) const { return mStringNames.size() > Index ? mStringNames[Index] : ""; }
|
||||
|
||||
/** Returns a string given a language/index pair */
|
||||
TString GetString(ELanguage Language, uint StringIndex) const;
|
||||
TString GetString(ELanguage Language, size_t StringIndex) const;
|
||||
|
||||
/** Updates a string for a given language */
|
||||
void SetString(ELanguage Language, uint StringIndex, const TString& kNewString);
|
||||
void SetString(ELanguage Language, size_t StringIndex, TString kNewString);
|
||||
|
||||
/** Updates a string name */
|
||||
void SetStringName(uint StringIndex, const TString& kNewName);
|
||||
void SetStringName(size_t StringIndex, TString kNewName);
|
||||
|
||||
/** Move string to another position in the table */
|
||||
void MoveString(uint StringIndex, uint NewIndex);
|
||||
void MoveString(size_t StringIndex, size_t NewIndex);
|
||||
|
||||
/** Add a new string to the table */
|
||||
void AddString(uint AtIndex);
|
||||
void AddString(size_t AtIndex);
|
||||
|
||||
/** Remove a string from the table */
|
||||
void RemoveString(uint StringIndex);
|
||||
void RemoveString(size_t StringIndex);
|
||||
|
||||
/** Initialize new resource data */
|
||||
void InitializeNewResource() override;
|
||||
|
||||
Reference in New Issue
Block a user