The idea is that all map-, env-, player- (etc.) data should be stored in a database rather than in files. Also, using that databases should be possible from lua.
[h]Why it is beneficial:[/h]
With such database interface (available in lua too) mods could easily and effectively store their own data or metadata for players, map, env etc.
If you are mod developer, you can add metadata to almost everything (map, players, etc.). You don't have to save data in files and do other boring things like that.
If you are c++ dev, you can easily create new databases and use existing ones. No more need to creating new files like ipban.txt. Just open a table in db and use it.
[h]What is done:[/h]
- new database interface (on templates, very easy to use without writing SQL queries)
- two databases created:
- map.sqlite contains: map, map metadata, sector meta
- env.sqlite contains: env metadata, auth data, ban manager data, players data and [new!] players metadata
- new lua functions for metadata:
- set_player_meta(string player_name, string meta_name, string type, value)
- get_player_meta(string player_name, string meta_name, string type)
- set_map_meta(string meta_name, string type, value)
- get_map_meta(string meta_name, string type)
- new lua functions for custom databases:
- get_database(string name) -> int (unique database ID)
- get_db_table(int database, string name) -> int (unique table ID)
- get_table_data(int table, string key_type, key, string data_type) -> nil / data of proper type
- set_table_data(int table, string key_type, key, string data_type, data)
- remove_table_data(int table, string key_type, key)
- NOTE: in all new lua functions, possible type names are: string, int, double, bool, v3s16, v3f, v3fpos. Example use in data/mods/jachoo/init.lua
- reading & writing map from/to files discontinued (anyone uses that?)
- writing data to files discontinued (loading works, but saving only to new databases)
[h]What is planned:[/h]
- add metadata to map blocks
- speed up database access
[h]Sources and code examples[/h]
https://github.com/jachoo/minetest-jachoo2/
(not tested on linux, may need some fixes)
Create/init the database and tables:
- Code: Select all
Database* m_database; //database
Table<v3s16,binary_t>& m_blocks; //table for map blocks. key=v3s16, data=binary
Table<std::string>& m_map_meta; //table for map metadata. key=string, data=automatic
[...]
m_database( new Database("map.sqlite") ),
m_blocks( m_database->getTable<v3s16,binary_t>("blocks") ),
m_map_meta( m_database->getTable<std::string>("map_meta") )
Writing binary data (ie map block) to database:
- Code: Select all
std::ostringstream o(std::ios_base::binary);
o.write((char*)&version, 1);
block->serialize(o, version, true);
// Write block to database
m_blocks.put(block_pos,o.str());
Interface for getting player metadata in C++:
- Code: Select all
template<class Data> Data getPlayerMeta(const Player& player, const std::string& name)
{
return m_players_meta.get<Data>(player.getName()+":"+name);
}
Lua simple example of using player metadata: command /lastmessage:
- Code: Select all
minetest.register_on_chat_message( function(name, message)
local meta_name = "lastmessage"
if message:sub(1,1) == '/' then
local cmd = "/lastmessage"
local s
if message:sub(0, #cmd) == cmd then
local v = minetest.get_player_meta(name,meta_name,"string")
if v then
minetest.chat_send_player(name, 'Your last message: '..v)
end
return true
end
else
minetest.set_player_meta(name,meta_name,"string",message)
end
end)
Using custom databases in lua mods:
- Code: Select all
db = minetest.get_database("my_database") -- creates/loads database from mod's directory
tab = minetest.get_db_table(db,"my_table") -- creates/loads table from that database
local n = minetest.get_table_data(tab,"string","some key","int") -- returns nil or number from table with key "some key"
local pos = minetest.get_table_data(tab,"int",666,"v3s16") -- returns nil or v3s16 from table with key 666
minetest.set_table_data(tab,"int",666,"v3s16",{x=0,y=0,z=1}) -- inserts/overrides record in a table