2017-05-07 00:18:59 +00:00
|
|
|
/*
|
2017-05-01 13:32:39 +00:00
|
|
|
* This file is part of ComputerCraft - http://www.computercraft.info
|
2017-05-13 18:20:39 +00:00
|
|
|
* Copyright Daniel Ratcliffe, 2011-2017. Do not distribute without permission.
|
2017-05-01 13:32:39 +00:00
|
|
|
* Send enquiries to dratcliffe@gmail.com
|
|
|
|
*/
|
|
|
|
|
|
|
|
package dan200.computercraft.core.apis;
|
|
|
|
|
|
|
|
import dan200.computercraft.api.lua.ILuaContext;
|
|
|
|
import dan200.computercraft.api.lua.LuaException;
|
2017-05-12 21:49:44 +00:00
|
|
|
import dan200.computercraft.core.apis.handles.BinaryInputHandle;
|
|
|
|
import dan200.computercraft.core.apis.handles.BinaryOutputHandle;
|
|
|
|
import dan200.computercraft.core.apis.handles.EncodedInputHandle;
|
|
|
|
import dan200.computercraft.core.apis.handles.EncodedOutputHandle;
|
2017-05-01 13:32:39 +00:00
|
|
|
import dan200.computercraft.core.filesystem.FileSystem;
|
|
|
|
import dan200.computercraft.core.filesystem.FileSystemException;
|
|
|
|
|
2017-05-06 23:07:42 +00:00
|
|
|
import javax.annotation.Nonnull;
|
2017-05-12 21:49:44 +00:00
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.OutputStream;
|
2017-05-01 13:32:39 +00:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
|
2017-06-12 09:28:31 +00:00
|
|
|
import static dan200.computercraft.core.apis.ArgumentHelper.getString;
|
|
|
|
|
2017-05-01 13:32:39 +00:00
|
|
|
public class FSAPI implements ILuaAPI
|
|
|
|
{
|
2017-05-01 14:48:44 +00:00
|
|
|
private IAPIEnvironment m_env;
|
|
|
|
private FileSystem m_fileSystem;
|
|
|
|
|
|
|
|
public FSAPI( IAPIEnvironment _env )
|
|
|
|
{
|
|
|
|
m_env = _env;
|
|
|
|
m_fileSystem = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-05-01 13:32:39 +00:00
|
|
|
public String[] getNames()
|
|
|
|
{
|
2017-05-01 14:48:44 +00:00
|
|
|
return new String[] {
|
|
|
|
"fs"
|
|
|
|
};
|
2017-05-01 13:32:39 +00:00
|
|
|
}
|
|
|
|
|
2017-05-01 14:48:44 +00:00
|
|
|
@Override
|
|
|
|
public void startup( )
|
|
|
|
{
|
|
|
|
m_fileSystem = m_env.getFileSystem();
|
|
|
|
}
|
2017-05-01 13:32:39 +00:00
|
|
|
|
2017-05-01 14:48:44 +00:00
|
|
|
@Override
|
|
|
|
public void advance( double _dt )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void shutdown( )
|
|
|
|
{
|
|
|
|
m_fileSystem = null;
|
|
|
|
}
|
2017-05-01 13:32:39 +00:00
|
|
|
|
2017-05-06 23:07:42 +00:00
|
|
|
@Nonnull
|
2017-05-01 14:48:44 +00:00
|
|
|
@Override
|
2017-05-01 13:32:39 +00:00
|
|
|
public String[] getMethodNames()
|
|
|
|
{
|
2017-05-01 14:48:44 +00:00
|
|
|
return new String[] {
|
|
|
|
"list",
|
|
|
|
"combine",
|
|
|
|
"getName",
|
|
|
|
"getSize",
|
|
|
|
"exists",
|
|
|
|
"isDir",
|
|
|
|
"isReadOnly",
|
|
|
|
"makeDir",
|
|
|
|
"move",
|
|
|
|
"copy",
|
|
|
|
"delete",
|
|
|
|
"open",
|
|
|
|
"getDrive",
|
|
|
|
"getFreeSpace",
|
2017-05-01 13:32:39 +00:00
|
|
|
"find",
|
|
|
|
"getDir",
|
2017-05-01 14:48:44 +00:00
|
|
|
};
|
2017-05-01 13:32:39 +00:00
|
|
|
}
|
|
|
|
|
2017-05-01 14:48:44 +00:00
|
|
|
@Override
|
2017-05-06 23:07:42 +00:00
|
|
|
public Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull Object[] args ) throws LuaException
|
2017-05-01 13:32:39 +00:00
|
|
|
{
|
2017-05-01 14:48:44 +00:00
|
|
|
switch( method )
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
{
|
|
|
|
// list
|
2017-06-12 09:28:31 +00:00
|
|
|
String path = getString( args, 0 );
|
2017-05-01 14:48:44 +00:00
|
|
|
try {
|
|
|
|
String[] results = m_fileSystem.list( path );
|
|
|
|
Map<Object,Object> table = new HashMap<Object,Object>();
|
|
|
|
for(int i=0; i<results.length; ++i ) {
|
|
|
|
table.put( i+1, results[i] );
|
|
|
|
}
|
|
|
|
return new Object[] { table };
|
|
|
|
}
|
2017-05-01 13:32:39 +00:00
|
|
|
catch( FileSystemException e )
|
|
|
|
{
|
2017-05-01 14:48:44 +00:00
|
|
|
throw new LuaException( e.getMessage() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case 1:
|
|
|
|
{
|
|
|
|
// combine
|
2017-06-12 09:28:31 +00:00
|
|
|
String pathA = getString( args, 0 );
|
|
|
|
String pathB = getString( args, 1 );
|
2017-05-01 14:48:44 +00:00
|
|
|
return new Object[] { m_fileSystem.combine( pathA, pathB ) };
|
|
|
|
}
|
|
|
|
case 2:
|
|
|
|
{
|
|
|
|
// getName
|
2017-06-12 09:28:31 +00:00
|
|
|
String path = getString( args, 0 );
|
2017-05-07 00:16:08 +00:00
|
|
|
return new Object[]{ FileSystem.getName( path ) };
|
2017-05-01 14:48:44 +00:00
|
|
|
}
|
|
|
|
case 3:
|
|
|
|
{
|
|
|
|
// getSize
|
2017-06-12 09:28:31 +00:00
|
|
|
String path = getString( args, 0 );
|
2017-05-01 14:48:44 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
return new Object[]{ m_fileSystem.getSize( path ) };
|
|
|
|
}
|
2017-05-01 13:32:39 +00:00
|
|
|
catch( FileSystemException e )
|
|
|
|
{
|
2017-05-01 14:48:44 +00:00
|
|
|
throw new LuaException( e.getMessage() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case 4:
|
|
|
|
{
|
|
|
|
// exists
|
2017-06-12 09:28:31 +00:00
|
|
|
String path = getString( args, 0 );
|
2017-05-01 14:48:44 +00:00
|
|
|
try {
|
|
|
|
return new Object[]{ m_fileSystem.exists( path ) };
|
|
|
|
} catch( FileSystemException e ) {
|
|
|
|
return new Object[]{ false };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case 5:
|
|
|
|
{
|
|
|
|
// isDir
|
2017-06-12 09:28:31 +00:00
|
|
|
String path = getString( args, 0 );
|
2017-05-01 14:48:44 +00:00
|
|
|
try {
|
|
|
|
return new Object[]{ m_fileSystem.isDir( path ) };
|
|
|
|
} catch( FileSystemException e ) {
|
|
|
|
return new Object[]{ false };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case 6:
|
|
|
|
{
|
|
|
|
// isReadOnly
|
2017-06-12 09:28:31 +00:00
|
|
|
String path = getString( args, 0 );
|
2017-05-01 14:48:44 +00:00
|
|
|
try {
|
|
|
|
return new Object[]{ m_fileSystem.isReadOnly( path ) };
|
|
|
|
} catch( FileSystemException e ) {
|
|
|
|
return new Object[]{ false };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case 7:
|
|
|
|
{
|
|
|
|
// makeDir
|
2017-06-12 09:28:31 +00:00
|
|
|
String path = getString( args, 0 );
|
2017-05-01 14:48:44 +00:00
|
|
|
try {
|
|
|
|
m_fileSystem.makeDir( path );
|
|
|
|
return null;
|
|
|
|
} catch( FileSystemException e ) {
|
|
|
|
throw new LuaException( e.getMessage() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case 8:
|
|
|
|
{
|
|
|
|
// move
|
2017-06-12 09:28:31 +00:00
|
|
|
String path = getString( args, 0 );
|
|
|
|
String dest = getString( args, 1 );
|
2017-05-01 14:48:44 +00:00
|
|
|
try {
|
|
|
|
m_fileSystem.move( path, dest );
|
|
|
|
return null;
|
|
|
|
} catch( FileSystemException e ) {
|
|
|
|
throw new LuaException( e.getMessage() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case 9:
|
|
|
|
{
|
|
|
|
// copy
|
2017-06-12 09:28:31 +00:00
|
|
|
String path = getString( args, 0 );
|
|
|
|
String dest = getString( args, 1 );
|
2017-05-01 14:48:44 +00:00
|
|
|
try {
|
|
|
|
m_fileSystem.copy( path, dest );
|
|
|
|
return null;
|
|
|
|
} catch( FileSystemException e ) {
|
|
|
|
throw new LuaException( e.getMessage() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case 10:
|
|
|
|
{
|
|
|
|
// delete
|
2017-06-12 09:28:31 +00:00
|
|
|
String path = getString( args, 0 );
|
2017-05-01 14:48:44 +00:00
|
|
|
try {
|
|
|
|
m_fileSystem.delete( path );
|
|
|
|
return null;
|
|
|
|
} catch( FileSystemException e ) {
|
|
|
|
throw new LuaException( e.getMessage() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case 11:
|
|
|
|
{
|
|
|
|
// open
|
2017-06-12 09:28:31 +00:00
|
|
|
String path = getString( args, 0 );
|
|
|
|
String mode = getString( args, 1 );
|
2017-05-01 14:48:44 +00:00
|
|
|
try {
|
|
|
|
if( mode.equals( "r" ) ) {
|
|
|
|
// Open the file for reading, then create a wrapper around the reader
|
2017-05-12 21:49:44 +00:00
|
|
|
InputStream reader = m_fileSystem.openForRead( path );
|
|
|
|
return new Object[] { new EncodedInputHandle( reader ) };
|
2017-05-01 14:48:44 +00:00
|
|
|
|
|
|
|
} else if( mode.equals( "w" ) ) {
|
|
|
|
// Open the file for writing, then create a wrapper around the writer
|
2017-05-12 21:49:44 +00:00
|
|
|
OutputStream writer = m_fileSystem.openForWrite( path, false );
|
|
|
|
return new Object[] { new EncodedOutputHandle( writer ) };
|
2017-05-01 14:48:44 +00:00
|
|
|
|
|
|
|
} else if( mode.equals( "a" ) ) {
|
|
|
|
// Open the file for appending, then create a wrapper around the writer
|
2017-05-12 21:49:44 +00:00
|
|
|
OutputStream writer = m_fileSystem.openForWrite( path, true );
|
|
|
|
return new Object[] { new EncodedOutputHandle( writer ) };
|
2017-05-01 14:48:44 +00:00
|
|
|
|
|
|
|
} else if( mode.equals( "rb" ) ) {
|
|
|
|
// Open the file for binary reading, then create a wrapper around the reader
|
2017-05-12 21:49:44 +00:00
|
|
|
InputStream reader = m_fileSystem.openForRead( path );
|
|
|
|
return new Object[] { new BinaryInputHandle( reader ) };
|
2017-05-01 14:48:44 +00:00
|
|
|
|
|
|
|
} else if( mode.equals( "wb" ) ) {
|
|
|
|
// Open the file for binary writing, then create a wrapper around the writer
|
2017-05-12 21:49:44 +00:00
|
|
|
OutputStream writer = m_fileSystem.openForWrite( path, false );
|
|
|
|
return new Object[] { new BinaryOutputHandle( writer ) };
|
2017-05-01 14:48:44 +00:00
|
|
|
|
|
|
|
} else if( mode.equals( "ab" ) ) {
|
|
|
|
// Open the file for binary appending, then create a wrapper around the reader
|
2017-05-12 21:49:44 +00:00
|
|
|
OutputStream writer = m_fileSystem.openForWrite( path, true );
|
|
|
|
return new Object[] { new BinaryOutputHandle( writer ) };
|
2017-05-01 14:48:44 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
throw new LuaException( "Unsupported mode" );
|
|
|
|
|
|
|
|
}
|
|
|
|
} catch( FileSystemException e ) {
|
2017-05-10 09:48:59 +00:00
|
|
|
return new Object[] { null, e.getMessage() };
|
2017-05-01 14:48:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case 12:
|
|
|
|
{
|
|
|
|
// getDrive
|
2017-06-12 09:28:31 +00:00
|
|
|
String path = getString( args, 0 );
|
2017-05-01 14:48:44 +00:00
|
|
|
try {
|
2017-05-01 13:32:39 +00:00
|
|
|
if( !m_fileSystem.exists( path ) )
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return new Object[]{ m_fileSystem.getMountLabel( path ) };
|
2017-05-01 14:48:44 +00:00
|
|
|
} catch( FileSystemException e ) {
|
|
|
|
throw new LuaException( e.getMessage() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case 13:
|
|
|
|
{
|
|
|
|
// getFreeSpace
|
2017-06-12 09:28:31 +00:00
|
|
|
String path = getString( args, 0 );
|
2017-05-01 14:48:44 +00:00
|
|
|
try {
|
2017-05-01 13:32:39 +00:00
|
|
|
long freeSpace = m_fileSystem.getFreeSpace( path );
|
|
|
|
if( freeSpace >= 0 )
|
|
|
|
{
|
|
|
|
return new Object[]{ freeSpace };
|
|
|
|
}
|
|
|
|
return new Object[]{ "unlimited" };
|
2017-05-01 14:48:44 +00:00
|
|
|
} catch( FileSystemException e ) {
|
|
|
|
throw new LuaException( e.getMessage() );
|
|
|
|
}
|
|
|
|
}
|
2017-05-01 13:32:39 +00:00
|
|
|
case 14:
|
|
|
|
{
|
|
|
|
// find
|
2017-06-12 09:28:31 +00:00
|
|
|
String path = getString( args, 0 );
|
2017-05-01 13:32:39 +00:00
|
|
|
try {
|
|
|
|
String[] results = m_fileSystem.find( path );
|
|
|
|
Map<Object,Object> table = new HashMap<Object,Object>();
|
|
|
|
for(int i=0; i<results.length; ++i ) {
|
|
|
|
table.put( i+1, results[i] );
|
|
|
|
}
|
|
|
|
return new Object[] { table };
|
|
|
|
} catch( FileSystemException e ) {
|
|
|
|
throw new LuaException( e.getMessage() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case 15:
|
|
|
|
{
|
|
|
|
// getDir
|
2017-06-12 09:28:31 +00:00
|
|
|
String path = getString( args, 0 );
|
2017-05-07 00:16:08 +00:00
|
|
|
return new Object[]{ FileSystem.getDirectory( path ) };
|
2017-05-01 13:32:39 +00:00
|
|
|
}
|
2017-05-01 14:48:44 +00:00
|
|
|
default:
|
|
|
|
{
|
|
|
|
assert( false );
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-05-01 13:32:39 +00:00
|
|
|
}
|