Add ParseMessageData to message data helper fns

This commit is contained in:
scawful
2025-01-09 21:23:14 -05:00
parent 30bfa91427
commit d9cc92edca
4 changed files with 113 additions and 118 deletions

View File

@@ -23,7 +23,7 @@ uint8_t FindDictionaryEntry(uint8_t value) {
TextElement FindMatchingCommand(uint8_t b) { TextElement FindMatchingCommand(uint8_t b) {
TextElement empty_element; TextElement empty_element;
for (const auto& text_element : TextCommands) { for (const auto &text_element : TextCommands) {
if (text_element.ID == b) { if (text_element.ID == b) {
return text_element; return text_element;
} }
@@ -33,7 +33,7 @@ TextElement FindMatchingCommand(uint8_t b) {
TextElement FindMatchingSpecial(uint8_t value) { TextElement FindMatchingSpecial(uint8_t value) {
auto it = std::find_if(SpecialChars.begin(), SpecialChars.end(), auto it = std::find_if(SpecialChars.begin(), SpecialChars.end(),
[value](const TextElement& text_element) { [value](const TextElement &text_element) {
return text_element.ID == value; return text_element.ID == value;
}); });
if (it != SpecialChars.end()) { if (it != SpecialChars.end()) {
@@ -43,9 +43,9 @@ TextElement FindMatchingSpecial(uint8_t value) {
return TextElement(); return TextElement();
} }
ParsedElement FindMatchingElement(const std::string& str) { ParsedElement FindMatchingElement(const std::string &str) {
std::smatch match; std::smatch match;
for (auto& textElement : TextCommands) { for (auto &textElement : TextCommands) {
match = textElement.MatchMe(str); match = textElement.MatchMe(str);
if (match.size() > 0) { if (match.size() > 0) {
if (textElement.HasArgument) { if (textElement.HasArgument) {
@@ -144,7 +144,7 @@ std::vector<uint8_t> ParseMessageToData(std::string str) {
return bytes; return bytes;
} }
std::vector<DictionaryEntry> BuildDictionaryEntries(Rom* rom) { std::vector<DictionaryEntry> BuildDictionaryEntries(Rom *rom) {
std::vector<DictionaryEntry> AllDictionaries; std::vector<DictionaryEntry> AllDictionaries;
for (int i = 0; i < kNumDictionaryEntries; i++) { for (int i = 0; i < kNumDictionaryEntries; i++) {
std::vector<uint8_t> bytes; std::vector<uint8_t> bytes;
@@ -169,12 +169,49 @@ std::vector<DictionaryEntry> BuildDictionaryEntries(Rom* rom) {
} }
std::sort(AllDictionaries.begin(), AllDictionaries.end(), std::sort(AllDictionaries.begin(), AllDictionaries.end(),
[](const DictionaryEntry& a, const DictionaryEntry& b) { [](const DictionaryEntry &a, const DictionaryEntry &b) {
return a.Contents.size() > b.Contents.size(); return a.Contents.size() > b.Contents.size();
}); });
return AllDictionaries; return AllDictionaries;
} }
std::vector<std::string>
ParseMessageData(std::vector<MessageData> &message_data,
const std::vector<DictionaryEntry> &dictionary_entries) {
std::vector<std::string> parsed_messages;
for (auto &message : message_data) {
std::cout << "Message #" << message.ID << " at address "
<< core::HexLong(message.Address) << std::endl;
std::cout << " " << message.RawString << std::endl;
std::string parsed_message = "";
for (const uint8_t &byte : message.Data) {
if (CharEncoder.contains(byte)) {
parsed_message.push_back(CharEncoder.at(byte));
} else {
if (byte >= DICTOFF && byte < (DICTOFF + 97)) {
auto dic_entry = dictionary_entries[byte];
parsed_message.append(dic_entry.Contents);
} else {
auto text_element = FindMatchingCommand(byte);
if (!text_element.Empty()) {
if (text_element.ID == kScrollVertical ||
text_element.ID == kLine2 || text_element.ID == kLine3) {
parsed_message.append("\n");
}
parsed_message.append(text_element.GenericToken);
}
}
}
}
parsed_messages.push_back(parsed_message);
}
return parsed_messages;
}
} // namespace editor } // namespace editor
} // namespace yaze } // namespace yaze

View File

@@ -76,8 +76,12 @@ constexpr int kTextData = 0xE0000;
constexpr int kTextDataEnd = 0xE7FFF; constexpr int kTextDataEnd = 0xE7FFF;
constexpr int kNumDictionaryEntries = 97; constexpr int kNumDictionaryEntries = 97;
constexpr int kPointersDictionaries = 0x74703; constexpr int kPointersDictionaries = 0x74703;
constexpr uint8_t kScrollVertical = 0x73;
constexpr uint8_t kLine1 = 0x74;
constexpr uint8_t kLine2 = 0x75;
constexpr uint8_t kLine3 = 0x76;
std::vector<DictionaryEntry> BuildDictionaryEntries(Rom* rom); std::vector<DictionaryEntry> BuildDictionaryEntries(Rom *rom);
std::string ReplaceAllDictionaryWords(std::string str, std::string ReplaceAllDictionaryWords(std::string str,
std::vector<DictionaryEntry> dictionary); std::vector<DictionaryEntry> dictionary);
@@ -94,19 +98,15 @@ struct MessageData {
std::vector<uint8_t> DataParsed; std::vector<uint8_t> DataParsed;
MessageData() = default; MessageData() = default;
MessageData(int id, int address, const std::string& rawString, MessageData(int id, int address, const std::string &rawString,
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), : ID(id), Address(address), RawString(rawString), Data(rawData),
Address(address), DataParsed(parsedData), ContentsParsed(parsedString) {}
RawString(rawString),
Data(rawData),
DataParsed(parsedData),
ContentsParsed(parsedString) {}
// Copy constructor // Copy constructor
MessageData(const MessageData& other) { MessageData(const MessageData &other) {
ID = other.ID; ID = other.ID;
Address = other.Address; Address = other.Address;
RawString = other.RawString; RawString = other.RawString;
@@ -119,12 +119,12 @@ struct MessageData {
return absl::StrFormat("%0X - %s", ID, ContentsParsed); return absl::StrFormat("%0X - %s", ID, ContentsParsed);
} }
std::string OptimizeMessageForDictionary( std::string
std::string messageString, OptimizeMessageForDictionary(std::string messageString,
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 : messageString) { for (const auto &c : messageString) {
if (c == '[') { if (c == '[') {
command = true; command = true;
} else if (c == ']') { } else if (c == ']') {
@@ -146,13 +146,15 @@ struct MessageData {
return finalString; return finalString;
} }
void SetMessage(const std::string& message, void SetMessage(const std::string &message,
const std::vector<DictionaryEntry>& dictionary) { const std::vector<DictionaryEntry> &dictionary) {
RawString = message; RawString = message;
ContentsParsed = OptimizeMessageForDictionary(message, dictionary); ContentsParsed = OptimizeMessageForDictionary(message, dictionary);
} }
}; };
struct TextElement { struct TextElement {
uint8_t ID; uint8_t ID;
std::string Token; std::string Token;
@@ -163,8 +165,8 @@ struct TextElement {
bool HasArgument; bool HasArgument;
TextElement() = default; TextElement() = default;
TextElement(uint8_t id, const std::string& token, bool arg, TextElement(uint8_t id, const std::string &token, bool arg,
const std::string& description) { const std::string &description) {
ID = id; ID = id;
Token = token; Token = token;
if (arg) { if (arg) {
@@ -203,7 +205,7 @@ struct TextElement {
bool Empty() const { return ID == 0; } bool Empty() const { return ID == 0; }
// Comparison operator // Comparison operator
bool operator==(const TextElement& other) const { return ID == other.ID; } bool operator==(const TextElement &other) const { return ID == other.ID; }
}; };
const static std::string kWindowBorder = "Window border"; const static std::string kWindowBorder = "Window border";
@@ -254,7 +256,6 @@ static const std::vector<TextElement> TextCommands = {
TextElement(0x70, "NONO", false, kCrash), TextElement(0x70, "NONO", false, kCrash),
}; };
TextElement FindMatchingCommand(uint8_t b); TextElement FindMatchingCommand(uint8_t b);
static const std::vector<TextElement> SpecialChars = { static const std::vector<TextElement> SpecialChars = {
@@ -296,10 +297,14 @@ struct ParsedElement {
} }
}; };
ParsedElement FindMatchingElement(const std::string& str); ParsedElement FindMatchingElement(const std::string &str);
std::string ParseTextDataByte(uint8_t value); std::string ParseTextDataByte(uint8_t value);
std::vector<std::string>
ParseMessageData(std::vector<MessageData> &message_data,
const std::vector<DictionaryEntry> &dictionary_entries);
} // namespace editor } // namespace editor
} // namespace yaze } // namespace yaze

View File

@@ -27,7 +27,6 @@ using ImGui::BeginTable;
using ImGui::Button; using ImGui::Button;
using ImGui::EndChild; using ImGui::EndChild;
using ImGui::EndTable; using ImGui::EndTable;
using ImGui::InputText;
using ImGui::InputTextMultiline; using ImGui::InputTextMultiline;
using ImGui::SameLine; using ImGui::SameLine;
using ImGui::Separator; using ImGui::Separator;
@@ -45,9 +44,9 @@ constexpr ImGuiTableFlags kDictTableFlags =
ImGuiTableFlags_Borders | ImGuiTableFlags_Resizable; ImGuiTableFlags_Borders | ImGuiTableFlags_Resizable;
absl::Status MessageEditor::Initialize() { absl::Status MessageEditor::Initialize() {
for (int i = 0; i < kWidthArraySize; i++) { std::copy(rom()->vector().begin() + kCharactersWidth,
width_array[i] = rom()->data()[kCharactersWidth + i]; rom()->vector().begin() + kCharactersWidth + kWidthArraySize,
} width_array.begin());
all_dictionaries_ = BuildDictionaryEntries(rom()); all_dictionaries_ = BuildDictionaryEntries(rom());
ReadAllTextDataV2(); ReadAllTextDataV2();
@@ -58,9 +57,8 @@ absl::Status MessageEditor::Initialize() {
font_preview_colors_.AddColor(0x001F); // Blue font_preview_colors_.AddColor(0x001F); // Blue
std::vector<uint8_t> data(0x4000, 0); std::vector<uint8_t> data(0x4000, 0);
for (int i = 0; i < 0x4000; i++) { std::copy(rom()->vector().begin() + kGfxFont,
data[i] = rom()->data()[kGfxFont + i]; rom()->vector().begin() + kGfxFont + 0x4000, data.begin());
}
font_gfx16_data_ = gfx::SnesTo8bppSheet(data, /*bpp=*/2, /*num_sheets=*/2); font_gfx16_data_ = gfx::SnesTo8bppSheet(data, /*bpp=*/2, /*num_sheets=*/2);
// 4bpp // 4bpp
@@ -79,50 +77,9 @@ absl::Status MessageEditor::Initialize() {
kCurrentMessageWidth, kCurrentMessageHeight, 64, current_font_gfx16_data_, kCurrentMessageWidth, kCurrentMessageHeight, 64, current_font_gfx16_data_,
current_font_gfx16_bitmap_, font_preview_colors_)) current_font_gfx16_bitmap_, font_preview_colors_))
gfx::SnesPalette color_palette = font_gfx_bitmap_.palette(); *font_gfx_bitmap_.mutable_palette() = font_preview_colors_;
for (int i = 0; i < font_preview_colors_.size(); i++) {
*color_palette.mutable_color(i) = font_preview_colors_[i];
}
*font_gfx_bitmap_.mutable_palette() = color_palette;
for (const auto& each_message : list_of_texts_) {
std::cout << "Message #" << each_message.ID << " at address "
<< core::HexLong(each_message.Address) << std::endl;
std::cout << " " << each_message.RawString << std::endl;
// Each string has a [:XX] char encoded
// The corresponding character is found in CharEncoder unordered_map
std::string parsed_message = "";
for (const auto& byte : each_message.Data) {
// Find the char byte in the CharEncoder map
if (CharEncoder.contains(byte)) {
parsed_message.push_back(CharEncoder.at(byte));
} else {
// If the byte is not found in the CharEncoder map, it is a command
// or a dictionary entry
if (byte >= DICTOFF && byte < (DICTOFF + 97)) {
// Dictionary entry
auto dictionaryEntry = GetDictionaryFromID(byte - DICTOFF);
parsed_message.append(dictionaryEntry.Contents);
} else {
// Command
TextElement textElement = FindMatchingCommand(byte);
if (!textElement.Empty()) {
// If the element is line 2, 3 or V we add a newline
if (textElement.ID == kScrollVertical || textElement.ID == kLine2 ||
textElement.ID == kLine3)
parsed_message.append("\n");
parsed_message.append(textElement.GenericToken);
}
}
}
}
std::cout << " > " << parsed_message << std::endl;
parsed_messages_.push_back(parsed_message);
}
parsed_messages_ = ParseMessageData(list_of_texts_, all_dictionaries_);
DrawMessagePreview(); DrawMessagePreview();
return absl::OkStatus(); return absl::OkStatus();
@@ -171,7 +128,7 @@ void MessageEditor::DrawMessageList() {
TableHeadersRow(); TableHeadersRow();
for (const auto& message : list_of_texts_) { for (const auto &message : list_of_texts_) {
TableNextColumn(); TableNextColumn();
if (Button(core::HexWord(message.ID).c_str())) { if (Button(core::HexWord(message.ID).c_str())) {
current_message_ = message; current_message_ = message;
@@ -180,8 +137,7 @@ void MessageEditor::DrawMessageList() {
TableNextColumn(); TableNextColumn();
TextWrapped("%s", parsed_messages_[message.ID].c_str()); TextWrapped("%s", parsed_messages_[message.ID].c_str());
TableNextColumn(); TableNextColumn();
TextWrapped( TextWrapped("%s",
"%s",
core::HexLong(list_of_texts_[message.ID].Address).c_str()); core::HexLong(list_of_texts_[message.ID].Address).c_str());
} }
@@ -232,7 +188,7 @@ void MessageEditor::DrawCurrentMessage() {
void MessageEditor::DrawTextCommands() { void MessageEditor::DrawTextCommands() {
if (BeginChild("##TextCommands", ImVec2(0, 0), true, if (BeginChild("##TextCommands", ImVec2(0, 0), true,
ImGuiWindowFlags_AlwaysVerticalScrollbar)) { ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
for (const auto& text_element : TextCommands) { for (const auto &text_element : TextCommands) {
if (Button(text_element.GenericToken.c_str())) { if (Button(text_element.GenericToken.c_str())) {
} }
SameLine(); SameLine();
@@ -250,7 +206,7 @@ void MessageEditor::DrawDictionary() {
TableSetupColumn("ID"); TableSetupColumn("ID");
TableSetupColumn("Contents"); TableSetupColumn("Contents");
for (const auto& dictionary : all_dictionaries_) { for (const auto &dictionary : all_dictionaries_) {
TableNextColumn(); TableNextColumn();
Text("%s", core::HexWord(dictionary.ID).c_str()); Text("%s", core::HexWord(dictionary.ID).c_str());
TableNextColumn(); TableNextColumn();
@@ -337,9 +293,9 @@ void MessageEditor::ReadAllTextDataV2() {
uint32_t address = core::Get24LocalFromPC( uint32_t address = core::Get24LocalFromPC(
rom()->mutable_data(), kPointersDictionaries + (dictionary * 2)); rom()->mutable_data(), kPointersDictionaries + (dictionary * 2));
uint32_t address_end = core::Get24LocalFromPC( uint32_t address_end = core::Get24LocalFromPC(rom()->mutable_data(),
rom()->mutable_data(), kPointersDictionaries +
kPointersDictionaries + ((dictionary + 1) * 2)); ((dictionary + 1) * 2));
for (uint32_t i = address; i < address_end; i++) { for (uint32_t i = address; i < address_end; i++) {
parsed_message.push_back(rom()->data()[i]); parsed_message.push_back(rom()->data()[i]);
@@ -437,8 +393,9 @@ void MessageEditor::ReadAllTextData() {
uint32_t address = core::Get24LocalFromPC( uint32_t address = core::Get24LocalFromPC(
rom()->mutable_data(), kPointersDictionaries + (dictionary * 2)); rom()->mutable_data(), kPointersDictionaries + (dictionary * 2));
uint32_t address_end = core::Get24LocalFromPC( uint32_t address_end = core::Get24LocalFromPC(rom()->mutable_data(),
rom()->mutable_data(), kPointersDictionaries + ((dictionary + 1) * 2)); kPointersDictionaries +
((dictionary + 1) * 2));
for (uint32_t i = address; i < address_end; i++) { for (uint32_t i = address; i < address_end; i++) {
temp_bytes_parsed.push_back(rom()->data()[i]); temp_bytes_parsed.push_back(rom()->data()[i]);
@@ -462,7 +419,7 @@ void MessageEditor::ReadAllTextData() {
std::string ReplaceAllDictionaryWords(std::string str, std::string ReplaceAllDictionaryWords(std::string str,
std::vector<DictionaryEntry> dictionary) { std::vector<DictionaryEntry> dictionary) {
std::string temp = str; std::string temp = str;
for (const auto& entry : dictionary) { for (const auto &entry : dictionary) {
if (absl::StrContains(temp, entry.Contents)) { if (absl::StrContains(temp, entry.Contents)) {
temp = absl::StrReplaceAll(temp, {{entry.Contents, entry.Contents}}); temp = absl::StrReplaceAll(temp, {{entry.Contents, entry.Contents}});
} }
@@ -519,8 +476,8 @@ void MessageEditor::DrawCharacterToPreview(char c) {
DrawCharacterToPreview(FindMatchingCharacter(c)); DrawCharacterToPreview(FindMatchingCharacter(c));
} }
void MessageEditor::DrawCharacterToPreview(const std::vector<uint8_t>& text) { void MessageEditor::DrawCharacterToPreview(const std::vector<uint8_t> &text) {
for (const uint8_t& value : text) { for (const uint8_t &value : text) {
if (skip_next) { if (skip_next) {
skip_next = false; skip_next = false;
continue; continue;
@@ -632,7 +589,7 @@ absl::Status MessageEditor::Save() {
int pos = kTextData; int pos = kTextData;
bool in_second_bank = false; bool in_second_bank = false;
for (const auto& message : list_of_texts_) { for (const auto &message : list_of_texts_) {
for (const auto value : message.Data) { for (const auto value : message.Data) {
RETURN_IF_ERROR(rom()->WriteByte(pos, value)); RETURN_IF_ERROR(rom()->WriteByte(pos, value));
@@ -651,8 +608,8 @@ absl::Status MessageEditor::Save() {
pos++; pos++;
} }
RETURN_IF_ERROR( RETURN_IF_ERROR(rom()->WriteByte(
rom()->WriteByte(pos++, kMessageTerminator)); // , true, "Terminator text" pos++, kMessageTerminator)); // , true, "Terminator text"
} }
// Verify that we didn't go over the space available for the second block. // Verify that we didn't go over the space available for the second block.
@@ -673,8 +630,8 @@ std::string MessageEditor::DisplayTextOverflowError(int pos, bool bank) {
std::string posSTR = std::string posSTR =
bank ? absl::StrFormat("%X4", pos & 0xFFFF) bank ? absl::StrFormat("%X4", pos & 0xFFFF)
: absl::StrFormat("%X4", (pos - kTextData2) & 0xFFFF); : absl::StrFormat("%X4", (pos - kTextData2) & 0xFFFF);
std::string message = absl::StrFormat( std::string message =
"There is too much text data in the %s block to save.\n" absl::StrFormat("There is too much text data in the %s block to save.\n"
"Available: %X4 | Used: %s", "Available: %X4 | Used: %s",
bankSTR, space, posSTR); bankSTR, space, posSTR);
return message; return message;

View File

@@ -28,10 +28,6 @@ constexpr int kFontGfxMessageDepth = 8;
constexpr uint8_t kWidthArraySize = 100; constexpr uint8_t kWidthArraySize = 100;
constexpr uint8_t kBlockTerminator = 0x80; constexpr uint8_t kBlockTerminator = 0x80;
constexpr uint8_t kMessageBankChangeId = 0x80; constexpr uint8_t kMessageBankChangeId = 0x80;
constexpr uint8_t kScrollVertical = 0x73;
constexpr uint8_t kLine1 = 0x74;
constexpr uint8_t kLine2 = 0x75;
constexpr uint8_t kLine3 = 0x76;
static TextElement DictionaryElement = static TextElement DictionaryElement =
TextElement(0x80, DICTIONARYTOKEN, true, "Dictionary"); TextElement(0x80, DICTIONARYTOKEN, true, "Dictionary");