From 659b8a43d342dfe48fafb4b18648399776bddf5d Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sat, 14 Mar 2020 19:59:58 -0400 Subject: [PATCH] CGameOptions: Make use of std::string_view where applicable Enforces the use of valid strings in the interface. Also reduces c_str() noise a little bit. --- Runtime/CGameHintInfo.cpp | 8 +++--- Runtime/CGameHintInfo.hpp | 2 +- Runtime/CGameOptions.cpp | 34 +++++++++++++++--------- Runtime/CGameOptions.hpp | 7 ++--- Runtime/World/CScriptSpecialFunction.cpp | 13 ++++----- 5 files changed, 37 insertions(+), 27 deletions(-) diff --git a/Runtime/CGameHintInfo.cpp b/Runtime/CGameHintInfo.cpp index 9544f593c..6b2fded69 100644 --- a/Runtime/CGameHintInfo.cpp +++ b/Runtime/CGameHintInfo.cpp @@ -31,12 +31,12 @@ CGameHintInfo::SHintLocation::SHintLocation(CInputStream& in, s32) , x8_areaId(in.readUint32Big()) , xc_stringId(in.readUint32Big()) {} -int CGameHintInfo::FindHintIndex(const char* str) { +int CGameHintInfo::FindHintIndex(std::string_view str) { const std::vector& gameHints = g_MemoryCardSys->GetHints(); - const auto& it = std::find_if(gameHints.begin(), gameHints.end(), - [&str](const CGameHint& gh) -> bool { return gh.GetName() == str; }); + const auto it = + std::find_if(gameHints.cbegin(), gameHints.cend(), [&str](const CGameHint& gh) { return gh.GetName() == str; }); - return (it != gameHints.end() ? it - gameHints.begin() : -1); + return it != gameHints.cend() ? it - gameHints.cbegin() : -1; } CFactoryFnReturn FHintFactory(const SObjectTag&, CInputStream& in, const CVParamTransfer, CObjectReference*) { diff --git a/Runtime/CGameHintInfo.hpp b/Runtime/CGameHintInfo.hpp index 7b0e75ee3..2c530cc99 100644 --- a/Runtime/CGameHintInfo.hpp +++ b/Runtime/CGameHintInfo.hpp @@ -42,7 +42,7 @@ private: public: CGameHintInfo(CInputStream&, s32); const std::vector& GetHints() const { return x0_hints; } - static int FindHintIndex(const char* str); + static int FindHintIndex(std::string_view str); }; CFactoryFnReturn FHintFactory(const SObjectTag&, CInputStream&, const CVParamTransfer, CObjectReference*); diff --git a/Runtime/CGameOptions.cpp b/Runtime/CGameOptions.cpp index bd6dece7c..fb9a7f6ed 100644 --- a/Runtime/CGameOptions.cpp +++ b/Runtime/CGameOptions.cpp @@ -578,43 +578,51 @@ const CHintOptions::SHintState* CHintOptions::GetCurrentDisplayedHint() const { return nullptr; } -void CHintOptions::DelayHint(const char* name) { - int idx = CGameHintInfo::FindHintIndex(name); - if (idx == -1) +void CHintOptions::DelayHint(std::string_view name) { + const int idx = CGameHintInfo::FindHintIndex(name); + if (idx == -1) { return; + } - if (x10_nextHintIdx == idx) - for (SHintState& state : x0_hintStates) + if (x10_nextHintIdx == idx) { + for (SHintState& state : x0_hintStates) { state.x4_time += 60.f; + } + } x0_hintStates[idx].x0_state = EHintState::Delayed; } -void CHintOptions::ActivateImmediateHintTimer(const char* name) { - int idx = CGameHintInfo::FindHintIndex(name); - if (idx == -1) +void CHintOptions::ActivateImmediateHintTimer(std::string_view name) { + const int idx = CGameHintInfo::FindHintIndex(name); + if (idx == -1) { return; + } SHintState& hintState = x0_hintStates[idx]; const CGameHintInfo::CGameHint& hint = g_MemoryCardSys->GetHints()[idx]; - if (hintState.x0_state != EHintState::Zero) + if (hintState.x0_state != EHintState::Zero) { return; + } hintState.x0_state = EHintState::Waiting; hintState.x4_time = hint.GetImmediateTime(); } -void CHintOptions::ActivateContinueDelayHintTimer(const char* name) { +void CHintOptions::ActivateContinueDelayHintTimer(std::string_view name) { int idx = x10_nextHintIdx; - if (idx != 0) + if (idx != 0) { idx = CGameHintInfo::FindHintIndex(name); - if (idx == -1) + } + if (idx == -1) { return; + } SHintState& hintState = x0_hintStates[idx]; const CGameHintInfo::CGameHint& hint = g_MemoryCardSys->GetHints()[idx]; - if (hintState.x0_state != EHintState::Displaying) + if (hintState.x0_state != EHintState::Displaying) { return; + } hintState.x4_time = hint.GetTextTime(); } diff --git a/Runtime/CGameOptions.hpp b/Runtime/CGameOptions.hpp index cc290db78..acc3bec0c 100644 --- a/Runtime/CGameOptions.hpp +++ b/Runtime/CGameOptions.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include "Runtime/CSaveWorld.hpp" @@ -205,9 +206,9 @@ public: void SetNextHintTime(); void InitializeMemoryState(); const SHintState* GetCurrentDisplayedHint() const; - void DelayHint(const char* name); - void ActivateImmediateHintTimer(const char* name); - void ActivateContinueDelayHintTimer(const char* name); + void DelayHint(std::string_view name); + void ActivateImmediateHintTimer(std::string_view name); + void ActivateContinueDelayHintTimer(std::string_view name); void DismissDisplayedHint(); u32 GetNextHintIdx() const; const std::vector& GetHintStates() const { return x0_hintStates; } diff --git a/Runtime/World/CScriptSpecialFunction.cpp b/Runtime/World/CScriptSpecialFunction.cpp index e15b55b52..d1fa1b38b 100644 --- a/Runtime/World/CScriptSpecialFunction.cpp +++ b/Runtime/World/CScriptSpecialFunction.cpp @@ -396,12 +396,13 @@ void CScriptSpecialFunction::AcceptScriptMsg(EScriptObjectMessage msg, TUniqueId } case ESpecialFunction::RedundantHintSystem: { CHintOptions& hintOptions = g_GameState->HintOptions(); - if (msg == EScriptObjectMessage::Action) - hintOptions.ActivateContinueDelayHintTimer(xec_locatorName.c_str()); - else if (msg == EScriptObjectMessage::Increment) - hintOptions.ActivateImmediateHintTimer(xec_locatorName.c_str()); - else if (msg == EScriptObjectMessage::Decrement) - hintOptions.DelayHint(xec_locatorName.c_str()); + if (msg == EScriptObjectMessage::Action) { + hintOptions.ActivateContinueDelayHintTimer(xec_locatorName); + } else if (msg == EScriptObjectMessage::Increment) { + hintOptions.ActivateImmediateHintTimer(xec_locatorName); + } else if (msg == EScriptObjectMessage::Decrement) { + hintOptions.DelayHint(xec_locatorName); + } break; } case ESpecialFunction::Billboard: {