Giter VIP home page Giter VIP logo

ccapis's Introduction

ComputerCraft APIs

This repository contains a list of smaller APIs for ComputerCraft computers and turtles, which I feel don't deserve their own repository.

This API provides an easy way of handling multiple startup scripts. Startup scripts will be stored in a configurable folder (default: autorun). You can add (startup.addFile, startup.addScript) and remove (startup.remove), as well as enable (startup.enable) or disable (startup.disable) single startup scripts via this API. In general, you'll want to load this API in your actual startup script and then call startup.run. See the example startup file.

You can interact with this API from the shell by using the startup-conf program.

This API provides an easy way of handling daemons (or services, if you prefer). Daemon scripts will be stored in a configurable folder (default: daemons). Similar to the startup API, daemons can be added and removed, as well as enabled and disabled via the API. You can also start (daemon.start) and stop (daemon.stop, daemon.kill) daemons via the API. Daemons will always run in the background, and will, in particular, not interfere with the terminal (unless explicitly using the term API).

You can interact with this API from the shell by using the daemon-conf program.

This API provides a single function, stacktrace.tpcall, which is functionally the same as pcall, except that it will provide a full stacktrace instead of just the exact error location. I find it useful to hook it in as a replacement for pcall in my startup script.

This API provides an alternative to the build-in os.loadAPI function. It will not print messages to the screen but return the error message as a second result instead. Also, it will block (sleep) if the API in question is currently being loaded (and not return false). A custom timeout for this blocking behavior can be provided.

A minimalistic logger that provides uniform log output with automatic date, time and level prefixing. Message format is like this: YYYY-MM-DD HH:MM [LEVEL] message The date is generated from the current os.day() (including leap-years!) and starts in year zero. Log file names can automatically be postfixed with the current day, for programs that log a lot, to keep file size at an acceptable size and make deleting old log entries easier.

The API everyone and their dog wrote at least once. This API offers replacements for the output setting functions in the redstone API (including analog, if supported), and will write the current output state to a file on disk, which is read when the API is loaded again after a reboot, to restore the last known output states. I recommend using this together with the startup API (or something similar) and add a high priority startup script that initializes this API and makes it replace the functions in the redstone API by calling predstone.hijackRedstoneAPI().

This API provides a constructor function, state.new, which allows creating a state machine. This state machine can be populated with states by calling its add function, and run using its run function. It provides an internal environment to all state functions and saves this environment as well as the name of the currently executing state, to allow resuming execution. It is intended to make writing resumable programs a little easier.

Example use:

-- Load the API and create a new state machine.
os.loadAPI("apis/state")
local program = state.new("/.program-state")

-- Add some states. Mind the colon! The first state added to the state
-- machine is assumed to be the entry state.
program:add("start", function()
    print("Running program...")
    stateGlobal = "This variable is available in all states."
    -- State variables will also be saved with the state.
    if wasRestarted == nil then
        wasRestarted = false
    else
        wasRestarted = true
    end
    save()
    -- If the program were to be terminated while waiting for this sleep
    -- call to return, wasRestarted would already hold a value (false), and
    -- be consequently set to true when the program is resumed.
    os.sleep(5)
    switchTo("end")
end)

program:add("end", function()
    print("Shutting down...")
    if wasRestarted then
        print("This program was interrupted at least once!")
    else
        print("This program finished in one go.")
    end
    -- Setting the next state to nil will make the state machine's run
    -- function return.
    switchTo(nil)
end)

-- Run the state machine.
program:run()

Example startup script

This is an example startup script (placed in the root folder, so this would be the "original" startup file) loading and initializing the APIs.

Installation

Sorry, but I'm too lazy to maintain pastebin versions of these. You'll have to manually download and install these scripts.

ccapis's People

Contributors

fnuecke avatar shura1oplot avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

ccapis's Issues

Improvement to API load tests

Hi Sangar
In startup-conf (for example) you check:

assert(os.loadAPI("apis/startup"))

Would it be better to test whether it's already in ROM e.g. as part of a resource pack?

Something like:

if ( not os.loadAPI("/rom/apis/startup") ) then
assert( os.loadAPI("apis/startup") )
end

Without this, startup-conf dies with an assertion failure when the API is in ROM and not in the filesystem.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.