Skip to content

Commit 97f13dc

Browse files
committed
Fix regression in 1.8.3. Updated unit tests.
1 parent 9e15e5b commit 97f13dc

File tree

5 files changed

+120
-10
lines changed

5 files changed

+120
-10
lines changed

CHANGES

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
[1.8.4]
2+
- Fix regression in 1.8.3. Updated unit tests.
3+
14
[1.8.3]
25
- Fix interactive scripts not triggering onScriptBegin
36

core/src/main/java/org/mini2Dx/miniscript/core/InteractiveScriptListener.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ public void onScriptException(int scriptId, Exception e) {
9797
}
9898
}
9999

100+
public ScriptInvocationListener getInvocationListener() {
101+
return invocationListener.get();
102+
}
103+
100104
@Override
101105
public boolean callOnGameThread() {
102106
return false;

core/src/main/java/org/mini2Dx/miniscript/core/ScriptExecutionTask.java

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,11 @@ public ScriptExecutionTask(int taskId, GameScriptingEngine gameScriptingEngine,
6565
public void run() {
6666
try {
6767
if(scriptInvocationListener != null) {
68-
if(scriptInvocationListener.callOnGameThread() && !syncCall) {
69-
final ScriptBeginNotification beginNotification = new ScriptBeginNotification(scriptInvocationListener, scriptId);
70-
scriptingEngine.scriptNotifications.offer(beginNotification);
71-
72-
while(!beginNotification.isProcessed()) {
73-
synchronized(beginNotification) {
74-
beginNotification.wait();
75-
}
76-
}
68+
if(scriptInvocationListener instanceof InteractiveScriptListener) {
69+
final ScriptInvocationListener actualListener = ((InteractiveScriptListener) scriptInvocationListener).getInvocationListener();
70+
invokeOnScriptBegin(actualListener);
7771
} else {
78-
scriptInvocationListener.onScriptBegin(scriptId);
72+
invokeOnScriptBegin(scriptInvocationListener);
7973
}
8074
}
8175

@@ -115,6 +109,24 @@ public void run() {
115109
completed.set(true);
116110
}
117111

112+
private void invokeOnScriptBegin(ScriptInvocationListener listener) throws InterruptedException {
113+
if(listener == null) {
114+
return;
115+
}
116+
if(listener.callOnGameThread() && !syncCall) {
117+
final ScriptBeginNotification beginNotification = new ScriptBeginNotification(listener, scriptId);
118+
scriptingEngine.scriptNotifications.offer(beginNotification);
119+
120+
while(!beginNotification.isProcessed()) {
121+
synchronized(beginNotification) {
122+
beginNotification.wait();
123+
}
124+
}
125+
} else {
126+
listener.onScriptBegin(scriptId);
127+
}
128+
}
129+
118130
public void skipScript() {
119131
if (taskFuture.isDone()) {
120132
return;

core/src/test/java/org/mini2Dx/miniscript/core/AbstractGameScriptingEngineTest.java

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import java.io.InputStream;
2727
import java.util.concurrent.atomic.AtomicBoolean;
28+
import java.util.concurrent.atomic.AtomicInteger;
2829
import java.util.concurrent.atomic.AtomicLong;
2930
import java.util.concurrent.atomic.AtomicReference;
3031

@@ -225,12 +226,19 @@ public boolean callOnGameThread() {
225226
public void testInvokeScriptBeginNotificationOnGameThread() throws Exception {
226227
final int expectedScriptId = scriptingEngine.compileScript(getDefaultScript());
227228
final AtomicBoolean notificationReceived = new AtomicBoolean(false);
229+
final AtomicLong notificationThreadId = new AtomicLong(-1);
230+
final AtomicBoolean beginResult = new AtomicBoolean(false);
228231

229232
scriptingEngine.invokeCompiledScript(expectedScriptId, scriptBindings, new ScriptInvocationListener() {
230233

231234
@Override
232235
public void onScriptBegin(int scriptId) {
236+
try {
237+
Thread.sleep(500);
238+
} catch (Exception e) {}
239+
scriptBindings.put("beginNotificationResult", true);
233240
notificationReceived.set(true);
241+
notificationThreadId.set(Thread.currentThread().getId());
234242
}
235243

236244
@Override
@@ -244,6 +252,9 @@ public void onScriptSuccess(int scriptId, ScriptExecutionResult executionResult)
244252
} else {
245253
scriptResult.set(ScriptResult.SUCCESS);
246254
scriptExecuted.set(true);
255+
256+
//Ensure begin is processed before execution
257+
beginResult.set(executionResult.containsKey("beginNotificationResult"));
247258
}
248259
}
249260

@@ -266,6 +277,84 @@ public boolean callOnGameThread() {
266277
}
267278
});
268279
while(!scriptExecuted.get()) {
280+
if(!notificationReceived.get()) {
281+
try {
282+
Thread.sleep(100);
283+
} catch (Exception e) {}
284+
}
285+
286+
scriptingEngine.update(1f);
287+
}
288+
Assert.assertEquals(ScriptResult.SUCCESS, scriptResult.get());
289+
Assert.assertEquals(true, gameFuture.isUpdated());
290+
Assert.assertEquals(false, gameFuture.waitOccurred());
291+
Assert.assertEquals(false, gameFuture.isFutureSkipped());
292+
Assert.assertEquals(false, gameFuture.isScriptSkipped());
293+
Assert.assertEquals(true, notificationReceived.get());
294+
Assert.assertEquals(Thread.currentThread().getId(), notificationThreadId.get());
295+
}
296+
297+
@Test
298+
public void testInvokeInteractiveScriptBeginNotificationOnGameThread() throws Exception {
299+
final int expectedScriptId = scriptingEngine.compileScript(getDefaultScript());
300+
final AtomicBoolean notificationReceived = new AtomicBoolean(false);
301+
final AtomicLong notificationThreadId = new AtomicLong(-1);
302+
final AtomicBoolean beginResult = new AtomicBoolean(false);
303+
304+
scriptingEngine.invokeCompiledScript(expectedScriptId, scriptBindings, new ScriptInvocationListener() {
305+
306+
@Override
307+
public void onScriptBegin(int scriptId) {
308+
try {
309+
Thread.sleep(500);
310+
} catch (Exception e) {}
311+
scriptBindings.put("beginNotificationResult", true);
312+
notificationReceived.set(true);
313+
notificationThreadId.set(Thread.currentThread().getId());
314+
}
315+
316+
@Override
317+
public void onScriptSuccess(int scriptId, ScriptExecutionResult executionResult) {
318+
if(scriptId != expectedScriptId) {
319+
scriptResult.set(ScriptResult.INCORRECT_SCRIPT_ID);
320+
scriptExecuted.set(true);
321+
} else if(!checkExpectedScriptResults(executionResult)) {
322+
scriptResult.set(ScriptResult.INCORRECT_VARIABLES);
323+
scriptExecuted.set(true);
324+
} else {
325+
scriptResult.set(ScriptResult.SUCCESS);
326+
scriptExecuted.set(true);
327+
328+
//Ensure begin is processed before execution
329+
beginResult.set(executionResult.containsKey("beginNotificationResult"));
330+
}
331+
}
332+
333+
@Override
334+
public void onScriptSkipped(int scriptId) {
335+
scriptResult.set(ScriptResult.SKIPPED);
336+
scriptExecuted.set(true);
337+
}
338+
339+
@Override
340+
public void onScriptException(int scriptId, Exception e) {
341+
e.printStackTrace();
342+
scriptResult.set(ScriptResult.EXCEPTION);
343+
scriptExecuted.set(true);
344+
}
345+
346+
@Override
347+
public boolean callOnGameThread() {
348+
return true;
349+
}
350+
}, 0, true);
351+
while(!scriptExecuted.get()) {
352+
if(!notificationReceived.get()) {
353+
try {
354+
Thread.sleep(100);
355+
} catch (Exception e) {}
356+
}
357+
269358
scriptingEngine.update(1f);
270359
}
271360
Assert.assertEquals(ScriptResult.SUCCESS, scriptResult.get());
@@ -274,6 +363,7 @@ public boolean callOnGameThread() {
274363
Assert.assertEquals(false, gameFuture.isFutureSkipped());
275364
Assert.assertEquals(false, gameFuture.isScriptSkipped());
276365
Assert.assertEquals(true, notificationReceived.get());
366+
Assert.assertEquals(Thread.currentThread().getId(), notificationThreadId.get());
277367
}
278368

279369
@Test

kotlin/src/test/resources/default.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
val beginNotificationResult: Boolean = false
12
val stringValue: String = bindings["stringValue"] as String + "123"
23
val intValue: Int = 101
34
val booleanValue: Boolean = true

0 commit comments

Comments
 (0)