From 877d5a2fecf66db66d3a7d7d800bb8a1ef0b1571 Mon Sep 17 00:00:00 2001 From: Chris Toph Date: Sat, 1 Mar 2025 19:01:40 -0500 Subject: [PATCH] Rename SimplyCompatExpectPlatform to SimplyCompatPlatform and update references --- .../SimplyCompatExpectPlatform.java | 19 - .../simplycompat/SimplyCompatPlatform.java | 20 + .../simplycompat/config/ConfigProvider.java | 4 +- .../simplycompat/config/SimpleConfig.java | 460 +++++++++--------- .../SimplyCompatExpectPlatformImpl.java | 25 - .../fabric/SimplyCompatPlatformImpl.java | 25 + .../forge/SimplyCompatExpectPlatformImpl.java | 27 - .../forge/SimplyCompatPlatformImpl.java | 26 + 8 files changed, 303 insertions(+), 303 deletions(-) delete mode 100644 common/src/main/java/cc/toph/simplycompat/SimplyCompatExpectPlatform.java create mode 100644 common/src/main/java/cc/toph/simplycompat/SimplyCompatPlatform.java delete mode 100644 fabric/src/main/java/cc/toph/simplycompat/fabric/SimplyCompatExpectPlatformImpl.java create mode 100644 fabric/src/main/java/cc/toph/simplycompat/fabric/SimplyCompatPlatformImpl.java delete mode 100644 forge/src/main/java/cc/toph/simplycompat/forge/SimplyCompatExpectPlatformImpl.java create mode 100644 forge/src/main/java/cc/toph/simplycompat/forge/SimplyCompatPlatformImpl.java diff --git a/common/src/main/java/cc/toph/simplycompat/SimplyCompatExpectPlatform.java b/common/src/main/java/cc/toph/simplycompat/SimplyCompatExpectPlatform.java deleted file mode 100644 index 7601d19..0000000 --- a/common/src/main/java/cc/toph/simplycompat/SimplyCompatExpectPlatform.java +++ /dev/null @@ -1,19 +0,0 @@ -package cc.toph.simplycompat; - -import dev.architectury.injectables.annotations.ExpectPlatform; -import java.nio.file.Path; - -public class SimplyCompatExpectPlatform { - - @ExpectPlatform - public static Path getConfigDirectory() { - // Content will be replaced by platform (forge,fabric) implementation at runtime. - throw new AssertionError("This should never run"); - } - - @ExpectPlatform - public static String getVersion() { - // Content will be replaced by platform (forge,fabric) implementation at runtime. - throw new AssertionError("This should never run"); - } -} \ No newline at end of file diff --git a/common/src/main/java/cc/toph/simplycompat/SimplyCompatPlatform.java b/common/src/main/java/cc/toph/simplycompat/SimplyCompatPlatform.java new file mode 100644 index 0000000..27bda26 --- /dev/null +++ b/common/src/main/java/cc/toph/simplycompat/SimplyCompatPlatform.java @@ -0,0 +1,20 @@ +package cc.toph.simplycompat; + +import dev.architectury.injectables.annotations.ExpectPlatform; + +import java.nio.file.Path; + +public class SimplyCompatPlatform { + + @ExpectPlatform + public static Path getConfigDirectory() { + // Content will be replaced by platform (forge,fabric) implementation at runtime. + throw new AssertionError("This should never run"); + } + + @ExpectPlatform + public static String getVersion() { + // Content will be replaced by platform (forge,fabric) implementation at runtime. + throw new AssertionError("This should never run"); + } +} \ No newline at end of file diff --git a/common/src/main/java/cc/toph/simplycompat/config/ConfigProvider.java b/common/src/main/java/cc/toph/simplycompat/config/ConfigProvider.java index e6169fc..efe820e 100644 --- a/common/src/main/java/cc/toph/simplycompat/config/ConfigProvider.java +++ b/common/src/main/java/cc/toph/simplycompat/config/ConfigProvider.java @@ -1,6 +1,6 @@ package cc.toph.simplycompat.config; -import cc.toph.simplycompat.SimplyCompatExpectPlatform; +import cc.toph.simplycompat.SimplyCompatPlatform; import java.util.HashMap; @@ -11,7 +11,7 @@ public class ConfigProvider implements SimpleConfig.DefaultConfig { ## Simply Compat Config ## Version: %s - """, SimplyCompatExpectPlatform.getVersion()); + """, SimplyCompatPlatform.getVersion()); public ConfigProvider(String namespace) { this.namespace = namespace; diff --git a/common/src/main/java/cc/toph/simplycompat/config/SimpleConfig.java b/common/src/main/java/cc/toph/simplycompat/config/SimpleConfig.java index 37df534..60f24d8 100644 --- a/common/src/main/java/cc/toph/simplycompat/config/SimpleConfig.java +++ b/common/src/main/java/cc/toph/simplycompat/config/SimpleConfig.java @@ -22,7 +22,7 @@ package cc.toph.simplycompat.config; */ import cc.toph.simplycompat.SimplyCompat; -import cc.toph.simplycompat.SimplyCompatExpectPlatform; +import cc.toph.simplycompat.SimplyCompatPlatform; import java.io.File; import java.io.IOException; @@ -38,255 +38,255 @@ import static cc.toph.simplycompat.SimplyCompat.LOGGER; public class SimpleConfig { - public static final String FILE_EXTENSION = ".config"; - private final HashMap config = new HashMap<>(); - private final ConfigRequest request; - private boolean broken = false; + public static final String FILE_EXTENSION = ".config"; + private final HashMap config = new HashMap<>(); + private final ConfigRequest request; + private boolean broken = false; - private SimpleConfig(ConfigRequest request) { - this.request = request; - String identifier = "Config '" + request.filename + "'"; + private SimpleConfig(ConfigRequest request) { + this.request = request; + String identifier = "Config '" + request.filename + "'"; - if (!request.file.exists()) { - LOGGER.info("{} is missing, generating default one...", identifier); + if (!request.file.exists()) { + LOGGER.info("{} is missing, generating default one...", identifier); - try { - createConfig(); - } catch (IOException e) { - LOGGER.error("{} failed to generate!", identifier); - LOGGER.trace(e); - broken = true; - } - } + try { + createConfig(); + } catch (IOException e) { + LOGGER.error("{} failed to generate!", identifier); + LOGGER.trace(e); + broken = true; + } + } - if (!broken) { - try { - loadConfig(); - } catch (Exception e) { - LOGGER.error("{} failed to load!", identifier); - LOGGER.trace(e); - broken = true; - } - } + if (!broken) { + try { + loadConfig(); + } catch (Exception e) { + LOGGER.error("{} failed to load!", identifier); + LOGGER.trace(e); + broken = true; + } + } - } - - /** - * Creates new config request object, ideally `namespace` - * should be the name of the mod id of the requesting mod - * - * @param filename - name of the config file - * @return new config request object - */ - public static ConfigRequest of(String filename) { - // File - // Path path = SimplyCompatExpectPlatform.getConfigDirectory().resolve(filename + FILE_EXTENSION); - // Folder - Path path = SimplyCompatExpectPlatform.getConfigDirectory().resolve(String.format("%s/%s%s", SimplyCompat.MOD_ID, filename, FILE_EXTENSION)); - return new ConfigRequest(path.toFile(), filename); - } - - public void update() { - // If no provider or if we had an error, do nothing - if (request.provider == null || broken) return; - - boolean changed = false; - // For each provider key, see if our config HashMap is missing it - for (var entry : request.getConfigMap().entrySet()) { - String key = entry.getKey(); - String value = entry.getValue(); - // If it's missing from 'config', add it - if (!config.containsKey(key)) { - config.put(key, value); - changed = true; - } - } - - // If new keys were added, regen the file with createConfig() - if (changed) { - try { - delete(); - createConfig(); - } catch (IOException e) { - e.printStackTrace(); - } - } - } - - - private void createConfig() throws IOException { - - // try creating missing files - request.file.getParentFile().mkdirs(); - Files.createFile(request.file.toPath()); - - // write default config data - PrintWriter writer = new PrintWriter(request.file, StandardCharsets.UTF_8); - writer.write(request.getConfig()); - writer.close(); - - } - - private void loadConfig() throws IOException { - Scanner reader = new Scanner(request.file); - for (int line = 1; reader.hasNextLine(); line++) { - parseConfigEntry(reader.nextLine(), line); - } - } - - private void parseConfigEntry(String entry, int line) { - if (!entry.isEmpty() && !entry.startsWith("#")) { - String[] parts = entry.split("=", 2); - if (parts.length == 2) { - config.put(parts[0].trim(), parts[1].trim()); - } else { - throw new RuntimeException("Syntax error in config file on line " + line + "!"); - } - } - } - - /** - * Queries a value from config, returns `null` if the - * key does not exist. - * - * @return value corresponding to the given key - * @see SimpleConfig#getOrDefault - */ - @Deprecated - public String get(String key) { - return config.get(key); - } - - /** - * Returns string value from config corresponding to the given - * key, or the default string if the key is missing. - * - * @return value corresponding to the given key, or the default value - */ - public String getOrDefault(String key, String def) { - String val = get(key); - return val == null ? def : val; - } - - /** - * Returns integer value from config corresponding to the given - * key, or the default integer if the key is missing or invalid. - * - * @return value corresponding to the given key, or the default value - */ - public int getOrDefault(String key, int def) { - try { - return Integer.parseInt(get(key)); - } catch (Exception e) { - return def; - } - } - - /** - * Returns boolean value from config corresponding to the given - * key, or the default boolean if the key is missing. - * - * @return value corresponding to the given key, or the default value - */ - public boolean getOrDefault(String key, boolean def) { - String val = get(key); - if (val != null) { - return val.equalsIgnoreCase("true"); - } - - return def; - } - - /** - * Returns double value from config corresponding to the given - * key, or the default string if the key is missing or invalid. - * - * @return value corresponding to the given key, or the default value - */ - public double getOrDefault(String key, double def) { - try { - return Double.parseDouble(get(key)); - } catch (Exception e) { - return def; - } - } - - /** - * If any error occurred during loading or reading from the config - * a 'broken' flag is set, indicating that the config's state - * is undefined and should be discarded using `delete()` - * - * @return the 'broken' flag of the configuration - */ - public boolean isBroken() { - return broken; - } - - /** - * Deletes the config file from the filesystem - */ - public boolean delete() { - LOGGER.warn("Config '{}' will rise from the ashes! Please restart game.", request.filename); - return request.file.delete(); - } - - public interface DefaultConfig { - - static String empty(String namespace) { - return ""; - } - - String get(String namespace); - - default Map getMap() { - return new HashMap<>(); - } - } - - - public static class ConfigRequest { - - private final File file; - private final String filename; - private DefaultConfig provider; - - private ConfigRequest(File file, String filename) { - this.file = file; - this.filename = filename; - this.provider = DefaultConfig::empty; } /** - * Sets the default config provider, used to generate the - * config if it's missing. + * Creates new config request object, ideally `namespace` + * should be the name of the mod id of the requesting mod * - * @param provider default config provider - * @return current config request object - * @see DefaultConfig + * @param filename - name of the config file + * @return new config request object */ - public ConfigRequest provider(DefaultConfig provider) { - this.provider = provider; - return this; + public static ConfigRequest of(String filename) { + // File + // Path path = SimplyCompatExpectPlatform.getConfigDirectory().resolve(filename + FILE_EXTENSION); + // Folder + Path path = SimplyCompatPlatform.getConfigDirectory().resolve(String.format("%s/%s%s", SimplyCompat.MOD_ID, filename, FILE_EXTENSION)); + return new ConfigRequest(path.toFile(), filename); + } + + public void update() { + // If no provider or if we had an error, do nothing + if (request.provider == null || broken) return; + + boolean changed = false; + // For each provider key, see if our config HashMap is missing it + for (var entry : request.getConfigMap().entrySet()) { + String key = entry.getKey(); + String value = entry.getValue(); + // If it's missing from 'config', add it + if (!config.containsKey(key)) { + config.put(key, value); + changed = true; + } + } + + // If new keys were added, regen the file with createConfig() + if (changed) { + try { + delete(); + createConfig(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + + private void createConfig() throws IOException { + + // try creating missing files + request.file.getParentFile().mkdirs(); + Files.createFile(request.file.toPath()); + + // write default config data + PrintWriter writer = new PrintWriter(request.file, StandardCharsets.UTF_8); + writer.write(request.getConfig()); + writer.close(); + + } + + private void loadConfig() throws IOException { + Scanner reader = new Scanner(request.file); + for (int line = 1; reader.hasNextLine(); line++) { + parseConfigEntry(reader.nextLine(), line); + } + } + + private void parseConfigEntry(String entry, int line) { + if (!entry.isEmpty() && !entry.startsWith("#")) { + String[] parts = entry.split("=", 2); + if (parts.length == 2) { + config.put(parts[0].trim(), parts[1].trim()); + } else { + throw new RuntimeException("Syntax error in config file on line " + line + "!"); + } + } } /** - * Loads the config from the filesystem. + * Queries a value from config, returns `null` if the + * key does not exist. * - * @return config object - * @see SimpleConfig + * @return value corresponding to the given key + * @see SimpleConfig#getOrDefault */ - public SimpleConfig request() { - return new SimpleConfig(this); + @Deprecated + public String get(String key) { + return config.get(key); } - private String getConfig() { - return provider.get(filename) + "\n"; + /** + * Returns string value from config corresponding to the given + * key, or the default string if the key is missing. + * + * @return value corresponding to the given key, or the default value + */ + public String getOrDefault(String key, String def) { + String val = get(key); + return val == null ? def : val; } - private Map getConfigMap() { - return provider.getMap(); + /** + * Returns integer value from config corresponding to the given + * key, or the default integer if the key is missing or invalid. + * + * @return value corresponding to the given key, or the default value + */ + public int getOrDefault(String key, int def) { + try { + return Integer.parseInt(get(key)); + } catch (Exception e) { + return def; + } } - } + /** + * Returns boolean value from config corresponding to the given + * key, or the default boolean if the key is missing. + * + * @return value corresponding to the given key, or the default value + */ + public boolean getOrDefault(String key, boolean def) { + String val = get(key); + if (val != null) { + return val.equalsIgnoreCase("true"); + } + + return def; + } + + /** + * Returns double value from config corresponding to the given + * key, or the default string if the key is missing or invalid. + * + * @return value corresponding to the given key, or the default value + */ + public double getOrDefault(String key, double def) { + try { + return Double.parseDouble(get(key)); + } catch (Exception e) { + return def; + } + } + + /** + * If any error occurred during loading or reading from the config + * a 'broken' flag is set, indicating that the config's state + * is undefined and should be discarded using `delete()` + * + * @return the 'broken' flag of the configuration + */ + public boolean isBroken() { + return broken; + } + + /** + * Deletes the config file from the filesystem + */ + public boolean delete() { + LOGGER.warn("Config '{}' will rise from the ashes! Please restart game.", request.filename); + return request.file.delete(); + } + + public interface DefaultConfig { + + static String empty(String namespace) { + return ""; + } + + String get(String namespace); + + default Map getMap() { + return new HashMap<>(); + } + } + + + public static class ConfigRequest { + + private final File file; + private final String filename; + private DefaultConfig provider; + + private ConfigRequest(File file, String filename) { + this.file = file; + this.filename = filename; + this.provider = DefaultConfig::empty; + } + + /** + * Sets the default config provider, used to generate the + * config if it's missing. + * + * @param provider default config provider + * @return current config request object + * @see DefaultConfig + */ + public ConfigRequest provider(DefaultConfig provider) { + this.provider = provider; + return this; + } + + /** + * Loads the config from the filesystem. + * + * @return config object + * @see SimpleConfig + */ + public SimpleConfig request() { + return new SimpleConfig(this); + } + + private String getConfig() { + return provider.get(filename) + "\n"; + } + + private Map getConfigMap() { + return provider.getMap(); + } + + } } \ No newline at end of file diff --git a/fabric/src/main/java/cc/toph/simplycompat/fabric/SimplyCompatExpectPlatformImpl.java b/fabric/src/main/java/cc/toph/simplycompat/fabric/SimplyCompatExpectPlatformImpl.java deleted file mode 100644 index 26c6cfa..0000000 --- a/fabric/src/main/java/cc/toph/simplycompat/fabric/SimplyCompatExpectPlatformImpl.java +++ /dev/null @@ -1,25 +0,0 @@ -package cc.toph.simplycompat.fabric; - -import cc.toph.simplycompat.SimplyCompat; -import cc.toph.simplycompat.SimplyCompatExpectPlatform; -import net.fabricmc.loader.api.FabricLoader; - -import java.nio.file.Path; - -public class SimplyCompatExpectPlatformImpl { - /** - * Implementation of {@link SimplyCompatExpectPlatform#getConfigDirectory()}. - */ - public static Path getConfigDirectory() { - return FabricLoader.getInstance().getConfigDir(); - } - - /** - * Implementation of {@link SimplyCompatExpectPlatform#getConfigDirectory()}. - */ - public static String getVersion() { - return FabricLoader.getInstance().getModContainer(SimplyCompat.MOD_ID).get().getMetadata().getVersion().toString(); - } - - -} \ No newline at end of file diff --git a/fabric/src/main/java/cc/toph/simplycompat/fabric/SimplyCompatPlatformImpl.java b/fabric/src/main/java/cc/toph/simplycompat/fabric/SimplyCompatPlatformImpl.java new file mode 100644 index 0000000..39132f5 --- /dev/null +++ b/fabric/src/main/java/cc/toph/simplycompat/fabric/SimplyCompatPlatformImpl.java @@ -0,0 +1,25 @@ +package cc.toph.simplycompat.fabric; + +import cc.toph.simplycompat.SimplyCompat; +import cc.toph.simplycompat.SimplyCompatPlatform; +import net.fabricmc.loader.api.FabricLoader; + +import java.nio.file.Path; + +public class SimplyCompatPlatformImpl { + /** + * Implementation of {@link SimplyCompatPlatform#getConfigDirectory()}. + */ + public static Path getConfigDirectory() { + return FabricLoader.getInstance().getConfigDir(); + } + + /** + * Implementation of {@link SimplyCompatPlatform#getConfigDirectory()}. + */ + public static String getVersion() { + return FabricLoader.getInstance().getModContainer(SimplyCompat.MOD_ID).get().getMetadata().getVersion().toString(); + } + + +} \ No newline at end of file diff --git a/forge/src/main/java/cc/toph/simplycompat/forge/SimplyCompatExpectPlatformImpl.java b/forge/src/main/java/cc/toph/simplycompat/forge/SimplyCompatExpectPlatformImpl.java deleted file mode 100644 index e85f125..0000000 --- a/forge/src/main/java/cc/toph/simplycompat/forge/SimplyCompatExpectPlatformImpl.java +++ /dev/null @@ -1,27 +0,0 @@ -package cc.toph.simplycompat.forge; - -import cc.toph.simplycompat.SimplyCompat; -import cc.toph.simplycompat.SimplyCompatExpectPlatform; -import net.minecraftforge.fml.ModList; -import net.minecraftforge.fml.loading.FMLPaths; - -import java.nio.file.Path; - -public class SimplyCompatExpectPlatformImpl { - /** - * Implementation of {@link SimplyCompatExpectPlatform#getConfigDirectory()}. - */ - public static Path getConfigDirectory() { - return FMLPaths.CONFIGDIR.get(); - } - - /** - * Implementation of {@link SimplyCompatExpectPlatform#getConfigDirectory()}. - */ - public static String getVersion() { - return ModList.get().getModContainerById(SimplyCompat.MOD_ID).map(it -> it.getModInfo().getVersion().toString()).orElseThrow(); - } - - - -} \ No newline at end of file diff --git a/forge/src/main/java/cc/toph/simplycompat/forge/SimplyCompatPlatformImpl.java b/forge/src/main/java/cc/toph/simplycompat/forge/SimplyCompatPlatformImpl.java new file mode 100644 index 0000000..9b03f17 --- /dev/null +++ b/forge/src/main/java/cc/toph/simplycompat/forge/SimplyCompatPlatformImpl.java @@ -0,0 +1,26 @@ +package cc.toph.simplycompat.forge; + +import cc.toph.simplycompat.SimplyCompat; +import cc.toph.simplycompat.SimplyCompatPlatform; +import net.minecraftforge.fml.ModList; +import net.minecraftforge.fml.loading.FMLPaths; + +import java.nio.file.Path; + +public class SimplyCompatPlatformImpl { + /** + * Implementation of {@link SimplyCompatPlatform#getConfigDirectory()}. + */ + public static Path getConfigDirectory() { + return FMLPaths.CONFIGDIR.get(); + } + + /** + * Implementation of {@link SimplyCompatPlatform#getConfigDirectory()}. + */ + public static String getVersion() { + return ModList.get().getModContainerById(SimplyCompat.MOD_ID).map(it -> it.getModInfo().getVersion().toString()).orElseThrow(); + } + + +} \ No newline at end of file