Fixed unknown names, Assetmanager now has initializeloader

This commit is contained in:
lieght
2025-09-30 23:17:32 +02:00
parent cf9c281992
commit d78fd1b606
14 changed files with 98 additions and 136 deletions

2
.idea/misc.xml generated
View File

@@ -13,7 +13,7 @@
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_25" default="true" project-jdk-name="openjdk-25" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_X" default="true" project-jdk-name="openjdk-25" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

View File

@@ -1,7 +1,8 @@
package org.toop;
import org.toop.app.gui.LocalServerSelector;
import org.toop.framework.assets.AssetManager;
import org.toop.framework.asset.AssetLoader;
import org.toop.framework.asset.AssetManager;
import org.toop.framework.audio.SoundManager;
import org.toop.framework.audio.events.AudioEvents;
import org.toop.framework.eventbus.EventFlow;
@@ -15,9 +16,10 @@ import java.nio.file.NotDirectoryException;
public class Main {
static void main(String[] args) throws IOException, UnsupportedAudioFileException, LineUnavailableException, InterruptedException {
var a = new AssetManager(new File("app/src/main/resources/assets"));
AssetManager.initializeLoader(new File("app/src/main/resources/assets"));
var b = new NetworkingClientManager();
var c = new SoundManager(a);
var c = new SoundManager();
new EventFlow().addPostEvent(new AudioEvents.PlayAudio("mainmenu.wav", true)).asyncPostEvent();
new EventFlow().addPostEvent(new AudioEvents.PlayAudio("sadtrombone.wav", true)).asyncPostEvent();
@@ -31,5 +33,6 @@ public class Main {
javax.swing.SwingUtilities.invokeLater(LocalServerSelector::new);
}
private static void initSystems() throws NetworkingInitializationException, NotDirectoryException {}
private static void initSystems() throws NetworkingInitializationException, NotDirectoryException {
}
}

View File

@@ -1,7 +1,7 @@
package org.toop.framework.assets;
package org.toop.framework.asset;
import org.toop.framework.SnowflakeGenerator;
import org.toop.framework.assets.resources.BaseResource;
import org.toop.framework.asset.resources.BaseResource;
public class Asset <T extends BaseResource> {
private final Long id;

View File

@@ -1,8 +1,8 @@
package org.toop.framework.assets;
package org.toop.framework.asset;
import org.toop.framework.assets.resources.AudioAsset;
import org.toop.framework.assets.resources.BaseResource;
import org.toop.framework.assets.resources.ImageAsset;
import org.toop.framework.asset.resources.AudioAsset;
import org.toop.framework.asset.resources.BaseResource;
import org.toop.framework.asset.resources.ImageAsset;
import java.io.File;
import java.io.FileNotFoundException;

View File

@@ -1,10 +1,12 @@
package org.toop.framework.assets;
package org.toop.framework.asset;
import org.toop.framework.assets.resources.*;
import org.toop.framework.asset.resources.*;
import java.io.File;
import java.util.*;
public class AssetManager {
private static AssetLoader loader = null;
private static final AssetManager INSTANCE = new AssetManager();
private static final Map<String, Asset<? extends BaseResource>> assets = new HashMap<>();
@@ -14,6 +16,12 @@ public class AssetManager {
return INSTANCE;
}
public static void initializeLoader(File rootFolder) {
if (loader == null) {
loader = new AssetLoader(rootFolder);
}
}
public <T extends BaseResource> ArrayList<Asset<T>> getAllOfType(Class<T> type) {
ArrayList<Asset<T>> list = new ArrayList<>();
for (Asset<? extends BaseResource> asset : assets.values()) { // <-- use .values()

View File

@@ -1,4 +1,4 @@
package org.toop.framework.assets.resources;
package org.toop.framework.asset.resources;
import javax.sound.sampled.*;
import java.io.*;

View File

@@ -0,0 +1,27 @@
package org.toop.framework.asset.resources;
import java.io.*;
public abstract class BaseResource {
final InputStream stream;
final File file;
BaseResource(final File file) {
this.file = file;
try {
this.stream = new BufferedInputStream(new FileInputStream(file));
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
}
public InputStream getInputStream() {
return this.stream;
}
public File getFile() {
return this.file;
}
}

View File

@@ -1,4 +1,4 @@
package org.toop.framework.assets.resources;
package org.toop.framework.asset.resources;
import java.io.File;
import java.io.FileNotFoundException;

View File

@@ -1,4 +1,4 @@
package org.toop.framework.assets.resources;
package org.toop.framework.asset.resources;
import javafx.scene.image.Image;
import java.io.File;

View File

@@ -1,4 +1,4 @@
package org.toop.framework.assets.resources;
package org.toop.framework.asset.resources;
import java.io.FileNotFoundException;

View File

@@ -1,14 +1,7 @@
package org.toop.framework.assets.resources;
package org.toop.framework.asset.resources;
import org.w3c.dom.Text;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
public class TextAsset extends BaseResource implements LoadableResource {

View File

@@ -1,38 +0,0 @@
package org.toop.framework.assets.resources;
import java.io.*;
import java.nio.file.Files;
public abstract class BaseResource {
final InputStream stream;
final File file;
public abstract class Resource {
final private byte[] rawData;
final private File file;
BaseResource(final File file) {
Resource(final File file) throws RuntimeException {
this.file = file;
try {
this.rawData = Files.readAllBytes(file.toPath());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public InputStream getInputStream() {
return this.stream;
public Resource load() {
return this;
}
public InputStream getStream() {
return new BufferedInputStream(new ByteArrayInputStream(this.rawData));
}
public File getFile() {
return this.file;
}
}

View File

@@ -1,27 +0,0 @@
package org.toop.framework.assets.resources;
import javafx.scene.image.Image;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
public class ImageResource extends Resource {
private Image image = null;
public ImageResource(File imageFile) {
super(imageFile);
}
public Image getImage() {
return this.image;
}
@Override
public Resource load() {
this.image = new Image(this.getStream());
return this;
}
}

View File

@@ -1,9 +1,9 @@
package org.toop.framework.audio;
import org.toop.framework.SnowflakeGenerator;
import org.toop.framework.assets.Asset;
import org.toop.framework.assets.AssetManager;
import org.toop.framework.assets.resources.AudioResource;
import org.toop.framework.asset.Asset;
import org.toop.framework.asset.AssetManager;
import org.toop.framework.asset.resources.AudioAsset;
import org.toop.framework.audio.events.AudioEvents;
import org.toop.framework.eventbus.EventFlow;
@@ -12,15 +12,16 @@ import java.util.*;
import javax.sound.sampled.*;
public class SoundManager {
private final AssetManager asm = AssetManager.getInstance();
private final Map<Long, Clip> activeClips = new HashMap<>();
private final HashMap<String, AudioResource> audioResources = new HashMap<>();
private final HashMap<String, AudioAsset> audioResources = new HashMap<>();
private final SnowflakeGenerator idGenerator = new SnowflakeGenerator(); // TODO: Don't create a new generator
public SoundManager(AssetManager asm) {
public SoundManager() {
// Get all Audio Resources and add them to a list.
for (Asset<AudioResource> resource : asm.getAllResourceOfType(AudioResource.class).values()) {
for (Asset<AudioAsset> asset : asm.getAllOfType(AudioAsset.class)) {
try {
this.addAudioResource(resource);
this.addAudioResource(asset);
} catch (IOException | LineUnavailableException | UnsupportedAudioFileException e) {
throw new RuntimeException(e);
}
@@ -38,52 +39,47 @@ public class SoundManager {
this.stopSound(event.clipId());
}
private void addAudioResource(Asset<AudioResource> audioAsset)
private void addAudioResource(Asset<AudioAsset> audioAsset)
throws IOException, UnsupportedAudioFileException, LineUnavailableException {
AudioResource audioResource = audioAsset.getResource();
this.audioResources.put(audioAsset.getName(), audioResource);
this.audioResources.put(audioAsset.getName(), audioAsset.getResource());
}
private long playSound(String audioFileName, boolean loop) {
try {
AudioResource resource = audioResources.get(audioFileName);
AudioAsset asset = audioResources.get(audioFileName);
// Return -1 which indicates resource wasn't available
if (resource == null){
return -1;
}
// Get a new clip from resource
Clip clip = resource.getNewClip();
// If supposed to loop make it loop, else just start it once
if (loop){
clip.loop(Clip.LOOP_CONTINUOUSLY);
}
else {
clip.start();
}
// Generate id for clip
long clipId = idGenerator.nextId();
// store it so we can stop it later
activeClips.put(clipId, clip); // TODO: Do on snowflake for specific sound to stop
// remove when finished (only for non-looping sounds)
clip.addLineListener(event -> {
if (event.getType() == LineEvent.Type.STOP && !clip.isRunning()) {
activeClips.remove(clipId);
clip.close();
}
});
// Return id so it can be stopped
return clipId;
} catch (LineUnavailableException | IOException | UnsupportedAudioFileException e) {
throw new RuntimeException(e);
// Return -1 which indicates resource wasn't available
if (asset == null){
return -1;
}
// Get a new clip from resource
Clip clip = asset.getClip();
// If supposed to loop make it loop, else just start it once
if (loop) {
clip.loop(Clip.LOOP_CONTINUOUSLY);
}
else {
clip.start();
}
// Generate id for clip
long clipId = idGenerator.nextId();
// store it so we can stop it later
activeClips.put(clipId, clip); // TODO: Do on snowflake for specific sound to stop
// remove when finished (only for non-looping sounds)
clip.addLineListener(event -> {
if (event.getType() == LineEvent.Type.STOP && !clip.isRunning()) {
activeClips.remove(clipId);
clip.close();
}
});
// Return id so it can be stopped
return clipId;
}
public void stopSound(long clipId) {