Advertisement
Guest User

Untitled

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