CStringLoader: Make use of unsigned stream utilities

This commit is contained in:
Lioncash 2020-06-20 02:35:19 -04:00
parent f0b82908d5
commit 4672b75ec1
1 changed files with 55 additions and 52 deletions

View File

@ -9,19 +9,19 @@ void CStringLoader::LoadPrimeDemoSTRG(IInputStream& STRG)
mpStringTable->mLanguages.resize(1);
CStringTable::SLanguageData& Language = mpStringTable->mLanguages[0];
Language.Language = ELanguage::English;
uint TableStart = STRG.Tell();
const uint32 TableStart = STRG.Tell();
// Header
uint NumStrings = STRG.ReadLong();
const uint32 NumStrings = STRG.ReadULong();
Language.Strings.resize(NumStrings);
// String offsets (yeah, that wasn't much of a header)
std::vector<uint> StringOffsets(NumStrings);
for (uint32 OffsetIdx = 0; OffsetIdx < NumStrings; OffsetIdx++)
StringOffsets[OffsetIdx] = STRG.ReadLong();
std::vector<uint32> StringOffsets(NumStrings);
for (auto& offset : StringOffsets)
offset = STRG.ReadULong();
// Strings
for (uint StringIdx = 0; StringIdx < NumStrings; StringIdx++)
for (size_t StringIdx = 0; StringIdx < NumStrings; StringIdx++)
{
STRG.GoTo( TableStart + StringOffsets[StringIdx] );
Language.Strings[StringIdx].String = STRG.Read16String().ToUTF8();
@ -32,17 +32,17 @@ void CStringLoader::LoadPrimeSTRG(IInputStream& STRG)
{
// This function starts at 0x8 in the file, after magic/version
// Header
uint NumLanguages = STRG.ReadLong();
uint NumStrings = STRG.ReadLong();
const uint32 NumLanguages = STRG.ReadULong();
const uint32 NumStrings = STRG.ReadULong();
// Language definitions
mpStringTable->mLanguages.resize(NumLanguages);
std::vector<uint> LanguageOffsets(NumLanguages);
std::vector<uint32> LanguageOffsets(NumLanguages);
int EnglishIdx = -1;
for (uint LanguageIdx = 0; LanguageIdx < NumLanguages; LanguageIdx++)
for (uint32 LanguageIdx = 0; LanguageIdx < NumLanguages; LanguageIdx++)
{
mpStringTable->mLanguages[LanguageIdx].Language = (ELanguage) STRG.ReadFourCC();
mpStringTable->mLanguages[LanguageIdx].Language = static_cast<ELanguage>(STRG.ReadFourCC());
LanguageOffsets[LanguageIdx] = STRG.ReadLong();
// Skip strings size in MP2
@ -53,7 +53,7 @@ void CStringLoader::LoadPrimeSTRG(IInputStream& STRG)
if (mpStringTable->mLanguages[LanguageIdx].Language == ELanguage::English)
{
EnglishIdx = (int) LanguageIdx;
EnglishIdx = static_cast<int>(LanguageIdx);
}
}
ASSERT(EnglishIdx != -1);
@ -65,10 +65,10 @@ void CStringLoader::LoadPrimeSTRG(IInputStream& STRG)
}
// Strings
uint StringsStart = STRG.Tell();
const uint32 StringsStart = STRG.Tell();
for (uint32 LanguageIdx = 0; LanguageIdx < NumLanguages; LanguageIdx++)
{
STRG.GoTo( StringsStart + LanguageOffsets[LanguageIdx] );
STRG.GoTo(StringsStart + LanguageOffsets[LanguageIdx]);
// Skip strings size in MP1
if (mVersion == EGame::Prime)
@ -80,31 +80,31 @@ void CStringLoader::LoadPrimeSTRG(IInputStream& STRG)
Language.Strings.resize(NumStrings);
// Offsets
uint LanguageStart = STRG.Tell();
std::vector<uint> StringOffsets(NumStrings);
const uint32 LanguageStart = STRG.Tell();
std::vector<uint32> StringOffsets(NumStrings);
for (uint StringIdx = 0; StringIdx < NumStrings; StringIdx++)
for (auto& offset : StringOffsets)
{
StringOffsets[StringIdx] = LanguageStart + STRG.ReadLong();
offset = LanguageStart + STRG.ReadULong();
}
// The actual strings
for (uint StringIdx = 0; StringIdx < NumStrings; StringIdx++)
for (uint32 StringIdx = 0; StringIdx < NumStrings; StringIdx++)
{
STRG.GoTo( StringOffsets[StringIdx] );
STRG.GoTo(StringOffsets[StringIdx]);
TString String = STRG.Read16String().ToUTF8();
Language.Strings[StringIdx].String = String;
Language.Strings[StringIdx].String = std::move(String);
}
}
// Set "localized" flags on strings
const CStringTable::SLanguageData& kEnglishData = mpStringTable->mLanguages[EnglishIdx];
for (uint LanguageIdx = 0; LanguageIdx < NumLanguages; LanguageIdx++)
for (uint32 LanguageIdx = 0; LanguageIdx < NumLanguages; LanguageIdx++)
{
CStringTable::SLanguageData& LanguageData = mpStringTable->mLanguages[LanguageIdx];
for (uint StringIdx = 0; StringIdx < NumStrings; StringIdx++)
for (uint32 StringIdx = 0; StringIdx < NumStrings; StringIdx++)
{
// Flag the string as localized if it is different than the English
// version of the same string.
@ -119,50 +119,50 @@ void CStringLoader::LoadCorruptionSTRG(IInputStream& STRG)
{
// This function starts at 0x8 in the file, after magic/version
// Header
uint NumLanguages = STRG.ReadLong();
uint NumStrings = STRG.ReadLong();
const uint32 NumLanguages = STRG.ReadULong();
const uint32 NumStrings = STRG.ReadULong();
// String names
LoadNameTable(STRG);
// Language definitions
mpStringTable->mLanguages.resize(NumLanguages);
std::vector< std::vector<uint> > LanguageOffsets(NumLanguages);
std::vector<std::vector<uint>> LanguageOffsets(NumLanguages);
int EnglishIdx = -1;
for (uint LanguageIdx = 0; LanguageIdx < NumLanguages; LanguageIdx++)
for (uint32 LanguageIdx = 0; LanguageIdx < NumLanguages; LanguageIdx++)
{
mpStringTable->mLanguages[LanguageIdx].Language = (ELanguage) STRG.ReadFourCC();
mpStringTable->mLanguages[LanguageIdx].Language = static_cast<ELanguage>(STRG.ReadFourCC());
if (mpStringTable->mLanguages[LanguageIdx].Language == ELanguage::English)
{
EnglishIdx = (int) LanguageIdx;
EnglishIdx = static_cast<int>(LanguageIdx);
}
}
ASSERT(EnglishIdx != -1);
for (uint LanguageIdx = 0; LanguageIdx < NumLanguages; LanguageIdx++)
for (uint32 LanguageIdx = 0; LanguageIdx < NumLanguages; LanguageIdx++)
{
LanguageOffsets[LanguageIdx].resize(NumStrings);
STRG.Skip(4); // Skipping total string size
for (uint StringIdx = 0; StringIdx < NumStrings; StringIdx++)
for (uint32 StringIdx = 0; StringIdx < NumStrings; StringIdx++)
{
LanguageOffsets[LanguageIdx][StringIdx] = STRG.ReadLong();
}
}
// Strings
uint StringsStart = STRG.Tell();
const uint32 StringsStart = STRG.Tell();
for (uint LanguageIdx = 0; LanguageIdx < NumLanguages; LanguageIdx++)
for (uint32 LanguageIdx = 0; LanguageIdx < NumLanguages; LanguageIdx++)
{
CStringTable::SLanguageData& Language = mpStringTable->mLanguages[LanguageIdx];
Language.Strings.resize(NumStrings);
for (uint StringIdx = 0; StringIdx < NumStrings; StringIdx++)
for (uint32 StringIdx = 0; StringIdx < NumStrings; StringIdx++)
{
STRG.GoTo( StringsStart + LanguageOffsets[LanguageIdx][StringIdx] );
STRG.GoTo(StringsStart + LanguageOffsets[LanguageIdx][StringIdx]);
STRG.Skip(4); // Skipping string size
Language.Strings[StringIdx].String = STRG.ReadString();
@ -176,14 +176,15 @@ void CStringLoader::LoadCorruptionSTRG(IInputStream& STRG)
void CStringLoader::LoadNameTable(IInputStream& STRG)
{
// Name table header
uint NameCount = STRG.ReadLong();
uint NameTableSize = STRG.ReadLong();
uint NameTableStart = STRG.Tell();
uint NameTableEnd = NameTableStart + NameTableSize;
const uint32 NameCount = STRG.ReadULong();
const uint32 NameTableSize = STRG.ReadULong();
const uint32 NameTableStart = STRG.Tell();
const uint32 NameTableEnd = NameTableStart + NameTableSize;
// Name definitions
struct SNameDef {
uint NameOffset, StringIndex;
uint32 NameOffset;
uint32 StringIndex;
};
std::vector<SNameDef> NameDefs(NameCount);
@ -191,17 +192,17 @@ void CStringLoader::LoadNameTable(IInputStream& STRG)
// Note that it is possible that not every string in the table has a name.
int MaxIndex = -1;
for (uint NameIdx = 0; NameIdx < NameCount; NameIdx++)
for (size_t NameIdx = 0; NameIdx < NameCount; NameIdx++)
{
NameDefs[NameIdx].NameOffset = STRG.ReadLong() + NameTableStart;
NameDefs[NameIdx].StringIndex = STRG.ReadLong();
MaxIndex = Math::Max(MaxIndex, (int) NameDefs[NameIdx].StringIndex);
NameDefs[NameIdx].NameOffset = STRG.ReadULong() + NameTableStart;
NameDefs[NameIdx].StringIndex = STRG.ReadULong();
MaxIndex = Math::Max(MaxIndex, static_cast<int>(NameDefs[NameIdx].StringIndex));
}
// Name strings
mpStringTable->mStringNames.resize(MaxIndex + 1);
for (uint NameIdx = 0; NameIdx < NameCount; NameIdx++)
for (size_t NameIdx = 0; NameIdx < NameCount; NameIdx++)
{
SNameDef& NameDef = NameDefs[NameIdx];
STRG.GoTo(NameDef.NameOffset);
@ -217,7 +218,7 @@ std::unique_ptr<CStringTable> CStringLoader::LoadSTRG(IInputStream& STRG, CResou
return nullptr;
// Verify that this is a valid STRG
uint Magic = STRG.ReadLong();
const uint32 Magic = STRG.ReadULong();
EGame Version = EGame::Invalid;
if (Magic != 0x87654321)
@ -225,7 +226,7 @@ std::unique_ptr<CStringTable> CStringLoader::LoadSTRG(IInputStream& STRG, CResou
// Check for MP1 Demo STRG format - no magic/version; the first value is actually the filesize
// so the best I can do is verify the first value actually points to the end of the file.
// The file can have up to 31 padding bytes at the end so we account for that
if (Magic <= (uint) STRG.Size() && Magic > STRG.Size() - 32)
if (Magic <= static_cast<uint32>(STRG.Size()) && Magic > STRG.Size() - 32)
{
Version = EGame::PrimeDemo;
}
@ -237,10 +238,9 @@ std::unique_ptr<CStringTable> CStringLoader::LoadSTRG(IInputStream& STRG, CResou
return nullptr;
}
}
else
{
uint FileVersion = STRG.ReadLong();
const uint32 FileVersion = STRG.ReadULong();
Version = GetFormatVersion(FileVersion);
if (Version == EGame::Invalid)
@ -256,9 +256,12 @@ std::unique_ptr<CStringTable> CStringLoader::LoadSTRG(IInputStream& STRG, CResou
Loader.mpStringTable = ptr.get();
Loader.mVersion = Version;
if (Version == EGame::PrimeDemo) Loader.LoadPrimeDemoSTRG(STRG);
else if (Version < EGame::Corruption) Loader.LoadPrimeSTRG(STRG);
else Loader.LoadCorruptionSTRG(STRG);
if (Version == EGame::PrimeDemo)
Loader.LoadPrimeDemoSTRG(STRG);
else if (Version < EGame::Corruption)
Loader.LoadPrimeSTRG(STRG);
else
Loader.LoadCorruptionSTRG(STRG);
return ptr;
}