This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
BigSHinyToys's profile picture

specifing name of api example: os.loadAPI("/apis/api1.lua","gui")

Started by BigSHinyToys, 29 March 2013 - 01:56 PM
BigSHinyToys #1
Posted 29 March 2013 - 02:56 PM
As title a small modification to os.loadAPI to allow user specified api name. Only a small modification is needed in os.loadAPI and it would be back words compatible with previous functions.
Spoiler

function os.loadAPI( _sPath , _sName)
    local sName = _sName or fs.getName( _sPath )
    if tAPIsLoading[sName] == true then
	    printError( "API "..sName.." is already being loaded" )
	    return false
    end
    tAPIsLoading[sName] = true
	   
    local tEnv = {}
    setmetatable( tEnv, { __index = _G } )
    local fnAPI, err = loadfile( _sPath )
    if fnAPI then
	    setfenv( fnAPI, tEnv )
	    fnAPI()
    else
	    printError( err )
	    tAPIsLoading[sName] = nil
	    return false
    end
   
    local tAPI = {}
    for k,v in pairs( tEnv ) do
	    tAPI[k] =  v
    end
   
    _G[sName] = tAPI   
    tAPIsLoading[sName] = nil
    return true
end
Shnupbups #2
Posted 29 March 2013 - 03:25 PM
I think this is a pretty good idea!
FuuuAInfiniteLoop(F.A.I.L) #3
Posted 29 March 2013 - 03:30 PM
you can do it like this…

function customname(api)
 local a = os.loadAPI(api)
 return a
end
newname = customname(apiname)

and you can use newname like apiname
BigSHinyToys #4
Posted 29 March 2013 - 03:37 PM
you can do it like this…

function customname(api)
local a = os.loadAPI(api)
return a
end
newname = customname(apiname)

and you can use newname like apiname
I don't know how that would work could you explain it in more detail ? I was under the impression that to use the inbuilt system access to the local in bios table tAPIsLoading was needed ?? or the os.unloadAPI function won't work correctly and it will allow apis to be over ridden.
Lyqyd #5
Posted 29 March 2013 - 03:41 PM
This suggestion falls under the already-possible-enhancements clause of the Suggestions Not to Make post; locked.


function fancyAPILoad(path, name)
  fs.makeDir("/tmp")
  fs.move(path, "/tmp/"..name)
  os.loadAPI("/tmp/"..name)
  fs.move("/tmp/"..name, path)
  fs.delete("/tmp")
end