mirror of
https://github.com/appgurueu/modlib.git
synced 2024-11-26 01:03:46 +01:00
Format file.lua
This commit is contained in:
parent
f39596efb6
commit
2e4f03c94e
142
file.lua
142
file.lua
@ -1,92 +1,96 @@
|
|||||||
function read(filename)
|
function read(filename)
|
||||||
local file=io.open(filename,"r")
|
local file = io.open(filename, "r")
|
||||||
if file==nil then
|
if file == nil then return nil end
|
||||||
return nil
|
local content = file:read"*a"
|
||||||
end
|
file:close()
|
||||||
local content=file:read("*a")
|
return content
|
||||||
file:close()
|
|
||||||
return content
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function write(filename, new_content)
|
function write(filename, new_content)
|
||||||
local file=io.open(filename,"w")
|
local file = io.open(filename, "w")
|
||||||
if file==nil then
|
if file == nil then return false end
|
||||||
return false
|
file:write(new_content)
|
||||||
end
|
file:close()
|
||||||
file:write(new_content)
|
return true
|
||||||
file:close()
|
|
||||||
return true
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function append(filename, new_content)
|
function append(filename, new_content)
|
||||||
local file=io.open(filename,"a")
|
local file = io.open(filename, "a")
|
||||||
if file==nil then
|
if file == nil then return false end
|
||||||
return false
|
file:write(new_content)
|
||||||
end
|
file:close()
|
||||||
file:write(new_content)
|
return true
|
||||||
file:close()
|
|
||||||
return true
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function exists(filename)
|
function exists(filename)
|
||||||
local file=io.open(filename, "r")
|
local file = io.open(filename, "r")
|
||||||
if file==nil then
|
if file == nil then return false end
|
||||||
return false
|
file:close()
|
||||||
end
|
return true
|
||||||
file:close()
|
|
||||||
return true
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function create_if_not_exists(filename, content)
|
function create_if_not_exists(filename, content)
|
||||||
if not exists(filename) then
|
if not exists(filename) then
|
||||||
write(filename, content or "")
|
write(filename, content or "")
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
return false
|
return false
|
||||||
end
|
|
||||||
function create_if_not_exists_from_file(filename, src_filename)
|
|
||||||
return create_if_not_exists(filename, read(src_filename))
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function create_if_not_exists_from_file(filename, src_filename) return create_if_not_exists(filename, read(src_filename)) end
|
||||||
|
|
||||||
-- Process Bridge Helpers
|
-- Process Bridge Helpers
|
||||||
process_bridges={}
|
process_bridges = {}
|
||||||
|
|
||||||
function process_bridge_build(name, input, output, logs)
|
function process_bridge_build(name, input, output, logs)
|
||||||
if not input or not output or not logs then
|
if not input or not output or not logs then
|
||||||
minetest.mkdir(minetest.get_worldpath().."/bridges/"..name)
|
minetest.mkdir(minetest.get_worldpath() .. "/bridges/" .. name)
|
||||||
end
|
end
|
||||||
input=input or minetest.get_worldpath().."/bridges/"..name.."/input.txt"
|
input = input or minetest.get_worldpath() .. "/bridges/" .. name .. "/input.txt"
|
||||||
output=output or minetest.get_worldpath().."/bridges/"..name.."/output.txt"
|
output = output or minetest.get_worldpath() .. "/bridges/" .. name .. "/output.txt"
|
||||||
logs=logs or minetest.get_worldpath().."/bridges/"..name.."/logs.txt"
|
logs = logs or minetest.get_worldpath() .. "/bridges/" .. name .. "/logs.txt"
|
||||||
-- Clear input
|
-- Clear input
|
||||||
write(input, "")
|
write(input, "")
|
||||||
-- Clear output
|
-- Clear output
|
||||||
write(output, "")
|
write(output, "")
|
||||||
-- Create logs if not exists
|
-- Create logs if not exists
|
||||||
create_if_not_exists(logs, "")
|
create_if_not_exists(logs, "")
|
||||||
process_bridges[name]={input=input, output=output, logs=logs, output_file=io.open(output,"a")}
|
process_bridges[name] = {
|
||||||
|
input = input,
|
||||||
|
output = output,
|
||||||
|
logs = logs,
|
||||||
|
output_file = io.open(output, "a")
|
||||||
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
function process_bridge_listen(name, line_consumer, step)
|
function process_bridge_listen(name, line_consumer, step)
|
||||||
local bridge=process_bridges[name]
|
local bridge = process_bridges[name]
|
||||||
modlib.minetest.register_globalstep(step or 0.1, function(dtime)
|
modlib.minetest.register_globalstep(step or 0.1, function(dtime)
|
||||||
local content=io.open(bridge.input, "r")
|
local content = io.open(bridge.input, "r")
|
||||||
local line=content:read()
|
local line = content:read()
|
||||||
while line do
|
while line do
|
||||||
line_consumer(line)
|
line_consumer(line)
|
||||||
line=content:read()
|
line = content:read()
|
||||||
end
|
end
|
||||||
write(bridge.input, "")
|
write(bridge.input, "")
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
function process_bridge_serve(name, step)
|
function process_bridge_serve(name, step)
|
||||||
local bridge=process_bridges[name]
|
local bridge = process_bridges[name]
|
||||||
modlib.minetest.register_globalstep(step or 0.1, function(dtime)
|
modlib.minetest.register_globalstep(step or 0.1, function(dtime)
|
||||||
bridge.output_file:close()
|
bridge.output_file:close()
|
||||||
process_bridges[name].output_file=io.open(bridge.output,"a")
|
process_bridges[name].output_file = io.open(bridge.output, "a")
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
function process_bridge_write(name, message)
|
function process_bridge_write(name, message)
|
||||||
local bridge=process_bridges[name]
|
local bridge = process_bridges[name]
|
||||||
bridge.output_file:write(message.."\n")
|
bridge.output_file:write(message .. "\n")
|
||||||
--append(bridge.input, message)
|
-- append(bridge.input, message)
|
||||||
end
|
end
|
||||||
|
|
||||||
function process_bridge_start(name, command, os_execute)
|
function process_bridge_start(name, command, os_execute)
|
||||||
local bridge=process_bridges[name]
|
local bridge = process_bridges[name]
|
||||||
os_execute(string.format(command, bridge.output, bridge.input, bridge.logs))
|
os_execute(string.format(command, bridge.output, bridge.input, bridge.logs))
|
||||||
end
|
end
|
Loading…
Reference in New Issue
Block a user