diff options
Diffstat (limited to 'src/com/dabomstew')
11 files changed, 237 insertions, 396 deletions
diff --git a/src/com/dabomstew/pkrandom/config/gameboy_jap.tbl b/src/com/dabomstew/pkrandom/config/gameboy_jap.tbl index fce8c2f..ed5410e 100755 --- a/src/com/dabomstew/pkrandom/config/gameboy_jap.tbl +++ b/src/com/dabomstew/pkrandom/config/gameboy_jap.tbl @@ -176,4 +176,5 @@ FF=9 4F=\n
51=\p
55=\l
-57=\e
\ No newline at end of file +57=\e
+58=\r
\ No newline at end of file diff --git a/src/com/dabomstew/pkrandom/config/gsc_english.tbl b/src/com/dabomstew/pkrandom/config/gsc_english.tbl index ac7cd6e..e5fe32a 100755 --- a/src/com/dabomstew/pkrandom/config/gsc_english.tbl +++ b/src/com/dabomstew/pkrandom/config/gsc_english.tbl @@ -1,6 +1,5 @@ 4A=[pk]
54=[POKé]
-58=$
74=№
75=…
7F=
diff --git a/src/com/dabomstew/pkrandom/config/gsc_espita.tbl b/src/com/dabomstew/pkrandom/config/gsc_espita.tbl index adb4537..d9f603c 100755 --- a/src/com/dabomstew/pkrandom/config/gsc_espita.tbl +++ b/src/com/dabomstew/pkrandom/config/gsc_espita.tbl @@ -1,6 +1,5 @@ 4A=[pk]
54=[POKé]
-58=$
74=№
75=…
7F=
diff --git a/src/com/dabomstew/pkrandom/config/gsc_freger.tbl b/src/com/dabomstew/pkrandom/config/gsc_freger.tbl index 7e481b3..b896459 100755 --- a/src/com/dabomstew/pkrandom/config/gsc_freger.tbl +++ b/src/com/dabomstew/pkrandom/config/gsc_freger.tbl @@ -1,6 +1,5 @@ 4A=[pk]
54=[POKé]
-58=$
74=№
75=…
7F=
diff --git a/src/com/dabomstew/pkrandom/config/rby_english.tbl b/src/com/dabomstew/pkrandom/config/rby_english.tbl index 9ec4cbe..88c21d6 100755 --- a/src/com/dabomstew/pkrandom/config/rby_english.tbl +++ b/src/com/dabomstew/pkrandom/config/rby_english.tbl @@ -1,6 +1,5 @@ 4A=[pk]
54=[POKé]
-58=$
74=№
75=…
7F=
diff --git a/src/com/dabomstew/pkrandom/config/rby_espita.tbl b/src/com/dabomstew/pkrandom/config/rby_espita.tbl index 405b0ba..3ca057c 100755 --- a/src/com/dabomstew/pkrandom/config/rby_espita.tbl +++ b/src/com/dabomstew/pkrandom/config/rby_espita.tbl @@ -1,6 +1,5 @@ 4A=[pk]
54=[POKé]
-58=$
74=№
75=…
7F=
diff --git a/src/com/dabomstew/pkrandom/config/rby_freger.tbl b/src/com/dabomstew/pkrandom/config/rby_freger.tbl index 3829931..93ad462 100755 --- a/src/com/dabomstew/pkrandom/config/rby_freger.tbl +++ b/src/com/dabomstew/pkrandom/config/rby_freger.tbl @@ -1,6 +1,5 @@ 4A=[pk]
54=[POKé]
-58=$
74=№
75=…
7F=
diff --git a/src/com/dabomstew/pkrandom/constants/GBConstants.java b/src/com/dabomstew/pkrandom/constants/GBConstants.java index 2e84ca3..64cbb7d 100644 --- a/src/com/dabomstew/pkrandom/constants/GBConstants.java +++ b/src/com/dabomstew/pkrandom/constants/GBConstants.java @@ -7,7 +7,7 @@ public class GBConstants { public static final int jpFlagOffset = 0x14A, versionOffset = 0x14C, crcOffset = 0x14E, romSigOffset = 0x134, isGBCOffset = 0x143, romCodeOffset = 0x13F; - public static final int stringTerminator = 0x50, stringNull = 0x00; + public static final int stringTerminator = 0x50, stringPrintedTextEnd = 0x57, stringPrintedTextPromptEnd = 0x58; public static final int bankSize = 0x4000; diff --git a/src/com/dabomstew/pkrandom/romhandlers/AbstractGBCRomHandler.java b/src/com/dabomstew/pkrandom/romhandlers/AbstractGBCRomHandler.java new file mode 100644 index 0000000..ef2aa3b --- /dev/null +++ b/src/com/dabomstew/pkrandom/romhandlers/AbstractGBCRomHandler.java @@ -0,0 +1,183 @@ +package com.dabomstew.pkrandom.romhandlers; + +import java.io.ByteArrayOutputStream; +import java.io.FileNotFoundException; +import java.io.PrintStream; +import java.io.UnsupportedEncodingException; +import java.util.HashMap; +import java.util.Map; +import java.util.Random; +import java.util.Scanner; + +import com.dabomstew.pkrandom.FileFunctions; +import com.dabomstew.pkrandom.constants.GBConstants; + +public abstract class AbstractGBCRomHandler extends AbstractGBRomHandler { + + private String[] tb; + private Map<String, Byte> d; + private int longestTableToken; + + public AbstractGBCRomHandler(Random random, PrintStream logStream) { + super(random, logStream); + } + + protected void clearTextTables() { + tb = new String[256]; + if (d != null) { + d.clear(); + } else { + d = new HashMap<String, Byte>(); + } + longestTableToken = 0; + } + + protected void readTextTable(String name) { + try { + Scanner sc = new Scanner(FileFunctions.openConfig(name + ".tbl"), "UTF-8"); + while (sc.hasNextLine()) { + String q = sc.nextLine(); + if (!q.trim().isEmpty()) { + String[] r = q.split("=", 2); + if (r[1].endsWith("\r\n")) { + r[1] = r[1].substring(0, r[1].length() - 2); + } + int hexcode = Integer.parseInt(r[0], 16); + if (tb[hexcode] != null) { + String oldMatch = tb[hexcode]; + tb[hexcode] = null; + if (d.get(oldMatch) == hexcode) { + d.remove(oldMatch); + } + } + tb[hexcode] = r[1]; + longestTableToken = Math.max(longestTableToken, r[1].length()); + d.put(r[1], (byte) hexcode); + } + } + sc.close(); + } catch (FileNotFoundException e) { + } + + } + + protected String readString(int offset, int maxLength, boolean textEngineMode) { + StringBuilder string = new StringBuilder(); + for (int c = 0; c < maxLength; c++) { + int currChar = rom[offset + c] & 0xFF; + if (tb[currChar] != null) { + string.append(tb[currChar]); + if (textEngineMode && (tb[currChar].equals("\\r") || tb[currChar].equals("\\e"))) { + break; + } + } else { + if (currChar == GBConstants.stringTerminator) { + break; + } else { + string.append("\\x" + String.format("%02X", currChar)); + } + } + } + return string.toString(); + } + + protected int lengthOfStringAt(int offset, boolean textEngineMode) { + int len = 0; + while (rom[offset + len] != GBConstants.stringTerminator + && (!textEngineMode || (rom[offset + len] != GBConstants.stringPrintedTextEnd && rom[offset + len] != GBConstants.stringPrintedTextPromptEnd))) { + len++; + } + + if (textEngineMode + && (rom[offset + len] == GBConstants.stringPrintedTextEnd || rom[offset + len] == GBConstants.stringPrintedTextPromptEnd)) { + len++; + } + return len; + } + + protected byte[] translateString(String text) { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + while (text.length() != 0) { + int i = Math.max(0, longestTableToken - text.length()); + if (text.charAt(0) == '\\' && text.charAt(1) == 'x') { + baos.write(Integer.parseInt(text.substring(2, 4), 16)); + text = text.substring(4); + } else { + while (!(d.containsKey(text.substring(0, longestTableToken - i)) || (i == longestTableToken))) { + i++; + } + if (i == longestTableToken) { + text = text.substring(1); + } else { + baos.write(d.get(text.substring(0, longestTableToken - i)) & 0xFF); + text = text.substring(longestTableToken - i); + } + } + } + return baos.toByteArray(); + } + + protected String readFixedLengthString(int offset, int length) { + return readString(offset, length, false); + } + + // pads the length with terminators, so length should be at least str's len + 1 + protected void writeFixedLengthString(String str, int offset, int length) { + byte[] translated = translateString(str); + int len = Math.min(translated.length, length); + System.arraycopy(translated, 0, rom, offset, len); + while (len < length) { + rom[offset + len] = GBConstants.stringTerminator; + len++; + } + } + + protected void writeVariableLengthString(String str, int offset, boolean alreadyTerminated) { + byte[] translated = translateString(str); + System.arraycopy(translated, 0, rom, offset, translated.length); + if (!alreadyTerminated) { + rom[offset + translated.length] = GBConstants.stringTerminator; + } + } + + protected int makeGBPointer(int offset) { + if (offset < GBConstants.bankSize) { + return offset; + } else { + return (offset % GBConstants.bankSize) + GBConstants.bankSize; + } + } + + protected int bankOf(int offset) { + return (offset / GBConstants.bankSize); + } + + protected int calculateOffset(int bank, int pointer) { + if (pointer < GBConstants.bankSize) { + return pointer; + } else { + return (pointer % GBConstants.bankSize) + bank * GBConstants.bankSize; + } + } + + protected String readVariableLengthString(int offset, boolean textEngineMode) { + return readString(offset, Integer.MAX_VALUE, textEngineMode); + } + + protected static boolean romSig(byte[] rom, String sig) { + try { + int sigOffset = GBConstants.romSigOffset; + byte[] sigBytes = sig.getBytes("US-ASCII"); + for (int i = 0; i < sigBytes.length; i++) { + if (rom[sigOffset + i] != sigBytes[i]) { + return false; + } + } + return true; + } catch (UnsupportedEncodingException ex) { + return false; + } + + } + +} diff --git a/src/com/dabomstew/pkrandom/romhandlers/Gen1RomHandler.java b/src/com/dabomstew/pkrandom/romhandlers/Gen1RomHandler.java index 7b00f27..a9fd0c0 100755 --- a/src/com/dabomstew/pkrandom/romhandlers/Gen1RomHandler.java +++ b/src/com/dabomstew/pkrandom/romhandlers/Gen1RomHandler.java @@ -29,7 +29,6 @@ import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.PrintStream; -import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -44,7 +43,6 @@ import java.util.TreeMap; import com.dabomstew.pkrandom.FileFunctions; import com.dabomstew.pkrandom.GFXFunctions; import com.dabomstew.pkrandom.MiscTweak; -import com.dabomstew.pkrandom.RomFunctions; import com.dabomstew.pkrandom.constants.GBConstants; import com.dabomstew.pkrandom.constants.Gen1Constants; import com.dabomstew.pkrandom.constants.GlobalConstants; @@ -65,7 +63,7 @@ import com.dabomstew.pkrandom.pokemon.TrainerPokemon; import com.dabomstew.pkrandom.pokemon.Type; import compressors.Gen1Decmp; -public class Gen1RomHandler extends AbstractGBRomHandler { +public class Gen1RomHandler extends AbstractGBCRomHandler { public static class Factory extends RomHandler.Factory { @@ -340,9 +338,6 @@ public class Gen1RomHandler extends AbstractGBRomHandler { private List<Pokemon> pokemonList; private RomEntry romEntry; private Move[] moves; - private String[] tb; - private Map<String, Byte> d; - private int longestTableToken; private String[] itemNames; private String[] mapNames; private SubMap[] maps; @@ -367,8 +362,6 @@ public class Gen1RomHandler extends AbstractGBRomHandler { pokeRBYToNumTable = new int[256]; moveNumToRomTable = new int[256]; moveRomToNumTable = new int[256]; - tb = new String[256]; - d = new HashMap<String, Byte>(); maps = new SubMap[256]; xAccNerfed = false; clearTextTables(); @@ -420,41 +413,6 @@ public class Gen1RomHandler extends AbstractGBRomHandler { return null; } - private void clearTextTables() { - tb = new String[256]; - d.clear(); - longestTableToken = 0; - } - - private void readTextTable(String name) { - try { - Scanner sc = new Scanner(FileFunctions.openConfig(name + ".tbl"), "UTF-8"); - while (sc.hasNextLine()) { - String q = sc.nextLine(); - if (!q.trim().isEmpty()) { - String[] r = q.split("=", 2); - if (r[1].endsWith("\r\n")) { - r[1] = r[1].substring(0, r[1].length() - 2); - } - int hexcode = Integer.parseInt(r[0], 16); - if (tb[hexcode] != null) { - String oldMatch = tb[hexcode]; - tb[hexcode] = null; - if (d.get(oldMatch) == hexcode) { - d.remove(oldMatch); - } - } - tb[hexcode] = r[1]; - longestTableToken = Math.max(longestTableToken, r[1].length()); - d.put(r[1], (byte) hexcode); - } - } - sc.close(); - } catch (FileNotFoundException e) { - } - - } - @Override public void savingRom() { savePokemonStats(); @@ -466,8 +424,8 @@ public class Gen1RomHandler extends AbstractGBRomHandler { int offset = romEntry.getValue("MoveNamesOffset"); String[] moveNames = new String[moveCount + 1]; for (int i = 1; i <= moveCount; i++) { - moveNames[i] = readVariableLengthString(offset); - offset += lengthOfStringAt(offset) + 1; + moveNames[i] = readVariableLengthString(offset, false); + offset += lengthOfStringAt(offset, false) + 1; } return moveNames; } @@ -644,129 +602,7 @@ public class Gen1RomHandler extends AbstractGBRomHandler { return names; } - private String readString(int offset, int maxLength) { - StringBuilder string = new StringBuilder(); - for (int c = 0; c < maxLength; c++) { - int currChar = rom[offset + c] & 0xFF; - if (tb[currChar] != null) { - string.append(tb[currChar]); - } else { - if (currChar == GBConstants.stringTerminator || currChar == GBConstants.stringNull) { - break; - } else { - string.append("\\x" + String.format("%02X", currChar)); - } - } - } - return string.toString(); - } - - public byte[] translateString(String text) { - List<Byte> data = new ArrayList<Byte>(); - while (text.length() != 0) { - int i = Math.max(0, longestTableToken - text.length()); - if (text.charAt(0) == '\\' && text.charAt(1) == 'x') { - data.add((byte) Integer.parseInt(text.substring(2, 4), 16)); - text = text.substring(4); - } else { - while (!(d.containsKey(text.substring(0, longestTableToken - i)) || (i == longestTableToken))) { - i++; - } - if (i == longestTableToken) { - text = text.substring(1); - } else { - data.add(d.get(text.substring(0, longestTableToken - i))); - text = text.substring(longestTableToken - i); - } - } - } - byte[] ret = new byte[data.size()]; - for (int i = 0; i < ret.length; i++) { - ret[i] = data.get(i); - } - return ret; - } - - private String readFixedLengthString(int offset, int length) { - return readString(offset, length); - } - - private void writeFixedLengthString(String str, int offset, int length) { - byte[] translated = translateString(str); - int len = Math.min(translated.length, length); - System.arraycopy(translated, 0, rom, offset, len); - while (len < length) { - rom[offset + len] = GBConstants.stringTerminator; - len++; - } - } - - private int makeGBPointer(int offset) { - if (offset < GBConstants.bankSize) { - return offset; - } else { - return (offset % GBConstants.bankSize) + GBConstants.bankSize; - } - } - - private int bankOf(int offset) { - return (offset / GBConstants.bankSize); - } - - private int calculateOffset(int bank, int pointer) { - if (pointer < GBConstants.bankSize) { - return pointer; - } else { - return (pointer % GBConstants.bankSize) + bank * GBConstants.bankSize; - } - } - - public String readVariableLengthString(int offset) { - return readString(offset, Integer.MAX_VALUE); - } - - public byte[] traduire(String str) { - return translateString(str); - } - - public String readVariableLengthScriptString(int offset) { - return readString(offset, Integer.MAX_VALUE); - } - - private void writeFixedLengthScriptString(String str, int offset, int length) { - byte[] translated = translateString(str); - int len = Math.min(translated.length, length); - System.arraycopy(translated, 0, rom, offset, len); - while (len < length) { - rom[offset + len] = GBConstants.stringNull; - len++; - } - } - - private int lengthOfStringAt(int offset) { - int len = 0; - while (rom[offset + len] != GBConstants.stringTerminator && rom[offset + len] != GBConstants.stringNull) { - len++; - } - return len; - } - - private static boolean romSig(byte[] rom, String sig) { - try { - int sigOffset = GBConstants.romSigOffset; - byte[] sigBytes = sig.getBytes("US-ASCII"); - for (int i = 0; i < sigBytes.length; i++) { - if (rom[sigOffset + i] != sigBytes[i]) { - return false; - } - } - return true; - } catch (UnsupportedEncodingException ex) { - return false; - } - - } - + @Override public List<Pokemon> getStarters() { // Get the starters @@ -807,10 +643,10 @@ public class Gen1RomHandler extends AbstractGBRomHandler { // Starter text if (romEntry.getValue("CanChangeStarterText") > 0) { - List<Integer> starterTextOffsets = RomFunctions.search(rom, traduire("So! You want the")); - for (int i = 0; i < 3 && i < starterTextOffsets.size(); i++) { - writeFixedLengthScriptString("So! You want\\n" + newStarters.get(i).name + "?\\e", - starterTextOffsets.get(i), lengthOfStringAt(starterTextOffsets.get(i)) + 1); + int[] starterTextOffsets = romEntry.arrayEntries.get("StarterTextOffsets"); + for (int i = 0; i < 3 && i < starterTextOffsets.length; i++) { + writeVariableLengthString(String.format("So! You want\\n%s?\\e", newStarters.get(i).name), + starterTextOffsets[i], true); } } @@ -1439,7 +1275,7 @@ public class Gen1RomHandler extends AbstractGBRomHandler { for (TMTextEntry tte : romEntry.tmTexts) { String moveName = moveNames[moveNumToRomTable[moveIndexes.get(tte.number - 1)]]; String text = tte.template.replace("%m", moveName); - writeFixedLengthScriptString(text, tte.offset, lengthOfStringAt(tte.offset)); + writeVariableLengthString(text, tte.offset, true); } } @@ -1589,8 +1425,8 @@ public class Gen1RomHandler extends AbstractGBRomHandler { List<String> tcNames = new ArrayList<String>(); int offset = offsets[offsets.length - 1]; for (int j = 0; j < Gen1Constants.tclassesCounts[1]; j++) { - String name = readVariableLengthString(offset); - offset += (internalStringLength(name) + 1); + String name = readVariableLengthString(offset, false); + offset += lengthOfStringAt(offset, false) + 1; tcNames.add(name); } return tcNames; @@ -1612,8 +1448,8 @@ public class Gen1RomHandler extends AbstractGBRomHandler { List<String> trainerNames = new ArrayList<String>(); int offset = offsets[offsets.length - 1]; for (int j = 0; j < Gen1Constants.tclassesCounts[1]; j++) { - String name = readVariableLengthString(offset); - offset += (internalStringLength(name) + 1); + String name = readVariableLengthString(offset, false); + offset += lengthOfStringAt(offset, false) + 1; if (Gen1Constants.singularTrainers.contains(j)) { trainerNames.add(name); } @@ -1628,12 +1464,12 @@ public class Gen1RomHandler extends AbstractGBRomHandler { Iterator<String> trainerNamesI = trainerNames.iterator(); int offset = offsets[offsets.length - 1]; for (int j = 0; j < Gen1Constants.tclassesCounts[1]; j++) { - String name = readVariableLengthString(offset); + int oldLength = lengthOfStringAt(offset, false) + 1; if (Gen1Constants.singularTrainers.contains(j)) { String newName = trainerNamesI.next(); - writeFixedLengthString(newName, offset, internalStringLength(name) + 1); + writeFixedLengthString(newName, offset, oldLength); } - offset += (internalStringLength(name) + 1); + offset += oldLength; } } } @@ -1657,8 +1493,8 @@ public class Gen1RomHandler extends AbstractGBRomHandler { for (int i = 0; i < offsets.length; i++) { int offset = offsets[i]; for (int j = 0; j < Gen1Constants.tclassesCounts[i]; j++) { - String name = readVariableLengthString(offset); - offset += (internalStringLength(name) + 1); + String name = readVariableLengthString(offset, false); + offset += lengthOfStringAt(offset, false) + 1; if (i == 0 || !Gen1Constants.singularTrainers.contains(j)) { trainerClassNames.add(name); } @@ -1667,8 +1503,8 @@ public class Gen1RomHandler extends AbstractGBRomHandler { } else { int offset = offsets[0]; for (int j = 0; j < Gen1Constants.tclassesCounts[1]; j++) { - String name = readVariableLengthString(offset); - offset += (internalStringLength(name) + 1); + String name = readVariableLengthString(offset, false); + offset += lengthOfStringAt(offset, false) + 1; if (!Gen1Constants.singularTrainers.contains(j)) { trainerClassNames.add(name); } @@ -1686,23 +1522,23 @@ public class Gen1RomHandler extends AbstractGBRomHandler { for (int i = 0; i < offsets.length; i++) { int offset = offsets[i]; for (int j = 0; j < Gen1Constants.tclassesCounts[i]; j++) { - String name = readVariableLengthString(offset); + int oldLength = lengthOfStringAt(offset, false) + 1; if (i == 0 || !Gen1Constants.singularTrainers.contains(j)) { String newName = tcNamesIter.next(); - writeFixedLengthString(newName, offset, internalStringLength(name) + 1); + writeFixedLengthString(newName, offset, oldLength); } - offset += (internalStringLength(name) + 1); + offset += oldLength; } } } else { int offset = offsets[0]; for (int j = 0; j < Gen1Constants.tclassesCounts[1]; j++) { - String name = readVariableLengthString(offset); + int oldLength = lengthOfStringAt(offset, false) + 1; if (!Gen1Constants.singularTrainers.contains(j)) { String newName = tcNamesIter.next(); - writeFixedLengthString(newName, offset, internalStringLength(name) + 1); + writeFixedLengthString(newName, offset, oldLength); } - offset += (internalStringLength(name) + 1); + offset += oldLength; } } } @@ -2046,7 +1882,7 @@ public class Gen1RomHandler extends AbstractGBRomHandler { for (int i = 0; i < 0x25; i++) { int externalOffset = calculateOffset(mapNameBank, readWord(mapNameTableOffset + 1)); usedExternal.add(externalOffset); - mapNames[i] = readVariableLengthString(externalOffset); + mapNames[i] = readVariableLengthString(externalOffset, false); mapNameTableOffset += 3; } @@ -2056,7 +1892,7 @@ public class Gen1RomHandler extends AbstractGBRomHandler { while ((rom[mapNameTableOffset] & 0xFF) != 0xFF) { int maxMap = rom[mapNameTableOffset] & 0xFF; int nameOffset = calculateOffset(mapNameBank, readWord(mapNameTableOffset + 2)); - String actualName = readVariableLengthString(nameOffset).trim(); + String actualName = readVariableLengthString(nameOffset, false).trim(); if (usedExternal.contains(nameOffset)) { for (int i = lastMaxMap; i < maxMap; i++) { if (maps[i] != null) { @@ -2220,7 +2056,7 @@ public class Gen1RomHandler extends AbstractGBRomHandler { int entryOffset = tableOffset + entry * entryLength; trade.requestedPokemon = pokes[pokeRBYToNumTable[rom[entryOffset] & 0xFF]]; trade.givenPokemon = pokes[pokeRBYToNumTable[rom[entryOffset + 1] & 0xFF]]; - trade.nickname = readString(entryOffset + 3, nicknameLength); + trade.nickname = readString(entryOffset + 3, nicknameLength, false); trades.add(trade); } diff --git a/src/com/dabomstew/pkrandom/romhandlers/Gen2RomHandler.java b/src/com/dabomstew/pkrandom/romhandlers/Gen2RomHandler.java index 8b753a8..a6315da 100755 --- a/src/com/dabomstew/pkrandom/romhandlers/Gen2RomHandler.java +++ b/src/com/dabomstew/pkrandom/romhandlers/Gen2RomHandler.java @@ -29,7 +29,6 @@ import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.PrintStream; -import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; @@ -64,7 +63,7 @@ import com.dabomstew.pkrandom.pokemon.Trainer; import com.dabomstew.pkrandom.pokemon.TrainerPokemon; import compressors.Gen2Decmp; -public class Gen2RomHandler extends AbstractGBRomHandler { +public class Gen2RomHandler extends AbstractGBCRomHandler { public static class Factory extends RomHandler.Factory { @@ -273,9 +272,6 @@ public class Gen2RomHandler extends AbstractGBRomHandler { private List<Pokemon> pokemonList; private RomEntry romEntry; private Move[] moves; - private String[] tb; - private Map<String, Byte> d; - private int longestTableToken; private boolean havePatchedFleeing; private String[] itemNames; private List<Integer> itemOffs; @@ -299,8 +295,6 @@ public class Gen2RomHandler extends AbstractGBRomHandler { @Override public void loadedRom() { romEntry = checkRomEntry(this.rom); - tb = new String[256]; - d = new HashMap<String, Byte>(); clearTextTables(); readTextTable("gameboy_jap"); if (romEntry.extraTableFile != null && romEntry.extraTableFile.equalsIgnoreCase("none") == false) { @@ -346,39 +340,6 @@ public class Gen2RomHandler extends AbstractGBRomHandler { return null; } - private void clearTextTables() { - tb = new String[256]; - d.clear(); - longestTableToken = 0; - } - - private void readTextTable(String name) { - try { - Scanner sc = new Scanner(FileFunctions.openConfig(name + ".tbl"), "UTF-8"); - while (sc.hasNextLine()) { - String q = sc.nextLine(); - if (!q.trim().isEmpty()) { - String[] r = q.split("=", 2); - if (r[1].endsWith("\r\n")) { - r[1] = r[1].substring(0, r[1].length() - 2); - } - int hexcode = Integer.parseInt(r[0], 16); - if (tb[hexcode] != null) { - String oldMatch = tb[hexcode]; - tb[hexcode] = null; - d.remove(oldMatch); - } - tb[hexcode] = r[1]; - longestTableToken = Math.max(longestTableToken, r[1].length()); - d.put(r[1], (byte) hexcode); - } - } - sc.close(); - } catch (FileNotFoundException e) { - } - - } - @Override public void savingRom() { savePokemonStats(); @@ -425,8 +386,8 @@ public class Gen2RomHandler extends AbstractGBRomHandler { int offset = romEntry.getValue("MoveNamesOffset"); String[] moveNames = new String[Gen2Constants.moveCount + 1]; for (int i = 1; i <= Gen2Constants.moveCount; i++) { - moveNames[i] = readVariableLengthString(offset); - offset += lengthOfStringAt(offset) + 1; + moveNames[i] = readVariableLengthString(offset, false); + offset += lengthOfStringAt(offset, false) + 1; } return moveNames; } @@ -533,129 +494,6 @@ public class Gen2RomHandler extends AbstractGBRomHandler { return names; } - private String readString(int offset, int maxLength) { - StringBuilder string = new StringBuilder(); - for (int c = 0; c < maxLength; c++) { - int currChar = rom[offset + c] & 0xFF; - if (tb[currChar] != null) { - string.append(tb[currChar]); - } else { - if (currChar == GBConstants.stringTerminator || currChar == GBConstants.stringNull) { - break; - } else { - string.append("\\x" + String.format("%02X", currChar)); - } - } - } - return string.toString(); - } - - public byte[] translateString(String text) { - List<Byte> data = new ArrayList<Byte>(); - while (text.length() != 0) { - int i = Math.max(0, longestTableToken - text.length()); - if (text.charAt(0) == '\\' && text.charAt(1) == 'x') { - data.add((byte) Integer.parseInt(text.substring(2, 4), 16)); - text = text.substring(4); - } else { - while (!(d.containsKey(text.substring(0, longestTableToken - i)) || (i == longestTableToken))) { - i++; - } - if (i == longestTableToken) { - text = text.substring(1); - } else { - data.add(d.get(text.substring(0, longestTableToken - i))); - text = text.substring(longestTableToken - i); - } - } - } - byte[] ret = new byte[data.size()]; - for (int i = 0; i < ret.length; i++) { - ret[i] = data.get(i); - } - return ret; - } - - private String readFixedLengthString(int offset, int length) { - return readString(offset, length); - } - - public String readVariableLengthString(int offset) { - return readString(offset, Integer.MAX_VALUE); - } - - public String readVariableLengthScriptString(int offset) { - return readString(offset, Integer.MAX_VALUE); - } - - public byte[] traduire(String str) { - return translateString(str); - } - - private void writeFixedLengthString(String str, int offset, int length) { - byte[] translated = translateString(str); - int len = Math.min(translated.length, length); - System.arraycopy(translated, 0, rom, offset, len); - while (len < length) { - rom[offset + len] = GBConstants.stringTerminator; - len++; - } - } - - private void writeFixedLengthScriptString(String str, int offset, int length) { - byte[] translated = translateString(str); - int len = Math.min(translated.length, length); - System.arraycopy(translated, 0, rom, offset, len); - while (len < length) { - rom[offset + len] = GBConstants.stringNull; - len++; - } - } - - private int lengthOfStringAt(int offset) { - int len = 0; - while (rom[offset + len] != GBConstants.stringTerminator && rom[offset + len] != GBConstants.stringNull) { - len++; - } - return len; - } - - private int makeGBPointer(int offset) { - if (offset < GBConstants.bankSize) { - return offset; - } else { - return (offset % GBConstants.bankSize) + GBConstants.bankSize; - } - } - - private int bankOf(int offset) { - return (offset / GBConstants.bankSize); - } - - private int calculateOffset(int bank, int pointer) { - if (pointer < GBConstants.bankSize) { - return pointer; - } else { - return (pointer % GBConstants.bankSize) + bank * GBConstants.bankSize; - } - } - - private static boolean romSig(byte[] rom, String sig) { - try { - int sigOffset = GBConstants.romCodeOffset; - byte[] sigBytes = sig.getBytes("US-ASCII"); - for (int i = 0; i < sigBytes.length; i++) { - if (rom[sigOffset + i] != sigBytes[i]) { - return false; - } - } - return true; - } catch (UnsupportedEncodingException ex) { - return false; - } - - } - @Override public List<Pokemon> getStarters() { // Get the starters @@ -684,20 +522,11 @@ public class Gen2RomHandler extends AbstractGBRomHandler { // Attempt to replace text if (romEntry.getValue("CanChangeStarterText") > 0) { - List<Integer> cyndaTexts = RomFunctions.search(rom, traduire(Gen2Constants.starterNames[0])); - int offset = cyndaTexts.get(romEntry.isCrystal ? 1 : 0); - String pokeName = newStarters.get(0).name; - writeFixedLengthScriptString(pokeName + "?\\e", offset, lengthOfStringAt(offset) + 1); - - List<Integer> totoTexts = RomFunctions.search(rom, traduire(Gen2Constants.starterNames[1])); - offset = totoTexts.get(romEntry.isCrystal ? 1 : 0); - pokeName = newStarters.get(1).name; - writeFixedLengthScriptString(pokeName + "?\\e", offset, lengthOfStringAt(offset) + 1); - - List<Integer> chikoTexts = RomFunctions.search(rom, traduire(Gen2Constants.starterNames[2])); - offset = chikoTexts.get(romEntry.isCrystal ? 1 : 0); - pokeName = newStarters.get(2).name; - writeFixedLengthScriptString(pokeName + "?\\e", offset, lengthOfStringAt(offset) + 1); + int[] starterTextOffsets = romEntry.arrayEntries.get("StarterTextOffsets"); + for (int i = 0; i < 3 && i < starterTextOffsets.length; i++) { + writeVariableLengthString(String.format("%s?\\e", newStarters.get(i).name), + starterTextOffsets[i], true); + } } return true; } @@ -1017,11 +846,10 @@ public class Gen2RomHandler extends AbstractGBRomHandler { Trainer tr = new Trainer(); tr.offset = offs; tr.trainerclass = i; - String name = readVariableLengthString(offs); + String name = readVariableLengthString(offs, false); tr.name = name; tr.fullDisplayName = tcnames.get(i) + " " + name; - int len = lengthOfStringAt(offs); - offs += len + 1; + offs += lengthOfStringAt(offs, false) + 1; int dataType = rom[offs] & 0xFF; tr.poketype = dataType; offs++; @@ -1267,7 +1095,7 @@ public class Gen2RomHandler extends AbstractGBRomHandler { private void writePaddedPokemonName(String name, int length, int offset) { String paddedName = String.format("%-" + length + "s", name); - byte[] rawData = traduire(paddedName); + byte[] rawData = translateString(paddedName); for (int i = 0; i < length; i++) { rom[offset + i] = rawData[i]; } @@ -1305,7 +1133,7 @@ public class Gen2RomHandler extends AbstractGBRomHandler { for (TMTextEntry tte : romEntry.tmTexts) { String moveName = moveNames[moveIndexes.get(tte.number - 1)]; String text = tte.template.replace("%m", moveName); - writeFixedLengthScriptString(text, tte.offset, lengthOfStringAt(tte.offset)); + writeVariableLengthString(text, tte.offset, true); } } @@ -1397,7 +1225,7 @@ public class Gen2RomHandler extends AbstractGBRomHandler { rom[menuOffset++] = Gen2Constants.mtMenuInitByte; rom[menuOffset++] = 0x4; for (int i = 0; i < 4; i++) { - byte[] trans = traduire(names[i]); + byte[] trans = translateString(names[i]); System.arraycopy(trans, 0, rom, menuOffset, trans.length); menuOffset += trans.length; rom[menuOffset++] = GBConstants.stringTerminator; @@ -1589,9 +1417,9 @@ public class Gen2RomHandler extends AbstractGBRomHandler { int offs = pointers[i]; int limit = trainerclasslimits[i]; for (int trnum = 0; trnum < limit; trnum++) { - String name = readVariableLengthString(offs); + String name = readVariableLengthString(offs, false); allTrainers.add(name); - offs += name.length() + 1; + offs += lengthOfStringAt(offs, false) + 1; int dataType = rom[offs] & 0xFF; offs++; while ((rom[offs] & 0xFF) != 0xFF) { @@ -1633,13 +1461,12 @@ public class Gen2RomHandler extends AbstractGBRomHandler { int limit = trainerclasslimits[i]; offsetsInNew[i] = oInNewCurrent; for (int trnum = 0; trnum < limit; trnum++) { - String name = readVariableLengthString(offs); String newName = allTrainers.next(); byte[] newNameStr = translateString(newName); newData.write(newNameStr); newData.write(GBConstants.stringTerminator); oInNewCurrent += newNameStr.length + 1; - offs += internalStringLength(name) + 1; + offs += lengthOfStringAt(offs, false) + 1; int dataType = rom[offs] & 0xFF; offs++; newData.write(dataType); @@ -1721,8 +1548,8 @@ public class Gen2RomHandler extends AbstractGBRomHandler { int offset = romEntry.getValue("TrainerClassNamesOffset"); List<String> trainerClassNames = new ArrayList<String>(); for (int j = 0; j < amount; j++) { - String name = readVariableLengthString(offset); - offset += lengthOfStringAt(offset) + 1; + String name = readVariableLengthString(offset, false); + offset += lengthOfStringAt(offset, false) + 1; trainerClassNames.add(name); } return trainerClassNames; @@ -1735,7 +1562,7 @@ public class Gen2RomHandler extends AbstractGBRomHandler { int offset = romEntry.getValue("TrainerClassNamesOffset"); Iterator<String> trainerClassNamesI = trainerClassNames.iterator(); for (int j = 0; j < amount; j++) { - int len = lengthOfStringAt(offset) + 1; + int len = lengthOfStringAt(offset, false) + 1; String newName = trainerClassNamesI.next(); writeFixedLengthString(newName, offset, len); offset += len; @@ -1918,7 +1745,7 @@ public class Gen2RomHandler extends AbstractGBRomHandler { for (int i = 0; i < lmCount; i++) { int lmNameOffset = calculateOffset(lmBank, readWord(lmOffset + i * 4 + 2)); - landmarkNames[i] = readVariableLengthString(lmNameOffset).replace("\\x1F", " "); + landmarkNames[i] = readVariableLengthString(lmNameOffset, false).replace("\\x1F", " "); } } @@ -2132,13 +1959,13 @@ public class Gen2RomHandler extends AbstractGBRomHandler { int entryOffset = tableOffset + entry * entryLength; trade.requestedPokemon = pokes[rom[entryOffset + 1] & 0xFF]; trade.givenPokemon = pokes[rom[entryOffset + 2] & 0xFF]; - trade.nickname = readString(entryOffset + 3, nicknameLength); + trade.nickname = readString(entryOffset + 3, nicknameLength, false); int atkdef = rom[entryOffset + 3 + nicknameLength] & 0xFF; int spdspc = rom[entryOffset + 4 + nicknameLength] & 0xFF; trade.ivs = new int[] { (atkdef >> 4) & 0xF, atkdef & 0xF, (spdspc >> 4) & 0xF, spdspc & 0xF }; trade.item = rom[entryOffset + 5 + nicknameLength] & 0xFF; trade.otId = readWord(entryOffset + 6 + nicknameLength); - trade.otName = readString(entryOffset + 8 + nicknameLength, otLength); + trade.otName = readString(entryOffset + 8 + nicknameLength, otLength, false); trades.add(trade); } |