Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Extensions version 2.1 by KnightMiner
- -- Check if we are loading as an API or running the startup
- if not shell then
- -- Load the list of extensions
- local file = fs.open( "extensions.cfg", "r" )
- list = textutils.unserialize( file.readAll() )
- file.close()
- -- Function to check if a program is a file and what to run it with
- function program( name )
- local prog, file, noExt
- if name:find( '^".-"' ) then
- prog = list[string.match( name, '^".-%.([^. ]+)"' )]
- file = string.match( name, '^(".-%.[^. ]+")' )
- noExt = name:gsub( '^"(.-)%.[^. ]+"', '"%1"' )
- else
- prog = list[string.match( name, '^[^ ]-%.([^. ]+)' )]
- file = string.match( name, '^([^ ]-%.[^. ]+)' )
- noExt = name:gsub( '^([^ ]-)%.[^. ]+', '%1' )
- end
- if type( prog ) == "table" then
- if prog.noExt then
- return prog[1], file, noExt
- else
- return prog[1], file
- end
- else
- return prog, file
- end
- end
- -- Running as a program
- else
- -- validate args
- local args = { ... }
- if #args > 2 or args[1] == "usage" then
- print( "Usage: extensions [option] [option2]" )
- end
- -- Check for an extensions configuration file
- -- If it does not exist, ask to create it
- if not fs.exists( "extensions.cfg" ) then
- function create( default )
- print( "Creating extension.cfg" )
- local file = fs.open( "extensions.cfg", "w" )
- file.write( '{\n -- Lua table containing file extensions\n cfg = "edit"\n}' )
- file.close()
- if default then
- print( "Created default extensions.cfg" )
- else
- shell.run( "edit extensions.cfg" )
- end
- end
- if args[1] == "create" or args[1] == "create-default" then
- create( args[1] == "create-default" )
- elseif args[1] == "skip" then
- print( "Missing extensions.cfg file, cancelling" )
- return
- else
- print( "Missing extensions.cfg file, create it? (y/n)" )
- while true do
- local _, key = os.pullEvent( "key" )
- -- remove any stray character events
- os.startTimer(0.1)
- os.pullEvent()
- if key == keys.y then
- create()
- break
- elseif key == keys.n then
- print( "Cancelling extension loading" )
- return
- end
- end
- end
- end
- -- Check to see if we already ran the extension program
- -- And if so, ask if we should reload
- if extensions then
- if ( args[2] or args[1] ) == "load" then
- print( "Extensions already loaded, cancelling" )
- return
- elseif ( args[2] or args[1] ) == "reload" then
- print( "Reloading extension support" )
- else
- print( "Extensions already loaded, reload them? (y/n)" )
- while true do
- local _, key = os.pullEvent( "key" )
- -- remove any stray character events
- os.startTimer(0.1)
- os.pullEvent()
- if key == keys.y then
- print( "Reloading extension support" )
- break
- elseif key == keys.n then
- print( "Cancelling extension reloading" )
- return
- end
- end
- end
- os.unloadAPI( "extensions" )
- else
- -- Preserve the original method of running
- print( "Loading extension support" )
- shell.runRaw = shell.run
- end
- os.loadAPI( "extensions" )
- if not extensions.list then
- print( "Error loading extensions" )
- return
- end
- -- Override the shell.run function, so extensions run is used by default
- shell.run = function( name, ... )
- local program, progName, noExt = extensions.program( name )
- if program then
- if fs.exists( progName ) then
- return shell.runRaw( program, noExt or name, ... )
- else
- printError( "No such file" )
- return false
- end
- else
- return shell.runRaw( name, ... )
- end
- end
- print( "Extensions successfully loaded" )
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement