Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AssetLoader doesn't have access to assets folder in a modular project #644

Closed
jperedadnr opened this issue Jul 8, 2019 · 15 comments
Closed

Comments

@jperedadnr
Copy link

When the project is modular, since AssetLoader is in a different module, it doesn't have access to the assets/sound or assets/texture resources folders.

Fatal exception occurred: java.lang.IllegalArgumentException : Failed to load asset: brick.png. Cause: Asset "/assets/textures/brick.png" was not found!
E: com.almasb.fxgl.all/com.almasb.fxgl.app.AssetLoader.loadFailed(AssetLoader.kt:628)
E: com.almasb.fxgl.all/com.almasb.fxgl.app.AssetLoader.loadTexture(AssetLoader.kt:158)
E: com.almasb.fxgl.all/com.almasb.fxgl.dsl.FXGL$Companion.texture(FXGL.kt:203)
E: com.almasb.fxgl.all/com.almasb.fxgl.dsl.EntityBuilder.view(EntityBuilder.kt:96)
E: hellofx/org.openjfx.BasicGameApp.initGame(BasicGameApp.java:78)

which is caused by:
GameApplication.FXGLApplication.app?.javaClass?.getResource(name) being null, in `AssetLoader::getURL.

There are a few options to solve this (using getClassLoader().getResource() should do it, and it is the simplest way. Other options involve something more complex like ModuleLayer.boot().findModule(moduleName)...)

@AlmasB
Copy link
Owner

AlmasB commented Jul 8, 2019 via email

@jperedadnr
Copy link
Author

No, if you do:

    opens assets.textures;
    ```
you get:
```Error occurred during initialization of boot layer
java.lang.module.FindException: Error reading module: /.../hellofx/build/classes/java/main
Caused by: java.lang.module.InvalidModuleDescriptorException: Package assets.textures not found in module

@AlmasB
Copy link
Owner

AlmasB commented Jul 8, 2019 via email

@jperedadnr
Copy link
Author

Yes, that works. But I wouldn't do it...

@AlmasB
Copy link
Owner

AlmasB commented Jul 8, 2019 via email

@AlmasB
Copy link
Owner

AlmasB commented Jul 8, 2019

GameApplication.FXGLApplication.app?.javaClass?.classLoader?.getResource(name) also fails, is this the right class loader?

@jperedadnr
Copy link
Author

Could you do a quick check with GameApplication.FXGLApplication.app?.javaClass?.classLoader?.getResourceAsStream(name) ?

@AlmasB
Copy link
Owner

AlmasB commented Jul 8, 2019

Sure

@AlmasB
Copy link
Owner

AlmasB commented Jul 8, 2019

Also fails, I'll see if there is anything on the web related to module API access

@jperedadnr
Copy link
Author

We use that from Gluon Mobile to get access to some configuration files in the user's project (at the root of the resources folder), something like:

Configuration.class.getClassLoader().getResourceAsStream("config.json");

@AlmasB
Copy link
Owner

AlmasB commented Jul 8, 2019

Configuration.class is Gluon Mobile's class I presume? Ok ...

@AlmasB
Copy link
Owner

AlmasB commented Jul 8, 2019

Right, I've got something. I've found one approach, I don't know if it's the only one. It appears that to open a resource, it must be placed in the same package that is open to a different module, e.g.:

module samples.main {
requires com.almasb.fxgl.all;

// this is for reflection
exports basics to com.almasb.fxgl.core;

// this is for assets
opens basics to com.almasb.fxgl.all;

}

So brick.png must be under basics package in some form: basics/ ... / brick.png

The problem with this is the unified assets directory architecture is ruined since everything needs to be under their respective packages for this to work

@AlmasB
Copy link
Owner

AlmasB commented Jul 8, 2019

In any case, were you planning to use fxgl to test Gluon client plugin? Because I haven't yet merged changes that allow compile and run to succeed

@jperedadnr
Copy link
Author

Following that approach, you could always add a dummy class to assets/textures, so that package exists and can be opened from the user's module descriptor.

But still the user has to open it.

It should be better to have a solution that doesn't require fixed package names for resources nor opening them from the user's module descriptor.

About the Client plugin: No, I wasn't (I noticed this issue while I was checking this question https://stackoverflow.com/questions/56931096/fxgl-noclassdeffounderror-when-calling-fxgl-playdrop-wav)

@AlmasB
Copy link
Owner

AlmasB commented Jul 8, 2019

I think the pragmatic workaround of declaring the module as open will have to do. I agree this is not ideal, but I cannot propose anything better at this stage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants