-
-
Save thomas15v/1c0fd9919198f1eb7775 to your computer and use it in GitHub Desktop.
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 com.thedeviantnetwork.deviantfix.patcher; | |
import org.apache.commons.io.IOUtils; | |
import java.io.*; | |
import java.util.ArrayList; | |
import java.util.Enumeration; | |
import java.util.List; | |
import java.util.zip.ZipEntry; | |
import java.util.zip.ZipFile; | |
import java.util.zip.ZipOutputStream; | |
public class CoreModMaker { | |
private List<File> coremods = new ArrayList<File>(); | |
public void addMod(File mod){ | |
coremods.add(mod); | |
} | |
private boolean didPatch = false; | |
public void addMod(String file){ | |
if (file.contains("*")) { | |
file = file.substring(0, file.length() - 1).replace("mods/",""); | |
File folder = new File("mods/"); | |
for (File f : folder.listFiles()) { | |
if (f.getName().startsWith(file)) { | |
addMod(f); | |
return; | |
} | |
} | |
} | |
else { | |
addMod(new File("mods/" + file + ".jar")); | |
} | |
} | |
private void bake(File file){ | |
try { | |
System.out.println("Patching... " + file); | |
ZipFile zip = new ZipFile(file); | |
File zipTemp = new File("out.tmp"); | |
zipTemp.deleteOnExit(); | |
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipTemp)); | |
String toModify = "META-INF/MANIFEST.MF"; | |
Enumeration<? extends ZipEntry> entities = zip.entries(); | |
int needpatch = 2; | |
while (entities.hasMoreElements()) { | |
ZipEntry e = entities.nextElement(); | |
zos.putNextEntry(new ZipEntry(e.getName())); | |
if(!e.getName().equalsIgnoreCase(toModify)) { | |
byte[] bytes = IOUtils.toByteArray(zip.getInputStream(e)); | |
zos.write(bytes, 0, bytes.length); | |
} else { | |
BufferedReader reader = new BufferedReader(new InputStreamReader(zip.getInputStream(e))); | |
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(zos)); | |
while (reader.ready()) { | |
String line = reader.readLine(); | |
if (line.contains("FMLCorePlugin")) | |
needpatch--; | |
if (line.length() > 0) | |
writer.write(line + "\n"); | |
} | |
if (needpatch != 0) { | |
writer.write( "FMLCorePlugin: com.thedeviantnetwork.deviantfix.patcher.FakeCoremod\n" + | |
"FMLCorePluginContainsFMLMod: true\n"); | |
writer.flush(); | |
} | |
} | |
zos.closeEntry(); | |
} | |
zos.close(); | |
zip.close(); | |
if (needpatch != 0){ | |
file.delete(); | |
System.out.println(zipTemp.renameTo(file)); | |
System.out.println("Patched: " + file); | |
didPatch = true; | |
}else { | |
zipTemp.delete(); | |
System.out.println(file + " Already Patched!"); | |
} | |
} catch (IOException e1) { | |
e1.printStackTrace(); | |
} | |
} | |
public void bakeCoreMods(){ | |
for (File mod : coremods) | |
bake(mod); | |
} | |
public boolean didPatch() { | |
return didPatch; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment