Skip to content

Commit

Permalink
Formspecs: Introduce formspec_version to mods
Browse files Browse the repository at this point in the history
  • Loading branch information
SmallJoker committed Sep 14, 2019
1 parent b0baa69 commit 9acd36b
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 10 deletions.
1 change: 1 addition & 0 deletions builtin/game/features.lua
Expand Up @@ -13,6 +13,7 @@ core.features = {
object_use_texture_alpha = true,
object_independent_selectionbox = true,
httpfetch_binary_data = true,
formspec_version_element = true,
}

function core.has_feature(arg)
Expand Down
14 changes: 14 additions & 0 deletions doc/lua_api.txt
Expand Up @@ -1959,6 +1959,15 @@ Examples
Elements
--------

### `formspec_version[<version>]`

* Set the formspec version to a certain number. If not specified,
version 1 is assumed.
* Must be specified before `size` element.
* Clients older than this version can neither show newer elements nor display
elements with new arguments correctly.
* Available since feature `formspec_version_element`.

### `size[<W>,<H>,<fixed_size>]`

* Define the size of the menu in inventory slots
Expand Down Expand Up @@ -1995,6 +2004,7 @@ Elements

### `real_coordinates[<bool>]`

* INFORMATION: Enable it automatically using `formspec_version` version 2 or newer.
* When set to true, all following formspec elements will use the new coordinate system.
* If used immediately after `size`, `position`, `anchor`, and `no_prepend` elements
(if present), the form size will use the new coordinate system.
Expand Down Expand Up @@ -2114,6 +2124,7 @@ Elements
image shall be sized 8 times 16px times 4 times 16px
* If `auto_clip` is `true`, the background is clipped to the formspec size
(`x` and `y` are used as offset values, `w` and `h` are ignored)
* Available since formspec version 2

### `pwdfield[<X>,<Y>;<W>,<H>;<name>;<label>]`

Expand Down Expand Up @@ -3788,6 +3799,8 @@ Utilities
-- Specifies whether binary data can be uploaded or downloaded using
-- the HTTP API (5.1.0)
httpfetch_binary_data = true,
-- Whether formspec_version[<version>] may be used (5.1.0)
formspec_version_element = true,
}

* `minetest.has_feature(arg)`: returns `boolean, missing_features`
Expand All @@ -3807,6 +3820,7 @@ Utilities
avg_jitter = 0.03, -- average packet time jitter
connection_uptime = 200, -- seconds since client connected
protocol_version = 32, -- protocol version used by client
formspec_version = 2, -- supported formspec version
-- following information is available on debug build only!!!
-- DO NOT USE IN MODS
--ser_vers = 26, -- serialization version used by client
Expand Down
3 changes: 2 additions & 1 deletion src/client/client.cpp
Expand Up @@ -1222,12 +1222,13 @@ void Client::sendRespawn()
void Client::sendReady()
{
NetworkPacket pkt(TOSERVER_CLIENT_READY,
1 + 1 + 1 + 1 + 2 + sizeof(char) * strlen(g_version_hash));
1 + 1 + 1 + 1 + 2 + sizeof(char) * strlen(g_version_hash) + 2);

pkt << (u8) VERSION_MAJOR << (u8) VERSION_MINOR << (u8) VERSION_PATCH
<< (u8) 0 << (u16) strlen(g_version_hash);

pkt.putRawString(g_version_hash, (u16) strlen(g_version_hash));
pkt << (u16)FORMSPEC_API_VERSION;
Send(&pkt);
}

Expand Down
2 changes: 1 addition & 1 deletion src/client/game.cpp
Expand Up @@ -4055,7 +4055,7 @@ void Game::showPauseMenu()
float ypos = simple_singleplayer_mode ? 0.7f : 0.1f;
std::ostringstream os;

os << FORMSPEC_VERSION_STRING << SIZE_TAG
os << FORMSPEC_VERSION_STRING << SIZE_TAG
<< "button_exit[4," << (ypos++) << ";3,0.5;btn_continue;"
<< strgettext("Continue") << "]";

Expand Down
9 changes: 8 additions & 1 deletion src/gui/guiFormSpecMenu.cpp
Expand Up @@ -2167,6 +2167,9 @@ void GUIFormSpecMenu::parseElement(parserData* data, const std::string &element)
if (element.empty())
return;

if (parseVersionDirect(element))
return;

std::vector<std::string> parts = split(element,'[');

// ugly workaround to keep compatibility
Expand Down Expand Up @@ -2503,7 +2506,7 @@ void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
}

/* Copy of the "real_coordinates" element for after the form size. */
mydata.real_coordinates = false;
mydata.real_coordinates = m_formspec_version >= 2;
for (; i < elements.size(); i++) {
std::vector<std::string> parts = split(elements[i], '[');
std::string name = trim(parts[0]);
Expand Down Expand Up @@ -2648,10 +2651,14 @@ void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
if (enable_prepends) {
// Backup the coordinates so that prepends can use the coordinates of choice.
bool rc_backup = mydata.real_coordinates;
bool version_backup = m_formspec_version;
mydata.real_coordinates = false; // Old coordinates by default.

std::vector<std::string> prepend_elements = split(m_formspec_prepend, ']');
for (const auto &element : prepend_elements)
parseElement(&mydata, element);

m_formspec_version = version_backup;
mydata.real_coordinates = rc_backup; // Restore coordinates
}

Expand Down
4 changes: 2 additions & 2 deletions src/gui/guiFormSpecMenu.h
Expand Up @@ -472,7 +472,7 @@ class GUIFormSpecMenu : public GUIModalMenu
private:
IFormSource *m_form_src;
TextDest *m_text_dst;
u32 m_formspec_version = 0;
u16 m_formspec_version = 1;
std::string m_focused_element = "";
JoystickController *m_joystick;

Expand Down Expand Up @@ -591,7 +591,7 @@ class FormspecFormSource: public IFormSource

void setForm(const std::string &formspec)
{
m_formspec = FORMSPEC_VERSION_STRING + formspec;
m_formspec = formspec;
}

const std::string &getForm() const
Expand Down
12 changes: 11 additions & 1 deletion src/network/networkprotocol.h
Expand Up @@ -198,6 +198,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
PROTOCOL VERSION 38:
Incremental inventory sending mode
Unknown inventory serialization fields no longer throw an error
Mod-specific formspec version
*/

#define LATEST_PROTOCOL_VERSION 38
Expand All @@ -219,7 +220,16 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#define PASSWORD_SIZE 28 // Maximum password length. Allows for
// base64-encoded SHA-1 (27+\0).

#define FORMSPEC_API_VERSION 1
/*
Changes by FORMSPEC_API_VERSION:
FORMSPEC VERSION 1:
(too much)
FORMSPEC VERSION 2:
Forced real coordinates
background[]: 9-slice scaling parameters
*/
#define FORMSPEC_API_VERSION 2
#define FORMSPEC_VERSION_STRING "formspec_version[" TOSTRING(FORMSPEC_API_VERSION) "]"

#define TEXTURENAME_ALLOWED_CHARS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_.-"
Expand Down
3 changes: 3 additions & 0 deletions src/network/serverpackethandler.cpp
Expand Up @@ -386,6 +386,9 @@ void Server::handleCommand_ClientReady(NetworkPacket* pkt)
peer_id, major_ver, minor_ver, patch_ver,
full_ver);

if (pkt->getRemainingBytes() >= 2)
*pkt >> playersao->getPlayer()->formspec_version;

const std::vector<std::string> &players = m_clients.getPlayerNames();
NetworkPacket list_pkt(TOCLIENT_UPDATE_PLAYER_LIST, 0, peer_id);
list_pkt << (u8) PLAYER_LIST_INIT << (u16) players.size();
Expand Down
3 changes: 3 additions & 0 deletions src/remoteplayer.h
Expand Up @@ -130,6 +130,9 @@ class RemotePlayer : public Player

u16 protocol_version = 0;

// v1 for clients older than 5.1.0-dev
u16 formspec_version = 1;

session_t getPeerId() const { return m_peer_id; }

void setPeerId(session_t peer_id) { m_peer_id = peer_id; }
Expand Down
4 changes: 4 additions & 0 deletions src/script/lua_api/l_server.cpp
Expand Up @@ -233,6 +233,10 @@ int ModApiServer::l_get_player_information(lua_State *L)
lua_pushnumber(L, prot_vers);
lua_settable(L, table);

lua_pushstring(L, "formspec_version");
lua_pushnumber(L, player->formspec_version);
lua_settable(L, table);

#ifndef NDEBUG
lua_pushstring(L,"serialization_version");
lua_pushnumber(L, ser_vers);
Expand Down
9 changes: 5 additions & 4 deletions src/server.cpp
Expand Up @@ -1568,7 +1568,7 @@ void Server::SendChatMessage(session_t peer_id, const ChatMessage &message)
void Server::SendShowFormspecMessage(session_t peer_id, const std::string &formspec,
const std::string &formname)
{
NetworkPacket pkt(TOCLIENT_SHOW_FORMSPEC, 0 , peer_id);
NetworkPacket pkt(TOCLIENT_SHOW_FORMSPEC, 0, peer_id);
if (formspec.empty()){
//the client should close the formspec
//but make sure there wasn't another one open in meantime
Expand All @@ -1579,7 +1579,7 @@ void Server::SendShowFormspecMessage(session_t peer_id, const std::string &forms
pkt.putLongString("");
} else {
m_formspec_state_data[peer_id] = formname;
pkt.putLongString(FORMSPEC_VERSION_STRING + formspec);
pkt.putLongString(formspec);
}
pkt << formname;

Expand Down Expand Up @@ -1908,7 +1908,8 @@ void Server::SendPlayerInventoryFormspec(session_t peer_id)
return;

NetworkPacket pkt(TOCLIENT_INVENTORY_FORMSPEC, 0, peer_id);
pkt.putLongString(FORMSPEC_VERSION_STRING + player->inventory_formspec);
pkt.putLongString(player->inventory_formspec);

Send(&pkt);
}

Expand All @@ -1920,7 +1921,7 @@ void Server::SendPlayerFormspecPrepend(session_t peer_id)
return;

NetworkPacket pkt(TOCLIENT_FORMSPEC_PREPEND, 0, peer_id);
pkt << FORMSPEC_VERSION_STRING + player->formspec_prepend;
pkt << player->formspec_prepend;
Send(&pkt);
}

Expand Down

0 comments on commit 9acd36b

Please sign in to comment.