mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-10-25 19:07:39 +00:00
Make the list of API factories per-ComputerContext
The registry is still a singleton inside the Minecraft code, but this makes the core a little cleaner.
This commit is contained in:
@@ -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<ILuaAPIFactory> apiFactories;
|
||||
|
||||
ComputerContext(
|
||||
GlobalEnvironment globalEnvironment, ComputerThread computerScheduler,
|
||||
MainThreadScheduler mainThreadScheduler, ILuaMachine.Factory luaFactory
|
||||
MainThreadScheduler mainThreadScheduler, ILuaMachine.Factory luaFactory,
|
||||
List<ILuaAPIFactory> 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<ILuaAPIFactory> 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<ILuaAPIFactory> 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<ILuaAPIFactory> 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
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,29 +0,0 @@
|
||||
// SPDX-FileCopyrightText: 2018 The CC: Tweaked Developers
|
||||
//
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
package dan200.computercraft.core.apis;
|
||||
|
||||
import dan200.computercraft.api.lua.ILuaAPIFactory;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Objects;
|
||||
|
||||
public final class ApiFactories {
|
||||
private ApiFactories() {
|
||||
}
|
||||
|
||||
private static final Collection<ILuaAPIFactory> factories = new LinkedHashSet<>();
|
||||
private static final Collection<ILuaAPIFactory> factoriesView = Collections.unmodifiableCollection(factories);
|
||||
|
||||
public static synchronized void register(ILuaAPIFactory factory) {
|
||||
Objects.requireNonNull(factory, "provider cannot be null");
|
||||
factories.add(factory);
|
||||
}
|
||||
|
||||
public static Iterable<ILuaAPIFactory> getAll() {
|
||||
return factoriesView;
|
||||
}
|
||||
}
|
||||
@@ -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));
|
||||
|
||||
Reference in New Issue
Block a user