forked from MinecraftForge/ForgeGradle
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTaskExtractNew.java
More file actions
196 lines (166 loc) · 5.73 KB
/
TaskExtractNew.java
File metadata and controls
196 lines (166 loc) · 5.73 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
* 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.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import net.minecraftforge.gradle.util.SequencedInputSupplier;
import net.minecraftforge.srg2source.util.io.FolderSupplier;
import net.minecraftforge.srg2source.util.io.InputSupplier;
import net.minecraftforge.srg2source.util.io.ZipInputSupplier;
import org.gradle.api.DefaultTask;
import org.gradle.api.file.FileCollection;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.InputFiles;
import org.gradle.api.tasks.Optional;
import org.gradle.api.tasks.OutputFile;
import org.gradle.api.tasks.TaskAction;
import com.google.common.base.Strings;
import com.google.common.collect.Sets;
import com.google.common.io.ByteStreams;
/**
* The point of this task is to take 2 input sets, and then build a zip/jar containing the files that exist in the 2nd set, but not the first.
*/
class TaskExtractNew extends DefaultTask
{
//@formatter:off
private final List<Object> clean = new LinkedList<Object>();
private final List<Object> dirty = new LinkedList<Object>();
@Input @Optional private String ending;
@OutputFile private Object output;
//@formatter:on
//@formatter:off
public TaskExtractNew() { super(); }
//@formatter:on
@TaskAction
public void doStuff() throws IOException
{
ending = Strings.nullToEmpty(ending);
try (InputSupplier cleanSupplier = getSupplier(getCleanSource());
InputSupplier dirtySupplier = getSupplier(getDirtySource()))
{
Set<String> cleanFiles = Sets.newHashSet(cleanSupplier.gatherAll(ending));
File output = getOutput();
output.getParentFile().mkdirs();
boolean isClassEnding = false; //TODO: Figure out Abrar's logic for this... ending.equals(".class"); // this is a trigger for custom stuff
try (ZipOutputStream zout = new ZipOutputStream(new FileOutputStream(output)))
{
for (String path : dirtySupplier.gatherAll(ending))
{
if ((isClassEnding && matchesClass(cleanFiles, path)) || cleanFiles.contains(path))
{
continue;
}
zout.putNextEntry(new ZipEntry(path));
try (InputStream stream = dirtySupplier.getInput(path))
{
ByteStreams.copy(stream, zout);
}
}
}
}
}
private String stripEnding(String path)
{
if (path == null || path.length() < ending.length())
return null;
return path.substring(0, path.length() - ending.length());
}
private boolean matchesClass(Set<String> cleans, String path)
{
int innerIndex = path.indexOf('$');
if (innerIndex > 0) // better not be starting with $
{
// get the parent class, since its an inner
path = stripEnding(path).substring(0, innerIndex) + ending; // from start till just before $
}
return cleans.contains(path);
}
private static InputSupplier getSupplier(List<File> files) throws IOException
{
SequencedInputSupplier supplier = new SequencedInputSupplier(files.size() + 1);
for (File f : files)
{
if (f.isDirectory())
supplier.add(new FolderSupplier(f));
else
{
ZipInputSupplier supp = new ZipInputSupplier();
supp.readZip(f);
supplier.add(supp);
}
}
return supplier;
}
@InputFiles
public FileCollection getCleanSources()
{
return getProject().files(clean);
}
public List<File> getCleanSource()
{
List<File> files = new LinkedList<File>();
for (Object f : clean)
files.add(getProject().file(f));
return files;
}
public void addCleanSource(Object in)
{
this.clean.add(in);
}
@InputFiles
public FileCollection getDirtySources()
{
return getProject().files(dirty);
}
public List<File> getDirtySource()
{
List<File> files = new LinkedList<File>();
for (Object f : dirty)
files.add(getProject().file(f));
return files;
}
public void addDirtySource(Object in)
{
this.dirty.add(in);
}
public String getEnding()
{
return ending;
}
public void setEnding(String ending)
{
this.ending = ending;
}
public File getOutput()
{
return getProject().file(output);
}
public void setOutput(Object output)
{
this.output = output;
}
}