Skip to content

Commit

Permalink
Add dispose function to train scripting
Browse files Browse the repository at this point in the history
  • Loading branch information
zbx1425 committed Aug 21, 2023
1 parent 75e8ae7 commit 28ca633
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ MtrModelRegistryUtil.resourceManager, new ResourceLocation(obj.get("atlasIndex"
new ResourceLocation(obj.get("model").getAsString()), MainClient.atlasManager).copy();

if (obj.has("textureId")) {
rawModel.replaceAllTexture("default.png", new ResourceLocation(obj.get("textureId").getAsString()));
rawModel.replaceTexture("default.png", new ResourceLocation(obj.get("textureId").getAsString()));
}
if (obj.has("flipV") && obj.get("flipV").getAsBoolean()) {
rawModel.applyUVMirror(false, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ MtrModelRegistryUtil.resourceManager, new ResourceLocation(obj.get("atlasIndex"
new ResourceLocation(obj.get("model").getAsString()), MainClient.atlasManager).copy();

if (obj.has("textureId")) {
rawModel.replaceAllTexture("default.png", new ResourceLocation(obj.get("textureId").getAsString()));
rawModel.replaceTexture("default.png", new ResourceLocation(obj.get("textureId").getAsString()));
}
if (obj.has("flipV") && obj.get("flipV").getAsBoolean()) {
rawModel.applyUVMirror(false, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import cn.zbx1425.mtrsteamloco.render.RailPicker;
import cn.zbx1425.mtrsteamloco.render.RenderUtil;
import cn.zbx1425.mtrsteamloco.render.rail.RailRenderDispatcher;
import cn.zbx1425.mtrsteamloco.render.scripting.train.RenderTrainScripted;
import cn.zbx1425.sowcer.util.GlStateTracker;
import com.mojang.blaze3d.vertex.PoseStack;
import cn.zbx1425.sowcer.math.Matrix4f;
Expand Down Expand Up @@ -62,6 +63,8 @@ private static void renderTail(EntitySeat entity, float tickDelta, PoseStack mat
} else {
RailPicker.pickedRail = null;
}

RenderTrainScripted.disposeInactiveScripts();
}

@Inject(at = @At("HEAD"), cancellable = true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import mtr.model.ModelTrainBase;
import net.minecraft.resources.ResourceLocation;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;

import java.io.File;
import java.util.*;
Expand Down Expand Up @@ -186,7 +185,7 @@ public static void loadInto(JsonObject model, DynamicTrainModel target) {
String repaintTexture = MtrModelRegistryUtil.getTextureIdFromDummyBbData(model);
if (!StringUtils.isEmpty(repaintTexture)) {
for (RawModel partModel : models.values()) {
partModel.replaceAllTexture("default.png", new ResourceLocation(repaintTexture));
partModel.replaceTexture("default.png", new ResourceLocation(repaintTexture));
}
}
// Apply FlipV
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ public void tryCallRender(ScriptHolder jsContext) {
}
}

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

public void scriptFinished() {
synchronized (this) {
EyeCandyDrawCalls temp = scriptResultWriting;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import net.minecraft.world.level.LightLayer;
import net.minecraft.world.phys.Vec3;

import java.util.WeakHashMap;

public class RenderTrainScripted extends TrainRendererBase {

private final ScriptHolder typeScripting;
Expand All @@ -36,12 +38,24 @@ private RenderTrainScripted(RenderTrainScripted base, TrainClient trainClient) {
this.trainScripting = new TrainScriptContext(trainClient);
}

private static final WeakHashMap<TrainClient, RenderTrainScripted> activeRenderers = new WeakHashMap<>();

@Override
public TrainRendererBase createTrainInstance(TrainClient trainClient) {
RenderTrainScripted result = new RenderTrainScripted(this, trainClient);
activeRenderers.put(trainClient, result);
return result;
}

public static void disposeInactiveScripts() {
for (TrainClient train : activeRenderers.keySet()) {
if (train.isRemoved) {
activeRenderers.get(train).trainScripting.tryCallDispose(activeRenderers.get(train).typeScripting);
activeRenderers.remove(train);
}
}
}

@Override
public void renderCar(int carIndex, double x, double y, double z, float yaw, float pitch, boolean doorLeftOpen, boolean doorRightOpen) {
assert train != null && trainScripting != null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ public void tryCallRender(ScriptHolder jsContext) {
}
}

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

public void scriptFinished() {
synchronized (this) {
TrainDrawCalls temp = scriptResultWriting;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
import java.awt.*;
import java.awt.color.ColorSpace;
import java.awt.image.*;
import java.io.Closeable;
import java.io.IOException;
import java.util.UUID;

@SuppressWarnings("unused")
public class GraphicsTexture {
public class GraphicsTexture implements Closeable {

private final DynamicTexture dynamicTexture;
public final ResourceLocation identifier;
Expand Down Expand Up @@ -47,4 +49,11 @@ public void upload() {
RenderSystem.recordRenderCall(dynamicTexture::upload);
}

@Override
public void close() {
Minecraft.getInstance().execute(() -> {
Minecraft.getInstance().getTextureManager().release(identifier);
});
graphics.dispose();
}
}
33 changes: 32 additions & 1 deletion common/src/main/java/cn/zbx1425/sowcer/model/VertArrays.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
package cn.zbx1425.sowcer.model;

import cn.zbx1425.sowcer.batch.MaterialProp;
import cn.zbx1425.sowcer.object.InstanceBuf;
import cn.zbx1425.sowcer.object.VertArray;
import cn.zbx1425.sowcer.vertex.VertAttrMapping;
import cn.zbx1425.sowcerext.model.RawMesh;
import net.minecraft.resources.ResourceLocation;

import java.io.Closeable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class VertArrays implements Closeable {

public ArrayList<VertArray> meshList = new ArrayList<>();
public final ArrayList<VertArray> meshList = new ArrayList<>();

public static VertArrays createAll(Model model, VertAttrMapping mapping, InstanceBuf instanceBuf) {
VertArrays result = new VertArrays();
Expand All @@ -21,6 +27,31 @@ public static VertArrays createAll(Model model, VertAttrMapping mapping, Instanc
return result;
}

public void replaceTexture(String oldTexture, ResourceLocation newTexture) {
for (VertArray vertArray : meshList) {
if (vertArray.materialProp.texture == null) continue;
String oldPath = vertArray.materialProp.texture.getPath();
if (oldPath.substring(oldPath.lastIndexOf("/") + 1).equals(oldTexture)) {
vertArray.materialProp.texture = newTexture;
}
}
}

public void replaceAllTexture(ResourceLocation newTexture) {
for (VertArray vertArray : meshList) {
vertArray.materialProp.texture = newTexture;
}
}

public VertArrays copyForMaterialChanges() {
VertArrays result = new VertArrays();
for (VertArray vertArray : meshList) {
VertArray newVertArray = vertArray.copyForMaterialChanges();
result.meshList.add(newVertArray);
}
return result;
}

@Override
public void close() {
for (VertArray mesh : meshList) {
Expand Down
18 changes: 12 additions & 6 deletions common/src/main/java/cn/zbx1425/sowcer/object/VertArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ public class VertArray implements Closeable {
public InstanceBuf instanceBuf;
public VertAttrMapping mapping;

private static VertArray dummyVao;

public VertArray() {
id = GL33.glGenVertexArrays();
}

public static VertArray getDummyVao() {

if (dummyVao == null) dummyVao = new VertArray();
return dummyVao;
private VertArray(VertArray other) {
this.id = other.id;
this.materialProp = other.materialProp;
this.indexBuf = other.indexBuf;
this.instanceBuf = other.instanceBuf;
this.mapping = other.mapping;
}

public void create(Mesh mesh, VertAttrMapping mapping, InstanceBuf instanceBuf) {
Expand Down Expand Up @@ -63,6 +63,12 @@ public int getFaceCount() {
return indexBuf.faceCount * (instanceBuf == null ? 1 : instanceBuf.size);
}

public VertArray copyForMaterialChanges() {
VertArray result = new VertArray(this);
result.materialProp = result.materialProp.copy();
return result;
}

@Override
public void close() {
if (RenderSystem.isOnRenderThread()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public class RawModel {

Expand Down Expand Up @@ -149,7 +148,7 @@ public void setAllRenderType(String renderType) {
}
}

public void replaceAllTexture(String oldTexture, ResourceLocation newTexture) {
public void replaceTexture(String oldTexture, ResourceLocation newTexture) {
for (Map.Entry<MaterialProp, RawMesh> entry : meshList.entrySet()) {
if (entry.getKey().texture == null) continue;
String oldPath = entry.getKey().texture.getPath();
Expand Down

0 comments on commit 28ca633

Please sign in to comment.