/** * This file is part of ComputerCraft - http://www.computercraft.info * Copyright Daniel Ratcliffe, 2011-2016. Do not distribute without permission. * Send enquiries to dratcliffe@gmail.com */ package dan200.computercraft.core.apis; import dan200.computercraft.api.lua.ILuaContext; import dan200.computercraft.api.lua.ILuaObject; import dan200.computercraft.api.lua.LuaException; import dan200.computercraft.core.filesystem.FileSystem; import dan200.computercraft.core.filesystem.FileSystemException; import dan200.computercraft.core.filesystem.IMountedFileBinary; import dan200.computercraft.core.filesystem.IMountedFileNormal; import javax.annotation.Nonnull; import java.io.IOException; import java.util.HashMap; import java.util.Map; public class FSAPI implements ILuaAPI { private IAPIEnvironment m_env; private FileSystem m_fileSystem; public FSAPI( IAPIEnvironment _env ) { m_env = _env; m_fileSystem = null; } @Override public String[] getNames() { return new String[] { "fs" }; } @Override public void startup( ) { m_fileSystem = m_env.getFileSystem(); } @Override public void advance( double _dt ) { } @Override public void shutdown( ) { m_fileSystem = null; } @Nonnull @Override public String[] getMethodNames() { return new String[] { "list", "combine", "getName", "getSize", "exists", "isDir", "isReadOnly", "makeDir", "move", "copy", "delete", "open", "getDrive", "getFreeSpace", "find", "getDir", }; } @Override public Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull Object[] args ) throws LuaException { switch( method ) { case 0: { // list if( args.length != 1 || args[0] == null || !(args[0] instanceof String) ) { throw new LuaException( "Expected string" ); } String path = (String)args[0]; try { String[] results = m_fileSystem.list( path ); Map table = new HashMap(); for(int i=0; i= 0 ) { return new Object[]{ freeSpace }; } return new Object[]{ "unlimited" }; } catch( FileSystemException e ) { throw new LuaException( e.getMessage() ); } } case 14: { // find if( args.length != 1 || args[0] == null || !(args[0] instanceof String) ) { throw new LuaException( "Expected string" ); } String path = (String)args[0]; try { String[] results = m_fileSystem.find( path ); Map table = new HashMap(); for(int i=0; i 0 && args[0] != null ) { text = args[0].toString(); } else { text = ""; } try { writer.write( text, 0, text.length(), false ); return null; } catch( IOException e ) { throw new LuaException( e.getMessage() ); } } case 1: { // writeLine String text; if( args.length > 0 && args[0] != null ) { text = args[0].toString(); } else { text = ""; } try { writer.write( text, 0, text.length(), true ); return null; } catch( IOException e ) { throw new LuaException( e.getMessage() ); } } case 2: { // close try { writer.close(); return null; } catch( IOException e ) { return null; } } case 3: { try { writer.flush(); return null; } catch ( IOException e ) { return null; } } default: { assert( false ); return null; } } } } }; } private static Object[] wrapInputStream( final IMountedFileBinary reader ) { return new Object[] { new ILuaObject() { @Nonnull @Override public String[] getMethodNames() { return new String[] { "read", "close" }; } @Override public Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull Object[] args) throws LuaException { switch( method ) { case 0: { // read try { int b = reader.read(); if( b != -1 ) { return new Object[] { b }; } else { return null; } } catch( IOException e ) { return null; } } case 1: { //close try { reader.close(); return null; } catch( IOException e ) { return null; } } default: { assert( false ); return null; } } } }}; } private static Object[] wrapOutputStream( final IMountedFileBinary writer ) { return new Object[] { new ILuaObject() { @Nonnull @Override public String[] getMethodNames() { return new String[] { "write", "close", "flush" }; } @Override public Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull Object[] args) throws LuaException { switch( method ) { case 0: { // write try { if( args.length > 0 && args[0] instanceof Number ) { int number = ((Number)args[0]).intValue(); writer.write( number ); } return null; } catch( IOException e ) { throw new LuaException(e.getMessage()); } } case 1: { //close try { writer.close(); return null; } catch( IOException e ) { return null; } } case 2: { try { writer.flush(); return null; } catch ( IOException e ) { return null; } } default: { assert( false ); return null; } } } }}; } }