Moved loads to own class for better memory management. Added ability to add text- and localizationassets.

This commit is contained in:
lieght
2025-10-01 04:19:28 +02:00
parent f11ff0421b
commit 5e4db91750
11 changed files with 177 additions and 93 deletions

View File

@@ -13,29 +13,16 @@ import org.toop.framework.asset.resources.FileExtension;
public class AssetLoader {
private static AssetLoader INSTANCE;
private final File rootFolder;
private final List<Asset<? extends BaseResource>> assets = new CopyOnWriteArrayList<>();
private final Map<String, Function<File, ? extends BaseResource>> registry = new ConcurrentHashMap<>();
private AssetLoader(File rootFolder) {
this.rootFolder = rootFolder;
public AssetLoader(File rootFolder) {
autoRegisterResources();
fileSearcher(rootFolder);
}
public static synchronized void initialize(String rootFolderPath) {
if (INSTANCE == null) {
INSTANCE = new AssetLoader(new File(rootFolderPath));
}
}
public static AssetLoader getInstance() {
if (INSTANCE == null) {
throw new IllegalStateException("AssetLoader not initialized. Call initialize() first.");
}
return INSTANCE;
public AssetLoader(String rootFolder) {
this(new File(rootFolder));
}
public List<Asset<? extends BaseResource>> getAssets() {

View File

@@ -26,6 +26,26 @@ public class AudioAsset extends BaseResource implements LoadableResource {
// Generates a new audio stream from byte array
private AudioInputStream getAudioStream() throws UnsupportedAudioFileException, IOException {
return AudioSystem.getAudioInputStream(this.getInputStream());
return AudioSystem.getAudioInputStream(this.file);
}
@Override
public void load() {
try {
this.getAudioStream();
this.isLoaded = true;
} catch (UnsupportedAudioFileException | IOException e) {
throw new RuntimeException(e);
}
}
@Override
public void unload() {
this.isLoaded = false; // TODO?
}
@Override
public boolean isLoaded() {
return this.isLoaded;
}
}

View File

@@ -1,53 +1,18 @@
package org.toop.framework.asset.resources;
import java.io.*;
import java.nio.file.Files;
public abstract class BaseResource {
private byte[] rawData;
final File file;
private boolean isLoaded = false;
boolean isLoaded = false;
BaseResource(final File file) {
protected BaseResource(final File file) {
this.file = file;
}
public boolean isLoaded() {
return this.isLoaded;
}
public void load() throws FileNotFoundException {
this.loadRawData();
isLoaded = true;
}
private void loadRawData() throws FileNotFoundException{
try {
this.rawData = Files.readAllBytes(file.toPath());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public void unload(){
this.unloadRawData();
isLoaded = false;
}
private void unloadRawData() {
this.rawData = null;
}
public File getFile() {
return this.file;
}
public InputStream getInputStream() throws FileNotFoundException {
if (!isLoaded){
// Manually load the data, makes sure it doesn't call subclass load()
loadRawData();
isLoaded = true;
}
return new BufferedInputStream(new ByteArrayInputStream(this.rawData));
}
}

View File

@@ -1,28 +1,25 @@
package org.toop.framework.asset.resources;
import java.io.File;
import java.io.FileNotFoundException;
public class FontAsset extends BaseResource implements LoadableResource {
private boolean isLoaded = false;
public FontAsset(final File fontFile) {
super(fontFile);
}
@Override
public void load() throws FileNotFoundException {
public void load() {
this.isLoaded = true;
}
@Override
public void unload() {
this.isLoaded = false;
}
@Override
public boolean isLoaded() {
return false;
return this.isLoaded;
}
}

View File

@@ -2,9 +2,9 @@ package org.toop.framework.asset.resources;
import javafx.scene.image.Image;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileInputStream;
@FileExtension({"png"})
@FileExtension({"png", "jpg", "jpeg"})
public class ImageAsset extends BaseResource implements LoadableResource {
private Image image;
@@ -13,25 +13,33 @@ public class ImageAsset extends BaseResource implements LoadableResource {
}
@Override
public void load() throws FileNotFoundException {
if (!this.isLoaded()) {
super.load(); // Make sure that base class (byte[]) is loaded
this.image = new Image(this.getInputStream());
public void load() {
if (!this.isLoaded) {
try (FileInputStream fis = new FileInputStream(this.file)) {
this.image = new Image(fis);
this.isLoaded = true;
} catch (Exception e) {
throw new RuntimeException("Failed to load image: " + this.file, e);
}
}
}
@Override
public void unload() {
this.image = null;
super.unload();
this.isLoaded = false;
}
@Override
public boolean isLoaded() {
return this.isLoaded;
}
public Image getImage() {
if (!this.isLoaded()) try {
load();
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
if (!this.isLoaded) {
this.load();
return image;
}
return image;
return null;
}
}
}

View File

@@ -3,7 +3,7 @@ package org.toop.framework.asset.resources;
import java.io.FileNotFoundException;
public interface LoadableResource {
void load() throws FileNotFoundException;
void load();
void unload();
boolean isLoaded();
}

View File

@@ -0,0 +1,85 @@
package org.toop.framework.asset.resources;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.*;
@FileExtension({"properties"})
public class LocalizationAsset extends BaseResource implements LoadableResource {
private final Map<Locale, ResourceBundle> bundles = new HashMap<>();
private boolean isLoaded = false;
public LocalizationAsset(File file) {
super(file);
}
@Override
public void load() {
// Convention: file names like messages_en.properties, ui_de.properties, etc.
String baseName = getBaseName(getFile().getName());
// Scan the parent folder for all matching *.properties with same basename
File folder = getFile().getParentFile();
File[] files = folder.listFiles((dir, name) ->
name.startsWith(baseName) && name.endsWith(".properties"));
if (files != null) {
for (File f : files) {
Locale locale = extractLocale(f.getName(), baseName);
try (InputStreamReader reader =
new InputStreamReader(new FileInputStream(f), StandardCharsets.UTF_8)) {
this.bundles.put(locale, new PropertyResourceBundle(reader));
} catch (IOException e) {
throw new RuntimeException("Failed to load localization file: " + f, e);
}
}
}
this.isLoaded = true;
}
@Override
public void unload() {
this.bundles.clear();
this.isLoaded = false;
}
@Override
public boolean isLoaded() {
return this.isLoaded;
}
public String getString(String key, Locale locale) {
ResourceBundle bundle = this.bundles.get(locale);
if (bundle == null) throw new MissingResourceException(
"No bundle for locale: " + locale, getClass().getName(), key);
return bundle.getString(key);
}
public boolean hasLocale(Locale locale) {
return this.bundles.containsKey(locale);
}
public Set<Locale> getAvailableLocales() {
return Collections.unmodifiableSet(this.bundles.keySet());
}
private String getBaseName(String fileName) {
int underscoreIndex = fileName.indexOf('_');
int dotIndex = fileName.lastIndexOf('.');
if (underscoreIndex > 0) {
return fileName.substring(0, underscoreIndex);
}
return fileName.substring(0, dotIndex);
}
private Locale extractLocale(String fileName, String baseName) {
int underscoreIndex = fileName.indexOf('_');
int dotIndex = fileName.lastIndexOf('.');
if (underscoreIndex > 0 && dotIndex > underscoreIndex) {
String localePart = fileName.substring(underscoreIndex + 1, dotIndex);
return Locale.forLanguageTag(localePart.replace('_', '-'));
}
return Locale.getDefault(); // fallback
}
}

View File

@@ -1,26 +1,41 @@
package org.toop.framework.asset.resources;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
@FileExtension({"txt", "json", "xml"})
public class TextAsset extends BaseResource implements LoadableResource {
private String content;
TextAsset(final File file) {
public TextAsset(File file) {
super(file);
}
@Override
public void load() throws FileNotFoundException {
public void load() {
try {
byte[] bytes = Files.readAllBytes(getFile().toPath());
this.content = new String(bytes, StandardCharsets.UTF_8);
this.isLoaded = true;
} catch (IOException e) {
throw new RuntimeException("Failed to load text asset: " + getFile(), e);
}
}
@Override
public void unload() {
this.content = null;
this.isLoaded = false;
}
@Override
public boolean isLoaded() {
return false;
return this.isLoaded;
}
}
public String getContent() {
return this.content;
}
}