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

35 lines
1.4 KiB
Java
Raw Normal View History

2021-09-15 16:45:54 +02:00
package io.gitlab.jfronny.respackopts.filters.util;
2021-08-24 17:42:46 +02:00
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.*;
2021-08-24 17:42:46 +02:00
import java.io.*;
import java.util.*;
2021-08-24 17:42:46 +02:00
2021-09-15 16:45:54 +02:00
public class FileExpansionProvider {
public static synchronized InputStream replace(OAny<?> parameter, InputStream is, Map<String, Expr.StringExpr> expansions) throws IOException {
2021-08-24 17:42:46 +02:00
String s = new String(is.readAllBytes());
for (Map.Entry<String, Expr.StringExpr> entry : expansions.entrySet()) {
s = s.replace("${" + entry.getKey() + "}", entry.getValue().get(parameter));
2021-08-24 17:42:46 +02:00
}
return new ByteArrayInputStream(s.getBytes());
}
public static InputStream replace(InputStream inputStream, ResourcePack pack, String file) {
return FileRpoSearchProvider.modifyWithRpo(file, pack, rpo -> {
2021-08-24 17:42:46 +02:00
if (rpo.expansions == null || rpo.expansions.isEmpty())
return inputStream;
2021-09-15 17:13:14 +02:00
try {
return replace(MetaCache.getParameter(MetaCache.getKeyByPack(pack)), inputStream, rpo.expansions);
2021-09-15 17:13:14 +02:00
} catch (IOException e) {
Respackopts.LOGGER.error("Could not perform file expansion on " + file, e);
2021-09-15 17:13:14 +02:00
return inputStream;
}
}, inputStream);
2021-08-24 17:42:46 +02:00
}
}