Skip to content

Commit 6696536

Browse files
committed
Fix bug in cancel.
1 parent 760b33b commit 6696536

File tree

1 file changed

+46
-37
lines changed
  • blade-engine/src/com/bladecoder/engine/model

1 file changed

+46
-37
lines changed

blade-engine/src/com/bladecoder/engine/model/Verb.java

Lines changed: 46 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public class Verb implements VerbRunner, Serializable {
5050

5151
private int ip = -1;
5252
private String currentTarget;
53-
53+
5454
private ActionCallback cb;
5555

5656
public Verb() {
@@ -75,7 +75,7 @@ public String getState() {
7575
public void setState(String state) {
7676
this.state = state;
7777
}
78-
78+
7979
public String getIcon() {
8080
return icon;
8181
}
@@ -91,16 +91,16 @@ public String getTarget() {
9191
public void setTarget(String target) {
9292
this.target = target;
9393
}
94-
94+
9595
public String getHashKey() {
9696
String key = id;
97-
97+
9898
if (target != null)
9999
key = key + "." + target;
100100

101101
if (state != null)
102102
key = key + "." + state;
103-
103+
104104
return key;
105105
}
106106

@@ -111,22 +111,22 @@ public void add(Action a) {
111111
public ArrayList<Action> getActions() {
112112
return actions;
113113
}
114-
114+
115115
public String getCurrentTarget() {
116116
return currentTarget;
117117
}
118118

119119
public void run(String currentTarget, ActionCallback cb) {
120120
this.currentTarget = currentTarget;
121121
this.cb = cb;
122-
122+
123123
if (EngineLogger.debugMode()) {
124124
StringBuilder sb = new StringBuilder(">>> Running verb: ").append(id);
125-
126-
if(currentTarget != null) {
125+
126+
if (currentTarget != null) {
127127
sb.append(" currentTarget: " + currentTarget);
128128
}
129-
129+
130130
EngineLogger.debug(sb.toString());
131131
}
132132

@@ -150,16 +150,21 @@ public void nextStep() {
150150
else
151151
ip++;
152152
} catch (Exception e) {
153-
EngineLogger.error("EXCEPTION EXECUTING ACTION: " + a.getClass().getSimpleName() + " - " + e.getMessage(), e);
153+
EngineLogger.error(
154+
"EXCEPTION EXECUTING ACTION: " + a.getClass().getSimpleName() + " - " + e.getMessage(), e);
154155
ip++;
155156
}
156157
}
157158

158159
if (ip == actions.size()) {
159160
EngineLogger.debug(">>> Verb FINISHED: " + id);
160-
161-
if(cb != null)
162-
cb.resume();
161+
162+
if (cb != null) {
163+
ActionCallback cb2 = cb;
164+
cb = null;
165+
166+
cb2.resume();
167+
}
163168
}
164169
}
165170

@@ -182,34 +187,38 @@ public void setIP(int ip) {
182187
}
183188

184189
public void cancel() {
190+
ip = actions.size() + 1;
191+
185192
for (Action c : actions) {
186193
if (c instanceof VerbRunner)
187194
((VerbRunner) c).cancel();
188195
}
189196

190-
ip = actions.size() + 1;
191-
197+
if (cb != null) {
198+
ActionCallback cb2 = cb;
199+
cb = null;
200+
201+
cb2.resume();
202+
}
203+
192204
EngineLogger.debug(">>> Verb CANCELLED: " + id);
193-
194-
if(cb != null)
195-
cb.resume();
196205
}
197206

198207
@Override
199208
public void write(Json json) {
200209

201210
if (SerializationHelper.getInstance().getMode() == Mode.MODEL) {
202211
json.writeValue("id", id);
203-
204-
if(target != null)
212+
213+
if (target != null)
205214
json.writeValue("target", target);
206-
207-
if(state != null)
215+
216+
if (state != null)
208217
json.writeValue("state", state);
209-
210-
if(icon != null)
218+
219+
if (icon != null)
211220
json.writeValue("icon", icon);
212-
221+
213222
json.writeArrayStart("actions");
214223
for (Action a : actions) {
215224
ActionUtils.writeJson(a, json);
@@ -218,8 +227,8 @@ public void write(Json json) {
218227
} else {
219228
json.writeValue("ip", ip);
220229
json.writeValue("cb", ActionCallbackSerialization.find(cb));
221-
222-
if(currentTarget != null)
230+
231+
if (currentTarget != null)
223232
json.writeValue("currentTarget", currentTarget);
224233

225234
json.writeArrayStart("actions");
@@ -239,27 +248,27 @@ public void read(Json json, JsonValue jsonData) {
239248

240249
if (SerializationHelper.getInstance().getMode() == Mode.MODEL) {
241250
id = json.readValue("id", String.class, jsonData);
242-
target = json.readValue("target", String.class, (String)null, jsonData);
243-
state = json.readValue("state", String.class, (String)null, jsonData);
244-
icon = json.readValue("icon", String.class, (String)null, jsonData);
245-
251+
target = json.readValue("target", String.class, (String) null, jsonData);
252+
state = json.readValue("state", String.class, (String) null, jsonData);
253+
icon = json.readValue("icon", String.class, (String) null, jsonData);
254+
246255
actions.clear();
247256
JsonValue actionsValue = jsonData.get("actions");
248257
for (int i = 0; i < actionsValue.size; i++) {
249258
JsonValue aValue = actionsValue.get(i);
250259
String clazz = aValue.getString("class");
251260

252-
try {
261+
try {
253262
Action a = ActionUtils.readJson(json, aValue);
254263
actions.add(a);
255-
} catch(SerializationException e) {
264+
} catch (SerializationException e) {
256265
EngineLogger.error("Error loading action: " + clazz + " " + aValue.toString());
257266
throw e;
258267
}
259268
}
260269
} else {
261270
// MUTABLE
262-
currentTarget = json.readValue("currentTarget", String.class, (String)null, jsonData);
271+
currentTarget = json.readValue("currentTarget", String.class, (String) null, jsonData);
263272
ip = json.readValue("ip", Integer.class, jsonData);
264273
String sCb = json.readValue("cb", String.class, jsonData);
265274
cb = ActionCallbackSerialization.find(sCb);
@@ -270,9 +279,9 @@ public void read(Json json, JsonValue jsonData) {
270279

271280
for (Action a : actions) {
272281
if (a instanceof Serializable && i < actionsValue.size) {
273-
if(actionsValue.get(i) == null)
282+
if (actionsValue.get(i) == null)
274283
break;
275-
284+
276285
((Serializable) a).read(json, actionsValue.get(i));
277286
i++;
278287
}

0 commit comments

Comments
 (0)