awsumben13

Package

May 29th, 2014
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.13 KB | None | 0 0
  1.  
  2. local commonFileData = [[
  3.  
  4. _G.NovaPackageData = _G.NovaPackageData or { } -- so packages aren't loaded multiple times
  5.  
  6. local packages = { }
  7.  
  8. local find
  9. find = function( name, path )
  10.     if _G.NovaPackageData[name] then
  11.         return _G.NovaPackageData[name]
  12.     end
  13.     path = path or ""
  14.     local files = fs.list( path )
  15.     for i = 1,#files do
  16.         if fs.isDir( path .. "/" .. files[ i ] ) then
  17.             if path .. "/" .. files[ i ] ~= "/rom" then
  18.                 local p = find( name, path .. "/" .. files[ i ] )
  19.                 if p then
  20.                     return p
  21.                 end
  22.             end
  23.         else
  24.             local h = fs.open( path .. "/" .. files[ i ], "r" )
  25.             if h then
  26.                 local content = h.readAll( )
  27.                 h.close( )
  28.                 if content:sub( 1, 25 ) == "-- Nova package(library)\n" then
  29.                     if content:sub( 26, 28 + #name ) == "-- " .. name then
  30.                         local f, err = loadstring( content, files[ i ] )
  31.                         if f then
  32.                             setfenv( f, getfenv( ) )
  33.                             local ok, data = pcall( f )
  34.                             if ok then
  35.                                 _G.NovaPackageData[name] = data
  36.                                 return data
  37.                             else
  38.                                 error( data, 0 )
  39.                             end
  40.                         else
  41.                             error( err, 0 )
  42.                         end
  43.                     end
  44.                 end
  45.             end
  46.         end
  47.     end
  48. end
  49.  
  50. for i = 1,#dependencies do
  51.     local pack = find( dependencies[ i ] )
  52.     if pack then
  53.         packages[ dependencies[ i ] ] = pack
  54.     else
  55.         error( "requires \"" .. dependencies[ i ] .. "\" package", 0 )
  56.     end
  57. end
  58.  
  59. local package = { }
  60. local env = { }
  61. setmetatable( env, { __index = getfenv( ) } )
  62. _G.NovaPackageData[name] = package
  63.  
  64. env.package = package
  65. env.require = function( file )
  66.     local pos = file:find( "%." )
  67.     if pos then
  68.         local pack = file:sub( 1, pos - 1 )
  69.         if packages[ pack ] then
  70.             return packages[ pack ].run( file:sub( pos + 1 ) )
  71.         else
  72.             error( "no such package \"" .. pack .. "\"", 0 )
  73.         end
  74.     else
  75.         return package.run( file )
  76.     end
  77. end
  78.  
  79. function package.read( name )
  80.     if files[ name:lower( ) ] then
  81.         return files[ name:lower( ) ]
  82.     end
  83.     return false
  84. end
  85. function package.run( file, ... )
  86.     local content = files[ file:lower( ) ]
  87.     if content then
  88.         local f, err = loadstring( content, file )
  89.         if f then
  90.             setfenv( f, env )
  91.             local ok, data = pcall( f, ... )
  92.             if ok then
  93.                 return data
  94.             else
  95.                 error( data, 0 )
  96.             end
  97.         else
  98.             error( err,0 )
  99.         end
  100.     else
  101.         error( "could not read file", 0 )
  102.     end
  103. end
  104. function package.getName( )
  105.     return name
  106. end
  107. function package.getDependencies( ) -- clones the table so you can't change the local one:
  108.     local t = { }
  109.     for i = 1,#dependencies do
  110.         t[ i ] = dependencies[ i ]
  111.     end
  112.     return t
  113. end
  114. ]]
  115.  
  116. local runFileData = [[
  117. return package.run( mainFile, ... )
  118. ]]
  119.  
  120. function create( name )
  121.     if type( name ) ~= "string" then
  122.         error( "string name expected, got " .. type( name ), 2 )
  123.     end
  124.     local p = { }
  125.     p.name = name
  126.     p.files = { }
  127.     p.requires = { }
  128.     function p:addFile( name, content )
  129.         if type( name ) ~= "string" then
  130.             error( "string name expected, got " .. type( name ), 2 )
  131.         end
  132.         if type( content ) ~= "string" then
  133.             error( "string content (or path) expected [#2], got " .. type( content ), 2 )
  134.         end
  135.         if fs.exists( content ) and not fs.isDir( content ) then
  136.             local h = fs.open( content, "r" )
  137.             if h then
  138.                 content = h.readAll( )
  139.                 h.close( )
  140.             else
  141.                 error( "could not read file", 0 )
  142.             end
  143.         end
  144.         self.files[name:lower( )] = content
  145.     end
  146.     function p:addDependency( pack )
  147.         if type( pack ) ~= "string" then
  148.             error( "string pack expected, got " .. type( pack ), 2 )
  149.         end
  150.         self.requires[ #self.requires + 1 ] = pack
  151.     end
  152.     function p:save( path )
  153.         if type( path ) ~= "string" then
  154.             error( "string path expected, got " .. type( path ), 2 )
  155.         end
  156.         local content = "-- Nova package(library)\n-- " .. self.name .. "\n"
  157.         content = content .. "local name = " .. string.format( "%q", self.name ) .. "\n"
  158.         content = content .. "local files = " .. textutils.serialize( self.files ) .. "\n"
  159.         content = content .. "local dependencies = " .. textutils.serialize( self.requires ) .. "\n"
  160.         content = content .. commonFileData
  161.         content = content .. "return package\n"
  162.         local h = fs.open( path, "w" )
  163.         if h then
  164.             h.write( content )
  165.             h.close( )
  166.         else
  167.             error( "could not write to file", 0 )
  168.         end
  169.     end
  170.     function p:saveExecutable( path, mainFile )
  171.         if type( path ) ~= "string" then
  172.             error( "string path expected, got " .. type( path ), 2 )
  173.         end
  174.         if type( mainFile ) ~= "string" then
  175.             error( "string main file expected [#2], got " .. type( mainFile ), 2 )
  176.         end
  177.         local content = "-- Nova package(library)\n"
  178.         content = content .. "local mainFile = " .. string.format( "%q", mainFile ) .. "\n"
  179.         content = content .. "local name = " .. string.format( "%q", self.name ) .. "\n"
  180.         content = content .. "local files = " .. textutils.serialize( self.files ) .. "\n"
  181.         content = content .. "local dependencies = " .. textutils.serialize( self.requires ) .. "\n"
  182.         content = content .. commonFileData
  183.         content = content .. runFileData
  184.         local h = fs.open( path, "w" )
  185.         if h then
  186.             h.write( content )
  187.             h.close( )
  188.         else
  189.             error( "could not write to file", 0 )
  190.         end
  191.     end
  192.     return p
  193. end
  194.  
  195. function load( path )
  196.     if type( path ) ~= "string" then
  197.         error( "string path expected, got " .. type( path ), 2 )
  198.     end
  199.     local f, err = loadfile( path )
  200.     if f then
  201.         setfenv( f, getfenv( ) )
  202.         local ok, data = pcall( f )
  203.         if ok then
  204.             return data
  205.         else
  206.             error( data, 0 )
  207.         end
  208.     else
  209.         error( err, 0 )
  210.     end
  211. end
  212.  
  213. local f
  214. f = function( name, path )
  215.     if _G.NovaPackageData[name] then
  216.         return _G.NovaPackageData[name]
  217.     end
  218.     path = path or ""
  219.     local files = fs.list( path )
  220.     for i = 1,#files do
  221.         if fs.isDir( path .. "/" .. files[ i ] ) then
  222.             if path .. "/" .. files[ i ] ~= "/rom" then
  223.                 local p = f( name, path .. "/" .. files[ i ] )
  224.                 if p then
  225.                     return p
  226.                 end
  227.             end
  228.         else
  229.             local h = fs.open( path .. "/" .. files[ i ], "r" )
  230.             if h then
  231.                 local content = h.readAll( )
  232.                 h.close( )
  233.                 if content:sub( 1, 25 ) == "-- Nova package(library)\n" then
  234.                     if content:sub( 26, 28 + #name ) == "-- " .. name then
  235.                         return path .. "/" .. files[i]
  236.                     end
  237.                 end
  238.             end
  239.         end
  240.     end
  241. end
  242.  
  243. function find( name )
  244.     return f( name )
  245. end
Advertisement
Add Comment
Please, Sign In to add comment