diff --git a/projects/core/src/main/java/dan200/computercraft/core/computer/ApiWrapper.java b/projects/core/src/main/java/dan200/computercraft/core/computer/ApiWrapper.java index 270f2c4e4..535cb6b8d 100644 --- a/projects/core/src/main/java/dan200/computercraft/core/computer/ApiWrapper.java +++ b/projects/core/src/main/java/dan200/computercraft/core/computer/ApiWrapper.java @@ -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; } } 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 3680ccba1..2ff466c02 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 @@ -61,7 +61,7 @@ final class ComputerExecutor { private final Computer computer; private final ComputerEnvironment computerEnvironment; private final MetricsObserver metrics; - private final List apis = new ArrayList<>(); + private final List apis = new ArrayList<>(); private final ComputerThread scheduler; private final MethodSupplier 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);