Skip to content

Commit

Permalink
Add AbstractScriptContext
Browse files Browse the repository at this point in the history
  • Loading branch information
zbx1425 committed Sep 12, 2023
1 parent bc721b6 commit 4f8521b
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package cn.zbx1425.mtrsteamloco.render.scripting;

import vendor.cn.zbx1425.mtrsteamloco.org.mozilla.javascript.Scriptable;

import java.util.concurrent.Future;

public abstract class AbstractScriptContext {

public Scriptable state;
protected boolean created = false;
public Future<?> scriptStatus;
public double lastExecuteTime = 0;

public abstract void renderFunctionFinished();

public abstract Object getWrapperObject();
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public class ScriptHolder {
private Scriptable scope;

public long failTime = 0;
public double lastExecuteTime = 0;

public void load(Map<ResourceLocation, String> scripts) {
Context rhinoCtx = Context.enter();
Expand Down Expand Up @@ -98,19 +97,18 @@ public void load(Map<ResourceLocation, String> scripts) {
}
}

public Future<?> callTrainFunction(String function, TrainScriptContext trainCtx) {
public Future<?> callFunctionAsync(String function, AbstractScriptContext scriptCtx) {
if (duringFailTimeout()) return null;
return SCRIPT_THREAD.submit(() -> {
if (Thread.currentThread().isInterrupted()) return;
Context rhinoCtx = Context.enter();
if (trainCtx.state == null) trainCtx.state = rhinoCtx.newObject(scope);
if (scriptCtx.state == null) scriptCtx.state = rhinoCtx.newObject(scope);
try {
Object jsFunction = scope.get(function, scope);
if (jsFunction instanceof Function && jsFunction != Scriptable.NOT_FOUND) {
TimingUtil.prepareForScript(this);
Object[] functionParam = { trainCtx, trainCtx.state, trainCtx.trainExtra };
TimingUtil.prepareForScript(scriptCtx);
Object[] functionParam = { scriptCtx, scriptCtx.state, scriptCtx.getWrapperObject() };
((Function)jsFunction).call(rhinoCtx, scope, scope, functionParam);
trainCtx.scriptFinished();
}
} catch (Exception ex) {
Main.LOGGER.error("Error in NTE Resource Pack JavaScript", ex);
Expand All @@ -121,19 +119,19 @@ public Future<?> callTrainFunction(String function, TrainScriptContext trainCtx)
});
}

public Future<?> callEyeCandyFunction(String function, EyeCandyScriptContext eyeCandyCtx) {
public Future<?> callRenderFunctionAsync(String function, AbstractScriptContext scriptCtx) {
if (duringFailTimeout()) return null;
return SCRIPT_THREAD.submit(() -> {
if (Thread.currentThread().isInterrupted()) return;
Context rhinoCtx = Context.enter();
if (eyeCandyCtx.state == null) eyeCandyCtx.state = rhinoCtx.newObject(scope);
if (scriptCtx.state == null) scriptCtx.state = rhinoCtx.newObject(scope);
try {
Object jsFunction = scope.get(function, scope);
if (jsFunction instanceof Function && jsFunction != Scriptable.NOT_FOUND) {
TimingUtil.prepareForScript(this);
Object[] functionParam = {eyeCandyCtx, eyeCandyCtx.state, eyeCandyCtx.entity};
((Function) jsFunction).call(rhinoCtx, scope, scope, functionParam);
eyeCandyCtx.scriptFinished();
TimingUtil.prepareForScript(scriptCtx);
Object[] functionParam = { scriptCtx, scriptCtx.state, scriptCtx.getWrapperObject() };
((Function)jsFunction).call(rhinoCtx, scope, scope, functionParam);
scriptCtx.renderFunctionFinished();
}
} catch (Exception ex) {
Main.LOGGER.error("Error in NTE Resource Pack JavaScript", ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import cn.zbx1425.mtrsteamloco.Main;
import cn.zbx1425.mtrsteamloco.block.BlockEyeCandy;
import cn.zbx1425.mtrsteamloco.render.scripting.AbstractScriptContext;
import cn.zbx1425.mtrsteamloco.render.scripting.ScriptHolder;
import cn.zbx1425.sowcer.math.Matrices;
import cn.zbx1425.sowcer.math.Matrix4f;
Expand All @@ -12,7 +13,7 @@

import java.util.concurrent.Future;

public class EyeCandyScriptContext {
public class EyeCandyScriptContext extends AbstractScriptContext {

public Future<?> scriptStatus;

Expand All @@ -33,23 +34,24 @@ public EyeCandyScriptContext(BlockEyeCandy.BlockEntityEyeCandy entity) {

public void tryCallRender(ScriptHolder jsContext) {
if (!created) {
scriptStatus = jsContext.callEyeCandyFunction("createBlock", this);
scriptStatus = jsContext.callFunctionAsync("createBlock", this);
created = true;
return;
}
if (scriptStatus == null || scriptStatus.isDone()) {
scriptStatus = jsContext.callEyeCandyFunction("renderBlock", this);
scriptStatus = jsContext.callRenderFunctionAsync("renderBlock", this);
}
}

public void tryCallDispose(ScriptHolder jsContext) {
if (created) {
jsContext.callEyeCandyFunction("disposeBlock", this);
jsContext.callFunctionAsync("disposeBlock", this);
created = false;
}
}

public void scriptFinished() {
@Override
public void renderFunctionFinished() {
synchronized (this) {
EyeCandyDrawCalls temp = scriptResultWriting;
scriptResultWriting = scriptResult;
Expand All @@ -58,6 +60,11 @@ public void scriptFinished() {
}
}

@Override
public Object getWrapperObject() {
return entity;
}

public void drawModel(ModelCluster model, Matrices poseStack) {
scriptResultWriting.addModel(model, poseStack == null ? Matrix4f.IDENTITY : poseStack.last().copy());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cn.zbx1425.mtrsteamloco.render.scripting.train;

import cn.zbx1425.mtrsteamloco.render.scripting.AbstractScriptContext;
import cn.zbx1425.mtrsteamloco.render.scripting.ScriptHolder;
import cn.zbx1425.sowcer.math.Matrices;
import cn.zbx1425.sowcer.math.Matrix4f;
Expand All @@ -20,9 +21,7 @@
import java.util.concurrent.Future;

@SuppressWarnings("unused")
public class TrainScriptContext {

public Future<?> scriptStatus;
public class TrainScriptContext extends AbstractScriptContext {

public TrainClient train;
public TrainWrapper trainExtra;
Expand All @@ -33,10 +32,6 @@ public class TrainScriptContext {

public TrainRendererBase baseRenderer = null;

public Scriptable state;

private boolean created = false;

public TrainScriptContext(TrainClient train) {
this.scriptResult = new TrainDrawCalls(train.trainCars);
this.scriptResultWriting = new TrainDrawCalls(train.trainCars);
Expand All @@ -49,23 +44,24 @@ public void tryCallRender(ScriptHolder jsContext) {
if (!created) {
trainExtra = new TrainWrapper(train);
trainExtraWriting = new TrainWrapper(train);
scriptStatus = jsContext.callTrainFunction("createTrain", this);
scriptStatus = jsContext.callFunctionAsync("createTrain", this);
created = true;
return;
}
if (scriptStatus == null || scriptStatus.isDone()) {
scriptStatus = jsContext.callTrainFunction("renderTrain", this);
scriptStatus = jsContext.callRenderFunctionAsync("renderTrain", this);
}
}

public void tryCallDispose(ScriptHolder jsContext) {
if (created) {
jsContext.callTrainFunction("disposeTrain", this);
jsContext.callFunctionAsync("disposeTrain", this);
created = false;
}
}

public void scriptFinished() {
@Override
public void renderFunctionFinished() {
synchronized (this) {
TrainDrawCalls temp = scriptResultWriting;
scriptResultWriting = scriptResult;
Expand All @@ -74,6 +70,11 @@ public void scriptFinished() {
}
}

@Override
public Object getWrapperObject() {
return trainExtra;
}

public void extraFinished() {
synchronized (this) {
TrainWrapper temp = trainExtraWriting;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cn.zbx1425.mtrsteamloco.render.scripting.util;

import cn.zbx1425.mtrsteamloco.render.RenderUtil;
import cn.zbx1425.mtrsteamloco.render.scripting.AbstractScriptContext;
import cn.zbx1425.mtrsteamloco.render.scripting.ScriptHolder;

@SuppressWarnings("unused")
Expand All @@ -9,10 +10,10 @@ public class TimingUtil {
private static double timeElapsedForScript = 0;
private static double frameDeltaForScript = 0;

public static void prepareForScript(ScriptHolder scriptHolder) {
public static void prepareForScript(AbstractScriptContext scriptContext) {
timeElapsedForScript = RenderUtil.runningSeconds;
frameDeltaForScript = timeElapsedForScript - scriptHolder.lastExecuteTime;
scriptHolder.lastExecuteTime = timeElapsedForScript;
frameDeltaForScript = timeElapsedForScript - scriptContext.lastExecuteTime;
scriptContext.lastExecuteTime = timeElapsedForScript;
}

public static double elapsed() {
Expand Down

0 comments on commit 4f8521b

Please sign in to comment.