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;
}
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,6 +76,10 @@ 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);
@@ -98,12 +102,8 @@ struct MessageData {
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) {}
: ID(id), Address(address), RawString(rawString), Data(rawData),
DataParsed(parsedData), ContentsParsed(parsedString) {}
// Copy constructor
MessageData(const MessageData &other) {
@@ -119,8 +119,8 @@ struct MessageData {
return absl::StrFormat("%0X - %s", ID, ContentsParsed);
}
std::string OptimizeMessageForDictionary(
std::string messageString,
std::string
OptimizeMessageForDictionary(std::string messageString,
const std::vector<DictionaryEntry> &dictionary) {
std::stringstream protons;
bool command = false;
@@ -153,6 +153,8 @@ struct MessageData {
}
};
struct TextElement {
uint8_t ID;
std::string Token;
@@ -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 = {
@@ -300,6 +301,10 @@ ParsedElement FindMatchingElement(const std::string& str);
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 yaze

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,9 +44,9 @@ 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();
@@ -58,9 +57,8 @@ absl::Status MessageEditor::Initialize() {
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();
@@ -180,8 +137,7 @@ void MessageEditor::DrawMessageList() {
TableNextColumn();
TextWrapped("%s", parsed_messages_[message.ID].c_str());
TableNextColumn();
TextWrapped(
"%s",
TextWrapped("%s",
core::HexLong(list_of_texts_[message.ID].Address).c_str());
}
@@ -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]);
@@ -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.
@@ -673,8 +630,8 @@ 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"
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;

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");