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
+1 -1
View File
@@ -8,7 +8,7 @@ yarn_mappings=1.16.5+build.5
loader_version=0.11.2 loader_version=0.11.2
# Mod Properties # Mod Properties
mod_version=1.0.3 mod_version=1.0.4
maven_group=draylar maven_group=draylar
archives_base_name=omega-config archives_base_name=omega-config
@@ -147,7 +147,9 @@ public class OmegaConfig {
lines.forEach(str -> res.append(String.format("%s%n", str))); lines.forEach(str -> res.append(String.format("%s%n", str)));
try { 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) { } catch (IOException ioException) {
LOGGER.error(ioException); LOGGER.error(ioException);
LOGGER.info(String.format("Write error, using default values for config %s.", configClass.toString())); 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) { 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) { public static boolean configExists(Config config) {
@@ -78,7 +78,33 @@ public interface Config {
return null; 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 "";
} }
} }
@@ -0,0 +1,18 @@
package draylar.omegatest;
import draylar.omegaconfig.api.Config;
public class NestedConfig implements Config {
public boolean test = false;
@Override
public String getName() {
return "nested";
}
@Override
public String getDirectory() {
return "test";
}
}
@@ -123,4 +123,9 @@ public class NewTestConfig implements Config {
public int killer_bunny_castle_seperation = 25; public int killer_bunny_castle_seperation = 25;
public int killer_bunny_castle_spacing = 50; public int killer_bunny_castle_spacing = 50;
} }
@Override
public String getExtension() {
return "json5";
}
} }
@@ -7,6 +7,7 @@ public class OmegaTestMain implements ModInitializer {
public static final TestConfig CONFIG = OmegaConfig.register(TestConfig.class); public static final TestConfig CONFIG = OmegaConfig.register(TestConfig.class);
public static final NewTestConfig MO_CONFIG = OmegaConfig.register(NewTestConfig.class); public static final NewTestConfig MO_CONFIG = OmegaConfig.register(NewTestConfig.class);
public static final NestedConfig NESTED = OmegaConfig.register(NestedConfig.class);
@Override @Override
public void onInitialize() { public void onInitialize() {