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;
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,17 +37,29 @@ 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));
} catch (Exception e) {
Controlify.LOGGER.error("Failed to load HID DB from source", e);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return typeMap.getOrDefault(hid, UNKNOWN);
}
private static void readControllerIdFiles(JsonReader reader) throws IOException {
reader.beginArray();
while (reader.hasNext()) {
String friendlyName = null;
@ -98,14 +101,5 @@ public class ControllerType {
}
}
reader.endArray();
} catch (Exception e) {
Controlify.LOGGER.error("Failed to load HID DB from pack " + packName, e);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return typeMap.getOrDefault(hid, UNKNOWN);
}
}