Advertisement
KnightMiner

[CC] extensions

Aug 28th, 2015
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.88 KB | None | 0 0
  1. -- Extensions version 2.1 by KnightMiner
  2.  
  3. -- Check if we are loading as an API or running the startup
  4. if not shell then
  5.   -- Load the list of extensions
  6.   local file = fs.open( "extensions.cfg", "r" )
  7.   list = textutils.unserialize( file.readAll() )
  8.   file.close()
  9.  
  10.   -- Function to check if a program is a file and what to run it with
  11.   function program( name )
  12.     local prog, file, noExt
  13.     if name:find( '^".-"' ) then
  14.       prog = list[string.match( name, '^".-%.([^. ]+)"' )]
  15.       file = string.match( name, '^(".-%.[^. ]+")' )
  16.       noExt = name:gsub( '^"(.-)%.[^. ]+"', '"%1"' )
  17.     else
  18.       prog = list[string.match( name, '^[^ ]-%.([^. ]+)' )]
  19.       file = string.match( name, '^([^ ]-%.[^. ]+)' )
  20.       noExt = name:gsub( '^([^ ]-)%.[^. ]+', '%1' )
  21.     end
  22.     if type( prog ) == "table" then
  23.       if prog.noExt then
  24.         return prog[1], file, noExt
  25.       else
  26.         return prog[1], file
  27.       end
  28.     else
  29.       return prog, file
  30.     end
  31.   end
  32.  
  33. -- Running as a program
  34. else
  35.   -- validate args
  36.   local args = { ... }
  37.   if #args > 2 or args[1] == "usage" then
  38.     print( "Usage: extensions [option] [option2]" )
  39.   end
  40.  
  41.   -- Check for an extensions configuration file
  42.   -- If it does not exist, ask to create it
  43.   if not fs.exists( "extensions.cfg" ) then
  44.     function create( default )
  45.       print( "Creating extension.cfg" )
  46.       local file = fs.open( "extensions.cfg", "w" )
  47.       file.write( '{\n  -- Lua table containing file extensions\n  cfg = "edit"\n}' )
  48.       file.close()
  49.       if default then
  50.         print( "Created default extensions.cfg" )
  51.       else
  52.         shell.run( "edit extensions.cfg" )
  53.       end
  54.     end
  55.     if args[1] == "create" or args[1] == "create-default" then
  56.       create( args[1] == "create-default" )
  57.     elseif args[1] == "skip" then
  58.       print( "Missing extensions.cfg file, cancelling" )
  59.       return
  60.     else
  61.       print( "Missing extensions.cfg file, create it? (y/n)" )
  62.       while true do
  63.         local _, key = os.pullEvent( "key" )
  64.         -- remove any stray character events
  65.         os.startTimer(0.1)
  66.         os.pullEvent()
  67.         if key == keys.y then
  68.           create()
  69.           break
  70.         elseif key == keys.n then
  71.           print( "Cancelling extension loading" )
  72.           return
  73.         end
  74.       end
  75.     end
  76.   end
  77.  
  78.   -- Check to see if we already ran the extension program
  79.   -- And if so, ask if we should reload
  80.   if extensions then
  81.     if ( args[2] or args[1] ) == "load" then
  82.       print( "Extensions already loaded, cancelling" )
  83.       return
  84.     elseif ( args[2] or args[1] ) == "reload" then
  85.       print( "Reloading extension support" )
  86.     else
  87.       print( "Extensions already loaded, reload them? (y/n)" )
  88.       while true do
  89.         local _, key = os.pullEvent( "key" )
  90.         -- remove any stray character events
  91.         os.startTimer(0.1)
  92.         os.pullEvent()
  93.         if key == keys.y then
  94.           print( "Reloading extension support" )
  95.           break
  96.         elseif key == keys.n then
  97.           print( "Cancelling extension reloading" )
  98.           return
  99.         end
  100.       end
  101.     end
  102.     os.unloadAPI( "extensions" )
  103.   else
  104.     -- Preserve the original method of running
  105.     print( "Loading extension support" )
  106.     shell.runRaw = shell.run
  107.   end
  108.  
  109.   os.loadAPI( "extensions" )
  110.   if not extensions.list then
  111.     print( "Error loading extensions" )
  112.     return
  113.   end
  114.  
  115.   -- Override the shell.run function, so extensions run is used by default
  116.   shell.run = function( name, ... )
  117.     local program, progName, noExt = extensions.program( name )
  118.     if program then
  119.       if fs.exists( progName ) then
  120.         return shell.runRaw( program, noExt or name, ... )
  121.       else
  122.         printError( "No such file" )
  123.         return false
  124.       end
  125.     else
  126.       return shell.runRaw( name, ... )
  127.     end
  128.   end
  129.  
  130.   print( "Extensions successfully loaded" )
  131. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement