aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/github/moulberry/notenoughupdates/envcheck/EnvironmentScan.java
blob: bedbc87b7c5e325bf1841a43dc91069924c85df0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package io.github.moulberry.notenoughupdates.envcheck;

import javax.swing.*;
import java.lang.reflect.Field;
import java.util.Objects;

public class EnvironmentScan {

		static Class<?> tryGetClass(String name) {
				try {
						return Class.forName(name);
				} catch (ClassNotFoundException e) {
						return null;
				}
		}

		static Object tryGetField(Class<?> clazz, Object inst, String name) {
				if (clazz == null) return null;
				try {
						Field declaredField = clazz.getDeclaredField(name);
						return declaredField.get(inst);
				} catch (NoSuchFieldException | IllegalAccessException ignored) {
				}
				return null;
		}


		static boolean isAtLeast(Object left, int right) {
				if (left instanceof Integer) {
						return (Integer) left >= right;
				}
				return false;
		}

		static boolean shouldCheckOnce = true;

		public static void checkEnvironmentOnce() {
				if (shouldCheckOnce) checkEnvironment();
		}


		static void checkEnvironment() {
				shouldCheckOnce = false;
				checkForgeEnvironment();
		}

		static void checkForgeEnvironment() {
				Class<?> forgeVersion = tryGetClass("net.minecraftforge.common.ForgeVersion");
				if (forgeVersion == null
								|| !Objects.equals(tryGetField(forgeVersion, null, "majorVersion"), 11)
								|| !Objects.equals(tryGetField(forgeVersion, null, "minorVersion"), 15)
								|| !isAtLeast(tryGetField(forgeVersion, null, "revisionVersion"), 1)
								|| !Objects.equals(tryGetField(forgeVersion, null, "mcVersion"), "1.8.9")
				) {

						System.out.printf("Forge Version : %s%nMajor : %s%nMinor : %s%nRevision : %s%nMinecraft : %s%n",
										forgeVersion,
										tryGetField(forgeVersion, null, "majorVersion"),
										tryGetField(forgeVersion, null, "minorVersion"),
										tryGetField(forgeVersion, null, "revisionVersion"),
										tryGetField(forgeVersion, null, "mcVersion")
						);
						missingOrOutdatedForgeError();
				}
		}

		static void missingOrOutdatedForgeError() {
				showErrorMessage(
								"You just launched NotEnoughUpdates with the wrong (or no) modloader installed.",
								"",
								"NotEnoughUpdates only works in Minecraft 1.8.9, with Forge 11.15.1+",
								"Please relaunch NotEnoughUpdates in the correct environment.",
								"If you are using Minecraft 1.8.9 with Forge 11.15.1+ installed, please contact support.",
								"Click OK to launch anyways."
				);
		}


		public static void showErrorMessage(String... messages) {
				String message = String.join("\n", messages);
				System.setProperty("java.awt.headless", "false");
				JOptionPane.showMessageDialog(
								null, message, "NotEnoughUpdates - Problematic System Configuration", JOptionPane.ERROR_MESSAGE
				);
		}
}