Made Via* config patching less hacky

This commit is contained in:
RaphiMC 2024-04-10 21:25:44 +02:00
parent 7b8bf9edc6
commit 1f7c0b6c15
No known key found for this signature in database
GPG Key ID: 0F6BB0657A03AC94
5 changed files with 29 additions and 118 deletions

View File

@ -1,38 +0,0 @@
/*
* This file is part of ViaFabricPlus - https://github.com/FlorianMichael/ViaFabricPlus
* Copyright (C) 2021-2024 FlorianMichael/EnZaXD <florian.michael07@gmail.com> and RK_01/RaphiMC
* Copyright (C) 2023-2024 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 de.florianmichael.viafabricplus.injection.mixin.viaversion;
import com.viaversion.viaversion.util.Config;
import de.florianmichael.viafabricplus.protocoltranslator.util.ConfigPatcher;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;
import java.util.Map;
@Mixin(value = Config.class, remap = false)
public abstract class MixinConfig {
@Redirect(method = "loadConfig(Ljava/io/File;Lcom/viaversion/viaversion/util/InputStreamSupplier;)Ljava/util/Map;", at = @At(value = "INVOKE", target = "Ljava/util/Map;containsKey(Ljava/lang/Object;)Z"))
private boolean allowConfigPatching(final Map<String, Object> map, final Object key) {
return ((Object) this) instanceof ConfigPatcher || map.containsKey(key);
}
}

View File

@ -42,7 +42,6 @@ import de.florianmichael.viafabricplus.protocoltranslator.impl.platform.ViaFabri
import de.florianmichael.viafabricplus.protocoltranslator.impl.viaversion.ViaFabricPlusVLInjector;
import de.florianmichael.viafabricplus.protocoltranslator.impl.viaversion.ViaFabricPlusVLLoader;
import de.florianmichael.viafabricplus.protocoltranslator.netty.ViaFabricPlusVLLegacyPipeline;
import de.florianmichael.viafabricplus.protocoltranslator.util.ConfigPatcher;
import de.florianmichael.viafabricplus.protocoltranslator.util.NoPacketSendChannel;
import io.netty.channel.Channel;
import io.netty.util.AttributeKey;
@ -60,10 +59,11 @@ import net.raphimc.vialoader.impl.platform.ViaBedrockPlatformImpl;
import org.cloudburstmc.netty.channel.raknet.config.RakChannelOption;
import java.io.File;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadLocalRandom;
@ -246,19 +246,31 @@ public class ProtocolTranslator {
* @param configFolder The directory where the ViaVersion config files are located
*/
public static void patchConfigs(final File configFolder) {
final File viaVersionConfig = new File(configFolder, "viaversion.yml");
final Map<String, Object> viaVersionPatches = new HashMap<>();
viaVersionPatches.put("fix-infested-block-breaking", false);
viaVersionPatches.put("shield-blocking", false);
viaVersionPatches.put("no-delay-shield-blocking", true);
viaVersionPatches.put("chunk-border-fix", true);
new ConfigPatcher(viaVersionConfig, viaVersionPatches);
configFolder.mkdirs();
final File viaLegacyConfig = new File(configFolder, "vialegacy.yml");
final Map<String, Object> viaLegacyPatches = new HashMap<>();
viaLegacyPatches.put("legacy-skull-loading", true);
viaLegacyPatches.put("legacy-skin-loading", true);
new ConfigPatcher(viaLegacyConfig, viaLegacyPatches);
try {
final File viaVersionConfig = new File(configFolder, "viaversion.yml");
Files.writeString(viaVersionConfig.toPath(), """
fix-infested-block-breaking: false
shield-blocking: false
no-delay-shield-blocking: true
chunk-border-fix: true
""", StandardOpenOption.CREATE_NEW);
} catch (FileAlreadyExistsException ignored) {
} catch (Throwable e) {
throw new RuntimeException("Failed to patch ViaVersion config", e);
}
try {
final File viaLegacyConfig = new File(configFolder, "vialegacy.yml");
Files.writeString(viaLegacyConfig.toPath(), """
legacy-skull-loading: true
legacy-skin-loading: true
""", StandardOpenOption.CREATE_NEW);
} catch (FileAlreadyExistsException ignored) {
} catch (Throwable e) {
throw new RuntimeException("Failed to patch ViaLegacy config", e);
}
}
/**

View File

@ -1,60 +0,0 @@
/*
* This file is part of ViaFabricPlus - https://github.com/FlorianMichael/ViaFabricPlus
* Copyright (C) 2021-2024 FlorianMichael/EnZaXD <florian.michael07@gmail.com> and RK_01/RaphiMC
* Copyright (C) 2023-2024 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 de.florianmichael.viafabricplus.protocoltranslator.util;
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.reload();
}
@Override
public URL getDefaultConfigURL() {
return ConfigPatcher.class.getClassLoader().getResource("assets/viafabricplus/dummy-config.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<>();
}
}

View File

@ -1,2 +0,0 @@
# Dummy file for the Config patcher features
nothing_to_see_here: true

View File

@ -175,6 +175,7 @@
"fixes.viaversion.MixinCommandBlockProvider",
"fixes.viaversion.MixinCommonBoss",
"fixes.viaversion.MixinEntityIdRewriter",
"fixes.viaversion.MixinEntityPacketRewriter1_20_3",
"fixes.viaversion.MixinEntityPackets1_17",
"fixes.viaversion.MixinEntityPackets1_19_4",
"fixes.viaversion.MixinEntityTracker1_9",
@ -200,9 +201,7 @@
"viabedrock.MixinJoinPackets",
"vialegacy.MixinExtensionProtocolMetadataStorage",
"vialegacy.MixinViaLegacyConfig",
"viaversion.MixinConfig",
"viaversion.MixinProtocolVersion",
"fixes.viaversion.MixinEntityPacketRewriter1_20_3"
"viaversion.MixinProtocolVersion"
],
"injectors": {
"defaultRequire": 1