From 8228f035494789d70183eaa3397cfd696d341f94 Mon Sep 17 00:00:00 2001
From: RaphiMC <50594595+RaphiMC@users.noreply.github.com>
Date: Wed, 1 Feb 2023 17:42:43 +0100
Subject: [PATCH] Fixed configs being reset to default on startup

---
 .../transformer/ConfigTransformer.java        | 40 +++++++++++++
 .../viaproxy/protocolhack/ConfigPatcher.java  | 58 +++++++++++++++++++
 .../viaproxy/protocolhack/ProtocolHack.java   | 35 ++++++++++-
 .../ViaProxyViaBackwardsPlatformImpl.java     | 40 -------------
 .../impl/ViaProxyViaRewindPlatformImpl.java   | 52 -----------------
 .../impl/ViaProxyViaVersionPlatformImpl.java  | 16 -----
 .../viaproxy/config_diff/viabackwards.yml     |  4 --
 .../assets/viaproxy/config_diff/viarewind.yml |  4 --
 .../viaproxy/config_diff/viaversion.yml       |  6 --
 src/main/resources/assets/viaproxy/dummy.yml  |  2 +
 10 files changed, 133 insertions(+), 124 deletions(-)
 create mode 100644 src/main/java/net/raphimc/viaproxy/injection/transformer/ConfigTransformer.java
 create mode 100644 src/main/java/net/raphimc/viaproxy/protocolhack/ConfigPatcher.java
 delete mode 100644 src/main/java/net/raphimc/viaproxy/protocolhack/impl/ViaProxyViaBackwardsPlatformImpl.java
 delete mode 100644 src/main/java/net/raphimc/viaproxy/protocolhack/impl/ViaProxyViaRewindPlatformImpl.java
 delete mode 100644 src/main/resources/assets/viaproxy/config_diff/viabackwards.yml
 delete mode 100644 src/main/resources/assets/viaproxy/config_diff/viarewind.yml
 delete mode 100644 src/main/resources/assets/viaproxy/config_diff/viaversion.yml
 create mode 100644 src/main/resources/assets/viaproxy/dummy.yml

diff --git a/src/main/java/net/raphimc/viaproxy/injection/transformer/ConfigTransformer.java b/src/main/java/net/raphimc/viaproxy/injection/transformer/ConfigTransformer.java
new file mode 100644
index 0000000..5a9d2c3
--- /dev/null
+++ b/src/main/java/net/raphimc/viaproxy/injection/transformer/ConfigTransformer.java
@@ -0,0 +1,40 @@
+/*
+ * This file is part of ViaProxy - https://github.com/RaphiMC/ViaProxy
+ * Copyright (C) 2023 RK_01/RaphiMC and contributors
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package net.raphimc.viaproxy.injection.transformer;
+
+import com.viaversion.viaversion.util.Config;
+import net.lenni0451.classtransform.annotations.CTarget;
+import net.lenni0451.classtransform.annotations.CTransformer;
+import net.lenni0451.classtransform.annotations.injection.CRedirect;
+import net.raphimc.viaproxy.protocolhack.ConfigPatcher;
+
+import java.util.Map;
+
+@CTransformer(Config.class)
+public abstract class ConfigTransformer {
+
+    @CRedirect(method = "loadConfig(Ljava/io/File;Ljava/net/URL;)Ljava/util/Map;", target = @CTarget(value = "INVOKE", target = "Ljava/util/Map;containsKey(Ljava/lang/Object;)Z"))
+    private boolean allowConfigPatching(final Map<String, Object> map, final Object key) {
+        if (((Object) this) instanceof ConfigPatcher) {
+            return true;
+        }
+
+        return map.containsKey(key);
+    }
+
+}
diff --git a/src/main/java/net/raphimc/viaproxy/protocolhack/ConfigPatcher.java b/src/main/java/net/raphimc/viaproxy/protocolhack/ConfigPatcher.java
new file mode 100644
index 0000000..6bee828
--- /dev/null
+++ b/src/main/java/net/raphimc/viaproxy/protocolhack/ConfigPatcher.java
@@ -0,0 +1,58 @@
+/*
+ * This file is part of ViaProxy - https://github.com/RaphiMC/ViaProxy
+ * Copyright (C) 2023 RK_01/RaphiMC and contributors
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package net.raphimc.viaproxy.protocolhack;
+
+import com.viaversion.viaversion.util.Config;
+
+import java.io.File;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+public class ConfigPatcher extends Config {
+
+    private final Map<String, Object> patches;
+
+    public ConfigPatcher(final File configFile, final Map<String, Object> patches) {
+        super(configFile);
+
+        this.patches = patches;
+        this.reloadConfig();
+    }
+
+    @Override
+    public URL getDefaultConfigURL() {
+        return ConfigPatcher.class.getClassLoader().getResource("assets/viaproxy/dummy.yml");
+    }
+
+    @Override
+    protected void handleConfig(Map<String, Object> config) {
+        for (final Map.Entry<String, Object> entry : this.patches.entrySet()) {
+            if (!config.containsKey(entry.getKey())) {
+                config.put(entry.getKey(), entry.getValue());
+            }
+        }
+    }
+
+    @Override
+    public List<String> getUnsupportedOptions() {
+        return new ArrayList<>();
+    }
+
+}
diff --git a/src/main/java/net/raphimc/viaproxy/protocolhack/ProtocolHack.java b/src/main/java/net/raphimc/viaproxy/protocolhack/ProtocolHack.java
index 8d11127..b53bd7e 100644
--- a/src/main/java/net/raphimc/viaproxy/protocolhack/ProtocolHack.java
+++ b/src/main/java/net/raphimc/viaproxy/protocolhack/ProtocolHack.java
@@ -18,18 +18,49 @@
 package net.raphimc.viaproxy.protocolhack;
 
 import net.raphimc.viaprotocolhack.ViaProtocolHack;
+import net.raphimc.viaprotocolhack.impl.platform.ViaBackwardsPlatformImpl;
 import net.raphimc.viaprotocolhack.impl.platform.ViaLegacyPlatformImpl;
+import net.raphimc.viaprotocolhack.impl.platform.ViaRewindPlatformImpl;
 import net.raphimc.viaproxy.plugins.PluginManager;
 import net.raphimc.viaproxy.plugins.events.ProtocolHackInitEvent;
-import net.raphimc.viaproxy.protocolhack.impl.*;
+import net.raphimc.viaproxy.protocolhack.impl.ViaAprilFoolsPlatformImpl;
+import net.raphimc.viaproxy.protocolhack.impl.ViaProxyVPLoader;
+import net.raphimc.viaproxy.protocolhack.impl.ViaProxyViaVersionPlatformImpl;
 
+import java.io.File;
+import java.util.HashMap;
+import java.util.Map;
 import java.util.function.Supplier;
 
 public class ProtocolHack {
 
     public static void init() {
+        patchConfigs();
         final Supplier<?>[] additionalPlatformSuppliers = PluginManager.EVENT_MANAGER.call(new ProtocolHackInitEvent(ViaAprilFoolsPlatformImpl::new)).getPlatformSuppliers().toArray(new Supplier[0]);
-        ViaProtocolHack.init(new ViaProxyViaVersionPlatformImpl(), new ViaProxyVPLoader(), null, null, ViaProxyViaBackwardsPlatformImpl::new, ViaProxyViaRewindPlatformImpl::new, ViaLegacyPlatformImpl::new, additionalPlatformSuppliers);
+        ViaProtocolHack.init(new ViaProxyViaVersionPlatformImpl(), new ViaProxyVPLoader(), null, null, ViaBackwardsPlatformImpl::new, ViaRewindPlatformImpl::new, ViaLegacyPlatformImpl::new, additionalPlatformSuppliers);
+    }
+
+    private static void patchConfigs() {
+        final File configFolder = new File("ViaProtocolHack");
+
+        final File viaVersionConfig = new File(configFolder, "viaversion.yml");
+        final Map<String, Object> viaVersionPatches = new HashMap<>();
+        viaVersionPatches.put("1_13-tab-complete-delay", 5);
+        viaVersionPatches.put("no-delay-shield-blocking", true);
+        viaVersionPatches.put("chunk-border-fix", true);
+        new ConfigPatcher(viaVersionConfig, viaVersionPatches);
+
+        final File viaBackwardsConfig = new File(configFolder, "config.yml");
+        final Map<String, Object> viaBackwardsPatches = new HashMap<>();
+        viaBackwardsPatches.put("fix-1_13-face-player", true);
+        viaBackwardsPatches.put("handle-pings-as-inv-acknowledgements", true);
+        new ConfigPatcher(viaBackwardsConfig, viaBackwardsPatches);
+
+        final File viaRewindConfig = new File(configFolder, "viarewind.yml");
+        final Map<String, Object> viaRewindPatches = new HashMap<>();
+        viaRewindPatches.put("replace-adventure", true);
+        viaRewindPatches.put("replace-particles", true);
+        new ConfigPatcher(viaRewindConfig, viaRewindPatches);
     }
 
 }
diff --git a/src/main/java/net/raphimc/viaproxy/protocolhack/impl/ViaProxyViaBackwardsPlatformImpl.java b/src/main/java/net/raphimc/viaproxy/protocolhack/impl/ViaProxyViaBackwardsPlatformImpl.java
deleted file mode 100644
index 130fdaa..0000000
--- a/src/main/java/net/raphimc/viaproxy/protocolhack/impl/ViaProxyViaBackwardsPlatformImpl.java
+++ /dev/null
@@ -1,40 +0,0 @@
-/*
- * This file is part of ViaProxy - https://github.com/RaphiMC/ViaProxy
- * Copyright (C) 2023 RK_01/RaphiMC and contributors
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
- */
-package net.raphimc.viaproxy.protocolhack.impl;
-
-import com.viaversion.viabackwards.ViaBackwardsConfig;
-import net.raphimc.viaprotocolhack.impl.platform.ViaBackwardsPlatformImpl;
-
-import java.io.File;
-import java.net.URL;
-
-public class ViaProxyViaBackwardsPlatformImpl extends ViaBackwardsPlatformImpl {
-
-    @Override
-    public void init(File dataFolder) {
-        new ViaBackwardsConfig(new File(dataFolder, "config.yml")) {
-            @Override
-            public URL getDefaultConfigURL() {
-                return ViaProxyViaVersionPlatformImpl.class.getClassLoader().getResource("assets/viaproxy/config_diff/viabackwards.yml");
-            }
-        }.reloadConfig();
-
-        super.init(dataFolder);
-    }
-
-}
diff --git a/src/main/java/net/raphimc/viaproxy/protocolhack/impl/ViaProxyViaRewindPlatformImpl.java b/src/main/java/net/raphimc/viaproxy/protocolhack/impl/ViaProxyViaRewindPlatformImpl.java
deleted file mode 100644
index 1e12eb6..0000000
--- a/src/main/java/net/raphimc/viaproxy/protocolhack/impl/ViaProxyViaRewindPlatformImpl.java
+++ /dev/null
@@ -1,52 +0,0 @@
-/*
- * This file is part of ViaProxy - https://github.com/RaphiMC/ViaProxy
- * Copyright (C) 2023 RK_01/RaphiMC and contributors
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
- */
-package net.raphimc.viaproxy.protocolhack.impl;
-
-import com.viaversion.viaversion.api.Via;
-import de.gerrygames.viarewind.api.ViaRewindConfigImpl;
-import de.gerrygames.viarewind.api.ViaRewindPlatform;
-import net.raphimc.viaprotocolhack.util.JLoggerToSLF4J;
-import org.slf4j.LoggerFactory;
-
-import java.io.File;
-import java.net.URL;
-import java.util.logging.Logger;
-
-public class ViaProxyViaRewindPlatformImpl implements ViaRewindPlatform {
-
-    private static final Logger LOGGER = new JLoggerToSLF4J(LoggerFactory.getLogger("ViaRewind"));
-
-    public ViaProxyViaRewindPlatformImpl() {
-        new ViaRewindConfigImpl(new File(Via.getPlatform().getDataFolder(), "viarewind.yml")) {
-            @Override
-            public URL getDefaultConfigURL() {
-                return ViaProxyViaVersionPlatformImpl.class.getClassLoader().getResource("assets/viaproxy/config_diff/viarewind.yml");
-            }
-        }.reloadConfig();
-
-        final ViaRewindConfigImpl config = new ViaRewindConfigImpl(new File(Via.getPlatform().getDataFolder(), "viarewind.yml"));
-        config.reloadConfig();
-        this.init(config);
-    }
-
-    @Override
-    public Logger getLogger() {
-        return LOGGER;
-    }
-
-}
diff --git a/src/main/java/net/raphimc/viaproxy/protocolhack/impl/ViaProxyViaVersionPlatformImpl.java b/src/main/java/net/raphimc/viaproxy/protocolhack/impl/ViaProxyViaVersionPlatformImpl.java
index 1b919de..a5feb00 100644
--- a/src/main/java/net/raphimc/viaproxy/protocolhack/impl/ViaProxyViaVersionPlatformImpl.java
+++ b/src/main/java/net/raphimc/viaproxy/protocolhack/impl/ViaProxyViaVersionPlatformImpl.java
@@ -17,13 +17,9 @@
  */
 package net.raphimc.viaproxy.protocolhack.impl;
 
-import com.viaversion.viaversion.configuration.AbstractViaConfig;
 import net.raphimc.viaprotocolhack.impl.platform.ViaVersionPlatformImpl;
-import net.raphimc.viaprotocolhack.impl.viaversion.VPViaConfig;
 import net.raphimc.viaproxy.cli.ConsoleFormatter;
 
-import java.io.File;
-import java.net.URL;
 import java.util.UUID;
 
 public class ViaProxyViaVersionPlatformImpl extends ViaVersionPlatformImpl {
@@ -37,16 +33,4 @@ public class ViaProxyViaVersionPlatformImpl extends ViaVersionPlatformImpl {
         super.sendMessage(uuid, ConsoleFormatter.convert(msg));
     }
 
-    @Override
-    protected AbstractViaConfig createConfig() {
-        new VPViaConfig(new File(this.getDataFolder(), "viaversion.yml")) {
-            @Override
-            public URL getDefaultConfigURL() {
-                return ViaProxyViaVersionPlatformImpl.class.getClassLoader().getResource("assets/viaproxy/config_diff/viaversion.yml");
-            }
-        }.reloadConfig();
-
-        return super.createConfig();
-    }
-
 }
diff --git a/src/main/resources/assets/viaproxy/config_diff/viabackwards.yml b/src/main/resources/assets/viaproxy/config_diff/viabackwards.yml
deleted file mode 100644
index 72558fb..0000000
--- a/src/main/resources/assets/viaproxy/config_diff/viabackwards.yml
+++ /dev/null
@@ -1,4 +0,0 @@
-#Overwrite default value
-fix-1_13-face-player: true
-#Overwrite default value
-handle-pings-as-inv-acknowledgements: true
diff --git a/src/main/resources/assets/viaproxy/config_diff/viarewind.yml b/src/main/resources/assets/viaproxy/config_diff/viarewind.yml
deleted file mode 100644
index 4eb05c6..0000000
--- a/src/main/resources/assets/viaproxy/config_diff/viarewind.yml
+++ /dev/null
@@ -1,4 +0,0 @@
-#Overwrite default value
-replace-adventure: true
-#Overwrite default value
-replace-particles: true
diff --git a/src/main/resources/assets/viaproxy/config_diff/viaversion.yml b/src/main/resources/assets/viaproxy/config_diff/viaversion.yml
deleted file mode 100644
index c35f9ca..0000000
--- a/src/main/resources/assets/viaproxy/config_diff/viaversion.yml
+++ /dev/null
@@ -1,6 +0,0 @@
-#Overwrite default value
-1_13-tab-complete-delay: 5
-#Overwrite default value
-no-delay-shield-blocking: true
-#Overwrite default value
-chunk-border-fix: true
diff --git a/src/main/resources/assets/viaproxy/dummy.yml b/src/main/resources/assets/viaproxy/dummy.yml
new file mode 100644
index 0000000..187843d
--- /dev/null
+++ b/src/main/resources/assets/viaproxy/dummy.yml
@@ -0,0 +1,2 @@
+#Dummy config
+this_is_a_dummy_config: true