implement custom file extensions and directories with appropriate tests, vbump to 1.0.4

This commit is contained in:
Draylar
2021-03-25 11:32:56 -05:00
parent 4e24988fa3
commit 9e9eac06fc
6 changed files with 57 additions and 5 deletions
@@ -147,7 +147,9 @@ public class OmegaConfig {
lines.forEach(str -> res.append(String.format("%s%n", str)));
try {
Files.write(getConfigPath(instance), res.toString().getBytes());
Path configPath = getConfigPath(instance);
configPath.toFile().getParentFile().mkdirs();
Files.write(configPath, res.toString().getBytes());
} catch (IOException ioException) {
LOGGER.error(ioException);
LOGGER.info(String.format("Write error, using default values for config %s.", configClass.toString()));
@@ -199,7 +201,7 @@ public class OmegaConfig {
}
public static Path getConfigPath(Config config) {
return Paths.get(FabricLoader.getInstance().getConfigDir().toString(), String.format("%s.json", config.getName()));
return Paths.get(FabricLoader.getInstance().getConfigDir().toString(), config.getDirectory(), String.format("%s.%s", config.getName(), config.getExtension()));
}
public static boolean configExists(Config config) {
@@ -78,7 +78,33 @@ public interface Config {
return null;
}
default boolean hasMenu() {
return true;
/**
* Returns the file extension of this config.
*
* <p>
* The file extension is used while serializing this config to a local file.
* The primary use-case of switching this would be supporting existing config files
* when porting from other json5 config libraries.
*
* @return the file extension of this config
*/
default String getExtension() {
return "json";
}
/**
* Returns the directory of this config, assuming the base directory is the instance config directory.
*
* <p>
* By default, a config such as 'my_config' will appear at /config/my_config.json.
* If this method specifies a directory, such as 'configurations',
* the config file will appear at /config/configurations/my_config.json.
*
* Nested directories can be specified by using a string such as 'configurations/client'.
*
* @return the directory of this config
*/
default String getDirectory() {
return "";
}
}