Add index to merged file-access, change resource-loading log to debug instead of warnings and make it fail faster on model-errors

This commit is contained in:
Blue (Lukas Rieger) 2020-08-29 02:18:35 +02:00
parent 96959550c6
commit 0aa69c7591
9 changed files with 83 additions and 34 deletions

View File

@ -43,7 +43,6 @@
import com.flowpowered.math.vector.Vector3f;
import com.flowpowered.math.vector.Vector4f;
import de.bluecolored.bluemap.core.logger.Logger;
import de.bluecolored.bluemap.core.resourcepack.BlockModelResource.Element.Face;
import de.bluecolored.bluemap.core.resourcepack.fileaccess.FileAccess;
import de.bluecolored.bluemap.core.util.Axis;
@ -285,18 +284,14 @@ private BlockModelResource buildNoReset(String modelPath, boolean renderElements
parentPath = ResourcePack.namespacedToAbsoluteResourcePath(parentPath, "models") + ".json";
blockModel = this.buildNoReset(parentPath, config.getNode("elements").isVirtual(), topModelPath);
} catch (IOException ex) {
Logger.global.logWarning("Failed to load parent model " + parentPath + " of model " + topModelPath + ": " + ex);
throw new ParseResourceException("Failed to load parent model " + parentPath + " of model " + topModelPath, ex);
}
}
}
if (renderElements) {
for (ConfigurationNode elementNode : config.getNode("elements").getChildrenList()) {
try {
blockModel.elements.add(buildElement(blockModel, elementNode, topModelPath));
} catch (ParseResourceException ex) {
Logger.global.logWarning("Failed to parse element of model " + modelPath + " (" + topModelPath + "): " + ex);
}
blockModel.elements.add(buildElement(blockModel, elementNode, topModelPath));
}
}
@ -304,7 +299,7 @@ private BlockModelResource buildNoReset(String modelPath, boolean renderElements
try {
blockModel.textures.put(key, getTexture("#" + key));
} catch (NoSuchElementException | FileNotFoundException ex) {
Logger.global.logDebug("Failed to map Texture key '" + key + "': " + ex);
throw new ParseResourceException("Failed to map Texture key '" + key + "' for model '" + topModelPath + "': " + ex);
}
}
@ -365,7 +360,7 @@ private Element buildElement(BlockModelResource model, ConfigurationNode node, S
Face face = buildFace(element, direction, faceNode);
element.faces.put(direction, face);
} catch (ParseResourceException | IOException ex) {
Logger.global.logDebug("Failed to parse an " + direction + " face for the model " + topModelPath + "! " + ex);
throw new ParseResourceException("Failed to parse an " + direction + " face for the model " + topModelPath + "!", ex);
}
} else {
allDirs = false;

View File

@ -193,7 +193,7 @@ public BlockStateResource build(String blockstateFile) throws IOException {
blockState.variants.add(variant);
} catch (ParseResourceException | RuntimeException e) {
Logger.global.logWarning("Failed to parse a variant of " + blockstateFile + ": " + e);
Logger.global.logDebug("Failed to parse a variant of " + blockstateFile + ": " + e);
}
}
@ -212,7 +212,7 @@ public BlockStateResource build(String blockstateFile) throws IOException {
blockState.multipart.add(variant);
} catch (ParseResourceException | RuntimeException e) {
Logger.global.logWarning("Failed to parse a multipart-part of " + blockstateFile + ": " + e);
Logger.global.logDebug("Failed to parse a multipart-part of " + blockstateFile + ": " + e);
}
}
@ -227,14 +227,14 @@ private Collection<Weighted<TransformedBlockModelResource>> loadModels(Configura
try {
models.add(loadModel(modelNode, overrideTextures));
} catch (ParseResourceException ex) {
Logger.global.logWarning("Failed to load a model trying to parse " + blockstateFile + ": " + ex);
Logger.global.logDebug("Failed to load a model trying to parse " + blockstateFile + ": " + ex);
}
}
} else if (node.hasMapChildren()) {
try {
models.add(loadModel(node, overrideTextures));
} catch (ParseResourceException ex) {
Logger.global.logWarning("Failed to load a model trying to parse " + blockstateFile + ": " + ex);
Logger.global.logDebug("Failed to load a model trying to parse " + blockstateFile + ": " + ex);
}
}
@ -387,7 +387,7 @@ private BlockStateResource buildForge(ConfigurationNode config, String blockstat
variant.checkValid();
blockState.variants.add(variant);
} catch (ParseResourceException ex) {
Logger.global.logWarning("Failed to parse a variant (forge/property) of " + blockstateFile + ": " + ex);
Logger.global.logDebug("Failed to parse a variant (forge/property) of " + blockstateFile + ": " + ex);
}
}
@ -420,7 +420,7 @@ private BlockStateResource buildForge(ConfigurationNode config, String blockstat
variant.checkValid();
blockState.variants.add(variant);
} catch (ParseResourceException ex) {
Logger.global.logWarning("Failed to parse a variant (forge/straight) of " + blockstateFile + ": " + ex);
Logger.global.logDebug("Failed to parse a variant (forge/straight) of " + blockstateFile + ": " + ex);
}
}

View File

@ -134,7 +134,8 @@ public void load(Collection<File> sources) throws IOException {
*/
public void load(File... sources) {
try (CombinedFileAccess combinedSources = new CombinedFileAccess()){
for (File file : sources) {
for (int i = 0; i < sources.length; i++) {
File file = sources[i];
try {
combinedSources.addFileAccess(FileAccess.of(file));
} catch (IOException e) {
@ -149,11 +150,21 @@ public void load(File... sources) {
Builder builder = BlockStateResource.builder(sourcesAccess, this);
Collection<String> namespaces = sourcesAccess.listFolders("assets");
int i = 0;
for (String namespaceRoot : namespaces) {
i++;
//load blockstates
String namespace = namespaceRoot.substring("assets/".length());
Logger.global.logInfo("Loading " + namespace + " assets (" + i + "/" + namespaces.size() + ")...");
Collection<String> blockstateFiles = sourcesAccess.listFiles(namespaceRoot + "/blockstates", true);
for (String blockstateFile : blockstateFiles) {
if (Thread.interrupted()) {
Thread.currentThread().interrupt();
return;
}
String filename = FileAccess.getFileName(blockstateFile);
if (!filename.endsWith(".json")) continue;

View File

@ -42,6 +42,11 @@ public BluemapAssetOverrideFileAccess(FileAccess parent) {
this.parent = parent;
}
@Override
public String getName() {
return parent.getName() + "*";
}
@Override
public InputStream readFile(String path) throws FileNotFoundException, IOException {
String[] pathParts = StringUtils.split(path, "/");

View File

@ -40,6 +40,11 @@ public CaseInsensitiveFileAccess(FileAccess parent) {
this.parent = parent;
}
@Override
public String getName() {
return parent.getName() + "(CI)";
}
@Override
public InputStream readFile(String path) throws FileNotFoundException, IOException {
try {

View File

@ -29,28 +29,43 @@
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class CombinedFileAccess implements FileAccess {
public List<FileAccess> sources;
private Map<String, FileAccess> sourceMap;
public CombinedFileAccess() {
sources = new ArrayList<>();
sourceMap = new HashMap<>();
}
public void addFileAccess(FileAccess source) {
sources.add(source);
synchronized (sources) {
sources.add(source);
}
synchronized (sourceMap) {
for (String path : source.listFiles("", true)) {
sourceMap.put(FileAccess.normalize(path), source);
}
}
}
@Override
public String getName() {
return "CombinedFileAccess(" + sources.size() + ")";
}
@Override
public InputStream readFile(String path) throws FileNotFoundException, IOException {
for (int i = sources.size() - 1; i >= 0; i--) { //reverse order because later sources override earlier ones
try {
return sources.get(i).readFile(path);
} catch (FileNotFoundException ex) {}
synchronized (sourceMap) {
FileAccess source = sourceMap.get(FileAccess.normalize(path));
if (source != null) return source.readFile(path);
}
throw new FileNotFoundException("File " + path + " does not exist in any of the sources!");
@ -59,9 +74,11 @@ public InputStream readFile(String path) throws FileNotFoundException, IOExcepti
@Override
public Collection<String> listFiles(String path, boolean recursive) {
Set<String> files = new HashSet<>();
for (int i = 0; i < sources.size(); i++) {
files.addAll(sources.get(i).listFiles(path, recursive));
synchronized (sources) {
for (int i = 0; i < sources.size(); i++) {
files.addAll(sources.get(i).listFiles(path, recursive));
}
}
return files;
@ -70,9 +87,11 @@ public Collection<String> listFiles(String path, boolean recursive) {
@Override
public Collection<String> listFolders(String path) {
Set<String> folders = new HashSet<>();
for (int i = 0; i < sources.size(); i++) {
folders.addAll(sources.get(i).listFolders(path));
synchronized (sources) {
for (int i = 0; i < sources.size(); i++) {
folders.addAll(sources.get(i).listFolders(path));
}
}
return folders;
@ -81,13 +100,15 @@ public Collection<String> listFolders(String path) {
@Override
public void close() throws IOException {
IOException exception = null;
for (FileAccess source : sources) {
try {
source.close();
} catch (IOException ex) {
if (exception == null) exception = ex;
else exception.addSuppressed(ex);
synchronized (sources) {
for (FileAccess source : sources) {
try {
source.close();
} catch (IOException ex) {
if (exception == null) exception = ex;
else exception.addSuppressed(ex);
}
}
}

View File

@ -33,6 +33,8 @@
public interface FileAccess extends Closeable, AutoCloseable {
String getName();
InputStream readFile(String path) throws FileNotFoundException, IOException;
Collection<String> listFiles(String path, boolean recursive);

View File

@ -46,6 +46,11 @@ public FolderFileAccess(File folder) {
openedStreams = new ArrayList<>();
}
@Override
public String getName() {
return folder.getName();
}
@Override
public synchronized InputStream readFile(String path) throws FileNotFoundException {
InputStream stream = new FileInputStream(resolve(path).toFile());

View File

@ -43,6 +43,11 @@ public class ZipFileAccess implements FileAccess {
public ZipFileAccess(File file) throws ZipException, IOException {
this.file = new ZipFile(file);
}
@Override
public String getName() {
return file.getName();
}
@Override
public InputStream readFile(String path) throws FileNotFoundException, IOException {