Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ temp/*
data/*

//Development tools
.vscode
*replace*

//Linux/MacOS generated files (at compilation time)
*.dSYM
.DS_Store

//=)
files/bw2_changes/ow_sprites/*
*/2_00000616.bin
20 changes: 10 additions & 10 deletions src/Gen4/RomHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ inline string csvGetNext(string str) {
}

void UnpackRom(string romPath, string arm9) {
string command = string(NDS_TOOL) + " -x \"" + romPath + "\" -9 temp\\arm9.bin -7 temp\\arm7.bin -y9 temp\\y9.bin -y7 temp\\y7.bin -d data -y temp\\overlay -t temp\\banner.bin -h temp\\header.bin";
string command = string(NDS_TOOL) + " -x \"" + romPath + "\" -9 temp/arm9.bin -7 temp/arm7.bin -y9 temp/y9.bin -y7 temp/y7.bin -d data -y temp/overlay -t temp/banner.bin -h temp/header.bin";
cout << command << endl;
system(command.c_str());

//Updating some of the files...

command = string(ARM9_DEC) + " temp\\arm9.bin " + " temp\\arm9_.bin";
command = string(ARM9_DEC) + " temp/arm9.bin " + " temp/arm9_.bin";

system(command.c_str());

fstream bin;
bin.open("temp\\arm9_.bin", ios::binary|ios::in|ios::out|ios::ate);
bin.open("temp/arm9_.bin", ios::binary|ios::in|ios::out|ios::ate);

unsigned char lang = 2;

Expand Down Expand Up @@ -83,33 +83,33 @@ void UnpackRom(string romPath, string arm9) {
changes.close();
bin.close();

command = "copy files\\scr_seq.narc data\\a\\0\\1\\2";
command = string(COPY_CMD) + " files/scr_seq.narc data/a/0/1/2";
system(command.c_str());

}

void UnpackFieldNarc() {
string command = string(KNARC) + " -d temp\\event_data -u data\\a\\0\\3\\2";
string command = string(KNARC) + " -d temp/event_data -u data/a/0/3/2";
system(command.c_str());

//Updating some select files:
for(const auto & file : directory_iterator(EVENT_DATA)) {
string pstr = file.path().string();
command = "copy " + file.path().string();
pstr = pstr.substr(pstr.find_last_of("\\") + 1, pstr.length());
command += " temp\\event_data\\" + pstr;
command = string(COPY_CMD) + " " + file.path().string();
pstr = pstr.substr(pstr.find_last_of("/") + 1, pstr.length());
command += " temp/event_data/" + pstr;
system(command.c_str());
}
}

void PackFieldNarc() {
string command = string(KNARC) + " -d temp\\event_data -p data\\a\\0\\3\\2";
string command = string(KNARC) + " -d temp/event_data -p data/a/0/3/2";
system(command.c_str());
}

void RepackRom(string output_name) {
cout << output_name << endl;
string command = string(NDS_TOOL) + " -c " + "\"" + string(OUT_PATH) + output_name + "\"" + " -9 temp\\arm9_.bin -7 temp\\arm7.bin -y9 temp\\y9.bin -y7 temp\\y7.bin -d data -y temp\\overlay -t temp\\banner.bin -h temp\\header.bin";
string command = string(NDS_TOOL) + " -c " + "\"" + string(OUT_PATH) + output_name + "\"" + " -9 temp/arm9_.bin -7 temp/arm7.bin -y9 temp/y9.bin -y7 temp/y7.bin -d data -y temp/overlay -t temp/banner.bin -h temp/header.bin";
system(command.c_str());
}

Expand Down
24 changes: 15 additions & 9 deletions src/Gen4/RomHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,23 @@
using std::string;

#define HGSS_COMMON "./files/Blocks_HGSS_Shared/"
#define SS_PATH "soulsilver_rom\\"
#define SS_ARM9 "files\\hgss_script.csv"
#define HG_ARM9 "files\\hgss_script.csv"
#define HG_PATH "heartgold_rom\\"
#define OUT_PATH "randomized_roms\\"
#define SS_PATH "soulsilver_rom/"
#define SS_ARM9 "files/hgss_script.csv"
#define HG_ARM9 "files/hgss_script.csv"
#define HG_PATH "heartgold_rom/"
#define OUT_PATH "randomized_roms/"
#define HG_DATA "./files/Blocks_HG/"
#define SS_DATA "./files/Blocks_SS/"
#define NDS_TOOL "tools\\ndstool.exe"
#define KNARC "tools\\knarc.exe"
#define ARM9_DEC "tools\\arm9dec.exe"
#define EVENT_DATA "files\\event_data"
#define NDS_TOOL "tools/ndstool.exe"
#define KNARC "tools/knarc.exe"
#define ARM9_DEC "tools/arm9dec.exe"
#define EVENT_DATA "files/event_data"

#ifdef __CYGWIN__
#define COPY_CMD "copy"
#else
#define COPY_CMD "cp"
#endif

void UnpackRom(string path, string arm9);
void UnpackFieldNarc();
Expand Down
2 changes: 1 addition & 1 deletion src/Gen4/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ static void HandleRandomization(string directory, string data, string arm9) {
ClearData();
}
cout << "Rom randomized with seed: " << seed << "." << endl;
pstr = pstr.substr(pstr.find_last_of("\\") + 1, pstr.length()-4) + "_map_randomized";
pstr = pstr.substr(pstr.find_last_of("/") + 1, pstr.length()-4) + "_map_randomized";
GenerateLogFile(OUT_PATH + pstr + ".log");
SetWarps();
PackFieldNarc();
Expand Down
3 changes: 1 addition & 2 deletions src/Gen4/make.bat
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
g++ -g main.cpp RomHandler.cpp Randomizer.cpp -o ../../map_randomizer.exe
pause
g++ -std=c++17 -g main.cpp RomHandler.cpp Randomizer.cpp -o ../../map_randomizer.exe
33 changes: 17 additions & 16 deletions src/Gen5/RomHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <iostream>
#include <filesystem>
#include <fstream>
#include <vector>

using namespace std;
using std::filesystem::directory_iterator;
Expand All @@ -21,7 +22,7 @@ void ApplyChanges(string changesPath) {
changesPath += langExt;

fstream bin;
bin.open("temp\\arm9.bin", ios::binary|ios::in|ios::out|ios::ate);
bin.open("temp/arm9.bin", ios::binary|ios::in|ios::out|ios::ate);

ifstream changes;
changes.open(changesPath);
Expand All @@ -46,7 +47,7 @@ void LockSeason(string changesPath, const char season) {
changesPath += langExt;

fstream bin;
bin.open("temp\\arm9.bin", ios::binary|ios::in|ios::out|ios::ate);
bin.open("temp/arm9.bin", ios::binary|ios::in|ios::out|ios::ate);

ifstream changes;
changes.open(changesPath);
Expand Down Expand Up @@ -76,17 +77,17 @@ void LockSeason(string changesPath, const char season) {
}

void UnpackRom(string romPath, string arm9) {
string command = string(NDS_TOOL) + " -x \"" + romPath + "\" -9 temp\\arm9.bin -7 temp\\arm7.bin -y9 temp\\y9.bin -y7 temp\\y7.bin -d data -y temp\\overlay -t temp\\banner.bin -h temp\\header.bin";
string command = string(NDS_TOOL) + " -x \"" + romPath + "\" -9 temp/arm9.bin -7 temp/arm7.bin -y9 temp/y9.bin -y7 temp/y7.bin -d data -y temp/overlay -t temp/banner.bin -h temp/header.bin";
cout << command << endl;
system(command.c_str());

command = string(ARM9_DEC) + " -d temp\\arm9.bin";
command = string(ARM9_DEC) + " -d temp/arm9.bin";
system(command.c_str());

ApplyChanges(string(BW2_UNIVERSAL));

fstream bin;
bin.open("temp\\arm9.bin", ios::binary|ios::in|ios::out|ios::ate);
bin.open("temp/arm9.bin", ios::binary|ios::in|ios::out|ios::ate);

unsigned char lang;

Expand Down Expand Up @@ -120,7 +121,7 @@ inline void CopyFiles(string narc_path, string temp_path, string data_path) {
string command = string(KNARC) + " -d " + temp_path + " -u " + narc_path;
system(command.c_str());

command = "copy " + string(data_path);
command = string(COPY_CMD) + " " + string(data_path);
command += " " + temp_path;
system(command.c_str());

Expand All @@ -133,22 +134,22 @@ void UnpackFieldNarc() {

//Updating some select files:
//Event Data
CopyFiles("data\\a\\1\\2\\6", "temp\\event_data", EVENT_DATA);
CopyFiles("data/a/1/2/6", "temp/event_data", EVENT_DATA);

//Trainer Data
CopyFiles("data\\a\\0\\9\\1", "temp\\trainer_data", TRAINER_DATA);
CopyFiles("data/a/0/9/1", "temp/trainer_data", TRAINER_DATA);

//Trainer Poke
CopyFiles("data\\a\\0\\9\\2", "temp\\trainer_poke", TRAINER_POKE);
CopyFiles("data/a/0/9/2", "temp/trainer_poke", TRAINER_POKE);

//Map Scripts
CopyFiles("data\\a\\0\\5\\6", "temp\\map_script", MAP_FUNCTIONS);
CopyFiles("data/a/0/5/6", "temp/map_script", MAP_FUNCTIONS);

//Map headers
string command = string(KNARC) + " -d temp\\map_header -u data\\a\\0\\1\\2";
string command = string(KNARC) + " -d temp/map_header -u data/a/0/1/2";
system(command.c_str());

for(const auto & file : directory_iterator("temp\\map_header")) {
for(const auto & file : directory_iterator("temp/map_header")) {
fstream bin;
bin.open(file.path(), ios::binary|ios::in|ios::out|ios::ate);
bin.seekg(0, ios::end);
Expand All @@ -171,22 +172,22 @@ void UnpackFieldNarc() {
bin.close();
}

command = string(KNARC) + " -d temp\\map_header -p data\\a\\0\\1\\2";
command = string(KNARC) + " -d temp/map_header -p data/a/0/1/2";
system(command.c_str());

//=)
CopyFiles("data\\a\\0\\4\\8", "temp\\ow_spr", OW_SPRITES);
CopyFiles("data/a/0/4/8", "temp/ow_spr", OW_SPRITES);
system("echo on");
}

void PackFieldNarc() {
string command = string(KNARC) + " -d temp\\event_data -p data\\a\\1\\2\\6";
string command = string(KNARC) + " -d temp/event_data -p data/a/1/2/6";
system(command.c_str());
}

void RepackRom(string output_name) {
cout << output_name << endl;
string command = string(NDS_TOOL) + " -c " + "\"" + string(OUT_PATH) + output_name + "\"" + " -9 temp\\arm9.bin -7 temp\\arm7.bin -y9 temp\\y9.bin -y7 temp\\y7.bin -d data -y temp\\overlay -t temp\\banner.bin -h temp\\header.bin";
string command = string(NDS_TOOL) + " -c " + "\"" + string(OUT_PATH) + output_name + "\"" + " -9 temp/arm9.bin -7 temp/arm7.bin -y9 temp/y9.bin -y7 temp/y7.bin -d data -y temp/overlay -t temp/banner.bin -h temp/header.bin";
system(command.c_str());
}

Expand Down
36 changes: 21 additions & 15 deletions src/Gen5/RomHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,30 @@

using std::string;

#define ARM9_DEC "tools\\blz.exe"
#define W2_PATH "white2_rom\\"
#define B2_PATH "black2_rom\\"
#define OUT_PATH "randomized_roms\\"
#define ARM9_DEC "tools/blz.exe"
#define W2_PATH "white2_rom/"
#define B2_PATH "black2_rom/"
#define OUT_PATH "randomized_roms/"
#define W2_DATA "./files/Blocks_W2/"
#define B2_DATA "./files/Blocks_B2/"
#define BW2_DATA_SHARED "./files/Blocks_BW2_Shared/"
#define SPRINGSUMMER_DATA "./files/Blocks_BW2_Seasons/SpringSummer/"
#define AUTUMN_DATA "./files/Blocks_BW2_Seasons/Autumn/"
#define WINTER_DATA "./files/Blocks_BW2_Seasons/Winter/"
#define NDS_TOOL "tools\\ndstool.exe"
#define KNARC "tools\\knarc.exe"
#define EVENT_DATA "files\\bw2_changes\\event_data_bw2"
#define TRAINER_DATA "files\\bw2_changes\\tr_data_bw2"
#define TRAINER_POKE "files\\bw2_changes\\tr_poke_bw2"
#define MAP_FUNCTIONS "files\\bw2_changes\\map_functions_bw2"
#define OW_SPRITES "files\\bw2_changes\\ow_sprites"
#define MUGSHOTS "files\\bw2_changes\\mugshots"
#define NDS_TOOL "tools/ndstool.exe"
#define KNARC "tools/knarc.exe"
#define EVENT_DATA "files/bw2_changes/event_data_bw2"
#define TRAINER_DATA "files/bw2_changes/tr_data_bw2"
#define TRAINER_POKE "files/bw2_changes/tr_poke_bw2"
#define MAP_FUNCTIONS "files/bw2_changes/map_functions_bw2"
#define OW_SPRITES "files/bw2_changes/ow_sprites"
#define MUGSHOTS "files/bw2_changes/mugshots"

#ifdef __CYGWIN__
#define COPY_CMD "copy"
#else
#define COPY_CMD "cp -r"
#endif

//Season consts
enum Seasons {
Expand All @@ -33,9 +39,9 @@ enum Seasons {
};

//File change locations
#define BW2_UNIVERSAL "files\\bw2_script_universal.csv"
#define SEASON_LOCK_BASE_W2 "files\\Blocks_BW2_Seasons\\SeasonScripts\\W2SeasonLock"
#define SEASON_LOCK_BASE_B2 "files\\Blocks_BW2_Seasons\\SeasonScripts\\B2SeasonLock"
#define BW2_UNIVERSAL "files/bw2_script_universal.csv"
#define SEASON_LOCK_BASE_W2 "files/Blocks_BW2_Seasons/SeasonScripts/W2SeasonLock"
#define SEASON_LOCK_BASE_B2 "files/Blocks_BW2_Seasons/SeasonScripts/B2SeasonLock"

void LockSeason(string changesPath, const char season);
void UnpackRom(string path, string arm9);
Expand Down
2 changes: 1 addition & 1 deletion src/Gen5/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ static void HandleRandomization(string directory, string data, string season_pat
ClearData();
}
LockSeason(season_path, season);
pstr = pstr.substr(pstr.find_last_of("\\") + 1, pstr.length()-4) + "_map_randomized";
pstr = pstr.substr(pstr.find_last_of("/") + 1, pstr.length()-4) + "_map_randomized";
GenerateLogFile(OUT_PATH + pstr + ".log");
SetWarps();
PackFieldNarc();
Expand Down
3 changes: 1 addition & 2 deletions src/Gen5/make.bat
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
g++ -g main.cpp RomHandler.cpp Randomizer.cpp -o ../../map_randomizer_bw2.exe
pause
g++ -std=c++17 -g main.cpp RomHandler.cpp Randomizer.cpp -o ../../map_randomizer_bw2.exe
55 changes: 55 additions & 0 deletions src/arm9dec/blz.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include "blz.h"

static int get_decompressed_length(u8 *end, u32 input_len)
{
return input_len + *(u32 *)&end[-4];
}

static void decompress(u8 *end)
{
footer f = *(footer *)&end[-8];

u8 *data = end - f.total_size;

u32 cmpSize = f.total_size - f.hdr_size,
outSize = f.total_size + f.ext_size;

while (outSize)
{
u32 flags = data[--cmpSize];

for (u32 i = 0; i < 8; i++)
{
if ((flags << i) & 0x80)
{
cmpSize -= 2;

u16 op = *(u16 *)&data[cmpSize];

u32 len = (op >> 12) + 3,
ofs = (op & 0xFFF) + 3;

outSize -= len;

while (len--)
data[outSize + len] = data[outSize + len + ofs];
}
else
data[--outSize] = data[--cmpSize];

if (!outSize)
break;
}
}
}

u8 *decompress_blz_buffer(u8 *in, u32 length_in, u32 *length_out)
{
*length_out = get_decompressed_length(in + length_in, length_in);

u8 *buf = malloc(*length_out);
memcpy(buf, in, length_in);
decompress(buf + length_in);

return buf;
}
10 changes: 10 additions & 0 deletions src/arm9dec/blz.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include "common.h"

typedef struct footer
{
u32 total_size : 24;
u32 hdr_size : 8;
u32 ext_size;
} footer;

u8 *decompress_blz_buffer(u8 *in, u32 length_in, u32 *length_out);
10 changes: 10 additions & 0 deletions src/arm9dec/common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once

#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <stdio.h>

typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
Loading