initial commit
separated from servertools (http://208.69.243.45:3000/octacian/datalib) to be more expandable and available directly for other modders. full commit history can be found at http://208.69.243.45:3000/octacian/servertools/src/storage
This commit is contained in:
commit
4605165e86
53
API.md
Normal file
53
API.md
Normal file
@ -0,0 +1,53 @@
|
||||
# datalib API
|
||||
This documentation is also available at the official [datalib Wiki](http://208.69.243.45:3000/octacian/datalib/wiki). Divided into several parts, one for each main topic, this documentation includes all functions available for use from other mods.
|
||||
|
||||
## Global Variables
|
||||
Variables available for use within functions documented in the below sections.
|
||||
|
||||
* `datalib` : defines modname for global functions
|
||||
* `datalib.modpath` : datalib Modpath for use with the dmAPI.
|
||||
* `datalib.worldpath` : path of current world for use with the dmAPI.
|
||||
* `datalib.datapath` : datalib folder within the world path for use with the dmAPI. It is recommended that you store general data within this folder rather than cluttering the root world directory.
|
||||
|
||||
## Data Manipulation API (dmAPI)
|
||||
API for manipulating data stored within external files. Global variables are provided as mentioned above for storing files withing the `modpath`, `worldpath`, and `datapath`. It is recommended that you store unique files within the `datapath` as this prevents cluttering of the root world directory.
|
||||
|
||||
### initdata
|
||||
**Usage:** `datalib.initdata()`
|
||||
|
||||
Initializes directory with in the world path in which datalib stores most of its data. Must be run before data can be written within the `datapath` (called within `init.lua`).
|
||||
|
||||
### check_file
|
||||
**Usage:** `datalib.exists(path)`
|
||||
|
||||
Checks the file specified through `path` returning true if it exists, and false if it does not.
|
||||
|
||||
### create_file
|
||||
**Usage:** `datalib.create(path)`
|
||||
|
||||
Create file in location specified by `path`. Returns true if already exists, nothing if creation is successful, also printing like output to the log.
|
||||
|
||||
### write_file
|
||||
**Usage:** `datalib.write(path, data, serialize)`
|
||||
|
||||
Write data to file. File path is specified in `path`. Data to write is specified with field `data` and supports any value including strings, booleans, and integers. The `serialize` option tells datalib whether or not to run `minetest.serialize` on the data before writing, and is by default set to `true` if left blank.
|
||||
|
||||
### load_file
|
||||
**Usage:** `datalib.read(path, deserialize)`
|
||||
|
||||
Load data from file (`path`) and return through variable `data`. The `deserialize` option tells datalib whether or not to run `minetest.deserialize` on the data before returning, and is by default set to `true` if left blank.
|
||||
|
||||
### write_table
|
||||
**Usage:** `datalib.table_write(path, table)`
|
||||
|
||||
Write table to file. File path is specified in `path`. Table to write is specified with field `table` and is designed for use with tables. The only difference between `write_table` and `write_file` is that `write_table` always serializes the data with `minetest.serialize` before writing because tables cannot be directly written to files, however, they must first be converted to strings through serialization.
|
||||
|
||||
### load_table
|
||||
**Usage:** `datalib.table_load(path)`
|
||||
|
||||
Load table from file (`path`) and return through variable `table`. The only difference between `load_table` and `load_file` is that `load_table` always deserializes the data with `minetest.deserialize` to convert previously written string back to a table.
|
||||
|
||||
### dofile
|
||||
**Usage:** `datalib.dofile(path)`
|
||||
|
||||
Syntax as with the `dofile` command, the only difference being the function checks whether the file exists (using `check_file`) before running dofile to prevent errors. File specified through `path` is run as Lua, and returns `true` if successful, and `false` if unsuccessful (indicates file does not exist).
|
21
README.md
Normal file
21
README.md
Normal file
@ -0,0 +1,21 @@
|
||||
datalib [datalib]
|
||||
=========================
|
||||
Version 0.1
|
||||
|
||||
Licence: MIT (see license.txt)
|
||||
datalib is an API that makes data interaction simple. Functions include writing and appending to files, writing and reading tables, and more. This mod is still work in progress, and new functionality will be added from time to time, so read the updating instructions below to make sure you always have the latest version.
|
||||
|
||||
## API
|
||||
See API.md, also available at the official [datalib wiki](http://208.69.243.45:3000/octacian/datalib/wiki) through Gogs ([http://208.69.243.45:3000/octacian/datalib/wiki](http://208.69.243.45:3000/octacian/datalib/wiki)).
|
||||
|
||||
### Installation and Updates
|
||||
Unzip the archive, rename the folder to servertools and place it in `minetest/mods` or in the mods folder of the subgame in which you wish to use ServerTools.
|
||||
|
||||
You can also install this mod in the `worldmods` folder inside any world directory to use it only within one world.
|
||||
|
||||
For further information or help see:
|
||||
http://wiki.minetest.com/wiki/Installing_Mods
|
||||
|
||||
To update, periodically check the [Gogs repository](http://208.69.243.45:3000/octacian/datalib) and download either the latest release or the master branch (most up to date, but often unstable).
|
||||
|
||||
The mod will soon include an auto update function, which will work on its own so long as you have internet.
|
94
init.lua
Normal file
94
init.lua
Normal file
@ -0,0 +1,94 @@
|
||||
-- datalib/init.lua
|
||||
datalib = {}
|
||||
|
||||
-- global path variables
|
||||
datalib.modpath = minetest.get_modpath("datalib") -- modpath
|
||||
datalib.worldpath = minetest.get_worldpath() -- worldpath
|
||||
datalib.datapath = datalib.worldpath.."/datalib" -- path for general datalib storage
|
||||
-- local path variables
|
||||
local modpath = datalib.modpath
|
||||
local worldpath = datalib.worldpath
|
||||
local datapath = datalib.datapath
|
||||
|
||||
-- check if datalib world folder exists
|
||||
function datalib.initdata()
|
||||
local f = io.open(datapath)
|
||||
if not f then
|
||||
if minetest.mkdir then
|
||||
minetest.mkdir(datapath) -- create directory if minetest.mkdir is available
|
||||
return
|
||||
else
|
||||
os.execute('mkdir "'..datapath..'"') -- create directory with os mkdir command
|
||||
return
|
||||
end
|
||||
end
|
||||
f:close() -- close file
|
||||
end
|
||||
|
||||
datalib.initdata() -- initialize world data directory
|
||||
|
||||
-- check if file exists
|
||||
function datalib.exists(path)
|
||||
local f = io.open(path, "r") -- open file
|
||||
if f ~= nil then f:close() return true else return false end
|
||||
end
|
||||
|
||||
-- create file
|
||||
function datalib.create(path)
|
||||
-- check if file already exists
|
||||
if datalib.check_file(path) == true then
|
||||
datalib.log("File ("..path..") already exists.") -- log
|
||||
return true -- exit and return
|
||||
end
|
||||
local f = io.open(path, "w") -- create file
|
||||
f:close() -- close file
|
||||
datalib.log("Created file "..path) -- log
|
||||
end
|
||||
|
||||
-- write to file
|
||||
function datalib.write(path, data, serialize)
|
||||
if not serialize then local serialize = true end -- if blank serialize = true
|
||||
local f = io.open(path, "w") -- open file for writing
|
||||
if serialize == true then local data = minetest.serialize(data) end -- serialize data
|
||||
f:write(data) -- write data
|
||||
f:close() -- close file
|
||||
datalib.log('Wrote "'..data..'" to '..path) -- log
|
||||
end
|
||||
|
||||
-- load file
|
||||
function datalib.read(path, deserialize)
|
||||
if not deserialize then local deserialize = true end -- if blank deserialize = true
|
||||
local f = io.open(path, "r") -- open file for reading
|
||||
local data = f:read() -- read and store file data in variable data
|
||||
if deserialize == true then local data = minetest.deserialize(data) end -- deserialize data
|
||||
return data -- return file contents
|
||||
end
|
||||
|
||||
-- write table to file
|
||||
function datalib.table_write(path, intable)
|
||||
local intable = minetest.serialize(intable) -- serialize intable
|
||||
local f = io.open(path, "w") -- open file for writing
|
||||
f:write(intable) -- write intable
|
||||
f:close() -- close file
|
||||
datalib.log("Wrote table to "..path)
|
||||
end
|
||||
|
||||
-- load table from file
|
||||
function datalib.table_load(path)
|
||||
local f = io.open(path, "r") -- open file for reading
|
||||
local externaltable = minetest.deserialize(f:read()) -- deserialize and read externaltable
|
||||
f:close() -- close file
|
||||
return externaltable
|
||||
end
|
||||
|
||||
-- dofile
|
||||
function datalib.dofile(path)
|
||||
-- check if file exists
|
||||
if datalib.check_file(path) == true then
|
||||
dofile(path)
|
||||
return true -- return true, successful
|
||||
else
|
||||
datalib.log("File "..path.." does not exist.")
|
||||
return false -- return false, unsuccessful
|
||||
end
|
||||
end
|
Loading…
x
Reference in New Issue
Block a user