yaze 0.2.0
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
message_data.cc
Go to the documentation of this file.
1#include "message_data.h"
2
3#include "app/core/common.h"
4
5namespace yaze {
6namespace app {
7namespace editor {
8
9uint8_t FindMatchingCharacter(char value) {
10 for (const auto [key, char_value] : CharEncoder) {
11 if (value == char_value) {
12 return key;
13 }
14 }
15 return 0xFF;
16}
17
18uint8_t FindDictionaryEntry(uint8_t value) {
19 if (value < DICTOFF || value == 0xFF) {
20 return -1;
21 }
22 return value - DICTOFF;
23}
24
26 TextElement empty_element;
27 for (const auto& text_element : TextCommands) {
28 if (text_element.ID == b) {
29 return text_element;
30 }
31 }
32 return empty_element;
33}
34
36 auto it = std::find_if(SpecialChars.begin(), SpecialChars.end(),
37 [value](const TextElement& text_element) {
38 return text_element.ID == value;
39 });
40 if (it != SpecialChars.end()) {
41 return *it;
42 }
43
44 return TextElement();
45}
46
47ParsedElement FindMatchingElement(const std::string& str) {
48 std::smatch match;
49 for (auto& textElement : TextCommands) {
50 match = textElement.MatchMe(str);
51 if (match.size() > 0) {
52 if (textElement.HasArgument) {
53 return ParsedElement(textElement,
54 std::stoi(match[1].str(), nullptr, 16));
55 } else {
56 return ParsedElement(textElement, 0);
57 }
58 }
59 }
60
61 const auto dictionary_element =
62 TextElement(0x80, DICTIONARYTOKEN, true, "Dictionary");
63
64 match = dictionary_element.MatchMe(str);
65 if (match.size() > 0) {
66 return ParsedElement(dictionary_element,
67 DICTOFF + std::stoi(match[1].str(), nullptr, 16));
68 }
69 return ParsedElement();
70}
71
72std::string ParseTextDataByte(uint8_t value) {
73 if (CharEncoder.contains(value)) {
74 char c = CharEncoder.at(value);
75 std::string str = "";
76 str.push_back(c);
77 return str;
78 }
79
80 // Check for command.
81 TextElement textElement = FindMatchingCommand(value);
82 if (!textElement.Empty()) {
83 return textElement.GenericToken;
84 }
85
86 // Check for special characters.
87 textElement = FindMatchingSpecial(value);
88 if (!textElement.Empty()) {
89 return textElement.GenericToken;
90 }
91
92 // Check for dictionary.
93 int dictionary = FindDictionaryEntry(value);
94 if (dictionary >= 0) {
95 return absl::StrFormat("[%s:%X]", DICTIONARYTOKEN, dictionary);
96 }
97
98 return "";
99}
100
101std::vector<uint8_t> ParseMessageToData(std::string str) {
102 std::vector<uint8_t> bytes;
103 std::string temp_string = str;
104 int pos = 0;
105
106 while (pos < temp_string.size()) {
107 // Get next text fragment.
108 if (temp_string[pos] == '[') {
109 int next = temp_string.find(']', pos);
110 if (next == -1) {
111 break;
112 }
113
114 ParsedElement parsedElement =
115 FindMatchingElement(temp_string.substr(pos, next - pos + 1));
116
117 const auto dictionary_element =
118 TextElement(0x80, DICTIONARYTOKEN, true, "Dictionary");
119
120 if (!parsedElement.Active) {
121 core::logf("Error parsing message: %s", temp_string);
122 break;
123 } else if (parsedElement.Parent == dictionary_element) {
124 bytes.push_back(parsedElement.Value);
125 } else {
126 bytes.push_back(parsedElement.Parent.ID);
127
128 if (parsedElement.Parent.HasArgument) {
129 bytes.push_back(parsedElement.Value);
130 }
131 }
132
133 pos = next + 1;
134 continue;
135 } else {
136 uint8_t bb = FindMatchingCharacter(temp_string[pos++]);
137
138 if (bb != 0xFF) {
139 core::logf("Error parsing message: %s", temp_string);
140 bytes.push_back(bb);
141 }
142 }
143 }
144
145 return bytes;
146}
147
148std::vector<DictionaryEntry> BuildDictionaryEntries(app::Rom* rom) {
149 std::vector<DictionaryEntry> AllDictionaries;
150 for (int i = 0; i < kNumDictionaryEntries; i++) {
151 std::vector<uint8_t> bytes;
152 std::stringstream stringBuilder;
153
154 int address = core::SnesToPc(
155 kTextData + (rom->data()[kPointersDictionaries + (i * 2) + 1] << 8) +
156 rom->data()[kPointersDictionaries + (i * 2)]);
157
158 int temppush_backress = core::SnesToPc(
159 kTextData +
160 (rom->data()[kPointersDictionaries + ((i + 1) * 2) + 1] << 8) +
161 rom->data()[kPointersDictionaries + ((i + 1) * 2)]);
162
163 while (address < temppush_backress) {
164 uint8_t uint8_tDictionary = rom->data()[address++];
165 bytes.push_back(uint8_tDictionary);
166 stringBuilder << ParseTextDataByte(uint8_tDictionary);
167 }
168
169 AllDictionaries.push_back(DictionaryEntry{(uint8_t)i, stringBuilder.str()});
170 }
171
172 std::sort(AllDictionaries.begin(), AllDictionaries.end(),
173 [](const DictionaryEntry& a, const DictionaryEntry& b) {
174 return a.Contents.size() > b.Contents.size();
175 });
176
177 return AllDictionaries;
178}
179
180} // namespace editor
181} // namespace app
182} // namespace yaze
The Rom class is used to load, save, and modify Rom data.
Definition rom.h:136
auto data()
Definition rom.h:463
uint32_t SnesToPc(uint32_t addr) noexcept
Definition common.h:226
const std::string DICTIONARYTOKEN
uint8_t FindDictionaryEntry(uint8_t value)
ParsedElement FindMatchingElement(const std::string &str)
constexpr uint8_t DICTOFF
constexpr int kPointersDictionaries
constexpr int kTextData
std::vector< DictionaryEntry > BuildDictionaryEntries(app::Rom *rom)
uint8_t FindMatchingCharacter(char value)
TextElement FindMatchingSpecial(uint8_t value)
TextElement FindMatchingCommand(uint8_t b)
std::vector< uint8_t > ParseMessageToData(std::string str)
constexpr int kNumDictionaryEntries
std::string ParseTextDataByte(uint8_t value)
Definition common.cc:22