mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2024-06-26 07:03:22 +00:00
![SquidDev](/assets/img/avatar_default.png)
- Move the encoding/decoding from the Filesystem implementation to the individual handles. - Move each handle into an core.apis.handles package from the main fs API. - Move the HTTP response to inherit from these handles. - Allow binary handles' read function to accept a number, specifying how many characters to read - these will be returned as a Lua string. - Add readAll to binary handles - Allow binary handles' write function to accept a string which is decoded into the individual bytes. - Add "binary" argument to http.request and friends in order to return a binary handle. - Ensure file handles are open when reading from/writing to them. - Return the error message when opening a file fails.
374 lines
14 KiB
Java
374 lines
14 KiB
Java
/*
|
|
* 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.BinaryInputHandle;
|
|
import dan200.computercraft.core.apis.handles.BinaryOutputHandle;
|
|
import dan200.computercraft.core.apis.handles.EncodedInputHandle;
|
|
import dan200.computercraft.core.apis.handles.EncodedOutputHandle;
|
|
import dan200.computercraft.core.filesystem.FileSystem;
|
|
import dan200.computercraft.core.filesystem.FileSystemException;
|
|
|
|
import javax.annotation.Nonnull;
|
|
import java.io.InputStream;
|
|
import java.io.OutputStream;
|
|
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<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 1:
|
|
{
|
|
// combine
|
|
if( args.length != 2 || args[0] == null || !(args[0] instanceof String) || args[1] == null || !(args[1] instanceof String) )
|
|
{
|
|
throw new LuaException( "Expected string, string" );
|
|
}
|
|
String pathA = (String)args[0];
|
|
String pathB = (String)args[1];
|
|
return new Object[] { m_fileSystem.combine( pathA, pathB ) };
|
|
}
|
|
case 2:
|
|
{
|
|
// getName
|
|
if( args.length != 1 || args[0] == null || !(args[0] instanceof String) )
|
|
{
|
|
throw new LuaException( "Expected string" );
|
|
}
|
|
String path = (String)args[0];
|
|
return new Object[]{ FileSystem.getName( path ) };
|
|
}
|
|
case 3:
|
|
{
|
|
// getSize
|
|
if( args.length != 1 || args[0] == null || !(args[0] instanceof String) )
|
|
{
|
|
throw new LuaException( "Expected string" );
|
|
}
|
|
String path = (String)args[0];
|
|
try
|
|
{
|
|
return new Object[]{ m_fileSystem.getSize( path ) };
|
|
}
|
|
catch( FileSystemException e )
|
|
{
|
|
throw new LuaException( e.getMessage() );
|
|
}
|
|
}
|
|
case 4:
|
|
{
|
|
// exists
|
|
if( args.length != 1 || args[0] == null || !(args[0] instanceof String) )
|
|
{
|
|
throw new LuaException( "Expected string" );
|
|
}
|
|
String path = (String)args[0];
|
|
try {
|
|
return new Object[]{ m_fileSystem.exists( path ) };
|
|
} catch( FileSystemException e ) {
|
|
return new Object[]{ false };
|
|
}
|
|
}
|
|
case 5:
|
|
{
|
|
// isDir
|
|
if( args.length != 1 || args[0] == null || !(args[0] instanceof String) )
|
|
{
|
|
throw new LuaException( "Expected string" );
|
|
}
|
|
String path = (String)args[0];
|
|
try {
|
|
return new Object[]{ m_fileSystem.isDir( path ) };
|
|
} catch( FileSystemException e ) {
|
|
return new Object[]{ false };
|
|
}
|
|
}
|
|
case 6:
|
|
{
|
|
// isReadOnly
|
|
if( args.length != 1 || args[0] == null || !(args[0] instanceof String) )
|
|
{
|
|
throw new LuaException( "Expected string" );
|
|
}
|
|
String path = (String)args[0];
|
|
try {
|
|
return new Object[]{ m_fileSystem.isReadOnly( path ) };
|
|
} catch( FileSystemException e ) {
|
|
return new Object[]{ false };
|
|
}
|
|
}
|
|
case 7:
|
|
{
|
|
// makeDir
|
|
if( args.length != 1 || args[0] == null || !(args[0] instanceof String) )
|
|
{
|
|
throw new LuaException( "Expected string" );
|
|
}
|
|
String path = (String)args[0];
|
|
try {
|
|
m_fileSystem.makeDir( path );
|
|
return null;
|
|
} catch( FileSystemException e ) {
|
|
throw new LuaException( e.getMessage() );
|
|
}
|
|
}
|
|
case 8:
|
|
{
|
|
// move
|
|
if( args.length != 2 || args[0] == null || !(args[0] instanceof String) || args[1] == null || !(args[1] instanceof String) )
|
|
{
|
|
throw new LuaException( "Expected string, string" );
|
|
}
|
|
String path = (String)args[0];
|
|
String dest = (String)args[1];
|
|
try {
|
|
m_fileSystem.move( path, dest );
|
|
return null;
|
|
} catch( FileSystemException e ) {
|
|
throw new LuaException( e.getMessage() );
|
|
}
|
|
}
|
|
case 9:
|
|
{
|
|
// copy
|
|
if( args.length != 2 || args[0] == null || !(args[0] instanceof String) || args[1] == null || !(args[1] instanceof String) )
|
|
{
|
|
throw new LuaException( "Expected string, string" );
|
|
}
|
|
String path = (String)args[0];
|
|
String dest = (String)args[1];
|
|
try {
|
|
m_fileSystem.copy( path, dest );
|
|
return null;
|
|
} catch( FileSystemException e ) {
|
|
throw new LuaException( e.getMessage() );
|
|
}
|
|
}
|
|
case 10:
|
|
{
|
|
// delete
|
|
if( args.length != 1 || args[0] == null || !(args[0] instanceof String) )
|
|
{
|
|
throw new LuaException( "Expected string" );
|
|
}
|
|
String path = (String)args[0];
|
|
try {
|
|
m_fileSystem.delete( path );
|
|
return null;
|
|
} catch( FileSystemException e ) {
|
|
throw new LuaException( e.getMessage() );
|
|
}
|
|
}
|
|
case 11:
|
|
{
|
|
// open
|
|
if( args.length < 2 || args[0] == null || !(args[0] instanceof String) || args[1] == null || !(args[1] instanceof String) )
|
|
{
|
|
throw new LuaException( "Expected string, string" );
|
|
}
|
|
String path = (String)args[0];
|
|
String mode = (String)args[1];
|
|
try {
|
|
if( mode.equals( "r" ) ) {
|
|
// Open the file for reading, then create a wrapper around the reader
|
|
InputStream reader = m_fileSystem.openForRead( path );
|
|
return new Object[] { new EncodedInputHandle( reader ) };
|
|
|
|
} else if( mode.equals( "w" ) ) {
|
|
// Open the file for writing, then create a wrapper around the writer
|
|
OutputStream writer = m_fileSystem.openForWrite( path, false );
|
|
return new Object[] { new EncodedOutputHandle( writer ) };
|
|
|
|
} else if( mode.equals( "a" ) ) {
|
|
// Open the file for appending, then create a wrapper around the writer
|
|
OutputStream writer = m_fileSystem.openForWrite( path, true );
|
|
return new Object[] { new EncodedOutputHandle( writer ) };
|
|
|
|
} else if( mode.equals( "rb" ) ) {
|
|
// Open the file for binary reading, then create a wrapper around the reader
|
|
InputStream reader = m_fileSystem.openForRead( path );
|
|
return new Object[] { new BinaryInputHandle( reader ) };
|
|
|
|
} else if( mode.equals( "wb" ) ) {
|
|
// Open the file for binary writing, then create a wrapper around the writer
|
|
OutputStream writer = m_fileSystem.openForWrite( path, false );
|
|
return new Object[] { new BinaryOutputHandle( writer ) };
|
|
|
|
} else if( mode.equals( "ab" ) ) {
|
|
// Open the file for binary appending, then create a wrapper around the reader
|
|
OutputStream writer = m_fileSystem.openForWrite( path, true );
|
|
return new Object[] { new BinaryOutputHandle( writer ) };
|
|
|
|
} else {
|
|
throw new LuaException( "Unsupported mode" );
|
|
|
|
}
|
|
} catch( FileSystemException e ) {
|
|
return new Object[] { null, e.getMessage() };
|
|
}
|
|
}
|
|
case 12:
|
|
{
|
|
// getDrive
|
|
if( args.length != 1 || args[0] == null || !(args[0] instanceof String) )
|
|
{
|
|
throw new LuaException( "Expected string" );
|
|
}
|
|
String path = (String)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
|
|
if( args.length != 1 || args[0] == null || !(args[0] instanceof String) )
|
|
{
|
|
throw new LuaException( "Expected string" );
|
|
}
|
|
String path = (String)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
|
|
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<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
|
|
if( args.length != 1 || args[0] == null || !(args[0] instanceof String) )
|
|
{
|
|
throw new LuaException( "Expected string" );
|
|
}
|
|
String path = (String)args[0];
|
|
return new Object[]{ FileSystem.getDirectory( path ) };
|
|
}
|
|
default:
|
|
{
|
|
assert( false );
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
}
|