Skip to content

Commit

Permalink
stage2/ml9: reword restart ui using mod name marker file
Browse files Browse the repository at this point in the history
  • Loading branch information
Sychic committed Jun 27, 2024
1 parent 68b1918 commit f7e7347
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@
import org.apache.logging.log4j.LogManager;

import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
* The initial entrypoint for stage2. With ModLauncher, we cannot yet know the MC version at this point, so this is a
Expand All @@ -34,7 +40,7 @@ public void load() {
}

@SuppressWarnings("unused") // called via reflection from stage1
public void loadFromMixin(Path gameDir) throws IOException {
public void loadFromMixin(Path gameDir) throws Exception {
final Path modsDir = gameDir.resolve("mods");
LoaderUI ui = LoaderUI.all(
new LoaderLoggingUI().updatesEveryMillis(1000),
Expand All @@ -46,7 +52,13 @@ public void loadFromMixin(Path gameDir) throws IOException {
} finally {
ui.complete();
}
ForkedRestartUI restartUI = new ForkedRestartUI("Restart Required", "One of the mods you have installed requires Essential. To complete the installation process, please restart Minecraft.");
List<URL> modNameMarkers = Collections.list(this.getClass().getClassLoader().getResources("META-INF/essential-loader-mod-name.txt"));
List<String> modNames = new ArrayList<>();
for (URL url : modNameMarkers) {
String modName = Files.readString(Paths.get(url.toURI()));
modNames.add(modName);
}
ForkedRestartUI restartUI = new ForkedRestartUI(modNames);
restartUI.show();
restartUI.waitForClose();
System.exit(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,28 @@
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ForkedRestartUI {
private final Logger LOGGER = LogManager.getLogger();
private final String title;
private final String description;
private final List<String> mods;
private ForkedJvm jvm;

public ForkedRestartUI(String title, String description) {
this.title = title;
this.description = description;
public ForkedRestartUI(List<String> mods) {
this.mods = mods;
}

public void show() {
try {
this.jvm = new ForkedJvm(getClass());

DataOutputStream out = new DataOutputStream(this.jvm.process.getOutputStream());
out.writeUTF(title);
out.writeUTF(description);
for (String name : this.mods) {
out.writeBoolean(true); // signal more entries
out.writeUTF(name);
}
out.writeBoolean(false); // signal end of list
out.flush();
} catch (IOException e) {
LOGGER.warn("Failed to fork JVM for RestartUI:", e);
Expand All @@ -50,12 +53,14 @@ public Boolean waitForClose() {
public static void main(String[] args) throws IOException {
DataInputStream in = new DataInputStream(System.in);

String title = in.readUTF();
String description = in.readUTF();
List<String> mods = new ArrayList<>();
while(in.readBoolean()) {
mods.add(in.readUTF());
}

Boolean verdict = null;
try {
RestartUI ui = new RestartUI(title, description);
RestartUI ui = new RestartUI(mods);
ui.show();

verdict = ui.waitForClose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@ public class RestartUI implements EssentialStyle {

private final JFrame frame = makeFrame(it -> closedFuture.complete(null));

private final String title;
private final String description;
private final List<String> mods;

public RestartUI(String title, String description) {
this.title = title;
this.description = description;
public RestartUI(List<String> mods) {
this.mods = mods;
}

public void show() {
Expand All @@ -30,9 +28,9 @@ public void show() {
content.setBorder(new EmptyBorder(0, 60 - X_SHADOW, 0, 60 - X_SHADOW));
content.add(Box.createHorizontalStrut(CONTENT_WIDTH));

htmlLabels.add(makeTitle(content, html(centered(title))));
htmlLabels.add(makeTitle(content, html(centered("Please restart your game to automatically install Essential."))));

final JLabel explanation = new JLabel(html(centered(description)), SwingConstants.CENTER);
final JLabel explanation = new JLabel(html(centered("The following mods require Essential Mod's API:")), SwingConstants.CENTER);
explanation.setMaximumSize(new Dimension(CONTENT_WIDTH, Integer.MAX_VALUE));
explanation.setForeground(COLOR_FOREGROUND);
explanation.setAlignmentX(Container.CENTER_ALIGNMENT);
Expand All @@ -42,6 +40,25 @@ public void show() {
content.add(explanation);
htmlLabels.add(explanation);

content.add(Box.createVerticalStrut(19));

final JPanel modList = new JPanel();
modList.setMaximumSize(new Dimension(CONTENT_WIDTH, Integer.MAX_VALUE));
modList.setBackground(COLOR_BACKGROUND);
modList.setLayout(new BoxLayout(modList, BoxLayout.Y_AXIS));
content.add(modList);

for (String modName : mods) {
final JLabel text = new JLabel(html(centered(modName)), SwingConstants.CENTER);
text.setForeground(COLOR_HIGHLIGHT);
text.setAlignmentX(Container.CENTER_ALIGNMENT);
if (Fonts.mediumItalic != null) {
text.setFont(Fonts.mediumItalic.deriveFont(16F));
}
modList.add(text);
htmlLabels.add(text);
}

content.add(Box.createVerticalStrut(32 - Y_SHADOW));

final JPanel buttons = new JPanel();
Expand Down Expand Up @@ -72,7 +89,7 @@ public void close() {
}

public static void main(String[] args) {
RestartUI ui = new RestartUI("Restart required!", "A restart is required for something.");
RestartUI ui = new RestartUI(List.of("Skytils"));
ui.show();
System.out.println(ui.waitForClose());
}
Expand Down

0 comments on commit f7e7347

Please sign in to comment.