From d661e0d4b5799241616c9499cc7e19a1272a31f8 Mon Sep 17 00:00:00 2001 From: Stef Date: Wed, 1 Oct 2025 00:06:46 +0200 Subject: [PATCH] Moved isLoaded to BaseResource. Loading and unloading will still be overridden if necessary. Added some subclass/super safe load and unload functions. --- .../asset/resources/BaseResource.java | 27 +++++++++++-------- .../framework/asset/resources/ImageAsset.java | 14 +++------- 2 files changed, 20 insertions(+), 21 deletions(-) diff --git a/framework/src/main/java/org/toop/framework/asset/resources/BaseResource.java b/framework/src/main/java/org/toop/framework/asset/resources/BaseResource.java index 601cd30..95ce7aa 100644 --- a/framework/src/main/java/org/toop/framework/asset/resources/BaseResource.java +++ b/framework/src/main/java/org/toop/framework/asset/resources/BaseResource.java @@ -16,33 +16,38 @@ public abstract class BaseResource { return this.isLoaded; } - public void load() throws FileNotFoundException{ + public void load() throws FileNotFoundException { + this.loadRawData(); + isLoaded = true; + } + + private void loadRawData() throws FileNotFoundException{ try { this.rawData = Files.readAllBytes(file.toPath()); - isLoaded = true; } catch (IOException e) { throw new RuntimeException(e); } } - public void unload() { + public void unload(){ + this.unloadRawData(); + isLoaded = false; + } + + private void unloadRawData() { this.rawData = null; - this.isLoaded = false; } public File getFile() { return this.file; } - public InputStream getInputStream() { + public InputStream getInputStream() throws FileNotFoundException { if (!isLoaded){ - try { - this.load(); - } catch (FileNotFoundException e) { - throw new RuntimeException(e); - } + // Manually load the data, makes sure it doesn't call subclass load() + loadRawData(); + isLoaded = true; } return new BufferedInputStream(new ByteArrayInputStream(this.rawData)); - } } diff --git a/framework/src/main/java/org/toop/framework/asset/resources/ImageAsset.java b/framework/src/main/java/org/toop/framework/asset/resources/ImageAsset.java index e732afc..e626551 100644 --- a/framework/src/main/java/org/toop/framework/asset/resources/ImageAsset.java +++ b/framework/src/main/java/org/toop/framework/asset/resources/ImageAsset.java @@ -6,7 +6,6 @@ import java.io.FileNotFoundException; public class ImageAsset extends BaseResource implements LoadableResource { private Image image; - private boolean isLoaded = false; public ImageAsset(final File file) throws FileNotFoundException { super(file); @@ -14,25 +13,20 @@ public class ImageAsset extends BaseResource implements LoadableResource { @Override public void load() throws FileNotFoundException { - if (!this.isLoaded) { + if (!this.isLoaded()) { + super.load(); // Make sure that base class (byte[]) is loaded this.image = new Image(this.getInputStream()); - this.isLoaded = true; } } @Override public void unload() { this.image = null; - this.isLoaded = false; - } - - @Override - public boolean isLoaded() { - return this.isLoaded; + super.unload(); } public Image getImage() { - if (!this.isLoaded) try { + if (!this.isLoaded()) try { load(); } catch (FileNotFoundException e) { throw new RuntimeException(e);