Skip to content

Commit

Permalink
testing things
Browse files Browse the repository at this point in the history
Firestar99 committed Nov 2, 2017
1 parent a780160 commit 145ecb3
Showing 65 changed files with 7,910 additions and 0 deletions.
17 changes: 17 additions & 0 deletions SpaceEngine.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="jdk" jdkName="1.8" jdkType="JavaSDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module" module-name="spaceGameUtil" />
<orderEntry type="library" name="LWJGL3 Base" level="application" />
<orderEntry type="library" name="LWJGL3 GLFW" level="application" />
<orderEntry type="library" name="LWJGL3 ASSIMP" level="application" />
<orderEntry type="library" name="LWJGL3 OVR" level="application" />
<orderEntry type="library" name="LWJGL3 Vulkan" level="application" />
</component>
</module>
8 changes: 8 additions & 0 deletions src/space/engine/environment/EnvFieldType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package space.engine.environment;

public enum EnvFieldType {

GENERAL,
THLOCAL

}
26 changes: 26 additions & 0 deletions src/space/engine/environment/Environment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package space.engine.environment;

import space.util.keygen.IKey;
import space.util.keygen.IKeyGenerator;
import space.util.keygen.impl.IDKey.IDKey.IDKeyGenerator;
import space.util.keygen.map.IKeyMapGeneralGeneric;
import space.util.keygen.map.KeyMapGeneralGeneric;
import space.util.keygen.map.KeyMapKeyGeneric;

public class Environment {

public static IKeyGenerator gen;
public static ThreadLocal<KeyMapKeyGeneric<?>> env;
public static IKeyMapGeneralGeneric<EnvFieldType> type;

static {
type = new KeyMapGeneralGeneric<>();
gen = new IDKeyGenerator();
}

public static <VALUE> IKey<VALUE> makeKey(EnvFieldType ftype) {
IKey<VALUE> key = gen.generate();
type.put(key, ftype);
return key;
}
}
28 changes: 28 additions & 0 deletions src/space/engine/environmentOld/Environment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package space.engine.environmentOld;

import space.engine.manager.ManagerEnvironment;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

public class Environment implements IdBasedObject {

//id
public static final AtomicInteger IDGEN = new AtomicInteger();
public int id = IDGEN.getAndIncrement();
//general
public ManagerEnvironment manager;
public List<Thread> threads = new ArrayList<>();
//events
public EventChainRunnable onStart = new EventChainRunnable();
public EventChainRunnable onDeath = new EventChainRunnable();
public EventChainConsumer<Thread> onThreadCreation = new EventChainConsumer<>();
public EventChainConsumer<Thread> onThreadStart = new EventChainConsumer<>();
public EventChainConsumer<Thread> onThreadDeath = new EventChainConsumer<>();

@Override
public int getId() {
return id;
}
}
5 changes: 5 additions & 0 deletions src/space/engine/environmentOld/ThLocal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package space.engine.environmentOld;

public class ThLocal {

}
42 changes: 42 additions & 0 deletions src/space/engine/environmentOld/TheMain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package space.engine.environmentOld;

public class TheMain {

public static ThreadLocal<ThreadEnvironment> theMain = ThreadLocal.withInitial(ThreadEnvironment::new);
public static EnvironmentCollection collection = new EnvironmentCollection();

public static ThreadEnvironment getMain() {
return theMain.get();
}

public static void setEnvironment(Environment env) {
getMain().env = env;
}

public static void setThLocal(ThLocal th) {
getMain().th = th;
}

public static class ThreadEnvironment {

public Environment env;
public ThLocal th;
}

public static class EnvironmentCollection {

public IntKeyMapIdBased<Environment> list = new IntKeyMapIdBased<>();

public void add(Environment v) {
list.put(v);
}

public Environment get(int id) {
return list.get(id);
}

public void remove(int id) {
list.remove(id);
}
}
}
13 changes: 13 additions & 0 deletions src/space/engine/manager/ManagerEnvironment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package space.engine.manager;

import space.engine.environmentOld.Environment;
import space.engine.manager.classcollection.ClassCollection;

public class ManagerEnvironment extends Environment {

public ClassCollection classCollection;

public ManagerEnvironment() {

}
}
179 changes: 179 additions & 0 deletions src/space/engine/manager/classcollection/ClassCollection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
package space.engine.manager.classcollection;

import space.engine.event.searcher.Requires;

import java.io.File;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Modifier;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.jar.JarFile;

public class ClassCollection {

public static final ClassModifier ModifierPublic = new ClassModifier(Modifier.PUBLIC, Modifier.PRIVATE | Modifier.PROTECTED);
public static final ClassModifier ModifierProtected = new ClassModifier(Modifier.PROTECTED, Modifier.PUBLIC | Modifier.PRIVATE);
public static final ClassModifier ModifierPackagePrivate = new ClassModifier(0, Modifier.PUBLIC | Modifier.PRIVATE | Modifier.PROTECTED);
public static final ClassModifier ModifierPrivate = new ClassModifier(Modifier.PRIVATE, Modifier.PUBLIC | Modifier.PROTECTED);
public static final ClassModifier ModifierAbstract = new ClassModifier(Modifier.ABSTRACT, 0);
public static final ClassModifier ModifierNotAbstract = new ClassModifier(0, Modifier.ABSTRACT);
public static final ClassModifier ModifierFinal = new ClassModifier(Modifier.FINAL, 0);
public static final ClassModifier ModifierNotFinal = new ClassModifier(0, Modifier.FINAL);
public static final ClassModifier ModifierStrict = new ClassModifier(Modifier.STRICT, 0);
public static final ClassModifier ModifierNotStrict = new ClassModifier(0, Modifier.STRICT);

public static final IClassCollectionCondition<?> standardAll = new ClassCollectionCondition<>().setModifier(ModifierPublic);
public static final IClassCollectionCondition<?> standardNoAbstract = new ClassCollectionCondition<>().setModifier(ModifierPublic, ModifierNotAbstract);
public static final IClassCollectionCondition<Runnable> standardRunnable = new ClassCollectionCondition<>().setAssignable(Runnable.class).setModifier(ModifierPublic, ModifierNotAbstract);
public static final IClassCollectionCondition<Runnable> standardRunnableRequires = new ClassCollectionCondition<>(standardRunnable).setAnnotations(Requires.class);

private static final String fileEndingClass = ".class";
private static final int fileEndingClassLength = fileEndingClass.length();

public List<Class<?>> all;
public Map<IClassCollectionCondition, List<Class<?>>> map = new HashMap<>();
public boolean preBuildStandard = true;

public ClassCollection() {
clearAll();
}

//find all classes
public static List<Class<?>> getAll() throws IOException {
return getAll(new ArrayList<>(), Thread.currentThread().getContextClassLoader());
}

public static List<Class<?>> getAll(List<Class<?>> list) throws IOException {
return getAll(list, Thread.currentThread().getContextClassLoader());
}

public static List<Class<?>> getAll(List<Class<?>> list, ClassLoader classLoader) throws IOException {
for (Enumeration<URL> enumeration = classLoader.getResources(""); enumeration.hasMoreElements(); ) {
URL url = enumeration.nextElement();
File file = new File(url.getFile());

if (file.isDirectory())
addDirectory(list, file, classLoader, true);
if (file.getName().endsWith(".jar"))
addJar(list, new JarFile(file), classLoader);
}
return list;
}

//find directory
@SuppressWarnings("SameParameterValue")
public static void addDirectory(List<Class<?>> list, File file, ClassLoader classLoader, boolean reclusive) {
addDirectory(list, null, file, classLoader, reclusive);
}

@SuppressWarnings({"ConstantConditions", "SameParameterValue"})
private static void addDirectory(List<Class<?>> list, String base, File file, ClassLoader classLoader, boolean reclusive) {
File[] files = file.listFiles();
if (files == null)
return;

for (File f : files) {
String name = (base == null) ? f.getName() : base + '.' + f.getName();
if (reclusive && f.isDirectory())
addDirectory(list, name, f, classLoader, reclusive);

if (f.isFile() && name.endsWith(fileEndingClass)) {
addFile(list, name.substring(0, name.length() - fileEndingClassLength), classLoader);
}
}
}

//find jar
public static void addJar(List<Class<?>> list, JarFile file, ClassLoader classLoader) {
file.stream().forEach(jarEntry -> {
String f = jarEntry.getName();
if (f.endsWith(fileEndingClass))
addFile(list, f, classLoader);
});
}

public static void addFile(List<Class<?>> list, String file, ClassLoader classLoader) {
try {
Class<?> clazz = classLoader.loadClass(file);
if (!clazz.isAnnotationPresent(Ignore.class))
list.add(clazz);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}

//get
@SuppressWarnings("unchecked")
public synchronized <CLS> List<Class<? extends CLS>> get(IClassCollectionCondition<CLS> condition) {
List<Class<? extends CLS>> l = (List<Class<? extends CLS>>) ((Object) map.get(condition));
if (l != null)
return l;
return make(condition);
}

@SuppressWarnings("unchecked")
public synchronized <CLS> List<Class<? extends CLS>> make(IClassCollectionCondition<CLS> condition) {
List<Class<? extends CLS>> l = new ArrayList<>();
for (Class<?> c : getBestSource(condition))
if (condition.test(c))
l.add((Class<? extends CLS>) c);

map.put(condition, (List<Class<?>>) ((Object) l));
return l;
}

@SuppressWarnings("unchecked")
public synchronized <CLS> List<Class<? extends CLS>> getBestSource(IClassCollectionCondition<CLS> condition) {
return (List<Class<? extends CLS>>) ((Object) getBestSource0(condition));
}

private List<Class<?>> getBestSource0(IClassCollectionCondition<?> condition) {
ClassCollectionCondition<?> ccc = new ClassCollectionCondition<>(condition);
List<Class<?>> l;

//remove Annotation
l = map.get(ccc.setAnnotations((Class<? extends Annotation>[]) null));
if (l != null)
return l;

//remove assignable
l = map.get(ccc.setAssignable((Class[]) null));
if (l != null)
return l;

//remove modifier -> all
return all;
}

public void clearAll() {
List<Class<?>> l;
try {
l = getAll();
} catch (IOException e) {
throw new RuntimeException("IOException while searching classes!", e);
}
if (l == null)
throw new RuntimeException();

synchronized (this) {
all = l;
map.clear();

if (preBuildStandard) {
make(standardAll);
make(standardNoAbstract);
make(standardRunnable);
make(standardRunnableRequires);
}
}
}

public synchronized void clearCache() {
map.clear();
}
}
Loading

0 comments on commit 145ecb3

Please sign in to comment.