summaryrefslogtreecommitdiff
path: root/source/Twilight Editor.cpp
blob: 68dc3a1c7e1576815724427dde93087871c485dd (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
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
#include "Twilight Editor.h"


int main(int argc, char* argv[])
{
	auto result = parse(argc, argv);
	auto arguments = result.arguments();

	if (TwilightEditor::Settings.VERBOSE)
	{
		std::cout << "Settings:" << std::endl;
		std::cout << "Input File: " << TwilightEditor::Settings.INPUT_FILE << std::endl;
		std::cout << "Output File: " << TwilightEditor::Settings.OUTPUT_FILE << std::endl;
		std::cout << "Mode: " << TwilightEditor::Settings.MODE << std::endl;
		std::cout << "QuestLog: " << TwilightEditor::Settings.QUESTLOG << std::endl;
		std::cout << "Fix checksum: " << TwilightEditor::Settings.FIX << std::endl;


		std::cout << "Offset: " << TwilightEditor::Settings.OFFSET << std::endl;
		std::cout << "Length: " << TwilightEditor::Settings.LENGTH << std::endl;
		std::cout << "Type: " << TwilightEditor::Settings.TYPE << std::endl;
		std::cout << "Value: " << TwilightEditor::Settings.STR_VALUE << std::endl;

		std::cout << "Verbose: " << TwilightEditor::Settings.VERBOSE << std::endl;
		std::cout << "Force: " << TwilightEditor::Settings.FORCE << std::endl;
	}

	fastPrint("initializing file...");

	TwilightEditor::initFile();

	TwilightEditor::getChecksums();
	return 0;

	switch (TwilightEditor::Settings.MODE)
	{
		case TwilightEditor::mode::GET:
			fastPrint("> GET");

			std::cout << TwilightEditor::get() << std::endl;
		break;

		case TwilightEditor::mode::SET:
			fastPrint("> SET");

			TwilightEditor::set();
		break;
	}

	fastPrint("Closing file(s)...");
	fclose(TwilightEditor::currentFilePtr);
	fclose(TwilightEditor::outputFile);
	return 0;
}

void fastPrint(const std::string& message, bool newline)
{
	if (TwilightEditor::Settings.VERBOSE)
	{
		std::cout << message;
		if (newline) std::cout << std::endl;
	}
}

cxxopts::ParseResult parse(int argc, char* argv[])
{
	try
	{
		cxxopts::Options options(argv[0], "Twilight Editor | (C) AECX 2019 | *required");

		options.add_options("General")
			("h,help", " Prints this help")
			("i,input", "*(string) Path to input file", cxxopts::value<std::string>(), "<path/to/file>")
			("m,mode", " (string) <GET> or <SET>", cxxopts::value<std::string>()->default_value("GET"), "<GET|SET>")
			("o,output", " (string) If empty: Override input file", cxxopts::value<std::string>(), "<path/to/file>")
			("q,questlog", " (int) QuestLog Index (0 based)", cxxopts::value<std::uint16_t>()->default_value("0"), "<n>")
			("fix", " (bool) Correct checksum?", cxxopts::value<bool>()->default_value("false"))
			("hex", " (bool) Output in Hexadecimal format?", cxxopts::value<bool>()->default_value("false"));

		options.add_options("Offset")
			("offset", "*(hex/dec) QuestLog offset", cxxopts::value<uint16_t>(), "<0x000>")
			("length", "*(hex/dec) Length", cxxopts::value<uint16_t>(), "<0x00>")
			("type", "*(string) Value type/interpretation", cxxopts::value<std::string>(), "<int|flg|uflg|str>")
			("value", "(string) The value you want to apply", cxxopts::value<std::string>()->default_value("0"), "<\"Link\">");

		options.add_options("Additional")
			("v,version", " Display current version information")
			("verbose", " Print additional output information", cxxopts::value<bool>()->default_value("false"))
			("f,force", " Interpret file as raw questlog", cxxopts::value<bool>()->default_value("false"));

		auto result = options.parse(argc, argv);

		if (result.count("help"))
		{
			std::cout << options.help({ "General", "Offset", "Additional" }) << std::endl;
			exit(0);
		}

		if (result.count("version"))
		{
			std::cout << "Twilight Editor (" << OS_STRING << ") v" << TE_VERSION_MAIN << "." << TE_VERSION_SUB << " (C) AECX 2019" << std::endl;
			std::cout << "https://editor.tpspeed.run | https://aecx.cc | https://twitter.com/theAECX" << std::endl;
			exit(0);
		}

		// Set values to global settings struct
		if (result.count("input"))
		{
			TwilightEditor::Settings.INPUT_FILE = result["input"].as<std::string>();
		}
		else
		{
			throw cxxopts::OptionException("Missing parameter --input");
		}

		TwilightEditor::Settings.MODE = (result["mode"].as<std::string>() == "GET" ? TwilightEditor::mode::GET : TwilightEditor::mode::SET);

		if (result.count("output"))
		{
			TwilightEditor::Settings.OUTPUT_FILE = result["output"].as<std::string>();
		}
		else
		{
			// Overwrite original
			TwilightEditor::Settings.OUTPUT_FILE = TwilightEditor::Settings.INPUT_FILE;
		}

		TwilightEditor::Settings.QUESTLOG = result["questlog"].as<std::uint16_t>();
		if (TwilightEditor::Settings.QUESTLOG > 2)
		{
			throw cxxopts::OptionException("Invalid QuestLog Index");
		}

		if (result.count("hex"))
		{
			TwilightEditor::Settings.HEX = result["hex"].as<bool>();
		}

		TwilightEditor::Settings.FIX = result["fix"].as<bool>();

		if (result.count("offset"))
		{
			TwilightEditor::Settings.OFFSET = result["offset"].as<uint16_t>();
		}
		else
		{
			throw cxxopts::OptionException("Missing parameter --offset");
		}

		if (result.count("length"))
		{
			TwilightEditor::Settings.LENGTH = result["length"].as<uint16_t>();
		}
		else
		{
			throw cxxopts::OptionException("Missing parameter --length");
		}

		// --type
		if (result.count("type"))
		{
			TwilightEditor::Settings.TYPE = (result["type"].as<std::string>() == "int" ? TwilightEditor::offsetType::TPINT : result["type"].as<std::string>() == "flg" ? TwilightEditor::offsetType::TPFLAG : result["type"].as<std::string>() == "uflg" ? TwilightEditor::offsetType::TPUFLAG : TwilightEditor::offsetType::TPSTRING);
			if (TwilightEditor::Settings.TYPE == TwilightEditor::offsetType::TPFLAG || TwilightEditor::Settings.TYPE == TwilightEditor::offsetType::TPUFLAG)
			{
				fastPrint("Changed size to 1 since you're supplying a flag!");
				TwilightEditor::Settings.LENGTH = 1;
			}
		}

		// --value
		if (result.count("value"))
		{
			TwilightEditor::Settings.STR_VALUE = result["value"].as<std::string>();
		}

		// -v,	--verbose
		TwilightEditor::Settings.VERBOSE = result["verbose"].as<bool>();

		// -f,	--force
		TwilightEditor::Settings.FORCE = result["force"].as<bool>();

		return result;
	}
	catch (const cxxopts::OptionException& e)
	{
		std::cout << "error parsing options: " << e.what() << std::endl << "Maybe try --help" << std::endl;
		exit(1);
	}
}

namespace TwilightEditor
{
	uint32_t checksum = 0;
	uint32_t nchecksum = 0;

	FILE* currentFilePtr = nullptr;
	FILE* outputFile = nullptr;

	std::size_t fileSize = 0;
	uint16_t questLogSize = 0;
	uint32_t questLogReadOffset = 0;

	std::string get()
	{
		try
		{
			if ((Settings.OFFSET + questLogReadOffset) + Settings.LENGTH > fileSize)
			{
				throw std::runtime_error("offset + length exceeds the filesize");
			}

			if (Settings.OFFSET + Settings.LENGTH > questLogSize)
			{
				throw std::runtime_error("offset + length exceeds the QuestLog size");
			}

			uint8_t* buffer = new uint8_t[Settings.LENGTH];
			fseek(currentFilePtr, questLogReadOffset + Settings.OFFSET, SEEK_SET);
			fread(buffer, sizeof(uint8_t), Settings.LENGTH, currentFilePtr);

			return TwilightEditor::Converter::toStr(buffer, Settings.TYPE, Settings.LENGTH, Converter::u8(Settings.STR_VALUE));
		}
		catch (const std::runtime_error& ex)
		{
			std::cerr << "error getting from file: " << ex.what() << std::endl;
			exit(1);
		}
		catch (...)
		{
			std::cerr << "unknown error while getting from file" << std::endl;
			exit(1);
		}
	}

	void set()
	{
		try
		{
			// write --value to --offset[--length]
			// Save into --output
			
			// Templates
			uint16_t t16 = 0;
			uint32_t t32 = 0;

			// buffer for str
			char* data = new char[Settings.LENGTH];
			memset(data, 0, Settings.LENGTH);

			// buffer for single char
			unsigned* buffer = new unsigned[1];

			std::size_t i = 0;
			for (i; i < fileSize; i++)
			{
				// Loop through entire file
				if (i == Settings.OFFSET)
				{
					// Modify this part

					// TODO convert --value to big endian and write it
					if (Settings.TYPE == offsetType::TPINT)
					{
						switch (Settings.LENGTH)
						{
							case 1:
								// Big endian == little endian, no conversion
								buffer[0] = Converter::u8(Settings.STR_VALUE);
								
								fwrite(buffer, sizeof(uint8_t), 1, outputFile);
							break;

							case 2:
								t16 = Converter::bigEndian(Converter::u16(Settings.STR_VALUE));

								fwrite(&t16, sizeof(uint16_t), 1, outputFile);
							break;

							case 4:
								t32 = Converter::bigEndian(Converter::u32(Settings.STR_VALUE));

								fwrite(&t32, sizeof(uint32_t), 1, outputFile);
							break;

							default:
								throw std::runtime_error("invalid length supplied for type int");
							break;
						}
					} // Integer

					else if (Settings.TYPE == offsetType::TPFLAG || Settings.TYPE == offsetType::TPUFLAG)
					{
						unsigned flag = Converter::u8(Settings.STR_VALUE);
						if (Settings.TYPE == offsetType::TPFLAG)
						{
							// Set the flag
							buffer[0] = fgetc(currentFilePtr) | flag;
						}
						else
						{
							// Unset the flag
							buffer[0] = (fgetc(currentFilePtr) & ~flag);
						}
						
						fwrite(buffer, sizeof(char), 1, outputFile);
					} // Flag

					else
					{
						std::strncpy(data, Settings.STR_VALUE.c_str(), Settings.LENGTH);

						fwrite(data, sizeof(char), Settings.LENGTH, outputFile);
					} // String/Data

					// Continue after the offset + length
					i += (Settings.LENGTH - 1); // - 1 because at the end of the loop it's ++ agian
					fseek(currentFilePtr, Settings.LENGTH, SEEK_CUR);
				}
				else
				{
					// Write next byte to input file
					buffer[0] = fgetc(currentFilePtr);
					fwrite(reinterpret_cast<void*>(buffer), sizeof(char), 1, outputFile);
				}
			}
		}
		catch (const std::runtime_error& ex)
		{
			std::cerr << "error setting to file: " << ex.what() << std::endl;
			exit(1);
		}
		catch (...)
		{
			std::cerr << "unknown error while setting to file" << std::endl;
			exit(1);
		}
	}

	void initFile()
	{
		try
		{
			if (!fs::exists(Settings.INPUT_FILE))
			{
				throw std::runtime_error("input file does not exist");
			}

			if (Settings.INPUT_FILE == Settings.OUTPUT_FILE)
			{
				throw std::runtime_error("currently unsupported. Please enter an output file");
			}

			fs::path filePath{ Settings.INPUT_FILE };

			currentFilePtr = fopen(Settings.INPUT_FILE.c_str(), "rb");
			outputFile = fopen(Settings.OUTPUT_FILE.c_str(), "wb");

			if (currentFilePtr == NULL)
			{
				throw std::runtime_error("couldn't open input file for read");
			}

			if (outputFile == NULL)
			{
				throw std::runtime_error("couldn't open output file for write");
			}

			fileSize = static_cast<std::size_t>(fs::file_size(filePath));
			
			switch (fileSize)
			{
				case 0x8040:
					// GCI/Container
					fastPrint("Operating in GCI mode");
					questLogSize = 0xA94;
					switch (Settings.QUESTLOG)
					{
						case 2:
							questLogReadOffset = 0x5570;
						break;

						case 1:
							questLogReadOffset = 0x4ADC;
						break;

						default:
							questLogReadOffset = 0x4048;
						break;
					}
				break;

				case 0xE00:
					// WiiU/QuestLog
					fastPrint("Operating in Wii U mode");
					questLogSize = 0xE00;
					questLogReadOffset = 0;
				break;

				default:
					if (!Settings.FORCE)
					{
						std::cout << "File could not be identified, do you want to treat it as a single QuestLog? (y/n)" << std::endl << "Is this a Questlog? ";
						int result = getchar();
						if (result != 'Y' && result != 'y')
						{
							std::cout << "Exiting application..." << std::endl;
							exit(1);
						}
						questLogSize = static_cast<uint16_t>(fileSize);
						questLogReadOffset = 0;
					}
				break;
			}
		}
		catch (const std::runtime_error& ex)
		{
			std::cerr << "error opening file " << Settings.INPUT_FILE  << ": " << ex.what() << std::endl;
			exit(1);
		}
		catch (...)
		{
			std::cerr << "unknown error opening file " << Settings.INPUT_FILE << std::endl;
			exit(1);
		}
	}

	void getChecksums()
	{
		try
		{
			uint16_t questLogDataLength = questLogSize - 8;

			uint8_t* questLogData = new uint8_t[questLogDataLength];

			printf("read: %x\n", questLogReadOffset);
			printf("len: %x\n", questLogDataLength);
			
			// Set read position to current questlog
			fseek(currentFilePtr, questLogReadOffset, SEEK_SET);

			// Read the entire questlog (- checksums)
			fread(questLogData, sizeof(uint8_t), questLogDataLength, currentFilePtr);

			checkSum(questLogData, questLogDataLength, &checksum, &nchecksum);

			printf("Calculated: %08x %08x\n", checksum, nchecksum);
		}
		catch (...)
		{
			std::cerr << "unknown error calculating checksums" << std::endl;
			exit(1);
		}
	}
}