/*     */ package org.run;
/*     */ 
/*     */ import java.io.File;
/*     */ import java.io.IOException;
/*     */ import java.lang.reflect.InvocationTargetException;
/*     */ import java.lang.reflect.Method;
/*     */ import java.net.MalformedURLException;
/*     */ import java.net.URL;
/*     */ import java.net.URLClassLoader;
/*     */ import java.nio.file.Files;
/*     */ import java.nio.file.Path;
/*     */ import java.nio.file.Paths;
/*     */ import java.nio.file.attribute.FileAttribute;
/*     */ import java.util.Arrays;
/*     */ import javax.tools.Diagnostic;
/*     */ import javax.tools.DiagnosticCollector;
/*     */ import javax.tools.JavaCompiler;
/*     */ import javax.tools.JavaFileObject;
/*     */ import javax.tools.StandardJavaFileManager;
/*     */ import javax.tools.ToolProvider;
/*     */ import org.logic.GyteController;
/*     */ import org.logic.GyteProcessor;
/*     */ import org.logic.Program;
/*     */ import org.ui.debug.Console;
/*     */ import org.ui.debug.Entry;
/*     */ import org.ui.editors.code.CodeEditor;
/*     */ import org.world.World;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class GyteCompiler
/*     */ {
/*     */   private static final String CLASS_NAME = "GyteProgram";
/*     */   private static final String METHOD_NAME = "run";
/*     */   private static final boolean SUCCESS = true;
/*     */   private volatile GyteProcessor processor;
/*     */   private World world;
/*     */   private CodeEditor editor;
/*     */   private Console console;
/*     */   private File[] file;
/*     */   private JavaCompiler compiler;
/*     */   private StandardJavaFileManager fileManager;
/*     */   private Iterable<? extends JavaFileObject> compilationUnits;
/*     */   private DiagnosticCollector<JavaFileObject> diagnostics;
/*     */   private boolean isCompilerInstalled;
/*     */   
/*     */   public GyteCompiler(World world, Console console, CodeEditor editor) {
/*  73 */     this.world = world;
/*  74 */     this.console = console;
/*  75 */     this.editor = editor;
/*     */ 
/*     */     
/*  78 */     this.compiler = ToolProvider.getSystemJavaCompiler();
/*     */     
/*  80 */     this.isCompilerInstalled = (this.compiler != null);
/*     */     
/*  82 */     if (this.isCompilerInstalled) {
/*     */       
/*  84 */       this.fileManager = this.compiler.getStandardFileManager(null, null, null);
/*     */     
/*     */     }
/*     */     else {
/*     */       
/*  89 */       console.createLog("No Java Compiler could be found on this system\n", Entry.WARNING);
/*  90 */       console.createLog("See README.MD for futher instructions/help\n", Entry.WARNING);
/*     */     } 
/*     */ 
/*     */     
/*  94 */     System.setOut(console.getStdOut());
/*  95 */     System.setErr(console.getStdErr());
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public final void compileAndRunProgram(String source) {
/* 104 */     this.console.createLog("Compiling source...\n", Entry.INFORMATION);
/*     */ 
/*     */     
/* 107 */     Path temp = Paths.get(System.getProperty("java.io.tmpdir"), new String[] { "GyteProgram" });
/*     */ 
/*     */     
/*     */     try {
/* 111 */       Files.createDirectories(temp, (FileAttribute<?>[])new FileAttribute[0]);
/*     */ 
/*     */       
/* 114 */       Path javaSourceFile = Paths.get(temp.normalize().toAbsolutePath().toString(), new String[] { "GyteProgram.java" });
/*     */ 
/*     */       
/* 117 */       Files.write(javaSourceFile, source.getBytes(), new java.nio.file.OpenOption[0]);
/*     */ 
/*     */       
/* 120 */       compile(javaSourceFile, temp);
/*     */       
/* 122 */       Path classFile = Paths.get(temp.normalize().toAbsolutePath().toString(), new String[] { "GyteProgram.class" });
/*     */ 
/*     */       
/* 125 */       if (Files.exists(classFile, new java.nio.file.LinkOption[0])) {
/* 126 */         Files.delete(classFile);
/*     */       }
/*     */       
/* 129 */       Files.delete(javaSourceFile);
/*     */       
/* 131 */       Files.delete(temp);
/*     */     }
/* 133 */     catch (IOException e1) {
/* 134 */       this.console.createLog("Unable to find or edit \"java.io.tmpdir\": " + temp.toString() + "\n", Entry.ERROR);
/*     */     } 
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   private void compile(Path javaSourceFile, Path temp) {
/* 145 */     this.file = new File[] { javaSourceFile.toFile() };
/* 146 */     this.compilationUnits = this.fileManager.getJavaFileObjectsFromFiles(Arrays.asList(this.file));
/* 147 */     this.diagnostics = new DiagnosticCollector<>();
/*     */ 
/*     */     
/* 150 */     JavaCompiler.CompilationTask task = this.compiler.getTask(null, this.fileManager, this.diagnostics, null, null, this.compilationUnits);
/*     */ 
/*     */     
/* 153 */     boolean compilationStatus = task.call().booleanValue();
/*     */ 
/*     */     
/* 156 */     for (Diagnostic<?> diagnostic : this.diagnostics.getDiagnostics()) {
/*     */       
/* 158 */       this.console.createLog(String.format("Error in line: %d\nat: %s\n%s\n\n", new Object[] { Long.valueOf(diagnostic.getLineNumber()), diagnostic.getSource(), diagnostic.getMessage(null) }), Entry.ERROR);
/*     */       
/* 160 */       this.editor.markLine((int)diagnostic.getLineNumber());
/*     */     } 
/*     */ 
/*     */     
/* 164 */     if (compilationStatus) {
/* 165 */       runProgram(temp);
/*     */     }
/*     */     
/*     */     try {
/* 169 */       this.fileManager.close();
/* 170 */     } catch (IOException e) {
/* 171 */       this.console.createLog("The compiler's file resources could not be deleted\n", Entry.ERROR);
/*     */     } 
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   private void runProgram(Path temp) {
/*     */     try {
/* 184 */       ClassLoader classLoader = getClass().getClassLoader();
/* 185 */       URLClassLoader urlClassLoader = new URLClassLoader(new URL[] { temp.toUri().toURL() }, classLoader);
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */       
/*     */       try {
/* 192 */         Class<?> javaDemoClass = urlClassLoader.loadClass("GyteProgram");
/*     */         
/*     */         try {
/* 195 */           Object instance = javaDemoClass.getConstructor(new Class[0]).newInstance(new Object[0]);
/* 196 */           Method method = instance.getClass().getMethod("run", new Class[] { GyteController.class });
/*     */           
/* 198 */           if (this.processor != null && this.processor.isAlive()) {
/*     */ 
/*     */             
/* 201 */             this.processor.halt();
/* 202 */             this.console.createLog("A processor was still running, an was thus shutdown\n", Entry.WARNING);
/*     */           } 
/*     */ 
/*     */           
/* 206 */           this.processor = new GyteProcessor(new GyteController(this.world), this.editor);
/*     */           
/* 208 */           this.processor.dispatchProgram(new Program(instance, method));
/*     */           
/* 210 */           this.console.createLog("Dispatching program...\n", Entry.INFORMATION);
/*     */         }
/* 212 */         catch (InstantiationException e) {
/* 213 */           e.printStackTrace();
/* 214 */         } catch (IllegalAccessException e) {
/* 215 */           e.printStackTrace();
/* 216 */         } catch (IllegalArgumentException e) {
/* 217 */           e.printStackTrace();
/* 218 */         } catch (InvocationTargetException e) {
/* 219 */           e.printStackTrace();
/*     */         } 
/*     */         
/* 222 */         urlClassLoader.close();
/*     */       }
/* 224 */       catch (ClassNotFoundException e1) {
/* 225 */         this.console.createLog("The required Class \"GyteProgram\" was not defined or could not be compiled.\n", Entry.ERROR);
/* 226 */       } catch (NoSuchMethodException e1) {
/* 227 */         this.console.createLog("The required Method \"run\"\nin type \"GyteProgram\" was not defined.\n", Entry.ERROR);
/* 228 */       } catch (SecurityException e1) {
/* 229 */         this.console.createLog("Security violation detected.", Entry.ERROR);
/* 230 */       } catch (IOException e) {
/* 231 */         this.console.createLog("Unable to close class loader", Entry.ERROR);
/*     */       } 
/* 233 */     } catch (MalformedURLException e) {
/* 234 */       this.console.createLog("The class file(s) URL was invalid!\n", Entry.ERROR);
/*     */     } 
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public final GyteProcessor getProcessor() {
/* 244 */     return this.processor;
/*     */   }
/*     */ }


/* Location:              /home/teridax/Projects/Gytebot/GYTEBOT.jar!/org/run/GyteCompiler.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */