Skip to content

Commit

Permalink
gracefully convert json config to json5
Browse files Browse the repository at this point in the history
  • Loading branch information
deirn committed Aug 8, 2024
1 parent d8736fa commit cddc322
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 6 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ dependencies {
val minecraftlessImplementation by configurations

minecraftlessImplementation("com.google.code.gson:gson:2.8.9")
minecraftlessImplementation("commons-io:commons-io:2.11.0")
minecraftlessImplementation("org.jetbrains:annotations:24.1.0")

testImplementation(platform("org.junit:junit-bom:5.10.2"))
Expand Down
21 changes: 16 additions & 5 deletions src/main/java/mcp/mobius/waila/config/JsonConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import mcp.mobius.waila.mcless.config.ConfigIo;
import mcp.mobius.waila.util.CachedSupplier;
import mcp.mobius.waila.util.Log;
import org.apache.commons.io.FilenameUtils;
import org.jetbrains.annotations.Nullable;

public class JsonConfig<T> implements IJsonConfig<T> {
Expand Down Expand Up @@ -100,7 +101,7 @@ public void backup(@Nullable String cause) {
public static class Builder<T> implements Builder0<T>, Builder1<T> {

final Type type;
Path path;
Supplier<Path> path;
boolean json5;
Function<String, @Nullable String> commenter;
Gson gson;
Expand Down Expand Up @@ -130,19 +131,29 @@ public Builder(Type type) {

@Override
public Builder1<T> file(File file) {
this.path = file.toPath();
this.path = file::toPath;
return this;
}

@Override
public Builder1<T> file(Path path) {
this.path = path;
this.path = () -> path;
return this;
}

@Override
public Builder1<T> file(String fileName) {
this.path = Waila.CONFIG_DIR.resolve(fileName + (fileName.endsWith(".json") ? "" : ".json"));
this.path = () -> {
var path = fileName;
if (json5) {
if (!path.endsWith(".json5")) path += ".json5";
} else {
if (!path.endsWith(".json")) path += ".json";
}

return Waila.CONFIG_DIR.resolve(path);
};

return this;
}

Expand Down Expand Up @@ -181,7 +192,7 @@ public Builder1<T> gson(Gson gson) {
@Override
public IJsonConfig<T> build() {
Preconditions.checkNotNull(factory, "Default value factory must not be null");
return new JsonConfig<>(path, type, factory, json5, commenter, gson, currentVersion, versionGetter, versionSetter);
return new JsonConfig<>(path.get(), type, factory, json5, commenter, gson, currentVersion, versionGetter, versionSetter);
}

}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/mcp/mobius/waila/config/PluginConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public enum PluginConfig implements IPluginConfig {

private static final Log LOG = Log.create();

private static final Path PATH = Waila.CONFIG_DIR.resolve(WailaConstants.NAMESPACE + "/" + WailaConstants.WAILA + "_plugins.json");
private static final Path PATH = Waila.CONFIG_DIR.resolve(WailaConstants.NAMESPACE + "/" + WailaConstants.WAILA + "_plugins.json5");
private static final ConfigIo<Map<String, Map<String, JsonElement>>> IO = new ConfigIo<>(
LOG::warn, LOG::error,
true,
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/mcp/mobius/waila/gui/widget/CategoryEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public class CategoryEntry extends ConfigListWidget.Entry {
.file(WailaConstants.NAMESPACE + "/category_entries")
.json5()
.factory(HashMap::new)
.commenter(p -> !p.equals("$") ? null : """
This config controls the category entries collapsed state.
You shouldn't edit this config by hand.
""")
.build();

public final Component title;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import mcp.mobius.waila.mcless.json5.Json5MapTypeAdapterFactory;
import mcp.mobius.waila.mcless.json5.Json5Reader;
import mcp.mobius.waila.mcless.json5.Json5Writer;
import org.apache.commons.io.FilenameUtils;
import org.jetbrains.annotations.Nullable;

public class ConfigIo<T> {
Expand Down Expand Up @@ -61,6 +62,20 @@ public T read(Path path) {
T config;
var init = true;
if (!Files.exists(path)) {
if (json5) {
var pathString = path.toString();
if (FilenameUtils.getExtension(pathString).equals("json5")) {
var jsonPath = path.resolveSibling(FilenameUtils.getBaseName(pathString) + ".json");
try {
Files.copy(jsonPath, path);
Files.delete(jsonPath);
return read(path);
} catch (IOException e) {
error.accept("Failed to move " + path + " to " + jsonPath, e);
}
}
}

var parent = path.getParent();
if (!Files.exists(parent)) {
try {
Expand Down

0 comments on commit cddc322

Please sign in to comment.