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

Commit 36064de

Browse files
committed
sync VP 1.2.8
1 parent 6c3d7c6 commit 36064de

22 files changed

+331
-147
lines changed

README-EN.md

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ An unofficial Fabric Port of Vault Patcher. Let the hard coded strings change in
88

99
[ZH-CN](README.md) / English
1010

11+
## ⚠Warning: Version 1.0.1 and below is no longer supported!
12+
1113
</div></center>
1214

1315
# Configs File
1416

1517
## Modular
1618

17-
the format in `config/HardcodePatcher/` directory like is `config.json` and several `module.json`. (Module files are currently required to exist in one or more of the Fabric versions)
19+
The format in `config/HardcodePatcher/` directory like is `config.json` and several `module.json`. (Module files are currently required to exist in one or more of the Fabric versions)
1820

1921
`config.json` must be provided.
2022
It is as follows:
@@ -27,6 +29,11 @@ It is as follows:
2729
"is_enable": false,
2830
"output_mode": 0,
2931
"output_format": "<source> -> <target>"
32+
},
33+
"optimize_params": {
34+
"disable_export": true,
35+
"stack_min": -1,
36+
"stack_max": -1
3037
}
3138
}
3239
```
@@ -36,24 +43,33 @@ It is as follows:
3643
Only in this way can `module.json` read and used normally.
3744

3845
### Debug Mode
39-
**(Tips: ONLY DEBUG)**
46+
Can be used when debugging and finding text.
4047

41-
`is_enable` determines whether the debugging feature is enabled.
42-
If it is `true`, a line of debugging information will be output to the log when replacing the string,
43-
The format of debugging information is `output_format`, and determines the content of debugging information by `output_format`.
44-
If it is `false`, Will not output anything to the log when replacing the string.
48+
`is_enable` determines whether the debugging feature is enabled. If it is `true`, a line of debugging information will be output to the log when replacing the string,
49+
The format of debugging information is `output_format`, and determines the content of debugging information by `output_mode`.
4550

4651
`output_format` determines the format of debugging information. The default is `<source> -> <target>`.
47-
`<source>` is the source string, that is, the string before replacement. and `<target>`is the replacement string.
48-
There is another `<stack>` that is not shown, if you include it in `output_format`,
49-
the log will be polluted because of the large amount of output information.
50-
`<stack>` is a stacktrace array, which is the `StackTrace` (including this mod) of the class of this string,
51-
This value is more for `stack_depth` and `name` in `target_class`.
52+
* `<source>`: Source string, the string before replacement.
53+
* `<target>`: The replacement string.
54+
* `<stack>`: Stacktrace array, which is the `StackTrace` (including this mod) of the class of this string.
55+
* `<method>`: The method called when this text rendering.
5256

5357
`output_mode` determines the content of debugging information.
5458
If it is 0, only the replaced string will be output,
5559
If it is 1, only the strings that are not replaced are output.
5660

61+
### Optimization Parameters
62+
63+
Optimize the parameters of the algorithm.
64+
65+
`disable_export` determines whether to disable the `export` feature, which is very useful for optimization.
66+
But at the same time, `/vaultpatcher export` will also be disabled.
67+
The default is false.
68+
69+
`stack_min` and `stack_max` determines the upper and lower limits of the stack trace array,
70+
and the optimization effect can be achieved by properly adjusting the parameters.
71+
The default is -1 (The upper and lower limits are not changed).
72+
5773
## Module
5874

5975
The format of the Module File is roughly as follows:

README.md

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ Vault Patcher 的非官方 Fabric 移植版。让一些mod中硬编码的字符
88

99
ZH-CN / [English](README-EN.md)
1010

11+
## ⚠警告:1.0.1及以下的版本不再受支持!
12+
1113
</div></center>
1214

1315
# 配置文件
@@ -27,6 +29,11 @@ ZH-CN / [English](README-EN.md)
2729
"is_enable": false,
2830
"output_mode": 0,
2931
"output_format": "<source> -> <target>"
32+
},
33+
"optimize_params": {
34+
"disable_export": true,
35+
"stack_min": -1,
36+
"stack_max": -1
3037
}
3138
}
3239
```
@@ -35,21 +42,32 @@ ZH-CN / [English](README-EN.md)
3542
`模块.json`才会被正常读取读取并使用。反之亦然。
3643

3744
### 调试模式(Debug Mode)
38-
**(Tips: 仅供调试使用)**
45+
可以在调试和查找文本时使用。
3946

40-
`is_enable`决定调试功能是否开启。
41-
若为`true`,那么会在替换字符串时向日志中输出一行调试信息,调试信息的格式由`output_format`决定,调试信息的内容由`output_mode`决定。
42-
若为`false`,则不会在替换字符串时向日志中输出任何内容。
47+
`is_enable`决定调试功能是否开启。开启时会在替换字符串时向日志中输出一行调试信息,调试信息的格式由`output_format`决定,调试信息的内容由`output_mode`决定。
48+
默认为 false。
4349

4450
`output_format`决定了调试信息的格式,默认为`<source> -> <target>`
45-
`<source>`为源内容,即未做替换前的字符串;`<target>`为做替换后的字符串。
46-
还有一个`<stack>`未展示出来,因为输出的很多,容易污染日志。
47-
`<stack>`为堆栈跟踪数组,是此字符串所在类的`StackTrace`(包括本mod),它更多是为`target_class`中的`stack_depth``name`服务的。
51+
允许占位符,支持的占位符如下:
52+
* `<source>`:源内容,即未做替换前的字符串。
53+
* `<target>`:替换后的字符串。
54+
* `<stack>`:堆栈跟踪数组,是此字符串所在类的`StackTrace`(包括本mod)。
55+
* `<method>`:该文本渲染所调用的方法。
4856

4957
`output_mode`决定了调试信息的内容。
5058
若为 0, 则仅输出替换的字符串;
5159
若为 1, 则仅输出不被替换的字符串。
5260

61+
### 优化参数(Optimization Parameters)
62+
63+
更改优化替换算法的参数。
64+
65+
`disable_export`决定是否禁用`export`功能,该选项对于优化有很大的作用。但同时`/vaultpatcher export`也将禁用。
66+
默认为 false。
67+
68+
`stack_min``stack_max`,决定了堆栈跟踪数组中的上限与下限,适当调整参数可以达到优化效果。
69+
默认均为 -1(即不更改上限下限)。
70+
5371
## 模块
5472

5573
模块的格式大概这样:

build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ archivesBaseName = project.archives_base_name
99
version = project.mod_version
1010
group = project.maven_group
1111

12+
13+
1214
repositories {
1315

1416
}

gradle.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ org.gradle.jvmargs=-Xmx1G
88
loader_version=0.14.6
99

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

1515
# Dependencies

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

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
public class HardcodePatcher implements ModInitializer {
1818
public static final String MODID = "hardcodepatcher";
19-
public static final String patchFileName = "hardcodepatcher.json";
19+
public static final String patchFileName = "langpatcher.json";
2020
public static final Path configPath = FabricLoader.getInstance().getConfigDir().resolve("HardcodePatcher");
2121
public static final Logger LOGGER = LogUtils.getLogger();
2222
public static List<HardcodePatcherPatch> vpps = new ArrayList<>();
@@ -31,7 +31,7 @@ public void onInitialize() {
3131
for (String mod : mods) {
3232
HardcodePatcherPatch vpp = new HardcodePatcherPatch(mod + ".json");
3333
try {
34-
vpp.readConfig();
34+
vpp.read();
3535
vpps.add(vpp);
3636
} catch (IOException e) {
3737
e.printStackTrace();
@@ -42,24 +42,4 @@ public void onInitialize() {
4242
throw new RuntimeException(e);
4343
}
4444
}
45-
/*
46-
public static void loadConfig() {
47-
try {
48-
HardcodeTextPatcherConfig.readConfig();
49-
List<String> mods = HardcodeTextPatcherConfig.getMods();
50-
for (String mod : mods) {
51-
HardcodeTextPatcherPatch vpp = new HardcodeTextPatcherPatch(mod + ".json");
52-
try {
53-
vpp.readConfig();
54-
vpps.add(vpp);
55-
} catch (IOException e) {
56-
e.printStackTrace();
57-
}
58-
}
59-
} catch (IOException e) {
60-
LOGGER.error("Failed to load config: ", e);
61-
throw new RuntimeException(e);
62-
}
63-
}
64-
*/
6545
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
public class HardcodePatcherUtils {
88
public static ArrayList<String> exportList = new ArrayList<>();
99
public static Map<String, Boolean> exported = new HashMap<>();
10+
1011
public static void addToExportList(String text) {
1112
if (!exported.getOrDefault(text, false) || !isInExportList(text)) {
1213
exported.put(text, true);
1314
exportList.add(text);
1415
}
1516
}
17+
1618
public static boolean isInExportList(String text) {
1719
return exportList.lastIndexOf(text) != -1;
1820
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.localmc.tools.hardcodepatcher;
2+
3+
import org.localmc.tools.hardcodepatcher.config.HardcodePatcherConfig;
4+
import org.localmc.tools.hardcodepatcher.config.HardcodePatcherPatch;
5+
6+
import java.io.IOException;
7+
import java.util.List;
8+
9+
public class ModuleEvent {
10+
static void loadConfig() {
11+
try {
12+
HardcodePatcherConfig.readConfig();
13+
List<String> mods = HardcodePatcherConfig.getMods();
14+
for (String mod : mods) {
15+
HardcodePatcherPatch vpp = new HardcodePatcherPatch(mod + ".json");
16+
try {
17+
vpp.read();
18+
HardcodePatcher.vpps.add(vpp);
19+
} catch (IOException e) {
20+
e.printStackTrace();
21+
}
22+
}
23+
} catch (IOException e) {
24+
HardcodePatcher.LOGGER.error("Failed to load config: ", e);
25+
throw new RuntimeException(e);
26+
}
27+
}
28+
}

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

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,38 +10,51 @@ public class ThePatcher {
1010
public ThePatcher() {
1111
}
1212

13-
public static String patch(String s) {
14-
if (s == null || s.isBlank()) {
15-
return s;
13+
public static String patch(String string, String method) {
14+
if (string == null || string.equals("") || string.isBlank()) {
15+
return string;
1616
}
17-
HardcodePatcherUtils.addToExportList(s);
18-
// HardcodeTextPatcher.LOGGER.info(Arrays.toString(Thread.currentThread().getStackTrace()));
17+
18+
if (!HardcodePatcherConfig.getOptimize().isDisableExport()) {
19+
HardcodePatcherUtils.addToExportList(string);
20+
}
21+
1922
String ret;
2023
for (HardcodePatcherPatch vpp : HardcodePatcher.vpps) {
2124
StackTraceElement[] stacks = Thread.currentThread().getStackTrace();
22-
ret = vpp.patch(s, stacks);
25+
ret = vpp.patch(string, stacks);
26+
2327
DebugMode debug = HardcodePatcherConfig.getDebugMode();
24-
String format = debug.getOutputFormat();
25-
if (ret != null && !ret.equals(s)) {
26-
if (debug.isEnable() && (debug.getOutputMode() == 1 || debug.getOutputMode() == 0)) {
27-
HardcodePatcher.LOGGER.info(
28-
format.replace("<source>", s)
29-
.replace("<target>", ret)
30-
.replace("<stack>", Arrays.toString(stacks))
31-
);
32-
}
33-
return ret;
34-
} else {
35-
if (debug.isEnable() && debug.getOutputMode() == 1) {
36-
HardcodePatcher.LOGGER.info(
37-
format.replace("<source>", s)
38-
.replace("<target>", s)
39-
.replace("<stack>", Arrays.toString(stacks))
40-
);
41-
}
42-
return s;
28+
29+
if (debug.isEnable()) {
30+
return outputDebugIndo(string, method, ret, stacks, debug);
31+
} else return ret;
32+
33+
}
34+
return string;
35+
}
36+
37+
private static String outputDebugIndo(String s, String m, String ret, StackTraceElement[] stacks, DebugMode debug) {
38+
String format = debug.getOutputFormat();
39+
if (ret != null && !ret.equals(s)) {
40+
if (debug.getOutputMode() == 1 || debug.getOutputMode() == 0) {
41+
HardcodePatcher.LOGGER.info(
42+
format.replace("<source>", s)
43+
.replace("<target>", ret)
44+
.replace("<method>", m)
45+
.replace("<stack>", Arrays.toString(stacks))
46+
);
47+
}
48+
} else {
49+
if (debug.getOutputMode() == 1) {
50+
HardcodePatcher.LOGGER.info(
51+
format.replace("<source>", s)
52+
.replace("<target>", s)
53+
.replace("<method>", m)
54+
.replace("<stack>", Arrays.toString(stacks))
55+
);
4356
}
4457
}
45-
return s;
58+
return ret;
4659
}
4760
}

src/main/java/org/localmc/tools/hardcodepatcher/command/CommandEventHandler.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ public class CommandEventHandler {
1010
public static void registerClientCommands(CommandDispatcher<ServerCommandSource> dispatcher, Boolean dedicated) {
1111
LiteralCommandNode<ServerCommandSource> cmd = dispatcher.register(
1212
CommandManager.literal(HardcodePatcher.MODID
13-
).then(
14-
CommandManager.literal("export")
13+
).then(
14+
CommandManager.literal("export")
1515
.executes(ExportCommand.instance)
16-
).then(
17-
CommandManager.literal("list")
16+
).then(
17+
CommandManager.literal("list")
1818
.executes(ListCommand.instance)
19-
).then(
20-
CommandManager.literal("reload")
19+
).then(
20+
CommandManager.literal("reload")
2121
.executes(ReloadCommand.instance)
2222
));
2323
dispatcher.register(CommandManager.literal("hp").redirect(cmd));

src/main/java/org/localmc/tools/hardcodepatcher/command/ExportCommand.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import net.minecraft.server.command.ServerCommandSource;
1010
import net.minecraft.text.TranslatableText;
1111
import org.localmc.tools.hardcodepatcher.HardcodePatcherUtils;
12+
import org.localmc.tools.hardcodepatcher.config.HardcodePatcherConfig;
1213

1314
import java.io.BufferedWriter;
1415
import java.io.FileWriter;
@@ -21,10 +22,15 @@ public class ExportCommand implements Command<ServerCommandSource> {
2122

2223
@Override
2324
public int run(CommandContext<ServerCommandSource> context) {
24-
context.getSource().sendFeedback(new TranslatableText("commands.hardcodepatcher.export.warning.wip"), true);
2525
Gson gson = new Gson();
2626
String json = gson.toJson(HardcodePatcherUtils.exportList, new TypeToken<ArrayList<String>>() {
2727
}.getType());
28+
29+
if (HardcodePatcherConfig.getOptimize().isDisableExport()) {
30+
context.getSource().sendError(new TranslatableText("commands.hardcodepatcher.export.tips.disabled"));
31+
return 1;
32+
}
33+
2834
//Export
2935
try {
3036
BufferedWriter bw = new BufferedWriter(
@@ -38,7 +44,7 @@ public int run(CommandContext<ServerCommandSource> context) {
3844
} catch (IOException e) {
3945
e.printStackTrace();
4046
}
41-
context.getSource().sendFeedback(new TranslatableText("commands.hardcodepatcher.export.tips.success"), true);
47+
context.getSource().sendFeedback(new TranslatableText("commands.hardcodepatcher.export.tips.success" + HardcodePatcher.patchFileName), true);
4248
return 0;
4349
}
4450
}

0 commit comments

Comments
 (0)