Add ParseSingleMessage function to handle message parsing from ROM data
This commit is contained in:
@@ -1,8 +1,9 @@
|
|||||||
#include "message_data.h"
|
#include "message_data.h"
|
||||||
|
|
||||||
#include "app/core/common.h"
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
#include "app/core/common.h"
|
||||||
|
|
||||||
namespace yaze {
|
namespace yaze {
|
||||||
namespace editor {
|
namespace editor {
|
||||||
|
|
||||||
@@ -177,6 +178,86 @@ std::vector<DictionaryEntry> BuildDictionaryEntries(Rom *rom) {
|
|||||||
return AllDictionaries;
|
return AllDictionaries;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
absl::StatusOr<MessageData> ParseSingleMessage(
|
||||||
|
const std::vector<uint8_t> &rom_data, int *current_pos) {
|
||||||
|
MessageData message_data;
|
||||||
|
int pos = *current_pos;
|
||||||
|
uint8_t current_byte;
|
||||||
|
std::vector<uint8_t> temp_bytes_raw;
|
||||||
|
std::vector<uint8_t> temp_bytes_parsed;
|
||||||
|
std::string current_message_raw;
|
||||||
|
std::string current_message_parsed;
|
||||||
|
|
||||||
|
// Read the message data
|
||||||
|
while (true) {
|
||||||
|
current_byte = rom_data[pos++];
|
||||||
|
|
||||||
|
if (current_byte == kMessageTerminator) {
|
||||||
|
message_data.ID = message_data.ID + 1;
|
||||||
|
message_data.Address = pos;
|
||||||
|
message_data.RawString = current_message_raw;
|
||||||
|
message_data.Data = temp_bytes_raw;
|
||||||
|
message_data.DataParsed = temp_bytes_parsed;
|
||||||
|
message_data.ContentsParsed = current_message_parsed;
|
||||||
|
|
||||||
|
temp_bytes_raw.clear();
|
||||||
|
temp_bytes_parsed.clear();
|
||||||
|
current_message_raw.clear();
|
||||||
|
current_message_parsed.clear();
|
||||||
|
|
||||||
|
break;
|
||||||
|
} else if (current_byte == 0xFF) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
temp_bytes_raw.push_back(current_byte);
|
||||||
|
|
||||||
|
// Check for command.
|
||||||
|
TextElement text_element = FindMatchingCommand(current_byte);
|
||||||
|
if (!text_element.Empty()) {
|
||||||
|
current_message_raw.append(text_element.GetParamToken());
|
||||||
|
current_message_parsed.append(text_element.GetParamToken());
|
||||||
|
temp_bytes_parsed.push_back(current_byte);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check for dictionary.
|
||||||
|
int dictionary = FindDictionaryEntry(current_byte);
|
||||||
|
if (dictionary >= 0) {
|
||||||
|
current_message_raw.append("[");
|
||||||
|
current_message_raw.append(DICTIONARYTOKEN);
|
||||||
|
current_message_raw.append(":");
|
||||||
|
current_message_raw.append(core::HexWord(dictionary));
|
||||||
|
current_message_raw.append("]");
|
||||||
|
|
||||||
|
auto mutable_rom_data = const_cast<uint8_t *>(rom_data.data());
|
||||||
|
uint32_t address = core::Get24LocalFromPC(
|
||||||
|
mutable_rom_data, kPointersDictionaries + (dictionary * 2));
|
||||||
|
uint32_t address_end = core::Get24LocalFromPC(
|
||||||
|
mutable_rom_data, kPointersDictionaries + ((dictionary + 1) * 2));
|
||||||
|
|
||||||
|
for (uint32_t i = address; i < address_end; i++) {
|
||||||
|
temp_bytes_parsed.push_back(rom_data[i]);
|
||||||
|
current_message_parsed.append(ParseTextDataByte(rom_data[i]));
|
||||||
|
}
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Everything else.
|
||||||
|
if (CharEncoder.contains(current_byte)) {
|
||||||
|
std::string str = "";
|
||||||
|
str.push_back(CharEncoder.at(current_byte));
|
||||||
|
current_message_raw.append(str);
|
||||||
|
current_message_parsed.append(str);
|
||||||
|
temp_bytes_parsed.push_back(current_byte);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
*current_pos = pos;
|
||||||
|
return message_data;
|
||||||
|
}
|
||||||
|
|
||||||
std::vector<std::string> ParseMessageData(
|
std::vector<std::string> ParseMessageData(
|
||||||
std::vector<MessageData> &message_data,
|
std::vector<MessageData> &message_data,
|
||||||
const std::vector<DictionaryEntry> &dictionary_entries) {
|
const std::vector<DictionaryEntry> &dictionary_entries) {
|
||||||
|
|||||||
@@ -100,8 +100,12 @@ struct MessageData {
|
|||||||
const std::vector<uint8_t> &rawData,
|
const std::vector<uint8_t> &rawData,
|
||||||
const std::string &parsedString,
|
const std::string &parsedString,
|
||||||
const std::vector<uint8_t> &parsedData)
|
const std::vector<uint8_t> &parsedData)
|
||||||
: ID(id), Address(address), RawString(rawString), Data(rawData),
|
: ID(id),
|
||||||
DataParsed(parsedData), ContentsParsed(parsedString) {}
|
Address(address),
|
||||||
|
RawString(rawString),
|
||||||
|
Data(rawData),
|
||||||
|
DataParsed(parsedData),
|
||||||
|
ContentsParsed(parsedString) {}
|
||||||
|
|
||||||
// Copy constructor
|
// Copy constructor
|
||||||
MessageData(const MessageData &other) {
|
MessageData(const MessageData &other) {
|
||||||
@@ -117,9 +121,9 @@ struct MessageData {
|
|||||||
return absl::StrFormat("%0X - %s", ID, ContentsParsed);
|
return absl::StrFormat("%0X - %s", ID, ContentsParsed);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string
|
std::string OptimizeMessageForDictionary(
|
||||||
OptimizeMessageForDictionary(std::string message_string,
|
std::string message_string,
|
||||||
const std::vector<DictionaryEntry> &dictionary) {
|
const std::vector<DictionaryEntry> &dictionary) {
|
||||||
std::stringstream protons;
|
std::stringstream protons;
|
||||||
bool command = false;
|
bool command = false;
|
||||||
for (const auto &c : message_string) {
|
for (const auto &c : message_string) {
|
||||||
@@ -297,13 +301,16 @@ ParsedElement FindMatchingElement(const std::string &str);
|
|||||||
|
|
||||||
std::string ParseTextDataByte(uint8_t value);
|
std::string ParseTextDataByte(uint8_t value);
|
||||||
|
|
||||||
std::vector<std::string>
|
absl::StatusOr<MessageData> ParseSingleMessage(
|
||||||
ParseMessageData(std::vector<MessageData> &message_data,
|
const std::vector<uint8_t> &rom_data, int *current_pos);
|
||||||
const std::vector<DictionaryEntry> &dictionary_entries);
|
|
||||||
|
std::vector<std::string> ParseMessageData(
|
||||||
|
std::vector<MessageData> &message_data,
|
||||||
|
const std::vector<DictionaryEntry> &dictionary_entries);
|
||||||
|
|
||||||
std::vector<std::string> ImportMessageData(std::string_view filename);
|
std::vector<std::string> ImportMessageData(std::string_view filename);
|
||||||
|
|
||||||
} // namespace editor
|
} // namespace editor
|
||||||
} // namespace yaze
|
} // namespace yaze
|
||||||
|
|
||||||
#endif // YAZE_APP_EDITOR_MESSAGE_MESSAGE_DATA_H
|
#endif // YAZE_APP_EDITOR_MESSAGE_MESSAGE_DATA_H
|
||||||
|
|||||||
Reference in New Issue
Block a user