summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authortom-overton <tom.overton@outlook.com>2021-02-22 12:47:07 -0800
committertom-overton <tom.overton@outlook.com>2021-02-22 12:47:07 -0800
commit47dcbd1c5084abe7aed15d43bdae7e67877a4eef (patch)
treeb241d0c2b71673b6799bfa3a35b35391ab63df40 /src
parente83688d474708088ca494d052d4c22975363b4f4 (diff)
Add diagnostics for the loaded ROM to both spoiler logs and error logs
Diffstat (limited to 'src')
-rw-r--r--src/com/dabomstew/pkrandom/Randomizer.java5
-rw-r--r--src/com/dabomstew/pkrandom/ctr/NCCH.java54
-rw-r--r--src/com/dabomstew/pkrandom/ctr/RomfsFile.java5
-rw-r--r--src/com/dabomstew/pkrandom/newgui/NewRandomizerGUI.java3
-rwxr-xr-xsrc/com/dabomstew/pkrandom/newnds/NDSFile.java5
-rwxr-xr-xsrc/com/dabomstew/pkrandom/newnds/NDSRom.java39
-rwxr-xr-xsrc/com/dabomstew/pkrandom/newnds/NDSY9Entry.java5
-rw-r--r--src/com/dabomstew/pkrandom/romhandlers/Abstract3DSRomHandler.java5
-rwxr-xr-xsrc/com/dabomstew/pkrandom/romhandlers/AbstractDSRomHandler.java5
-rwxr-xr-xsrc/com/dabomstew/pkrandom/romhandlers/AbstractGBRomHandler.java12
-rwxr-xr-xsrc/com/dabomstew/pkrandom/romhandlers/RomHandler.java2
11 files changed, 128 insertions, 12 deletions
diff --git a/src/com/dabomstew/pkrandom/Randomizer.java b/src/com/dabomstew/pkrandom/Randomizer.java
index 079960a..038393c 100644
--- a/src/com/dabomstew/pkrandom/Randomizer.java
+++ b/src/com/dabomstew/pkrandom/Randomizer.java
@@ -639,6 +639,11 @@ public class Randomizer {
log.println("Time elapsed: " + (System.currentTimeMillis() - startTime) + "ms");
log.println("RNG Calls: " + RandomSource.callsSinceSeed());
log.println("------------------------------------------------------------------");
+ log.println();
+
+ // Diagnostics
+ log.println("--ROM Diagnostics--");
+ romHandler.printRomDiagnostics(log);
return checkValue;
}
diff --git a/src/com/dabomstew/pkrandom/ctr/NCCH.java b/src/com/dabomstew/pkrandom/ctr/NCCH.java
index 90f4f98..c0607d9 100644
--- a/src/com/dabomstew/pkrandom/ctr/NCCH.java
+++ b/src/com/dabomstew/pkrandom/ctr/NCCH.java
@@ -30,10 +30,8 @@ import cuecompressors.BLZCoder;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.security.*;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
-import java.util.TreeMap;
+import java.util.*;
+import java.util.zip.CRC32;
public class NCCH {
@@ -54,6 +52,9 @@ public class NCCH {
private boolean codeCompressed, codeOpen, codeChanged;
private byte[] codeRamstored;
+ // Public so the base game can read it from the game update NCCH
+ public long originalCodeCRC, originalRomfsHeaderCRC;
+
private static final int media_unit_size = 0x200;
private static final int header_and_exheader_size = 0xA00;
private static final int exefs_header_size = 0x200;
@@ -158,6 +159,9 @@ public class NCCH {
byte[] romfsHeaderData = new byte[romfs_header_size];
baseRom.seek(romfsOffset);
baseRom.readFully(romfsHeaderData);
+ CRC32 checksum = new CRC32();
+ checksum.update(romfsHeaderData);
+ originalRomfsHeaderCRC = checksum.getValue();
int magic1 = FileFunctions.readFullInt(romfsHeaderData, 0x00);
int magic2 = FileFunctions.readFullInt(romfsHeaderData, 0x04);
if (magic1 != romfs_magic_1 || magic2 != romfs_magic_2) {
@@ -642,6 +646,9 @@ public class NCCH {
// size of the exefs header, so we need to add it back ourselves.
baseRom.seek(exefsOffset + exefs_header_size + codeFileHeader.offset);
baseRom.readFully(code);
+ CRC32 checksum = new CRC32();
+ checksum.update(code);
+ originalCodeCRC = checksum.getValue();
if (codeCompressed) {
code = new BLZCoder(null).BLZ_DecodePub(code, ".code");
@@ -714,6 +721,45 @@ public class NCCH {
}
}
+ public void printRomDiagnostics(PrintStream logStream, NCCH gameUpdate) {
+ if (gameUpdate == null) {
+ logStream.println(".code: " + String.format("%08X", this.originalCodeCRC));
+ } else {
+ logStream.println(".code: " + String.format("%08X", gameUpdate.originalCodeCRC));
+ }
+ logStream.println("romfs header: " + String.format("%08X", this.originalRomfsHeaderCRC));
+ if (gameUpdate != null) {
+ logStream.println("romfs header (game update): " + String.format("%08X", gameUpdate.originalRomfsHeaderCRC));
+ }
+ List<String> fileList = new ArrayList<>();
+ Map<String, String> baseRomfsFileDiagnostics = this.getRomfsFilesDiagnostics();
+ Map<String, String> updateRomfsFileDiagnostics = new HashMap<>();
+ if (gameUpdate != null) {
+ updateRomfsFileDiagnostics = gameUpdate.getRomfsFilesDiagnostics();
+ }
+ for (Map.Entry<String, String> entry : updateRomfsFileDiagnostics.entrySet()) {
+ baseRomfsFileDiagnostics.remove(entry.getKey());
+ fileList.add(entry.getValue());
+ }
+ for (Map.Entry<String, String> entry : baseRomfsFileDiagnostics.entrySet()) {
+ fileList.add(entry.getValue());
+ }
+ Collections.sort(fileList);
+ for (String fileLog : fileList) {
+ logStream.println(fileLog);
+ }
+ }
+
+ public Map<String, String> getRomfsFilesDiagnostics() {
+ Map<String, String> fileDiagnostics = new HashMap<>();
+ for (Map.Entry<String, RomfsFile> entry : romfsFiles.entrySet()) {
+ if (entry.getValue().originalCRC != 0) {
+ fileDiagnostics.put(entry.getKey(), entry.getKey() + ": " + String.format("%08X", entry.getValue().originalCRC));
+ }
+ }
+ return fileDiagnostics;
+ }
+
public String getTmpFolder() {
return tmpFolder;
}
diff --git a/src/com/dabomstew/pkrandom/ctr/RomfsFile.java b/src/com/dabomstew/pkrandom/ctr/RomfsFile.java
index ca4f73f..881ff47 100644
--- a/src/com/dabomstew/pkrandom/ctr/RomfsFile.java
+++ b/src/com/dabomstew/pkrandom/ctr/RomfsFile.java
@@ -28,6 +28,7 @@ import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
+import java.util.zip.CRC32;
public class RomfsFile {
@@ -39,6 +40,7 @@ public class RomfsFile {
private String extFilename;
public byte[] data;
public boolean fileChanged = false;
+ public long originalCRC;
public RomfsFile(NCCH parent) {
this.parent = parent;
@@ -52,6 +54,9 @@ public class RomfsFile {
byte[] buf = new byte[this.size];
rom.seek(this.offset);
rom.readFully(buf);
+ CRC32 checksum = new CRC32();
+ checksum.update(buf);
+ originalCRC = checksum.getValue();
if (parent.isWritingEnabled()) {
// make a file
String tmpDir = parent.getTmpFolder();
diff --git a/src/com/dabomstew/pkrandom/newgui/NewRandomizerGUI.java b/src/com/dabomstew/pkrandom/newgui/NewRandomizerGUI.java
index a7acb02..02424be 100644
--- a/src/com/dabomstew/pkrandom/newgui/NewRandomizerGUI.java
+++ b/src/com/dabomstew/pkrandom/newgui/NewRandomizerGUI.java
@@ -1692,6 +1692,9 @@ public class NewRandomizerGUI {
}
}
ex.printStackTrace();
+ ps.println();
+ ps.println("--ROM Diagnostics--");
+ romHandler.printRomDiagnostics(ps);
System.setErr(e1);
ps.close();
if (showMessage) {
diff --git a/src/com/dabomstew/pkrandom/newnds/NDSFile.java b/src/com/dabomstew/pkrandom/newnds/NDSFile.java
index 4442554..c0e7463 100755
--- a/src/com/dabomstew/pkrandom/newnds/NDSFile.java
+++ b/src/com/dabomstew/pkrandom/newnds/NDSFile.java
@@ -4,6 +4,7 @@ import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
+import java.util.zip.CRC32;
import com.dabomstew.pkrandom.FileFunctions;
@@ -37,6 +38,7 @@ public class NDSFile {
private Extracted status = Extracted.NOT;
private String extFilename;
public byte[] data;
+ public long originalCRC;
public NDSFile(NDSRom parent) {
this.parent = parent;
@@ -50,6 +52,9 @@ public class NDSFile {
byte[] buf = new byte[this.size];
rom.seek(this.offset);
rom.readFully(buf);
+ CRC32 checksum = new CRC32();
+ checksum.update(buf);
+ originalCRC = checksum.getValue();
if (parent.isWritingEnabled()) {
// make a file
String tmpDir = parent.getTmpFolder();
diff --git a/src/com/dabomstew/pkrandom/newnds/NDSRom.java b/src/com/dabomstew/pkrandom/newnds/NDSRom.java
index d0951c9..46cd232 100755
--- a/src/com/dabomstew/pkrandom/newnds/NDSRom.java
+++ b/src/com/dabomstew/pkrandom/newnds/NDSRom.java
@@ -1,13 +1,8 @@
package com.dabomstew.pkrandom.newnds;
-import java.io.File;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.RandomAccessFile;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.TreeMap;
+import java.io.*;
+import java.util.*;
+import java.util.zip.CRC32;
import com.dabomstew.pkrandom.SysConstants;
import com.dabomstew.pkrandom.FileFunctions;
@@ -54,6 +49,7 @@ public class NDSRom {
private int arm9_szmode, arm9_szoffset;
private byte[] arm9_footer;
private byte[] arm9_ramstored;
+ private long originalArm9CRC;
private static final int arm9_align = 0x1FF, arm7_align = 0x1FF;
private static final int fnt_align = 0x1FF, fat_align = 0x1FF;
@@ -477,6 +473,9 @@ public class NDSRom {
byte[] arm9 = new byte[arm9_size];
this.baseRom.seek(arm9_offset);
this.baseRom.readFully(arm9);
+ CRC32 checksum = new CRC32();
+ checksum.update(arm9);
+ originalArm9CRC = checksum.getValue();
// footer check
int nitrocode = readFromFile(this.baseRom, 4);
if (nitrocode == 0xDEC00621) {
@@ -621,6 +620,30 @@ public class NDSRom {
}
}
+ public void printRomDiagnostics(PrintStream logStream) {
+ List<String> overlayList = new ArrayList<>();
+ List<String> fileList = new ArrayList<>();
+ for (Map.Entry<Integer, NDSY9Entry> entry : arm9overlaysByFileID.entrySet()) {
+ if (entry.getValue().originalCRC != 0) {
+ overlayList.add("overlay9_" + entry.getKey() + ": " + String.format("%08X", entry.getValue().originalCRC));
+ }
+ }
+ for (Map.Entry<String, NDSFile> entry : files.entrySet()) {
+ if (entry.getValue().originalCRC != 0) {
+ fileList.add(entry.getKey() + ": " + String.format("%08X", entry.getValue().originalCRC));
+ }
+ }
+ Collections.sort(overlayList);
+ Collections.sort(fileList);
+ logStream.println("arm9: " + String.format("%08X", originalArm9CRC));
+ for (String overlayLog : overlayList) {
+ logStream.println(overlayLog);
+ }
+ for (String fileLog : fileList) {
+ logStream.println(fileLog);
+ }
+ }
+
// Helper methods to get variable-size ints out of files
public String getTmpFolder() {
diff --git a/src/com/dabomstew/pkrandom/newnds/NDSY9Entry.java b/src/com/dabomstew/pkrandom/newnds/NDSY9Entry.java
index 07d8963..c6005af 100755
--- a/src/com/dabomstew/pkrandom/newnds/NDSY9Entry.java
+++ b/src/com/dabomstew/pkrandom/newnds/NDSY9Entry.java
@@ -4,6 +4,7 @@ import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
+import java.util.zip.CRC32;
import com.dabomstew.pkrandom.FileFunctions;
@@ -44,6 +45,7 @@ public class NDSY9Entry {
private Extracted status = Extracted.NOT;
private String extFilename;
public byte[] data;
+ public long originalCRC;
private boolean decompressed_data = false;
public NDSY9Entry(NDSRom parent) {
@@ -58,6 +60,9 @@ public class NDSY9Entry {
byte[] buf = new byte[this.original_size];
rom.seek(this.offset);
rom.readFully(buf);
+ CRC32 checksum = new CRC32();
+ checksum.update(buf);
+ originalCRC = checksum.getValue();
// Compression?
if (compress_flag != 0 && this.original_size == this.compressed_size && this.compressed_size != 0) {
buf = new BLZCoder(null).BLZ_DecodePub(buf, "overlay " + overlay_id);
diff --git a/src/com/dabomstew/pkrandom/romhandlers/Abstract3DSRomHandler.java b/src/com/dabomstew/pkrandom/romhandlers/Abstract3DSRomHandler.java
index ef83a84..e034239 100644
--- a/src/com/dabomstew/pkrandom/romhandlers/Abstract3DSRomHandler.java
+++ b/src/com/dabomstew/pkrandom/romhandlers/Abstract3DSRomHandler.java
@@ -144,6 +144,11 @@ public abstract class Abstract3DSRomHandler extends AbstractRomHandler {
return getGameVersion();
}
+ @Override
+ public void printRomDiagnostics(PrintStream logStream) {
+ baseRom.printRomDiagnostics(logStream, gameUpdate);
+ }
+
public void closeInnerRom() throws IOException {
baseRom.closeROM();
}
diff --git a/src/com/dabomstew/pkrandom/romhandlers/AbstractDSRomHandler.java b/src/com/dabomstew/pkrandom/romhandlers/AbstractDSRomHandler.java
index a020e70..b27bc28 100755
--- a/src/com/dabomstew/pkrandom/romhandlers/AbstractDSRomHandler.java
+++ b/src/com/dabomstew/pkrandom/romhandlers/AbstractDSRomHandler.java
@@ -120,6 +120,11 @@ public abstract class AbstractDSRomHandler extends AbstractRomHandler {
return null;
}
+ @Override
+ public void printRomDiagnostics(PrintStream logStream) {
+ baseRom.printRomDiagnostics(logStream);
+ }
+
public void closeInnerRom() throws IOException {
baseRom.closeROM();
}
diff --git a/src/com/dabomstew/pkrandom/romhandlers/AbstractGBRomHandler.java b/src/com/dabomstew/pkrandom/romhandlers/AbstractGBRomHandler.java
index 09d235c..5d48249 100755
--- a/src/com/dabomstew/pkrandom/romhandlers/AbstractGBRomHandler.java
+++ b/src/com/dabomstew/pkrandom/romhandlers/AbstractGBRomHandler.java
@@ -31,6 +31,7 @@ import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Random;
+import java.util.zip.CRC32;
import com.dabomstew.pkrandom.FileFunctions;
import com.dabomstew.pkrandom.exceptions.RandomizerIOException;
@@ -38,6 +39,7 @@ import com.dabomstew.pkrandom.exceptions.RandomizerIOException;
public abstract class AbstractGBRomHandler extends AbstractRomHandler {
protected byte[] rom;
+ protected byte[] originalRom;
private String loadedFN;
public AbstractGBRomHandler(Random random, PrintStream logStream) {
@@ -51,6 +53,8 @@ public abstract class AbstractGBRomHandler extends AbstractRomHandler {
return false;
}
this.rom = loaded;
+ this.originalRom = new byte[rom.length];
+ System.arraycopy(rom, 0, originalRom, 0, rom.length);
loadedFN = filename;
loadedRom();
return true;
@@ -103,6 +107,14 @@ public abstract class AbstractGBRomHandler extends AbstractRomHandler {
}
@Override
+ public void printRomDiagnostics(PrintStream logStream) {
+ CRC32 checksum = new CRC32();
+ checksum.update(originalRom);
+ long crc = checksum.getValue();
+ logStream.println("Original ROM CRC32: " + String.format("%08X", crc));
+ }
+
+ @Override
public boolean canChangeStaticPokemon() {
return true;
}
diff --git a/src/com/dabomstew/pkrandom/romhandlers/RomHandler.java b/src/com/dabomstew/pkrandom/romhandlers/RomHandler.java
index 5e5bb55..06a5397 100755
--- a/src/com/dabomstew/pkrandom/romhandlers/RomHandler.java
+++ b/src/com/dabomstew/pkrandom/romhandlers/RomHandler.java
@@ -72,6 +72,8 @@ public interface RomHandler {
void setLog(PrintStream logStream);
+ void printRomDiagnostics(PrintStream logStream);
+
// Get a List of Pokemon objects in this game.
// 0 = null 1-whatever = the Pokemon.
List<Pokemon> getPokemon();