diff --git a/src/main/java/covers1624/ccintelli/gui/CCIntelliSetupMainWindow.java b/src/main/java/covers1624/ccintelli/gui/CCIntelliSetupMainWindow.java index f212531..204e713 100644 --- a/src/main/java/covers1624/ccintelli/gui/CCIntelliSetupMainWindow.java +++ b/src/main/java/covers1624/ccintelli/gui/CCIntelliSetupMainWindow.java @@ -1048,7 +1048,6 @@ else if (e.getButton() == 3) { } } - if (moduleTree.getEditingPath() != null) { String editing = moduleTree.getEditingPath().getLastPathComponent().toString(); if (editing.equals("Modules") || editing.equals("Forge")) { @@ -1281,6 +1280,7 @@ private void importSetup(ActionEvent evt) { return; } GuiFields.importSetup(chooser.getSelectedFile()); + JOptionPane.showMessageDialog(this, "Successfully imported setup!"); reloadModuleTree(); reloadCorePluginList(); } @@ -1300,6 +1300,7 @@ private void exportSetup(ActionEvent evt) { return; } GuiFields.exportSetup(chooser.getSelectedFile()); + JOptionPane.showMessageDialog(this, "Successfully exported setup!"); return; } @@ -1307,9 +1308,10 @@ private void exportSetup(ActionEvent evt) { if (chooser.getFileFilter() == filter && !chooser.getSelectedFile().getAbsolutePath().endsWith(".json")) { GuiFields.exportSetup(new File(path + ".json")); - } - else { + JOptionPane.showMessageDialog(this, "Successfully exported setup!"); + } else { GuiFields.exportSetup(chooser.getSelectedFile()); + JOptionPane.showMessageDialog(this, "Successfully exported setup!"); } reloadModuleTree(); reloadCorePluginList(); diff --git a/src/main/java/covers1624/ccintelli/gui/GuiFields.java b/src/main/java/covers1624/ccintelli/gui/GuiFields.java index 36475b4..0580d82 100644 --- a/src/main/java/covers1624/ccintelli/gui/GuiFields.java +++ b/src/main/java/covers1624/ccintelli/gui/GuiFields.java @@ -1,5 +1,6 @@ package covers1624.ccintelli.gui; +import com.google.common.collect.ImmutableList; import covers1624.ccintelli.launch.SetupSerializer; import covers1624.ccintelli.module.Module; import covers1624.ccintelli.module.ModuleEntry; @@ -17,12 +18,13 @@ * Created by brandon3055 on 23/01/2017. */ public class GuiFields { + //The order of the entries in this list will be the order they show. //I can apply a comparator to the list before display to sort it. public static List modules = new LinkedList<>(); public static Module forgeModule; public static List fmlCorePlugins = new LinkedList<>(); - public static List vmArgs = new LinkedList<>(); + public static List vmArgs = new LinkedList<>(ImmutableList.of("-Xmx4G", "-Xms1G")); public static EnumLanguageLevel projectLangLevel = EnumLanguageLevel.JDK_1_8; public static EnumLanguageLevel projectBytecodeLevel = EnumLanguageLevel.JDK_1_8; @@ -72,9 +74,9 @@ public static void importSetup(File fileToImport) { public static void exportSetup(File targetFile) { try { SetupSerializer.writeSetup(targetFile); - LogHelper.info("Successfully exported %s modules to: %s", modules.size() - 1, targetFile.getAbsolutePath()); + LogHelper.info("Successfully exported %s modules to: %s", modules.size() - 1, targetFile.getAbsolutePath()); } catch (IOException e) { - LogHelper.errorError("Exception was thrown whils exporting modules to: %s", e, targetFile.getAbsolutePath()); + LogHelper.errorError("Exception was thrown whilst exporting modules to: %s", e, targetFile.getAbsolutePath()); } } } diff --git a/src/main/java/covers1624/ccintelli/launch/SetupSerializer.java b/src/main/java/covers1624/ccintelli/launch/SetupSerializer.java index c72a281..ac787b2 100644 --- a/src/main/java/covers1624/ccintelli/launch/SetupSerializer.java +++ b/src/main/java/covers1624/ccintelli/launch/SetupSerializer.java @@ -36,6 +36,10 @@ public static void readSetup(File json) throws IOException { for (JsonElement element : object.getAsJsonArray("core_plugins")) { GuiFields.fmlCorePlugins.add(element.getAsString()); } + GuiFields.vmArgs.clear(); + for (JsonElement element : object.getAsJsonArray("vm_args")) { + GuiFields.vmArgs.add(element.getAsString()); + } } public static void writeSetup(File json) throws IOException { @@ -56,6 +60,13 @@ public static void writeSetup(File json) throws IOException { corePlugins.add(corePlugin); } object.add("core_plugins", corePlugins); + + JsonArray vmArgs = new JsonArray(); + for (String arg : GuiFields.vmArgs) { + vmArgs.add(arg); + } + object.add("vm_args", vmArgs); + Streams.write(object, writer); writer.flush(); } diff --git a/src/main/java/covers1624/ccintelli/workspace/RunConfigGenerator.java b/src/main/java/covers1624/ccintelli/workspace/RunConfigGenerator.java new file mode 100644 index 0000000..9898825 --- /dev/null +++ b/src/main/java/covers1624/ccintelli/workspace/RunConfigGenerator.java @@ -0,0 +1,182 @@ +package covers1624.ccintelli.workspace; + +import covers1624.ccintelli.gui.GuiFields; +import covers1624.ccintelli.launch.Launch; +import org.w3c.dom.Document; +import org.w3c.dom.Element; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.transform.OutputKeys; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; +import java.io.File; +import java.util.ArrayList; +import java.util.List; + +import static covers1624.util.XMLUtils.createAndAdd; +import static covers1624.util.XMLUtils.getFirstElementNode; + +/** + * Created by covers1624 on 20/02/2017. + */ +public class RunConfigGenerator { + + public static void generateRunConfigs(String projectName) { + File workspaceIWS = new File(Launch.WORKSPACE, projectName + ".iws"); + try { + List vmArgs = new ArrayList<>(); + + vmArgs.addAll(GuiFields.vmArgs); + + if (!GuiFields.fmlCorePlugins.isEmpty()) { + StringBuilder builder = new StringBuilder("-Dfml.coreMods.load="); + boolean hasFirst = false; + for (String corePlugin : GuiFields.fmlCorePlugins) { + if (hasFirst) { + builder.append(","); + } + hasFirst = true; + builder.append(corePlugin); + } + vmArgs.add(builder.toString()); + } + + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + DocumentBuilder builder = factory.newDocumentBuilder(); + Document document = builder.parse(RunConfigGenerator.class.getResourceAsStream("/iwsTemplate.xml")); + document.getDocumentElement().normalize(); + + Element documentElement = document.getDocumentElement(); + Element componentElement = getFirstElementNode(documentElement, "component"); + { + Element clientRun = createAndAdd(document, componentElement, "configuration"); + clientRun.setAttribute("factoryName", "Application"); + clientRun.setAttribute("type", "Application"); + clientRun.setAttribute("name", "Minecraft Client"); + clientRun.setAttribute("default", "false"); + Element extension = createAndAdd(document, clientRun, "extension"); + extension.setAttribute("runner", "idea"); + extension.setAttribute("merge", "false"); + extension.setAttribute("enabled", "false"); + extension.setAttribute("name", "coverage"); + Element mainClassOpt = createAndAdd(document, clientRun, "option"); + mainClassOpt.setAttribute("value", "GradleStart"); + mainClassOpt.setAttribute("name", "MAIN_CLASS_NAME"); + StringBuilder argBuilder = new StringBuilder(); + boolean hasFirst = false; + for (String arg : vmArgs) { + if (hasFirst) { + argBuilder.append(" "); + } + hasFirst = true; + argBuilder.append(arg); + } + Element vmArgsOpt = createAndAdd(document, clientRun, "option"); + vmArgsOpt.setAttribute("value", argBuilder.toString()); + vmArgsOpt.setAttribute("name", "VM_PARAMETERS"); + Element progArgsOpt = createAndAdd(document, clientRun, "option"); + progArgsOpt.setAttribute("value", ""); + progArgsOpt.setAttribute("name", "PROGRAM_PARAMETERS"); + Element workingDirOpt = createAndAdd(document, clientRun, "option"); + workingDirOpt.setAttribute("value", "file://" + Launch.PROJECT_RUN.getAbsoluteFile().getPath()); + workingDirOpt.setAttribute("name", "WORKING_DIRECTORY"); + Element altJERpathEnableOpt = createAndAdd(document, clientRun, "option"); + altJERpathEnableOpt.setAttribute("value", "false"); + altJERpathEnableOpt.setAttribute("name", "ALTERNATIVE_JRE_PATH_ENABLED"); + Element aldJrePathOpt = createAndAdd(document, clientRun, "option"); + aldJrePathOpt.setAttribute("name", "ALTERNATIVE_JRE_PATH"); + Element swingInspecOpt = createAndAdd(document, clientRun, "option"); + swingInspecOpt.setAttribute("value", "false"); + swingInspecOpt.setAttribute("name", "ENABLE_SWING_INSPECTOR"); + Element envVarsOpt = createAndAdd(document, clientRun, "option"); + envVarsOpt.setAttribute("name", "ENV_VARIABLES"); + Element passParentEnvsOpt = createAndAdd(document, clientRun, "option"); + passParentEnvsOpt.setAttribute("value", "true"); + passParentEnvsOpt.setAttribute("name", "PASS_PARENT_ENVS"); + + Element moduleClassPathElement = createAndAdd(document, clientRun, "module"); + moduleClassPathElement.setAttribute("name", GuiFields.forgeModule.NAME); + createAndAdd(document, clientRun, "envs"); + createAndAdd(document, clientRun, "method"); + } + { + Element serverRun = createAndAdd(document, componentElement, "configuration"); + serverRun.setAttribute("factoryName", "Application"); + serverRun.setAttribute("type", "Application"); + serverRun.setAttribute("name", "Minecraft Server"); + serverRun.setAttribute("default", "false"); + Element extension = createAndAdd(document, serverRun, "extension"); + extension.setAttribute("runner", "idea"); + extension.setAttribute("merge", "false"); + extension.setAttribute("enabled", "false"); + extension.setAttribute("name", "coverage"); + Element mainClassOpt = createAndAdd(document, serverRun, "option"); + mainClassOpt.setAttribute("value", "GradleStartServer"); + mainClassOpt.setAttribute("name", "MAIN_CLASS_NAME"); + StringBuilder argBuilder = new StringBuilder(); + boolean hasFirst = false; + for (String arg : vmArgs) { + if (hasFirst) { + argBuilder.append(" "); + } + hasFirst = true; + argBuilder.append(arg); + } + Element vmArgsOpt = createAndAdd(document, serverRun, "option"); + vmArgsOpt.setAttribute("value", argBuilder.toString()); + vmArgsOpt.setAttribute("name", "VM_PARAMETERS"); + Element progArgsOpt = createAndAdd(document, serverRun, "option"); + progArgsOpt.setAttribute("value", ""); + progArgsOpt.setAttribute("name", "PROGRAM_PARAMETERS"); + Element workingDirOpt = createAndAdd(document, serverRun, "option"); + workingDirOpt.setAttribute("value", "file://" + Launch.PROJECT_RUN.getAbsoluteFile().getPath()); + workingDirOpt.setAttribute("name", "WORKING_DIRECTORY"); + Element altJERpathEnableOpt = createAndAdd(document, serverRun, "option"); + altJERpathEnableOpt.setAttribute("value", "false"); + altJERpathEnableOpt.setAttribute("name", "ALTERNATIVE_JRE_PATH_ENABLED"); + Element aldJrePathOpt = createAndAdd(document, serverRun, "option"); + aldJrePathOpt.setAttribute("name", "ALTERNATIVE_JRE_PATH"); + Element swingInspecOpt = createAndAdd(document, serverRun, "option"); + swingInspecOpt.setAttribute("value", "false"); + swingInspecOpt.setAttribute("name", "ENABLE_SWING_INSPECTOR"); + Element envVarsOpt = createAndAdd(document, serverRun, "option"); + envVarsOpt.setAttribute("name", "ENV_VARIABLES"); + Element passParentEnvsOpt = createAndAdd(document, serverRun, "option"); + passParentEnvsOpt.setAttribute("value", "true"); + passParentEnvsOpt.setAttribute("name", "PASS_PARENT_ENVS"); + + Element moduleClassPathElement = createAndAdd(document, serverRun, "module"); + moduleClassPathElement.setAttribute("name", GuiFields.forgeModule.NAME); + createAndAdd(document, serverRun, "envs"); + createAndAdd(document, serverRun, "method"); + } + + Element listElement = getFirstElementNode(componentElement, "list"); + listElement.setAttribute("size", "2"); + Element clientChild = createAndAdd(document, listElement, "item"); + clientChild.setAttribute("itemvalue", "Application.Minecraft Client"); + clientChild.setAttribute("class", "java.lang.String"); + clientChild.setAttribute("index", "0"); + + Element serverChild = createAndAdd(document, listElement, "item"); + serverChild.setAttribute("itemvalue", "Application.Minecraft Server"); + serverChild.setAttribute("class", "java.lang.String"); + serverChild.setAttribute("index", "1"); + + TransformerFactory transformerFactory = TransformerFactory.newInstance(); + Transformer transformer = transformerFactory.newTransformer(); + transformer.setOutputProperty(OutputKeys.INDENT, "yes"); + transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); + DOMSource source = new DOMSource(document); + StreamResult result = new StreamResult(workspaceIWS); + transformer.transform(source, result); + + } catch (Exception e) { + e.printStackTrace(); + //throw new RuntimeException("Exception thrown whilst generating run configs.", e); + } + } +} diff --git a/src/main/java/covers1624/ccintelli/workspace/WorkspaceGenerator.java b/src/main/java/covers1624/ccintelli/workspace/WorkspaceGenerator.java index eadd1e8..9dbd180 100644 --- a/src/main/java/covers1624/ccintelli/workspace/WorkspaceGenerator.java +++ b/src/main/java/covers1624/ccintelli/workspace/WorkspaceGenerator.java @@ -3,6 +3,7 @@ import com.google.common.base.Strings; import covers1624.ccintelli.gui.GuiFields; import covers1624.ccintelli.launch.Launch; +import covers1624.ccintelli.launch.SetupSerializer; import covers1624.ccintelli.module.Module; import covers1624.ccintelli.util.ATFileFilter; import covers1624.ccintelli.util.ResourceWalker; @@ -11,7 +12,10 @@ import org.apache.commons.io.IOUtils; import java.io.*; -import java.util.*; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; import java.util.Map.Entry; /** @@ -32,6 +36,8 @@ public static void generateWorkspace(String projectName) { runForgeSetup(); exportModules(); ProjectGenerator.generateWorkspace(projectName); + RunConfigGenerator.generateRunConfigs(projectName); + exportCurrentSetup(projectName); LogHelper.info("Done!"); } @@ -167,4 +173,14 @@ private static void runProcessAndLog(Process process) throws Exception { } } + private static void exportCurrentSetup(String projectName) { + try { + File export = new File(Launch.WORKSPACE, projectName + ".json"); + SetupSerializer.writeSetup(export); + LogHelper.info("Your current workspace has been exported to: " + export.getAbsoluteFile().getPath()); + } catch (Exception e) { + LogHelper.errorError("Exception was thrown whilst exporting setup..", e); + } + } + } diff --git a/src/main/java/covers1624/util/NodeListIterator.java b/src/main/java/covers1624/util/NodeListIterator.java new file mode 100644 index 0000000..0e0a7cb --- /dev/null +++ b/src/main/java/covers1624/util/NodeListIterator.java @@ -0,0 +1,38 @@ +package covers1624.util; + +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +import java.util.Iterator; + +/** + * Created by covers1624 on 28/02/2017. + */ +public class NodeListIterator implements Iterator, Iterable { + + private final NodeList list; + private int index; + + public NodeListIterator(NodeList list) { + this.list = list; + } + + @Override + public boolean hasNext() { + return list != null && index < list.getLength(); + } + + @Override + public Node next() { + Node object = list.item(index); + if (object != null) { + index++; + } + return object; + } + + @Override + public Iterator iterator() { + return this; + } +} diff --git a/src/main/java/covers1624/util/XMLUtils.java b/src/main/java/covers1624/util/XMLUtils.java new file mode 100644 index 0000000..752bf29 --- /dev/null +++ b/src/main/java/covers1624/util/XMLUtils.java @@ -0,0 +1,51 @@ +package covers1624.util; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; + +import java.util.Objects; + +/** + * Created by covers1624 on 28/02/2017. + */ +public class XMLUtils { + + /** + * Walks the provided elements child nodes for the first Element node. + * + * @param element The element in which to walk the child nodes + * @return The first element, Null if no Element nodes exist. + */ + public static Element getFirstElementNode(Element element) { + for (Node node : new NodeListIterator(element.getChildNodes())) { + if (node.getNodeType() == Node.ELEMENT_NODE) { + return ((Element) node); + } + } + return null; + } + + /** + * Walks the provided elements child nodes for the first Element node with the given name. + * + * @param element The element in which to walk the child nodes + * @param name The name for the child element to find. + * @return The first element, Null if no Element nodes exist with the given name. + */ + public static Element getFirstElementNode(Element element, String name) { + for (Node node : new NodeListIterator(element.getChildNodes())) { + if (node.getNodeType() == Node.ELEMENT_NODE && Objects.equals(node.getNodeName(), name)) { + return ((Element) node); + } + } + return null; + } + + public static Element createAndAdd(Document document, Element parent, String name) { + Element element = document.createElement(name); + parent.appendChild(element); + return element; + } + +} diff --git a/src/main/resources/iwsTemplate.xml b/src/main/resources/iwsTemplate.xml new file mode 100644 index 0000000..e977a4d --- /dev/null +++ b/src/main/resources/iwsTemplate.xml @@ -0,0 +1,225 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + localhost + 5050 + + +