Rename SimplyCompatExpectPlatform to SimplyCompatPlatform and update references
This commit is contained in:
parent
b614dc85a3
commit
877d5a2fec
8 changed files with 303 additions and 303 deletions
|
@ -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");
|
||||
}
|
||||
}
|
|
@ -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");
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
|
|
|
@ -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<String, String> config = new HashMap<>();
|
||||
private final ConfigRequest request;
|
||||
private boolean broken = false;
|
||||
public static final String FILE_EXTENSION = ".config";
|
||||
private final HashMap<String, String> 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<String, String> 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<String, String> 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<String, String> 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<String, String> getConfigMap() {
|
||||
return provider.getMap();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Add table
Reference in a new issue