Skip to content

Commit 73a1fa4

Browse files
authored
feat: Allow remapping strings inside specified packages (@M41G)
Opt-in feature to allow specified packages to have string constants be remapped.
2 parents c094346 + 4dc472a commit 73a1fa4

6 files changed

Lines changed: 553 additions & 5 deletions

File tree

gui/src/main/java/org/mcphackers/mcp/gui/MenuBar.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,14 @@ public void reloadSide() {
186186

187187
public void reloadOptions() {
188188
for (Map.Entry<TaskParameter, JMenuItem> entry : optionItems.entrySet()) {
189-
entry.getValue().setSelected(mcp.options.getBooleanParameter(entry.getKey()));
189+
TaskParameter param = entry.getKey();
190+
JMenuItem item = entry.getValue();
191+
if (param.type == String[].class) {
192+
String[] arr = mcp.options.getStringArrayParameter(param);
193+
item.setSelected(arr != null && arr.length > 0);
194+
} else if (param.type == Boolean.class) {
195+
item.setSelected(mcp.options.getBooleanParameter(param));
196+
}
190197
}
191198
}
192199

@@ -222,6 +229,23 @@ private void initOptions() {
222229
mcp.options.setParameter(param, b.isSelected());
223230
mcp.options.save();
224231
});
232+
} else if (param.type == String[].class) {
233+
b = new JRadioButtonMenuItem();
234+
translatableComponents.put(b, "task.param." + param.name);
235+
optionItems.put(param, b);
236+
final JRadioButtonMenuItem checkbox = (JRadioButtonMenuItem) b;
237+
b.addActionListener(e -> {
238+
if (checkbox.isSelected()) {
239+
mcp.inputOptionsValue(param);
240+
String[] result = mcp.options.getStringArrayParameter(param);
241+
if (result == null || result.length == 0) {
242+
checkbox.setSelected(false);
243+
}
244+
} else {
245+
mcp.options.setParameter(param, new String[0]);
246+
mcp.options.save();
247+
}
248+
});
225249
} else {
226250
b = new JMenuItem(param.getDesc());
227251
b.addActionListener(u -> mcp.inputOptionsValue(param));

gui/src/main/java/org/mcphackers/mcp/main/MainGUI.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public class MainGUI extends MCP {
4646
public static final String[] TABS = {"task.decompile", "task.recompile", "task.reobfuscate", "task.build", "options.running"};
4747
public static final TaskParameter[][] TAB_PARAMETERS = {
4848
{TaskParameter.PATCHES, TaskParameter.FERNFLOWER_OPTIONS, TaskParameter.IGNORED_PACKAGES, TaskParameter.OUTPUT_SRC, TaskParameter.DECOMPILE_RESOURCES, TaskParameter.GUESS_GENERICS, TaskParameter.STRIP_GENERICS},
49-
{TaskParameter.SOURCE_VERSION, TaskParameter.TARGET_VERSION, TaskParameter.JAVA_HOME, TaskParameter.JAVAC_ARGS}, {TaskParameter.OBFUSCATION, TaskParameter.SRG_OBFUSCATION, TaskParameter.EXCLUDED_CLASSES, TaskParameter.STRIP_SOURCE_FILE},
49+
{TaskParameter.SOURCE_VERSION, TaskParameter.TARGET_VERSION, TaskParameter.JAVA_HOME, TaskParameter.JAVAC_ARGS}, {TaskParameter.OBFUSCATION, TaskParameter.SRG_OBFUSCATION, TaskParameter.EXCLUDED_CLASSES, TaskParameter.STRIP_SOURCE_FILE, TaskParameter.STRING_REMAP_PACKAGES},
5050
{TaskParameter.FULL_BUILD}, {TaskParameter.RUN_BUILD, TaskParameter.RUN_ARGS, TaskParameter.GAME_ARGS}
5151
};
5252
public Theme theme = Theme.THEMES_MAP.get(UIManager.getCrossPlatformLookAndFeelClassName());
@@ -293,10 +293,22 @@ public void changeWorkingDirectory() {
293293

294294
public void inputOptionsValue(TaskParameter param) {
295295
String s = MCP.TRANSLATOR.translateKey("options.enterValue");
296+
Object initialValue;
296297
if (param.type == String[].class) {
297298
s = MCP.TRANSLATOR.translateKey("options.enterValues") + "\n" + MCP.TRANSLATOR.translateKey("options.enterValues.info");
299+
String[] current = options.getStringArrayParameter(param);
300+
StringBuilder b = new StringBuilder();
301+
if (current != null) {
302+
for (int i = 0; i < current.length; i++) {
303+
if (i > 0) b.append(",");
304+
b.append(Util.convertToEscapedString(current[i]));
305+
}
306+
}
307+
initialValue = b.toString();
308+
} else {
309+
initialValue = Util.convertToEscapedString(String.valueOf(options.getParameter(param)));
298310
}
299-
String value = (String) JOptionPane.showInputDialog(frame, s, param.getDesc(), JOptionPane.PLAIN_MESSAGE, null, null, Util.convertToEscapedString(String.valueOf(options.getParameter(param))));
311+
String value = (String) JOptionPane.showInputDialog(frame, s, param.getDesc(), JOptionPane.PLAIN_MESSAGE, null, null, initialValue);
300312
safeSetParameter(param, value);
301313
options.save();
302314
}

src/main/java/org/mcphackers/mcp/tasks/TaskReobfuscate.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.mcphackers.mcp.tasks.mode.TaskParameter;
1919
import org.mcphackers.mcp.tools.FileUtil;
2020
import org.mcphackers.mcp.tools.injector.SourceFileTransformer;
21+
import org.mcphackers.mcp.tools.injector.StringLiteralRemapper;
2122
import org.mcphackers.mcp.tools.mappings.MappingUtil;
2223
import org.mcphackers.rdi.injector.data.ClassStorage;
2324
import org.mcphackers.rdi.injector.data.Mappings;
@@ -80,6 +81,10 @@ private void reobfuscate() throws IOException {
8081
RDInjector injector = new RDInjector(reobfBin);
8182
Mappings mappings = getMappings(injector.getStorage(), localSide);
8283
if (mappings != null) {
84+
String[] stringRemapPackages = mcp.getOptions().getStringArrayParameter(TaskParameter.STRING_REMAP_PACKAGES);
85+
if (stringRemapPackages != null && stringRemapPackages.length > 0) {
86+
new StringLiteralRemapper(mappings, stringRemapPackages).transform(injector.getStorage());
87+
}
8388
injector.applyMappings(mappings);
8489
}
8590
if (stripSourceFile) {

src/main/java/org/mcphackers/mcp/tasks/mode/TaskParameter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import org.mcphackers.mcp.Options;
99
import org.mcphackers.mcp.tasks.Task.Side;
1010

11-
1211
/**
1312
* Any optional parameters which can be accessed from tasks
1413
*
@@ -36,7 +35,8 @@ public enum TaskParameter {
3635
GUESS_GENERICS("generics", Boolean.class, false),
3736
STRIP_GENERICS("stripgenerics", Boolean.class, false),
3837
OUTPUT_SRC("outputsrc", Boolean.class, true),
39-
STRIP_SOURCE_FILE("stripsourcefile", Boolean.class, true);
38+
STRIP_SOURCE_FILE("stripsourcefile", Boolean.class, true),
39+
STRING_REMAP_PACKAGES("stringremap", String[].class, new String[0]);
4040

4141
public static final TaskParameter[] VALUES = TaskParameter.values();
4242

0 commit comments

Comments
 (0)