mirror of
https://github.com/appgurueu/modlib.git
synced 2024-11-25 16:53:46 +01:00
rolling-5
This commit is contained in:
parent
08bb51c1be
commit
0ba259425e
35
Readme.md
35
Readme.md
@ -1,34 +1,11 @@
|
|||||||
# modlib
|
# modlib
|
||||||
Lightweight Minetest Modding Library
|
|
||||||
|
Multipurpose Minetest Modding Library
|
||||||
|
|
||||||
## About
|
## About
|
||||||
No dependencies. Compatibility is checked automatically.
|
|
||||||
Licensed under the MIT License. Written by Lars Mueller aka LMD or appguru(eu).
|
No dependencies. Licensed under the MIT License. Written by Lars Mueller aka LMD or appguru(eu).
|
||||||
|
|
||||||
## API
|
## API
|
||||||
### Some more stuff
|
|
||||||
The things listed below are just a few, and as the library grows, I am not mentioning them all here, but I hope the function names are self-explaining. If not, feel free to contact me or create an issue / PR on GitHub.
|
Mostly self-documenting code. Mod namespace is `modlib` or `_ml`, containing all variables & functions.
|
||||||
### `table_ext`
|
|
||||||
Table Helpers. Self-explaining - merge tables, count tables, add items to tables, etc.
|
|
||||||
### `number_ext`
|
|
||||||
Number Helpers. Also self-explaining. Currently only rounding.
|
|
||||||
### `log`
|
|
||||||
Several logchannels which can be written to. Look at sourcecode.
|
|
||||||
### `conf`
|
|
||||||
Configuration files as .json - world specific. Constraint checks can be done.
|
|
||||||
Use `load_or_create(confname, replacement_file, constraints)` to load the conf, if it does not exist create it using replacement file, and then load it, checking the constraints.
|
|
||||||
##### Constraints
|
|
||||||
Are a table.
|
|
||||||
```
|
|
||||||
{
|
|
||||||
type="boolean"/"number"/"string"/"table",
|
|
||||||
If type = table :
|
|
||||||
keys = constraints_for_keys,
|
|
||||||
values = constraints_for_values
|
|
||||||
children = {child_key:constraints_for_value},
|
|
||||||
If type = number or string :
|
|
||||||
range={start=num, end=num},
|
|
||||||
func=function(value) returns error or nil,
|
|
||||||
possible_values = {v1, v2}
|
|
||||||
}
|
|
||||||
```
|
|
66
log.lua
66
log.lua
@ -1,24 +1,24 @@
|
|||||||
-- Log helpers - write to log, force writing to file
|
-- Log helpers - write to log, force writing to file
|
||||||
minetest.mkdir(minetest.get_worldpath().."/logs")
|
minetest.mkdir(minetest.get_worldpath() .. "/logs")
|
||||||
channels={}
|
channels = {}
|
||||||
last_day=os.date("%d")
|
last_day = os.date("%d")
|
||||||
function get_path(logname)
|
function get_path(logname)
|
||||||
return minetest.get_worldpath().."/logs/"..logname
|
return minetest.get_worldpath() .. "/logs/" .. logname
|
||||||
end
|
end
|
||||||
function create_channel(title)
|
function create_channel(title)
|
||||||
local dir=get_path(title)
|
local dir = get_path(title)
|
||||||
minetest.mkdir(dir)
|
minetest.mkdir(dir)
|
||||||
channels[title]={dirname=dir,queue={}}
|
channels[title] = {dirname = dir, queue = {}}
|
||||||
write(title, "Initialisation")
|
write(title, "Initialisation")
|
||||||
end
|
end
|
||||||
function write(channelname, msg)
|
function write(channelname, msg)
|
||||||
local channel=channels[channelname]
|
local channel = channels[channelname]
|
||||||
local current_day=os.date("%d")
|
local current_day = os.date("%d")
|
||||||
if current_day ~= last_day then
|
if current_day ~= last_day then
|
||||||
last_day=current_day
|
last_day = current_day
|
||||||
write_to_file(channelname, channel, os.date("%Y-%m-%d"))
|
write_to_file(channelname, channel, os.date("%Y-%m-%d"))
|
||||||
end
|
end
|
||||||
table.insert(channel.queue, os.date("[%H:%M:%S] ")..msg)
|
table.insert(channel.queue, os.date("[%H:%M:%S] ") .. msg)
|
||||||
end
|
end
|
||||||
function write_to_all(msg)
|
function write_to_all(msg)
|
||||||
for channelname, _ in pairs(channels) do
|
for channelname, _ in pairs(channels) do
|
||||||
@ -27,40 +27,46 @@ function write_to_all(msg)
|
|||||||
end
|
end
|
||||||
function write_to_file(name, channel, current_date)
|
function write_to_file(name, channel, current_date)
|
||||||
if not channel then
|
if not channel then
|
||||||
channel=channels[name]
|
channel = channels[name]
|
||||||
end
|
end
|
||||||
if #(channel.queue) > 0 then
|
if #(channel.queue) > 0 then
|
||||||
local filename=channel.dirname.."/"..(current_date or os.date("%Y-%m-%d"))..".txt"
|
local filename = channel.dirname .. "/" .. (current_date or os.date("%Y-%m-%d")) .. ".txt"
|
||||||
local rope={}
|
local rope = {}
|
||||||
for _, msg in ipairs(channel.queue) do
|
for _, msg in ipairs(channel.queue) do
|
||||||
table.insert(rope, msg)
|
table.insert(rope, msg)
|
||||||
end
|
end
|
||||||
modlib.file.append(filename, table.concat(rope, "\n").."\n")
|
modlib.file.append(filename, table.concat(rope, "\n") .. "\n")
|
||||||
channels[name].queue={}
|
channels[name].queue = {}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
function write_all_to_file()
|
function write_all_to_file()
|
||||||
local current_date=os.date("%Y-%m-%d")
|
local current_date = os.date("%Y-%m-%d")
|
||||||
for name, channel in pairs(channels) do
|
for name, channel in pairs(channels) do
|
||||||
write_to_file(name, channel, current_date)
|
write_to_file(name, channel, current_date)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local timer=0
|
local timer = 0
|
||||||
|
|
||||||
minetest.register_globalstep(function(dtime)
|
minetest.register_globalstep(
|
||||||
timer=timer+dtime
|
function(dtime)
|
||||||
if timer > 5 then
|
timer = timer + dtime
|
||||||
write_all_to_file()
|
if timer > 5 then
|
||||||
timer=0
|
write_all_to_file()
|
||||||
|
timer = 0
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end)
|
)
|
||||||
|
|
||||||
minetest.register_on_mods_loaded(function()
|
minetest.register_on_mods_loaded(
|
||||||
write_to_all("Mods loaded")
|
function()
|
||||||
end)
|
write_to_all("Mods loaded")
|
||||||
|
end
|
||||||
|
)
|
||||||
|
|
||||||
minetest.register_on_shutdown(function()
|
minetest.register_on_shutdown(
|
||||||
write_to_all("Shutdown")
|
function()
|
||||||
write_all_to_file()
|
write_to_all("Shutdown")
|
||||||
end)
|
write_all_to_file()
|
||||||
|
end
|
||||||
|
)
|
||||||
|
2
mod.conf
2
mod.conf
@ -1,2 +1,2 @@
|
|||||||
name=modlib
|
name=modlib
|
||||||
description=A lightweight modding library.
|
description=Multipurpose Minetest Modding Library
|
||||||
|
BIN
screenshot.png
Normal file
BIN
screenshot.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 50 KiB |
Loading…
Reference in New Issue
Block a user