diff --git a/src/de/mas/wiiu/jnus/NUSTitle.java b/src/de/mas/wiiu/jnus/NUSTitle.java index 04b841e..75104df 100644 --- a/src/de/mas/wiiu/jnus/NUSTitle.java +++ b/src/de/mas/wiiu/jnus/NUSTitle.java @@ -16,25 +16,20 @@ ****************************************************************************/ package de.mas.wiiu.jnus; -import java.io.File; import java.io.IOException; import java.text.ParseException; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; -import java.util.Map.Entry; import java.util.Optional; -import java.util.regex.Pattern; -import java.util.stream.Collectors; import java.util.stream.Stream; import de.mas.wiiu.jnus.entities.TMD; import de.mas.wiiu.jnus.entities.Ticket; import de.mas.wiiu.jnus.entities.content.Content; -import de.mas.wiiu.jnus.entities.content.ContentFSTInfo; import de.mas.wiiu.jnus.entities.fst.FST; import de.mas.wiiu.jnus.entities.fst.FSTEntry; import de.mas.wiiu.jnus.interfaces.NUSDataProvider; +import de.mas.wiiu.jnus.utils.FSTUtils; import lombok.Getter; import lombok.NonNull; import lombok.Setter; @@ -52,116 +47,24 @@ public class NUSTitle { byte[] tmdData = dataProvider.getRawTMD().orElseThrow(() -> new ParseException("No TMD data found", 0)); this.TMD = de.mas.wiiu.jnus.entities.TMD.parseTMD(tmdData); this.dataProvider = dataProvider; - - } - - public List getAllFSTEntriesFlatByContentID(short ID) { - return getFSTEntriesFlatByContent(getTMD().getContentByID((int) ID)); - } - - public List getFSTEntriesFlatByContentIndex(int index) { - return getFSTEntriesFlatByContent(getTMD().getContentByIndex(index)); - } - - public List getFSTEntriesFlatByContent(Content content) { - return getFSTEntriesFlatByContents(new ArrayList(Arrays.asList(content))); - } - - public List getFSTEntriesFlatByContents(List list) { - return list.stream().flatMap(c -> c.getEntries().stream()).collect(Collectors.toList()); - } - - public List getAllFSTEntriesFlat() { - return getFSTEntriesFlatByContents(new ArrayList(getTMD().getAllContents().values())); } public Stream getAllFSTEntriesAsStream() { if (!FST.isPresent()) { return Stream.empty(); } - return getAllFSTEntryChildrenAsStream(FST.get().getRoot()); - } - - public Stream getAllFSTEntryChildrenAsStream(FSTEntry cur) { - return getAllFSTEntryChildrenAsStream(cur, false); - } - - public Stream getAllFSTEntryChildrenAsStream(FSTEntry cur, boolean allowNotInPackage) { - return cur.getChildren().stream() // - .filter(e -> allowNotInPackage || !e.isNotInPackage()) // - .flatMap(e -> { - if (!e.isDir()) { - return Stream.of(e); - } - return getAllFSTEntryChildrenAsStream(e, allowNotInPackage); - }); + return FSTUtils.getAllFSTEntryChildrenAsStream(FST.get().getRoot()); } public List getFSTEntriesByRegEx(String regEx) { - if (!FST.isPresent()) { - return new ArrayList<>(); - } - - return getFSTEntriesByRegEx(regEx, FST.get().getRoot()); - } - - public List getFSTEntriesByRegEx(String regEx, FSTEntry entry) { - return getFSTEntriesByRegEx(regEx, entry, true); + return getFSTEntriesByRegEx(regEx, true); } public List getFSTEntriesByRegEx(String regEx, boolean onlyInPackage) { if (!FST.isPresent()) { return new ArrayList<>(); } - return getFSTEntriesByRegEx(regEx, FST.get().getRoot(), onlyInPackage); - } - - public List getFSTEntriesByRegEx(String regEx, FSTEntry entry, boolean allowNotInPackage) { - Pattern p = Pattern.compile(regEx); - return getFSTEntriesByRegExStream(p, entry, allowNotInPackage).collect(Collectors.toList()); - } - - private Stream getFSTEntriesByRegExStream(Pattern p, FSTEntry entry, boolean allowNotInPackage) { - return entry.getChildren().stream()// - .filter(e -> allowNotInPackage || !e.isNotInPackage()) // - .flatMap(e -> { - if (!e.isDir()) { - if (p.matcher(e.getFullPath()).matches()) { - return Stream.of(e); - } else { - return Stream.empty(); - } - } - return getFSTEntriesByRegExStream(p, e, allowNotInPackage); - }); - } - - public void printFiles() { - if (FST.isPresent()) { - FST.get().getRoot().printRecursive(0); - } - } - - public void printContentFSTInfos() { - if (FST.isPresent()) { - for (Entry e : FST.get().getContentFSTInfos().entrySet()) { - System.out.println(String.format("%08X", e.getKey()) + ": " + e.getValue()); - } - } - - } - - public void printContentInfos() { - for (Entry e : getTMD().getAllContents().entrySet()) { - - System.out.println(String.format("%08X", e.getKey()) + ": " + e.getValue()); - System.out.println(e.getValue().getContentFSTInfo()); - for (FSTEntry entry : e.getValue().getEntries()) { - System.out.println(entry.getFullPath() + String.format(" size: %016X", entry.getFileSize()) - + String.format(" offset: %016X", entry.getFileOffset()) + String.format(" flags: %04X", entry.getFlags())); - } - System.out.println("-"); - } + return FSTUtils.getFSTEntriesByRegEx(FST.get().getRoot(), regEx, onlyInPackage); } public void cleanup() throws IOException { @@ -170,14 +73,6 @@ public class NUSTitle { } } - public void printDetailedData() { - printFiles(); - printContentFSTInfos(); - printContentInfos(); - - System.out.println(); - } - @Override public String toString() { return "NUSTitle [dataProvider=" + dataProvider + "]"; diff --git a/src/de/mas/wiiu/jnus/utils/FSTUtils.java b/src/de/mas/wiiu/jnus/utils/FSTUtils.java index cab7582..9134203 100644 --- a/src/de/mas/wiiu/jnus/utils/FSTUtils.java +++ b/src/de/mas/wiiu/jnus/utils/FSTUtils.java @@ -25,6 +25,8 @@ import java.util.stream.Stream; import org.apache.commons.io.FilenameUtils; +import de.mas.wiiu.jnus.entities.content.ContentFSTInfo; +import de.mas.wiiu.jnus.entities.fst.FST; import de.mas.wiiu.jnus.entities.fst.FSTEntry; import lombok.val; @@ -95,15 +97,15 @@ public class FSTUtils { } public static List getFSTEntriesByRegEx(FSTEntry root, String string) { - return getFSTEntriesByRegEx(string, root, false); + return getFSTEntriesByRegEx(root, string, false); } - public static List getFSTEntriesByRegEx(String regEx, FSTEntry entry, boolean allowNotInPackage) { + public static List getFSTEntriesByRegEx(FSTEntry entry, String regEx, boolean allowNotInPackage) { Pattern p = Pattern.compile(regEx); - return getFSTEntriesByRegExStream(p, entry, allowNotInPackage).collect(Collectors.toList()); + return getFSTEntriesByRegExStream(entry, p, allowNotInPackage).collect(Collectors.toList()); } - private static Stream getFSTEntriesByRegExStream(Pattern p, FSTEntry entry, boolean allowNotInPackage) { + private static Stream getFSTEntriesByRegExStream(FSTEntry entry, Pattern p, boolean allowNotInPackage) { return entry.getChildren().stream()// .filter(e -> allowNotInPackage || !e.isNotInPackage()) // .flatMap(e -> { @@ -114,7 +116,44 @@ public class FSTUtils { return Stream.empty(); } } - return getFSTEntriesByRegExStream(p, e, allowNotInPackage); + return getFSTEntriesByRegExStream(e, p, allowNotInPackage); + }); + } + + public static Optional getFSTInfoForContent(FST fst, short contentIndex) { + return fst.getContentFSTInfos().entrySet().stream().filter(e -> e.getKey().shortValue() == contentIndex).map(e -> e.getValue()).findAny(); + } + + public static List getFSTEntriesByContentIndex(FSTEntry entry, short index) { + return getFSTEntriesByContentIndexAsStream(entry, index).collect(Collectors.toList()); + } + + public static Stream getFSTEntriesByContentIndexAsStream(FSTEntry entry, short index) { + return entry.getChildren().stream()// + .filter(e -> e.getContentIndex() == index) // + .flatMap(e -> { + if (!e.isDir()) { + return Stream.of(e); + } + return getFSTEntriesByContentIndexAsStream(e, index); + }); + } + + /** + * Does not include entries that are not in the package.. + */ + public static Stream getAllFSTEntryChildrenAsStream(FSTEntry root) { + return getAllFSTEntryChildrenAsStream(root, false); + } + + public static Stream getAllFSTEntryChildrenAsStream(FSTEntry root, boolean allowNotInPackage) { + return root.getChildren().stream() // + .filter(e -> allowNotInPackage || !e.isNotInPackage()) // + .flatMap(e -> { + if (!e.isDir()) { + return Stream.of(e); + } + return getAllFSTEntryChildrenAsStream(e, allowNotInPackage); }); } }