1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-05-05 17:04:14 +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 @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 ) switch( method )
{ {
case 0: // write case 0: // write
@ -89,10 +94,13 @@ public class PrinterPeripheral implements IPeripheral
return new Object[] { width, height }; return new Object[] { width, height };
} }
case 4: // newPage case 4: // newPage
return new Object[] { m_printer.startNewPage() }; return context.executeMainThreadTask( () -> new Object[] { m_printer.startNewPage() } );
case 5: // endPage case 5: // endPage
getCurrentPage();
return context.executeMainThreadTask( () -> {
getCurrentPage(); getCurrentPage();
return new Object[] { m_printer.endCurrentPage() }; return new Object[] { m_printer.endCurrentPage() };
} );
case 6: // getInkLevel case 6: // getInkLevel
return new Object[] { m_printer.getInkLevel() }; return new Object[] { m_printer.getInkLevel() };
case 7: case 7:
@ -123,13 +131,11 @@ public class PrinterPeripheral implements IPeripheral
return m_printer; return m_printer;
} }
@Nonnull
private Terminal getCurrentPage() throws LuaException private Terminal getCurrentPage() throws LuaException
{ {
Terminal currentPage = m_printer.getCurrentPage(); Terminal currentPage = m_printer.getCurrentPage();
if( currentPage == null ) if( currentPage == null ) throw new LuaException( "Page not started" );
{
throw new LuaException( "Page not started" );
}
return currentPage; return currentPage;
} }
} }

View File

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

View File

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