Split out code for serializing/deserializing cheat lines

This commit is contained in:
JosJuice
2021-08-05 19:05:12 +02:00
parent fb96ecb7da
commit b90008aadb
8 changed files with 149 additions and 155 deletions

View File

@ -4,6 +4,7 @@
#include "Core/GeckoCodeConfig.h"
#include <algorithm>
#include <optional>
#include <sstream>
#include <string>
#include <vector>
@ -176,8 +177,10 @@ std::vector<GeckoCode> LoadCodes(const IniFile& globalIni, const IniFile& localI
{
GeckoCode::Code new_code;
// TODO: support options
new_code.original_line = line;
ss >> std::hex >> new_code.address >> new_code.data;
if (std::optional<GeckoCode::Code> code = DeserializeLine(line))
new_code = *code;
else
new_code.original_line = line;
gcode.codes.push_back(new_code);
}
break;
@ -251,4 +254,23 @@ void SaveCodes(IniFile& inifile, const std::vector<GeckoCode>& gcodes)
inifile.SetLines("Gecko_Enabled", enabled_lines);
inifile.SetLines("Gecko_Disabled", disabled_lines);
}
std::optional<GeckoCode::Code> DeserializeLine(const std::string& line)
{
std::vector<std::string> items = SplitString(line, ' ');
GeckoCode::Code code;
code.original_line = line;
if (items.size() < 2)
return std::nullopt;
if (!TryParse(items[0], &code.address, 16))
return std::nullopt;
if (!TryParse(items[1], &code.data, 16))
return std::nullopt;
return code;
}
} // namespace Gecko