Skip to content

Commit f3c8db7

Browse files
me-nxCrystal15118
andauthored
feature: add not enough memory warning screen (#96)
* Feature: Warning for high or low max allocated RAM Added a method which checks the ram allocation and then gives a warning if it is offset by 1100 MB from the recommended max ram allocation. The offset for 1100 is set because AA-like categories use about 1000 MB more than other categories. MultiMC's Ram Allocation amounts may pass the 1000 MB barrier, to avoid it we add 100 more to the 1000 MB. There's also a system property to disable it. * feature: warning screen for allocated memory check Replaced the log message with a screen, removed the upper memory limit. * feat: check memory when starting resetting - Change the low memory warning to trigger when starting resetting, not on launch, as it depends on the max queued seeds config. - Mention max queued seeds in the warning, and link to the wiki. * feat: memory warning wiki link description Link: #96 --------- Co-authored-by: Crystal15118 <138210704+Crystal15118@users.noreply.github.com>
1 parent 783ee86 commit f3c8db7

5 files changed

Lines changed: 183 additions & 4 deletions

File tree

src/main/java/me/contaria/seedqueue/SeedQueue.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import me.contaria.seedqueue.compat.ModCompat;
55
import me.contaria.seedqueue.debug.SeedQueueSystemInfo;
66
import me.contaria.seedqueue.debug.SeedQueueWatchdog;
7+
import me.contaria.seedqueue.gui.MemoryWarningScreen;
78
import me.contaria.seedqueue.gui.wall.SeedQueueWallScreen;
89
import me.contaria.seedqueue.mixin.accessor.MinecraftClientAccessor;
910
import me.contaria.seedqueue.mixin.accessor.MinecraftServerAccessor;
@@ -15,9 +16,7 @@
1516
import net.fabricmc.loader.api.Version;
1617
import net.minecraft.client.MinecraftClient;
1718
import net.minecraft.client.gui.WorldGenerationProgressTracker;
18-
import net.minecraft.client.gui.screen.ProgressScreen;
19-
import net.minecraft.client.gui.screen.SaveLevelScreen;
20-
import net.minecraft.client.gui.screen.Screen;
19+
import net.minecraft.client.gui.screen.*;
2120
import net.minecraft.server.MinecraftServer;
2221
import org.apache.logging.log4j.LogManager;
2322
import org.apache.logging.log4j.Logger;
@@ -45,11 +44,33 @@ public class SeedQueue implements ClientModInitializer {
4544
public static final ThreadLocal<SeedQueueEntry> LOCAL_ENTRY = new ThreadLocal<>();
4645
public static SeedQueueEntry currentEntry;
4746

47+
public static boolean memoryWarningShown = false;
48+
4849
@Override
4950
public void onInitializeClient() {
5051
SeedQueueSounds.init();
5152
}
5253

54+
/**
55+
* @return true if a warning was shown
56+
*/
57+
public static boolean checkRamAllocation() {
58+
int allocatedMem = (int)(Runtime.getRuntime().maxMemory() / (1024 * 1024));
59+
60+
int recommendedMinRam = 1800 + (config.maxCapacity * 180);
61+
62+
if (allocatedMem < recommendedMinRam && config.maxCapacity != 0) {
63+
MinecraftClient.getInstance().openScreen(
64+
// Add 128 MiB to the recommended amount to account for Runtime#maxMemory() inaccuracies
65+
new MemoryWarningScreen(allocatedMem, recommendedMinRam + 128)
66+
);
67+
68+
return true;
69+
}
70+
71+
return false;
72+
}
73+
5374
/**
5475
* Polls a new {@link SeedQueueEntry} from the queue and plays it.
5576
*

src/main/java/me/contaria/seedqueue/SeedQueueConfig.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ public class SeedQueueConfig implements SpeedrunConfig {
113113
@Config.Numbers.Whole.Bounds(min = -1, max = 500, enforce = Config.Numbers.EnforceBounds.MIN_ONLY)
114114
public long chunkMapFreezing = -1;
115115

116+
@Config.Category("troubleshooting")
117+
public boolean checkMinMemory = true;
118+
116119
@Config.Category("advanced")
117120
public boolean showAdvancedSettings = false;
118121

@@ -347,6 +350,12 @@ public boolean isAvailable() {
347350
return !SeedQueue.isActive();
348351
}
349352

353+
@Override
354+
public void onSave(JsonObject jsonObject) {
355+
// Changing the config can change whether the warning should be shown
356+
SeedQueue.memoryWarningShown = false;
357+
}
358+
350359
public static class WindowSize {
351360
private int width;
352361
private int height;
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
package me.contaria.seedqueue.gui;
2+
3+
import me.contaria.seedqueue.SeedQueue;
4+
import net.minecraft.client.MinecraftClient;
5+
import net.minecraft.client.gui.screen.ConfirmChatLinkScreen;
6+
import net.minecraft.client.gui.screen.Screen;
7+
import net.minecraft.client.gui.screen.ScreenTexts;
8+
import net.minecraft.client.gui.widget.ButtonWidget;
9+
import net.minecraft.client.util.Rect2i;
10+
import net.minecraft.client.util.math.MatrixStack;
11+
import net.minecraft.text.*;
12+
import net.minecraft.util.Formatting;
13+
import net.minecraft.util.Util;
14+
15+
import java.io.IOException;
16+
import java.util.List;
17+
18+
public class MemoryWarningScreen extends Screen {
19+
static final String WIKI_URI = "https://github.com/contariaa/seedqueue/wiki/Troubleshooting#not-enough-memory";
20+
21+
static final MutableText HOVERED_WIKI_LINK = new LiteralText(WIKI_URI)
22+
.styled(style -> style.withFormatting(Formatting.UNDERLINE));
23+
24+
final Text message;
25+
List<StringRenderable> messageParts;
26+
int wikiLinkY;
27+
28+
public MemoryWarningScreen(int allocatedMem, int recommendedMinMem) {
29+
super(new TranslatableText("seedqueue.menu.memoryWarning.title"));
30+
31+
this.message = new TranslatableText("seedqueue.menu.memoryWarning.message", allocatedMem, recommendedMinMem);
32+
}
33+
34+
@Override
35+
protected void init() {
36+
super.init();
37+
38+
this.messageParts = this.textRenderer.wrapLines(this.message, this.width - 50);
39+
40+
int btnXBase = this.width / 2 - 155;
41+
int btnY = this.height / 6 + 100;
42+
43+
this.addButton(new ButtonWidget(btnXBase, btnY, 150, 20, ScreenTexts.PROCEED, (_btn) ->
44+
MinecraftClient.getInstance().openScreen(null)
45+
));
46+
47+
Text ignoreWarningText = new TranslatableText("seedqueue.menu.memoryWarning.doNotWarnAgain");
48+
49+
this.addButton(new ButtonWidget(btnXBase + 160, btnY, 150, 20, ignoreWarningText, (_btn) -> {
50+
SeedQueue.config.checkMinMemory = false;
51+
52+
try {
53+
SeedQueue.config.container.save();
54+
} catch (IOException e) {
55+
SeedQueue.LOGGER.error("failed to save the SeedQueue config", e);
56+
}
57+
58+
MinecraftClient.getInstance().openScreen(null);
59+
}));
60+
}
61+
62+
@Override
63+
public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) {
64+
this.renderBackground(matrices);
65+
this.drawCenteredText(matrices, this.textRenderer, this.title, this.width / 2, 70, 0xffffff);
66+
67+
int nextLineY = 90;
68+
69+
for (StringRenderable line : messageParts) {
70+
this.drawCenteredText(matrices, this.textRenderer, line, this.width / 2, nextLineY, 0xffffff);
71+
nextLineY += 9;
72+
}
73+
74+
nextLineY += 9;
75+
76+
Text wikiLinkInfo = new TranslatableText("seedqueue.menu.memoryWarning.wikiLinkInfo");
77+
this.drawCenteredText(matrices, this.textRenderer, wikiLinkInfo, this.width / 2, nextLineY, 0xffffff);
78+
nextLineY += 9;
79+
80+
this.drawCenteredText(
81+
matrices,
82+
this.textRenderer,
83+
this.mouseOverWikiText(mouseX, mouseY) ? HOVERED_WIKI_LINK : StringRenderable.plain(WIKI_URI),
84+
this.width / 2,
85+
nextLineY,
86+
0xffffff
87+
);
88+
wikiLinkY = nextLineY;
89+
90+
super.render(matrices, mouseX, mouseY, delta);
91+
}
92+
93+
@Override
94+
public boolean mouseClicked(double mouseX, double mouseY, int button) {
95+
assert this.client != null;
96+
97+
if (button == 0 && mouseOverWikiText((int) mouseX, (int) mouseY)) {
98+
if (this.client.options.chatLinksPrompt) {
99+
this.client.openScreen(new ConfirmChatLinkScreen((open) -> {
100+
if (open) {
101+
Util.getOperatingSystem().open(WIKI_URI);
102+
}
103+
104+
this.client.openScreen(this);
105+
}, WIKI_URI, true));
106+
} else {
107+
Util.getOperatingSystem().open(WIKI_URI);
108+
}
109+
}
110+
111+
return super.mouseClicked(mouseX, mouseY, button);
112+
}
113+
114+
private boolean mouseOverWikiText(int mouseX, int mouseY) {
115+
int wikiTextWidth = this.textRenderer.getWidth(WIKI_URI);
116+
117+
int startX = this.width / 2 - wikiTextWidth / 2;
118+
119+
Rect2i wikiTextRect = new Rect2i(startX, wikiLinkY, wikiTextWidth, textRenderer.fontHeight);
120+
121+
return wikiTextRect.contains(mouseX, mouseY);
122+
}
123+
124+
@Override
125+
public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
126+
if (keyCode == 256) {
127+
MinecraftClient.getInstance().openScreen(null);
128+
return true;
129+
} else {
130+
return super.keyPressed(keyCode, scanCode, modifiers);
131+
}
132+
}
133+
}

src/main/java/me/contaria/seedqueue/mixin/compat/atum/AtumMixin.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import net.minecraft.client.gui.screen.ProgressScreen;
1313
import net.minecraft.client.gui.screen.Screen;
1414
import org.spongepowered.asm.mixin.Mixin;
15+
import org.spongepowered.asm.mixin.Unique;
1516
import org.spongepowered.asm.mixin.injection.At;
1617
import org.spongepowered.asm.mixin.injection.Inject;
1718
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
@@ -30,6 +31,13 @@ public abstract class AtumMixin {
3031
)
3132
)
3233
private static void openSeedQueueWallScreen(MinecraftClient client, Screen screen, Operation<Void> original) {
34+
if (!SeedQueue.memoryWarningShown && SeedQueue.config.checkMinMemory) {
35+
if (SeedQueue.checkRamAllocation()) {
36+
SeedQueue.memoryWarningShown = true;
37+
return;
38+
}
39+
}
40+
3341
if (!SeedQueue.isActive()) {
3442
original.call(client, screen);
3543
return;

src/main/resources/assets/seedqueue/lang/en_us.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77
"seedqueue.menu.layout_exception.title": "Custom Layout failed!",
88
"seedqueue.menu.layout_exception.description": "Failed with %s, check the log for more information.",
99

10+
"seedqueue.menu.memoryWarning.title": "§6§lNot enough memory allocated",
11+
"seedqueue.menu.memoryWarning.message": "SeedQueue might not have enough memory available (%d MiB). Try allocating at least %d MiB or lowering Max Queued Seeds.",
12+
"seedqueue.menu.memoryWarning.messageLowerQueued": "SeedQueue might not have enough memory available (%d MiB). Try lowering 'Max Queued Seeds'.",
13+
"seedqueue.menu.memoryWarning.wikiLinkInfo": "Click on this link to read more on the SeedQueue Wiki:",
14+
"seedqueue.menu.memoryWarning.doNotWarnAgain": "Do not warn again",
15+
1016
"seedqueue.menu.benchmark.title": "SeedQueue Benchmark",
1117
"seedqueue.menu.benchmark.result": "%s resets in %ss (%s RPS)",
1218

@@ -48,6 +54,7 @@
4854
"speedrunapi.config.seedqueue.category.wall": "Wall Screen Settings",
4955
"speedrunapi.config.seedqueue.category.performance": "Performance Settings",
5056
"speedrunapi.config.seedqueue.category.misc": "Misc. Settings",
57+
"speedrunapi.config.seedqueue.category.troubleshooting": "Troubleshooting",
5158
"speedrunapi.config.seedqueue.category.advanced": "Advanced Settings",
5259
"speedrunapi.config.seedqueue.category.threading": "Threading Settings",
5360
"speedrunapi.config.seedqueue.category.experimental": "Experimental Settings",
@@ -126,5 +133,6 @@
126133
"speedrunapi.config.seedqueue.option.benchmarkResets": "Benchmark Resets",
127134
"speedrunapi.config.seedqueue.option.benchmarkResets.description": "The default amount of resets done when starting a benchmark.\nYou can start a benchmark using the \"Start Benchmark\" keybind and cancel it before it reaches the configured amount of resets using the \"Stop Benchmark\" keybind.",
128135
"speedrunapi.config.seedqueue.option.showChunkMaps": "Chunkmap Overlay",
129-
"speedrunapi.config.seedqueue.option.showChunkMaps.description": "Renders an overlay with all the chunkmaps of currently generating seeds."
136+
"speedrunapi.config.seedqueue.option.showChunkMaps.description": "Renders an overlay with all the chunkmaps of currently generating seeds.",
137+
"speedrunapi.config.seedqueue.option.checkMinMemory": "Check Allocated Memory"
130138
}

0 commit comments

Comments
 (0)