2022-10-24 14:34:30 +02:00
|
|
|
package love.distributedrebirth.bassboon;
|
2022-10-20 03:06:56 +02:00
|
|
|
|
|
|
|
|
import java.lang.reflect.Field;
|
|
|
|
|
import java.lang.reflect.InvocationTargetException;
|
|
|
|
|
import java.lang.reflect.Method;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
import java.util.Collection;
|
|
|
|
|
import java.util.Collections;
|
|
|
|
|
import java.util.Comparator;
|
2022-10-20 23:04:45 +02:00
|
|
|
import java.util.HashSet;
|
2022-10-20 03:06:56 +02:00
|
|
|
import java.util.LinkedHashSet;
|
|
|
|
|
import java.util.List;
|
2022-10-22 02:55:03 +02:00
|
|
|
import java.util.Map;
|
2022-10-20 03:06:56 +02:00
|
|
|
import java.util.Set;
|
|
|
|
|
import java.util.function.Consumer;
|
|
|
|
|
import java.util.function.Function;
|
|
|
|
|
|
|
|
|
|
import love.distributedrebirth.bassboon.clazz.BãßBȍőnAuthor注;
|
2022-10-24 14:34:30 +02:00
|
|
|
import love.distributedrebirth.bassboon.clazz.pirate.BãßBȍőnPiratePhaseBarrier注;
|
|
|
|
|
import love.distributedrebirth.bassboon.clazz.pirate.BãßBȍőnPiratePhase注;
|
|
|
|
|
import love.distributedrebirth.bassboon.clazz.pirate.BãßBȍőnPirateʸᴰ;
|
|
|
|
|
import love.distributedrebirth.bassboon.clazz.pirate.BãßBȍőnꝐŕḯṿª₮ḕ;
|
|
|
|
|
import love.distributedrebirth.bassboon.clazz.pirate.BãßBȍőnꝐŕḯṿª₮ḕ響ᴼᶠ;
|
2022-10-22 02:55:03 +02:00
|
|
|
import love.distributedrebirth.bassboon.death.BãßBȍőnCoffinDuytschenᵗˣᵗ;
|
2022-10-20 03:06:56 +02:00
|
|
|
import love.distributedrebirth.bassboon.death.spider.BãßBȍőnSpider;
|
|
|
|
|
import love.distributedrebirth.bassboon.death.spider.BãßBȍőnSpiderEgg注;
|
|
|
|
|
import love.distributedrebirth.bassboon.death.spider.BãßBȍőnSpiderSilk;
|
|
|
|
|
import love.distributedrebirth.bassboon.death.spider.BãßBȍőnSpiderWire注;
|
|
|
|
|
|
|
|
|
|
@BãßBȍőnAuthor注(name = "للَّٰهِilLצسُو", copyright = "©Δ∞ 仙上主天")
|
2022-10-24 14:34:30 +02:00
|
|
|
public final class DefaultBȍőnLoader implements Bãß.𝔅𝔬𝔫𝔢𝔏𝔬𝔞𝔡𝔢𝔯ʸᴰ {
|
2022-10-20 03:06:56 +02:00
|
|
|
|
2022-10-24 14:34:30 +02:00
|
|
|
protected DefaultBȍőnLoader() {
|
2022-10-20 03:06:56 +02:00
|
|
|
}
|
|
|
|
|
|
2022-10-24 14:34:30 +02:00
|
|
|
public <T extends BãßBȍőnʸᴰ<T>> void 𝔩𝔬𝔠𝔨𝔅𝔬𝔫𝔢𝔐𝔞𝔤𝔦𝔠(BãßBȍőnʸᴰ<T> boon, BãßBȍőnSpider spider) {
|
2022-10-20 03:06:56 +02:00
|
|
|
try {
|
2022-10-20 23:04:45 +02:00
|
|
|
List<PiratePhaseBoat<T>> filoBoats = loadPirateBoats(boon);// TODO: run from spider objects...
|
2022-10-20 03:06:56 +02:00
|
|
|
Collections.reverse(filoBoats);
|
2022-10-20 23:04:45 +02:00
|
|
|
|
|
|
|
|
// Set<Class<?>> ydmods = new HashSet<>();
|
2022-10-20 03:06:56 +02:00
|
|
|
for (PiratePhaseBoat<T> boat : filoBoats) {
|
2022-10-20 23:04:45 +02:00
|
|
|
// if (ydmods.add(boat.pirateClazz.getDeclaringClass())) {
|
|
|
|
|
// spider.registrateEgg(boat.pirateClazz.getDeclaringClass(), new BãßBȍőnSpiderSilk() {
|
|
|
|
|
// @Override
|
|
|
|
|
// public Object senseWire(BãßBȍőnʸᴰ<?> boonTmp, String name, String description)
|
|
|
|
|
// throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
|
|
|
|
|
// return boonTmp;
|
|
|
|
|
// }
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
//registrateSpiderEggs(boat.pirateClazz, spider, boat.pirate);
|
|
|
|
|
initSpiderWires(boat.pirate, boon, spider);
|
|
|
|
|
boat.pirate.lock();
|
2022-10-20 03:06:56 +02:00
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-24 14:34:30 +02:00
|
|
|
public <T extends BãßBȍőnʸᴰ<T>> void 𝔦𝔫𝔦𝔱𝔅𝔬𝔫𝔢𝔐𝔞𝔤𝔦𝔠(BãßBȍőnʸᴰ<T> boon, BãßBȍőnSpider spider, Map<Class<? extends BãßBȍőnCoffinDuytschenᵗˣᵗ>,BãßBȍőnCoffinDuytschenᵗˣᵗ> duytschenCoffins) {
|
2022-10-20 03:06:56 +02:00
|
|
|
try {
|
|
|
|
|
//System.out.println("INIT "+boon.getClass().getName());
|
2022-10-22 02:55:03 +02:00
|
|
|
|
|
|
|
|
for (Class<?> duytschenType:duytschenCoffins.keySet()) {
|
|
|
|
|
BãßBȍőnCoffinDuytschenᵗˣᵗ duytschenCoffin = duytschenCoffins.get(duytschenType);
|
2022-10-23 03:04:55 +02:00
|
|
|
//System.out.println("INIT.REG "+duytschenType+" senseResult="+duytschenCoffin);
|
2022-10-22 02:55:03 +02:00
|
|
|
spider.registrateEgg(duytschenType, new BãßBȍőnSpiderSilk() {
|
|
|
|
|
@Override
|
|
|
|
|
public Object senseWire(BãßBȍőnʸᴰ<?> boonTmp, String name, String description)
|
|
|
|
|
throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
|
|
|
|
|
return duytschenCoffin;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-20 23:04:45 +02:00
|
|
|
Set<Class<?>> ydmods = new HashSet<>();
|
2022-10-20 03:06:56 +02:00
|
|
|
for (PiratePhaseBoat<T> boat : loadPirateBoats(boon)) {
|
2022-10-20 23:04:45 +02:00
|
|
|
//System.out.println("INIT.RUN "+boat.pirateClazz.getName());
|
|
|
|
|
|
|
|
|
|
if (ydmods.add(boat.pirateClazz.getDeclaringClass())) {
|
|
|
|
|
spider.registrateEgg(boat.pirateClazz.getDeclaringClass(), new BãßBȍőnSpiderSilk() {
|
|
|
|
|
@Override
|
|
|
|
|
public Object senseWire(BãßBȍőnʸᴰ<?> boonTmp, String name, String description)
|
|
|
|
|
throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
|
|
|
|
|
return boonTmp;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2022-10-20 03:06:56 +02:00
|
|
|
registrateSpiderEggs(boat.pirateClazz, spider, boat.pirate);
|
|
|
|
|
initSpiderWires(boat.pirate, boon, spider);
|
2022-10-21 01:25:17 +02:00
|
|
|
BãßBȍőnꝐŕḯṿª₮ḕ響ᴼᶠ soundOf = boat.pirate.init();
|
2022-10-20 23:04:45 +02:00
|
|
|
Class<?> soundOfType = soundOf.getClass().getInterfaces()[0]; // TOOD ....
|
|
|
|
|
if (ydmods.add(soundOfType)) {
|
|
|
|
|
//System.out.println("INIT.SOUND "+soundOfType);
|
|
|
|
|
spider.registrateEgg(soundOfType, new BãßBȍőnSpiderSilk() {
|
|
|
|
|
@Override
|
|
|
|
|
public Object senseWire(BãßBȍőnʸᴰ<?> boonTmp, String name, String description)
|
|
|
|
|
throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
|
|
|
|
|
return soundOf;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2022-10-20 03:06:56 +02:00
|
|
|
}
|
|
|
|
|
initSpiderWires(boon, boon, spider);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void initSpiderWires(Object target, BãßBȍőnʸᴰ<?> boon, BãßBȍőnSpider spider) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
|
|
|
|
|
//System.out.println("SCAN-BOON-FIELD: "+boon.getClass().getSimpleName());
|
|
|
|
|
for (Field m:target.getClass().getDeclaredFields()) {
|
|
|
|
|
if (!m.isAnnotationPresent(BãßBȍőnSpiderWire注.class)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
BãßBȍőnSpiderWire注 anno = m.getAnnotation(BãßBȍőnSpiderWire注.class);
|
|
|
|
|
BãßBȍőnSpiderSilk silkRoad = spider.silkRoad(m.getType());
|
|
|
|
|
Object bean = silkRoad.senseWire(boon, anno.name(), anno.description());
|
|
|
|
|
//System.out.println("type: "+ m.getType() +" wth name: "+anno.name()+" of boon: "+boon+" result: "+bean.getClass().getName());
|
|
|
|
|
|
|
|
|
|
m.setAccessible(true);
|
|
|
|
|
m.set(target, bean);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static <T extends BãßBȍőnʸᴰ<T>> void registrateSpiderEggs(Class<?> scanClass, BãßBȍőnSpider spider, BãßBȍőnꝐŕḯṿª₮ḕ<T> pirate) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
|
|
|
|
|
for (Method m:scanClass.getDeclaredMethods()) {
|
|
|
|
|
if (!m.isAnnotationPresent(BãßBȍőnSpiderEgg注.class)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
//System.out.println("annoEgg: "+boon.getClass().getSimpleName()+"."+boon+" from: "+declaredClass.getName()+" m="+m.toGenericString());
|
|
|
|
|
spider.registrateEgg(m.getReturnType(), pirate, m);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static class PiratePhaseBoat<T extends BãßBȍőnʸᴰ<T>> {
|
|
|
|
|
private Class<BãßBȍőnꝐŕḯṿª₮ḕ<T>> pirateClazz;
|
|
|
|
|
private BãßBȍőnꝐŕḯṿª₮ḕ<T> pirate;
|
|
|
|
|
|
|
|
|
|
private PiratePhaseBoat(Class<BãßBȍőnꝐŕḯṿª₮ḕ<T>> pirateClazz) {
|
2022-10-23 03:04:55 +02:00
|
|
|
if (pirateClazz.isEnum()) {
|
|
|
|
|
throw new IllegalStateException("Can't hide enum pirate type: "+pirateClazz);
|
|
|
|
|
}
|
2022-10-20 03:06:56 +02:00
|
|
|
this.pirateClazz = pirateClazz;
|
|
|
|
|
try {
|
|
|
|
|
this.pirate = (BãßBȍőnꝐŕḯṿª₮ḕ<T>) pirateClazz.getConstructor().newInstance();
|
|
|
|
|
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
|
|
|
|
|
| NoSuchMethodException | SecurityException e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static <T extends BãßBȍőnʸᴰ<T>> List<PiratePhaseBoat<T>> loadPirateBoats(BãßBȍőnʸᴰ<T> boon) {
|
|
|
|
|
List<PiratePhaseBoat<T>> boonInits = new ArrayList<>();
|
|
|
|
|
List<PiratePhaseBoat<T>> serviceInits = new ArrayList<>();
|
|
|
|
|
walkPrivate(boon, v -> {
|
|
|
|
|
//System.out.println("INIT.SCAN "+v.getName());
|
|
|
|
|
BãßBȍőnPiratePhaseBarrier注 anno = v.getAnnotation(BãßBȍőnPiratePhaseBarrier注.class);
|
|
|
|
|
if (anno != null) {
|
|
|
|
|
boonInits.add(new PiratePhaseBoat<>(v));
|
|
|
|
|
} else {
|
|
|
|
|
serviceInits.add(new PiratePhaseBoat<>(v));
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
boonInits.sort(new Comparator<PiratePhaseBoat<T>>() {
|
|
|
|
|
@Override
|
|
|
|
|
public int compare(PiratePhaseBoat<T> p0, PiratePhaseBoat<T> p1) {
|
|
|
|
|
BãßBȍőnPiratePhaseBarrier注 anno0 = p0.pirateClazz.getAnnotation(BãßBȍőnPiratePhaseBarrier注.class);
|
|
|
|
|
BãßBȍőnPiratePhaseBarrier注 anno1 = p1.pirateClazz.getAnnotation(BãßBȍőnPiratePhaseBarrier注.class);
|
|
|
|
|
return Integer.compare(anno0.order(), anno1.order());
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
List<PiratePhaseBoat<T>> result = new ArrayList<>();
|
|
|
|
|
for (PiratePhaseBoat<T> startupPhase:boonInits) {
|
|
|
|
|
result.add(startupPhase);
|
|
|
|
|
for (PiratePhaseBoat<T> v:new ArrayList<>(serviceInits)) {
|
|
|
|
|
int depCount = 0;
|
|
|
|
|
List<Class<? extends BãßBȍőnꝐŕḯṿª₮ḕ<?>>> deps = new ArrayList<>();
|
|
|
|
|
BãßBȍőnPiratePhase注 anno = v.pirateClazz.getAnnotation(BãßBȍőnPiratePhase注.class);
|
|
|
|
|
if (anno != null) {
|
|
|
|
|
deps.addAll(Arrays.asList(anno.dependencies()));
|
|
|
|
|
} else {
|
|
|
|
|
deps.add(BãßBȍőnPirateʸᴰ.שְׁלֹמֹה.DEFAULT_PHASE);
|
|
|
|
|
}
|
|
|
|
|
for (Class<?> clazz:deps) {
|
|
|
|
|
for (PiratePhaseBoat<T> doneInit:result) {
|
|
|
|
|
if (clazz.equals(doneInit.pirateClazz)) {
|
|
|
|
|
depCount++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (depCount == deps.size()) {
|
|
|
|
|
result.add(v);
|
|
|
|
|
serviceInits.remove(v);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
result.addAll(serviceInits);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
|
private static <T extends BãßBȍőnʸᴰ<T>> void walkPrivate(BãßBȍőnʸᴰ<T> boon, Consumer<Class<BãßBȍőnꝐŕḯṿª₮ḕ<T>>> runPrivate) {
|
|
|
|
|
for (Class<?> interfaceClass:walkInterfaces(boon.getClass(), new LinkedHashSet<>())) {
|
|
|
|
|
for (Class<?> declaredClass:interfaceClass.getDeclaredClasses()) {
|
|
|
|
|
if (!BãßBȍőnꝐŕḯṿª₮ḕ.class.isAssignableFrom(declaredClass)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
runPrivate.accept((Class<BãßBȍőnꝐŕḯṿª₮ḕ<T>>)declaredClass);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for (Class<?> declaredClass:boon.getClass().getDeclaredClasses()) {
|
|
|
|
|
if (!BãßBȍőnꝐŕḯṿª₮ḕ.class.isAssignableFrom(declaredClass)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
runPrivate.accept((Class<BãßBȍőnꝐŕḯṿª₮ḕ<T>>)declaredClass);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Set<Class<?>> walkInterfaces(Class<?> clazz, Set<Class<?>> result) {
|
|
|
|
|
return walkTree(clazz, result, v -> Arrays.asList(v.getInterfaces()), v -> v.getSuperclass());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static <N> Set<N> walkTree(N node, Set<N> result, Function<N, Collection<N>> childs, Function<N,N> resolve) {
|
|
|
|
|
while (node != null) {
|
|
|
|
|
for (N next : childs.apply(node)) {
|
|
|
|
|
if (result.add(next)) {
|
|
|
|
|
walkTree(next, result, childs, resolve);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
node = resolve.apply(node);
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
}
|