Skip to content

Commit

Permalink
Add BbModelPreload toggle
Browse files Browse the repository at this point in the history
  • Loading branch information
zbx1425 committed Sep 1, 2023
1 parent bc2498e commit 3d9ee3d
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 17 deletions.
29 changes: 23 additions & 6 deletions common/src/main/java/cn/zbx1425/mtrsteamloco/ClientConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,21 @@
import cn.zbx1425.mtrsteamloco.render.ShadersModHandler;
import cn.zbx1425.sowcer.ContextCapability;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import net.minecraft.client.Minecraft;

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.function.Function;

public class ClientConfig {

private static Path path;

public static boolean disableOptimization = false;
public static boolean enableBbModelPreload = false;

public static boolean enableRail3D = true;
public static boolean enableRailRender = true;
public static boolean enableTrainRender = true;
Expand All @@ -23,21 +27,33 @@ public class ClientConfig {

public static void load(Path path) {
ClientConfig.path = path;
if (!Files.exists(path)) {
save();
}
try {
JsonObject configObject = Main.JSON_PARSER.parse(Files.readString(path)).getAsJsonObject();
disableOptimization = configObject.get("shaderCompatMode").getAsBoolean();
enableRail3D = configObject.get("enableRail3D").getAsBoolean();
enableRailRender = configObject.get("enableRailRender").getAsBoolean();
enableTrainRender = configObject.get("enableTrainRender").getAsBoolean();
enableSmoke = configObject.get("enableSmoke").getAsBoolean();
hideRidingTrain = configObject.get("hideRidingTrain").getAsBoolean();
disableOptimization = getOrDefault(configObject, "shaderCompatMode", JsonElement::getAsBoolean, false);
enableBbModelPreload = getOrDefault(configObject, "enableBbModelPreload", JsonElement::getAsBoolean, false);
enableRail3D = getOrDefault(configObject, "enableRail3D", JsonElement::getAsBoolean, true);
enableRailRender = getOrDefault(configObject, "enableRailRender", JsonElement::getAsBoolean, true);
enableTrainRender = getOrDefault(configObject, "enableTrainRender", JsonElement::getAsBoolean, true);
enableSmoke = getOrDefault(configObject, "enableSmoke", JsonElement::getAsBoolean, true);
hideRidingTrain = getOrDefault(configObject, "hideRidingTrain", JsonElement::getAsBoolean, false);
} catch (Exception ex) {
Main.LOGGER.warn("Failed loading client config:", ex);
ex.printStackTrace();
save();
}
}

private static <T> T getOrDefault(JsonObject jsonObject, String key, Function<JsonElement, T> getter, T defaultValue) {
if (jsonObject.has(key)) {
return getter.apply(jsonObject.get(key));
} else {
return defaultValue;
}
}

public static int getRailRenderLevel() {
if (!useRenderOptimization()) {
return enableRailRender ? 1 : 0;
Expand All @@ -57,6 +73,7 @@ public static void save() {
if (path == null) return;
JsonObject configObject = new JsonObject();
configObject.addProperty("shaderCompatMode", disableOptimization);
configObject.addProperty("enableBbModelPreload", enableBbModelPreload);
configObject.addProperty("enableRail3D", enableRail3D);
configObject.addProperty("enableRailRender", enableRailRender);
configObject.addProperty("enableTrainRender", enableTrainRender);
Expand Down
13 changes: 13 additions & 0 deletions common/src/main/java/cn/zbx1425/mtrsteamloco/gui/ConfigScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,19 @@ public static Screen createScreen(Screen parent) {
Text.translatable("gui.mtrsteamloco.config.client.rail3d.description")
).build()
);
common.addEntry(entryBuilder
.startBooleanToggle(
Text.translatable("gui.mtrsteamloco.config.client.preloadbbmodel"),
ClientConfig.enableBbModelPreload
).setSaveConsumer(checked -> {
ClientConfig.enableBbModelPreload = checked;
Minecraft.getInstance().execute(() -> Minecraft.getInstance().reloadResourcePacks());
}).setDefaultValue(false).build()
);
common.addEntry(entryBuilder.startTextDescription(
Text.translatable("gui.mtrsteamloco.config.client.preloadbbmodel.description")
).build()
);

ConfigCategory misc = builder.getOrCreateCategory(
Text.translatable("gui.mtrsteamloco.config.client.category.misc")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private static void reloadTail(ResourceManager manager, CallbackInfo ci) {

@Inject(at = @At("HEAD"), method = "readResource", cancellable = true)
private static void readResource(ResourceManager manager, String path, Consumer<JsonObject> callback, CallbackInfo ci) {
JsonObject dummyBbData = MtrModelRegistryUtil.createDummyBbDataPack(path, capturedTextureId, capturedFlipV);
JsonObject dummyBbData = MtrModelRegistryUtil.createDummyBbDataPack(path, capturedTextureId, capturedFlipV, captureBbModelPreload);
if (path.toLowerCase(Locale.ROOT).endsWith(".obj") || path.contains("|")) {
callback.accept(dummyBbData);
} else {
Expand All @@ -89,16 +89,16 @@ private static void readResource(ResourceManager manager, String path, Consumer<
ci.cancel();
}

@Unique
private static String capturedTextureId = "";
@Unique
private static boolean capturedFlipV = false;
@Unique private static String capturedTextureId = "";
@Unique private static boolean capturedFlipV = false;
@Unique private static boolean captureBbModelPreload = false;

@Inject(at = @At("RETURN"), method = "getOrDefault", remap = false)
private static <T> void getOrDefault(JsonObject jsonObject, String key, T defaultValue, Function<JsonElement, T> function, CallbackInfoReturnable<T> cir) {
if (key.equals(ICustomResources.CUSTOM_TRAINS_TEXTURE_ID)) {
capturedTextureId = jsonObject.has(key) ? jsonObject.get(key).getAsString() : defaultValue.toString();
capturedFlipV = jsonObject.has("flipV") && jsonObject.get("flipV").getAsBoolean();
captureBbModelPreload = jsonObject.has("preloadBbModel") && jsonObject.get("preloadBbModel").getAsBoolean();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cn.zbx1425.mtrsteamloco.render.integration;

import cn.zbx1425.mtrsteamloco.ClientConfig;
import cn.zbx1425.mtrsteamloco.CustomResources;
import cn.zbx1425.mtrsteamloco.Main;
import cn.zbx1425.mtrsteamloco.MainClient;
Expand Down Expand Up @@ -40,7 +41,12 @@ public static void loadInto(JsonObject model, DynamicTrainModel target) {
if (MtrModelRegistryUtil.isDummyBbData(model)) {
loadObjInto(model, target);
} else {
loadVanillaModelInto(model, target);
if (!model.has("dummyBbData")) return;
boolean bbModelPreload = MtrModelRegistryUtil.getBbModelPreloadFromDummyBbData(
model.get("dummyBbData").getAsJsonObject());
if (ClientConfig.enableBbModelPreload || bbModelPreload) {
loadVanillaModelInto(model, target);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,13 @@ public static List<Pair<ResourceLocation, Resource>> listResources(ResourceManag
#endif
}

public static JsonObject createDummyBbDataPack(String actualPath, String textureId, boolean flipV) {
public static JsonObject createDummyBbDataPack(String actualPath, String textureId, boolean flipV, boolean preloadBbModel) {
JsonObject result = createDummyBbData();
result.addProperty("zbxFlag", "dummyBbData.resourceLocation");
result.addProperty("actualPath", actualPath);
result.addProperty("textureId", textureId);
result.addProperty("flipV", flipV);
result.addProperty("preloadBbModel", preloadBbModel);
return result;
}

Expand All @@ -78,6 +79,7 @@ public static JsonObject createDummyBbDataExternal(String actualPath) {
result.addProperty("actualPath", actualPath);
result.addProperty("textureId", "");
result.addProperty("flipV", false);
result.addProperty("preloadBbModel", false);
return result;
}

Expand Down Expand Up @@ -114,4 +116,8 @@ public static String getTextureIdFromDummyBbData(JsonObject obj) {
public static boolean getFlipVFromDummyBbData(JsonObject obj) {
return obj.get("flipV").getAsBoolean();
}

public static boolean getBbModelPreloadFromDummyBbData(JsonObject obj) {
return obj.get("preloadBbModel").getAsBoolean();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"gui.mtrsteamloco.config.client.trainrender": "Show Trains",
"gui.mtrsteamloco.config.client.hideridingtrain": "Hide the Train on Which You Are Riding",
"gui.mtrsteamloco.config.client.preloadbbmodel": "BBMODEL Preload Optimization",
"gui.mtrsteamloco.config.client.preloadbbmodel.description": "This optimizes the rendering efficiency for custom trains with BBMODEL, especially for detailed models.\nHowever it increases RAM usage, and on some computers it's reported to not improve performance.",
"gui.mtrsteamloco.config.client.preloadbbmodel.description": "This optimizes the rendering efficiency for custom trains with BBMODEL, especially for detailed models.\nHowever it increases RAM usage, and on some computers it was reported to make little or even negative contributions.",

"gui.mtrsteamloco.mtr_version_low": "Please upgrade MTR: NTE %s requires MTR %s or above, while a lower version %s is installed."
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@
"gui.mtrsteamloco.config.client.category.common": "常用",
"gui.mtrsteamloco.config.client.category.misc": "杂项",
"gui.mtrsteamloco.config.client.preloadbbmodel": "BBMODEL 预加载优化",
"gui.mtrsteamloco.config.client.preloadbbmodel.description": "这项优化提升使用 BBMODEL 的自定义列车的渲染效率,对于精细的模型尤其有提升。\n但是它增加内存用量,且据报在一些设备上并不使性能更好"
"gui.mtrsteamloco.config.client.preloadbbmodel.description": "这项优化提升使用 BBMODEL 的自定义列车的渲染效率,对于精细的模型尤其有提升。\n但是它增加内存用量,且据报在一些设备上优化效果不佳或甚至负优化"
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,5 @@
"gui.mtrsteamloco.config.client.category.common": "常用",
"gui.mtrsteamloco.config.client.category.misc": "雜項",
"gui.mtrsteamloco.config.client.preloadbbmodel": "BBMODEL 預加載優化",
"gui.mtrsteamloco.config.client.preloadbbmodel.description": "這項優化提升使用 BBMODEL 的自定義列車的渲染效率,對於精細的模型尤其有提升。\n但是它增加記憶體用量,且據報在一些設備上並不使性能更好"
"gui.mtrsteamloco.config.client.preloadbbmodel.description": "這項優化提升使用 BBMODEL 的自定義列車的渲染效率,對於精細的模型尤其有提升。\n但是它增加記憶體用量,且據報在一些設備上優化效果不佳或甚至負優化"
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@
"gui.mtrsteamloco.config.client.category.common": "常用",
"gui.mtrsteamloco.config.client.category.misc": "雜項",
"gui.mtrsteamloco.config.client.preloadbbmodel": "BBMODEL 預加載優化",
"gui.mtrsteamloco.config.client.preloadbbmodel.description": "這項優化提升使用 BBMODEL 的自定義列車的渲染效率,對於精細的模型尤其有提升。\n但是它增加記憶體用量,且據報在一些設備上並不使性能更好"
"gui.mtrsteamloco.config.client.preloadbbmodel.description": "這項優化提升使用 BBMODEL 的自定義列車的渲染效率,對於精細的模型尤其有提升。\n但是它增加記憶體用量,且據報在一些設備上優化效果不佳或甚至負優化"
}

0 comments on commit 3d9ee3d

Please sign in to comment.