Skip to content

Commit

Permalink
fix default naming not applying correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
Bawnorton committed Oct 13, 2024
1 parent 82847dc commit bdb3c23
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 14 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 2.0.1

- Fix default naming not applying correctly

# 2.0.0

- Migrated from json to json5 configs
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ org.gradle.jvmargs=-Xmx4G
fabric_versions=1.20.1, 1.21.1
neoforge_versions=1.21.1

mod_version=2.0.0
mod_version=2.0.1
mod_group=com.bawnorton
mod_id=configurable
mod_name=Configurable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public final class ConfigLoaderGenerator extends ConfigurableGenerator {
import com.bawnorton.configurable.ref.Reference;
import com.bawnorton.configurable.ref.gson.ReferenceSerializer;
import com.bawnorton.configurable.platform.Platform;
import com.google.gson.FieldNamingStrategy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonPrimitive;
Expand All @@ -42,24 +43,42 @@ public final class ConfigLoaderGenerator extends ConfigurableGenerator {
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.UnaryOperator;
public final class ConfigLoader implements GeneratedConfigLoader<Config> {
private static final Path configPath = Platform.getConfigDir()
.resolve("configurable/<file_name>.json5");
private static final Path legacyConfigPath = Platform.getConfigDir()
.resolve("configurable/<file_name>.json");
private static final Map<String, Field> FIELDS_BY_KEY_PATH = new HashMap<>();
private static final Gson GSON = createGson();
private static Gson createGson() {
GsonBuilder builder = new GsonBuilder()
.setPrettyPrinting()
.registerTypeAdapter(Reference.class, new ReferenceSerializer());
ConfigurableMain.getTypeAdapters("<name>", "<source_set>").forEach(builder::registerTypeHierarchyAdapter);
builder.setFieldNamingStrategy(ConfigurableMain.getFieldNamingStrategy("<name>", "<source_set>"));
FieldNamingStrategy namingStrategy = ConfigurableMain.getFieldNamingStrategy("<name>", "<source_set>");
builder.setFieldNamingStrategy(namingStrategy);
recordNestedKeyPaths(Config.class, "", namingStrategy, FIELDS_BY_KEY_PATH::put);
return builder.create();
}
private static void recordNestedKeyPaths(Class<?> clazz, String keyPath, FieldNamingStrategy namingStrategy, BiConsumer<String, Field> setter) {
for (Field field : clazz.getDeclaredFields()) {
if(field.getName().equals("CONFIGURABLE_COMMENT")) continue;
String fieldPath = keyPath + namingStrategy.translateName(field);
setter.accept(fieldPath, field);
if(!field.getType().equals(Reference.class)) {
recordNestedKeyPaths(field.getType(), "%s.".formatted(fieldPath), namingStrategy, setter);
}
}
}
@Override
public Config loadConfig(UnaryOperator<String> datafixer) {
try {
Expand Down Expand Up @@ -135,7 +154,7 @@ private void writeRefField(Field field, Object instance, JsonWriter writer) thro
if(ref.hasComment()) {
writer.blockComment(ref.getComment());
}
writer.name(field.getName());
writer.name(GSON.fieldNamingStrategy().translateName(field));
JsonElement elemnt = GSON.toJsonTree(ref, field.getGenericType());
GSON.toJson(elemnt, new GsonWriter(writer));
}
Expand All @@ -148,7 +167,7 @@ private void writeNestedField(Field field, Object instance, JsonWriter writer) t
writer.blockComment(comment);
}
} catch (NoSuchFieldException ignored) {}
writer.name(field.getName());
writer.name(GSON.fieldNamingStrategy().translateName(field));
writer.beginObject();
Field[] nestedFields = instance.getClass().getDeclaredFields();
for(Field nestedField : nestedFields) {
Expand Down Expand Up @@ -201,26 +220,28 @@ private void parseReference(String key, JsonPrimitive value, List<String> parent
}
keyPath += key;
Field target;
Field target = FIELDS_BY_KEY_PATH.get(keyPath);
Object instance = config;
try {
if(parents.isEmpty()) {
target = configClass.getDeclaredField(key);
} else {
Class<?> nested = configClass;
for(String parent : parents) {
Field parentField = nested.getDeclaredField(parent);
nested = parentField.getType();
if (!parents.isEmpty()) {
String parentKeyPath = parents.get(0);
Field parentField = FIELDS_BY_KEY_PATH.get(parentKeyPath);
for (int i = 0; i < parents.size(); i++) {
if (i > 0) {
parentKeyPath += "." + parents.get(i);
parentField = FIELDS_BY_KEY_PATH.get(parentKeyPath);
}
try {
instance = parentField.get(instance);
} catch (ReflectiveOperationException e) {
ConfigurableMain.LOGGER.error("Field: \\"%s\\" could not be set.".formatted(keyPath), e);
}
}
target = nested.getDeclaredField(key);
}
} catch (NoSuchFieldException ignored) {
} catch (RuntimeException ignored) {
ConfigurableMain.LOGGER.warn("Field: \\"%s\\" could not be found.".formatted(keyPath));
return;
}
Expand Down

0 comments on commit bdb3c23

Please sign in to comment.