forked from Dabomstew/universal-pokemon-randomizer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor custom names system completely.
This combines everything into one binary file (customnames.rncn for now). The GUI will now ask if you want to convert your old text files into a new file, if they exist. Still TODO: * Create a GUI editor for custom names, since they are no longer in plaintext files * Refactor ROM-side detection of doubles classes/trainers to no longer use the dumb string tests that only (barely) work for English language. Even constant value arrays of which classes are doubles classes will be better. This also fixes a minor bug with the new moveset randomization code.
- Loading branch information
Showing
19 changed files
with
397 additions
and
508 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
package com.dabomstew.pkrandom; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Scanner; | ||
|
||
public class CustomNamesSet { | ||
|
||
private List<String> trainerNames; | ||
private List<String> trainerClasses; | ||
private List<String> doublesTrainerNames; | ||
private List<String> doublesTrainerClasses; | ||
private List<String> pokemonNicknames; | ||
|
||
private static final int CUSTOM_NAMES_VERSION = 1; | ||
|
||
// Standard constructor: read binary data from an input stream. | ||
public CustomNamesSet(InputStream data) throws IOException { | ||
|
||
if (data.read() != CUSTOM_NAMES_VERSION) { | ||
throw new IOException("Invalid custom names file provided."); | ||
} | ||
|
||
trainerNames = readNamesBlock(data); | ||
trainerClasses = readNamesBlock(data); | ||
doublesTrainerNames = readNamesBlock(data); | ||
doublesTrainerClasses = readNamesBlock(data); | ||
pokemonNicknames = readNamesBlock(data); | ||
} | ||
|
||
// Private constructor: blank all lists. | ||
// Used for importing old names. | ||
private CustomNamesSet() { | ||
trainerNames = new ArrayList<String>(); | ||
trainerClasses = new ArrayList<String>(); | ||
doublesTrainerNames = new ArrayList<String>(); | ||
doublesTrainerClasses = new ArrayList<String>(); | ||
pokemonNicknames = new ArrayList<String>(); | ||
} | ||
|
||
private List<String> readNamesBlock(InputStream in) throws IOException { | ||
// Read the size of the block to come. | ||
byte[] szData = FileFunctions.readFullyIntoBuffer(in, 4); | ||
int size = FileFunctions.readFullInt(szData, 0); | ||
if (in.available() < size) { | ||
throw new IOException("Invalid size specified."); | ||
} | ||
|
||
// Read the block and translate it into a list of names. | ||
byte[] namesData = FileFunctions.readFullyIntoBuffer(in, size); | ||
List<String> names = new ArrayList<String>(); | ||
Scanner sc = new Scanner(new ByteArrayInputStream(namesData), "UTF-8"); | ||
while (sc.hasNextLine()) { | ||
String name = sc.nextLine().trim(); | ||
if (!name.isEmpty()) { | ||
names.add(name); | ||
} | ||
} | ||
sc.close(); | ||
|
||
return names; | ||
} | ||
|
||
public byte[] getBytes() throws IOException { | ||
ByteArrayOutputStream baos = new ByteArrayOutputStream(); | ||
|
||
baos.write(CUSTOM_NAMES_VERSION); | ||
|
||
writeNamesBlock(baos, trainerNames); | ||
writeNamesBlock(baos, trainerClasses); | ||
writeNamesBlock(baos, doublesTrainerNames); | ||
writeNamesBlock(baos, doublesTrainerClasses); | ||
writeNamesBlock(baos, pokemonNicknames); | ||
|
||
return baos.toByteArray(); | ||
} | ||
|
||
private void writeNamesBlock(OutputStream out, List<String> names) throws IOException { | ||
String newln = System.getProperty("line.separator"); | ||
StringBuffer outNames = new StringBuffer(); | ||
boolean first = true; | ||
for (String name : names) { | ||
if (!first) { | ||
outNames.append(newln); | ||
} | ||
first = false; | ||
outNames.append(name); | ||
} | ||
byte[] namesData = outNames.toString().getBytes("UTF-8"); | ||
byte[] szData = new byte[4]; | ||
FileFunctions.writeFullInt(szData, 0, namesData.length); | ||
out.write(szData); | ||
out.write(namesData); | ||
} | ||
|
||
public List<String> getTrainerNames() { | ||
return Collections.unmodifiableList(trainerNames); | ||
} | ||
|
||
public List<String> getTrainerClasses() { | ||
return Collections.unmodifiableList(trainerClasses); | ||
} | ||
|
||
public List<String> getDoublesTrainerNames() { | ||
return Collections.unmodifiableList(doublesTrainerNames); | ||
} | ||
|
||
public List<String> getDoublesTrainerClasses() { | ||
return Collections.unmodifiableList(doublesTrainerClasses); | ||
} | ||
|
||
public List<String> getPokemonNicknames() { | ||
return Collections.unmodifiableList(pokemonNicknames); | ||
} | ||
|
||
public static CustomNamesSet importOldNames() throws FileNotFoundException { | ||
CustomNamesSet cns = new CustomNamesSet(); | ||
|
||
// Trainer Names | ||
if (FileFunctions.configExists(SysConstants.tnamesFile)) { | ||
Scanner sc = new Scanner(FileFunctions.openConfig(SysConstants.tnamesFile), "UTF-8"); | ||
while (sc.hasNextLine()) { | ||
String trainername = sc.nextLine().trim(); | ||
if (trainername.isEmpty()) { | ||
continue; | ||
} | ||
if (trainername.startsWith("\uFEFF")) { | ||
trainername = trainername.substring(1); | ||
} | ||
if (trainername.contains("&")) { | ||
cns.doublesTrainerNames.add(trainername); | ||
} else { | ||
cns.trainerNames.add(trainername); | ||
} | ||
} | ||
sc.close(); | ||
} | ||
|
||
// Trainer Classes | ||
if (FileFunctions.configExists(SysConstants.tclassesFile)) { | ||
Scanner sc = new Scanner(FileFunctions.openConfig(SysConstants.tclassesFile), "UTF-8"); | ||
while (sc.hasNextLine()) { | ||
String trainerClassName = sc.nextLine().trim(); | ||
if (trainerClassName.isEmpty()) { | ||
continue; | ||
} | ||
if (trainerClassName.startsWith("\uFEFF")) { | ||
trainerClassName = trainerClassName.substring(1); | ||
} | ||
String checkName = trainerClassName.toLowerCase(); | ||
int idx = (checkName.endsWith("couple") || checkName.contains(" and ") || checkName.endsWith("kin") | ||
|| checkName.endsWith("team") || checkName.contains("&") || (checkName.endsWith("s") && !checkName | ||
.endsWith("ss"))) ? 1 : 0; | ||
if (idx == 1) { | ||
cns.doublesTrainerClasses.add(trainerClassName); | ||
} else { | ||
cns.trainerClasses.add(trainerClassName); | ||
} | ||
} | ||
sc.close(); | ||
} | ||
|
||
// Nicknames | ||
if (FileFunctions.configExists(SysConstants.nnamesFile)) { | ||
Scanner sc = new Scanner(FileFunctions.openConfig(SysConstants.nnamesFile), "UTF-8"); | ||
while (sc.hasNextLine()) { | ||
String nickname = sc.nextLine().trim(); | ||
if (nickname.isEmpty()) { | ||
continue; | ||
} | ||
if (nickname.startsWith("\uFEFF")) { | ||
nickname = nickname.substring(1); | ||
} | ||
cns.pokemonNicknames.add(nickname); | ||
} | ||
sc.close(); | ||
} | ||
|
||
return cns; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.