Use new helper functions ParseAsArray and ParseAsPairs of InfoStringToStructConverterBase to parse notetracksoundmap and hidetags

This commit is contained in:
Jan 2021-03-26 12:12:32 +01:00
parent aaf350d088
commit 3520a9bd2c
3 changed files with 140 additions and 127 deletions

View File

@ -21,62 +21,34 @@ namespace T6
{ {
bool ConvertHideTags(const cspField_t& field, const std::string& value) bool ConvertHideTags(const cspField_t& field, const std::string& value)
{ {
std::vector<std::string> valueArray;
if (!ParseAsArray(value, valueArray))
{
std::cout << "Failed to parse hide tags as array" << std::endl;
return false;
}
if (valueArray.size() > std::extent<decltype(WeaponFullDef::hideTags)>::value)
{
std::cout << "Cannot have more than " << std::extent<decltype(WeaponFullDef::hideTags)>::value << "hide tags!" << std::endl;
return false;
}
auto* hideTags = reinterpret_cast<scr_string_t*>(reinterpret_cast<uintptr_t>(m_structure) + field.iOffset); auto* hideTags = reinterpret_cast<scr_string_t*>(reinterpret_cast<uintptr_t>(m_structure) + field.iOffset);
std::ostringstream hideTag;
auto currentHideTag = 0u; auto currentHideTag = 0u;
auto hasHideTagText = false;
for (auto ci = 0u; ci < value.size(); ci++) if (valueArray.size() < std::extent<decltype(WeaponFullDef::hideTags)>::value)
{
auto c = value[ci];
if (c == '\r' && ci + 1 < value.size() && value[ci + 1] == '\n')
c = value[++ci];
if (c == '\n')
{
if (hasHideTagText)
{
if (currentHideTag >= std::extent<decltype(WeaponFullDef::hideTags)>::value)
{
std::cout << "Cannot have more than " << std::extent<decltype(WeaponFullDef::hideTags)>::value << "hide tags!" << std::endl;
return false;
}
auto scrString = m_zone_script_strings.AddOrGetScriptString(hideTag.str());
hideTags[currentHideTag++] = scrString;
m_used_script_string_list.emplace(scrString);
hasHideTagText = false;
hideTag.clear();
hideTag.str(std::string());
}
}
else
{
hideTag << c;
hasHideTagText = true;
}
}
if (hasHideTagText)
{
if (currentHideTag >= std::extent<decltype(WeaponFullDef::hideTags)>::value)
{
std::cout << "Cannot have more than " << std::extent<decltype(WeaponFullDef::hideTags)>::value << "hide tags!" << std::endl;
return false;
}
auto scrString = m_zone_script_strings.AddOrGetScriptString(hideTag.str());
hideTags[currentHideTag++] = scrString;
m_used_script_string_list.emplace(scrString);
}
if (currentHideTag < std::extent<decltype(WeaponFullDef::hideTags)>::value)
{ {
m_used_script_string_list.emplace(m_zone_script_strings.AddOrGetScriptString("")); m_used_script_string_list.emplace(m_zone_script_strings.AddOrGetScriptString(""));
} }
for (; currentHideTag < valueArray.size(); currentHideTag++)
{
const auto scrString = m_zone_script_strings.AddOrGetScriptString(valueArray[currentHideTag]);
hideTags[currentHideTag] = scrString;
m_used_script_string_list.emplace(scrString);
}
for (; currentHideTag < std::extent<decltype(WeaponFullDef::hideTags)>::value; currentHideTag++) for (; currentHideTag < std::extent<decltype(WeaponFullDef::hideTags)>::value; currentHideTag++)
{ {
hideTags[currentHideTag] = m_zone_script_strings.GetScriptString(""); hideTags[currentHideTag] = m_zone_script_strings.GetScriptString("");
@ -106,90 +78,38 @@ namespace T6
_NODISCARD bool ConvertNotetrackSoundMap(const cspField_t& field, const std::string& value) _NODISCARD bool ConvertNotetrackSoundMap(const cspField_t& field, const std::string& value)
{ {
auto* keys = reinterpret_cast<scr_string_t*>(reinterpret_cast<uintptr_t>(m_structure) + field.iOffset); std::vector<std::pair<std::string, std::string>> pairs;
auto* values = &keys[std::extent<decltype(WeaponFullDef::notetrackSoundMapKeys)>::value]; if(!ParseAsPairs(value, pairs))
std::ostringstream str;
std::string key;
auto isKey = true;
auto currentEntryNum = 0u;
for (auto ci = 0u; ci < value.size(); ci++)
{ {
auto c = value[ci]; std::cout << "Failed to parse notetracksoundmap as pairs" << std::endl;
return false;
if (c == '\r' && ci + 1 < value.size() && value[ci + 1] == '\n')
c = value[++ci];
if (c == '\n' && !isKey)
{
std::cout << "Expected notetracksoundmap value but got new line" << std::endl;
return false;
}
if(isspace(c))
continue;
int separator;
while(true)
{
str << c;
ci++;
if(ci >= value.size())
{
separator = EOF;
break;
}
c = value[ci];
if (c == '\r' && ci + 1 < value.size() && value[ci + 1] == '\n')
c = value[++ci];
if(isspace(c))
{
separator = static_cast<int>(static_cast<unsigned char>(c));
break;
}
}
if(isKey)
{
if(separator == '\n' || separator == EOF)
{
std::cout << "Expected notetracksoundmap value but got new line" << std::endl;
return false;
}
key = str.str();
str.clear();
str.str("");
}
else
{
auto parsedValue = str.str();
str.clear();
str.str("");
if(currentEntryNum >= std::extent<decltype(WeaponFullDef::notetrackSoundMapKeys)>::value)
{
std::cout << "Too many notetracksoundmap entries: Max is " << std::extent<decltype(WeaponFullDef::notetrackSoundMapKeys)>::value << std::endl;
return false;
}
keys[currentEntryNum] = m_zone_script_strings.AddOrGetScriptString(key);
m_used_script_string_list.emplace(keys[currentEntryNum]);
values[currentEntryNum] = m_zone_script_strings.AddOrGetScriptString(parsedValue);
m_used_script_string_list.emplace(values[currentEntryNum]);
currentEntryNum++;
}
isKey = !isKey;
} }
if(currentEntryNum < std::extent<decltype(WeaponFullDef::notetrackSoundMapKeys)>::value) if(pairs.size() > std::extent<decltype(WeaponFullDef::notetrackSoundMapKeys)>::value)
{
std::cout << "Cannot have more than " << std::extent<decltype(WeaponFullDef::notetrackSoundMapKeys)>::value << "notetracksoundmap entries!" << std::endl;
return false;
}
auto* keys = reinterpret_cast<scr_string_t*>(reinterpret_cast<uintptr_t>(m_structure) + field.iOffset);
auto* values = &keys[std::extent<decltype(WeaponFullDef::notetrackSoundMapKeys)>::value];
auto currentEntryNum = 0u;
if (pairs.size() < std::extent<decltype(WeaponFullDef::notetrackSoundMapKeys)>::value)
{ {
m_used_script_string_list.emplace(m_zone_script_strings.AddOrGetScriptString("")); m_used_script_string_list.emplace(m_zone_script_strings.AddOrGetScriptString(""));
} }
for(; currentEntryNum < std::extent<decltype(WeaponFullDef::notetrackSoundMapKeys)>::value; currentEntryNum++)
for (; currentEntryNum < pairs.size(); currentEntryNum++)
{
keys[currentEntryNum] = m_zone_script_strings.AddOrGetScriptString(pairs[currentEntryNum].first);
m_used_script_string_list.emplace(keys[currentEntryNum]);
values[currentEntryNum] = m_zone_script_strings.AddOrGetScriptString(pairs[currentEntryNum].second);
m_used_script_string_list.emplace(values[currentEntryNum]);
}
for (; currentEntryNum < std::extent<decltype(WeaponFullDef::notetrackSoundMapKeys)>::value; currentEntryNum++)
{ {
const auto emptyScr = m_zone_script_strings.GetScriptString(""); const auto emptyScr = m_zone_script_strings.GetScriptString("");
keys[currentEntryNum] = emptyScr; keys[currentEntryNum] = emptyScr;

View File

@ -11,6 +11,95 @@ InfoStringToStructConverterBase::InfoStringToStructConverterBase(const InfoStrin
{ {
} }
bool InfoStringToStructConverterBase::ParseAsArray(const std::string& value, std::vector<std::string>& valueArray)
{
auto startPos = 0u;
for (auto ci = 0u; ci < value.size(); ci++)
{
const auto c = value[ci];
if (c == '\r' && ci + 1 < value.size() && value[ci + 1] == '\n')
{
valueArray.emplace_back(value, startPos, ci - startPos);
startPos = ++ci + 1;
}
else if(c == '\n')
{
valueArray.emplace_back(value, startPos, ci - startPos);
startPos = ci + 1;
}
}
if(startPos < value.size())
{
valueArray.emplace_back(value, startPos, value.size() - startPos);
}
return true;
}
bool InfoStringToStructConverterBase::ParseAsPairs(const std::string& value, std::vector<std::pair<std::string, std::string>>& valueArray) const
{
std::string key;
auto isKey = true;
for (auto ci = 0u; ci < value.size(); ci++)
{
auto c = value[ci];
if (c == '\r' && ci + 1 < value.size() && value[ci + 1] == '\n')
c = value[++ci];
if (c == '\n' && !isKey)
{
std::cout << "Expected value but got new line" << std::endl;
return false;
}
if (isspace(c))
continue;
int separator;
const auto startPos = ci;
while (true)
{
ci++;
if (ci >= value.size())
{
separator = EOF;
break;
}
c = value[ci];
if (c == '\r' && ci + 1 < value.size() && value[ci + 1] == '\n')
c = value[++ci];
if (isspace(c))
{
separator = static_cast<int>(static_cast<unsigned char>(c));
break;
}
}
if (isKey)
{
if (separator == '\n' || separator == EOF)
{
std::cout << "Expected value but got new line" << std::endl;
return false;
}
key = std::string(value, startPos, ci - startPos);
}
else
{
auto parsedValue = std::string(value, startPos, ci - startPos);
valueArray.emplace_back(std::make_pair(std::move(key), std::move(parsedValue)));
key = std::string();
}
isKey = !isKey;
}
return true;
}
bool InfoStringToStructConverterBase::ConvertString(const std::string& value, const size_t offset) bool InfoStringToStructConverterBase::ConvertString(const std::string& value, const size_t offset)
{ {
*reinterpret_cast<const char**>(reinterpret_cast<uintptr_t>(m_structure) + offset) = m_memory->Dup(value.c_str()); *reinterpret_cast<const char**>(reinterpret_cast<uintptr_t>(m_structure) + offset) = m_memory->Dup(value.c_str());

View File

@ -2,6 +2,7 @@
#include <string> #include <string>
#include <unordered_set> #include <unordered_set>
#include <utility>
#include "Utils/ClassUtils.h" #include "Utils/ClassUtils.h"
#include "InfoString/InfoString.h" #include "InfoString/InfoString.h"
@ -19,6 +20,9 @@ protected:
MemoryManager* m_memory; MemoryManager* m_memory;
void* m_structure; void* m_structure;
static bool ParseAsArray(const std::string& value, std::vector<std::string>& valueArray);
bool ParseAsPairs(const std::string& value, std::vector<std::pair<std::string, std::string>>& valueArray) const;
bool ConvertString(const std::string& value, size_t offset); bool ConvertString(const std::string& value, size_t offset);
bool ConvertStringBuffer(const std::string& value, size_t offset, size_t bufferSize); bool ConvertStringBuffer(const std::string& value, size_t offset, size_t bufferSize);
bool ConvertInt(const std::string& value, size_t offset); bool ConvertInt(const std::string& value, size_t offset);