forked from MinecraftForge/MinecraftMavenizer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMCPDataTask.java
More file actions
200 lines (164 loc) · 7.48 KB
/
MCPDataTask.java
File metadata and controls
200 lines (164 loc) · 7.48 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
/*
* Copyright (c) Forge Development LLC and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/
package net.minecraftforge.mcmaven.cli;
import java.io.File;
import java.nio.file.Files;
import java.util.zip.ZipFile;
import joptsimple.OptionParser;
import net.minecraftforge.mcmaven.impl.MinecraftMaven;
import net.minecraftforge.mcmaven.impl.cache.Cache;
import net.minecraftforge.mcmaven.impl.mappings.Mappings;
import net.minecraftforge.mcmaven.impl.mappings.ParchmentMappings;
import net.minecraftforge.mcmaven.impl.repo.mcpconfig.MCP;
import net.minecraftforge.mcmaven.impl.repo.mcpconfig.MCPConfigRepo;
import net.minecraftforge.mcmaven.impl.repo.mcpconfig.MinecraftTasks;
import net.minecraftforge.mcmaven.impl.util.Artifact;
import net.minecraftforge.srgutils.IMappingFile;
import net.minecraftforge.srgutils.IRenamer;
import net.minecraftforge.srgutils.IMappingFile.IField;
import net.minecraftforge.srgutils.IMappingFile.IMethod;
import net.minecraftforge.srgutils.IMappingFile.IParameter;
import static net.minecraftforge.mcmaven.impl.Mavenizer.LOGGER;
// TODO [Mavenizer][MCPDataTask] This is a copy of FG6's ExtractMCPData task.
// its not the best, but I dont want to re-wrok INSTALLER_TOOLS to put the tsrg in the mappings zip
class MCPDataTask {
static OptionParser run(String[] args, boolean getParser) throws Exception {
var parser = new OptionParser();
parser.allowsUnrecognizedOptions();
//@formatter:off
// help message
var helpO = parser.accepts("help",
"Displays this help message and exits")
.forHelp();
// root cache directory
var cacheO = parser.accepts("cache",
"Directory to store data needed for this program")
.withRequiredArg().ofType(File.class).defaultsTo(new File("cache"));
// jdk cache directory
var jdkCacheO = parser.accepts("jdk-cache",
"Directory to store jdks downloaded from the disoco api")
.withRequiredArg().ofType(File.class).defaultsTo(new File("cache/jdks"));
var outputO = parser.accepts("output",
"File to place output data into")
.withRequiredArg().ofType(File.class);
var artifactO = parser.accepts("artifact",
"MCPConfig artifact coordinates")
.withRequiredArg();
var versionO = parser.accepts("version",
"MCPConfig artifact version")
.availableUnless(artifactO)
.withRequiredArg();
var officialO = parser.accepts("mappings",
"Use to enable using official mappings");
var parchmentO = parser.accepts("parchment",
"Version of parchment mappings to use, snapshots are not supported")
.availableUnless(officialO)
.withRequiredArg();
officialO
.availableUnless(parchmentO);
var keyO = parser.accepts("key",
"The key for which data file to extract")
.withRequiredArg();
//@formatter:on
if (getParser)
return parser;
var options = parser.parse(args);
if (options.has(helpO)) {
parser.printHelpOn(LOGGER.getInfo());
return parser;
}
var output = options.valueOf(outputO);
var cacheRoot = options.valueOf(cacheO);
var jdkCacheRoot = !options.has(cacheO) || options.has(jdkCacheO)
? options.valueOf(jdkCacheO)
: new File(cacheRoot, "jdks");
var artifact =
options.has(artifactO) ? Artifact.from(options.valueOf(artifactO)) :
options.has(versionO) ? MCP.artifact(options.valueOf(versionO)) :
null;
if (artifact == null) {
LOGGER.error("Missing mcp --version or --artifact");
return parser;
}
var mcVersion = MinecraftMaven.mcpToMcVersion(artifact.getVersion());
Mappings mappings;
if (options.has(officialO))
mappings = new Mappings("official", null).withMCVersion(mcVersion);
else if (options.has(parchmentO))
mappings = new ParchmentMappings(options.valueOf(parchmentO)).withMCVersion(mcVersion);
else
mappings = null;
var key = options.valueOf(keyO);
if (key == null) {
LOGGER.error("Missing --key option");
return parser;
}
var repo = new MCPConfigRepo(new Cache(cacheRoot, jdkCacheRoot), false);
LOGGER.info(" Output: " + output.getAbsolutePath());
LOGGER.info(" Cache: " + cacheRoot.getAbsolutePath());
LOGGER.info(" JDK Cache: " + jdkCacheRoot.getAbsolutePath());
LOGGER.info(" Artifact: " + artifact);
LOGGER.info(" Key: " + key);
if (mappings != null)
LOGGER.info(" Mappings: " + mappings);
LOGGER.info();
var mcp = repo.get(artifact);
var side = mcp.getSide("joined");
var cfg = mcp.getConfig().getData("joined");
var path = cfg.get(key);
if (path == null && "statics".equals(key))
path = "config/static_methods.txt";
if (path == null) {
LOGGER.error("Could not find data entry for '%s'".formatted(key));
return parser;
}
try (ZipFile zip = new ZipFile(mcp.getData())) {
var entry = zip.getEntry(path);
if (entry == null) {
LOGGER.error("Invalid config zip, missing file: " + path);
return parser;
}
if ("mappings".equals(key)) {
var ret = IMappingFile.load(zip.getInputStream(entry));
if (mcp.getConfig().official) {
var mc = mcp.getMinecraftTasks();
var client = mc.versionFile(MinecraftTasks.MCFile.CLIENT_MAPPINGS);
var server = mc.versionFile(MinecraftTasks.MCFile.SERVER_MAPPINGS);
var obf2OffClient = IMappingFile.load(client.execute());
var obf2OffServer = IMappingFile.load(server.execute());
var obf2Off = obf2OffClient.merge(obf2OffServer).reverse();
ret = ret.rename(new IRenamer() {
@Override
public String rename(IMappingFile.IClass value) {
return obf2Off.remapClass(value.getOriginal());
}
});
}
if (mappings != null) {
var csv = mappings.getCsvZip(side).execute();
var names = Mappings.load(csv).names();
ret = ret.rename(new IRenamer() {
@Override
public String rename(IField value) {
return names.getOrDefault(value.getMapped(), value.getMapped());
}
@Override
public String rename(IMethod value) {
return names.getOrDefault(value.getMapped(), value.getMapped());
}
@Override
public String rename(IParameter value) {
return names.getOrDefault(value.getMapped(), value.getMapped());
}
});
}
ret.write(output.getAbsoluteFile().toPath(), IMappingFile.Format.TSRG2, false);
} else {
Files.copy(zip.getInputStream(entry), output.toPath());
}
}
return parser;
}
}