-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ee84a6b
commit c04ad2e
Showing
10 changed files
with
120 additions
and
165 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 0 additions & 50 deletions
50
src/main/java/io/github/zekerzhayard/forgewrapper/Main.java
This file was deleted.
Oops, something went wrong.
82 changes: 50 additions & 32 deletions
82
src/main/java/io/github/zekerzhayard/forgewrapper/Utils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,65 @@ | ||
package io.github.zekerzhayard.forgewrapper; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.File; | ||
import java.net.MalformedURLException; | ||
import java.net.URISyntaxException; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.net.URL; | ||
import java.net.URLClassLoader; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
import cpw.mods.modlauncher.Launcher; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
import java.nio.file.StandardCopyOption; | ||
import java.security.MessageDigest; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.util.Locale; | ||
|
||
public class Utils { | ||
public static URL[] getURLs(List<String> blackList) { | ||
ClassLoader cl = Utils.class.getClassLoader(); | ||
List<URL> urls = new ArrayList<>(); | ||
if (cl instanceof URLClassLoader) { | ||
urls.addAll(Stream.of(((URLClassLoader) cl).getURLs()).filter(url -> blackList.stream().noneMatch(str -> url.getFile().endsWith(str))).collect(Collectors.toList())); | ||
} else { | ||
String[] elements = System.getProperty("java.class.path").split(File.pathSeparator); | ||
private static final char[] DIGITS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; | ||
|
||
for (String ele : elements) { | ||
try { | ||
if (blackList.stream().noneMatch(ele::endsWith)) { | ||
urls.add(new File(ele).toURI().toURL()); | ||
} | ||
} catch (MalformedURLException e) { | ||
e.printStackTrace(); | ||
public static void download(String url, String location) throws Exception { | ||
File localFile = new File(location); | ||
localFile.getParentFile().mkdirs(); | ||
if (localFile.isFile()) { | ||
try { | ||
System.out.println("Checking Fingerprints of installer..."); | ||
String md5 = new BufferedReader(new InputStreamReader(new URL(url + ".md5").openConnection().getInputStream())).readLine(); | ||
String sha1 = new BufferedReader(new InputStreamReader(new URL(url + ".sha1").openConnection().getInputStream())).readLine(); | ||
if (!checkMD5(location, md5) || !checkSHA1(location, sha1)) { | ||
System.out.println("Fingerprints do not match!"); | ||
localFile.delete(); | ||
} | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
if (!localFile.isFile()) { | ||
if (localFile.isDirectory()) { | ||
throw new RuntimeException(location + " must be a file!"); | ||
} | ||
System.out.println("Downloading forge installer... (" + url + " ---> " + location + ")"); | ||
Files.copy(new URL(url).openConnection().getInputStream(), Paths.get(location), StandardCopyOption.REPLACE_EXISTING); | ||
download(url, location); | ||
} | ||
return urls.toArray(new URL[0]); | ||
} | ||
|
||
public static File getLibrariesDir() { | ||
try { | ||
File laucnher = new File(Launcher.class.getProtectionDomain().getCodeSource().getLocation().toURI()); | ||
// see https://github.com/MinecraftForge/MinecraftForge/blob/863ab2ca184cf2e2dfa134d07bfc20d6a9a6a4e8/src/main/java/net/minecraftforge/fml/relauncher/libraries/LibraryManager.java#L151 | ||
// /<version> /modlauncher /mods /cpw /libraries | ||
return laucnher.getParentFile().getParentFile().getParentFile().getParentFile().getParentFile(); | ||
} catch (URISyntaxException e) { | ||
throw new RuntimeException(e); | ||
public static boolean checkMD5(String path, String hash) throws IOException, NoSuchAlgorithmException { | ||
String md5 = new String(encodeHex(MessageDigest.getInstance("MD5").digest(Files.readAllBytes(Paths.get(path))))); | ||
System.out.println("MD5: " + hash + " ---> " + md5); | ||
return md5.toLowerCase(Locale.ENGLISH).equals(hash.toLowerCase(Locale.ENGLISH)); | ||
} | ||
|
||
public static boolean checkSHA1(String path, String hash) throws IOException, NoSuchAlgorithmException { | ||
String sha1 = new String(encodeHex(MessageDigest.getInstance("SHA-1").digest(Files.readAllBytes(Paths.get(path))))); | ||
System.out.println("SHA-1: " + hash + " ---> " + sha1); | ||
return sha1.toLowerCase(Locale.ENGLISH).equals(hash.toLowerCase(Locale.ENGLISH)); | ||
} | ||
|
||
private static char[] encodeHex(final byte[] data) { | ||
final int l = data.length; | ||
final char[] out = new char[l << 1]; | ||
for (int i = 0, j = 0; i < l; i++) { | ||
out[j++] = DIGITS[(0xF0 & data[i]) >>> 4]; | ||
out[j++] = DIGITS[0x0F & data[i]]; | ||
} | ||
return out; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 0 additions & 65 deletions
65
src/main/java/io/github/zekerzhayard/forgewrapper/installer/Download.java
This file was deleted.
Oops, something went wrong.
34 changes: 34 additions & 0 deletions
34
src/main/java/io/github/zekerzhayard/forgewrapper/installer/Main.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package io.github.zekerzhayard.forgewrapper.installer; | ||
|
||
import java.io.File; | ||
import java.net.URL; | ||
import java.net.URLClassLoader; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
import cpw.mods.modlauncher.Launcher; | ||
import io.github.zekerzhayard.forgewrapper.Utils; | ||
|
||
public class Main { | ||
public static void main(String[] args) throws Exception { | ||
List<String> argsList = Stream.of(args).collect(Collectors.toList()); | ||
String version = argsList.get(argsList.indexOf("--fml.mcVersion") + 1) + "-" + argsList.get(argsList.indexOf("--fml.forgeVersion") + 1); | ||
String installerUrl = String.format("https://files.minecraftforge.net/maven/net/minecraftforge/forge/%s/forge-%s-installer.jar", version, version); | ||
String installerFileStr = String.format("./.forgewrapper/forge-%s-installer.jar", version); | ||
Utils.download(installerUrl, installerFileStr); | ||
|
||
URLClassLoader ucl = URLClassLoader.newInstance(new URL[] { | ||
Main.class.getProtectionDomain().getCodeSource().getLocation(), | ||
Launcher.class.getProtectionDomain().getCodeSource().getLocation(), | ||
new File(installerFileStr).toURI().toURL() | ||
}, null); | ||
|
||
Class<?> installer = ucl.loadClass("io.github.zekerzhayard.forgewrapper.installer.Installer"); | ||
if (!(boolean) installer.getMethod("install").invoke(null)) { | ||
return; | ||
} | ||
|
||
Launcher.main(args); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package why.does.multimc.target; | ||
|
||
import io.github.zekerzhayard.forgewrapper.installer.Main; | ||
|
||
public class Me { | ||
public static void main(String[] args) throws Exception { | ||
Main.main(args); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters