mirror of
https://github.com/wesnoth/wesnoth
synced 2025-05-07 20:54:52 +00:00
eclipse plugin: add menu entry for starting the campaign in the game
This commit is contained in:
parent
69c1cb8f26
commit
7e6ac568fa
@ -179,6 +179,13 @@
|
|||||||
label="Upload Add-on"
|
label="Upload Add-on"
|
||||||
menubarPath="_wesnoth">
|
menubarPath="_wesnoth">
|
||||||
</action>
|
</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>
|
</objectContribution>
|
||||||
</extension>
|
</extension>
|
||||||
<extension
|
<extension
|
||||||
|
@ -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)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
@ -1,21 +1,11 @@
|
|||||||
package wesnoth_eclipse_plugin.action;
|
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.action.IAction;
|
||||||
import org.eclipse.jface.viewers.ISelection;
|
import org.eclipse.jface.viewers.ISelection;
|
||||||
import org.eclipse.ui.IObjectActionDelegate;
|
import org.eclipse.ui.IObjectActionDelegate;
|
||||||
import org.eclipse.ui.IWorkbenchPart;
|
import org.eclipse.ui.IWorkbenchPart;
|
||||||
|
|
||||||
import wesnoth_eclipse_plugin.Constants;
|
import wesnoth_eclipse_plugin.utils.GameUtils;
|
||||||
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;
|
|
||||||
|
|
||||||
public class OpenScenarioInGame implements IObjectActionDelegate
|
public class OpenScenarioInGame implements IObjectActionDelegate
|
||||||
{
|
{
|
||||||
@ -30,56 +20,7 @@ public class OpenScenarioInGame implements IObjectActionDelegate
|
|||||||
@Override
|
@Override
|
||||||
public void run(IAction action)
|
public void run(IAction action)
|
||||||
{
|
{
|
||||||
if (WorkspaceUtils.getSelectedProject() == null &&
|
GameUtils.runCampaignScenario();
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -242,11 +242,13 @@ public class ExternalToolInvoker
|
|||||||
{
|
{
|
||||||
final ExternalToolInvoker toolInvoker = new ExternalToolInvoker(fileName, args, useThread);
|
final ExternalToolInvoker toolInvoker = new ExternalToolInvoker(fileName, args, useThread);
|
||||||
|
|
||||||
|
System.out.println("Tool args: " + args);
|
||||||
|
|
||||||
MessageConsoleStream stream = null;
|
MessageConsoleStream stream = null;
|
||||||
if ((outputFlags & Constants.TI_SHOW_OUTPUT_USER) == Constants.TI_SHOW_OUTPUT_USER)
|
if ((outputFlags & Constants.TI_SHOW_OUTPUT_USER) == Constants.TI_SHOW_OUTPUT_USER)
|
||||||
{
|
{
|
||||||
MessageConsole console = new MessageConsole("", null);
|
MessageConsole console = new MessageConsole("", null);
|
||||||
//console.activate();
|
console.activate();
|
||||||
ConsolePlugin.getDefault().getConsoleManager().addConsoles(new IConsole[] { console });
|
ConsolePlugin.getDefault().getConsoleManager().addConsoles(new IConsole[] { console });
|
||||||
stream = console.newMessageStream();
|
stream = console.newMessageStream();
|
||||||
}
|
}
|
||||||
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -17,6 +17,8 @@ public class ProjectUtils
|
|||||||
return null;
|
return null;
|
||||||
|
|
||||||
String fileContents = ResourceUtils.getFileContents(file);
|
String fileContents = ResourceUtils.getFileContents(file);
|
||||||
|
if (fileContents == null)
|
||||||
|
return null;
|
||||||
int index = fileContents.indexOf(propertyName + "=");
|
int index = fileContents.indexOf(propertyName + "=");
|
||||||
if (index == -1)
|
if (index == -1)
|
||||||
return null;
|
return null;
|
||||||
@ -38,9 +40,7 @@ public class ProjectUtils
|
|||||||
}
|
}
|
||||||
public static String getCampaignID() throws Exception
|
public static String getCampaignID() throws Exception
|
||||||
{
|
{
|
||||||
if (WorkspaceUtils.getSelectedProject() == null &&
|
if (WorkspaceUtils.getSelectedResource() == null)
|
||||||
WorkspaceUtils.getSelectedFile() == null &&
|
|
||||||
WorkspaceUtils.getSelectedFolder() == null)
|
|
||||||
{
|
{
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -62,12 +62,14 @@ public class ProjectUtils
|
|||||||
public static boolean isCampaignFile(String fileName)
|
public static boolean isCampaignFile(String fileName)
|
||||||
{
|
{
|
||||||
//TODO: replace this with a better checking
|
//TODO: replace this with a better checking
|
||||||
|
//TODO: check extension
|
||||||
String fileContentString = ResourceUtils.getFileContents(new File(fileName));
|
String fileContentString = ResourceUtils.getFileContents(new File(fileName));
|
||||||
return (fileContentString.contains("[campaign]") && fileContentString.contains("[/campaign]"));
|
return (fileContentString.contains("[campaign]") && fileContentString.contains("[/campaign]"));
|
||||||
}
|
}
|
||||||
public static boolean isScenarioFile(String fileName)
|
public static boolean isScenarioFile(String fileName)
|
||||||
{
|
{
|
||||||
//TODO: replace this with a better checking
|
//TODO: replace this with a better checking
|
||||||
|
//TODO: check extension
|
||||||
String fileContentString = ResourceUtils.getFileContents(new File(fileName));
|
String fileContentString = ResourceUtils.getFileContents(new File(fileName));
|
||||||
return (fileContentString.contains("[scenario]") && fileContentString.contains("[/scenario]"));
|
return (fileContentString.contains("[scenario]") && fileContentString.contains("[/scenario]"));
|
||||||
}
|
}
|
||||||
|
@ -8,6 +8,7 @@ import java.io.ByteArrayInputStream;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
@ -54,7 +55,7 @@ public class ResourceUtils
|
|||||||
|
|
||||||
public static String getFileContents(File file)
|
public static String getFileContents(File file)
|
||||||
{
|
{
|
||||||
if (!file.exists())
|
if (!file.exists() || !file.isFile())
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
String contentsString = "";
|
String contentsString = "";
|
||||||
@ -67,14 +68,14 @@ public class ResourceUtils
|
|||||||
{
|
{
|
||||||
contentsString += (line + "\n");
|
contentsString += (line + "\n");
|
||||||
}
|
}
|
||||||
} catch (Exception e)
|
} catch (IOException e)
|
||||||
{
|
{
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return null;
|
|
||||||
} finally
|
} finally
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
if (reader!= null)
|
||||||
reader.close();
|
reader.close();
|
||||||
} catch (Exception e)
|
} catch (Exception e)
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user