Add boilerplate code

Includes basic functions like contact.log, contact.load, and contact.save.
master
octacian 2017-02-20 15:41:13 -08:00
parent acc0b6d75e
commit 849d213303
2 changed files with 69 additions and 0 deletions

21
api.md Normal file
View File

@ -0,0 +1,21 @@
API
===
The Contact mod provides a rather simple yet comprehensive API for use by other mods. After all, the mod itself is not overly complex, so neither is its API. All functions available outside of the mod are documented below.
__Note:__ all the functions documented below should be prefixed by `contact.`, however, it has been removed from the headers for simplicity.
#### `log`
__Usage:__ `contact.log(<content (string)>, <type (string)>)`
Prints to the log just like `minetest.log`. This automatically inserts `[contact]` before each log message and defaults the log type to `action`. See the [documentation](http://dev.minetest.net/minetest.log) for available log types.
#### `load`
__Usage:__ `contact.load()`
Loads everything stored in the contact world-files to the global `contact` table storing information in the proper sub-tables. No parameters are required. __Note:__ this is automatically called at load and will rarely (if ever) be used elsewhere.
#### `save`
__Usage:__ `contact.save()`
Gathers specific data from the global `contact` table and saves it in `<worldname>/mod_contact.txt`. __Note:__ this is automatically called at shutdown and whenever specific actions take place.

View File

@ -0,0 +1,48 @@
-- contact/init.lua
contact = {}
local modpath = minetest.get_modpath("contact")
local worldpath = minetest.get_worldpath()
-- [function] Logger
function contact.log(content, log_type)
assert(content, "contact.log: content nil")
if log_type == nil then log_type = "action" end
minetest.log(log_type, "[contact] "..content)
end
-- [function] Load
function contact.load()
local res = io.open(worldpath.."/mod_contact.txt", "r")
if res then
res = minetest.deserialize(res:read("*all"))
if type(res) == "table" then
contact.reports = res.reports
contact.msg = res.msg
end
end
-- Initialize sub-tables
if not contact.reports then
contact.reports = {}
end
if not contact.msg then
contact.msg = {}
end
end
-- [function] Save
function contact.save()
local data = {
reports = contact.reports,
msg = contact.msg
}
io.open(worldpath.."/mod_contact.txt", "w"):write(minetest.serialize(data))
end
-- Save on shutdown
minetest.register_on_shutdown(contact.save)
-- Load on start
contact.load()