Skip to content

Commit

Permalink
updater
Browse files Browse the repository at this point in the history
  • Loading branch information
GiantLuigi4 committed Oct 30, 2023
1 parent 4b0acec commit 8e6693f
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 11 deletions.
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);
}
}
}
}
22 changes: 11 additions & 11 deletions src/main/java/turniplabs/halplibe/util/TomlConfigHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,14 @@ public class TomlConfigHandler {
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())
Expand Down Expand Up @@ -48,16 +55,6 @@ public String getFilePath() {
return CONFIG_DIRECTORY + configFileName;
}

/**
* equivalent to {@link TomlConfigHandler#getString(String)}
* @param key the key of the property
* @return the property's value, as a string
*/
@Deprecated
public String getProperty(String key) {
return getString(key);
}

public String getString(String key) {
Object o = this.config.get(key);
if (o == null) return null;
Expand All @@ -67,6 +64,7 @@ public String getString(String key) {
public int getInt(String key) {
return this.config.get(key, Integer.class);
}

public long getLong(String key) {
return this.config.get(key, Long.class);
}
Expand Down Expand Up @@ -105,7 +103,7 @@ public void loadConfig() {
loadConfig(configFile, this.config);
}

private static void loadConfig(File configFile, Toml properties) {
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();
Expand All @@ -117,6 +115,8 @@ private static void loadConfig(File configFile, Toml properties) {
}

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

input.close();
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/turniplabs/halplibe/util/toml/Toml.java
Original file line number Diff line number Diff line change
Expand Up @@ -253,4 +253,24 @@ public void addMissing(Toml other) {
}
}
}

public void remove(String s) {
if (s.startsWith(".")) {
if (s.substring(1).contains(".")) {
categories.get(s.substring(1).split("\\.")[0])
.remove("." + s.substring(1).split("\\.")[1]);
return;
}
orderedKeys.remove(s);
categories.remove(s.substring(1));
} else {
if (s.contains(".")) {
categories.get(s.split("\\.")[0])
.remove(s.split("\\.")[1]);
return;
}
orderedKeys.remove(s);
entries.remove(s);
}
}
}

0 comments on commit 8e6693f

Please sign in to comment.