forked from MinecraftForge/ForgeGradle
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBaseExtension.java
More file actions
354 lines (301 loc) · 11.1 KB
/
BaseExtension.java
File metadata and controls
354 lines (301 loc) · 11.1 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
* 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.common;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableMap;
import com.google.common.io.Resources;
import com.google.common.primitives.Ints;
import net.minecraftforge.gradle.util.GradleConfigurationException;
import net.minecraftforge.gradle.util.delayed.ReplacementProvider;
import org.gradle.api.Project;
import java.net.URL;
import java.util.Arrays;
import java.util.Map;
import java.util.Map.Entry;
public abstract class BaseExtension
{
protected static final transient Map<String, String> MCP_VERSION_MAP = ImmutableMap.of("1.8", "9.10");
private static final String MIN_VERSION = "1.12";
// The max supported version of minecraft. null means latest is supported
private static final String MAX_VERSION = null;
public final String forgeGradleVersion;
protected transient Project project;
protected transient ReplacementProvider replacer;
protected String version;
protected String mcpVersion = "unknown";
// this should never be touched except by the base plugin in this package
Map<String, Map<String, int[]>> mcpJson;
protected boolean mappingsSet = false;
protected String mappingsChannel = null;
protected int mappingsVersion = -1;
// custom version for custom mappings
protected String mappingsCustom = null;
private boolean suppressVersionTest;
public BaseExtension(BasePlugin<? extends BaseExtension> plugin)
{
this.project = plugin.project;
this.replacer = plugin.replacer;
String version;
try
{
URL url = BaseExtension.class.getClassLoader().getResource("forgegradle.version.txt");
version = Resources.toString(url, Constants.CHARSET).trim();
if (version.equals("${version}"))
{
version = "2.1-SNAPSHOT";// fallback
}
}
catch (Exception e)
{
// again, the fallback
version = "2.0-SNAPSHOT";
}
forgeGradleVersion = version;
}
public void setSuppressVersionTest(boolean b) {
this.suppressVersionTest = b;
}
/**
* Get the Minecraft version
*
* @return The Minecraft version
*/
public String getVersion()
{
return version;
}
/**
* Set the Minecraft version
*
* @param version The Minecraft version
*/
public void setVersion(String version)
{
this.version = version;
replacer.putReplacement(Constants.REPLACE_MC_VERSION, version);
mcpVersion = MCP_VERSION_MAP.get(version);
// maybe they set the mappings first
checkMappings();
}
/**
* Get the MCP data version
*
* @return The MCP data version
*/
public String getMcpVersion()
{
return mcpVersion == null ? "unknown" : mcpVersion;
}
public void setMcpVersion(String mcpVersion)
{
this.mcpVersion = mcpVersion;
}
public void copyFrom(BaseExtension ext)
{
if ("null".equals(version))
{
setVersion(ext.getVersion());
}
if ("unknown".equals(mcpVersion))
{
setMcpVersion(ext.getMcpVersion());
}
}
/**
* Get the MCP mappings channel and version<br>
* Examples: {@code stable_17, snapshot_20151113}
*
* @return The channel and version in format: {@code channel_version}.
*
* @see <a href="http://export.mcpbot.bspk.rs/">http://export.mcpbot.bspk.rs/</a>
*/
public String getMappings()
{
return mappingsChannel + "_" + (mappingsCustom == null ? mappingsVersion : mappingsCustom);
}
/**
* Get the MCP mappings channel
*
* @return The channel
*
* @see <a href="http://export.mcpbot.bspk.rs/">http://export.mcpbot.bspk.rs/</a>
*/
public String getMappingsChannel()
{
return mappingsChannel;
}
/**
* Strips the _nodoc and _verbose channel subtypes from the channel name.
*
* @return The channel without subtype
*/
public String getMappingsChannelNoSubtype()
{
int underscore = mappingsChannel.indexOf('_');
if (underscore <= 0) // already has docs.
return mappingsChannel;
else
return mappingsChannel.substring(0, underscore);
}
/**
* Get the MCP mappings version
*
* @return The version
*
* @see <a href="http://export.mcpbot.bspk.rs/">http://export.mcpbot.bspk.rs/</a>
*/
public String getMappingsVersion()
{
return mappingsCustom == null ? "" + mappingsVersion : mappingsCustom;
}
/**
* Set the MCP mappings channel and version<br>
* The format is: {@code channel_version}.<br>
* Examples: {@code stable_17, snapshot_20151113}
*
* @param mappings The channel and version
*
* @see <a href="http://export.mcpbot.bspk.rs/">http://export.mcpbot.bspk.rs/</a>
*/
public void setMappings(String mappings)
{
if (Strings.isNullOrEmpty(mappings))
{
mappingsChannel = null;
mappingsVersion = -1;
replacer.putReplacement(Constants.REPLACE_MCP_CHANNEL, mappingsChannel);
replacer.putReplacement(Constants.REPLACE_MCP_VERSION, getMappingsVersion());
return;
}
mappings = mappings.toLowerCase();
if (!mappings.contains("_"))
{
throw new IllegalArgumentException("Mappings must be in format 'channel_version' or 'custom_something'. eg: snapshot_20140910 custom_AbrarIsCool");
}
int index = mappings.lastIndexOf('_');
mappingsChannel = mappings.substring(0, index);
mappingsCustom = mappings.substring(index + 1);
if (!mappingsCustom.equals("custom"))
{
try
{
mappingsVersion = Integer.parseInt(mappingsCustom);
mappingsCustom = null;
}
catch (NumberFormatException e)
{
throw new GradleConfigurationException("The mappings version must be a number! eg: channel_### or channel_custom (for custom mappings).");
}
}
mappingsSet = true;
replacer.putReplacement(Constants.REPLACE_MCP_CHANNEL, mappingsChannel);
replacer.putReplacement(Constants.REPLACE_MCP_VERSION, getMappingsVersion());
// check
checkMappings();
}
protected void checkMappings() {
// mappings or mc version are null
if (mappingsChannel == null || Strings.isNullOrEmpty(version))
return;
// set now.
replacer.putReplacement(Constants.REPLACE_MCP_MCVERSION, version);
// gotta do this after setting the MC version
if (mappingsCustom != null)
return;
// check if it exists
Map<String, int[]> versionMap = mcpJson.get(version);
String channel = getMappingsChannelNoSubtype();
if (versionMap != null)
{
int[] channelList = versionMap.get(channel);
if (channelList == null)
throw new GradleConfigurationException("There is no such MCP mapping channel named " + channel);
// all is well with the world
if (searchArray(channelList, mappingsVersion))
return;
}
// if it gets here.. it wasnt found. Now we try to actually find it..
for (Entry<String, Map<String, int[]>> mcEntry : mcpJson.entrySet())
{
for (Entry<String, int[]> channelEntry : mcEntry.getValue().entrySet())
{
// found it!
if (searchArray(channelEntry.getValue(), mappingsVersion))
{
boolean rightMc = mcEntry.getKey().equals(version);
boolean rightChannel = channelEntry.getKey().equals(channel);
// right channel, but wrong mc
if (rightChannel && !rightMc)
{
project.getLogger().warn("Fuzzing selected mapping '" + getMappings() + "' to designed MC version " + mcEntry.getKey());
replacer.putReplacement(Constants.REPLACE_MCP_MCVERSION, mcEntry.getKey()); // set MC version
return;
}
// right MC , but wrong channel
else if (rightMc && !rightChannel)
{
throw new GradleConfigurationException("Selected mapping '" + getMappings() + "' doesnt exist! Perhaps you meant '" + channelEntry.getKey() + "_" + mappingsVersion + "'?");
}
}
}
}
// wasnt found
throw new GradleConfigurationException("The specified mapping '" + getMappings() + "' does not exist!");
}
@SuppressWarnings("unused")
private static String getMappedString(String key, String value) {
if (value == null) return "";
return key + ": " + value;
}
/**
* Compares 2 versions. Returns -1 if version1 is outdated, +1 if version2 is outdated, and 0 if they are the same.
* @param version1
* @param version2
* @return
*/
private static int compareVersion(String version1, String version2)
{
if (version2 == null || version2.equals(version1))
return 0;
int[] v1 = stringToInt(version1.split("\\."));
int[] v2 = stringToInt(version2.split("\\."));
// FIXME 1.9.0 > 1.9
return Ints.lexicographicalComparator().compare(v1, v2);
}
private static int[] stringToInt(String[] strings)
{
int[] ints = new int[strings.length];
for (int i = 0; i < ints.length; i++)
{
Integer v = Ints.tryParse(strings[i]);
ints[i] = v == null ? 0 : v;
}
return ints;
}
private static String nullStringTo(String string, String to) {
return string == null ? to : string;
}
private static boolean searchArray(int[] array, int key)
{
Arrays.sort(array);
int foundIndex = Arrays.binarySearch(array, key);
return foundIndex >= 0 && array[foundIndex] == key;
}
}