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

31 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
2022-07-28 14:10:59 +02:00
import io.gitlab.jfronny.commons.data.dynamic.Dynamic;
import io.gitlab.jfronny.muscript.compiler.expr.StringExpr;
import io.gitlab.jfronny.respackopts.Respackopts;
2022-12-08 19:00:41 +01:00
import io.gitlab.jfronny.respackopts.RespackoptsConfig;
2022-07-28 14:10:59 +02:00
import io.gitlab.jfronny.respackopts.util.MetaCache;
2022-12-08 19:00:41 +01:00
import net.minecraft.resource.InputSupplier;
2022-07-28 14:10:59 +02:00
import net.minecraft.resource.ResourcePack;
2021-08-24 17:42:46 +02:00
import java.io.*;
2022-07-28 14:10:59 +02:00
import java.util.Map;
2021-08-24 17:42:46 +02:00
2021-09-15 16:45:54 +02:00
public class FileExpansionProvider {
2022-06-07 13:52:19 +02:00
public static synchronized InputStream replace(Dynamic<?> parameter, InputStream is, Map<String, StringExpr> expansions) throws IOException {
2021-08-24 17:42:46 +02:00
String s = new String(is.readAllBytes());
2022-06-07 13:52:19 +02:00
for (Map.Entry<String, 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());
}
2022-12-08 19:00:41 +01:00
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);
2021-09-15 17:13:14 +02:00
}
}, inputStream);
2021-08-24 17:42:46 +02:00
}
}