From 823a2cd4df8ede694c85e78febe65cd838df459b Mon Sep 17 00:00:00 2001 From: lieght <49651652+BAFGdeJong@users.noreply.github.com> Date: Wed, 1 Oct 2025 14:50:47 +0200 Subject: [PATCH] Added ability to track loading amount through event. --- app/src/main/java/org/toop/Main.java | 20 ++++-- .../org/toop/framework/asset/AssetLoader.java | 69 +++++++++++++++---- .../toop/framework/asset/AssetManager.java | 8 +-- .../framework/asset/events/AssetEvents.java | 7 ++ 4 files changed, 81 insertions(+), 23 deletions(-) create mode 100644 framework/src/main/java/org/toop/framework/asset/events/AssetEvents.java diff --git a/app/src/main/java/org/toop/Main.java b/app/src/main/java/org/toop/Main.java index a3dbd73..ed6a662 100644 --- a/app/src/main/java/org/toop/Main.java +++ b/app/src/main/java/org/toop/Main.java @@ -3,6 +3,7 @@ package org.toop; import org.toop.app.gui.LocalServerSelector; import org.toop.framework.asset.AssetLoader; import org.toop.framework.asset.AssetManager; +import org.toop.framework.asset.events.AssetEvents; import org.toop.framework.asset.resources.TextAsset; import org.toop.framework.audio.SoundManager; import org.toop.framework.audio.events.AudioEvents; @@ -10,22 +11,27 @@ import org.toop.framework.eventbus.EventFlow; import org.toop.framework.networking.NetworkingClientManager; import org.toop.framework.networking.NetworkingInitializationException; -import javax.sound.sampled.*; -import java.io.IOException; import java.nio.file.NotDirectoryException; public class Main { static void main(String[] args) { + javax.swing.SwingUtilities.invokeLater(LocalServerSelector::new); + + new EventFlow().listen(Main::loadingHandler); + AssetManager.loadAssets(new AssetLoader("app/src/main/resources/assets")); var text = AssetManager.getAllOfType(TextAsset.class).getFirst().getResource(); + var jpg = AssetManager.getByName("background.jpg"); + + System.out.println(jpg.getResource().getFile()); text.load(); IO.println(text.getContent()); - var b = new NetworkingClientManager(); - var c = new SoundManager(); + new Thread(NetworkingClientManager::new).start(); + new Thread(SoundManager::new).start(); new EventFlow().addPostEvent(new AudioEvents.PlayAudio("mainmenu.wav", true)).asyncPostEvent(); // new EventFlow().addPostEvent(new AudioEvents.PlayAudio("sadtrombone.wav", false)).asyncPostEvent(); @@ -35,8 +41,12 @@ public class Main { // Thread.sleep(200); // new EventFlow().addPostEvent(new AudioEvents.PlayAudio("mainmenu.wav", false)).asyncPostEvent(); // new EventFlow().addPostEvent(new AudioEvents.PlayAudio("sadtrombone.wav", false)).asyncPostEvent(); + } - javax.swing.SwingUtilities.invokeLater(LocalServerSelector::new); + private static void loadingHandler(AssetEvents.LoadingProgressUpdate update) { + int loaded = update.hasLoadedAmount(); + int total = update.isLoadingAmount(); + double percent = (total == 0) ? 100.0 : (loaded * 100.0 / total); } private static void initSystems() throws NetworkingInitializationException, NotDirectoryException { diff --git a/framework/src/main/java/org/toop/framework/asset/AssetLoader.java b/framework/src/main/java/org/toop/framework/asset/AssetLoader.java index 2a622fb..6d632fc 100644 --- a/framework/src/main/java/org/toop/framework/asset/AssetLoader.java +++ b/framework/src/main/java/org/toop/framework/asset/AssetLoader.java @@ -1,5 +1,8 @@ package org.toop.framework.asset; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.toop.framework.asset.events.AssetEvents; import org.toop.framework.asset.resources.BaseResource; import java.io.File; @@ -11,32 +14,52 @@ import java.util.function.Function; import org.reflections.Reflections; import org.toop.framework.asset.resources.FileExtension; import org.toop.framework.asset.resources.FontAsset; +import org.toop.framework.eventbus.EventFlow; public class AssetLoader { - + private static final Logger logger = LogManager.getLogger(AssetLoader.class); private final List> assets = new CopyOnWriteArrayList<>(); private final Map> registry = new ConcurrentHashMap<>(); + private volatile int loadedCount = 0; + private volatile int totalCount = 0; + public AssetLoader(File rootFolder) { - autoRegisterResources(); - fileSearcher(rootFolder); + autoRegisterResources(); // make sure resources are registered! + + List foundFiles = new ArrayList<>(); + fileSearcher(rootFolder, foundFiles); + this.totalCount = foundFiles.size(); + loader(foundFiles); } public AssetLoader(String rootFolder) { this(new File(rootFolder)); } + public double getProgress() { + return (this.totalCount == 0) ? 1.0 : (this.loadedCount / (double) this.totalCount); + } + + public int getLoadedCount() { + return this.loadedCount; + } + + public int getTotalCount() { + return this.totalCount; + } + public List> getAssets() { - return new ArrayList<>(assets); + return new ArrayList<>(this.assets); } public void register(String extension, Function factory) { - registry.put(extension, factory); + this.registry.put(extension, factory); } private T resourceMapper(File file, Class type) { String ext = getExtension(file.getName()); - Function factory = registry.get(ext); + Function factory = this.registry.get(ext); if (factory == null) return null; @@ -51,16 +74,34 @@ public class AssetLoader { return type.cast(resource); } - private void fileSearcher(final File folder) { + private void loader(List files) { + for (File file : files) { + BaseResource resource = resourceMapper(file, BaseResource.class); + if (resource != null) { + Asset asset = new Asset<>(file.getName(), resource); + this.assets.add(asset); + + if (resource instanceof FontAsset fontAsset) { + fontAsset.load(); + } + + logger.info("Loaded {} from {}", resource.getClass().getSimpleName(), file.getAbsolutePath()); + + this.loadedCount++; // TODO: Fix non atmomic operation + new EventFlow() + .addPostEvent(new AssetEvents.LoadingProgressUpdate(this.loadedCount, this.totalCount)) + .postEvent(); + } + } + logger.info("Loaded {} assets", files.size()); + } + + private void fileSearcher(final File folder, List foundFiles) { for (File fileEntry : Objects.requireNonNull(folder.listFiles())) { if (fileEntry.isDirectory()) { - fileSearcher(fileEntry); + fileSearcher(fileEntry, foundFiles); } else { - BaseResource resource = resourceMapper(fileEntry, BaseResource.class); - if (resource != null) { - assets.add(new Asset<>(fileEntry.getName(), resource)); - if (resource instanceof FontAsset fontAsset) fontAsset.load(); // Autoload if is a FONT for easy css support. - } + foundFiles.add(fileEntry); } } } @@ -73,7 +114,7 @@ public class AssetLoader { if (!cls.isAnnotationPresent(FileExtension.class)) continue; FileExtension annotation = cls.getAnnotation(FileExtension.class); for (String ext : annotation.value()) { - registry.put(ext, file -> { + this.registry.put(ext, file -> { try { return cls.getConstructor(File.class).newInstance(file); } catch (Exception e) { diff --git a/framework/src/main/java/org/toop/framework/asset/AssetManager.java b/framework/src/main/java/org/toop/framework/asset/AssetManager.java index 620cbeb..e53aac0 100644 --- a/framework/src/main/java/org/toop/framework/asset/AssetManager.java +++ b/framework/src/main/java/org/toop/framework/asset/AssetManager.java @@ -33,7 +33,7 @@ public class AssetManager { return list; } - public Asset getById(String id) { + public static Asset getById(String id) { for (Asset asset : assets.values()) { if (asset.getId().toString().equals(id)) { return asset; @@ -42,15 +42,15 @@ public class AssetManager { return null; } - public Asset getByName(String name) { + public static Asset getByName(String name) { return assets.get(name); } - public Optional> findByName(String name) { + public static Optional> findByName(String name) { return Optional.ofNullable(assets.get(name)); } - public void addAsset(Asset asset) { + public static void addAsset(Asset asset) { assets.put(asset.getName(), asset); } diff --git a/framework/src/main/java/org/toop/framework/asset/events/AssetEvents.java b/framework/src/main/java/org/toop/framework/asset/events/AssetEvents.java new file mode 100644 index 0000000..faf8bfb --- /dev/null +++ b/framework/src/main/java/org/toop/framework/asset/events/AssetEvents.java @@ -0,0 +1,7 @@ +package org.toop.framework.asset.events; + +import org.toop.framework.eventbus.events.EventWithoutSnowflake; + +public class AssetEvents { + public record LoadingProgressUpdate(int hasLoadedAmount, int isLoadingAmount) implements EventWithoutSnowflake {} +}