2021-09-15 16:45:54 +02:00
|
|
|
package io.gitlab.jfronny.respackopts.filters.util;
|
2021-08-24 17:42:46 +02:00
|
|
|
|
2022-06-07 13:52:19 +02:00
|
|
|
import io.gitlab.jfronny.muscript.compiler.expr.*;
|
|
|
|
import io.gitlab.jfronny.muscript.dynamic.*;
|
2022-06-04 19:43:49 +02:00
|
|
|
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.*;
|
2022-06-04 19:43:49 +02:00
|
|
|
import java.util.*;
|
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()) {
|
2022-06-04 19:43:49 +02:00
|
|
|
s = s.replace("${" + entry.getKey() + "}", entry.getValue().get(parameter));
|
2021-08-24 17:42:46 +02:00
|
|
|
}
|
|
|
|
return new ByteArrayInputStream(s.getBytes());
|
|
|
|
}
|
|
|
|
|
2022-01-12 18:23:49 +01:00
|
|
|
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 {
|
2022-06-04 19:43:49 +02:00
|
|
|
return replace(MetaCache.getParameter(MetaCache.getKeyByPack(pack)), inputStream, rpo.expansions);
|
2021-09-15 17:13:14 +02:00
|
|
|
} catch (IOException e) {
|
2022-01-12 18:23:49 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|