aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/dev/isxander/yacl3/config/v3/JsonFileCodecConfig.java
blob: 49a0dac9ccf93a52c1cb18f7f24f782777087c0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package dev.isxander.yacl3.config.v3;

import com.google.gson.*;
import com.mojang.serialization.DataResult;
import com.mojang.serialization.JsonOps;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;

@ApiStatus.Experimental
public abstract class JsonFileCodecConfig extends CodecConfig {
    private final Path configPath;
    private final Gson gson;

    public JsonFileCodecConfig(Path configPath) {
        this.configPath = configPath;
        this.gson = createGson();
    }

    public void saveToFile() {
        DataResult<JsonElement> jsonTreeResult = this.encodeStart(JsonOps.INSTANCE);
        if (jsonTreeResult.error().isPresent()) {
            onSaveError(
                    SaveError.ENCODING,
                    new IllegalStateException("Failed to encode: " + jsonTreeResult.error().get().message())
            );
            return;
        }

        JsonElement jsonTree = jsonTreeResult.result().orElseThrow();
        String json = gson.toJson(jsonTree);

        try {
            Files.writeString(configPath, json, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE);
        } catch (IOException e) {
            onSaveError(SaveError.WRITING, e);
        }
    }

    public boolean loadFromFile() {
        if (Files.notExists(configPath)) {
            return false;
        }

        String json;
        try {
            json = Files.readString(configPath);
        } catch (IOException e) {
            onLoadError(LoadError.READING, e);
            return false;
        }

        JsonElement jsonTree;
        try {
            jsonTree = JsonParser.parseString(json);
        } catch (JsonParseException e) {
            onLoadError(LoadError.JSON_PARSING, e);
            return false;
        }

        return this.decode(jsonTree, JsonOps.INSTANCE);
    }

    protected Gson createGson() {
        return new GsonBuilder().setPrettyPrinting().create();
    }

    protected void onSaveError(SaveError error, @Nullable Throwable e) {
        throw new IllegalStateException("Error whilst " + error.name().toLowerCase(), e);
    }

    protected void onLoadError(LoadError error, @Nullable Throwable e) {
        throw new IllegalStateException("Error whilst " + error.name().toLowerCase(), e);
    }

    protected enum SaveError {
        WRITING,
        ENCODING,
    }

    protected enum LoadError {
        READING,
        JSON_PARSING,
        DECODING,
    }
}