forked from Clones/Controlify
fix controller id db file finding
This commit is contained in:
@ -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<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 identifier;
|
||||
@ -46,60 +37,19 @@ public class ControllerType {
|
||||
|
||||
typeMap = new HashMap<>();
|
||||
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) {
|
||||
String packName = pack.packId();
|
||||
IoSupplier<InputStream> 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<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) {
|
||||
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<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();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user