diff --git a/README.md b/README.md
index 699e1b3..2127e0a 100644
--- a/README.md
+++ b/README.md
@@ -3,7 +3,7 @@
UniPatcher
----------
-UniPatcher is a ROM patcher for Android that supports IPS, UPS, BPS, PPF, DPS, EBP and XDelta3 patch types.
+UniPatcher is a ROM patcher for Android that supports IPS, UPS, BPS, APS (N64), PPF, DPS, EBP and XDelta3 patch types.
### Additional features:
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index 15e85e6..16b2542 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -34,6 +34,7 @@
+
diff --git a/app/src/main/java/org/emunix/unipatcher/WorkerService.java b/app/src/main/java/org/emunix/unipatcher/WorkerService.java
index 86339f9..c2b5880 100644
--- a/app/src/main/java/org/emunix/unipatcher/WorkerService.java
+++ b/app/src/main/java/org/emunix/unipatcher/WorkerService.java
@@ -29,6 +29,7 @@ import android.support.v4.app.NotificationCompat;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
+import org.emunix.unipatcher.patch.APS;
import org.emunix.unipatcher.patch.BPS;
import org.emunix.unipatcher.patch.DPS;
import org.emunix.unipatcher.patch.EBP;
@@ -119,6 +120,8 @@ public class WorkerService extends IntentService {
patcher = new BPS(this, patchFile, romFile, outputFile);
else if ("ppf".equals(ext))
patcher = new PPF(this, patchFile, romFile, outputFile);
+ else if ("aps".equals(ext))
+ patcher = new APS(this, patchFile, romFile, outputFile);
else if ("ebp".equals(ext))
patcher = new EBP(this, patchFile, romFile, outputFile);
else if ("dps".equals(ext))
diff --git a/app/src/main/java/org/emunix/unipatcher/patch/APS.java b/app/src/main/java/org/emunix/unipatcher/patch/APS.java
new file mode 100644
index 0000000..bb9296c
--- /dev/null
+++ b/app/src/main/java/org/emunix/unipatcher/patch/APS.java
@@ -0,0 +1,76 @@
+/*
+Copyright (C) 2017 Boris Timofeev
+
+This file is part of UniPatcher.
+
+UniPatcher is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+UniPatcher is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with UniPatcher. If not, see .
+*/
+
+package org.emunix.unipatcher.patch;
+
+import android.content.Context;
+
+import org.apache.commons.io.IOUtils;
+import org.emunix.unipatcher.R;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.util.Arrays;
+
+public class APS extends Patch {
+
+ public static final int NOT_APS_PATCH = 0;
+ public static final int APS_N64_PATCH = 1;
+ public static final int APS_GBA_PATCH = 2;
+
+ private static final byte[] APS_N64_MAGIC = {0x41, 0x50, 0x53, 0x31, 0x30}; // APS10
+ private static final byte[] APS_GBA_MAGIC = {0x41, 0x50, 0x53, 0x31, 0x00}; // APS1
+
+ public APS(Context context, File patch, File rom, File output) {
+ super(context, patch, rom, output);
+ }
+
+ @Override
+ public void apply() throws PatchException, IOException {
+ Patch aps = null;
+ switch (checkAPS(patchFile)) {
+ case APS_N64_PATCH:
+ aps = new APS_N64(context, patchFile, romFile, outputFile); break;
+ case APS_GBA_PATCH:
+ throw new PatchException(context.getString(R.string.notify_error_aps_gba));
+ case NOT_APS_PATCH:
+ throw new PatchException(context.getString(R.string.notify_error_not_aps_patch));
+ }
+
+ aps.apply();
+ }
+
+ public int checkAPS(File file) throws IOException {
+ FileInputStream stream = null;
+ try {
+ stream = new FileInputStream(file);
+ byte[] magic = new byte[5];
+ stream.read(magic);
+ if (Arrays.equals(magic, APS_N64_MAGIC)) {
+ return APS_N64_PATCH;
+ } else if (Arrays.equals(magic, APS_GBA_MAGIC)) {
+ return APS_GBA_PATCH;
+ }
+ } finally {
+ IOUtils.closeQuietly(stream);
+ }
+ return NOT_APS_PATCH;
+ }
+}
diff --git a/app/src/main/java/org/emunix/unipatcher/patch/APS_N64.java b/app/src/main/java/org/emunix/unipatcher/patch/APS_N64.java
new file mode 100644
index 0000000..bf8577e
--- /dev/null
+++ b/app/src/main/java/org/emunix/unipatcher/patch/APS_N64.java
@@ -0,0 +1,232 @@
+/*
+Copyright (C) 2017 Boris Timofeev
+
+This file is part of UniPatcher.
+
+UniPatcher is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+UniPatcher is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with UniPatcher. If not, see .
+*/
+
+package org.emunix.unipatcher.patch;
+
+import android.content.Context;
+
+import org.apache.commons.io.IOUtils;
+import org.emunix.unipatcher.R;
+import org.emunix.unipatcher.Utils;
+
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.RandomAccessFile;
+import java.util.Arrays;
+
+public class APS_N64 extends Patch {
+
+ private static final byte[] MAGIC_NUMBER = {0x41, 0x50, 0x53, 0x31, 0x30}; // APS10
+ private static final int TYPE_SIMPLE_PATCH = 0;
+ private static final int TYPE_N64_PATCH = 1;
+ private static final int ENCODING_SIMPLE = 0;
+
+ public APS_N64(Context context, File patch, File rom, File output) {
+ super(context, patch, rom, output);
+ }
+
+ @Override
+ public void apply() throws PatchException, IOException {
+ BufferedInputStream romStream = null;
+ BufferedInputStream patchStream = null;
+ BufferedOutputStream outputStream = null;
+
+ try {
+ patchStream = new BufferedInputStream(new FileInputStream(patchFile));
+
+ long patchSize = patchFile.length();
+ long romSize = romFile.length();
+ long outSize;
+ int romPos = 0;
+ int outPos = 0;
+ int patchPos = 0;
+ long offset, size;
+
+ // check magic string
+ byte[] magic = new byte[5];
+ size = patchStream.read(magic);
+ if (size != 5 || !Arrays.equals(magic, MAGIC_NUMBER))
+ throw new PatchException(context.getString(R.string.notify_error_not_aps_patch));
+ patchPos += 5;
+
+ // read and check type of the patch
+ int patchType = patchStream.read();
+ if ((patchType != TYPE_SIMPLE_PATCH) && (patchType != TYPE_N64_PATCH))
+ throw new PatchException(context.getString(R.string.notify_error_not_aps_patch));
+ patchPos++;
+
+ // check encoding method
+ int encoding = patchStream.read();
+ if (encoding != ENCODING_SIMPLE)
+ throw new PatchException(context.getString(R.string.notify_error_not_aps_patch));
+ patchPos++;
+
+ // skip description
+ byte[] description = new byte[50];
+ size = patchStream.read(description);
+ if (size < 50)
+ throw new PatchException(context.getString(R.string.notify_error_not_aps_patch));
+ patchPos += 50;
+
+ // validate ROM
+ if (patchType == TYPE_N64_PATCH) {
+ int endianness = patchStream.read();
+ int cardID = ((patchStream.read() & 0xff) << 8) + (patchStream.read() & 0xff);
+ int country = patchStream.read();
+ byte[] crc = new byte[8];
+ patchStream.read(crc);
+ if (!validateROM(endianness, cardID, country, crc))
+ throw new PatchException(context.getString(R.string.notify_error_rom_not_compatible_with_patch));
+ // skip bytes for future expansion
+ byte[] skip = new byte[5];
+ patchStream.read(skip);
+ patchPos += 17;
+ }
+
+ // read size of destination image.
+ outSize = readLELong(patchStream);
+ patchPos += 4;
+
+ romStream = new BufferedInputStream(new FileInputStream(romFile));
+ outputStream = new BufferedOutputStream(new FileOutputStream(outputFile));
+
+ // apply patch
+ while (patchPos < patchSize) {
+ offset = readLELong(patchStream);
+ if (offset < 0)
+ throw new PatchException(context.getString(R.string.notify_error_patch_corrupted));
+ patchPos += 4;
+
+ // copy data from rom to out
+ if (offset <= romSize) {
+ if (outPos < offset) {
+ size = offset - outPos;
+ Utils.copy(romStream, outputStream, size);
+ romPos += size;
+ outPos += size;
+ }
+ } else {
+ if (outPos < romSize) {
+ size = (int) romSize - outPos;
+ Utils.copy(romStream, outputStream, size);
+ romPos += size;
+ outPos += size;
+ }
+ if (outPos < offset) {
+ size = offset - outPos;
+ Utils.copy(size, (byte) 0x0, outputStream);
+ outPos += size;
+ }
+ }
+
+ // copy data from patch to out
+ size = patchStream.read();
+ patchPos++;
+ if (size != 0) {
+ byte[] data = new byte[(int)size];
+ patchStream.read(data);
+ patchPos += size;
+ outputStream.write(data);
+ outPos += size;
+ } else { // RLE
+ byte val = (byte) patchStream.read();
+ size = patchStream.read();
+ patchPos += 2;
+ byte[] data = new byte[(int)size];
+ Arrays.fill(data, val);
+ outputStream.write(data);
+ outPos += size;
+ }
+
+ // skip rom data
+ if (offset <= romSize) {
+ if (romPos + size > romSize) {
+ romPos = (int) romSize;
+ } else {
+ byte[] buf = new byte[(int)size];
+ romStream.read(buf);
+ romPos += size;
+ }
+ }
+ }
+ // write rom tail and trim
+ Utils.copy(romStream, outputStream, outSize - outPos);
+ } finally {
+ IOUtils.closeQuietly(romStream);
+ IOUtils.closeQuietly(patchStream);
+ IOUtils.closeQuietly(outputStream);
+ }
+ }
+
+ private boolean validateROM(int endianness, int cartID, int country, byte[] crc) throws IOException {
+ RandomAccessFile rom = new RandomAccessFile(romFile, "r");
+ int val;
+ try {
+ // check endianness
+ val = rom.read();
+ if ((endianness == 1 && val != 0x80) || (endianness == 0 && val != 0x37))
+ return false;
+
+ // check cartID
+ rom.seek(0x3c);
+ if (endianness == 1) {
+ val = ((rom.read() & 0xff) << 8) + (rom.read() & 0xff);
+ } else {
+ val = (rom.read() & 0xff) + ((rom.read() & 0xff) << 8);
+ }
+ if (cartID != val)
+ return false;
+
+ // check country
+ val = rom.read();
+ if (endianness == 0)
+ val = rom.read();
+ if (country != val)
+ return false;
+
+ // check crc
+ byte[] buf = new byte[8];
+ rom.seek(0x10);
+ rom.read(buf);
+ if (endianness == 0) {
+ byte tmp;
+ for (int i = 0; i < buf.length; i += 2) {
+ tmp = buf[i];
+ buf[i] = buf[i + 1];
+ buf[i + 1] = tmp;
+ }
+ }
+ if (!Arrays.equals(crc, buf))
+ return false;
+ } finally {
+ IOUtils.closeQuietly(rom);
+ }
+ return true;
+ }
+
+ private long readLELong(InputStream stream) throws IOException {
+ return (stream.read() & 0xff) + ((stream.read() & 0xff) << 8)
+ + ((stream.read() & 0xff) << 16) + ((stream.read() & 0xff) << 24);
+ }
+}
diff --git a/app/src/main/res/raw/about.html b/app/src/main/res/raw/about.html
index cfbc952..2af4025 100644
--- a/app/src/main/res/raw/about.html
+++ b/app/src/main/res/raw/about.html
@@ -1,5 +1,5 @@
-UniPatcher is a ROM patcher that supports IPS, UPS, BPS, PPF, DPS, EBP and XDelta3 patch types.
+UniPatcher is a ROM patcher that supports IPS, UPS, BPS, APS (N64), PPF, DPS, EBP and XDelta3 patch types.
Additional features:
- Fix checksum in Sega Mega Drive ROMs
diff --git a/app/src/main/res/raw/changelog.html b/app/src/main/res/raw/changelog.html
index 6dd8f1b..487f3f7 100644
--- a/app/src/main/res/raw/changelog.html
+++ b/app/src/main/res/raw/changelog.html
@@ -1,5 +1,10 @@
+0.12 (January 15, 2017)
+
+ - Support APS patches (Nintendo 64 only)
+
+
0.11 (December 25, 2016)
- Support EBP patches (for EarthBound game)
diff --git a/app/src/main/res/raw/faq.html b/app/src/main/res/raw/faq.html
index c01dc0e..43aefc5 100644
--- a/app/src/main/res/raw/faq.html
+++ b/app/src/main/res/raw/faq.html
@@ -6,7 +6,7 @@
UniPatcher is an Android tool for applying patches to ROM images of various video game consoles.
What patch formats are supported?
-The app supports IPS, UPS, BPS, PPF, DPS, EBP and XDelta3 patches.
+The app supports IPS, UPS, BPS, APS (N64), PPF, DPS, EBP and XDelta3 patches.
Can I hack or crack Android game using this app?
No. UniPatcher is not designed to hack the Android games.
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index 2b5f8b4..0f4eee6 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -64,6 +64,8 @@
Not a valid UPS patch
Not a valid BPS patch
Not a valid PPF patch
+ Not a valid APS patch
+ APS for GBA not supported yet
Not a valid EBP patch
Not a valid DPS patch
Not a valid XDelta3 patch
@@ -124,7 +126,7 @@
There are no email clients installed
Share UniPatcher
- Download the best ROM patcher for Android. It supports IPS, UPS, BPS, PPF, DPS, EBP and XDelta3 patch types.\n\n
+ Download the best ROM patcher for Android. It supports IPS, UPS, BPS, APS (N64), PPF, DPS, EBP and XDelta3 patch types.\n\n
Apply patch
diff --git a/google-play/en/google-play.txt b/google-play/en/google-play.txt
index 32ce938..614b9e5 100644
--- a/google-play/en/google-play.txt
+++ b/google-play/en/google-play.txt
@@ -10,6 +10,7 @@ Utility to apply patches to game rom.
• IPS
• UPS
• BPS
+• APS (N64)
• PPF
• DPS
• EBP