/* * This file is part of ComputerCraft - http://www.computercraft.info * Copyright Daniel Ratcliffe, 2011-2017. 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.LuaException; import dan200.computercraft.core.apis.handles.BinaryReadableHandle; import dan200.computercraft.core.apis.handles.BinaryWritableHandle; import dan200.computercraft.core.apis.handles.EncodedReadableHandle; import dan200.computercraft.core.apis.handles.EncodedWritableHandle; import dan200.computercraft.core.filesystem.FileSystem; import dan200.computercraft.core.filesystem.FileSystemException; import dan200.computercraft.core.filesystem.FileSystemWrapper; import javax.annotation.Nonnull; import java.io.BufferedReader; import java.io.BufferedWriter; import java.nio.channels.ReadableByteChannel; import java.nio.channels.WritableByteChannel; import java.util.HashMap; import java.util.Map; import java.util.function.Function; import static dan200.computercraft.core.apis.ArgumentHelper.getString; 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 String path = getString( args, 0 ); try { String[] results = m_fileSystem.list( path ); Map table = new HashMap<>(); for(int i=0; i reader = m_fileSystem.openForRead( path, EncodedReadableHandle::openUtf8 ); return new Object[] { new EncodedReadableHandle( reader.get(), reader ) }; } case "w": { // Open the file for writing, then create a wrapper around the writer FileSystemWrapper writer = m_fileSystem.openForWrite( path, false, EncodedWritableHandle::openUtf8 ); return new Object[] { new EncodedWritableHandle( writer.get(), writer ) }; } case "a": { // Open the file for appending, then create a wrapper around the writer FileSystemWrapper writer = m_fileSystem.openForWrite( path, true, EncodedWritableHandle::openUtf8 ); return new Object[] { new EncodedWritableHandle( writer.get(), writer ) }; } case "rb": { // Open the file for binary reading, then create a wrapper around the reader FileSystemWrapper reader = m_fileSystem.openForRead( path, Function.identity() ); return new Object[] { new BinaryReadableHandle( reader.get(), reader ) }; } case "wb": { // Open the file for binary writing, then create a wrapper around the writer FileSystemWrapper writer = m_fileSystem.openForWrite( path, false, Function.identity() ); return new Object[] { new BinaryWritableHandle( writer.get(), writer ) }; } case "ab": { // Open the file for binary appending, then create a wrapper around the reader FileSystemWrapper writer = m_fileSystem.openForWrite( path, true, Function.identity() ); return new Object[] { new BinaryWritableHandle( writer.get(), writer ) }; } default: throw new LuaException( "Unsupported mode" ); } } catch( FileSystemException e ) { return new Object[] { null, e.getMessage() }; } } case 12: { // getDrive String path = getString( args, 0 ); try { if( !m_fileSystem.exists( path ) ) { return null; } return new Object[]{ m_fileSystem.getMountLabel( path ) }; } catch( FileSystemException e ) { throw new LuaException( e.getMessage() ); } } case 13: { // getFreeSpace String path = getString( args, 0 ); try { long freeSpace = m_fileSystem.getFreeSpace( path ); if( freeSpace >= 0 ) { return new Object[]{ freeSpace }; } return new Object[]{ "unlimited" }; } catch( FileSystemException e ) { throw new LuaException( e.getMessage() ); } } case 14: { // find String path = getString( args, 0 ); try { String[] results = m_fileSystem.find( path ); Map table = new HashMap<>(); for(int i=0; i