Last active
December 19, 2015 10:29
-
-
Save azenla/5941298 to your computer and use it in GitHub Desktop.
Minetweak Launcher
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
package org.minetweak.launcher; | |
import com.google.gson.Gson; | |
import com.google.gson.GsonBuilder; | |
import org.apache.commons.io.IOUtils; | |
import java.io.File; | |
import java.io.FileReader; | |
import java.io.IOException; | |
import java.lang.reflect.Array; | |
import java.lang.reflect.InvocationTargetException; | |
import java.lang.reflect.Method; | |
import java.net.MalformedURLException; | |
import java.net.URL; | |
import java.net.URLClassLoader; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
public class Launcher { | |
public static Logger logger = Logger.getGlobal(); | |
public static DownloadConfig config; | |
public static String oldJSON; | |
public static String json; | |
public static Gson gson = new GsonBuilder().create(); | |
public static File dataDirectory = new File(System.getProperty("user.home"), ".minetweak"); | |
public static File configFile = new File(dataDirectory, "download.cfg"); | |
public static void main(String[] args) throws Exception { | |
logger.setLevel(Level.ALL); | |
if (!dataDirectory.exists()) { | |
if (!dataDirectory.mkdirs()) { | |
logger.severe("Unable to create data directory at " + dataDirectory.getAbsolutePath()); | |
} | |
} | |
logger.info("Downloading Minetweak Download Configuration"); | |
downloadConfig(); | |
logger.info("Reading Minetweak Download Configuration"); | |
if (readConfig()) { | |
logger.info("Downloading Libraries"); | |
downloadLibraries(); | |
} | |
launch(args); | |
} | |
public static void downloadLibraries() { | |
for (DownloadConfig.library lib : config.libraries) { | |
logger.info("Downloading " + lib.id + " to " + lib.name + "."); | |
Utils.downloadFile(dataDirectory.getAbsolutePath() + "/" + lib.name, lib.url); | |
logger.info("Done downloading " + lib.id + "."); | |
} | |
} | |
public static void downloadConfig() { | |
if (configFile.exists()) { | |
try { | |
oldJSON = IOUtils.toString(new FileReader(configFile)); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
Utils.downloadFile(configFile.getAbsolutePath(), "https://gist.github.com/kaendfinger/5941105/raw/downloadInfo.json"); | |
} | |
public static boolean readConfig() { | |
try { | |
json = IOUtils.toString(new FileReader(configFile)); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
config = gson.fromJson(json, DownloadConfig.class); | |
if (json.equals(oldJSON)) { | |
logger.info("Download Config Up-to-date!"); | |
return false; | |
} | |
return true; | |
} | |
public static void launch(String[] args) throws Exception { | |
List<URL> jarPaths = new ArrayList<URL>(); | |
for (DownloadConfig.library lib : config.libraries) { | |
jarPaths.add(new File(dataDirectory.getAbsolutePath() + "/" + lib.name).toURI().toURL()); | |
} | |
URL[] urls = jarPaths.toArray(new URL[jarPaths.size()]); | |
for (URL url : urls) { | |
System.out.println("JAR: " + url); | |
} | |
URLClassLoader loader = new URLClassLoader(urls, Launcher.class.getClassLoader()); | |
Class clazz = loader.loadClass("org.minetweak.Minetweak"); | |
if (clazz==null) { | |
logger.severe("Unable to launch Minetweak. Could not find class org.minetweak.Minetweak!"); | |
System.exit(1); | |
} | |
Method method = clazz.getDeclaredMethod("main", String[].class); | |
if (method==null) { | |
logger.severe("Unable to launch Minetweak. Could not invoke method main(String[])!"); | |
System.exit(1); | |
} | |
try { | |
method.invoke(null, args); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment