forked from MinecraftForge/ForgeGradle
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTaskExtractExcModifiers.java
More file actions
159 lines (133 loc) · 4.64 KB
/
TaskExtractExcModifiers.java
File metadata and controls
159 lines (133 loc) · 4.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
* A Gradle plugin for the creation of Minecraft mods and MinecraftForge plugins.
* Copyright (C) 2013-2019 Minecraft Forge
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
* USA
*/
package net.minecraftforge.gradle.patcher;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import org.gradle.api.DefaultTask;
import org.gradle.api.tasks.InputFile;
import org.gradle.api.tasks.OutputFile;
import org.gradle.api.tasks.TaskAction;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import com.google.common.base.Charsets;
import com.google.common.io.ByteStreams;
import com.google.common.io.Files;
class TaskExtractExcModifiers extends DefaultTask
{
@InputFile
private Object inJar;
@OutputFile
private Object outExc;
/**
* Used to override the prefix matched in the jar for testing
* Default is for minecraft
*/
String matchingPrefix = "net/minecraft/";
//@formatter:off
public TaskExtractExcModifiers() { super(); }
//@formatter:on
@TaskAction
public void doStuff() throws IOException
{
File output = getOutExc();
File input = getInJar();
if (output.exists())
output.delete();
output.getParentFile().mkdirs();
output.createNewFile();
try (BufferedWriter writer = Files.newWriter(output, Charsets.UTF_8);
ZipInputStream zin = new ZipInputStream(new FileInputStream(input)))
{
ZipEntry entry;
while ((entry = zin.getNextEntry()) != null)
{
if (entry.isDirectory())
continue;
String entryName = entry.getName();
if (!entryName.endsWith(".class") || !entryName.startsWith(matchingPrefix))
continue;
getProject().getLogger().debug("Processing " + entryName);
byte[] entryData = ByteStreams.toByteArray(zin);
ClassReader cr = new ClassReader(entryData);
ClassVisitor ca = new GenerateMapClassAdapter(writer);
cr.accept(ca, 0);
}
}
}
private static class GenerateMapClassAdapter extends ClassVisitor
{
String className;
BufferedWriter writer;
public GenerateMapClassAdapter(BufferedWriter writer)
{
super(Opcodes.ASM5);
this.writer = writer;
}
@Override
public void visit(int version, int access, String name, String signature, String superName, String[] interfaces)
{
this.className = name;
super.visit(version, access, name, signature, superName, interfaces);
}
@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions)
{
if (name.equals("<clinit>"))
return super.visitMethod(access, name, desc, signature, exceptions);
String clsSig = this.className + "/" + name + desc;
try
{
if ((access & Opcodes.ACC_STATIC) == Opcodes.ACC_STATIC)
{
writer.write(clsSig);
writer.write("=static");
writer.newLine();
}
}
catch (IOException e)
{
throw new RuntimeException(e);
}
return super.visitMethod(access, name, desc, signature, exceptions);
}
}
public File getInJar()
{
return getProject().file(inJar);
}
public void setInJar(Object inJar)
{
this.inJar = inJar;
}
public File getOutExc()
{
return getProject().file(outExc);
}
public void setOutExc(Object outExc)
{
this.outExc = outExc;
}
}