diff --git a/projects/common/src/main/java/dan200/computercraft/impl/AbstractComputerCraftAPI.java b/projects/common/src/main/java/dan200/computercraft/impl/AbstractComputerCraftAPI.java index 45e4ed707..b6ba303f8 100644 --- a/projects/common/src/main/java/dan200/computercraft/impl/AbstractComputerCraftAPI.java +++ b/projects/common/src/main/java/dan200/computercraft/impl/AbstractComputerCraftAPI.java @@ -19,7 +19,6 @@ import dan200.computercraft.api.pocket.PocketUpgradeSerialiser; import dan200.computercraft.api.redstone.BundledRedstoneProvider; import dan200.computercraft.api.turtle.TurtleRefuelHandler; import dan200.computercraft.api.turtle.TurtleUpgradeSerialiser; -import dan200.computercraft.core.apis.ApiFactories; import dan200.computercraft.core.asm.GenericMethod; import dan200.computercraft.core.filesystem.WritableFileMount; import dan200.computercraft.impl.detail.DetailRegistryImpl; diff --git a/projects/core/src/main/java/dan200/computercraft/core/apis/ApiFactories.java b/projects/common/src/main/java/dan200/computercraft/impl/ApiFactories.java similarity index 88% rename from projects/core/src/main/java/dan200/computercraft/core/apis/ApiFactories.java rename to projects/common/src/main/java/dan200/computercraft/impl/ApiFactories.java index 232419aa1..ef96738ef 100644 --- a/projects/core/src/main/java/dan200/computercraft/core/apis/ApiFactories.java +++ b/projects/common/src/main/java/dan200/computercraft/impl/ApiFactories.java @@ -2,7 +2,7 @@ // // SPDX-License-Identifier: MPL-2.0 -package dan200.computercraft.core.apis; +package dan200.computercraft.impl; import dan200.computercraft.api.lua.ILuaAPIFactory; @@ -23,7 +23,7 @@ public final class ApiFactories { factories.add(factory); } - public static Iterable getAll() { + public static Collection getAll() { return factoriesView; } } diff --git a/projects/common/src/main/java/dan200/computercraft/shared/computer/core/ServerContext.java b/projects/common/src/main/java/dan200/computercraft/shared/computer/core/ServerContext.java index 4172ad1d9..1ddff50d2 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/computer/core/ServerContext.java +++ b/projects/common/src/main/java/dan200/computercraft/shared/computer/core/ServerContext.java @@ -15,6 +15,7 @@ import dan200.computercraft.core.computer.mainthread.MainThreadConfig; import dan200.computercraft.core.lua.CobaltLuaMachine; import dan200.computercraft.core.lua.ILuaMachine; import dan200.computercraft.impl.AbstractComputerCraftAPI; +import dan200.computercraft.impl.ApiFactories; import dan200.computercraft.shared.CommonHooks; import dan200.computercraft.shared.computer.metrics.GlobalMetrics; import dan200.computercraft.shared.config.ConfigSpec; @@ -70,6 +71,7 @@ public final class ServerContext { .computerThreads(ConfigSpec.computerThreads.get()) .mainThreadScheduler(mainThread) .luaFactory(luaMachine) + .apiFactories(ApiFactories.getAll()) .build(); idAssigner = new IDAssigner(storageDir.resolve("ids.json")); } diff --git a/projects/core/src/main/java/dan200/computercraft/core/ComputerContext.java b/projects/core/src/main/java/dan200/computercraft/core/ComputerContext.java index 94a01997a..ce0270687 100644 --- a/projects/core/src/main/java/dan200/computercraft/core/ComputerContext.java +++ b/projects/core/src/main/java/dan200/computercraft/core/ComputerContext.java @@ -4,6 +4,7 @@ package dan200.computercraft.core; +import dan200.computercraft.api.lua.ILuaAPIFactory; import dan200.computercraft.core.computer.ComputerThread; import dan200.computercraft.core.computer.GlobalEnvironment; import dan200.computercraft.core.computer.mainthread.MainThreadScheduler; @@ -13,6 +14,8 @@ import dan200.computercraft.core.lua.ILuaMachine; import javax.annotation.CheckReturnValue; import javax.annotation.Nullable; +import java.util.Collection; +import java.util.List; import java.util.Objects; import java.util.concurrent.TimeUnit; @@ -24,15 +27,18 @@ public final class ComputerContext { private final ComputerThread computerScheduler; private final MainThreadScheduler mainThreadScheduler; private final ILuaMachine.Factory luaFactory; + private final List apiFactories; ComputerContext( GlobalEnvironment globalEnvironment, ComputerThread computerScheduler, - MainThreadScheduler mainThreadScheduler, ILuaMachine.Factory luaFactory + MainThreadScheduler mainThreadScheduler, ILuaMachine.Factory luaFactory, + List apiFactories ) { this.globalEnvironment = globalEnvironment; this.computerScheduler = computerScheduler; this.mainThreadScheduler = mainThreadScheduler; this.luaFactory = luaFactory; + this.apiFactories = apiFactories; } /** @@ -72,6 +78,15 @@ public final class ComputerContext { return luaFactory; } + /** + * Additional APIs to inject into each computer. + * + * @return All available API factories. + */ + public List apiFactories() { + return apiFactories; + } + /** * Close the current {@link ComputerContext}, disposing of any resources inside. * @@ -119,6 +134,7 @@ public final class ComputerContext { private int threads = 1; private @Nullable MainThreadScheduler mainThreadScheduler; private @Nullable ILuaMachine.Factory luaFactory; + private @Nullable List apiFactories; Builder(GlobalEnvironment environment) { this.environment = environment; @@ -165,6 +181,20 @@ public final class ComputerContext { return this; } + /** + * Set the additional {@linkplain ILuaAPIFactory APIs} to add to each computer. + * + * @param apis A list of API factories. + * @return {@code this}, for chaining + * @see ComputerContext#apiFactories() + */ + public Builder apiFactories(Collection apis) { + Objects.requireNonNull(apis); + if (apiFactories != null) throw new IllegalStateException("Main-thread scheduler already specified"); + apiFactories = List.copyOf(apis); + return this; + } + /** * Create a new {@link ComputerContext}. * @@ -175,7 +205,8 @@ public final class ComputerContext { environment, new ComputerThread(threads), mainThreadScheduler == null ? new NoWorkMainThreadScheduler() : mainThreadScheduler, - luaFactory == null ? CobaltLuaMachine::new : luaFactory + luaFactory == null ? CobaltLuaMachine::new : luaFactory, + apiFactories == null ? List.of() : apiFactories ); } } diff --git a/projects/core/src/main/java/dan200/computercraft/core/computer/ComputerExecutor.java b/projects/core/src/main/java/dan200/computercraft/core/computer/ComputerExecutor.java index 36220801e..4c7015502 100644 --- a/projects/core/src/main/java/dan200/computercraft/core/computer/ComputerExecutor.java +++ b/projects/core/src/main/java/dan200/computercraft/core/computer/ComputerExecutor.java @@ -181,7 +181,7 @@ final class ComputerExecutor { if (CoreConfig.httpEnabled) apis.add(new HTTPAPI(environment)); // Load in the externally registered APIs. - for (var factory : ApiFactories.getAll()) { + for (var factory : context.apiFactories()) { var system = new ComputerSystem(environment); var api = factory.create(system); if (api != null) apis.add(new ApiWrapper(api, system));