Clean up NUSTitle class, move functions to FSTUtils class

This commit is contained in:
Maschell 2019-04-26 09:55:52 +02:00
parent 5a57d42c52
commit 448eea1092
2 changed files with 48 additions and 114 deletions

View File

@ -16,25 +16,20 @@
****************************************************************************/ ****************************************************************************/
package de.mas.wiiu.jnus; package de.mas.wiiu.jnus;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.text.ParseException; import java.text.ParseException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Map.Entry;
import java.util.Optional; import java.util.Optional;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
import de.mas.wiiu.jnus.entities.TMD; import de.mas.wiiu.jnus.entities.TMD;
import de.mas.wiiu.jnus.entities.Ticket; import de.mas.wiiu.jnus.entities.Ticket;
import de.mas.wiiu.jnus.entities.content.Content; 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.FST;
import de.mas.wiiu.jnus.entities.fst.FSTEntry; import de.mas.wiiu.jnus.entities.fst.FSTEntry;
import de.mas.wiiu.jnus.interfaces.NUSDataProvider; import de.mas.wiiu.jnus.interfaces.NUSDataProvider;
import de.mas.wiiu.jnus.utils.FSTUtils;
import lombok.Getter; import lombok.Getter;
import lombok.NonNull; import lombok.NonNull;
import lombok.Setter; import lombok.Setter;
@ -52,116 +47,24 @@ public class NUSTitle {
byte[] tmdData = dataProvider.getRawTMD().orElseThrow(() -> new ParseException("No TMD data found", 0)); byte[] tmdData = dataProvider.getRawTMD().orElseThrow(() -> new ParseException("No TMD data found", 0));
this.TMD = de.mas.wiiu.jnus.entities.TMD.parseTMD(tmdData); this.TMD = de.mas.wiiu.jnus.entities.TMD.parseTMD(tmdData);
this.dataProvider = dataProvider; this.dataProvider = dataProvider;
}
public List<FSTEntry> getAllFSTEntriesFlatByContentID(short ID) {
return getFSTEntriesFlatByContent(getTMD().getContentByID((int) ID));
}
public List<FSTEntry> getFSTEntriesFlatByContentIndex(int index) {
return getFSTEntriesFlatByContent(getTMD().getContentByIndex(index));
}
public List<FSTEntry> getFSTEntriesFlatByContent(Content content) {
return getFSTEntriesFlatByContents(new ArrayList<Content>(Arrays.asList(content)));
}
public List<FSTEntry> getFSTEntriesFlatByContents(List<Content> list) {
return list.stream().flatMap(c -> c.getEntries().stream()).collect(Collectors.toList());
}
public List<FSTEntry> getAllFSTEntriesFlat() {
return getFSTEntriesFlatByContents(new ArrayList<Content>(getTMD().getAllContents().values()));
} }
public Stream<FSTEntry> getAllFSTEntriesAsStream() { public Stream<FSTEntry> getAllFSTEntriesAsStream() {
if (!FST.isPresent()) { if (!FST.isPresent()) {
return Stream.empty(); return Stream.empty();
} }
return getAllFSTEntryChildrenAsStream(FST.get().getRoot()); return FSTUtils.getAllFSTEntryChildrenAsStream(FST.get().getRoot());
}
public Stream<FSTEntry> getAllFSTEntryChildrenAsStream(FSTEntry cur) {
return getAllFSTEntryChildrenAsStream(cur, false);
}
public Stream<FSTEntry> 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);
});
} }
public List<FSTEntry> getFSTEntriesByRegEx(String regEx) { public List<FSTEntry> getFSTEntriesByRegEx(String regEx) {
if (!FST.isPresent()) { return getFSTEntriesByRegEx(regEx, true);
return new ArrayList<>();
}
return getFSTEntriesByRegEx(regEx, FST.get().getRoot());
}
public List<FSTEntry> getFSTEntriesByRegEx(String regEx, FSTEntry entry) {
return getFSTEntriesByRegEx(regEx, entry, true);
} }
public List<FSTEntry> getFSTEntriesByRegEx(String regEx, boolean onlyInPackage) { public List<FSTEntry> getFSTEntriesByRegEx(String regEx, boolean onlyInPackage) {
if (!FST.isPresent()) { if (!FST.isPresent()) {
return new ArrayList<>(); return new ArrayList<>();
} }
return getFSTEntriesByRegEx(regEx, FST.get().getRoot(), onlyInPackage); return FSTUtils.getFSTEntriesByRegEx(FST.get().getRoot(), regEx, onlyInPackage);
}
public List<FSTEntry> getFSTEntriesByRegEx(String regEx, FSTEntry entry, boolean allowNotInPackage) {
Pattern p = Pattern.compile(regEx);
return getFSTEntriesByRegExStream(p, entry, allowNotInPackage).collect(Collectors.toList());
}
private Stream<FSTEntry> 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<Integer, ContentFSTInfo> e : FST.get().getContentFSTInfos().entrySet()) {
System.out.println(String.format("%08X", e.getKey()) + ": " + e.getValue());
}
}
}
public void printContentInfos() {
for (Entry<Integer, Content> 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("-");
}
} }
public void cleanup() throws IOException { public void cleanup() throws IOException {
@ -170,14 +73,6 @@ public class NUSTitle {
} }
} }
public void printDetailedData() {
printFiles();
printContentFSTInfos();
printContentInfos();
System.out.println();
}
@Override @Override
public String toString() { public String toString() {
return "NUSTitle [dataProvider=" + dataProvider + "]"; return "NUSTitle [dataProvider=" + dataProvider + "]";

View File

@ -25,6 +25,8 @@ import java.util.stream.Stream;
import org.apache.commons.io.FilenameUtils; 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 de.mas.wiiu.jnus.entities.fst.FSTEntry;
import lombok.val; import lombok.val;
@ -95,15 +97,15 @@ public class FSTUtils {
} }
public static List<FSTEntry> getFSTEntriesByRegEx(FSTEntry root, String string) { public static List<FSTEntry> getFSTEntriesByRegEx(FSTEntry root, String string) {
return getFSTEntriesByRegEx(string, root, false); return getFSTEntriesByRegEx(root, string, false);
} }
public static List<FSTEntry> getFSTEntriesByRegEx(String regEx, FSTEntry entry, boolean allowNotInPackage) { public static List<FSTEntry> getFSTEntriesByRegEx(FSTEntry entry, String regEx, boolean allowNotInPackage) {
Pattern p = Pattern.compile(regEx); Pattern p = Pattern.compile(regEx);
return getFSTEntriesByRegExStream(p, entry, allowNotInPackage).collect(Collectors.toList()); return getFSTEntriesByRegExStream(entry, p, allowNotInPackage).collect(Collectors.toList());
} }
private static Stream<FSTEntry> getFSTEntriesByRegExStream(Pattern p, FSTEntry entry, boolean allowNotInPackage) { private static Stream<FSTEntry> getFSTEntriesByRegExStream(FSTEntry entry, Pattern p, boolean allowNotInPackage) {
return entry.getChildren().stream()// return entry.getChildren().stream()//
.filter(e -> allowNotInPackage || !e.isNotInPackage()) // .filter(e -> allowNotInPackage || !e.isNotInPackage()) //
.flatMap(e -> { .flatMap(e -> {
@ -114,7 +116,44 @@ public class FSTUtils {
return Stream.empty(); return Stream.empty();
} }
} }
return getFSTEntriesByRegExStream(p, e, allowNotInPackage); return getFSTEntriesByRegExStream(e, p, allowNotInPackage);
});
}
public static Optional<ContentFSTInfo> getFSTInfoForContent(FST fst, short contentIndex) {
return fst.getContentFSTInfos().entrySet().stream().filter(e -> e.getKey().shortValue() == contentIndex).map(e -> e.getValue()).findAny();
}
public static List<FSTEntry> getFSTEntriesByContentIndex(FSTEntry entry, short index) {
return getFSTEntriesByContentIndexAsStream(entry, index).collect(Collectors.toList());
}
public static Stream<FSTEntry> 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<FSTEntry> getAllFSTEntryChildrenAsStream(FSTEntry root) {
return getAllFSTEntryChildrenAsStream(root, false);
}
public static Stream<FSTEntry> 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);
}); });
} }
} }