teddyxlandlee
脚本在此:https://github.com/Featurehouse/epx_packs/tree/master/v2/src

teddyxlandlee
已自行解决:
  1. package xland.mcmod.remoteresourcepack;

  2. import com.google.gson.JsonArray;
  3. import com.google.gson.JsonElement;
  4. import com.google.gson.JsonObject;
  5. import com.google.gson.JsonParseException;

  6. import java.io.IOException;
  7. import java.net.URL;
  8. import java.nio.charset.StandardCharsets;
  9. import java.nio.file.Files;
  10. import java.nio.file.Path;
  11. import java.util.Base64;
  12. import java.util.Map;
  13. import java.util.random.RandomGenerator;
  14. import java.util.zip.ZipEntry;
  15. import java.util.zip.ZipOutputStream;

  16. import static net.minecraft.util.GsonHelper.*;

  17. final class ZipConfigUtil {
  18.     private ZipConfigUtil() {}
  19.     private static final Base64.Decoder B64DECODER = Base64.getDecoder();
  20.     private static final ThreadLocal<RandomGenerator> RANDOM = ThreadLocal.withInitial(RandomGenerator::getDefault);

  21.     private static void addFileToZip(ZipOutputStream zos, String filename,
  22.                                      JsonObject data, URL baseUrl)
  23.             throws IOException {
  24.         zos.putNextEntry(new ZipEntry(filename));
  25.         if (!filename.endsWith("/")) {  // otherwise is directory
  26.             if (isStringValue(data, "fetch")) {
  27.                 final String s = getAsString(data, "fetch");
  28.                 try (var is = new URL(baseUrl, s).openStream()) {
  29.                     is.transferTo(zos);
  30.                 }
  31.             } else if (isStringValue(data, "base64")) {
  32.                 final byte[] b = B64DECODER.decode(getAsString(data, "base64"));
  33.                 zos.write(b);
  34.             } else if (isStringValue(data, "raw")) {
  35.                 zos.write(getAsString(data, "raw").getBytes(StandardCharsets.UTF_8));
  36.             }   // else: put an empty entry
  37.         }
  38.         zos.closeEntry();
  39.     }

  40.     static void generateZip(JsonObject zipConfig, URL baseUrl,
  41.                             Map<String, String> args, Path dest)
  42.             throws IOException {
  43.         try (ZipOutputStream zos = new ZipOutputStream(Files.newOutputStream(dest))) {
  44.             final JsonObject staticFiles = getAsJsonObject(zipConfig, "static");
  45.             for (Map.Entry<String, JsonElement> entry : staticFiles.entrySet()) {
  46.                 addFileToZip(zos, entry, baseUrl);
  47.             }
  48.             
  49.             final JsonObject dynamicFiles = getAsJsonObject(zipConfig, "dynamic");
  50.             for (Map.Entry<String, JsonElement> dynArgEntry : dynamicFiles.entrySet()) {
  51.                 final JsonObject dynamicData = convertToJsonObject(dynArgEntry.getValue(), dynArgEntry.getKey());
  52.                 // paramValue
  53.                 int paramValue;
  54.                 String paramString = args.get(dynArgEntry.getKey());
  55.                
  56.                 if ("random".equals(paramString))
  57.                     paramValue = -1;
  58.                 else try {
  59.                     paramValue = Integer.parseUnsignedInt(paramString);
  60.                 } catch (NumberFormatException ex) {
  61.                     final JsonElement e = dynamicData.get("default");
  62.                     if (e != null && e.isJsonPrimitive() ) {
  63.                         if ("random".equals(e.getAsString()))
  64.                             paramValue = -1;
  65.                         // may throw another NFE, here we assume it is provider's fault
  66.                         else {
  67.                             paramValue = e.getAsJsonPrimitive().getAsInt();
  68.                             if (paramValue < 0)
  69.                                 throw new JsonParseException(
  70.                                         "dynamic default value of %s is %s while negative value is illegal".formatted(
  71.                                                 dynArgEntry.getKey(), paramValue
  72.                                         ));
  73.                         }
  74.                     } else {
  75.                         throw new JsonParseException("Missing default value for " + dynArgEntry.getKey()
  76.                                 + " or it is not primitive");
  77.                     }
  78.                 }
  79.                
  80.                 if (paramValue < 0) {   // is random
  81.                     final JsonArray items = getAsJsonArray(dynamicData, "items");
  82.                     final String errDesc = "dynamic." + dynArgEntry.getKey() + ".items";
  83.                     
  84.                     int totalWeight = 0;
  85.                     int index = 0;
  86.                     int[] weights = new int[items.size()];
  87.                     
  88.                     for (JsonElement item0 : items) {
  89.                         final JsonObject item = convertToJsonObject(item0, errDesc + '.' + index);
  90.                         int weight = getAsInt(item, "weight", 100);
  91.                         if (weight == 0) weight = 100;
  92.                         totalWeight += (weights[index++] = weight);
  93.                     }
  94.                     
  95.                     int randomNum = RANDOM.get().nextInt(totalWeight);
  96.                     index = 0;
  97.                     for (JsonElement item0 : items) {
  98.                         randomNum -= weights[index++];
  99.                         if (randomNum < 0) {
  100.                             final JsonObject files = getAsJsonObject(item0.getAsJsonObject(), "files");
  101.                             for (Map.Entry<String, JsonElement> fileEntry : files.entrySet()) {
  102.                                 addFileToZip(zos, fileEntry, baseUrl);
  103.                             }
  104.                             break;
  105.                         }
  106.                     }
  107.                 } else {
  108.                     final JsonArray items = convertToJsonArray(dynamicData, "items");
  109.                     if (paramValue < items.size()) {    // index in bounds
  110.                         final JsonObject files = getAsJsonObject(convertToJsonObject(items.get(paramValue),
  111.                                 "dynamic." + dynArgEntry.getKey() + ".items." + paramValue), "files");
  112.                         for (Map.Entry<String, JsonElement> fileEntry : files.entrySet()) {
  113.                             addFileToZip(zos, fileEntry, baseUrl);
  114.                         }
  115.                     }
  116.                 }
  117.             }
  118.         }
  119.     }

  120.     private static void addFileToZip(ZipOutputStream zos, Map.Entry<String, ? extends JsonElement> fileEntry,
  121.                                      URL baseUrl) throws IOException {
  122.         addFileToZip(zos, fileEntry.getKey(), convertToJsonObject(fileEntry.getValue(), fileEntry.getKey()), baseUrl);
  123.     }
  124. }
复制代码