| 1 | package net.minecraft.client.resources;
|
| 2 |
|
| 3 | import com.google.common.base.Splitter;
|
| 4 | import com.google.gson.JsonElement;
|
| 5 | import com.google.gson.JsonObject;
|
| 6 | import com.google.gson.JsonParseException;
|
| 7 | import com.mojang.logging.LogUtils;
|
| 8 | import java.io.BufferedReader;
|
| 9 | import java.io.IOException;
|
| 10 | import java.nio.charset.StandardCharsets;
|
| 11 | import java.nio.file.Files;
|
| 12 | import java.nio.file.Path;
|
| 13 | import java.util.List;
|
| 14 | import java.util.Map.Entry;
|
| 15 | import net.fabricmc.api.EnvType;
|
| 16 | import net.fabricmc.api.Environment;
|
| 17 | import net.minecraft.server.packs.linkfs.LinkFileSystem;
|
| 18 | import net.minecraft.server.packs.linkfs.LinkFileSystem.Builder;
|
| 19 | import net.minecraft.util.GsonHelper;
|
| 20 | import org.slf4j.Logger;
|
| 21 |
|
| 22 | @Environment(EnvType.CLIENT)
|
| 23 | public class IndexedAssetSource {
|
| 24 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 25 | public static final Splitter PATH_SPLITTER = Splitter.on('/');
|
| 26 |
|
| 27 | public static Path createIndexFs(final Path assetsDirectory, final String index) {
|
| 28 | Path objectsDirectory = assetsDirectory.resolve("objects");
|
| 29 | Builder builder = LinkFileSystem.builder();
|
| 30 | Path indexFile = assetsDirectory.resolve("indexes/" + index + ".json");
|
| 31 |
|
| 32 | try (BufferedReader reader = Files.newBufferedReader(indexFile, StandardCharsets.UTF_8)) {
|
| 33 | JsonObject root = GsonHelper.parse(reader);
|
| 34 | JsonObject objects = GsonHelper.getAsJsonObject(root, "objects", null);
|
| 35 | if (objects != null) {
|
| 36 | for (Entry<String, JsonElement> entry : objects.entrySet()) {
|
| 37 | JsonObject object = (JsonObject)entry.getValue();
|
| 38 | String filename = entry.getKey();
|
| 39 | List<String> path = PATH_SPLITTER.splitToList(filename);
|
| 40 | String hash = GsonHelper.getAsString(object, "hash");
|
| 41 | Path file = objectsDirectory.resolve(hash.substring(0, 2) + "/" + hash);
|
| 42 | builder.put(path, file);
|
| 43 | }
|
| 44 | }
|
| 45 | } catch (JsonParseException ignored) {
|
| 46 | LOGGER.error("Unable to parse resource index file: {}", indexFile);
|
| 47 | } catch (IOException ignored) {
|
| 48 | LOGGER.error("Can't open the resource index file: {}", indexFile);
|
| 49 | }
|
| 50 |
|
| 51 | return builder.build("index-" + index).getPath("/");
|
| 52 | }
|
| 53 | }
|
| 54 |
|