mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-01-24 07:56:54 +00:00
Clean up ApiWrapper a little
We now wrap every Api with an ApiWrapper, rather than making it pretend to be an ILuaApi.
This commit is contained in:
parent
55510c42db
commit
43744b0e85
@ -6,40 +6,34 @@ package dan200.computercraft.core.computer;
|
||||
|
||||
import dan200.computercraft.api.lua.ILuaAPI;
|
||||
|
||||
/**
|
||||
* A wrapper for {@link ILuaAPI}s which cleans up after a {@link ComputerSystem} when the computer is shutdown.
|
||||
*/
|
||||
final class ApiWrapper implements ILuaAPI {
|
||||
private final ILuaAPI delegate;
|
||||
private final ComputerSystem system;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
ApiWrapper(ILuaAPI delegate, ComputerSystem system) {
|
||||
this.delegate = delegate;
|
||||
/**
|
||||
* A wrapper for {@link ILuaAPI}s which optionally manages the lifecycle of a {@link ComputerSystem}.
|
||||
*/
|
||||
final class ApiWrapper {
|
||||
private final ILuaAPI api;
|
||||
private final @Nullable ComputerSystem system;
|
||||
|
||||
ApiWrapper(ILuaAPI api, @Nullable ComputerSystem system) {
|
||||
this.api = api;
|
||||
this.system = system;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getNames() {
|
||||
return delegate.getNames();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void startup() {
|
||||
delegate.startup();
|
||||
api.startup();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update() {
|
||||
delegate.update();
|
||||
api.update();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void shutdown() {
|
||||
delegate.shutdown();
|
||||
system.unmountAll();
|
||||
api.shutdown();
|
||||
if (system != null) system.unmountAll();
|
||||
}
|
||||
|
||||
public ILuaAPI getDelegate() {
|
||||
return delegate;
|
||||
public ILuaAPI api() {
|
||||
return api;
|
||||
}
|
||||
}
|
||||
|
@ -61,7 +61,7 @@ final class ComputerExecutor {
|
||||
private final Computer computer;
|
||||
private final ComputerEnvironment computerEnvironment;
|
||||
private final MetricsObserver metrics;
|
||||
private final List<ILuaAPI> apis = new ArrayList<>();
|
||||
private final List<ApiWrapper> apis = new ArrayList<>();
|
||||
private final ComputerThread scheduler;
|
||||
private final MethodSupplier<LuaMethod> luaMethods;
|
||||
final TimeoutState timeout;
|
||||
@ -177,12 +177,12 @@ final class ComputerExecutor {
|
||||
var environment = computer.getEnvironment();
|
||||
|
||||
// Add all default APIs to the loaded list.
|
||||
apis.add(new TermAPI(environment));
|
||||
apis.add(new RedstoneAPI(environment));
|
||||
apis.add(new FSAPI(environment));
|
||||
apis.add(new PeripheralAPI(environment, context.peripheralMethods()));
|
||||
apis.add(new OSAPI(environment));
|
||||
if (CoreConfig.httpEnabled) apis.add(new HTTPAPI(environment));
|
||||
addApi(new TermAPI(environment));
|
||||
addApi(new RedstoneAPI(environment));
|
||||
addApi(new FSAPI(environment));
|
||||
addApi(new PeripheralAPI(environment, context.peripheralMethods()));
|
||||
addApi(new OSAPI(environment));
|
||||
if (CoreConfig.httpEnabled) addApi(new HTTPAPI(environment));
|
||||
|
||||
// Load in the externally registered APIs.
|
||||
for (var factory : context.apiFactories()) {
|
||||
@ -207,7 +207,7 @@ final class ComputerExecutor {
|
||||
}
|
||||
|
||||
void addApi(ILuaAPI api) {
|
||||
apis.add(api);
|
||||
apis.add(new ApiWrapper(api, null));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -385,7 +385,7 @@ final class ComputerExecutor {
|
||||
try (var bios = biosStream) {
|
||||
return luaFactory.create(new MachineEnvironment(
|
||||
new LuaContext(computer), metrics, timeout,
|
||||
() -> apis.stream().map(api -> api instanceof ApiWrapper wrapper ? wrapper.getDelegate() : api).iterator(),
|
||||
() -> apis.stream().map(ApiWrapper::api).iterator(),
|
||||
luaMethods,
|
||||
computer.getGlobalEnvironment().getHostString()
|
||||
), bios);
|
||||
|
Loading…
Reference in New Issue
Block a user