From 861f97be3c47526c7f461dc2df1a8373e1bb1e87 Mon Sep 17 00:00:00 2001 From: isXander Date: Sun, 19 Feb 2023 22:18:22 +0000 Subject: [PATCH] fix controller id db file finding --- .../controlify/controller/ControllerType.java | 118 +++++++++--------- .../controller_identification.json5} | 0 2 files changed, 56 insertions(+), 62 deletions(-) rename src/main/resources/assets/controlify/{hiddb.json5 => controllers/controller_identification.json5} (100%) diff --git a/src/main/java/dev/isxander/controlify/controller/ControllerType.java b/src/main/java/dev/isxander/controlify/controller/ControllerType.java index 389e4fb..6fa6554 100644 --- a/src/main/java/dev/isxander/controlify/controller/ControllerType.java +++ b/src/main/java/dev/isxander/controlify/controller/ControllerType.java @@ -1,29 +1,20 @@ package dev.isxander.controlify.controller; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import com.google.gson.JsonArray; import dev.isxander.controlify.Controlify; import dev.isxander.controlify.controller.hid.HIDIdentifier; import net.minecraft.client.Minecraft; import net.minecraft.resources.ResourceLocation; -import net.minecraft.server.packs.PackResources; -import net.minecraft.server.packs.PackType; -import net.minecraft.server.packs.resources.IoSupplier; -import org.apache.commons.io.function.IOSupplier; +import net.minecraft.server.packs.resources.Resource; import org.quiltmc.json5.JsonReader; import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; import java.util.*; public class ControllerType { public static final ControllerType UNKNOWN = new ControllerType("Unknown", "unknown"); - private static final Gson GSON = new GsonBuilder().setLenient().create(); private static Map typeMap = null; - private static final ResourceLocation hidDbLocation = new ResourceLocation("controlify", "hiddb.json5"); + private static final ResourceLocation hidDbLocation = new ResourceLocation("controlify", "controllers/controller_identification.json5"); private final String friendlyName; private final String identifier; @@ -46,60 +37,19 @@ public class ControllerType { typeMap = new HashMap<>(); try { - List packs = Minecraft.getInstance().getResourceManager().listPacks().toList(); + List dbs = Minecraft.getInstance().getResourceManager() + .listResourceStacks("controllers", s -> s.equals(hidDbLocation)) // get the db file from every pack + .values().stream() // above ^^ function supports multiple resource locations so returns a map, we just want the one + .flatMap(Collection::stream) // flatten the list of list of resources + .toList(); - for (var pack : packs) { - String packName = pack.packId(); - IoSupplier isSupplier = pack.getResource(PackType.CLIENT_RESOURCES, hidDbLocation); - if (isSupplier == null) continue; - Controlify.LOGGER.info("Loading controller HID DB from pack " + packName); + for (var resource : dbs) { + try (var resourceReader = resource.openAsReader()) { + JsonReader reader = JsonReader.json5(resourceReader); + readControllerIdFiles(reader); - try (var hidDb = isSupplier.get()) { - JsonReader reader = JsonReader.json5(new InputStreamReader(hidDb)); - - reader.beginArray(); - while (reader.hasNext()) { - String friendlyName = null; - String identifier = null; - int vendorId = -1; - Set productIds = new HashSet<>(); - - reader.beginObject(); - while (reader.hasNext()) { - String name = reader.nextName(); - - switch (name) { - case "name" -> friendlyName = reader.nextString(); - case "identifier" -> identifier = reader.nextString(); - case "vendor" -> vendorId = reader.nextInt(); - case "product" -> { - reader.beginArray(); - while (reader.hasNext()) { - productIds.add(reader.nextInt()); - } - reader.endArray(); - } - default -> { - Controlify.LOGGER.warn("Unknown key in HID DB: " + name + ". Skipping..."); - reader.skipValue(); - } - } - } - reader.endObject(); - - if (friendlyName == null || identifier == null || vendorId == -1 || productIds.isEmpty()) { - Controlify.LOGGER.warn("Invalid entry in HID DB. Skipping..."); - continue; - } - - var type = new ControllerType(friendlyName, identifier); - for (int productId : productIds) { - typeMap.put(new HIDIdentifier(vendorId, productId), type); - } - } - reader.endArray(); } catch (Exception e) { - Controlify.LOGGER.error("Failed to load HID DB from pack " + packName, e); + Controlify.LOGGER.error("Failed to load HID DB from source", e); } } } catch (Exception e) { @@ -108,4 +58,48 @@ public class ControllerType { return typeMap.getOrDefault(hid, UNKNOWN); } + + private static void readControllerIdFiles(JsonReader reader) throws IOException { + reader.beginArray(); + while (reader.hasNext()) { + String friendlyName = null; + String identifier = null; + int vendorId = -1; + Set productIds = new HashSet<>(); + + reader.beginObject(); + while (reader.hasNext()) { + String name = reader.nextName(); + + switch (name) { + case "name" -> friendlyName = reader.nextString(); + case "identifier" -> identifier = reader.nextString(); + case "vendor" -> vendorId = reader.nextInt(); + case "product" -> { + reader.beginArray(); + while (reader.hasNext()) { + productIds.add(reader.nextInt()); + } + reader.endArray(); + } + default -> { + Controlify.LOGGER.warn("Unknown key in HID DB: " + name + ". Skipping..."); + reader.skipValue(); + } + } + } + reader.endObject(); + + if (friendlyName == null || identifier == null || vendorId == -1 || productIds.isEmpty()) { + Controlify.LOGGER.warn("Invalid entry in HID DB. Skipping..."); + continue; + } + + var type = new ControllerType(friendlyName, identifier); + for (int productId : productIds) { + typeMap.put(new HIDIdentifier(vendorId, productId), type); + } + } + reader.endArray(); + } } diff --git a/src/main/resources/assets/controlify/hiddb.json5 b/src/main/resources/assets/controlify/controllers/controller_identification.json5 similarity index 100% rename from src/main/resources/assets/controlify/hiddb.json5 rename to src/main/resources/assets/controlify/controllers/controller_identification.json5