1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-07-05 03:23:20 +00:00

Store additional state in WiredModemPeripheral

This means wired peripherals now correctly track their current mounts
and attached state, rather than inheriting from the origin wired modem.

Closes #890
This commit is contained in:
Jonathan Coates 2021-09-27 22:18:32 +01:00
parent 7bb7b5e638
commit 8ba20985d7
No known key found for this signature in database
GPG Key ID: B9E431FF07C98D06
2 changed files with 54 additions and 13 deletions

View File

@ -5,6 +5,7 @@
*/ */
package dan200.computercraft.core.apis; package dan200.computercraft.core.apis;
import dan200.computercraft.ComputerCraft;
import dan200.computercraft.api.filesystem.IMount; import dan200.computercraft.api.filesystem.IMount;
import dan200.computercraft.api.filesystem.IWritableMount; import dan200.computercraft.api.filesystem.IWritableMount;
import dan200.computercraft.api.peripheral.IComputerAccess; import dan200.computercraft.api.peripheral.IComputerAccess;
@ -30,6 +31,11 @@ protected ComputerAccess( IAPIEnvironment environment )
public void unmountAll() public void unmountAll()
{ {
FileSystem fileSystem = environment.getFileSystem(); FileSystem fileSystem = environment.getFileSystem();
if( !mounts.isEmpty() )
{
ComputerCraft.log.warn( "Peripheral or API called mount but did not call unmount for {}", mounts );
}
for( String mount : mounts ) for( String mount : mounts )
{ {
fileSystem.unmount( mount ); fileSystem.unmount( mount );

View File

@ -6,6 +6,7 @@
package dan200.computercraft.shared.peripheral.modem.wired; package dan200.computercraft.shared.peripheral.modem.wired;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import dan200.computercraft.ComputerCraft;
import dan200.computercraft.api.filesystem.IMount; import dan200.computercraft.api.filesystem.IMount;
import dan200.computercraft.api.filesystem.IWritableMount; import dan200.computercraft.api.filesystem.IWritableMount;
import dan200.computercraft.api.lua.*; import dan200.computercraft.api.lua.*;
@ -15,6 +16,7 @@
import dan200.computercraft.api.peripheral.IComputerAccess; import dan200.computercraft.api.peripheral.IComputerAccess;
import dan200.computercraft.api.peripheral.IPeripheral; import dan200.computercraft.api.peripheral.IPeripheral;
import dan200.computercraft.api.peripheral.IWorkMonitor; import dan200.computercraft.api.peripheral.IWorkMonitor;
import dan200.computercraft.api.peripheral.NotAttachedException;
import dan200.computercraft.core.apis.PeripheralAPI; import dan200.computercraft.core.apis.PeripheralAPI;
import dan200.computercraft.core.asm.PeripheralMethod; import dan200.computercraft.core.asm.PeripheralMethod;
import dan200.computercraft.shared.peripheral.modem.ModemPeripheral; import dan200.computercraft.shared.peripheral.modem.ModemPeripheral;
@ -23,10 +25,7 @@
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.util.Collection; import java.util.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap; import java.util.concurrent.ConcurrentMap;
@ -311,6 +310,9 @@ private static class RemotePeripheralWrapper implements IComputerAccess
private final String type; private final String type;
private final Map<String, PeripheralMethod> methodMap; private final Map<String, PeripheralMethod> methodMap;
private volatile boolean attached;
private final Set<String> mounts = new HashSet<>();
RemotePeripheralWrapper( WiredModemElement element, IPeripheral peripheral, IComputerAccess computer, String name ) RemotePeripheralWrapper( WiredModemElement element, IPeripheral peripheral, IComputerAccess computer, String name )
{ {
this.element = element; this.element = element;
@ -324,6 +326,7 @@ private static class RemotePeripheralWrapper implements IComputerAccess
public void attach() public void attach()
{ {
attached = true;
peripheral.attach( this ); peripheral.attach( this );
computer.queueEvent( "peripheral", getAttachmentName() ); computer.queueEvent( "peripheral", getAttachmentName() );
} }
@ -332,6 +335,18 @@ public void detach()
{ {
peripheral.detach( this ); peripheral.detach( this );
computer.queueEvent( "peripheral_detach", getAttachmentName() ); computer.queueEvent( "peripheral_detach", getAttachmentName() );
attached = false;
synchronized( this )
{
if( !mounts.isEmpty() )
{
ComputerCraft.log.warn( "Peripheral {} called mount but did not call unmount for {}", peripheral, mounts );
}
for( String mount : mounts ) computer.unmount( mount );
mounts.clear();
}
} }
public String getType() public String getType()
@ -354,44 +369,60 @@ public MethodResult callMethod( ILuaContext context, String methodName, IArgumen
// IComputerAccess implementation // IComputerAccess implementation
@Override @Override
public String mount( @Nonnull String desiredLocation, @Nonnull IMount mount ) public synchronized String mount( @Nonnull String desiredLocation, @Nonnull IMount mount )
{ {
return computer.mount( desiredLocation, mount, name ); if( !attached ) throw new NotAttachedException();
String mounted = computer.mount( desiredLocation, mount, name );
mounts.add( mounted );
return mounted;
} }
@Override @Override
public String mount( @Nonnull String desiredLocation, @Nonnull IMount mount, @Nonnull String driveName ) public synchronized String mount( @Nonnull String desiredLocation, @Nonnull IMount mount, @Nonnull String driveName )
{ {
return computer.mount( desiredLocation, mount, driveName ); if( !attached ) throw new NotAttachedException();
String mounted = computer.mount( desiredLocation, mount, driveName );
mounts.add( mounted );
return mounted;
} }
@Override @Override
public String mountWritable( @Nonnull String desiredLocation, @Nonnull IWritableMount mount ) public synchronized String mountWritable( @Nonnull String desiredLocation, @Nonnull IWritableMount mount )
{ {
return computer.mountWritable( desiredLocation, mount, name ); if( !attached ) throw new NotAttachedException();
String mounted = computer.mountWritable( desiredLocation, mount, name );
mounts.add( mounted );
return mounted;
} }
@Override @Override
public String mountWritable( @Nonnull String desiredLocation, @Nonnull IWritableMount mount, @Nonnull String driveName ) public synchronized String mountWritable( @Nonnull String desiredLocation, @Nonnull IWritableMount mount, @Nonnull String driveName )
{ {
return computer.mountWritable( desiredLocation, mount, driveName ); if( !attached ) throw new NotAttachedException();
String mounted = computer.mountWritable( desiredLocation, mount, driveName );
mounts.add( mounted );
return mounted;
} }
@Override @Override
public void unmount( String location ) public synchronized void unmount( String location )
{ {
if( !attached ) throw new NotAttachedException();
computer.unmount( location ); computer.unmount( location );
mounts.remove( location );
} }
@Override @Override
public int getID() public int getID()
{ {
if( !attached ) throw new NotAttachedException();
return computer.getID(); return computer.getID();
} }
@Override @Override
public void queueEvent( @Nonnull String event, Object... arguments ) public void queueEvent( @Nonnull String event, Object... arguments )
{ {
if( !attached ) throw new NotAttachedException();
computer.queueEvent( event, arguments ); computer.queueEvent( event, arguments );
} }
@ -399,6 +430,7 @@ public void queueEvent( @Nonnull String event, Object... arguments )
@Override @Override
public IWorkMonitor getMainThreadMonitor() public IWorkMonitor getMainThreadMonitor()
{ {
if( !attached ) throw new NotAttachedException();
return computer.getMainThreadMonitor(); return computer.getMainThreadMonitor();
} }
@ -406,6 +438,7 @@ public IWorkMonitor getMainThreadMonitor()
@Override @Override
public String getAttachmentName() public String getAttachmentName()
{ {
if( !attached ) throw new NotAttachedException();
return name; return name;
} }
@ -413,6 +446,7 @@ public String getAttachmentName()
@Override @Override
public Map<String, IPeripheral> getAvailablePeripherals() public Map<String, IPeripheral> getAvailablePeripherals()
{ {
if( !attached ) throw new NotAttachedException();
synchronized( element.getRemotePeripherals() ) synchronized( element.getRemotePeripherals() )
{ {
return ImmutableMap.copyOf( element.getRemotePeripherals() ); return ImmutableMap.copyOf( element.getRemotePeripherals() );
@ -423,6 +457,7 @@ public Map<String, IPeripheral> getAvailablePeripherals()
@Override @Override
public IPeripheral getAvailablePeripheral( @Nonnull String name ) public IPeripheral getAvailablePeripheral( @Nonnull String name )
{ {
if( !attached ) throw new NotAttachedException();
synchronized( element.getRemotePeripherals() ) synchronized( element.getRemotePeripherals() )
{ {
return element.getRemotePeripherals().get( name ); return element.getRemotePeripherals().get( name );