1
0
forked from Clones/Controlify

fix controller id db file finding

This commit is contained in:
isXander
2023-02-19 22:18:22 +00:00
parent 0f719c0f45
commit 861f97be3c
2 changed files with 56 additions and 62 deletions

View File

@ -1,29 +1,20 @@
package dev.isxander.controlify.controller; 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.Controlify;
import dev.isxander.controlify.controller.hid.HIDIdentifier; import dev.isxander.controlify.controller.hid.HIDIdentifier;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.PackResources; import net.minecraft.server.packs.resources.Resource;
import net.minecraft.server.packs.PackType;
import net.minecraft.server.packs.resources.IoSupplier;
import org.apache.commons.io.function.IOSupplier;
import org.quiltmc.json5.JsonReader; import org.quiltmc.json5.JsonReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.*; import java.util.*;
public class ControllerType { public class ControllerType {
public static final ControllerType UNKNOWN = new ControllerType("Unknown", "unknown"); public static final ControllerType UNKNOWN = new ControllerType("Unknown", "unknown");
private static final Gson GSON = new GsonBuilder().setLenient().create();
private static Map<HIDIdentifier, ControllerType> typeMap = null; private static Map<HIDIdentifier, ControllerType> 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 friendlyName;
private final String identifier; private final String identifier;
@ -46,60 +37,19 @@ public class ControllerType {
typeMap = new HashMap<>(); typeMap = new HashMap<>();
try { try {
List<PackResources> packs = Minecraft.getInstance().getResourceManager().listPacks().toList(); List<Resource> 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) { for (var resource : dbs) {
String packName = pack.packId(); try (var resourceReader = resource.openAsReader()) {
IoSupplier<InputStream> isSupplier = pack.getResource(PackType.CLIENT_RESOURCES, hidDbLocation); JsonReader reader = JsonReader.json5(resourceReader);
if (isSupplier == null) continue; readControllerIdFiles(reader);
Controlify.LOGGER.info("Loading controller HID DB from pack " + packName);
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<Integer> 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) { } 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) { } catch (Exception e) {
@ -108,4 +58,48 @@ public class ControllerType {
return typeMap.getOrDefault(hid, UNKNOWN); 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<Integer> 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();
}
} }