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
|
/*
* This file is part of fabric-loom, licensed under the MIT License (MIT).
*
* Copyright (c) 2016-2021 FabricMC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package net.fabricmc.loom.configuration.ide;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Consumer;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableMap;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import org.gradle.api.Project;
import org.gradle.api.tasks.SourceSet;
import org.gradle.plugins.ide.eclipse.model.EclipseModel;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import net.fabricmc.loom.LoomGradleExtension;
import net.fabricmc.loom.configuration.InstallerData;
import net.fabricmc.loom.util.OperatingSystem;
public class RunConfig {
public String configName;
public String eclipseProjectName;
public String ideaModuleName;
public String vscodeProjectName;
public String mainClass;
public String runDirIdeaUrl;
public String runDir;
public String vmArgs;
public String programArgs;
public List<String> vscodeBeforeRun = new ArrayList<>();
public final Map<String, String> envVariables = new HashMap<>();
public SourceSet sourceSet;
public Element genRuns(Element doc) {
Element root = this.addXml(doc, "component", ImmutableMap.of("name", "ProjectRunConfigurationManager"));
root = addXml(root, "configuration", ImmutableMap.of("default", "false", "name", configName, "type", "Application", "factoryName", "Application"));
this.addXml(root, "module", ImmutableMap.of("name", ideaModuleName));
this.addXml(root, "option", ImmutableMap.of("name", "MAIN_CLASS_NAME", "value", mainClass));
this.addXml(root, "option", ImmutableMap.of("name", "WORKING_DIRECTORY", "value", runDirIdeaUrl));
if (!Strings.isNullOrEmpty(vmArgs)) {
this.addXml(root, "option", ImmutableMap.of("name", "VM_PARAMETERS", "value", vmArgs));
}
if (!Strings.isNullOrEmpty(programArgs)) {
this.addXml(root, "option", ImmutableMap.of("name", "PROGRAM_PARAMETERS", "value", programArgs));
}
if (!envVariables.isEmpty()) {
Element envs = this.addXml(root, "envs", ImmutableMap.of());
for (Map.Entry<String, String> envEntry : envVariables.entrySet()) {
this.addXml(envs, "env", ImmutableMap.of("name", envEntry.getKey(), "value", envEntry.getValue()));
}
}
return root;
}
public 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 (Map.Entry<String, String> entry : values.entrySet()) {
e.setAttribute(entry.getKey(), entry.getValue());
}
parent.appendChild(e);
return e;
}
private static String getIdeaModuleName(Project project, SourceSet srcs) {
String module = project.getName() + "." + srcs.getName();
while ((project = project.getParent()) != null) {
module = project.getName() + "." + module;
}
return module;
}
private static void populate(Project project, LoomGradleExtension extension, RunConfig runConfig, String environment) {
runConfig.configName += extension.isRootProject() ? "" : " (" + project.getPath() + ")";
runConfig.eclipseProjectName = project.getExtensions().getByType(EclipseModel.class).getProject().getName();
runConfig.vscodeProjectName = extension.isRootProject() ? "" : project.getPath();
runConfig.vmArgs = "";
runConfig.programArgs = "";
runConfig.mainClass = "net.fabricmc.devlaunchinjector.Main";
runConfig.vmArgs = "-Dfabric.dli.config=" + encodeEscaped(extension.getFiles().getDevLauncherConfig().getAbsolutePath()) + " -Dfabric.dli.env=" + environment.toLowerCase();
}
// Turns camelCase/PascalCase into Capital Case
// caseConversionExample -> Case Conversion Example
private static String capitalizeCamelCaseName(String name) {
if (name.length() == 0) {
return "";
}
return name.substring(0, 1).toUpperCase() + name.substring(1).replaceAll("([^A-Z])([A-Z])", "$1 $2");
}
public static RunConfig runConfig(Project project, RunConfigSettings settings) {
settings.evaluateNow();
LoomGradleExtension extension = LoomGradleExtension.get(project);
String name = settings.getName();
String configName = settings.getConfigName();
String environment = settings.getEnvironment();
SourceSet sourceSet = settings.getSource(project);
String defaultMain = settings.getDefaultMainClass();
if (defaultMain == null) {
throw new IllegalArgumentException("Run configuration '" + name + "' must specify 'defaultMainClass'");
}
if (configName == null) {
configName = "";
String srcName = sourceSet.getName();
if (!srcName.equals(SourceSet.MAIN_SOURCE_SET_NAME)) {
configName += capitalizeCamelCaseName(srcName) + " ";
}
configName += "Minecraft " + capitalizeCamelCaseName(name);
}
Objects.requireNonNull(environment, "No environment set for run config");
String runDir = settings.getRunDir();
if (runDir == null) {
runDir = "run";
}
RunConfig runConfig = new RunConfig();
runConfig.envVariables.putAll(settings.envVariables);
runConfig.configName = configName;
populate(project, extension, runConfig, environment);
runConfig.ideaModuleName = getIdeaModuleName(project, sourceSet);
runConfig.runDirIdeaUrl = "file://$PROJECT_DIR$/" + runDir;
runConfig.runDir = runDir;
runConfig.sourceSet = sourceSet;
// Custom parameters
for (String progArg : settings.getProgramArgs()) {
runConfig.programArgs += " " + progArg;
}
for (String vmArg : settings.getVmArgs()) {
runConfig.vmArgs += " " + vmArg;
}
runConfig.vmArgs += " -Dfabric.dli.main=" + getMainClass(environment, extension, defaultMain);
// Remove unnecessary leading/trailing whitespaces we might have generated
runConfig.programArgs = runConfig.programArgs.trim();
runConfig.vmArgs = runConfig.vmArgs.trim();
for (Consumer<RunConfig> consumer : extension.getSettingsPostEdit()) {
consumer.accept(runConfig);
}
return runConfig;
}
public String fromDummy(String dummy, boolean relativeDir, Project project) throws IOException {
String dummyConfig;
try (InputStream input = SetupIntelijRunConfigs.class.getClassLoader().getResourceAsStream(dummy)) {
dummyConfig = new String(input.readAllBytes(), StandardCharsets.UTF_8);
}
String runDir = this.runDir;
if (relativeDir && project.getRootProject() != project) {
Path rootPath = project.getRootDir().toPath();
Path projectPath = project.getProjectDir().toPath();
String relativePath = rootPath.relativize(projectPath).toString();
runDir = relativePath + "/" + runDir;
}
dummyConfig = dummyConfig.replace("%NAME%", configName);
dummyConfig = dummyConfig.replace("%MAIN_CLASS%", mainClass);
dummyConfig = dummyConfig.replace("%ECLIPSE_PROJECT%", eclipseProjectName);
dummyConfig = dummyConfig.replace("%IDEA_MODULE%", ideaModuleName);
dummyConfig = dummyConfig.replace("%RUN_DIRECTORY%", runDir);
dummyConfig = dummyConfig.replace("%PROGRAM_ARGS%", programArgs.replaceAll("\"", """));
dummyConfig = dummyConfig.replace("%VM_ARGS%", vmArgs.replaceAll("\"", """));
String envs = "";
if (!envVariables.isEmpty()) {
StringBuilder builder = new StringBuilder("<envs>");
for (Map.Entry<String, String> env : envVariables.entrySet()) {
builder.append("<env name=\"");
builder.append(env.getKey().replaceAll("\"", """));
builder.append("\" value=\"");
builder.append(env.getValue().replaceAll("\"", """));
builder.append("\"/>");
}
builder.append("</envs>");
envs = builder.toString();
}
dummyConfig = dummyConfig.replace("%ENVS%", envs);
return dummyConfig;
}
public static String getOSClientJVMArgs() {
if (OperatingSystem.getOS().equalsIgnoreCase("osx")) {
return " -XstartOnFirstThread";
}
return "";
}
private static String getMainClass(String side, LoomGradleExtension extension, String defaultMainClass) {
InstallerData installerData = extension.getInstallerData() == null ? null : extension.getInstallerData();
if (installerData == null) {
return defaultMainClass;
}
JsonObject installerJson = installerData.installerJson();
if (installerJson != null && installerJson.has("mainClass")) {
JsonElement mainClassJson = installerJson.get("mainClass");
String mainClassName = "";
if (mainClassJson.isJsonObject()) {
JsonObject mainClassesJson = mainClassJson.getAsJsonObject();
if (mainClassesJson.has(side)) {
mainClassName = mainClassesJson.get(side).getAsString();
}
} else {
mainClassName = mainClassJson.getAsString();
}
return mainClassName;
}
return defaultMainClass;
}
private static String encodeEscaped(String s) {
StringBuilder ret = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == '@' && i > 0 && s.charAt(i - 1) == '@' || c == ' ') {
ret.append(String.format("@@%04x", (int) c));
} else {
ret.append(c);
}
}
return ret.toString();
}
}
|