First commit
This commit is contained in:
commit
58c64ab472
62 changed files with 29291 additions and 0 deletions
16
src/mrdev023/utils/BufferTools.java
Normal file
16
src/mrdev023/utils/BufferTools.java
Normal file
|
@ -0,0 +1,16 @@
|
|||
package mrdev023.utils;
|
||||
|
||||
import java.nio.*;
|
||||
|
||||
import org.lwjgl.*;
|
||||
|
||||
public class BufferTools {
|
||||
|
||||
public static FloatBuffer asFlippedFloatBuffer(float[] data){
|
||||
FloatBuffer buffer = BufferUtils.createFloatBuffer(data.length);
|
||||
buffer.put(data);
|
||||
buffer.flip();
|
||||
return buffer;
|
||||
}
|
||||
|
||||
}
|
28
src/mrdev023/utils/ClassCompiler.java
Normal file
28
src/mrdev023/utils/ClassCompiler.java
Normal file
|
@ -0,0 +1,28 @@
|
|||
package mrdev023.utils;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
import mrdev023.utils.compile.*;
|
||||
|
||||
public class ClassCompiler {
|
||||
|
||||
public static Class compile(String path,String className,String packageName){
|
||||
String f = "";
|
||||
try {
|
||||
f = IO.loadFile(path + className + ".java");
|
||||
} catch (IOException e1) {
|
||||
e1.printStackTrace();
|
||||
System.exit(-1);
|
||||
}
|
||||
f = "package " + packageName + ";\n\n" + f;
|
||||
System.out.println(f);
|
||||
Class cl = null;
|
||||
try {
|
||||
cl = InMemoryJavaCompiler.compile(packageName + "." + className, f);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return cl;
|
||||
}
|
||||
|
||||
}
|
24
src/mrdev023/utils/IO.java
Normal file
24
src/mrdev023/utils/IO.java
Normal file
|
@ -0,0 +1,24 @@
|
|||
package mrdev023.utils;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
public class IO {
|
||||
|
||||
public static String loadFile(String input) throws IOException {
|
||||
String r = "";
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(input)));
|
||||
String buffer = "";
|
||||
while ((buffer = reader.readLine()) != null) {
|
||||
r += buffer + "\n";
|
||||
}
|
||||
reader.close();
|
||||
return r;
|
||||
}
|
||||
|
||||
public static void writeFile(String path,String output) throws Exception{
|
||||
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(output)));
|
||||
writer.write(output);
|
||||
writer.close();
|
||||
}
|
||||
|
||||
}
|
39
src/mrdev023/utils/OBJLoader.java
Normal file
39
src/mrdev023/utils/OBJLoader.java
Normal file
|
@ -0,0 +1,39 @@
|
|||
package mrdev023.utils;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
|
||||
import mrdev023.math.*;
|
||||
|
||||
public class OBJLoader {
|
||||
|
||||
public static ArrayList<Vector3f> loadOBJ(String path){
|
||||
ArrayList<Vector3f> vecList = new ArrayList<Vector3f>();
|
||||
String file = "";
|
||||
try {
|
||||
file = IO.loadFile(path);
|
||||
} catch (IOException e1) {
|
||||
e1.printStackTrace();
|
||||
System.exit(-1);
|
||||
}
|
||||
BufferedReader reader = new BufferedReader(new StringReader(file));
|
||||
String buffer = "";
|
||||
try {
|
||||
while ((buffer = reader.readLine()) != null) {
|
||||
if(buffer.contains("v ")){
|
||||
String[] line = buffer.split(" ");
|
||||
if(line.length == 4){
|
||||
vecList.add(new Vector3f(Float.parseFloat(line[1]),Float.parseFloat(line[2]),Float.parseFloat(line[3])));
|
||||
}
|
||||
}
|
||||
}
|
||||
reader.close();
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
System.out.println(vecList.size());
|
||||
return vecList;
|
||||
}
|
||||
|
||||
}
|
69
src/mrdev023/utils/Pointer.java
Normal file
69
src/mrdev023/utils/Pointer.java
Normal file
|
@ -0,0 +1,69 @@
|
|||
package mrdev023.utils;
|
||||
|
||||
import java.lang.reflect.*;
|
||||
|
||||
import mrdev023.*;
|
||||
import sun.misc.*;
|
||||
|
||||
public class Pointer{
|
||||
|
||||
private static Unsafe unsafe = null;
|
||||
|
||||
static{
|
||||
try {
|
||||
Field field = sun.misc.Unsafe.class.getDeclaredField("theUnsafe");
|
||||
field.setAccessible(true);
|
||||
unsafe = (sun.misc.Unsafe) field.get(null);
|
||||
} catch (Exception e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
}
|
||||
|
||||
private long address;
|
||||
|
||||
public Pointer(){
|
||||
address = unsafe.allocateMemory(unsafe.addressSize());
|
||||
}
|
||||
|
||||
public Pointer(long address){
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
public Pointer(Object obj){
|
||||
this.address = unsafe.allocateMemory(unsafe.addressSize());
|
||||
setValue(obj);
|
||||
}
|
||||
|
||||
public void setValue(Object value){
|
||||
unsafe.putObject(0, address, value);
|
||||
}
|
||||
|
||||
public Object getValue(){
|
||||
return unsafe.getObject(0, address);
|
||||
}
|
||||
|
||||
public void delete(){
|
||||
unsafe.freeMemory(address);
|
||||
}
|
||||
|
||||
public static Unsafe getUnsafe() {
|
||||
return unsafe;
|
||||
}
|
||||
|
||||
public static void setUnsafe(Unsafe unsafe) {
|
||||
Pointer.unsafe = unsafe;
|
||||
}
|
||||
|
||||
public String toString(){
|
||||
return Long.toHexString(address).toUpperCase();
|
||||
}
|
||||
|
||||
public long getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(long address) {
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
}
|
53
src/mrdev023/utils/Timer.java
Normal file
53
src/mrdev023/utils/Timer.java
Normal file
|
@ -0,0 +1,53 @@
|
|||
package mrdev023.utils;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.Map.*;
|
||||
|
||||
public class Timer {
|
||||
|
||||
public static HashMap<String,Long> timers = new HashMap<String,Long>();
|
||||
|
||||
private static long current,previous,currentDelta,previousDelta,delta;
|
||||
|
||||
|
||||
public static void init(){
|
||||
current = System.nanoTime();
|
||||
previous = System.nanoTime();
|
||||
currentDelta = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
public static void udpate(){
|
||||
previous = current;
|
||||
current = System.nanoTime();
|
||||
for(Entry<String, Long> timer : timers.entrySet()){
|
||||
timer.setValue(timer.getValue() + (current - previous));
|
||||
}
|
||||
}
|
||||
|
||||
public static void deltaUpdate(){
|
||||
previousDelta = currentDelta;
|
||||
currentDelta = System.currentTimeMillis();
|
||||
delta = currentDelta - previousDelta;
|
||||
}
|
||||
|
||||
public static void addTimer(String name){
|
||||
timers.put(name, 0L);
|
||||
}
|
||||
|
||||
public static Long getNanoTime(String name){
|
||||
return timers.get(name);
|
||||
}
|
||||
|
||||
public static Long getMillisTime(String name){
|
||||
return timers.get(name)/1000000;
|
||||
}
|
||||
|
||||
public static void setValue(String name,Long value){
|
||||
timers.replace(name, value);
|
||||
}
|
||||
|
||||
public static int getDeltaTime(){
|
||||
return (int)delta;
|
||||
}
|
||||
|
||||
}
|
24
src/mrdev023/utils/compile/CompiledCode.java
Normal file
24
src/mrdev023/utils/compile/CompiledCode.java
Normal file
|
@ -0,0 +1,24 @@
|
|||
package mrdev023.utils.compile;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
|
||||
import javax.tools.*;
|
||||
import javax.tools.JavaFileObject.*;
|
||||
|
||||
public class CompiledCode extends SimpleJavaFileObject {
|
||||
private ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
|
||||
public CompiledCode(String className) throws Exception {
|
||||
super(new URI(className), Kind.CLASS);
|
||||
}
|
||||
|
||||
@Override
|
||||
public OutputStream openOutputStream() throws IOException {
|
||||
return baos;
|
||||
}
|
||||
|
||||
public byte[] getByteCode() {
|
||||
return baos.toByteArray();
|
||||
}
|
||||
}
|
28
src/mrdev023/utils/compile/DynamicClassLoader.java
Normal file
28
src/mrdev023/utils/compile/DynamicClassLoader.java
Normal file
|
@ -0,0 +1,28 @@
|
|||
package mrdev023.utils.compile;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import mrdev023.*;
|
||||
|
||||
public class DynamicClassLoader extends ClassLoader {
|
||||
|
||||
private Map<String, CompiledCode> customCompiledCode = new HashMap<>();
|
||||
|
||||
public DynamicClassLoader(ClassLoader parent) {
|
||||
super(parent);
|
||||
}
|
||||
|
||||
public void setCode(CompiledCode cc) {
|
||||
customCompiledCode.put(cc.getName(), cc);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Class<?> findClass(String name) throws ClassNotFoundException {
|
||||
CompiledCode cc = customCompiledCode.get(name);
|
||||
if (cc == null) {
|
||||
return super.findClass(name);
|
||||
}
|
||||
byte[] byteCode = cc.getByteCode();
|
||||
return defineClass(name, byteCode, 0, byteCode.length);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
package mrdev023.utils.compile;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
import javax.tools.*;
|
||||
|
||||
import mrdev023.*;
|
||||
|
||||
public class ExtendedStandardJavaFileManager extends ForwardingJavaFileManager<JavaFileManager> {
|
||||
|
||||
private CompiledCode compiledCode;
|
||||
private DynamicClassLoader cl;
|
||||
|
||||
/**
|
||||
* Creates a new instance of ForwardingJavaFileManager.
|
||||
*
|
||||
* @param fileManager delegate to this file manager
|
||||
* @param cl
|
||||
*/
|
||||
protected ExtendedStandardJavaFileManager(JavaFileManager fileManager, CompiledCode compiledCode, DynamicClassLoader cl) {
|
||||
super(fileManager);
|
||||
this.compiledCode = compiledCode;
|
||||
this.cl = cl;
|
||||
this.cl.setCode(compiledCode);
|
||||
}
|
||||
|
||||
@Override
|
||||
public JavaFileObject getJavaFileForOutput(JavaFileManager.Location location, String className, JavaFileObject.Kind kind, FileObject sibling) throws IOException {
|
||||
return compiledCode;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ClassLoader getClassLoader(JavaFileManager.Location location) {
|
||||
return cl;
|
||||
}
|
||||
}
|
27
src/mrdev023/utils/compile/InMemoryJavaCompiler.java
Normal file
27
src/mrdev023/utils/compile/InMemoryJavaCompiler.java
Normal file
|
@ -0,0 +1,27 @@
|
|||
package mrdev023.utils.compile;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import javax.tools.*;
|
||||
|
||||
import mrdev023.*;
|
||||
|
||||
public class InMemoryJavaCompiler {
|
||||
static JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
|
||||
|
||||
public static Class<?> compile(String className, String sourceCodeInText) throws Exception {
|
||||
// System.setProperty("java.home", "C:\\Program Files\\Java\\jdk1.8.0_51\\bin");
|
||||
SourceCode sourceCode = new SourceCode(className, sourceCodeInText);
|
||||
CompiledCode compiledCode = new CompiledCode(className);
|
||||
Iterable<? extends JavaFileObject> compilationUnits = Arrays.asList(sourceCode);
|
||||
DynamicClassLoader cl = new DynamicClassLoader(ClassLoader.getSystemClassLoader());
|
||||
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
|
||||
ExtendedStandardJavaFileManager fileManager = new ExtendedStandardJavaFileManager(javac.getStandardFileManager(diagnostics, null, null),compiledCode, cl);
|
||||
JavaCompiler.CompilationTask task = javac.getTask(null, fileManager, null, null, null, compilationUnits);
|
||||
boolean result = task.call();
|
||||
if(!result)System.out.println(diagnostics.getDiagnostics());
|
||||
if(cl != null)
|
||||
return cl.loadClass(className);
|
||||
else return null;
|
||||
}
|
||||
}
|
20
src/mrdev023/utils/compile/SourceCode.java
Normal file
20
src/mrdev023/utils/compile/SourceCode.java
Normal file
|
@ -0,0 +1,20 @@
|
|||
package mrdev023.utils.compile;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
|
||||
import javax.tools.*;
|
||||
import javax.tools.JavaFileObject.*;
|
||||
|
||||
public class SourceCode extends SimpleJavaFileObject {
|
||||
private String contents = null;
|
||||
|
||||
public SourceCode(String className, String contents) throws Exception {
|
||||
super(URI.create("string:///" + className.replace('.', '/') + Kind.SOURCE.extension), Kind.SOURCE);
|
||||
this.contents = contents;
|
||||
}
|
||||
|
||||
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
|
||||
return contents;
|
||||
}
|
||||
}
|
Reference in a new issue