Skip to content

Commit

Permalink
Added several improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
RaphiMC committed Sep 21, 2024
1 parent f4abd96 commit b7b9e59
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public void onEnable() {
injectionClassLoader.getTransformerManager().addTransformerPreprocessor(new MixinsTranslator());
injectionClassLoader.getTransformerManager().addTransformer("net.raphimc.b2rplugin.mixins.MixinBlockMappings");
injectionClassLoader.getTransformerManager().addTransformer("net.raphimc.b2rplugin.mixins.MixinConfiguration");
injectionClassLoader.getTransformerManager().addTransformer("net.raphimc.b2rplugin.mixins.MixinLangStorage");
injectionClassLoader.getTransformerManager().addTransformer("net.raphimc.b2rplugin.mixins.MixinModernClient");
injectionClassLoader.getTransformerManager().addTransformer("net.raphimc.b2rplugin.mixins.MixinServer");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public abstract class MixinBlockMappings {

@Redirect(method = "init", at = @At(value = "INVOKE", target = "Ljava/io/File;<init>(Ljava/lang/String;)V"))
private static File redirectMappingsFile(String name) {
Beta2ReleasePlugin.ROOT_FOLDER.mkdirs();
return new File(Beta2ReleasePlugin.ROOT_FOLDER, name);
}

Expand Down
33 changes: 31 additions & 2 deletions src/main/java/net/raphimc/b2rplugin/mixins/MixinConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,47 @@
*/
package net.raphimc.b2rplugin.mixins;

import com.electronwill.nightconfig.core.file.CommentedFileConfig;
import com.github.dirtpowered.betatorelease.configuration.Configuration;
import com.llamalad7.mixinextras.sugar.Local;
import net.raphimc.b2rplugin.Beta2ReleasePlugin;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.Constant;
import org.spongepowered.asm.mixin.injection.ModifyConstant;
import org.spongepowered.asm.mixin.injection.*;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.util.Map;

@Mixin(Configuration.class)
public abstract class MixinConfiguration {

private static Map<String, Object> OVERRIDE_OPTIONS;

@ModifyConstant(method = "<init>", constant = @Constant(stringValue = "config.toml"))
private String redirectConfig(String filePath) {
OVERRIDE_OPTIONS = Map.of(
"bind-address", "127.0.0.1",
"bind-port", 25565,
"remote-address", "127.0.0.1",
"remote-port", 25565,
"haproxy-support", false
);

return Beta2ReleasePlugin.ROOT_FOLDER.toPath().resolve("config.toml").toString();
}

@Inject(method = "<init>", at = @At(value = "INVOKE", target = "Lcom/electronwill/nightconfig/core/file/CommentedFileConfig;save()V", shift = At.Shift.BEFORE))
private void removeUnsupportedOptions(CallbackInfo ci, @Local CommentedFileConfig config) {
OVERRIDE_OPTIONS.keySet().forEach(config::remove);
}

@Redirect(method = "<init>", at = @At(value = "INVOKE", target = "Lcom/electronwill/nightconfig/core/file/CommentedFileConfig;get(Ljava/lang/String;)Ljava/lang/Object;"))
private Object handleUnsupportedOptions(CommentedFileConfig config, String key) {
return OVERRIDE_OPTIONS.getOrDefault(key, config.get(key));
}

@Redirect(method = "<init>", at = @At(value = "INVOKE", target = "Lcom/electronwill/nightconfig/core/file/CommentedFileConfig;getOrElse(Ljava/lang/String;Ljava/lang/Object;)Ljava/lang/Object;"))
private Object handleUnsupportedOptions(CommentedFileConfig config, String key, Object fallback) {
return OVERRIDE_OPTIONS.getOrDefault(key, config.getOrElse(key, fallback));
}

}
43 changes: 43 additions & 0 deletions src/main/java/net/raphimc/b2rplugin/mixins/MixinLangStorage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* This file is part of ViaProxyBeta2Release - https://github.com/ViaVersionAddons/ViaProxyBeta2Release
* Copyright (C) 2024-2024 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.b2rplugin.mixins;

import com.github.dirtpowered.betatorelease.data.lang.LangStorage;
import net.raphimc.b2rplugin.Beta2ReleasePlugin;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;

@Mixin(LangStorage.class)
public abstract class MixinLangStorage {

@Redirect(method = "init", at = @At(value = "INVOKE", target = "Ljava/nio/file/Paths;get(Ljava/lang/String;[Ljava/lang/String;)Ljava/nio/file/Path;"))
private static Path redirectLangFile(String first, String... more) {
if (first.equals("lang")) {
final String other = String.join(File.separator, more);
return Beta2ReleasePlugin.ROOT_FOLDER.toPath().resolve(first).resolve(other);
} else {
return Paths.get(first, more);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,15 @@

import com.github.dirtpowered.betatorelease.network.session.Session;
import com.github.dirtpowered.betatorelease.proxy.ModernClient;
import com.github.steveice10.packetlib.Client;
import com.github.steveice10.packetlib.SessionFactory;
import com.github.steveice10.packetlib.packet.PacketProtocol;
import net.lenni0451.reflect.stream.RStream;
import net.raphimc.b2rplugin.session.ViaProxyModernSessionFactory;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(ModernClient.class)
public abstract class MixinModernClient {
Expand All @@ -37,9 +36,9 @@ public abstract class MixinModernClient {
@Final
private Session betaSession;

@Redirect(method = "connect", at = @At(value = "NEW", target = "(Ljava/lang/String;ILcom/github/steveice10/packetlib/packet/PacketProtocol;Lcom/github/steveice10/packetlib/SessionFactory;)Lcom/github/steveice10/packetlib/Client;"))
private Client redirectClientInit(String host, int port, PacketProtocol protocol, SessionFactory factory) {
return new Client(host, port, protocol, new ViaProxyModernSessionFactory(RStream.of(this.betaSession).fields().by("channel").get()));
@Inject(method = "getSessionFactory", at = @At("HEAD"), cancellable = true)
private void useViaProxySessionFactory(CallbackInfoReturnable<SessionFactory> cir) {
cir.setReturnValue(new ViaProxyModernSessionFactory(RStream.of(this.betaSession).fields().by("channel").get()));
}

}

0 comments on commit b7b9e59

Please sign in to comment.