Skip to content

Commit

Permalink
Add makeips
Browse files Browse the repository at this point in the history
  • Loading branch information
Pseurae committed Jul 25, 2023
1 parent b8a2360 commit 706304f
Show file tree
Hide file tree
Showing 9 changed files with 355 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ output.*
snakewood.*
pokeruby.map
.D[Ss]_[Ss]tore
[Dd]esktop.ini
[Dd]esktop.ini
*.ips
18 changes: 15 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#!/usr/bin/env make

INPUT := snakewood.gba
OUTPUT := output.gba

ifeq ($(strip $(DEVKITARM)),)
$(error "Please set DEVKITARM in your environment. export DEVKITARM=<path to>devkitARM")
endif
Expand Down Expand Up @@ -29,9 +32,12 @@ LD = $(PREFIX)ld
LDFLAGS = -i rom.ld -T linker.ld

ARMIPS := armips
ARMIPSFLAGS := -strequ INPUT_FILE $(INPUT) -strequ OUTPUT_FILE $(OUTPUT)

ELFEDIT := tools/elfedit/elfedit$(EXE)
PREPROC := tools/preproc/preproc$(EXE)
SCANINC := tools/scaninc/scaninc$(EXE)
MAKEIPS := tools/makeips/makeips$(EXE)

.DEFAULT_GOAL = all

Expand All @@ -41,18 +47,24 @@ ifeq (,$(filter-out all,$(MAKECMDGOALS)))
$(shell $(MAKE) tools > /dev/null)
endif

all: test.gba
$(ARMIPS) main.asm -sym2 output.map
all: $(OUTPUT)
@$(ARMIPS) $(ARMIPSFLAGS) main.asm -sym2 output.map
@echo "$(ARMIPS) <flags> main.asm -sym2 output.map"

patch: all
$(MAKEIPS) $(OUTPUT) $(INPUT) patch.ips

test.gba: snakewood.gba build/linked_processed.o
$(OUTPUT): $(INPUT) build/linked_processed.o

tools:
@$(MAKE) -C tools/elfedit
@$(MAKE) -C tools/makeips
@$(MAKE) -C tools/preproc
@$(MAKE) -C tools/scaninc

clean_tools:
@$(MAKE) -C tools/elfedit clean
@$(MAKE) -C tools/makeips clean
@$(MAKE) -C tools/preproc clean
@$(MAKE) -C tools/scaninc clean

Expand Down
2 changes: 1 addition & 1 deletion main.asm
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.gba
.thumb

.open "snakewood.gba", "output.gba", 0x8000000
.open INPUT_FILE, OUTPUT_FILE, 0x8000000

.loadtable "charmap.tbl"

Expand Down
22 changes: 22 additions & 0 deletions tools/elfedit/elfedit.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
/*
Copyright 2023 Pseurae
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the “Software”), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#include "elfedit.h"

static int check_file(t_elf *const elf)
Expand Down
22 changes: 22 additions & 0 deletions tools/elfedit/elfedit.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
/*
Copyright 2023 Pseurae
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the “Software”), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#ifndef ELFEDIT_H
#define ELFEDIT_H

Expand Down
1 change: 1 addition & 0 deletions tools/makeips/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
makeips
18 changes: 18 additions & 0 deletions tools/makeips/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
CXX = g++

CXXFLAGS = -Wall -std=c++20 -O2

SRCS = makeips.cpp

HEADERS := makeips.h

.PHONY: all clean

all: makeips
@:

makeips: $(SRCS) $(HEADERS)
$(CXX) $(CXXFLAGS) $(SRCS) -o $@ $(LDFLAGS)

clean:
$(RM) makeips makeips.exe
202 changes: 202 additions & 0 deletions tools/makeips/makeips.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
/*
Copyright 2023 Pseurae
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the “Software”), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#include "makeips.h"

void fill_records(Context &ctx)
{
std::ifstream input(ctx.argv[1], std::ios::binary | std::ios::ate), base(ctx.argv[2], std::ios::binary | std::ios::ate);
if (input.fail())
throw std::runtime_error("cannot open input file");
if (base.fail())
throw std::runtime_error("cannot open base file");

size_t inputsize = input.tellg(), basesize = base.tellg();

input.seekg(0), base.seekg(0);

bool changed = false;

size_t offset = 0;
Record::Bytes bufferInput = {}, bufferBase = {};

while (!input.eof() || !base.eof())
{
char i, b;
input.read(&i, 1);
base.read(&b, 1);

if (!changed)
{
if (i != b)
{
offset = (size_t)base.tellg() - 1;
bufferInput.push_back(i);
bufferBase.push_back(b);
changed = true;
}
}
else
{
if (i == b)
{
ctx.records.emplace_back(offset, bufferInput, bufferBase);
bufferInput.clear();
bufferBase.clear();
changed = false;
}
else
{
bufferInput.push_back(i);
bufferBase.push_back(b);
}
}
}

if (changed)
{
ctx.records.emplace_back(offset, bufferInput, bufferBase);
bufferInput.clear();
bufferBase.clear();
}

if (inputsize > basesize)
{
bufferInput.resize(inputsize - basesize);
input.read((char *)bufferInput.data(), inputsize - basesize);

bufferBase.resize(inputsize - basesize);
std::memset((char *)bufferBase.data(), 0, inputsize - basesize);

ctx.records.emplace_back(basesize, bufferInput, bufferBase);
bufferInput.clear();
bufferBase.clear();
}

ctx.inputsize = inputsize;
ctx.basesize = basesize;

input.close();
base.close();
}

void generate_ips_patch(Context &ctx)
{
std::ofstream patch(ctx.argv[3], std::ios::binary);
patch.write("PATCH", 5);

for (auto &r : ctx.records)
{
unsigned int address = r.GetOffset();

if (address >= 0x1000000)
throw std::runtime_error("address too big");

auto data = r.GetInput();
unsigned short size = data.size();
unsigned int offset = 0;

if (size == 0)
throw std::runtime_error("size is 0");

while (size)
{
unsigned char block[5] = { 0 };
unsigned char rle_block[3] = { 0 };
unsigned int localAddress = address + offset;
unsigned short localSize = size % __UINT16_MAX__;
size -= localSize;

auto addressBytes = reinterpret_cast<unsigned char *>(&localAddress);
auto sizeBytes = reinterpret_cast<unsigned char *>(&localSize);

if (std::adjacent_find(data.begin(), data.end(), std::not_equal_to<unsigned char>()) == data.end() && size > 2)
{

block[0] = addressBytes[2];
block[1] = addressBytes[1];
block[2] = addressBytes[0];

block[3] = 0;
block[4] = 0;

patch.write((char *)block, 5);

rle_block[0] = sizeBytes[1];
rle_block[1] = sizeBytes[0];
rle_block[2] = data[0];

patch.write((char *)rle_block, 3);
}
else
{
block[0] = addressBytes[2];
block[1] = addressBytes[1];
block[2] = addressBytes[0];

block[3] = sizeBytes[1];
block[4] = sizeBytes[0];

patch.write((char *)block, 5);
patch.write((char *)data.data(), localSize);
}

offset += localSize;
}
}

patch.write("EOF", 3);
patch.close();
}

void generate_patch(Context &ctx)
{
if (ctx.records.empty())
throw std::runtime_error("no file diff");

generate_ips_patch(ctx);
}

void create_patch(Context &ctx)
{
fill_records(ctx);
generate_patch(ctx);
}

int main(int argc, char *argv[])
{
if (argc < 4)
THROW_ERROR("error: patch [INPUT] [BASE] [OUTPUT]");

Context ctx { argv };

try
{
create_patch(ctx);
}
catch (std::runtime_error &err)
{
THROW_ERROR("error: %s", err.what());
}

return 0;
}
Loading

0 comments on commit 706304f

Please sign in to comment.