-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
246 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
platform/fabric/src/main/resources/META-INF/services/mcp.mobius.waila.api.IPluginDiscoverer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
mcp.mobius.waila.fabric.FabricPluginDiscoverer |
1 change: 0 additions & 1 deletion
1
platform/fabric/src/main/resources/META-INF/services/mcp.mobius.waila.plugin.PluginLoader
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
platform/forge/src/main/resources/META-INF/services/mcp.mobius.waila.api.IPluginDiscoverer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
mcp.mobius.waila.forge.ForgePluginDiscoverer |
1 change: 0 additions & 1 deletion
1
platform/forge/src/main/resources/META-INF/services/mcp.mobius.waila.plugin.PluginLoader
This file was deleted.
Oops, something went wrong.
31 changes: 31 additions & 0 deletions
31
platform/neo/src/main/java/mcp/mobius/waila/neo/NeoPluginDiscoverer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package mcp.mobius.waila.neo; | ||
|
||
import java.nio.file.Files; | ||
|
||
import mcp.mobius.waila.Waila; | ||
import mcp.mobius.waila.plugin.DefaultPluginDiscoverer; | ||
import net.minecraft.resources.ResourceLocation; | ||
import net.neoforged.fml.ModList; | ||
|
||
public class NeoPluginDiscoverer extends DefaultPluginDiscoverer { | ||
|
||
public static final ResourceLocation ID = Waila.id("neo"); | ||
|
||
@Override | ||
public ResourceLocation getDiscovererId() { | ||
return ID; | ||
} | ||
|
||
@Override | ||
public void discover(Candidates candidates) { | ||
for (var modFile : ModList.get().getModFiles()) { | ||
for (var file : PLUGIN_JSON_FILES) { | ||
var path = modFile.getFile().findResource(file); | ||
if (Files.exists(path)) { | ||
readPluginsJson(candidates, modFile.getMods().get(0).getModId(), path); | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
22 changes: 0 additions & 22 deletions
22
platform/neo/src/main/java/mcp/mobius/waila/neo/NeoPluginLoader.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
platform/neo/src/main/resources/META-INF/services/mcp.mobius.waila.api.IPluginDiscoverer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
mcp.mobius.waila.neo.NeoPluginDiscoverer |
1 change: 0 additions & 1 deletion
1
platform/neo/src/main/resources/META-INF/services/mcp.mobius.waila.plugin.PluginLoader
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package mcp.mobius.waila.api; | ||
|
||
import java.util.List; | ||
|
||
import net.minecraft.resources.ResourceLocation; | ||
import org.jetbrains.annotations.ApiStatus; | ||
|
||
/** | ||
* Dynamic plugin discoverer. | ||
* <p> | ||
* Implement this class as a service provider. | ||
*/ | ||
@ApiStatus.Experimental | ||
public interface IPluginDiscoverer { | ||
|
||
/** | ||
* Returns the discoverer ID. | ||
*/ | ||
ResourceLocation getDiscovererId(); | ||
|
||
/** | ||
* Discover plugin candidates. | ||
* <p> | ||
* Will only called once per game session. | ||
* | ||
* @param candidates the plugin candidates | ||
*/ | ||
void discover(Candidates candidates); | ||
|
||
@ApiStatus.NonExtendable | ||
interface Candidates { | ||
|
||
/** | ||
* Adds a plugin candidate. | ||
* | ||
* @param modId the mod origin | ||
* @param pluginId the plugin id | ||
* @param side the plugin environment side | ||
* @param requiredModIds the plugin dependencies | ||
* @param defaultEnabled whether the plugin is enabled by default or require end user to manually enable it | ||
* @param factory the instance factory, will only be called when the environment and dependencies match | ||
*/ | ||
void add(String modId, ResourceLocation pluginId, IPluginInfo.Side side, List<String> requiredModIds, boolean defaultEnabled, Factory factory); | ||
|
||
} | ||
|
||
interface Factory { | ||
|
||
IWailaPlugin get() throws Exception; | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
src/main/java/mcp/mobius/waila/plugin/DefaultPluginDiscoverer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package mcp.mobius.waila.plugin; | ||
|
||
import java.io.IOException; | ||
import java.io.Reader; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.stream.Collectors; | ||
|
||
import com.google.gson.JsonParser; | ||
import mcp.mobius.waila.Waila; | ||
import mcp.mobius.waila.api.IPluginDiscoverer; | ||
import mcp.mobius.waila.api.IPluginInfo; | ||
import mcp.mobius.waila.api.IWailaPlugin; | ||
import net.minecraft.resources.ResourceLocation; | ||
|
||
public abstract class DefaultPluginDiscoverer implements IPluginDiscoverer { | ||
|
||
public static final ResourceLocation LEGACY = Waila.id("legacy"); | ||
|
||
protected static final String[] PLUGIN_JSON_FILES = { | ||
"waila_plugins.json", | ||
"wthit_plugins.json" | ||
}; | ||
|
||
protected static final String KEY_INITIALIZER = "initializer"; | ||
protected static final String KEY_SIDE = "side"; | ||
protected static final String KEY_REQUIRED = "required"; | ||
protected static final String KEY_DEFAULT_ENABLED = "defaultEnabled"; | ||
protected static final Map<String, IPluginInfo.Side> SIDES = Map.of( | ||
"client", IPluginInfo.Side.CLIENT, | ||
"server", IPluginInfo.Side.SERVER, | ||
"both", IPluginInfo.Side.BOTH, | ||
"*", IPluginInfo.Side.BOTH | ||
); | ||
|
||
protected Factory factory(String initializer) { | ||
return () -> (IWailaPlugin) Class.forName(initializer).getConstructor().newInstance(); | ||
} | ||
|
||
protected void registerLegacy(String modId, String pluginId, IPluginInfo.Side side, List<String> requiredModIds, String initializer) { | ||
PluginInfo.register(LEGACY, modId, new ResourceLocation(pluginId), side, requiredModIds, true, factory(initializer)); | ||
} | ||
|
||
protected void readPluginsJson(Candidates candidates, String modId, Path path) { | ||
try (Reader reader = Files.newBufferedReader(path)) { | ||
var object = JsonParser.parseReader(reader).getAsJsonObject(); | ||
|
||
for (var pluginId : object.keySet()) { | ||
var plugin = object.getAsJsonObject(pluginId); | ||
|
||
var initializer = plugin.getAsJsonPrimitive(KEY_INITIALIZER).getAsString(); | ||
var side = plugin.has(KEY_SIDE) | ||
? Objects.requireNonNull(SIDES.get(plugin.get(KEY_SIDE).getAsString()), () -> readError(path) + ", invalid side, available: " + SIDES.keySet().stream().collect(Collectors.joining(", ", "[", "]"))) | ||
: IPluginInfo.Side.BOTH; | ||
|
||
List<String> required = new ArrayList<>(); | ||
if (plugin.has(KEY_REQUIRED)) { | ||
var array = plugin.getAsJsonArray(KEY_REQUIRED); | ||
for (var element : array) { | ||
var requiredModId = element.getAsString(); | ||
required.add(requiredModId); | ||
} | ||
} | ||
|
||
var defaultEnabled = !plugin.has(KEY_DEFAULT_ENABLED) || plugin.get(KEY_DEFAULT_ENABLED).getAsBoolean(); | ||
candidates.add(modId, new ResourceLocation(pluginId), side, required, defaultEnabled, factory(initializer)); | ||
} | ||
} catch (IOException e) { | ||
throw new RuntimeException(readError(path), e); | ||
} | ||
} | ||
|
||
private static String readError(Path path) { | ||
return "Failed to read [" + path + "]"; | ||
} | ||
|
||
} |
Oops, something went wrong.