Respackopts/src/main/java/io/gitlab/jfronny/respackopts/filters/util/FileExpansionProvider.java

35 lines
1.4 KiB
Java

package io.gitlab.jfronny.respackopts.filters.util;
import io.gitlab.jfronny.muscript.*;
import io.gitlab.jfronny.muscript.compiler.*;
import io.gitlab.jfronny.muscript.optic.*;
import io.gitlab.jfronny.respackopts.*;
import io.gitlab.jfronny.respackopts.util.*;
import net.minecraft.resource.*;
import java.io.*;
import java.util.*;
public class FileExpansionProvider {
public static synchronized InputStream replace(OAny<?> parameter, InputStream is, Map<String, Expr.StringExpr> expansions) throws IOException {
String s = new String(is.readAllBytes());
for (Map.Entry<String, Expr.StringExpr> entry : expansions.entrySet()) {
s = s.replace("${" + entry.getKey() + "}", entry.getValue().get(parameter));
}
return new ByteArrayInputStream(s.getBytes());
}
public static InputStream replace(InputStream inputStream, ResourcePack pack, String file) {
return FileRpoSearchProvider.modifyWithRpo(file, pack, rpo -> {
if (rpo.expansions == null || rpo.expansions.isEmpty())
return inputStream;
try {
return replace(MetaCache.getParameter(MetaCache.getKeyByPack(pack)), inputStream, rpo.expansions);
} catch (IOException e) {
Respackopts.LOGGER.error("Could not perform file expansion on " + file, e);
return inputStream;
}
}, inputStream);
}
}