31 lines
1.4 KiB
Java
31 lines
1.4 KiB
Java
package io.gitlab.jfronny.respackopts.filters.util;
|
|
|
|
import io.gitlab.jfronny.muscript.data.dynamic.Dynamic;
|
|
import io.gitlab.jfronny.muscript.ast.StringExpr;
|
|
import io.gitlab.jfronny.respackopts.Respackopts;
|
|
import io.gitlab.jfronny.respackopts.RespackoptsConfig;
|
|
import io.gitlab.jfronny.respackopts.util.MetaCache;
|
|
import net.minecraft.resource.InputSupplier;
|
|
import net.minecraft.resource.ResourcePack;
|
|
|
|
import java.io.*;
|
|
import java.util.Map;
|
|
|
|
public class FileExpansionProvider {
|
|
public static synchronized InputStream replace(Dynamic<?> parameter, InputStream is, Map<String, StringExpr> expansions) throws IOException {
|
|
String s = new String(is.readAllBytes());
|
|
for (Map.Entry<String, StringExpr> entry : expansions.entrySet()) {
|
|
s = s.replace("${" + entry.getKey() + "}", entry.getValue().get(parameter));
|
|
}
|
|
return new ByteArrayInputStream(s.getBytes());
|
|
}
|
|
|
|
public static InputSupplier<InputStream> replace(InputSupplier<InputStream> inputStream, ResourcePack pack, String file) {
|
|
return FileRpoSearchProvider.modifyWithRpo(file, pack, rpo -> rpo.expansions == null || rpo.expansions.isEmpty() ? inputStream : () -> {
|
|
try (InputStream is = inputStream.get()) {
|
|
return replace(MetaCache.getParameter(MetaCache.getKeyByPack(pack)), is, rpo.expansions);
|
|
}
|
|
}, inputStream);
|
|
}
|
|
}
|