From 352af7d37add6539dd870cfd73dfc4d36c279ead Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20N=C3=BCcke?= Date: Sun, 13 Feb 2022 16:12:32 +0100 Subject: [PATCH] Move dispose method to parent interface. --- .../li/cil/oc2/api/bus/device/Device.java | 8 +++++ .../cil/oc2/api/bus/device/rpc/RPCDevice.java | 29 ++++++------------- .../cil/oc2/api/bus/device/vm/VMDevice.java | 28 +++++++----------- 3 files changed, 28 insertions(+), 37 deletions(-) diff --git a/src/main/java/li/cil/oc2/api/bus/device/Device.java b/src/main/java/li/cil/oc2/api/bus/device/Device.java index 6f692bc9..6a6b699b 100644 --- a/src/main/java/li/cil/oc2/api/bus/device/Device.java +++ b/src/main/java/li/cil/oc2/api/bus/device/Device.java @@ -22,6 +22,14 @@ import net.minecraftforge.common.util.INBTSerializable; * devices can be detected. */ public interface Device extends INBTSerializable { + /** + * Called to dispose this device. + *

+ * Called when the connected virtual machine stops or the device is removed from a {@link DeviceBus}. + */ + default void dispose() { + } + /** * Called to serialize this device into its container's persistent storage. * diff --git a/src/main/java/li/cil/oc2/api/bus/device/rpc/RPCDevice.java b/src/main/java/li/cil/oc2/api/bus/device/rpc/RPCDevice.java index 3a828c01..68942d14 100644 --- a/src/main/java/li/cil/oc2/api/bus/device/rpc/RPCDevice.java +++ b/src/main/java/li/cil/oc2/api/bus/device/rpc/RPCDevice.java @@ -2,6 +2,7 @@ package li.cil.oc2.api.bus.device.rpc; +import li.cil.oc2.api.bus.DeviceBus; import li.cil.oc2.api.bus.device.Device; import li.cil.oc2.api.bus.device.object.ObjectDevice; @@ -74,35 +75,23 @@ public interface RPCDevice extends Device { /** * Called to start this device. *

- * This is called when the connected virtual machine starts, or when the device is added to an already running - * virtual machine. + * This is called when the connected virtual machine starts, or when the device + * is added to a {@link DeviceBus} with a currently running virtual machine. */ default void mount() { } /** - * Called to stop this device. + * Called to pause this device. *

- * Called when the connected virtual machine stops, the device is removed from a currently running - * virtual machine, or the connected virtual machine is suspended (chunk unload/server stopped/...). + * Called when the connected virtual machine is suspended (chunk unload/server stopped/...). + *

+ * Also called when the connected virtual machine stops or the device is removed from a + * {@link DeviceBus} with a currently running virtual machine. In this case, {@link #dispose()} + * will be called after this method returns. *

* If {@link #mount()} was called, this is guaranteed to be called. */ default void unmount() { } - - /** - * Called to dispose this device. - *

- * Called when the connected virtual machine stops or the device is removed from a currently running - * virtual machine. - *

- * Will only be called on unmounted devices (i.e. will always be called after {@link #unmount()} if - * {@link #mount()} was called before). May be called without intermediary {@link #mount()} calls, e.g. - * virtual machine stops, then device is disconnected from the virtual machine. - *

- * Intended for releasing persistent unmanaged resources. - */ - default void dispose() { - } } diff --git a/src/main/java/li/cil/oc2/api/bus/device/vm/VMDevice.java b/src/main/java/li/cil/oc2/api/bus/device/vm/VMDevice.java index 845644a8..a6574819 100644 --- a/src/main/java/li/cil/oc2/api/bus/device/vm/VMDevice.java +++ b/src/main/java/li/cil/oc2/api/bus/device/vm/VMDevice.java @@ -2,6 +2,7 @@ package li.cil.oc2.api.bus.device.vm; +import li.cil.oc2.api.bus.DeviceBus; import li.cil.oc2.api.bus.device.Device; import li.cil.oc2.api.bus.device.rpc.RPCDevice; import li.cil.oc2.api.bus.device.vm.context.InterruptAllocator; @@ -51,7 +52,10 @@ import li.cil.sedna.api.device.MemoryMappedDevice; */ public interface VMDevice extends Device { /** - * Called to initialize this device. + * Called to start this device. + *

+ * This is called when the connected virtual machine starts, or when the device + * is added to a {@link DeviceBus} with a currently running virtual machine. *

* Register {@link MemoryMappedDevice}s and claim interrupts via the * {@link InterruptAllocator} made available through the {@code context}. @@ -59,7 +63,7 @@ public interface VMDevice extends Device { * If loading cannot complete, e.g. because resources cannot be allocated, * this should return {@code false}. The virtual machine will periodically * try again to load failed devices. The virtual machine will only start - * running after all devices have successfully loaded. + * or resume after all devices have successfully loaded. * * @param context the virtual machine context. * @return {@code true} if the device was loaded successfully; {@code false} otherwise. @@ -67,23 +71,13 @@ public interface VMDevice extends Device { VMDeviceLoadResult mount(VMContext context); /** - * Called when the device is removed from the context it was loaded with. + * Called to pause this device. *

- * This can happen because the VM was stopped or the device was removed from - * the device bus that connected it to the VM, for example. + * Called when the connected virtual machine is suspended (chunk unload/server stopped/...). *

- * Intended for releasing resources acquired in {@link #mount(VMContext)}. + * Also called when the connected virtual machine stops or the device is removed from a + * {@link DeviceBus} with a currently running virtual machine. In this case, {@link #dispose()} + * will be called after this method returns. */ void unmount(); - - /** - * Called when the device is suspended. - *

- * This can happen when the level area containing the context the device was loaded in is unloaded, - * e.g. due to player moving too far away from the area. - *

- * Intended for soft-releasing resources acquired in {@link #mount(VMContext)}, i.e. non-persisted - * unmanaged resources. - */ - void dispose(); }