Made AssetManager be Thread safe.

This commit is contained in:
lieght
2025-09-30 23:42:56 +02:00
parent 7c970d9a4d
commit 21763851e8
2 changed files with 23 additions and 15 deletions

View File

@@ -2,15 +2,12 @@ package org.toop.framework.asset;
import org.toop.framework.asset.resources.*; import org.toop.framework.asset.resources.*;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import java.io.File;
import java.io.IOException;
import java.util.*; import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
public class AssetManager { public class AssetManager {
private static final AssetManager INSTANCE = new AssetManager(); private static final AssetManager INSTANCE = new AssetManager();
private static final Map<String, Asset<? extends BaseResource>> assets = new HashMap<>(); private static final Map<String, Asset<? extends BaseResource>> assets = new ConcurrentHashMap<>();
private AssetManager() {} private AssetManager() {}
@@ -18,13 +15,13 @@ public class AssetManager {
return INSTANCE; return INSTANCE;
} }
public static void loadAssets(AssetLoader loader) { public synchronized static void loadAssets(AssetLoader loader) {
for (var asset : loader.getAssets()) { for (var asset : loader.getAssets()) {
assets.put(asset.getName(), asset); assets.put(asset.getName(), asset);
} }
} }
public <T extends BaseResource> ArrayList<Asset<T>> getAllOfType(Class<T> type) { public static <T extends BaseResource> ArrayList<Asset<T>> getAllOfType(Class<T> type) {
ArrayList<Asset<T>> list = new ArrayList<>(); ArrayList<Asset<T>> list = new ArrayList<>();
for (Asset<? extends BaseResource> asset : assets.values()) { // <-- use .values() for (Asset<? extends BaseResource> asset : assets.values()) { // <-- use .values()
if (type.isInstance(asset.getResource())) { if (type.isInstance(asset.getResource())) {
@@ -36,13 +33,25 @@ public class AssetManager {
return list; return list;
} }
public static Asset<? extends BaseResource> getById(String guid) { public Asset<? extends BaseResource> getById(String id) {
return assets.get(guid); for (Asset<? extends BaseResource> asset : assets.values()) {
if (asset.getId().toString().equals(id)) {
return asset;
}
}
return null;
} }
public static Optional<Asset<? extends BaseResource>> findByName(String name) { public Asset<? extends BaseResource> getByName(String name) {
return assets.values().stream() return assets.get(name);
.filter(a -> a.getName().equals(name))
.findFirst();
} }
public Optional<Asset<? extends BaseResource>> findByName(String name) {
return Optional.ofNullable(assets.get(name));
}
public void addAsset(Asset<? extends BaseResource> asset) {
assets.put(asset.getName(), asset);
}
} }

View File

@@ -12,14 +12,13 @@ import java.util.*;
import javax.sound.sampled.*; import javax.sound.sampled.*;
public class SoundManager { public class SoundManager {
private final AssetManager asm = AssetManager.getInstance();
private final Map<Long, Clip> activeClips = new HashMap<>(); private final Map<Long, Clip> activeClips = new HashMap<>();
private final HashMap<String, AudioAsset> audioResources = new HashMap<>(); private final HashMap<String, AudioAsset> audioResources = new HashMap<>();
private final SnowflakeGenerator idGenerator = new SnowflakeGenerator(); // TODO: Don't create a new generator private final SnowflakeGenerator idGenerator = new SnowflakeGenerator(); // TODO: Don't create a new generator
public SoundManager() { public SoundManager() {
// Get all Audio Resources and add them to a list. // Get all Audio Resources and add them to a list.
for (Asset<AudioAsset> asset : asm.getAllOfType(AudioAsset.class)) { for (Asset<AudioAsset> asset : AssetManager.getAllOfType(AudioAsset.class)) {
try { try {
this.addAudioResource(asset); this.addAudioResource(asset);
} catch (IOException | LineUnavailableException | UnsupportedAudioFileException e) { } catch (IOException | LineUnavailableException | UnsupportedAudioFileException e) {