mirror of
https://github.com/AngelAuraMC/Amethyst-Android.git
synced 2025-09-12 14:16:58 -04:00
Use Apache Commons IOUtils to read byte array; cleanup unused parts
This commit is contained in:
parent
e71fc849e3
commit
a54e605e73
@ -15,11 +15,11 @@ import java.nio.charset.*;
|
|||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.zip.*;
|
import java.util.zip.*;
|
||||||
import javax.microedition.khronos.egl.*;
|
import javax.microedition.khronos.egl.*;
|
||||||
import net.kdt.pojavlaunch.patcher.*;
|
|
||||||
import net.kdt.pojavlaunch.util.*;
|
import net.kdt.pojavlaunch.util.*;
|
||||||
import net.kdt.pojavlaunch.value.*;
|
import net.kdt.pojavlaunch.value.*;
|
||||||
import net.kdt.pojavlaunch.prefs.*;
|
import net.kdt.pojavlaunch.prefs.*;
|
||||||
import java.nio.*;
|
import java.nio.*;
|
||||||
|
import org.apache.commons.compress.utils.*;
|
||||||
|
|
||||||
public final class Tools
|
public final class Tools
|
||||||
{
|
{
|
||||||
@ -586,13 +586,6 @@ public final class Tools
|
|||||||
return libDir.toArray(new String[0]);
|
return libDir.toArray(new String[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String[] patchOptifineInstaller(Activity ctx, File inFile) throws Exception {
|
|
||||||
File optifineDirFile = new File(optifineDir);
|
|
||||||
optifineDirFile.mkdir();
|
|
||||||
|
|
||||||
return new OptiFinePatcher(inFile).saveInstaller(optifineDirFile);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static JMinecraftVersionList.Version getVersionInfo(String versionName) {
|
public static JMinecraftVersionList.Version getVersionInfo(String versionName) {
|
||||||
try {
|
try {
|
||||||
JMinecraftVersionList.Version customVer = new Gson().fromJson(read(versnDir + "/" + versionName + "/" + versionName + ".json"), JMinecraftVersionList.Version.class);
|
JMinecraftVersionList.Version customVer = new Gson().fromJson(read(versnDir + "/" + versionName + "/" + versionName + ".json"), JMinecraftVersionList.Version.class);
|
||||||
@ -701,19 +694,7 @@ public final class Tools
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static byte[] getByteArray(InputStream stream) throws IOException {
|
public static byte[] getByteArray(InputStream stream) throws IOException {
|
||||||
ByteBuffer byteBuff = ByteBuffer.allocateDirect(stream.available()).order(ByteOrder.nativeOrder());
|
return IOUtils.toByteArray(stream);
|
||||||
byteBuff.position(0);
|
|
||||||
|
|
||||||
BufferedInputStream buf = new BufferedInputStream(stream);
|
|
||||||
// should be 1kb?
|
|
||||||
byte[] buffer = new byte[512];
|
|
||||||
int read;
|
|
||||||
while((read = buf.read(buffer)) != -1){
|
|
||||||
byteBuff.put(buffer, 0, read);
|
|
||||||
}
|
|
||||||
buf.close();
|
|
||||||
|
|
||||||
return byteBuff.array();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String read(InputStream is) throws Exception {
|
public static String read(InputStream is) throws Exception {
|
||||||
|
@ -1,121 +0,0 @@
|
|||||||
package net.kdt.pojavlaunch.patcher;
|
|
||||||
|
|
||||||
import android.util.*;
|
|
||||||
import java.io.*;
|
|
||||||
import java.math.*;
|
|
||||||
import java.security.*;
|
|
||||||
import java.util.*;
|
|
||||||
import java.util.jar.*;
|
|
||||||
import net.kdt.pojavlaunch.*;
|
|
||||||
|
|
||||||
public class OptiFinePatcher
|
|
||||||
{
|
|
||||||
private File input;
|
|
||||||
private JarFile inputFile;
|
|
||||||
private Enumeration<? extends JarEntry> inputEntries;
|
|
||||||
|
|
||||||
public OptiFinePatcher(File input) throws IOException {
|
|
||||||
this.input = input;
|
|
||||||
inputFile = new JarFile(input);
|
|
||||||
inputEntries = inputFile.entries();
|
|
||||||
}
|
|
||||||
|
|
||||||
public String[] saveInstaller(File patchDir) throws Exception {
|
|
||||||
String md5File = calculateMD5(input);
|
|
||||||
File optifineJar = new File(patchDir, md5File + "_OptiFine_patched.jar");
|
|
||||||
BufferedOutputStream optifineBuf = new BufferedOutputStream(new FileOutputStream(optifineJar));
|
|
||||||
JarOutputStream optifineJarStream = new JarOutputStream(optifineBuf);
|
|
||||||
|
|
||||||
while (inputEntries.hasMoreElements()) {
|
|
||||||
JarEntry inEntry = inputEntries.nextElement();
|
|
||||||
if (!inEntry.getName().equals("optifine/OptiFineClassTransformer.class")) {
|
|
||||||
optifineJarStream.putNextEntry(inEntry);
|
|
||||||
optifineJarStream.write(Tools.getByteArray(inputFile.getInputStream(inEntry)));
|
|
||||||
optifineJarStream.closeEntry();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (File patchClass : new File(patchDir, "optifine_patch").listFiles()) {
|
|
||||||
if (patchClass.isFile()) {
|
|
||||||
byte[] bArr = Tools.getByteArray(patchClass.getAbsolutePath());
|
|
||||||
String patchName = patchClass.getName();
|
|
||||||
JarEntry entry = new JarEntry("optifine/" + patchName.replace(".class.patch", ".class"));
|
|
||||||
entry.setSize(bArr.length);
|
|
||||||
optifineJarStream.putNextEntry(entry);
|
|
||||||
optifineJarStream.write(bArr);
|
|
||||||
optifineJarStream.closeEntry();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
optifineJarStream.finish();
|
|
||||||
optifineJarStream.flush();
|
|
||||||
optifineBuf.flush();
|
|
||||||
optifineBuf.close();
|
|
||||||
|
|
||||||
return new String[]{md5File, optifineJar.getAbsolutePath()};
|
|
||||||
}
|
|
||||||
|
|
||||||
public void saveTweaker() throws Exception {
|
|
||||||
File patchedFile = new File(input.getAbsolutePath().replace(".jar", "_patched.jar"));
|
|
||||||
BufferedOutputStream optifineBuf = new BufferedOutputStream(new FileOutputStream(patchedFile));
|
|
||||||
JarOutputStream optifineJarStream = new JarOutputStream(optifineBuf);
|
|
||||||
|
|
||||||
while (inputEntries.hasMoreElements()) {
|
|
||||||
JarEntry inEntry = inputEntries.nextElement();
|
|
||||||
if (!inEntry.getName().startsWith("classes") && !inEntry.getName().endsWith(".dex")) {
|
|
||||||
optifineJarStream.putNextEntry(inEntry);
|
|
||||||
optifineJarStream.write(Tools.getByteArray(inputFile.getInputStream(inEntry)));
|
|
||||||
optifineJarStream.closeEntry();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
optifineJarStream.finish();
|
|
||||||
optifineJarStream.flush();
|
|
||||||
optifineBuf.flush();
|
|
||||||
optifineBuf.close();
|
|
||||||
|
|
||||||
input.delete();
|
|
||||||
patchedFile.renameTo(input);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String calculateMD5(File updateFile) {
|
|
||||||
MessageDigest digest;
|
|
||||||
String TAG = "MD5Check";
|
|
||||||
try {
|
|
||||||
digest = MessageDigest.getInstance("MD5");
|
|
||||||
} catch (NoSuchAlgorithmException e) {
|
|
||||||
Log.e(TAG, "Exception while getting digest", e);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
InputStream is;
|
|
||||||
try {
|
|
||||||
is = new FileInputStream(updateFile);
|
|
||||||
} catch (FileNotFoundException e) {
|
|
||||||
Log.e(TAG, "Exception while getting FileInputStream", e);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
byte[] buffer = new byte[8192];
|
|
||||||
int read;
|
|
||||||
try {
|
|
||||||
while ((read = is.read(buffer)) > 0) {
|
|
||||||
digest.update(buffer, 0, read);
|
|
||||||
}
|
|
||||||
byte[] md5sum = digest.digest();
|
|
||||||
BigInteger bigInt = new BigInteger(1, md5sum);
|
|
||||||
String output = bigInt.toString(16);
|
|
||||||
// Fill to 32 chars
|
|
||||||
output = String.format("%32s", output).replace(' ', '0');
|
|
||||||
return output;
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new RuntimeException("Unable to process file for MD5", e);
|
|
||||||
} finally {
|
|
||||||
try {
|
|
||||||
is.close();
|
|
||||||
} catch (IOException e) {
|
|
||||||
Log.e(TAG, "Exception on closing MD5 input stream", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,107 +0,0 @@
|
|||||||
package net.kdt.pojavlaunch.signer;
|
|
||||||
|
|
||||||
import android.util.*;
|
|
||||||
import java.io.*;
|
|
||||||
import java.util.*;
|
|
||||||
import java.util.jar.*;
|
|
||||||
import java.util.zip.*;
|
|
||||||
import net.kdt.pojavlaunch.*;
|
|
||||||
//import org.apache.commons.codec.digest.*;
|
|
||||||
import net.kdt.pojavlaunch.util.*;
|
|
||||||
|
|
||||||
public class JarSigner
|
|
||||||
{
|
|
||||||
private static final String DEX_IN_JAR_NAME = "classes.dex";
|
|
||||||
private static final String MANIFEST_PATH = "META-INF/MANIFEST.MF";
|
|
||||||
|
|
||||||
private TreeMap<String, byte[]> outputResources = new TreeMap<String, byte[]>();
|
|
||||||
|
|
||||||
public static void sign(String inputJar, String outputJar) throws Exception
|
|
||||||
{
|
|
||||||
new JarSigner(inputJar, outputJar);
|
|
||||||
}
|
|
||||||
private JarSigner(String inputJar, String outputJar) throws Exception
|
|
||||||
{
|
|
||||||
ZipFile jarFile = new ZipFile(inputJar);
|
|
||||||
Enumeration<? extends ZipEntry> entries = jarFile.entries();
|
|
||||||
|
|
||||||
while (entries.hasMoreElements()) {
|
|
||||||
ZipEntry entry = entries.nextElement();
|
|
||||||
outputResources.put(entry.getName(), Tools.getByteArray(jarFile.getInputStream(entry)));
|
|
||||||
}
|
|
||||||
createJar(outputJar);
|
|
||||||
}
|
|
||||||
private byte[] makeManifest() {
|
|
||||||
StringBuilder baos = new StringBuilder();
|
|
||||||
|
|
||||||
// First, put some general information:
|
|
||||||
baos.append("Manifest-Version: 1.0\n");
|
|
||||||
baos.append("Created-By: " + Tools.usingVerName + " (" + Tools.APP_NAME + ": JarSigner)\n");
|
|
||||||
baos.append("Build-Jdk: 1.6.0_29");
|
|
||||||
baos.append("Dex-Location: " + DEX_IN_JAR_NAME + "\n");
|
|
||||||
|
|
||||||
return baos.toString().getBytes();
|
|
||||||
}
|
|
||||||
private boolean createJar(String fileName) {
|
|
||||||
/*
|
|
||||||
* Make or modify the manifest (as appropriate), put the dex
|
|
||||||
* array into the resources map, and then process the entire
|
|
||||||
* resources map in a uniform manner.
|
|
||||||
*/
|
|
||||||
|
|
||||||
try {
|
|
||||||
byte[] manifest = makeManifest();
|
|
||||||
OutputStream out = new FileOutputStream(fileName);
|
|
||||||
JarOutputStream jarOut = new JarOutputStream(out);
|
|
||||||
|
|
||||||
try {
|
|
||||||
for (Map.Entry<String, byte[]> e : outputResources.entrySet()) {
|
|
||||||
String name = e.getKey();
|
|
||||||
byte[] contents = e.getValue();
|
|
||||||
JarEntry entry = new JarEntry(name);
|
|
||||||
int length = contents.length;
|
|
||||||
entry.setSize(length);
|
|
||||||
/*
|
|
||||||
if (args.verbose) {
|
|
||||||
context.out.println("writing " + name + "; size " + length + "...");
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
if (name.endsWith(".SF") ||
|
|
||||||
name.endsWith(".RSA")) {
|
|
||||||
// Remove these files.
|
|
||||||
continue;
|
|
||||||
} else if (name.endsWith(MANIFEST_PATH)) {
|
|
||||||
length = manifest.length;
|
|
||||||
jarOut.putNextEntry(entry);
|
|
||||||
jarOut.write(manifest);
|
|
||||||
jarOut.closeEntry();
|
|
||||||
} else {
|
|
||||||
jarOut.putNextEntry(entry);
|
|
||||||
jarOut.write(contents);
|
|
||||||
jarOut.closeEntry();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
jarOut.finish();
|
|
||||||
jarOut.flush();
|
|
||||||
closeOutput(out);
|
|
||||||
}
|
|
||||||
} catch (Exception ex) {
|
|
||||||
throw new RuntimeException("Trouble writing output:", ex);
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
private void closeOutput(OutputStream stream) throws IOException {
|
|
||||||
if (stream == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
stream.flush();
|
|
||||||
|
|
||||||
if (stream != System.out) {
|
|
||||||
stream.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user