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

os.loadAPI add to a variable or redirecting

Started by Syli, 23 December 2012 - 05:58 AM
Syli #1
Posted 23 December 2012 - 06:58 AM
Hey,

i would love to make file extantions, because i would love to have the syntax highlights in my LUA Editor, the problem is that i dont want to call my functions like:


os.loadAPI("foo.lua")

foo.lua.bar()
-- instead i want:
-- foo.bar()

did somebody has a idear to realize that?

i tried:

foo = os.loadAPI("foo.lua")
foo.bar()
-- get nil
-- dont work because os.loadAPI returns true/false

dont know what to do, else than rename any files befor testing a script, eventually it will help if somebody has an editor who dont cares about file extantions, at the moment i use the Decoder Editor (From Unkown Worlds - Natural Selection) - i dont like Notepad++


thanks for you help
Syli
Lyqyd #2
Posted 23 December 2012 - 07:19 AM
If the file isn't in rom:


fs.move("foo.lua","foo")
os.loadAPI("foo")
fs.move("foo","foo.lua")

You can actually automate this process with a relatively simple function, so that you can load any of your APIs this way.


function loadLuaAPI(path)
  if path then
    if string.match(fs.getName(path), "(%.lua)$") and fs.exists(path) and not fs.isReadOnly(path) then
      fs.move(path, string.match(path, "(.*)%.lua$"))
      local success = os.loadAPI(string.match(path, "(.*)%.lua$"))
      fs.move(string.match(path, "(.*)%.lua$"), path)
      return success
    elseif fs.exists(path) then
      return os.loadAPI(path)
    end
  end
  return false
end

Also, SublimeText doesn't (and most other good text editors don't) care about file extension–you can select whatever syntax highlighting you please.
Doyle3694 #3
Posted 23 December 2012 - 07:57 AM
also,
os.loadAPI("foo.lua")
foo = foo.lua
foo.bar()
Syli #4
Posted 23 December 2012 - 08:04 AM
that looks more like a hack, to work around a problem, but thats fine with me, after implementing the fs.move my loadAPI(apiName) looks like:


function internalLoadAPI(apiName)
  local apiPath = getSysRootPath() .. getApiPath() .. apiName .. ".api"
  local apiTempPath = string.match(apiPath, "(.*)%.api$")
  local bSuccess = false

  if not fs.exists(apiPath) then
	return false
  end

  if fs.isReadOnly(apiPath) then
	fs.copy(apiPath, apiTempPath)
      bSuccess = os.loadAPI(apiTempPath)
	fs.delete(apiTempPath)
  else
	fs.move(apiPath, apiTempPath)
		 bSuccess = os.loadAPI(apiTempPath)
	fs.move(apiTempPath, apiPath)
  end
  return bSuccess
end
my Editor dont care about extantions i can choose a highlight whatever extation i have, except for no extantion at all, i think its a bug, i reported that.
Syli #5
Posted 23 December 2012 - 08:06 AM
also,
os.loadAPI("foo.lua")
foo = foo.lua
foo.bar()

= nil
PixelToast #6
Posted 23 December 2012 - 08:07 AM
you cant do
foo.lua
or

foo.lua.bar
without indexing foo
you might be able to do something like

local foo=(getfenv())["foo.lua"]
foo.bar()
and it sould work
Syli #7
Posted 23 December 2012 - 09:17 AM
yeha i got it, no "." in os.load() i use my internalLoadAPI function to load them now, its a hack (little bit dirty) because of fs.move() / fs.copy() but it works fine, i will test the getfenv() aswell
ChunLing #8
Posted 23 December 2012 - 05:22 PM
Can't you just program your editor to default to Lua highlighting when opening a file without an extension?
Doyle3694 #9
Posted 24 December 2012 - 03:56 AM
or just use the best editor in the world, notepad++ //commercial
Ferdi265 #10
Posted 26 March 2013 - 04:35 AM
I wrote this code thingy just now, it allows you to load APIs with extension AND store them in a variable/table
Oh and the Header in my Code, that's just some kind of Signature of mine. You can load this file as an API with the standard os.loadAPI and then use the <how you named the file>.load function

For anyone who wants the File, here's the Pastebin: http://pastebin.com/zgXu9G3d


-- //265 265utils API-Loader
ver = 1.0

function load(filepath)
  os.loadAPI(filepath)
  return _G[fs.getName(filepath)]
end

-- This Thing is just a redirect to os.unloadAPI
function unload(filepath)
  os.unloadAPI(filename)
end

EDIT: Use it like this:

os.loadAPI("Path/To/My/API/File123")
local apivariable = File123.load("Path/to/someApi.extension")
local stuff = apivariable.getSomeStuff()
print(stuff)
local apiVar = apivariable.someVariable
print(apiVar)