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

@@ -176,5 +176,42 @@ std::vector<DictionaryEntry> BuildDictionaryEntries(Rom* rom) {
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,6 +76,10 @@ 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);
@@ -98,12 +102,8 @@ 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), : 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) {
@@ -119,8 +119,8 @@ 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;
@@ -153,6 +153,8 @@ struct MessageData {
} }
}; };
struct TextElement { struct TextElement {
uint8_t ID; uint8_t ID;
std::string Token; std::string Token;
@@ -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 = {
@@ -300,6 +301,10 @@ 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();
@@ -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());
} }
@@ -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]);
@@ -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");