--"random" api
for k, v in pairs(fs.list("rom/apis")) do
setFenv(k, function() print(k) end)
end
--end api
--start program "useless"
os.loadAPI("random")
random.math()
--end program
>random --*enter*
math
>_
--"random" api
for k, v in pairs(fs.list("rom/apis")) do
setFenv(k, function() print(k) end)
end
--end api
--start program "useless"
os.loadAPI("random")
random.math()
--end program
>random --*enter*
math
>_
local f = {}
for k, v in pairs( fs.list( "rom/apis" ) ) do
f[k] = function() print(k) end
end
setmetatable(f, {__index = _G}
setfenv(f)
would something like this work?
local apis = {}
for k, v in pairs( fs.list( "rom/apis" ) )
apis[v] = print( v )
end
so that my api will contain api.redstone(), api.color(), etc. If I can figure this out, I can make the rest of my code work relatively easily.Yeah, exactly. I am updating wireless peripherals to control anything you want. Obviously stuff like colors and colours will never be used, but I want to reduce file size as much as possible. Also it's just an interesting concept, an api that builds itself…So you want your API to have all native-CC pure-Lua APIs inside your API? And if so, why?
local function createWireless( api )
local wireless = { }
for k, v in pairs( api ) do
wireless[ k ] = function( self, ... )
modem.transmit( self.ca, self.cb, { type = tostring( api ), method = k, args = { ... } } )
local event
repeat
event = { os.pullEvent() }
until event[ 5 ].type == tostring( api )
return unpack( event )
end
end
return wireless
end
local apis = { }
local tApis = fs.list( "rom/apis" )
for n,sFile in ipairs( tApis ) do
if string.sub( sFile, 1, 1 ) ~= "." then
local sPath = fs.combine( "rom/apis", sFile )
if not fs.isDir( sPath ) then
apis[ sPath ] = sPath
end
end
end
if not turtle then
apis[ "turtle" ] = {
craft = "craft",
forward = "forward",
back = "back",
up = "up",
down = "down",
turnLeft = "turnLeft",
turnRight = "turnRight",
select = "select",
getSelectedSlot = "getSelectedSlot",
getItemSpace = "getItemSpace",
equipLeft = "equipLeft",
equipRight = "equipRight",
attack = "attack",
attackUp = "attackUp",
dig = "dig",
digUp = "digUp",
digDown = "digDown",
place = "place",
placeUp = "placeUp",
placeDown = "placeDown",
detect = "detect",
detectUp = "detectUp",
detectDown = "detectDown",
compare = "compare",
compareUp = "compareUp",
compareDown = "compareDown",
compareTo = "compareTo",
drop = "drop",
dropUp = "dropUp",
dropDown = "dropDown",
suck = "suckdown",
refuel = "refuel",
getFuelLevel = "getFuelLevel",
getFuelLimit = "getFuelLimit",
transferTo = "transferTo",
}
end
apis.peripheral = nil
for k, v in pairs( apis ) do
if type( k ) == "string" and type( v ) == "table" then
function k ( ca, cb )
local per = createWireless( v )
local tbl = {
ca = ca,
cb = cb,
}
setmetatable( tbl, { __index = per } )
return tbl
end
end
end
function wrap()
return apis
end
--[[ Example of usage:
wc = wc.wrap()
turtle = wc.turtle( 204, 204 )
turtle.forward()
]]
local API = _G[APIname]
for k,v in pairs(_G) do
if type(v) == "table" then
--k is an API name
end
end
Yeah, exactly. I am updating wireless peripherals to control anything you want. Obviously stuff like colors and colours will never be used, but I want to reduce file size as much as possible. Also it's just an interesting concept, an api that builds itself…So you want your API to have all native-CC pure-Lua APIs inside your API? And if so, why?Spoiler
local function createWireless( api ) local wireless = { } for k, v in pairs( api ) do wireless[ k ] = function( self, ... ) modem.transmit( self.ca, self.cb, { type = tostring( api ), method = k, args = { ... } } ) local event repeat event = { os.pullEvent() } until event[ 5 ].type == tostring( api ) return unpack( event ) end end return wireless end local apis = { } local tApis = fs.list( "rom/apis" ) for n,sFile in ipairs( tApis ) do if string.sub( sFile, 1, 1 ) ~= "." then local sPath = fs.combine( "rom/apis", sFile ) if not fs.isDir( sPath ) then apis[ sPath ] = sPath end end end if not turtle then apis[ "turtle" ] = { craft = "craft", forward = "forward", back = "back", up = "up", down = "down", turnLeft = "turnLeft", turnRight = "turnRight", select = "select", getSelectedSlot = "getSelectedSlot", getItemSpace = "getItemSpace", equipLeft = "equipLeft", equipRight = "equipRight", attack = "attack", attackUp = "attackUp", dig = "dig", digUp = "digUp", digDown = "digDown", place = "place", placeUp = "placeUp", placeDown = "placeDown", detect = "detect", detectUp = "detectUp", detectDown = "detectDown", compare = "compare", compareUp = "compareUp", compareDown = "compareDown", compareTo = "compareTo", drop = "drop", dropUp = "dropUp", dropDown = "dropDown", suck = "suckdown", refuel = "refuel", getFuelLevel = "getFuelLevel", getFuelLimit = "getFuelLimit", transferTo = "transferTo", } end apis.peripheral = nil for k, v in pairs( apis ) do if type( k ) == "string" and type( v ) == "table" then function k ( ca, cb ) local per = createWireless( v ) local tbl = { ca = ca, cb = cb, } setmetatable( tbl, { __index = per } ) return tbl end end end function wrap() return apis end --[[ Example of usage: wc = wc.wrap() turtle = wc.turtle( 204, 204 ) turtle.forward() ]]
In this code I am already finding the name of the api, I meant how do I know what the user named my api that is doing thisgotta get creative.for k,v in pairs(_G) do if type(v) == "table" then --k is an API name end end
local oldLoad = os.loadAPI
function os.loadAPI(apiname)
oldLoad(apiname)
NAME_OF_API = apiname -- make a global var with the name of the api
end
-- Just have the user do this at the top of their code
function os.loadAPI(apiname) oldLoad(apiname) NAME_OF_API = apiname end
hmm… could do that. or I could just make my api returnWhy not hook into os.loadAPI()?local oldLoad = os.loadAPI function os.loadAPI(apiname) oldLoad(apiname) NAME_OF_API = apiname -- make a global var with the name of the api end -- Just have the user do this at the top of their code function os.loadAPI(apiname) oldLoad(apiname) NAME_OF_API = apiname end
return function() oldLoad(NAME_OF_API) end
-- API file
function init()
dostring("code that makes a table containing your api code. you now know what the table for your API is called"
end
-- Program
os.loadAPI("someobscurefile")
someobscurefile.init()
Good_API_Name.someFunction()
just passing your function the name of the API is enough.local API = _G[APIname]
Doesn't that bit of code need the api name?Why do you need to know the name though?
to add to that, why would you need to access your own API, from within itself, where you couldn't just invoke the function directly?Why would you need to access your own API, from within itself?
for k, v in pairs( table_of_apis ) do
function v(...) --rest of stuff
end
end
neither did
for k, v in pairs( table_of_apis ) do
v = function(...) --rest of stuff
end
end
gotta get creative.for k,v in pairs(_G) do if type(v) == "table" then --k is an API name end end
local env = getfenv() --api environment
for k,v in pairs(_G) do
if type(v) == "table" then --keep in mind that _G has more than just APIs
env[k] = function(...)
--function
end
end
end
I was stating code I had previously tried, before posting this topic. I need the name of my api to put inNote that i said "k is an API name" meaning you'd have to use that.gotta get creative.for k,v in pairs(_G) do if type(v) == "table" then --k is an API name end end
local env = getfenv() --api environment for k,v in pairs(_G) do if type(v) == "table" then --keep in mind that _G has more than just APIs env[k] = function(...) --function end end end
You still didn't answer why you needed the filename of your own API though.
local API = _G[APIname]
unless I misunderstand what exactly this is doing.
apis = fs.list( "rom/apis" )
if not fs.isDir( apis[k] ) and k:sub(1, 1) ~= "." then
--it's an api
end
which I pulled from the actual computercraft files.This would be the same asI was stating code I had previously tried, before posting this topic. I need the name of my api to put inunless I misunderstand what exactly this is doing.local API = _G[APIname]
local API = getfenv()
Provided it's run from within your API with os.loadAPI. But again, what is the point? You might aswell just doThis wouldn't even work, since you appear to be missing the for loop, and fs.isDir requires absolute paths. Even if those mistakes were corrected, it would still be heavier computation wise than my approach. Also, not all the APIs are present in the apis folder. os being a qucik example from the top of my head. Why would you only want default APIs though? I want to know what this code is actually forAlso, to get apis, since I only need the default ones… I use this:which I pulled from the actual computercraft files.apis = fs.list( "rom/apis" ) if not fs.isDir( apis[k] ) and k:sub(1, 1) ~= "." then --it's an api end
I am trying to update this. It seems too much trouble doing it this way, I'll just write out the apis I'm going to include. This discussion is closed unless someone finds an easy to understand way of doing things.-snip-
local id,message = rednet.receive()
rednet.send(id,loadstring(message)())