←  APIs and Utilities

ComputerCraft | Programmable Computers for Minecraft

»

cc-require: Standards compatible require f...

Oddstr13's Photo Oddstr13 17 Jul 2015

cc-require

A standards compatible implementation of require,
and part of the package library, as specified by the Lua Manual

Allows for compatability with many non-CC Lua programs and API.

Examples

Standards compatible:

local api = {}
function api.hello(what)
    print("Hello, " .. what .. "!")
end

return api

The ComputerCraft way:

function hello(what)
    print("Hello, " .. what .. "!")
end

The main difference is that with standard require, APIs return a table
containing the API, where as in ComputerCraft's os.loadAPI, the global
environment of the API file is put into _G[api_filename].

This implementation of require allows you to import either type API.
It will however not place it in the global environment.

-- API file: /lib/api.lua
local api = require("api")

api.hello("World")

-- API file: /lib/test/api.lua
local api = require("test.api")

api.hello("ComputerCraft")

The search paths, where require looks for the API, can be found at the top of require.lua

Contribute

Download and installation
Resource pack can be downloaded from the Bitbucket download section.
Put the downloaded zip into the directory resourcepacks/, innside your minecraft directory, creating it if needed.
Quote

MKlegoman357's Photo MKlegoman357 17 Jul 2015

Very nice! You should probably add compatibility with Lua 5.2 though, because CC is already starting to convert to it and it will be updated in later CC versions. I'd recommend supporting both: Lua 5.1 and Lua 5.2.
Quote

Exerro's Photo Exerro 17 Jul 2015

Very nice, I've always wanted require() in computercraft. My one suggestion is that you don't require people to download the resource pack, maybe make a single file that can be downloaded that adds the necessary globals when run? If I were on a server, or wanted to make a program that used this and distribute it, needing a custom resource pack wouldn't work.
Quote

Oddstr13's Photo Oddstr13 18 Jul 2015

View PostMKlegoman357, on 17 July 2015 - 07:55 PM, said:

Very nice! You should probably add compatibility with Lua 5.2 though, because CC is already starting to convert to it and it will be updated in later CC versions. I'd recommend supporting both: Lua 5.1 and Lua 5.2.
What exactly is missing before this is Lua 5.2 compatable?
If there is anything missing, please create a issue on Bitbucket, with details outlining what the problem is, and how to correct it.



View Postawsumben13, on 17 July 2015 - 07:57 PM, said:

Very nice, I've always wanted require() in computercraft. My one suggestion is that you don't require people to download the resource pack, maybe make a single file that can be downloaded that adds the necessary globals when run? If I were on a server, or wanted to make a program that used this and distribute it, needing a custom resource pack wouldn't work.
The resource pack is provided as a convinience, as it is needed for server(and client)-wide install of the require function.
You can allways just download and run the require.lua file (raw file).
I'll consider making a pastebin that downloads and installs the function into the global environment.
Quote

Exerro's Photo Exerro 18 Jul 2015

View PostOddstr13, on 18 July 2015 - 01:38 AM, said:

View PostMKlegoman357, on 17 July 2015 - 07:55 PM, said:

-snip-
What exactly is missing before this is Lua 5.2 compatable?
If there is anything missing, please create a issue on Bitbucket, with details outlining what the problem is, and how to correct it.

I'd assume it's just the setfenv and loadfile calls that wouldn't be compatible. In lua 5.2, you need to use load( string, name, nil (something to do with bytecode, ignore this), env ).

View PostOddstr13, on 18 July 2015 - 01:38 AM, said:

View Postawsumben13, on 17 July 2015 - 07:57 PM, said:

-snip-
The resource pack is provided as a convinience, as it is needed for server(and client)-wide install of the require function.
You can allways just download and run the require.lua file (raw file).
I'll consider making a pastebin that downloads and installs the function into the global environment.

Ah, silly me, didn't see that link to the file in the post (just looked in the downloads section when I wanted to download it :P), might be worth making a mention of the single file in the downloads section too. Pastebin is always good because of its integration into CC: being able to install a file with a simple line in the shell.
Quote

Yarillo's Photo Yarillo 20 Jun 2016

Extremely underrated post

It was very useful to me, thank you very much.
Quote

CrazedProgrammer's Photo CrazedProgrammer 21 Jun 2016

I agree, this is very good.
Quote

Creator's Photo Creator 21 Jun 2016

This should be the way to go for APIs.
Quote