Skip to content

Commit

Permalink
Merge pull request #24 from GiantLuigi4/1.7.7.0
Browse files Browse the repository at this point in the history
TOML
  • Loading branch information
MartinSVK12 authored Oct 30, 2023
2 parents 9efe34c + 8e6693f commit 7952828
Show file tree
Hide file tree
Showing 7 changed files with 554 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/main/java/turniplabs/halplibe/util/ConfigHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import java.util.Properties;

public class ConfigHandler {
private static final String CONFIG_DIRECTORY = FabricLoader.getInstance().getConfigDir().toString() + "/config/";
private static final String CONFIG_DIRECTORY = FabricLoader.getInstance().getGameDir().toString() + "/config/";
private final Properties defaultProperties;
private final Properties properties;
private String configFileName = "";
Expand Down
45 changes: 45 additions & 0 deletions src/main/java/turniplabs/halplibe/util/ConfigUpdater.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package turniplabs.halplibe.util;

import turniplabs.halplibe.util.toml.Toml;

import java.util.HashMap;

/**
* Mainly
*/
public abstract class ConfigUpdater {
Toml updating;

public static ConfigUpdater fromProperties(
String... text
) {
TomlConverter converter = new TomlConverter();
for (int i = 0; i < text.length; i += 2) {
converter.conversions.put(
text[i], text[i + 1]
);
}
return converter;
}

public abstract void update();

private static class TomlConverter extends ConfigUpdater {
HashMap<String, String> conversions = new HashMap<>();

@Override
public void update() {
// if it's a toml, then there's no point in updating
for (String orderedKey : updating.getOrderedKeys()) {
if (orderedKey.startsWith("."))
return;
}

for (String s : conversions.keySet()) {
String str = updating.get(s).toString();
updating.remove(s);
updating.addEntry(conversions.get(s), str);
}
}
}
}
127 changes: 127 additions & 0 deletions src/main/java/turniplabs/halplibe/util/TomlConfigHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package turniplabs.halplibe.util;

import net.fabricmc.loader.api.FabricLoader;
import turniplabs.halplibe.HalpLibe;
import turniplabs.halplibe.util.toml.Toml;
import turniplabs.halplibe.util.toml.TomlParser;

import java.io.*;

public class TomlConfigHandler {
private static final String CONFIG_DIRECTORY = FabricLoader.getInstance().getGameDir().toString() + "/config/";
private final Toml defaults;
private final Toml config;
private String configFileName = "";

private ConfigUpdater updater;

public TomlConfigHandler(String modID, Toml defaults) {
this(null, modID, defaults);
}

public TomlConfigHandler(ConfigUpdater updater, String modID, Toml defaults) {
this.updater = updater;
this.configFileName = modID + ".cfg";
this.defaults = defaults;
if (defaults.getComment().isPresent())
this.config = new Toml(defaults.getComment().get());
else this.config = new Toml();

// make sure the actual config has all the required entries
config.addMissing(defaults);

HalpLibe.LOGGER.info("Config file name: " + this.configFileName);

File configFile = new File(getFilePath());
HalpLibe.LOGGER.info("Config file path: " + configFile.getAbsolutePath());
try {
if (!configFile.exists()) {
HalpLibe.LOGGER.info("Config file does not exist. Creating...");
configFile.getParentFile().mkdirs();
configFile.createNewFile();
writeConfig();
} else {
// load only reads the entries in the file
loadConfig();
// ensure that new entries are written to the file
writeConfig();
}
} catch (IOException e) {
e.printStackTrace();
}
}

public String getFilePath() {
return CONFIG_DIRECTORY + configFileName;
}

public String getString(String key) {
Object o = this.config.get(key);
if (o == null) return null;
return o.toString();
}

public int getInt(String key) {
return this.config.get(key, Integer.class);
}

public long getLong(String key) {
return this.config.get(key, Long.class);
}

public float getFloat(String key) {
return this.config.get(key, Float.class);
}

public double getDouble(String key) {
return this.config.get(key, Double.class);
}

public boolean getBoolean(String key) {
return this.config.get(key, Boolean.class);
}

public void writeConfig() {
File configFile = new File(getFilePath());

// make sure the actual config has all the required entries
config.merge(defaults);

// write the config
try (OutputStream output = new FileOutputStream(configFile)) {
output.write(config.toString().getBytes());
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}

public void loadConfig() {
File configFile = new File(getFilePath());
// make sure the actual config has all the required entries
config.merge(defaults);
loadConfig(configFile, this.config);
}

private void loadConfig(File configFile, Toml properties) {
try (InputStream input = new FileInputStream(configFile)) {
// only loads the ones that it finds in the file
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while (true) {
byte[] buf = new byte[Math.max(2048, input.available())];
int count = input.read(buf);
if (count == -1) break;
baos.write(buf, 0, count);
}

Toml parsed = TomlParser.parse(baos.toString());
updater.updating = parsed;
updater.update();
properties.merge(true, parsed);

input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
27 changes: 27 additions & 0 deletions src/main/java/turniplabs/halplibe/util/toml/CommentedEntry.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package turniplabs.halplibe.util.toml;

public class CommentedEntry<T> extends Entry<T> {
String comment;

public String getComment() {
return comment;
}

public CommentedEntry(String comment, T t) {
super(t);
this.comment = comment;
}

public T getT() {
return t;
}

@Override
public String toString(String key) {
StringBuilder res = new StringBuilder();
for (String s : comment.split("\n"))
res.append("# ").append(s).append("\n");
res.append(super.toString(key));
return res.toString();
}
}
27 changes: 27 additions & 0 deletions src/main/java/turniplabs/halplibe/util/toml/Entry.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package turniplabs.halplibe.util.toml;

public class Entry<T> {
T t;
boolean string = false;

public Entry(T t) {
this.t = t;
string = t instanceof String;
}

public T getT() {
return t;
}

public String toString(String key) {
return key + " = " + this;
}

@Override
public String toString() {
if (string) {
return "\"" + t.toString() + "\"";
}
return t.toString();
}
}
Loading

0 comments on commit 7952828

Please sign in to comment.