From 4709e57f05c4b0b7f5d918bc72eb6b939da95511 Mon Sep 17 00:00:00 2001 From: Bawnorton Date: Sun, 13 Oct 2024 10:33:20 -0700 Subject: [PATCH] fixes --- gradle.properties | 2 +- .../configurable/ConfigurableMain.java | 22 +++++-------------- .../ap/generator/ConfigLoaderGenerator.java | 14 +++++++----- .../configurable/api/ConfigurableApi.java | 15 ++++++++++--- .../load/ConfigurableApiImplLoader.java | 10 ++------- .../load/ConfigurableWrapper.java | 15 +++++++++++++ 6 files changed, 43 insertions(+), 35 deletions(-) diff --git a/gradle.properties b/gradle.properties index c86c6df..6aded9b 100644 --- a/gradle.properties +++ b/gradle.properties @@ -3,7 +3,7 @@ org.gradle.jvmargs=-Xmx4G fabric_versions=1.20.1, 1.21.1 neoforge_versions=1.21.1 -mod_version=1.4.20 +mod_version=2.0.0 mod_group=com.bawnorton mod_id=configurable mod_name=Configurable diff --git a/src/main/java/com/bawnorton/configurable/ConfigurableMain.java b/src/main/java/com/bawnorton/configurable/ConfigurableMain.java index c6e8420..49cd8ae 100644 --- a/src/main/java/com/bawnorton/configurable/ConfigurableMain.java +++ b/src/main/java/com/bawnorton/configurable/ConfigurableMain.java @@ -36,8 +36,6 @@ public final class ConfigurableMain { .setPrettyPrinting() .create(); - private static final Map, Object>> typeAdapters = new HashMap<>(); - public static Identifier id(String path) { return Identifier.of(MOD_ID, path); } @@ -76,15 +74,13 @@ public static void init() { } } - registerDefaultTypeAdapters(configName); - try { ConfigurableWrapper wrapper = new ConfigurableWrapper(ConfigurableApiImplLoader.getImpl(configName)); + WRAPPERS.computeIfAbsent(configName, k -> new HashMap<>()).put(sourceSet, wrapper); addToWrapped(settings::fullyQualifiedLoader, wrapper::setLoader, configName); if(settings.hasScreenFactory() && !Platform.isServer()) { addToWrapped(settings::fullyQualifiedScreenFactory, wrapper::setScreenFactory, configName); } - WRAPPERS.computeIfAbsent(configName, k -> new HashMap<>()).put(sourceSet, wrapper); } catch (IllegalStateException e) { LOGGER.error("Could not create configurable wrapper for \"%s\"".formatted(configName), e); } @@ -105,20 +101,12 @@ private static void addToWrapped(Supplier nameGetter, Consumer void registerTypeAdapater(String configName, Class type, Object typeAdapter) { - typeAdapters.computeIfAbsent(configName, k -> new HashMap<>()).put(type, typeAdapter); - } - - public static Map, Object> getTypeAdapters(String configName) { - return typeAdapters.getOrDefault(configName, Map.of()); + public static Map, Object> getTypeAdapters(String configName, String sourceSet) { + return WRAPPERS.get(configName).get(sourceSet).getTypeAdapters(); } - public static FieldNamingStrategy getFieldNamingStrategy(String configName) { - return WRAPPERS.get(configName) + public static FieldNamingStrategy getFieldNamingStrategy(String configName, String sourceSet) { + return WRAPPERS.get(configName).get(sourceSet).getFieldNamingStrategy(); } public static Map> getAllWrappers() { diff --git a/src/main/java/com/bawnorton/configurable/ap/generator/ConfigLoaderGenerator.java b/src/main/java/com/bawnorton/configurable/ap/generator/ConfigLoaderGenerator.java index 446b0fa..404a9c3 100644 --- a/src/main/java/com/bawnorton/configurable/ap/generator/ConfigLoaderGenerator.java +++ b/src/main/java/com/bawnorton/configurable/ap/generator/ConfigLoaderGenerator.java @@ -41,7 +41,8 @@ public final class ConfigLoaderGenerator extends ConfigurableGenerator { import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.Set;import java.util.function.UnaryOperator; +import java.util.Set; +import java.util.function.UnaryOperator; public final class ConfigLoader implements GeneratedConfigLoader { private static final Path configPath = Platform.getConfigDir() @@ -54,7 +55,8 @@ private static Gson createGson() { GsonBuilder builder = new GsonBuilder() .setPrettyPrinting() .registerTypeAdapter(Reference.class, new ReferenceSerializer()); - ConfigurableMain.getTypeAdapters("").forEach(builder::registerTypeHierarchyAdapter); + ConfigurableMain.getTypeAdapters("", "").forEach(builder::registerTypeHierarchyAdapter); + builder.setFieldNamingStrategy(ConfigurableMain.getFieldNamingStrategy("", "")); return builder.create(); } @@ -81,7 +83,7 @@ public Config loadConfig(UnaryOperator datafixer) { if(usingLegacyConfig) { ConfigurableMain.LOGGER.info("Migrating legacy config \\"\\""); Files.deleteIfExists(legacyConfigPath); - saveConfig(config); + saveConfig(parsed); } ConfigurableMain.LOGGER.info("Successfully loaded config \\"\\""); @@ -232,11 +234,11 @@ private void parseReference(String key, JsonPrimitive value, List parent } else { reference.setMemento(refValue); } - } catch (ClassCastException e) { - ConfigurableMain.LOGGER.error("Field: \\"%s\\" of type \\"%s\\" could not be set.".formatted(keyPath, expected), e); + } catch (ClassCastException | IllegalArgumentException e) { + ConfigurableMain.LOGGER.warn("Field: \\"%s\\" of type \\"%s\\" could not be set to \\"%s\\". Falling back to default.".formatted(keyPath, expected, value.toString())); } } catch (IllegalAccessException e) { - ConfigurableMain.LOGGER.error("Field: \\"%s\\" could not be set.".formatted(keyPath), e); + ConfigurableMain.LOGGER.error("Field: \\"%s\\" could not be set. Falling back to default".formatted(keyPath), e); } } diff --git a/src/main/java/com/bawnorton/configurable/api/ConfigurableApi.java b/src/main/java/com/bawnorton/configurable/api/ConfigurableApi.java index 9384451..512568f 100644 --- a/src/main/java/com/bawnorton/configurable/api/ConfigurableApi.java +++ b/src/main/java/com/bawnorton/configurable/api/ConfigurableApi.java @@ -6,7 +6,8 @@ import java.util.Map; public interface ConfigurableApi { - ConfigurableApi DEFAULT = new ConfigurableApi() {}; + ConfigurableApi DEFAULT = new ConfigurableApi() { + }; default Map, Object> getTypeAdapters() { return Map.of(); @@ -32,6 +33,14 @@ default FieldNamingStrategy defaultFieldNamingStrategy() { return FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES; } - //? if neoforge - /*String getConfigName();*/ + //? if !fabric { + /*/^* + * The name of the config this API impl belongs to, this is necessary for non-fabric implementations + *
+ * THIS DOES NOT SET THE NAME OF THE CONFIG + ^/ + default String getConfigName() { + throw new UnsupportedOperationException(); + } + *///?} } diff --git a/src/main/java/com/bawnorton/configurable/load/ConfigurableApiImplLoader.java b/src/main/java/com/bawnorton/configurable/load/ConfigurableApiImplLoader.java index a74669e..58af742 100644 --- a/src/main/java/com/bawnorton/configurable/load/ConfigurableApiImplLoader.java +++ b/src/main/java/com/bawnorton/configurable/load/ConfigurableApiImplLoader.java @@ -21,22 +21,16 @@ public static void load() { FabricLoader.getInstance().getEntrypointContainers("configurable", ConfigurableApi.class).forEach(container -> { String id = container.getProvider().getMetadata().getId(); try { - applyImpl(id, container.getEntrypoint()); + impls.put(id, container.getEntrypoint()); } catch (Throwable e) { ConfigurableMain.LOGGER.error("Mod {} provides a broken ConfigurableApi implemenation", id, e); } }); //?} elif neoforge { - /*serviceLoader.forEach(apiImpl -> applyImpl(apiImpl.getConfigName(), apiImpl)); + /*serviceLoader.forEach(apiImpl -> impls.put(apiImpl.getConfigName(), apiImpl)); *///?} } - private static void applyImpl(String id, ConfigurableApi apiImpl) { - Map, Object> adapters = apiImpl.getTypeAdapters(); - adapters.forEach((type, adapter) -> ConfigurableMain.registerTypeAdapater(id, type, adapter)); - impls.put(id, apiImpl); - } - public static ConfigurableApi getImpl(String name) { return impls.getOrDefault(name, ConfigurableApi.DEFAULT); } diff --git a/src/main/java/com/bawnorton/configurable/load/ConfigurableWrapper.java b/src/main/java/com/bawnorton/configurable/load/ConfigurableWrapper.java index 393be23..fa1e2a7 100644 --- a/src/main/java/com/bawnorton/configurable/load/ConfigurableWrapper.java +++ b/src/main/java/com/bawnorton/configurable/load/ConfigurableWrapper.java @@ -4,9 +4,14 @@ import com.bawnorton.configurable.generated.GeneratedConfig; import com.bawnorton.configurable.generated.GeneratedConfigLoader; import com.bawnorton.configurable.generated.GeneratedConfigScreenFactory; +import com.bawnorton.configurable.ref.gson.ItemTypeAdapter; +import com.google.gson.FieldNamingStrategy; import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.screen.Screen; +import net.minecraft.item.Item; import java.lang.reflect.Constructor; +import java.util.HashMap; +import java.util.Map; public final class ConfigurableWrapper { private final ConfigurableApi apiImpl; @@ -43,6 +48,16 @@ public boolean serverEnforces() { return apiImpl.serverEnforces(); } + public FieldNamingStrategy getFieldNamingStrategy() { + return apiImpl.defaultFieldNamingStrategy(); + } + + public Map, Object> getTypeAdapters() { + Map, Object> typeAdapters = new HashMap<>(apiImpl.getTypeAdapters()); + typeAdapters.put(Item.class, new ItemTypeAdapter()); + return typeAdapters; + } + public GeneratedConfig getConfig() { if(lastLoadedConfig == null) { loadConfig();