2013-12-27 23:59:59 +01:00
|
|
|
package appeng.me.storage;
|
|
|
|
|
2014-01-20 17:41:37 +01:00
|
|
|
import java.util.ArrayList;
|
2013-12-27 23:59:59 +01:00
|
|
|
import java.util.Comparator;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.LinkedList;
|
2014-01-20 17:41:37 +01:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.TreeMap;
|
2013-12-27 23:59:59 +01:00
|
|
|
|
|
|
|
import appeng.api.config.AccessRestriction;
|
|
|
|
import appeng.api.config.Actionable;
|
2014-01-27 05:00:36 +01:00
|
|
|
import appeng.api.config.SecurityPermissions;
|
|
|
|
import appeng.api.networking.IGrid;
|
|
|
|
import appeng.api.networking.IGridNode;
|
2014-01-20 17:41:37 +01:00
|
|
|
import appeng.api.networking.security.BaseActionSource;
|
2014-01-27 05:00:36 +01:00
|
|
|
import appeng.api.networking.security.ISecurityGrid;
|
|
|
|
import appeng.api.networking.security.MachineSource;
|
|
|
|
import appeng.api.networking.security.PlayerSource;
|
2013-12-27 23:59:59 +01:00
|
|
|
import appeng.api.storage.IMEInventoryHandler;
|
|
|
|
import appeng.api.storage.StorageChannel;
|
|
|
|
import appeng.api.storage.data.IAEStack;
|
|
|
|
import appeng.api.storage.data.IItemList;
|
2014-01-27 05:00:36 +01:00
|
|
|
import appeng.me.cache.SecurityCache;
|
2013-12-27 23:59:59 +01:00
|
|
|
|
|
|
|
public class NetworkInventoryHandler<T extends IAEStack<T>> implements IMEInventoryHandler<T>
|
|
|
|
{
|
|
|
|
|
|
|
|
private final static Comparator prioritySorter = new Comparator<Integer>() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int compare(Integer o1, Integer o2)
|
|
|
|
{
|
2014-01-26 07:48:09 +01:00
|
|
|
return o2 - o1;
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
final StorageChannel myChannel;
|
2014-01-27 05:00:36 +01:00
|
|
|
final SecurityCache security;
|
|
|
|
|
2014-01-20 17:41:37 +01:00
|
|
|
// final TreeMultimap<Integer, IMEInventoryHandler<T>> prorityInventory;
|
|
|
|
final TreeMap<Integer, List<IMEInventoryHandler<T>>> prorityInventory;
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2014-01-27 05:00:36 +01:00
|
|
|
public NetworkInventoryHandler(StorageChannel chan, SecurityCache security) {
|
2013-12-27 23:59:59 +01:00
|
|
|
myChannel = chan;
|
2014-01-27 05:00:36 +01:00
|
|
|
this.security = security;
|
2014-01-20 17:41:37 +01:00
|
|
|
prorityInventory = new TreeMap( prioritySorter ); // TreeMultimap.create( prioritySorter, hashSorter );
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void addNewStorage(IMEInventoryHandler<T> h)
|
|
|
|
{
|
2014-01-20 17:41:37 +01:00
|
|
|
int priority = h.getPriority();
|
|
|
|
List<IMEInventoryHandler<T>> list = prorityInventory.get( priority );
|
|
|
|
if ( list == null )
|
|
|
|
prorityInventory.put( priority, list = new ArrayList() );
|
|
|
|
|
|
|
|
list.add( h );
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int currentPass = 0;
|
|
|
|
int myPass = 0;
|
2014-05-02 07:00:14 +02:00
|
|
|
static final ThreadLocal<LinkedList> depth = new ThreadLocal<LinkedList>();
|
|
|
|
|
|
|
|
private LinkedList getDepth()
|
|
|
|
{
|
|
|
|
LinkedList s = depth.get();
|
|
|
|
|
|
|
|
if ( s == null )
|
|
|
|
depth.set( s = new LinkedList() );
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
|
|
|
|
private boolean diveList(NetworkInventoryHandler<T> networkInventoryHandler)
|
|
|
|
{
|
2014-05-02 07:00:14 +02:00
|
|
|
LinkedList cDepth = getDepth();
|
|
|
|
if ( cDepth.contains( networkInventoryHandler ) )
|
2013-12-27 23:59:59 +01:00
|
|
|
return true;
|
|
|
|
|
2014-05-02 07:00:14 +02:00
|
|
|
cDepth.push( this );
|
2013-12-27 23:59:59 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean diveIteration(NetworkInventoryHandler<T> networkInventoryHandler)
|
|
|
|
{
|
2014-05-02 07:00:14 +02:00
|
|
|
if ( getDepth().isEmpty() )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
currentPass++;
|
|
|
|
myPass = currentPass;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ( currentPass == myPass )
|
|
|
|
return true;
|
|
|
|
else
|
|
|
|
myPass = currentPass;
|
|
|
|
}
|
|
|
|
|
2014-05-02 07:00:14 +02:00
|
|
|
getDepth().push( this );
|
2013-12-27 23:59:59 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void surface(NetworkInventoryHandler<T> networkInventoryHandler)
|
|
|
|
{
|
2014-05-02 07:00:14 +02:00
|
|
|
if ( getDepth().pop() != this )
|
2013-12-27 23:59:59 +01:00
|
|
|
throw new RuntimeException( "Invalid Access to Networked Storage API detected." );
|
|
|
|
}
|
|
|
|
|
2014-02-01 06:37:27 +01:00
|
|
|
private boolean testPermission(BaseActionSource src, SecurityPermissions permission)
|
2014-01-27 05:00:36 +01:00
|
|
|
{
|
|
|
|
if ( src.isPlayer() )
|
|
|
|
{
|
2014-02-01 06:37:27 +01:00
|
|
|
if ( !security.hasPermission( ((PlayerSource) src).player, permission ) )
|
2014-01-27 05:00:36 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if ( src.isMachine() )
|
|
|
|
{
|
|
|
|
if ( security.isAvailable() )
|
|
|
|
{
|
|
|
|
IGridNode n = ((MachineSource) src).via.getActionableNode();
|
|
|
|
if ( n == null )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
IGrid gn = n.getGrid();
|
|
|
|
if ( gn != security.myGrid )
|
|
|
|
{
|
|
|
|
int playerID = -1;
|
|
|
|
|
|
|
|
ISecurityGrid sg = gn.getCache( ISecurityGrid.class );
|
|
|
|
playerID = sg.getOwner();
|
|
|
|
|
2014-02-01 06:37:27 +01:00
|
|
|
if ( !security.hasPermission( playerID, permission ) )
|
2014-01-27 05:00:36 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
@Override
|
2014-01-20 17:41:37 +01:00
|
|
|
public T injectItems(T input, Actionable type, BaseActionSource src)
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
if ( diveList( this ) )
|
|
|
|
return input;
|
|
|
|
|
2014-01-27 05:00:36 +01:00
|
|
|
if ( testPermission( src, SecurityPermissions.INJECT ) )
|
|
|
|
{
|
|
|
|
surface( this );
|
|
|
|
return input;
|
|
|
|
}
|
|
|
|
|
2014-01-20 17:41:37 +01:00
|
|
|
Iterator<List<IMEInventoryHandler<T>>> i = prorityInventory.values().iterator();// asMap().entrySet().iterator();
|
2013-12-27 23:59:59 +01:00
|
|
|
|
|
|
|
while (i.hasNext())
|
|
|
|
{
|
2014-01-20 17:41:37 +01:00
|
|
|
List<IMEInventoryHandler<T>> invList = i.next();
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2014-01-20 17:41:37 +01:00
|
|
|
Iterator<IMEInventoryHandler<T>> ii = invList.iterator();
|
2013-12-27 23:59:59 +01:00
|
|
|
while (ii.hasNext() && input != null)
|
|
|
|
{
|
|
|
|
IMEInventoryHandler<T> inv = ii.next();
|
|
|
|
|
2014-01-20 17:41:37 +01:00
|
|
|
if ( inv.canAccept( input ) && (inv.extractItems( input, Actionable.SIMULATE, src ) != null || inv.isPrioritized( input )) )
|
|
|
|
input = inv.injectItems( input, type, src );
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
2014-01-20 17:41:37 +01:00
|
|
|
ii = invList.iterator();
|
2013-12-27 23:59:59 +01:00
|
|
|
while (ii.hasNext() && input != null)
|
|
|
|
{
|
|
|
|
IMEInventoryHandler<T> inv = ii.next();
|
|
|
|
if ( inv.canAccept( input ) )
|
2014-01-20 17:41:37 +01:00
|
|
|
input = inv.injectItems( input, type, src );
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
surface( this );
|
|
|
|
|
|
|
|
return input;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-01-20 17:41:37 +01:00
|
|
|
public T extractItems(T request, Actionable mode, BaseActionSource src)
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
if ( diveList( this ) )
|
|
|
|
return null;
|
|
|
|
|
2014-01-27 05:00:36 +01:00
|
|
|
if ( testPermission( src, SecurityPermissions.EXTRACT ) )
|
|
|
|
{
|
|
|
|
surface( this );
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2014-01-20 17:41:37 +01:00
|
|
|
Iterator<List<IMEInventoryHandler<T>>> i = prorityInventory.descendingMap().values().iterator();// prorityInventory.asMap().descendingMap().entrySet().iterator();
|
2013-12-27 23:59:59 +01:00
|
|
|
|
|
|
|
T output = request.copy();
|
|
|
|
request = request.copy();
|
|
|
|
output.setStackSize( 0 );
|
|
|
|
long req = request.getStackSize();
|
|
|
|
|
|
|
|
while (i.hasNext())
|
|
|
|
{
|
2014-01-20 17:41:37 +01:00
|
|
|
List<IMEInventoryHandler<T>> invList = i.next();
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2014-01-20 17:41:37 +01:00
|
|
|
Iterator<IMEInventoryHandler<T>> ii = invList.iterator();
|
2013-12-27 23:59:59 +01:00
|
|
|
while (ii.hasNext() && output.getStackSize() < req)
|
|
|
|
{
|
|
|
|
IMEInventoryHandler<T> inv = ii.next();
|
|
|
|
|
|
|
|
request.setStackSize( req - output.getStackSize() );
|
2014-01-20 17:41:37 +01:00
|
|
|
output.add( inv.extractItems( request, mode, src ) );
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
surface( this );
|
|
|
|
|
|
|
|
if ( output.getStackSize() <= 0 )
|
|
|
|
return null;
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public IItemList<T> getAvailableItems(IItemList out)
|
|
|
|
{
|
|
|
|
if ( diveIteration( this ) )
|
|
|
|
return out;
|
|
|
|
|
2014-01-20 17:41:37 +01:00
|
|
|
// for (Entry<Integer, IMEInventoryHandler<T>> h : prorityInventory.entries())
|
|
|
|
for (List<IMEInventoryHandler<T>> i : prorityInventory.values())
|
|
|
|
for (IMEInventoryHandler<T> j : i)
|
|
|
|
out = j.getAvailableItems( out );
|
2013-12-27 23:59:59 +01:00
|
|
|
|
|
|
|
surface( this );
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public StorageChannel getChannel()
|
|
|
|
{
|
|
|
|
return myChannel;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public AccessRestriction getAccess()
|
|
|
|
{
|
|
|
|
return AccessRestriction.READ_WRITE;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isPrioritized(T input)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canAccept(T input)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getPriority()
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getSlot()
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-06-20 09:10:02 +02:00
|
|
|
@Override
|
|
|
|
public boolean validForPass(int i)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|