20 package net.sf.gridarta.plugin;
22 import bsh.ConsoleInterface;
24 import bsh.Interpreter;
25 import bsh.TargetError;
26 import java.io.CharArrayWriter;
27 import java.io.InputStreamReader;
28 import java.io.PrintStream;
29 import java.io.PrintWriter;
30 import java.io.Reader;
31 import java.util.HashMap;
39 import org.jetbrains.annotations.NotNull;
40 import org.jetbrains.annotations.Nullable;
46 @SuppressWarnings(
"UseOfSystemOutOrSystemErr")
67 this.pluginModel = pluginModel;
68 this.pluginParameters = pluginParameters;
77 public int executePlugin(@NotNull
final String plugin, @NotNull
final Iterable<String> parameters) {
79 if (modelTemplate == null) {
80 System.err.println(
"plugin " + plugin +
" does not exist");
85 final Map<String, Object> runValues =
new HashMap<>();
86 for (
final String parameter : parameters) {
88 if (tmp.length != 2) {
89 System.err.println(
"syntax error: " + parameter);
95 final StringBuilder sb =
new StringBuilder();
96 boolean firstParameter =
true;
98 sb.append(firstParameter ?
" " :
", ");
99 sb.append(clonedParameter.getName());
100 firstParameter =
false;
102 System.err.println(
"plugin " + plugin +
" has no parameter " + tmp[0] +
"; available parameters:" + sb);
110 throw new AssertionError(ex);
113 System.out.println(
"invalid value " + tmp[1] +
" for parameter " + tmp[0]);
117 runValues.put(clonedParameter.
getName(), tmp[1]);
120 final String parameterName = parameter.getName();
121 if (!runValues.containsKey(parameterName)) {
122 final Object parameterValue = parameter.getValueOrNull();
123 if (parameterValue == null) {
124 System.err.println(
"no value for parameter " + parameterName);
127 runValues.put(parameterName, parameterValue);
131 final ConsoleInterface console =
new ConsoleInterface() {
134 public Reader getIn() {
135 return new InputStreamReader(System.in);
139 public PrintStream getOut() {
144 public PrintStream getErr() {
149 public void println(
final Object o) {
150 System.out.println(o);
154 public void print(
final Object o) {
159 public void error(
final Object o) {
160 System.err.println(o);
164 final BshThread<G, A, R> pluginThread = doRunPlugin(clonedPlugin, console, runValues);
165 if (pluginThread == null) {
170 }
catch (
final InterruptedException ignored) {
171 pluginThread.interrupt();
172 Thread.currentThread().interrupt();
173 System.out.println(
"interrupted");
178 System.err.println(
"plugin failed");
194 final Interpreter runner =
new Interpreter();
195 runner.setConsole(console);
198 for (
final Map.Entry<String, Object> parameter : parameters.entrySet()) {
199 runner.set(parameter.getKey(), parameter.getValue());
201 }
catch (
final TargetError ex) {
202 final CharArrayWriter charArrayWriter =
new CharArrayWriter();
204 try (PrintWriter printWriter =
new PrintWriter(charArrayWriter)) {
205 ex.getTarget().printStackTrace(printWriter);
208 charArrayWriter.close();
210 console.print(
"target error: " + charArrayWriter);
212 }
catch (
final EvalError ex) {
213 console.print(
"evaluation error: " + ex.getMessage());
217 pluginThread.start();
int executePlugin(@NotNull final String plugin, @NotNull final Iterable< String > parameters)
Executes an editor plugin.
Utility class for string manipulation.
static final Pattern PATTERN_EQUAL
The pattern that matches a single equal sign ("=").
Plugin< G, A, R > getPlugin(@NotNull final String name)
PluginExecutor(@NotNull final PluginModel< G, A, R > pluginModel, @NotNull final PluginParameters pluginParameters)
Creates a new instance.
int getParameter(@NotNull final String parameterName)
Returns the index for a plugin parameter name.
boolean isSuccess()
Returns whether the plugin has been executed successfully.
Allows execution of Plugins.
Base package of all Gridarta classes.
Reflects a game object (object on a map).
Plugin< G, A, R > clonePlugin()
Returns a clone copy of this plugin.
GameObjects are the objects based on Archetypes found on maps.
final PluginModel< G, A, R > pluginModel
The PluginModel to execute.
The run mode of a plugin plugin.
Thrown if a parameter does not exist.
BshThread< G, A, R > doRunPlugin(@NotNull final Plugin< G, A, R > plugin, @NotNull final ConsoleInterface console, @NotNull final Map< String, Object > parameters)
Runs a plugin model.
Makes basic Gridarta classes available to scripts.
void setInterpreterValues(@NotNull final Interpreter interpreter, @NotNull final PluginRunMode pluginRunMode)
Adds variables to a Interpreter instance.
String getName()
The name of the parameter.
final PluginParameters pluginParameters
The parameters to pass to the plugin.
boolean setStringValue(@NotNull String stringValue)
Sets the parameter value from string representation.
Interface for MapArchObjects.