Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[1.15-1.16] [FG4] Service files in mod


Recommended Posts

I doing my annotation processor which generate service file for current mod.

And I doing my mod which looking for such services in all mods in modpack.

Forge can provide location of mod, whether it is an jar or in-dev-folder.

The problem is getting actual location of service file.

For jar is trivially and work fine.

For in-dev-folder it's hard: File provided by Forge is not only one folder with mod files (look screenshot)

It's really problematic and now I'm stumped.

Any ideas?

Also I can consider other ways instead of services.

Related code:

https://github.com/ElegantNetworking/ElegantNetworking_1.16/blob/master/src/main/java/hohserg/elegant/networking/impl/Main.java#L18-L20

https://github.com/ElegantNetworking/ElegantNetworking_1.16/blob/master/build.gradle#L111

Debug screenshot:

image.thumb.png.fa870d62c6b9e37c8010a0c0137e9b51.png

Some note: for example I made mod which looking for services and mod within services in one bottle, and you can see it on screenshot, but it's irrelevant to the problem.

Edited by hohserg
Link to post
Share on other sites
  • hohserg changed the title to [1.15-1.16] [FG4] Service files in mod

If I use ServiceLoader, how it will work with multiple jars with identically named service files?

As I know, ServiceLoader resolves existence of multiple same service files by taking first.

It's also can resolve by ClassLoader, but I dont know, is Forge use separeted class loaders for each mod and is it be true it future.

Also, ServiceLoader gives instances of classes which specified in service, but I'm need to instances of Class<?> of these classes.

So, standard utility is not look as appropriate

Link to post
Share on other sites
32 minutes ago, hohserg said:

If I use ServiceLoader, how it will work with multiple jars with identically named service files?

It will give you all of them. 

32 minutes ago, hohserg said:

As I know, ServiceLoader resolves existence of multiple same service files by taking first.

This is not true. It lets you iterate through all of them.

33 minutes ago, hohserg said:

It's also can resolve by ClassLoader, but I dont know, is Forge use separeted class loaders for each mod and is it be true it future.

What?

33 minutes ago, hohserg said:

Also, ServiceLoader gives instances of classes which specified in service, but I'm need to instances of Class<?> of these classes.

Why? That is pretty bad API design. If you really do need a Class instance, make a service interface that provides that Class.

  • Like 1
Link to post
Share on other sites
Posted (edited)
52 minutes ago, diesieben07 said:

all of them

Well. How to determine which class does the mod relate to?

52 minutes ago, diesieben07 said:

make a service interface that provides that Class

Look excessive

Maybe it's a bad idea to call this thing as 'service'. Service is a most similar thing.

Generally thing which I need is a file with list of names of classes which been found(and generated) by my annotation processor and readable by my mod. And multiple mods in one modpack can contain such file

 

  

52 minutes ago, diesieben07 said:

What?

Sorry, it's my wrong understanding of ServiceLoader 

Edited by hohserg
Link to post
Share on other sites
51 minutes ago, hohserg said:

Generally thing which I need is a file with list of names of classes which been found(and generated) by my annotation processor

Okay. And you can't just use ClassLoader#getResources for this, because...?

Link to post
Share on other sites
52 minutes ago, hohserg said:

Well. How to determine which class does the mod relate to?

You cannot reliably do this. What you can do is try to find out by looking through ModList#getModFiles and check where the class came from.

But this is unreliable, which is why for example @EventBusSubscriber requires you to specify the ModID.

  • Like 1
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.