Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local commonFileData = [[
- _G.NovaPackageData = _G.NovaPackageData or { } -- so packages aren't loaded multiple times
- local packages = { }
- local find
- find = function( name, path )
- if _G.NovaPackageData[name] then
- return _G.NovaPackageData[name]
- end
- path = path or ""
- local files = fs.list( path )
- for i = 1,#files do
- if fs.isDir( path .. "/" .. files[ i ] ) then
- if path .. "/" .. files[ i ] ~= "/rom" then
- local p = find( name, path .. "/" .. files[ i ] )
- if p then
- return p
- end
- end
- else
- local h = fs.open( path .. "/" .. files[ i ], "r" )
- if h then
- local content = h.readAll( )
- h.close( )
- if content:sub( 1, 25 ) == "-- Nova package(library)\n" then
- if content:sub( 26, 28 + #name ) == "-- " .. name then
- local f, err = loadstring( content, files[ i ] )
- if f then
- setfenv( f, getfenv( ) )
- local ok, data = pcall( f )
- if ok then
- _G.NovaPackageData[name] = data
- return data
- else
- error( data, 0 )
- end
- else
- error( err, 0 )
- end
- end
- end
- end
- end
- end
- end
- for i = 1,#dependencies do
- local pack = find( dependencies[ i ] )
- if pack then
- packages[ dependencies[ i ] ] = pack
- else
- error( "requires \"" .. dependencies[ i ] .. "\" package", 0 )
- end
- end
- local package = { }
- local env = { }
- setmetatable( env, { __index = getfenv( ) } )
- _G.NovaPackageData[name] = package
- env.package = package
- env.require = function( file )
- local pos = file:find( "%." )
- if pos then
- local pack = file:sub( 1, pos - 1 )
- if packages[ pack ] then
- return packages[ pack ].run( file:sub( pos + 1 ) )
- else
- error( "no such package \"" .. pack .. "\"", 0 )
- end
- else
- return package.run( file )
- end
- end
- function package.read( name )
- if files[ name:lower( ) ] then
- return files[ name:lower( ) ]
- end
- return false
- end
- function package.run( file, ... )
- local content = files[ file:lower( ) ]
- if content then
- local f, err = loadstring( content, file )
- if f then
- setfenv( f, env )
- local ok, data = pcall( f, ... )
- if ok then
- return data
- else
- error( data, 0 )
- end
- else
- error( err,0 )
- end
- else
- error( "could not read file", 0 )
- end
- end
- function package.getName( )
- return name
- end
- function package.getDependencies( ) -- clones the table so you can't change the local one:
- local t = { }
- for i = 1,#dependencies do
- t[ i ] = dependencies[ i ]
- end
- return t
- end
- ]]
- local runFileData = [[
- return package.run( mainFile, ... )
- ]]
- function create( name )
- if type( name ) ~= "string" then
- error( "string name expected, got " .. type( name ), 2 )
- end
- local p = { }
- p.name = name
- p.files = { }
- p.requires = { }
- function p:addFile( name, content )
- if type( name ) ~= "string" then
- error( "string name expected, got " .. type( name ), 2 )
- end
- if type( content ) ~= "string" then
- error( "string content (or path) expected [#2], got " .. type( content ), 2 )
- end
- if fs.exists( content ) and not fs.isDir( content ) then
- local h = fs.open( content, "r" )
- if h then
- content = h.readAll( )
- h.close( )
- else
- error( "could not read file", 0 )
- end
- end
- self.files[name:lower( )] = content
- end
- function p:addDependency( pack )
- if type( pack ) ~= "string" then
- error( "string pack expected, got " .. type( pack ), 2 )
- end
- self.requires[ #self.requires + 1 ] = pack
- end
- function p:save( path )
- if type( path ) ~= "string" then
- error( "string path expected, got " .. type( path ), 2 )
- end
- local content = "-- Nova package(library)\n-- " .. self.name .. "\n"
- content = content .. "local name = " .. string.format( "%q", self.name ) .. "\n"
- content = content .. "local files = " .. textutils.serialize( self.files ) .. "\n"
- content = content .. "local dependencies = " .. textutils.serialize( self.requires ) .. "\n"
- content = content .. commonFileData
- content = content .. "return package\n"
- local h = fs.open( path, "w" )
- if h then
- h.write( content )
- h.close( )
- else
- error( "could not write to file", 0 )
- end
- end
- function p:saveExecutable( path, mainFile )
- if type( path ) ~= "string" then
- error( "string path expected, got " .. type( path ), 2 )
- end
- if type( mainFile ) ~= "string" then
- error( "string main file expected [#2], got " .. type( mainFile ), 2 )
- end
- local content = "-- Nova package(library)\n"
- content = content .. "local mainFile = " .. string.format( "%q", mainFile ) .. "\n"
- content = content .. "local name = " .. string.format( "%q", self.name ) .. "\n"
- content = content .. "local files = " .. textutils.serialize( self.files ) .. "\n"
- content = content .. "local dependencies = " .. textutils.serialize( self.requires ) .. "\n"
- content = content .. commonFileData
- content = content .. runFileData
- local h = fs.open( path, "w" )
- if h then
- h.write( content )
- h.close( )
- else
- error( "could not write to file", 0 )
- end
- end
- return p
- end
- function load( path )
- if type( path ) ~= "string" then
- error( "string path expected, got " .. type( path ), 2 )
- end
- local f, err = loadfile( path )
- if f then
- setfenv( f, getfenv( ) )
- local ok, data = pcall( f )
- if ok then
- return data
- else
- error( data, 0 )
- end
- else
- error( err, 0 )
- end
- end
- local f
- f = function( name, path )
- if _G.NovaPackageData[name] then
- return _G.NovaPackageData[name]
- end
- path = path or ""
- local files = fs.list( path )
- for i = 1,#files do
- if fs.isDir( path .. "/" .. files[ i ] ) then
- if path .. "/" .. files[ i ] ~= "/rom" then
- local p = f( name, path .. "/" .. files[ i ] )
- if p then
- return p
- end
- end
- else
- local h = fs.open( path .. "/" .. files[ i ], "r" )
- if h then
- local content = h.readAll( )
- h.close( )
- if content:sub( 1, 25 ) == "-- Nova package(library)\n" then
- if content:sub( 26, 28 + #name ) == "-- " .. name then
- return path .. "/" .. files[i]
- end
- end
- end
- end
- end
- end
- function find( name )
- return f( name )
- end
Advertisement
Add Comment
Please, Sign In to add comment