CGameTemplate: Collapse loops into ranged for where applicable

This commit is contained in:
Lioncash 2020-06-14 18:48:33 -04:00
parent d9bb255518
commit 15708b8624
1 changed files with 60 additions and 64 deletions

View File

@ -28,32 +28,31 @@ void CGameTemplate::Load(const TString& kFilePath)
// Load all sub-templates // Load all sub-templates
const TString gkGameRoot = GetGameDirectory(); const TString gkGameRoot = GetGameDirectory();
for (auto Iter = mScriptTemplates.begin(); Iter != mScriptTemplates.end(); Iter++) for (auto& [id, path] : mScriptTemplates)
{ {
SScriptTemplatePath& ScriptPath = Iter->second; TString AbsPath = gkGameRoot + path.Path;
TString AbsPath = gkGameRoot + ScriptPath.Path; path.pTemplate = std::make_shared<CScriptTemplate>(this, id, AbsPath);
ScriptPath.pTemplate = std::make_shared<CScriptTemplate>(this, Iter->first, AbsPath);
} }
for (auto Iter = mPropertyTemplates.begin(); Iter != mPropertyTemplates.end(); Iter++) for (auto& entry : mPropertyTemplates)
{ {
// For properties, remember that property archetypes can reference other archetypes which // For properties, remember that property archetypes can reference other archetypes which
// may not be loaded yet.. so if this happens, the referenced property will be loaded, // may not be loaded yet.. so if this happens, the referenced property will be loaded,
// meaning property templates can be loaded out of order, so we need to make sure // meaning property templates can be loaded out of order, so we need to make sure
// that we don't load any template more than once. // that we don't load any template more than once.
SPropertyTemplatePath& PropertyPath = Iter->second; SPropertyTemplatePath& PropertyPath = entry.second;
if (!PropertyPath.pTemplate) if (!PropertyPath.pTemplate)
{ {
Internal_LoadPropertyTemplate(Iter->second); Internal_LoadPropertyTemplate(PropertyPath);
} }
} }
for (auto Iter = mMiscTemplates.begin(); Iter != mMiscTemplates.end(); Iter++) for (auto& entry : mMiscTemplates)
{ {
SScriptTemplatePath& MiscPath = Iter->second; SScriptTemplatePath& MiscPath = entry.second;
TString AbsPath = gkGameRoot + MiscPath.Path; TString AbsPath = gkGameRoot + MiscPath.Path;
MiscPath.pTemplate = std::make_shared<CScriptTemplate>(this, -1, AbsPath); MiscPath.pTemplate = std::make_shared<CScriptTemplate>(this, UINT32_MAX, AbsPath);
} }
} }
@ -83,7 +82,7 @@ void CGameTemplate::Internal_LoadPropertyTemplate(SPropertyTemplatePath& Path)
Path.pTemplate->Initialize(nullptr, nullptr, 0); Path.pTemplate->Initialize(nullptr, nullptr, 0);
} }
void CGameTemplate::SaveGameTemplates(bool ForceAll /*= false*/) void CGameTemplate::SaveGameTemplates(bool ForceAll)
{ {
const TString kGameDir = GetGameDirectory(); const TString kGameDir = GetGameDirectory();
@ -92,26 +91,26 @@ void CGameTemplate::SaveGameTemplates(bool ForceAll /*= false*/)
Save(); Save();
} }
for (auto Iter = mScriptTemplates.begin(); Iter != mScriptTemplates.end(); Iter++) for (auto& entry : mScriptTemplates)
{ {
SScriptTemplatePath& Path = Iter->second; SScriptTemplatePath& Path = entry.second;
if( Path.pTemplate ) if (Path.pTemplate)
{ {
Path.pTemplate->Save(ForceAll); Path.pTemplate->Save(ForceAll);
} }
} }
for (auto Iter = mPropertyTemplates.begin(); Iter != mPropertyTemplates.end(); Iter++) for (auto& entry : mPropertyTemplates)
{ {
SPropertyTemplatePath& Path = Iter->second; SPropertyTemplatePath& Path = entry.second;
if( Path.pTemplate ) if (Path.pTemplate)
{ {
if( ForceAll || Path.pTemplate->IsDirty() ) if (ForceAll || Path.pTemplate->IsDirty())
{ {
const TString kOutPath = kGameDir + Path.Path; const TString kOutPath = kGameDir + Path.Path;
FileUtil::MakeDirectory( kOutPath.GetFileDirectory() ); FileUtil::MakeDirectory(kOutPath.GetFileDirectory());
debugf("Saving property template: %s", *kOutPath); debugf("Saving property template: %s", *kOutPath);
CXMLWriter Writer(kOutPath, "PropertyTemplate", 0, Game()); CXMLWriter Writer(kOutPath, "PropertyTemplate", 0, Game());
@ -123,11 +122,11 @@ void CGameTemplate::SaveGameTemplates(bool ForceAll /*= false*/)
} }
} }
for (auto Iter = mMiscTemplates.begin(); Iter != mMiscTemplates.end(); Iter++) for (auto& entry : mMiscTemplates)
{ {
SScriptTemplatePath& Path = Iter->second; SScriptTemplatePath& Path = entry.second;
if( Path.pTemplate ) if (Path.pTemplate)
{ {
Path.pTemplate->Save(ForceAll); Path.pTemplate->Save(ForceAll);
} }
@ -136,17 +135,17 @@ void CGameTemplate::SaveGameTemplates(bool ForceAll /*= false*/)
uint32 CGameTemplate::GameVersion(TString VersionName) uint32 CGameTemplate::GameVersion(TString VersionName)
{ {
return -1; return UINT32_MAX;
} }
CScriptTemplate* CGameTemplate::TemplateByID(uint32 ObjectID) CScriptTemplate* CGameTemplate::TemplateByID(uint32 ObjectID)
{ {
auto it = mScriptTemplates.find(ObjectID); const auto it = mScriptTemplates.find(ObjectID);
if (it != mScriptTemplates.end()) if (it == mScriptTemplates.end())
return it->second.pTemplate.get();
else
return nullptr; return nullptr;
return it->second.pTemplate.get();
} }
CScriptTemplate* CGameTemplate::TemplateByID(const CFourCC& ObjectID) CScriptTemplate* CGameTemplate::TemplateByID(const CFourCC& ObjectID)
@ -156,18 +155,18 @@ CScriptTemplate* CGameTemplate::TemplateByID(const CFourCC& ObjectID)
CScriptTemplate* CGameTemplate::TemplateByIndex(uint32 Index) CScriptTemplate* CGameTemplate::TemplateByIndex(uint32 Index)
{ {
auto it = mScriptTemplates.begin(); const auto it = mScriptTemplates.begin();
return (std::next(it, Index))->second.pTemplate.get(); return (std::next(it, Index))->second.pTemplate.get();
} }
SState CGameTemplate::StateByID(uint32 StateID) SState CGameTemplate::StateByID(uint32 StateID)
{ {
auto Iter = mStates.find(StateID); const auto iter = mStates.find(StateID);
if (Iter != mStates.end()) if (iter == mStates.cend())
return SState(Iter->first, Iter->second); return SState(UINT32_MAX, "Invalid");
else
return SState(-1, "Invalid"); return SState(iter->first, iter->second);
} }
SState CGameTemplate::StateByID(const CFourCC& State) SState CGameTemplate::StateByID(const CFourCC& State)
@ -184,12 +183,12 @@ SState CGameTemplate::StateByIndex(uint32 Index)
SMessage CGameTemplate::MessageByID(uint32 MessageID) SMessage CGameTemplate::MessageByID(uint32 MessageID)
{ {
auto Iter = mMessages.find(MessageID); const auto iter = mMessages.find(MessageID);
if (Iter != mMessages.end()) if (iter == mMessages.cend())
return SMessage(Iter->first, Iter->second); return SMessage(UINT32_MAX, "Invalid");
else
return SMessage(-1, "Invalid"); return SMessage(iter->first, iter->second);
} }
SMessage CGameTemplate::MessageByID(const CFourCC& MessageID) SMessage CGameTemplate::MessageByID(const CFourCC& MessageID)
@ -206,7 +205,7 @@ SMessage CGameTemplate::MessageByIndex(uint32 Index)
IProperty* CGameTemplate::FindPropertyArchetype(const TString& kTypeName) IProperty* CGameTemplate::FindPropertyArchetype(const TString& kTypeName)
{ {
auto Iter = mPropertyTemplates.find(kTypeName); const auto Iter = mPropertyTemplates.find(kTypeName);
if (Iter == mPropertyTemplates.end()) if (Iter == mPropertyTemplates.end())
{ {
@ -228,43 +227,43 @@ IProperty* CGameTemplate::FindPropertyArchetype(const TString& kTypeName)
TString CGameTemplate::GetPropertyArchetypeFilePath(const TString& kTypeName) TString CGameTemplate::GetPropertyArchetypeFilePath(const TString& kTypeName)
{ {
auto Iter = mPropertyTemplates.find(kTypeName); const auto it = mPropertyTemplates.find(kTypeName);
ASSERT(Iter != mPropertyTemplates.end()); ASSERT(it != mPropertyTemplates.cend());
return GetGameDirectory() + Iter->second.Path; return GetGameDirectory() + it->second.Path;
} }
bool CGameTemplate::RenamePropertyArchetype(const TString& kTypeName, const TString& kNewTypeName) bool CGameTemplate::RenamePropertyArchetype(const TString& kTypeName, const TString& kNewTypeName)
{ {
if( kTypeName != kNewTypeName ) if (kTypeName != kNewTypeName)
{ {
// Fetch the property that we are going to be renaming. // Fetch the property that we are going to be renaming.
// Validate type, too, because we only support renaming struct archetypes at the moment // Validate type, too, because we only support renaming struct archetypes at the moment
auto Iter = mPropertyTemplates.find(kTypeName); const auto Iter = mPropertyTemplates.find(kTypeName);
if( Iter != mPropertyTemplates.end() ) if (Iter != mPropertyTemplates.cend())
{ {
SPropertyTemplatePath& Path = Iter->second; SPropertyTemplatePath& Path = Iter->second;
IProperty* pArchetype = Path.pTemplate.get(); IProperty* pArchetype = Path.pTemplate.get();
if( pArchetype ) if (pArchetype)
{ {
// Attempt to move the XML to the new location. // Attempt to move the XML to the new location.
TString OldPath = GetGameDirectory() + Path.Path; const TString OldPath = GetGameDirectory() + Path.Path;
TString NewPath = OldPath.GetFileDirectory() + kNewTypeName + ".xml"; const TString NewPath = OldPath.GetFileDirectory() + kNewTypeName + ".xml";
if( FileUtil::MoveFile(OldPath, NewPath) ) if (FileUtil::MoveFile(OldPath, NewPath))
{ {
// Update the name in the game template's internal mapping // Update the name in the game template's internal mapping
TString RelativePath = FileUtil::MakeRelative( NewPath, GetGameDirectory() ); const TString RelativePath = FileUtil::MakeRelative(NewPath, GetGameDirectory());
auto MapNode = mPropertyTemplates.extract(Iter); auto MapNode = mPropertyTemplates.extract(Iter);
MapNode.key() = kNewTypeName; MapNode.key() = kNewTypeName;
MapNode.mapped().Path = RelativePath; MapNode.mapped().Path = RelativePath;
mPropertyTemplates.insert( std::move(MapNode) ); mPropertyTemplates.insert(std::move(MapNode));
mDirty = true; mDirty = true;
// Renaming the archetype will handle updating the actual type name, and // Renaming the archetype will handle updating the actual type name, and
// dirtying/invalidating property sub-instances. // dirtying/invalidating property sub-instances.
TString OldTypeName = pArchetype->HashableTypeName(); const TString OldTypeName = pArchetype->HashableTypeName();
pArchetype->SetName(kNewTypeName); pArchetype->SetName(kNewTypeName);
// For MP2 and up, we also need to update the type names stored in the property map. // For MP2 and up, we also need to update the type names stored in the property map.
@ -280,13 +279,11 @@ bool CGameTemplate::RenamePropertyArchetype(const TString& kTypeName, const TStr
std::list<IProperty*> SubInstances; std::list<IProperty*> SubInstances;
pArchetype->GatherAllSubInstances(SubInstances, true); pArchetype->GatherAllSubInstances(SubInstances, true);
for (auto Iter = SubInstances.begin(); Iter != SubInstances.end(); Iter++) for (auto* property : SubInstances)
{ {
IProperty* pProperty = *Iter; if (property->Name() == kTypeName)
if (pProperty->Name() == kTypeName)
{ {
pProperty->SetName(kNewTypeName); property->SetName(kNewTypeName);
} }
} }
} }
@ -302,17 +299,16 @@ bool CGameTemplate::RenamePropertyArchetype(const TString& kTypeName, const TStr
CScriptTemplate* CGameTemplate::FindMiscTemplate(const TString& kTemplateName) CScriptTemplate* CGameTemplate::FindMiscTemplate(const TString& kTemplateName)
{ {
auto Iter = mMiscTemplates.find(kTemplateName); const auto Iter = mMiscTemplates.find(kTemplateName);
if (Iter == mMiscTemplates.end()) if (Iter == mMiscTemplates.cend())
{ {
return nullptr; return nullptr;
} }
else
{
SScriptTemplatePath& Path = Iter->second; SScriptTemplatePath& Path = Iter->second;
return Path.pTemplate.get(); return Path.pTemplate.get();
}
} }
TString CGameTemplate::GetGameDirectory() const TString CGameTemplate::GetGameDirectory() const