From 7e6ac568faca44b965ec8e58d11e3555e5ba5099 Mon Sep 17 00:00:00 2001 From: Timotei Dolean Date: Thu, 15 Jul 2010 08:25:12 +0000 Subject: [PATCH] eclipse plugin: add menu entry for starting the campaign in the game --- utils/java/eclipse_plugin/plugin.xml | 7 ++ .../action/OpenCampaignInGame.java | 30 +++++++ .../action/OpenScenarioInGame.java | 63 +------------- .../builder/ExternalToolInvoker.java | 16 ++-- .../utils/GameUtils.java | 86 +++++++++++++++++++ .../utils/ProjectUtils.java | 8 +- .../utils/ResourceUtils.java | 7 +- 7 files changed, 143 insertions(+), 74 deletions(-) create mode 100644 utils/java/eclipse_plugin/src/wesnoth_eclipse_plugin/action/OpenCampaignInGame.java create mode 100644 utils/java/eclipse_plugin/src/wesnoth_eclipse_plugin/utils/GameUtils.java diff --git a/utils/java/eclipse_plugin/plugin.xml b/utils/java/eclipse_plugin/plugin.xml index 9d6064eb075..4dbab3d2e2d 100644 --- a/utils/java/eclipse_plugin/plugin.xml +++ b/utils/java/eclipse_plugin/plugin.xml @@ -179,6 +179,13 @@ label="Upload Add-on" menubarPath="_wesnoth"> + + args = new ArrayList(); - args.add("-c"); - args.add(campaignId); - args.add(scenarioId); - - String wesnothExec = Preferences.getString(Constants.P_WESNOTH_EXEC_PATH); - if (wesnothExec.isEmpty()) - { - GUIUtils.showMessageBox(WorkspaceUtils.getWorkbenchWindow(), "Please set the wesnoth's executable path first."); - return; - } - - String workingDir = Preferences.getString(Constants.P_WESNOTH_WORKING_DIR); - - if (workingDir.isEmpty()) - workingDir = wesnothExec.substring(0, wesnothExec.lastIndexOf(new File(wesnothExec).getName())); - - // we need to add the working dir (backward compatibility) - args.add(workingDir); - System.out.printf("Launching args: %s \n", args); - ExternalToolInvoker.launchTool(wesnothExec, args, Constants.TI_SHOW_OUTPUT | Constants.TI_SHOW_OUTPUT_USER, true); - } catch (Exception e) - { - e.printStackTrace(); - } + GameUtils.runCampaignScenario(); } @Override diff --git a/utils/java/eclipse_plugin/src/wesnoth_eclipse_plugin/builder/ExternalToolInvoker.java b/utils/java/eclipse_plugin/src/wesnoth_eclipse_plugin/builder/ExternalToolInvoker.java index fdb1328547e..0216b0c7ede 100644 --- a/utils/java/eclipse_plugin/src/wesnoth_eclipse_plugin/builder/ExternalToolInvoker.java +++ b/utils/java/eclipse_plugin/src/wesnoth_eclipse_plugin/builder/ExternalToolInvoker.java @@ -35,7 +35,7 @@ public class ExternalToolInvoker /** * Creates an external tool invoker with specified options - * + * * @param fileName the file name to be invoked * @param arguments the arguments passed to the file * @param useThread true if the process will run in a thread @@ -89,7 +89,7 @@ public class ExternalToolInvoker /** * Waits for the current tool, and returns the return value - * + * * @return the return value of the tool */ public int waitForTool() @@ -197,7 +197,7 @@ public class ExternalToolInvoker /** * Gets the owned thread used to run the process. * This is non-null if the tool was invoked with "useThread=true" - * + * * @return */ public Thread getOwnThread() @@ -207,7 +207,7 @@ public class ExternalToolInvoker /** * Gets the attached thread, usually when someone runs this tool in another thread - * + * * @return */ public Thread getAttachedThread() @@ -217,7 +217,7 @@ public class ExternalToolInvoker /** * Sets the attached thread - * + * * @param thread */ public void setAttachedThread(Thread thread) @@ -227,7 +227,7 @@ public class ExternalToolInvoker /** * Launches the specified tool, with the specified argument list - * + * * @param fileName the full path to the executable to be launched * @param args the arguments list * @param outputFlags a composition of flags used for output @@ -242,11 +242,13 @@ public class ExternalToolInvoker { final ExternalToolInvoker toolInvoker = new ExternalToolInvoker(fileName, args, useThread); + System.out.println("Tool args: " + args); + MessageConsoleStream stream = null; if ((outputFlags & Constants.TI_SHOW_OUTPUT_USER) == Constants.TI_SHOW_OUTPUT_USER) { MessageConsole console = new MessageConsole("", null); - //console.activate(); + console.activate(); ConsolePlugin.getDefault().getConsoleManager().addConsoles(new IConsole[] { console }); stream = console.newMessageStream(); } diff --git a/utils/java/eclipse_plugin/src/wesnoth_eclipse_plugin/utils/GameUtils.java b/utils/java/eclipse_plugin/src/wesnoth_eclipse_plugin/utils/GameUtils.java new file mode 100644 index 00000000000..9c6e9887dc3 --- /dev/null +++ b/utils/java/eclipse_plugin/src/wesnoth_eclipse_plugin/utils/GameUtils.java @@ -0,0 +1,86 @@ +/** + * @author Timotei Dolean + * + */ +package wesnoth_eclipse_plugin.utils; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; + +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IResource; + +import wesnoth_eclipse_plugin.Constants; +import wesnoth_eclipse_plugin.builder.ExternalToolInvoker; +import wesnoth_eclipse_plugin.preferences.Preferences; + +public class GameUtils +{ + public static void runCampaignScenario() + { + if (WorkspaceUtils.getSelectedResource() == null) + { + GUIUtils.showMessageBox("Please select a campaign or a resource inside the " + + "campaign project before."); + return; + } + + IResource selectedResource = WorkspaceUtils.getSelectedResource(); + + //TODO: optimize this by checking if file really is a scenario + if (selectedResource instanceof IFile && + !ProjectUtils.isScenarioFile(WorkspaceUtils.getPathRelativeToUserDir(selectedResource))) + { + GUIUtils.showMessageBox("This is not a valid scenario file."); + return; + } + + try + { + String campaignId = ProjectUtils.getCampaignID(); + String scenarioId = ProjectUtils.getScenarioID( + WorkspaceUtils.getPathRelativeToUserDir(selectedResource)); + + if (campaignId == null) + { + GUIUtils.showMessageBox("You need to have a valid _main.cfg campaign file" + + " in your directory"); + return; + } + + List args = new ArrayList(); + args.add("-c"); + args.add(campaignId); + if (scenarioId != null) + args.add(scenarioId); + + String wesnothExec = Preferences.getString(Constants.P_WESNOTH_EXEC_PATH); + if (wesnothExec.isEmpty()) + { + GUIUtils.showMessageBox("Please set the wesnoth's executable path first."); + return; + } + + String workingDir = Preferences.getString(Constants.P_WESNOTH_WORKING_DIR); + + args.add("--config-dir"); + // add the user's data directory path + args.add(Preferences.getString(Constants.P_WESNOTH_USER_DIR)); + + if (workingDir.isEmpty()) + workingDir = wesnothExec.substring(0, + wesnothExec.lastIndexOf(new File(wesnothExec).getName())); + + // we need to add the working dir (backward compatibility) + args.add(workingDir); + + System.out.printf("Launching args: %s \n", args); + ExternalToolInvoker.launchTool(wesnothExec, args, + Constants.TI_SHOW_OUTPUT | Constants.TI_SHOW_OUTPUT_USER, true); + } catch (Exception e) + { + e.printStackTrace(); + } + } +} diff --git a/utils/java/eclipse_plugin/src/wesnoth_eclipse_plugin/utils/ProjectUtils.java b/utils/java/eclipse_plugin/src/wesnoth_eclipse_plugin/utils/ProjectUtils.java index e67860480e0..9163d65d4b9 100644 --- a/utils/java/eclipse_plugin/src/wesnoth_eclipse_plugin/utils/ProjectUtils.java +++ b/utils/java/eclipse_plugin/src/wesnoth_eclipse_plugin/utils/ProjectUtils.java @@ -17,6 +17,8 @@ public class ProjectUtils return null; String fileContents = ResourceUtils.getFileContents(file); + if (fileContents == null) + return null; int index = fileContents.indexOf(propertyName + "="); if (index == -1) return null; @@ -38,9 +40,7 @@ public class ProjectUtils } public static String getCampaignID() throws Exception { - if (WorkspaceUtils.getSelectedProject() == null && - WorkspaceUtils.getSelectedFile() == null && - WorkspaceUtils.getSelectedFolder() == null) + if (WorkspaceUtils.getSelectedResource() == null) { return null; } @@ -62,12 +62,14 @@ public class ProjectUtils public static boolean isCampaignFile(String fileName) { //TODO: replace this with a better checking + //TODO: check extension String fileContentString = ResourceUtils.getFileContents(new File(fileName)); return (fileContentString.contains("[campaign]") && fileContentString.contains("[/campaign]")); } public static boolean isScenarioFile(String fileName) { //TODO: replace this with a better checking + //TODO: check extension String fileContentString = ResourceUtils.getFileContents(new File(fileName)); return (fileContentString.contains("[scenario]") && fileContentString.contains("[/scenario]")); } diff --git a/utils/java/eclipse_plugin/src/wesnoth_eclipse_plugin/utils/ResourceUtils.java b/utils/java/eclipse_plugin/src/wesnoth_eclipse_plugin/utils/ResourceUtils.java index b176968c915..e9e643c0f22 100644 --- a/utils/java/eclipse_plugin/src/wesnoth_eclipse_plugin/utils/ResourceUtils.java +++ b/utils/java/eclipse_plugin/src/wesnoth_eclipse_plugin/utils/ResourceUtils.java @@ -8,6 +8,7 @@ import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; +import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; @@ -54,7 +55,7 @@ public class ResourceUtils public static String getFileContents(File file) { - if (!file.exists()) + if (!file.exists() || !file.isFile()) return null; String contentsString = ""; @@ -67,14 +68,14 @@ public class ResourceUtils { contentsString += (line + "\n"); } - } catch (Exception e) + } catch (IOException e) { e.printStackTrace(); - return null; } finally { try { + if (reader!= null) reader.close(); } catch (Exception e) {