Skip to content

Commit

Permalink
1.2.9
Browse files Browse the repository at this point in the history
  • Loading branch information
3093FengMing committed Apr 22, 2023
1 parent 1d88168 commit d8d59f2
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 15 deletions.
7 changes: 4 additions & 3 deletions src/main/java/me/fengming/vaultpatcher/ThePatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import java.util.Arrays;

public class ThePatcher {
public ThePatcher() {
}

public static String patch(String string, String method) {
if (string == null || string.equals("")) {
Expand All @@ -20,7 +18,10 @@ public static String patch(String string, String method) {

String ret;
for (VaultPatcherPatch vpp : Utils.vpps) {
StackTraceElement[] stacks = Thread.currentThread().getStackTrace();
StackTraceElement[] stacks = null;
if (!VaultPatcherConfig.getOptimize().isDisableStacks()) {
stacks = Thread.currentThread().getStackTrace();
}
ret = vpp.patch(string, stacks);

DebugMode debug = VaultPatcherConfig.getDebugMode();
Expand Down
37 changes: 37 additions & 0 deletions src/main/java/me/fengming/vaultpatcher/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,41 @@ public static void addToExportList(String text) {
public static boolean isInExportList(String text) {
return exportList.lastIndexOf(text) != -1;
}

private static int compare(String source, String target) {
int n = source.length();
int m = target.length();

if (n == 0) return m;
if (m == 0) return n;

int[][] d = new int[n + 1][m + 1];
int temp;

for (int i = 0; i <= n; i++) d[i][0] = i;
for (int i = 0; i <= m; i++) d[0][i] = i;

for (int i = 1; i <= n; i++) {
char ch1 = source.charAt(i - 1);
for (int j = 1; j <= m; j++) {
if (ch1 == target.charAt(j - 1)) {
temp = 0;
} else {
temp = 1;
}
d[i][j] = min(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + temp);
}
}

return d[n][m];
}

private static int min(int one, int two, int three) {
return (one = one < two ? one : two) < three ? one : three;
}

public static float getSimilarityRatio(String source, String target) {
int max = Math.max(source.length(), target.length());
return 1 - (float) compare(source, target) / max;
}
}
23 changes: 20 additions & 3 deletions src/main/java/me/fengming/vaultpatcher/config/DebugMode.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public class DebugMode {

private String outputFormat = "<source> -> <target>";

private boolean testMode = false;

public boolean isEnable() {
return isEnable;
}
Expand All @@ -38,6 +40,14 @@ public void setOutputFormat(String outputFormat) {
this.outputFormat = outputFormat;
}

public boolean getTestMode() {
return testMode;
}

public void setTestMode(boolean testMode) {
this.testMode = testMode;
}

public void readJson(JsonReader reader) throws IOException {
reader.beginObject();
while (reader.peek() != JsonToken.END_OBJECT) {
Expand All @@ -51,6 +61,9 @@ public void readJson(JsonReader reader) throws IOException {
case "output_mode":
setOutputMode(reader.nextInt());
break;
case "test_mode":
setTestMode(reader.nextBoolean());
break;
default:
reader.skipValue();
break;
Expand All @@ -64,19 +77,22 @@ public void writeJson(JsonWriter writer) throws IOException {
writer.name("is_enable").value(isEnable());
writer.name("output_format").value(getOutputFormat());
writer.name("output_mode").value(getOutputMode());
writer.name("test_mode").value(getTestMode());
writer.endObject();
}


@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof DebugMode debugMode)) return false;
return isEnable() == debugMode.isEnable() && getOutputMode() == debugMode.getOutputMode() && Objects.equals(getOutputFormat(), debugMode.getOutputFormat());
if (o == null || getClass() != o.getClass()) return false;
DebugMode debugMode = (DebugMode) o;
return isEnable() == debugMode.isEnable() && getOutputMode() == debugMode.getOutputMode() && getTestMode() == debugMode.getTestMode() && getOutputFormat().equals(debugMode.getOutputFormat());
}

@Override
public int hashCode() {
return Objects.hash(isEnable(), getOutputMode(), getOutputFormat());
return Objects.hash(isEnable(), getOutputMode(), getOutputFormat(), getTestMode());
}

@Override
Expand All @@ -85,6 +101,7 @@ public String toString() {
"isEnable=" + isEnable +
", outputMode=" + outputMode +
", outputFormat='" + outputFormat + '\'' +
", testMode=" + testMode +
'}';
}
}
13 changes: 13 additions & 0 deletions src/main/java/me/fengming/vaultpatcher/config/OptimizeParams.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@ public class OptimizeParams {
private int stackMin = -1;
private int stackMax = -1;
private boolean disableExport = false;
private boolean disableStacks = false;

public boolean isDisableExport() {
return disableExport;
}

public boolean isDisableStacks() {
return disableStacks;
}

public int getStackMin() {
return stackMin;
}
Expand All @@ -27,6 +32,10 @@ public void setDisableExport(boolean disableExport) {
this.disableExport = disableExport;
}

public void setDisableStacks(boolean disableStacks) {
this.disableStacks = disableStacks;
}

public void setStackMin(int stackMin) {
this.stackMin = stackMin;
}
Expand All @@ -42,6 +51,9 @@ public void readJson(JsonReader reader) throws IOException {
case "disable_export":
setDisableExport(reader.nextBoolean());
break;
case "disable_stacks":
setDisableStacks(reader.nextBoolean());
break;
case "stack_min":
setStackMin(reader.nextInt());
break;
Expand All @@ -59,6 +71,7 @@ public void readJson(JsonReader reader) throws IOException {
public void writeJson(JsonWriter writer) throws IOException {
writer.beginObject();
writer.name("disable_export").value(isDisableExport());
writer.name("disable_stacks").value(isDisableStacks());
writer.name("stack_min").value(getStackMin());
writer.name("stack_max").value(getStackMax());
writer.endObject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.gson.Gson;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import me.fengming.vaultpatcher.Utils;
import me.fengming.vaultpatcher.VaultPatcher;
import net.minecraft.client.resources.language.I18n;
import net.minecraftforge.fml.loading.FMLPaths;
Expand All @@ -17,7 +18,6 @@

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();
Expand Down Expand Up @@ -82,21 +82,21 @@ public String patch(String text, StackTraceElement[] stackTrace) {
if ((list = getList(text)) == null) return null;

for (TranslationInfo info : list) {
isSemimatch = info.getValue().startsWith("@");
boolean 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;
}

final TargetClassInfo targetClassInfo = info.getTargetClassInfo();
if (targetClassInfo.getName().isEmpty() || targetClassInfo.getStackDepth() <= 0 || matchStack(targetClassInfo.getName(), stackTrace)) {
return patchText(info.getValue(), info.getKey(), text);
if (stackTrace == null || targetClassInfo.getName().isEmpty() || targetClassInfo.getStackDepth() <= 0 || matchStack(targetClassInfo.getName(), stackTrace)) {
return patchText(info.getValue(), info.getKey(), text, isSemimatch);
}

int index = targetClassInfo.getStackDepth();
if (index >= stackTrace.length) continue;
if (stackTrace[index].getClassName().contains(targetClassInfo.getName())) {
return patchText(info.getValue(), info.getKey(), text);
return patchText(info.getValue(), info.getKey(), text, isSemimatch);
}
}

Expand All @@ -118,11 +118,21 @@ private boolean matchStack(String str, StackTraceElement[] stack) {
return false;
}

private String patchText(String value, String key, String text) {
private String patchText(String value, String key, String text, boolean isSemimatch) {
boolean isMarked = VaultPatcherConfig.getDebugMode().getTestMode();
boolean isSimilarity = isMarked && Utils.getSimilarityRatio(text, key) >= 0.5;
System.out.println("Utils.getSimilarityRatio(text, key) = " + Utils.getSimilarityRatio(text, key));
if (isSemimatch && !value.startsWith("@@")) {
value = value.replace("@@", "@").substring(1);
return text.replace(key, I18n.get(value));
} else return I18n.get(value);
String i18nValue = I18n.get(value.replace("@@", "@").substring(1));
if (isMarked) i18nValue = "§a[REPLACE MARKED]§f" + i18nValue;
if (isSimilarity) i18nValue = "§b[SIMILAR MARKED]§f" + i18nValue;
return text.replace(key, i18nValue);
} else {
String i18nValue = I18n.get(value);
if (isMarked) i18nValue = "§a[REPLACE MARKED]§f" + i18nValue;
if (isSimilarity) i18nValue = "§b[SIMILAR MARKED]§f" + i18nValue;
return i18nValue;
}
}


Expand Down

0 comments on commit d8d59f2

Please sign in to comment.