Skip to content

Commit

Permalink
修复config无法正常读取的Bug
Browse files Browse the repository at this point in the history
  • Loading branch information
CSneko committed Jan 8, 2025
1 parent 7e7935e commit 5b48708
Show file tree
Hide file tree
Showing 3 changed files with 270 additions and 8 deletions.
274 changes: 268 additions & 6 deletions common/src/main/java/org/cneko/toneko/common/util/ConfigBuilder.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@
package org.cneko.toneko.common.util;

import org.cneko.ctlib.common.file.YamlConfiguration;
import com.google.gson.Gson;
import org.cneko.ctlib.common.file.JsonConfiguration;
import org.jetbrains.annotations.Nullable;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.nodes.Tag;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.util.*;

import static org.cneko.toneko.common.Bootstrap.LOGGER;

public class ConfigBuilder {
private final Path path;
private YamlConfiguration config;
private YamlC config;
private final Map<String,Entry> defaults = new LinkedHashMap<>();
public ConfigBuilder(Path path){
this.path = path;
// 尝试读取文件
try {
config = new YamlConfiguration(path);
config = new YamlC(path);
} catch (Exception e) {
// 出现错误,创建一个空的配置文件
config = new YamlConfiguration("");
config = new YamlC("");
}
}

Expand Down Expand Up @@ -102,9 +105,9 @@ public ConfigBuilder build() {



public YamlConfiguration createConfig(){
public YamlC createConfig(){
try {
return YamlConfiguration.fromFile(path);
return new YamlC(path);
} catch (IOException e) {
return config;
}
Expand Down Expand Up @@ -183,6 +186,265 @@ public enum Types{
}
}

public static class YamlC{

private final Map<String, Object> data;
private final Path path;

public YamlC(Path path) throws IOException {
this.path = path;
if (Files.exists(path)) {
InputStream in = Files.newInputStream(path);

try {
// 使用 UTF-8 编码读取
InputStreamReader reader = new InputStreamReader(in, StandardCharsets.UTF_8);
Yaml yaml = new Yaml();
this.data = yaml.load(reader);
} catch (Throwable var6) {
try {
in.close();
} catch (Throwable var5) {
var6.addSuppressed(var5);
}

throw var6;
}

in.close();
} else {
this.data = new LinkedHashMap<>();
}
}

public YamlC(File file) throws IOException {
this(file.toPath());
}

public YamlC(String yamlContent) {
Yaml yaml = new Yaml();
this.data = yaml.load(yamlContent);
this.path = null;
}

public Object get(String path) {
String[] keys = path.split("\\.");
Map<String, Object> current = this.data;

for(int i = 0; i < keys.length - 1; ++i) {
current = (Map)current.get(keys[i]);
if (current == null) {
return null;
}
}

return current.get(keys[keys.length - 1]);
}

public void set(String path, Object value) {
String[] keys = path.split("\\.");
Map<String, Object> current = this.data;

for(int i = 0; i < keys.length - 1; ++i) {
current = (Map)current.computeIfAbsent(keys[i], (k) -> {
return new LinkedHashMap();
});
}

current.put(keys[keys.length - 1], value);

try {
this.save();
} catch (IOException var6) {
System.out.println(var6.getMessage());
}

}

public void save() throws IOException {
if (this.path != null) {
DumperOptions options = new DumperOptions();
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
Yaml yaml = new Yaml(options);
OutputStream out = Files.newOutputStream(this.path);

try {
yaml.dump(this.data, new OutputStreamWriter(out, StandardCharsets.UTF_8));
} catch (Throwable var7) {
if (out != null) {
try {
out.close();
} catch (Throwable var6) {
var7.addSuppressed(var6);
}
}

throw var7;
}

if (out != null) {
out.close();
}

}
}

public void save(Path targetPath) throws IOException {
DumperOptions options = new DumperOptions();
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
Yaml yaml = new Yaml(options);
OutputStream out = Files.newOutputStream(targetPath);

try {
yaml.dump(this.data, new OutputStreamWriter(out,StandardCharsets.UTF_8));
} catch (Throwable var8) {
if (out != null) {
try {
out.close();
} catch (Throwable var7) {
var8.addSuppressed(var7);
}
}

throw var8;
}

if (out != null) {
out.close();
}

}

public void save(File targetFile) throws IOException {
this.save(targetFile.toPath());
}

public String getString(String path) {
return (String)this.get(path);
}

public List<String> getStringList(String path) {
return (List)this.get(path);
}

public float getFloat(String path) {
Object value = this.get(path);
return value instanceof Float ? (Float)value : 0.0F;
}

public double getDouble(String path) {
Object value = this.get(path);
return value instanceof Double ? (Double)value : 0.0;
}

public int getInt(String path) {
Object value = this.get(path);
return value instanceof Integer ? (Integer)value : 0;
}

public boolean getBoolean(String path) {
Object value = this.get(path);
return value instanceof Boolean && (Boolean)value;
}

public boolean getBoolean(String path, boolean defValue) {
Object value = this.get(path);
return value instanceof Boolean ? (Boolean)value : defValue;
}

public boolean isSet(String path) {
return this.get(path) != null;
}

public boolean contains(String path) {
return this.isSet(path);
}

public ArrayList<Integer> getIntList(String path) {
Object value = this.get(path);
if (value instanceof List) {
ArrayList<Integer> list = new ArrayList();
Iterator var4 = ((List)value).iterator();

while(var4.hasNext()) {
Object obj = var4.next();
if (obj instanceof Integer) {
list.add((Integer)obj);
}
}

return list;
} else {
return new ArrayList();
}
}

public ArrayList<Double> getDoubleList(String path) {
Object value = this.get(path);
if (value instanceof List) {
ArrayList<Double> list = new ArrayList();
Iterator var4 = ((List)value).iterator();

while(var4.hasNext()) {
Object obj = var4.next();
if (obj instanceof Double) {
list.add((Double)obj);
}
}

return list;
} else {
return new ArrayList();
}
}

public ArrayList<Float> getFloatList(String path) {
Object value = this.get(path);
if (value instanceof List) {
ArrayList<Float> list = new ArrayList();
Iterator var4 = ((List)value).iterator();

while(var4.hasNext()) {
Object obj = var4.next();
if (obj instanceof Float) {
list.add((Float)obj);
}
}

return list;
} else {
return new ArrayList();
}
}

public ArrayList<Object> getList(String path) {
Object value = this.get(path);
return value instanceof List ? new ArrayList((List)value) : new ArrayList();
}

public String toString() {
DumperOptions options = new DumperOptions();
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
Yaml yaml = new Yaml(options);
return yaml.dump(this.data);
}

public boolean equals(Object obj) {
return obj.toString().equals(this.toString());
}

public boolean equalsCaseIgnoreCase(Object obj) {
return obj.toString() != null ? obj.toString().equalsIgnoreCase(this.toString()) : false;
}

public static JsonConfiguration toJson(String yamlContent) {
Yaml yaml = new Yaml();
Map<String, Object> yamlMap = (Map)yaml.load(yamlContent);
Gson gson = new Gson();
return JsonConfiguration.of(gson.toJson(yamlMap));
}

}

public static class YC {
private Map<String, Object> data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class ConfigUtil {
"启用统计功能,统计数据将发送到 toneko API,如何使用api请查看 https://s.cneko.org/toNekoOnlineAPI",
"Enable statistics, statistics data will be sent to the toneko API, how to use the api please see https://s.cneko.org/toNekoOnlineAPI")
.build();
public static YamlConfiguration CONFIG = CONFIG_BUILDER.createConfig();
public static ConfigBuilder.YamlC CONFIG = CONFIG_BUILDER.createConfig();

public static boolean IS_BIRTHDAY = false;
private static final int BIRTHDAY_MONTH = 9;
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=-Xmx2G
org.gradle.parallel=true

# Mod properties
mod_version = 1.4.6
mod_version = 1.4.7
maven_group = org.cneko
archives_name = toneko
enabled_platforms = fabric,neoforge
Expand Down

0 comments on commit 5b48708

Please sign in to comment.