Add completely broken automatic subsystem init

This commit is contained in:
JeremyStar™ 2024-06-29 22:18:09 +02:00
parent 2cbac22792
commit 9add9330bb
Signed by: JeremyStarTM
GPG key ID: E366BAEF67E4704D
2 changed files with 91 additions and 15 deletions

View file

@ -33,15 +33,24 @@ import de.staropensource.sosengine.base.logging.CrashHandler;
import de.staropensource.sosengine.base.logging.Logger; import de.staropensource.sosengine.base.logging.Logger;
import de.staropensource.sosengine.base.logging.LoggerInstance; import de.staropensource.sosengine.base.logging.LoggerInstance;
import de.staropensource.sosengine.base.types.CodePart; import de.staropensource.sosengine.base.types.CodePart;
import de.staropensource.sosengine.base.types.DependencyVector;
import de.staropensource.sosengine.base.types.ImmutableMap;
import de.staropensource.sosengine.base.utility.DependencyResolver;
import de.staropensource.sosengine.base.utility.Miscellaneous; import de.staropensource.sosengine.base.utility.Miscellaneous;
import de.staropensource.sosengine.base.utility.PlaceholderEngine; import de.staropensource.sosengine.base.utility.PlaceholderEngine;
import de.staropensource.sosengine.base.utility.ShortcodeConverter; import de.staropensource.sosengine.base.utility.ShortcodeConverter;
import lombok.Getter; import lombok.Getter;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Range; import org.jetbrains.annotations.Range;
import org.reflections.Reflections;
import org.reflections.scanners.Scanners;
import org.reflections.util.ClasspathHelper;
import org.reflections.util.ConfigurationBuilder;
import java.util.HashMap;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import java.util.Set;
/** /**
* sos!engine management object.<br/> * sos!engine management object.<br/>
@ -114,12 +123,8 @@ public final class Engine implements SubsystemMainClass {
// Only allow one instance // Only allow one instance
if (instance == null) if (instance == null)
instance = this; instance = this;
else { else
instance.logger.crash("sos!engine tried to initialize twice");
//noinspection DataFlowIssue
logger = null;
return; return;
}
long initTime = Miscellaneous.measureExecutionTime(() -> { long initTime = Miscellaneous.measureExecutionTime(() -> {
// Initialize variables // Initialize variables
@ -140,6 +145,16 @@ public final class Engine implements SubsystemMainClass {
// Start threads // Start threads
startThreads(); startThreads();
// Collect subsystems
collectSubsystems();
// Initialize subsystems
try {
initializeSubsystems();
} catch (Exception exception) {
logger.crash("Subsystem dependency resolution failed");
}
}); });
logger.info("Initialized sos!engine v%engine_version% (commit %engine_git_commit_id_long%-%engine_git_branch%, dirty %engine_git_dirty%) in " + initTime + "ms"); logger.info("Initialized sos!engine v%engine_version% (commit %engine_git_commit_id_long%-%engine_git_branch%, dirty %engine_git_dirty%) in " + initTime + "ms");
@ -221,7 +236,7 @@ public final class Engine implements SubsystemMainClass {
* *
* @since 1-alpha0 * @since 1-alpha0
*/ */
public void precomputeEventListeners() { private void precomputeEventListeners() {
// Internal events // Internal events
EventHelper.precomputeEventListeners(InternalEngineShutdownEvent.class); EventHelper.precomputeEventListeners(InternalEngineShutdownEvent.class);
@ -231,6 +246,65 @@ public final class Engine implements SubsystemMainClass {
EventHelper.precomputeEventListeners(LogEvent.class); EventHelper.precomputeEventListeners(LogEvent.class);
} }
/**
* Collects all subsystems by their {@link EngineSubsystem} annotation.
*
* @since 1-alpha1
*/
private void collectSubsystems() {
Map<@NotNull Class<? extends SubsystemMainClass>, @NotNull DependencyVector> subsystemsMap = new HashMap<>();
// Scan entire classpath through Reflections library
Reflections reflections = new Reflections(
new ConfigurationBuilder()
.setUrls(ClasspathHelper.forJavaClassPath())
.setScanners(Scanners.TypesAnnotated)
);
// Get annotated methods
Set<@NotNull Class<?>> annotatedClasses = reflections.getTypesAnnotatedWith(EngineSubsystem.class);
// Add to 'subsystemsMap'
for (Class<?> clazz : annotatedClasses) {
try {
//noinspection unchecked
subsystemsMap.put((Class<? extends SubsystemMainClass>) clazz, (DependencyVector) clazz.getMethod("getDependencyVector").invoke(null));
} catch (Exception ignored) {}
}
// Remove null values
for (Class<? extends SubsystemMainClass> subsystem : subsystemsMap.keySet())
if (subsystemsMap.get(subsystem) == null)
subsystemsMap.remove(subsystem);
subsystems = new ImmutableMap<>(subsystemsMap);
}
/**
* Initializes all subsystems.
*
* @throws Exception exceptions thrown by the {@link DependencyResolver}
* @since 1-alpha1
*/
private void initializeSubsystems() throws Exception {
DependencyResolver resolver = new DependencyResolver();
for (Class<? extends SubsystemMainClass> subsystem : subsystems.keySet())
resolver.addVector(subsystems.get(subsystem));
/*
try {
resolver.resolve();
} catch (Exception exception) {
logger.crash("An error occurred trying to initialize engine subsystems: " + exception.getClass().getName() + (exception.getMessage() == null ? "" : ": " + exception.getMessage()));
throw exception;
}
*/
for (Class<? extends SubsystemMainClass> subsystem : subsystems.keySet())
subsystem.getDeclaredConstructor().newInstance();
}
/** /**
* Starts engine threads. * Starts engine threads.
* *
@ -261,6 +335,17 @@ public final class Engine implements SubsystemMainClass {
Runtime.getRuntime().exit(exitCode); Runtime.getRuntime().exit(exitCode);
} }
/**
* Returns the {@link DependencyVector} for this subsystem.
*
* @see DependencyVector
* @since 1-alpha1
*/
@Override
public DependencyVector getDependencyVector() {
return new DependencyVector("engine", StarOpenSourceVersioningSystem.class, EngineInformation.getInstance().getVersioningString());
}
/** /**
* Shuts the engine and JVM down. * Shuts the engine and JVM down.
* *

View file

@ -27,9 +27,6 @@ import de.staropensource.sosengine.base.types.Vec2i;
import de.staropensource.sosengine.graphics.GraphicsSubsystem; import de.staropensource.sosengine.graphics.GraphicsSubsystem;
import de.staropensource.sosengine.graphics.classes.ApiMainClass; import de.staropensource.sosengine.graphics.classes.ApiMainClass;
import de.staropensource.sosengine.graphics.classes.ApiManagementClass; import de.staropensource.sosengine.graphics.classes.ApiManagementClass;
import de.staropensource.sosengine.graphics.opengl.OpenGlSubsystem;
import de.staropensource.sosengine.graphics.vulkan.VulkanSubsystem;
import de.staropensource.sosengine.slf4j_compat.Slf4jCompatibilitySubsystem;
import lombok.Getter; import lombok.Getter;
import lombok.SneakyThrows; import lombok.SneakyThrows;
@ -101,12 +98,6 @@ public class Main {
// Say hello to the world! // Say hello to the world!
logger.info("Hello world!"); logger.info("Hello world!");
// Initialize subsystems
new Slf4jCompatibilitySubsystem();
new GraphicsSubsystem();
new OpenGlSubsystem();
new VulkanSubsystem();
// Choose Graphics API to use // Choose Graphics API to use
if (!GraphicsSubsystem.getInstance().setGraphicsApi()) { if (!GraphicsSubsystem.getInstance().setGraphicsApi()) {
logger.crash("No Graphics API is compatible"); logger.crash("No Graphics API is compatible");