eclipse plugin: add menu entry for starting the campaign in the game

This commit is contained in:
Timotei Dolean 2010-07-15 08:25:12 +00:00
parent 69c1cb8f26
commit 7e6ac568fa
7 changed files with 143 additions and 74 deletions

View file

@ -179,6 +179,13 @@
label="Upload Add-on"
menubarPath="_wesnoth">
</action>
<action
class="wesnoth_eclipse_plugin.action.OpenCampaignInGame"
icon="icons/wesnoth-icon_16.png"
id="action.container.startCampaign"
label="Open campaign in game"
menubarPath="_wesnoth">
</action>
</objectContribution>
</extension>
<extension

View file

@ -0,0 +1,30 @@
package wesnoth_eclipse_plugin.action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
import wesnoth_eclipse_plugin.utils.GameUtils;
public class OpenCampaignInGame implements IObjectActionDelegate
{
public OpenCampaignInGame() {
}
@Override
public void setActivePart(IAction action, IWorkbenchPart targetPart)
{
}
@Override
public void run(IAction action)
{
GameUtils.runCampaignScenario();
}
@Override
public void selectionChanged(IAction action, ISelection selection)
{
}
}

View file

@ -1,21 +1,11 @@
package wesnoth_eclipse_plugin.action;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.core.resources.IFile;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
import wesnoth_eclipse_plugin.Constants;
import wesnoth_eclipse_plugin.builder.ExternalToolInvoker;
import wesnoth_eclipse_plugin.preferences.Preferences;
import wesnoth_eclipse_plugin.utils.GUIUtils;
import wesnoth_eclipse_plugin.utils.ProjectUtils;
import wesnoth_eclipse_plugin.utils.WorkspaceUtils;
import wesnoth_eclipse_plugin.utils.GameUtils;
public class OpenScenarioInGame implements IObjectActionDelegate
{
@ -30,56 +20,7 @@ public class OpenScenarioInGame implements IObjectActionDelegate
@Override
public void run(IAction action)
{
if (WorkspaceUtils.getSelectedProject() == null &&
WorkspaceUtils.getSelectedFile() == null &&
WorkspaceUtils.getSelectedFolder() == null)
{
GUIUtils.showMessageBox(WorkspaceUtils.getWorkbenchWindow(),
"Please select a campaign or a resource inside the campaign project before.");
return;
}
IFile selectedFile = WorkspaceUtils.getSelectedFile(WorkspaceUtils.getWorkbenchWindow());
if (selectedFile == null)
return;
//TODO: optimize this by checking if file really is a scenario (PersistentProperties?)
if (!ProjectUtils.isScenarioFile(WorkspaceUtils.getPathRelativeToUserDir(selectedFile)))
{
GUIUtils.showMessageBox(WorkspaceUtils.getWorkbenchWindow(), "This is not a scenario file.");
return;
}
try
{
String campaignId = ProjectUtils.getCampaignID();
String scenarioId = ProjectUtils.getScenarioID(WorkspaceUtils.getPathRelativeToUserDir(selectedFile));
List<String> args = new ArrayList<String>();
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

View file

@ -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();
}

View file

@ -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<String> args = new ArrayList<String>();
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();
}
}
}

View file

@ -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]"));
}

View file

@ -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)
{