2016-08-16 22:29:10 +00:00
|
|
|
/*
|
|
|
|
* This file is part of fabric-loom, licensed under the MIT License (MIT).
|
|
|
|
*
|
2021-07-10 20:50:53 +00:00
|
|
|
* Copyright (c) 2016-2021 FabricMC
|
2016-08-16 22:29:10 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2020-12-24 20:58:30 +00:00
|
|
|
package net.fabricmc.loom.configuration.ide;
|
2020-07-26 20:05:14 +00:00
|
|
|
|
2019-11-02 20:23:27 +00:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
import java.util.Map;
|
2021-01-31 00:09:40 +00:00
|
|
|
import java.util.Objects;
|
2019-11-02 20:23:27 +00:00
|
|
|
|
2016-08-15 10:22:14 +00:00
|
|
|
import com.google.common.base.Strings;
|
|
|
|
import com.google.common.collect.ImmutableMap;
|
2018-12-22 09:31:10 +00:00
|
|
|
import com.google.gson.JsonElement;
|
|
|
|
import com.google.gson.JsonObject;
|
2020-12-24 20:58:30 +00:00
|
|
|
import org.gradle.api.Project;
|
2021-01-29 18:04:39 +00:00
|
|
|
import org.gradle.api.tasks.SourceSet;
|
2020-12-24 20:58:30 +00:00
|
|
|
import org.gradle.plugins.ide.eclipse.model.EclipseModel;
|
2016-08-15 10:22:14 +00:00
|
|
|
import org.w3c.dom.Document;
|
|
|
|
import org.w3c.dom.Element;
|
|
|
|
import org.w3c.dom.Node;
|
|
|
|
|
2019-11-02 20:23:27 +00:00
|
|
|
import net.fabricmc.loom.LoomGradleExtension;
|
2020-12-24 20:58:30 +00:00
|
|
|
import net.fabricmc.loom.util.OperatingSystem;
|
2016-08-15 10:22:14 +00:00
|
|
|
|
2018-12-12 13:43:42 +00:00
|
|
|
public class RunConfig {
|
2016-08-17 16:38:54 +00:00
|
|
|
public String configName;
|
2020-07-26 20:05:14 +00:00
|
|
|
public String eclipseProjectName;
|
|
|
|
public String ideaModuleName;
|
2016-08-17 16:38:54 +00:00
|
|
|
public String mainClass;
|
2021-01-29 18:04:39 +00:00
|
|
|
public String runDirIdeaUrl;
|
2016-08-17 16:38:54 +00:00
|
|
|
public String runDir;
|
|
|
|
public String vmArgs;
|
|
|
|
public String programArgs;
|
2021-03-03 21:22:10 +00:00
|
|
|
public SourceSet sourceSet;
|
2016-08-15 10:22:14 +00:00
|
|
|
|
2020-07-26 20:32:10 +00:00
|
|
|
public Element genRuns(Element doc) {
|
2016-08-17 16:38:54 +00:00
|
|
|
Element root = this.addXml(doc, "component", ImmutableMap.of("name", "ProjectRunConfigurationManager"));
|
|
|
|
root = addXml(root, "configuration", ImmutableMap.of("default", "false", "name", configName, "type", "Application", "factoryName", "Application"));
|
2016-08-15 10:22:14 +00:00
|
|
|
|
2020-07-26 20:05:14 +00:00
|
|
|
this.addXml(root, "module", ImmutableMap.of("name", ideaModuleName));
|
2016-08-17 16:38:54 +00:00
|
|
|
this.addXml(root, "option", ImmutableMap.of("name", "MAIN_CLASS_NAME", "value", mainClass));
|
2021-01-29 18:04:39 +00:00
|
|
|
this.addXml(root, "option", ImmutableMap.of("name", "WORKING_DIRECTORY", "value", runDirIdeaUrl));
|
2016-08-15 10:22:14 +00:00
|
|
|
|
2016-08-17 16:38:54 +00:00
|
|
|
if (!Strings.isNullOrEmpty(vmArgs)) {
|
|
|
|
this.addXml(root, "option", ImmutableMap.of("name", "VM_PARAMETERS", "value", vmArgs));
|
|
|
|
}
|
2016-08-17 14:45:04 +00:00
|
|
|
|
2016-08-17 16:38:54 +00:00
|
|
|
if (!Strings.isNullOrEmpty(programArgs)) {
|
|
|
|
this.addXml(root, "option", ImmutableMap.of("name", "PROGRAM_PARAMETERS", "value", programArgs));
|
|
|
|
}
|
2019-11-02 20:23:27 +00:00
|
|
|
|
2016-08-17 16:38:54 +00:00
|
|
|
return root;
|
|
|
|
}
|
2016-08-15 10:22:14 +00:00
|
|
|
|
2016-08-17 16:38:54 +00:00
|
|
|
public Element addXml(Node parent, String name, Map<String, String> values) {
|
|
|
|
Document doc = parent.getOwnerDocument();
|
2019-11-02 20:23:27 +00:00
|
|
|
|
2016-08-17 16:38:54 +00:00
|
|
|
if (doc == null) {
|
|
|
|
doc = (Document) parent;
|
|
|
|
}
|
2016-08-15 10:22:14 +00:00
|
|
|
|
2016-08-17 16:38:54 +00:00
|
|
|
Element e = doc.createElement(name);
|
2019-11-02 20:23:27 +00:00
|
|
|
|
2016-08-17 16:38:54 +00:00
|
|
|
for (Map.Entry<String, String> entry : values.entrySet()) {
|
|
|
|
e.setAttribute(entry.getKey(), entry.getValue());
|
|
|
|
}
|
2019-11-02 20:23:27 +00:00
|
|
|
|
2016-08-17 16:38:54 +00:00
|
|
|
parent.appendChild(e);
|
|
|
|
return e;
|
|
|
|
}
|
2018-11-05 12:00:54 +00:00
|
|
|
|
2021-01-29 18:04:39 +00:00
|
|
|
private static String getIdeaModuleName(Project project, SourceSet srcs) {
|
|
|
|
String module = project.getName() + "." + srcs.getName();
|
2020-07-26 20:05:14 +00:00
|
|
|
|
|
|
|
while ((project = project.getParent()) != null) {
|
|
|
|
module = project.getName() + "." + module;
|
|
|
|
}
|
|
|
|
|
|
|
|
return module;
|
|
|
|
}
|
|
|
|
|
2021-01-31 00:09:40 +00:00
|
|
|
private static void populate(Project project, LoomGradleExtension extension, RunConfig runConfig, String environment) {
|
2020-12-24 20:58:30 +00:00
|
|
|
runConfig.configName += extension.isRootProject() ? "" : " (" + project.getPath() + ")";
|
2020-07-26 20:05:14 +00:00
|
|
|
runConfig.eclipseProjectName = project.getExtensions().getByType(EclipseModel.class).getProject().getName();
|
2019-11-11 18:52:19 +00:00
|
|
|
runConfig.vmArgs = "";
|
2021-01-29 18:04:39 +00:00
|
|
|
runConfig.programArgs = "";
|
2018-12-22 09:31:10 +00:00
|
|
|
|
2021-07-13 23:03:21 +00:00
|
|
|
runConfig.mainClass = "net.fabricmc.devlaunchinjector.Main";
|
|
|
|
runConfig.vmArgs = "-XX:+ShowCodeDetailsInExceptionMessages -Dfabric.dli.config=" + encodeEscaped(extension.getFiles().getDevLauncherConfig().getAbsolutePath()) + " -Dfabric.dli.env=" + environment.toLowerCase();
|
2018-12-22 09:31:10 +00:00
|
|
|
}
|
|
|
|
|
2021-01-29 18:04:39 +00:00
|
|
|
// Turns camelCase/PascalCase into Capital Case
|
|
|
|
// caseConversionExample -> Case Conversion Example
|
|
|
|
private static String capitalizeCamelCaseName(String name) {
|
|
|
|
if (name.length() == 0) {
|
|
|
|
return "";
|
|
|
|
}
|
2018-11-05 12:00:54 +00:00
|
|
|
|
2021-01-29 18:04:39 +00:00
|
|
|
return name.substring(0, 1).toUpperCase() + name.substring(1).replaceAll("([^A-Z])([A-Z])", "$1 $2");
|
2018-11-05 12:00:54 +00:00
|
|
|
}
|
|
|
|
|
2021-01-29 18:04:39 +00:00
|
|
|
public static RunConfig runConfig(Project project, RunConfigSettings settings) {
|
2021-07-13 23:03:21 +00:00
|
|
|
LoomGradleExtension extension = LoomGradleExtension.get(project);
|
2021-01-29 18:04:39 +00:00
|
|
|
String name = settings.getName();
|
2018-11-05 12:00:54 +00:00
|
|
|
|
2021-01-29 18:04:39 +00:00
|
|
|
String configName = settings.getConfigName();
|
2021-01-31 00:09:40 +00:00
|
|
|
String environment = settings.getEnvironment();
|
2021-01-29 18:04:39 +00:00
|
|
|
SourceSet sourceSet = settings.getSource(project);
|
2018-11-05 12:00:54 +00:00
|
|
|
|
2021-01-29 18:04:39 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2021-01-31 00:09:40 +00:00
|
|
|
Objects.requireNonNull(environment, "No environment set for run config");
|
2021-01-29 18:04:39 +00:00
|
|
|
|
|
|
|
String runDir = settings.getRunDir();
|
|
|
|
|
|
|
|
if (runDir == null) {
|
|
|
|
runDir = "run";
|
|
|
|
}
|
|
|
|
|
|
|
|
RunConfig runConfig = new RunConfig();
|
|
|
|
runConfig.configName = configName;
|
2021-01-31 00:09:40 +00:00
|
|
|
populate(project, extension, runConfig, environment);
|
2021-01-29 18:04:39 +00:00
|
|
|
runConfig.ideaModuleName = getIdeaModuleName(project, sourceSet);
|
|
|
|
runConfig.runDirIdeaUrl = "file://$PROJECT_DIR$/" + runDir;
|
|
|
|
runConfig.runDir = runDir;
|
2021-03-03 21:22:10 +00:00
|
|
|
runConfig.sourceSet = sourceSet;
|
2021-01-29 18:04:39 +00:00
|
|
|
|
|
|
|
// Custom parameters
|
|
|
|
for (String progArg : settings.getProgramArgs()) {
|
|
|
|
runConfig.programArgs += " " + progArg;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (String vmArg : settings.getVmArgs()) {
|
|
|
|
runConfig.vmArgs += " " + vmArg;
|
|
|
|
}
|
|
|
|
|
2021-01-31 00:09:40 +00:00
|
|
|
runConfig.vmArgs += " -Dfabric.dli.main=" + getMainClass(environment, extension, defaultMain);
|
2021-01-29 18:04:39 +00:00
|
|
|
|
|
|
|
// Remove unnecessary leading/trailing whitespaces we might have generated
|
|
|
|
runConfig.programArgs = runConfig.programArgs.trim();
|
|
|
|
runConfig.vmArgs = runConfig.vmArgs.trim();
|
2018-11-05 12:00:54 +00:00
|
|
|
|
2021-01-29 18:04:39 +00:00
|
|
|
return runConfig;
|
2019-11-11 18:52:19 +00:00
|
|
|
}
|
|
|
|
|
2018-12-12 13:43:42 +00:00
|
|
|
public String fromDummy(String dummy) throws IOException {
|
2019-04-21 22:39:09 +00:00
|
|
|
String dummyConfig;
|
2019-11-02 20:23:27 +00:00
|
|
|
|
2019-04-21 22:39:09 +00:00
|
|
|
try (InputStream input = SetupIntelijRunConfigs.class.getClassLoader().getResourceAsStream(dummy)) {
|
2021-05-13 21:06:34 +00:00
|
|
|
dummyConfig = new String(input.readAllBytes(), StandardCharsets.UTF_8);
|
2019-04-21 22:39:09 +00:00
|
|
|
}
|
2018-11-05 12:00:54 +00:00
|
|
|
|
|
|
|
dummyConfig = dummyConfig.replace("%NAME%", configName);
|
|
|
|
dummyConfig = dummyConfig.replace("%MAIN_CLASS%", mainClass);
|
2020-07-26 20:05:14 +00:00
|
|
|
dummyConfig = dummyConfig.replace("%ECLIPSE_PROJECT%", eclipseProjectName);
|
|
|
|
dummyConfig = dummyConfig.replace("%IDEA_MODULE%", ideaModuleName);
|
2021-01-29 18:04:39 +00:00
|
|
|
dummyConfig = dummyConfig.replace("%RUN_DIRECTORY%", runDir);
|
2018-11-05 12:00:54 +00:00
|
|
|
dummyConfig = dummyConfig.replace("%PROGRAM_ARGS%", programArgs.replaceAll("\"", """));
|
|
|
|
dummyConfig = dummyConfig.replace("%VM_ARGS%", vmArgs.replaceAll("\"", """));
|
|
|
|
|
|
|
|
return dummyConfig;
|
|
|
|
}
|
2018-12-12 18:10:47 +00:00
|
|
|
|
2019-11-02 20:23:27 +00:00
|
|
|
public static String getOSClientJVMArgs() {
|
|
|
|
if (OperatingSystem.getOS().equalsIgnoreCase("osx")) {
|
2018-12-12 18:10:47 +00:00
|
|
|
return " -XstartOnFirstThread";
|
|
|
|
}
|
2019-11-02 20:23:27 +00:00
|
|
|
|
2018-12-12 18:10:47 +00:00
|
|
|
return "";
|
|
|
|
}
|
2019-11-11 18:52:19 +00:00
|
|
|
|
2021-01-29 18:04:39 +00:00
|
|
|
private static String getMainClass(String side, LoomGradleExtension extension, String defaultMainClass) {
|
2021-07-13 22:10:07 +00:00
|
|
|
JsonObject installerJson = extension.getInstallerData().installerJson();
|
2019-11-11 18:52:19 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-01-29 18:04:39 +00:00
|
|
|
return defaultMainClass;
|
2019-11-11 18:52:19 +00:00
|
|
|
}
|
2019-11-15 09:11:04 +00:00
|
|
|
|
0.2.7 refactors (#178)
* Rough work on project based jars, skeleton for AccessEscalators?
* First working draft
* Minor changes
* Add support for mutable, better error checking when parsing file.
Code cleanup
Remap if needed when reading
* Fix inner classes and genSources
* Fix CME
* Caching, only regen jar when input changes
* Some work, untested
* Fix writing, fix checkstyle issues
* More fixes
* Move jars into a maven file structure, cleans up the file structure, and will benefit idea 2020
Add some basic validation to the AccessWidenerRemapper, will present any issues with the mappings when building (May need a way to disable?)
+ Some bugs fixes
* Fix issues with source jars in idea 2020, should be backwards compatible with 2019
* Move to lorenz-tiny
* Build fix + small cleanup
* Remove accesswidener's for now
* Update dev launch injector, should fix all issues with spaces in the path.
2020-03-06 11:15:34 +00:00
|
|
|
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 == ' ') {
|
2020-03-16 22:29:05 +00:00
|
|
|
ret.append(String.format("@@%04x", (int) c));
|
0.2.7 refactors (#178)
* Rough work on project based jars, skeleton for AccessEscalators?
* First working draft
* Minor changes
* Add support for mutable, better error checking when parsing file.
Code cleanup
Remap if needed when reading
* Fix inner classes and genSources
* Fix CME
* Caching, only regen jar when input changes
* Some work, untested
* Fix writing, fix checkstyle issues
* More fixes
* Move jars into a maven file structure, cleans up the file structure, and will benefit idea 2020
Add some basic validation to the AccessWidenerRemapper, will present any issues with the mappings when building (May need a way to disable?)
+ Some bugs fixes
* Fix issues with source jars in idea 2020, should be backwards compatible with 2019
* Move to lorenz-tiny
* Build fix + small cleanup
* Remove accesswidener's for now
* Update dev launch injector, should fix all issues with spaces in the path.
2020-03-06 11:15:34 +00:00
|
|
|
} else {
|
|
|
|
ret.append(c);
|
|
|
|
}
|
2019-11-15 09:11:04 +00:00
|
|
|
}
|
|
|
|
|
0.2.7 refactors (#178)
* Rough work on project based jars, skeleton for AccessEscalators?
* First working draft
* Minor changes
* Add support for mutable, better error checking when parsing file.
Code cleanup
Remap if needed when reading
* Fix inner classes and genSources
* Fix CME
* Caching, only regen jar when input changes
* Some work, untested
* Fix writing, fix checkstyle issues
* More fixes
* Move jars into a maven file structure, cleans up the file structure, and will benefit idea 2020
Add some basic validation to the AccessWidenerRemapper, will present any issues with the mappings when building (May need a way to disable?)
+ Some bugs fixes
* Fix issues with source jars in idea 2020, should be backwards compatible with 2019
* Move to lorenz-tiny
* Build fix + small cleanup
* Remove accesswidener's for now
* Update dev launch injector, should fix all issues with spaces in the path.
2020-03-06 11:15:34 +00:00
|
|
|
return ret.toString();
|
2019-11-15 09:11:04 +00:00
|
|
|
}
|
2019-11-02 20:23:27 +00:00
|
|
|
}
|