mirror of
https://github.com/2OOP/pism.git
synced 2026-02-04 10:54:51 +00:00
Made AssetManager be Thread safe.
This commit is contained in:
@@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -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) {
|
||||||
|
|||||||
Reference in New Issue
Block a user