Bump version to 0.5, beta

master
octacian 2017-01-29 15:28:59 -08:00
parent 416a60039f
commit b155acb3c2
5 changed files with 10 additions and 351 deletions

View File

@ -1,6 +1,6 @@
Digital Computers [digicompute]
=========================
* Version 0.1, unstable
* Version 0.5, beta
* Licence: MIT (see LICENSE)
* [Github Repository](https://github.com/octacian/digicompute)
@ -17,8 +17,8 @@ To get started, just place down a computer, right click on it to power it up, an
* mesecon support
### API
digicompute is a mod that is meant to be customized, and tweaked by both modders and players. Along with the computers available out of the box, a powerful API is provided allowing modders to easily and quickly register their own computers, peripherals, internal components, and other devices.
digicompute is a mod that is meant to be customized, and tweaked by both modders and players. Along with the computers available out of the box, a powerful API is provided allowing modders to easily and quickly register their own computers, and soon peripherals, internal components, and other devices.
The OS API allows anybody to create their own operating system to run and distrubute right on their digicomputers. The possibilities are becoming endless, as operating systems will soon be able to further use the formspec to create a graphical experience right inside Minetest.
To learn more about the API, see the [Wiki](https://github.com/octacian/digicompute/wiki/).
To learn more about the API, see the [Wiki](https://github.com/octacian/digicompute/wiki/). Or, see the files found in the `doc` directory.

88
api.lua
View File

@ -1,88 +0,0 @@
-- digicompute/api.lua
local modpath = digicompute.modpath -- modpath pointer
local path = digicompute.path -- path pointer
-- [function] refresh formspec
function digicompute.refresh(pos)
local meta = minetest.get_meta(pos) -- get meta
meta:set_string("formspec", digicompute.formspec(meta:get_string("input"), meta:get_string("output")))
end
dofile(modpath.."/fs.lua") -- do fs api file
dofile(modpath.."/env.lua") -- do env file
-- turn on
function digicompute.on(pos, node)
local temp = minetest.get_node(pos) -- get node
minetest.swap_node({x = pos.x, y = pos.y, z = pos.z}, {name = "digicompute:"..node.."_bios", param2 = temp.param2}) -- set node to bios
minetest.after(3.5, function(pos_)
minetest.swap_node({x = pos_.x, y = pos_.y, z = pos_.z}, {name = "digicompute:"..node.."_on", param2 = temp.param2}) -- set node to on after 5 seconds
end, vector.new(pos))
local meta = minetest.get_meta(pos) -- get meta
-- if setup is not true, use set name formspec
if meta:get_string("setup") ~= "true" then
meta:set_string("formspec", digicompute.formspec_name("")) -- set formspec
else -- use default formspec
local fields = {
input = meta:get_string("input"),
output = meta:get_string("output"),
}
-- if not when_on, use blank
if not digicompute.fs.run_file(pos, "os/start.lua", fields, "start.lua") then
meta:set_string("formspec", digicompute.formspec("", "")) -- set formspec
end
digicompute.refresh(pos) -- refresh
end
end
-- turn off
function digicompute.off(pos, node)
local temp = minetest.get_node(pos) -- get node
local meta = minetest.get_meta(pos) -- get meta
meta:set_string("formspec", "") -- clear formspec
minetest.swap_node({x = pos.x, y = pos.y, z = pos.z}, {name = "digicompute:"..node, param2 = temp.param2}) -- set node to off
end
-- reboot
function digicompute.reboot(pos, node)
digicompute.off(pos, node)
digicompute.on(pos, node)
end
-- clear
function digicompute.clear(field, pos)
local meta = minetest.get_meta(pos) -- get meta
meta:set_string(field, "") -- clear value
end
-- [function] run code (in sandbox env)
function digicompute.run(f, env)
setfenv(f, env)
local e, msg = pcall(f)
if e == false then return msg end
end
-- [function] run file under env
function digicompute.fs.run_file(pos, lpath, fields, replace)
local meta = minetest.get_meta(pos)
local lpath = path.."/"..meta:get_string("owner").."/"..meta:get_string("name").."/"..lpath
local env = digicompute.create_env(pos, fields) -- environment
local f, msg = loadfile(lpath) -- load func
-- if error, call error handle function and re-run start
if msg and replace then
datalib.copy(lpath, lpath..".old")
datalib.copy(modpath.."/bios/"..replace, lpath)
meta:set_string("output", "Error: \n"..msg.."\n\nRestoring OS, modified files will remain.") -- set output
digicompute.refresh(pos) -- refresh
minetest.after(13, function() -- after 13 seconds, run start
local s = loadfile(path.."/"..meta:get_string("owner").."/"..meta:get_string("name").."/os/start.lua") -- load func
digicompute.run(s, env) -- run func
digicompute.refresh(pos) -- refresh
return false
end)
elseif msg and not replace then return msg -- elseif no replace and error, return error msg
else local res = digicompute.run(f, env) end -- else, run function
end
dofile(modpath.."/c_api.lua") -- do computer API file

View File

@ -17,9 +17,12 @@ local computers = {}
-- [function] load computers
function digicompute.load_computers()
local data = minetest.deserialize(digicompute.builtin.read(path.."/computers.txt"))
if type(data) == "table" then
computers = data
local res = digicompute.builtin.read(path.."/computers.txt")
if res then
res = minetest.deserialize(res)
if type(res) == "table" then
computers = res
end
end
end

256
fs.lua
View File

@ -1,256 +0,0 @@
-- digicompute/fs.lua
--[[
filesystem API.
]]
digicompute.fs = {}
local fs = digicompute.fs
local path = digicompute.path
local modpath = digicompute.modpath
----- BASE FUNCTIONS -----
-- [function] check if file exists
local function exists(path)
local f = io.open(path, "r") -- open file
if f ~= nil then f:close() return true end
end
-- [function] make directory
local function mkdir(path)
local f = io.open(path)
if not f then
if minetest.mkdir then
minetest.mkdir(path) -- create directory if minetest.mkdir is available
return true
else
os.execute('mkdir "'..path..'"') -- create directory with os mkdir command
return true
end
end
f:close() -- close file
end
-- [function] remove directory
local function rmdir(path)
if not exists(path) then return end -- directory doesn't exist
-- [local function] remove files
local function rm_files(ppath, files)
for _, f in ipairs(files) do
os.remove(ppath.."/"..f)
end
end
-- [local function] check and rm dir
local function rm_dir(dpath)
local files = minetest.get_dir_list(dpath, false)
local subdirs = minetest.get_dir_list(dpath, true)
rm_files(dpath, files)
if subdirs then
for _, d in ipairs(subdirs) do
rm_dir(dpath.."/"..d)
end
end
os.remove(dpath)
end
rm_dir(path)
return true
end
-- [function] create file
local function create(path)
if exists(path) then return end -- if already exists, return
local f = io.open(path, "w") -- create file
f:close() -- close file
return true
end
-- [function] write to file
local function write(path, data, overwrite)
-- check if append or overwrite
if overwrite == false then local w = "a" else
if not exists(path) then return end
local w = "w"
end
local f = io.open(path, w) -- open file for writing
f:write(minetest.serialize(data)) -- write serialized data (prevents errors when writing tables)
f:close() -- close file
return true
end
-- [function] read file
local function read(path)
if not exists(path) then return end -- check if exists
local f = io.open(path, "r") -- open file for reading
local data = f:read("*all") -- read and store file data in variable "data"
return minetest.deserialize(data) -- return deserialized contents
end
-- [function] copy file
local function cp(path, new)
if not exists(path) then return end -- check if path exists
local original = read(path) -- read
write(new, original, true) -- write
return true
end
-- [function] copy directory
local function cpdir(path, new)
if not exists(path) then return end -- directory doesn't exist
-- [local function] copy files
local function cp_files(ppath, files)
for _, f in ipairs(files) do
cp(ppath.."/"..f, new.."/"..f) -- copy
end
end
-- [local function] check and copy dir
local function cp_dir(dpath)
mkdir(dpath) -- make new directory
local files = minetest.get_dir_list(dpath, false)
local subdirs = minetest.get_dir_list(dpath, true)
cp_files(dpath, files)
for _, d in ipairs(subdirs) do
cp_dir(dpath.."/"..d)
end
end
cp_dir(path)
return true
end
----- FS -----
-- [function] initalize fs
function digicompute.fs.init(pos, cname)
local meta = minetest.get_meta(pos) -- meta
local player = meta:get_string("owner") -- owner username
local cpath = path.."/"..player.."/"..cname -- comp path
if not exists(path.."/"..player) then mkdir(path.."/"..player) end -- check for user dir
if exists(cpath) then return "A computer with this name already exists." end
mkdir(cpath) -- make computer dir
cpdir(modpath.."/bios", cpath.."/os") -- copy biosOS
digicompute.log("Initialized computer "..cname.." placed by "..player..".")
end
-- [function] de-initialize fs (delete)
function digicompute.fs.deinit(pos)
local meta = minetest.get_meta(pos) -- meta
local player = meta:get_string("owner") -- owner username
local cname = meta:get_string("name") -- name
-- [local function] remove files
local function rm_files(ppath, files)
for _, f in ipairs(files) do
os.remove(ppath.."/"..f)
end
end
-- [local function] check and rm dir
local function rm_dir(dpath)
local files = minetest.get_dir_list(dpath, false)
local subdirs = minetest.get_dir_list(dpath, true)
rm_files(dpath, files)
if subdirs then
for _, d in ipairs(subdirs) do
rm_dir(dpath.."/"..d)
end
end
os.remove(dpath)
end
rm_dir(path.."/"..player.."/"..cname)
end
-- [function] get file contents
function digicompute.fs.get_file(pos, fpath)
local meta = minetest.get_meta(pos) -- meta
local cname = meta:get_string("name") -- computer name
local player = meta:get_string("owner") -- owner username
local cpath = path.."/"..player.."/"..cname -- comp path
local contents = datalib.read(cpath.."/"..fpath)
if not contents then return { error = "File does not exist.", contents = nil } end
return { error = nil, contents = contents }
end
-- [function] get directory contents
function digicompute.fs.get_dir(pos, dpath)
local meta = minetest.get_meta(pos) -- meta
local cname = meta:get_string("name") -- computer name
local player = meta:get_string("owner") -- owner username
local cpath = path.."/"..player.."/"..cname -- comp path
local files = minetest.get_dir_list(cpath.."/"..dpath, false)
local subdirs = minetest.get_dir_list(cpath.."/"..dpath, true)
if not files or files == {} or not subdirs or subdirs == {} then
return { error = "Directory does not exist, or is empty.", contents = nil }
end
return { error = nil, contents = { files = files, subdirs = subdirs } }
end
-- [function] exists
function digicompute.fs.exists(pos, fpath)
local meta = minetest.get_meta(pos) -- meta
local cname = meta:get_string("name") -- computer name
local name = meta:get_string("owner") -- owner username
local res = datalib.exists(path.."/"..name.."/"..cname.."/"..fpath)
if res == true then return "File exists."
else return "File does not exist."
end
end
-- [function] create directory
function digicompute.fs.mkdir(pos, fpath)
local meta = minetest.get_meta(pos) -- meta
local cname = meta:get_string("name") -- computer name
local name = meta:get_string("owner") -- owner username
local res = datalib.mkdir(path.."/"..name.."/"..cname.."/"..fpath)
if res == true then return "Directory already exists." end
end
-- [function] remove directory
function digicompute.fs.rmdir(pos, fpath)
local meta = minetest.get_meta(pos) -- meta
local cname = meta:get_string("name") -- computer name
local name = meta:get_string("owner") -- owner username
local res = datalib.rmdir(path.."/"..name.."/"..cname.."/"..fpath)
if res == false then return "Directory does not exist." end
end
-- [function] create file
function digicompute.fs.create(pos, fpath)
local meta = minetest.get_meta(pos) -- meta
local cname = meta:get_string("name") -- computer name
local name = meta:get_string("owner") -- owner username
local res = datalib.create(path.."/"..name.."/"..cname.."/"..fpath)
if res == true then return "File already exists." end
end
-- [function] write
function digicompute.fs.write(pos, fpath, data)
local meta = minetest.get_meta(pos) -- meta
local cname = meta:get_string("name") -- computer name
local name = meta:get_string("owner") -- owner username
datalib.write(path.."/"..name.."/"..cname.."/"..fpath, data, false)
end
-- [function] append
function digicompute.fs.append(pos, fpath, data)
local meta = minetest.get_meta(pos) -- meta
local cname = meta:get_string("name") -- computer name
local name = meta:get_string("owner") -- owner username
datalib.append(path.."/"..name.."/"..cname.."/"..fpath, data, false)
end
-- [function] copy file
function digicompute.fs.copy(pos, fpath, npath)
local meta = minetest.get_meta(pos) -- meta
local cname = meta:get_string("name") -- computer name
local name = meta:get_string("owner") -- owner username
local res = datalib.copy(path.."/"..name.."/"..cname.."/"..fpath, path.."/"..name.."/"..cname.."/"..npath)
if res == false then return "Base file does not exist." end
end
digicompute.fs.cp = digicompute.fs.copy

View File

@ -1,7 +1,7 @@
-- digicompute/init.lua
digicompute = {}
digicompute.VERSION = 0.1
digicompute.VERSION = 0.5
digicompute.RELEASE_TYPE = "beta"
digicompute.path = minetest.get_worldpath().."/digicompute/" -- digicompute directory