1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-01-15 11:45:42 +00:00

Make printers thread-safe

This commit is contained in:
SquidDev 2019-04-02 12:45:54 +01:00
parent 2ab79cf474
commit cbe6e9b5f5
3 changed files with 178 additions and 224 deletions

View File

@ -51,8 +51,13 @@ public class PrinterPeripheral implements IPeripheral
}
@Override
public Object[] callMethod( @Nonnull IComputerAccess computer, @Nonnull ILuaContext context, int method, @Nonnull Object[] args ) throws LuaException
public Object[] callMethod( @Nonnull IComputerAccess computer, @Nonnull ILuaContext context, int method, @Nonnull Object[] args ) throws LuaException, InterruptedException
{
// FIXME: There's a theoretical race condition here between getCurrentPage and then using the page. Ideally
// we'd lock on the page, consume it, and unlock.
// FIXME: None of our page modification functions actually mark the tile as dirty, so the page may not be
// persisted correctly.
switch( method )
{
case 0: // write
@ -89,10 +94,13 @@ public class PrinterPeripheral implements IPeripheral
return new Object[] { width, height };
}
case 4: // newPage
return new Object[] { m_printer.startNewPage() };
return context.executeMainThreadTask( () -> new Object[] { m_printer.startNewPage() } );
case 5: // endPage
getCurrentPage();
return context.executeMainThreadTask( () -> {
getCurrentPage();
return new Object[] { m_printer.endCurrentPage() };
} );
case 6: // getInkLevel
return new Object[] { m_printer.getInkLevel() };
case 7:
@ -123,13 +131,11 @@ public class PrinterPeripheral implements IPeripheral
return m_printer;
}
@Nonnull
private Terminal getCurrentPage() throws LuaException
{
Terminal currentPage = m_printer.getCurrentPage();
if( currentPage == null )
{
throw new LuaException( "Page not started" );
}
if( currentPage == null ) throw new LuaException( "Page not started" );
return currentPage;
}
}

View File

@ -45,9 +45,9 @@ public class TilePrinter extends TilePeripheralBase implements DefaultSidedInven
{
// Statics
private static final int[] bottomSlots = new int[] { 7, 8, 9, 10, 11, 12 };
private static final int[] topSlots = new int[] { 1, 2, 3, 4, 5, 6 };
private static final int[] sideSlots = new int[] { 0 };
private static final int[] BOTTOM_SLOTS = new int[] { 7, 8, 9, 10, 11, 12 };
private static final int[] TOP_SLOTS = new int[] { 1, 2, 3, 4, 5, 6 };
private static final int[] SIDE_SLOTS = new int[] { 0 };
// Members
@ -88,18 +88,12 @@ public class TilePrinter extends TilePeripheralBase implements DefaultSidedInven
}
// Read inventory
synchronized( m_inventory )
NBTTagList itemList = nbt.getTagList( "Items", Constants.NBT.TAG_COMPOUND );
for( int i = 0; i < itemList.tagCount(); i++ )
{
NBTTagList nbttaglist = nbt.getTagList( "Items", Constants.NBT.TAG_COMPOUND );
for( int i = 0; i < nbttaglist.tagCount(); i++ )
{
NBTTagCompound itemTag = nbttaglist.getCompoundTagAt( i );
int j = itemTag.getByte( "Slot" ) & 0xff;
if( j < m_inventory.size() )
{
m_inventory.set( j, new ItemStack( itemTag ) );
}
}
NBTTagCompound itemTag = itemList.getCompoundTagAt( i );
int slot = itemTag.getByte( "Slot" ) & 0xff;
if( slot < m_inventory.size() ) m_inventory.set( slot, new ItemStack( itemTag ) );
}
}
@ -116,21 +110,18 @@ public class TilePrinter extends TilePeripheralBase implements DefaultSidedInven
}
// Write inventory
synchronized( m_inventory )
{
NBTTagList nbttaglist = new NBTTagList();
NBTTagList itemList = new NBTTagList();
for( int i = 0; i < m_inventory.size(); i++ )
{
if( !m_inventory.get( i ).isEmpty() )
{
ItemStack stack = m_inventory.get( i );
if( stack.isEmpty() ) continue;
NBTTagCompound tag = new NBTTagCompound();
tag.setByte( "Slot", (byte) i );
m_inventory.get( i ).writeToNBT( tag );
nbttaglist.appendTag( tag );
}
}
nbt.setTag( "Items", nbttaglist );
stack.writeToNBT( tag );
itemList.appendTag( tag );
}
nbt.setTag( "Items", itemList );
return super.writeToNBT( nbt );
}
@ -148,7 +139,7 @@ public class TilePrinter extends TilePeripheralBase implements DefaultSidedInven
return super.shouldRefresh( world, pos, oldState, newState ) || BlockPeripheral.getPeripheralType( newState ) != PeripheralType.Printer;
}
public boolean isPrinting()
boolean isPrinting()
{
return m_printing;
}
@ -173,74 +164,60 @@ public class TilePrinter extends TilePeripheralBase implements DefaultSidedInven
@Nonnull
@Override
public ItemStack getStackInSlot( int i )
public ItemStack getStackInSlot( int slot )
{
return m_inventory.get( i );
return m_inventory.get( slot );
}
@Nonnull
@Override
public ItemStack removeStackFromSlot( int i )
public ItemStack removeStackFromSlot( int slot )
{
synchronized( m_inventory )
{
ItemStack result = m_inventory.get( i );
m_inventory.set( i, ItemStack.EMPTY );
ItemStack result = m_inventory.get( slot );
m_inventory.set( slot, ItemStack.EMPTY );
markDirty();
updateAnim();
return result;
}
}
@Nonnull
@Override
public ItemStack decrStackSize( int i, int j )
public ItemStack decrStackSize( int slot, int count )
{
synchronized( m_inventory )
{
if( m_inventory.get( i ).isEmpty() ) return ItemStack.EMPTY;
ItemStack stack = m_inventory.get( slot );
if( stack.isEmpty() ) return ItemStack.EMPTY;
if( m_inventory.get( i ).getCount() <= j )
if( stack.getCount() <= count )
{
ItemStack itemstack = m_inventory.get( i );
m_inventory.set( i, ItemStack.EMPTY );
markDirty();
updateAnim();
return itemstack;
setInventorySlotContents( slot, ItemStack.EMPTY );
return stack;
}
ItemStack part = m_inventory.get( i ).splitStack( j );
if( m_inventory.get( i ).isEmpty() )
ItemStack part = stack.splitStack( count );
if( m_inventory.get( slot ).isEmpty() )
{
m_inventory.set( i, ItemStack.EMPTY );
m_inventory.set( slot, ItemStack.EMPTY );
updateAnim();
}
markDirty();
return part;
}
}
@Override
public void setInventorySlotContents( int i, @Nonnull ItemStack stack )
public void setInventorySlotContents( int slot, @Nonnull ItemStack stack )
{
synchronized( m_inventory )
{
m_inventory.set( i, stack );
m_inventory.set( slot, stack );
markDirty();
updateAnim();
}
}
@Override
public void clear()
{
synchronized( m_inventory )
{
for( int i = 0; i < m_inventory.size(); i++ ) m_inventory.set( i, ItemStack.EMPTY );
markDirty();
updateAnim();
}
}
@Override
public boolean isItemValidForSlot( int slot, @Nonnull ItemStack stack )
@ -249,7 +226,7 @@ public class TilePrinter extends TilePeripheralBase implements DefaultSidedInven
{
return isInk( stack );
}
else if( slot >= topSlots[0] && slot <= topSlots[topSlots.length - 1] )
else if( slot >= TOP_SLOTS[0] && slot <= TOP_SLOTS[TOP_SLOTS.length - 1] )
{
return isPaper( stack );
}
@ -295,11 +272,11 @@ public class TilePrinter extends TilePeripheralBase implements DefaultSidedInven
switch( side )
{
case DOWN: // Bottom (Out tray)
return bottomSlots;
return BOTTOM_SLOTS;
case UP: // Top (In tray)
return topSlots;
return TOP_SLOTS;
default: // Sides (Ink)
return sideSlots;
return SIDE_SLOTS;
}
}
@ -311,14 +288,18 @@ public class TilePrinter extends TilePeripheralBase implements DefaultSidedInven
return new PrinterPeripheral( this );
}
public Terminal getCurrentPage()
@Nullable
Terminal getCurrentPage()
{
synchronized( m_page )
{
return m_printing ? m_page : null;
}
}
public boolean startNewPage()
boolean startNewPage()
{
synchronized( m_inventory )
synchronized( m_page )
{
if( !canInputPage() ) return false;
if( m_printing && !outputPage() ) return false;
@ -326,49 +307,36 @@ public class TilePrinter extends TilePeripheralBase implements DefaultSidedInven
}
}
public boolean endCurrentPage()
boolean endCurrentPage()
{
synchronized( m_inventory )
synchronized( m_page )
{
if( m_printing && outputPage() )
{
return true;
return m_printing && outputPage();
}
}
return false;
}
public int getInkLevel()
{
synchronized( m_inventory )
int getInkLevel()
{
ItemStack inkStack = m_inventory.get( 0 );
return isInk( inkStack ) ? inkStack.getCount() : 0;
}
}
public int getPaperLevel()
int getPaperLevel()
{
int count = 0;
synchronized( m_inventory )
{
for( int i = 1; i < 7; i++ )
{
ItemStack paperStack = m_inventory.get( i );
if( !paperStack.isEmpty() && isPaper( paperStack ) )
{
count += paperStack.getCount();
}
}
if( isPaper( paperStack ) ) count += paperStack.getCount();
}
return count;
}
public void setPageTitle( String title )
void setPageTitle( String title )
{
if( m_printing )
synchronized( m_page )
{
m_pageTitle = title;
if( m_printing ) m_pageTitle = title;
}
}
@ -384,17 +352,12 @@ public class TilePrinter extends TilePeripheralBase implements DefaultSidedInven
}
private boolean canInputPage()
{
synchronized( m_inventory )
{
ItemStack inkStack = m_inventory.get( 0 );
return !inkStack.isEmpty() && isInk( inkStack ) && getPaperLevel() > 0;
}
}
private boolean inputPage()
{
synchronized( m_inventory )
{
ItemStack inkStack = m_inventory.get( 0 );
if( !isInk( inkStack ) ) return false;
@ -402,8 +365,8 @@ public class TilePrinter extends TilePeripheralBase implements DefaultSidedInven
for( int i = 1; i < 7; i++ )
{
ItemStack paperStack = m_inventory.get( i );
if( !paperStack.isEmpty() && isPaper( paperStack ) )
{
if( paperStack.isEmpty() || !isPaper( paperStack ) ) continue;
// Setup the new page
int colour = inkStack.getItemDamage();
m_page.setTextColour( colour >= 0 && colour < 16 ? 15 - colour : 15 );
@ -441,14 +404,10 @@ public class TilePrinter extends TilePeripheralBase implements DefaultSidedInven
m_printing = true;
return true;
}
}
return false;
}
}
private boolean outputPage()
{
synchronized( m_page )
{
int height = m_page.getHeight();
String[] lines = new String[height];
@ -460,9 +419,7 @@ public class TilePrinter extends TilePeripheralBase implements DefaultSidedInven
}
ItemStack stack = ItemPrintout.createSingleFromTitleAndText( m_pageTitle, lines, colours );
synchronized( m_inventory )
{
for( int slot : bottomSlots )
for( int slot : BOTTOM_SLOTS )
{
if( m_inventory.get( slot ).isEmpty() )
{
@ -471,14 +428,10 @@ public class TilePrinter extends TilePeripheralBase implements DefaultSidedInven
return true;
}
}
}
return false;
}
}
private void ejectContents()
{
synchronized( m_inventory )
{
for( int i = 0; i < 13; i++ )
{
@ -497,11 +450,8 @@ public class TilePrinter extends TilePeripheralBase implements DefaultSidedInven
}
}
}
}
private void updateAnim()
{
synchronized( m_inventory )
{
int anim = 0;
for( int i = 1; i < 7; i++ )
@ -524,7 +474,6 @@ public class TilePrinter extends TilePeripheralBase implements DefaultSidedInven
}
setAnim( anim );
}
}
@Override
public boolean hasCapability( @Nonnull Capability<?> capability, @Nullable EnumFacing facing )

View File

@ -342,7 +342,6 @@ local tMenuFuncs = {
redrawMenu()
term.redirect( printerTerminal )
local timer = os.startTimer(0.5)
sleep(0.5)
end