Skip to content
This repository was archived by the owner on Sep 11, 2023. It is now read-only.

Commit 58882e9

Browse files
committed
disable ConfigEvent, sync VP 1.2.9
1 parent 399ae9f commit 58882e9

File tree

10 files changed

+134
-39
lines changed

10 files changed

+134
-39
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ org.gradle.jvmargs=-Xmx1G
88
loader_version=0.14.6
99

1010
# Mod Properties
11-
mod_version = 1.0.2-1.18.x
11+
mod_version = 1.0.3-1.18.x
1212
maven_group = org.localmc.tools
1313
archives_base_name = HardcodePatcher-Fabric
1414

src/main/java/org/localmc/tools/hardcodepatcher/HPConfigRegistrationCallback.java

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/main/java/org/localmc/tools/hardcodepatcher/HardcodePatcher.java

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,37 @@ public class HardcodePatcher implements ModInitializer {
2424
@Override
2525
public void onInitialize() {
2626
CommandRegistrationCallback.EVENT.register(CommandEventHandler::registerClientCommands);
27-
HPConfigRegistrationCallback.EVENT.register(this::loadConfig);
27+
28+
try {
29+
HardcodePatcherConfig.readConfig();
30+
List<String> mods = HardcodePatcherConfig.getMods();
31+
for (String mod : mods) {
32+
HardcodePatcherPatch vpp = new HardcodePatcherPatch(mod + ".json");
33+
try {
34+
vpp.read();
35+
vpps.add(vpp);
36+
} catch (IOException e) {
37+
e.printStackTrace();
38+
}
39+
}
40+
} catch (IOException e) {
41+
LOGGER.error("Failed to load config: ", e);
42+
throw new RuntimeException(e);
43+
}
44+
45+
//HardcodePatcherConfigRegistrationCallback.EVENT.register(this::loadConfig);
2846
}
2947

30-
public void loadConfig(HardcodePatcherPatch hpp, String json) {
31-
List<HardcodePatcherPatch> hpps = HardcodePatcher.vpps;
32-
json = ".json";
48+
/*
49+
public void loadConfig() {
3350
try {
3451
HardcodePatcherConfig.readConfig();
3552
List<String> mods = HardcodePatcherConfig.getMods();
3653
for (String mod : mods) {
37-
hpp = new HardcodePatcherPatch(mod + json);
54+
HardcodePatcherPatch vpp = new HardcodePatcherPatch(mod + ".json");
3855
try {
39-
hpp.read();
40-
hpps.add(hpp);
56+
vpp.read();
57+
vpps.add(vpp);
4158
} catch (IOException e) {
4259
e.printStackTrace();
4360
}
@@ -47,4 +64,5 @@ public void loadConfig(HardcodePatcherPatch hpp, String json) {
4764
throw new RuntimeException(e);
4865
}
4966
}
67+
*/
5068
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.localmc.tools.hardcodepatcher;
2+
3+
import net.fabricmc.fabric.api.event.Event;
4+
import net.fabricmc.fabric.api.event.EventFactory;
5+
6+
public interface HardcodePatcherConfigRegistrationCallback {
7+
Event<HardcodePatcherConfigRegistrationCallback> EVENT = EventFactory.createArrayBacked(HardcodePatcherConfigRegistrationCallback.class, (callbacks) -> () -> {
8+
for (HardcodePatcherConfigRegistrationCallback callback : callbacks) {
9+
callback.loadConfig();
10+
}
11+
});
12+
void loadConfig();
13+
}

src/main/java/org/localmc/tools/hardcodepatcher/HardcodePatcherUtils.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,41 @@ public static void addToExportList(String text) {
1818
public static boolean isInExportList(String text) {
1919
return exportList.lastIndexOf(text) != -1;
2020
}
21+
22+
private static int compare(String source, String target) {
23+
int n = source.length();
24+
int m = target.length();
25+
26+
if (n == 0) return m;
27+
if (m == 0) return n;
28+
29+
int[][] d = new int[n + 1][m + 1];
30+
int temp;
31+
32+
for (int i = 0; i <= n; i++) d[i][0] = i;
33+
for (int i = 0; i <= m; i++) d[0][i] = i;
34+
35+
for (int i = 1; i <= n; i++) {
36+
char ch1 = source.charAt(i - 1);
37+
for (int j = 1; j <= m; j++) {
38+
if (ch1 == target.charAt(j - 1)) {
39+
temp = 0;
40+
} else {
41+
temp = 1;
42+
}
43+
d[i][j] = min(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + temp);
44+
}
45+
}
46+
47+
return d[n][m];
48+
}
49+
50+
private static int min(int one, int two, int three) {
51+
return (one = one < two ? one : two) < three ? one : three;
52+
}
53+
54+
public static float getSimilarityRatio(String source, String target) {
55+
int max = Math.max(source.length(), target.length());
56+
return 1 - (float) compare(source, target) / max;
57+
}
2158
}

src/main/java/org/localmc/tools/hardcodepatcher/ThePatcher.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,8 @@
77
import java.util.Arrays;
88

99
public class ThePatcher {
10-
public ThePatcher() {
11-
}
12-
1310
public static String patch(String string, String method) {
14-
if (string == null || string.equals("") || string.isBlank()) {
11+
if (string == null || string.equals("")) {
1512
return string;
1613
}
1714

@@ -21,7 +18,10 @@ public static String patch(String string, String method) {
2118

2219
String ret;
2320
for (HardcodePatcherPatch vpp : HardcodePatcher.vpps) {
24-
StackTraceElement[] stacks = Thread.currentThread().getStackTrace();
21+
StackTraceElement[] stacks = null;
22+
if (!HardcodePatcherConfig.getOptimize().isDisableStacks()) {
23+
stacks = Thread.currentThread().getStackTrace();
24+
}
2525
ret = vpp.patch(string, stacks);
2626

2727
DebugMode debug = HardcodePatcherConfig.getDebugMode();

src/main/java/org/localmc/tools/hardcodepatcher/config/DebugMode.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ public class DebugMode {
1414

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

17+
private boolean testMode = false;
18+
1719
public boolean isEnable() {
1820
return isEnable;
1921
}
@@ -38,6 +40,14 @@ public void setOutputFormat(String outputFormat) {
3840
this.outputFormat = outputFormat;
3941
}
4042

43+
public boolean getTestMode() {
44+
return testMode;
45+
}
46+
47+
public void setTestMode(boolean testMode) {
48+
this.testMode = testMode;
49+
}
50+
4151
public void readJson(JsonReader reader) throws IOException {
4252
reader.beginObject();
4353
while (reader.peek() != JsonToken.END_OBJECT) {
@@ -51,6 +61,9 @@ public void readJson(JsonReader reader) throws IOException {
5161
case "output_mode":
5262
setOutputMode(reader.nextInt());
5363
break;
64+
case "test_mode":
65+
setTestMode(reader.nextBoolean());
66+
break;
5467
default:
5568
reader.skipValue();
5669
break;
@@ -64,19 +77,21 @@ public void writeJson(JsonWriter writer) throws IOException {
6477
writer.name("is_enable").value(isEnable());
6578
writer.name("output_format").value(getOutputFormat());
6679
writer.name("output_mode").value(getOutputMode());
80+
writer.name("test_mode").value(getTestMode());
6781
writer.endObject();
6882
}
6983

7084
@Override
7185
public boolean equals(Object o) {
7286
if (this == o) return true;
73-
if (!(o instanceof DebugMode debugMode)) return false;
74-
return isEnable() == debugMode.isEnable() && getOutputMode() == debugMode.getOutputMode() && Objects.equals(getOutputFormat(), debugMode.getOutputFormat());
87+
if (o == null || getClass() != o.getClass()) return false;
88+
DebugMode debugMode = (DebugMode) o;
89+
return isEnable() == debugMode.isEnable() && getOutputMode() == debugMode.getOutputMode() && getTestMode() == debugMode.getTestMode() && getOutputFormat().equals(debugMode.getOutputFormat());
7590
}
7691

7792
@Override
7893
public int hashCode() {
79-
return Objects.hash(isEnable(), getOutputMode(), getOutputFormat());
94+
return Objects.hash(isEnable(), getOutputMode(), getOutputFormat(), getTestMode());
8095
}
8196

8297
@Override
@@ -85,6 +100,7 @@ public String toString() {
85100
"isEnable=" + isEnable +
86101
", outputMode=" + outputMode +
87102
", outputFormat='" + outputFormat + '\'' +
103+
", testMode=" + testMode +
88104
'}';
89105
}
90106
}

src/main/java/org/localmc/tools/hardcodepatcher/config/HardcodePatcherPatch.java

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.google.gson.stream.JsonToken;
66
import org.localmc.tools.hardcodepatcher.HardcodePatcher;
77
import net.minecraft.client.resource.language.I18n;
8+
import org.localmc.tools.hardcodepatcher.HardcodePatcherUtils;
89

910
import java.io.FileInputStream;
1011
import java.io.IOException;
@@ -16,7 +17,6 @@
1617

1718
public class HardcodePatcherPatch {
1819
private static final Gson GSON = new Gson();
19-
private static boolean isSemimatch = false;
2020
private final Path patchFile;
2121
private Map<String, List<TranslationInfo>> map = new HashMap<>();
2222
private PatchInfo info = new PatchInfo();
@@ -81,21 +81,21 @@ public String patch(String text, StackTraceElement[] stackTrace) {
8181
if ((list = getList(text)) == null) return null;
8282

8383
for (TranslationInfo info : list) {
84-
isSemimatch = info.getValue().startsWith("@");
84+
boolean isSemimatch = info.getValue().startsWith("@");
8585
if (!isSemimatch && !text.equals(info.getKey())) continue;
8686
if (info.getValue() == null || info.getKey() == null || info.getKey().isEmpty() || info.getValue().isEmpty()) {
8787
continue;
8888
}
8989

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

9595
int index = targetClassInfo.getStackDepth();
9696
if (index >= stackTrace.length) continue;
9797
if (stackTrace[index].getClassName().contains(targetClassInfo.getName())) {
98-
return patchText(info.getValue(), info.getKey(), text);
98+
return patchText(info.getValue(), info.getKey(), text, isSemimatch);
9999
}
100100
}
101101

@@ -117,11 +117,21 @@ private boolean matchStack(String str, StackTraceElement[] stack) {
117117
return false;
118118
}
119119

120-
private String patchText(String value, String key, String text) {
120+
private String patchText(String value, String key, String text, boolean isSemimatch) {
121+
boolean isMarked = HardcodePatcherConfig.getDebugMode().getTestMode();
122+
boolean isSimilarity = isMarked && HardcodePatcherUtils.getSimilarityRatio(text, key) >= 0.5;
123+
System.out.println("Utils.getSimilarityRatio(text, key) = " + HardcodePatcherUtils.getSimilarityRatio(text, key));
121124
if (isSemimatch && !value.startsWith("@@")) {
122-
value = value.replace("@@", "@").substring(1);
123-
return text.replace(key, I18n.translate(value));
124-
} else return I18n.translate(value);
125+
String i18nValue = I18n.translate(value.replace("@@", "@").substring(1));
126+
if (isMarked) i18nValue = "§a[REPLACE MARKED]§f" + i18nValue;
127+
if (isSimilarity) i18nValue = "§b[SIMILAR MARKED]§f" + i18nValue;
128+
return text.replace(key, i18nValue);
129+
} else {
130+
String i18nValue = I18n.translate(value);
131+
if (isMarked) i18nValue = "§a[REPLACE MARKED]§f" + i18nValue;
132+
if (isSimilarity) i18nValue = "§b[SIMILAR MARKED]§f" + i18nValue;
133+
return i18nValue;
134+
}
125135
}
126136

127137

src/main/java/org/localmc/tools/hardcodepatcher/config/OptimizeParams.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,16 @@ public class OptimizeParams {
1010
private int stackMin = -1;
1111
private int stackMax = -1;
1212
private boolean disableExport = false;
13+
private boolean disableStacks = false;
1314

1415
public boolean isDisableExport() {
1516
return disableExport;
1617
}
1718

19+
public boolean isDisableStacks() {
20+
return disableStacks;
21+
}
22+
1823
public int getStackMin() {
1924
return stackMin;
2025
}
@@ -27,6 +32,10 @@ public void setDisableExport(boolean disableExport) {
2732
this.disableExport = disableExport;
2833
}
2934

35+
public void setDisableStacks(boolean disableStacks) {
36+
this.disableStacks = disableStacks;
37+
}
38+
3039
public void setStackMin(int stackMin) {
3140
this.stackMin = stackMin;
3241
}
@@ -42,6 +51,9 @@ public void readJson(JsonReader reader) throws IOException {
4251
case "disable_export":
4352
setDisableExport(reader.nextBoolean());
4453
break;
54+
case "disable_stacks":
55+
setDisableStacks(reader.nextBoolean());
56+
break;
4557
case "stack_min":
4658
setStackMin(reader.nextInt());
4759
break;
@@ -59,6 +71,7 @@ public void readJson(JsonReader reader) throws IOException {
5971
public void writeJson(JsonWriter writer) throws IOException {
6072
writer.beginObject();
6173
writer.name("disable_export").value(isDisableExport());
74+
writer.name("disable_stacks").value(isDisableStacks());
6275
writer.name("stack_min").value(getStackMin());
6376
writer.name("stack_max").value(getStackMax());
6477
writer.endObject();

src/main/java/org/localmc/tools/hardcodepatcher/mixin/BaseTextMixin.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public abstract class BaseTextMixin {
2828

2929
@ModifyArg(method = "asOrderedText", at = @At(value = "INVOKE", target = "Lnet/minecraft/util/Language;reorder(Lnet/minecraft/text/StringVisitable;)Lnet/minecraft/text/OrderedText;"))
3030
private StringVisitable proxy_asOrderedText(StringVisitable text1) {
31+
if (text1 instanceof TranslatableText) return text1;
3132
if (text1 instanceof LiteralText text) {
3233
String c = ThePatcher.patch(text.getString(), "BaseText#asOrderedText");
3334

@@ -39,6 +40,7 @@ private StringVisitable proxy_asOrderedText(StringVisitable text1) {
3940
}
4041
@Inject(method = "append", at = @At("HEAD"), cancellable = true)
4142
private void proxy_append(Text text2, CallbackInfoReturnable<MutableText> cir) {
43+
if (text2 instanceof TranslatableText) return;
4244
if (text2 instanceof LiteralText text) {
4345
String c = ThePatcher.patch(text.getString(), "BaseText#append");
4446

0 commit comments

Comments
 (0)