-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from GiantLuigi4/1.7.7.0
TOML
- Loading branch information
Showing
7 changed files
with
554 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
127
src/main/java/turniplabs/halplibe/util/TomlConfigHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
27
src/main/java/turniplabs/halplibe/util/toml/CommentedEntry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
Oops, something went wrong.