This repository has been archived by the owner on Aug 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Overhaul to the trainer and party loading systems - Replace spaghetti with interfaces - Creation of Party class to better manage party saving
- Loading branch information
1 parent
9dcdfe5
commit 177287d
Showing
29 changed files
with
767 additions
and
615 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package me.disturbo.data; | ||
|
||
import me.disturbo.data.composers.PartiesComposer; | ||
import me.disturbo.data.composers.TrainersComposer; | ||
import me.disturbo.data.indexers.PartyIndexer; | ||
import me.disturbo.data.indexers.TrainerIndexer; | ||
import me.disturbo.data.parsers.*; | ||
import me.disturbo.main.MainActivity; | ||
import me.disturbo.types.Party; | ||
import me.disturbo.types.Trainer; | ||
import me.disturbo.types.TrainerClass; | ||
|
||
import java.io.*; | ||
import java.util.*; | ||
|
||
public class DataManager { | ||
/* | ||
COMMENTS PENDING | ||
*/ | ||
|
||
public static final LinkedHashMap<String, TrainerClass> loadTrainerClasses(){ | ||
File trainerClasses = new File(MainActivity.projectDirectory + File.separator + "src" | ||
+ File.separator + "data" + File.separator + "text" + File.separator + "trainer_class_names.h"); | ||
|
||
return (new TrainerClassesParser()).parse(trainerClasses, new LinkedHashMap<>()); | ||
} | ||
|
||
public static final LinkedList<String> loadItems(){ | ||
File items = new File(MainActivity.projectDirectory | ||
+ File.separator + "include" + File.separator + "constants" + File.separator + "items.h"); | ||
|
||
return (new ItemsParser()).parse(items, new LinkedList<>()); | ||
} | ||
|
||
public static final LinkedHashMap<String, String> loadMoves(){ | ||
File moves = new File(MainActivity.projectDirectory + File.separator + "src" | ||
+ File.separator + "data" + File.separator + "text" + File.separator + "move_names.h"); | ||
|
||
return (new MovesParser()).parse(moves, new LinkedHashMap<>()); | ||
} | ||
|
||
public static final LinkedHashMap<String, String> loadSpecies(){ | ||
File species = new File(MainActivity.projectDirectory + File.separator + "src" | ||
+ File.separator + "data" + File.separator + "text" + File.separator + "species_names.h"); | ||
|
||
return (new SpeciesParser()).parse(species, new LinkedHashMap<>()); | ||
} | ||
|
||
public static final LinkedList<String> loadMusic(){ | ||
File music = new File(MainActivity.projectDirectory + File.separator + "include" | ||
+ File.separator + "constants" + File.separator + "trainers.h"); | ||
|
||
return (new MusicParser()).parse(music, new LinkedList<>()); | ||
} | ||
|
||
public static final LinkedList<String> loadAiFlags(){ | ||
File aiFlags = new File(MainActivity.projectDirectory + File.separator + "include" | ||
+ File.separator + "constants" + File.separator + "battle_ai.h"); | ||
|
||
return (new AiFlagsParser()).parse(aiFlags, new LinkedList<>()); | ||
} | ||
|
||
public static final LinkedList<String> loadTrainerPicsList(){ | ||
File picList = new File(MainActivity.projectDirectory + File.separator + "include" | ||
+ File.separator + "constants" + File.separator + "trainers.h"); | ||
|
||
return (new TrainerPicsListParser()).parse(picList, new LinkedList<>()); | ||
} | ||
|
||
public static final LinkedHashMap<String, String> loadTrainerPicsPaths(){ | ||
File picsPaths = new File(MainActivity.projectDirectory + File.separator + "src" | ||
+ File.separator + "data" + File.separator + "graphics" + File.separator + "trainers.h"); | ||
LinkedHashMap<String, String> picTable = loadTrainerPicTable(); | ||
|
||
return (new TrainerPicsPathsParser(picTable)).parse(picsPaths, new LinkedHashMap<>()); | ||
} | ||
|
||
public static final LinkedHashMap<String, String> loadTrainerPicTable(){ | ||
File picTable = new File(MainActivity.projectDirectory + File.separator + "src" | ||
+ File.separator + "data" + File.separator + "trainer_graphics" + File.separator + "front_pic_tables.h"); | ||
|
||
return (new TrainerPicTableParser()).parse(picTable, new LinkedHashMap<>()); | ||
} | ||
|
||
public static final LinkedHashMap<String, Integer> indexTrainers(){ | ||
File trainers = new File(MainActivity.projectDirectory | ||
+ File.separator + "src" + File.separator + "data" + File.separator + "trainers.h"); | ||
|
||
return (new TrainerIndexer()).parse(trainers, new LinkedHashMap<>()); | ||
} | ||
|
||
public static final Trainer loadTrainer(String name){ | ||
File trainers = new File(MainActivity.projectDirectory | ||
+ File.separator + "src" + File.separator + "data" + File.separator + "trainers.h"); | ||
|
||
return (new TrainerParser()).parser(trainers, MainActivity.trainerIndexes, name); | ||
} | ||
|
||
public static final void saveTrainers(LinkedList<Trainer> orderedTrainers){ | ||
File trainers = new File(MainActivity.projectDirectory | ||
+ File.separator + "src" + File.separator + "data" + File.separator + "trainers.h"); | ||
|
||
(new TrainersComposer()).compose(trainers, orderedTrainers, MainActivity.trainerIndexes); | ||
} | ||
|
||
public static final LinkedHashMap<String, Integer> indexParties(){ | ||
File parties = new File(MainActivity.projectDirectory | ||
+ File.separator + "src" + File.separator + "data" + File.separator + "trainer_parties.h"); | ||
|
||
return (new PartyIndexer()).parse(parties, new LinkedHashMap<>()); | ||
} | ||
|
||
public static final Party loadParty(String flags, String size, String name){ | ||
File parties = new File(MainActivity.projectDirectory | ||
+ File.separator + "src" + File.separator + "data" + File.separator + "trainer_parties.h"); | ||
|
||
return (new PartyParser(flags, size)).parser(parties, MainActivity.partyIndexes, name); | ||
} | ||
|
||
public static final void saveParties(LinkedList<Party> orderedParties){ | ||
File parties = new File(MainActivity.projectDirectory | ||
+ File.separator + "src" + File.separator + "data" + File.separator + "trainer_parties.h"); | ||
|
||
(new PartiesComposer()).compose(parties, orderedParties, MainActivity.partyIndexes); | ||
} | ||
} |
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,47 @@ | ||
package me.disturbo.data; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.File; | ||
import java.io.FileReader; | ||
import java.io.IOException; | ||
import java.util.LinkedHashMap; | ||
import java.util.LinkedList; | ||
|
||
public interface IndexedLineParser<T> { | ||
default T parser(File file, LinkedHashMap<String, Integer> indexes, String key){ | ||
LinkedList<String> keys = new LinkedList<>(indexes.keySet()); | ||
int index = indexes.get(key); | ||
int nextIndex = keys.indexOf(key) + 1 < keys.size() ? indexes.get(keys.get(keys.indexOf(key) + 1)) : -1; | ||
|
||
String raw = ""; | ||
int currentIndex = 0; | ||
|
||
FileReader fr = null; BufferedReader br = null; | ||
try { | ||
fr = new FileReader(file); br = new BufferedReader(fr); | ||
String line; | ||
while((line = br.readLine()) != null && (currentIndex < nextIndex || nextIndex == -1)) { | ||
line += System.lineSeparator(); | ||
if(currentIndex >= index) raw += line; | ||
currentIndex += line.length(); | ||
|
||
} | ||
fr.close(); | ||
} | ||
catch(IOException exception) { | ||
exception.printStackTrace(); | ||
} | ||
finally { | ||
try { | ||
fr.close(); br.close(); | ||
} | ||
catch (IOException exception) { | ||
exception.printStackTrace(); | ||
} | ||
} | ||
|
||
return parseObject(key, raw); | ||
} | ||
|
||
T parseObject(String key, String raw); | ||
} |
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,85 @@ | ||
package me.disturbo.data; | ||
|
||
import java.io.*; | ||
import java.util.LinkedHashMap; | ||
import java.util.LinkedList; | ||
|
||
public interface IndexedOrderedComposer<T> { | ||
default void compose(File file, LinkedList<T> ordered, LinkedHashMap<String, Integer> indexes){ | ||
LinkedList<String> keys = new LinkedList<>(indexes.keySet()); | ||
int orderedIndex = 0; | ||
String name = getName(ordered.get(orderedIndex)); | ||
int index = indexes.get(name), | ||
nextIndex = keys.indexOf(name) + 1 < keys.size() ? indexes.get(keys.get(keys.indexOf(name) + 1)) : -1, | ||
currentIndex = 0; | ||
|
||
String composed = ""; | ||
|
||
FileReader fr = null; BufferedReader br = null; | ||
try { | ||
fr = new FileReader(file); br = new BufferedReader(fr); | ||
String line; | ||
while((line = br.readLine()) != null) { | ||
line += System.lineSeparator(); | ||
boolean appended = false; | ||
while(!appended){ | ||
// If the current target has been passed and there is a another one | ||
if(currentIndex >= nextIndex && nextIndex > -1 && index != - 1){ | ||
orderedIndex++; | ||
if(orderedIndex < ordered.size()) { | ||
name = getName(ordered.get(orderedIndex)); | ||
index = indexes.get(name); | ||
nextIndex = keys.indexOf(name) + 1 < keys.size() ? indexes.get(keys.get(keys.indexOf(name) + 1)) : -1; | ||
} | ||
// No more left to save | ||
else index = -1; | ||
} | ||
else{ | ||
if(currentIndex < index || index == - 1) composed += line; | ||
// If the target starts here | ||
else if(currentIndex == index) composed += buildStruct(ordered.get(orderedIndex), nextIndex); | ||
appended = true; | ||
} | ||
} | ||
currentIndex += line.length(); | ||
} | ||
fr.close(); | ||
} | ||
catch(IOException exception) { | ||
exception.printStackTrace(); | ||
} | ||
finally { | ||
try { | ||
fr.close(); br.close(); | ||
} | ||
catch (IOException exception) { | ||
exception.printStackTrace(); | ||
} | ||
} | ||
|
||
FileWriter wr = null; | ||
try { | ||
wr = new FileWriter(file); | ||
wr.write(composed); | ||
} | ||
catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
finally { | ||
try { | ||
wr.close(); | ||
} | ||
catch (IOException exception) { | ||
exception.printStackTrace(); | ||
} | ||
} | ||
|
||
finalize(ordered); | ||
} | ||
|
||
String getName(T type); | ||
|
||
String buildStruct(T type, int nextIndex); | ||
|
||
void finalize(LinkedList<T> ordered); | ||
} |
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,36 @@ | ||
package me.disturbo.data; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.File; | ||
import java.io.FileReader; | ||
import java.io.IOException; | ||
|
||
public interface LineParser<T>{ | ||
default T parse(File file, T output){ | ||
FileReader fr = null; BufferedReader br = null; | ||
try { | ||
fr = new FileReader(file); br = new BufferedReader(fr); | ||
String line; | ||
while((line = br.readLine()) != null) { | ||
if(parseLine(output, line)) break; | ||
} | ||
fr.close(); | ||
} | ||
catch(IOException exception) { | ||
exception.printStackTrace(); | ||
} | ||
finally { | ||
try { | ||
fr.close(); br.close(); | ||
} | ||
catch (IOException exception) { | ||
exception.printStackTrace(); | ||
} | ||
|
||
} | ||
return output; | ||
} | ||
|
||
// If the result of this function is true no more lines will be read | ||
boolean parseLine(T output, String line); | ||
} |
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,26 @@ | ||
package me.disturbo.data.composers; | ||
|
||
import me.disturbo.data.DataManager; | ||
import me.disturbo.data.IndexedOrderedComposer; | ||
import me.disturbo.main.MainActivity; | ||
import me.disturbo.types.Party; | ||
import me.disturbo.types.Trainer; | ||
|
||
import java.util.LinkedList; | ||
|
||
public class PartiesComposer implements IndexedOrderedComposer<Party> { | ||
@Override | ||
public String getName(Party party) { | ||
return party.name; | ||
} | ||
|
||
@Override | ||
public String buildStruct(Party party, int nextTrainerIndex) { | ||
return party.buildPartyStruct(); | ||
} | ||
|
||
@Override | ||
public void finalize(LinkedList<Party> trainers) { | ||
DataManager.indexParties(); | ||
} | ||
} |
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,47 @@ | ||
package me.disturbo.data.composers; | ||
|
||
import me.disturbo.data.DataManager; | ||
import me.disturbo.data.IndexedOrderedComposer; | ||
import me.disturbo.main.MainActivity; | ||
import me.disturbo.types.Party; | ||
import me.disturbo.types.Trainer; | ||
|
||
import java.util.LinkedList; | ||
|
||
public class TrainersComposer implements IndexedOrderedComposer<Trainer> { | ||
@Override | ||
public String getName(Trainer trainer) { | ||
return trainer.name; | ||
} | ||
|
||
@Override | ||
public String buildStruct(Trainer trainer, int nextTrainerIndex) { | ||
String trainerStruct = trainer.buildTrainerStruct(); | ||
if(nextTrainerIndex == -1) trainerStruct += "};"; | ||
return trainerStruct; | ||
} | ||
|
||
@Override | ||
public void finalize(LinkedList<Trainer> trainers) { | ||
DataManager.indexTrainers(); | ||
LinkedList<Party> saveQueue = new LinkedList<>(); | ||
for(Trainer trainer : trainers){ | ||
if(saveQueue.isEmpty()) saveQueue.add(trainer.party); | ||
else { | ||
boolean inserted = false; | ||
for(int insertionIndex = 0; insertionIndex < saveQueue.size(); insertionIndex++){ | ||
|
||
int savingIndex = MainActivity.partyIndexes.get(trainer.party.name); | ||
int savedIndex = MainActivity.partyIndexes.get(saveQueue.get(insertionIndex).name); | ||
if(savingIndex < savedIndex){ | ||
saveQueue.add(insertionIndex, trainer.party); | ||
inserted = true; | ||
break; | ||
} | ||
} | ||
if(!inserted) saveQueue.add(trainer.party); | ||
} | ||
} | ||
DataManager.saveParties(saveQueue); | ||
} | ||
} |
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,19 @@ | ||
package me.disturbo.data.indexers; | ||
|
||
import me.disturbo.data.LineParser; | ||
|
||
import java.util.LinkedHashMap; | ||
|
||
public class PartyIndexer implements LineParser<LinkedHashMap<String, Integer>> { | ||
private int currentIndex = 0; | ||
|
||
@Override | ||
public boolean parseLine(LinkedHashMap<String, Integer> partyIndexes, String line) { | ||
if(line.contains("static")){ | ||
String partyName = line.substring(line.indexOf("sParty"), line.indexOf("[")).replace(" ", ""); | ||
partyIndexes.put(partyName, currentIndex); | ||
} | ||
currentIndex += (line + System.lineSeparator()).length(); | ||
return false; | ||
} | ||
} |
Oops, something went wrong.