forked from DimensionalDevelopment/ForgeGradle
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathConstants.java
More file actions
562 lines (491 loc) · 21.4 KB
/
Constants.java
File metadata and controls
562 lines (491 loc) · 21.4 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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
/*
* A Gradle plugin for the creation of Minecraft mods and MinecraftForge plugins.
* Copyright (C) 2013-2018 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 java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.io.StringReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.Callable;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import org.gradle.api.Project;
import org.gradle.api.file.FileCollection;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import com.google.common.base.Charsets;
import com.google.common.collect.ImmutableList;
import com.google.common.io.ByteStreams;
import com.google.common.io.CharStreams;
import com.google.common.io.Files;
import au.com.bytecode.opencsv.CSVParser;
import au.com.bytecode.opencsv.CSVReader;
import groovy.lang.Closure;
import net.minecraftforge.gradle.patcher.PatcherExtension;
import net.minecraftforge.gradle.util.json.version.OS;
public class Constants
{
// OS
public static enum SystemArch
{
BIT_32, BIT_64;
public String toString()
{
return lower(name()).replace("bit_", "");
}
}
public static final OS OPERATING_SYSTEM = OS.CURRENT;
public static final SystemArch SYSTEM_ARCH = getArch();
public static final Charset CHARSET = Charsets.UTF_8;
public static final String HASH_FUNC = "MD5";
public static final String USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11";
// extension names
public static final String EXT_NAME_MC = "minecraft";
public static final String GROUP_FG = "ForgeGradle";
@SuppressWarnings("serial")
public static final Closure<Boolean> CALL_FALSE = new Closure<Boolean>(Constants.class) {
public Boolean call(Object o)
{
return false;
}
};
// replacement strings
/** MC version in form "#.#.#(-appendage)" where the appendage may be -pre# or something. **/
public static final String REPLACE_MC_VERSION = "{MC_VERSION}";
/** the folder where to cache. ~/.gradle/caches/minecraft **/
public static final String REPLACE_CACHE_DIR = "{CACHE_DIR}";
/** the folder where to cache project specific. project/.gradle/ **/
public static final String REPLACE_PROJECT_CACHE_DIR = "{PROJECT_CACHE_DIR}";
/** project/build **/
public static final String REPLACE_BUILD_DIR = "{BUILD_DIR}";
/** MCP mapping channel **/
public static final String REPLACE_MCP_CHANNEL = "{MAPPING_CHANNEL}";
/** MCP mapping version **/
public static final String REPLACE_MCP_VERSION = "{MAPPING_VERSION}";
/** MCP mapping MC version **/
public static final String REPLACE_MCP_MCVERSION = "{MAPPING_MCVERSION}";
/** AssetIndex name **/
public static final String REPLACE_ASSET_INDEX = "{ASSET_INDEX}";
// urls
public static final String URL_MC_MANIFEST = "https://launchermeta.mojang.com/mc/game/version_manifest.json";
public static final String URL_FF = "http://files.minecraftforge.net/fernflower-fix-1.0.zip";
public static final String URL_ASSETS = "http://resources.download.minecraft.net";
public static final String URL_LIBRARY = "https://libraries.minecraft.net/"; // Mojang's Cloudflare front end
//public static final String URL_LIBRARY = "https://minecraft-libraries.s3.amazonaws.com/"; // Mojang's AWS server, as Cloudflare is having issues, TODO: Switch back to above when their servers are fixed.
public static final String URL_FORGE_MAVEN = "http://files.minecraftforge.net/maven";
public static final List<String> URLS_MCP_JSON = Arrays.asList(
URL_FORGE_MAVEN + "/de/oceanlabs/mcp/versions.json",
"http://export.mcpbot.bspk.rs/versions.json"
);
// configs
public static final String CONFIG_MCP_DATA = "forgeGradleMcpData";
public static final String CONFIG_MAPPINGS = "forgeGradleMcpMappings";
public static final String CONFIG_NATIVES = "forgeGradleMcNatives";
public static final String CONFIG_MC_DEPS = "forgeGradleMcDeps";
public static final String CONFIG_FFI_DEPS = "forgeGradleFfiDeps"; // FernFlowerInvoker
public static final String CONFIG_MC_DEPS_CLIENT = "forgeGradleMcDepsClient";
// things in the cache dir.
public static final String DIR_LOCAL_CACHE = REPLACE_PROJECT_CACHE_DIR + "/minecraft";
public static final String DIR_MCP_DATA = REPLACE_CACHE_DIR + "/de/oceanlabs/mcp/mcp/" + REPLACE_MC_VERSION;
public static final String DIR_MCP_MAPPINGS = REPLACE_CACHE_DIR + "/de/oceanlabs/mcp/mcp_" + REPLACE_MCP_CHANNEL + "/" + REPLACE_MCP_VERSION;
public static final String JAR_CLIENT_FRESH = REPLACE_CACHE_DIR + "/net/minecraft/minecraft/" + REPLACE_MC_VERSION + "/minecraft-" + REPLACE_MC_VERSION + ".jar";
public static final String JAR_SERVER_FRESH = REPLACE_CACHE_DIR + "/net/minecraft/minecraft_server/" + REPLACE_MC_VERSION + "/minecraft_server-" + REPLACE_MC_VERSION + ".jar";
public static final String JAR_MERGED = REPLACE_CACHE_DIR + "/net/minecraft/minecraft_merged/" + REPLACE_MC_VERSION + "/minecraft_merged-" + REPLACE_MC_VERSION + ".jar";
public static final String JAR_SERVER_PURE = REPLACE_CACHE_DIR + "/net/minecraft/minecraft_server/" + REPLACE_MC_VERSION + "/minecraft_server-" + REPLACE_MC_VERSION + "-pure.jar";
public static final String JAR_SERVER_DEPS = REPLACE_CACHE_DIR + "/net/minecraft/minecraft_server/" + REPLACE_MC_VERSION + "/minecraft_server-" + REPLACE_MC_VERSION + "-deps.jar";
public static final String DIR_NATIVES = REPLACE_CACHE_DIR + "/net/minecraft/natives/" + REPLACE_MC_VERSION + "/";
public static final String JAR_FERNFLOWER = REPLACE_CACHE_DIR + "/fernflower-fixed.jar";
public static final String DIR_ASSETS = REPLACE_CACHE_DIR + "/assets";
public static final String JSON_ASSET_INDEX = DIR_ASSETS + "/indexes/" + REPLACE_ASSET_INDEX + ".json";
public static final String DIR_JSONS = REPLACE_CACHE_DIR + "/versionJsons";
public static final String JSON_VERSION = DIR_JSONS + "/" + REPLACE_MC_VERSION + ".json";
public static final String GRADLE_START_CLIENT = "GradleStart";
public static final String GRADLE_START_SERVER = "GradleStartServer";
public static final String[] GRADLE_START_RESOURCES = new String[] {
"GradleStart.java",
"GradleStartServer.java",
"net/minecraftforge/gradle/GradleStartCommon.java",
// 1.7.10 only
//makeStart.addResource("net/minecraftforge/gradle/OldPropertyMapSerializer.java");
};
public static final String[] GRADLE_START_FML_RES = new String[] {
"net/minecraftforge/gradle/GradleForgeHacks.java",
"net/minecraftforge/gradle/tweakers/CoremodTweaker.java",
"net/minecraftforge/gradle/tweakers/AccessTransformerTweaker.java"
};
// mcp data constants
public static final String MCP_DATA_SRG = DIR_MCP_DATA + "/config/joined.srg";
public static final String MCP_DATA_STYLE = DIR_MCP_DATA + "/astyle.cfg";
public static final String MCP_DATA_ACCESS = DIR_MCP_DATA + "/config/access.txt";
public static final String MCP_DATA_EXCEPTIONS = DIR_MCP_DATA + "/config/exceptions.txt";
public static final String MCP_DATA_CONSTRUCTORS = DIR_MCP_DATA + "/config/constructors.txt";
public static final String MCP_DATA_STATICS = DIR_MCP_DATA + "/config/static_methods.txt";
public static final String MCP_PATCHES_CLIENT = DIR_MCP_DATA + "/patches/client";
public static final String MCP_PATCHES_SERVER = DIR_MCP_DATA + "/patches/server";
public static final String MCP_PATCHES_MERGED = DIR_MCP_DATA + "/patches/joined";
public static final String MCP_INJECT = DIR_MCP_DATA + "/config/inject";
// generated off of MCP data constants
public static final String CSV_METHOD = DIR_MCP_MAPPINGS + "/methods.csv";
public static final String CSV_FIELD = DIR_MCP_MAPPINGS + "/fields.csv";
public static final String CSV_PARAM = DIR_MCP_MAPPINGS + "/params.csv";
public static final String SRG_NOTCH_TO_SRG = DIR_MCP_MAPPINGS + "/" + REPLACE_MC_VERSION + "/srgs/notch-srg.srg";
public static final String SRG_NOTCH_TO_MCP = DIR_MCP_MAPPINGS + "/" + REPLACE_MC_VERSION + "/srgs/notch-mcp.srg";
public static final String SRG_SRG_TO_MCP = DIR_MCP_MAPPINGS + "/" + REPLACE_MC_VERSION + "/srgs/srg-mcp.srg";
public static final String SRG_MCP_TO_SRG = DIR_MCP_MAPPINGS + "/" + REPLACE_MC_VERSION + "/srgs/mcp-srg.srg";
public static final String SRG_MCP_TO_NOTCH = DIR_MCP_MAPPINGS + "/" + REPLACE_MC_VERSION + "/srgs/mcp-notch.srg";
public static final String EXC_SRG = DIR_MCP_MAPPINGS + "/" + REPLACE_MC_VERSION + "/srgs/srg.exc";
public static final String EXC_MCP = DIR_MCP_MAPPINGS + "/" + REPLACE_MC_VERSION + "/srgs/mcp.exc";
// task names
public static final String TASK_DL_CLIENT = "downloadClient";
public static final String TASK_DL_SERVER = "downloadServer";
public static final String TASK_SPLIT_SERVER = "splitServerJar";
public static final String TASK_MERGE_JARS = "mergeJars";
public static final String TASK_EXTRACT_NATIVES = "extractNatives";
public static final String TASK_DL_VERSION_JSON = "getVersionJson";
public static final String TASK_DL_ASSET_INDEX = "getAssetIndex";
public static final String TASK_DL_ASSETS = "getAssets";
public static final String TASK_EXTRACT_MCP = "extractMcpData";
public static final String TASK_EXTRACT_MAPPINGS = "extractMcpMappings";
public static final String TASK_GENERATE_SRGS = "genSrgs";
public static final String TASK_CLEAN_CACHE = "cleanCache";
// util
public static final String NEWLINE = System.getProperty("line.separator");
// helper methods
public static List<String> getClassPath()
{
URL[] urls = ((URLClassLoader) PatcherExtension.class.getClassLoader()).getURLs();
ArrayList<String> list = new ArrayList<String>();
for (URL url : urls)
{
list.add(url.getPath());
}
return list;
}
public static URL[] toUrls(FileCollection collection) throws MalformedURLException
{
ArrayList<URL> urls = new ArrayList<URL>();
for (File file : collection.getFiles())
urls.add(file.toURI().toURL());
return urls.toArray(new URL[urls.size()]);
}
public static File getMinecraftDirectory()
{
String userDir = System.getProperty("user.home");
switch (OPERATING_SYSTEM)
{
case LINUX:
return new File(userDir, ".minecraft");
case WINDOWS:
String appData = System.getenv("APPDATA");
String folder = appData != null ? appData : userDir;
return new File(folder, ".minecraft");
case OSX:
return new File(userDir, "Library/Application Support/minecraft");
default:
return new File(userDir, "minecraft");
}
}
private static SystemArch getArch()
{
String name = lower(System.getProperty("os.arch"));
if (name.contains("64"))
{
return SystemArch.BIT_64;
}
else
{
return SystemArch.BIT_32;
}
}
public static String lower(String string)
{
return string.toLowerCase(Locale.ENGLISH);
}
public static List<String> lines(final String text)
{
try
{
return ImmutableList.copyOf(CharStreams.readLines(new StringReader(text)));
}
catch (IOException e)
{
// IMPOSSIBRU
return ImmutableList.of();
}
}
/**
* This method constructs,, configures and returns a CSV reader instance to be used to read MCP CSV files.
* @param file File to read
* @return a configured CSVReader
* @throws IOException Propogated from openning the file
*/
public static CSVReader getReader(File file) throws IOException
{
return new CSVReader(Files.newReader(file, Charset.defaultCharset()), CSVParser.DEFAULT_SEPARATOR, CSVParser.DEFAULT_QUOTE_CHARACTER, CSVParser.NULL_CHARACTER, 1, false);
}
public static Element addXml(Node parent, String name, Map<String, String> values)
{
Document doc = parent.getOwnerDocument();
if (doc == null)
doc = (Document) parent;
Element e = doc.createElement(name);
for (Entry<String, String> entry : values.entrySet())
{
e.setAttribute(entry.getKey(), entry.getValue());
}
parent.appendChild(e);
return e;
}
/**
* This method uses the channels API which uses direct filesystem copies instead of loading it into
* ram and then outputting it.
* @param in file to copy
* @param out created with directories if needed
* @throws IOException In case anything goes wrong with the file IO
*/
public static void copyFile(File in, File out) throws IOException
{
// make dirs just in case
out.getParentFile().mkdirs();
try (FileInputStream fis = new FileInputStream(in);
FileOutputStream fout = new FileOutputStream(out);
FileChannel source = fis.getChannel();
FileChannel dest = fout.getChannel())
{
long size = source.size();
source.transferTo(0, size, dest);
}
}
/**
* This method uses the channels API which uses direct filesystem copies instead of loading it into
* ram and then outputting it.
* @param in file to copy
* @param out created with directories if needed
* @param size If you have it earlier
* @throws IOException In case anything goes wrong with the file IO
*/
public static void copyFile(File in, File out, long size) throws IOException
{
// make dirs just in case
out.getParentFile().mkdirs();
try (FileInputStream fis = new FileInputStream(in);
FileOutputStream fout = new FileOutputStream(out);
FileChannel source = fis.getChannel();
FileChannel dest = fout.getChannel())
{
source.transferTo(0, size, dest);
}
}
public static String hash(File file)
{
if (file.getPath().endsWith(".zip") || file.getPath().endsWith(".jar"))
return hashZip(file, HASH_FUNC);
else
return hash(file, HASH_FUNC);
}
public static List<String> hashAll(File file)
{
LinkedList<String> list = new LinkedList<String>();
if (file.isDirectory())
{
for (File f : file.listFiles())
list.addAll(hashAll(f));
}
else if (!file.getName().equals(".cache"))
list.add(hash(file));
return list;
}
public static String hash(File file, String function)
{
try
{
InputStream fis = new FileInputStream(file);
byte[] array = ByteStreams.toByteArray(fis);
fis.close();
return hash(array, function);
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
public static String hashZip(File file, String function)
{
try (ZipInputStream zin = new ZipInputStream(new FileInputStream(file)))
{
MessageDigest hasher = MessageDigest.getInstance(function);
ZipEntry entry;
while ((entry = zin.getNextEntry()) != null)
{
hasher.update(entry.getName().getBytes());
hasher.update(ByteStreams.toByteArray(zin));
}
zin.close();
byte[] hash = hasher.digest();
// convert to string
String result = "";
for (int i = 0; i < hash.length; i++)
{
result += Integer.toString((hash[i] & 0xff) + 0x100, 16).substring(1);
}
return result;
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
public static String hash(String str)
{
return hash(str.getBytes());
}
public static String hash(byte[] bytes)
{
return hash(bytes, HASH_FUNC);
}
public static String hash(byte[] bytes, String function)
{
try
{
MessageDigest complete = MessageDigest.getInstance(function);
byte[] hash = complete.digest(bytes);
String result = "";
for (int i = 0; i < hash.length; i++)
{
result += Integer.toString((hash[i] & 0xff) + 0x100, 16).substring(1);
}
return result;
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
public static File getTaskLogFile(Project project, String name)
{
final File taskLogs = new File(project.getBuildDir(), "taskLogs");
taskLogs.mkdirs();
final File logFile = new File(taskLogs, name);
logFile.delete();//Delete the old log
return logFile;
}
public static PrintStream getTaskLogStream(Project project, String name)
{
final File logFile = getTaskLogFile(project, name);
try
{
return new PrintStream(logFile);
}
catch (FileNotFoundException ignored)
{}
return null;// Should never get to here
}
/**
* Throws a null runtime exception if the resource isnt found.
* @param resource String name of the resource your looking for
* @return URL
*/
public static URL getResource(String resource)
{
ClassLoader loader = BaseExtension.class.getClassLoader();
if (loader == null)
throw new RuntimeException("ClassLoader is null! IMPOSSIBRU");
URL url = loader.getResource(resource);
if (url == null)
throw new RuntimeException("Resource " + resource + " not found");
return url;
}
/**
* Resolves the supplied object to a string.
* If the input is null, this will return null.
* Closures and Callables are called with no arguments.
* Arrays use Arrays.toString().
* File objects return their absolute paths.
* All other objects have their toString run.
* @param obj Object to resolve
* @return resolved string
*/
@SuppressWarnings("rawtypes")
public static String resolveString(Object obj)
{
if (obj == null)
return null;
// stop early if its the right type. no need to do more expensive checks
if (obj instanceof String)
return (String) obj;
if (obj instanceof Closure)
return resolveString(((Closure) obj).call());// yes recursive.
if (obj instanceof Callable)
{
try
{
return resolveString(((Callable) obj).call());
}
catch (Exception e)
{
return null;
}
}
else if (obj instanceof File)
return ((File) obj).getAbsolutePath();
// arrays
else if (obj.getClass().isArray())
{
if (obj instanceof Object[])
return Arrays.toString(((Object[]) obj));
else if (obj instanceof byte[])
return Arrays.toString(((byte[]) obj));
else if (obj instanceof char[])
return Arrays.toString(((char[]) obj));
else if (obj instanceof int[])
return Arrays.toString(((int[]) obj));
else if (obj instanceof float[])
return Arrays.toString(((float[]) obj));
else if (obj instanceof double[])
return Arrays.toString(((double[]) obj));
else if (obj instanceof long[])
return Arrays.toString(((long[]) obj));
else
return obj.getClass().getSimpleName();
}
else
return obj.toString();
}
}