From 90c4ebd208c11df3a26e5725aed1f8f37a7032b7 Mon Sep 17 00:00:00 2001 From: Wilma456 Date: Wed, 9 Aug 2017 19:32:29 +0200 Subject: [PATCH 1/6] Fix Bug in copy.lua, mkdir.lua and rename.lua --- .../assets/computercraft/lua/rom/programs/copy.lua | 4 +++- .../assets/computercraft/lua/rom/programs/mkdir.lua | 6 ++++++ .../assets/computercraft/lua/rom/programs/rename.lua | 5 +++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/main/resources/assets/computercraft/lua/rom/programs/copy.lua b/src/main/resources/assets/computercraft/lua/rom/programs/copy.lua index 344903f6d..c70cd0a64 100644 --- a/src/main/resources/assets/computercraft/lua/rom/programs/copy.lua +++ b/src/main/resources/assets/computercraft/lua/rom/programs/copy.lua @@ -10,7 +10,9 @@ local sDest = shell.resolve( tArgs[2] ) local tFiles = fs.find( sSource ) if #tFiles > 0 then for n,sFile in ipairs( tFiles ) do - if fs.isDir( sDest ) then + if fs.exists( sDest ) == true then + printError( "Destination exists" ) + elseif fs.isDir( sDest ) then fs.copy( sFile, fs.combine( sDest, fs.getName(sFile) ) ) elseif #tFiles == 1 then fs.copy( sFile, sDest ) diff --git a/src/main/resources/assets/computercraft/lua/rom/programs/mkdir.lua b/src/main/resources/assets/computercraft/lua/rom/programs/mkdir.lua index 956123ff6..cace3bd55 100644 --- a/src/main/resources/assets/computercraft/lua/rom/programs/mkdir.lua +++ b/src/main/resources/assets/computercraft/lua/rom/programs/mkdir.lua @@ -5,5 +5,11 @@ if #tArgs < 1 then end local sNewDir = shell.resolve( tArgs[1] ) + +if fs.exists( sNewDir ) == true then + printError( "Destination exists" ) + return +end + fs.makeDir( sNewDir ) diff --git a/src/main/resources/assets/computercraft/lua/rom/programs/rename.lua b/src/main/resources/assets/computercraft/lua/rom/programs/rename.lua index 6054feb04..411019787 100644 --- a/src/main/resources/assets/computercraft/lua/rom/programs/rename.lua +++ b/src/main/resources/assets/computercraft/lua/rom/programs/rename.lua @@ -6,4 +6,9 @@ end local sSource = shell.resolve( tArgs[1] ) local sDest = shell.resolve( tArgs[2] ) + +if fs.exists( sDest ) == true then + printError( "Destination exists" ) +end + fs.move( sSource, sDest ) From 1415dd0dae0bb3c3d92c426e79308d05d0fff64d Mon Sep 17 00:00:00 2001 From: Wilma456 Date: Tue, 12 Sep 2017 20:43:07 +0200 Subject: [PATCH 2/6] Changes requested by dan200 --- .../assets/computercraft/lua/rom/programs/copy.lua | 10 ++++++---- .../assets/computercraft/lua/rom/programs/mkdir.lua | 2 +- .../assets/computercraft/lua/rom/programs/rename.lua | 2 +- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/main/resources/assets/computercraft/lua/rom/programs/copy.lua b/src/main/resources/assets/computercraft/lua/rom/programs/copy.lua index c70cd0a64..8158b84a5 100644 --- a/src/main/resources/assets/computercraft/lua/rom/programs/copy.lua +++ b/src/main/resources/assets/computercraft/lua/rom/programs/copy.lua @@ -10,12 +10,14 @@ local sDest = shell.resolve( tArgs[2] ) local tFiles = fs.find( sSource ) if #tFiles > 0 then for n,sFile in ipairs( tFiles ) do - if fs.exists( sDest ) == true then - printError( "Destination exists" ) - elseif fs.isDir( sDest ) then + if fs.isDir( sDest ) then fs.copy( sFile, fs.combine( sDest, fs.getName(sFile) ) ) elseif #tFiles == 1 then - fs.copy( sFile, sDest ) + if fs.exists( sDest ) then + printError( "Destination exists" ) + else + fs.copy( sFile, sDest ) + end else printError( "Cannot overwrite file multiple times" ) return diff --git a/src/main/resources/assets/computercraft/lua/rom/programs/mkdir.lua b/src/main/resources/assets/computercraft/lua/rom/programs/mkdir.lua index cace3bd55..ed6c5e378 100644 --- a/src/main/resources/assets/computercraft/lua/rom/programs/mkdir.lua +++ b/src/main/resources/assets/computercraft/lua/rom/programs/mkdir.lua @@ -6,7 +6,7 @@ end local sNewDir = shell.resolve( tArgs[1] ) -if fs.exists( sNewDir ) == true then +if fs.exists( sNewDir ) then printError( "Destination exists" ) return end diff --git a/src/main/resources/assets/computercraft/lua/rom/programs/rename.lua b/src/main/resources/assets/computercraft/lua/rom/programs/rename.lua index 411019787..f22561c26 100644 --- a/src/main/resources/assets/computercraft/lua/rom/programs/rename.lua +++ b/src/main/resources/assets/computercraft/lua/rom/programs/rename.lua @@ -7,7 +7,7 @@ end local sSource = shell.resolve( tArgs[1] ) local sDest = shell.resolve( tArgs[2] ) -if fs.exists( sDest ) == true then +if fs.exists( sDest ) then printError( "Destination exists" ) end From b8630f739aeaa1c56b23e557f619c57fb73314e6 Mon Sep 17 00:00:00 2001 From: "Wilma456 (Jakob0815)" Date: Wed, 13 Sep 2017 19:21:17 +0200 Subject: [PATCH 3/6] Add Check requested by dan200 --- .../resources/assets/computercraft/lua/rom/programs/mkdir.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/resources/assets/computercraft/lua/rom/programs/mkdir.lua b/src/main/resources/assets/computercraft/lua/rom/programs/mkdir.lua index ed6c5e378..98585d3f1 100644 --- a/src/main/resources/assets/computercraft/lua/rom/programs/mkdir.lua +++ b/src/main/resources/assets/computercraft/lua/rom/programs/mkdir.lua @@ -6,7 +6,7 @@ end local sNewDir = shell.resolve( tArgs[1] ) -if fs.exists( sNewDir ) then +if fs.exists( sNewDir ) and not fs.isDir(sNewDir) then printError( "Destination exists" ) return end From 5be2202b2eb367525fe9e48e0ea4a23fa76e64dc Mon Sep 17 00:00:00 2001 From: Wilma456 Date: Sat, 16 Sep 2017 16:06:27 +0200 Subject: [PATCH 4/6] Add read() to Filehandle --- .../core/apis/handles/EncodedInputHandle.java | 29 +++++++++++++++++++ .../assets/computercraft/lua/rom/apis/io.lua | 2 ++ 2 files changed, 31 insertions(+) diff --git a/src/main/java/dan200/computercraft/core/apis/handles/EncodedInputHandle.java b/src/main/java/dan200/computercraft/core/apis/handles/EncodedInputHandle.java index 1cc5141a1..75bc549b4 100644 --- a/src/main/java/dan200/computercraft/core/apis/handles/EncodedInputHandle.java +++ b/src/main/java/dan200/computercraft/core/apis/handles/EncodedInputHandle.java @@ -6,6 +6,8 @@ import dan200.computercraft.api.lua.LuaException; import javax.annotation.Nonnull; import java.io.*; +import static dan200.computercraft.core.apis.ArgumentHelper.*; + public class EncodedInputHandle extends HandleGeneric { private final BufferedReader m_reader; @@ -49,6 +51,7 @@ public class EncodedInputHandle extends HandleGeneric "readLine", "readAll", "close", + "read", }; } @@ -102,6 +105,32 @@ public class EncodedInputHandle extends HandleGeneric // close close(); return null; + case 3: + // read + checkOpen(); + try + { + double count = optNumber( args, 0, 1 ); + StringBuilder result = new StringBuilder( "" ); + for ( int i = 1; i < count+1; i++ ) { + int character = m_reader.read(); + if ( character == -1 ) { + if ( result.length() == 0 ) { + return null; + } + else { + return new Object[] { result.toString() }; + } + } + char c = (char) character; + result.append( c ); + } + return new Object[] { result.toString() }; + } + catch( IOException e ) + { + return null; + } default: return null; } diff --git a/src/main/resources/assets/computercraft/lua/rom/apis/io.lua b/src/main/resources/assets/computercraft/lua/rom/apis/io.lua index 4667bf50d..51bd5ac13 100644 --- a/src/main/resources/assets/computercraft/lua/rom/apis/io.lua +++ b/src/main/resources/assets/computercraft/lua/rom/apis/io.lua @@ -91,6 +91,8 @@ function open( _sPath, _sMode ) return file.readLine() elseif sFormat == "*a" then return file.readAll() + elseif _G.type( sFormat ) == "number" then + return file.read( sFormat ) else error( "Unsupported format" ) end From f20a7afa7fbc094d6c4136f2081542aae0947667 Mon Sep 17 00:00:00 2001 From: "Wilma456 (Jakob0815)" Date: Mon, 18 Sep 2017 15:22:44 +0200 Subject: [PATCH 5/6] Better Code --- .../core/apis/handles/EncodedInputHandle.java | 25 ++++++++----------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/src/main/java/dan200/computercraft/core/apis/handles/EncodedInputHandle.java b/src/main/java/dan200/computercraft/core/apis/handles/EncodedInputHandle.java index 75bc549b4..467330f63 100644 --- a/src/main/java/dan200/computercraft/core/apis/handles/EncodedInputHandle.java +++ b/src/main/java/dan200/computercraft/core/apis/handles/EncodedInputHandle.java @@ -5,6 +5,7 @@ import dan200.computercraft.api.lua.LuaException; import javax.annotation.Nonnull; import java.io.*; +import java.util.Arrays; import static dan200.computercraft.core.apis.ArgumentHelper.*; @@ -110,22 +111,16 @@ public class EncodedInputHandle extends HandleGeneric checkOpen(); try { - double count = optNumber( args, 0, 1 ); - StringBuilder result = new StringBuilder( "" ); - for ( int i = 1; i < count+1; i++ ) { - int character = m_reader.read(); - if ( character == -1 ) { - if ( result.length() == 0 ) { - return null; - } - else { - return new Object[] { result.toString() }; - } - } - char c = (char) character; - result.append( c ); + int count = optInt( args, 0, 1 ); + if( count <= 0 || count >= 1024 * 16 ) + { + throw new LuaException( "Count out of range" ); } - return new Object[] { result.toString() }; + char[] bytes = new char[ count ]; + count = m_reader.read( bytes ); + if( count < 0 ) return null; + String str = new String( bytes ); + return new Object[] { str }; } catch( IOException e ) { From 4fb0240a36c36d8a2b218a4e366f95a43a077016 Mon Sep 17 00:00:00 2001 From: Wilma456 Date: Sun, 24 Sep 2017 17:36:20 +0200 Subject: [PATCH 6/6] Changes suggested by SquidDev and update help file --- .../computercraft/core/apis/handles/EncodedInputHandle.java | 3 +-- src/main/resources/assets/computercraft/lua/rom/help/fs.txt | 3 ++- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/dan200/computercraft/core/apis/handles/EncodedInputHandle.java b/src/main/java/dan200/computercraft/core/apis/handles/EncodedInputHandle.java index 467330f63..9f722f797 100644 --- a/src/main/java/dan200/computercraft/core/apis/handles/EncodedInputHandle.java +++ b/src/main/java/dan200/computercraft/core/apis/handles/EncodedInputHandle.java @@ -5,7 +5,6 @@ import dan200.computercraft.api.lua.LuaException; import javax.annotation.Nonnull; import java.io.*; -import java.util.Arrays; import static dan200.computercraft.core.apis.ArgumentHelper.*; @@ -119,7 +118,7 @@ public class EncodedInputHandle extends HandleGeneric char[] bytes = new char[ count ]; count = m_reader.read( bytes ); if( count < 0 ) return null; - String str = new String( bytes ); + String str = new String( bytes, 0, count ); return new Object[] { str }; } catch( IOException e ) diff --git a/src/main/resources/assets/computercraft/lua/rom/help/fs.txt b/src/main/resources/assets/computercraft/lua/rom/help/fs.txt index 400e5ba6f..363273b12 100644 --- a/src/main/resources/assets/computercraft/lua/rom/help/fs.txt +++ b/src/main/resources/assets/computercraft/lua/rom/help/fs.txt @@ -22,6 +22,7 @@ Functions on files opened with mode "r": readLine() readAll() close() +read( number ) Functions on files opened with mode "w" or "a": write( string ) @@ -36,4 +37,4 @@ close() Functions on files opened with mode "wb" or "ab": write( byte ) flush() -close() \ No newline at end of file +close()