Add wdata mod at Git commit e339375...

https://github.com/AntumMT/mod-wdata/tree/e339375
This commit is contained in:
Jordan Irwin 2021-05-28 04:56:19 -07:00
parent 482aad868c
commit 4392e5318b
8 changed files with 212 additions and 0 deletions

View File

@ -85,6 +85,7 @@ The game includes the mods from the default [minetest_game](https://github.com/m
* [listitems][] ([MIT][lic.listitems]) -- version: [0.7][ver.listitems] *2021-04-29*
* [painting][] (???) -- version [8961849 Git][ver.painting] *2016-05-05*
* [pipeworks][] ([LGPL][lic.lgpl3.0] / [CC BY-SA][lic.ccbysa4.0]) -- version: [2670fd8 Git][ver.pipeworks] *2021-05-06* ([patched][patch.pipeworks])
* [wdata][] ([MIT][lic.wdata]) -- version: [e339375 Git][ver.wdata] *2021-05-28*
* mobiles/
* [chicken][creatures] ([Zlib][lic.creatures] / [CC BY-SA][lic.ccbysa3.0]) -- version: [085706f Git][ver.cmer_chicken] *2021-05-25*
* [cow][mobs_animal] ([MIT][lic.mobs_cow]) -- version: [85af09a Git][ver.mobs_cow] *2021-05-19*
@ -380,6 +381,7 @@ The game includes the mods from the default [minetest_game](https://github.com/m
[walking_light]: https://github.com/petermaloney/walking_light
[wardrobe]: https://github.com/AntumMT/mod-wardrobe
[wardrobe.old]: https://forum.minetest.net/viewtopic.php?t=9680
[wdata]: https://forum.minetest.net/viewtopic.php?t=26804
[weather]: https://forum.minetest.net/viewtopic.php?t=5245
[whinny]: https://github.com/AntumMT/mod-whinny
[whitelist]: https://forum.minetest.net/viewtopic.php?t=8434
@ -461,6 +463,7 @@ The game includes the mods from the default [minetest_game](https://github.com/m
[lic.waffles]: mods/furniture/waffles/LICENSE.txt
[lic.walking_light]: mods/lighting/walking_light/README.md
[lic.wardrobe]: mods/player/visuals/wardrobe/LICENSE.txt
[lic.wdata]: mods/misc/wdata/LICENSE.txt
[lic.whinny]: mods/mobiles/whinny/LICENSE.txt
[lic.windmill]: mods/buildings/windmill/README.md
@ -624,6 +627,7 @@ The game includes the mods from the default [minetest_game](https://github.com/m
[ver.waffles]: https://github.com/GreenXenith/waffles/tree/15bcdce
[ver.walking_light]: https://github.com/petermaloney/walking_light/tree/766ef0f
[ver.wardrobe]: https://github.com/AntumMT/mod-wardrobe/releases/tag/v1.3
[ver.wdata]: https://github.com/AntumMT/mod-wdata/tree/e339375
[ver.whinny]: https://github.com/AntumMT/mod-whinny/releases/tag/v1.2
[ver.whitelist]: https://github.com/AntumMT/mod-whitelist/tree/b813b19
[ver.windmill]: https://github.com/Sokomine/windmill/tree/47b029d

View File

@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright © 2021 Jordan Irwin (AntumDeluge)
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

44
mods/misc/wdata/README.md Normal file
View File

@ -0,0 +1,44 @@
## World Data Manager library for Minetest
### Description:
A [Minetest][] library for managing data files in the world directory.
It takes a little work to read from & write to data in the world directory. `wdata` aims to make that easier by utilizing just two simple methods.
### Licensing:
- [MIT](LICENSE.txt)
### Usage:
There are two methods:
```
- wdata.read(fname)
- reads json data from file in world directory & converts to a table.
- fname: File basename without suffix (e.g. "my_config" or "my_mod/my_config").
- wdata.write(fname, data[, styled])
- converts table to json data & writes to file in world directory.
- fname: File basename without suffix (e.g. "my_config" or "my_mod/my_config").
- data: Table containing data to be exported.
- styled: Outputs in a human-readable format if this is set (default: true).
```
### Requirements:
```
Depends: none
Optional depends: none
```
### Links
- [Forum](https://forum.minetest.net/viewtopic.php?t=26804)
- [Git repo](https://github.com/AntumMT/mod-wdata)
- [API](https://antummt.github.io/mod-wdata/docs/api.html)
- [Changelog](changelog.txt)
- [TODO](TODO.txt)
[Minetest]: http://minetest.net/

2
mods/misc/wdata/TODO.txt Normal file
View File

@ -0,0 +1,2 @@
TODO:

104
mods/misc/wdata/api.lua Normal file
View File

@ -0,0 +1,104 @@
--- World Data Manager API
--
-- @module api.lua
-- store formatted world path
local world_path = core.get_worldpath():gsub("\\", "/")
--- Retrieves directory path where file is located.
--
-- @local
-- @function get_dir
-- @tparam string fpath Full path to file.
-- @treturn string Full path to directory.
local function get_dir(fpath)
-- format to make working with easier
fpath = fpath:gsub("\\", "/")
local idx = fpath:find("/[^/]*$")
-- use world directory by default
if not idx or idx == 0 then
return world_path
end
return fpath:sub(1, idx-1)
end
--- Reads config file from world directory.
--
-- @function wdata.read
-- @tparam string fname Base filename with optional directory structure (e.g. "my_mod/my_config")
-- @treturn table Table with contents read from json file or `nil`.
function wdata.read(fname)
local fpath = world_path .. "/" .. fname .. ".json"
-- check if file exists
local fopen = io.open(fpath, "r")
if not fopen then
wdata.log("warning", "file not found: " .. fpath)
return
end
local table_data = core.parse_json(fopen:read("*a"))
io.close(fopen)
if not table_data then
wdata.log("error", "cannot read json data from file: " .. fpath)
return
end
return table_data
end
--- Writes to config file in world directory.
--
-- @function wdata.write
-- @tparam string fname Base filename with optional directory structure (e.g. "my_mod/my_config").
-- @tparam table data Table data to be written to config file.
-- @tparam[opt] bool styled Outputs in a human-readable format if this is set (default: `true`).
-- @treturn bool `true` if succeeded, `false` if not.
function wdata.write(fname, data, styled)
styled = styled ~= false
local json_data = core.write_json(data, styled)
if not json_data then
wdata.log("error", "cannot convert data to json format")
return false
end
local fpath = world_path .. "/" .. fname .. ".json"
-- create directory tree if necessary
local dirname = get_dir(fpath)
if dirname ~= world_path then
if not core.mkdir(dirname) then
wdata.log("error", "cannot create directory: " .. dirname)
return false
end
end
return core.safe_file_write(fpath, json_data)
end
--- Aliases
--
-- @section aliases
--- Alias of `wdata.read`.
--
-- @falias minetest.read_world_config
if not core.read_world_config then
core.read_world_config = wdata.read
end
--- Alias of `wdata.write`.
--
-- @falias minetest.write_world_config
if not core.write_world_config then
core.write_world_config = wdata.write
end

View File

@ -0,0 +1,10 @@
v1.1
----
- changed "error" message to "warning" when file not found for reading
v1.0
----
- initial release
- added method for reading json data from world directory
- added method for writing json data to world directory

21
mods/misc/wdata/init.lua Normal file
View File

@ -0,0 +1,21 @@
wdata = {}
wdata.modname = core.get_current_modname()
wdata.modpath = core.get_modpath(wdata.modname)
function wdata.log(lvl, msg)
if not msg then
msg = lvl
lvl = nil
end
msg = "[" .. wdata.modname .. "] " .. msg
if not lvl then
core.log(msg)
else
core.log(lvl, msg)
end
end
dofile(wdata.modpath .. "/api.lua")

6
mods/misc/wdata/mod.conf Normal file
View File

@ -0,0 +1,6 @@
name = wdata
title = World Data Manager
description = A library for managing data files in the world directory.
version = 1.0
license = MIT
author = Jordan Irwin (AntumDeluge)