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.respackopts.Respackopts;
|
|
|
|
import meteordevelopment.starscript.Script;
|
2021-10-03 16:45:29 +02:00
|
|
|
import net.minecraft.resource.ResourcePack;
|
2021-08-24 17:42:46 +02:00
|
|
|
|
|
|
|
import java.io.*;
|
|
|
|
import java.util.Map;
|
|
|
|
|
2021-09-15 16:45:54 +02:00
|
|
|
public class FileExpansionProvider {
|
2021-08-24 17:42:46 +02:00
|
|
|
public static synchronized InputStream replace(InputStream is, Map<String, Script> expansions) throws IOException {
|
|
|
|
String s = new String(is.readAllBytes());
|
|
|
|
for (Map.Entry<String, Script> entry : expansions.entrySet()) {
|
2022-02-17 21:37:57 +01:00
|
|
|
s = s.replace("${" + entry.getKey() + "}", Respackopts.STAR_SCRIPT.run(entry.getValue()).toString());
|
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 {
|
|
|
|
return replace(inputStream, rpo.expansions);
|
|
|
|
} 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
|
|
|
}
|
|
|
|
}
|