|
| 1 | +package net.tcpshield.tcpshield.fabric.impl; |
| 2 | + |
| 3 | +import net.fabricmc.loader.api.FabricLoader; |
| 4 | +import net.tcpshield.tcpshield.abstraction.TCPShieldConfig; |
| 5 | +import net.tcpshield.tcpshield.exception.TCPShieldInitializationException; |
| 6 | + |
| 7 | +import java.io.File; |
| 8 | +import java.io.IOException; |
| 9 | +import java.io.InputStream; |
| 10 | +import java.nio.file.Files; |
| 11 | +import java.nio.file.Path; |
| 12 | +import java.util.List; |
| 13 | + |
| 14 | +public class FabricConfigImpl extends TCPShieldConfig { |
| 15 | + |
| 16 | + private final Path configLocation = new File(FabricLoader.getInstance().getConfigDir().toString(), "tcpshield.yml").toPath(); |
| 17 | + |
| 18 | + public FabricConfigImpl() { |
| 19 | + if(!configExists()) { |
| 20 | + createDefaultConfig(); |
| 21 | + } |
| 22 | + |
| 23 | + ConfigData config = loadConfig(); |
| 24 | + |
| 25 | + this.onlyProxy = config.onlyAllowProxyConnections; |
| 26 | + this.ipWhitelistFolder = new File(FabricLoader.getInstance().getGameDirectory(), "ip-whitelist"); |
| 27 | + this.geyser = false; |
| 28 | + this.debug = config.debugMode; |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * @return whether the file specified in {@link FabricConfigImpl#configLocation} exists. |
| 33 | + */ |
| 34 | + private boolean configExists() { |
| 35 | + return Files.exists(configLocation); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Creates the default configuration file at the location specified in {@link FabricConfigImpl#configLocation}. |
| 40 | + * |
| 41 | + * <p> |
| 42 | + * This operation will always override an existing configuration file if it exists. |
| 43 | + * Callers should check {@link FabricConfigImpl#configExists()} if they wish to avoid this behavior. |
| 44 | + */ |
| 45 | + private void createDefaultConfig() { |
| 46 | + // Ensure the parent directory of our config file exists. |
| 47 | + // If it does not exist, attempt to create it now. |
| 48 | + Path parentDirectory = configLocation.getParent(); |
| 49 | + if (!Files.exists(parentDirectory) || !Files.isDirectory(parentDirectory)) { |
| 50 | + parentDirectory.toFile().mkdirs(); |
| 51 | + } |
| 52 | + |
| 53 | + // Copy the config.yml data from our mod jar to the loader config folder. |
| 54 | + try (InputStream in = getClass().getClassLoader().getResourceAsStream("config.yml")) { |
| 55 | + Files.copy(in, configLocation); |
| 56 | + } catch (Exception e) { |
| 57 | + throw new TCPShieldInitializationException(e); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + private ConfigData loadConfig() { |
| 62 | + try { |
| 63 | + List<String> strings = Files.readAllLines(configLocation); |
| 64 | + |
| 65 | + boolean onlyAllowProxyConnections = true; |
| 66 | + boolean debugMode = false; |
| 67 | + |
| 68 | + // Rudimentary config parsing |
| 69 | + for (String line : strings) { |
| 70 | + String[] keyValue = line.split(": "); |
| 71 | + |
| 72 | + // A config option will only be valid if it is in the format 'a: b'. |
| 73 | + // Ensure that is the case now. |
| 74 | + if (keyValue.length == 2) { |
| 75 | + String key = keyValue[0]; |
| 76 | + String value = keyValue[1]; |
| 77 | + |
| 78 | + switch (key) { |
| 79 | + case "only-allow-proxy-connections": |
| 80 | + onlyAllowProxyConnections = Boolean.parseBoolean(value); |
| 81 | + break; |
| 82 | + case "debug-mode": |
| 83 | + debugMode = Boolean.parseBoolean(value); |
| 84 | + break; |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + return new ConfigData(onlyAllowProxyConnections, debugMode); |
| 90 | + } catch (Exception e) { |
| 91 | + throw new TCPShieldInitializationException("Couldn't load config in config/tclshield.yml!"); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + private static class ConfigData { |
| 96 | + |
| 97 | + protected boolean onlyAllowProxyConnections; |
| 98 | + protected boolean debugMode; |
| 99 | + |
| 100 | + public ConfigData(boolean onlyAllowProxyConnections, boolean debugMode) { |
| 101 | + this.onlyAllowProxyConnections = onlyAllowProxyConnections; |
| 102 | + this.debugMode = debugMode; |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments