Skip to content

Commit 22d9e5b

Browse files
committed
Add an easy way to reobfuscate multiple jars.
Example usage: https://gist.github.com/killjoy1221/c9487befa9cafd65054f
1 parent ad63e2f commit 22d9e5b

File tree

7 files changed

+422
-120
lines changed

7 files changed

+422
-120
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
* A Gradle plugin for the creation of Minecraft mods and MinecraftForge plugins.
3+
* Copyright (C) 2013 Minecraft Forge
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation; either
8+
* version 2.1 of the License, or (at your option) any later version.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18+
* USA
19+
*/
20+
package net.minecraftforge.gradle.user;
21+
22+
import java.io.File;
23+
import java.util.Collection;
24+
import java.util.List;
25+
26+
import org.gradle.api.file.FileCollection;
27+
28+
/**
29+
* Used to create a base interface for the reobf task and config classes so that
30+
* things can be copy-pasted between the two.
31+
*/
32+
public interface IReobfuscator
33+
{
34+
35+
/**
36+
* Gets the mappings file used to reobfuscate. It should be either a
37+
* {@link File} or a String path for a DelayedFile.
38+
*
39+
* @return The srg file or path to it
40+
*/
41+
Object getMappings();
42+
43+
/**
44+
* Sets the mappings file used to reobfuscate. It should be either a String
45+
* or {@link File}.
46+
*
47+
* @param srg The srg file or path to it
48+
*/
49+
void setMappings(Object srg);
50+
51+
/**
52+
* Sets the classpath used to reobfuscate. This is used by groovy for
53+
* simplicity. Use <code>classpath += otherClasspath</code> to add to it.
54+
*
55+
* @param classpath The new classpath
56+
*/
57+
void setClasspath(FileCollection classpath);
58+
59+
/**
60+
* Gets the classpath used to reobfuscate. Use
61+
* <code>classpath += otherClasspath</code> to add to it.
62+
*
63+
* @return The classpath
64+
*/
65+
FileCollection getClasspath();
66+
67+
/**
68+
* Gets the extra srg lines and files. Modders should prefer to use
69+
* {@link #extra(Object...)} or {@code extra += []} instead of setting the
70+
* list manually.
71+
*
72+
* @return The extra srg files or lines
73+
*/
74+
List<Object> getExtra();
75+
76+
/**
77+
* Sets the extra lines and files. Modders should prefer to use
78+
* {@link #extra(Object...)} or {@code extra += []} instead of setting the
79+
* list manually.
80+
*
81+
* @param extra The list of srgs
82+
*/
83+
void setExtra(List<Object> extra);
84+
85+
/**
86+
* Adds some additional srg files or lines for reobfuscating. Should be a
87+
* file or string path
88+
*
89+
* @param o The array to add
90+
*/
91+
void extra(Object... o);
92+
93+
/**
94+
* Adds a collection of additional srg files or lines for reobfuscating.
95+
*
96+
* @param o The collection to add
97+
*/
98+
void extra(Collection<Object> o);
99+
100+
/**
101+
* Sets the mappings to use Searge names. This is the default with the Forge
102+
* plugin.
103+
*
104+
* i.e. Minecraft.func_71410_x()
105+
*/
106+
void useSrgSrg();
107+
108+
/**
109+
* Sets the mappings to use Notch names. Useful for mods that want to be
110+
* able to run without Forge installed, such as libraries or hybrid mods.
111+
*
112+
* i.e. bsu.z()
113+
*/
114+
void useNotchSrg();
115+
116+
}
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
/*
2+
* A Gradle plugin for the creation of Minecraft mods and MinecraftForge plugins.
3+
* Copyright (C) 2013 Minecraft Forge
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation; either
8+
* version 2.1 of the License, or (at your option) any later version.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18+
* USA
19+
*/
20+
package net.minecraftforge.gradle.user;
21+
22+
import static net.minecraftforge.gradle.common.Constants.TASK_GENERATE_SRGS;
23+
24+
import java.util.Collection;
25+
import java.util.List;
26+
27+
import org.gradle.api.NamedDomainObjectFactory;
28+
import org.gradle.api.Task;
29+
import org.gradle.api.file.FileCollection;
30+
import org.gradle.api.tasks.bundling.Jar;
31+
32+
import net.minecraftforge.gradle.util.GradleConfigurationException;
33+
34+
public class ReobfTaskFactory implements NamedDomainObjectFactory<IReobfuscator>
35+
{
36+
private final UserBasePlugin<?> plugin;
37+
38+
public ReobfTaskFactory(UserBasePlugin<?> plugin)
39+
{
40+
this.plugin = plugin;
41+
}
42+
43+
@Override
44+
public IReobfuscator create(String jarName)
45+
{
46+
Task jar = plugin.project.getTasks().getByName(jarName);
47+
48+
if (!(jar instanceof Jar))
49+
{
50+
throw new GradleConfigurationException(jarName + " is not a jar task. Can only reobf jars!");
51+
}
52+
String name = "reobf" + Character.toUpperCase(jarName.charAt(0)) + jarName.substring(1);
53+
TaskSingleReobf task = plugin.maybeMakeTask(name, TaskSingleReobf.class);
54+
55+
task.setJar(((Jar) jar).getArchivePath());
56+
57+
task.dependsOn(TASK_GENERATE_SRGS, jar);
58+
task.mustRunAfter("test");
59+
60+
plugin.project.getTasks().getByName("build").dependsOn(task);
61+
plugin.project.getTasks().getByName("assemble").dependsOn(task);
62+
63+
plugin.setupReobf(task);
64+
65+
return new TaskWrapper(jarName, task);
66+
}
67+
68+
class TaskWrapper implements IReobfuscator
69+
{
70+
private final String name;
71+
private final IReobfuscator reobf;
72+
73+
public TaskWrapper(String name, IReobfuscator reobf)
74+
{
75+
this.name = name;
76+
this.reobf = reobf;
77+
}
78+
79+
public String getName()
80+
{
81+
return name;
82+
}
83+
84+
/**
85+
* Returns the instance of {@link TaskSingleReobf} that this object
86+
* wraps.
87+
*
88+
* @return The task
89+
*/
90+
public IReobfuscator getTask()
91+
{
92+
return reobf;
93+
}
94+
95+
@Override
96+
public boolean equals(Object obj)
97+
{
98+
if (obj instanceof TaskWrapper)
99+
{
100+
return name.equals(((TaskWrapper) obj).name);
101+
}
102+
return false;
103+
}
104+
105+
public Object getMappings()
106+
{
107+
return reobf.getMappings();
108+
}
109+
110+
public void setMappings(Object srg)
111+
{
112+
reobf.setMappings(srg);
113+
}
114+
115+
public void setClasspath(FileCollection classpath)
116+
{
117+
reobf.setClasspath(classpath);
118+
}
119+
120+
public FileCollection getClasspath()
121+
{
122+
return reobf.getClasspath();
123+
}
124+
125+
public List<Object> getExtra()
126+
{
127+
return reobf.getExtra();
128+
}
129+
130+
public void setExtra(List<Object> extra)
131+
{
132+
reobf.setExtra(extra);
133+
}
134+
135+
public void extra(Object... o)
136+
{
137+
reobf.extra(o);
138+
}
139+
140+
public void extra(Collection<Object> o)
141+
{
142+
reobf.extra(o);
143+
}
144+
145+
public void useSrgSrg()
146+
{
147+
reobf.useSrgSrg();
148+
}
149+
150+
public void useNotchSrg()
151+
{
152+
reobf.useNotchSrg();
153+
}
154+
}
155+
}

0 commit comments

Comments
 (0)