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

A better way to load APIs

Started by CrazedProgrammer, 10 May 2015 - 10:08 AM
CrazedProgrammer #1
Posted 10 May 2015 - 12:08 PM
The default way to load APIs in ComputerCraft is using os.loadAPI, which is pretty good but it has some problems.
First of all, it loads APIs into the global environment, and that is not a good practice.
You also can't properly load APIs with a .lua extention and you can't load APIs from strings.

With this piece of code you can load APIs into local variables from files or strings.
It is compatible with almost all APIs made for ComputerCraft.
I recommend you copy and paste this into your program, because it is not supposed to be loaded as an API.

Code:

function loadAPI(path)
	local env = {}
	setmetatable(env, {__index = _G})
	local fn, err = loadfile(path, env)
	if fn then
		local ok, err = pcall(fn)
		if not ok then
			error(err)
		end
	else
		error(err)
	end
	local api = {}
	for k,v in pairs(env) do
		if k ~= "_ENV" then
			api[k] =  v
		end
	end
	return api
end

function loadAPIString(name, str)
	local env = {}
	setmetatable(env, {__index = _G})
	local fn, err = loadstring(str, name)
	if fn then
		setfenv(fn, env)
		local ok, err = pcall(fn)
		if not ok then
			error(err)
		end
	else
		error(err)
	end
	local api = {}
	for k,v in pairs(env) do
		if k ~= "_ENV" then
			api[k] =  v
		end
	end
	return api
end
Usage:
loadAPI(apipath) - Returns the API if successful, or else it errors.
loadAPIString(apiname, apistring) - Returns the API if successful, or else it errors.

If you want to load an API without having to put it in an external file,
you can use my converter which converts Lua source code into a Lua string that you can paste into your program.

Screenshot:


Download
It requires Microsoft .NET 4.0 to be installed on your computer.

Buttons:
Paste - Pastes the Lua source code from the clipboard into the input textbox.
Convert - Converts the Lua source code to a Lua string.
Copy - Copies the output Lua string into the clipboard.

I hope it helps you with making programs.
If you have any questions, bug reports or suggestions then please leave a reply.
Edited on 12 June 2015 - 01:14 PM
FUNCTION MAN! #2
Posted 10 May 2015 - 01:38 PM
Cool.
Not cool that you need .NET for your thing. What about us *nix users?
theoriginalbit #3
Posted 10 May 2015 - 01:49 PM
Not cool that you need .NET for your thing. What about us *nix users?
You're better off using something like a Lua minifier anyway, it removes unneeded whitespace, unlike CrazedProgrammer's tool which keeps them unnecessary; just look for characters such as \n and \t.
Edited on 10 May 2015 - 11:50 AM
CrazedProgrammer #4
Posted 11 May 2015 - 08:57 AM
Cool.
Not cool that you need .NET for your thing. What about us *nix users?
Sorry for that, it was the easiest way for me.

Not cool that you need .NET for your thing. What about us *nix users?
You're better off using something like a Lua minifier anyway, it removes unneeded whitespace, unlike CrazedProgrammer's tool which keeps them unnecessary; just look for characters such as \n and \t.
It wasn't supposed to minify programs, but yes using a minifier before using my tool saves space.
theoriginalbit #5
Posted 11 May 2015 - 09:50 AM
It wasn't supposed to minify programs, but yes using a minifier before using my tool saves space.
your tool looks to be a minifier that puts quotes around it, so why not just have it perform as a minifier? I would actually be quite interested in seeing the source, I can't test it myself (because .NET) and I have a feeling I know a few areas where your tool will fail at creating a valid string.
CrazedProgrammer #6
Posted 11 May 2015 - 05:13 PM
It wasn't supposed to minify programs, but yes using a minifier before using my tool saves space.
your tool looks to be a minifier that puts quotes around it, so why not just have it perform as a minifier? I would actually be quite interested in seeing the source, I can't test it myself (because .NET) and I have a feeling I know a few areas where your tool will fail at creating a valid string.
I'm working on other things right now, but it could be used as a minifier.
The only thing it does right now is putting it around quotes and converting escape characters.
You can view the source MainForm.cs file (not the design) here:
http://pastebin.com/QMKNEqUt
theoriginalbit #7
Posted 12 May 2015 - 02:22 AM
-snip-
Yeah so you definitely have issues in there. Take this code, put it in your converter, put the resulting string in a program, and try and run it in ComputerCraft

--[[
Some program v0.1
]]
local a = "hello"
local b = "world"
local c = a + b -- this will be 'hello world'
print(c)
print("\""..c.."\"")
Based off the code you provided there'll be a few issues. An ideal output for this should be

[[local a = "hello" local b = "world" local c = a + b print(c) print("\""..c.."\"")]]
Edited on 12 May 2015 - 12:22 AM
CrazedProgrammer #8
Posted 12 May 2015 - 08:19 PM
-snip-
Yeah so you definitely have issues in there. Take this code, put it in your converter, put the resulting string in a program, and try and run it in ComputerCraft

--[[
Some program v0.1
]]
local a = "hello"
local b = "world"
local c = a + b -- this will be 'hello world'
print(c)
print("\""..c.."\"")
Based off the code you provided there'll be a few issues. An ideal output for this should be

[[local a = "hello" local b = "world" local c = a + b print(c) print("\""..c.."\"")]]
The converter isn't a minifier, and it isn't supposed to be.
It only converts escape characters such as \n and \t so it makes a valid Lua string.
If you want to minify it, use lua minifier (as you said).
I'm working on other things right now so I won't make a minifier.
Edited on 12 May 2015 - 06:19 PM
theoriginalbit #9
Posted 12 May 2015 - 11:22 PM
The converter isn't a minifier, and it isn't supposed to be.
It only converts escape characters such as \n and \t so it makes a valid Lua string.
If you want to minify it, use lua minifier (as you said).
I'm working on other things right now so I won't make a minifier.
Except the entire goal of your program fails with the code I supplied.
minebuild02 #10
Posted 09 June 2015 - 08:53 AM
What about us *nix users?
Get Mono.