Refactor Renderer Module Management
- Renamed `moduleManager` to `moduleService` in RendererData for clarity. - Updated G2DRenderer to use the new module service methods with string IDs instead of class references. - Modified RendererWorkingSet interface and implementation to use string IDs for module identification instead of class types. - Adjusted tests in ModuleManagerTest and RendererWorkingSetImplTest to reflect changes in module management and identification. - Ensured consistency across the codebase by replacing class references with module IDs in method calls and assertions.
This commit is contained in:
@@ -12,12 +12,18 @@ public abstract class AbstractModule {
|
||||
|
||||
protected static final Logger LOGGER = LoggerFactory.getLogger(AbstractModule.class);
|
||||
|
||||
protected String id = "Module_" + System.identityHashCode(this);
|
||||
|
||||
private ModuleService moduleService;
|
||||
|
||||
public AbstractModule() {
|
||||
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
public void onLoad() {
|
||||
|
||||
}
|
||||
@@ -46,7 +52,7 @@ public abstract class AbstractModule {
|
||||
return moduleService;
|
||||
}
|
||||
|
||||
public void setModuleService(ModuleService moduleService) {
|
||||
public void setModuleService(final ModuleService moduleService) {
|
||||
this.moduleService = moduleService;
|
||||
}
|
||||
|
||||
|
||||
@@ -4,9 +4,9 @@ import java.util.List;
|
||||
|
||||
public interface ModuleService {
|
||||
|
||||
void loadModule(Class<? extends AbstractModule> clazz);
|
||||
void loadModule(AbstractModule module);
|
||||
|
||||
void unloadModule(Class<? extends AbstractModule> clazz);
|
||||
void unloadModule(String id);
|
||||
|
||||
List<AbstractModule> getModulesAll();
|
||||
|
||||
@@ -16,23 +16,17 @@ public interface ModuleService {
|
||||
|
||||
List<AbstractClientModule> getModulesDisplayedClientModules();
|
||||
|
||||
void changeActiveState(String className, boolean active);
|
||||
|
||||
void changeActiveState(Class<? extends AbstractModule> clazz, boolean active);
|
||||
void changeActiveState(String id, boolean active);
|
||||
|
||||
void deactivateAll();
|
||||
|
||||
void changeDisplayState(String className, boolean active);
|
||||
void changeDisplayState(String id, boolean display);
|
||||
|
||||
void changeDisplayState(Class<? extends AbstractClientModule> clazz, boolean display);
|
||||
|
||||
boolean isDisplayed(String className);
|
||||
|
||||
boolean isDisplayed(Class<? extends AbstractClientModule> clazz);
|
||||
boolean isDisplayed(String id);
|
||||
|
||||
void hideAll();
|
||||
|
||||
void hideExcept(String... classNames);
|
||||
void hideExcept(String... ids);
|
||||
|
||||
void restoreVisibility();
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package de.extio.game_engine.module;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.EnumMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
@@ -24,6 +25,8 @@ public class ModuleServiceImpl implements InitializingBean, ModuleService {
|
||||
|
||||
private final List<AbstractModule> modulesAll = Collections.synchronizedList(new ArrayList<>());
|
||||
|
||||
private final Map<String, AbstractModule> modulesByIdMap = new ConcurrentHashMap<>();
|
||||
|
||||
private final RendererWorkingSet rendererWorkingSet;
|
||||
|
||||
private List<AbstractModule> modulesAllView = List.of();
|
||||
@@ -77,26 +80,19 @@ public class ModuleServiceImpl implements InitializingBean, ModuleService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void loadModule(final Class<? extends AbstractModule> clazz) {
|
||||
synchronized (this.modulesAll) {
|
||||
if (this.modulesAll.stream().anyMatch(module -> clazz.isInstance(module))) {
|
||||
LOGGER.debug("AbstractModule already loaded: " + clazz.getName());
|
||||
return;
|
||||
}
|
||||
public synchronized void loadModule(final AbstractModule module) {
|
||||
if (this.modulesByIdMap.containsKey(module.getId())) {
|
||||
LOGGER.debug("AbstractModule already loaded: " + module.getId());
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
final AbstractModule module = AbstractModule.class.cast(clazz.getDeclaredConstructor().newInstance());
|
||||
this.load(module);
|
||||
}
|
||||
catch (final Exception e) {
|
||||
LOGGER.error("Error loading module " + clazz.getName(), e);
|
||||
}
|
||||
this.load(module);
|
||||
}
|
||||
|
||||
private void load(final AbstractModule module) {
|
||||
module.setModuleService(this);
|
||||
this.modulesAll.add(module);
|
||||
this.modulesByIdMap.put(module.getId(), module);
|
||||
this.sortModules(this.modulesAll);
|
||||
synchronized (this.modulesAll) {
|
||||
this.modulesAllView = List.copyOf(this.modulesAll);
|
||||
@@ -106,20 +102,18 @@ public class ModuleServiceImpl implements InitializingBean, ModuleService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void unloadModule(final Class<? extends AbstractModule> clazz) {
|
||||
public synchronized void unloadModule(final String id) {
|
||||
try {
|
||||
AbstractModule module;
|
||||
synchronized (this.modulesAll) {
|
||||
module = this.modulesAll.stream().filter(mod -> clazz.isInstance(mod)).findAny().orElse(null);
|
||||
}
|
||||
final AbstractModule module = this.modulesByIdMap.get(id);
|
||||
|
||||
if (module == null) {
|
||||
LOGGER.debug("AbstractModule not loaded: " + clazz.getName());
|
||||
LOGGER.debug("AbstractModule not loaded: " + id);
|
||||
return;
|
||||
}
|
||||
|
||||
this.changeActiveState(clazz, false);
|
||||
this.changeActiveState(id, false);
|
||||
this.modulesAll.remove(module);
|
||||
this.modulesByIdMap.remove(id);
|
||||
this.sortModules(this.modulesAll);
|
||||
synchronized (this.modulesAll) {
|
||||
this.modulesAllView = List.copyOf(this.modulesAll);
|
||||
@@ -127,48 +121,37 @@ public class ModuleServiceImpl implements InitializingBean, ModuleService {
|
||||
|
||||
this.invokeSafe(module, m -> m.onUnload());
|
||||
if (module instanceof final AbstractClientModule clientModule) {
|
||||
this.rendererWorkingSet.clear(clientModule.getClass());
|
||||
this.rendererWorkingSet.clear(clientModule.getId());
|
||||
}
|
||||
|
||||
LOGGER.info("Unloaded module " + clazz.getName());
|
||||
LOGGER.info("Unloaded module " + id);
|
||||
}
|
||||
catch (final Exception e) {
|
||||
LOGGER.error("Error unloading module " + clazz.getName(), e);
|
||||
LOGGER.error("Error unloading module " + id, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void changeActiveState(final String className, final boolean active) {
|
||||
try {
|
||||
final var clazz = Class.forName(className);
|
||||
this.changeActiveState(clazz.asSubclass(AbstractModule.class), active);
|
||||
}
|
||||
catch (final ClassNotFoundException e) {
|
||||
LOGGER.error("Class not found: " + className, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void changeActiveState(final Class<? extends AbstractModule> clazz, final boolean active) {
|
||||
public synchronized void changeActiveState(final String id, final boolean active) {
|
||||
this.modulesActiveView.stream()
|
||||
.filter(module -> clazz.isAssignableFrom(module.getClass()))
|
||||
.filter(module -> module.getId().equals(id))
|
||||
.findAny()
|
||||
.ifPresentOrElse(module -> {
|
||||
if (!active) {
|
||||
if (module instanceof final AbstractClientModule clientModule) {
|
||||
this.changeDisplayState(clientModule.getClass(), false);
|
||||
this.changeDisplayState(clientModule.getId(), false);
|
||||
}
|
||||
this.modulesActive.remove(module);
|
||||
this.sortModules(this.modulesActive);
|
||||
synchronized (this.modulesActive) {
|
||||
this.modulesActiveView = List.copyOf(this.modulesActive);
|
||||
this.modulesActiveClientModulesView = this.modulesActive.stream()
|
||||
.filter(m -> m instanceof AbstractClientModule)
|
||||
.map(m -> (AbstractClientModule) m)
|
||||
.toList();
|
||||
.filter(m -> m instanceof AbstractClientModule)
|
||||
.map(m -> (AbstractClientModule) m)
|
||||
.toList();
|
||||
}
|
||||
if (module instanceof final AbstractClientModule clientModule) {
|
||||
this.rendererWorkingSet.clear(clientModule.getClass());
|
||||
this.rendererWorkingSet.clear(clientModule.getId());
|
||||
}
|
||||
synchronized (this.executorCallbackMap) {
|
||||
final var subscriptions = module.executorCallbackSubscriptions();
|
||||
@@ -180,39 +163,35 @@ public class ModuleServiceImpl implements InitializingBean, ModuleService {
|
||||
}
|
||||
this.invokeSafe(module, AbstractModule::onDeactivate);
|
||||
|
||||
LOGGER.info("Deactivated " + clazz.getName());
|
||||
LOGGER.info("Deactivated " + id);
|
||||
}
|
||||
}, () -> {
|
||||
if (active) {
|
||||
synchronized (this.modulesAll) {
|
||||
this.modulesAll.stream()
|
||||
.filter(module -> clazz.isAssignableFrom(module.getClass()))
|
||||
.findFirst()
|
||||
.ifPresent(module -> {
|
||||
if (module instanceof final AbstractClientModule clientModule && clientModule.isAlwaysDisplay()) {
|
||||
this.changeDisplayState(clientModule.getClass(), true);
|
||||
}
|
||||
this.modulesActive.add(module);
|
||||
this.sortModules(this.modulesActive);
|
||||
synchronized (this.modulesActive) {
|
||||
this.modulesActiveView = List.copyOf(this.modulesActive);
|
||||
this.modulesActiveClientModulesView = this.modulesActive.stream()
|
||||
.filter(m -> m instanceof AbstractClientModule)
|
||||
.map(m -> (AbstractClientModule) m)
|
||||
.toList();
|
||||
}
|
||||
synchronized (this.executorCallbackMap) {
|
||||
final var subscriptions = module.executorCallbackSubscriptions();
|
||||
if (subscriptions != null) {
|
||||
for (final var subscription : subscriptions) {
|
||||
executorCallbackMap.computeIfAbsent(subscription, k -> Collections.synchronizedList(new ArrayList<>())).add(module);
|
||||
}
|
||||
}
|
||||
}
|
||||
this.invokeSafe(module, AbstractModule::onActivate);
|
||||
|
||||
LOGGER.info("Activated " + clazz.getName());
|
||||
});
|
||||
final AbstractModule module = this.modulesByIdMap.get(id);
|
||||
if (module != null) {
|
||||
if (module instanceof final AbstractClientModule clientModule && clientModule.isAlwaysDisplay()) {
|
||||
this.changeDisplayState(clientModule.getId(), true);
|
||||
}
|
||||
this.modulesActive.add(module);
|
||||
this.sortModules(this.modulesActive);
|
||||
synchronized (this.modulesActive) {
|
||||
this.modulesActiveView = List.copyOf(this.modulesActive);
|
||||
this.modulesActiveClientModulesView = this.modulesActive.stream()
|
||||
.filter(m -> m instanceof AbstractClientModule)
|
||||
.map(m -> (AbstractClientModule) m)
|
||||
.toList();
|
||||
}
|
||||
synchronized (this.executorCallbackMap) {
|
||||
final var subscriptions = module.executorCallbackSubscriptions();
|
||||
if (subscriptions != null) {
|
||||
for (final var subscription : subscriptions) {
|
||||
executorCallbackMap.computeIfAbsent(subscription, k -> Collections.synchronizedList(new ArrayList<>())).add(module);
|
||||
}
|
||||
}
|
||||
}
|
||||
this.invokeSafe(module, AbstractModule::onActivate);
|
||||
|
||||
LOGGER.info("Activated " + id);
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -221,71 +200,41 @@ public class ModuleServiceImpl implements InitializingBean, ModuleService {
|
||||
@Override
|
||||
public void deactivateAll() {
|
||||
for (final AbstractModule module : this.modulesActiveView) {
|
||||
this.changeActiveState(module.getClass(), false);
|
||||
this.changeActiveState(module.getId(), false);
|
||||
}
|
||||
this.lastVisibleStates.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void changeDisplayState(final String className, final boolean active) {
|
||||
try {
|
||||
final var clazz = Class.forName(className);
|
||||
this.changeDisplayState(clazz.asSubclass(AbstractClientModule.class), active);
|
||||
}
|
||||
catch (final ClassNotFoundException e) {
|
||||
LOGGER.error("Class not found: " + className, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void changeDisplayState(final Class<? extends AbstractClientModule> clazz, final boolean display) {
|
||||
synchronized (this.modulesAll) {
|
||||
public synchronized void changeDisplayState(final String id, final boolean display) {
|
||||
final AbstractModule module = this.modulesByIdMap.get(id);
|
||||
if (module instanceof final AbstractClientModule clientModule) {
|
||||
synchronized (this.modulesDisplayed) {
|
||||
for (final var module : this.modulesAll) {
|
||||
if (module instanceof final AbstractClientModule clientModule && clazz.isAssignableFrom(module.getClass())) {
|
||||
final boolean curDisplayed = this.modulesDisplayed.stream().anyMatch(mod -> clazz.isAssignableFrom(mod.getClass()));
|
||||
|
||||
final boolean doDisplay = display || clientModule.isAlwaysDisplay();
|
||||
if (doDisplay && !curDisplayed) {
|
||||
this.modulesDisplayed.add(clientModule);
|
||||
clientModule.setDisplayed(true);
|
||||
LOGGER.info("Displayed {}", clazz.getName());
|
||||
this.invokeSafe(clientModule, m -> ((AbstractClientModule) m).onShow());
|
||||
}
|
||||
else if (!doDisplay && curDisplayed) {
|
||||
clientModule.setDisplayed(false);
|
||||
this.modulesDisplayed.removeIf(mod -> clazz.isAssignableFrom(mod.getClass()));
|
||||
LOGGER.info("Hidden {}", clazz.getName());
|
||||
this.invokeSafe(clientModule, m -> ((AbstractClientModule) m).onHide());
|
||||
}
|
||||
this.modulesDisplayedClientModulesView = List.copyOf(this.modulesDisplayed);
|
||||
|
||||
break;
|
||||
}
|
||||
final boolean curDisplayed = this.modulesDisplayed.stream().anyMatch(mod -> mod.getId().equals(id));
|
||||
|
||||
final boolean doDisplay = display || clientModule.isAlwaysDisplay();
|
||||
if (doDisplay && !curDisplayed) {
|
||||
this.modulesDisplayed.add(clientModule);
|
||||
clientModule.setDisplayed(true);
|
||||
LOGGER.info("Displayed {}", id);
|
||||
this.invokeSafe(clientModule, m -> ((AbstractClientModule) m).onShow());
|
||||
}
|
||||
else if (!doDisplay && curDisplayed) {
|
||||
clientModule.setDisplayed(false);
|
||||
this.modulesDisplayed.removeIf(mod -> mod.getId().equals(id));
|
||||
LOGGER.info("Hidden {}", id);
|
||||
this.invokeSafe(clientModule, m -> ((AbstractClientModule) m).onHide());
|
||||
}
|
||||
this.modulesDisplayedClientModulesView = List.copyOf(this.modulesDisplayed);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDisplayed(final String className) {
|
||||
try {
|
||||
final var clazz = Class.forName(className);
|
||||
return this.isDisplayed(clazz.asSubclass(AbstractClientModule.class));
|
||||
}
|
||||
catch (final ClassNotFoundException e) {
|
||||
LOGGER.error("Class not found: " + className, e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDisplayed(final Class<? extends AbstractClientModule> clazz) {
|
||||
final var modules = this.modulesDisplayedClientModulesView;
|
||||
for (int i = 0; i < modules.size(); i++) {
|
||||
if (clazz.isAssignableFrom(modules.get(i).getClass())) {
|
||||
return true;
|
||||
}
|
||||
public boolean isDisplayed(final String id) {
|
||||
final var module = this.modulesByIdMap.get(id);
|
||||
if (module != null && module instanceof final AbstractClientModule clientModule) {
|
||||
return clientModule.isDisplayed();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -297,23 +246,23 @@ public class ModuleServiceImpl implements InitializingBean, ModuleService {
|
||||
final var modules = this.modulesDisplayedClientModulesView;
|
||||
for (final var module : modules) {
|
||||
if ((module instanceof final AbstractClientModule clientModule) && !clientModule.isAlwaysDisplay()) {
|
||||
this.changeDisplayState(clientModule.getClass(), false);
|
||||
this.changeDisplayState(clientModule.getId(), false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@SafeVarargs
|
||||
public final void hideExcept(final String... classNames) {
|
||||
public final void hideExcept(final String... ids) {
|
||||
final List<AbstractClientModule> state = new ArrayList<>();
|
||||
final var keep = new HashSet<>(Arrays.asList(classNames));
|
||||
final var keep = new HashSet<>(Arrays.asList(ids));
|
||||
|
||||
final var modules = this.modulesDisplayedClientModulesView;
|
||||
for (final var module : modules) {
|
||||
if (module instanceof final AbstractClientModule clientModule &&
|
||||
!clientModule.isAlwaysDisplay() &&
|
||||
!keep.contains(clientModule.getClass().getName())) {
|
||||
this.changeDisplayState(clientModule.getClass(), false);
|
||||
!keep.contains(clientModule.getId())) {
|
||||
this.changeDisplayState(clientModule.getId(), false);
|
||||
state.add(clientModule);
|
||||
}
|
||||
}
|
||||
@@ -331,7 +280,7 @@ public class ModuleServiceImpl implements InitializingBean, ModuleService {
|
||||
|
||||
synchronized (this.lastVisibleStates) {
|
||||
for (final AbstractClientModule module : this.lastVisibleStates.pop()) {
|
||||
this.changeDisplayState(module.getClass(), true);
|
||||
this.changeDisplayState(module.getId(), true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package de.extio.game_engine.renderer;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
|
||||
@@ -40,7 +39,7 @@ public class RendererData {
|
||||
|
||||
private final RendererWorkingSet rendererWorkingSet;
|
||||
|
||||
private final ModuleService moduleManager;
|
||||
private final ModuleService moduleService;
|
||||
|
||||
private final ModuleExecutor moduleExecutor;
|
||||
|
||||
@@ -48,7 +47,7 @@ public class RendererData {
|
||||
|
||||
private RendererLoop rendererLoop;
|
||||
|
||||
public RendererData(final ApplicationContext applicationContext, final Renderer renderer, final RendererControl rendererControl, final RenderingBoPool renderingBoPool, final KeycodeRegistry keycodeRegistry, final EventService eventService, final StorageService storageService, final RendererWorkingSet rendererWorkingSet, final ModuleService moduleManager, final ModuleExecutor moduleExecutor, final StaticResourceService staticResourceService) {
|
||||
public RendererData(final ApplicationContext applicationContext, final Renderer renderer, final RendererControl rendererControl, final RenderingBoPool renderingBoPool, final KeycodeRegistry keycodeRegistry, final EventService eventService, final StorageService storageService, final RendererWorkingSet rendererWorkingSet, final ModuleService moduleService, final ModuleExecutor moduleExecutor, final StaticResourceService staticResourceService) {
|
||||
this.applicationContext = applicationContext;
|
||||
this.renderer = renderer;
|
||||
this.rendererControl = rendererControl;
|
||||
@@ -57,7 +56,7 @@ public class RendererData {
|
||||
this.eventService = eventService;
|
||||
this.storageService = storageService;
|
||||
this.rendererWorkingSet = rendererWorkingSet;
|
||||
this.moduleManager = moduleManager;
|
||||
this.moduleService = moduleService;
|
||||
this.moduleExecutor = moduleExecutor;
|
||||
this.staticResourceService = staticResourceService;
|
||||
}
|
||||
@@ -118,8 +117,8 @@ public class RendererData {
|
||||
return rendererWorkingSet;
|
||||
}
|
||||
|
||||
public ModuleService getModuleManager() {
|
||||
return moduleManager;
|
||||
public ModuleService getModuleService() {
|
||||
return moduleService;
|
||||
}
|
||||
|
||||
public ModuleExecutor getModuleExecutor() {
|
||||
|
||||
@@ -56,6 +56,8 @@ public class G2DRenderer implements Renderer {
|
||||
|
||||
private volatile G2DMainFrame mainFrame;
|
||||
|
||||
private String rendererModuleId;
|
||||
|
||||
private RendererData rendererData;
|
||||
|
||||
private long fpsMeasurement;
|
||||
@@ -71,7 +73,7 @@ public class G2DRenderer implements Renderer {
|
||||
private String title;
|
||||
|
||||
private volatile boolean takeScreenshot;
|
||||
|
||||
|
||||
private StaticResource previousDefaultFont = null;
|
||||
|
||||
public G2DRenderer() {
|
||||
@@ -89,10 +91,12 @@ public class G2DRenderer implements Renderer {
|
||||
public void show() {
|
||||
try {
|
||||
LOGGER.info("show()");
|
||||
|
||||
this.rendererData.getModuleManager().loadModule(G2DRendererModule.class);
|
||||
this.rendererData.getModuleManager().changeActiveState(G2DRendererModule.class, true);
|
||||
this.rendererData.getModuleManager().changeDisplayState(G2DRendererModule.class, true);
|
||||
|
||||
final var rendererModule = new G2DRendererModule();
|
||||
this.rendererModuleId = rendererModule.getId();
|
||||
this.rendererData.getModuleService().loadModule(rendererModule);
|
||||
this.rendererData.getModuleService().changeActiveState(rendererModule.getId(), true);
|
||||
this.rendererData.getModuleService().changeDisplayState(rendererModule.getId(), true);
|
||||
|
||||
EventQueue.invokeAndWait(() -> {
|
||||
this.rendererData.getRendererControl().applyVideoOptions();
|
||||
@@ -100,8 +104,8 @@ public class G2DRenderer implements Renderer {
|
||||
EventQueue.invokeAndWait(() -> {
|
||||
// this invokeAndWait() call is also important to wait until applyVideoOptions() has been completed, which also puts events to AWT event queue
|
||||
LOGGER.debug("Viewport initialized");
|
||||
this.rendererData.getRendererWorkingSet().add(G2DRendererModule.class, this.rendererData.getRenderingBoPool().acquire("G2DRenderer_background", G2DDrawBackground.class));
|
||||
this.rendererData.getRendererWorkingSet().add(G2DRendererModule.class, this.rendererData.getRenderingBoPool().acquire("G2DRenderer_tooltip", G2DDrawControlTooltip.class));
|
||||
this.rendererData.getRendererWorkingSet().add(this.rendererModuleId, this.rendererData.getRenderingBoPool().acquire("G2DRenderer_background", G2DDrawBackground.class));
|
||||
this.rendererData.getRendererWorkingSet().add(this.rendererModuleId, this.rendererData.getRenderingBoPool().acquire("G2DRenderer_tooltip", G2DDrawControlTooltip.class));
|
||||
});
|
||||
}
|
||||
catch (InvocationTargetException | InterruptedException e) {
|
||||
@@ -152,7 +156,7 @@ public class G2DRenderer implements Renderer {
|
||||
// Some race condition happens rarely (mainly Windows 11) that window size is not initialized after entering full screen. Put a plaster on it...
|
||||
throw new G2DRendererWindowNotInitializedException();
|
||||
}
|
||||
|
||||
|
||||
if (this.previousDefaultFont == null && this.rendererData.getUiOptions().getFontResource() != null && !this.rendererData.getUiOptions().getFontResource().equals(this.previousDefaultFont)) {
|
||||
this.previousDefaultFont = this.rendererData.getUiOptions().getFontResource();
|
||||
G2DDrawFont.updateDefaultFont(this.rendererData.getStaticResourceService(), this.rendererData.getUiOptions().getFontResource());
|
||||
@@ -169,8 +173,8 @@ public class G2DRenderer implements Renderer {
|
||||
}
|
||||
|
||||
this.drawStatistics();
|
||||
this.rendererData.getRendererWorkingSet().commit(G2DRendererModule.class, true);
|
||||
this.rendererData.getRendererWorkingSet().getLiveSet(this.renderingBOs, clientModuleClass -> this.rendererData.getModuleManager().isDisplayed(clientModuleClass));
|
||||
this.rendererData.getRendererWorkingSet().commit(this.rendererModuleId, true);
|
||||
this.rendererData.getRendererWorkingSet().getLiveSet(this.renderingBOs, this.rendererData.getModuleService()::isDisplayed);
|
||||
this.renderingBOs.sort((bo0, bo1) -> bo0.getLayer().compareTo(bo1.getLayer()));
|
||||
|
||||
Graphics2D screenGraphics = null;
|
||||
@@ -195,7 +199,7 @@ public class G2DRenderer implements Renderer {
|
||||
}
|
||||
}
|
||||
|
||||
this.rendererData.getRendererWorkingSet().get(G2DRendererModule.class, "G2DRenderer_tooltip").closeStatic();
|
||||
this.rendererData.getRendererWorkingSet().get(this.rendererModuleId, "G2DRenderer_tooltip").closeStatic();
|
||||
|
||||
//
|
||||
// Rendering cycle END
|
||||
@@ -251,14 +255,14 @@ public class G2DRenderer implements Renderer {
|
||||
.setColor(RgbaColor.WHITE)
|
||||
.setLayer(RenderingBoLayer.TOP)
|
||||
.withPositionAbsoluteAnchorTopRight(100, 35);
|
||||
this.rendererData.getRendererWorkingSet().add(G2DRendererModule.class, drawFont);
|
||||
this.rendererData.getRendererWorkingSet().add(this.rendererModuleId, drawFont);
|
||||
|
||||
this.fpsHistory.add(Integer.valueOf((int) this.frameDur));
|
||||
final var drawFpsHistory = this.rendererData.getRenderingBoPool().acquire("g2DRenderer_fpsHistory", G2DDrawFpsHistory.class)
|
||||
.setHistory(this.fpsHistory)
|
||||
.setLayer(RenderingBoLayer.TOP)
|
||||
.withPositionAbsoluteAnchorTopRight(100, 50);
|
||||
this.rendererData.getRendererWorkingSet().add(G2DRendererModule.class, drawFpsHistory);
|
||||
this.rendererData.getRendererWorkingSet().add(this.rendererModuleId, drawFpsHistory);
|
||||
}
|
||||
|
||||
private void frameCap() throws InterruptedException {
|
||||
|
||||
@@ -4,23 +4,22 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import de.extio.game_engine.module.AbstractClientModule;
|
||||
import de.extio.game_engine.renderer.model.RenderingBo;
|
||||
|
||||
public interface RendererWorkingSet {
|
||||
|
||||
void add(Class<? extends AbstractClientModule> producer, RenderingBo work);
|
||||
void add(String producerId, RenderingBo work);
|
||||
|
||||
void add(Class<? extends AbstractClientModule> producer, List<RenderingBo> work);
|
||||
void add(String producerId, List<RenderingBo> work);
|
||||
|
||||
RenderingBo get(Class<? extends AbstractClientModule> producer, String id);
|
||||
RenderingBo get(String producerId, String id);
|
||||
|
||||
Map<String, RenderingBo> getUncommittedWork(Class<? extends AbstractClientModule> producer);
|
||||
Map<String, RenderingBo> getUncommittedWork(String producerId);
|
||||
|
||||
Map<String, RenderingBo> commit(Class<? extends AbstractClientModule> producer, boolean clone);
|
||||
Map<String, RenderingBo> commit(String producerId, boolean clone);
|
||||
|
||||
void clear(Class<? extends AbstractClientModule> producer);
|
||||
void clear(String producerId);
|
||||
|
||||
void getLiveSet(List<RenderingBo> combinedLiveSet, Predicate<Class<? extends AbstractClientModule>> filter);
|
||||
void getLiveSet(List<RenderingBo> combinedLiveSet, Predicate<String> filter);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -13,12 +13,11 @@ import java.util.concurrent.ConcurrentMap;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import de.extio.game_engine.module.AbstractClientModule;
|
||||
import de.extio.game_engine.renderer.model.RenderingBo;
|
||||
|
||||
public class RendererWorkingSetImpl implements RendererWorkingSet {
|
||||
|
||||
private final ConcurrentMap<Class<? extends AbstractClientModule>, RendererWork> workingSet = new ConcurrentHashMap<>();
|
||||
private final ConcurrentMap<String, RendererWork> workingSet = new ConcurrentHashMap<>();
|
||||
|
||||
private final BlockingQueue<Map<String, RenderingBo>> mapsPool = new ArrayBlockingQueue<>(100);
|
||||
|
||||
@@ -31,34 +30,34 @@ public class RendererWorkingSetImpl implements RendererWorkingSet {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(final Class<? extends AbstractClientModule> producer, final RenderingBo work) {
|
||||
this.getWorkingSetByProducer(producer)
|
||||
public void add(final String producerId, final RenderingBo work) {
|
||||
this.getWorkingSetByProducer(producerId)
|
||||
.next()
|
||||
.put(work.getId(), work);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(final Class<? extends AbstractClientModule> producer, final List<RenderingBo> work) {
|
||||
final var nextMap = this.getWorkingSetByProducer(producer).next();
|
||||
public void add(final String producerId, final List<RenderingBo> work) {
|
||||
final var nextMap = this.getWorkingSetByProducer(producerId).next();
|
||||
for (final RenderingBo bo : work) {
|
||||
nextMap.put(bo.getId(), bo);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, RenderingBo> getUncommittedWork(final Class<? extends AbstractClientModule> producer) {
|
||||
return this.getWorkingSetByProducer(producer).next();
|
||||
public Map<String, RenderingBo> getUncommittedWork(final String producerId) {
|
||||
return this.getWorkingSetByProducer(producerId).next();
|
||||
}
|
||||
|
||||
@Override
|
||||
public RenderingBo get(final Class<? extends AbstractClientModule> producer, final String id) {
|
||||
return this.getWorkingSetByProducer(producer).next().get(id);
|
||||
public RenderingBo get(final String producerId, final String id) {
|
||||
return this.getWorkingSetByProducer(producerId).next().get(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, RenderingBo> commit(final Class<? extends AbstractClientModule> producer, final boolean clone) {
|
||||
public Map<String, RenderingBo> commit(final String producerId, final boolean clone) {
|
||||
final AtomicReference<Map<String, RenderingBo>> previousWork = new AtomicReference<>();
|
||||
final RendererWork rendererWork = this.workingSet.compute(producer, (k, v) -> {
|
||||
final RendererWork rendererWork = this.workingSet.compute(producerId, (k, v) -> {
|
||||
if (v == null) {
|
||||
return new RendererWork(this.obtainMapFromPool(), this.obtainMapFromPool());
|
||||
}
|
||||
@@ -82,8 +81,8 @@ public class RendererWorkingSetImpl implements RendererWorkingSet {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear(final Class<? extends AbstractClientModule> producer) {
|
||||
final RendererWork rendererWork = this.workingSet.remove(producer);
|
||||
public void clear(final String producerId) {
|
||||
final RendererWork rendererWork = this.workingSet.remove(producerId);
|
||||
if (rendererWork != null) {
|
||||
this.releaseBoClasses(rendererWork.live());
|
||||
this.releaseBoClasses(rendererWork.next());
|
||||
@@ -91,7 +90,7 @@ public class RendererWorkingSetImpl implements RendererWorkingSet {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void getLiveSet(final List<RenderingBo> combinedLiveSet, final Predicate<Class<? extends AbstractClientModule>> filter) {
|
||||
public void getLiveSet(final List<RenderingBo> combinedLiveSet, final Predicate<String> filter) {
|
||||
this.workingSet.forEach((producer, rendererWork) -> {
|
||||
if (filter == null || filter.test(producer)) {
|
||||
combinedLiveSet.addAll(rendererWork.live().values());
|
||||
@@ -99,8 +98,8 @@ public class RendererWorkingSetImpl implements RendererWorkingSet {
|
||||
});
|
||||
}
|
||||
|
||||
private RendererWork getWorkingSetByProducer(final Class<? extends AbstractClientModule> producer) {
|
||||
return this.workingSet.computeIfAbsent(producer, k -> new RendererWork(this.obtainMapFromPool(), this.obtainMapFromPool()));
|
||||
private RendererWork getWorkingSetByProducer(final String producerId) {
|
||||
return this.workingSet.computeIfAbsent(producerId, k -> new RendererWork(this.obtainMapFromPool(), this.obtainMapFromPool()));
|
||||
}
|
||||
|
||||
private void releaseBoClasses(final Map<String, RenderingBo> work) {
|
||||
|
||||
Reference in New Issue
Block a user