use datalib external mod with new functions rather than internal library

This commit is contained in:
octacian 2016-08-11 11:41:51 -07:00
parent d75137a9db
commit b34aa70765
5 changed files with 11 additions and 144 deletions

15
api.lua
View File

@ -143,17 +143,10 @@ function digicompute.register_terminal(termstring, desc)
meta:set_string("name", fields.name) -- set name meta:set_string("name", fields.name) -- set name
meta:set_string("setup", "true") -- set computer to configured meta:set_string("setup", "true") -- set computer to configured
-- create filesystem -- create filesystem
digicompute.file.mkdir(path.."/"..fields.name.."/os/") datalib.mkdir(path.."/"..fields.name.."/os/")
local conf = digicompute.file.read(modpath.."/bios/conf.lua") datalib.copy(modpath.."/bios/conf.lua", path.."/"..fields.name.."/os/conf.lua")
local main = digicompute.file.read(modpath.."/bios/main.lua") datalib.copy(modpath.."/bios/main.lua", path.."/"..fields.name.."/os/main.lua")
local start = digicompute.file.read(modpath.."/bios/start.lua") datalib.copy(modpath.."/bios/start.lua", path.."/"..fields.name.."/os/start.lua")
digicompute.file.write(path.."/"..fields.name.."/os/conf.lua", conf)
digicompute.file.write(path.."/"..fields.name.."/os/main.lua", main)
digicompute.file.write(path.."/"..fields.name.."/os/start.lua", start)
digicompute.file.mkdir(path.."/"..fields.name.."/user/")
local conf = nil -- unset
local main = nil -- unset
local start = nil -- unset
-- try to run when_on -- try to run when_on
if not dofile(path.."/"..meta:get_string("name").."/os/start.lua") then if not dofile(path.."/"..meta:get_string("name").."/os/start.lua") then
meta:set_string("formspec", digicompute.formspec("", "")) -- set formspec meta:set_string("formspec", digicompute.formspec("", "")) -- set formspec

130
data.lua
View File

@ -1,130 +0,0 @@
-- digicompute/data.lua
digicompute.file = {}
digicompute.file.table = {} -- internal table global
-- logger
function digicompute.log(content, log_type)
if log_type == nil then log_type = "action" end
minetest.log(log_type, "[digicompute.file:digicompute.file] "..content)
end
-- global path variables
digicompute.modpath = minetest.get_modpath("digicompute") -- modpath
digicompute.worldpath = minetest.get_worldpath() -- worldpath
digicompute.path = digicompute.worldpath.."/digicompute" -- path for general digicompute.file storage
-- local path variables
local modpath = digicompute.modpath
local worldpath = digicompute.worldpath
local path = digicompute.path
-- check if digicompute.file world folder exists
function digicompute.file.initdata()
local f = io.open(path)
if not f then
if minetest.mkdir then
minetest.mkdir(path) -- create directory if minetest.mkdir is available
return
else
os.execute('mkdir "'..path..'"') -- create directory with os mkdir command
return
end
end
f:close() -- close file
end
digicompute.file.initdata() -- initialize world data directory
-- check if file exists
function digicompute.file.exists(path)
local f = io.open(path, "r") -- open file
if f ~= nil then f:close() return true else return false end
end
-- create folder
function digicompute.file.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
else
os.execute('mkdir "'..path..'"') -- create directory with os mkdir command
return
end
end
f:close() -- close file
end
-- create file
function digicompute.file.create(path)
-- check if file already exists
if digicompute.file.exists(path) == true then
digicompute.log("File ("..path..") already exists.") -- log
return true -- exit and return
end
local f = io.open(path, "w") -- create file
f:close() -- close file
digicompute.log("Created file "..path) -- log
end
-- write to file
function digicompute.file.write(path, data, serialize)
if digicompute.file.exists(path) ~= true then digicompute.file.create(path) end -- check if exists
if not serialize then local serialize = false end -- if blank serialize = true
local f = io.open(path, "w") -- open file for writing
if serialize == true then local data = minetest.serialize(data) end -- serialize data
f:write(data) -- write data
f:close() -- close file
digicompute.log('Wrote "'..data..'" to '..path) -- log
end
-- append to file
function digicompute.file.append(path, data, serialize)
if digicompute.file.exists(path) ~= true then return false end -- check if exists
if not serialize then local serialize = false end -- if blank serialize = true
local f = io.open(path, "a") -- open file for writing
if serialize == true then local data = minetest.serialize(data) end -- serialize data
f:write(data) -- write data
f:close() -- close file
digicompute.log('Wrote "'..data..'" to '..path) -- log
end
-- load file
function digicompute.file.read(path, deserialize)
if digicompute.file.exists(path) ~= true then return false 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
if deserialize == true then local data = minetest.deserialize(data) end -- deserialize data
return data -- return file contents
end
-- write table to file
function digicompute.file.table.write(path, intable)
if digicompute.file.exists(path) ~= true then return false end -- check if exists
local intable = minetest.serialize(intable) -- serialize intable
local f = io.open(path, "w") -- open file for writing
f:write(intable) -- write intable
f:close() -- close file
digicompute.log("Wrote table to "..path)
end
-- load table from file
function digicompute.file.table.read(path)
if digicompute.file.exists(path) ~= true then return false end -- check if exists
local f = io.open(path, "r") -- open file for reading
local externaltable = minetest.deserialize(f:read()) -- deserialize and read externaltable
f:close() -- close file
return externaltable
end
-- dofile
function digicompute.file.dofile(path)
-- check if file exists
if digicompute.file.exists(path) == true then
dofile(path)
return true -- return true, successful
else
digicompute.log("File "..path.." does not exist.")
return false -- return false, unsuccessful
end
end

View File

@ -1,2 +1,3 @@
default default
datalib
digilines? digilines?

View File

@ -10,6 +10,10 @@ function digicompute.log(content, log_type)
minetest.log(log_type, "[digicompute] "..content) minetest.log(log_type, "[digicompute] "..content)
end end
-- create mod world dir
datalib.mkdir(datalib.worldpath.."/digicompute")
digicompute.path = datalib.worldpath.."/digicompute"
-- FORMSPECS -- FORMSPECS
-- normal -- normal
function digicompute.formspec(input, output) function digicompute.formspec(input, output)
@ -38,7 +42,6 @@ end
-- /FORMSPECS -- /FORMSPECS
-- load resources -- load resources
if not minetest.get_modpath("datalib") then dofile(modpath.."/data.lua") end -- load data api if not datalib mod
dofile(modpath.."/os.lua") -- load os api dofile(modpath.."/os.lua") -- load os api
dofile(modpath.."/api.lua") -- load api dofile(modpath.."/api.lua") -- load api
dofile(modpath.."/nodes.lua") -- load nodes dofile(modpath.."/nodes.lua") -- load nodes

4
os.lua
View File

@ -5,8 +5,8 @@ local path = digicompute.path -- datapath pointer
-- [function] load os -- [function] load os
function digicompute.os.load(os_name) function digicompute.os.load(os_name)
if digicompute.file.dofile(modpath.."/os/"..os_name..".lua") ~= true then if datalib.dofile(modpath.."/os/"..os_name..".lua") ~= true then
if digicompute.file.dofile(path.."/os/"..os_name.."/.lua") ~= true then if datalib.dofile(path.."/os/"..os_name.."/.lua") ~= true then
-- print error -- print error
digicompute.log(os_name.." os could not be found. Please place the OS file in "..modpath.."/os/ or "..path.."/os/ with extension '.lua'.", "error") digicompute.log(os_name.." os could not be found. Please place the OS file in "..modpath.."/os/ or "..path.."/os/ with extension '.lua'.", "error")
end end