Skip to content

Commit

Permalink
Add error report screen
Browse files Browse the repository at this point in the history
  • Loading branch information
zbx1425 committed Dec 3, 2022
1 parent cb47371 commit 9d56509
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static void load(Path path) {
enableSmoke = configObject.get("enableSmoke").getAsBoolean();
hideRidingTrain = configObject.get("hideRidingTrain").getAsBoolean();
} catch (Exception ex) {
Main.LOGGER.warn(ex);
Main.LOGGER.warn("Failed loading client config:", ex);
ex.printStackTrace();
save();
}
Expand Down Expand Up @@ -67,7 +67,7 @@ public static void save() {
configObject.addProperty("hideRidingTrain", hideRidingTrain);
Files.writeString(path, new GsonBuilder().setPrettyPrinting().create().toJson(configObject));
} catch (Exception ex) {
Main.LOGGER.warn(ex);
Main.LOGGER.warn("Failed loading client config:", ex);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static void reset(ResourceManager resourceManager) {
try {
MainClient.shaderManager.reloadShaders(resourceManager);
} catch (IOException e) {
Main.LOGGER.error("Failed to load shader:", e);
Main.LOGGER.error("Failed loading shader:", e);
}
MainClient.modelManager.clear();
MainClient.atlasManager.clear();
Expand All @@ -52,7 +52,7 @@ public static void init(ResourceManager resourceManager) {

stateCapture.restore();

Main.LOGGER.info("Models: " + MainClient.modelManager.loadedRawModels.size() + " models loaded, "
Main.LOGGER.info("MTR-NTE: " + MainClient.modelManager.loadedRawModels.size() + " models loaded, "
+ MainClient.modelManager.uploadedVertArraysCount + " VAOs uploaded.");

mtr.client.TrainClientRegistry.register("dk3", new TrainProperties(
Expand Down
62 changes: 0 additions & 62 deletions common/src/main/java/cn/zbx1425/mtrsteamloco/ServerConfig.java

This file was deleted.

93 changes: 93 additions & 0 deletions common/src/main/java/cn/zbx1425/mtrsteamloco/gui/ErrorScreen.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package cn.zbx1425.mtrsteamloco.gui;

import cn.zbx1425.mtrsteamloco.ClientConfig;
import com.mojang.blaze3d.vertex.PoseStack;
import mtr.mappings.Text;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.FormattedText;
import net.minecraft.network.chat.Style;

import java.util.Arrays;
import java.util.List;

public class ErrorScreen extends Screen {

private final List<String> errorList;

private String[] splitErrorList;

private Screen parentScreen;

public ErrorScreen(List<String> errorList, Screen parentScreen) {
super(Text.literal("Error"));
this.errorList = errorList;
this.parentScreen = parentScreen;
}

final int SQUARE_SIZE = 20;
final int TEXT_HEIGHT = 8;

private int offset = 0;
private int pageLines;
private int pages;

private final Button btnPrevPage = new Button(0, SQUARE_SIZE, SQUARE_SIZE, SQUARE_SIZE, Text.literal("↑"), sender -> {
changeOffset(-1);
});
private final Button btnNextPage = new Button(0, SQUARE_SIZE * 5, SQUARE_SIZE, SQUARE_SIZE, Text.literal("↓"), sender -> {
changeOffset(1);
});
private final Button btnClose = new Button(0, SQUARE_SIZE * 7, SQUARE_SIZE, SQUARE_SIZE, Text.literal("X"), sender -> {
Minecraft.getInstance().setScreen(this.parentScreen);
});

@Override
protected void init() {
super.init();
btnPrevPage.x = width - SQUARE_SIZE * 2;
btnNextPage.x = width - SQUARE_SIZE * 2;
btnClose.x = width - SQUARE_SIZE * 2;
btnClose.y = height - SQUARE_SIZE * 2;
addRenderableWidget(btnPrevPage);
addRenderableWidget(btnNextPage);
addRenderableWidget(btnClose);
splitErrorList = font.getSplitter()
.splitLines(Text.literal("NTE Resource Loading Exception Report\n\n" + String.join("\n",
errorList.stream().flatMap(l -> Arrays.stream(l.split("\n"))).filter(l ->
!(l.contains("CompletableFuture") || l.contains("SimpleReloadInstance") || l.contains("BlockableEventLoop")))
.map(l -> l.replace("\t", " ").replace("\r", "")).toList()
)), width - SQUARE_SIZE * 4, Style.EMPTY)
.stream().map(FormattedText::getString).toArray(String[]::new);
pageLines = (height - SQUARE_SIZE * 2) / (TEXT_HEIGHT + 1);
pages = (int) Math.ceil(splitErrorList.length * 1.0F / pageLines);
changeOffset(0);
}

@Override
public void render(PoseStack poseStack, int i, int j, float f) {
this.fillGradient(poseStack, 0, 0, this.width, this.height, 0xFF03458C, 0xFF001A3B);
super.render(poseStack, i, j, f);

drawCenteredString(poseStack, font, Integer.toString(offset + 1), (int)(width - SQUARE_SIZE * 1.5F), (int)(SQUARE_SIZE * 2.5F - TEXT_HEIGHT * 0.5F), 0xFFFFFFFF);
drawCenteredString(poseStack, font, "/", (int)(width - SQUARE_SIZE * 1.5F), (int)(SQUARE_SIZE * 3.5F - TEXT_HEIGHT * 0.5F), 0xFFFFFFFF);
drawCenteredString(poseStack, font, Integer.toString(pages), (int)(width - SQUARE_SIZE * 1.5F), (int)(SQUARE_SIZE * 4.5F - TEXT_HEIGHT * 0.5F), 0xFFFFFFFF);

for (int n = offset * pageLines; n < Math.min(splitErrorList.length, (offset + 1) * pageLines); n++) {
drawString(poseStack, font, splitErrorList[n], SQUARE_SIZE, (n - offset * pageLines) * (TEXT_HEIGHT + 1) + SQUARE_SIZE, 0xFFFFFFFF);
}
}

private void changeOffset(int pages) {
offset += pages;
offset = Math.max(0, Math.min(offset, this.pages - 1));
btnPrevPage.active = offset != 0;
btnNextPage.active = offset != this.pages - 1;
}

@Override
public void onClose() {
this.minecraft.setScreen(parentScreen);
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package cn.zbx1425.mtrsteamloco.mixin;

import cn.zbx1425.mtrsteamloco.CustomResources;
import cn.zbx1425.mtrsteamloco.gui.ConfigScreen;
import cn.zbx1425.mtrsteamloco.gui.ErrorScreen;
import cn.zbx1425.mtrsteamloco.gui.WidgetLabel;
import cn.zbx1425.mtrsteamloco.render.integration.MtrModelRegistryUtil;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import mtr.client.ClientData;
import mtr.client.ICustomResources;
import mtr.client.TrainClientRegistry;
import mtr.render.TrainRendererBase;
import net.minecraft.client.Minecraft;
import net.minecraft.server.packs.resources.ResourceManager;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
Expand All @@ -24,6 +28,7 @@ public class CustomResourcesMixin {

@Inject(at = @At("HEAD"), method = "reload(Lnet/minecraft/server/packs/resources/ResourceManager;)V")
private static void reloadHead(ResourceManager manager, CallbackInfo ci) {
MtrModelRegistryUtil.loadingErrorList.clear();
MtrModelRegistryUtil.resourceManager = manager;
CustomResources.reset(manager);
}
Expand All @@ -34,6 +39,9 @@ private static void reloadTail(ResourceManager manager, CallbackInfo ci) {
TrainRendererBase renderer = TrainClientRegistry.getTrainProperties(train.trainId).renderer;
((TrainClientAccessor)train).setTrainRenderer(renderer.createTrainInstance(train));
});
if (MtrModelRegistryUtil.loadingErrorList.size() > 0) {
Minecraft.getInstance().setScreen(new ErrorScreen(MtrModelRegistryUtil.loadingErrorList, Minecraft.getInstance().screen));
}
}

@Inject(at = @At("HEAD"), method = "readResource", cancellable = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ private void ctor(JsonObject model, JsonObject properties, DoorAnimationType doo
}
} catch (Exception e) {
Main.LOGGER.error("Failed loading OBJ into DynamicTrainModel", e);
MtrModelRegistryUtil.loadingErrorList.add(ExceptionUtils.getStackTrace(e));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.resources.ResourceManager;

import java.util.ArrayList;
import java.util.List;

public class MtrModelRegistryUtil {

public static ResourceManager resourceManager;

public static final List<String> loadingErrorList = new ArrayList<>();

public static final ResourceLocation PLACEHOLDER_TILE_TEXTURE_LOCATION = new ResourceLocation("mtrsteamloco:textures/misc/nte_tile_faded.png");

public static JsonObject createDummyBbDataPack(String actualPath, String textureId) {
Expand Down

0 comments on commit 9d56509

Please sign in to comment.