Skip to content

Commit

Permalink
And there'll be (ft. SHY Martin)
Browse files Browse the repository at this point in the history
Only me, no regrets
In the middle of the night
Buying drinks to myself
And you won't be on my mind
  • Loading branch information
My-Name-Is-Jeff committed Sep 16, 2022
1 parent 3043925 commit 6f73640
Show file tree
Hide file tree
Showing 12 changed files with 425 additions and 210 deletions.
5 changes: 1 addition & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,4 @@ run/
# Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored)
!gradle-wrapper.jar

.vscode/

!loader.jar
!loader-dev.jar
.vscode/
4 changes: 1 addition & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ loom {
runConfigs {
getByName("client") {
isIdeConfigGenerated = true
vmArg("-javaagent:\"../src/main/resources/assets/skytils/loader/loader-dev.jar\"")
}
remove(getByName("server"))
}
Expand Down Expand Up @@ -201,8 +200,7 @@ tasks {
"META-INF/maven/**",
"META-INF/versions/**",
"META-INF/com.android.tools/**",
"fabric.mod.json",
"**/loader-dev.jar"
"fabric.mod.json"
)
mergeServiceFiles()
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/gg/skytils/skytilsmod/Reference.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ public class Reference {
public static final String MODID = "skytils";
public static final String MOD_NAME = "Skytils";
public static final String VERSION = "1.3.0-pre3";

public static final int apiVersion = 1;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* Skytils - Hypixel Skyblock Quality of Life Mod
* Copyright (C) 2022 Skytils
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package gg.skytils.skytilsmod.tweaker;

import gg.skytils.skytilsmod.Reference;
import org.apache.commons.io.IOUtils;
import org.apache.logging.log4j.LogManager;

import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Locale;

import static gg.skytils.skytilsmod.tweaker.TweakerUtil.*;

public class EssentialPlatformSetup {
private static final String[] dataURLCandidates = {System.getProperty("skytils.dataURL"), Reference.dataUrl, "https://skytilsmod-data.pages.dev/", "https://cdn.jsdelivr.net/gh/Skytils/SkytilsMod-Data@main/"};

private static boolean trySetDataUrl(String url) {
if (url == null) return false;
try {
return makeRequest(url + "CANYOUSEEME").contains("YOUCANSEEME");
} catch (Exception e) {
LogManager.getLogger().error("Failed to contact url " + url);
return false;
}
}

@SuppressWarnings("unused")
public static void setup() {
try {
String ver = System.getProperty("java.runtime.version", "unknown");
String javaLoc = System.getProperty("java.home");
if (ver.contains("1.8.0_51") || javaLoc.contains("jre-legacy")) {
Path keyStoreLoc = Paths.get("./config/skytils/updates/files/skytilsletsencrypt.jks");
File keyStoreFile = keyStoreLoc.toFile();
if (!keyStoreFile.exists()) {
System.out.println("Skytils is attempting to run keytool.");
Files.createDirectories(keyStoreLoc.getParent());
try (InputStream in = EssentialPlatformSetup.class.getResourceAsStream("/skytilsletsencrypt.jks"); OutputStream os = Files.newOutputStream(keyStoreLoc)) {
IOUtils.copy(in, os);
}
String os = System.getProperty("os.name", "unknown");
String keyStorePath = javaLoc + File.separator + "lib" + File.separator + "security" + File.separator + "cacerts";
String keyToolPath = javaLoc + File.separator + "bin" + File.separator + (os.toLowerCase(Locale.ENGLISH).startsWith("windows") ? "keytool.exe" : "keytool");
File log = new File("./config/skytils/updates/files/sslfix-" + System.currentTimeMillis() + ".log");
new ProcessBuilder()
.command(keyToolPath, "-importkeystore", "-srckeystore", keyStoreFile.getAbsolutePath(), "-destkeystore", keyStorePath, "-srcstorepass", "skytilsontop", "-deststorepass", "changeit", "-noprompt")
.redirectOutput(log)
.redirectError(log)
.start().waitFor();
System.out.println("A reboot of Minecraft is required for the code to work, force closing the game");
exit();
}
}
} catch (Throwable t) {
t.printStackTrace();
}

registerTransformerExclusions(
"kotlin.",
"kotlinx.",
"gg.skytils.asmhelper.",
"gg.skytils.skytilsmod.tweaker.",
"gg.skytils.skytilsmod.asm."
);

for (final String url : dataURLCandidates) {
if (trySetDataUrl(url)) {
Reference.dataUrl = url;
break;
}
}
LogManager.getLogger().info("Data URL: " + Reference.dataUrl);

try {
if (Integer.parseInt(makeRequest(Reference.dataUrl + "api/version")) > Reference.apiVersion) {
showMessage("<html><p>" +
"Your version of Skytils requires a<br>" +
"mandatory update before you can play!<br>" +
"</p></html>");
}
} catch (Throwable t) {
t.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,13 @@
import kotlin.text.StringsKt;
import net.minecraftforge.fml.relauncher.IFMLLoadingPlugin;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.lang.reflect.Method;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Map;

import static gg.skytils.skytilsmod.tweaker.TweakerUtil.exit;
import static gg.skytils.skytilsmod.tweaker.TweakerUtil.showMessage;

@IFMLLoadingPlugin.Name("Skytils On Top")
@IFMLLoadingPlugin.SortingIndex(69)
public class SkytilsLoadingPlugin implements IFMLLoadingPlugin {
Expand Down Expand Up @@ -142,21 +138,6 @@ public SkytilsLoadingPlugin() throws URISyntaxException {
kotlinPlugin = new SkytilsLoadingPluginKt();
}

/**
* Bypasses forges security manager to exit the jvm
*/
public static void exit() {
try {
Class<?> clazz = Class.forName("java.lang.Shutdown");
Method m_exit = clazz.getDeclaredMethod("exit", int.class);
m_exit.setAccessible(true);
m_exit.invoke(null, 0);
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}

private boolean checkForClass(String className) {
try {
Class.forName(className, false, getClass().getClassLoader());
Expand All @@ -166,64 +147,6 @@ private boolean checkForClass(String className) {
}
}

private void showMessage(String errorMessage) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}

// This makes the JOptionPane show on taskbar and stay on top
JFrame frame = new JFrame();
frame.setUndecorated(true);
frame.setAlwaysOnTop(true);
frame.setLocationRelativeTo(null);
frame.setVisible(true);

Icon icon = null;
try {
URL url = SkytilsLoadingPlugin.class.getResource("/assets/skytils/sychicpet.gif");
if (url != null) {
icon = new ImageIcon(Toolkit.getDefaultToolkit().createImage(url).getScaledInstance(50, 50, Image.SCALE_DEFAULT));
}
} catch (Exception e) {
e.printStackTrace();
}

JButton discordLink = new JButton("Join the Discord");
discordLink.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent event) {
try {
Desktop.getDesktop().browse(new URI("https://discord.gg/skytils"));
} catch (Exception e) {
e.printStackTrace();
}
}
});

JButton close = new JButton("Close");
close.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
exit();
}
});

Object[] options = new Object[]{discordLink, close};
JOptionPane.showOptionDialog(
frame,
errorMessage,
"Skytils Error",
JOptionPane.DEFAULT_OPTION,
JOptionPane.ERROR_MESSAGE,
icon,
options,
options[0]
);
exit();
}

@Override
public String[] getASMTransformerClass() {
return kotlinPlugin.getASMTransformerClass();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/*
* Skytils - Hypixel Skyblock Quality of Life Mod
* Copyright (C) 2022 Skytils
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package gg.skytils.skytilsmod.tweaker;

import net.minecraftforge.fml.relauncher.FMLSecurityManager;
import sun.security.util.SecurityConstants;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.lang.reflect.Field;
import java.security.AccessController;
import java.security.Permission;
import java.security.PrivilegedAction;

public class SkytilsSecurityManager extends SecurityManager {
private final boolean isForge;

public SkytilsSecurityManager(boolean isForge) {
this.isForge = isForge;
}

@Override
public void checkPermission(Permission perm) {
String permName = perm.getName() != null ? perm.getName() : "missing";
if (permName.startsWith("exitVM")) {
Class<?>[] classContexts = getClassContext();
String callingClass = classContexts.length > 3 ? classContexts[4].getName() : "none";
String callingParent = classContexts.length > 4 ? classContexts[5].getName() : "none";
// Skytils: allow Skytils tweaker classes to close the game
if (callingClass.startsWith("gg.skytils.skytilsmod.tweaker.")) return;
if (callingClass.startsWith("gg.skytils.skytilsmod.loader.")) return;
// Skytils: allow the LaunchWrapper to close the game
if (callingClass.equals("net.minecraft.launchwrapper.Launch") && callingParent.equals("net.minecraft.launchwrapper.Launch")) {
showMessage();
return;
}
// FML is allowed to call system exit and the Minecraft applet (from the quit button)
if (!isForge && !(callingClass.startsWith("net.minecraftforge.fml.")
|| "net.minecraft.server.dedicated.ServerHangWatchdog$1".equals(callingClass)
|| "net.minecraft.server.dedicated.ServerHangWatchdog".equals(callingClass)
|| ("net.minecraft.client.Minecraft".equals(callingClass) && "net.minecraft.client.Minecraft".equals(callingParent))
|| ("net.minecraft.server.dedicated.DedicatedServer".equals(callingClass) && "net.minecraft.server.MinecraftServer".equals(callingParent)))
) {
throw new FMLSecurityManager.ExitTrappedException();
}
} else if ("setSecurityManager".equals(permName)) {
throw new SecurityException("Cannot replace the FML (Skytils) security manager");
}
}

private void showMessage() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}

// This makes the JOptionPane show on taskbar and stay on top
JFrame frame = new JFrame();
frame.setUndecorated(true);
frame.setAlwaysOnTop(true);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

JButton openLogs = new JButton("Open Logs Folder");
openLogs.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent event) {
try {
Desktop.getDesktop().open(new File("./logs"));
} catch (Exception e) {
e.printStackTrace();
}
}
});

JButton openLatestLog = new JButton("Open Latest Log");
openLatestLog.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent event) {
try {
Desktop.getDesktop().open(new File("./logs/latest.log"));
} catch (Exception e) {
e.printStackTrace();
}
}
});

Object[] options = new Object[]{openLatestLog, openLogs};
JOptionPane.showOptionDialog(
frame,
"The game crashed whilst launching.",
"Minecraft Crash",
JOptionPane.DEFAULT_OPTION,
JOptionPane.ERROR_MESSAGE,
null,
options,
options[0]
);
}

static void overrideSecurityManager(Boolean isForge) {
try {
SecurityManager s = new SkytilsSecurityManager(isForge);

if (s.getClass().getClassLoader() != null) {
AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
s.getClass().getProtectionDomain().implies
(SecurityConstants.ALL_PERMISSION);
return null;
});
}

Field field = TweakerUtil.findField(System.class, "security");
field.setAccessible(true);
field.set(null, s);
} catch (Throwable e) {
e.printStackTrace();
}
}
}
Loading

0 comments on commit 6f73640

Please sign in to comment.