Created
August 3, 2013 05:59
-
-
Save azenla/6145373 to your computer and use it in GitHub Desktop.
ModInstaller bug fix
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
#!/usr/bin/env groovy | |
import groovy.json.JsonSlurper | |
def mcDir = new File(System.getProperty('user.home') + '/.minecraft/') | |
def modsDir = new File(mcDir, 'mods') | |
def mods = [:] | |
def modsToDownload = args.toList() | |
def configFile = new File(mcDir, '/installer.cfg') | |
def ConfigObject config = null | |
def loadConfig = { -> | |
if (!configFile.exists()) {configFile.createNewFile()} | |
config = new ConfigSlurper().parse(configFile.newReader()) | |
} | |
def installForge = { -> | |
def installerJar = new File(mcDir.absolutePath + '/forgeInstaller.jar') | |
def installerURL = config.get('forgeInstaller', 'http://files.minecraftforge.net/minecraftforge/minecraftforge-installer-latest.jar') as String | |
download installerURL, installerJar.absolutePath | |
def classLoader = this.getClass().getClassLoader().rootLoader | |
classLoader.addURL(installerJar.toURI().toURL()) | |
println 'Please install Forge with the dialog that is displayed.' | |
sleep(3000) | |
def installerClass = Class.forName('cpw.mods.fml.installer.SimpleInstaller', true, classLoader) | |
installerClass.invokeMethod('main', new String[0]) | |
} | |
def loadModList = { -> | |
def modListFile = new File(mcDir.absolutePath + '/modlist.json') | |
def modsList = config.get('modlist', 'https://gist.github.com/kaendfinger/6144135/raw/mods.json') as String | |
download modsList, modListFile.absolutePath | |
mods = new JsonSlurper().parse(modListFile) as Map<String, String> | |
} | |
def runInstallScript = { String url -> | |
def scriptFile = new File(mcDir.absolutePath + '/installScript.groovy') | |
download url, scriptFile.absolutePath | |
def binding = new Binding() | |
binding.setVariable('mcDir', mcDir) | |
binding.setVariable('modsDir', modsDir) | |
binding.setVariable('mods', mods) | |
binding.setVariable('config', config) | |
def shell = new GroovyShell(this.class.classLoader, binding) | |
def script = shell.parse scriptFile | |
script.run() | |
scriptFile.delete() | |
} | |
def installMod = { String name -> | |
if (!mods.containsKey(name)) {println 'No Such Mod: ' + name ; System.exit(1)} | |
println 'Installing ' + name | |
def url = mods[name] as String | |
if (url.endsWith('.groovy')) { | |
runInstallScript(url) | |
return | |
} | |
def modFile = new File(modsDir, name + '.jar') | |
if (modFile.exists()) { | |
modFile.delete() | |
} | |
println 'Mod File: ' + modFile | |
download url, modFile.absolutePath | |
} | |
loadConfig() | |
loadModList() | |
args.eachWithIndex { String it, int index -> | |
if (it.equals('--list')) { | |
println 'Mods:' | |
mods.keySet().each { String name -> println ' ' + name} | |
System.exit(0) | |
} else if (it.equals('--install-forge')) { | |
println 'Installing Forge' | |
installForge() | |
modsToDownload.remove('--install-forge') | |
} else if (it.equals('--help')) { | |
println 'Usage: groovy ModInstaller.groovy [ARGS] [List of Mods]' | |
println 'Possible Arguments:' | |
println ' --list - Lists the Mods in the Mods List' | |
println ' --install-forge - Installs Forge' | |
System.exit(0) | |
} | |
} | |
if (!modsDir.exists()) {modsDir.mkdirs()} | |
println mods.size() + ' mods are on the mod list.' | |
if (modsToDownload.isEmpty()) {modsToDownload.addAll(mods.keySet())} | |
modsToDownload.each { String name -> | |
installMod name | |
} | |
static def download(String address, String path) { | |
def file = new FileOutputStream(path) | |
def out = new BufferedOutputStream(file) | |
out << new URL(address).openStream() | |
out.close() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment