Skip to content

Commit

Permalink
1.2.6 clean up code
Browse files Browse the repository at this point in the history
  • Loading branch information
3093FengMing committed Jan 21, 2023
1 parent 4145232 commit b1a6590
Show file tree
Hide file tree
Showing 12 changed files with 90 additions and 78 deletions.
4 changes: 0 additions & 4 deletions src/main/java/me/fengming/vaultpatcher/VaultPatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,9 @@
import com.mojang.logging.LogUtils;
import me.fengming.vaultpatcher.config.VaultPatcherConfig;
import me.fengming.vaultpatcher.config.VaultPatcherPatch;
import net.minecraft.network.chat.Component;
import net.minecraft.server.Bootstrap;
import net.minecraftforge.eventbus.api.EventPriority;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import net.minecraftforge.fml.event.lifecycle.FMLConstructModEvent;
import org.slf4j.Logger;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.google.gson.reflect.TypeToken;
import com.mojang.brigadier.Command;
import com.mojang.brigadier.context.CommandContext;
import net.minecraft.client.Minecraft;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraftforge.fml.loading.FMLPaths;
Expand Down
24 changes: 16 additions & 8 deletions src/main/java/me/fengming/vaultpatcher/config/DebugMode.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ public class DebugMode {

private String outputFormat = "%s -> %s";

public void setEnable(boolean enable) {
isEnable = enable;
}

public boolean isEnable() {
return isEnable;
}

public void setEnable(boolean enable) {
isEnable = enable;
}

public int getOutputMode() {
return outputMode;
}
Expand All @@ -42,10 +42,18 @@ public void readJson(JsonReader reader) throws IOException {
reader.beginObject();
while (reader.peek() != JsonToken.END_OBJECT) {
switch (reader.nextName()) {
case "is_enable" : setEnable(reader.nextBoolean()); break;
case "output_format" : setOutputFormat(reader.nextString()); break;
case "output_mode" : setOutputMode(reader.nextInt()); break;
default : reader.skipValue(); break;
case "is_enable":
setEnable(reader.nextBoolean());
break;
case "output_format":
setOutputFormat(reader.nextString());
break;
case "output_mode":
setOutputMode(reader.nextInt());
break;
default:
reader.skipValue();
break;
}
}
reader.endObject();
Expand Down
45 changes: 27 additions & 18 deletions src/main/java/me/fengming/vaultpatcher/config/PatchInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,30 @@ public String getName() {
return name;
}

public String getDesc() {
return desc;
}

public String getMods() {
return mods;
}

public String getAuthors() {
return authors;
}


public void setName(String name) {
this.name = name;
}

public String getDesc() {
return desc;
}

public void setDesc(String desc) {
this.desc = desc;
}

public String getMods() {
return mods;
}

public void setMods(String mods) {
this.mods = mods;
}

public String getAuthors() {
return authors;
}

public void setAuthors(String authors) {
this.authors = authors;
}
Expand All @@ -53,11 +52,21 @@ public void readJson(JsonReader reader) throws IOException {
reader.beginObject();
while (reader.peek() != JsonToken.END_OBJECT) {
switch (reader.nextName()) {
case "name" : setName(reader.nextString()); break;
case "desc" : setDesc(reader.nextString()); break;
case "mods" : setMods(reader.nextString()); break;
case "authors" : setAuthors(reader.nextString()); break;
default : reader.skipValue(); break;
case "name":
setName(reader.nextString());
break;
case "desc":
setDesc(reader.nextString());
break;
case "mods":
setMods(reader.nextString());
break;
case "authors":
setAuthors(reader.nextString());
break;
default:
reader.skipValue();
break;
}
}
reader.endObject();
Expand Down
16 changes: 12 additions & 4 deletions src/main/java/me/fengming/vaultpatcher/config/TargetClassInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,18 @@ public void readJson(JsonReader reader) throws IOException {
reader.beginObject();
while (reader.peek() != JsonToken.END_OBJECT) {
switch (reader.nextName()) {
case "name" : setName(reader.nextString()); break;
case "mapping" : setMapping(reader.nextString()); break;
case "stack_depth" : setStackDepth(reader.nextInt()); break;
default : reader.skipValue(); break;
case "name":
setName(reader.nextString());
break;
case "mapping":
setMapping(reader.nextString());
break;
case "stack_depth":
setStackDepth(reader.nextInt());
break;
default:
reader.skipValue();
break;
}
}
reader.endObject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,20 @@
import com.google.gson.stream.JsonWriter;
import net.minecraftforge.fml.loading.FMLPaths;

import java.io.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
import java.util.ArrayList;
import java.util.List;

public class VaultPatcherConfig {
private static final Gson GSON = new Gson();
private static final Path configFile = FMLPaths.CONFIGDIR.get().resolve("vaultpatcher").resolve("config.json");
private static List<String> mods = new ArrayList<>();
private static DebugMode debug = new DebugMode();
private static final DebugMode debug = new DebugMode();

public static List<String> getMods() {
return mods;
Expand Down Expand Up @@ -48,17 +52,20 @@ public static void readConfig() throws IOException {
jr.beginObject();
while (jr.peek() != JsonToken.END_OBJECT) {
switch (jr.nextName()) {
case "debug_mode" :
case "debug_mode":
if (jr.peek() == JsonToken.BEGIN_OBJECT) {
debug.readJson(jr);
}
break;
case "mods" :
case "mods":
if (jr.peek() == JsonToken.BEGIN_ARRAY) {
mods = GSON.fromJson(jr, new TypeToken<List<String>>() {}.getType());
mods = GSON.fromJson(jr, new TypeToken<List<String>>() {
}.getType());
}
break;
default : jr.skipValue(); break;
default:
jr.skipValue();
break;
}
}
jr.endObject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.google.gson.stream.JsonToken;
import me.fengming.vaultpatcher.VaultPatcher;
import net.minecraft.client.resources.language.I18n;
import net.minecraft.server.Bootstrap;
import net.minecraftforge.fml.loading.FMLPaths;

import java.io.FileInputStream;
Expand All @@ -17,6 +16,12 @@

public class VaultPatcherPatch {
private static final Gson GSON = new Gson();
private static boolean isSemimatch = false;
private final Path patchFile;

private Map<String, List<TranslationInfo>> map = new HashMap<>();

private PatchInfo info = new PatchInfo();

public VaultPatcherPatch(String patchFile) {
VaultPatcher.LOGGER.info("Load Module " + patchFile);
Expand All @@ -30,12 +35,6 @@ public VaultPatcherPatch(String patchFile) {
this.patchFile = p;
}

private final Path patchFile;

private Map<String, List<TranslationInfo>> map = new HashMap<>();

private PatchInfo info = new PatchInfo();

private static <K, T> void addEntry(Map<K, List<T>> p, K key, T val) {
p.computeIfAbsent(key, k -> new ArrayList<>()).add(val);
}
Expand Down Expand Up @@ -66,7 +65,7 @@ public void readConfig() throws IOException {
readConfig(jsonReader);
}
}

private List<TranslationInfo> getList(String str) {
Set<String> set = map.keySet();
for (String s : set) {
Expand All @@ -77,16 +76,15 @@ private List<TranslationInfo> getList(String str) {
return null;
}

private static boolean isSemimatch = false;

public String patch(String text, StackTraceElement[] stackTrace) {
List<TranslationInfo> list;
if ((list = getList(text)) == null) return null;

for (TranslationInfo info : list) {
isSemimatch = info.getValue().startsWith("@");
if (!isSemimatch && !text.equals(info.getKey())) continue;
if (info.getValue() == null || info.getKey() == null || info.getKey().isEmpty() || info.getValue().isEmpty()) continue;
if (info.getValue() == null || info.getKey() == null || info.getKey().isEmpty() || info.getValue().isEmpty())
continue;
final TargetClassInfo targetClassInfo = info.getTargetClassInfo();
if (targetClassInfo.getName().isEmpty() || targetClassInfo.getStackDepth() <= 0 || matchStack(targetClassInfo.getName(), stackTrace)) {
return patchText(info.getValue(), info.getKey(), text);
Expand All @@ -103,9 +101,8 @@ public String patch(String text, StackTraceElement[] stackTrace) {

private boolean matchStack(String str, StackTraceElement[] stack) {
String s = str.toLowerCase();
List<StackTraceElement> stackTrace = Arrays.stream(stack).toList();
stackTrace = stackTrace.subList(7, stackTrace.size() - 13);
for (StackTraceElement ste : stackTrace) {
stack = Arrays.copyOfRange(stack, 7, 13);
for (StackTraceElement ste : stack) {
if (s.startsWith("#")) {
return ste.getClassName().endsWith(s);
} else if (s.startsWith("@")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,12 @@ private void proxy_getSiblings(CallbackInfoReturnable<List<Component>> cir) {
}
*/

@Shadow public abstract MutableComponent copy();
@Shadow
@Final
protected List<Component> siblings;

@Shadow @Final protected List<Component> siblings;
@Shadow
public abstract MutableComponent copy();

@ModifyArg(
method = "getVisualOrderText",
Expand Down
10 changes: 0 additions & 10 deletions src/main/java/me/fengming/vaultpatcher/mixin/FontMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,11 @@

import me.fengming.vaultpatcher.ThePatcher;
import net.minecraft.client.gui.Font;
import net.minecraft.network.chat.FormattedText;
import net.minecraft.network.chat.Style;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.util.FormattedCharSequence;
import net.minecraft.util.FormattedCharSink;
import net.minecraft.util.StringDecomposer;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.ModifyArg;
import org.spongepowered.asm.mixin.injection.ModifyVariable;

import java.util.Objects;
import java.util.Optional;

import static me.fengming.vaultpatcher.VaultPatcher.exportList;

@Mixin(value = Font.class, priority = Integer.MAX_VALUE)
public class FontMixin {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,12 @@

import me.fengming.vaultpatcher.ThePatcher;
import net.minecraft.network.chat.TextComponent;
import org.objectweb.asm.Opcodes;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import static me.fengming.vaultpatcher.VaultPatcher.exportList;

@Mixin(value = TextComponent.class, priority = Integer.MAX_VALUE)
public abstract class TextComponentMixin {

Expand All @@ -24,6 +19,7 @@ private void proxy_getContents(CallbackInfoReturnable<String> cir) {
String c = ThePatcher.patch(this.getText1());
if (c != null) cir.setReturnValue(c);
}

@Inject(method = "getText", at = @At("HEAD"), cancellable = true)
private void proxy_getText(CallbackInfoReturnable<String> cir) {
String c = ThePatcher.patch(this.getText1());
Expand Down
8 changes: 4 additions & 4 deletions src/main/resources/mixins.vaultpatcher.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
"package": "me.fengming.vaultpatcher.mixin",
"refmap": "mixins.vaultpatcher.refmap.json",
"mixins": [
"TextComponentMixin",
"BaseComponentMixin",
"FontMixin"
"FontMixin",
"TextComponentMixin"
],
"client": [
"BaseComponentMixin",
"TextComponentMixin",
"FontMixin"
"FontMixin",
"TextComponentMixin"
],
"server": [
"BaseComponentMixin",
Expand Down

0 comments on commit b1a6590

Please sign in to comment.