docs: update API

master
Riceball LEE 2021-06-19 10:03:55 +08:00
parent a04117ed23
commit a6a9141a39
No known key found for this signature in database
GPG Key ID: 10F15E84852CB868
3 changed files with 57 additions and 7 deletions

View File

@ -1,2 +1,39 @@
# Minetest YAML Config Library Mod
## Usage
```lua
local MOD_NAME = minetest.get_current_modname()
-- load config from file,
-- first load the my-config.yml file in mod directory as default settings
-- then try to load from world directory:
-- the filename in the world folder is MOD_NAME .. "_my-config.yml"
local settings = yaml.readConfig(MOD_NAME, "my-config.yml")
-- save the config file to world directory
-- the default filename is "config.yml"
-- the filename in the world folder is MOD_NAME .. "_" .."config.yml"
yaml.writeConfig(settings)
```
## API
* yaml.readConfig(modName, filename = "config.yml")
* first load the file in mod directory as default settings
* then load from world directory
* merge the settings at last
* yaml.writeConfig(settings, filename = "config.yml", modName)
* save the config file to the world directory
* yaml.readYamlFile(filepath)
* read a YAML format file
* yaml.readModConfig(filename, modName)
* yaml.readWorldConfig(filename)
* yaml.writeYamlFile(filepath, content)
* yaml.writeModConfig(filename, content, modName)
* yaml.writeWorldConfig(content, filename)
* yaml.defaults(target, default)
* merge the default to the target table
* yaml.readFile(filepath)
* read whole file
* return content if successful
* yaml.writeFile(filepath)
* return true if successful

View File

@ -4,6 +4,8 @@ local MOD_NAME = minetest.get_current_modname()
local MOD_PATH = minetest.get_modpath(MOD_NAME) .. "/"
local WORLD_PATH = minetest.get_worldpath() .. "/"
local defaultName = "config.yml"
-- export the global yaml object
yaml = rawget(_G, MOD_NAME)
@ -56,9 +58,10 @@ if (not yaml) then
end
yaml.readFile = readYamlFile
local function readModConfig(filename, modPath)
local function readModConfig(filename, modName)
local modPath = minetest.get_modpath(modName) .. "/"
if modPath then
if modPath:sub(-1) ~= "/" then modPath = modPath .. "/" end
-- if modPath:sub(-1) ~= "/" then modPath = modPath .. "/" end
return readYamlFile(modPath .. filename)
end
end
@ -69,8 +72,9 @@ if (not yaml) then
end
yaml.readWorldConfig = readWorldConfig
yaml.readConfig = function(filename, modPath)
local modConf = readModConfig(filename, modPath)
yaml.readConfig = function(modName, filename)
if not filename then filename = defaultName end
local modConf = readModConfig(modName, filename)
local worldConf = readWorldConfig(filename)
return defaults(worldConf, modConf)
end
@ -84,7 +88,8 @@ if (not yaml) then
end
yaml.writeFile = writeYamlFile
local function writeModConfig(filename, content, modPath)
local function writeModConfig(filename, content, modName)
local modPath = minetest.get_modpath(modName) .. "/"
if modPath then
if modPath:sub(-1) ~= "/" then modPath = modPath .. "/" end
return writeYamlFile(modPath .. filename, content)
@ -92,11 +97,17 @@ if (not yaml) then
end
yaml.writeModConfig = writeModConfig
local function writeWorldConfig(filename, content)
local function writeWorldConfig(content, filename)
return writeYamlFile(WORLD_PATH .. filename, content)
end
yaml.writeWorldConfig = writeWorldConfig
yaml.writeConfig = writeWorldConfig
local function writeConfig(content, filename, modName)
if not filename then filename = defaultName end
if modName then filename = modName .. "_" .. filename end
return writeWorldConfig(content, filename)
end
yaml.writeConfig = writeConfig
end

View File

@ -1,2 +1,4 @@
name = yaml
description = YAML Config Library
author = Riceball
license = MIT