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