Compiler Cleanup

This commit is contained in:
Konloch 2021-07-03 23:24:54 -07:00
parent 7b26b62084
commit 0c53463fd7
5 changed files with 46 additions and 36 deletions

View file

@ -18,6 +18,10 @@ package the.bytecode.club.bytecodeviewer.compilers;
* along with this program. If not, see <http://www.gnu.org/licenses/>. * * along with this program. If not, see <http://www.gnu.org/licenses/>. *
***************************************************************************/ ***************************************************************************/
import the.bytecode.club.bytecodeviewer.compilers.impl.JavaCompiler;
import the.bytecode.club.bytecodeviewer.compilers.impl.KrakatauAssembler;
import the.bytecode.club.bytecodeviewer.compilers.impl.SmaliAssembler;
/** /**
* A collection of all of the supported compilers/assemblers inside of BCV * A collection of all of the supported compilers/assemblers inside of BCV
* *

View file

@ -26,7 +26,5 @@ package the.bytecode.club.bytecodeviewer.compilers;
public abstract class InternalCompiler public abstract class InternalCompiler
{ {
public abstract byte[] compile(String contents, String name); public abstract byte[] compile(String contents, String name);
}
}

View file

@ -1,4 +1,4 @@
package the.bytecode.club.bytecodeviewer.compilers; package the.bytecode.club.bytecodeviewer.compilers.impl;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File; import java.io.File;
@ -8,6 +8,7 @@ import java.io.InputStreamReader;
import me.konloch.kontainer.io.DiskWriter; import me.konloch.kontainer.io.DiskWriter;
import the.bytecode.club.bytecodeviewer.BytecodeViewer; import the.bytecode.club.bytecodeviewer.BytecodeViewer;
import the.bytecode.club.bytecodeviewer.Configuration; import the.bytecode.club.bytecodeviewer.Configuration;
import the.bytecode.club.bytecodeviewer.compilers.InternalCompiler;
import the.bytecode.club.bytecodeviewer.util.JarUtils; import the.bytecode.club.bytecodeviewer.util.JarUtils;
import the.bytecode.club.bytecodeviewer.util.MiscUtils; import the.bytecode.club.bytecodeviewer.util.MiscUtils;
@ -39,7 +40,6 @@ import static the.bytecode.club.bytecodeviewer.Constants.*;
public class JavaCompiler extends InternalCompiler public class JavaCompiler extends InternalCompiler
{ {
@Override @Override
public byte[] compile(String contents, String name) public byte[] compile(String contents, String name)
{ {
@ -102,7 +102,8 @@ public class JavaCompiler extends InternalCompiler
} }
} }
if (process.isAlive()) { if (process.isAlive())
{
System.out.println("Force killing javac process, assuming it's gotten stuck"); System.out.println("Force killing javac process, assuming it's gotten stuck");
process.destroyForcibly().destroy(); process.destroyForcibly().destroy();
} }
@ -116,18 +117,20 @@ public class JavaCompiler extends InternalCompiler
InputStreamReader isr = new InputStreamReader(is); InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr); BufferedReader br = new BufferedReader(isr);
String line; String line;
while ((line = br.readLine()) != null) {
while ((line = br.readLine()) != null)
log.append(nl).append(line); log.append(nl).append(line);
}
br.close(); br.close();
log.append(nl).append(nl).append("Error:").append(nl).append(nl); log.append(nl).append(nl).append("Error:").append(nl).append(nl);
is = process.getErrorStream(); is = process.getErrorStream();
isr = new InputStreamReader(is); isr = new InputStreamReader(is);
br = new BufferedReader(isr); br = new BufferedReader(isr);
while ((line = br.readLine()) != null) {
while ((line = br.readLine()) != null)
log.append(nl).append(line); log.append(nl).append(line);
}
br.close(); br.close();
log.append(nl).append(nl).append("Exit Value is ").append(exitValue); log.append(nl).append(nl).append("Exit Value is ").append(exitValue);
@ -135,12 +138,12 @@ public class JavaCompiler extends InternalCompiler
if (!clazz.exists()) if (!clazz.exists())
throw new Exception(log.toString()); throw new Exception(log.toString());
} catch (Exception e) { } catch (Exception e) {
cont = false; cont = false;
e.printStackTrace(); e.printStackTrace();
} finally {
BytecodeViewer.sm.setBlocking();
} }
BytecodeViewer.sm.setBlocking();
cp.delete(); cp.delete();

View file

@ -1,13 +1,15 @@
package the.bytecode.club.bytecodeviewer.compilers; package the.bytecode.club.bytecodeviewer.compilers.impl;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import me.konloch.kontainer.io.DiskWriter; import me.konloch.kontainer.io.DiskWriter;
import org.apache.commons.io.FileUtils;
import the.bytecode.club.bytecodeviewer.BytecodeViewer; import the.bytecode.club.bytecodeviewer.BytecodeViewer;
import the.bytecode.club.bytecodeviewer.Configuration; import the.bytecode.club.bytecodeviewer.Configuration;
import the.bytecode.club.bytecodeviewer.Constants; import the.bytecode.club.bytecodeviewer.Constants;
import the.bytecode.club.bytecodeviewer.compilers.InternalCompiler;
import the.bytecode.club.bytecodeviewer.util.JarUtils; import the.bytecode.club.bytecodeviewer.util.JarUtils;
import the.bytecode.club.bytecodeviewer.util.MiscUtils; import the.bytecode.club.bytecodeviewer.util.MiscUtils;
@ -51,11 +53,9 @@ public class KrakatauAssembler extends InternalCompiler
return null; return null;
} }
String origName = name; String origName = MiscUtils.randomString(20);
name = MiscUtils.randomString(20);
File tempD = File tempD = new File(Constants.tempDirectory + fs + MiscUtils.randomString(32) + fs);
new File(Constants.tempDirectory + fs + MiscUtils.randomString(32) + fs);
tempD.mkdir(); tempD.mkdir();
File tempJ = new File(tempD.getAbsolutePath() + fs + name + ".j"); File tempJ = new File(tempD.getAbsolutePath() + fs + name + ".j");
@ -63,12 +63,15 @@ public class KrakatauAssembler extends InternalCompiler
final File tempDirectory = new File(Constants.tempDirectory + fs + MiscUtils.randomString(32) + fs); final File tempDirectory = new File(Constants.tempDirectory + fs + MiscUtils.randomString(32) + fs);
tempDirectory.mkdir(); tempDirectory.mkdir();
final File tempJar = new File(Constants.tempDirectory + fs + "temp" + MiscUtils.randomString(32) + ".jar"); final File tempJar = new File(Constants.tempDirectory + fs + "temp" + MiscUtils.randomString(32) + ".jar");
JarUtils.saveAsJar(BytecodeViewer.getLoadedClasses(), tempJar.getAbsolutePath()); JarUtils.saveAsJar(BytecodeViewer.getLoadedClasses(), tempJar.getAbsolutePath());
BytecodeViewer.sm.stopBlocking();
StringBuilder log = new StringBuilder(); StringBuilder log = new StringBuilder();
try {
BytecodeViewer.sm.stopBlocking();
try
{
ProcessBuilder pb = new ProcessBuilder( ProcessBuilder pb = new ProcessBuilder(
Configuration.python, Configuration.python,
"-O", //love you storyyeller <3 "-O", //love you storyyeller <3
@ -86,26 +89,27 @@ public class KrakatauAssembler extends InternalCompiler
InputStreamReader isr = new InputStreamReader(is); InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr); BufferedReader br = new BufferedReader(isr);
String line; String line;
while ((line = br.readLine()) != null) {
while ((line = br.readLine()) != null)
log.append(nl).append(line); log.append(nl).append(line);
}
br.close(); br.close();
log.append(nl).append(nl).append("Error:").append(nl).append(nl); log.append(nl).append(nl).append("Error:").append(nl).append(nl);
is = process.getErrorStream(); is = process.getErrorStream();
isr = new InputStreamReader(is); isr = new InputStreamReader(is);
br = new BufferedReader(isr); br = new BufferedReader(isr);
while ((line = br.readLine()) != null) {
while ((line = br.readLine()) != null)
log.append(nl).append(line); log.append(nl).append(line);
}
br.close(); br.close();
int exitValue = process.waitFor(); int exitValue = process.waitFor();
log.append(nl).append(nl).append("Exit Value is ").append(exitValue); log.append(nl).append(nl).append("Exit Value is ").append(exitValue);
System.out.println(log); System.out.println(log);
byte[] b = byte[] b = FileUtils.readFileToByteArray(new File(tempDirectory.getAbsolutePath() + fs + origName + ".class"));
org.apache.commons.io.FileUtils.readFileToByteArray(new File(tempDirectory.getAbsolutePath() + fs + origName + ".class"));
tempDirectory.delete(); tempDirectory.delete();
tempJar.delete(); tempJar.delete();
return b; return b;

View file

@ -1,9 +1,11 @@
package the.bytecode.club.bytecodeviewer.compilers; package the.bytecode.club.bytecodeviewer.compilers.impl;
import java.io.File; import java.io.File;
import java.util.Objects; import java.util.Objects;
import me.konloch.kontainer.io.DiskWriter; import me.konloch.kontainer.io.DiskWriter;
import org.apache.commons.io.FileUtils;
import the.bytecode.club.bytecodeviewer.BytecodeViewer; import the.bytecode.club.bytecodeviewer.BytecodeViewer;
import the.bytecode.club.bytecodeviewer.compilers.InternalCompiler;
import the.bytecode.club.bytecodeviewer.util.Dex2Jar; import the.bytecode.club.bytecodeviewer.util.Dex2Jar;
import the.bytecode.club.bytecodeviewer.util.Enjarify; import the.bytecode.club.bytecodeviewer.util.Enjarify;
import the.bytecode.club.bytecodeviewer.util.MiscUtils; import the.bytecode.club.bytecodeviewer.util.MiscUtils;
@ -37,9 +39,9 @@ import static the.bytecode.club.bytecodeviewer.Constants.*;
public class SmaliAssembler extends InternalCompiler public class SmaliAssembler extends InternalCompiler
{ {
@Override @Override
public byte[] compile(String contents, String name) { public byte[] compile(String contents, String name)
{
String fileStart = tempDirectory + fs + "temp"; String fileStart = tempDirectory + fs + "temp";
int fileNumber = MiscUtils.getClassNumber(fileStart, ".dex"); int fileNumber = MiscUtils.getClassNumber(fileStart, ".dex");
@ -79,21 +81,20 @@ public class SmaliAssembler extends InternalCompiler
boolean found = false; boolean found = false;
File current = tempJarFolder; File current = tempJarFolder;
try { try {
while (!found) { while (!found)
{
File f = Objects.requireNonNull(current.listFiles())[0]; File f = Objects.requireNonNull(current.listFiles())[0];
if (f.isDirectory()) if (f.isDirectory())
current = f; current = f;
else { else
{
outputClass = f; outputClass = f;
found = true; found = true;
} }
} }
return org.apache.commons.io.FileUtils.readFileToByteArray(outputClass); return FileUtils.readFileToByteArray(outputClass);
} catch (java.lang.NullPointerException ignored) { } catch (java.lang.NullPointerException ignored) { }
}
} catch (Exception e) { } catch (Exception e) {
new the.bytecode.club.bytecodeviewer.api.ExceptionUI(e); new the.bytecode.club.bytecodeviewer.api.ExceptionUI(e);
} }