Skip to content

Commit 5766823

Browse files
authored
Add unobfuscation support (#38)
Fabric decided to add a brand new gradle plugin which remobes the "remapJar" task and just calls it "jar". We can't have that, so I switched it out with "build" which should always be the same... Furthermore, access wideners got changed, so you need to write "official" instead of "named". And of course, preprocessing doesn't work for this use case, because the statement *has to be* in the first line of the access widener... I was first gonna add an "EmergencyTransformer" to allow for these kinds of shenanigans... But since I want to redo some logic for Disco 2 that adds multi modloader preprocessing, I thought I will do the quick and temporary setup by only preprocessing the .accesswidener files... I already do some exceptions for that with the comments...
2 parents a3998bf + 5ee8a03 commit 5766823

13 files changed

Lines changed: 153 additions & 9 deletions

File tree

gradle.properties

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
version=1.4
2-
release=false
3-
org.gradle.configuration-cache=true
1+
version=1.5
2+
release=false
3+
org.gradle.configuration-cache=true

src/main/java/com/minecrafttas/discombobulator/Discombobulator.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ public class Discombobulator implements Plugin<Project> {
8080
*/
8181
private static String discoVersion;
8282

83+
/**
84+
* Versions where the accessor is changed to official mappings
85+
*/
86+
public static List<String> accessWidenerOfficialList = new ArrayList<>();
87+
8388
/**
8489
* Apply the gradle plugin to the project
8590
*/
@@ -111,7 +116,7 @@ public void apply(Project project) {
111116
List<Task> compileTaskList = new ArrayList<>();
112117
Map<String, Path> buildDirs = new HashMap<>();
113118
for (Project subProject : project.getSubprojects()) {
114-
Task compileTask = subProject.getTasksByName("remapJar", false).iterator().next();
119+
Task compileTask = subProject.getTasksByName("build", false).iterator().next();
115120
compileTaskList.add(compileTask);
116121

117122
buildDirs.put(subProject.getName(), getBuildDir(subProject).resolve("libs"));
@@ -153,12 +158,16 @@ public void apply(Project project) {
153158
return;
154159
}
155160
List<String> versionStrings = new ArrayList<>(versionPairs.keySet());
156-
LinePreprocessor processor = new LinePreprocessor(versionStrings, config.getPatterns().get(), inverted);
161+
LinePreprocessor lineProcessor = new LinePreprocessor(versionStrings, config.getPatterns().get(), inverted);
157162

158163
List<String> ignored = config.getIgnoredFileFormats().getOrElse(new ArrayList<>());
159164
WildcardFileFilter fileFilter = WildcardFileFilter.builder().setWildcards(ignored).get();
160165

161-
fileProcessor = new FilePreprocessor(processor, fileFilter);
166+
// Map<String, Map<String, Pair<String, String>>> emergencyTransformConfig = config.getEmergencyTransform().getOrNull();
167+
// EmergencyTransformer emergencyTransformer = new EmergencyTransformer(emergencyTransformConfig, versionStrings);
168+
accessWidenerOfficialList = config.getAccessWidenerOfficial().get();
169+
170+
fileProcessor = new FilePreprocessor(lineProcessor, fileFilter);
162171

163172
// Yes this is yoinked from the gradle forums to get the disco version. Is there
164173
// a better method? Probably. Do I care? Currently, no.
@@ -172,7 +181,7 @@ public void apply(Project project) {
172181

173182
public static String getSplash() {
174183
return "\n" + (DISABLE_ANSI ? getColorLessSplash() : getColoredSplash()) + "\n\n"
175-
+ getCenterText(String.format("Enable configuration cache today!")) + "\n"
184+
+ getCenterText(String.format("Now with unobfuscated support!")) + "\n"
176185
+ getCenterText("Created by Pancake and Scribble") + "\n"
177186
+ getCenterText(discoVersion) + "\n\n";
178187

src/main/java/com/minecrafttas/discombobulator/config/PreprocessingConfiguration.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,16 @@ public abstract class PreprocessingConfiguration {
125125
* @return The line feed string
126126
*/
127127
public abstract Property<String> getDefaultLineFeed();
128+
129+
// /**
130+
// * <p>If a file needs transforming but does not support comments
131+
// * @return A map with the version number as key and a map as value which contains the filename and a pair of strings with search and replace respectively
132+
// */
133+
// public abstract MapProperty<String, Map<String, Pair<String, String>>> getEmergencyTransform();
134+
135+
/**
136+
* @return Versions where the accesswidener is changed to official mappings
137+
*/
138+
public abstract ListProperty<String> getAccessWidenerOfficial();
139+
128140
}

src/main/java/com/minecrafttas/discombobulator/processor/FilePreprocessor.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.minecrafttas.discombobulator.Discombobulator;
2121
import com.minecrafttas.discombobulator.tasks.TaskPreprocessWatch.CurrentFilePreprocessAction;
2222
import com.minecrafttas.discombobulator.utils.BetterFileWalker;
23+
import com.minecrafttas.discombobulator.utils.EmergencyTransformer;
2324
import com.minecrafttas.discombobulator.utils.LineFeedHelper;
2425
import com.minecrafttas.discombobulator.utils.SafeFileOperations;
2526

@@ -45,6 +46,7 @@ public class FilePreprocessor {
4546
* Creates a new {@link FilePreprocessor}
4647
* @param processor The {@link #lineProcessor}
4748
* @param fileFilter The {@link #fileFilter}
49+
* @param emergencyTransformer The {@link EmergencyTransformer}
4850
*/
4951
public FilePreprocessor(LinePreprocessor processor, WildcardFileFilter fileFilter) {
5052
this.lineProcessor = processor;
@@ -179,6 +181,7 @@ public CurrentFilePreprocessAction preprocessVersions(Path inFile, Map<String, P
179181
*/
180182
public List<String> preprocessLines(List<String> inLines, Path outFile, String version, String extension) throws Exception {
181183
List<String> lines = lineProcessor.preprocess(version, inLines, extension);
184+
182185
writeLines(lines, outFile);
183186
return lines;
184187
}

src/main/java/com/minecrafttas/discombobulator/processor/LinePreprocessor.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.util.regex.Matcher;
99
import java.util.regex.Pattern;
1010

11+
import com.minecrafttas.discombobulator.Discombobulator;
1112
import com.minecrafttas.discombobulator.utils.Pair;
1213

1314
/*
@@ -227,6 +228,9 @@ public List<String> preprocess(String targetVersion, List<String> lines, String
227228
out.add(line);
228229
}
229230

231+
//TODO Very dumb, very temporary method to fix accesswideners
232+
out = makeAccessWidenerOfficial(fileending, targetVersion, out);
233+
230234
return out;
231235
}
232236

@@ -1000,4 +1004,29 @@ private String searchPatternsInverted(int targetIndex, Map<String, String> patte
10001004
}
10011005
return replacement;
10021006
}
1007+
1008+
/**
1009+
* Very temporary method to support unobfuscated mcversions, by changing the "named" in the first line of the accesswidener to "official"
1010+
* if a version is declared in the build.gradle
1011+
*
1012+
* @param extension
1013+
* @param targetVersion
1014+
* @param lines
1015+
* @return
1016+
*/
1017+
private List<String> makeAccessWidenerOfficial(String extension, String targetVersion, List<String> lines) {
1018+
if (!"accesswidener".equals(extension)) {
1019+
return lines;
1020+
}
1021+
1022+
if (Discombobulator.accessWidenerOfficialList.contains(targetVersion)) {
1023+
String line = lines.get(0).replace("named", "official");
1024+
lines.set(0, line);
1025+
} else {
1026+
String line = lines.get(0).replace("official", "named");
1027+
lines.set(0, line);
1028+
}
1029+
1030+
return lines;
1031+
}
10031032
}

src/main/java/com/minecrafttas/discombobulator/tasks/TaskCollectBuilds.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import org.gradle.api.DefaultTask;
1111
import org.gradle.api.provider.MapProperty;
12+
import org.gradle.api.tasks.CacheableTask;
1213
import org.gradle.api.tasks.Input;
1314
import org.gradle.api.tasks.TaskAction;
1415

@@ -19,6 +20,7 @@
1920
*
2021
* @author Pancake
2122
*/
23+
@CacheableTask
2224
public abstract class TaskCollectBuilds extends DefaultTask {
2325

2426
/**

src/main/java/com/minecrafttas/discombobulator/tasks/TaskPreprocessBase.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import org.apache.commons.io.FilenameUtils;
1010
import org.gradle.api.DefaultTask;
11+
import org.gradle.api.tasks.CacheableTask;
1112
import org.gradle.api.tasks.TaskAction;
1213

1314
import com.minecrafttas.discombobulator.Discombobulator;
@@ -30,6 +31,7 @@
3031
*
3132
* @author Pancake, Scribble
3233
*/
34+
@CacheableTask
3335
public class TaskPreprocessBase extends DefaultTask {
3436

3537
@TaskAction

src/main/java/com/minecrafttas/discombobulator/tasks/TaskPreprocessVersion.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212
import org.apache.commons.io.FilenameUtils;
1313
import org.gradle.api.DefaultTask;
1414
import org.gradle.api.file.DirectoryProperty;
15+
import org.gradle.api.tasks.CacheableTask;
1516
import org.gradle.api.tasks.InputDirectory;
17+
import org.gradle.api.tasks.PathSensitive;
18+
import org.gradle.api.tasks.PathSensitivity;
1619
import org.gradle.api.tasks.TaskAction;
1720

1821
import com.minecrafttas.discombobulator.Discombobulator;
@@ -35,8 +38,10 @@
3538
*
3639
* @author Scribble
3740
*/
41+
@CacheableTask
3842
public abstract class TaskPreprocessVersion extends DefaultTask {
3943

44+
@PathSensitive(PathSensitivity.RELATIVE)
4045
@InputDirectory
4146
abstract DirectoryProperty getVersionDirectory();
4247

src/main/java/com/minecrafttas/discombobulator/tasks/TaskPreprocessVersionError.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.minecrafttas.discombobulator.tasks;
22

33
import org.gradle.api.DefaultTask;
4+
import org.gradle.api.tasks.CacheableTask;
45
import org.gradle.api.tasks.TaskAction;
56

67
/**
@@ -11,6 +12,7 @@
1112
* <p>But you can register separate tasks to the root project, that will execute before everything else, so to stop it,<br>
1213
* we just need to throw an exception and it will stop all following tasks. Thanks Gradle!
1314
*/
15+
@CacheableTask
1416
public class TaskPreprocessVersionError extends DefaultTask {
1517

1618
/**

src/main/java/com/minecrafttas/discombobulator/tasks/TaskPreprocessWatch.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.apache.commons.io.FilenameUtils;
2626
import org.gradle.api.DefaultTask;
2727
import org.gradle.api.provider.Property;
28+
import org.gradle.api.tasks.CacheableTask;
2829
import org.gradle.api.tasks.Input;
2930
import org.gradle.api.tasks.TaskAction;
3031

@@ -42,6 +43,7 @@
4243
*
4344
* @author Pancake, Scribble
4445
*/
46+
@CacheableTask
4547
public abstract class TaskPreprocessWatch extends DefaultTask {
4648

4749
private List<FileWatcherThread> threads = new ArrayList<>();
@@ -152,7 +154,7 @@ protected void onModifyFile(Path path) {
152154
// Get path relative to the root dir
153155
String extension = FilenameUtils.getExtension(path.getFileName().toString());
154156
try {
155-
System.out.println(String.format("[%s%s%s]", PURPLE_BRIGHT, TaskPreprocessWatch.findVersionFromPath(path, versions), WHITE));
157+
System.out.println(String.format("\n[%s%s%s]", PURPLE_BRIGHT, TaskPreprocessWatch.findVersionFromPath(path, versions), WHITE));
156158
// Preprocess in all sub versions
157159
currentFileAction = Discombobulator.fileProcessor.preprocessVersions(path, versions, extension, subSourceDir, true);
158160

0 commit comments

Comments
 (0)