-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
229 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
decap |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
CXX := g++ | ||
|
||
CXXFLAGS := -std=c++11 -O2 -Wall -Wno-switch -Werror | ||
|
||
SRCS := decap.cpp file.cpp | ||
|
||
HEADERS := decap.h file.h | ||
|
||
.PHONY: all clean | ||
|
||
all: decap | ||
@: | ||
|
||
decap: $(SRCS) $(HEADERS) | ||
$(CXX) $(CXXFLAGS) $(SRCS) -o $@ $(LDFLAGS) | ||
|
||
clean: | ||
$(RM) decap decap.exe |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#include "decap.h" | ||
#include "file.h" | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
if (argc < 3 || argc > 4) | ||
{ | ||
fprintf(stderr, "Usage: %s ROM_FILE OUTPUT_FILE\n", argv[0]); | ||
return 1; | ||
} | ||
|
||
File f = File(argv[1]); | ||
f.ProcessStringArray(0x1f716c, 11, 412); // Species names | ||
f.ProcessStringArray(0x1f8320, 13, 355); // Move names | ||
f.ProcessStringArray(0x1fa248, 13, 78); // Ability names | ||
f.ProcessStringArray(0x1f0208, 13, 58); // Trainer class names | ||
|
||
f.ProcessStringPointerArray(0x3c1004, 25); // Nature names | ||
f.ProcessStringPointerArray(0x3c1068, 8); // Summary Screen headers | ||
f.ProcessStringPointerArray(0x39f494, 10, 8); // Party menu options | ||
f.ProcessStringPointerArray(0x1fa110, 78); // Ability descriptions | ||
f.ProcessStringPointerArray(0x3e73c4 + 4, 88, 8); // Map names | ||
|
||
f.Save(argv[2]); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#ifndef DECAP_H | ||
#define DECAP_H | ||
|
||
#include <stdio.h> | ||
#include <stddef.h> | ||
|
||
#ifdef _MSC_VER | ||
|
||
#define FATAL_ERROR(format, ...) \ | ||
do \ | ||
{ \ | ||
fprintf(stderr, format, __VA_ARGS__); \ | ||
exit(1); \ | ||
} while (0) | ||
|
||
#else | ||
|
||
#define FATAL_ERROR(format, ...) \ | ||
do \ | ||
{ \ | ||
fprintf(stderr, format, ##__VA_ARGS__); \ | ||
exit(1); \ | ||
} while (0) | ||
|
||
#endif // _MSC_VER | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
#include "file.h" | ||
#include "decap.h" | ||
|
||
File::File(const std::string &filename) | ||
{ | ||
FILE *fp = fopen(filename.c_str(), "r"); | ||
if (fp == NULL) FATAL_ERROR("Failed to open \"%s\" for reading.\n", filename.c_str()); | ||
|
||
fseek(fp, 0, SEEK_END); | ||
|
||
m_Size = ftell(fp); | ||
if (m_Size < 0) | ||
FATAL_ERROR("File size of \"%s\" is less than zero.\n", filename.c_str()); | ||
else if (m_Size == 0) | ||
return; // Empty file | ||
|
||
m_Buffer = new unsigned char[m_Size]; | ||
rewind(fp); | ||
|
||
if (fread(m_Buffer, m_Size, 1, fp) != 1) | ||
FATAL_ERROR("Failed to read \"%s\".\n", filename.c_str()); | ||
|
||
m_Pos = 0; | ||
|
||
fclose(fp); | ||
} | ||
|
||
void File::Save(const std::string &filename) | ||
{ | ||
FILE *fp = fopen(filename.c_str(), "w"); | ||
if (fp == NULL) FATAL_ERROR("Failed to open \"%s\" for writing.\n", filename.c_str()); | ||
|
||
fwrite(m_Buffer, m_Size, 1, fp); | ||
|
||
fclose(fp); | ||
} | ||
|
||
// 0x0 is space | ||
// 0xBB - 0xD4 -> A - Z | ||
// 0xD5 - 0xEE -> a - z | ||
void File::ProcessString(int location, int max_size) | ||
{ | ||
bool shouldCapLetter = true; | ||
|
||
for (int j = 0; j < max_size; ++j) | ||
{ | ||
int idx = location + j; | ||
if (idx > m_Size) FATAL_ERROR("Index %d is greater than file size (%ld)\n", idx, m_Size); | ||
|
||
unsigned char c = m_Buffer[idx]; | ||
|
||
if (c == 0x0) | ||
{ | ||
shouldCapLetter = true; | ||
} | ||
else if ((c >= 0xBB && c <= 0xD4) && !shouldCapLetter) | ||
{ | ||
m_Buffer[idx] = c - 0xBB + 0xD5; | ||
} | ||
else if ((c >= 0x01 && c <= 0x14) && !shouldCapLetter) | ||
{ | ||
m_Buffer[idx] = c - 0x01 + 0x16; | ||
} | ||
else if (c == 0xFF) | ||
{ | ||
break; | ||
} | ||
else | ||
{ | ||
shouldCapLetter = false; | ||
} | ||
} | ||
} | ||
|
||
void File::ProcessStringArray(int location, int size, int count) | ||
{ | ||
for (int i = 0; i < count; ++i) ProcessString(location + i * size, size); | ||
} | ||
|
||
void File::ProcessStringPointerArray(int location, int count, int stride) | ||
{ | ||
if (location + 4 * count > m_Size) FATAL_ERROR("Pointer array end %d greater than file size (%ld)\n", location + 4 * count, m_Size); | ||
|
||
for (int i = 0; i < count; ++i) | ||
{ | ||
unsigned int ptr = (m_Buffer[location] | m_Buffer[location + 1] << 8 | m_Buffer[location + 2] << 16 | m_Buffer[location + 3] << 24); | ||
ptr -= 0x8000000; | ||
|
||
if (ptr > m_Size) FATAL_ERROR("Pointer %d greater than file size (%ld)\n", ptr, m_Size); | ||
ProcessString(ptr, __INT32_MAX__); | ||
location += stride; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
|
||
class File | ||
{ | ||
public: | ||
File(const std::string &filename); | ||
~File() = default; | ||
|
||
void Save(const std::string &filename); | ||
|
||
void ProcessString(int location, int max_size); | ||
void ProcessStringArray(int location, int size, int count); | ||
void ProcessStringPointerArray(int location, int count, int stride = 4); | ||
private: | ||
unsigned char *m_Buffer; | ||
long m_Pos, m_Size; | ||
}; |