summaryrefslogtreecommitdiff
path: root/ZAPD/ZTextMM.cpp
blob: 7c57941f929e7f04b8fef1e1c2e5049b32a7571e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#include "ZTextMM.h"

#include "Globals.h"
#include "Utils/BitConverter.h"
#include <Utils/DiskFile.h>
#include "Utils/Path.h"
#include "Utils/StringHelper.h"
#include "ZFile.h"

REGISTER_ZFILENODE(TextMM, ZTextMM);

ZTextMM::ZTextMM(ZFile* nParent) : ZResource(nParent)
{
	RegisterOptionalAttribute("CodeOffset");
	RegisterOptionalAttribute("SegmentName");
	RegisterOptionalAttribute("LangOffset", "0");
}

void ZTextMM::ParseRawData()
{
	ZResource::ParseRawData();

	ParseMM();
}

void ZTextMM::ParseMM()
{
	const auto& rawData = parent->GetRawData();
	uint32_t currentPtr; 
	bool messageTableInOwnSegment = false;
	
	if (StringHelper::StrToL(registeredAttributes.at("CodeOffset").value, 16) != 0)
	{
		currentPtr = StringHelper::StrToL(registeredAttributes.at("CodeOffset").value, 16);
		messageTableInOwnSegment = false;
	}
	else
	{
		currentPtr = 0;
		messageTableInOwnSegment = true;
	}
	uint32_t langPtr = currentPtr;
	bool isPalLang = false;

	if (StringHelper::StrToL(registeredAttributes.at("LangOffset").value, 16) != 0)
	{
		langPtr = StringHelper::StrToL(registeredAttributes.at("LangOffset").value, 16);

		if (langPtr != currentPtr)
			isPalLang = true;
	}

	std::vector<uint8_t> codeData;

	if (Globals::Instance->fileMode == ZFileMode::ExtractDirectory)
		if (messageTableInOwnSegment)
		{
			if (registeredAttributes.at("SegmentName").value == "")
			{
				throw std::runtime_error(
					StringHelper::Sprintf("ZTextMM: Missing one of required attributes: CodeOffset or SegmentName"));
			}

			codeData = Globals::Instance->GetBaseromFile(registeredAttributes.at("SegmentName").value);
		}
		else {
			codeData = Globals::Instance->GetBaseromFile("code");
		}
	else
		if (messageTableInOwnSegment)
		{
			if (registeredAttributes.at("SegmentName").value == "")
			{
				throw std::runtime_error(
					StringHelper::Sprintf("ZTextMM: Missing one of required attributes: CodeOffset or SegmentName"));
			}
			codeData = Globals::Instance->GetBaseromFile(Globals::Instance->baseRomPath.string()
						+ registeredAttributes.at("SegmentName").value);
		}
		else
		{
			codeData =
				Globals::Instance->GetBaseromFile(Globals::Instance->baseRomPath.string() + "code");
		}

	while (true)
	{
		MessageEntryMM msgEntry;
		msgEntry.id = BitConverter::ToInt16BE(codeData, currentPtr + 0);
		msgEntry.msgOffset = BitConverter::ToInt32BE(codeData, langPtr + 4) & 0x00FFFFFF;

		uint32_t msgPtr = msgEntry.msgOffset;

		msgEntry.segmentId = (codeData[langPtr + 4]);

		// NES and STAFF has different control codes and arg sizes, so we parse them separately
		if (parent->GetName() == "staff_message_data_static") { // STAFF
			// Credits has no header
			// The textbox Type and Pos are in the code table just after the text ID as a single byte
			uint8_t typePos = (codeData[currentPtr + 2]);
			msgEntry.textboxType = (typePos & 0xF0) >> 4;
			msgEntry.textboxYPos = typePos & 0x0F;

			// The rest of these are unused and not provided, so we set them to zero;
			msgEntry.textboxType = 0;
			msgEntry.icon = 0;
			msgEntry.nextMessageID = 0;
			msgEntry.firstItemCost = 0;
			msgEntry.secondItemCost = 0;
		
			unsigned char c = rawData[msgPtr];

			// Read until end marker (0x02)
			while (true) {
				msgEntry.msg += c;

				if (c == 0x02) { // End marker
					break;
				}

				switch (c) {
					case 0x05: // Color: Change text color
					case 0x06: // Shift: Print xx Spaces
					case 0x0E: // Fade: Keep Text on Screen for xxxx Before Closing
					case 0x13: // Item Icon:
					case 0x14: // Text Speed:
					case 0x1E: // Highscores: Prints xx highscore value
						msgEntry.msg += rawData[msgPtr + 1];
						msgPtr++;
						break;
					case 0x07: // TextID: Open xxxx textID
					case 0x0C: // Box Break Delay: Delay for xxxx Before Printing Remaining Text
					case 0x11: // Fade2: Alternate delay
					case 0x12: // SFX: Play Sound Effect xxxx
						msgEntry.msg += rawData[msgPtr + 1];
						msgEntry.msg += rawData[msgPtr + 2];
						msgPtr += 2;
						break;
					case 0x15: // Background
						msgEntry.msg += rawData[msgPtr + 1];
						msgEntry.msg += rawData[msgPtr + 2];
						msgEntry.msg += rawData[msgPtr + 3];
						msgPtr += 3;
						break;
				}

				msgPtr++;
				c = rawData[msgPtr];
			}
		} else if (parent->GetName() == "message_data_static_jp") {
			msgEntry.textboxType = (rawData[msgPtr + 0]);
			msgEntry.textboxYPos = (rawData[msgPtr + 1]);
			msgEntry.icon = BitConverter::ToUInt16BE(rawData, msgPtr + 2);
			msgEntry.nextMessageID = BitConverter::ToInt16BE(rawData, msgPtr + 4);
			msgEntry.firstItemCost = BitConverter::ToInt16BE(rawData, msgPtr + 6);
			msgEntry.secondItemCost = BitConverter::ToInt16BE(rawData, msgPtr + 8);

			msgPtr += 12;

			uint16_t c = BitConverter::ToUInt16BE(rawData, msgPtr);

			// Read until end marker (0x0500)
			while (true) {
				msgEntry.msg += rawData[msgPtr + 1];
				msgEntry.msg += rawData[msgPtr];

				if (c == 0x0500) { // End marker
					break;
				}

				switch (c) {
					case 0x001F: // Shift: Print 00xx Spaces
						msgEntry.msg += rawData[msgPtr + 3];
						msgEntry.msg += rawData[msgPtr + 2];
						msgPtr += 2;
						break;
					case 0x0110: // Box Break Delay: Delay for xxxx Before Printing Remaining Text
					case 0x0111: // Fade: Keep Text on Screen for xxxx Before Closing
					case 0x0112: // Fade Skippable: Delay for xxxx Before Ending Conversation
					case 0x0120: // SFX: Play Sound Effect xxxx
					case 0x0128: // Delay: Delay for xxxx Before Resuming Text Flow
						msgEntry.msg += rawData[msgPtr + 3];
						msgEntry.msg += rawData[msgPtr + 2];
						msgPtr += 2;
						break;
				}

				msgPtr += 2;
				c = BitConverter::ToUInt16BE(rawData, msgPtr);
			}
		} else { // NES
			// NES has a header with extra information, parse that and move the ptr forward
			msgEntry.textboxType = (rawData[msgPtr + 0]);
			msgEntry.textboxYPos = (rawData[msgPtr + 1]);
			msgEntry.icon = (rawData[msgPtr + 2]);
			msgEntry.nextMessageID = BitConverter::ToInt16BE(rawData, msgPtr + 3);
			msgEntry.firstItemCost = BitConverter::ToInt16BE(rawData, msgPtr + 5);
			msgEntry.secondItemCost = BitConverter::ToInt16BE(rawData, msgPtr + 7);

			msgPtr += 11;

			unsigned char c = rawData[msgPtr];

			// Read until end marker (0xBF)
			while (true) {
				msgEntry.msg += c;

				if (c == 0xBF) { // End marker
					break;
				}

				switch (c) {
					case 0x14: // Shift: Print xx Spaces
						msgEntry.msg += rawData[msgPtr + 1];
						msgPtr++;
						break;
					case 0x1B: // Box Break Delay: Delay for xxxx Before Printing Remaining Text
					case 0x1C: // Fade: Keep Text on Screen for xxxx Before Closing
					case 0x1D: // Fade Skippable: Delay for xxxx Before Ending Conversation
					case 0x1E: // SFX: Play Sound Effect xxxx
					case 0x1F: // Delay: Delay for xxxx Before Resuming Text Flow
						msgEntry.msg += rawData[msgPtr + 1];
						msgEntry.msg += rawData[msgPtr + 2];
						msgPtr += 2;
						break;
				}

				msgPtr++;
				c = rawData[msgPtr];
			}
		}

		messages.push_back(msgEntry);

		if (msgEntry.id == 0xFFFC || msgEntry.id == 0xFFFF)
			break;

		currentPtr += 8;

		if (isPalLang)
			langPtr += 4;
		else
			langPtr += 8;
	}
}

std::string ZTextMM::GetSourceTypeName() const
{
	return "u8";
}

size_t ZTextMM::GetRawDataSize() const
{
	return 1;
}

ZResourceType ZTextMM::GetResourceType() const
{
	return ZResourceType::TextMM;
}