-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathBaseFile.cpp
294 lines (248 loc) · 7.45 KB
/
BaseFile.cpp
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#include "BaseFile.h"
#include <sstream>
SCRANTIC::BaseFile::BaseFile(const std::string &name) :
filename(name) {
size_t pos = filename.rfind('/');
if (pos != std::string::npos) {
filename = filename.substr(pos+1);
}
}
SCRANTIC::BaseFile::~BaseFile() {
}
v8 SCRANTIC::BaseFile::readFile(const std::string &filename) {
std::ifstream in;
in.open(filename, std::ios::binary | std::ios::in);
in.unsetf(std::ios::skipws);
u8 byte;
v8 data;
while (in.read((char*)&byte, 1)) {
data.push_back(byte);
}
in.close();
return data;
}
std::string SCRANTIC::BaseFile::readString(std::ifstream *in, u8 length, char delimiter) {
if (!in->is_open()) {
return "";
}
std::streampos pos = in->tellg();
std::streampos i = 0;
std::string str = "";
char c;
in->read(&c, 1);
i += 1;
while (c != delimiter) {
str += c;
in->read(&c, 1);
i += 1;
if ((length) && (i > length)) {
break;
}
}
if (length && (str.length() != length)) {
in->seekg(pos + static_cast<std::streampos>(length + 1), std::ios::beg);
}
return str;
}
std::string SCRANTIC::BaseFile::readString(v8::iterator &it, u8 length, char delimiter) {
std::string str = "";
u8 i = 0;
char c = (char)*it;
++it;
++i;
while (c != delimiter) {
str += c;
c = (char)*it;
++i;
++it;
if (length && (i > length)) {
break;
}
}
if (length) {
for ( ; i < length; ++i) {
++it;
}
}
return str;
}
std::string SCRANTIC::BaseFile::readConstString(std::ifstream *in, u8 length) {
if (!in->is_open()) {
return "";
}
char c;
std::string str = "";
for (u8 i = 0; i < length; ++i) {
in->read(&c, 1);
str += c;
}
return str;
}
std::string SCRANTIC::BaseFile::readConstString(v8::iterator &it, u8 length) {
std::string str = "";
for (u8 i = 0; i < length; ++i) {
str += (char)*it;
++it;
}
return str;
}
void SCRANTIC::BaseFile::assertString(v8::iterator &it, std::string expectedString) {
std::string actualString = readConstString(it, expectedString.size());
if (actualString != expectedString) {
std::cerr << filename << ": \"" << expectedString << "\" expected; got \"" << actualString << "\"" << std::endl;
throw BaseFileException();
}
}
void SCRANTIC::BaseFile::writeFile(const std::vector<u8> &data, std::string &name, std::string path) {
std::ofstream out;
if (path.length() && (path[path.length()-1] != '/')) {
path += "/";
}
out.open(path + name, std::ios::binary | std::ios::out);
out.unsetf(std::ios::skipws);
if (!out.is_open()) {
std::cerr << "BaseFile: Could not open " << path + name << std::endl;
return;
}
for (auto const &i: data) {
out.write((char*)&i, 1);
}
out.close();
}
void SCRANTIC::BaseFile::writeFile(const std::string &data, std::string &name, std::string path) {
std::ofstream out;
if (path.length() && (path[path.length()-1] != '/')) {
path += "/";
}
out.open(path + name, std::ios::out);
if (!out.is_open()) {
std::cerr << "BaseFile: Could not open " << path + name << std::endl;
return;
}
out << data;
out.close();
}
std::string SCRANTIC::BaseFile::commandToString(Command cmd, bool ads) {
std::string ret = " ";
std::string hex;
size_t len = 4;
for (size_t i = 0; i < cmd.data.size(); ++i) {
hex = hexToString(cmd.data.at(i), std::hex);
for (size_t j = hex.size(); j < len; ++j)
hex = "0" + hex;
ret += hex + " ";
}
if (cmd.name.length()) {
ret += cmd.name;
}
if (!ads) {
switch (cmd.opcode) {
case CMD_CLEAR_IMGSLOT:
return "Clear Image Slot" + ret;
case CMD_PURGE:
return "Purge" + ret;
case CMD_UPDATE:
return "Update" + ret;
case CMD_DELAY:
return "Delay" + ret;
case CMD_SEL_SLOT_IMG:
return "Select Image Slot" + ret;
case CMD_SEL_SLOT_PAL:
return "Select Palette Slot" + ret;
case CMD_SET_SCENE_LABEL:
return "Label Scene" + ret;
case CMD_SET_SCENE:
return "New Scene" + ret;
case CMD_UNK_1120:
return "Unkown 0x1120" + ret;
case CMD_JMP_SCENE:
return "Jump to Scene" + ret;
case CMD_SET_COLOR:
return "Set Color" + ret;
case CMD_UNK_2010:
return "Unkown 0x2010" + ret;
case CMD_TIMER:
return "Timer" + ret;
case CMD_CLIP_REGION:
return "Clip Region" + ret;
case CMD_SAVE_IMAGE:
return "Save Image" + ret;
case CMD_SAVE_IMAGE_NEW:
return "Save New Image" + ret;
case CMD_DRAW_PIXEL:
return "Draw Pixel" + ret;
case CMD_UNK_A050:
return "Unkown 0xA050" + ret;
case CMD_UNK_A060:
return "Unkown 0xA060" + ret;
case CMD_DRAW_LINE:
return "Draw Line" + ret;
case CMD_DRAW_RECTANGLE:
return "Draw Rectangle" + ret;
case CMD_DRAW_ELLIPSE:
return "Draw Ellipse" + ret;
case CMD_DRAW_SPRITE:
return "Draw Sprite (normal)" + ret;
case CMD_DRAW_SPRITE_MIRROR:
return "Draw Sprite (mirror)" + ret;
case CMD_CLEAR_RENDERER:
return "Clear Renderer" + ret;
case CMD_UNK_B600:
return "Unkown 0xB600" + ret;
case CMD_PLAY_SOUND:
return "Play Sound" + ret;
case CMD_LOAD_SCREEN:
return "Load Screen" + ret;
case CMD_LOAD_BITMAP:
return "Load Bitmap" + ret;
case CMD_LOAD_PALETTE:
return "Load Palette" + ret;
default:
return "DEFAULT 0x" + hexToString(cmd.opcode, std::hex) + ret;
}
} else {
switch (cmd.opcode) {
// ADS instructions
case CMD_SET_SCENE:
return "New ADS Movie" + ret;
case CMD_UNK_1070:
return "Unkown 0x1070" + ret;
case CMD_ADD_INIT_TTM:
return "Add Init TTM" + ret;
case CMD_AFTER_SCENE:
return "Label" + ret;
case CMD_SKIP_IF_PLAYED:
return "Skip Next If Played" + ret;
case CMD_ONLY_IF_PLAYED:
return "Play only with" + ret;
case CMD_OR_SKIP:
return "Or (Skip)" + ret;
case CMD_OR_AFTER:
return "Or (After)" + ret;
case CMD_PLAY_MOVIE:
return "Play Movie" + ret;
case CMD_UNK_1520:
return "Unkown 0x1520" + ret;
case CMD_ADD_TTM:
return "Add TTM" + ret;
case CMD_KILL_TTM:
return "Kill TTM" + ret;
case CMD_RANDOM_START:
return "Random Start" + ret;
case CMD_ZERO_CHANCE:
return "Zero chance" + ret;
case CMD_RANDOM_END:
return "Random End" + ret;
case CMD_UNK_LABEL:
return "Unkown 0x4000" + ret;
case CMD_UNK_F010:
return "Unkown 0xF010" + ret;
case CMD_PLAY_ADS_MOVIE:
return "Play ADS Movie" + ret;
case CMD_END_SCRIPT:
return "End script" + ret;
default:
return "DEFAULT 0x" + hexToString(cmd.opcode, std::hex) + ret;
}
}
}