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 empty_element;
for (const auto& text_element : TextCommands) {
for (const auto &text_element : TextCommands) {
if (text_element.ID == b) {
return text_element;
}
@@ -33,7 +33,7 @@ TextElement FindMatchingCommand(uint8_t b) {
TextElement FindMatchingSpecial(uint8_t value) {
auto it = std::find_if(SpecialChars.begin(), SpecialChars.end(),
[value](const TextElement& text_element) {
[value](const TextElement &text_element) {
return text_element.ID == value;
});
if (it != SpecialChars.end()) {
@@ -43,9 +43,9 @@ TextElement FindMatchingSpecial(uint8_t value) {
return TextElement();
}
ParsedElement FindMatchingElement(const std::string& str) {
ParsedElement FindMatchingElement(const std::string &str) {
std::smatch match;
for (auto& textElement : TextCommands) {
for (auto &textElement : TextCommands) {
match = textElement.MatchMe(str);
if (match.size() > 0) {
if (textElement.HasArgument) {
@@ -144,7 +144,7 @@ std::vector<uint8_t> ParseMessageToData(std::string str) {
return bytes;
}
std::vector<DictionaryEntry> BuildDictionaryEntries(Rom* rom) {
std::vector<DictionaryEntry> BuildDictionaryEntries(Rom *rom) {
std::vector<DictionaryEntry> AllDictionaries;
for (int i = 0; i < kNumDictionaryEntries; i++) {
std::vector<uint8_t> bytes;
@@ -169,12 +169,49 @@ std::vector<DictionaryEntry> BuildDictionaryEntries(Rom* rom) {
}
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 AllDictionaries;
}
} // namespace editor
} // namespace yaze
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 yaze

View File

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

View File

@@ -27,7 +27,6 @@ using ImGui::BeginTable;
using ImGui::Button;
using ImGui::EndChild;
using ImGui::EndTable;
using ImGui::InputText;
using ImGui::InputTextMultiline;
using ImGui::SameLine;
using ImGui::Separator;
@@ -45,22 +44,21 @@ constexpr ImGuiTableFlags kDictTableFlags =
ImGuiTableFlags_Borders | ImGuiTableFlags_Resizable;
absl::Status MessageEditor::Initialize() {
for (int i = 0; i < kWidthArraySize; i++) {
width_array[i] = rom()->data()[kCharactersWidth + i];
}
std::copy(rom()->vector().begin() + kCharactersWidth,
rom()->vector().begin() + kCharactersWidth + kWidthArraySize,
width_array.begin());
all_dictionaries_ = BuildDictionaryEntries(rom());
ReadAllTextDataV2();
font_preview_colors_.AddColor(0x7FFF); // White
font_preview_colors_.AddColor(0x7C00); // Red
font_preview_colors_.AddColor(0x03E0); // Green
font_preview_colors_.AddColor(0x001F); // Blue
font_preview_colors_.AddColor(0x7FFF); // White
font_preview_colors_.AddColor(0x7C00); // Red
font_preview_colors_.AddColor(0x03E0); // Green
font_preview_colors_.AddColor(0x001F); // Blue
std::vector<uint8_t> data(0x4000, 0);
for (int i = 0; i < 0x4000; i++) {
data[i] = rom()->data()[kGfxFont + i];
}
std::copy(rom()->vector().begin() + kGfxFont,
rom()->vector().begin() + kGfxFont + 0x4000, data.begin());
font_gfx16_data_ = gfx::SnesTo8bppSheet(data, /*bpp=*/2, /*num_sheets=*/2);
// 4bpp
@@ -79,50 +77,9 @@ absl::Status MessageEditor::Initialize() {
kCurrentMessageWidth, kCurrentMessageHeight, 64, current_font_gfx16_data_,
current_font_gfx16_bitmap_, font_preview_colors_))
gfx::SnesPalette color_palette = font_gfx_bitmap_.palette();
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);
}
*font_gfx_bitmap_.mutable_palette() = font_preview_colors_;
parsed_messages_ = ParseMessageData(list_of_texts_, all_dictionaries_);
DrawMessagePreview();
return absl::OkStatus();
@@ -171,7 +128,7 @@ void MessageEditor::DrawMessageList() {
TableHeadersRow();
for (const auto& message : list_of_texts_) {
for (const auto &message : list_of_texts_) {
TableNextColumn();
if (Button(core::HexWord(message.ID).c_str())) {
current_message_ = message;
@@ -180,9 +137,8 @@ void MessageEditor::DrawMessageList() {
TableNextColumn();
TextWrapped("%s", parsed_messages_[message.ID].c_str());
TableNextColumn();
TextWrapped(
"%s",
core::HexLong(list_of_texts_[message.ID].Address).c_str());
TextWrapped("%s",
core::HexLong(list_of_texts_[message.ID].Address).c_str());
}
EndTable();
@@ -232,7 +188,7 @@ void MessageEditor::DrawCurrentMessage() {
void MessageEditor::DrawTextCommands() {
if (BeginChild("##TextCommands", ImVec2(0, 0), true,
ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
for (const auto& text_element : TextCommands) {
for (const auto &text_element : TextCommands) {
if (Button(text_element.GenericToken.c_str())) {
}
SameLine();
@@ -250,7 +206,7 @@ void MessageEditor::DrawDictionary() {
TableSetupColumn("ID");
TableSetupColumn("Contents");
for (const auto& dictionary : all_dictionaries_) {
for (const auto &dictionary : all_dictionaries_) {
TableNextColumn();
Text("%s", core::HexWord(dictionary.ID).c_str());
TableNextColumn();
@@ -337,9 +293,9 @@ void MessageEditor::ReadAllTextDataV2() {
uint32_t address = core::Get24LocalFromPC(
rom()->mutable_data(), kPointersDictionaries + (dictionary * 2));
uint32_t address_end = core::Get24LocalFromPC(
rom()->mutable_data(),
kPointersDictionaries + ((dictionary + 1) * 2));
uint32_t address_end = core::Get24LocalFromPC(rom()->mutable_data(),
kPointersDictionaries +
((dictionary + 1) * 2));
for (uint32_t i = address; i < address_end; i++) {
parsed_message.push_back(rom()->data()[i]);
@@ -437,8 +393,9 @@ void MessageEditor::ReadAllTextData() {
uint32_t address = core::Get24LocalFromPC(
rom()->mutable_data(), kPointersDictionaries + (dictionary * 2));
uint32_t address_end = core::Get24LocalFromPC(
rom()->mutable_data(), kPointersDictionaries + ((dictionary + 1) * 2));
uint32_t address_end = core::Get24LocalFromPC(rom()->mutable_data(),
kPointersDictionaries +
((dictionary + 1) * 2));
for (uint32_t i = address; i < address_end; i++) {
temp_bytes_parsed.push_back(rom()->data()[i]);
@@ -462,7 +419,7 @@ void MessageEditor::ReadAllTextData() {
std::string ReplaceAllDictionaryWords(std::string str,
std::vector<DictionaryEntry> dictionary) {
std::string temp = str;
for (const auto& entry : dictionary) {
for (const auto &entry : dictionary) {
if (absl::StrContains(temp, entry.Contents)) {
temp = absl::StrReplaceAll(temp, {{entry.Contents, entry.Contents}});
}
@@ -480,7 +437,7 @@ DictionaryEntry MessageEditor::GetDictionaryFromID(uint8_t value) {
void MessageEditor::DrawTileToPreview(int x, int y, int srcx, int srcy, int pal,
int sizex, int sizey) {
const int num_x_tiles = 16;
const int img_width = 512; // (imgwidth/2)
const int img_width = 512; // (imgwidth/2)
int draw_id = srcx + (srcy * 32);
for (int yl = 0; yl < sizey * 8; yl++) {
for (int xl = 0; xl < 4; xl++) {
@@ -519,8 +476,8 @@ void MessageEditor::DrawCharacterToPreview(char c) {
DrawCharacterToPreview(FindMatchingCharacter(c));
}
void MessageEditor::DrawCharacterToPreview(const std::vector<uint8_t>& text) {
for (const uint8_t& value : text) {
void MessageEditor::DrawCharacterToPreview(const std::vector<uint8_t> &text) {
for (const uint8_t &value : text) {
if (skip_next) {
skip_next = false;
continue;
@@ -555,7 +512,7 @@ void MessageEditor::DrawCharacterToPreview(const std::vector<uint8_t>& text) {
skip_next = true;
continue;
} else if (value == 0x6C) // BCD numbers.
} else if (value == 0x6C) // BCD numbers.
{
DrawCharacterToPreview('0');
skip_next = true;
@@ -632,7 +589,7 @@ absl::Status MessageEditor::Save() {
int pos = kTextData;
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) {
RETURN_IF_ERROR(rom()->WriteByte(pos, value));
@@ -651,8 +608,8 @@ absl::Status MessageEditor::Save() {
pos++;
}
RETURN_IF_ERROR(
rom()->WriteByte(pos++, kMessageTerminator)); // , true, "Terminator text"
RETURN_IF_ERROR(rom()->WriteByte(
pos++, kMessageTerminator)); // , true, "Terminator text"
}
// Verify that we didn't go over the space available for the second block.
@@ -662,7 +619,7 @@ absl::Status MessageEditor::Save() {
return absl::InternalError(DisplayTextOverflowError(pos, false));
}
RETURN_IF_ERROR(rom()->WriteByte(pos, 0xFF)); // , true, "End of text"
RETURN_IF_ERROR(rom()->WriteByte(pos, 0xFF)); // , true, "End of text"
return absl::OkStatus();
}
@@ -673,10 +630,10 @@ std::string MessageEditor::DisplayTextOverflowError(int pos, bool bank) {
std::string posSTR =
bank ? absl::StrFormat("%X4", pos & 0xFFFF)
: absl::StrFormat("%X4", (pos - kTextData2) & 0xFFFF);
std::string message = absl::StrFormat(
"There is too much text data in the %s block to save.\n"
"Available: %X4 | Used: %s",
bankSTR, space, posSTR);
std::string message =
absl::StrFormat("There is too much text data in the %s block to save.\n"
"Available: %X4 | Used: %s",
bankSTR, space, posSTR);
return message;
}
@@ -699,5 +656,5 @@ void MessageEditor::SelectAll() {
}
}
} // namespace editor
} // namespace yaze
} // namespace editor
} // namespace yaze

View File

@@ -28,10 +28,6 @@ constexpr int kFontGfxMessageDepth = 8;
constexpr uint8_t kWidthArraySize = 100;
constexpr uint8_t kBlockTerminator = 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 =
TextElement(0x80, DICTIONARYTOKEN, true, "Dictionary");