summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/CMakeLists.txt234
-rw-r--r--src/chat.cpp151
-rw-r--r--src/chat.h22
-rw-r--r--src/client/CMakeLists.txt4
-rw-r--r--src/client/camera.cpp90
-rw-r--r--src/client/camera.h12
-rw-r--r--src/client/client.cpp243
-rw-r--r--src/client/client.h30
-rw-r--r--src/client/clientenvironment.cpp65
-rw-r--r--src/client/clientevent.h55
-rw-r--r--src/client/clientlauncher.cpp92
-rw-r--r--src/client/clientlauncher.h2
-rw-r--r--src/client/clientmap.cpp383
-rw-r--r--src/client/clientmap.h42
-rw-r--r--src/client/clientmedia.cpp260
-rw-r--r--src/client/clientmedia.h166
-rw-r--r--src/client/clientobject.h2
-rw-r--r--src/client/clouds.cpp4
-rw-r--r--src/client/clouds.h5
-rw-r--r--src/client/content_cao.cpp147
-rw-r--r--src/client/content_cao.h4
-rw-r--r--src/client/content_mapblock.cpp80
-rw-r--r--src/client/content_mapblock.h7
-rw-r--r--src/client/fontengine.cpp284
-rw-r--r--src/client/fontengine.h22
-rw-r--r--src/client/game.cpp891
-rw-r--r--src/client/game.h6
-rw-r--r--src/client/gameui.cpp68
-rw-r--r--src/client/gameui.h9
-rw-r--r--src/client/guiscalingfilter.cpp7
-rw-r--r--src/client/hud.cpp173
-rw-r--r--src/client/hud.h26
-rw-r--r--src/client/imagefilters.cpp113
-rw-r--r--src/client/imagefilters.h6
-rw-r--r--src/client/inputhandler.cpp47
-rw-r--r--src/client/inputhandler.h95
-rw-r--r--src/client/joystick_controller.cpp80
-rw-r--r--src/client/joystick_controller.h5
-rw-r--r--src/client/keycode.cpp2
-rw-r--r--src/client/keys.h3
-rw-r--r--src/client/localplayer.cpp50
-rw-r--r--src/client/localplayer.h6
-rw-r--r--src/client/mapblock_mesh.cpp24
-rw-r--r--src/client/mesh.cpp597
-rw-r--r--src/client/mesh.h7
-rw-r--r--src/client/mesh_generator_thread.cpp53
-rw-r--r--src/client/mesh_generator_thread.h5
-rw-r--r--src/client/minimap.cpp5
-rw-r--r--src/client/particles.cpp4
-rw-r--r--src/client/render/anaglyph.cpp3
-rw-r--r--src/client/render/core.cpp19
-rw-r--r--src/client/render/core.h5
-rw-r--r--src/client/render/interlaced.cpp4
-rw-r--r--src/client/renderingengine.cpp168
-rw-r--r--src/client/renderingengine.h100
-rw-r--r--src/client/shader.cpp84
-rw-r--r--src/client/shader.h5
-rw-r--r--src/client/shadows/dynamicshadows.cpp165
-rw-r--r--src/client/shadows/dynamicshadows.h109
-rw-r--r--src/client/shadows/dynamicshadowsrender.cpp630
-rw-r--r--src/client/shadows/dynamicshadowsrender.h147
-rw-r--r--src/client/shadows/shadowsScreenQuad.cpp61
-rw-r--r--src/client/shadows/shadowsScreenQuad.h54
-rw-r--r--src/client/shadows/shadowsshadercallbacks.cpp (renamed from src/unittest/test_player.cpp)29
-rw-r--r--src/client/shadows/shadowsshadercallbacks.h48
-rw-r--r--src/client/sky.cpp224
-rw-r--r--src/client/sky.h18
-rw-r--r--src/client/sound_openal.cpp12
-rw-r--r--src/client/tile.cpp231
-rw-r--r--src/client/tile.h3
-rw-r--r--src/client/wieldmesh.cpp79
-rw-r--r--src/client/wieldmesh.h3
-rw-r--r--src/clientiface.cpp25
-rw-r--r--src/clientiface.h2
-rw-r--r--src/cmake_config.h.in1
-rw-r--r--src/constants.h3
-rw-r--r--src/content/mods.cpp146
-rw-r--r--src/content/mods.h15
-rw-r--r--src/content/subgames.cpp32
-rw-r--r--src/content/subgames.h2
-rw-r--r--src/convert_json.cpp54
-rw-r--r--src/convert_json.h4
-rw-r--r--src/database/database-dummy.cpp38
-rw-r--r--src/database/database-dummy.h9
-rw-r--r--src/database/database-files.cpp125
-rw-r--r--src/database/database-files.h26
-rw-r--r--src/database/database-leveldb.cpp16
-rw-r--r--src/database/database-postgresql.cpp41
-rw-r--r--src/database/database-postgresql.h2
-rw-r--r--src/database/database-redis.cpp6
-rw-r--r--src/database/database-sqlite3.cpp135
-rw-r--r--src/database/database-sqlite3.h25
-rw-r--r--src/database/database.h13
-rw-r--r--src/defaultsettings.cpp60
-rw-r--r--src/emerge.cpp90
-rw-r--r--src/emerge.h18
-rw-r--r--src/environment.cpp6
-rw-r--r--src/filesys.cpp139
-rw-r--r--src/filesys.h20
-rw-r--r--src/gamedef.h3
-rw-r--r--src/gettext.cpp4
-rw-r--r--src/gettext.h50
-rw-r--r--src/gui/CMakeLists.txt7
-rw-r--r--src/gui/guiButton.cpp86
-rw-r--r--src/gui/guiButton.h43
-rw-r--r--src/gui/guiChatConsole.cpp131
-rw-r--r--src/gui/guiChatConsole.h10
-rw-r--r--src/gui/guiConfirmRegistration.cpp19
-rw-r--r--src/gui/guiEditBox.cpp169
-rw-r--r--src/gui/guiEditBox.h9
-rw-r--r--src/gui/guiEditBoxWithScrollbar.cpp35
-rw-r--r--src/gui/guiEditBoxWithScrollbar.h6
-rw-r--r--src/gui/guiEngine.cpp79
-rw-r--r--src/gui/guiEngine.h7
-rw-r--r--src/gui/guiFormSpecMenu.cpp2305
-rw-r--r--src/gui/guiFormSpecMenu.h13
-rw-r--r--src/gui/guiHyperText.cpp23
-rw-r--r--src/gui/guiHyperText.h11
-rw-r--r--src/gui/guiKeyChangeMenu.cpp72
-rw-r--r--src/gui/guiPasswordChange.cpp8
-rw-r--r--src/gui/guiScene.cpp16
-rw-r--r--src/gui/guiScene.h1
-rw-r--r--src/gui/guiSkin.cpp42
-rw-r--r--src/gui/guiSkin.h10
-rw-r--r--src/gui/guiTable.cpp5
-rw-r--r--src/gui/guiVolumeChange.cpp15
-rw-r--r--src/gui/intlGUIEditBox.cpp626
-rw-r--r--src/gui/intlGUIEditBox.h68
-rw-r--r--src/gui/modalMenu.cpp13
-rw-r--r--src/gui/modalMenu.h4
-rw-r--r--src/gui/profilergraph.cpp15
-rw-r--r--src/gui/touchscreengui.cpp47
-rw-r--r--src/gui/touchscreengui.h14
-rw-r--r--src/httpfetch.cpp80
-rw-r--r--src/httpfetch.h27
-rw-r--r--src/hud.cpp1
-rw-r--r--src/hud.h6
-rw-r--r--src/inventory.cpp23
-rw-r--r--src/inventory.h4
-rw-r--r--src/inventorymanager.cpp16
-rw-r--r--src/irrlicht_changes/CGUITTFont.cpp117
-rw-r--r--src/irrlicht_changes/CGUITTFont.h12
-rw-r--r--src/irrlicht_changes/CMakeLists.txt7
-rw-r--r--src/irrlicht_changes/irrUString.h3891
-rw-r--r--src/irrlicht_changes/static_text.cpp74
-rw-r--r--src/irrlicht_changes/static_text.h49
-rw-r--r--src/irrlichttypes.h26
-rw-r--r--src/itemdef.cpp14
-rw-r--r--src/itemdef.h1
-rw-r--r--src/itemstackmetadata.cpp2
-rw-r--r--src/main.cpp111
-rw-r--r--src/map.cpp23
-rw-r--r--src/map.h5
-rw-r--r--src/map_settings_manager.cpp40
-rw-r--r--src/map_settings_manager.h6
-rw-r--r--src/mapblock.cpp191
-rw-r--r--src/mapblock.h20
-rw-r--r--src/mapgen/mapgen.cpp11
-rw-r--r--src/mapgen/mapgen_v6.cpp13
-rw-r--r--src/mapgen/mapgen_v6.h4
-rw-r--r--src/mapgen/mapgen_valleys.cpp3
-rw-r--r--src/mapgen/mg_biome.cpp90
-rw-r--r--src/mapgen/mg_biome.h27
-rw-r--r--src/mapgen/mg_ore.h12
-rw-r--r--src/mapgen/mg_schematic.cpp143
-rw-r--r--src/mapgen/mg_schematic.h17
-rw-r--r--src/mapnode.cpp58
-rw-r--r--src/mapnode.h8
-rw-r--r--src/network/address.cpp119
-rw-r--r--src/network/address.h35
-rw-r--r--src/network/clientopcodes.cpp2
-rw-r--r--src/network/clientpackethandler.cpp250
-rw-r--r--src/network/connection.cpp477
-rw-r--r--src/network/connection.h446
-rw-r--r--src/network/connectionthreads.cpp190
-rw-r--r--src/network/connectionthreads.h31
-rw-r--r--src/network/networkpacket.h2
-rw-r--r--src/network/networkprotocol.h17
-rw-r--r--src/network/serveropcodes.cpp4
-rw-r--r--src/network/serverpackethandler.cpp128
-rw-r--r--src/network/socket.cpp74
-rw-r--r--src/network/socket.h14
-rw-r--r--src/nodedef.cpp82
-rw-r--r--src/nodedef.h43
-rw-r--r--src/nodemetadata.cpp6
-rw-r--r--src/nodemetadata.h2
-rw-r--r--src/noise.cpp51
-rw-r--r--src/noise.h15
-rw-r--r--src/object_properties.cpp36
-rw-r--r--src/object_properties.h2
-rw-r--r--src/player.cpp59
-rw-r--r--src/player.h52
-rw-r--r--src/porting.cpp13
-rw-r--r--src/porting.h6
-rw-r--r--src/remoteplayer.cpp30
-rw-r--r--src/remoteplayer.h3
-rw-r--r--src/rollback.cpp6
-rw-r--r--src/rollback.h1
-rw-r--r--src/script/common/c_content.cpp83
-rw-r--r--src/script/common/c_content.h14
-rw-r--r--src/script/common/c_converter.cpp30
-rw-r--r--src/script/common/c_internal.cpp84
-rw-r--r--src/script/common/c_internal.h18
-rw-r--r--src/script/cpp_api/s_async.cpp108
-rw-r--r--src/script/cpp_api/s_async.h39
-rw-r--r--src/script/cpp_api/s_base.cpp11
-rw-r--r--src/script/cpp_api/s_base.h17
-rw-r--r--src/script/cpp_api/s_client.cpp88
-rw-r--r--src/script/cpp_api/s_entity.cpp2
-rw-r--r--src/script/cpp_api/s_entity.h2
-rw-r--r--src/script/cpp_api/s_env.cpp63
-rw-r--r--src/script/cpp_api/s_env.h5
-rw-r--r--src/script/cpp_api/s_item.cpp36
-rw-r--r--src/script/cpp_api/s_item.h14
-rw-r--r--src/script/cpp_api/s_mainmenu.h2
-rw-r--r--src/script/cpp_api/s_node.cpp1
-rw-r--r--src/script/cpp_api/s_node.h1
-rw-r--r--src/script/cpp_api/s_nodemeta.cpp18
-rw-r--r--src/script/cpp_api/s_player.cpp2
-rw-r--r--src/script/cpp_api/s_player.h2
-rw-r--r--src/script/cpp_api/s_security.cpp131
-rw-r--r--src/script/cpp_api/s_security.h15
-rw-r--r--src/script/cpp_api/s_server.cpp64
-rw-r--r--src/script/cpp_api/s_server.h6
-rw-r--r--src/script/lua_api/l_areastore.cpp2
-rw-r--r--src/script/lua_api/l_auth.cpp5
-rw-r--r--src/script/lua_api/l_camera.cpp2
-rw-r--r--src/script/lua_api/l_env.cpp65
-rw-r--r--src/script/lua_api/l_env.h22
-rw-r--r--src/script/lua_api/l_http.cpp66
-rw-r--r--src/script/lua_api/l_http.h3
-rw-r--r--src/script/lua_api/l_inventory.cpp15
-rw-r--r--src/script/lua_api/l_inventory.h2
-rw-r--r--src/script/lua_api/l_item.cpp2
-rw-r--r--src/script/lua_api/l_itemstackmeta.cpp2
-rw-r--r--src/script/lua_api/l_localplayer.cpp36
-rw-r--r--src/script/lua_api/l_localplayer.h3
-rw-r--r--src/script/lua_api/l_mainmenu.cpp247
-rw-r--r--src/script/lua_api/l_mainmenu.h4
-rw-r--r--src/script/lua_api/l_mapgen.cpp125
-rw-r--r--src/script/lua_api/l_metadata.cpp5
-rw-r--r--src/script/lua_api/l_minimap.cpp2
-rw-r--r--src/script/lua_api/l_modchannels.cpp2
-rw-r--r--src/script/lua_api/l_nodemeta.cpp9
-rw-r--r--src/script/lua_api/l_nodetimer.cpp2
-rw-r--r--src/script/lua_api/l_noise.cpp10
-rw-r--r--src/script/lua_api/l_object.cpp207
-rw-r--r--src/script/lua_api/l_playermeta.cpp2
-rw-r--r--src/script/lua_api/l_server.cpp102
-rw-r--r--src/script/lua_api/l_server.h15
-rw-r--r--src/script/lua_api/l_settings.cpp80
-rw-r--r--src/script/lua_api/l_storage.cpp20
-rw-r--r--src/script/lua_api/l_util.cpp209
-rw-r--r--src/script/lua_api/l_util.h28
-rw-r--r--src/script/lua_api/l_vmanip.cpp2
-rw-r--r--src/script/scripting_mainmenu.cpp6
-rw-r--r--src/script/scripting_mainmenu.h5
-rw-r--r--src/serialization.cpp160
-rw-r--r--src/serialization.h14
-rw-r--r--src/server.cpp472
-rw-r--r--src/server.h43
-rw-r--r--src/server/luaentity_sao.cpp13
-rw-r--r--src/server/luaentity_sao.h5
-rw-r--r--src/server/mods.cpp8
-rw-r--r--src/server/player_sao.cpp45
-rw-r--r--src/server/player_sao.h10
-rw-r--r--src/server/serveractiveobject.h7
-rw-r--r--src/server/serverinventorymgr.cpp32
-rw-r--r--src/server/unit_sao.cpp31
-rw-r--r--src/serverenvironment.cpp23
-rw-r--r--src/serverenvironment.h15
-rw-r--r--src/serverlist.cpp1
-rw-r--r--src/settings.cpp133
-rw-r--r--src/settings.h52
-rw-r--r--src/settings_translation_file.cpp119
-rw-r--r--src/skyparams.h45
-rw-r--r--src/staticobject.cpp24
-rw-r--r--src/tool.cpp106
-rw-r--r--src/tool.h20
-rw-r--r--src/translation.cpp8
-rw-r--r--src/unittest/CMakeLists.txt5
-rw-r--r--src/unittest/test.cpp6
-rw-r--r--src/unittest/test_areastore.cpp4
-rw-r--r--src/unittest/test_clientactiveobjectmgr.cpp1
-rw-r--r--src/unittest/test_compression.cpp42
-rw-r--r--src/unittest/test_config.h.in1
-rw-r--r--src/unittest/test_connection.cpp10
-rw-r--r--src/unittest/test_gettext.cpp43
-rw-r--r--src/unittest/test_irrptr.cpp10
-rw-r--r--src/unittest/test_map.cpp68
-rw-r--r--src/unittest/test_map_settings_manager.cpp5
-rw-r--r--src/unittest/test_mod/test_mod/init.lua1
-rw-r--r--src/unittest/test_mod/test_mod/mod.conf2
-rw-r--r--src/unittest/test_modmetadatadatabase.cpp253
-rw-r--r--src/unittest/test_noderesolver.cpp2
-rw-r--r--src/unittest/test_schematic.cpp40
-rw-r--r--src/unittest/test_servermodmanager.cpp21
-rw-r--r--src/unittest/test_socket.cpp18
-rw-r--r--src/unittest/test_utilities.cpp99
-rw-r--r--src/unittest/test_voxelarea.cpp18
-rw-r--r--src/util/CMakeLists.txt1
-rw-r--r--src/util/Optional.h32
-rw-r--r--src/util/base64.cpp29
-rw-r--r--src/util/numeric.cpp19
-rw-r--r--src/util/numeric.h4
-rwxr-xr-xsrc/util/png.cpp68
-rwxr-xr-x[-rw-r--r--]src/util/png.h (renamed from src/cloudparams.h)17
-rw-r--r--src/util/pointer.h58
-rw-r--r--src/util/serialize.cpp5
-rw-r--r--src/util/string.cpp450
-rw-r--r--src/util/string.h43
-rw-r--r--src/version.cpp1
312 files changed, 11898 insertions, 12263 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 7bcf8d6c7..7f207244c 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -55,7 +55,7 @@ if(NOT USE_CURL)
endif()
-option(ENABLE_GETTEXT "Use GetText for internationalization" TRUE)
+option(ENABLE_GETTEXT "Use GetText for internationalization" ${BUILD_CLIENT})
set(USE_GETTEXT FALSE)
if(ENABLE_GETTEXT)
@@ -63,14 +63,13 @@ if(ENABLE_GETTEXT)
if(GETTEXTLIB_FOUND)
if(WIN32)
message(STATUS "GetText library: ${GETTEXT_LIBRARY}")
- message(STATUS "GetText DLL: ${GETTEXT_DLL}")
- message(STATUS "GetText iconv DLL: ${GETTEXT_ICONV_DLL}")
+ message(STATUS "GetText DLL(s): ${GETTEXT_DLL}")
endif()
set(USE_GETTEXT TRUE)
message(STATUS "GetText enabled; locales found: ${GETTEXT_AVAILABLE_LOCALES}")
endif(GETTEXTLIB_FOUND)
else()
- mark_as_advanced(GETTEXT_ICONV_DLL GETTEXT_INCLUDE_DIR GETTEXT_LIBRARY GETTEXT_MSGFMT)
+ mark_as_advanced(GETTEXT_INCLUDE_DIR GETTEXT_LIBRARY GETTEXT_MSGFMT)
message(STATUS "GetText disabled.")
endif()
@@ -102,12 +101,18 @@ endif()
option(ENABLE_GLES "Use OpenGL ES instead of OpenGL" FALSE)
mark_as_advanced(ENABLE_GLES)
+
+option(ENABLE_TOUCH "Enable Touchscreen support" FALSE)
+if(ENABLE_TOUCH)
+ add_definitions(-DHAVE_TOUCHSCREENGUI)
+endif()
+
if(BUILD_CLIENT)
- if(ENABLE_GLES)
- find_package(OpenGLES2 REQUIRED)
- else()
- # transitive dependency from Irrlicht (see longer explanation below)
- if(NOT WIN32)
+ # transitive dependency from Irrlicht (see longer explanation below)
+ if(NOT WIN32)
+ if(ENABLE_GLES)
+ find_package(OpenGLES2 REQUIRED)
+ else()
set(OPENGL_GL_PREFERENCE "LEGACY" CACHE STRING
"See CMake Policy CMP0072 for reference. GLVND is broken on some nvidia setups")
set(OpenGL_GL_PREFERENCE ${OPENGL_GL_PREFERENCE})
@@ -117,17 +122,9 @@ if(BUILD_CLIENT)
endif()
endif()
-
-option(ENABLE_FREETYPE "Enable FreeType2 (TrueType fonts and basic unicode support)" TRUE)
-set(USE_FREETYPE FALSE)
-
-if(ENABLE_FREETYPE)
- find_package(Freetype)
- if(FREETYPE_FOUND)
- message(STATUS "Freetype enabled.")
- set(USE_FREETYPE TRUE)
- endif()
-endif(ENABLE_FREETYPE)
+if(BUILD_CLIENT)
+ find_package(Freetype REQUIRED)
+endif()
option(ENABLE_CURSES "Enable ncurses console" TRUE)
set(USE_CURSES FALSE)
@@ -147,7 +144,17 @@ option(ENABLE_POSTGRESQL "Enable PostgreSQL backend" TRUE)
set(USE_POSTGRESQL FALSE)
if(ENABLE_POSTGRESQL)
- find_package("PostgreSQL")
+ if(CMAKE_VERSION VERSION_LESS "3.20")
+ find_package(PostgreSQL QUIET)
+ # Before CMake 3.20 FindPostgreSQL.cmake always looked for server includes
+ # but we don't need them, so continue anyway if only those are missing.
+ if(PostgreSQL_INCLUDE_DIR AND PostgreSQL_LIBRARY)
+ set(PostgreSQL_FOUND TRUE)
+ set(PostgreSQL_INCLUDE_DIRS ${PostgreSQL_INCLUDE_DIR})
+ endif()
+ else()
+ find_package(PostgreSQL)
+ endif()
if(PostgreSQL_FOUND)
set(USE_POSTGRESQL TRUE)
@@ -194,6 +201,7 @@ endif(ENABLE_REDIS)
find_package(SQLite3 REQUIRED)
+
OPTION(ENABLE_PROMETHEUS "Enable prometheus client support" FALSE)
set(USE_PROMETHEUS FALSE)
@@ -230,6 +238,10 @@ if(ENABLE_SPATIAL)
endif(ENABLE_SPATIAL)
+find_package(ZLIB REQUIRED)
+find_package(Zstd REQUIRED)
+
+
if(NOT MSVC)
set(USE_GPROF FALSE CACHE BOOL "Use -pg flag for g++")
endif()
@@ -256,62 +268,34 @@ if(WIN32)
else() # Probably MinGW = GCC
set(PLATFORM_LIBS "")
endif()
- set(PLATFORM_LIBS ws2_32.lib version.lib shlwapi.lib ${PLATFORM_LIBS})
+ set(PLATFORM_LIBS ws2_32.lib version.lib shlwapi.lib winmm.lib ${PLATFORM_LIBS})
- # Zlib stuff
- find_path(ZLIB_INCLUDE_DIR "zlib.h" DOC "Zlib include directory")
- find_library(ZLIB_LIBRARIES "zlib" DOC "Path to zlib library")
+ set(EXTRA_DLL "" CACHE FILEPATH "Optional paths to additional DLLs that should be packaged")
- # Dll's are automatically copied to the output directory by vcpkg when VCPKG_APPLOCAL_DEPS=ON
+ # DLLs are automatically copied to the output directory by vcpkg when VCPKG_APPLOCAL_DEPS=ON
if(NOT VCPKG_APPLOCAL_DEPS)
- find_file(ZLIB_DLL NAMES "zlib.dll" "zlib1.dll" DOC "Path to zlib.dll for installation (optional)")
+ set(ZLIB_DLL "" CACHE FILEPATH "Path to Zlib DLL for installation (optional)")
+ set(ZSTD_DLL "" CACHE FILEPATH "Path to Zstd DLL for installation (optional)")
if(ENABLE_SOUND)
set(OPENAL_DLL "" CACHE FILEPATH "Path to OpenAL32.dll for installation (optional)")
set(OGG_DLL "" CACHE FILEPATH "Path to libogg.dll for installation (optional)")
- set(VORBIS_DLL "" CACHE FILEPATH "Path to libvorbis.dll for installation (optional)")
- set(VORBISFILE_DLL "" CACHE FILEPATH "Path to libvorbisfile.dll for installation (optional)")
+ set(VORBIS_DLL "" CACHE FILEPATH "Path to Vorbis DLLs for installation (optional)")
+ endif()
+ if(USE_GETTEXT)
+ set(GETTEXT_DLL "" CACHE FILEPATH "Path to Intl/Iconv DLLs for installation (optional)")
endif()
if(USE_LUAJIT)
set(LUA_DLL "" CACHE FILEPATH "Path to luajit-5.1.dll for installation (optional)")
endif()
endif()
-
else()
# Unix probably
if(BUILD_CLIENT)
if(NOT HAIKU AND NOT APPLE)
find_package(X11 REQUIRED)
endif(NOT HAIKU AND NOT APPLE)
+ endif()
- ##
- # The following dependencies are transitive dependencies from Irrlicht.
- # Minetest itself does not use them, but we link them so that statically
- # linking Irrlicht works.
- if(NOT HAIKU AND NOT APPLE)
- # This way Xxf86vm is found on OpenBSD too
- find_library(XXF86VM_LIBRARY Xxf86vm)
- mark_as_advanced(XXF86VM_LIBRARY)
- set(CLIENT_PLATFORM_LIBS ${CLIENT_PLATFORM_LIBS} ${XXF86VM_LIBRARY})
- endif(NOT HAIKU AND NOT APPLE)
-
- find_package(JPEG REQUIRED)
- find_package(BZip2 REQUIRED)
- find_package(PNG REQUIRED)
- if(APPLE)
- find_library(CARBON_LIB Carbon REQUIRED)
- find_library(COCOA_LIB Cocoa REQUIRED)
- find_library(IOKIT_LIB IOKit REQUIRED)
- mark_as_advanced(
- CARBON_LIB
- COCOA_LIB
- IOKIT_LIB
- )
- SET(CLIENT_PLATFORM_LIBS ${CLIENT_PLATFORM_LIBS} ${CARBON_LIB} ${COCOA_LIB} ${IOKIT_LIB})
- endif(APPLE)
- ##
- endif(BUILD_CLIENT)
-
- find_package(ZLIB REQUIRED)
set(PLATFORM_LIBS -lpthread ${CMAKE_DL_LIBS})
if(APPLE)
set(PLATFORM_LIBS "-framework CoreFoundation" ${PLATFORM_LIBS})
@@ -501,15 +485,13 @@ endif()
include_directories(
${PROJECT_BINARY_DIR}
${PROJECT_SOURCE_DIR}
- ${IRRLICHT_INCLUDE_DIR}
${ZLIB_INCLUDE_DIR}
- ${PNG_INCLUDE_DIR}
- ${SOUND_INCLUDE_DIRS}
+ ${ZSTD_INCLUDE_DIR}
${SQLITE3_INCLUDE_DIR}
${LUA_INCLUDE_DIR}
${GMP_INCLUDE_DIR}
${JSON_INCLUDE_DIR}
- ${X11_INCLUDE_DIR}
+ ${LUA_BIT_INCLUDE_DIR}
${PROJECT_SOURCE_DIR}/script
)
@@ -517,8 +499,12 @@ if(USE_GETTEXT)
include_directories(${GETTEXT_INCLUDE_DIR})
endif()
-if(USE_FREETYPE)
- include_directories(${FREETYPE_INCLUDE_DIRS})
+if(BUILD_CLIENT)
+ include_directories(
+ ${FREETYPE_INCLUDE_DIRS}
+ ${SOUND_INCLUDE_DIRS}
+ ${X11_INCLUDE_DIR}
+ )
endif()
if(USE_CURL)
@@ -526,8 +512,11 @@ if(USE_CURL)
endif()
-set(EXECUTABLE_OUTPUT_PATH "${CMAKE_SOURCE_DIR}/bin")
-
+# When cross-compiling assume the user doesn't want to run the executable anyway,
+# otherwise place it in <source dir>/bin/ since Minetest can only run from there.
+if(NOT CMAKE_CROSSCOMPILING)
+ set(EXECUTABLE_OUTPUT_PATH "${CMAKE_SOURCE_DIR}/bin")
+endif()
if(BUILD_CLIENT)
add_executable(${PROJECT_NAME} ${client_SRCS} ${extra_windows_SRCS})
@@ -535,18 +524,17 @@ if(BUILD_CLIENT)
target_link_libraries(
${PROJECT_NAME}
${ZLIB_LIBRARIES}
- ${IRRLICHT_LIBRARY}
- ${JPEG_LIBRARIES}
- ${BZIP2_LIBRARIES}
- ${PNG_LIBRARIES}
+ IrrlichtMt::IrrlichtMt
+ ${ZSTD_LIBRARY}
${X11_LIBRARIES}
${SOUND_LIBRARIES}
${SQLITE3_LIBRARY}
${LUA_LIBRARY}
${GMP_LIBRARY}
${JSON_LIBRARY}
+ ${LUA_BIT_LIBRARY}
+ ${FREETYPE_LIBRARY}
${PLATFORM_LIBS}
- ${CLIENT_PLATFORM_LIBS}
)
if(NOT USE_LUAJIT)
set_target_properties(${PROJECT_NAME} PROPERTIES
@@ -580,17 +568,11 @@ if(BUILD_CLIENT)
${CURL_LIBRARY}
)
endif()
- if(USE_FREETYPE)
- if(FREETYPE_PKGCONFIG_FOUND)
- set_target_properties(${PROJECT_NAME}
- PROPERTIES
- COMPILE_FLAGS "${FREETYPE_CFLAGS_STR}"
- )
- endif()
- target_link_libraries(
- ${PROJECT_NAME}
- ${FREETYPE_LIBRARY}
- )
+ if(FREETYPE_PKGCONFIG_FOUND)
+ set_target_properties(${PROJECT_NAME}
+ PROPERTIES
+ COMPILE_FLAGS "${FREETYPE_CFLAGS_STR}"
+ )
endif()
if (USE_CURSES)
target_link_libraries(${PROJECT_NAME} ${CURSES_LIBRARIES})
@@ -616,12 +598,19 @@ endif(BUILD_CLIENT)
if(BUILD_SERVER)
add_executable(${PROJECT_NAME}server ${server_SRCS} ${extra_windows_SRCS})
add_dependencies(${PROJECT_NAME}server GenerateVersion)
+
+ get_target_property(
+ IRRLICHT_INCLUDES IrrlichtMt::IrrlichtMt INTERFACE_INCLUDE_DIRECTORIES)
+ # Doesn't work without PRIVATE/PUBLIC/INTERFACE mode specified.
+ target_include_directories(${PROJECT_NAME}server PRIVATE ${IRRLICHT_INCLUDES})
target_link_libraries(
${PROJECT_NAME}server
${ZLIB_LIBRARIES}
+ ${ZSTD_LIBRARY}
${SQLITE3_LIBRARY}
${JSON_LIBRARY}
${LUA_LIBRARY}
+ ${LUA_BIT_LIBRARY}
${GMP_LIBRARY}
${PLATFORM_LIBS}
)
@@ -668,13 +657,15 @@ endif(BUILD_SERVER)
# see issue #4638
set(GETTEXT_BLACKLISTED_LOCALES
ar
+ dv
he
- ky
+ hi
+ kn
ms_Arab
th
)
-option(APPLY_LOCALE_BLACKLIST "Use a blacklist to avoid broken locales" TRUE)
+option(APPLY_LOCALE_BLACKLIST "Use a blacklist to avoid known broken locales" TRUE)
if (GETTEXTLIB_FOUND AND APPLY_LOCALE_BLACKLIST)
set(GETTEXT_USED_LOCALES "")
@@ -684,6 +675,8 @@ if (GETTEXTLIB_FOUND AND APPLY_LOCALE_BLACKLIST)
endif()
endforeach()
message(STATUS "Locale blacklist applied; Locales used: ${GETTEXT_USED_LOCALES}")
+elseif (GETTEXTLIB_FOUND)
+ set(GETTEXT_USED_LOCALES ${GETTEXT_AVAILABLE_LOCALES})
endif()
# Set some optimizations and tweaks
@@ -713,7 +706,7 @@ if(MSVC)
# Flags that cannot be shared between cl and clang-cl
# https://clang.llvm.org/docs/UsersManual.html#clang-cl
- if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
+ if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fuse-ld=lld")
# Disable pragma-pack warning
@@ -731,7 +724,7 @@ else()
else()
set(RELEASE_WARNING_FLAGS "")
endif()
- if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
+ if(CMAKE_CXX_COMPILER_ID MATCHES "(Apple)?Clang")
set(WARNING_FLAGS "${WARNING_FLAGS} -Wsign-compare")
endif()
@@ -742,7 +735,16 @@ else()
check_c_source_compiles("#ifndef __aarch64__\n#error\n#endif\nint main(){}" IS_AARCH64)
if(IS_AARCH64)
# Move text segment below LuaJIT's 47-bit limit (see issue #9367)
- SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-Ttext-segment=0x200000000")
+ if(CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
+ # FreeBSD uses lld, and lld does not support -Ttext-segment, suggesting
+ # --image-base instead. Not sure if it's equivalent change for the purpose
+ # but at least if fixes build on FreeBSD/aarch64
+ # XXX: the condition should also be changed to check for lld regardless of
+ # os, bit CMake doesn't have anything like CMAKE_LINKER_IS_LLD yet
+ SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--image-base=0x200000000")
+ else()
+ SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-Ttext-segment=0x200000000")
+ endif()
endif()
endif()
@@ -756,6 +758,16 @@ else()
# - we don't deal with Inf/NaN or signed zero
set(MATH_FLAGS "-fno-math-errno -fno-trapping-math -ffinite-math-only -fno-signed-zeros")
+ # Enable SSE for floating point math on 32-bit x86 by default
+ # reasoning see minetest issue #11810 and https://gcc.gnu.org/wiki/FloatingPointMath
+ if(CMAKE_SIZEOF_VOID_P EQUAL 4)
+ check_c_source_compiles("#ifndef __i686__\n#error\n#endif\nint main(){}" IS_I686)
+ if(IS_I686)
+ message(STATUS "Detected Intel x86: using SSE instead of x87 FPU")
+ set(OTHER_FLAGS "${OTHER_FLAGS} -mfpmath=sse -msse")
+ endif()
+ endif()
+
set(CMAKE_CXX_FLAGS_RELEASE "-DNDEBUG ${RELEASE_WARNING_FLAGS} ${WARNING_FLAGS} ${OTHER_FLAGS} -Wall -pipe -funroll-loops")
if(CMAKE_SYSTEM_NAME MATCHES "(Darwin|BSD|DragonFly)")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Os")
@@ -768,7 +780,7 @@ else()
else()
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} ${MATH_FLAGS}")
endif()
- endif(CMAKE_SYSTEM_NAME MATCHES "(Darwin|BSD|DragonFly)")
+ endif()
set(CMAKE_CXX_FLAGS_SEMIDEBUG "-g -O1 -Wall ${WARNING_FLAGS} ${OTHER_FLAGS}")
set(CMAKE_CXX_FLAGS_DEBUG "-g -O0 -Wall ${WARNING_FLAGS} ${OTHER_FLAGS}")
@@ -785,6 +797,9 @@ endif()
# Installation
if(WIN32)
+ if(EXTRA_DLL)
+ install(FILES ${EXTRA_DLL} DESTINATION ${BINDIR})
+ endif()
if(VCPKG_APPLOCAL_DEPS)
# Collect the dll's from the output path
install(DIRECTORY ${EXECUTABLE_OUTPUT_PATH}/Release/
@@ -805,7 +820,7 @@ if(WIN32)
FILES_MATCHING PATTERN "*.dll")
else()
# Use the old-style way to install dll's
- if(USE_SOUND)
+ if(BUILD_CLIENT AND USE_SOUND)
if(OPENAL_DLL)
install(FILES ${OPENAL_DLL} DESTINATION ${BINDIR})
endif()
@@ -815,9 +830,6 @@ if(WIN32)
if(VORBIS_DLL)
install(FILES ${VORBIS_DLL} DESTINATION ${BINDIR})
endif()
- if(VORBISFILE_DLL)
- install(FILES ${VORBISFILE_DLL} DESTINATION ${BINDIR})
- endif()
endif()
if(CURL_DLL)
install(FILES ${CURL_DLL} DESTINATION ${BINDIR})
@@ -825,7 +837,10 @@ if(WIN32)
if(ZLIB_DLL)
install(FILES ${ZLIB_DLL} DESTINATION ${BINDIR})
endif()
- if(FREETYPE_DLL)
+ if(ZSTD_DLL)
+ install(FILES ${ZSTD_DLL} DESTINATION ${BINDIR})
+ endif()
+ if(BUILD_CLIENT AND FREETYPE_DLL)
install(FILES ${FREETYPE_DLL} DESTINATION ${BINDIR})
endif()
if(SQLITE3_DLL)
@@ -837,6 +852,12 @@ if(WIN32)
if(LUA_DLL)
install(FILES ${LUA_DLL} DESTINATION ${BINDIR})
endif()
+ if(BUILD_CLIENT AND IRRLICHT_DLL)
+ install(FILES ${IRRLICHT_DLL} DESTINATION ${BINDIR})
+ endif()
+ if(BUILD_CLIENT AND USE_GETTEXT AND GETTEXT_DLL)
+ install(FILES ${GETTEXT_DLL} DESTINATION ${BINDIR})
+ endif()
endif()
endif()
@@ -864,29 +885,8 @@ if(BUILD_CLIENT)
endforeach()
endif()
- if(USE_FREETYPE)
- install(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../fonts" DESTINATION "${SHAREDIR}"
- FILES_MATCHING PATTERN "*.ttf" PATTERN "*.txt")
- else()
- install(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../fonts" DESTINATION "${SHAREDIR}"
- FILES_MATCHING PATTERN "*.png" PATTERN "*.xml")
- endif()
-
- if(WIN32)
- if(NOT VCPKG_APPLOCAL_DEPS)
- if(DEFINED IRRLICHT_DLL)
- install(FILES ${IRRLICHT_DLL} DESTINATION ${BINDIR})
- endif()
- if(USE_GETTEXT)
- if(DEFINED GETTEXT_DLL)
- install(FILES ${GETTEXT_DLL} DESTINATION ${BINDIR})
- endif()
- if(DEFINED GETTEXT_ICONV_DLL)
- install(FILES ${GETTEXT_ICONV_DLL} DESTINATION ${BINDIR})
- endif()
- endif()
- endif()
- endif()
+ install(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../fonts" DESTINATION "${SHAREDIR}"
+ FILES_MATCHING PATTERN "*.ttf" PATTERN "*.txt")
endif(BUILD_CLIENT)
if(BUILD_SERVER)
diff --git a/src/chat.cpp b/src/chat.cpp
index c9317a079..92df038e8 100644
--- a/src/chat.cpp
+++ b/src/chat.cpp
@@ -35,10 +35,23 @@ ChatBuffer::ChatBuffer(u32 scrollback):
if (m_scrollback == 0)
m_scrollback = 1;
m_empty_formatted_line.first = true;
+
+ m_cache_clickable_chat_weblinks = false;
+ // Curses mode cannot access g_settings here
+ if (g_settings != nullptr) {
+ m_cache_clickable_chat_weblinks = g_settings->getBool("clickable_chat_weblinks");
+ if (m_cache_clickable_chat_weblinks) {
+ std::string colorval = g_settings->get("chat_weblink_color");
+ parseColorString(colorval, m_cache_chat_weblink_color, false, 255);
+ m_cache_chat_weblink_color.setAlpha(255);
+ }
+ }
}
void ChatBuffer::addLine(const std::wstring &name, const std::wstring &text)
{
+ m_lines_modified = true;
+
ChatLine line(name, text);
m_unformatted.push_back(line);
@@ -61,6 +74,7 @@ void ChatBuffer::clear()
m_unformatted.clear();
m_formatted.clear();
m_scroll = 0;
+ m_lines_modified = true;
}
u32 ChatBuffer::getLineCount() const
@@ -88,14 +102,11 @@ void ChatBuffer::deleteOldest(u32 count)
u32 del_unformatted = 0;
u32 del_formatted = 0;
- while (count > 0 && del_unformatted < m_unformatted.size())
- {
+ while (count > 0 && del_unformatted < m_unformatted.size()) {
++del_unformatted;
// keep m_formatted in sync
- if (del_formatted < m_formatted.size())
- {
-
+ if (del_formatted < m_formatted.size()) {
sanity_check(m_formatted[del_formatted].first);
++del_formatted;
while (del_formatted < m_formatted.size() &&
@@ -109,6 +120,9 @@ void ChatBuffer::deleteOldest(u32 count)
m_unformatted.erase(m_unformatted.begin(), m_unformatted.begin() + del_unformatted);
m_formatted.erase(m_formatted.begin(), m_formatted.begin() + del_formatted);
+ if (del_unformatted > 0)
+ m_lines_modified = true;
+
if (at_bottom)
m_scroll = getBottomScrollPos();
else
@@ -128,11 +142,6 @@ u32 ChatBuffer::getRows() const
return m_rows;
}
-void ChatBuffer::scrollTop()
-{
- m_scroll = getTopScrollPos();
-}
-
void ChatBuffer::reformat(u32 cols, u32 rows)
{
if (cols == 0 || rows == 0)
@@ -263,78 +272,144 @@ u32 ChatBuffer::formatChatLine(const ChatLine& line, u32 cols,
//EnrichedString line_text(line.text);
next_line.first = true;
- bool text_processing = false;
+ // Set/use forced newline after the last frag in each line
+ bool mark_newline = false;
// Produce fragments and layout them into lines
- while (!next_frags.empty() || in_pos < line.text.size())
- {
+ while (!next_frags.empty() || in_pos < line.text.size()) {
+ mark_newline = false; // now using this to USE line-end frag
+
// Layout fragments into lines
- while (!next_frags.empty())
- {
+ while (!next_frags.empty()) {
ChatFormattedFragment& frag = next_frags[0];
- if (frag.text.size() <= cols - out_column)
- {
+
+ // Force newline after this frag, if marked
+ if (frag.column == INT_MAX)
+ mark_newline = true;
+
+ if (frag.text.size() <= cols - out_column) {
// Fragment fits into current line
frag.column = out_column;
next_line.fragments.push_back(frag);
out_column += frag.text.size();
next_frags.erase(next_frags.begin());
- }
- else
- {
+ } else {
// Fragment does not fit into current line
// So split it up
temp_frag.text = frag.text.substr(0, cols - out_column);
temp_frag.column = out_column;
- //temp_frag.bold = frag.bold;
+ temp_frag.weblink = frag.weblink;
+
next_line.fragments.push_back(temp_frag);
frag.text = frag.text.substr(cols - out_column);
+ frag.column = 0;
out_column = cols;
}
- if (out_column == cols || text_processing)
- {
+
+ if (out_column == cols || mark_newline) {
// End the current line
destination.push_back(next_line);
num_added++;
next_line.fragments.clear();
next_line.first = false;
- out_column = text_processing ? hanging_indentation : 0;
+ out_column = hanging_indentation;
+ mark_newline = false;
}
}
- // Produce fragment
- if (in_pos < line.text.size())
- {
- u32 remaining_in_input = line.text.size() - in_pos;
- u32 remaining_in_output = cols - out_column;
+ // Produce fragment(s) for next formatted line
+ if (!(in_pos < line.text.size()))
+ continue;
+
+ const std::wstring &linestring = line.text.getString();
+ u32 remaining_in_output = cols - out_column;
+ size_t http_pos = std::wstring::npos;
+ mark_newline = false; // now using this to SET line-end frag
+ // Construct all frags for next output line
+ while (!mark_newline) {
// Determine a fragment length <= the minimum of
// remaining_in_{in,out}put. Try to end the fragment
// on a word boundary.
- u32 frag_length = 1, space_pos = 0;
+ u32 frag_length = 0, space_pos = 0;
+ u32 remaining_in_input = line.text.size() - in_pos;
+
+ if (m_cache_clickable_chat_weblinks) {
+ // Note: unsigned(-1) on fail
+ http_pos = linestring.find(L"https://", in_pos);
+ if (http_pos == std::wstring::npos)
+ http_pos = linestring.find(L"http://", in_pos);
+ if (http_pos != std::wstring::npos)
+ http_pos -= in_pos;
+ }
+
while (frag_length < remaining_in_input &&
- frag_length < remaining_in_output)
- {
- if (iswspace(line.text.getString()[in_pos + frag_length]))
+ frag_length < remaining_in_output) {
+ if (iswspace(linestring[in_pos + frag_length]))
space_pos = frag_length;
++frag_length;
}
+
+ if (http_pos >= remaining_in_output) {
+ // Http not in range, grab until space or EOL, halt as normal.
+ // Note this works because (http_pos = npos) is unsigned(-1)
+
+ mark_newline = true;
+ } else if (http_pos == 0) {
+ // At http, grab ALL until FIRST whitespace or end marker. loop.
+ // If at end of string, next loop will be empty string to mark end of weblink.
+
+ frag_length = 6; // Frag is at least "http://"
+
+ // Chars to mark end of weblink
+ // TODO? replace this with a safer (slower) regex whitelist?
+ static const std::wstring delim_chars = L"\'\";,";
+ wchar_t tempchar = linestring[in_pos+frag_length];
+ while (frag_length < remaining_in_input &&
+ !iswspace(tempchar) &&
+ delim_chars.find(tempchar) == std::wstring::npos) {
+ ++frag_length;
+ tempchar = linestring[in_pos+frag_length];
+ }
+
+ space_pos = frag_length - 1;
+ // This frag may need to be force-split. That's ok, urls aren't "words"
+ if (frag_length >= remaining_in_output) {
+ mark_newline = true;
+ }
+ } else {
+ // Http in range, grab until http, loop
+
+ space_pos = http_pos - 1;
+ frag_length = http_pos;
+ }
+
+ // Include trailing space in current frag
if (space_pos != 0 && frag_length < remaining_in_input)
frag_length = space_pos + 1;
temp_frag.text = line.text.substr(in_pos, frag_length);
- temp_frag.column = 0;
- //temp_frag.bold = 0;
+ // A hack so this frag remembers mark_newline for the layout phase
+ temp_frag.column = mark_newline ? INT_MAX : 0;
+
+ if (http_pos == 0) {
+ // Discard color stuff from the source frag
+ temp_frag.text = EnrichedString(temp_frag.text.getString());
+ temp_frag.text.setDefaultColor(m_cache_chat_weblink_color);
+ // Set weblink in the frag meta
+ temp_frag.weblink = wide_to_utf8(temp_frag.text.getString());
+ } else {
+ temp_frag.weblink.clear();
+ }
next_frags.push_back(temp_frag);
in_pos += frag_length;
- text_processing = true;
+ remaining_in_output -= std::min(frag_length, remaining_in_output);
}
}
// End the last line
- if (num_added == 0 || !next_line.fragments.empty())
- {
+ if (num_added == 0 || !next_line.fragments.empty()) {
destination.push_back(next_line);
num_added++;
}
diff --git a/src/chat.h b/src/chat.h
index 0b98e4d3c..fc080f64b 100644
--- a/src/chat.h
+++ b/src/chat.h
@@ -57,6 +57,8 @@ struct ChatFormattedFragment
EnrichedString text;
// starting column
u32 column;
+ // web link is empty for most frags
+ std::string weblink;
// formatting
//u8 bold:1;
};
@@ -108,8 +110,13 @@ public:
void scrollAbsolute(s32 scroll);
// Scroll to bottom of buffer (newest)
void scrollBottom();
- // Scroll to top of buffer (oldest)
- void scrollTop();
+
+ // Functions for keeping track of whether the lines were modified by any
+ // preceding operations
+ // If they were not changed, getLineCount() and getLine() output the same as
+ // before
+ bool getLinesModified() const { return m_lines_modified; }
+ void resetLinesModified() { m_lines_modified = false; }
// Format a chat line for the given number of columns.
// Appends the formatted lines to the destination array and
@@ -118,6 +125,7 @@ public:
std::vector<ChatFormattedLine>& destination) const;
void resize(u32 scrollback);
+
protected:
s32 getTopScrollPos() const;
s32 getBottomScrollPos() const;
@@ -138,6 +146,16 @@ private:
std::vector<ChatFormattedLine> m_formatted;
// Empty formatted line, for error returns
ChatFormattedLine m_empty_formatted_line;
+
+ // Enable clickable chat weblinks
+ bool m_cache_clickable_chat_weblinks;
+ // Color of clickable chat weblinks
+ irr::video::SColor m_cache_chat_weblink_color;
+
+ // Whether the lines were modified since last markLinesUnchanged()
+ // Is always set to true when m_unformatted is modified, because that's what
+ // determines the output of getLineCount() and getLine()
+ bool m_lines_modified = true;
};
class ChatPrompt
diff --git a/src/client/CMakeLists.txt b/src/client/CMakeLists.txt
index 140814911..8d058852a 100644
--- a/src/client/CMakeLists.txt
+++ b/src/client/CMakeLists.txt
@@ -58,5 +58,9 @@ set(client_SRCS
${CMAKE_CURRENT_SOURCE_DIR}/sky.cpp
${CMAKE_CURRENT_SOURCE_DIR}/tile.cpp
${CMAKE_CURRENT_SOURCE_DIR}/wieldmesh.cpp
+ ${CMAKE_CURRENT_SOURCE_DIR}/shadows/dynamicshadows.cpp
+ ${CMAKE_CURRENT_SOURCE_DIR}/shadows/dynamicshadowsrender.cpp
+ ${CMAKE_CURRENT_SOURCE_DIR}/shadows/shadowsshadercallbacks.cpp
+ ${CMAKE_CURRENT_SOURCE_DIR}/shadows/shadowsScreenQuad.cpp
PARENT_SCOPE
)
diff --git a/src/client/camera.cpp b/src/client/camera.cpp
index 350b685e1..d1f19adb3 100644
--- a/src/client/camera.cpp
+++ b/src/client/camera.cpp
@@ -26,6 +26,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "player.h"
#include <cmath>
#include "client/renderingengine.h"
+#include "client/content_cao.h"
#include "settings.h"
#include "wieldmesh.h"
#include "noise.h" // easeCurve
@@ -36,6 +37,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "constants.h"
#include "fontengine.h"
#include "script/scripting_client.h"
+#include "gettext.h"
#define CAMERA_OFFSET_STEP 200
#define WIELDMESH_OFFSET_X 55.0f
@@ -43,11 +45,11 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#define WIELDMESH_AMPLITUDE_X 7.0f
#define WIELDMESH_AMPLITUDE_Y 10.0f
-Camera::Camera(MapDrawControl &draw_control, Client *client):
+Camera::Camera(MapDrawControl &draw_control, Client *client, RenderingEngine *rendering_engine):
m_draw_control(draw_control),
m_client(client)
{
- scene::ISceneManager *smgr = RenderingEngine::get_scene_manager();
+ auto smgr = rendering_engine->get_scene_manager();
// note: making the camera node a child of the player node
// would lead to unexpected behaviour, so we don't do that.
m_playernode = smgr->addEmptySceneNode(smgr->getRootSceneNode());
@@ -132,28 +134,6 @@ void Camera::notifyFovChange()
}
}
-bool Camera::successfullyCreated(std::string &error_message)
-{
- if (!m_playernode) {
- error_message = "Failed to create the player scene node";
- } else if (!m_headnode) {
- error_message = "Failed to create the head scene node";
- } else if (!m_cameranode) {
- error_message = "Failed to create the camera scene node";
- } else if (!m_wieldmgr) {
- error_message = "Failed to create the wielded item scene manager";
- } else if (!m_wieldnode) {
- error_message = "Failed to create the wielded item scene node";
- } else {
- error_message.clear();
- }
-
- if (m_client->modsLoaded())
- m_client->getScript()->on_camera_ready(this);
-
- return error_message.empty();
-}
-
// Returns the fractional part of x
inline f32 my_modf(f32 x)
{
@@ -186,9 +166,7 @@ void Camera::step(f32 dtime)
m_view_bobbing_anim -= offset;
} else if (m_view_bobbing_anim > 0.75) {
m_view_bobbing_anim += offset;
- }
-
- if (m_view_bobbing_anim < 0.5) {
+ } else if (m_view_bobbing_anim < 0.5) {
m_view_bobbing_anim += offset;
if (m_view_bobbing_anim > 0.5)
m_view_bobbing_anim = 0.5;
@@ -330,7 +308,7 @@ void Camera::addArmInertia(f32 player_yaw)
}
}
-void Camera::update(LocalPlayer* player, f32 frametime, f32 busytime, f32 tool_reload_ratio)
+void Camera::update(LocalPlayer* player, f32 frametime, f32 tool_reload_ratio)
{
// Get player position
// Smooth the movement when walking up stairs
@@ -343,13 +321,16 @@ void Camera::update(LocalPlayer* player, f32 frametime, f32 busytime, f32 tool_r
if (player->getParent())
player_position = player->getParent()->getPosition();
- // Smooth the camera movement when the player instantly moves upward due to stepheight.
- // To smooth the 'not touching_ground' stepheight, smoothing is necessary when jumping
- // or swimming (for when moving from liquid to land).
- // Disable smoothing if climbing or flying, to avoid upwards offset of player model
- // when seen in 3rd person view.
- bool flying = g_settings->getBool("free_move") && m_client->checkLocalPrivilege("fly");
- if (player_position.Y > old_player_position.Y && !player->is_climbing && !flying) {
+ // Smooth the camera movement after the player instantly moves upward due to stepheight.
+ // The smoothing usually continues until the camera position reaches the player position.
+ float player_stepheight = player->getCAO() ? player->getCAO()->getStepHeight() : HUGE_VALF;
+ float upward_movement = player_position.Y - old_player_position.Y;
+ if (upward_movement < 0.01f || upward_movement > player_stepheight) {
+ m_stepheight_smooth_active = false;
+ } else if (player->touching_ground) {
+ m_stepheight_smooth_active = true;
+ }
+ if (m_stepheight_smooth_active) {
f32 oldy = old_player_position.Y;
f32 newy = player_position.Y;
f32 t = std::exp(-23 * frametime);
@@ -378,7 +359,8 @@ void Camera::update(LocalPlayer* player, f32 frametime, f32 busytime, f32 tool_r
// Smoothen and invert the above
fall_bobbing = sin(fall_bobbing * 0.5 * M_PI) * -1;
// Amplify according to the intensity of the impact
- fall_bobbing *= (1 - rangelim(50 / player->camera_impact, 0, 1)) * 5;
+ if (player->camera_impact > 0.0f)
+ fall_bobbing *= (1 - rangelim(50 / player->camera_impact, 0, 1)) * 5;
fall_bobbing *= m_cache_fall_bobbing_amount;
}
@@ -409,41 +391,17 @@ void Camera::update(LocalPlayer* player, f32 frametime, f32 busytime, f32 tool_r
f32 bobfrac = my_modf(m_view_bobbing_anim * 2);
f32 bobdir = (m_view_bobbing_anim < 0.5) ? 1.0 : -1.0;
- #if 1
f32 bobknob = 1.2;
f32 bobtmp = sin(pow(bobfrac, bobknob) * M_PI);
- //f32 bobtmp2 = cos(pow(bobfrac, bobknob) * M_PI);
v3f bobvec = v3f(
0.3 * bobdir * sin(bobfrac * M_PI),
-0.28 * bobtmp * bobtmp,
0.);
- //rel_cam_pos += 0.2 * bobvec;
- //rel_cam_target += 0.03 * bobvec;
- //rel_cam_up.rotateXYBy(0.02 * bobdir * bobtmp * M_PI);
- float f = 1.0;
- f *= m_cache_view_bobbing_amount;
- rel_cam_pos += bobvec * f;
- //rel_cam_target += 0.995 * bobvec * f;
- rel_cam_target += bobvec * f;
- rel_cam_target.Z -= 0.005 * bobvec.Z * f;
- //rel_cam_target.X -= 0.005 * bobvec.X * f;
- //rel_cam_target.Y -= 0.005 * bobvec.Y * f;
- rel_cam_up.rotateXYBy(-0.03 * bobdir * bobtmp * M_PI * f);
- #else
- f32 angle_deg = 1 * bobdir * sin(bobfrac * M_PI);
- f32 angle_rad = angle_deg * M_PI / 180;
- f32 r = 0.05;
- v3f off = v3f(
- r * sin(angle_rad),
- r * (cos(angle_rad) - 1),
- 0);
- rel_cam_pos += off;
- //rel_cam_target += off;
- rel_cam_up.rotateXYBy(angle_deg);
- #endif
-
+ rel_cam_pos += bobvec * m_cache_view_bobbing_amount;
+ rel_cam_target += bobvec * m_cache_view_bobbing_amount;
+ rel_cam_up.rotateXYBy(-0.03 * bobdir * bobtmp * M_PI * m_cache_view_bobbing_amount);
}
// Compute absolute camera position and target
@@ -541,7 +499,7 @@ void Camera::update(LocalPlayer* player, f32 frametime, f32 busytime, f32 tool_r
m_curr_fov_degrees = rangelim(m_curr_fov_degrees, 1.0f, 160.0f);
// FOV and aspect ratio
- const v2u32 &window_size = RenderingEngine::get_instance()->getWindowSize();
+ const v2u32 &window_size = RenderingEngine::getWindowSize();
m_aspect = (f32) window_size.X / (f32) window_size.Y;
m_fov_y = m_curr_fov_degrees * M_PI / 180.0;
// Increase vertical FOV on lower aspect ratios (<16:10)
@@ -612,6 +570,8 @@ void Camera::update(LocalPlayer* player, f32 frametime, f32 busytime, f32 tool_r
const bool walking = movement_XZ && player->touching_ground;
const bool swimming = (movement_XZ || player->swimming_vertical) && player->in_liquid;
const bool climbing = movement_Y && player->is_climbing;
+ const bool flying = g_settings->getBool("free_move")
+ && m_client->checkLocalPrivilege("fly");
if ((walking || swimming || climbing) && !flying) {
// Start animation
m_view_bobbing_state = 1;
@@ -664,7 +624,7 @@ void Camera::wield(const ItemStack &item)
void Camera::drawWieldedTool(irr::core::matrix4* translation)
{
// Clear Z buffer so that the wielded tool stays in front of world geometry
- m_wieldmgr->getVideoDriver()->clearZBuffer();
+ m_wieldmgr->getVideoDriver()->clearBuffers(video::ECBF_DEPTH);
// Draw the wielded node (in a separate scene manager)
scene::ICameraSceneNode* cam = m_wieldmgr->getActiveCamera();
diff --git a/src/client/camera.h b/src/client/camera.h
index 6fd8d9aa7..403d6024c 100644
--- a/src/client/camera.h
+++ b/src/client/camera.h
@@ -30,6 +30,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
class LocalPlayer;
struct MapDrawControl;
class Client;
+class RenderingEngine;
class WieldMeshSceneNode;
struct Nametag
@@ -78,7 +79,7 @@ enum CameraMode {CAMERA_MODE_FIRST, CAMERA_MODE_THIRD, CAMERA_MODE_THIRD_FRONT};
class Camera
{
public:
- Camera(MapDrawControl &draw_control, Client *client);
+ Camera(MapDrawControl &draw_control, Client *client, RenderingEngine *rendering_engine);
~Camera();
// Get camera scene node.
@@ -135,16 +136,11 @@ public:
// Notify about new server-sent FOV and initialize smooth FOV transition
void notifyFovChange();
- // Checks if the constructor was able to create the scene nodes
- bool successfullyCreated(std::string &error_message);
-
// Step the camera: updates the viewing range and view bobbing.
void step(f32 dtime);
// Update the camera from the local player's position.
- // busytime is used to adjust the viewing range.
- void update(LocalPlayer* player, f32 frametime, f32 busytime,
- f32 tool_reload_ratio);
+ void update(LocalPlayer* player, f32 frametime, f32 tool_reload_ratio);
// Update render distance
void updateViewingRange();
@@ -217,6 +213,8 @@ private:
// Camera offset
v3s16 m_camera_offset;
+ bool m_stepheight_smooth_active = false;
+
// Server-sent FOV variables
bool m_server_sent_fov = false;
f32 m_curr_fov_degrees, m_old_fov_degrees, m_target_fov_degrees;
diff --git a/src/client/client.cpp b/src/client/client.cpp
index ef4a3cdfc..935a82653 100644
--- a/src/client/client.cpp
+++ b/src/client/client.cpp
@@ -50,6 +50,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "clientmap.h"
#include "clientmedia.h"
#include "version.h"
+#include "database/database-files.h"
#include "database/database-sqlite3.h"
#include "serialization.h"
#include "guiscalingfilter.h"
@@ -97,6 +98,7 @@ Client::Client(
NodeDefManager *nodedef,
ISoundManager *sound,
MtEventManager *event,
+ RenderingEngine *rendering_engine,
bool ipv6,
GameUI *game_ui
):
@@ -106,9 +108,10 @@ Client::Client(
m_nodedef(nodedef),
m_sound(sound),
m_event(event),
+ m_rendering_engine(rendering_engine),
m_mesh_update_thread(this),
m_env(
- new ClientMap(this, control, 666),
+ new ClientMap(this, rendering_engine, control, 666),
tsrc, this
),
m_particle_manager(&m_env),
@@ -126,6 +129,11 @@ Client::Client(
// Add local player
m_env.setLocalPlayer(new LocalPlayer(this, playername));
+ // Make the mod storage database and begin the save for later
+ m_mod_storage_database =
+ new ModMetadataDatabaseSQLite3(porting::path_user + DIR_DELIM + "client");
+ m_mod_storage_database->beginSave();
+
if (g_settings->getBool("enable_minimap")) {
m_minimap = new Minimap(this);
}
@@ -133,6 +141,33 @@ Client::Client(
m_cache_save_interval = g_settings->getU16("server_map_save_interval");
}
+void Client::migrateModStorage()
+{
+ std::string mod_storage_dir = porting::path_user + DIR_DELIM + "client";
+ std::string old_mod_storage = mod_storage_dir + DIR_DELIM + "mod_storage";
+ if (fs::IsDir(old_mod_storage)) {
+ infostream << "Migrating client mod storage to SQLite3 database" << std::endl;
+ {
+ ModMetadataDatabaseFiles files_db(mod_storage_dir);
+ std::vector<std::string> mod_list;
+ files_db.listMods(&mod_list);
+ for (const std::string &modname : mod_list) {
+ infostream << "Migrating client mod storage for mod " << modname << std::endl;
+ StringMap meta;
+ files_db.getModEntries(modname, &meta);
+ for (const auto &pair : meta) {
+ m_mod_storage_database->setModEntry(modname, pair.first, pair.second);
+ }
+ }
+ }
+ if (!fs::Rename(old_mod_storage, old_mod_storage + ".bak")) {
+ // Execution cannot move forward if the migration does not complete.
+ throw BaseException("Could not finish migrating client mod storage");
+ }
+ infostream << "Finished migration of client mod storage" << std::endl;
+ }
+}
+
void Client::loadMods()
{
// Don't load mods twice.
@@ -175,11 +210,7 @@ void Client::loadMods()
// Load "mod" scripts
for (const ModSpec &mod : m_mods) {
- if (!string_allowed(mod.name, MODNAME_ALLOWED_CHARS)) {
- throw ModError("Error loading mod \"" + mod.name +
- "\": Mod name does not follow naming conventions: "
- "Only characters [a-z0-9_] are allowed.");
- }
+ mod.checkAndLog();
scanModIntoMemory(mod.name, mod.path);
}
@@ -208,6 +239,9 @@ void Client::scanModSubfolder(const std::string &mod_name, const std::string &mo
std::string full_path = mod_path + DIR_DELIM + mod_subpath;
std::vector<fs::DirListNode> mod = fs::GetDirListing(full_path);
for (const fs::DirListNode &j : mod) {
+ if (j.name[0] == '.')
+ continue;
+
if (j.dir) {
scanModSubfolder(mod_name, mod_path, mod_subpath + j.name + DIR_DELIM);
continue;
@@ -298,17 +332,17 @@ Client::~Client()
}
// cleanup 3d model meshes on client shutdown
- while (RenderingEngine::get_mesh_cache()->getMeshCount() != 0) {
- scene::IAnimatedMesh *mesh = RenderingEngine::get_mesh_cache()->getMeshByIndex(0);
-
- if (mesh)
- RenderingEngine::get_mesh_cache()->removeMesh(mesh);
- }
+ m_rendering_engine->cleanupMeshCache();
delete m_minimap;
m_minimap = nullptr;
delete m_media_downloader;
+
+ // Write the changes and delete
+ if (m_mod_storage_database)
+ m_mod_storage_database->endSave();
+ delete m_mod_storage_database;
}
void Client::connect(Address address, bool is_local_server)
@@ -559,6 +593,29 @@ void Client::step(float dtime)
m_media_downloader = NULL;
}
}
+ {
+ // Acknowledge dynamic media downloads to server
+ std::vector<u32> done;
+ for (auto it = m_pending_media_downloads.begin();
+ it != m_pending_media_downloads.end();) {
+ assert(it->second->isStarted());
+ it->second->step(this);
+ if (it->second->isDone()) {
+ done.emplace_back(it->first);
+
+ it = m_pending_media_downloads.erase(it);
+ } else {
+ it++;
+ }
+
+ if (done.size() == 255) { // maximum in one packet
+ sendHaveMedia(done);
+ done.clear();
+ }
+ }
+ if (!done.empty())
+ sendHaveMedia(done);
+ }
/*
If the server didn't update the inventory in a while, revert
@@ -622,19 +679,12 @@ void Client::step(float dtime)
}
}
+ // Write changes to the mod storage
m_mod_storage_save_timer -= dtime;
if (m_mod_storage_save_timer <= 0.0f) {
m_mod_storage_save_timer = g_settings->getFloat("server_map_save_interval");
- int n = 0;
- for (std::unordered_map<std::string, ModMetadata *>::const_iterator
- it = m_mod_storages.begin(); it != m_mod_storages.end(); ++it) {
- if (it->second->isModified()) {
- it->second->save(getModStoragePath());
- n++;
- }
- }
- if (n > 0)
- infostream << "Saved " << n << " modified mod storages." << std::endl;
+ m_mod_storage_database->endSave();
+ m_mod_storage_database->beginSave();
}
// Write server map
@@ -660,15 +710,11 @@ bool Client::loadMedia(const std::string &data, const std::string &filename,
TRACESTREAM(<< "Client: Attempting to load image "
<< "file \"" << filename << "\"" << std::endl);
- io::IFileSystem *irrfs = RenderingEngine::get_filesystem();
- video::IVideoDriver *vdrv = RenderingEngine::get_video_driver();
-
- // Silly irrlicht's const-incorrectness
- Buffer<char> data_rw(data.c_str(), data.size());
+ io::IFileSystem *irrfs = m_rendering_engine->get_filesystem();
+ video::IVideoDriver *vdrv = m_rendering_engine->get_video_driver();
- // Create an irrlicht memory file
io::IReadFile *rfile = irrfs->createMemoryReadFile(
- *data_rw, data_rw.getSize(), "_tempreadfile");
+ data.c_str(), data.size(), "_tempreadfile");
FATAL_ERROR_IF(!rfile, "Could not create irrlicht memory file.");
@@ -778,7 +824,8 @@ void Client::request_media(const std::vector<std::string> &file_requests)
Send(&pkt);
infostream << "Client: Sending media request list to server ("
- << file_requests.size() << " files. packet size)" << std::endl;
+ << file_requests.size() << " files, packet size "
+ << pkt.getSize() << ")" << std::endl;
}
void Client::initLocalMapSaving(const Address &address,
@@ -861,7 +908,7 @@ void Client::ProcessData(NetworkPacket *pkt)
*/
if(sender_peer_id != PEER_ID_SERVER) {
infostream << "Client::ProcessData(): Discarding data not "
- "coming from server: peer_id=" << sender_peer_id
+ "coming from server: peer_id=" << sender_peer_id << " command=" << pkt->getCommand()
<< std::endl;
return;
}
@@ -912,7 +959,7 @@ void writePlayerPos(LocalPlayer *myplayer, ClientMap *clientMap, NetworkPacket *
v3f sf = myplayer->getSpeed() * 100;
s32 pitch = myplayer->getPitch() * 100;
s32 yaw = myplayer->getYaw() * 100;
- u32 keyPressed = myplayer->keyPressed;
+ u32 keyPressed = myplayer->control.getKeysPressed();
// scaled by 80, so that pi can fit into a u8
u8 fov = clientMap->getCameraFov() * 80;
u8 wanted_range = MYMIN(255,
@@ -1268,22 +1315,24 @@ void Client::sendPlayerPos()
if (!player)
return;
- ClientMap &map = m_env.getClientMap();
- u8 camera_fov = map.getCameraFov();
- u8 wanted_range = map.getControl().wanted_range;
-
// Save bandwidth by only updating position when
// player is not dead and something changed
if (m_activeobjects_received && player->isDead())
return;
+ ClientMap &map = m_env.getClientMap();
+ u8 camera_fov = map.getCameraFov();
+ u8 wanted_range = map.getControl().wanted_range;
+
+ u32 keyPressed = player->control.getKeysPressed();
+
if (
player->last_position == player->getPosition() &&
player->last_speed == player->getSpeed() &&
player->last_pitch == player->getPitch() &&
player->last_yaw == player->getYaw() &&
- player->last_keyPressed == player->keyPressed &&
+ player->last_keyPressed == keyPressed &&
player->last_camera_fov == camera_fov &&
player->last_wanted_range == wanted_range)
return;
@@ -1292,7 +1341,7 @@ void Client::sendPlayerPos()
player->last_speed = player->getSpeed();
player->last_pitch = player->getPitch();
player->last_yaw = player->getYaw();
- player->last_keyPressed = player->keyPressed;
+ player->last_keyPressed = keyPressed;
player->last_camera_fov = camera_fov;
player->last_wanted_range = wanted_range;
@@ -1303,6 +1352,19 @@ void Client::sendPlayerPos()
Send(&pkt);
}
+void Client::sendHaveMedia(const std::vector<u32> &tokens)
+{
+ NetworkPacket pkt(TOSERVER_HAVE_MEDIA, 1 + tokens.size() * 4);
+
+ sanity_check(tokens.size() < 256);
+
+ pkt << static_cast<u8>(tokens.size());
+ for (u32 token : tokens)
+ pkt << token;
+
+ Send(&pkt);
+}
+
void Client::removeNode(v3s16 p)
{
std::map<v3s16, MapBlock*> modified_blocks;
@@ -1416,6 +1478,11 @@ bool Client::updateWieldedItem()
return true;
}
+scene::ISceneManager* Client::getSceneManager()
+{
+ return m_rendering_engine->get_scene_manager();
+}
+
Inventory* Client::getInventory(const InventoryLocation &loc)
{
switch(loc.type){
@@ -1577,20 +1644,7 @@ void Client::addUpdateMeshTask(v3s16 p, bool ack_to_server, bool urgent)
void Client::addUpdateMeshTaskWithEdge(v3s16 blockpos, bool ack_to_server, bool urgent)
{
- try{
- addUpdateMeshTask(blockpos, ack_to_server, urgent);
- }
- catch(InvalidPositionException &e){}
-
- // Leading edge
- for (int i=0;i<6;i++)
- {
- try{
- v3s16 p = blockpos + g_6dirs[i];
- addUpdateMeshTask(p, false, urgent);
- }
- catch(InvalidPositionException &e){}
- }
+ m_mesh_update_thread.updateBlock(&m_env.getMap(), blockpos, ack_to_server, urgent, true);
}
void Client::addUpdateMeshTaskForNode(v3s16 nodepos, bool ack_to_server, bool urgent)
@@ -1602,38 +1656,16 @@ void Client::addUpdateMeshTaskForNode(v3s16 nodepos, bool ack_to_server, bool ur
<<std::endl;
}
- v3s16 blockpos = getNodeBlockPos(nodepos);
+ v3s16 blockpos = getNodeBlockPos(nodepos);
v3s16 blockpos_relative = blockpos * MAP_BLOCKSIZE;
-
- try{
- addUpdateMeshTask(blockpos, ack_to_server, urgent);
- }
- catch(InvalidPositionException &e) {}
-
+ m_mesh_update_thread.updateBlock(&m_env.getMap(), blockpos, ack_to_server, urgent, false);
// Leading edge
- if(nodepos.X == blockpos_relative.X){
- try{
- v3s16 p = blockpos + v3s16(-1,0,0);
- addUpdateMeshTask(p, false, urgent);
- }
- catch(InvalidPositionException &e){}
- }
-
- if(nodepos.Y == blockpos_relative.Y){
- try{
- v3s16 p = blockpos + v3s16(0,-1,0);
- addUpdateMeshTask(p, false, urgent);
- }
- catch(InvalidPositionException &e){}
- }
-
- if(nodepos.Z == blockpos_relative.Z){
- try{
- v3s16 p = blockpos + v3s16(0,0,-1);
- addUpdateMeshTask(p, false, urgent);
- }
- catch(InvalidPositionException &e){}
- }
+ if (nodepos.X == blockpos_relative.X)
+ addUpdateMeshTask(blockpos + v3s16(-1, 0, 0), false, urgent);
+ if (nodepos.Y == blockpos_relative.Y)
+ addUpdateMeshTask(blockpos + v3s16(0, -1, 0), false, urgent);
+ if (nodepos.Z == blockpos_relative.Z)
+ addUpdateMeshTask(blockpos + v3s16(0, 0, -1), false, urgent);
}
ClientEvent *Client::getClientEvent()
@@ -1659,15 +1691,15 @@ float Client::mediaReceiveProgress()
return 1.0; // downloader only exists when not yet done
}
-typedef struct TextureUpdateArgs {
+struct TextureUpdateArgs {
gui::IGUIEnvironment *guienv;
u64 last_time_ms;
u16 last_percent;
const wchar_t* text_base;
ITextureSource *tsrc;
-} TextureUpdateArgs;
+};
-void texture_update_progress(void *args, u32 progress, u32 max_progress)
+void Client::showUpdateProgressTexture(void *args, u32 progress, u32 max_progress)
{
TextureUpdateArgs* targs = (TextureUpdateArgs*) args;
u16 cur_percent = ceil(progress / (double) max_progress * 100.);
@@ -1684,9 +1716,9 @@ void texture_update_progress(void *args, u32 progress, u32 max_progress)
if (do_draw) {
targs->last_time_ms = time_ms;
- std::basic_stringstream<wchar_t> strm;
- strm << targs->text_base << " " << targs->last_percent << "%...";
- RenderingEngine::draw_load_screen(strm.str(), targs->guienv, targs->tsrc, 0,
+ std::wostringstream strm;
+ strm << targs->text_base << L" " << targs->last_percent << L"%...";
+ m_rendering_engine->draw_load_screen(strm.str(), targs->guienv, targs->tsrc, 0,
72 + (u16) ((18. / 100.) * (double) targs->last_percent), true);
}
}
@@ -1707,21 +1739,21 @@ void Client::afterContentReceived()
// Rebuild inherited images and recreate textures
infostream<<"- Rebuilding images and textures"<<std::endl;
- RenderingEngine::draw_load_screen(text, guienv, m_tsrc, 0, 70);
+ m_rendering_engine->draw_load_screen(text, guienv, m_tsrc, 0, 70);
m_tsrc->rebuildImagesAndTextures();
delete[] text;
// Rebuild shaders
infostream<<"- Rebuilding shaders"<<std::endl;
text = wgettext("Rebuilding shaders...");
- RenderingEngine::draw_load_screen(text, guienv, m_tsrc, 0, 71);
+ m_rendering_engine->draw_load_screen(text, guienv, m_tsrc, 0, 71);
m_shsrc->rebuildShaders();
delete[] text;
// Update node aliases
infostream<<"- Updating node aliases"<<std::endl;
text = wgettext("Initializing nodes...");
- RenderingEngine::draw_load_screen(text, guienv, m_tsrc, 0, 72);
+ m_rendering_engine->draw_load_screen(text, guienv, m_tsrc, 0, 72);
m_nodedef->updateAliases(m_itemdef);
for (const auto &path : getTextureDirs()) {
TextureOverrideSource override_source(path + DIR_DELIM + "override.txt");
@@ -1738,9 +1770,9 @@ void Client::afterContentReceived()
tu_args.guienv = guienv;
tu_args.last_time_ms = porting::getTimeMs();
tu_args.last_percent = 0;
- tu_args.text_base = wgettext("Initializing nodes");
+ tu_args.text_base = wgettext("Initializing nodes");
tu_args.tsrc = m_tsrc;
- m_nodedef->updateTextures(this, texture_update_progress, &tu_args);
+ m_nodedef->updateTextures(this, &tu_args);
delete[] tu_args.text_base;
// Start mesh update thread after setting up content definitions
@@ -1754,7 +1786,7 @@ void Client::afterContentReceived()
m_script->on_client_ready(m_env.getLocalPlayer());
text = wgettext("Done!");
- RenderingEngine::draw_load_screen(text, guienv, m_tsrc, 0, 100);
+ m_rendering_engine->draw_load_screen(text, guienv, m_tsrc, 0, 100);
infostream<<"Client::afterContentReceived() done"<<std::endl;
delete[] text;
}
@@ -1772,7 +1804,7 @@ float Client::getCurRate()
void Client::makeScreenshot()
{
- irr::video::IVideoDriver *driver = RenderingEngine::get_video_driver();
+ irr::video::IVideoDriver *driver = m_rendering_engine->get_video_driver();
irr::video::IImage* const raw_image = driver->createScreenShot();
if (!raw_image)
@@ -1914,16 +1946,17 @@ scene::IAnimatedMesh* Client::getMesh(const std::string &filename, bool cache)
// Create the mesh, remove it from cache and return it
// This allows unique vertex colors and other properties for each instance
- Buffer<char> data_rw(data.c_str(), data.size()); // Const-incorrect Irrlicht
- io::IReadFile *rfile = RenderingEngine::get_filesystem()->createMemoryReadFile(
- *data_rw, data_rw.getSize(), filename.c_str());
+ io::IReadFile *rfile = m_rendering_engine->get_filesystem()->createMemoryReadFile(
+ data.c_str(), data.size(), filename.c_str());
FATAL_ERROR_IF(!rfile, "Could not create/open RAM file");
- scene::IAnimatedMesh *mesh = RenderingEngine::get_scene_manager()->getMesh(rfile);
+ scene::IAnimatedMesh *mesh = m_rendering_engine->get_scene_manager()->getMesh(rfile);
rfile->drop();
+ if (!mesh)
+ return nullptr;
mesh->grab();
if (!cache)
- RenderingEngine::get_mesh_cache()->removeMesh(mesh);
+ m_rendering_engine->removeMesh(mesh);
return mesh;
}
@@ -1960,16 +1993,8 @@ void Client::unregisterModStorage(const std::string &name)
{
std::unordered_map<std::string, ModMetadata *>::const_iterator it =
m_mod_storages.find(name);
- if (it != m_mod_storages.end()) {
- // Save unconditionaly on unregistration
- it->second->save(getModStoragePath());
+ if (it != m_mod_storages.end())
m_mod_storages.erase(name);
- }
-}
-
-std::string Client::getModStoragePath() const
-{
- return porting::path_user + DIR_DELIM + "client" + DIR_DELIM + "mod_storage";
}
/*
diff --git a/src/client/client.h b/src/client/client.h
index 2dba1506e..84c85471d 100644
--- a/src/client/client.h
+++ b/src/client/client.h
@@ -45,6 +45,7 @@ struct ClientEvent;
struct MeshMakeData;
struct ChatMessage;
class MapBlockMesh;
+class RenderingEngine;
class IWritableTextureSource;
class IWritableShaderSource;
class IWritableItemDefManager;
@@ -52,6 +53,7 @@ class ISoundManager;
class NodeDefManager;
//class IWritableCraftDefManager;
class ClientMediaDownloader;
+class SingleMediaDownloader;
struct MapDrawControl;
class ModChannelMgr;
class MtEventManager;
@@ -123,6 +125,7 @@ public:
NodeDefManager *nodedef,
ISoundManager *sound,
MtEventManager *event,
+ RenderingEngine *rendering_engine,
bool ipv6,
GameUI *game_ui
);
@@ -243,6 +246,7 @@ public:
void sendDamage(u16 damage);
void sendRespawn();
void sendReady();
+ void sendHaveMedia(const std::vector<u32> &tokens);
ClientEnvironment& getEnv() { return m_env; }
ITextureSource *tsrc() { return getTextureSource(); }
@@ -321,18 +325,22 @@ public:
m_access_denied = true;
m_access_denied_reason = reason;
}
+ inline void setFatalError(const LuaError &e)
+ {
+ setFatalError(std::string("Lua: ") + e.what());
+ }
// Renaming accessDeniedReason to better name could be good as it's used to
// disconnect client when CSM failed.
const std::string &accessDeniedReason() const { return m_access_denied_reason; }
- const bool itemdefReceived() const
+ bool itemdefReceived() const
{ return m_itemdef_received; }
- const bool nodedefReceived() const
+ bool nodedefReceived() const
{ return m_nodedef_received; }
- const bool mediaReceived() const
+ bool mediaReceived() const
{ return !m_media_downloader; }
- const bool activeObjectsReceived() const
+ bool activeObjectsReceived() const
{ return m_activeobjects_received; }
u16 getProtoVersion()
@@ -345,6 +353,7 @@ public:
float mediaReceiveProgress();
void afterContentReceived();
+ void showUpdateProgressTexture(void *args, u32 progress, u32 max_progress);
float getRTT();
float getCurRate();
@@ -353,6 +362,7 @@ public:
void setCamera(Camera* camera) { m_camera = camera; }
Camera* getCamera () { return m_camera; }
+ scene::ISceneManager *getSceneManager();
bool shouldShowMinimap() const;
@@ -370,15 +380,19 @@ public:
{ return checkPrivilege(priv); }
virtual scene::IAnimatedMesh* getMesh(const std::string &filename, bool cache = false);
const std::string* getModFile(std::string filename);
+ ModMetadataDatabase *getModStorageDatabase() override { return m_mod_storage_database; }
- std::string getModStoragePath() const override;
bool registerModStorage(ModMetadata *meta) override;
void unregisterModStorage(const std::string &name) override;
+ // Migrates away old files-based mod storage if necessary
+ void migrateModStorage();
+
// The following set of functions is used by ClientMediaDownloader
// Insert a media file appropriately into the appropriate manager
bool loadMedia(const std::string &data, const std::string &filename,
bool from_media_push = false);
+
// Send a request for conventional media transfer
void request_media(const std::vector<std::string> &file_requests);
@@ -469,6 +483,7 @@ private:
NodeDefManager *m_nodedef;
ISoundManager *m_sound;
MtEventManager *m_event;
+ RenderingEngine *m_rendering_engine;
MeshUpdateThread m_mesh_update_thread;
@@ -530,9 +545,13 @@ private:
bool m_activeobjects_received = false;
bool m_mods_loaded = false;
+ std::vector<std::string> m_remote_media_servers;
+ // Media downloader, only exists during init
ClientMediaDownloader *m_media_downloader;
// Set of media filenames pushed by server at runtime
std::unordered_set<std::string> m_media_pushed_files;
+ // Pending downloads of dynamic media (key: token)
+ std::vector<std::pair<u32, std::shared_ptr<SingleMediaDownloader>>> m_pending_media_downloads;
// time_of_day speed approximation for old protocol
bool m_time_of_day_set = false;
@@ -574,6 +593,7 @@ private:
// Client modding
ClientScripting *m_script = nullptr;
std::unordered_map<std::string, ModMetadata *> m_mod_storages;
+ ModMetadataDatabase *m_mod_storage_database = nullptr;
float m_mod_storage_save_timer = 10.0f;
std::vector<ModSpec> m_mods;
StringMap m_mod_vfs;
diff --git a/src/client/clientenvironment.cpp b/src/client/clientenvironment.cpp
index fc7cbe254..448af36c6 100644
--- a/src/client/clientenvironment.cpp
+++ b/src/client/clientenvironment.cpp
@@ -194,32 +194,41 @@ void ClientEnvironment::step(float dtime)
lplayer->applyControl(dtime_part, this);
// Apply physics
- if (!free_move && !is_climbing) {
+ if (!free_move) {
// Gravity
v3f speed = lplayer->getSpeed();
- if (!lplayer->in_liquid)
+ if (!is_climbing && !lplayer->in_liquid)
speed.Y -= lplayer->movement_gravity *
lplayer->physics_override_gravity * dtime_part * 2.0f;
// Liquid floating / sinking
- if (lplayer->in_liquid && !lplayer->swimming_vertical &&
+ if (!is_climbing && lplayer->in_liquid &&
+ !lplayer->swimming_vertical &&
!lplayer->swimming_pitch)
speed.Y -= lplayer->movement_liquid_sink * dtime_part * 2.0f;
- // Liquid resistance
- if (lplayer->in_liquid_stable || lplayer->in_liquid) {
- // How much the node's viscosity blocks movement, ranges
- // between 0 and 1. Should match the scale at which viscosity
+ // Movement resistance
+ if (lplayer->move_resistance > 0) {
+ // How much the node's move_resistance blocks movement, ranges
+ // between 0 and 1. Should match the scale at which liquid_viscosity
// increase affects other liquid attributes.
- static const f32 viscosity_factor = 0.3f;
-
- v3f d_wanted = -speed / lplayer->movement_liquid_fluidity;
+ static const f32 resistance_factor = 0.3f;
+
+ v3f d_wanted;
+ bool in_liquid_stable = lplayer->in_liquid_stable || lplayer->in_liquid;
+ if (in_liquid_stable) {
+ d_wanted = -speed / lplayer->movement_liquid_fluidity;
+ } else {
+ d_wanted = -speed / BS;
+ }
f32 dl = d_wanted.getLength();
- if (dl > lplayer->movement_liquid_fluidity_smooth)
- dl = lplayer->movement_liquid_fluidity_smooth;
+ if (in_liquid_stable) {
+ if (dl > lplayer->movement_liquid_fluidity_smooth)
+ dl = lplayer->movement_liquid_fluidity_smooth;
+ }
- dl *= (lplayer->liquid_viscosity * viscosity_factor) +
- (1 - viscosity_factor);
+ dl *= (lplayer->move_resistance * resistance_factor) +
+ (1 - resistance_factor);
v3f d = d_wanted.normalize() * (dl * dtime_part * 100.0f);
speed += d;
}
@@ -235,7 +244,16 @@ void ClientEnvironment::step(float dtime)
&player_collisions);
}
- bool player_immortal = lplayer->getCAO() && lplayer->getCAO()->isImmortal();
+ bool player_immortal = false;
+ f32 player_fall_factor = 1.0f;
+ GenericCAO *playercao = lplayer->getCAO();
+ if (playercao) {
+ player_immortal = playercao->isImmortal();
+ int addp_p = itemgroup_get(playercao->getGroups(),
+ "fall_damage_add_percent");
+ // convert armor group into an usable fall damage factor
+ player_fall_factor = 1.0f + (float)addp_p / 100.0f;
+ }
for (const CollisionInfo &info : player_collisions) {
v3f speed_diff = info.new_speed - info.old_speed;;
@@ -248,17 +266,20 @@ void ClientEnvironment::step(float dtime)
speed_diff.Z = 0;
f32 pre_factor = 1; // 1 hp per node/s
f32 tolerance = BS*14; // 5 without damage
- f32 post_factor = 1; // 1 hp per node/s
if (info.type == COLLISION_NODE) {
const ContentFeatures &f = m_client->ndef()->
get(m_map->getNode(info.node_p));
- // Determine fall damage multiplier
- int addp = itemgroup_get(f.groups, "fall_damage_add_percent");
- pre_factor = 1.0f + (float)addp / 100.0f;
+ // Determine fall damage modifier
+ int addp_n = itemgroup_get(f.groups, "fall_damage_add_percent");
+ // convert node group to an usable fall damage factor
+ f32 node_fall_factor = 1.0f + (float)addp_n / 100.0f;
+ // combine both player fall damage modifiers
+ pre_factor = node_fall_factor * player_fall_factor;
}
float speed = pre_factor * speed_diff.getLength();
- if (speed > tolerance && !player_immortal) {
- f32 damage_f = (speed - tolerance) / BS * post_factor;
+
+ if (speed > tolerance && !player_immortal && pre_factor > 0.0f) {
+ f32 damage_f = (speed - tolerance) / BS;
u16 damage = (u16)MYMIN(damage_f + 0.5, U16_MAX);
if (damage != 0) {
damageLocalPlayer(damage, true);
@@ -340,7 +361,7 @@ u16 ClientEnvironment::addActiveObject(ClientActiveObject *object)
if (!m_ao_manager.registerObject(object))
return 0;
- object->addToScene(m_texturesource);
+ object->addToScene(m_texturesource, m_client->getSceneManager());
// Update lighting immediately
object->updateLight(getDayNightRatio());
diff --git a/src/client/clientevent.h b/src/client/clientevent.h
index 9bd31efce..17d3aedd6 100644
--- a/src/client/clientevent.h
+++ b/src/client/clientevent.h
@@ -52,6 +52,31 @@ enum ClientEventType : u8
CLIENTEVENT_MAX,
};
+struct ClientEventHudAdd
+{
+ u32 server_id;
+ u8 type;
+ v2f pos, scale;
+ std::string name;
+ std::string text, text2;
+ u32 number, item, dir, style;
+ v2f align, offset;
+ v3f world_pos;
+ v2s32 size;
+ s16 z_index;
+};
+
+struct ClientEventHudChange
+{
+ u32 id;
+ HudElementStat stat;
+ v2f v2fdata;
+ std::string sdata;
+ u32 data;
+ v3f v3fdata;
+ v2s32 v2s32data;
+};
+
struct ClientEvent
{
ClientEventType type;
@@ -93,38 +118,12 @@ struct ClientEvent
{
u32 id;
} delete_particlespawner;
- struct
- {
- u32 server_id;
- u8 type;
- v2f *pos;
- std::string *name;
- v2f *scale;
- std::string *text;
- u32 number;
- u32 item;
- u32 dir;
- v2f *align;
- v2f *offset;
- v3f *world_pos;
- v2s32 *size;
- s16 z_index;
- std::string *text2;
- } hudadd;
+ ClientEventHudAdd *hudadd;
struct
{
u32 id;
} hudrm;
- struct
- {
- u32 id;
- HudElementStat stat;
- v2f *v2fdata;
- std::string *sdata;
- u32 data;
- v3f *v3fdata;
- v2s32 *v2s32data;
- } hudchange;
+ ClientEventHudChange *hudchange;
SkyboxParams *set_sky;
struct
{
diff --git a/src/client/clientlauncher.cpp b/src/client/clientlauncher.cpp
index 2bb0bc385..063154316 100644
--- a/src/client/clientlauncher.cpp
+++ b/src/client/clientlauncher.cpp
@@ -38,9 +38,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#if USE_SOUND
#include "sound_openal.h"
#endif
-#ifdef __ANDROID__
- #include "porting.h"
-#endif
/* mainmenumanager.h
*/
@@ -73,14 +70,14 @@ static void dump_start_data(const GameStartData &data)
ClientLauncher::~ClientLauncher()
{
- delete receiver;
-
delete input;
+ delete receiver;
+
delete g_fontengine;
delete g_gamecallback;
- delete RenderingEngine::get_instance();
+ delete m_rendering_engine;
#if USE_SOUND
g_sound_manager_singleton.reset();
@@ -99,10 +96,6 @@ bool ClientLauncher::run(GameStartData &start_data, const Settings &cmd_args)
init_args(start_data, cmd_args);
- // List video modes if requested
- if (list_video_modes)
- return RenderingEngine::print_video_modes();
-
#if USE_SOUND
if (g_settings->getBool("enable_sound"))
g_sound_manager_singleton = createSoundManagerSingleton();
@@ -120,12 +113,12 @@ bool ClientLauncher::run(GameStartData &start_data, const Settings &cmd_args)
return true;
}
- if (RenderingEngine::get_video_driver() == NULL) {
+ if (m_rendering_engine->get_video_driver() == NULL) {
errorstream << "Could not initialize video driver." << std::endl;
return false;
}
- RenderingEngine::get_instance()->setupTopLevelWindow(PROJECT_NAME_C);
+ m_rendering_engine->setupTopLevelWindow(PROJECT_NAME_C);
/*
This changes the minimum allowed number of vertices in a VBO.
@@ -136,23 +129,23 @@ bool ClientLauncher::run(GameStartData &start_data, const Settings &cmd_args)
// Create game callback for menus
g_gamecallback = new MainGameCallback();
- RenderingEngine::get_instance()->setResizable(true);
+ m_rendering_engine->setResizable(true);
init_input();
- RenderingEngine::get_scene_manager()->getParameters()->
+ m_rendering_engine->get_scene_manager()->getParameters()->
setAttribute(scene::ALLOW_ZWRITE_ON_TRANSPARENT, true);
- guienv = RenderingEngine::get_gui_env();
- skin = RenderingEngine::get_gui_env()->getSkin();
+ guienv = m_rendering_engine->get_gui_env();
+ skin = guienv->getSkin();
skin->setColor(gui::EGDC_BUTTON_TEXT, video::SColor(255, 255, 255, 255));
skin->setColor(gui::EGDC_3D_LIGHT, video::SColor(0, 0, 0, 0));
skin->setColor(gui::EGDC_3D_HIGH_LIGHT, video::SColor(255, 30, 30, 30));
skin->setColor(gui::EGDC_3D_SHADOW, video::SColor(255, 0, 0, 0));
skin->setColor(gui::EGDC_HIGH_LIGHT, video::SColor(255, 70, 120, 50));
skin->setColor(gui::EGDC_HIGH_LIGHT_TEXT, video::SColor(255, 255, 255, 255));
-#ifdef __ANDROID__
- float density = porting::getDisplayDensity();
+#ifdef HAVE_TOUCHSCREENGUI
+ float density = RenderingEngine::getDisplayDensity();
skin->setSize(gui::EGDS_CHECK_BOX_WIDTH, (s32)(17.0f * density));
skin->setSize(gui::EGDS_SCROLLBAR_SIZE, (s32)(14.0f * density));
skin->setSize(gui::EGDS_WINDOW_BUTTON_WIDTH, (s32)(15.0f * density));
@@ -166,7 +159,7 @@ bool ClientLauncher::run(GameStartData &start_data, const Settings &cmd_args)
sprite_path.append("checkbox_16.png");
// Texture dimensions should be a power of 2
gui::IGUISpriteBank *sprites = skin->getSpriteBank();
- video::IVideoDriver *driver = RenderingEngine::get_video_driver();
+ video::IVideoDriver *driver = m_rendering_engine->get_video_driver();
video::ITexture *sprite_texture = driver->getTexture(sprite_path.c_str());
if (sprite_texture) {
s32 sprite_id = sprites->addTextureAsSprite(sprite_texture);
@@ -178,15 +171,13 @@ bool ClientLauncher::run(GameStartData &start_data, const Settings &cmd_args)
g_fontengine = new FontEngine(guienv);
FATAL_ERROR_IF(g_fontengine == NULL, "Font engine creation failed.");
-#if (IRRLICHT_VERSION_MAJOR >= 1 && IRRLICHT_VERSION_MINOR >= 8) || IRRLICHT_VERSION_MAJOR >= 2
// Irrlicht 1.8 input colours
skin->setColor(gui::EGDC_EDITABLE, video::SColor(255, 128, 128, 128));
skin->setColor(gui::EGDC_FOCUSED_EDITABLE, video::SColor(255, 96, 134, 49));
-#endif
// Create the menu clouds
if (!g_menucloudsmgr)
- g_menucloudsmgr = RenderingEngine::get_scene_manager()->createNewSceneManager();
+ g_menucloudsmgr = m_rendering_engine->get_scene_manager()->createNewSceneManager();
if (!g_menuclouds)
g_menuclouds = new Clouds(g_menucloudsmgr, -1, rand());
g_menuclouds->setHeight(100.0f);
@@ -214,11 +205,11 @@ bool ClientLauncher::run(GameStartData &start_data, const Settings &cmd_args)
bool retval = true;
bool *kill = porting::signal_handler_killstatus();
- while (RenderingEngine::run() && !*kill &&
+ while (m_rendering_engine->run() && !*kill &&
!g_gamecallback->shutdown_requested) {
// Set the window caption
const wchar_t *text = wgettext("Main Menu");
- RenderingEngine::get_raw_device()->
+ m_rendering_engine->get_raw_device()->
setWindowCaption((utf8_to_wide(PROJECT_NAME_C) +
L" " + utf8_to_wide(g_version_hash) +
L" [" + text + L"]").c_str());
@@ -226,14 +217,14 @@ bool ClientLauncher::run(GameStartData &start_data, const Settings &cmd_args)
try { // This is used for catching disconnects
- RenderingEngine::get_gui_env()->clear();
+ m_rendering_engine->get_gui_env()->clear();
/*
We need some kind of a root node to be able to add
custom gui elements directly on the screen.
Otherwise they won't be automatically drawn.
*/
- guiroot = RenderingEngine::get_gui_env()->addStaticText(L"",
+ guiroot = m_rendering_engine->get_gui_env()->addStaticText(L"",
core::rect<s32>(0, 0, 10000, 10000));
bool game_has_run = launch_game(error_message, reconnect_requested,
@@ -256,36 +247,29 @@ bool ClientLauncher::run(GameStartData &start_data, const Settings &cmd_args)
}
// Break out of menu-game loop to shut down cleanly
- if (!RenderingEngine::get_raw_device()->run() || *kill) {
+ if (!m_rendering_engine->run() || *kill) {
if (!g_settings_path.empty())
g_settings->updateConfigFile(g_settings_path.c_str());
break;
}
- RenderingEngine::get_video_driver()->setTextureCreationFlag(
+ m_rendering_engine->get_video_driver()->setTextureCreationFlag(
video::ETCF_CREATE_MIP_MAPS, g_settings->getBool("mip_map"));
#ifdef HAVE_TOUCHSCREENGUI
- receiver->m_touchscreengui = new TouchScreenGUI(RenderingEngine::get_raw_device(), receiver);
+ receiver->m_touchscreengui = new TouchScreenGUI(m_rendering_engine->get_raw_device(), receiver);
g_touchscreengui = receiver->m_touchscreengui;
#endif
the_game(
kill,
input,
+ m_rendering_engine,
start_data,
error_message,
chat_backend,
&reconnect_requested
);
- RenderingEngine::get_scene_manager()->clear();
-
-#ifdef HAVE_TOUCHSCREENGUI
- delete g_touchscreengui;
- g_touchscreengui = NULL;
- receiver->m_touchscreengui = NULL;
-#endif
-
} //try
catch (con::PeerNotFoundException &e) {
error_message = gettext("Connection error (timed out?)");
@@ -301,6 +285,14 @@ bool ClientLauncher::run(GameStartData &start_data, const Settings &cmd_args)
}
#endif
+ m_rendering_engine->get_scene_manager()->clear();
+
+#ifdef HAVE_TOUCHSCREENGUI
+ delete g_touchscreengui;
+ g_touchscreengui = NULL;
+ receiver->m_touchscreengui = NULL;
+#endif
+
// If no main menu, show error and exit
if (skip_main_menu) {
if (!error_message.empty()) {
@@ -337,8 +329,6 @@ void ClientLauncher::init_args(GameStartData &start_data, const Settings &cmd_ar
if (cmd_args.exists("name"))
start_data.name = cmd_args.get("name");
- list_video_modes = cmd_args.getFlag("videomodes");
-
random_input = g_settings->getBool("random_input")
|| cmd_args.getFlag("random-input");
}
@@ -346,8 +336,8 @@ void ClientLauncher::init_args(GameStartData &start_data, const Settings &cmd_ar
bool ClientLauncher::init_engine()
{
receiver = new MyEventReceiver();
- new RenderingEngine(receiver);
- return RenderingEngine::get_raw_device() != nullptr;
+ m_rendering_engine = new RenderingEngine(receiver);
+ return m_rendering_engine->get_raw_device() != nullptr;
}
void ClientLauncher::init_input()
@@ -364,7 +354,7 @@ void ClientLauncher::init_input()
// Make sure this is called maximum once per
// irrlicht device, otherwise it will give you
// multiple events for the same joystick.
- if (RenderingEngine::get_raw_device()->activateJoysticks(infos)) {
+ if (m_rendering_engine->get_raw_device()->activateJoysticks(infos)) {
infostream << "Joystick support enabled" << std::endl;
joystick_infos.reserve(infos.size());
for (u32 i = 0; i < infos.size(); i++) {
@@ -471,7 +461,7 @@ bool ClientLauncher::launch_game(std::string &error_message,
start_data.address.empty() && !start_data.name.empty();
}
- if (!RenderingEngine::run())
+ if (!m_rendering_engine->run())
return false;
if (!start_data.isSinglePlayer() && start_data.name.empty()) {
@@ -519,8 +509,8 @@ bool ClientLauncher::launch_game(std::string &error_message,
// Load gamespec for required game
start_data.game_spec = findWorldSubgame(worldspec.path);
if (!start_data.game_spec.isValid()) {
- error_message = gettext("Could not find or load game \"")
- + worldspec.gameid + "\"";
+ error_message = gettext("Could not find or load game: ")
+ + worldspec.gameid;
errorstream << error_message << std::endl;
return false;
}
@@ -543,14 +533,14 @@ bool ClientLauncher::launch_game(std::string &error_message,
void ClientLauncher::main_menu(MainMenuData *menudata)
{
bool *kill = porting::signal_handler_killstatus();
- video::IVideoDriver *driver = RenderingEngine::get_video_driver();
+ video::IVideoDriver *driver = m_rendering_engine->get_video_driver();
infostream << "Waiting for other menus" << std::endl;
- while (RenderingEngine::get_raw_device()->run() && !*kill) {
+ while (m_rendering_engine->run() && !*kill) {
if (!isMenuActive())
break;
driver->beginScene(true, true, video::SColor(255, 128, 128, 128));
- RenderingEngine::get_gui_env()->drawAll();
+ m_rendering_engine->get_gui_env()->drawAll();
driver->endScene();
// On some computers framerate doesn't seem to be automatically limited
sleep_ms(25);
@@ -559,14 +549,14 @@ void ClientLauncher::main_menu(MainMenuData *menudata)
// Cursor can be non-visible when coming from the game
#ifndef ANDROID
- RenderingEngine::get_raw_device()->getCursorControl()->setVisible(true);
+ m_rendering_engine->get_raw_device()->getCursorControl()->setVisible(true);
#endif
/* show main menu */
- GUIEngine mymenu(&input->joystick, guiroot, &g_menumgr, menudata, *kill);
+ GUIEngine mymenu(&input->joystick, guiroot, m_rendering_engine, &g_menumgr, menudata, *kill);
/* leave scene manager in a clean state */
- RenderingEngine::get_scene_manager()->clear();
+ m_rendering_engine->get_scene_manager()->clear();
}
void ClientLauncher::speed_tests()
diff --git a/src/client/clientlauncher.h b/src/client/clientlauncher.h
index b280d8e6b..d1fd9a258 100644
--- a/src/client/clientlauncher.h
+++ b/src/client/clientlauncher.h
@@ -46,9 +46,9 @@ private:
void speed_tests();
- bool list_video_modes = false;
bool skip_main_menu = false;
bool random_input = false;
+ RenderingEngine *m_rendering_engine = nullptr;
InputHandler *input = nullptr;
MyEventReceiver *receiver = nullptr;
gui::IGUISkin *skin = nullptr;
diff --git a/src/client/clientmap.cpp b/src/client/clientmap.cpp
index 8e02fea63..1a024e464 100644
--- a/src/client/clientmap.cpp
+++ b/src/client/clientmap.cpp
@@ -64,15 +64,24 @@ void MeshBufListList::add(scene::IMeshBuffer *buf, v3s16 position, u8 layer)
ClientMap::ClientMap(
Client *client,
+ RenderingEngine *rendering_engine,
MapDrawControl &control,
s32 id
):
Map(client),
- scene::ISceneNode(RenderingEngine::get_scene_manager()->getRootSceneNode(),
- RenderingEngine::get_scene_manager(), id),
+ scene::ISceneNode(rendering_engine->get_scene_manager()->getRootSceneNode(),
+ rendering_engine->get_scene_manager(), id),
m_client(client),
- m_control(control)
+ m_rendering_engine(rendering_engine),
+ m_control(control),
+ m_drawlist(MapBlockComparer(v3s16(0,0,0)))
{
+
+ /*
+ * @Liso: Sadly C++ doesn't have introspection, so the only way we have to know
+ * the class is whith a name ;) Name property cames from ISceneNode base class.
+ */
+ Name = "ClientMap";
m_box = aabb3f(-BS*1000000,-BS*1000000,-BS*1000000,
BS*1000000,BS*1000000,BS*1000000);
@@ -114,12 +123,21 @@ void ClientMap::OnRegisterSceneNode()
}
ISceneNode::OnRegisterSceneNode();
+
+ if (!m_added_to_shadow_renderer) {
+ m_added_to_shadow_renderer = true;
+ if (auto shadows = m_rendering_engine->get_shadow_renderer())
+ shadows->addNodeToShadowList(this);
+ }
}
void ClientMap::getBlocksInViewRange(v3s16 cam_pos_nodes,
- v3s16 *p_blocks_min, v3s16 *p_blocks_max)
+ v3s16 *p_blocks_min, v3s16 *p_blocks_max, float range)
{
- v3s16 box_nodes_d = m_control.wanted_range * v3s16(1, 1, 1);
+ if (range <= 0.0f)
+ range = m_control.wanted_range;
+
+ v3s16 box_nodes_d = range * v3s16(1, 1, 1);
// Define p_nodes_min/max as v3s32 because 'cam_pos_nodes -/+ box_nodes_d'
// can exceed the range of v3s16 when a large view range is used near the
// world edges.
@@ -147,6 +165,8 @@ void ClientMap::updateDrawList()
{
ScopeProfiler sp(g_profiler, "CM::updateDrawList()", SPT_AVG);
+ m_needs_update_drawlist = false;
+
for (auto &i : m_drawlist) {
MapBlock *block = i.second;
block->refDrop();
@@ -161,10 +181,14 @@ void ClientMap::updateDrawList()
const f32 camera_fov = m_camera_fov * 1.1f;
v3s16 cam_pos_nodes = floatToInt(camera_position, BS);
+
v3s16 p_blocks_min;
v3s16 p_blocks_max;
getBlocksInViewRange(cam_pos_nodes, &p_blocks_min, &p_blocks_max);
+ // Read the vision range, unless unlimited range is enabled.
+ float range = m_control.range_all ? 1e7 : m_control.wanted_range;
+
// Number of blocks currently loaded by the client
u32 blocks_loaded = 0;
// Number of blocks with mesh in rendering range
@@ -182,6 +206,9 @@ void ClientMap::updateDrawList()
occlusion_culling_enabled = false;
}
+ v3s16 camera_block = getContainerPos(cam_pos_nodes, MAP_BLOCKSIZE);
+ m_drawlist = std::map<v3s16, MapBlock*, MapBlockComparer>(MapBlockComparer(camera_block));
+
// Uncomment to debug occluded blocks in the wireframe mode
// TODO: Include this as a flag for an extended debugging setting
//if (occlusion_culling_enabled && m_control.show_wireframe)
@@ -218,32 +245,34 @@ void ClientMap::updateDrawList()
continue;
}
- float range = 100000 * BS;
- if (!m_control.range_all)
- range = m_control.wanted_range * BS;
+ v3s16 block_coord = block->getPos();
+ v3s16 block_position = block->getPosRelative() + MAP_BLOCKSIZE / 2;
- float d = 0.0;
- if (!isBlockInSight(block->getPos(), camera_position,
- camera_direction, camera_fov, range, &d))
- continue;
+ // First, perform a simple distance check, with a padding of one extra block.
+ if (!m_control.range_all &&
+ block_position.getDistanceFrom(cam_pos_nodes) > range + MAP_BLOCKSIZE)
+ continue; // Out of range, skip.
+ // Keep the block alive as long as it is in range.
+ block->resetUsageTimer();
blocks_in_range_with_mesh++;
- /*
- Occlusion culling
- */
+ // Frustum culling
+ float d = 0.0;
+ if (!isBlockInSight(block_coord, camera_position,
+ camera_direction, camera_fov, range * BS, &d))
+ continue;
+
+ // Occlusion culling
if ((!m_control.range_all && d > m_control.wanted_range * BS) ||
(occlusion_culling_enabled && isBlockOccluded(block, cam_pos_nodes))) {
blocks_occlusion_culled++;
continue;
}
- // This block is in range. Reset usage timer.
- block->resetUsageTimer();
-
// Add to set
block->refGrab();
- m_drawlist[block->getPos()] = block;
+ m_drawlist[block_coord] = block;
sector_blocks_drawn++;
} // foreach sectorblocks
@@ -282,8 +311,6 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass)
const u32 daynight_ratio = m_client->getEnv().getDayNightRatio();
const v3f camera_position = m_camera_position;
- const v3f camera_direction = m_camera_direction;
- const f32 camera_fov = m_camera_fov;
/*
Get all blocks and draw all visible ones
@@ -300,7 +327,20 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass)
Draw the selected MapBlocks
*/
- MeshBufListList drawbufs;
+ MeshBufListList grouped_buffers;
+
+ struct DrawDescriptor {
+ v3s16 m_pos;
+ scene::IMeshBuffer *m_buffer;
+ bool m_reuse_material;
+
+ DrawDescriptor(const v3s16 &pos, scene::IMeshBuffer *buffer, bool reuse_material) :
+ m_pos(pos), m_buffer(buffer), m_reuse_material(reuse_material)
+ {}
+ };
+
+ std::vector<DrawDescriptor> draw_order;
+ video::SMaterial previous_material;
for (auto &i : m_drawlist) {
v3s16 block_pos = i.first;
@@ -310,14 +350,12 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass)
if (!block->mesh)
continue;
- float d = 0.0;
- if (!isBlockInSight(block->getPos(), camera_position,
- camera_direction, camera_fov, 100000 * BS, &d))
- continue;
+ v3f block_pos_r = intToFloat(block->getPosRelative() + MAP_BLOCKSIZE / 2, BS);
+ float d = camera_position.getDistanceFrom(block_pos_r);
+ d = MYMAX(0,d - BLOCK_MAX_RADIUS);
// Mesh animation
if (pass == scene::ESNRP_SOLID) {
- //MutexAutoLock lock(block->mesh_mutex);
MapBlockMesh *mapBlockMesh = block->mesh;
assert(mapBlockMesh);
// Pretty random but this should work somewhat nicely
@@ -338,8 +376,6 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass)
Get the meshbuffers of the block
*/
{
- //MutexAutoLock lock(block->mesh_mutex);
-
MapBlockMesh *mapBlockMesh = block->mesh;
assert(mapBlockMesh);
@@ -369,42 +405,79 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass)
material.setFlag(video::EMF_WIREFRAME,
m_control.show_wireframe);
- drawbufs.add(buf, block_pos, layer);
+ if (is_transparent_pass) {
+ // Same comparison as in MeshBufListList
+ bool new_material = material.getTexture(0) != previous_material.getTexture(0) ||
+ material != previous_material;
+
+ draw_order.emplace_back(block_pos, buf, !new_material);
+
+ if (new_material)
+ previous_material = material;
+ }
+ else {
+ grouped_buffers.add(buf, block_pos, layer);
+ }
}
}
}
}
}
+ // Capture draw order for all solid meshes
+ for (auto &lists : grouped_buffers.lists) {
+ for (MeshBufList &list : lists) {
+ // iterate in reverse to draw closest blocks first
+ for (auto it = list.bufs.rbegin(); it != list.bufs.rend(); ++it) {
+ draw_order.emplace_back(it->first, it->second, it != list.bufs.rbegin());
+ }
+ }
+ }
+
TimeTaker draw("Drawing mesh buffers");
core::matrix4 m; // Model matrix
v3f offset = intToFloat(m_camera_offset, BS);
+ u32 material_swaps = 0;
- // Render all layers in order
- for (auto &lists : drawbufs.lists) {
- for (MeshBufList &list : lists) {
- // Check and abort if the machine is swapping a lot
- if (draw.getTimerTime() > 2000) {
- infostream << "ClientMap::renderMap(): Rendering took >2s, " <<
- "returning." << std::endl;
- return;
- }
- driver->setMaterial(list.m);
-
- drawcall_count += list.bufs.size();
- for (auto &pair : list.bufs) {
- scene::IMeshBuffer *buf = pair.second;
+ // Render all mesh buffers in order
+ drawcall_count += draw_order.size();
+ for (auto &descriptor : draw_order) {
+ scene::IMeshBuffer *buf = descriptor.m_buffer;
- v3f block_wpos = intToFloat(pair.first * MAP_BLOCKSIZE, BS);
- m.setTranslation(block_wpos - offset);
+ // Check and abort if the machine is swapping a lot
+ if (draw.getTimerTime() > 2000) {
+ infostream << "ClientMap::renderMap(): Rendering took >2s, " <<
+ "returning." << std::endl;
+ return;
+ }
- driver->setTransform(video::ETS_WORLD, m);
- driver->drawMeshBuffer(buf);
- vertex_count += buf->getVertexCount();
+ if (!descriptor.m_reuse_material) {
+ auto &material = buf->getMaterial();
+ // pass the shadow map texture to the buffer texture
+ ShadowRenderer *shadow = m_rendering_engine->get_shadow_renderer();
+ if (shadow && shadow->is_active()) {
+ auto &layer = material.TextureLayer[3];
+ layer.Texture = shadow->get_texture();
+ layer.TextureWrapU = video::E_TEXTURE_CLAMP::ETC_CLAMP_TO_EDGE;
+ layer.TextureWrapV = video::E_TEXTURE_CLAMP::ETC_CLAMP_TO_EDGE;
+ // Do not enable filter on shadow texture to avoid visual artifacts
+ // with colored shadows.
+ // Filtering is done in shader code anyway
+ layer.TrilinearFilter = false;
}
+ driver->setMaterial(material);
+ ++material_swaps;
}
+
+ v3f block_wpos = intToFloat(descriptor.m_pos * MAP_BLOCKSIZE, BS);
+ m.setTranslation(block_wpos - offset);
+
+ driver->setTransform(video::ETS_WORLD, m);
+ driver->drawMeshBuffer(buf);
+ vertex_count += buf->getVertexCount();
}
+
g_profiler->avg(prefix + "draw meshes [ms]", draw.stop(true));
// Log only on solid pass because values are the same
@@ -412,8 +485,13 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass)
g_profiler->avg("renderMap(): animated meshes [#]", mesh_animate_count);
}
+ if (pass == scene::ESNRP_TRANSPARENT) {
+ g_profiler->avg("renderMap(): transparent buffers [#]", draw_order.size());
+ }
+
g_profiler->avg(prefix + "vertices drawn [#]", vertex_count);
g_profiler->avg(prefix + "drawcalls [#]", drawcall_count);
+ g_profiler->avg(prefix + "material swaps [#]", material_swaps);
}
static bool getVisibleBrightness(Map *map, const v3f &p0, v3f dir, float step,
@@ -606,3 +684,210 @@ void ClientMap::PrintInfo(std::ostream &out)
{
out<<"ClientMap: ";
}
+
+void ClientMap::renderMapShadows(video::IVideoDriver *driver,
+ const video::SMaterial &material, s32 pass, int frame, int total_frames)
+{
+ bool is_transparent_pass = pass != scene::ESNRP_SOLID;
+ std::string prefix;
+ if (is_transparent_pass)
+ prefix = "renderMap(SHADOW TRANS): ";
+ else
+ prefix = "renderMap(SHADOW SOLID): ";
+
+ u32 drawcall_count = 0;
+ u32 vertex_count = 0;
+
+ MeshBufListList drawbufs;
+
+ int count = 0;
+ int low_bound = is_transparent_pass ? 0 : m_drawlist_shadow.size() / total_frames * frame;
+ int high_bound = is_transparent_pass ? m_drawlist_shadow.size() : m_drawlist_shadow.size() / total_frames * (frame + 1);
+
+ // transparent pass should be rendered in one go
+ if (is_transparent_pass && frame != total_frames - 1) {
+ return;
+ }
+
+ for (auto &i : m_drawlist_shadow) {
+ // only process specific part of the list & break early
+ ++count;
+ if (count <= low_bound)
+ continue;
+ if (count > high_bound)
+ break;
+
+ v3s16 block_pos = i.first;
+ MapBlock *block = i.second;
+
+ // If the mesh of the block happened to get deleted, ignore it
+ if (!block->mesh)
+ continue;
+
+ /*
+ Get the meshbuffers of the block
+ */
+ {
+ MapBlockMesh *mapBlockMesh = block->mesh;
+ assert(mapBlockMesh);
+
+ for (int layer = 0; layer < MAX_TILE_LAYERS; layer++) {
+ scene::IMesh *mesh = mapBlockMesh->getMesh(layer);
+ assert(mesh);
+
+ u32 c = mesh->getMeshBufferCount();
+ for (u32 i = 0; i < c; i++) {
+ scene::IMeshBuffer *buf = mesh->getMeshBuffer(i);
+
+ video::SMaterial &mat = buf->getMaterial();
+ auto rnd = driver->getMaterialRenderer(mat.MaterialType);
+ bool transparent = rnd && rnd->isTransparent();
+ if (transparent == is_transparent_pass)
+ drawbufs.add(buf, block_pos, layer);
+ }
+ }
+ }
+ }
+
+ TimeTaker draw("Drawing shadow mesh buffers");
+
+ core::matrix4 m; // Model matrix
+ v3f offset = intToFloat(m_camera_offset, BS);
+
+ // Render all layers in order
+ for (auto &lists : drawbufs.lists) {
+ for (MeshBufList &list : lists) {
+ // Check and abort if the machine is swapping a lot
+ if (draw.getTimerTime() > 1000) {
+ infostream << "ClientMap::renderMapShadows(): Rendering "
+ "took >1s, returning." << std::endl;
+ break;
+ }
+ for (auto &pair : list.bufs) {
+ scene::IMeshBuffer *buf = pair.second;
+
+ // override some material properties
+ video::SMaterial local_material = buf->getMaterial();
+ local_material.MaterialType = material.MaterialType;
+ local_material.BackfaceCulling = material.BackfaceCulling;
+ local_material.FrontfaceCulling = material.FrontfaceCulling;
+ local_material.BlendOperation = material.BlendOperation;
+ local_material.Lighting = false;
+ driver->setMaterial(local_material);
+
+ v3f block_wpos = intToFloat(pair.first * MAP_BLOCKSIZE, BS);
+ m.setTranslation(block_wpos - offset);
+
+ driver->setTransform(video::ETS_WORLD, m);
+ driver->drawMeshBuffer(buf);
+ vertex_count += buf->getVertexCount();
+ }
+
+ drawcall_count += list.bufs.size();
+ }
+ }
+
+ // restore the driver material state
+ video::SMaterial clean;
+ clean.BlendOperation = video::EBO_ADD;
+ driver->setMaterial(clean); // reset material to defaults
+ driver->draw3DLine(v3f(), v3f(), video::SColor(0));
+
+ g_profiler->avg(prefix + "draw meshes [ms]", draw.stop(true));
+ g_profiler->avg(prefix + "vertices drawn [#]", vertex_count);
+ g_profiler->avg(prefix + "drawcalls [#]", drawcall_count);
+}
+
+/*
+ Custom update draw list for the pov of shadow light.
+*/
+void ClientMap::updateDrawListShadow(const v3f &shadow_light_pos, const v3f &shadow_light_dir, float shadow_range)
+{
+ ScopeProfiler sp(g_profiler, "CM::updateDrawListShadow()", SPT_AVG);
+
+ const v3f camera_position = shadow_light_pos;
+ const v3f camera_direction = shadow_light_dir;
+ // I "fake" fov just to avoid creating a new function to handle orthographic
+ // projection.
+ const f32 camera_fov = m_camera_fov * 1.9f;
+
+ v3s16 cam_pos_nodes = floatToInt(camera_position, BS);
+ v3s16 p_blocks_min;
+ v3s16 p_blocks_max;
+ getBlocksInViewRange(cam_pos_nodes, &p_blocks_min, &p_blocks_max, shadow_range);
+
+ std::vector<v2s16> blocks_in_range;
+
+ for (auto &i : m_drawlist_shadow) {
+ MapBlock *block = i.second;
+ block->refDrop();
+ }
+ m_drawlist_shadow.clear();
+
+ // We need to append the blocks from the camera POV because sometimes
+ // they are not inside the light frustum and it creates glitches.
+ // FIXME: This could be removed if we figure out why they are missing
+ // from the light frustum.
+ for (auto &i : m_drawlist) {
+ i.second->refGrab();
+ m_drawlist_shadow[i.first] = i.second;
+ }
+
+ // Number of blocks currently loaded by the client
+ u32 blocks_loaded = 0;
+ // Number of blocks with mesh in rendering range
+ u32 blocks_in_range_with_mesh = 0;
+ // Number of blocks occlusion culled
+ u32 blocks_occlusion_culled = 0;
+
+ for (auto &sector_it : m_sectors) {
+ MapSector *sector = sector_it.second;
+ if (!sector)
+ continue;
+ blocks_loaded += sector->size();
+
+ MapBlockVect sectorblocks;
+ sector->getBlocks(sectorblocks);
+
+ /*
+ Loop through blocks in sector
+ */
+ for (MapBlock *block : sectorblocks) {
+ if (!block->mesh) {
+ // Ignore if mesh doesn't exist
+ continue;
+ }
+
+ float range = shadow_range;
+
+ float d = 0.0;
+ if (!isBlockInSight(block->getPos(), camera_position,
+ camera_direction, camera_fov, range, &d))
+ continue;
+
+ blocks_in_range_with_mesh++;
+
+ /*
+ Occlusion culling
+ */
+ if (isBlockOccluded(block, cam_pos_nodes)) {
+ blocks_occlusion_culled++;
+ continue;
+ }
+
+ // This block is in range. Reset usage timer.
+ block->resetUsageTimer();
+
+ // Add to set
+ if (m_drawlist_shadow.find(block->getPos()) == m_drawlist_shadow.end()) {
+ block->refGrab();
+ m_drawlist_shadow[block->getPos()] = block;
+ }
+ }
+ }
+
+ g_profiler->avg("SHADOW MapBlock meshes in range [#]", blocks_in_range_with_mesh);
+ g_profiler->avg("SHADOW MapBlocks occlusion culled [#]", blocks_occlusion_culled);
+ g_profiler->avg("SHADOW MapBlocks drawn [#]", m_drawlist_shadow.size());
+ g_profiler->avg("SHADOW MapBlocks loaded [#]", blocks_loaded);
+}
diff --git a/src/client/clientmap.h b/src/client/clientmap.h
index 57cc4427e..b4dc42395 100644
--- a/src/client/clientmap.h
+++ b/src/client/clientmap.h
@@ -68,6 +68,7 @@ class ClientMap : public Map, public scene::ISceneNode
public:
ClientMap(
Client *client,
+ RenderingEngine *rendering_engine,
MapDrawControl &control,
s32 id
);
@@ -86,10 +87,18 @@ public:
void updateCamera(const v3f &pos, const v3f &dir, f32 fov, const v3s16 &offset)
{
+ v3s16 previous_block = getContainerPos(floatToInt(m_camera_position, BS) + m_camera_offset, MAP_BLOCKSIZE);
+
m_camera_position = pos;
m_camera_direction = dir;
m_camera_fov = fov;
m_camera_offset = offset;
+
+ v3s16 current_block = getContainerPos(floatToInt(m_camera_position, BS) + m_camera_offset, MAP_BLOCKSIZE);
+
+ // reorder the blocks when camera crosses block boundary
+ if (previous_block != current_block)
+ m_needs_update_drawlist = true;
}
/*
@@ -118,10 +127,16 @@ public:
}
void getBlocksInViewRange(v3s16 cam_pos_nodes,
- v3s16 *p_blocks_min, v3s16 *p_blocks_max);
+ v3s16 *p_blocks_min, v3s16 *p_blocks_max, float range=-1.0f);
void updateDrawList();
+ void updateDrawListShadow(const v3f &shadow_light_pos, const v3f &shadow_light_dir, float shadow_range);
+ // Returns true if draw list needs updating before drawing the next frame.
+ bool needsUpdateDrawList() { return m_needs_update_drawlist; }
void renderMap(video::IVideoDriver* driver, s32 pass);
+ void renderMapShadows(video::IVideoDriver *driver,
+ const video::SMaterial &material, s32 pass, int frame, int total_frames);
+
int getBackgroundBrightness(float max_d, u32 daylight_factor,
int oldvalue, bool *sunlight_seen_result);
@@ -131,9 +146,29 @@ public:
virtual void PrintInfo(std::ostream &out);
const MapDrawControl & getControl() const { return m_control; }
+ f32 getWantedRange() const { return m_control.wanted_range; }
f32 getCameraFov() const { return m_camera_fov; }
+
private:
+ // Orders blocks by distance to the camera
+ class MapBlockComparer
+ {
+ public:
+ MapBlockComparer(const v3s16 &camera_block) : m_camera_block(camera_block) {}
+
+ bool operator() (const v3s16 &left, const v3s16 &right) const
+ {
+ auto distance_left = left.getDistanceFromSQ(m_camera_block);
+ auto distance_right = right.getDistanceFromSQ(m_camera_block);
+ return distance_left > distance_right || (distance_left == distance_right && left > right);
+ }
+
+ private:
+ v3s16 m_camera_block;
+ };
+
Client *m_client;
+ RenderingEngine *m_rendering_engine;
aabb3f m_box = aabb3f(-BS * 1000000, -BS * 1000000, -BS * 1000000,
BS * 1000000, BS * 1000000, BS * 1000000);
@@ -145,11 +180,14 @@ private:
f32 m_camera_fov = M_PI;
v3s16 m_camera_offset;
- std::map<v3s16, MapBlock*> m_drawlist;
+ std::map<v3s16, MapBlock*, MapBlockComparer> m_drawlist;
+ std::map<v3s16, MapBlock*> m_drawlist_shadow;
+ bool m_needs_update_drawlist;
std::set<v2s16> m_last_drawn_sectors;
bool m_cache_trilinear_filter;
bool m_cache_bilinear_filter;
bool m_cache_anistropic_filter;
+ bool m_added_to_shadow_renderer{false};
};
diff --git a/src/client/clientmedia.cpp b/src/client/clientmedia.cpp
index c4c08c05d..6c5d4a8bf 100644
--- a/src/client/clientmedia.cpp
+++ b/src/client/clientmedia.cpp
@@ -49,7 +49,6 @@ bool clientMediaUpdateCache(const std::string &raw_hash, const std::string &file
*/
ClientMediaDownloader::ClientMediaDownloader():
- m_media_cache(getMediaCacheDir()),
m_httpfetch_caller(HTTPFETCH_DISCARD)
{
}
@@ -66,6 +65,12 @@ ClientMediaDownloader::~ClientMediaDownloader()
delete remote;
}
+bool ClientMediaDownloader::loadMedia(Client *client, const std::string &data,
+ const std::string &name)
+{
+ return client->loadMedia(data, name);
+}
+
void ClientMediaDownloader::addFile(const std::string &name, const std::string &sha1)
{
assert(!m_initial_step_done); // pre-condition
@@ -105,7 +110,7 @@ void ClientMediaDownloader::addRemoteServer(const std::string &baseurl)
{
assert(!m_initial_step_done); // pre-condition
- #ifdef USE_CURL
+#ifdef USE_CURL
if (g_settings->getBool("enable_remote_media_server")) {
infostream << "Client: Adding remote server \""
@@ -117,13 +122,13 @@ void ClientMediaDownloader::addRemoteServer(const std::string &baseurl)
m_remotes.push_back(remote);
}
- #else
+#else
infostream << "Client: Ignoring remote server \""
<< baseurl << "\" because cURL support is not compiled in"
<< std::endl;
- #endif
+#endif
}
void ClientMediaDownloader::step(Client *client)
@@ -172,36 +177,21 @@ void ClientMediaDownloader::initialStep(Client *client)
// Check media cache
m_uncached_count = m_files.size();
for (auto &file_it : m_files) {
- std::string name = file_it.first;
+ const std::string &name = file_it.first;
FileStatus *filestatus = file_it.second;
const std::string &sha1 = filestatus->sha1;
- std::ostringstream tmp_os(std::ios_base::binary);
- bool found_in_cache = m_media_cache.load(hex_encode(sha1), tmp_os);
-
- // If found in cache, try to load it from there
- if (found_in_cache) {
- bool success = checkAndLoad(name, sha1,
- tmp_os.str(), true, client);
- if (success) {
- filestatus->received = true;
- m_uncached_count--;
- }
+ if (tryLoadFromCache(name, sha1, client)) {
+ filestatus->received = true;
+ m_uncached_count--;
}
}
assert(m_uncached_received_count == 0);
// Create the media cache dir if we are likely to write to it
- if (m_uncached_count != 0) {
- bool did = fs::CreateAllDirs(getMediaCacheDir());
- if (!did) {
- errorstream << "Client: "
- << "Could not create media cache directory: "
- << getMediaCacheDir()
- << std::endl;
- }
- }
+ if (m_uncached_count != 0)
+ createCacheDirs();
// If we found all files in the cache, report this fact to the server.
// If the server reported no remote servers, immediately start
@@ -216,7 +206,6 @@ void ClientMediaDownloader::initialStep(Client *client)
// This is the first time we use httpfetch, so alloc a caller ID
m_httpfetch_caller = httpfetch_caller_alloc();
- m_httpfetch_timeout = g_settings->getS32("curl_timeout");
// Set the active fetch limit to curl_parallel_limit or 84,
// whichever is greater. This gives us some leeway so that
@@ -258,8 +247,6 @@ void ClientMediaDownloader::initialStep(Client *client)
remote->baseurl + MTHASHSET_FILE_NAME;
fetch_request.caller = m_httpfetch_caller;
fetch_request.request_id = m_httpfetch_next_id; // == i
- fetch_request.timeout = m_httpfetch_timeout;
- fetch_request.connect_timeout = m_httpfetch_timeout;
fetch_request.method = HTTP_POST;
fetch_request.raw_data = required_hash_set;
fetch_request.extra_headers.emplace_back(
@@ -304,8 +291,7 @@ void ClientMediaDownloader::remoteHashSetReceived(
// available on this server, add this server
// to the available_remotes array
- for(std::map<std::string, FileStatus*>::iterator
- it = m_files.upper_bound(m_name_bound);
+ for(auto it = m_files.upper_bound(m_name_bound);
it != m_files.end(); ++it) {
FileStatus *f = it->second;
if (!f->received && sha1_set.count(f->sha1))
@@ -331,8 +317,7 @@ void ClientMediaDownloader::remoteMediaReceived(
std::string name;
{
- std::unordered_map<unsigned long, std::string>::iterator it =
- m_remote_file_transfers.find(fetch_result.request_id);
+ auto it = m_remote_file_transfers.find(fetch_result.request_id);
assert(it != m_remote_file_transfers.end());
name = it->second;
m_remote_file_transfers.erase(it);
@@ -401,8 +386,7 @@ void ClientMediaDownloader::startRemoteMediaTransfers()
{
bool changing_name_bound = true;
- for (std::map<std::string, FileStatus*>::iterator
- files_iter = m_files.upper_bound(m_name_bound);
+ for (auto files_iter = m_files.upper_bound(m_name_bound);
files_iter != m_files.end(); ++files_iter) {
// Abort if active fetch limit is exceeded
@@ -432,9 +416,8 @@ void ClientMediaDownloader::startRemoteMediaTransfers()
fetch_request.url = url;
fetch_request.caller = m_httpfetch_caller;
fetch_request.request_id = m_httpfetch_next_id;
- fetch_request.timeout = 0; // no data timeout!
- fetch_request.connect_timeout =
- m_httpfetch_timeout;
+ fetch_request.timeout =
+ g_settings->getS32("curl_file_download_timeout");
httpfetch_async(fetch_request);
m_remote_file_transfers.insert(std::make_pair(
@@ -481,19 +464,18 @@ void ClientMediaDownloader::startConventionalTransfers(Client *client)
}
}
-void ClientMediaDownloader::conventionalTransferDone(
+bool ClientMediaDownloader::conventionalTransferDone(
const std::string &name,
const std::string &data,
Client *client)
{
// Check that file was announced
- std::map<std::string, FileStatus*>::iterator
- file_iter = m_files.find(name);
+ auto file_iter = m_files.find(name);
if (file_iter == m_files.end()) {
errorstream << "Client: server sent media file that was"
<< "not announced, ignoring it: \"" << name << "\""
<< std::endl;
- return;
+ return false;
}
FileStatus *filestatus = file_iter->second;
assert(filestatus != NULL);
@@ -503,7 +485,7 @@ void ClientMediaDownloader::conventionalTransferDone(
errorstream << "Client: server sent media file that we already"
<< "received, ignoring it: \"" << name << "\""
<< std::endl;
- return;
+ return true;
}
// Mark file as received, regardless of whether loading it works and
@@ -516,9 +498,45 @@ void ClientMediaDownloader::conventionalTransferDone(
// Check that received file matches announced checksum
// If so, load it
checkAndLoad(name, filestatus->sha1, data, false, client);
+
+ return true;
+}
+
+/*
+ IClientMediaDownloader
+*/
+
+IClientMediaDownloader::IClientMediaDownloader():
+ m_media_cache(getMediaCacheDir()), m_write_to_cache(true)
+{
}
-bool ClientMediaDownloader::checkAndLoad(
+void IClientMediaDownloader::createCacheDirs()
+{
+ if (!m_write_to_cache)
+ return;
+
+ std::string path = getMediaCacheDir();
+ if (!fs::CreateAllDirs(path)) {
+ errorstream << "Client: Could not create media cache directory: "
+ << path << std::endl;
+ }
+}
+
+bool IClientMediaDownloader::tryLoadFromCache(const std::string &name,
+ const std::string &sha1, Client *client)
+{
+ std::ostringstream tmp_os(std::ios_base::binary);
+ bool found_in_cache = m_media_cache.load(hex_encode(sha1), tmp_os);
+
+ // If found in cache, try to load it from there
+ if (found_in_cache)
+ return checkAndLoad(name, sha1, tmp_os.str(), true, client);
+
+ return false;
+}
+
+bool IClientMediaDownloader::checkAndLoad(
const std::string &name, const std::string &sha1,
const std::string &data, bool is_from_cache, Client *client)
{
@@ -548,7 +566,7 @@ bool ClientMediaDownloader::checkAndLoad(
}
// Checksum is ok, try loading the file
- bool success = client->loadMedia(data, name);
+ bool success = loadMedia(client, data, name);
if (!success) {
infostream << "Client: "
<< "Failed to load " << cached_or_received << " media: "
@@ -563,7 +581,7 @@ bool ClientMediaDownloader::checkAndLoad(
<< std::endl;
// Update cache (unless we just loaded the file from the cache)
- if (!is_from_cache)
+ if (!is_from_cache && m_write_to_cache)
m_media_cache.update(sha1_hex, data);
return true;
@@ -591,12 +609,10 @@ std::string ClientMediaDownloader::serializeRequiredHashSet()
// Write list of hashes of files that have not been
// received (found in cache) yet
- for (std::map<std::string, FileStatus*>::iterator
- it = m_files.begin();
- it != m_files.end(); ++it) {
- if (!it->second->received) {
- FATAL_ERROR_IF(it->second->sha1.size() != 20, "Invalid SHA1 size");
- os << it->second->sha1;
+ for (const auto &it : m_files) {
+ if (!it.second->received) {
+ FATAL_ERROR_IF(it.second->sha1.size() != 20, "Invalid SHA1 size");
+ os << it.second->sha1;
}
}
@@ -632,3 +648,145 @@ void ClientMediaDownloader::deSerializeHashSet(const std::string &data,
result.insert(data.substr(pos, 20));
}
}
+
+/*
+ SingleMediaDownloader
+*/
+
+SingleMediaDownloader::SingleMediaDownloader(bool write_to_cache):
+ m_httpfetch_caller(HTTPFETCH_DISCARD)
+{
+ m_write_to_cache = write_to_cache;
+}
+
+SingleMediaDownloader::~SingleMediaDownloader()
+{
+ if (m_httpfetch_caller != HTTPFETCH_DISCARD)
+ httpfetch_caller_free(m_httpfetch_caller);
+}
+
+bool SingleMediaDownloader::loadMedia(Client *client, const std::string &data,
+ const std::string &name)
+{
+ return client->loadMedia(data, name, true);
+}
+
+void SingleMediaDownloader::addFile(const std::string &name, const std::string &sha1)
+{
+ assert(m_stage == STAGE_INIT); // pre-condition
+
+ assert(!name.empty());
+ assert(sha1.size() == 20);
+
+ FATAL_ERROR_IF(!m_file_name.empty(), "Cannot add a second file");
+ m_file_name = name;
+ m_file_sha1 = sha1;
+}
+
+void SingleMediaDownloader::addRemoteServer(const std::string &baseurl)
+{
+ assert(m_stage == STAGE_INIT); // pre-condition
+
+ if (g_settings->getBool("enable_remote_media_server"))
+ m_remotes.emplace_back(baseurl);
+}
+
+void SingleMediaDownloader::step(Client *client)
+{
+ if (m_stage == STAGE_INIT) {
+ m_stage = STAGE_CACHE_CHECKED;
+ initialStep(client);
+ }
+
+ // Remote media: check for completion of fetches
+ if (m_httpfetch_caller != HTTPFETCH_DISCARD) {
+ HTTPFetchResult fetch_result;
+ while (httpfetch_async_get(m_httpfetch_caller, fetch_result)) {
+ remoteMediaReceived(fetch_result, client);
+ }
+ }
+}
+
+bool SingleMediaDownloader::conventionalTransferDone(const std::string &name,
+ const std::string &data, Client *client)
+{
+ if (name != m_file_name)
+ return false;
+
+ // Mark file as received unconditionally and try to load it
+ m_stage = STAGE_DONE;
+ checkAndLoad(name, m_file_sha1, data, false, client);
+ return true;
+}
+
+void SingleMediaDownloader::initialStep(Client *client)
+{
+ if (tryLoadFromCache(m_file_name, m_file_sha1, client))
+ m_stage = STAGE_DONE;
+ if (isDone())
+ return;
+
+ createCacheDirs();
+
+ // If the server reported no remote servers, immediately fall back to
+ // conventional transfer.
+ if (!USE_CURL || m_remotes.empty()) {
+ startConventionalTransfer(client);
+ } else {
+ // Otherwise start by requesting the file from the first remote media server
+ m_httpfetch_caller = httpfetch_caller_alloc();
+ m_current_remote = 0;
+ startRemoteMediaTransfer();
+ }
+}
+
+void SingleMediaDownloader::remoteMediaReceived(
+ const HTTPFetchResult &fetch_result, Client *client)
+{
+ sanity_check(!isDone());
+ sanity_check(m_current_remote >= 0);
+
+ // If fetch succeeded, try to load it
+ if (fetch_result.succeeded) {
+ bool success = checkAndLoad(m_file_name, m_file_sha1,
+ fetch_result.data, false, client);
+ if (success) {
+ m_stage = STAGE_DONE;
+ return;
+ }
+ }
+
+ // Otherwise try the next remote server or fall back to conventional transfer
+ m_current_remote++;
+ if (m_current_remote >= (int)m_remotes.size()) {
+ infostream << "Client: Failed to remote-fetch \"" << m_file_name
+ << "\". Requesting it the usual way." << std::endl;
+ m_current_remote = -1;
+ startConventionalTransfer(client);
+ } else {
+ startRemoteMediaTransfer();
+ }
+}
+
+void SingleMediaDownloader::startRemoteMediaTransfer()
+{
+ std::string url = m_remotes.at(m_current_remote) + hex_encode(m_file_sha1);
+ verbosestream << "Client: Requesting remote media file "
+ << "\"" << m_file_name << "\" " << "\"" << url << "\"" << std::endl;
+
+ HTTPFetchRequest fetch_request;
+ fetch_request.url = url;
+ fetch_request.caller = m_httpfetch_caller;
+ fetch_request.request_id = m_httpfetch_next_id;
+ fetch_request.timeout = g_settings->getS32("curl_file_download_timeout");
+ httpfetch_async(fetch_request);
+
+ m_httpfetch_next_id++;
+}
+
+void SingleMediaDownloader::startConventionalTransfer(Client *client)
+{
+ std::vector<std::string> requests;
+ requests.emplace_back(m_file_name);
+ client->request_media(requests);
+}
diff --git a/src/client/clientmedia.h b/src/client/clientmedia.h
index 5a918535b..c297d737f 100644
--- a/src/client/clientmedia.h
+++ b/src/client/clientmedia.h
@@ -21,6 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "irrlichttypes.h"
#include "filecache.h"
+#include "util/basic_macros.h"
#include <ostream>
#include <map>
#include <set>
@@ -38,7 +39,62 @@ struct HTTPFetchResult;
bool clientMediaUpdateCache(const std::string &raw_hash,
const std::string &filedata);
-class ClientMediaDownloader
+// more of a base class than an interface but this name was most convenient...
+class IClientMediaDownloader
+{
+public:
+ DISABLE_CLASS_COPY(IClientMediaDownloader)
+
+ virtual bool isStarted() const = 0;
+
+ // If this returns true, the downloader is done and can be deleted
+ virtual bool isDone() const = 0;
+
+ // Add a file to the list of required file (but don't fetch it yet)
+ virtual void addFile(const std::string &name, const std::string &sha1) = 0;
+
+ // Add a remote server to the list; ignored if not built with cURL
+ virtual void addRemoteServer(const std::string &baseurl) = 0;
+
+ // Steps the media downloader:
+ // - May load media into client by calling client->loadMedia()
+ // - May check media cache for files
+ // - May add files to media cache
+ // - May start remote transfers by calling httpfetch_async
+ // - May check for completion of current remote transfers
+ // - May start conventional transfers by calling client->request_media()
+ // - May inform server that all media has been loaded
+ // by calling client->received_media()
+ // After step has been called once, don't call addFile/addRemoteServer.
+ virtual void step(Client *client) = 0;
+
+ // Must be called for each file received through TOCLIENT_MEDIA
+ // returns true if this file belongs to this downloader
+ virtual bool conventionalTransferDone(const std::string &name,
+ const std::string &data, Client *client) = 0;
+
+protected:
+ IClientMediaDownloader();
+ virtual ~IClientMediaDownloader() = default;
+
+ // Forwards the call to the appropriate Client method
+ virtual bool loadMedia(Client *client, const std::string &data,
+ const std::string &name) = 0;
+
+ void createCacheDirs();
+
+ bool tryLoadFromCache(const std::string &name, const std::string &sha1,
+ Client *client);
+
+ bool checkAndLoad(const std::string &name, const std::string &sha1,
+ const std::string &data, bool is_from_cache, Client *client);
+
+ // Filesystem-based media cache
+ FileCache m_media_cache;
+ bool m_write_to_cache;
+};
+
+class ClientMediaDownloader : public IClientMediaDownloader
{
public:
ClientMediaDownloader();
@@ -52,39 +108,29 @@ public:
return 0.0f;
}
- bool isStarted() const {
+ bool isStarted() const override {
return m_initial_step_done;
}
- // If this returns true, the downloader is done and can be deleted
- bool isDone() const {
+ bool isDone() const override {
return m_initial_step_done &&
m_uncached_received_count == m_uncached_count;
}
- // Add a file to the list of required file (but don't fetch it yet)
- void addFile(const std::string &name, const std::string &sha1);
+ void addFile(const std::string &name, const std::string &sha1) override;
- // Add a remote server to the list; ignored if not built with cURL
- void addRemoteServer(const std::string &baseurl);
+ void addRemoteServer(const std::string &baseurl) override;
- // Steps the media downloader:
- // - May load media into client by calling client->loadMedia()
- // - May check media cache for files
- // - May add files to media cache
- // - May start remote transfers by calling httpfetch_async
- // - May check for completion of current remote transfers
- // - May start conventional transfers by calling client->request_media()
- // - May inform server that all media has been loaded
- // by calling client->received_media()
- // After step has been called once, don't call addFile/addRemoteServer.
- void step(Client *client);
+ void step(Client *client) override;
- // Must be called for each file received through TOCLIENT_MEDIA
- void conventionalTransferDone(
+ bool conventionalTransferDone(
const std::string &name,
const std::string &data,
- Client *client);
+ Client *client) override;
+
+protected:
+ bool loadMedia(Client *client, const std::string &data,
+ const std::string &name) override;
private:
struct FileStatus {
@@ -107,13 +153,9 @@ private:
void startRemoteMediaTransfers();
void startConventionalTransfers(Client *client);
- bool checkAndLoad(const std::string &name, const std::string &sha1,
- const std::string &data, bool is_from_cache,
- Client *client);
-
- std::string serializeRequiredHashSet();
static void deSerializeHashSet(const std::string &data,
std::set<std::string> &result);
+ std::string serializeRequiredHashSet();
// Maps filename to file status
std::map<std::string, FileStatus*> m_files;
@@ -121,9 +163,6 @@ private:
// Array of remote media servers
std::vector<RemoteServerStatus*> m_remotes;
- // Filesystem-based media cache
- FileCache m_media_cache;
-
// Has an attempt been made to load media files from the file cache?
// Have hash sets been requested from remote servers?
bool m_initial_step_done = false;
@@ -135,13 +174,12 @@ private:
s32 m_uncached_received_count = 0;
// Status of remote transfers
- unsigned long m_httpfetch_caller;
- unsigned long m_httpfetch_next_id = 0;
- long m_httpfetch_timeout = 0;
+ u64 m_httpfetch_caller;
+ u64 m_httpfetch_next_id = 0;
s32 m_httpfetch_active = 0;
s32 m_httpfetch_active_limit = 0;
s32 m_outstanding_hash_sets = 0;
- std::unordered_map<unsigned long, std::string> m_remote_file_transfers;
+ std::unordered_map<u64, std::string> m_remote_file_transfers;
// All files up to this name have either been received from a
// remote server or failed on all remote servers, so those files
@@ -150,3 +188,63 @@ private:
std::string m_name_bound = "";
};
+
+// A media downloader that only downloads a single file.
+// It does/doesn't do several things the normal downloader does:
+// - won't fetch hash sets from remote servers
+// - will mark loaded media as coming from file push
+// - writing to file cache is optional
+class SingleMediaDownloader : public IClientMediaDownloader
+{
+public:
+ SingleMediaDownloader(bool write_to_cache);
+ ~SingleMediaDownloader();
+
+ bool isStarted() const override {
+ return m_stage > STAGE_INIT;
+ }
+
+ bool isDone() const override {
+ return m_stage >= STAGE_DONE;
+ }
+
+ void addFile(const std::string &name, const std::string &sha1) override;
+
+ void addRemoteServer(const std::string &baseurl) override;
+
+ void step(Client *client) override;
+
+ bool conventionalTransferDone(const std::string &name,
+ const std::string &data, Client *client) override;
+
+protected:
+ bool loadMedia(Client *client, const std::string &data,
+ const std::string &name) override;
+
+private:
+ void initialStep(Client *client);
+ void remoteMediaReceived(const HTTPFetchResult &fetch_result, Client *client);
+ void startRemoteMediaTransfer();
+ void startConventionalTransfer(Client *client);
+
+ enum Stage {
+ STAGE_INIT,
+ STAGE_CACHE_CHECKED, // we have tried to load the file from cache
+ STAGE_DONE
+ };
+
+ // Information about the one file we want to fetch
+ std::string m_file_name;
+ std::string m_file_sha1;
+ s32 m_current_remote;
+
+ // Array of remote media servers
+ std::vector<std::string> m_remotes;
+
+ enum Stage m_stage = STAGE_INIT;
+
+ // Status of remote transfers
+ unsigned long m_httpfetch_caller;
+ unsigned long m_httpfetch_next_id = 0;
+
+};
diff --git a/src/client/clientobject.h b/src/client/clientobject.h
index ecd8059ef..b192f0dcd 100644
--- a/src/client/clientobject.h
+++ b/src/client/clientobject.h
@@ -39,7 +39,7 @@ public:
ClientActiveObject(u16 id, Client *client, ClientEnvironment *env);
virtual ~ClientActiveObject();
- virtual void addToScene(ITextureSource *tsrc) {}
+ virtual void addToScene(ITextureSource *tsrc, scene::ISceneManager *smgr) = 0;
virtual void removeFromScene(bool permanent) {}
virtual void updateLight(u32 day_night_ratio) {}
diff --git a/src/client/clouds.cpp b/src/client/clouds.cpp
index 5a075aaf0..383a1d799 100644
--- a/src/client/clouds.cpp
+++ b/src/client/clouds.cpp
@@ -30,7 +30,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
// Menu clouds are created later
class Clouds;
Clouds *g_menuclouds = NULL;
-irr::scene::ISceneManager *g_menucloudsmgr = NULL;
+scene::ISceneManager *g_menucloudsmgr = NULL;
// Constant for now
static constexpr const float cloud_size = BS * 64.0f;
@@ -352,7 +352,7 @@ void Clouds::update(const v3f &camera_p, const video::SColorf &color_diffuse)
// is the camera inside the cloud mesh?
m_camera_inside_cloud = false; // default
if (m_enable_3d) {
- float camera_height = camera_p.Y;
+ float camera_height = camera_p.Y - BS * m_camera_offset.Y;
if (camera_height >= m_box.MinEdge.Y &&
camera_height <= m_box.MaxEdge.Y) {
v2f camera_in_noise;
diff --git a/src/client/clouds.h b/src/client/clouds.h
index a4d810faa..6db88d93c 100644
--- a/src/client/clouds.h
+++ b/src/client/clouds.h
@@ -22,15 +22,14 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "irrlichttypes_extrabloated.h"
#include <iostream>
#include "constants.h"
-#include "cloudparams.h"
+#include "skyparams.h"
// Menu clouds
class Clouds;
extern Clouds *g_menuclouds;
// Scene manager used for menu clouds
-namespace irr{namespace scene{class ISceneManager;}}
-extern irr::scene::ISceneManager *g_menucloudsmgr;
+extern scene::ISceneManager *g_menucloudsmgr;
class Clouds : public scene::ISceneNode
{
diff --git a/src/client/content_cao.cpp b/src/client/content_cao.cpp
index 97ae9afc4..1d4636a08 100644
--- a/src/client/content_cao.cpp
+++ b/src/client/content_cao.cpp
@@ -20,7 +20,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "content_cao.h"
#include <IBillboardSceneNode.h>
#include <ICameraSceneNode.h>
-#include <ITextSceneNode.h>
#include <IMeshManipulator.h>
#include <IAnimatedMeshSceneNode.h>
#include "client/client.h"
@@ -28,7 +27,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "client/sound.h"
#include "client/tile.h"
#include "util/basic_macros.h"
-#include "util/numeric.h" // For IntervalLimiter & setPitchYawRoll
+#include "util/numeric.h"
#include "util/serialize.h"
#include "camera.h" // CameraModes
#include "collision.h"
@@ -172,6 +171,20 @@ static void updatePositionRecursive(scene::ISceneNode *node)
node->updateAbsolutePosition();
}
+static bool logOnce(const std::ostringstream &from, std::ostream &log_to)
+{
+ thread_local std::vector<u64> logged;
+
+ std::string message = from.str();
+ u64 hash = murmur_hash_64_ua(message.data(), message.length(), 0xBADBABE);
+
+ if (std::find(logged.begin(), logged.end(), hash) != logged.end())
+ return false;
+ logged.push_back(hash);
+ log_to << message << std::endl;
+ return true;
+}
+
/*
TestCAO
*/
@@ -189,7 +202,7 @@ public:
static ClientActiveObject* create(Client *client, ClientEnvironment *env);
- void addToScene(ITextureSource *tsrc);
+ void addToScene(ITextureSource *tsrc, scene::ISceneManager *smgr);
void removeFromScene(bool permanent);
void updateLight(u32 day_night_ratio);
void updateNodePos();
@@ -220,7 +233,7 @@ ClientActiveObject* TestCAO::create(Client *client, ClientEnvironment *env)
return new TestCAO(client, env);
}
-void TestCAO::addToScene(ITextureSource *tsrc)
+void TestCAO::addToScene(ITextureSource *tsrc, scene::ISceneManager *smgr)
{
if(m_node != NULL)
return;
@@ -249,7 +262,7 @@ void TestCAO::addToScene(ITextureSource *tsrc)
// Add to mesh
mesh->addMeshBuffer(buf);
buf->drop();
- m_node = RenderingEngine::get_scene_manager()->addMeshSceneNode(mesh, NULL);
+ m_node = smgr->addMeshSceneNode(mesh, NULL);
mesh->drop();
updateNodePos();
}
@@ -347,18 +360,6 @@ void GenericCAO::initialize(const std::string &data)
infostream<<"GenericCAO: Got init data"<<std::endl;
processInitData(data);
- if (m_is_player) {
- // Check if it's the current player
- LocalPlayer *player = m_env->getLocalPlayer();
- if (player && strcmp(player->getName(), m_name.c_str()) == 0) {
- m_is_local_player = true;
- m_is_visible = false;
- player->setCAO(this);
-
- m_prop.show_on_minimap = false;
- }
- }
-
m_enable_shaders = g_settings->getBool("enable_shaders");
}
@@ -381,6 +382,16 @@ void GenericCAO::processInitData(const std::string &data)
m_rotation = readV3F32(is);
m_hp = readU16(is);
+ if (m_is_player) {
+ // Check if it's the current player
+ LocalPlayer *player = m_env->getLocalPlayer();
+ if (player && strcmp(player->getName(), m_name.c_str()) == 0) {
+ m_is_local_player = true;
+ m_is_visible = false;
+ player->setCAO(this);
+ }
+ }
+
const u8 num_messages = readU8(is);
for (int i = 0; i < num_messages; i++) {
@@ -558,6 +569,9 @@ void GenericCAO::removeFromScene(bool permanent)
clearParentAttachment();
}
+ if (auto shadow = RenderingEngine::get_shadow_renderer())
+ shadow->removeNodeFromShadowList(getSceneNode());
+
if (m_meshnode) {
m_meshnode->remove();
m_meshnode->drop();
@@ -591,9 +605,9 @@ void GenericCAO::removeFromScene(bool permanent)
m_client->getMinimap()->removeMarker(&m_marker);
}
-void GenericCAO::addToScene(ITextureSource *tsrc)
+void GenericCAO::addToScene(ITextureSource *tsrc, scene::ISceneManager *smgr)
{
- m_smgr = RenderingEngine::get_scene_manager();
+ m_smgr = smgr;
if (getSceneNode() != NULL) {
return;
@@ -625,8 +639,7 @@ void GenericCAO::addToScene(ITextureSource *tsrc)
}
auto grabMatrixNode = [this] {
- m_matrixnode = RenderingEngine::get_scene_manager()->
- addDummyTransformationSceneNode();
+ m_matrixnode = m_smgr->addDummyTransformationSceneNode();
m_matrixnode->grab();
};
@@ -644,11 +657,11 @@ void GenericCAO::addToScene(ITextureSource *tsrc)
if (m_prop.visual == "sprite") {
grabMatrixNode();
- m_spritenode = RenderingEngine::get_scene_manager()->addBillboardSceneNode(
+ m_spritenode = m_smgr->addBillboardSceneNode(
m_matrixnode, v2f(1, 1), v3f(0,0,0), -1);
m_spritenode->grab();
m_spritenode->setMaterialTexture(0,
- tsrc->getTextureForMesh("unknown_node.png"));
+ tsrc->getTextureForMesh("no_texture.png"));
setSceneNodeMaterial(m_spritenode);
@@ -729,8 +742,7 @@ void GenericCAO::addToScene(ITextureSource *tsrc)
mesh->addMeshBuffer(buf);
buf->drop();
}
- m_meshnode = RenderingEngine::get_scene_manager()->
- addMeshSceneNode(mesh, m_matrixnode);
+ m_meshnode = m_smgr->addMeshSceneNode(mesh, m_matrixnode);
m_meshnode->grab();
mesh->drop();
// Set it to use the materials of the meshbuffers directly.
@@ -739,8 +751,7 @@ void GenericCAO::addToScene(ITextureSource *tsrc)
} else if (m_prop.visual == "cube") {
grabMatrixNode();
scene::IMesh *mesh = createCubeMesh(v3f(BS,BS,BS));
- m_meshnode = RenderingEngine::get_scene_manager()->
- addMeshSceneNode(mesh, m_matrixnode);
+ m_meshnode = m_smgr->addMeshSceneNode(mesh, m_matrixnode);
m_meshnode->grab();
mesh->drop();
@@ -753,11 +764,6 @@ void GenericCAO::addToScene(ITextureSource *tsrc)
grabMatrixNode();
scene::IAnimatedMesh *mesh = m_client->getMesh(m_prop.mesh, true);
if (mesh) {
- m_animated_meshnode = RenderingEngine::get_scene_manager()->
- addAnimatedMeshSceneNode(mesh, m_matrixnode);
- m_animated_meshnode->grab();
- mesh->drop(); // The scene node took hold of it
-
if (!checkMeshNormals(mesh)) {
infostream << "GenericCAO: recalculating normals for mesh "
<< m_prop.mesh << std::endl;
@@ -765,6 +771,9 @@ void GenericCAO::addToScene(ITextureSource *tsrc)
recalculateNormals(mesh, true, false);
}
+ m_animated_meshnode = m_smgr->addAnimatedMeshSceneNode(mesh, m_matrixnode);
+ m_animated_meshnode->grab();
+ mesh->drop(); // The scene node took hold of it
m_animated_meshnode->animateJoints(); // Needed for some animations
m_animated_meshnode->setScale(m_prop.visual_size);
@@ -795,8 +804,7 @@ void GenericCAO::addToScene(ITextureSource *tsrc)
infostream << "serialized form: " << m_prop.wield_item << std::endl;
item.deSerialize(m_prop.wield_item, m_client->idef());
}
- m_wield_meshnode = new WieldMeshSceneNode(
- RenderingEngine::get_scene_manager(), -1);
+ m_wield_meshnode = new WieldMeshSceneNode(m_smgr, -1);
m_wield_meshnode->setItem(item, m_client,
(m_prop.visual == "wielditem"));
@@ -811,10 +819,13 @@ void GenericCAO::addToScene(ITextureSource *tsrc)
if (m_reset_textures_timer < 0)
updateTextures(m_current_texture_modifier);
- scene::ISceneNode *node = getSceneNode();
+ if (scene::ISceneNode *node = getSceneNode()) {
+ if (m_matrixnode)
+ node->setParent(m_matrixnode);
- if (node && m_matrixnode)
- node->setParent(m_matrixnode);
+ if (auto shadow = RenderingEngine::get_shadow_renderer())
+ shadow->addNodeToShadowList(node);
+ }
updateNametag();
updateMarker();
@@ -824,6 +835,28 @@ void GenericCAO::addToScene(ITextureSource *tsrc)
updateAttachments();
setNodeLight(m_last_light);
updateMeshCulling();
+
+ if (m_animated_meshnode) {
+ u32 mat_count = m_animated_meshnode->getMaterialCount();
+ if (mat_count == 0 || m_prop.textures.empty()) {
+ // nothing
+ } else if (mat_count > m_prop.textures.size()) {
+ std::ostringstream oss;
+ oss << "GenericCAO::addToScene(): Model "
+ << m_prop.mesh << " loaded with " << mat_count
+ << " mesh buffers but only " << m_prop.textures.size()
+ << " texture(s) specifed, this is deprecated.";
+ logOnce(oss, warningstream);
+
+ video::ITexture *last = m_animated_meshnode->getMaterial(0).TextureLayer[0].Texture;
+ for (u32 i = 1; i < mat_count; i++) {
+ auto &layer = m_animated_meshnode->getMaterial(i).TextureLayer[0];
+ if (!layer.Texture)
+ layer.Texture = last;
+ last = layer.Texture;
+ }
+ }
+ }
}
void GenericCAO::updateLight(u32 day_night_ratio)
@@ -998,14 +1031,14 @@ void GenericCAO::step(float dtime, ClientEnvironment *env)
m_velocity = v3f(0,0,0);
m_acceleration = v3f(0,0,0);
const PlayerControl &controls = player->getPlayerControl();
+ f32 new_speed = player->local_animation_speed;
bool walking = false;
- if (controls.up || controls.down || controls.left || controls.right ||
- controls.forw_move_joystick_axis != 0.f ||
- controls.sidew_move_joystick_axis != 0.f)
+ if (controls.movement_speed > 0.001f) {
+ new_speed *= controls.movement_speed;
walking = true;
+ }
- f32 new_speed = player->local_animation_speed;
v2s32 new_anim = v2s32(0,0);
bool allow_update = false;
@@ -1017,7 +1050,7 @@ void GenericCAO::step(float dtime, ClientEnvironment *env)
g_settings->getBool("free_move") &&
m_client->checkLocalPrivilege("fly"))))
new_speed *= 1.5;
- // slowdown speed if sneeking
+ // slowdown speed if sneaking
if (controls.sneak && walking)
new_speed /= 2;
@@ -1074,7 +1107,7 @@ void GenericCAO::step(float dtime, ClientEnvironment *env)
}
removeFromScene(false);
- addToScene(m_client->tsrc());
+ addToScene(m_client->tsrc(), m_smgr);
// Attachments, part 2: Now that the parent has been refreshed, put its attachments back
for (u16 cao_id : m_attachment_child_ids) {
@@ -1291,7 +1324,7 @@ void GenericCAO::updateTextures(std::string mod)
if (m_spritenode) {
if (m_prop.visual == "sprite") {
- std::string texturestring = "unknown_node.png";
+ std::string texturestring = "no_texture.png";
if (!m_prop.textures.empty())
texturestring = m_prop.textures[0];
texturestring += mod;
@@ -1370,7 +1403,7 @@ void GenericCAO::updateTextures(std::string mod)
{
for (u32 i = 0; i < 6; ++i)
{
- std::string texturestring = "unknown_node.png";
+ std::string texturestring = "no_texture.png";
if(m_prop.textures.size() > i)
texturestring = m_prop.textures[i];
texturestring += mod;
@@ -1403,7 +1436,7 @@ void GenericCAO::updateTextures(std::string mod)
} else if (m_prop.visual == "upright_sprite") {
scene::IMesh *mesh = m_meshnode->getMesh();
{
- std::string tname = "unknown_object.png";
+ std::string tname = "no_texture.png";
if (!m_prop.textures.empty())
tname = m_prop.textures[0];
tname += mod;
@@ -1425,7 +1458,7 @@ void GenericCAO::updateTextures(std::string mod)
buf->getMaterial().setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
}
{
- std::string tname = "unknown_object.png";
+ std::string tname = "no_texture.png";
if (m_prop.textures.size() >= 2)
tname = m_prop.textures[1];
else if (!m_prop.textures.empty())
@@ -1473,11 +1506,8 @@ void GenericCAO::updateAnimation()
if (m_animated_meshnode->getAnimationSpeed() != m_animation_speed)
m_animated_meshnode->setAnimationSpeed(m_animation_speed);
m_animated_meshnode->setTransitionTime(m_animation_blend);
-// Requires Irrlicht 1.8 or greater
-#if (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR >= 8) || IRRLICHT_VERSION_MAJOR > 1
if (m_animated_meshnode->getLoopMode() != m_animation_loop)
m_animated_meshnode->setLoopMode(m_animation_loop);
-#endif
}
void GenericCAO::updateAnimationSpeed()
@@ -1493,10 +1523,10 @@ void GenericCAO::updateBonePosition()
if (m_bone_position.empty() || !m_animated_meshnode)
return;
- m_animated_meshnode->setJointMode(irr::scene::EJUOR_CONTROL); // To write positions to the mesh on render
+ m_animated_meshnode->setJointMode(scene::EJUOR_CONTROL); // To write positions to the mesh on render
for (auto &it : m_bone_position) {
std::string bone_name = it.first;
- irr::scene::IBoneSceneNode* bone = m_animated_meshnode->getJointNode(bone_name.c_str());
+ scene::IBoneSceneNode* bone = m_animated_meshnode->getJointNode(bone_name.c_str());
if (bone) {
bone->setPosition(it.second.X);
bone->setRotation(it.second.Y);
@@ -1505,7 +1535,7 @@ void GenericCAO::updateBonePosition()
// search through bones to find mistakenly rotated bones due to bug in Irrlicht
for (u32 i = 0; i < m_animated_meshnode->getJointCount(); ++i) {
- irr::scene::IBoneSceneNode *bone = m_animated_meshnode->getJointNode(i);
+ scene::IBoneSceneNode *bone = m_animated_meshnode->getJointNode(i);
if (!bone)
continue;
@@ -1727,6 +1757,7 @@ void GenericCAO::processMessage(const std::string &data)
m_tx_basepos = p;
m_anim_num_frames = num_frames;
+ m_anim_frame = 0;
m_anim_framelength = framelength;
m_tx_select_horiz_by_yawpitch = select_horiz_by_yawpitch;
@@ -1784,6 +1815,7 @@ void GenericCAO::processMessage(const std::string &data)
{
updateAnimation();
}
+ // FIXME: ^ This code is trash. It's also broken.
}
} else if (cmd == AO_CMD_SET_ANIMATION_SPEED) {
m_animation_speed = readF32(is);
@@ -1828,6 +1860,8 @@ void GenericCAO::processMessage(const std::string &data)
m_reset_textures_timer = 0.05;
if(damage >= 2)
m_reset_textures_timer += 0.05 * damage;
+ // Cap damage overlay to 1 second
+ m_reset_textures_timer = std::min(m_reset_textures_timer, 1.0f);
updateTextures(m_current_texture_modifier + m_prop.damage_texture_modifier);
}
}
@@ -1875,7 +1909,8 @@ bool GenericCAO::directReportPunch(v3f dir, const ItemStack *punchitem,
m_armor_groups,
toolcap,
punchitem,
- time_from_last_punch);
+ time_from_last_punch,
+ punchitem->wear);
if(result.did_punch && result.damage != 0)
{
@@ -1895,6 +1930,8 @@ bool GenericCAO::directReportPunch(v3f dir, const ItemStack *punchitem,
m_reset_textures_timer = 0.05;
if (result.damage >= 2)
m_reset_textures_timer += 0.05 * result.damage;
+ // Cap damage overlay to 1 second
+ m_reset_textures_timer = std::min(m_reset_textures_timer, 1.0f);
updateTextures(m_current_texture_modifier + m_prop.damage_texture_modifier);
}
}
@@ -1933,7 +1970,7 @@ void GenericCAO::updateMeshCulling()
return;
}
- irr::scene::ISceneNode *node = getSceneNode();
+ scene::ISceneNode *node = getSceneNode();
if (!node)
return;
diff --git a/src/client/content_cao.h b/src/client/content_cao.h
index 7c134fb48..4bbba9134 100644
--- a/src/client/content_cao.h
+++ b/src/client/content_cao.h
@@ -174,6 +174,8 @@ public:
const bool isImmortal();
+ inline const ObjectProperties &getProperties() const { return m_prop; }
+
scene::ISceneNode *getSceneNode() const;
scene::IAnimatedMeshSceneNode *getAnimatedMeshSceneNode() const;
@@ -234,7 +236,7 @@ public:
void removeFromScene(bool permanent);
- void addToScene(ITextureSource *tsrc);
+ void addToScene(ITextureSource *tsrc, scene::ISceneManager *smgr);
inline void expireVisuals()
{
diff --git a/src/client/content_mapblock.cpp b/src/client/content_mapblock.cpp
index 90284ecce..bb2d6398f 100644
--- a/src/client/content_mapblock.cpp
+++ b/src/client/content_mapblock.cpp
@@ -60,18 +60,16 @@ static constexpr u16 quad_indices[] = {0, 1, 2, 2, 3, 0};
const std::string MapblockMeshGenerator::raillike_groupname = "connect_to_raillike";
-MapblockMeshGenerator::MapblockMeshGenerator(MeshMakeData *input, MeshCollector *output)
+MapblockMeshGenerator::MapblockMeshGenerator(MeshMakeData *input, MeshCollector *output,
+ scene::IMeshManipulator *mm):
+ data(input),
+ collector(output),
+ nodedef(data->m_client->ndef()),
+ meshmanip(mm),
+ blockpos_nodes(data->m_blockpos * MAP_BLOCKSIZE)
{
- data = input;
- collector = output;
-
- nodedef = data->m_client->ndef();
- meshmanip = RenderingEngine::get_scene_manager()->getMeshManipulator();
-
enable_mesh_cache = g_settings->getBool("enable_mesh_cache") &&
!data->m_smooth_lighting; // Mesh cache is not supported with smooth lighting
-
- blockpos_nodes = data->m_blockpos * MAP_BLOCKSIZE;
}
void MapblockMeshGenerator::useTile(int index, u8 set_flags, u8 reset_flags, bool special)
@@ -960,15 +958,43 @@ void MapblockMeshGenerator::drawPlantlikeQuad(float rotation, float quad_offset,
vertex.rotateXZBy(rotation + rotate_degree);
vertex += offset;
}
+
+ u8 wall = n.getWallMounted(nodedef);
+ if (wall != DWM_YN) {
+ for (v3f &vertex : vertices) {
+ switch (wall) {
+ case DWM_YP:
+ vertex.rotateYZBy(180);
+ vertex.rotateXZBy(180);
+ break;
+ case DWM_XP:
+ vertex.rotateXYBy(90);
+ break;
+ case DWM_XN:
+ vertex.rotateXYBy(-90);
+ vertex.rotateYZBy(180);
+ break;
+ case DWM_ZP:
+ vertex.rotateYZBy(-90);
+ vertex.rotateXYBy(90);
+ break;
+ case DWM_ZN:
+ vertex.rotateYZBy(90);
+ vertex.rotateXYBy(90);
+ break;
+ }
+ }
+ }
+
drawQuad(vertices, v3s16(0, 0, 0), plant_height);
}
-void MapblockMeshGenerator::drawPlantlike()
+void MapblockMeshGenerator::drawPlantlike(bool is_rooted)
{
draw_style = PLANT_STYLE_CROSS;
scale = BS / 2 * f->visual_scale;
offset = v3f(0, 0, 0);
- rotate_degree = 0;
+ rotate_degree = 0.0f;
random_offset_Y = false;
face_num = 0;
plant_height = 1.0;
@@ -988,7 +1014,8 @@ void MapblockMeshGenerator::drawPlantlike()
break;
case CPT2_DEGROTATE:
- rotate_degree = n.param2 * 2;
+ case CPT2_COLORED_DEGROTATE:
+ rotate_degree = 1.5f * n.getDegRotate(nodedef);
break;
case CPT2_LEVELED:
@@ -999,6 +1026,22 @@ void MapblockMeshGenerator::drawPlantlike()
break;
}
+ if (is_rooted) {
+ u8 wall = n.getWallMounted(nodedef);
+ switch (wall) {
+ case DWM_YP:
+ offset.Y += BS*2;
+ break;
+ case DWM_XN:
+ case DWM_XP:
+ case DWM_ZN:
+ case DWM_ZP:
+ offset.X += -BS;
+ offset.Y += BS;
+ break;
+ }
+ }
+
switch (draw_style) {
case PLANT_STYLE_CROSS:
drawPlantlikeQuad(46);
@@ -1049,7 +1092,7 @@ void MapblockMeshGenerator::drawPlantlikeRootedNode()
MapNode ntop = data->m_vmanip.getNodeNoEx(blockpos_nodes + p);
light = LightPair(getInteriorLight(ntop, 1, nodedef));
}
- drawPlantlike();
+ drawPlantlike(true);
p.Y--;
}
@@ -1343,6 +1386,7 @@ void MapblockMeshGenerator::drawMeshNode()
u8 facedir = 0;
scene::IMesh* mesh;
bool private_mesh; // as a grab/drop pair is not thread-safe
+ int degrotate = 0;
if (f->param_type_2 == CPT2_FACEDIR ||
f->param_type_2 == CPT2_COLORED_FACEDIR) {
@@ -1354,9 +1398,12 @@ void MapblockMeshGenerator::drawMeshNode()
facedir = n.getWallMounted(nodedef);
if (!enable_mesh_cache)
facedir = wallmounted_to_facedir[facedir];
+ } else if (f->param_type_2 == CPT2_DEGROTATE ||
+ f->param_type_2 == CPT2_COLORED_DEGROTATE) {
+ degrotate = n.getDegRotate(nodedef);
}
- if (!data->m_smooth_lighting && f->mesh_ptr[facedir]) {
+ if (!data->m_smooth_lighting && f->mesh_ptr[facedir] && !degrotate) {
// use cached meshes
private_mesh = false;
mesh = f->mesh_ptr[facedir];
@@ -1364,7 +1411,10 @@ void MapblockMeshGenerator::drawMeshNode()
// no cache, clone and rotate mesh
private_mesh = true;
mesh = cloneMesh(f->mesh_ptr[0]);
- rotateMeshBy6dFacedir(mesh, facedir);
+ if (facedir)
+ rotateMeshBy6dFacedir(mesh, facedir);
+ else if (degrotate)
+ rotateMeshXZby(mesh, 1.5f * degrotate);
recalculateBoundingBox(mesh);
meshmanip->recalculateNormals(mesh, true, false);
} else
diff --git a/src/client/content_mapblock.h b/src/client/content_mapblock.h
index 487d84a07..7344f05ee 100644
--- a/src/client/content_mapblock.h
+++ b/src/client/content_mapblock.h
@@ -139,14 +139,14 @@ public:
// plantlike-specific
PlantlikeStyle draw_style;
v3f offset;
- int rotate_degree;
+ float rotate_degree;
bool random_offset_Y;
int face_num;
float plant_height;
void drawPlantlikeQuad(float rotation, float quad_offset = 0,
bool offset_top_only = false);
- void drawPlantlike();
+ void drawPlantlike(bool is_rooted = false);
// firelike-specific
void drawFirelikeQuad(float rotation, float opening_angle,
@@ -172,7 +172,8 @@ public:
void drawNode();
public:
- MapblockMeshGenerator(MeshMakeData *input, MeshCollector *output);
+ MapblockMeshGenerator(MeshMakeData *input, MeshCollector *output,
+ scene::IMeshManipulator *mm);
void generate();
void renderSingle(content_t node, u8 param2 = 0x00);
};
diff --git a/src/client/fontengine.cpp b/src/client/fontengine.cpp
index 47218c0d9..ad8305b45 100644
--- a/src/client/fontengine.cpp
+++ b/src/client/fontengine.cpp
@@ -24,10 +24,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "porting.h"
#include "filesys.h"
#include "gettext.h"
-
-#if USE_FREETYPE
#include "irrlicht_changes/CGUITTFont.h"
-#endif
/** maximum size distance for getting a "similar" font size */
#define MAX_FONT_SIZE_OFFSET 10
@@ -45,9 +42,8 @@ static void font_setting_changed(const std::string &name, void *userdata)
FontEngine::FontEngine(gui::IGUIEnvironment* env) :
m_env(env)
{
-
for (u32 &i : m_default_size) {
- i = (FontMode) FONT_SIZE_UNSPECIFIED;
+ i = FONT_SIZE_UNSPECIFIED;
}
assert(g_settings != NULL); // pre-condition
@@ -56,28 +52,19 @@ FontEngine::FontEngine(gui::IGUIEnvironment* env) :
readSettings();
- if (m_currentMode == FM_Standard) {
- g_settings->registerChangedCallback("font_size", font_setting_changed, NULL);
- g_settings->registerChangedCallback("font_bold", font_setting_changed, NULL);
- g_settings->registerChangedCallback("font_italic", font_setting_changed, NULL);
- g_settings->registerChangedCallback("font_path", font_setting_changed, NULL);
- g_settings->registerChangedCallback("font_path_bold", font_setting_changed, NULL);
- g_settings->registerChangedCallback("font_path_italic", font_setting_changed, NULL);
- g_settings->registerChangedCallback("font_path_bolditalic", font_setting_changed, NULL);
- g_settings->registerChangedCallback("font_shadow", font_setting_changed, NULL);
- g_settings->registerChangedCallback("font_shadow_alpha", font_setting_changed, NULL);
- }
- else if (m_currentMode == FM_Fallback) {
- g_settings->registerChangedCallback("fallback_font_size", font_setting_changed, NULL);
- g_settings->registerChangedCallback("fallback_font_path", font_setting_changed, NULL);
- g_settings->registerChangedCallback("fallback_font_shadow", font_setting_changed, NULL);
- g_settings->registerChangedCallback("fallback_font_shadow_alpha", font_setting_changed, NULL);
- }
+ const char *settings[] = {
+ "font_size", "font_bold", "font_italic", "font_size_divisible_by",
+ "mono_font_size", "mono_font_size_divisible_by",
+ "font_shadow", "font_shadow_alpha",
+ "font_path", "font_path_bold", "font_path_italic", "font_path_bold_italic",
+ "mono_font_path", "mono_font_path_bold", "mono_font_path_italic",
+ "mono_font_path_bold_italic",
+ "fallback_font_path",
+ "screen_dpi", "gui_scaling",
+ };
- g_settings->registerChangedCallback("mono_font_path", font_setting_changed, NULL);
- g_settings->registerChangedCallback("mono_font_size", font_setting_changed, NULL);
- g_settings->registerChangedCallback("screen_dpi", font_setting_changed, NULL);
- g_settings->registerChangedCallback("gui_scaling", font_setting_changed, NULL);
+ for (auto name : settings)
+ g_settings->registerChangedCallback(name, font_setting_changed, NULL);
}
/******************************************************************************/
@@ -89,11 +76,13 @@ FontEngine::~FontEngine()
/******************************************************************************/
void FontEngine::cleanCache()
{
+ RecursiveMutexAutoLock l(m_font_mutex);
+
for (auto &font_cache_it : m_font_cache) {
for (auto &font_it : font_cache_it) {
font_it.second->drop();
- font_it.second = NULL;
+ font_it.second = nullptr;
}
font_cache_it.clear();
}
@@ -102,14 +91,15 @@ void FontEngine::cleanCache()
/******************************************************************************/
irr::gui::IGUIFont *FontEngine::getFont(FontSpec spec)
{
+ return getFont(spec, false);
+}
+
+irr::gui::IGUIFont *FontEngine::getFont(FontSpec spec, bool may_fail)
+{
if (spec.mode == FM_Unspecified) {
spec.mode = m_currentMode;
- } else if (m_currentMode == FM_Simple) {
- // Freetype disabled -> Force simple mode
- spec.mode = (spec.mode == FM_Mono ||
- spec.mode == FM_SimpleMono) ?
- FM_SimpleMono : FM_Simple;
- // Support for those could be added, but who cares?
+ } else if (spec.mode == _FM_Fallback) {
+ // Fallback font doesn't support these
spec.bold = false;
spec.italic = false;
}
@@ -118,17 +108,22 @@ irr::gui::IGUIFont *FontEngine::getFont(FontSpec spec)
if (spec.size == FONT_SIZE_UNSPECIFIED)
spec.size = m_default_size[spec.mode];
+ RecursiveMutexAutoLock l(m_font_mutex);
+
const auto &cache = m_font_cache[spec.getHash()];
auto it = cache.find(spec.size);
if (it != cache.end())
return it->second;
// Font does not yet exist
- gui::IGUIFont *font = nullptr;
- if (spec.mode == FM_Simple || spec.mode == FM_SimpleMono)
- font = initSimpleFont(spec);
- else
- font = initFont(spec);
+ gui::IGUIFont *font = initFont(spec);
+
+ if (!font && !may_fail) {
+ errorstream << "Minetest cannot continue without a valid font. "
+ "Please correct the 'font_path' setting or install the font "
+ "file in the proper location." << std::endl;
+ abort();
+ }
m_font_cache[spec.getHash()][spec.size] = font;
@@ -138,13 +133,7 @@ irr::gui::IGUIFont *FontEngine::getFont(FontSpec spec)
/******************************************************************************/
unsigned int FontEngine::getTextHeight(const FontSpec &spec)
{
- irr::gui::IGUIFont *font = getFont(spec);
-
- // use current skin font as fallback
- if (font == NULL) {
- font = m_env->getSkin()->getFont();
- }
- FATAL_ERROR_IF(font == NULL, "Could not get skin font");
+ gui::IGUIFont *font = getFont(spec);
return font->getDimension(L"Some unimportant example String").Height;
}
@@ -152,28 +141,15 @@ unsigned int FontEngine::getTextHeight(const FontSpec &spec)
/******************************************************************************/
unsigned int FontEngine::getTextWidth(const std::wstring &text, const FontSpec &spec)
{
- irr::gui::IGUIFont *font = getFont(spec);
-
- // use current skin font as fallback
- if (font == NULL) {
- font = m_env->getSkin()->getFont();
- }
- FATAL_ERROR_IF(font == NULL, "Could not get font");
+ gui::IGUIFont *font = getFont(spec);
return font->getDimension(text.c_str()).Width;
}
-
/** get line height for a specific font (including empty room between lines) */
unsigned int FontEngine::getLineHeight(const FontSpec &spec)
{
- irr::gui::IGUIFont *font = getFont(spec);
-
- // use current skin font as fallback
- if (font == NULL) {
- font = m_env->getSkin()->getFont();
- }
- FATAL_ERROR_IF(font == NULL, "Could not get font");
+ gui::IGUIFont *font = getFont(spec);
return font->getDimension(L"Some unimportant example String").Height
+ font->getKerningHeight();
@@ -187,13 +163,6 @@ unsigned int FontEngine::getDefaultFontSize()
unsigned int FontEngine::getFontSize(FontMode mode)
{
- if (m_currentMode == FM_Simple) {
- if (mode == FM_Mono || mode == FM_SimpleMono)
- return m_default_size[FM_SimpleMono];
- else
- return m_default_size[FM_Simple];
- }
-
if (mode == FM_Unspecified)
return m_default_size[FM_Standard];
@@ -203,31 +172,12 @@ unsigned int FontEngine::getFontSize(FontMode mode)
/******************************************************************************/
void FontEngine::readSettings()
{
- if (USE_FREETYPE && g_settings->getBool("freetype")) {
- m_default_size[FM_Standard] = g_settings->getU16("font_size");
- m_default_size[FM_Fallback] = g_settings->getU16("fallback_font_size");
- m_default_size[FM_Mono] = g_settings->getU16("mono_font_size");
-
- /*~ DO NOT TRANSLATE THIS LITERALLY!
- This is a special string. Put either "no" or "yes"
- into the translation field (literally).
- Choose "yes" if the language requires use of the fallback
- font, "no" otherwise.
- The fallback font is (normally) required for languages with
- non-Latin script, like Chinese.
- When in doubt, test your translation. */
- m_currentMode = is_yes(gettext("needs_fallback_font")) ?
- FM_Fallback : FM_Standard;
-
- m_default_bold = g_settings->getBool("font_bold");
- m_default_italic = g_settings->getBool("font_italic");
-
- } else {
- m_currentMode = FM_Simple;
- }
+ m_default_size[FM_Standard] = g_settings->getU16("font_size");
+ m_default_size[_FM_Fallback] = g_settings->getU16("font_size");
+ m_default_size[FM_Mono] = g_settings->getU16("mono_font_size");
- m_default_size[FM_Simple] = g_settings->getU16("font_size");
- m_default_size[FM_SimpleMono] = g_settings->getU16("mono_font_size");
+ m_default_bold = g_settings->getBool("font_bold");
+ m_default_italic = g_settings->getBool("font_italic");
cleanCache();
updateFontCache();
@@ -238,22 +188,9 @@ void FontEngine::readSettings()
void FontEngine::updateSkin()
{
gui::IGUIFont *font = getFont();
+ assert(font);
- if (font)
- m_env->getSkin()->setFont(font);
- else
- errorstream << "FontEngine: Default font file: " <<
- "\n\t\"" << g_settings->get("font_path") << "\"" <<
- "\n\trequired for current screen configuration was not found" <<
- " or was invalid file format." <<
- "\n\tUsing irrlicht default font." << std::endl;
-
- // If we did fail to create a font our own make irrlicht find a default one
- font = m_env->getSkin()->getFont();
- FATAL_ERROR_IF(font == NULL, "Could not create/get font");
-
- u32 text_height = font->getDimension(L"Hello, world!").Height;
- infostream << "FontEngine: measured text_height=" << text_height << std::endl;
+ m_env->getSkin()->setFont(font);
}
/******************************************************************************/
@@ -271,18 +208,8 @@ gui::IGUIFont *FontEngine::initFont(const FontSpec &spec)
assert(spec.size != FONT_SIZE_UNSPECIFIED);
std::string setting_prefix = "";
-
- switch (spec.mode) {
- case FM_Fallback:
- setting_prefix = "fallback_";
- break;
- case FM_Mono:
- case FM_SimpleMono:
- setting_prefix = "mono_";
- break;
- default:
- break;
- }
+ if (spec.mode == FM_Mono)
+ setting_prefix = "mono_";
std::string setting_suffix = "";
if (spec.bold)
@@ -290,121 +217,52 @@ gui::IGUIFont *FontEngine::initFont(const FontSpec &spec)
if (spec.italic)
setting_suffix.append("_italic");
- u32 size = std::floor(RenderingEngine::getDisplayDensity() *
- g_settings->getFloat("gui_scaling") * spec.size);
+ u32 size = std::max<u32>(spec.size * RenderingEngine::getDisplayDensity() *
+ g_settings->getFloat("gui_scaling"), 1);
- if (size == 0) {
- errorstream << "FontEngine: attempt to use font size 0" << std::endl;
- errorstream << " display density: " << RenderingEngine::getDisplayDensity() << std::endl;
- abort();
+ // Constrain the font size to a certain multiple, if necessary
+ u16 divisible_by = g_settings->getU16(setting_prefix + "font_size_divisible_by");
+ if (divisible_by > 1) {
+ size = std::max<u32>(
+ std::round((double)size / divisible_by) * divisible_by, divisible_by);
}
+ sanity_check(size != 0);
+
u16 font_shadow = 0;
u16 font_shadow_alpha = 0;
g_settings->getU16NoEx(setting_prefix + "font_shadow", font_shadow);
g_settings->getU16NoEx(setting_prefix + "font_shadow_alpha",
font_shadow_alpha);
- std::string wanted_font_path;
- wanted_font_path = g_settings->get(setting_prefix + "font_path" + setting_suffix);
+ std::string path_setting;
+ if (spec.mode == _FM_Fallback)
+ path_setting = "fallback_font_path";
+ else
+ path_setting = setting_prefix + "font_path" + setting_suffix;
std::string fallback_settings[] = {
- wanted_font_path,
- g_settings->get("fallback_font_path"),
- Settings::getLayer(SL_DEFAULTS)->get(setting_prefix + "font_path")
+ g_settings->get(path_setting),
+ Settings::getLayer(SL_DEFAULTS)->get(path_setting)
};
-#if USE_FREETYPE
for (const std::string &font_path : fallback_settings) {
- irr::gui::IGUIFont *font = gui::CGUITTFont::createTTFont(m_env,
+ gui::CGUITTFont *font = gui::CGUITTFont::createTTFont(m_env,
font_path.c_str(), size, true, true, font_shadow,
font_shadow_alpha);
- if (font)
- return font;
-
- errorstream << "FontEngine: Cannot load '" << font_path <<
+ if (!font) {
+ errorstream << "FontEngine: Cannot load '" << font_path <<
"'. Trying to fall back to another path." << std::endl;
- }
-
-
- // give up
- errorstream << "minetest can not continue without a valid font. "
- "Please correct the 'font_path' setting or install the font "
- "file in the proper location" << std::endl;
-#else
- errorstream << "FontEngine: Tried to load freetype fonts but Minetest was"
- " not compiled with that library." << std::endl;
-#endif
- abort();
-}
-
-/** initialize a font without freetype */
-gui::IGUIFont *FontEngine::initSimpleFont(const FontSpec &spec)
-{
- assert(spec.mode == FM_Simple || spec.mode == FM_SimpleMono);
- assert(spec.size != FONT_SIZE_UNSPECIFIED);
-
- const std::string &font_path = g_settings->get(
- (spec.mode == FM_SimpleMono) ? "mono_font_path" : "font_path");
-
- size_t pos_dot = font_path.find_last_of('.');
- std::string basename = font_path;
- std::string ending = lowercase(font_path.substr(pos_dot));
-
- if (ending == ".ttf") {
- errorstream << "FontEngine: Found font \"" << font_path
- << "\" but freetype is not available." << std::endl;
- return nullptr;
- }
-
- if (ending == ".xml" || ending == ".png")
- basename = font_path.substr(0, pos_dot);
-
- u32 size = std::floor(
- RenderingEngine::getDisplayDensity() *
- g_settings->getFloat("gui_scaling") *
- spec.size);
-
- irr::gui::IGUIFont *font = nullptr;
- std::string font_extensions[] = { ".png", ".xml" };
-
- // Find nearest matching font scale
- // Does a "zig-zag motion" (positibe/negative), from 0 to MAX_FONT_SIZE_OFFSET
- for (s32 zoffset = 0; zoffset < MAX_FONT_SIZE_OFFSET * 2; zoffset++) {
- std::stringstream path;
-
- // LSB to sign
- s32 sign = (zoffset & 1) ? -1 : 1;
- s32 offset = zoffset >> 1;
-
- for (const std::string &ext : font_extensions) {
- path.str(""); // Clear
- path << basename << "_" << (size + offset * sign) << ext;
-
- if (!fs::PathExists(path.str()))
- continue;
-
- font = m_env->getFont(path.str().c_str());
-
- if (font) {
- verbosestream << "FontEngine: found font: " << path.str() << std::endl;
- break;
- }
+ continue;
}
- if (font)
- break;
- }
-
- // try name direct
- if (font == NULL) {
- if (fs::PathExists(font_path)) {
- font = m_env->getFont(font_path.c_str());
- if (font)
- verbosestream << "FontEngine: found font: " << font_path << std::endl;
+ if (spec.mode != _FM_Fallback) {
+ FontSpec spec2(spec);
+ spec2.mode = _FM_Fallback;
+ font->setFallback(getFont(spec2, true));
}
+ return font;
}
-
- return font;
+ return nullptr;
}
diff --git a/src/client/fontengine.h b/src/client/fontengine.h
index e27ef60e9..78608e517 100644
--- a/src/client/fontengine.h
+++ b/src/client/fontengine.h
@@ -20,22 +20,20 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#pragma once
#include <map>
-#include <vector>
#include "util/basic_macros.h"
#include "irrlichttypes.h"
#include <IGUIFont.h>
#include <IGUISkin.h>
#include <IGUIEnvironment.h>
#include "settings.h"
+#include "threading/mutex_auto_lock.h"
#define FONT_SIZE_UNSPECIFIED 0xFFFFFFFF
enum FontMode : u8 {
FM_Standard = 0,
FM_Mono,
- FM_Fallback,
- FM_Simple,
- FM_SimpleMono,
+ _FM_Fallback, // do not use directly
FM_MaxMode,
FM_Unspecified
};
@@ -47,7 +45,7 @@ struct FontSpec {
bold(bold),
italic(italic) {}
- u16 getHash()
+ u16 getHash() const
{
return (mode << 2) | (static_cast<u8>(bold) << 1) | static_cast<u8>(italic);
}
@@ -132,15 +130,14 @@ public:
void readSettings();
private:
+ irr::gui::IGUIFont *getFont(FontSpec spec, bool may_fail);
+
/** update content of font cache in case of a setting change made it invalid */
void updateFontCache();
- /** initialize a new font */
+ /** initialize a new TTF font */
gui::IGUIFont *initFont(const FontSpec &spec);
- /** initialize a font without freetype */
- gui::IGUIFont *initSimpleFont(const FontSpec &spec);
-
/** update current minetest skin with font changes */
void updateSkin();
@@ -150,6 +147,9 @@ private:
/** pointer to irrlicht gui environment */
gui::IGUIEnvironment* m_env = nullptr;
+ /** mutex used to protect font init and cache */
+ std::recursive_mutex m_font_mutex;
+
/** internal storage for caching fonts of different size */
std::map<unsigned int, irr::gui::IGUIFont*> m_font_cache[FM_MaxMode << 2];
@@ -160,8 +160,8 @@ private:
bool m_default_bold = false;
bool m_default_italic = false;
- /** current font engine mode */
- FontMode m_currentMode = FM_Standard;
+ /** default font engine mode (fixed) */
+ static const FontMode m_currentMode = FM_Standard;
DISABLE_CLASS_COPY(FontEngine);
};
diff --git a/src/client/game.cpp b/src/client/game.cpp
index a88972d53..4337d308e 100644
--- a/src/client/game.cpp
+++ b/src/client/game.cpp
@@ -68,6 +68,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "util/pointedthing.h"
#include "util/quicktune_shortcutter.h"
#include "irrlicht_changes/static_text.h"
+#include "irr_ptr.h"
#include "version.h"
#include "script/scripting_client.h"
#include "hud.h"
@@ -399,12 +400,7 @@ public:
};
-// before 1.8 there isn't a "integer interface", only float
-#if (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 8)
-typedef f32 SamplerLayer_t;
-#else
typedef s32 SamplerLayer_t;
-#endif
class GameGlobalShaderConstantSetter : public IShaderConstantSetter
@@ -512,38 +508,20 @@ public:
float eye_position_array[3];
v3f epos = m_client->getEnv().getLocalPlayer()->getEyePosition();
-#if (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 8)
- eye_position_array[0] = epos.X;
- eye_position_array[1] = epos.Y;
- eye_position_array[2] = epos.Z;
-#else
epos.getAs3Values(eye_position_array);
-#endif
m_eye_position_pixel.set(eye_position_array, services);
m_eye_position_vertex.set(eye_position_array, services);
if (m_client->getMinimap()) {
float minimap_yaw_array[3];
v3f minimap_yaw = m_client->getMinimap()->getYawVec();
-#if (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 8)
- minimap_yaw_array[0] = minimap_yaw.X;
- minimap_yaw_array[1] = minimap_yaw.Y;
- minimap_yaw_array[2] = minimap_yaw.Z;
-#else
minimap_yaw.getAs3Values(minimap_yaw_array);
-#endif
m_minimap_yaw.set(minimap_yaw_array, services);
}
float camera_offset_array[3];
v3f offset = intToFloat(m_client->getCamera()->getOffset(), BS);
-#if (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 8)
- camera_offset_array[0] = offset.X;
- camera_offset_array[1] = offset.Y;
- camera_offset_array[2] = offset.Z;
-#else
offset.getAs3Values(camera_offset_array);
-#endif
m_camera_offset_pixel.set(camera_offset_array, services);
m_camera_offset_vertex.set(camera_offset_array, services);
@@ -588,7 +566,7 @@ public:
}
};
-#ifdef __ANDROID__
+#ifdef HAVE_TOUCHSCREENGUI
#define SIZE_TAG "size[11,5.5]"
#else
#define SIZE_TAG "size[11,5.5,true]" // Fixed size on desktop
@@ -597,10 +575,19 @@ public:
/****************************************************************************
****************************************************************************/
-const float object_hit_delay = 0.2;
+const static float object_hit_delay = 0.2;
struct FpsControl {
- u32 last_time, busy_time, sleep_time;
+ FpsControl() : last_time(0), busy_time(0), sleep_time(0) {}
+
+ void reset();
+
+ void limit(IrrlichtDevice *device, f32 *dtime);
+
+ u32 getBusyMs() const { return busy_time / 1000; }
+
+ // all values in microseconds (us)
+ u64 last_time, busy_time, sleep_time;
};
@@ -650,6 +637,8 @@ struct ClientEventHandler
THE GAME
****************************************************************************/
+using PausedNodesList = std::vector<std::pair<irr_ptr<scene::IAnimatedMeshSceneNode>, float>>;
+
/* This is not intended to be a public class. If a public class becomes
* desirable then it may be better to create another 'wrapper' class that
* hides most of the stuff in this class (nothing in this class is required
@@ -662,6 +651,7 @@ public:
bool startup(bool *kill,
InputHandler *input,
+ RenderingEngine *rendering_engine,
const GameStartData &game_params,
std::string &error_message,
bool *reconnect,
@@ -672,8 +662,6 @@ public:
protected:
- void extendedResourceCleanup();
-
// Basic initialisation
bool init(const std::string &map_dir, const std::string &address,
u16 port, const SubgameSpec &gamespec);
@@ -697,6 +685,7 @@ protected:
bool handleCallbacks();
void processQueues();
void updateProfilers(const RunStats &stats, const FpsControl &draw_times, f32 dtime);
+ void updateDebugState();
void updateStats(RunStats *stats, const FpsControl &draw_times, f32 dtime);
void updateProfilerGraphs(ProfilerGraph *graph);
@@ -714,6 +703,7 @@ protected:
void toggleFast();
void toggleNoClip();
void toggleCinematic();
+ void toggleBlockBounds();
void toggleAutoforward();
void toggleMinimap(bool shift_pressed);
@@ -731,7 +721,7 @@ protected:
void updatePlayerControl(const CameraOrientation &cam);
void step(f32 *dtime);
void processClientEvents(CameraOrientation *cam);
- void updateCamera(u32 busy_time, f32 dtime);
+ void updateCamera(f32 dtime);
void updateSound(f32 dtime);
void processPlayerInteraction(f32 dtime, bool show_hud, bool show_debug);
/*!
@@ -759,10 +749,9 @@ protected:
const ItemStack &selected_item, const ItemStack &hand_item, f32 dtime);
void updateFrame(ProfilerGraph *graph, RunStats *stats, f32 dtime,
const CameraOrientation &cam);
+ void updateShadows();
// Misc
- void limitFps(FpsControl *fps_timings, f32 *dtime);
-
void showOverlayMessage(const char *msg, float dtime, int percent,
bool draw_clouds = true);
@@ -799,6 +788,9 @@ private:
void showDeathFormspec();
void showPauseMenu();
+ void pauseAnimation();
+ void resumeAnimation();
+
// ClientEvent handlers
void handleClientEvent_None(ClientEvent *event, CameraOrientation *cam);
void handleClientEvent_PlayerDamage(ClientEvent *event, CameraOrientation *cam);
@@ -819,13 +811,15 @@ private:
CameraOrientation *cam);
void handleClientEvent_CloudParams(ClientEvent *event, CameraOrientation *cam);
- void updateChat(f32 dtime, const v2u32 &screensize);
+ void updateChat(f32 dtime);
bool nodePlacement(const ItemDefinition &selected_def, const ItemStack &selected_item,
const v3s16 &nodepos, const v3s16 &neighbourpos, const PointedThing &pointed,
const NodeMetadata *meta);
static const ClientEventHandler clientEventHandler[CLIENTEVENT_MAX];
+ f32 getSensitivityScaleFactor() const;
+
InputHandler *input = nullptr;
Client *client = nullptr;
@@ -870,12 +864,14 @@ private:
these items (e.g. device)
*/
IrrlichtDevice *device;
+ RenderingEngine *m_rendering_engine;
video::IVideoDriver *driver;
scene::ISceneManager *smgr;
bool *kill;
std::string *error_message;
bool *reconnect_requested;
scene::ISceneNode *skybox;
+ PausedNodesList paused_animated_nodes;
bool simple_singleplayer_mode;
/* End 'cache' */
@@ -912,8 +908,10 @@ private:
bool m_does_lost_focus_pause_game = false;
int m_reset_HW_buffer_counter = 0;
-#ifdef __ANDROID__
+#ifdef HAVE_TOUCHSCREENGUI
bool m_cache_hold_aux1;
+#endif
+#ifdef __ANDROID__
bool m_android_chat_open;
#endif
};
@@ -951,7 +949,7 @@ Game::Game() :
readSettings();
-#ifdef __ANDROID__
+#ifdef HAVE_TOUCHSCREENGUI
m_cache_hold_aux1 = false; // This is initialised properly later
#endif
@@ -982,7 +980,7 @@ Game::~Game()
delete itemdef_manager;
delete draw_control;
- extendedResourceCleanup();
+ clearTextureNameCache();
g_settings->deregisterChangedCallback("doubletap_jump",
&settingChangedCallback, this);
@@ -1010,6 +1008,7 @@ Game::~Game()
bool Game::startup(bool *kill,
InputHandler *input,
+ RenderingEngine *rendering_engine,
const GameStartData &start_data,
std::string &error_message,
bool *reconnect,
@@ -1017,21 +1016,21 @@ bool Game::startup(bool *kill,
{
// "cache"
- this->device = RenderingEngine::get_raw_device();
+ m_rendering_engine = rendering_engine;
+ device = m_rendering_engine->get_raw_device();
this->kill = kill;
this->error_message = &error_message;
- this->reconnect_requested = reconnect;
+ reconnect_requested = reconnect;
this->input = input;
this->chat_backend = chat_backend;
- this->simple_singleplayer_mode = start_data.isSinglePlayer();
+ simple_singleplayer_mode = start_data.isSinglePlayer();
input->keycache.populate();
driver = device->getVideoDriver();
- smgr = RenderingEngine::get_scene_manager();
+ smgr = m_rendering_engine->get_scene_manager();
- RenderingEngine::get_scene_manager()->getParameters()->
- setAttribute(scene::OBJ_LOADER_IGNORE_MATERIAL_FILES, true);
+ smgr->getParameters()->setAttribute(scene::OBJ_LOADER_IGNORE_MATERIAL_FILES, true);
// Reinit runData
runData = GameRunData();
@@ -1052,7 +1051,7 @@ bool Game::startup(bool *kill,
if (!createClient(start_data))
return false;
- RenderingEngine::initialize(client, hud);
+ m_rendering_engine->initialize(client, hud);
return true;
}
@@ -1064,18 +1063,18 @@ void Game::run()
RunStats stats = { 0 };
CameraOrientation cam_view_target = { 0 };
CameraOrientation cam_view = { 0 };
- FpsControl draw_times = { 0 };
+ FpsControl draw_times;
f32 dtime; // in seconds
/* Clear the profiler */
Profiler::GraphValues dummyvalues;
g_profiler->graphGet(dummyvalues);
- draw_times.last_time = RenderingEngine::get_timer_time();
+ draw_times.reset();
set_light_table(g_settings->getFloat("display_gamma"));
-#ifdef __ANDROID__
+#ifdef HAVE_TOUCHSCREENGUI
m_cache_hold_aux1 = g_settings->getBool("fast_move")
&& client->checkPrivilege("fast");
#endif
@@ -1083,12 +1082,12 @@ void Game::run()
irr::core::dimension2d<u32> previous_screen_size(g_settings->getU16("screen_w"),
g_settings->getU16("screen_h"));
- while (RenderingEngine::run()
+ while (m_rendering_engine->run()
&& !(*kill || g_gamecallback->shutdown_requested
|| (server && server->isShutdownRequested()))) {
const irr::core::dimension2d<u32> &current_screen_size =
- RenderingEngine::get_video_driver()->getScreenSize();
+ m_rendering_engine->get_video_driver()->getScreenSize();
// Verify if window size has changed and save it if it's the case
// Ensure evaluating settings->getBool after verifying screensize
// First condition is cheaper
@@ -1101,9 +1100,9 @@ void Game::run()
}
// Calculate dtime =
- // RenderingEngine::run() from this iteration
+ // m_rendering_engine->run() from this iteration
// + Sleep time until the wanted FPS are reached
- limitFps(&draw_times, &dtime);
+ draw_times.limit(device, &dtime);
// Prepare render data for next iteration
@@ -1120,6 +1119,7 @@ void Game::run()
m_game_ui->clearInfoText();
hud->resizeHotbar();
+
updateProfilers(stats, draw_times, dtime);
processUserInput(dtime);
// Update camera before player movement to avoid camera lag of one frame
@@ -1131,10 +1131,11 @@ void Game::run()
updatePlayerControl(cam_view);
step(&dtime);
processClientEvents(&cam_view_target);
- updateCamera(draw_times.busy_time, dtime);
+ updateDebugState();
+ updateCamera(dtime);
updateSound(dtime);
processPlayerInteraction(dtime, m_game_ui->m_flags.show_hud,
- m_game_ui->m_flags.show_debug);
+ m_game_ui->m_flags.show_basic_debug);
updateFrame(&graph, &stats, dtime, cam_view);
updateProfilerGraphs(&graph);
@@ -1150,7 +1151,7 @@ void Game::run()
void Game::shutdown()
{
- RenderingEngine::finalize();
+ m_rendering_engine->finalize();
#if IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR <= 8
if (g_settings->get("3d_mode") == "pageflip") {
driver->setRenderTarget(irr::video::ERT_STEREO_BOTH_BUFFERS);
@@ -1288,9 +1289,8 @@ bool Game::createSingleplayerServer(const std::string &map_dir,
}
if (bind_addr.isIPv6() && !g_settings->getBool("enable_ipv6")) {
- *error_message = "Unable to listen on " +
- bind_addr.serializeString() +
- " because IPv6 is disabled";
+ *error_message = fmtgettext("Unable to listen on %s because IPv6 is disabled",
+ bind_addr.serializeString().c_str());
errorstream << *error_message << std::endl;
return false;
}
@@ -1306,7 +1306,7 @@ bool Game::createClient(const GameStartData &start_data)
{
showOverlayMessage(N_("Creating client..."), 0, 10);
- draw_control = new MapDrawControl;
+ draw_control = new MapDrawControl();
if (!draw_control)
return false;
@@ -1323,7 +1323,7 @@ bool Game::createClient(const GameStartData &start_data)
if (!could_connect) {
if (error_message->empty() && !connect_aborted) {
// Should not happen if error messages are set properly
- *error_message = "Connection failed for unknown reason";
+ *error_message = gettext("Connection failed for unknown reason");
errorstream << *error_message << std::endl;
}
return false;
@@ -1332,7 +1332,7 @@ bool Game::createClient(const GameStartData &start_data)
if (!getServerContent(&connect_aborted)) {
if (error_message->empty() && !connect_aborted) {
// Should not happen if error messages are set properly
- *error_message = "Connection failed for unknown reason";
+ *error_message = gettext("Connection failed for unknown reason");
errorstream << *error_message << std::endl;
}
return false;
@@ -1347,9 +1347,9 @@ bool Game::createClient(const GameStartData &start_data)
/* Camera
*/
- camera = new Camera(*draw_control, client);
- if (!camera->successfullyCreated(*error_message))
- return false;
+ camera = new Camera(*draw_control, client, m_rendering_engine);
+ if (client->modsLoaded())
+ client->getScript()->on_camera_ready(camera);
client->setCamera(camera);
/* Clouds
@@ -1359,7 +1359,7 @@ bool Game::createClient(const GameStartData &start_data)
/* Skybox
*/
- sky = new Sky(-1, texture_src, shader_src);
+ sky = new Sky(-1, m_rendering_engine, texture_src, shader_src);
scsf->setSky(sky);
skybox = NULL; // This is used/set later on in the main run loop
@@ -1381,16 +1381,28 @@ bool Game::createClient(const GameStartData &start_data)
std::wstring str = utf8_to_wide(PROJECT_NAME_C);
str += L" ";
str += utf8_to_wide(g_version_hash);
+ {
+ const wchar_t *text = nullptr;
+ if (simple_singleplayer_mode)
+ text = wgettext("Singleplayer");
+ else
+ text = wgettext("Multiplayer");
+ str += L" [";
+ str += text;
+ str += L"]";
+ delete[] text;
+ }
str += L" [";
str += driver->getName();
str += L"]";
+
device->setWindowCaption(str.c_str());
LocalPlayer *player = client->getEnv().getLocalPlayer();
player->hurt_tilt_timer = 0;
player->hurt_tilt_strength = 0;
- hud = new Hud(guienv, client, player, &player->inventory);
+ hud = new Hud(client, player, &player->inventory);
mapper = client->getMinimap();
@@ -1439,7 +1451,6 @@ bool Game::connectToServer(const GameStartData &start_data,
connect_address.Resolve(start_data.address.c_str());
if (connect_address.isZero()) { // i.e. INADDR_ANY, IN6ADDR_ANY
- //connect_address.Resolve("localhost");
if (connect_address.isIPv6()) {
IPv6AddressBytes addr_bytes;
addr_bytes.bytes[15] = 1;
@@ -1450,24 +1461,30 @@ bool Game::connectToServer(const GameStartData &start_data,
local_server_mode = true;
}
} catch (ResolveError &e) {
- *error_message = std::string("Couldn't resolve address: ") + e.what();
+ *error_message = fmtgettext("Couldn't resolve address: %s", e.what());
+
errorstream << *error_message << std::endl;
return false;
}
if (connect_address.isIPv6() && !g_settings->getBool("enable_ipv6")) {
- *error_message = "Unable to connect to " +
- connect_address.serializeString() +
- " because IPv6 is disabled";
+ *error_message = fmtgettext("Unable to connect to %s because IPv6 is disabled", connect_address.serializeString().c_str());
errorstream << *error_message << std::endl;
return false;
}
- client = new Client(start_data.name.c_str(),
- start_data.password, start_data.address,
- *draw_control, texture_src, shader_src,
- itemdef_manager, nodedef_manager, sound, eventmgr,
- connect_address.isIPv6(), m_game_ui.get());
+ try {
+ client = new Client(start_data.name.c_str(),
+ start_data.password, start_data.address,
+ *draw_control, texture_src, shader_src,
+ itemdef_manager, nodedef_manager, sound, eventmgr,
+ m_rendering_engine, connect_address.isIPv6(), m_game_ui.get());
+ client->migrateModStorage();
+ } catch (const BaseException &e) {
+ *error_message = fmtgettext("Error creating client: %s", e.what());
+ errorstream << *error_message << std::endl;
+ return false;
+ }
client->m_simple_singleplayer_mode = simple_singleplayer_mode;
@@ -1485,15 +1502,15 @@ bool Game::connectToServer(const GameStartData &start_data,
try {
input->clear();
- FpsControl fps_control = { 0 };
+ FpsControl fps_control;
f32 dtime;
f32 wait_time = 0; // in seconds
- fps_control.last_time = RenderingEngine::get_timer_time();
+ fps_control.reset();
- while (RenderingEngine::run()) {
+ while (m_rendering_engine->run()) {
- limitFps(&fps_control, &dtime);
+ fps_control.limit(device, &dtime);
// Update client and server
client->step(dtime);
@@ -1512,8 +1529,7 @@ bool Game::connectToServer(const GameStartData &start_data,
break;
if (client->accessDenied()) {
- *error_message = "Access denied. Reason: "
- + client->accessDeniedReason();
+ *error_message = fmtgettext("Access denied. Reason: %s", client->accessDeniedReason().c_str());
*reconnect_requested = client->reconnectRequested();
errorstream << *error_message << std::endl;
break;
@@ -1528,7 +1544,7 @@ bool Game::connectToServer(const GameStartData &start_data,
if (client->m_is_registration_confirmation_state) {
if (registration_confirmation_shown) {
// Keep drawing the GUI
- RenderingEngine::draw_menu_scene(guienv, dtime, true);
+ m_rendering_engine->draw_menu_scene(guienv, dtime, true);
} else {
registration_confirmation_shown = true;
(new GUIConfirmRegistration(guienv, guienv->getRootGUIElement(), -1,
@@ -1539,7 +1555,7 @@ bool Game::connectToServer(const GameStartData &start_data,
wait_time += dtime;
// Only time out if we aren't waiting for the server we started
if (!start_data.address.empty() && wait_time > 10) {
- *error_message = "Connection timed out.";
+ *error_message = gettext("Connection timed out.");
errorstream << *error_message << std::endl;
break;
}
@@ -1561,14 +1577,14 @@ bool Game::getServerContent(bool *aborted)
{
input->clear();
- FpsControl fps_control = { 0 };
+ FpsControl fps_control;
f32 dtime; // in seconds
- fps_control.last_time = RenderingEngine::get_timer_time();
+ fps_control.reset();
- while (RenderingEngine::run()) {
+ while (m_rendering_engine->run()) {
- limitFps(&fps_control, &dtime);
+ fps_control.limit(device, &dtime);
// Update client and server
client->step(dtime);
@@ -1587,7 +1603,7 @@ bool Game::getServerContent(bool *aborted)
return false;
if (client->getState() < LC_Init) {
- *error_message = "Client disconnected";
+ *error_message = gettext("Client disconnected");
errorstream << *error_message << std::endl;
return false;
}
@@ -1604,17 +1620,17 @@ bool Game::getServerContent(bool *aborted)
if (!client->itemdefReceived()) {
const wchar_t *text = wgettext("Item definitions...");
progress = 25;
- RenderingEngine::draw_load_screen(text, guienv, texture_src,
+ m_rendering_engine->draw_load_screen(text, guienv, texture_src,
dtime, progress);
delete[] text;
} else if (!client->nodedefReceived()) {
const wchar_t *text = wgettext("Node definitions...");
progress = 30;
- RenderingEngine::draw_load_screen(text, guienv, texture_src,
+ m_rendering_engine->draw_load_screen(text, guienv, texture_src,
dtime, progress);
delete[] text;
} else {
- std::stringstream message;
+ std::ostringstream message;
std::fixed(message);
message.precision(0);
float receive = client->mediaReceiveProgress() * 100;
@@ -1637,7 +1653,7 @@ bool Game::getServerContent(bool *aborted)
}
progress = 30 + client->mediaReceiveProgress() * 35 + 0.5;
- RenderingEngine::draw_load_screen(utf8_to_wide(message.str()), guienv,
+ m_rendering_engine->draw_load_screen(utf8_to_wide(message.str()), guienv,
texture_src, dtime, progress);
}
}
@@ -1669,8 +1685,7 @@ inline void Game::updateInteractTimers(f32 dtime)
inline bool Game::checkConnection()
{
if (client->accessDenied()) {
- *error_message = "Access denied. Reason: "
- + client->accessDeniedReason();
+ *error_message = fmtgettext("Access denied. Reason: %s", client->accessDeniedReason().c_str());
*reconnect_requested = client->reconnectRequested();
errorstream << *error_message << std::endl;
return false;
@@ -1723,6 +1738,25 @@ void Game::processQueues()
shader_src->processQueue();
}
+void Game::updateDebugState()
+{
+ const bool has_basic_debug = true;
+ bool has_debug = client->checkPrivilege("debug");
+
+ if (m_game_ui->m_flags.show_basic_debug) {
+ if (!has_basic_debug) {
+ m_game_ui->m_flags.show_basic_debug = false;
+ }
+ } else if (m_game_ui->m_flags.show_minimal_debug) {
+ if (has_basic_debug) {
+ m_game_ui->m_flags.show_basic_debug = true;
+ }
+ }
+ if (!has_basic_debug)
+ hud->disableBlockBounds();
+ if (!has_debug)
+ draw_control->show_wireframe = false;
+}
void Game::updateProfilers(const RunStats &stats, const FpsControl &draw_times,
f32 dtime)
@@ -1747,10 +1781,10 @@ void Game::updateProfilers(const RunStats &stats, const FpsControl &draw_times,
}
// Update update graphs
- g_profiler->graphAdd("Time non-rendering [ms]",
+ g_profiler->graphAdd("Time non-rendering [us]",
draw_times.busy_time - stats.drawtime);
- g_profiler->graphAdd("Sleep [ms]", draw_times.sleep_time);
+ g_profiler->graphAdd("Sleep [us]", draw_times.sleep_time);
g_profiler->graphAdd("FPS", 1.0f / dtime);
}
@@ -1783,9 +1817,9 @@ void Game::updateStats(RunStats *stats, const FpsControl &draw_times,
/* Busytime average and jitter calculation
*/
jp = &stats->busy_time_jitter;
- jp->avg = jp->avg + draw_times.busy_time * 0.02;
+ jp->avg = jp->avg + draw_times.getBusyMs() * 0.02;
- jitter = draw_times.busy_time - jp->avg;
+ jitter = draw_times.getBusyMs() - jp->avg;
if (jitter > jp->max)
jp->max = jitter;
@@ -1822,6 +1856,7 @@ void Game::processUserInput(f32 dtime)
else if (g_touchscreengui) {
/* on touchscreengui step may generate own input events which ain't
* what we want in case we just did clear them */
+ g_touchscreengui->show();
g_touchscreengui->step(dtime);
}
#endif
@@ -1904,24 +1939,18 @@ void Game::processKeyInput()
} else if (wasKeyDown(KeyType::INC_VOLUME)) {
if (g_settings->getBool("enable_sound")) {
float new_volume = rangelim(g_settings->getFloat("sound_volume") + 0.1f, 0.0f, 1.0f);
- wchar_t buf[100];
g_settings->setFloat("sound_volume", new_volume);
- const wchar_t *str = wgettext("Volume changed to %d%%");
- swprintf(buf, sizeof(buf) / sizeof(wchar_t), str, myround(new_volume * 100));
- delete[] str;
- m_game_ui->showStatusText(buf);
+ std::wstring msg = fwgettext("Volume changed to %d%%", myround(new_volume * 100));
+ m_game_ui->showStatusText(msg);
} else {
m_game_ui->showTranslatedStatusText("Sound system is disabled");
}
} else if (wasKeyDown(KeyType::DEC_VOLUME)) {
if (g_settings->getBool("enable_sound")) {
float new_volume = rangelim(g_settings->getFloat("sound_volume") - 0.1f, 0.0f, 1.0f);
- wchar_t buf[100];
g_settings->setFloat("sound_volume", new_volume);
- const wchar_t *str = wgettext("Volume changed to %d%%");
- swprintf(buf, sizeof(buf) / sizeof(wchar_t), str, myround(new_volume * 100));
- delete[] str;
- m_game_ui->showStatusText(buf);
+ std::wstring msg = fwgettext("Volume changed to %d%%", myround(new_volume * 100));
+ m_game_ui->showStatusText(msg);
} else {
m_game_ui->showTranslatedStatusText("Sound system is disabled");
}
@@ -1934,6 +1963,8 @@ void Game::processKeyInput()
toggleCinematic();
} else if (wasKeyDown(KeyType::SCREENSHOT)) {
client->makeScreenshot();
+ } else if (wasKeyDown(KeyType::TOGGLE_BLOCK_BOUNDS)) {
+ toggleBlockBounds();
} else if (wasKeyDown(KeyType::TOGGLE_HUD)) {
m_game_ui->toggleHud();
} else if (wasKeyDown(KeyType::MINIMAP)) {
@@ -2042,15 +2073,22 @@ void Game::openInventory()
InventoryLocation inventoryloc;
inventoryloc.setCurrentPlayer();
- if (!client->modsLoaded()
- || !client->getScript()->on_inventory_open(fs_src->m_client->getInventory(inventoryloc))) {
- TextDest *txt_dst = new TextDestPlayerInventory(client);
- auto *&formspec = m_game_ui->updateFormspec("");
- GUIFormSpecMenu::create(formspec, client, &input->joystick, fs_src,
- txt_dst, client->getFormspecPrepend(), sound);
+ if (client->modsLoaded() && client->getScript()->on_inventory_open(fs_src->m_client->getInventory(inventoryloc))) {
+ delete fs_src;
+ return;
+ }
- formspec->setFormSpec(fs_src->getForm(), inventoryloc);
+ if (fs_src->getForm().empty()) {
+ delete fs_src;
+ return;
}
+
+ TextDest *txt_dst = new TextDestPlayerInventory(client);
+ auto *&formspec = m_game_ui->updateFormspec("");
+ GUIFormSpecMenu::create(formspec, client, m_rendering_engine->get_gui_env(),
+ &input->joystick, fs_src, txt_dst, client->getFormspecPrepend(), sound);
+
+ formspec->setFormSpec(fs_src->getForm(), inventoryloc);
}
@@ -2138,7 +2176,7 @@ void Game::toggleFast()
m_game_ui->showTranslatedStatusText("Fast mode disabled");
}
-#ifdef __ANDROID__
+#ifdef HAVE_TOUCHSCREENGUI
m_cache_hold_aux1 = fast_move && has_fast_privs;
#endif
}
@@ -2171,6 +2209,32 @@ void Game::toggleCinematic()
m_game_ui->showTranslatedStatusText("Cinematic mode disabled");
}
+void Game::toggleBlockBounds()
+{
+ if (true /* basic_debug */) {
+ enum Hud::BlockBoundsMode newmode = hud->toggleBlockBounds();
+ switch (newmode) {
+ case Hud::BLOCK_BOUNDS_OFF:
+ m_game_ui->showTranslatedStatusText("Block bounds hidden");
+ break;
+ case Hud::BLOCK_BOUNDS_CURRENT:
+ m_game_ui->showTranslatedStatusText("Block bounds shown for current block");
+ break;
+ case Hud::BLOCK_BOUNDS_NEAR:
+ m_game_ui->showTranslatedStatusText("Block bounds shown for nearby blocks");
+ break;
+ case Hud::BLOCK_BOUNDS_MAX:
+ m_game_ui->showTranslatedStatusText("Block bounds shown for all blocks");
+ break;
+ default:
+ break;
+ }
+
+ } else {
+ m_game_ui->showTranslatedStatusText("Can't show block bounds (need 'basic_debug' privilege)");
+ }
+}
+
// Autoforward by toggling continuous forward.
void Game::toggleAutoforward()
{
@@ -2234,24 +2298,39 @@ void Game::toggleFog()
void Game::toggleDebug()
{
- // Initial / 4x toggle: Chat only
- // 1x toggle: Debug text with chat
+ // Initial: No debug info
+ // 1x toggle: Debug text
// 2x toggle: Debug text with profiler graph
- // 3x toggle: Debug text and wireframe
- if (!m_game_ui->m_flags.show_debug) {
- m_game_ui->m_flags.show_debug = true;
+ // 3x toggle: Debug text and wireframe (needs "debug" priv)
+ // Next toggle: Back to initial
+ //
+ // The debug text can be in 2 modes: minimal and basic.
+ // * Minimal: Only technical client info that not gameplay-relevant
+ // * Basic: Info that might give gameplay advantage, e.g. pos, angle
+ // Basic mode is always used.
+
+ const bool has_basic_debug = true;
+ if (!m_game_ui->m_flags.show_minimal_debug) {
+ m_game_ui->m_flags.show_minimal_debug = true;
+ if (has_basic_debug)
+ m_game_ui->m_flags.show_basic_debug = true;
m_game_ui->m_flags.show_profiler_graph = false;
draw_control->show_wireframe = false;
m_game_ui->showTranslatedStatusText("Debug info shown");
} else if (!m_game_ui->m_flags.show_profiler_graph && !draw_control->show_wireframe) {
+ if (has_basic_debug)
+ m_game_ui->m_flags.show_basic_debug = true;
m_game_ui->m_flags.show_profiler_graph = true;
m_game_ui->showTranslatedStatusText("Profiler graph shown");
} else if (!draw_control->show_wireframe && client->checkPrivilege("debug")) {
+ if (has_basic_debug)
+ m_game_ui->m_flags.show_basic_debug = true;
m_game_ui->m_flags.show_profiler_graph = false;
draw_control->show_wireframe = true;
m_game_ui->showTranslatedStatusText("Wireframe shown");
} else {
- m_game_ui->m_flags.show_debug = false;
+ m_game_ui->m_flags.show_minimal_debug = false;
+ m_game_ui->m_flags.show_basic_debug = false;
m_game_ui->m_flags.show_profiler_graph = false;
draw_control->show_wireframe = false;
if (client->checkPrivilege("debug")) {
@@ -2278,20 +2357,13 @@ void Game::increaseViewRange()
s16 range = g_settings->getS16("viewing_range");
s16 range_new = range + 10;
- wchar_t buf[255];
- const wchar_t *str;
if (range_new > 4000) {
range_new = 4000;
- str = wgettext("Viewing range is at maximum: %d");
- swprintf(buf, sizeof(buf) / sizeof(wchar_t), str, range_new);
- delete[] str;
- m_game_ui->showStatusText(buf);
-
+ std::wstring msg = fwgettext("Viewing range is at maximum: %d", range_new);
+ m_game_ui->showStatusText(msg);
} else {
- str = wgettext("Viewing range changed to %d");
- swprintf(buf, sizeof(buf) / sizeof(wchar_t), str, range_new);
- delete[] str;
- m_game_ui->showStatusText(buf);
+ std::wstring msg = fwgettext("Viewing range changed to %d", range_new);
+ m_game_ui->showStatusText(msg);
}
g_settings->set("viewing_range", itos(range_new));
}
@@ -2302,19 +2374,13 @@ void Game::decreaseViewRange()
s16 range = g_settings->getS16("viewing_range");
s16 range_new = range - 10;
- wchar_t buf[255];
- const wchar_t *str;
if (range_new < 20) {
range_new = 20;
- str = wgettext("Viewing range is at minimum: %d");
- swprintf(buf, sizeof(buf) / sizeof(wchar_t), str, range_new);
- delete[] str;
- m_game_ui->showStatusText(buf);
+ std::wstring msg = fwgettext("Viewing range is at minimum: %d", range_new);
+ m_game_ui->showStatusText(msg);
} else {
- str = wgettext("Viewing range changed to %d");
- swprintf(buf, sizeof(buf) / sizeof(wchar_t), str, range_new);
- delete[] str;
- m_game_ui->showStatusText(buf);
+ std::wstring msg = fwgettext("Viewing range changed to %d", range_new);
+ m_game_ui->showStatusText(msg);
}
g_settings->set("viewing_range", itos(range_new));
}
@@ -2337,7 +2403,6 @@ void Game::checkZoomEnabled()
m_game_ui->showTranslatedStatusText("Zoom currently disabled by game or mod");
}
-
void Game::updateCameraDirection(CameraOrientation *cam, float dtime)
{
if ((device->isWindowActive() && device->isWindowFocused()
@@ -2373,6 +2438,18 @@ void Game::updateCameraDirection(CameraOrientation *cam, float dtime)
}
}
+// Get the factor to multiply with sensitivity to get the same mouse/joystick
+// responsiveness independently of FOV.
+f32 Game::getSensitivityScaleFactor() const
+{
+ f32 fov_y = client->getCamera()->getFovY();
+
+ // Multiply by a constant such that it becomes 1.0 at 72 degree FOV and
+ // 16:9 aspect ratio to minimize disruption of existing sensitivity
+ // settings.
+ return tan(fov_y / 2.0f) * 1.3763818698f;
+}
+
void Game::updateCameraOrientation(CameraOrientation *cam, float dtime)
{
#ifdef HAVE_TOUCHSCREENGUI
@@ -2388,8 +2465,9 @@ void Game::updateCameraOrientation(CameraOrientation *cam, float dtime)
dist.Y = -dist.Y;
}
- cam->camera_yaw -= dist.X * m_cache_mouse_sensitivity;
- cam->camera_pitch += dist.Y * m_cache_mouse_sensitivity;
+ f32 sens_scale = getSensitivityScaleFactor();
+ cam->camera_yaw -= dist.X * m_cache_mouse_sensitivity * sens_scale;
+ cam->camera_pitch += dist.Y * m_cache_mouse_sensitivity * sens_scale;
if (dist.X != 0 || dist.Y != 0)
input->setMousePos(center.X, center.Y);
@@ -2398,7 +2476,8 @@ void Game::updateCameraOrientation(CameraOrientation *cam, float dtime)
#endif
if (m_cache_enable_joysticks) {
- f32 c = m_cache_joystick_frustum_sensitivity * (1.f / 32767.f) * dtime;
+ f32 sens_scale = getSensitivityScaleFactor();
+ f32 c = m_cache_joystick_frustum_sensitivity * dtime * sens_scale;
cam->camera_yaw -= input->joystick.getAxisWithoutDead(JA_FRUSTUM_HORIZONTAL) * c;
cam->camera_pitch += input->joystick.getAxisWithoutDead(JA_FRUSTUM_VERTICAL) * c;
}
@@ -2409,71 +2488,46 @@ void Game::updateCameraOrientation(CameraOrientation *cam, float dtime)
void Game::updatePlayerControl(const CameraOrientation &cam)
{
- //TimeTaker tt("update player control", NULL, PRECISION_NANO);
+ LocalPlayer *player = client->getEnv().getLocalPlayer();
- // DO NOT use the isKeyDown method for the forward, backward, left, right
- // buttons, as the code that uses the controls needs to be able to
- // distinguish between the two in order to know when to use joysticks.
+ //TimeTaker tt("update player control", NULL, PRECISION_NANO);
PlayerControl control(
- input->isKeyDown(KeyType::FORWARD),
- input->isKeyDown(KeyType::BACKWARD),
- input->isKeyDown(KeyType::LEFT),
- input->isKeyDown(KeyType::RIGHT),
- isKeyDown(KeyType::JUMP),
- isKeyDown(KeyType::SPECIAL1),
+ isKeyDown(KeyType::FORWARD),
+ isKeyDown(KeyType::BACKWARD),
+ isKeyDown(KeyType::LEFT),
+ isKeyDown(KeyType::RIGHT),
+ isKeyDown(KeyType::JUMP) || player->getAutojump(),
+ isKeyDown(KeyType::AUX1),
isKeyDown(KeyType::SNEAK),
isKeyDown(KeyType::ZOOM),
isKeyDown(KeyType::DIG),
isKeyDown(KeyType::PLACE),
cam.camera_pitch,
cam.camera_yaw,
- input->joystick.getAxisWithoutDead(JA_SIDEWARD_MOVE),
- input->joystick.getAxisWithoutDead(JA_FORWARD_MOVE)
+ input->getMovementSpeed(),
+ input->getMovementDirection()
);
- u32 keypress_bits = (
- ( (u32)(isKeyDown(KeyType::FORWARD) & 0x1) << 0) |
- ( (u32)(isKeyDown(KeyType::BACKWARD) & 0x1) << 1) |
- ( (u32)(isKeyDown(KeyType::LEFT) & 0x1) << 2) |
- ( (u32)(isKeyDown(KeyType::RIGHT) & 0x1) << 3) |
- ( (u32)(isKeyDown(KeyType::JUMP) & 0x1) << 4) |
- ( (u32)(isKeyDown(KeyType::SPECIAL1) & 0x1) << 5) |
- ( (u32)(isKeyDown(KeyType::SNEAK) & 0x1) << 6) |
- ( (u32)(isKeyDown(KeyType::DIG) & 0x1) << 7) |
- ( (u32)(isKeyDown(KeyType::PLACE) & 0x1) << 8) |
- ( (u32)(isKeyDown(KeyType::ZOOM) & 0x1) << 9)
- );
+ // autoforward if set: move towards pointed position at maximum speed
+ if (player->getPlayerSettings().continuous_forward &&
+ client->activeObjectsReceived() && !player->isDead()) {
+ control.movement_speed = 1.0f;
+ control.movement_direction = 0.0f;
+ }
-#ifdef ANDROID
- /* For Android, simulate holding down AUX1 (fast move) if the user has
+#ifdef HAVE_TOUCHSCREENGUI
+ /* For touch, simulate holding down AUX1 (fast move) if the user has
* the fast_move setting toggled on. If there is an aux1 key defined for
- * Android then its meaning is inverted (i.e. holding aux1 means walk and
+ * touch then its meaning is inverted (i.e. holding aux1 means walk and
* not fast)
*/
if (m_cache_hold_aux1) {
control.aux1 = control.aux1 ^ true;
- keypress_bits ^= ((u32)(1U << 5));
}
#endif
- LocalPlayer *player = client->getEnv().getLocalPlayer();
-
- // autojump if set: simulate "jump" key
- if (player->getAutojump()) {
- control.jump = true;
- keypress_bits |= 1U << 4;
- }
-
- // autoforward if set: simulate "up" key
- if (player->getPlayerSettings().continuous_forward &&
- client->activeObjectsReceived() && !player->isDead()) {
- control.up = true;
- keypress_bits |= 1U << 0;
- }
-
client->setPlayerControl(control);
- player->keyPressed = keypress_bits;
//tt.stop();
}
@@ -2487,6 +2541,9 @@ inline void Game::step(f32 *dtime)
if (can_be_and_is_paused) { // This is for a singleplayer server
*dtime = 0; // No time passes
} else {
+ if (simple_singleplayer_mode && !paused_animated_nodes.empty())
+ resumeAnimation();
+
if (server)
server->step(*dtime);
@@ -2494,6 +2551,33 @@ inline void Game::step(f32 *dtime)
}
}
+static void pauseNodeAnimation(PausedNodesList &paused, scene::ISceneNode *node) {
+ if (!node)
+ return;
+ for (auto &&child: node->getChildren())
+ pauseNodeAnimation(paused, child);
+ if (node->getType() != scene::ESNT_ANIMATED_MESH)
+ return;
+ auto animated_node = static_cast<scene::IAnimatedMeshSceneNode *>(node);
+ float speed = animated_node->getAnimationSpeed();
+ if (!speed)
+ return;
+ paused.push_back({grab(animated_node), speed});
+ animated_node->setAnimationSpeed(0.0f);
+}
+
+void Game::pauseAnimation()
+{
+ pauseNodeAnimation(paused_animated_nodes, smgr->getRootSceneNode());
+}
+
+void Game::resumeAnimation()
+{
+ for (auto &&pair: paused_animated_nodes)
+ pair.first->setAnimationSpeed(pair.second);
+ paused_animated_nodes.clear();
+}
+
const ClientEventHandler Game::clientEventHandler[CLIENTEVENT_MAX] = {
{&Game::handleClientEvent_None},
{&Game::handleClientEvent_PlayerDamage},
@@ -2527,14 +2611,18 @@ void Game::handleClientEvent_PlayerDamage(ClientEvent *event, CameraOrientation
// Damage flash and hurt tilt are not used at death
if (client->getHP() > 0) {
- runData.damage_flash += 95.0f + 3.2f * event->player_damage.amount;
- runData.damage_flash = MYMIN(runData.damage_flash, 127.0f);
-
LocalPlayer *player = client->getEnv().getLocalPlayer();
+ f32 hp_max = player->getCAO() ?
+ player->getCAO()->getProperties().hp_max : PLAYER_MAX_HP_DEFAULT;
+ f32 damage_ratio = event->player_damage.amount / hp_max;
+
+ runData.damage_flash += 95.0f + 64.f * damage_ratio;
+ runData.damage_flash = MYMIN(runData.damage_flash, 127.0f);
+
player->hurt_tilt_timer = 1.5f;
player->hurt_tilt_strength =
- rangelim(event->player_damage.amount / 4.0f, 1.0f, 4.0f);
+ rangelim(damage_ratio * 5.0f, 1.0f, 4.0f);
}
// Play damage sound
@@ -2578,8 +2666,8 @@ void Game::handleClientEvent_ShowFormSpec(ClientEvent *event, CameraOrientation
new TextDestPlayerInventory(client, *(event->show_formspec.formname));
auto *&formspec = m_game_ui->updateFormspec(*(event->show_formspec.formname));
- GUIFormSpecMenu::create(formspec, client, &input->joystick,
- fs_src, txt_dst, client->getFormspecPrepend(), sound);
+ GUIFormSpecMenu::create(formspec, client, m_rendering_engine->get_gui_env(),
+ &input->joystick, fs_src, txt_dst, client->getFormspecPrepend(), sound);
}
delete event->show_formspec.formspec;
@@ -2591,8 +2679,8 @@ void Game::handleClientEvent_ShowLocalFormSpec(ClientEvent *event, CameraOrienta
FormspecFormSource *fs_src = new FormspecFormSource(*event->show_formspec.formspec);
LocalFormspecHandler *txt_dst =
new LocalFormspecHandler(*event->show_formspec.formname, client);
- GUIFormSpecMenu::create(m_game_ui->getFormspecGUI(), client, &input->joystick,
- fs_src, txt_dst, client->getFormspecPrepend(), sound);
+ GUIFormSpecMenu::create(m_game_ui->getFormspecGUI(), client, m_rendering_engine->get_gui_env(),
+ &input->joystick, fs_src, txt_dst, client->getFormspecPrepend(), sound);
delete event->show_formspec.formspec;
delete event->show_formspec.formname;
@@ -2609,48 +2697,33 @@ void Game::handleClientEvent_HudAdd(ClientEvent *event, CameraOrientation *cam)
{
LocalPlayer *player = client->getEnv().getLocalPlayer();
- u32 server_id = event->hudadd.server_id;
+ u32 server_id = event->hudadd->server_id;
// ignore if we already have a HUD with that ID
auto i = m_hud_server_to_client.find(server_id);
if (i != m_hud_server_to_client.end()) {
- delete event->hudadd.pos;
- delete event->hudadd.name;
- delete event->hudadd.scale;
- delete event->hudadd.text;
- delete event->hudadd.align;
- delete event->hudadd.offset;
- delete event->hudadd.world_pos;
- delete event->hudadd.size;
- delete event->hudadd.text2;
+ delete event->hudadd;
return;
}
HudElement *e = new HudElement;
- e->type = (HudElementType)event->hudadd.type;
- e->pos = *event->hudadd.pos;
- e->name = *event->hudadd.name;
- e->scale = *event->hudadd.scale;
- e->text = *event->hudadd.text;
- e->number = event->hudadd.number;
- e->item = event->hudadd.item;
- e->dir = event->hudadd.dir;
- e->align = *event->hudadd.align;
- e->offset = *event->hudadd.offset;
- e->world_pos = *event->hudadd.world_pos;
- e->size = *event->hudadd.size;
- e->z_index = event->hudadd.z_index;
- e->text2 = *event->hudadd.text2;
+ e->type = static_cast<HudElementType>(event->hudadd->type);
+ e->pos = event->hudadd->pos;
+ e->name = event->hudadd->name;
+ e->scale = event->hudadd->scale;
+ e->text = event->hudadd->text;
+ e->number = event->hudadd->number;
+ e->item = event->hudadd->item;
+ e->dir = event->hudadd->dir;
+ e->align = event->hudadd->align;
+ e->offset = event->hudadd->offset;
+ e->world_pos = event->hudadd->world_pos;
+ e->size = event->hudadd->size;
+ e->z_index = event->hudadd->z_index;
+ e->text2 = event->hudadd->text2;
+ e->style = event->hudadd->style;
m_hud_server_to_client[server_id] = player->addHud(e);
- delete event->hudadd.pos;
- delete event->hudadd.name;
- delete event->hudadd.scale;
- delete event->hudadd.text;
- delete event->hudadd.align;
- delete event->hudadd.offset;
- delete event->hudadd.world_pos;
- delete event->hudadd.size;
- delete event->hudadd.text2;
+ delete event->hudadd;
}
void Game::handleClientEvent_HudRemove(ClientEvent *event, CameraOrientation *cam)
@@ -2672,77 +2745,54 @@ void Game::handleClientEvent_HudChange(ClientEvent *event, CameraOrientation *ca
HudElement *e = nullptr;
- auto i = m_hud_server_to_client.find(event->hudchange.id);
+ auto i = m_hud_server_to_client.find(event->hudchange->id);
if (i != m_hud_server_to_client.end()) {
e = player->getHud(i->second);
}
if (e == nullptr) {
- delete event->hudchange.v3fdata;
- delete event->hudchange.v2fdata;
- delete event->hudchange.sdata;
- delete event->hudchange.v2s32data;
+ delete event->hudchange;
return;
}
- switch (event->hudchange.stat) {
- case HUD_STAT_POS:
- e->pos = *event->hudchange.v2fdata;
- break;
+#define CASE_SET(statval, prop, dataprop) \
+ case statval: \
+ e->prop = event->hudchange->dataprop; \
+ break
- case HUD_STAT_NAME:
- e->name = *event->hudchange.sdata;
- break;
+ switch (event->hudchange->stat) {
+ CASE_SET(HUD_STAT_POS, pos, v2fdata);
- case HUD_STAT_SCALE:
- e->scale = *event->hudchange.v2fdata;
- break;
+ CASE_SET(HUD_STAT_NAME, name, sdata);
- case HUD_STAT_TEXT:
- e->text = *event->hudchange.sdata;
- break;
+ CASE_SET(HUD_STAT_SCALE, scale, v2fdata);
- case HUD_STAT_NUMBER:
- e->number = event->hudchange.data;
- break;
+ CASE_SET(HUD_STAT_TEXT, text, sdata);
- case HUD_STAT_ITEM:
- e->item = event->hudchange.data;
- break;
+ CASE_SET(HUD_STAT_NUMBER, number, data);
- case HUD_STAT_DIR:
- e->dir = event->hudchange.data;
- break;
+ CASE_SET(HUD_STAT_ITEM, item, data);
- case HUD_STAT_ALIGN:
- e->align = *event->hudchange.v2fdata;
- break;
+ CASE_SET(HUD_STAT_DIR, dir, data);
- case HUD_STAT_OFFSET:
- e->offset = *event->hudchange.v2fdata;
- break;
+ CASE_SET(HUD_STAT_ALIGN, align, v2fdata);
- case HUD_STAT_WORLD_POS:
- e->world_pos = *event->hudchange.v3fdata;
- break;
+ CASE_SET(HUD_STAT_OFFSET, offset, v2fdata);
- case HUD_STAT_SIZE:
- e->size = *event->hudchange.v2s32data;
- break;
+ CASE_SET(HUD_STAT_WORLD_POS, world_pos, v3fdata);
- case HUD_STAT_Z_INDEX:
- e->z_index = event->hudchange.data;
- break;
+ CASE_SET(HUD_STAT_SIZE, size, v2s32data);
- case HUD_STAT_TEXT2:
- e->text2 = *event->hudchange.sdata;
- break;
+ CASE_SET(HUD_STAT_Z_INDEX, z_index, data);
+
+ CASE_SET(HUD_STAT_TEXT2, text2, sdata);
+
+ CASE_SET(HUD_STAT_STYLE, style, data);
}
- delete event->hudchange.v3fdata;
- delete event->hudchange.v2fdata;
- delete event->hudchange.sdata;
- delete event->hudchange.v2s32data;
+#undef CASE_SET
+
+ delete event->hudchange;
}
void Game::handleClientEvent_SetSky(ClientEvent *event, CameraOrientation *cam)
@@ -2797,6 +2847,7 @@ void Game::handleClientEvent_SetSky(ClientEvent *event, CameraOrientation *cam)
"custom"
);
}
+
delete event->set_sky;
}
@@ -2823,7 +2874,7 @@ void Game::handleClientEvent_SetMoon(ClientEvent *event, CameraOrientation *cam)
void Game::handleClientEvent_SetStars(ClientEvent *event, CameraOrientation *cam)
{
sky->setStarsVisible(event->star_params->visible);
- sky->setStarCount(event->star_params->count, false);
+ sky->setStarCount(event->star_params->count);
sky->setStarColor(event->star_params->starcolor);
sky->setStarScale(event->star_params->scale);
delete event->star_params;
@@ -2860,7 +2911,7 @@ void Game::processClientEvents(CameraOrientation *cam)
}
}
-void Game::updateChat(f32 dtime, const v2u32 &screensize)
+void Game::updateChat(f32 dtime)
{
// Get new messages from error log buffer
while (!m_chat_log_buf.empty())
@@ -2876,11 +2927,17 @@ void Game::updateChat(f32 dtime, const v2u32 &screensize)
chat_backend->step(dtime);
// Display all messages in a static text element
- m_game_ui->setChatText(chat_backend->getRecentChat(),
- chat_backend->getRecentBuffer().getLineCount());
+ auto &buf = chat_backend->getRecentBuffer();
+ if (buf.getLinesModified()) {
+ buf.resetLinesModified();
+ m_game_ui->setChatText(chat_backend->getRecentChat(), buf.getLineCount());
+ }
+
+ // Make sure that the size is still correct
+ m_game_ui->updateChatSize();
}
-void Game::updateCamera(u32 busy_time, f32 dtime)
+void Game::updateCamera(f32 dtime)
{
LocalPlayer *player = client->getEnv().getLocalPlayer();
@@ -2919,7 +2976,7 @@ void Game::updateCamera(u32 busy_time, f32 dtime)
float tool_reload_ratio = runData.time_from_last_punch / full_punch_interval;
tool_reload_ratio = MYMIN(tool_reload_ratio, 1.0);
- camera->update(player, dtime, busy_time / 1000.0f, tool_reload_ratio);
+ camera->update(player, dtime, tool_reload_ratio);
camera->step(dtime);
v3f camera_position = camera->getPosition();
@@ -3260,9 +3317,8 @@ void Game::handlePointingAtNode(const PointedThing &pointed,
} else {
MapNode n = map.getNode(nodepos);
- if (nodedef_manager->get(n).tiledef[0].name == "unknown_node.png") {
- m_game_ui->setInfoText(L"Unknown node: " +
- utf8_to_wide(nodedef_manager->get(n).name));
+ if (nodedef_manager->get(n).name == "unknown") {
+ m_game_ui->setInfoText(L"Unknown node");
}
}
@@ -3294,7 +3350,8 @@ bool Game::nodePlacement(const ItemDefinition &selected_def,
const ItemStack &selected_item, const v3s16 &nodepos, const v3s16 &neighbourpos,
const PointedThing &pointed, const NodeMetadata *meta)
{
- std::string prediction = selected_def.node_placement_prediction;
+ const auto &prediction = selected_def.node_placement_prediction;
+
const NodeDefManager *nodedef = client->ndef();
ClientMap &map = client->getEnv().getClientMap();
MapNode node;
@@ -3323,8 +3380,8 @@ bool Game::nodePlacement(const ItemDefinition &selected_def,
TextDest *txt_dst = new TextDestNodeMetadata(nodepos, client);
auto *&formspec = m_game_ui->updateFormspec("");
- GUIFormSpecMenu::create(formspec, client, &input->joystick, fs_src,
- txt_dst, client->getFormspecPrepend(), sound);
+ GUIFormSpecMenu::create(formspec, client, m_rendering_engine->get_gui_env(),
+ &input->joystick, fs_src, txt_dst, client->getFormspecPrepend(), sound);
formspec->setFormSpec(meta->getString("formspec"), inventoryloc);
return false;
@@ -3364,8 +3421,7 @@ bool Game::nodePlacement(const ItemDefinition &selected_def,
if (!found) {
errorstream << "Node placement prediction failed for "
- << selected_def.name << " (places "
- << prediction
+ << selected_def.name << " (places " << prediction
<< ") - Name not known" << std::endl;
// Handle this as if prediction was empty
// Report to server
@@ -3376,9 +3432,14 @@ bool Game::nodePlacement(const ItemDefinition &selected_def,
const ContentFeatures &predicted_f = nodedef->get(id);
// Predict param2 for facedir and wallmounted nodes
+ // Compare core.item_place_node() for what the server does
u8 param2 = 0;
- if (predicted_f.param_type_2 == CPT2_WALLMOUNTED ||
+ const u8 place_param2 = selected_def.place_param2;
+
+ if (place_param2) {
+ param2 = place_param2;
+ } else if (predicted_f.param_type_2 == CPT2_WALLMOUNTED ||
predicted_f.param_type_2 == CPT2_COLORED_WALLMOUNTED) {
v3s16 dir = nodepos - neighbourpos;
@@ -3389,9 +3450,7 @@ bool Game::nodePlacement(const ItemDefinition &selected_def,
} else {
param2 = dir.Z < 0 ? 5 : 4;
}
- }
-
- if (predicted_f.param_type_2 == CPT2_FACEDIR ||
+ } else if (predicted_f.param_type_2 == CPT2_FACEDIR ||
predicted_f.param_type_2 == CPT2_COLORED_FACEDIR) {
v3s16 dir = nodepos - floatToInt(client->getEnv().getLocalPlayer()->getPosition(), BS);
@@ -3402,11 +3461,9 @@ bool Game::nodePlacement(const ItemDefinition &selected_def,
}
}
- assert(param2 <= 5);
-
- //Check attachment if node is in group attached_node
- if (((ItemGroupList) predicted_f.groups)["attached_node"] != 0) {
- static v3s16 wallmounted_dirs[8] = {
+ // Check attachment if node is in group attached_node
+ if (itemgroup_get(predicted_f.groups, "attached_node") != 0) {
+ const static v3s16 wallmounted_dirs[8] = {
v3s16(0, 1, 0),
v3s16(0, -1, 0),
v3s16(1, 0, 0),
@@ -3431,11 +3488,11 @@ bool Game::nodePlacement(const ItemDefinition &selected_def,
}
// Apply color
- if ((predicted_f.param_type_2 == CPT2_COLOR
+ if (!place_param2 && (predicted_f.param_type_2 == CPT2_COLOR
|| predicted_f.param_type_2 == CPT2_COLORED_FACEDIR
|| predicted_f.param_type_2 == CPT2_COLORED_WALLMOUNTED)) {
- const std::string &indexstr = selected_item.metadata.getString(
- "palette_index", 0);
+ const auto &indexstr = selected_item.metadata.
+ getString("palette_index", 0);
if (!indexstr.empty()) {
s32 index = mystoi(indexstr);
if (predicted_f.param_type_2 == CPT2_COLOR) {
@@ -3475,11 +3532,10 @@ bool Game::nodePlacement(const ItemDefinition &selected_def,
soundmaker->m_player_rightpunch_sound = selected_def.sound_place_failed;
return false;
}
- } catch (InvalidPositionException &e) {
+ } catch (const InvalidPositionException &e) {
errorstream << "Node placement prediction failed for "
<< selected_def.name << " (places "
- << prediction
- << ") - Position not loaded" << std::endl;
+ << prediction << ") - Position not loaded" << std::endl;
soundmaker->m_player_rightpunch_sound = selected_def.sound_place_failed;
return false;
}
@@ -3549,7 +3605,8 @@ void Game::handleDigging(const PointedThing &pointed, const v3s16 &nodepos,
// cheat detection.
// Get digging parameters
DigParams params = getDigParams(nodedef_manager->get(n).groups,
- &selected_item.getToolCapabilities(itemdef_manager));
+ &selected_item.getToolCapabilities(itemdef_manager),
+ selected_item.wear);
// If can't dig, try hand
if (!params.diggable) {
@@ -3796,12 +3853,28 @@ void Game::updateFrame(ProfilerGraph *graph, RunStats *stats, f32 dtime,
}
/*
- Get chat messages from client
+ Damage camera tilt
*/
+ if (player->hurt_tilt_timer > 0.0f) {
+ player->hurt_tilt_timer -= dtime * 6.0f;
- v2u32 screensize = driver->getScreenSize();
+ if (player->hurt_tilt_timer < 0.0f)
+ player->hurt_tilt_strength = 0.0f;
+ }
+
+ /*
+ Update minimap pos and rotation
+ */
+ if (mapper && m_game_ui->m_flags.show_hud) {
+ mapper->setPos(floatToInt(player->getPosition(), BS));
+ mapper->setAngle(player->getYaw());
+ }
- updateChat(dtime, screensize);
+ /*
+ Get chat messages from client
+ */
+
+ updateChat(dtime);
/*
Inventory
@@ -3823,15 +3896,22 @@ void Game::updateFrame(ProfilerGraph *graph, RunStats *stats, f32 dtime,
*/
runData.update_draw_list_timer += dtime;
+ float update_draw_list_delta = 0.2f;
+
v3f camera_direction = camera->getDirection();
- if (runData.update_draw_list_timer >= 0.2
+ if (runData.update_draw_list_timer >= update_draw_list_delta
|| runData.update_draw_list_last_cam_dir.getDistanceFrom(camera_direction) > 0.2
- || m_camera_offset_changed) {
+ || m_camera_offset_changed
+ || client->getEnv().getClientMap().needsUpdateDrawList()) {
runData.update_draw_list_timer = 0;
client->getEnv().getClientMap().updateDrawList();
runData.update_draw_list_last_cam_dir = camera_direction;
}
+ if (RenderingEngine::get_shadow_renderer()) {
+ updateShadows();
+ }
+
m_game_ui->update(*stats, client, draw_control, cam, runData.pointed_old, gui_chat_console, dtime);
/*
@@ -3863,11 +3943,11 @@ void Game::updateFrame(ProfilerGraph *graph, RunStats *stats, f32 dtime,
} while (false);
/*
- Drawing begins
+ ==================== Drawing begins ====================
*/
- const video::SColor &skycolor = sky->getSkyColor();
+ const video::SColor skycolor = sky->getSkyColor();
- TimeTaker tt_draw("Draw scene");
+ TimeTaker tt_draw("Draw scene", nullptr, PRECISION_MICRO);
driver->beginScene(true, true, skycolor);
bool draw_wield_tool = (m_game_ui->m_flags.show_hud &&
@@ -3882,12 +3962,14 @@ void Game::updateFrame(ProfilerGraph *graph, RunStats *stats, f32 dtime,
} catch (SettingNotFoundException) {
}
#endif
- RenderingEngine::draw_scene(skycolor, m_game_ui->m_flags.show_hud,
+ m_rendering_engine->draw_scene(skycolor, m_game_ui->m_flags.show_hud,
m_game_ui->m_flags.show_minimap, draw_wield_tool, draw_crosshair);
/*
Profiler graph
*/
+ v2u32 screensize = driver->getScreenSize();
+
if (m_game_ui->m_flags.show_profiler_graph)
graph->draw(10, screensize.Y - 10, driver, g_fontengine->getFont());
@@ -3904,25 +3986,7 @@ void Game::updateFrame(ProfilerGraph *graph, RunStats *stats, f32 dtime,
}
/*
- Damage camera tilt
- */
- if (player->hurt_tilt_timer > 0.0f) {
- player->hurt_tilt_timer -= dtime * 6.0f;
-
- if (player->hurt_tilt_timer < 0.0f)
- player->hurt_tilt_strength = 0.0f;
- }
-
- /*
- Update minimap pos and rotation
- */
- if (mapper && m_game_ui->m_flags.show_hud) {
- mapper->setPos(floatToInt(player->getPosition(), BS));
- mapper->setAngle(player->getYaw());
- }
-
- /*
- End scene
+ ==================== End scene ====================
*/
if (++m_reset_HW_buffer_counter > 500) {
/*
@@ -3945,11 +4009,12 @@ void Game::updateFrame(ProfilerGraph *graph, RunStats *stats, f32 dtime,
driver->removeAllHardwareBuffers();
m_reset_HW_buffer_counter = 0;
}
+
driver->endScene();
stats->drawtime = tt_draw.stop(true);
- g_profiler->avg("Game::updateFrame(): draw scene [ms]", stats->drawtime);
- g_profiler->graphAdd("Update frame [ms]", tt_update.stop(true));
+ g_profiler->graphAdd("Draw scene [us]", stats->drawtime);
+ g_profiler->avg("Game::updateFrame(): update frame [ms]", tt_update.stop(true));
}
/* Log times and stuff for visualization */
@@ -3960,59 +4025,85 @@ inline void Game::updateProfilerGraphs(ProfilerGraph *graph)
graph->put(values);
}
+/****************************************************************************
+ * Shadows
+ *****************************************************************************/
+void Game::updateShadows()
+{
+ ShadowRenderer *shadow = RenderingEngine::get_shadow_renderer();
+ if (!shadow)
+ return;
+
+ float in_timeofday = fmod(runData.time_of_day_smooth, 1.0f);
+
+ float timeoftheday = fmod(getWickedTimeOfDay(in_timeofday) + 0.75f, 0.5f) + 0.25f;
+ const float offset_constant = 10000.0f;
+
+ v3f light(0.0f, 0.0f, -1.0f);
+ light.rotateXZBy(90);
+ light.rotateXYBy(timeoftheday * 360 - 90);
+ light.rotateYZBy(sky->getSkyBodyOrbitTilt());
+ v3f sun_pos = light * offset_constant;
+
+ if (shadow->getDirectionalLightCount() == 0)
+ shadow->addDirectionalLight();
+ shadow->getDirectionalLight().setDirection(sun_pos);
+ shadow->setTimeOfDay(in_timeofday);
+
+ shadow->getDirectionalLight().update_frustum(camera, client, m_camera_offset_changed);
+}
/****************************************************************************
Misc
****************************************************************************/
-/* On some computers framerate doesn't seem to be automatically limited
- */
-inline void Game::limitFps(FpsControl *fps_timings, f32 *dtime)
+void FpsControl::reset()
{
- // not using getRealTime is necessary for wine
- device->getTimer()->tick(); // Maker sure device time is up-to-date
- u32 time = device->getTimer()->getTime();
- u32 last_time = fps_timings->last_time;
-
- if (time > last_time) // Make sure time hasn't overflowed
- fps_timings->busy_time = time - last_time;
- else
- fps_timings->busy_time = 0;
+ last_time = porting::getTimeUs();
+}
- u32 frametime_min = 1000 / (
+/*
+ * On some computers framerate doesn't seem to be automatically limited
+ */
+void FpsControl::limit(IrrlichtDevice *device, f32 *dtime)
+{
+ const u64 frametime_min = 1000000.0f / (
device->isWindowFocused() && !g_menumgr.pausesGame()
? g_settings->getFloat("fps_max")
: g_settings->getFloat("fps_max_unfocused"));
- if (fps_timings->busy_time < frametime_min) {
- fps_timings->sleep_time = frametime_min - fps_timings->busy_time;
- device->sleep(fps_timings->sleep_time);
+ u64 time = porting::getTimeUs();
+
+ if (time > last_time) // Make sure time hasn't overflowed
+ busy_time = time - last_time;
+ else
+ busy_time = 0;
+
+ if (busy_time < frametime_min) {
+ sleep_time = frametime_min - busy_time;
+ if (sleep_time > 1000)
+ sleep_ms(sleep_time / 1000);
} else {
- fps_timings->sleep_time = 0;
+ sleep_time = 0;
}
- /* Get the new value of the device timer. Note that device->sleep() may
- * not sleep for the entire requested time as sleep may be interrupted and
- * therefore it is arguably more accurate to get the new time from the
- * device rather than calculating it by adding sleep_time to time.
- */
-
- device->getTimer()->tick(); // Update device timer
- time = device->getTimer()->getTime();
+ // Read the timer again to accurately determine how long we actually slept,
+ // rather than calculating it by adding sleep_time to time.
+ time = porting::getTimeUs();
- if (time > last_time) // Make sure last_time hasn't overflowed
- *dtime = (time - last_time) / 1000.0;
+ if (time > last_time) // Make sure last_time hasn't overflowed
+ *dtime = (time - last_time) / 1000000.0f;
else
*dtime = 0;
- fps_timings->last_time = time;
+ last_time = time;
}
void Game::showOverlayMessage(const char *msg, float dtime, int percent, bool draw_clouds)
{
const wchar_t *wmsg = wgettext(msg);
- RenderingEngine::draw_load_screen(wmsg, guienv, texture_src, dtime, percent,
+ m_rendering_engine->draw_load_screen(wmsg, guienv, texture_src, dtime, percent,
draw_clouds);
delete[] wmsg;
}
@@ -4057,27 +4148,6 @@ void Game::readSettings()
****************************************************************************/
/****************************************************************************/
-void Game::extendedResourceCleanup()
-{
- // Extended resource accounting
- infostream << "Irrlicht resources after cleanup:" << std::endl;
- infostream << "\tRemaining meshes : "
- << RenderingEngine::get_mesh_cache()->getMeshCount() << std::endl;
- infostream << "\tRemaining textures : "
- << driver->getTextureCount() << std::endl;
-
- for (unsigned int i = 0; i < driver->getTextureCount(); i++) {
- irr::video::ITexture *texture = driver->getTextureByIndex(i);
- infostream << "\t\t" << i << ":" << texture->getName().getPath().c_str()
- << std::endl;
- }
-
- clearTextureNameCache();
- infostream << "\tRemaining materials: "
- << driver-> getMaterialRendererCount()
- << " (note: irrlicht doesn't support removing renderers)" << std::endl;
-}
-
void Game::showDeathFormspec()
{
static std::string formspec_str =
@@ -4095,15 +4165,15 @@ void Game::showDeathFormspec()
LocalFormspecHandler *txt_dst = new LocalFormspecHandler("MT_DEATH_SCREEN", client);
auto *&formspec = m_game_ui->getFormspecGUI();
- GUIFormSpecMenu::create(formspec, client, &input->joystick,
- fs_src, txt_dst, client->getFormspecPrepend(), sound);
+ GUIFormSpecMenu::create(formspec, client, m_rendering_engine->get_gui_env(),
+ &input->joystick, fs_src, txt_dst, client->getFormspecPrepend(), sound);
formspec->setFocus("btn_respawn");
}
#define GET_KEY_NAME(KEY) gettext(getKeySetting(#KEY).name())
void Game::showPauseMenu()
{
-#ifdef __ANDROID__
+#ifdef HAVE_TOUCHSCREENGUI
static const std::string control_text = strgettext("Default Controls:\n"
"No menu visible:\n"
"- single tap: button activate\n"
@@ -4203,16 +4273,18 @@ void Game::showPauseMenu()
if (simple_singleplayer_mode || address.empty()) {
static const std::string on = strgettext("On");
static const std::string off = strgettext("Off");
- const std::string &damage = g_settings->getBool("enable_damage") ? on : off;
- const std::string &creative = g_settings->getBool("creative_mode") ? on : off;
+ // Note: Status of enable_damage and creative_mode settings is intentionally
+ // NOT shown here because the game might roll its own damage system and/or do
+ // a per-player Creative Mode, in which case writing it here would mislead.
+ bool damage = g_settings->getBool("enable_damage");
const std::string &announced = g_settings->getBool("server_announce") ? on : off;
- os << strgettext("- Damage: ") << damage << "\n"
- << strgettext("- Creative Mode: ") << creative << "\n";
if (!simple_singleplayer_mode) {
- const std::string &pvp = g_settings->getBool("enable_pvp") ? on : off;
- //~ PvP = Player versus Player
- os << strgettext("- PvP: ") << pvp << "\n"
- << strgettext("- Public: ") << announced << "\n";
+ if (damage) {
+ const std::string &pvp = g_settings->getBool("enable_pvp") ? on : off;
+ //~ PvP = Player versus Player
+ os << strgettext("- PvP: ") << pvp << "\n";
+ }
+ os << strgettext("- Public: ") << announced << "\n";
std::string server_name = g_settings->get("server_name");
str_formspec_escape(server_name);
if (announced == on && !server_name.empty())
@@ -4229,10 +4301,13 @@ void Game::showPauseMenu()
LocalFormspecHandler *txt_dst = new LocalFormspecHandler("MT_PAUSE_MENU");
auto *&formspec = m_game_ui->getFormspecGUI();
- GUIFormSpecMenu::create(formspec, client, &input->joystick,
- fs_src, txt_dst, client->getFormspecPrepend(), sound);
+ GUIFormSpecMenu::create(formspec, client, m_rendering_engine->get_gui_env(),
+ &input->joystick, fs_src, txt_dst, client->getFormspecPrepend(), sound);
formspec->setFocus("btn_continue");
formspec->doPause = true;
+
+ if (simple_singleplayer_mode)
+ pauseAnimation();
}
/****************************************************************************/
@@ -4243,6 +4318,7 @@ void Game::showPauseMenu()
void the_game(bool *kill,
InputHandler *input,
+ RenderingEngine *rendering_engine,
const GameStartData &start_data,
std::string &error_message,
ChatBackend &chat_backend,
@@ -4257,20 +4333,21 @@ void the_game(bool *kill,
try {
- if (game.startup(kill, input, start_data, error_message,
- reconnect_requested, &chat_backend)) {
+ if (game.startup(kill, input, rendering_engine, start_data,
+ error_message, reconnect_requested, &chat_backend)) {
game.run();
}
} catch (SerializationError &e) {
- error_message = std::string("A serialization error occurred:\n")
- + e.what() + "\n\nThe server is probably "
- " running a different version of " PROJECT_NAME_C ".";
+ const std::string ver_err = fmtgettext("The server is probably running a different version of %s.", PROJECT_NAME_C);
+ error_message = strgettext("A serialization error occurred:") +"\n"
+ + e.what() + "\n\n" + ver_err;
errorstream << error_message << std::endl;
} catch (ServerError &e) {
error_message = e.what();
errorstream << "ServerError: " << error_message << std::endl;
} catch (ModError &e) {
+ // DO NOT TRANSLATE the `ModError`, it's used by ui.lua
error_message = std::string("ModError: ") + e.what() +
strgettext("\nCheck debug.txt for details.");
errorstream << error_message << std::endl;
diff --git a/src/client/game.h b/src/client/game.h
index d04153271..d87e747c5 100644
--- a/src/client/game.h
+++ b/src/client/game.h
@@ -23,7 +23,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <string>
class InputHandler;
-class ChatBackend; /* to avoid having to include chat.h */
+class ChatBackend;
+class RenderingEngine;
struct SubgameSpec;
struct GameStartData;
@@ -32,7 +33,7 @@ struct Jitter {
};
struct RunStats {
- u32 drawtime;
+ u64 drawtime; // (us)
Jitter dtime_jitter, busy_time_jitter;
};
@@ -45,6 +46,7 @@ struct CameraOrientation {
void the_game(bool *kill,
InputHandler *input,
+ RenderingEngine *rendering_engine,
const GameStartData &start_data,
std::string &error_message,
ChatBackend &chat_backend,
diff --git a/src/client/gameui.cpp b/src/client/gameui.cpp
index 0c08efeb5..8505ea3ae 100644
--- a/src/client/gameui.cpp
+++ b/src/client/gameui.cpp
@@ -71,11 +71,14 @@ void GameUI::init()
chat_font_size, FM_Unspecified));
}
- // At the middle of the screen
- // Object infos are shown in this
+
+ // Infotext of nodes and objects.
+ // If in debug mode, object debug infos shown here, too.
+ // Located on the left on the screen, below chat.
u32 chat_font_height = m_guitext_chat->getActiveFont()->getDimension(L"Ay").Height;
m_guitext_info = gui::StaticText::add(guienv, L"",
- core::rect<s32>(0, 0, 400, g_fontengine->getTextHeight() * 5 + 5) +
+ // Size is limited; text will be truncated after 6 lines.
+ core::rect<s32>(0, 0, 400, g_fontengine->getTextHeight() * 6) +
v2s32(100, chat_font_height *
(g_settings->getU16("recent_chat_messages") + 3)),
false, true, guiroot);
@@ -97,19 +100,20 @@ void GameUI::update(const RunStats &stats, Client *client, MapDrawControl *draw_
const CameraOrientation &cam, const PointedThing &pointed_old,
const GUIChatConsole *chat_console, float dtime)
{
- v2u32 screensize = RenderingEngine::get_instance()->getWindowSize();
+ v2u32 screensize = RenderingEngine::getWindowSize();
- if (m_flags.show_debug) {
- static float drawtime_avg = 0;
- drawtime_avg = drawtime_avg * 0.95 + stats.drawtime * 0.05;
- u16 fps = 1.0 / stats.dtime_jitter.avg;
+ // Minimal debug text must only contain info that can't give a gameplay advantage
+ if (m_flags.show_minimal_debug) {
+ const u16 fps = 1.0 / stats.dtime_jitter.avg;
+ m_drawtime_avg *= 0.95f;
+ m_drawtime_avg += 0.05f * (stats.drawtime / 1000);
std::ostringstream os(std::ios_base::binary);
os << std::fixed
<< PROJECT_NAME_C " " << g_version_hash
<< " | FPS: " << fps
<< std::setprecision(0)
- << " | drawtime: " << drawtime_avg << "ms"
+ << " | drawtime: " << m_drawtime_avg << "ms"
<< std::setprecision(1)
<< " | dtime jitter: "
<< (stats.dtime_jitter.max_fraction * 100.0) << "%"
@@ -125,9 +129,10 @@ void GameUI::update(const RunStats &stats, Client *client, MapDrawControl *draw_
}
// Finally set the guitext visible depending on the flag
- m_guitext->setVisible(m_flags.show_debug);
+ m_guitext->setVisible(m_flags.show_minimal_debug);
- if (m_flags.show_debug) {
+ // Basic debug text also shows info that might give a gameplay advantage
+ if (m_flags.show_basic_debug) {
LocalPlayer *player = client->getEnv().getLocalPlayer();
v3f player_position = player->getPosition();
@@ -160,7 +165,7 @@ void GameUI::update(const RunStats &stats, Client *client, MapDrawControl *draw_
));
}
- m_guitext2->setVisible(m_flags.show_debug);
+ m_guitext2->setVisible(m_flags.show_basic_debug);
setStaticText(m_guitext_info, m_infotext.c_str());
m_guitext_info->setVisible(m_flags.show_hud && g_menumgr.menuCount() == 0);
@@ -204,7 +209,7 @@ void GameUI::update(const RunStats &stats, Client *client, MapDrawControl *draw_
void GameUI::initFlags()
{
m_flags = GameUI::Flags();
- m_flags.show_debug = g_settings->getBool("show_debug");
+ m_flags.show_minimal_debug = g_settings->getBool("show_debug");
}
void GameUI::showMinimap(bool show)
@@ -221,24 +226,32 @@ void GameUI::showTranslatedStatusText(const char *str)
void GameUI::setChatText(const EnrichedString &chat_text, u32 recent_chat_count)
{
+ setStaticText(m_guitext_chat, chat_text);
+
+ m_recent_chat_count = recent_chat_count;
+}
+void GameUI::updateChatSize()
+{
// Update gui element size and position
s32 chat_y = 5;
- if (m_flags.show_debug)
- chat_y += 2 * g_fontengine->getLineHeight();
+ if (m_flags.show_minimal_debug)
+ chat_y += g_fontengine->getLineHeight();
+ if (m_flags.show_basic_debug)
+ chat_y += g_fontengine->getLineHeight();
- const v2u32 &window_size = RenderingEngine::get_instance()->getWindowSize();
+ const v2u32 &window_size = RenderingEngine::getWindowSize();
- core::rect<s32> chat_size(10, chat_y,
- window_size.X - 20, 0);
+ core::rect<s32> chat_size(10, chat_y, window_size.X - 20, 0);
chat_size.LowerRightCorner.Y = std::min((s32)window_size.Y,
- m_guitext_chat->getTextHeight() + chat_y);
+ m_guitext_chat->getTextHeight() + chat_y);
- m_guitext_chat->setRelativePosition(chat_size);
- setStaticText(m_guitext_chat, chat_text);
+ if (chat_size == m_current_chat_size)
+ return;
+ m_current_chat_size = chat_size;
- m_recent_chat_count = recent_chat_count;
+ m_guitext_chat->setRelativePosition(chat_size);
}
void GameUI::updateProfiler()
@@ -260,7 +273,7 @@ void GameUI::updateProfiler()
core::position2di upper_left(6, 50);
core::position2di lower_right = upper_left;
lower_right.X += size.Width + 10;
- lower_right.Y += size.Height;
+ lower_right.Y += size.Height;
m_guitext_profiler->setRelativePosition(core::rect<s32>(upper_left, lower_right));
}
@@ -294,12 +307,9 @@ void GameUI::toggleProfiler()
updateProfiler();
if (m_profiler_current_page != 0) {
- wchar_t buf[255];
- const wchar_t* str = wgettext("Profiler shown (page %d of %d)");
- swprintf(buf, sizeof(buf) / sizeof(wchar_t), str,
- m_profiler_current_page, m_profiler_max_page);
- delete[] str;
- showStatusText(buf);
+ std::wstring msg = fwgettext("Profiler shown (page %d of %d)",
+ m_profiler_current_page, m_profiler_max_page);
+ showStatusText(msg);
} else {
showTranslatedStatusText("Profiler hidden");
}
diff --git a/src/client/gameui.h b/src/client/gameui.h
index b6c8a224d..cc9377bdc 100644
--- a/src/client/gameui.h
+++ b/src/client/gameui.h
@@ -58,7 +58,8 @@ public:
bool show_chat = true;
bool show_hud = true;
bool show_minimap = false;
- bool show_debug = true;
+ bool show_minimal_debug = false;
+ bool show_basic_debug = false;
bool show_profiler_graph = false;
};
@@ -83,11 +84,12 @@ public:
void showTranslatedStatusText(const char *str);
inline void clearStatusText() { m_statustext.clear(); }
- const bool isChatVisible()
+ bool isChatVisible()
{
return m_flags.show_chat && m_recent_chat_count != 0 && m_profiler_current_page == 0;
}
void setChatText(const EnrichedString &chat_text, u32 recent_chat_count);
+ void updateChatSize();
void updateProfiler();
@@ -108,6 +110,8 @@ public:
private:
Flags m_flags;
+ float m_drawtime_avg = 0;
+
gui::IGUIStaticText *m_guitext = nullptr; // First line of debug text
gui::IGUIStaticText *m_guitext2 = nullptr; // Second line of debug text
@@ -121,6 +125,7 @@ private:
gui::IGUIStaticText *m_guitext_chat = nullptr; // Chat text
u32 m_recent_chat_count = 0;
+ core::rect<s32> m_current_chat_size{0, 0, 0, 0};
gui::IGUIStaticText *m_guitext_profiler = nullptr; // Profiler text
u8 m_profiler_current_page = 0;
diff --git a/src/client/guiscalingfilter.cpp b/src/client/guiscalingfilter.cpp
index 406c096e6..232219237 100644
--- a/src/client/guiscalingfilter.cpp
+++ b/src/client/guiscalingfilter.cpp
@@ -23,7 +23,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "util/numeric.h"
#include <cstdio>
#include "client/renderingengine.h"
-#include "client/tile.h" // hasNPotSupport()
/* Maintain a static cache to store the images that correspond to textures
* in a format that's manipulable by code. Some platforms exhibit issues
@@ -102,7 +101,7 @@ video::ITexture *guiScalingResizeCached(video::IVideoDriver *driver,
if (!g_settings->getBool("gui_scaling_filter_txr2img"))
return src;
srcimg = driver->createImageFromData(src->getColorFormat(),
- src->getSize(), src->lock(), false);
+ src->getSize(), src->lock(video::ETLM_READ_ONLY), false);
src->unlock();
g_imgCache[origname] = srcimg;
}
@@ -117,7 +116,7 @@ video::ITexture *guiScalingResizeCached(video::IVideoDriver *driver,
#if ENABLE_GLES
// Some platforms are picky about textures being powers of 2, so expand
// the image dimensions to the next power of 2, if necessary.
- if (!hasNPotSupport()) {
+ if (!driver->queryFeature(video::EVDF_TEXTURE_NPOT)) {
video::IImage *po2img = driver->createImage(src->getColorFormat(),
core::dimension2d<u32>(npot2((u32)destrect.getWidth()),
npot2((u32)destrect.getHeight())));
@@ -129,7 +128,7 @@ video::ITexture *guiScalingResizeCached(video::IVideoDriver *driver,
#endif
// Convert the scaled image back into a texture.
- scaled = driver->addTexture(scalename, destimg, NULL);
+ scaled = driver->addTexture(scalename, destimg);
destimg->drop();
g_txrCache[scalename] = scaled;
diff --git a/src/client/hud.cpp b/src/client/hud.cpp
index 3de116c06..6011a8cff 100644
--- a/src/client/hud.cpp
+++ b/src/client/hud.cpp
@@ -20,6 +20,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
*/
#include "client/hud.h"
+#include <string>
+#include <iostream>
#include <cmath>
#include "settings.h"
#include "util/numeric.h"
@@ -45,11 +47,10 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#define OBJECT_CROSSHAIR_LINE_SIZE 8
#define CROSSHAIR_LINE_SIZE 10
-Hud::Hud(gui::IGUIEnvironment *guienv, Client *client, LocalPlayer *player,
+Hud::Hud(Client *client, LocalPlayer *player,
Inventory *inventory)
{
driver = RenderingEngine::get_video_driver();
- this->guienv = guienv;
this->client = client;
this->player = player;
this->inventory = inventory;
@@ -223,6 +224,7 @@ void Hud::drawItem(const ItemStack &item, const core::rect<s32>& rect,
}
//NOTE: selectitem = 0 -> no selected; selectitem 1-based
+// mainlist can be NULL, but draw the frame anyway.
void Hud::drawItems(v2s32 upperleftpos, v2s32 screen_offset, s32 itemcount,
s32 inv_offset, InventoryList *mainlist, u16 selectitem, u16 direction)
{
@@ -270,7 +272,8 @@ void Hud::drawItems(v2s32 upperleftpos, v2s32 screen_offset, s32 itemcount,
// Draw items
core::rect<s32> imgrect(0, 0, m_hotbar_imagesize, m_hotbar_imagesize);
- for (s32 i = inv_offset; i < itemcount && (size_t)i < mainlist->getSize(); i++) {
+ const s32 list_size = mainlist ? mainlist->getSize() : 0;
+ for (s32 i = inv_offset; i < itemcount && i < list_size; i++) {
s32 fullimglen = m_hotbar_imagesize + m_padding * 2;
v2s32 steppos;
@@ -315,7 +318,7 @@ bool Hud::calculateScreenPos(const v3s16 &camera_offset, HudElement *e, v2s32 *p
{
v3f w_pos = e->world_pos * BS;
scene::ICameraSceneNode* camera =
- RenderingEngine::get_scene_manager()->getActiveCamera();
+ client->getSceneManager()->getActiveCamera();
w_pos -= intToFloat(camera_offset, BS);
core::matrix4 trans = camera->getProjectionMatrix();
trans *= camera->getViewMatrix();
@@ -332,8 +335,8 @@ bool Hud::calculateScreenPos(const v3s16 &camera_offset, HudElement *e, v2s32 *p
void Hud::drawLuaElements(const v3s16 &camera_offset)
{
- u32 text_height = g_fontengine->getTextHeight();
- irr::gui::IGUIFont* font = g_fontengine->getFont();
+ const u32 text_height = g_fontengine->getTextHeight();
+ gui::IGUIFont *const font = g_fontengine->getFont();
// Reorder elements by z_index
std::vector<HudElement*> elems;
@@ -357,40 +360,40 @@ void Hud::drawLuaElements(const v3s16 &camera_offset)
floor(e->pos.Y * (float) m_screensize.Y + 0.5));
switch (e->type) {
case HUD_ELEM_TEXT: {
- irr::gui::IGUIFont *textfont = font;
unsigned int font_size = g_fontengine->getDefaultFontSize();
if (e->size.X > 0)
font_size *= e->size.X;
- if (font_size != g_fontengine->getDefaultFontSize())
- textfont = g_fontengine->getFont(font_size);
+#ifdef __ANDROID__
+ // The text size on Android is not proportional with the actual scaling
+ // FIXME: why do we have such a weird unportable hack??
+ if (font_size > 3 && e->offset.X < -20)
+ font_size -= 3;
+#endif
+ auto textfont = g_fontengine->getFont(FontSpec(font_size,
+ (e->style & HUD_STYLE_MONO) ? FM_Mono : FM_Unspecified,
+ e->style & HUD_STYLE_BOLD, e->style & HUD_STYLE_ITALIC));
video::SColor color(255, (e->number >> 16) & 0xFF,
(e->number >> 8) & 0xFF,
(e->number >> 0) & 0xFF);
std::wstring text = unescape_translate(utf8_to_wide(e->text));
core::dimension2d<u32> textsize = textfont->getDimension(text.c_str());
-#ifdef __ANDROID__
- // The text size on Android is not proportional with the actual scaling
- irr::gui::IGUIFont *font_scaled = font_size <= 3 ?
- textfont : g_fontengine->getFont(font_size - 3);
- if (e->offset.X < -20)
- textsize = font_scaled->getDimension(text.c_str());
-#endif
- v2s32 offset((e->align.X - 1.0) * (textsize.Width / 2),
- (e->align.Y - 1.0) * (textsize.Height / 2));
+
+ v2s32 offset(0, (e->align.Y - 1.0) * (textsize.Height / 2));
core::rect<s32> size(0, 0, e->scale.X * m_scale_factor,
- text_height * e->scale.Y * m_scale_factor);
+ text_height * e->scale.Y * m_scale_factor);
v2s32 offs(e->offset.X * m_scale_factor,
- e->offset.Y * m_scale_factor);
-#ifdef __ANDROID__
- if (e->offset.X < -20)
- font_scaled->draw(text.c_str(), size + pos + offset + offs, color);
- else
-#endif
+ e->offset.Y * m_scale_factor);
+ std::wstringstream wss(text);
+ std::wstring line;
+ while (std::getline(wss, line, L'\n'))
{
- textfont->draw(text.c_str(), size + pos + offset + offs, color);
+ core::dimension2d<u32> linesize = textfont->getDimension(line.c_str());
+ v2s32 line_offset((e->align.X - 1.0) * (linesize.Width / 2), 0);
+ textfont->draw(line.c_str(), size + pos + offset + offs + line_offset, color);
+ offset.Y += linesize.Height;
}
break; }
case HUD_ELEM_STATBAR: {
@@ -400,6 +403,8 @@ void Hud::drawLuaElements(const v3s16 &camera_offset)
break; }
case HUD_ELEM_INVENTORY: {
InventoryList *inv = inventory->getList(e->text);
+ if (!inv)
+ warningstream << "HUD: Unknown inventory list. name=" << e->text << std::endl;
drawItems(pos, v2s32(e->offset.X, e->offset.Y), e->number, 0,
inv, e->item, e->dir);
break; }
@@ -475,7 +480,7 @@ void Hud::drawLuaElements(const v3s16 &camera_offset)
// Angle according to camera view
v3f fore(0.f, 0.f, 1.f);
- scene::ICameraSceneNode *cam = RenderingEngine::get_scene_manager()->getActiveCamera();
+ scene::ICameraSceneNode *cam = client->getSceneManager()->getActiveCamera();
cam->getAbsoluteTransformation().rotateVect(fore);
int angle = - fore.getHorizontalAngle().Y;
@@ -747,7 +752,7 @@ void Hud::drawHotbar(u16 playeritem) {
s32 width = hotbar_itemcount * (m_hotbar_imagesize + m_padding * 2);
v2s32 pos = centerlowerpos - v2s32(width / 2, m_hotbar_imagesize + m_padding * 3);
- const v2u32 &window_size = RenderingEngine::get_instance()->getWindowSize();
+ const v2u32 &window_size = RenderingEngine::getWindowSize();
if ((float) width / (float) window_size.X <=
g_settings->getFloat("hud_hotbar_max_width")) {
if (player->hud_flags & HUD_FLAG_HOTBAR_VISIBLE) {
@@ -862,6 +867,60 @@ void Hud::drawSelectionMesh()
}
}
+enum Hud::BlockBoundsMode Hud::toggleBlockBounds()
+{
+ m_block_bounds_mode = static_cast<BlockBoundsMode>(m_block_bounds_mode + 1);
+
+ if (m_block_bounds_mode >= BLOCK_BOUNDS_MAX) {
+ m_block_bounds_mode = BLOCK_BOUNDS_OFF;
+ }
+ return m_block_bounds_mode;
+}
+
+void Hud::disableBlockBounds()
+{
+ m_block_bounds_mode = BLOCK_BOUNDS_OFF;
+}
+
+void Hud::drawBlockBounds()
+{
+ if (m_block_bounds_mode == BLOCK_BOUNDS_OFF) {
+ return;
+ }
+
+ video::SMaterial old_material = driver->getMaterial2D();
+ driver->setMaterial(m_selection_material);
+
+ v3s16 pos = player->getStandingNodePos();
+
+ v3s16 blockPos(
+ floorf((float) pos.X / MAP_BLOCKSIZE),
+ floorf((float) pos.Y / MAP_BLOCKSIZE),
+ floorf((float) pos.Z / MAP_BLOCKSIZE)
+ );
+
+ v3f offset = intToFloat(client->getCamera()->getOffset(), BS);
+
+ s8 radius = m_block_bounds_mode == BLOCK_BOUNDS_NEAR ? 2 : 0;
+
+ v3f halfNode = v3f(BS, BS, BS) / 2.0f;
+
+ for (s8 x = -radius; x <= radius; x++)
+ for (s8 y = -radius; y <= radius; y++)
+ for (s8 z = -radius; z <= radius; z++) {
+ v3s16 blockOffset(x, y, z);
+
+ aabb3f box(
+ intToFloat((blockPos + blockOffset) * MAP_BLOCKSIZE, BS) - offset - halfNode,
+ intToFloat(((blockPos + blockOffset) * MAP_BLOCKSIZE) + (MAP_BLOCKSIZE - 1), BS) - offset + halfNode
+ );
+
+ driver->draw3DBox(box, video::SColor(255, 255, 0, 0));
+ }
+
+ driver->setMaterial(old_material);
+}
+
void Hud::updateSelectionMesh(const v3s16 &camera_offset)
{
m_camera_offset = camera_offset;
@@ -908,7 +967,7 @@ void Hud::updateSelectionMesh(const v3s16 &camera_offset)
}
void Hud::resizeHotbar() {
- const v2u32 &window_size = RenderingEngine::get_instance()->getWindowSize();
+ const v2u32 &window_size = RenderingEngine::getWindowSize();
if (m_screensize != window_size) {
m_hotbar_imagesize = floor(HOTBAR_IMAGE_SIZE *
@@ -945,12 +1004,24 @@ void drawItemStack(
return;
}
+ const static thread_local bool enable_animations =
+ g_settings->getBool("inventory_items_animations");
+
const ItemDefinition &def = item.getDefinition(client->idef());
- ItemMesh *imesh = client->idef()->getWieldMesh(def.name, client);
- if (imesh && imesh->mesh) {
+ bool draw_overlay = false;
+
+ bool has_mesh = false;
+ ItemMesh *imesh;
+
+ // Render as mesh if animated or no inventory image
+ if ((enable_animations && rotation_kind < IT_ROT_NONE) || def.inventory_image.empty()) {
+ imesh = client->idef()->getWieldMesh(def.name, client);
+ has_mesh = imesh && imesh->mesh;
+ }
+ if (has_mesh) {
scene::IMesh *mesh = imesh->mesh;
- driver->clearZBuffer();
+ driver->clearBuffers(video::ECBF_DEPTH);
s32 delta = 0;
if (rotation_kind < IT_ROT_NONE) {
MeshTimeInfo &ti = rotation_time_infos[rotation_kind];
@@ -992,9 +1063,6 @@ void drawItemStack(
core::matrix4 matrix;
matrix.makeIdentity();
- static thread_local bool enable_animations =
- g_settings->getBool("inventory_items_animations");
-
if (enable_animations) {
float timer_f = (float) delta / 5000.f;
matrix.setRotationDegrees(v3f(
@@ -1040,15 +1108,36 @@ void drawItemStack(
driver->setTransform(video::ETS_PROJECTION, oldProjMat);
driver->setViewPort(oldViewPort);
- // draw the inventory_overlay
- if (def.type == ITEM_NODE && def.inventory_image.empty() &&
- !def.inventory_overlay.empty()) {
+ draw_overlay = def.type == ITEM_NODE && def.inventory_image.empty();
+ } else { // Otherwise just draw as 2D
+ video::ITexture *texture = client->idef()->getInventoryTexture(def.name, client);
+ video::SColor color;
+ if (texture) {
+ color = client->idef()->getItemstackColor(item, client);
+ } else {
+ color = video::SColor(255, 255, 255, 255);
ITextureSource *tsrc = client->getTextureSource();
- video::ITexture *overlay_texture = tsrc->getTexture(def.inventory_overlay);
- core::dimension2d<u32> dimens = overlay_texture->getOriginalSize();
- core::rect<s32> srcrect(0, 0, dimens.Width, dimens.Height);
- draw2DImageFilterScaled(driver, overlay_texture, rect, srcrect, clip, 0, true);
+ texture = tsrc->getTexture("no_texture.png");
+ if (!texture)
+ return;
}
+
+ const video::SColor colors[] = { color, color, color, color };
+
+ draw2DImageFilterScaled(driver, texture, rect,
+ core::rect<s32>({0, 0}, core::dimension2di(texture->getOriginalSize())),
+ clip, colors, true);
+
+ draw_overlay = true;
+ }
+
+ // draw the inventory_overlay
+ if (!def.inventory_overlay.empty() && draw_overlay) {
+ ITextureSource *tsrc = client->getTextureSource();
+ video::ITexture *overlay_texture = tsrc->getTexture(def.inventory_overlay);
+ core::dimension2d<u32> dimens = overlay_texture->getOriginalSize();
+ core::rect<s32> srcrect(0, 0, dimens.Width, dimens.Height);
+ draw2DImageFilterScaled(driver, overlay_texture, rect, srcrect, clip, 0, true);
}
if (def.type == ITEM_TOOL && item.wear != 0) {
diff --git a/src/client/hud.h b/src/client/hud.h
index d46545d71..fd79183a0 100644
--- a/src/client/hud.h
+++ b/src/client/hud.h
@@ -35,13 +35,13 @@ struct ItemStack;
class Hud
{
public:
- video::IVideoDriver *driver;
- scene::ISceneManager *smgr;
- gui::IGUIEnvironment *guienv;
- Client *client;
- LocalPlayer *player;
- Inventory *inventory;
- ITextureSource *tsrc;
+ enum BlockBoundsMode
+ {
+ BLOCK_BOUNDS_OFF,
+ BLOCK_BOUNDS_CURRENT,
+ BLOCK_BOUNDS_NEAR,
+ BLOCK_BOUNDS_MAX
+ } m_block_bounds_mode = BLOCK_BOUNDS_OFF;
video::SColor crosshair_argb;
video::SColor selectionbox_argb;
@@ -55,10 +55,14 @@ public:
bool pointing_at_object = false;
- Hud(gui::IGUIEnvironment *guienv, Client *client, LocalPlayer *player,
+ Hud(Client *client, LocalPlayer *player,
Inventory *inventory);
~Hud();
+ enum BlockBoundsMode toggleBlockBounds();
+ void disableBlockBounds();
+ void drawBlockBounds();
+
void drawHotbar(u16 playeritem);
void resizeHotbar();
void drawCrosshair();
@@ -103,6 +107,12 @@ private:
void drawCompassRotate(HudElement *e, video::ITexture *texture,
const core::rect<s32> &rect, int way);
+ Client *client = nullptr;
+ video::IVideoDriver *driver = nullptr;
+ LocalPlayer *player = nullptr;
+ Inventory *inventory = nullptr;
+ ITextureSource *tsrc = nullptr;
+
float m_hud_scaling; // cached minetest setting
float m_scale_factor;
v3s16 m_camera_offset;
diff --git a/src/client/imagefilters.cpp b/src/client/imagefilters.cpp
index 0fa501410..b62e336f7 100644
--- a/src/client/imagefilters.cpp
+++ b/src/client/imagefilters.cpp
@@ -19,63 +19,140 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "imagefilters.h"
#include "util/numeric.h"
#include <cmath>
+#include <cassert>
+#include <vector>
+#include <algorithm>
+
+// Simple 2D bitmap class with just the functionality needed here
+class Bitmap {
+ u32 linesize, lines;
+ std::vector<u8> data;
+
+ static inline u32 bytepos(u32 index) { return index >> 3; }
+ static inline u8 bitpos(u32 index) { return index & 7; }
+
+public:
+ Bitmap(u32 width, u32 height) : linesize(width), lines(height),
+ data(bytepos(width * height) + 1) {}
+
+ inline bool get(u32 x, u32 y) const {
+ u32 index = y * linesize + x;
+ return data[bytepos(index)] & (1 << bitpos(index));
+ }
+
+ inline void set(u32 x, u32 y) {
+ u32 index = y * linesize + x;
+ data[bytepos(index)] |= 1 << bitpos(index);
+ }
+
+ inline bool all() const {
+ for (u32 i = 0; i < data.size() - 1; i++) {
+ if (data[i] != 0xff)
+ return false;
+ }
+ // last byte not entirely filled
+ for (u8 i = 0; i < bitpos(linesize * lines); i++) {
+ bool value_of_bit = data.back() & (1 << i);
+ if (!value_of_bit)
+ return false;
+ }
+ return true;
+ }
+
+ inline void copy(Bitmap &to) const {
+ assert(to.linesize == linesize && to.lines == lines);
+ to.data = data;
+ }
+};
/* Fill in RGB values for transparent pixels, to correct for odd colors
* appearing at borders when blending. This is because many PNG optimizers
* like to discard RGB values of transparent pixels, but when blending then
- * with non-transparent neighbors, their RGB values will shpw up nonetheless.
+ * with non-transparent neighbors, their RGB values will show up nonetheless.
*
* This function modifies the original image in-place.
*
* Parameter "threshold" is the alpha level below which pixels are considered
- * transparent. Should be 127 for 3d where alpha is threshold, but 0 for
- * 2d where alpha is blended.
+ * transparent. Should be 127 when the texture is used with ALPHA_CHANNEL_REF,
+ * 0 when alpha blending is used.
*/
void imageCleanTransparent(video::IImage *src, u32 threshold)
{
core::dimension2d<u32> dim = src->getDimension();
- // Walk each pixel looking for fully transparent ones.
+ Bitmap bitmap(dim.Width, dim.Height);
+
+ // First pass: Mark all opaque pixels
// Note: loop y around x for better cache locality.
for (u32 ctry = 0; ctry < dim.Height; ctry++)
for (u32 ctrx = 0; ctrx < dim.Width; ctrx++) {
+ if (src->getPixel(ctrx, ctry).getAlpha() > threshold)
+ bitmap.set(ctrx, ctry);
+ }
+
+ // Exit early if all pixels opaque
+ if (bitmap.all())
+ return;
+
+ Bitmap newmap = bitmap;
+
+ // Cap iterations to keep runtime reasonable, for higher-res textures we can
+ // get away with filling less pixels.
+ int iter_max = 11 - std::max(dim.Width, dim.Height) / 16;
+ iter_max = std::max(iter_max, 2);
- // Ignore opaque pixels.
- irr::video::SColor c = src->getPixel(ctrx, ctry);
- if (c.getAlpha() > threshold)
+ // Then repeatedly look for transparent pixels, filling them in until
+ // we're finished.
+ for (int iter = 0; iter < iter_max; iter++) {
+
+ for (u32 ctry = 0; ctry < dim.Height; ctry++)
+ for (u32 ctrx = 0; ctrx < dim.Width; ctrx++) {
+ // Skip pixels we have already processed
+ if (bitmap.get(ctrx, ctry))
continue;
- // Sample size and total weighted r, g, b values.
+ video::SColor c = src->getPixel(ctrx, ctry);
+
+ // Sample size and total weighted r, g, b values
u32 ss = 0, sr = 0, sg = 0, sb = 0;
- // Walk each neighbor pixel (clipped to image bounds).
+ // Walk each neighbor pixel (clipped to image bounds)
for (u32 sy = (ctry < 1) ? 0 : (ctry - 1);
sy <= (ctry + 1) && sy < dim.Height; sy++)
for (u32 sx = (ctrx < 1) ? 0 : (ctrx - 1);
sx <= (ctrx + 1) && sx < dim.Width; sx++) {
-
- // Ignore transparent pixels.
- irr::video::SColor d = src->getPixel(sx, sy);
- if (d.getAlpha() <= threshold)
+ // Ignore pixels we haven't processed
+ if (!bitmap.get(sx, sy))
continue;
-
- // Add RGB values weighted by alpha.
- u32 a = d.getAlpha();
+
+ // Add RGB values weighted by alpha IF the pixel is opaque, otherwise
+ // use full weight since we want to propagate colors.
+ video::SColor d = src->getPixel(sx, sy);
+ u32 a = d.getAlpha() <= threshold ? 255 : d.getAlpha();
ss += a;
sr += a * d.getRed();
sg += a * d.getGreen();
sb += a * d.getBlue();
}
- // If we found any neighbor RGB data, set pixel to average
- // weighted by alpha.
+ // Set pixel to average weighted by alpha
if (ss > 0) {
c.setRed(sr / ss);
c.setGreen(sg / ss);
c.setBlue(sb / ss);
src->setPixel(ctrx, ctry, c);
+ newmap.set(ctrx, ctry);
}
}
+
+ if (newmap.all())
+ return;
+
+ // Apply changes to bitmap for next run. This is done so we don't introduce
+ // a bias in color propagation in the direction pixels are processed.
+ newmap.copy(bitmap);
+
+ }
}
/* Scale a region of an image into another image, using nearest-neighbor with
diff --git a/src/client/imagefilters.h b/src/client/imagefilters.h
index 5676faf85..c9bdefbb6 100644
--- a/src/client/imagefilters.h
+++ b/src/client/imagefilters.h
@@ -23,13 +23,13 @@ with this program; if not, write to the Free Software Foundation, Inc.,
/* Fill in RGB values for transparent pixels, to correct for odd colors
* appearing at borders when blending. This is because many PNG optimizers
* like to discard RGB values of transparent pixels, but when blending then
- * with non-transparent neighbors, their RGB values will shpw up nonetheless.
+ * with non-transparent neighbors, their RGB values will show up nonetheless.
*
* This function modifies the original image in-place.
*
* Parameter "threshold" is the alpha level below which pixels are considered
- * transparent. Should be 127 for 3d where alpha is threshold, but 0 for
- * 2d where alpha is blended.
+ * transparent. Should be 127 when the texture is used with ALPHA_CHANNEL_REF,
+ * 0 when alpha blending is used.
*/
void imageCleanTransparent(video::IImage *src, u32 threshold);
diff --git a/src/client/inputhandler.cpp b/src/client/inputhandler.cpp
index 978baa320..a6ba87e8d 100644
--- a/src/client/inputhandler.cpp
+++ b/src/client/inputhandler.cpp
@@ -35,7 +35,7 @@ void KeyCache::populate()
key[KeyType::LEFT] = getKeySetting("keymap_left");
key[KeyType::RIGHT] = getKeySetting("keymap_right");
key[KeyType::JUMP] = getKeySetting("keymap_jump");
- key[KeyType::SPECIAL1] = getKeySetting("keymap_special1");
+ key[KeyType::AUX1] = getKeySetting("keymap_aux1");
key[KeyType::SNEAK] = getKeySetting("keymap_sneak");
key[KeyType::DIG] = getKeySetting("keymap_dig");
key[KeyType::PLACE] = getKeySetting("keymap_place");
@@ -60,6 +60,7 @@ void KeyCache::populate()
key[KeyType::DEC_VOLUME] = getKeySetting("keymap_decrease_volume");
key[KeyType::CINEMATIC] = getKeySetting("keymap_cinematic");
key[KeyType::SCREENSHOT] = getKeySetting("keymap_screenshot");
+ key[KeyType::TOGGLE_BLOCK_BOUNDS] = getKeySetting("keymap_toggle_block_bounds");
key[KeyType::TOGGLE_HUD] = getKeySetting("keymap_toggle_hud");
key[KeyType::TOGGLE_CHAT] = getKeySetting("keymap_toggle_chat");
key[KeyType::TOGGLE_FOG] = getKeySetting("keymap_toggle_fog");
@@ -137,11 +138,8 @@ bool MyEventReceiver::OnEvent(const SEvent &event)
#endif
} else if (event.EventType == irr::EET_JOYSTICK_INPUT_EVENT) {
- /* TODO add a check like:
- if (event.JoystickEvent != joystick_we_listen_for)
- return false;
- */
- return joystick->handleEvent(event.JoystickEvent);
+ // joystick may be nullptr if game is launched with '--random-input' parameter
+ return joystick && joystick->handleEvent(event.JoystickEvent);
} else if (event.EventType == irr::EET_MOUSE_INPUT_EVENT) {
// Handle mouse events
KeyPress key;
@@ -219,7 +217,7 @@ void RandomInputHandler::step(float dtime)
{
static RandomInputHandlerSimData rnd_data[] = {
{ "keymap_jump", 0.0f, 40 },
- { "keymap_special1", 0.0f, 40 },
+ { "keymap_aux1", 0.0f, 40 },
{ "keymap_forward", 0.0f, 40 },
{ "keymap_left", 0.0f, 40 },
{ "keymap_dig", 0.0f, 30 },
@@ -242,4 +240,39 @@ void RandomInputHandler::step(float dtime)
}
}
mousepos += mousespeed;
+ static bool useJoystick = false;
+ {
+ static float counterUseJoystick = 0;
+ counterUseJoystick -= dtime;
+ if (counterUseJoystick < 0.0) {
+ counterUseJoystick = 5.0; // switch between joystick and keyboard direction input
+ useJoystick = !useJoystick;
+ }
+ }
+ if (useJoystick) {
+ static float counterMovement = 0;
+ counterMovement -= dtime;
+ if (counterMovement < 0.0) {
+ counterMovement = 0.1 * Rand(1, 40);
+ movementSpeed = Rand(0,100)*0.01;
+ movementDirection = Rand(-100, 100)*0.01 * M_PI;
+ }
+ } else {
+ bool f = keydown[keycache.key[KeyType::FORWARD]],
+ l = keydown[keycache.key[KeyType::LEFT]];
+ if (f || l) {
+ movementSpeed = 1.0f;
+ if (f && !l)
+ movementDirection = 0.0;
+ else if (!f && l)
+ movementDirection = -M_PI_2;
+ else if (f && l)
+ movementDirection = -M_PI_4;
+ else
+ movementDirection = 0.0;
+ } else {
+ movementSpeed = 0.0;
+ movementDirection = 0.0;
+ }
+ }
}
diff --git a/src/client/inputhandler.h b/src/client/inputhandler.h
index 1fb4cf0ec..3db105c51 100644
--- a/src/client/inputhandler.h
+++ b/src/client/inputhandler.h
@@ -152,8 +152,14 @@ public:
// in the subsequent iteration of Game::processPlayerInteraction
bool WasKeyReleased(const KeyPress &keycode) const { return keyWasReleased[keycode]; }
- void listenForKey(const KeyPress &keyCode) { keysListenedFor.set(keyCode); }
- void dontListenForKeys() { keysListenedFor.clear(); }
+ void listenForKey(const KeyPress &keyCode)
+ {
+ keysListenedFor.set(keyCode);
+ }
+ void dontListenForKeys()
+ {
+ keysListenedFor.clear();
+ }
s32 getMouseWheel()
{
@@ -189,8 +195,6 @@ public:
#endif
}
- s32 mouse_wheel = 0;
-
JoystickController *joystick = nullptr;
#ifdef HAVE_TOUCHSCREENGUI
@@ -198,6 +202,8 @@ public:
#endif
private:
+ s32 mouse_wheel = 0;
+
// The current state of keys
KeyList keyIsDown;
@@ -240,6 +246,9 @@ public:
virtual bool wasKeyReleased(GameKeyType k) = 0;
virtual bool cancelPressed() = 0;
+ virtual float getMovementSpeed() = 0;
+ virtual float getMovementDirection() = 0;
+
virtual void clearWasKeyPressed() {}
virtual void clearWasKeyReleased() {}
@@ -269,6 +278,12 @@ public:
{
m_receiver->joystick = &joystick;
}
+
+ virtual ~RealInputHandler()
+ {
+ m_receiver->joystick = nullptr;
+ }
+
virtual bool isKeyDown(GameKeyType k)
{
return m_receiver->IsKeyDown(keycache.key[k]) || joystick.isKeyDown(k);
@@ -285,10 +300,52 @@ public:
{
return m_receiver->WasKeyReleased(keycache.key[k]) || joystick.wasKeyReleased(k);
}
+
+ virtual float getMovementSpeed()
+ {
+ bool f = m_receiver->IsKeyDown(keycache.key[KeyType::FORWARD]),
+ b = m_receiver->IsKeyDown(keycache.key[KeyType::BACKWARD]),
+ l = m_receiver->IsKeyDown(keycache.key[KeyType::LEFT]),
+ r = m_receiver->IsKeyDown(keycache.key[KeyType::RIGHT]);
+ if (f || b || l || r)
+ {
+ // if contradictory keys pressed, stay still
+ if (f && b && l && r)
+ return 0.0f;
+ else if (f && b && !l && !r)
+ return 0.0f;
+ else if (!f && !b && l && r)
+ return 0.0f;
+ return 1.0f; // If there is a keyboard event, assume maximum speed
+ }
+ return joystick.getMovementSpeed();
+ }
+
+ virtual float getMovementDirection()
+ {
+ float x = 0, z = 0;
+
+ /* Check keyboard for input */
+ if (m_receiver->IsKeyDown(keycache.key[KeyType::FORWARD]))
+ z += 1;
+ if (m_receiver->IsKeyDown(keycache.key[KeyType::BACKWARD]))
+ z -= 1;
+ if (m_receiver->IsKeyDown(keycache.key[KeyType::RIGHT]))
+ x += 1;
+ if (m_receiver->IsKeyDown(keycache.key[KeyType::LEFT]))
+ x -= 1;
+
+ if (x != 0 || z != 0) /* If there is a keyboard event, it takes priority */
+ return atan2(x, z);
+ else
+ return joystick.getMovementDirection();
+ }
+
virtual bool cancelPressed()
{
return wasKeyDown(KeyType::ESC) || m_receiver->WasKeyDown(CancelKey);
}
+
virtual void clearWasKeyPressed()
{
m_receiver->clearWasKeyPressed();
@@ -297,17 +354,21 @@ public:
{
m_receiver->clearWasKeyReleased();
}
+
virtual void listenForKey(const KeyPress &keyCode)
{
m_receiver->listenForKey(keyCode);
}
- virtual void dontListenForKeys() { m_receiver->dontListenForKeys(); }
+ virtual void dontListenForKeys()
+ {
+ m_receiver->dontListenForKeys();
+ }
+
virtual v2s32 getMousePos()
{
- if (RenderingEngine::get_raw_device()->getCursorControl()) {
- return RenderingEngine::get_raw_device()
- ->getCursorControl()
- ->getPosition();
+ auto control = RenderingEngine::get_raw_device()->getCursorControl();
+ if (control) {
+ return control->getPosition();
}
return m_mousepos;
@@ -315,16 +376,18 @@ public:
virtual void setMousePos(s32 x, s32 y)
{
- if (RenderingEngine::get_raw_device()->getCursorControl()) {
- RenderingEngine::get_raw_device()
- ->getCursorControl()
- ->setPosition(x, y);
+ auto control = RenderingEngine::get_raw_device()->getCursorControl();
+ if (control) {
+ control->setPosition(x, y);
} else {
m_mousepos = v2s32(x, y);
}
}
- virtual s32 getMouseWheel() { return m_receiver->getMouseWheel(); }
+ virtual s32 getMouseWheel()
+ {
+ return m_receiver->getMouseWheel();
+ }
void clear()
{
@@ -352,6 +415,8 @@ public:
virtual bool wasKeyPressed(GameKeyType k) { return false; }
virtual bool wasKeyReleased(GameKeyType k) { return false; }
virtual bool cancelPressed() { return false; }
+ virtual float getMovementSpeed() { return movementSpeed; }
+ virtual float getMovementDirection() { return movementDirection; }
virtual v2s32 getMousePos() { return mousepos; }
virtual void setMousePos(s32 x, s32 y) { mousepos = v2s32(x, y); }
@@ -365,4 +430,6 @@ private:
KeyList keydown;
v2s32 mousepos;
v2s32 mousespeed;
+ float movementSpeed;
+ float movementDirection;
};
diff --git a/src/client/joystick_controller.cpp b/src/client/joystick_controller.cpp
index f61ae4ae6..aae73c62d 100644
--- a/src/client/joystick_controller.cpp
+++ b/src/client/joystick_controller.cpp
@@ -79,7 +79,7 @@ JoystickLayout create_default_layout()
// Accessible without any modifier pressed
JLO_B_PB(KeyType::JUMP, bm | 1 << 0, 1 << 0);
- JLO_B_PB(KeyType::SPECIAL1, bm | 1 << 1, 1 << 1);
+ JLO_B_PB(KeyType::AUX1, bm | 1 << 1, 1 << 1);
// Accessible with start button not pressed, but four pressed
// TODO find usage for button 0
@@ -126,11 +126,11 @@ JoystickLayout create_xbox_layout()
// 4 Buttons
JLO_B_PB(KeyType::JUMP, 1 << 0, 1 << 0); // A/green
JLO_B_PB(KeyType::ESC, 1 << 1, 1 << 1); // B/red
- JLO_B_PB(KeyType::SPECIAL1, 1 << 2, 1 << 2); // X/blue
+ JLO_B_PB(KeyType::AUX1, 1 << 2, 1 << 2); // X/blue
JLO_B_PB(KeyType::INVENTORY, 1 << 3, 1 << 3); // Y/yellow
// Analog Sticks
- JLO_B_PB(KeyType::SPECIAL1, 1 << 11, 1 << 11); // left
+ JLO_B_PB(KeyType::AUX1, 1 << 11, 1 << 11); // left
JLO_B_PB(KeyType::SNEAK, 1 << 12, 1 << 12); // right
// Triggers
@@ -154,12 +154,61 @@ JoystickLayout create_xbox_layout()
return jlo;
}
+JoystickLayout create_dragonrise_gamecube_layout()
+{
+ JoystickLayout jlo;
+
+ jlo.axes_deadzone = 7000;
+
+ const JoystickAxisLayout axes[JA_COUNT] = {
+ // Control Stick
+ {0, 1}, // JA_SIDEWARD_MOVE
+ {1, 1}, // JA_FORWARD_MOVE
+
+ // C-Stick
+ {3, 1}, // JA_FRUSTUM_HORIZONTAL
+ {4, 1}, // JA_FRUSTUM_VERTICAL
+ };
+ memcpy(jlo.axes, axes, sizeof(jlo.axes));
+
+ // The center button
+ JLO_B_PB(KeyType::ESC, 1 << 9, 1 << 9); // Start/Pause Button
+
+ // Front right buttons
+ JLO_B_PB(KeyType::JUMP, 1 << 2, 1 << 2); // A Button
+ JLO_B_PB(KeyType::SNEAK, 1 << 3, 1 << 3); // B Button
+ JLO_B_PB(KeyType::DROP, 1 << 0, 1 << 0); // Y Button
+ JLO_B_PB(KeyType::AUX1, 1 << 1, 1 << 1); // X Button
+
+ // Triggers
+ JLO_B_PB(KeyType::DIG, 1 << 4, 1 << 4); // L Trigger
+ JLO_B_PB(KeyType::PLACE, 1 << 5, 1 << 5); // R Trigger
+ JLO_B_PB(KeyType::INVENTORY, 1 << 6, 1 << 6); // Z Button
+
+ // D-Pad
+ JLO_A_PB(KeyType::HOTBAR_PREV, 5, 1, jlo.axes_deadzone); // left
+ JLO_A_PB(KeyType::HOTBAR_NEXT, 5, -1, jlo.axes_deadzone); // right
+ // Axis are hard to actuate independantly, best to leave up and down unused.
+ //JLO_A_PB(0, 6, 1, jlo.axes_deadzone); // up
+ //JLO_A_PB(0, 6, -1, jlo.axes_deadzone); // down
+
+ // Movements tied to Control Stick, important for vessels
+ JLO_A_PB(KeyType::LEFT, 0, 1, jlo.axes_deadzone);
+ JLO_A_PB(KeyType::RIGHT, 0, -1, jlo.axes_deadzone);
+ JLO_A_PB(KeyType::FORWARD, 1, 1, jlo.axes_deadzone);
+ JLO_A_PB(KeyType::BACKWARD, 1, -1, jlo.axes_deadzone);
+
+ return jlo;
+}
+
+
JoystickController::JoystickController() :
doubling_dtime(g_settings->getFloat("repeat_joystick_button_time"))
{
for (float &i : m_past_pressed_time) {
i = 0;
}
+ m_layout.axes_deadzone = 0;
clear();
}
@@ -187,6 +236,8 @@ void JoystickController::setLayoutFromControllerName(const std::string &name)
{
if (lowercase(name).find("xbox") != std::string::npos) {
m_layout = create_xbox_layout();
+ } else if (lowercase(name).find("dragonrise_gamecube") != std::string::npos) {
+ m_layout = create_dragonrise_gamecube_layout();
} else {
m_layout = create_default_layout();
}
@@ -251,10 +302,27 @@ void JoystickController::clear()
memset(m_axes_vals, 0, sizeof(m_axes_vals));
}
-s16 JoystickController::getAxisWithoutDead(JoystickAxis axis)
+float JoystickController::getAxisWithoutDead(JoystickAxis axis)
{
s16 v = m_axes_vals[axis];
+
if (abs(v) < m_layout.axes_deadzone)
- return 0;
- return v;
+ return 0.0f;
+
+ v += (v < 0 ? m_layout.axes_deadzone : -m_layout.axes_deadzone);
+
+ return (float)v / ((float)(INT16_MAX - m_layout.axes_deadzone));
+}
+
+float JoystickController::getMovementDirection()
+{
+ return atan2(getAxisWithoutDead(JA_SIDEWARD_MOVE), -getAxisWithoutDead(JA_FORWARD_MOVE));
+}
+
+float JoystickController::getMovementSpeed()
+{
+ float speed = sqrt(pow(getAxisWithoutDead(JA_FORWARD_MOVE), 2) + pow(getAxisWithoutDead(JA_SIDEWARD_MOVE), 2));
+ if (speed > 1.0f)
+ speed = 1.0f;
+ return speed;
}
diff --git a/src/client/joystick_controller.h b/src/client/joystick_controller.h
index 3f361e4ef..cbc60886c 100644
--- a/src/client/joystick_controller.h
+++ b/src/client/joystick_controller.h
@@ -144,7 +144,10 @@ public:
return m_axes_vals[axis];
}
- s16 getAxisWithoutDead(JoystickAxis axis);
+ float getAxisWithoutDead(JoystickAxis axis);
+
+ float getMovementDirection();
+ float getMovementSpeed();
f32 doubling_dtime;
diff --git a/src/client/keycode.cpp b/src/client/keycode.cpp
index ce5214f54..fac077f0f 100644
--- a/src/client/keycode.cpp
+++ b/src/client/keycode.cpp
@@ -197,7 +197,6 @@ static const struct table_key table[] = {
DEFINEKEY1(KEY_MODECHANGE, N_("IME Mode Change"))
DEFINEKEY1(KEY_APPS, N_("Apps"))
DEFINEKEY1(KEY_SLEEP, N_("Sleep"))
-#if !(IRRLICHT_VERSION_MAJOR <= 1 && IRRLICHT_VERSION_MINOR <= 7 && IRRLICHT_VERSION_REVISION < 3)
DEFINEKEY1(KEY_OEM_1, "OEM 1") // KEY_OEM_[0-9] and KEY_OEM_102 are assigned to multiple
DEFINEKEY1(KEY_OEM_2, "OEM 2") // different chars (on different platforms too) and thus w/o char
DEFINEKEY1(KEY_OEM_3, "OEM 3")
@@ -208,7 +207,6 @@ static const struct table_key table[] = {
DEFINEKEY1(KEY_OEM_8, "OEM 8")
DEFINEKEY1(KEY_OEM_AX, "OEM AX")
DEFINEKEY1(KEY_OEM_102, "OEM 102")
-#endif
DEFINEKEY1(KEY_ATTN, "Attn")
DEFINEKEY1(KEY_CRSEL, "CrSel")
DEFINEKEY1(KEY_EXSEL, "ExSel")
diff --git a/src/client/keys.h b/src/client/keys.h
index 60a7a3c45..e120a2d92 100644
--- a/src/client/keys.h
+++ b/src/client/keys.h
@@ -32,7 +32,7 @@ public:
LEFT,
RIGHT,
JUMP,
- SPECIAL1,
+ AUX1,
SNEAK,
AUTOFORWARD,
DIG,
@@ -59,6 +59,7 @@ public:
DEC_VOLUME,
CINEMATIC,
SCREENSHOT,
+ TOGGLE_BLOCK_BOUNDS,
TOGGLE_HUD,
TOGGLE_CHAT,
TOGGLE_FOG,
diff --git a/src/client/localplayer.cpp b/src/client/localplayer.cpp
index f3eb1a2dd..4f1ea7bda 100644
--- a/src/client/localplayer.cpp
+++ b/src/client/localplayer.cpp
@@ -227,8 +227,9 @@ void LocalPlayer::move(f32 dtime, Environment *env, f32 pos_max_d,
pp = floatToInt(position + v3f(0.0f, BS * 0.1f, 0.0f), BS);
node = map->getNode(pp, &is_valid_position);
if (is_valid_position) {
- in_liquid = nodemgr->get(node.getContent()).isLiquid();
- liquid_viscosity = nodemgr->get(node.getContent()).liquid_viscosity;
+ const ContentFeatures &cf = nodemgr->get(node.getContent());
+ in_liquid = cf.liquid_move_physics;
+ move_resistance = cf.move_resistance;
} else {
in_liquid = false;
}
@@ -238,8 +239,9 @@ void LocalPlayer::move(f32 dtime, Environment *env, f32 pos_max_d,
pp = floatToInt(position + v3f(0.0f, BS * 0.5f, 0.0f), BS);
node = map->getNode(pp, &is_valid_position);
if (is_valid_position) {
- in_liquid = nodemgr->get(node.getContent()).isLiquid();
- liquid_viscosity = nodemgr->get(node.getContent()).liquid_viscosity;
+ const ContentFeatures &cf = nodemgr->get(node.getContent());
+ in_liquid = cf.liquid_move_physics;
+ move_resistance = cf.move_resistance;
} else {
in_liquid = false;
}
@@ -252,7 +254,7 @@ void LocalPlayer::move(f32 dtime, Environment *env, f32 pos_max_d,
pp = floatToInt(position + v3f(0.0f), BS);
node = map->getNode(pp, &is_valid_position);
if (is_valid_position) {
- in_liquid_stable = nodemgr->get(node.getContent()).isLiquid();
+ in_liquid_stable = nodemgr->get(node.getContent()).liquid_move_physics;
} else {
in_liquid_stable = false;
}
@@ -566,23 +568,7 @@ void LocalPlayer::applyControl(float dtime, Environment *env)
}
}
- if (control.up)
- speedH += v3f(0.0f, 0.0f, 1.0f);
-
- if (control.down)
- speedH -= v3f(0.0f, 0.0f, 1.0f);
-
- if (!control.up && !control.down)
- speedH -= v3f(0.0f, 0.0f, 1.0f) * (control.forw_move_joystick_axis / 32767.f);
-
- if (control.left)
- speedH += v3f(-1.0f, 0.0f, 0.0f);
-
- if (control.right)
- speedH += v3f(1.0f, 0.0f, 0.0f);
-
- if (!control.left && !control.right)
- speedH += v3f(1.0f, 0.0f, 0.0f) * (control.sidew_move_joystick_axis / 32767.f);
+ speedH = v3f(sin(control.movement_direction), 0.0f, cos(control.movement_direction));
if (m_autojump) {
// release autojump after a given time
@@ -639,6 +625,8 @@ void LocalPlayer::applyControl(float dtime, Environment *env)
else
speedH = speedH.normalize() * movement_speed_walk;
+ speedH *= control.movement_speed; /* Apply analog input */
+
// Acceleration increase
f32 incH = 0.0f; // Horizontal (X, Z)
f32 incV = 0.0f; // Vertical (Y)
@@ -814,8 +802,9 @@ void LocalPlayer::old_move(f32 dtime, Environment *env, f32 pos_max_d,
pp = floatToInt(position + v3f(0.0f, BS * 0.1f, 0.0f), BS);
node = map->getNode(pp, &is_valid_position);
if (is_valid_position) {
- in_liquid = nodemgr->get(node.getContent()).isLiquid();
- liquid_viscosity = nodemgr->get(node.getContent()).liquid_viscosity;
+ const ContentFeatures &cf = nodemgr->get(node.getContent());
+ in_liquid = cf.liquid_move_physics;
+ move_resistance = cf.move_resistance;
} else {
in_liquid = false;
}
@@ -824,8 +813,9 @@ void LocalPlayer::old_move(f32 dtime, Environment *env, f32 pos_max_d,
pp = floatToInt(position + v3f(0.0f, BS * 0.5f, 0.0f), BS);
node = map->getNode(pp, &is_valid_position);
if (is_valid_position) {
- in_liquid = nodemgr->get(node.getContent()).isLiquid();
- liquid_viscosity = nodemgr->get(node.getContent()).liquid_viscosity;
+ const ContentFeatures &cf = nodemgr->get(node.getContent());
+ in_liquid = cf.liquid_move_physics;
+ move_resistance = cf.move_resistance;
} else {
in_liquid = false;
}
@@ -837,7 +827,7 @@ void LocalPlayer::old_move(f32 dtime, Environment *env, f32 pos_max_d,
pp = floatToInt(position + v3f(0.0f), BS);
node = map->getNode(pp, &is_valid_position);
if (is_valid_position)
- in_liquid_stable = nodemgr->get(node.getContent()).isLiquid();
+ in_liquid_stable = nodemgr->get(node.getContent()).liquid_move_physics;
else
in_liquid_stable = false;
@@ -1106,12 +1096,8 @@ void LocalPlayer::handleAutojump(f32 dtime, Environment *env,
if (m_autojump)
return;
- bool control_forward = control.up ||
- (!control.up && !control.down &&
- control.forw_move_joystick_axis < -0.05f);
-
bool could_autojump =
- m_can_jump && !control.jump && !control.sneak && control_forward;
+ m_can_jump && !control.jump && !control.sneak && control.isMoving();
if (!could_autojump)
return;
diff --git a/src/client/localplayer.h b/src/client/localplayer.h
index 345aec9d9..577be2803 100644
--- a/src/client/localplayer.h
+++ b/src/client/localplayer.h
@@ -55,8 +55,8 @@ public:
bool in_liquid = false;
// This is more stable and defines the maximum speed of the player
bool in_liquid_stable = false;
- // Gets the viscosity of water to calculate friction
- u8 liquid_viscosity = 0;
+ // Slows down the player when moving through
+ u8 move_resistance = 0;
bool is_climbing = false;
bool swimming_vertical = false;
bool swimming_pitch = false;
@@ -86,7 +86,7 @@ public:
v3f last_speed;
float last_pitch = 0.0f;
float last_yaw = 0.0f;
- unsigned int last_keyPressed = 0;
+ u32 last_keyPressed = 0;
u8 last_camera_fov = 0;
u8 last_wanted_range = 0;
diff --git a/src/client/mapblock_mesh.cpp b/src/client/mapblock_mesh.cpp
index 167e1e3ec..249a56087 100644
--- a/src/client/mapblock_mesh.cpp
+++ b/src/client/mapblock_mesh.cpp
@@ -407,20 +407,20 @@ static void getNodeTextureCoords(v3f base, const v3f &scale, const v3s16 &dir, f
if (dir.X > 0 || dir.Y != 0 || dir.Z < 0)
base -= scale;
if (dir == v3s16(0,0,1)) {
- *u = -base.X - 1;
- *v = -base.Y - 1;
+ *u = -base.X;
+ *v = -base.Y;
} else if (dir == v3s16(0,0,-1)) {
*u = base.X + 1;
- *v = -base.Y - 2;
+ *v = -base.Y - 1;
} else if (dir == v3s16(1,0,0)) {
*u = base.Z + 1;
- *v = -base.Y - 2;
- } else if (dir == v3s16(-1,0,0)) {
- *u = -base.Z - 1;
*v = -base.Y - 1;
+ } else if (dir == v3s16(-1,0,0)) {
+ *u = -base.Z;
+ *v = -base.Y;
} else if (dir == v3s16(0,1,0)) {
*u = base.X + 1;
- *v = -base.Z - 2;
+ *v = -base.Z - 1;
} else if (dir == v3s16(0,-1,0)) {
*u = base.X + 1;
*v = base.Z + 1;
@@ -860,6 +860,9 @@ static void updateFastFaceRow(
g_settings->getBool("enable_shaders") &&
g_settings->getBool("enable_waving_water");
+ static thread_local const bool force_not_tiling =
+ false && g_settings->getBool("enable_dynamic_shadows");
+
v3s16 p = startpos;
u16 continuous_tiles_count = 1;
@@ -898,7 +901,8 @@ static void updateFastFaceRow(
waving,
next_tile);
- if (next_makes_face == makes_face
+ if (!force_not_tiling
+ && next_makes_face == makes_face
&& next_p_corrected == p_corrected + translate_dir
&& next_face_dir_corrected == face_dir_corrected
&& memcmp(next_lights, lights, sizeof(lights)) == 0
@@ -1072,8 +1076,8 @@ MapBlockMesh::MapBlockMesh(MeshMakeData *data, v3s16 camera_offset):
*/
{
- MapblockMeshGenerator generator(data, &collector);
- generator.generate();
+ MapblockMeshGenerator(data, &collector,
+ data->m_client->getSceneManager()->getMeshManipulator()).generate();
}
/*
diff --git a/src/client/mesh.cpp b/src/client/mesh.cpp
index 2400a374c..c56eba2e2 100644
--- a/src/client/mesh.cpp
+++ b/src/client/mesh.cpp
@@ -27,14 +27,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <SAnimatedMesh.h>
#include <IAnimatedMeshSceneNode.h>
-// In Irrlicht 1.8 the signature of ITexture::lock was changed from
-// (bool, u32) to (E_TEXTURE_LOCK_MODE, u32).
-#if IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR <= 7
-#define MY_ETLM_READ_ONLY true
-#else
-#define MY_ETLM_READ_ONLY video::ETLM_READ_ONLY
-#endif
-
inline static void applyShadeFactor(video::SColor& color, float factor)
{
color.setRed(core::clamp(core::round32(color.getRed()*factor), 0, 255));
@@ -506,592 +498,3 @@ scene::IMesh* convertNodeboxesToMesh(const std::vector<aabb3f> &boxes,
}
return dst_mesh;
}
-
-struct vcache
-{
- core::array<u32> tris;
- float score;
- s16 cachepos;
- u16 NumActiveTris;
-};
-
-struct tcache
-{
- u16 ind[3];
- float score;
- bool drawn;
-};
-
-const u16 cachesize = 32;
-
-float FindVertexScore(vcache *v)
-{
- const float CacheDecayPower = 1.5f;
- const float LastTriScore = 0.75f;
- const float ValenceBoostScale = 2.0f;
- const float ValenceBoostPower = 0.5f;
- const float MaxSizeVertexCache = 32.0f;
-
- if (v->NumActiveTris == 0)
- {
- // No tri needs this vertex!
- return -1.0f;
- }
-
- float Score = 0.0f;
- int CachePosition = v->cachepos;
- if (CachePosition < 0)
- {
- // Vertex is not in FIFO cache - no score.
- }
- else
- {
- if (CachePosition < 3)
- {
- // This vertex was used in the last triangle,
- // so it has a fixed score.
- Score = LastTriScore;
- }
- else
- {
- // Points for being high in the cache.
- const float Scaler = 1.0f / (MaxSizeVertexCache - 3);
- Score = 1.0f - (CachePosition - 3) * Scaler;
- Score = powf(Score, CacheDecayPower);
- }
- }
-
- // Bonus points for having a low number of tris still to
- // use the vert, so we get rid of lone verts quickly.
- float ValenceBoost = powf(v->NumActiveTris,
- -ValenceBoostPower);
- Score += ValenceBoostScale * ValenceBoost;
-
- return Score;
-}
-
-/*
- A specialized LRU cache for the Forsyth algorithm.
-*/
-
-class f_lru
-{
-
-public:
- f_lru(vcache *v, tcache *t): vc(v), tc(t)
- {
- for (int &i : cache) {
- i = -1;
- }
- }
-
- // Adds this vertex index and returns the highest-scoring triangle index
- u32 add(u16 vert, bool updatetris = false)
- {
- bool found = false;
-
- // Mark existing pos as empty
- for (u16 i = 0; i < cachesize; i++)
- {
- if (cache[i] == vert)
- {
- // Move everything down
- for (u16 j = i; j; j--)
- {
- cache[j] = cache[j - 1];
- }
-
- found = true;
- break;
- }
- }
-
- if (!found)
- {
- if (cache[cachesize-1] != -1)
- vc[cache[cachesize-1]].cachepos = -1;
-
- // Move everything down
- for (u16 i = cachesize - 1; i; i--)
- {
- cache[i] = cache[i - 1];
- }
- }
-
- cache[0] = vert;
-
- u32 highest = 0;
- float hiscore = 0;
-
- if (updatetris)
- {
- // Update cache positions
- for (u16 i = 0; i < cachesize; i++)
- {
- if (cache[i] == -1)
- break;
-
- vc[cache[i]].cachepos = i;
- vc[cache[i]].score = FindVertexScore(&vc[cache[i]]);
- }
-
- // Update triangle scores
- for (int i : cache) {
- if (i == -1)
- break;
-
- const u16 trisize = vc[i].tris.size();
- for (u16 t = 0; t < trisize; t++)
- {
- tcache *tri = &tc[vc[i].tris[t]];
-
- tri->score =
- vc[tri->ind[0]].score +
- vc[tri->ind[1]].score +
- vc[tri->ind[2]].score;
-
- if (tri->score > hiscore)
- {
- hiscore = tri->score;
- highest = vc[i].tris[t];
- }
- }
- }
- }
-
- return highest;
- }
-
-private:
- s32 cache[cachesize];
- vcache *vc;
- tcache *tc;
-};
-
-/**
-Vertex cache optimization according to the Forsyth paper:
-http://home.comcast.net/~tom_forsyth/papers/fast_vert_cache_opt.html
-
-The function is thread-safe (read: you can optimize several meshes in different threads)
-
-\param mesh Source mesh for the operation. */
-scene::IMesh* createForsythOptimizedMesh(const scene::IMesh *mesh)
-{
- if (!mesh)
- return 0;
-
- scene::SMesh *newmesh = new scene::SMesh();
- newmesh->BoundingBox = mesh->getBoundingBox();
-
- const u32 mbcount = mesh->getMeshBufferCount();
-
- for (u32 b = 0; b < mbcount; ++b)
- {
- const scene::IMeshBuffer *mb = mesh->getMeshBuffer(b);
-
- if (mb->getIndexType() != video::EIT_16BIT)
- {
- //os::Printer::log("Cannot optimize a mesh with 32bit indices", ELL_ERROR);
- newmesh->drop();
- return 0;
- }
-
- const u32 icount = mb->getIndexCount();
- const u32 tcount = icount / 3;
- const u32 vcount = mb->getVertexCount();
- const u16 *ind = mb->getIndices();
-
- vcache *vc = new vcache[vcount];
- tcache *tc = new tcache[tcount];
-
- f_lru lru(vc, tc);
-
- // init
- for (u16 i = 0; i < vcount; i++)
- {
- vc[i].score = 0;
- vc[i].cachepos = -1;
- vc[i].NumActiveTris = 0;
- }
-
- // First pass: count how many times a vert is used
- for (u32 i = 0; i < icount; i += 3)
- {
- vc[ind[i]].NumActiveTris++;
- vc[ind[i + 1]].NumActiveTris++;
- vc[ind[i + 2]].NumActiveTris++;
-
- const u32 tri_ind = i/3;
- tc[tri_ind].ind[0] = ind[i];
- tc[tri_ind].ind[1] = ind[i + 1];
- tc[tri_ind].ind[2] = ind[i + 2];
- }
-
- // Second pass: list of each triangle
- for (u32 i = 0; i < tcount; i++)
- {
- vc[tc[i].ind[0]].tris.push_back(i);
- vc[tc[i].ind[1]].tris.push_back(i);
- vc[tc[i].ind[2]].tris.push_back(i);
-
- tc[i].drawn = false;
- }
-
- // Give initial scores
- for (u16 i = 0; i < vcount; i++)
- {
- vc[i].score = FindVertexScore(&vc[i]);
- }
- for (u32 i = 0; i < tcount; i++)
- {
- tc[i].score =
- vc[tc[i].ind[0]].score +
- vc[tc[i].ind[1]].score +
- vc[tc[i].ind[2]].score;
- }
-
- switch(mb->getVertexType())
- {
- case video::EVT_STANDARD:
- {
- video::S3DVertex *v = (video::S3DVertex *) mb->getVertices();
-
- scene::SMeshBuffer *buf = new scene::SMeshBuffer();
- buf->Material = mb->getMaterial();
-
- buf->Vertices.reallocate(vcount);
- buf->Indices.reallocate(icount);
-
- core::map<const video::S3DVertex, const u16> sind; // search index for fast operation
- typedef core::map<const video::S3DVertex, const u16>::Node snode;
-
- // Main algorithm
- u32 highest = 0;
- u32 drawcalls = 0;
- for (;;)
- {
- if (tc[highest].drawn)
- {
- bool found = false;
- float hiscore = 0;
- for (u32 t = 0; t < tcount; t++)
- {
- if (!tc[t].drawn)
- {
- if (tc[t].score > hiscore)
- {
- highest = t;
- hiscore = tc[t].score;
- found = true;
- }
- }
- }
- if (!found)
- break;
- }
-
- // Output the best triangle
- u16 newind = buf->Vertices.size();
-
- snode *s = sind.find(v[tc[highest].ind[0]]);
-
- if (!s)
- {
- buf->Vertices.push_back(v[tc[highest].ind[0]]);
- buf->Indices.push_back(newind);
- sind.insert(v[tc[highest].ind[0]], newind);
- newind++;
- }
- else
- {
- buf->Indices.push_back(s->getValue());
- }
-
- s = sind.find(v[tc[highest].ind[1]]);
-
- if (!s)
- {
- buf->Vertices.push_back(v[tc[highest].ind[1]]);
- buf->Indices.push_back(newind);
- sind.insert(v[tc[highest].ind[1]], newind);
- newind++;
- }
- else
- {
- buf->Indices.push_back(s->getValue());
- }
-
- s = sind.find(v[tc[highest].ind[2]]);
-
- if (!s)
- {
- buf->Vertices.push_back(v[tc[highest].ind[2]]);
- buf->Indices.push_back(newind);
- sind.insert(v[tc[highest].ind[2]], newind);
- }
- else
- {
- buf->Indices.push_back(s->getValue());
- }
-
- vc[tc[highest].ind[0]].NumActiveTris--;
- vc[tc[highest].ind[1]].NumActiveTris--;
- vc[tc[highest].ind[2]].NumActiveTris--;
-
- tc[highest].drawn = true;
-
- for (u16 j : tc[highest].ind) {
- vcache *vert = &vc[j];
- for (u16 t = 0; t < vert->tris.size(); t++)
- {
- if (highest == vert->tris[t])
- {
- vert->tris.erase(t);
- break;
- }
- }
- }
-
- lru.add(tc[highest].ind[0]);
- lru.add(tc[highest].ind[1]);
- highest = lru.add(tc[highest].ind[2], true);
- drawcalls++;
- }
-
- buf->setBoundingBox(mb->getBoundingBox());
- newmesh->addMeshBuffer(buf);
- buf->drop();
- }
- break;
- case video::EVT_2TCOORDS:
- {
- video::S3DVertex2TCoords *v = (video::S3DVertex2TCoords *) mb->getVertices();
-
- scene::SMeshBufferLightMap *buf = new scene::SMeshBufferLightMap();
- buf->Material = mb->getMaterial();
-
- buf->Vertices.reallocate(vcount);
- buf->Indices.reallocate(icount);
-
- core::map<const video::S3DVertex2TCoords, const u16> sind; // search index for fast operation
- typedef core::map<const video::S3DVertex2TCoords, const u16>::Node snode;
-
- // Main algorithm
- u32 highest = 0;
- u32 drawcalls = 0;
- for (;;)
- {
- if (tc[highest].drawn)
- {
- bool found = false;
- float hiscore = 0;
- for (u32 t = 0; t < tcount; t++)
- {
- if (!tc[t].drawn)
- {
- if (tc[t].score > hiscore)
- {
- highest = t;
- hiscore = tc[t].score;
- found = true;
- }
- }
- }
- if (!found)
- break;
- }
-
- // Output the best triangle
- u16 newind = buf->Vertices.size();
-
- snode *s = sind.find(v[tc[highest].ind[0]]);
-
- if (!s)
- {
- buf->Vertices.push_back(v[tc[highest].ind[0]]);
- buf->Indices.push_back(newind);
- sind.insert(v[tc[highest].ind[0]], newind);
- newind++;
- }
- else
- {
- buf->Indices.push_back(s->getValue());
- }
-
- s = sind.find(v[tc[highest].ind[1]]);
-
- if (!s)
- {
- buf->Vertices.push_back(v[tc[highest].ind[1]]);
- buf->Indices.push_back(newind);
- sind.insert(v[tc[highest].ind[1]], newind);
- newind++;
- }
- else
- {
- buf->Indices.push_back(s->getValue());
- }
-
- s = sind.find(v[tc[highest].ind[2]]);
-
- if (!s)
- {
- buf->Vertices.push_back(v[tc[highest].ind[2]]);
- buf->Indices.push_back(newind);
- sind.insert(v[tc[highest].ind[2]], newind);
- }
- else
- {
- buf->Indices.push_back(s->getValue());
- }
-
- vc[tc[highest].ind[0]].NumActiveTris--;
- vc[tc[highest].ind[1]].NumActiveTris--;
- vc[tc[highest].ind[2]].NumActiveTris--;
-
- tc[highest].drawn = true;
-
- for (u16 j : tc[highest].ind) {
- vcache *vert = &vc[j];
- for (u16 t = 0; t < vert->tris.size(); t++)
- {
- if (highest == vert->tris[t])
- {
- vert->tris.erase(t);
- break;
- }
- }
- }
-
- lru.add(tc[highest].ind[0]);
- lru.add(tc[highest].ind[1]);
- highest = lru.add(tc[highest].ind[2]);
- drawcalls++;
- }
-
- buf->setBoundingBox(mb->getBoundingBox());
- newmesh->addMeshBuffer(buf);
- buf->drop();
-
- }
- break;
- case video::EVT_TANGENTS:
- {
- video::S3DVertexTangents *v = (video::S3DVertexTangents *) mb->getVertices();
-
- scene::SMeshBufferTangents *buf = new scene::SMeshBufferTangents();
- buf->Material = mb->getMaterial();
-
- buf->Vertices.reallocate(vcount);
- buf->Indices.reallocate(icount);
-
- core::map<const video::S3DVertexTangents, const u16> sind; // search index for fast operation
- typedef core::map<const video::S3DVertexTangents, const u16>::Node snode;
-
- // Main algorithm
- u32 highest = 0;
- u32 drawcalls = 0;
- for (;;)
- {
- if (tc[highest].drawn)
- {
- bool found = false;
- float hiscore = 0;
- for (u32 t = 0; t < tcount; t++)
- {
- if (!tc[t].drawn)
- {
- if (tc[t].score > hiscore)
- {
- highest = t;
- hiscore = tc[t].score;
- found = true;
- }
- }
- }
- if (!found)
- break;
- }
-
- // Output the best triangle
- u16 newind = buf->Vertices.size();
-
- snode *s = sind.find(v[tc[highest].ind[0]]);
-
- if (!s)
- {
- buf->Vertices.push_back(v[tc[highest].ind[0]]);
- buf->Indices.push_back(newind);
- sind.insert(v[tc[highest].ind[0]], newind);
- newind++;
- }
- else
- {
- buf->Indices.push_back(s->getValue());
- }
-
- s = sind.find(v[tc[highest].ind[1]]);
-
- if (!s)
- {
- buf->Vertices.push_back(v[tc[highest].ind[1]]);
- buf->Indices.push_back(newind);
- sind.insert(v[tc[highest].ind[1]], newind);
- newind++;
- }
- else
- {
- buf->Indices.push_back(s->getValue());
- }
-
- s = sind.find(v[tc[highest].ind[2]]);
-
- if (!s)
- {
- buf->Vertices.push_back(v[tc[highest].ind[2]]);
- buf->Indices.push_back(newind);
- sind.insert(v[tc[highest].ind[2]], newind);
- }
- else
- {
- buf->Indices.push_back(s->getValue());
- }
-
- vc[tc[highest].ind[0]].NumActiveTris--;
- vc[tc[highest].ind[1]].NumActiveTris--;
- vc[tc[highest].ind[2]].NumActiveTris--;
-
- tc[highest].drawn = true;
-
- for (u16 j : tc[highest].ind) {
- vcache *vert = &vc[j];
- for (u16 t = 0; t < vert->tris.size(); t++)
- {
- if (highest == vert->tris[t])
- {
- vert->tris.erase(t);
- break;
- }
- }
- }
-
- lru.add(tc[highest].ind[0]);
- lru.add(tc[highest].ind[1]);
- highest = lru.add(tc[highest].ind[2]);
- drawcalls++;
- }
-
- buf->setBoundingBox(mb->getBoundingBox());
- newmesh->addMeshBuffer(buf);
- buf->drop();
- }
- break;
- }
-
- delete [] vc;
- delete [] tc;
-
- } // for each meshbuffer
-
- return newmesh;
-}
diff --git a/src/client/mesh.h b/src/client/mesh.h
index dbc091a06..1ed753c01 100644
--- a/src/client/mesh.h
+++ b/src/client/mesh.h
@@ -133,10 +133,3 @@ void recalculateBoundingBox(scene::IMesh *src_mesh);
We assume normal to be valid when it's 0 < length < Inf. and not NaN
*/
bool checkMeshNormals(scene::IMesh *mesh);
-
-/*
- Vertex cache optimization according to the Forsyth paper:
- http://home.comcast.net/~tom_forsyth/papers/fast_vert_cache_opt.html
- Ported from irrlicht 1.8
-*/
-scene::IMesh* createForsythOptimizedMesh(const scene::IMesh *mesh);
diff --git a/src/client/mesh_generator_thread.cpp b/src/client/mesh_generator_thread.cpp
index c8d1cba26..5c3f4180b 100644
--- a/src/client/mesh_generator_thread.cpp
+++ b/src/client/mesh_generator_thread.cpp
@@ -23,6 +23,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "client.h"
#include "mapblock.h"
#include "map.h"
+#include "util/directiontables.h"
/*
CachedMapBlockData
@@ -69,7 +70,7 @@ MeshUpdateQueue::~MeshUpdateQueue()
}
}
-void MeshUpdateQueue::addBlock(Map *map, v3s16 p, bool ack_block_to_server, bool urgent)
+bool MeshUpdateQueue::addBlock(Map *map, v3s16 p, bool ack_block_to_server, bool urgent)
{
MutexAutoLock lock(m_mutex);
@@ -81,20 +82,15 @@ void MeshUpdateQueue::addBlock(Map *map, v3s16 p, bool ack_block_to_server, bool
*/
std::vector<CachedMapBlockData*> cached_blocks;
size_t cache_hit_counter = 0;
+ CachedMapBlockData *cached_block = cacheBlock(map, p, FORCE_UPDATE);
+ if (!cached_block->data)
+ return false; // nothing to update
cached_blocks.reserve(3*3*3);
- v3s16 dp;
- for (dp.X = -1; dp.X <= 1; dp.X++)
- for (dp.Y = -1; dp.Y <= 1; dp.Y++)
- for (dp.Z = -1; dp.Z <= 1; dp.Z++) {
- v3s16 p1 = p + dp;
- CachedMapBlockData *cached_block;
- if (dp == v3s16(0, 0, 0))
- cached_block = cacheBlock(map, p1, FORCE_UPDATE);
- else
- cached_block = cacheBlock(map, p1, SKIP_UPDATE_IF_ALREADY_CACHED,
- &cache_hit_counter);
- cached_blocks.push_back(cached_block);
- }
+ cached_blocks.push_back(cached_block);
+ for (v3s16 dp : g_26dirs)
+ cached_blocks.push_back(cacheBlock(map, p + dp,
+ SKIP_UPDATE_IF_ALREADY_CACHED,
+ &cache_hit_counter));
g_profiler->avg("MeshUpdateQueue: MapBlocks from cache [%]",
100.0f * cache_hit_counter / cached_blocks.size());
@@ -116,7 +112,7 @@ void MeshUpdateQueue::addBlock(Map *map, v3s16 p, bool ack_block_to_server, bool
q->ack_block_to_server = true;
q->crack_level = m_client->getCrackLevel();
q->crack_pos = m_client->getCrackPos();
- return;
+ return true;
}
}
@@ -134,6 +130,7 @@ void MeshUpdateQueue::addBlock(Map *map, v3s16 p, bool ack_block_to_server, bool
for (CachedMapBlockData *cached_block : cached_blocks) {
cached_block->refcount_from_queue++;
}
+ return true;
}
// Returned pointer must be deleted
@@ -212,10 +209,7 @@ void MeshUpdateQueue::fillDataFromMapBlockCache(QueuedMeshUpdate *q)
std::time_t t_now = std::time(0);
// Collect data for 3*3*3 blocks from cache
- v3s16 dp;
- for (dp.X = -1; dp.X <= 1; dp.X++)
- for (dp.Y = -1; dp.Y <= 1; dp.Y++)
- for (dp.Z = -1; dp.Z <= 1; dp.Z++) {
+ for (v3s16 dp : g_27dirs) {
v3s16 p = q->p + dp;
CachedMapBlockData *cached_block = getCachedBlock(p);
if (cached_block) {
@@ -272,10 +266,25 @@ MeshUpdateThread::MeshUpdateThread(Client *client):
}
void MeshUpdateThread::updateBlock(Map *map, v3s16 p, bool ack_block_to_server,
- bool urgent)
+ bool urgent, bool update_neighbors)
{
- // Allow the MeshUpdateQueue to do whatever it wants
- m_queue_in.addBlock(map, p, ack_block_to_server, urgent);
+ static thread_local const bool many_neighbors =
+ g_settings->getBool("smooth_lighting")
+ && !g_settings->getFlag("performance_tradeoffs");
+ if (!m_queue_in.addBlock(map, p, ack_block_to_server, urgent)) {
+ warningstream << "Update requested for non-existent block at ("
+ << p.X << ", " << p.Y << ", " << p.Z << ")" << std::endl;
+ return;
+ }
+ if (update_neighbors) {
+ if (many_neighbors) {
+ for (v3s16 dp : g_26dirs)
+ m_queue_in.addBlock(map, p + dp, false, urgent);
+ } else {
+ for (v3s16 dp : g_6dirs)
+ m_queue_in.addBlock(map, p + dp, false, urgent);
+ }
+ }
deferUpdate();
}
diff --git a/src/client/mesh_generator_thread.h b/src/client/mesh_generator_thread.h
index 4371b8390..1b734bc06 100644
--- a/src/client/mesh_generator_thread.h
+++ b/src/client/mesh_generator_thread.h
@@ -66,7 +66,7 @@ public:
// Caches the block at p and its neighbors (if needed) and queues a mesh
// update for the block at p
- void addBlock(Map *map, v3s16 p, bool ack_block_to_server, bool urgent);
+ bool addBlock(Map *map, v3s16 p, bool ack_block_to_server, bool urgent);
// Returned pointer must be deleted
// Returns NULL if queue is empty
@@ -113,7 +113,8 @@ public:
// Caches the block at p and its neighbors (if needed) and queues a mesh
// update for the block at p
- void updateBlock(Map *map, v3s16 p, bool ack_block_to_server, bool urgent);
+ void updateBlock(Map *map, v3s16 p, bool ack_block_to_server, bool urgent,
+ bool update_neighbors = false);
v3s16 m_camera_offset;
MutexedQueue<MeshUpdateResult> m_queue_out;
diff --git a/src/client/minimap.cpp b/src/client/minimap.cpp
index dd810ee0a..f26aa1c70 100644
--- a/src/client/minimap.cpp
+++ b/src/client/minimap.cpp
@@ -491,7 +491,8 @@ video::ITexture *Minimap::getMinimapTexture()
// Want to use texture source, to : 1 find texture, 2 cache it
video::ITexture* texture = m_tsrc->getTexture(data->mode.texture);
video::IImage* image = driver->createImageFromData(
- texture->getColorFormat(), texture->getSize(), texture->lock(), true, false);
+ texture->getColorFormat(), texture->getSize(),
+ texture->lock(video::ETLM_READ_ONLY), true, false);
texture->unlock();
auto dim = image->getDimension();
@@ -576,7 +577,7 @@ scene::SMeshBuffer *Minimap::getMinimapMeshBuffer()
void Minimap::drawMinimap()
{
// Non hud managed minimap drawing (legacy minimap)
- v2u32 screensize = RenderingEngine::get_instance()->getWindowSize();
+ v2u32 screensize = RenderingEngine::getWindowSize();
const u32 size = 0.25 * screensize.Y;
drawMinimap(core::rect<s32>(
diff --git a/src/client/particles.cpp b/src/client/particles.cpp
index 7acd996dc..288826a5f 100644
--- a/src/client/particles.cpp
+++ b/src/client/particles.cpp
@@ -64,8 +64,8 @@ Particle::Particle(
v2f texsize,
video::SColor color
):
- scene::ISceneNode(RenderingEngine::get_scene_manager()->getRootSceneNode(),
- RenderingEngine::get_scene_manager())
+ scene::ISceneNode(((Client *)gamedef)->getSceneManager()->getRootSceneNode(),
+ ((Client *)gamedef)->getSceneManager())
{
// Misc
m_gamedef = gamedef;
diff --git a/src/client/render/anaglyph.cpp b/src/client/render/anaglyph.cpp
index 9ba4464a2..2571f7333 100644
--- a/src/client/render/anaglyph.cpp
+++ b/src/client/render/anaglyph.cpp
@@ -30,6 +30,7 @@ void RenderingCoreAnaglyph::drawAll()
void RenderingCoreAnaglyph::setupMaterial(int color_mask)
{
video::SOverrideMaterial &mat = driver->getOverrideMaterial();
+ mat.reset();
mat.Material.ColorMask = color_mask;
mat.EnableFlags = video::EMF_COLOR_MASK;
mat.EnablePasses = scene::ESNRP_SKY_BOX | scene::ESNRP_SOLID |
@@ -40,7 +41,7 @@ void RenderingCoreAnaglyph::setupMaterial(int color_mask)
void RenderingCoreAnaglyph::useEye(bool right)
{
RenderingCoreStereo::useEye(right);
- driver->clearZBuffer();
+ driver->clearBuffers(video::ECBF_DEPTH);
setupMaterial(right ? video::ECP_GREEN | video::ECP_BLUE : video::ECP_RED);
}
diff --git a/src/client/render/core.cpp b/src/client/render/core.cpp
index 92a7137ea..44ef1c98c 100644
--- a/src/client/render/core.cpp
+++ b/src/client/render/core.cpp
@@ -24,25 +24,35 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "client/clientmap.h"
#include "client/hud.h"
#include "client/minimap.h"
+#include "client/shadows/dynamicshadowsrender.h"
RenderingCore::RenderingCore(IrrlichtDevice *_device, Client *_client, Hud *_hud)
: device(_device), driver(device->getVideoDriver()), smgr(device->getSceneManager()),
guienv(device->getGUIEnvironment()), client(_client), camera(client->getCamera()),
- mapper(client->getMinimap()), hud(_hud)
+ mapper(client->getMinimap()), hud(_hud),
+ shadow_renderer(nullptr)
{
screensize = driver->getScreenSize();
virtual_size = screensize;
+
+ if (g_settings->getBool("enable_shaders") &&
+ false && g_settings->getBool("enable_dynamic_shadows")) {
+ shadow_renderer = new ShadowRenderer(device, client);
+ }
}
RenderingCore::~RenderingCore()
{
clearTextures();
+ delete shadow_renderer;
}
void RenderingCore::initialize()
{
// have to be called late as the VMT is not ready in the constructor:
initTextures();
+ if (shadow_renderer)
+ shadow_renderer->initialize();
}
void RenderingCore::updateScreenSize()
@@ -66,6 +76,9 @@ void RenderingCore::draw(video::SColor _skycolor, bool _show_hud, bool _show_min
draw_wield_tool = _draw_wield_tool;
draw_crosshair = _draw_crosshair;
+ if (shadow_renderer)
+ shadow_renderer->update();
+
beforeDraw();
drawAll();
}
@@ -73,9 +86,13 @@ void RenderingCore::draw(video::SColor _skycolor, bool _show_hud, bool _show_min
void RenderingCore::draw3D()
{
smgr->drawAll();
+ if (shadow_renderer)
+ shadow_renderer->drawDebug();
+
driver->setTransform(video::ETS_WORLD, core::IdentityMatrix);
if (!show_hud)
return;
+ hud->drawBlockBounds();
hud->drawSelectionMesh();
if (draw_wield_tool)
camera->drawWieldedTool();
diff --git a/src/client/render/core.h b/src/client/render/core.h
index 52ea8f99f..cabfbbfad 100644
--- a/src/client/render/core.h
+++ b/src/client/render/core.h
@@ -21,6 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#pragma once
#include "irrlichttypes_extrabloated.h"
+class ShadowRenderer;
class Camera;
class Client;
class Hud;
@@ -47,6 +48,8 @@ protected:
Minimap *mapper;
Hud *hud;
+ ShadowRenderer *shadow_renderer;
+
void updateScreenSize();
virtual void initTextures() {}
virtual void clearTextures() {}
@@ -72,4 +75,6 @@ public:
bool _draw_wield_tool, bool _draw_crosshair);
inline v2u32 getVirtualSize() const { return virtual_size; }
+
+ ShadowRenderer *get_shadow_renderer() { return shadow_renderer; };
};
diff --git a/src/client/render/interlaced.cpp b/src/client/render/interlaced.cpp
index ce8e92f21..3f79a8eb5 100644
--- a/src/client/render/interlaced.cpp
+++ b/src/client/render/interlaced.cpp
@@ -35,7 +35,11 @@ void RenderingCoreInterlaced::initMaterial()
IShaderSource *s = client->getShaderSource();
mat.UseMipMaps = false;
mat.ZBuffer = false;
+#if IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR > 8
+ mat.ZWriteEnable = video::EZW_OFF;
+#else
mat.ZWriteEnable = false;
+#endif
u32 shader = s->getShader("3d_interlaced_merge", TILE_MATERIAL_BASIC);
mat.MaterialType = s->getShaderInfo(shader).material;
for (int k = 0; k < 3; ++k) {
diff --git a/src/client/renderingengine.cpp b/src/client/renderingengine.cpp
index 99ff8c1ee..723865db4 100644
--- a/src/client/renderingengine.cpp
+++ b/src/client/renderingengine.cpp
@@ -19,7 +19,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
*/
#include <IrrlichtDevice.h>
-#include <irrlicht.h>
#include "fontengine.h"
#include "client.h"
#include "clouds.h"
@@ -92,7 +91,6 @@ RenderingEngine::RenderingEngine(IEventReceiver *receiver)
// bpp, fsaa, vsync
bool vsync = g_settings->getBool("vsync");
- u16 bits = g_settings->getU16("fullscreen_bpp");
u16 fsaa = g_settings->getU16("fsaa");
// stereo buffer required for pageflip stereo
@@ -106,7 +104,7 @@ RenderingEngine::RenderingEngine(IEventReceiver *receiver)
u32 i;
for (i = 0; i != drivers.size(); i++) {
if (!strcasecmp(driverstring.c_str(),
- RenderingEngine::getVideoDriverName(drivers[i]))) {
+ RenderingEngine::getVideoDriverInfo(drivers[i]).name.c_str())) {
driverType = drivers[i];
break;
}
@@ -118,17 +116,17 @@ RenderingEngine::RenderingEngine(IEventReceiver *receiver)
}
SIrrlichtCreationParameters params = SIrrlichtCreationParameters();
+ if (g_logger.getTraceEnabled())
+ params.LoggingLevel = irr::ELL_DEBUG;
params.DriverType = driverType;
params.WindowSize = core::dimension2d<u32>(screen_w, screen_h);
- params.Bits = bits;
params.AntiAlias = fsaa;
params.Fullscreen = fullscreen;
params.Stencilbuffer = false;
params.Stereobuffer = stereo_buffer;
params.Vsync = vsync;
params.EventReceiver = receiver;
- params.HighPrecisionFPU = g_settings->getBool("high_precision_fpu");
- params.ZBufferBits = 24;
+ params.HighPrecisionFPU = true;
#ifdef __ANDROID__
params.PrivateData = porting::app_global;
#endif
@@ -157,7 +155,7 @@ RenderingEngine::~RenderingEngine()
s_singleton = nullptr;
}
-v2u32 RenderingEngine::getWindowSize() const
+v2u32 RenderingEngine::_getWindowSize() const
{
if (core)
return core->getVirtualSize();
@@ -169,58 +167,18 @@ void RenderingEngine::setResizable(bool resize)
m_device->setResizable(resize);
}
-bool RenderingEngine::print_video_modes()
+void RenderingEngine::removeMesh(const scene::IMesh* mesh)
{
- IrrlichtDevice *nulldevice;
-
- bool vsync = g_settings->getBool("vsync");
- u16 fsaa = g_settings->getU16("fsaa");
- MyEventReceiver *receiver = new MyEventReceiver();
-
- SIrrlichtCreationParameters params = SIrrlichtCreationParameters();
- params.DriverType = video::EDT_NULL;
- params.WindowSize = core::dimension2d<u32>(640, 480);
- params.Bits = 24;
- params.AntiAlias = fsaa;
- params.Fullscreen = false;
- params.Stencilbuffer = false;
- params.Vsync = vsync;
- params.EventReceiver = receiver;
- params.HighPrecisionFPU = g_settings->getBool("high_precision_fpu");
-
- nulldevice = createDeviceEx(params);
-
- if (!nulldevice) {
- delete receiver;
- return false;
- }
-
- std::cout << _("Available video modes (WxHxD):") << std::endl;
-
- video::IVideoModeList *videomode_list = nulldevice->getVideoModeList();
-
- if (videomode_list != NULL) {
- s32 videomode_count = videomode_list->getVideoModeCount();
- core::dimension2d<u32> videomode_res;
- s32 videomode_depth;
- for (s32 i = 0; i < videomode_count; ++i) {
- videomode_res = videomode_list->getVideoModeResolution(i);
- videomode_depth = videomode_list->getVideoModeDepth(i);
- std::cout << videomode_res.Width << "x" << videomode_res.Height
- << "x" << videomode_depth << std::endl;
- }
+ m_device->getSceneManager()->getMeshCache()->removeMesh(mesh);
+}
- std::cout << _("Active video mode (WxHxD):") << std::endl;
- videomode_res = videomode_list->getDesktopResolution();
- videomode_depth = videomode_list->getDesktopDepth();
- std::cout << videomode_res.Width << "x" << videomode_res.Height << "x"
- << videomode_depth << std::endl;
+void RenderingEngine::cleanupMeshCache()
+{
+ auto mesh_cache = m_device->getSceneManager()->getMeshCache();
+ while (mesh_cache->getMeshCount() != 0) {
+ if (scene::IAnimatedMesh *mesh = mesh_cache->getMeshByIndex(0))
+ mesh_cache->removeMesh(mesh);
}
-
- nulldevice->drop();
- delete receiver;
-
- return videomode_list != NULL;
}
bool RenderingEngine::setupTopLevelWindow(const std::string &name)
@@ -294,7 +252,7 @@ void RenderingEngine::setupTopLevelXorgWindow(const std::string &name)
// force a shutdown of an application if it doesn't respond to the destroy
// window message.
- verbosestream << "Client: Setting Xorg _NET_WM_PID extened window manager property"
+ verbosestream << "Client: Setting Xorg _NET_WM_PID extended window manager property"
<< std::endl;
Atom NET_WM_PID = XInternAtom(x11_dpl, "_NET_WM_PID", false);
@@ -325,12 +283,10 @@ static bool getWindowHandle(irr::video::IVideoDriver *driver, HWND &hWnd)
const video::SExposedVideoData exposedData = driver->getExposedVideoData();
switch (driver->getDriverType()) {
- case video::EDT_DIRECT3D8:
- hWnd = reinterpret_cast<HWND>(exposedData.D3D8.HWnd);
- break;
- case video::EDT_DIRECT3D9:
- hWnd = reinterpret_cast<HWND>(exposedData.D3D9.HWnd);
- break;
+#if ENABLE_GLES
+ case video::EDT_OGLES1:
+ case video::EDT_OGLES2:
+#endif
case video::EDT_OPENGL:
hWnd = reinterpret_cast<HWND>(exposedData.OpenGLWin32.HWnd);
break;
@@ -471,11 +427,11 @@ bool RenderingEngine::setXorgWindowIconFromPath(const std::string &icon_file)
Text will be removed when the screen is drawn the next time.
Additionally, a progressbar can be drawn when percent is set between 0 and 100.
*/
-void RenderingEngine::_draw_load_screen(const std::wstring &text,
+void RenderingEngine::draw_load_screen(const std::wstring &text,
gui::IGUIEnvironment *guienv, ITextureSource *tsrc, float dtime,
int percent, bool clouds)
{
- v2u32 screensize = RenderingEngine::get_instance()->getWindowSize();
+ v2u32 screensize = getWindowSize();
v2s32 textsize(g_fontengine->getTextWidth(text), g_fontengine->getLineHeight());
v2s32 center(screensize.X / 2, screensize.Y / 2);
@@ -543,7 +499,7 @@ void RenderingEngine::_draw_load_screen(const std::wstring &text,
/*
Draws the menu scene including (optional) cloud background.
*/
-void RenderingEngine::_draw_menu_scene(gui::IGUIEnvironment *guienv,
+void RenderingEngine::draw_menu_scene(gui::IGUIEnvironment *guienv,
float dtime, bool clouds)
{
bool cloud_menu_background = clouds && g_settings->getBool("menu_clouds");
@@ -560,85 +516,53 @@ void RenderingEngine::_draw_menu_scene(gui::IGUIEnvironment *guienv,
get_video_driver()->endScene();
}
-std::vector<core::vector3d<u32>> RenderingEngine::getSupportedVideoModes()
-{
- IrrlichtDevice *nulldevice = createDevice(video::EDT_NULL);
- sanity_check(nulldevice);
-
- std::vector<core::vector3d<u32>> mlist;
- video::IVideoModeList *modelist = nulldevice->getVideoModeList();
-
- s32 num_modes = modelist->getVideoModeCount();
- for (s32 i = 0; i != num_modes; i++) {
- core::dimension2d<u32> mode_res = modelist->getVideoModeResolution(i);
- u32 mode_depth = (u32)modelist->getVideoModeDepth(i);
- mlist.emplace_back(mode_res.Width, mode_res.Height, mode_depth);
- }
-
- nulldevice->drop();
- return mlist;
-}
-
std::vector<irr::video::E_DRIVER_TYPE> RenderingEngine::getSupportedVideoDrivers()
{
+ // Only check these drivers.
+ // We do not support software and D3D in any capacity.
+ static const irr::video::E_DRIVER_TYPE glDrivers[4] = {
+ irr::video::EDT_NULL,
+ irr::video::EDT_OPENGL,
+ irr::video::EDT_OGLES1,
+ irr::video::EDT_OGLES2,
+ };
std::vector<irr::video::E_DRIVER_TYPE> drivers;
- for (int i = 0; i != irr::video::EDT_COUNT; i++) {
- if (irr::IrrlichtDevice::isDriverSupported((irr::video::E_DRIVER_TYPE)i))
- drivers.push_back((irr::video::E_DRIVER_TYPE)i);
+ for (int i = 0; i < 4; i++) {
+ if (irr::IrrlichtDevice::isDriverSupported(glDrivers[i]))
+ drivers.push_back(glDrivers[i]);
}
return drivers;
}
-void RenderingEngine::_initialize(Client *client, Hud *hud)
+void RenderingEngine::initialize(Client *client, Hud *hud)
{
const std::string &draw_mode = g_settings->get("3d_mode");
core.reset(createRenderingCore(draw_mode, m_device, client, hud));
core->initialize();
}
-void RenderingEngine::_finalize()
+void RenderingEngine::finalize()
{
core.reset();
}
-void RenderingEngine::_draw_scene(video::SColor skycolor, bool show_hud,
+void RenderingEngine::draw_scene(video::SColor skycolor, bool show_hud,
bool show_minimap, bool draw_wield_tool, bool draw_crosshair)
{
core->draw(skycolor, show_hud, show_minimap, draw_wield_tool, draw_crosshair);
}
-const char *RenderingEngine::getVideoDriverName(irr::video::E_DRIVER_TYPE type)
-{
- static const char *driver_ids[] = {
- "null",
- "software",
- "burningsvideo",
- "direct3d8",
- "direct3d9",
- "opengl",
- "ogles1",
- "ogles2",
- };
-
- return driver_ids[type];
-}
-
-const char *RenderingEngine::getVideoDriverFriendlyName(irr::video::E_DRIVER_TYPE type)
+const VideoDriverInfo &RenderingEngine::getVideoDriverInfo(irr::video::E_DRIVER_TYPE type)
{
- static const char *driver_names[] = {
- "NULL Driver",
- "Software Renderer",
- "Burning's Video",
- "Direct3D 8",
- "Direct3D 9",
- "OpenGL",
- "OpenGL ES1",
- "OpenGL ES2",
+ static const std::unordered_map<int, VideoDriverInfo> driver_info_map = {
+ {(int)video::EDT_NULL, {"null", "NULL Driver"}},
+ {(int)video::EDT_OPENGL, {"opengl", "OpenGL"}},
+ {(int)video::EDT_OGLES1, {"ogles1", "OpenGL ES1"}},
+ {(int)video::EDT_OGLES2, {"ogles2", "OpenGL ES2"}},
};
-
- return driver_names[type];
+ return driver_info_map.at((int)type);
}
#ifndef __ANDROID__
@@ -674,7 +598,7 @@ static float calcDisplayDensity()
float RenderingEngine::getDisplayDensity()
{
static float cached_display_density = calcDisplayDensity();
- return cached_display_density;
+ return cached_display_density * g_settings->getFloat("display_density_factor");
}
#elif defined(_WIN32)
@@ -702,14 +626,14 @@ float RenderingEngine::getDisplayDensity()
display_density = calcDisplayDensity(get_video_driver());
cached = true;
}
- return display_density;
+ return display_density * g_settings->getFloat("display_density_factor");
}
#else
float RenderingEngine::getDisplayDensity()
{
- return g_settings->getFloat("screen_dpi") / 96.0;
+ return (g_settings->getFloat("screen_dpi") / 96.0) * g_settings->getFloat("display_density_factor");
}
#endif
diff --git a/src/client/renderingengine.h b/src/client/renderingengine.h
index 34cc60630..a0ddb0d9a 100644
--- a/src/client/renderingengine.h
+++ b/src/client/renderingengine.h
@@ -25,6 +25,14 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <string>
#include "irrlichttypes_extrabloated.h"
#include "debug.h"
+#include "client/render/core.h"
+// include the shadow mapper classes too
+#include "client/shadows/dynamicshadowsrender.h"
+
+struct VideoDriverInfo {
+ std::string name;
+ std::string friendly_name;
+};
class ITextureSource;
class Camera;
@@ -41,13 +49,11 @@ public:
RenderingEngine(IEventReceiver *eventReceiver);
~RenderingEngine();
- v2u32 getWindowSize() const;
void setResizable(bool resize);
video::IVideoDriver *getVideoDriver() { return driver; }
- static const char *getVideoDriverName(irr::video::E_DRIVER_TYPE type);
- static const char *getVideoDriverFriendlyName(irr::video::E_DRIVER_TYPE type);
+ static const VideoDriverInfo &getVideoDriverInfo(irr::video::E_DRIVER_TYPE type);
static float getDisplayDensity();
static v2u32 getDisplaySize();
@@ -56,31 +62,30 @@ public:
bool setWindowIcon();
bool setXorgWindowIconFromPath(const std::string &icon_file);
static bool print_video_modes();
+ void cleanupMeshCache();
- static RenderingEngine *get_instance() { return s_singleton; }
+ void removeMesh(const scene::IMesh* mesh);
- static io::IFileSystem *get_filesystem()
+ static v2u32 getWindowSize()
{
- sanity_check(s_singleton && s_singleton->m_device);
- return s_singleton->m_device->getFileSystem();
+ sanity_check(s_singleton);
+ return s_singleton->_getWindowSize();
}
- static video::IVideoDriver *get_video_driver()
+ io::IFileSystem *get_filesystem()
{
- sanity_check(s_singleton && s_singleton->m_device);
- return s_singleton->m_device->getVideoDriver();
+ return m_device->getFileSystem();
}
- static scene::IMeshCache *get_mesh_cache()
+ static video::IVideoDriver *get_video_driver()
{
sanity_check(s_singleton && s_singleton->m_device);
- return s_singleton->m_device->getSceneManager()->getMeshCache();
+ return s_singleton->m_device->getVideoDriver();
}
- static scene::ISceneManager *get_scene_manager()
+ scene::ISceneManager *get_scene_manager()
{
- sanity_check(s_singleton && s_singleton->m_device);
- return s_singleton->m_device->getSceneManager();
+ return m_device->getSceneManager();
}
static irr::IrrlichtDevice *get_raw_device()
@@ -89,70 +94,43 @@ public:
return s_singleton->m_device;
}
- static u32 get_timer_time()
+ u32 get_timer_time()
{
- sanity_check(s_singleton && s_singleton->m_device &&
- s_singleton->m_device->getTimer());
- return s_singleton->m_device->getTimer()->getTime();
+ return m_device->getTimer()->getTime();
}
- static gui::IGUIEnvironment *get_gui_env()
+ gui::IGUIEnvironment *get_gui_env()
{
- sanity_check(s_singleton && s_singleton->m_device);
- return s_singleton->m_device->getGUIEnvironment();
+ return m_device->getGUIEnvironment();
}
- inline static void draw_load_screen(const std::wstring &text,
+ void draw_load_screen(const std::wstring &text,
gui::IGUIEnvironment *guienv, ITextureSource *tsrc,
- float dtime = 0, int percent = 0, bool clouds = true)
- {
- s_singleton->_draw_load_screen(
- text, guienv, tsrc, dtime, percent, clouds);
- }
+ float dtime = 0, int percent = 0, bool clouds = true);
- inline static void draw_menu_scene(
- gui::IGUIEnvironment *guienv, float dtime, bool clouds)
- {
- s_singleton->_draw_menu_scene(guienv, dtime, clouds);
- }
+ void draw_menu_scene(gui::IGUIEnvironment *guienv, float dtime, bool clouds);
+ void draw_scene(video::SColor skycolor, bool show_hud,
+ bool show_minimap, bool draw_wield_tool, bool draw_crosshair);
- inline static void draw_scene(video::SColor skycolor, bool show_hud,
- bool show_minimap, bool draw_wield_tool, bool draw_crosshair)
- {
- s_singleton->_draw_scene(skycolor, show_hud, show_minimap,
- draw_wield_tool, draw_crosshair);
- }
+ void initialize(Client *client, Hud *hud);
+ void finalize();
- inline static void initialize(Client *client, Hud *hud)
+ bool run()
{
- s_singleton->_initialize(client, hud);
+ return m_device->run();
}
- inline static void finalize() { s_singleton->_finalize(); }
-
- static bool run()
+ // FIXME: this is still global when it shouldn't be
+ static ShadowRenderer *get_shadow_renderer()
{
- sanity_check(s_singleton && s_singleton->m_device);
- return s_singleton->m_device->run();
+ //if (s_singleton && s_singleton->core)
+ // return s_singleton->core->get_shadow_renderer();
+ return nullptr;
}
-
- static std::vector<core::vector3d<u32>> getSupportedVideoModes();
static std::vector<irr::video::E_DRIVER_TYPE> getSupportedVideoDrivers();
private:
- void _draw_load_screen(const std::wstring &text, gui::IGUIEnvironment *guienv,
- ITextureSource *tsrc, float dtime = 0, int percent = 0,
- bool clouds = true);
-
- void _draw_menu_scene(gui::IGUIEnvironment *guienv, float dtime = 0,
- bool clouds = true);
-
- void _draw_scene(video::SColor skycolor, bool show_hud, bool show_minimap,
- bool draw_wield_tool, bool draw_crosshair);
-
- void _initialize(Client *client, Hud *hud);
-
- void _finalize();
+ v2u32 _getWindowSize() const;
std::unique_ptr<RenderingCore> core;
irr::IrrlichtDevice *m_device = nullptr;
diff --git a/src/client/shader.cpp b/src/client/shader.cpp
index b3e4911f4..c04a25862 100644
--- a/src/client/shader.cpp
+++ b/src/client/shader.cpp
@@ -225,6 +225,16 @@ class MainShaderConstantSetter : public IShaderConstantSetter
{
CachedVertexShaderSetting<float, 16> m_world_view_proj;
CachedVertexShaderSetting<float, 16> m_world;
+
+ // Shadow-related
+ CachedPixelShaderSetting<float, 16> m_shadow_view_proj;
+ CachedPixelShaderSetting<float, 3> m_light_direction;
+ CachedPixelShaderSetting<float> m_texture_res;
+ CachedPixelShaderSetting<float> m_shadow_strength;
+ CachedPixelShaderSetting<float> m_time_of_day;
+ CachedPixelShaderSetting<float> m_shadowfar;
+ CachedPixelShaderSetting<s32> m_shadow_texture;
+
#if ENABLE_GLES
// Modelview matrix
CachedVertexShaderSetting<float, 16> m_world_view;
@@ -243,6 +253,13 @@ public:
, m_texture("mTexture")
, m_normal("mNormal")
#endif
+ , m_shadow_view_proj("m_ShadowViewProj")
+ , m_light_direction("v_LightDirection")
+ , m_texture_res("f_textureresolution")
+ , m_shadow_strength("f_shadow_strength")
+ , m_time_of_day("f_timeofday")
+ , m_shadowfar("f_shadowfar")
+ , m_shadow_texture("ShadowMapSampler")
{}
~MainShaderConstantSetter() = default;
@@ -280,6 +297,36 @@ public:
};
m_normal.set(m, services);
#endif
+
+ // Set uniforms for Shadow shader
+ if (ShadowRenderer *shadow = RenderingEngine::get_shadow_renderer()) {
+ const auto &light = shadow->getDirectionalLight();
+
+ core::matrix4 shadowViewProj = light.getProjectionMatrix();
+ shadowViewProj *= light.getViewMatrix();
+ m_shadow_view_proj.set(shadowViewProj.pointer(), services);
+
+ float v_LightDirection[3];
+ light.getDirection().getAs3Values(v_LightDirection);
+ m_light_direction.set(v_LightDirection, services);
+
+ float TextureResolution = light.getMapResolution();
+ m_texture_res.set(&TextureResolution, services);
+
+ float ShadowStrength = shadow->getShadowStrength();
+ m_shadow_strength.set(&ShadowStrength, services);
+
+ float timeOfDay = shadow->getTimeOfDay();
+ m_time_of_day.set(&timeOfDay, services);
+
+ float shadowFar = shadow->getMaxShadowFar();
+ m_shadowfar.set(&shadowFar, services);
+
+ // I dont like using this hardcoded value. maybe something like
+ // MAX_TEXTURE - 1 or somthing like that??
+ s32 TextureLayerID = 3;
+ m_shadow_texture.set(&TextureLayerID, services);
+ }
}
};
@@ -579,8 +626,10 @@ ShaderInfo ShaderSource::generateShader(const std::string &name,
if (use_gles) {
shaders_header << R"(
#version 100
- )";
+ )";
vertex_header = R"(
+ precision mediump float;
+
uniform highp mat4 mWorldView;
uniform highp mat4 mWorldViewProj;
uniform mediump mat4 mTexture;
@@ -592,17 +641,17 @@ ShaderInfo ShaderSource::generateShader(const std::string &name,
attribute mediump vec3 inVertexNormal;
attribute mediump vec4 inVertexTangent;
attribute mediump vec4 inVertexBinormal;
- )";
+ )";
fragment_header = R"(
precision mediump float;
- )";
+ )";
} else {
shaders_header << R"(
#version 120
#define lowp
#define mediump
#define highp
- )";
+ )";
vertex_header = R"(
#define mWorldView gl_ModelViewMatrix
#define mWorldViewProj gl_ModelViewProjectionMatrix
@@ -615,7 +664,7 @@ ShaderInfo ShaderSource::generateShader(const std::string &name,
#define inVertexNormal gl_Normal
#define inVertexTangent gl_MultiTexCoord1
#define inVertexBinormal gl_MultiTexCoord2
- )";
+ )";
}
bool use_discard = use_gles;
@@ -625,8 +674,12 @@ ShaderInfo ShaderSource::generateShader(const std::string &name,
if (strstr(gl_renderer, "GC7000"))
use_discard = true;
#endif
- if (use_discard && shaderinfo.base_material != video::EMT_SOLID)
- shaders_header << "#define USE_DISCARD 1\n";
+ if (use_discard) {
+ if (shaderinfo.base_material == video::EMT_TRANSPARENT_ALPHA_CHANNEL)
+ shaders_header << "#define USE_DISCARD 1\n";
+ else if (shaderinfo.base_material == video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF)
+ shaders_header << "#define USE_DISCARD_REF 1\n";
+ }
#define PROVIDE(constant) shaders_header << "#define " #constant " " << (int)constant << "\n"
@@ -680,6 +733,23 @@ ShaderInfo ShaderSource::generateShader(const std::string &name,
shaders_header << "#define FOG_START " << core::clamp(g_settings->getFloat("fog_start"), 0.0f, 0.99f) << "\n";
+ if (false && g_settings->getBool("enable_dynamic_shadows")) {
+ shaders_header << "#define ENABLE_DYNAMIC_SHADOWS 1\n";
+ if (g_settings->getBool("shadow_map_color"))
+ shaders_header << "#define COLORED_SHADOWS 1\n";
+
+ if (g_settings->getBool("shadow_poisson_filter"))
+ shaders_header << "#define POISSON_FILTER 1\n";
+
+ s32 shadow_filter = g_settings->getS32("shadow_filters");
+ shaders_header << "#define SHADOW_FILTER " << shadow_filter << "\n";
+
+ float shadow_soft_radius = g_settings->getFloat("shadow_soft_radius");
+ if (shadow_soft_radius < 1.0f)
+ shadow_soft_radius = 1.0f;
+ shaders_header << "#define SOFTSHADOWRADIUS " << shadow_soft_radius << "\n";
+ }
+
std::string common_header = shaders_header.str();
std::string vertex_shader = m_sourcecache.getOrLoad(name, "opengl_vertex.glsl");
diff --git a/src/client/shader.h b/src/client/shader.h
index 38ab76704..49a563115 100644
--- a/src/client/shader.h
+++ b/src/client/shader.h
@@ -96,9 +96,10 @@ public:
if (has_been_set && std::equal(m_sent, m_sent + count, value))
return;
if (is_pixel)
- services->setPixelShaderConstant(m_name, value, count);
+ services->setPixelShaderConstant(services->getPixelShaderConstantID(m_name), value, count);
else
- services->setVertexShaderConstant(m_name, value, count);
+ services->setVertexShaderConstant(services->getVertexShaderConstantID(m_name), value, count);
+
std::copy(value, value + count, m_sent);
has_been_set = true;
}
diff --git a/src/client/shadows/dynamicshadows.cpp b/src/client/shadows/dynamicshadows.cpp
new file mode 100644
index 000000000..6ef5a4f1d
--- /dev/null
+++ b/src/client/shadows/dynamicshadows.cpp
@@ -0,0 +1,165 @@
+/*
+Minetest
+Copyright (C) 2021 Liso <anlismon@gmail.com>
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU Lesser General Public License as published by
+the Free Software Foundation; either version 2.1 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU Lesser General Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public License along
+with this program; if not, write to the Free Software Foundation, Inc.,
+51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+
+#include <cmath>
+
+#include "client/shadows/dynamicshadows.h"
+#include "client/client.h"
+#include "client/clientenvironment.h"
+#include "client/clientmap.h"
+#include "client/camera.h"
+
+using m4f = core::matrix4;
+
+void DirectionalLight::createSplitMatrices(const Camera *cam)
+{
+ float radius;
+ v3f newCenter;
+ v3f look = cam->getDirection();
+
+ // camera view tangents
+ float tanFovY = tanf(cam->getFovY() * 0.5f);
+ float tanFovX = tanf(cam->getFovX() * 0.5f);
+
+ // adjusted frustum boundaries
+ float sfNear = future_frustum.zNear;
+ float sfFar = adjustDist(future_frustum.zFar, cam->getFovY());
+
+ // adjusted camera positions
+ v3f camPos2 = cam->getPosition();
+ v3f camPos = v3f(camPos2.X - cam->getOffset().X * BS,
+ camPos2.Y - cam->getOffset().Y * BS,
+ camPos2.Z - cam->getOffset().Z * BS);
+ camPos += look * sfNear;
+ camPos2 += look * sfNear;
+
+ // center point of light frustum
+ float end = sfNear + sfFar;
+ newCenter = camPos + look * (sfNear + 0.05f * end);
+ v3f world_center = camPos2 + look * (sfNear + 0.05f * end);
+
+ // Create a vector to the frustum far corner
+ const v3f &viewUp = cam->getCameraNode()->getUpVector();
+ v3f viewRight = look.crossProduct(viewUp);
+
+ v3f farCorner = look + viewRight * tanFovX + viewUp * tanFovY;
+ // Compute the frustumBoundingSphere radius
+ v3f boundVec = (camPos + farCorner * sfFar) - newCenter;
+ radius = boundVec.getLength() * 2.0f;
+ // boundVec.getLength();
+ float vvolume = radius * 2.0f;
+
+ v3f frustumCenter = newCenter;
+ // probar radius multipliacdor en funcion del I, a menor I mas multiplicador
+ v3f eye_displacement = direction * vvolume;
+
+ // we must compute the viewmat with the position - the camera offset
+ // but the future_frustum position must be the actual world position
+ v3f eye = frustumCenter - eye_displacement;
+ future_frustum.position = world_center - eye_displacement;
+ future_frustum.length = vvolume;
+ future_frustum.ViewMat.buildCameraLookAtMatrixLH(eye, frustumCenter, v3f(0.0f, 1.0f, 0.0f));
+ future_frustum.ProjOrthMat.buildProjectionMatrixOrthoLH(future_frustum.length,
+ future_frustum.length, -future_frustum.length,
+ future_frustum.length,false);
+ future_frustum.camera_offset = cam->getOffset();
+}
+
+DirectionalLight::DirectionalLight(const u32 shadowMapResolution,
+ const v3f &position, video::SColorf lightColor,
+ f32 farValue) :
+ diffuseColor(lightColor),
+ farPlane(farValue), mapRes(shadowMapResolution), pos(position)
+{}
+
+void DirectionalLight::update_frustum(const Camera *cam, Client *client, bool force)
+{
+ if (dirty && !force)
+ return;
+
+ float zNear = cam->getCameraNode()->getNearValue();
+ float zFar = getMaxFarValue();
+
+ ///////////////////////////////////
+ // update splits near and fars
+ future_frustum.zNear = zNear;
+ future_frustum.zFar = zFar;
+
+ // update shadow frustum
+ createSplitMatrices(cam);
+ // get the draw list for shadows
+ client->getEnv().getClientMap().updateDrawListShadow(
+ getPosition(), getDirection(), future_frustum.length);
+ should_update_map_shadow = true;
+ dirty = true;
+
+ // when camera offset changes, adjust the current frustum view matrix to avoid flicker
+ v3s16 cam_offset = cam->getOffset();
+ if (cam_offset != shadow_frustum.camera_offset) {
+ v3f rotated_offset;
+ shadow_frustum.ViewMat.rotateVect(rotated_offset, intToFloat(cam_offset - shadow_frustum.camera_offset, BS));
+ shadow_frustum.ViewMat.setTranslation(shadow_frustum.ViewMat.getTranslation() + rotated_offset);
+ shadow_frustum.camera_offset = cam_offset;
+ }
+}
+
+void DirectionalLight::commitFrustum()
+{
+ if (!dirty)
+ return;
+
+ shadow_frustum = future_frustum;
+ dirty = false;
+}
+
+void DirectionalLight::setDirection(v3f dir)
+{
+ direction = -dir;
+ direction.normalize();
+}
+
+v3f DirectionalLight::getPosition() const
+{
+ return shadow_frustum.position;
+}
+
+const m4f &DirectionalLight::getViewMatrix() const
+{
+ return shadow_frustum.ViewMat;
+}
+
+const m4f &DirectionalLight::getProjectionMatrix() const
+{
+ return shadow_frustum.ProjOrthMat;
+}
+
+const m4f &DirectionalLight::getFutureViewMatrix() const
+{
+ return future_frustum.ViewMat;
+}
+
+const m4f &DirectionalLight::getFutureProjectionMatrix() const
+{
+ return future_frustum.ProjOrthMat;
+}
+
+m4f DirectionalLight::getViewProjMatrix()
+{
+ return shadow_frustum.ProjOrthMat * shadow_frustum.ViewMat;
+}
diff --git a/src/client/shadows/dynamicshadows.h b/src/client/shadows/dynamicshadows.h
new file mode 100644
index 000000000..d8be66be8
--- /dev/null
+++ b/src/client/shadows/dynamicshadows.h
@@ -0,0 +1,109 @@
+/*
+Minetest
+Copyright (C) 2021 Liso <anlismon@gmail.com>
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU Lesser General Public License as published by
+the Free Software Foundation; either version 2.1 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU Lesser General Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public License along
+with this program; if not, write to the Free Software Foundation, Inc.,
+51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+
+#pragma once
+
+#include "irrlichttypes_bloated.h"
+#include <matrix4.h>
+#include "util/basic_macros.h"
+
+class Camera;
+class Client;
+
+struct shadowFrustum
+{
+ float zNear{0.0f};
+ float zFar{0.0f};
+ float length{0.0f};
+ core::matrix4 ProjOrthMat;
+ core::matrix4 ViewMat;
+ v3f position;
+ v3s16 camera_offset;
+};
+
+class DirectionalLight
+{
+public:
+ DirectionalLight(const u32 shadowMapResolution,
+ const v3f &position,
+ video::SColorf lightColor = video::SColor(0xffffffff),
+ f32 farValue = 100.0f);
+ ~DirectionalLight() = default;
+
+ //DISABLE_CLASS_COPY(DirectionalLight)
+
+ void update_frustum(const Camera *cam, Client *client, bool force = false);
+
+ // when set direction is updated to negative normalized(direction)
+ void setDirection(v3f dir);
+ v3f getDirection() const{
+ return direction;
+ };
+ v3f getPosition() const;
+
+ /// Gets the light's matrices.
+ const core::matrix4 &getViewMatrix() const;
+ const core::matrix4 &getProjectionMatrix() const;
+ const core::matrix4 &getFutureViewMatrix() const;
+ const core::matrix4 &getFutureProjectionMatrix() const;
+ core::matrix4 getViewProjMatrix();
+
+ /// Gets the light's far value.
+ f32 getMaxFarValue() const
+ {
+ return farPlane;
+ }
+
+
+ /// Gets the light's color.
+ const video::SColorf &getLightColor() const
+ {
+ return diffuseColor;
+ }
+
+ /// Sets the light's color.
+ void setLightColor(const video::SColorf &lightColor)
+ {
+ diffuseColor = lightColor;
+ }
+
+ /// Gets the shadow map resolution for this light.
+ u32 getMapResolution() const
+ {
+ return mapRes;
+ }
+
+ bool should_update_map_shadow{true};
+
+ void commitFrustum();
+
+private:
+ void createSplitMatrices(const Camera *cam);
+
+ video::SColorf diffuseColor;
+
+ f32 farPlane;
+ u32 mapRes;
+
+ v3f pos;
+ v3f direction{0};
+ shadowFrustum shadow_frustum;
+ shadowFrustum future_frustum;
+ bool dirty{false};
+};
diff --git a/src/client/shadows/dynamicshadowsrender.cpp b/src/client/shadows/dynamicshadowsrender.cpp
new file mode 100644
index 000000000..a913a9290
--- /dev/null
+++ b/src/client/shadows/dynamicshadowsrender.cpp
@@ -0,0 +1,630 @@
+/*
+Minetest
+Copyright (C) 2021 Liso <anlismon@gmail.com>
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU Lesser General Public License as published by
+the Free Software Foundation; either version 2.1 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU Lesser General Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public License along
+with this program; if not, write to the Free Software Foundation, Inc.,
+51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+
+#include <cstring>
+#include "client/shadows/dynamicshadowsrender.h"
+#include "client/shadows/shadowsScreenQuad.h"
+#include "client/shadows/shadowsshadercallbacks.h"
+#include "settings.h"
+#include "filesys.h"
+#include "util/string.h"
+#include "client/shader.h"
+#include "client/client.h"
+#include "client/clientmap.h"
+#include "profiler.h"
+
+ShadowRenderer::ShadowRenderer(IrrlichtDevice *device, Client *client) :
+ m_device(device), m_smgr(device->getSceneManager()),
+ m_driver(device->getVideoDriver()), m_client(client), m_current_frame(0)
+{
+ m_shadows_enabled = true;
+
+ m_shadow_strength = g_settings->getFloat("shadow_strength");
+
+ m_shadow_map_max_distance = g_settings->getFloat("shadow_map_max_distance");
+
+ m_shadow_map_texture_size = g_settings->getFloat("shadow_map_texture_size");
+
+ m_shadow_map_texture_32bit = g_settings->getBool("shadow_map_texture_32bit");
+ m_shadow_map_colored = g_settings->getBool("shadow_map_color");
+ m_shadow_samples = g_settings->getS32("shadow_filters");
+ m_map_shadow_update_frames = g_settings->getS16("shadow_update_frames");
+}
+
+ShadowRenderer::~ShadowRenderer()
+{
+ if (m_shadow_depth_cb)
+ delete m_shadow_depth_cb;
+ if (m_shadow_mix_cb)
+ delete m_shadow_mix_cb;
+ m_shadow_node_array.clear();
+ m_light_list.clear();
+
+ if (shadowMapTextureDynamicObjects)
+ m_driver->removeTexture(shadowMapTextureDynamicObjects);
+
+ if (shadowMapTextureFinal)
+ m_driver->removeTexture(shadowMapTextureFinal);
+
+ if (shadowMapTextureColors)
+ m_driver->removeTexture(shadowMapTextureColors);
+
+ if (shadowMapClientMap)
+ m_driver->removeTexture(shadowMapClientMap);
+
+ if (shadowMapClientMapFuture)
+ m_driver->removeTexture(shadowMapClientMapFuture);
+}
+
+void ShadowRenderer::initialize()
+{
+ auto *gpu = m_driver->getGPUProgrammingServices();
+
+ // we need glsl
+ if (m_shadows_enabled && gpu && m_driver->queryFeature(video::EVDF_ARB_GLSL)) {
+ createShaders();
+ } else {
+ m_shadows_enabled = false;
+
+ warningstream << "Shadows: GLSL Shader not supported on this system."
+ << std::endl;
+ return;
+ }
+
+ m_texture_format = m_shadow_map_texture_32bit
+ ? video::ECOLOR_FORMAT::ECF_R32F
+ : video::ECOLOR_FORMAT::ECF_R16F;
+
+ m_texture_format_color = m_shadow_map_texture_32bit
+ ? video::ECOLOR_FORMAT::ECF_G32R32F
+ : video::ECOLOR_FORMAT::ECF_G16R16F;
+}
+
+
+size_t ShadowRenderer::addDirectionalLight()
+{
+ m_light_list.emplace_back(m_shadow_map_texture_size,
+ v3f(0.f, 0.f, 0.f),
+ video::SColor(255, 255, 255, 255), m_shadow_map_max_distance);
+ return m_light_list.size() - 1;
+}
+
+DirectionalLight &ShadowRenderer::getDirectionalLight(u32 index)
+{
+ return m_light_list[index];
+}
+
+size_t ShadowRenderer::getDirectionalLightCount() const
+{
+ return m_light_list.size();
+}
+
+f32 ShadowRenderer::getMaxShadowFar() const
+{
+ if (!m_light_list.empty()) {
+ float wanted_range = m_client->getEnv().getClientMap().getWantedRange();
+
+ float zMax = m_light_list[0].getMaxFarValue() > wanted_range
+ ? wanted_range
+ : m_light_list[0].getMaxFarValue();
+ return zMax * MAP_BLOCKSIZE;
+ }
+ return 0.0f;
+}
+
+void ShadowRenderer::addNodeToShadowList(
+ scene::ISceneNode *node, E_SHADOW_MODE shadowMode)
+{
+ m_shadow_node_array.emplace_back(NodeToApply(node, shadowMode));
+}
+
+void ShadowRenderer::removeNodeFromShadowList(scene::ISceneNode *node)
+{
+ for (auto it = m_shadow_node_array.begin(); it != m_shadow_node_array.end();) {
+ if (it->node == node) {
+ it = m_shadow_node_array.erase(it);
+ break;
+ } else {
+ ++it;
+ }
+ }
+}
+
+void ShadowRenderer::updateSMTextures()
+{
+ if (!m_shadows_enabled || m_smgr->getActiveCamera() == nullptr) {
+ return;
+ }
+
+ if (!shadowMapTextureDynamicObjects) {
+
+ shadowMapTextureDynamicObjects = getSMTexture(
+ std::string("shadow_dynamic_") + itos(m_shadow_map_texture_size),
+ m_texture_format, true);
+ }
+
+ if (!shadowMapClientMap) {
+
+ shadowMapClientMap = getSMTexture(
+ std::string("shadow_clientmap_") + itos(m_shadow_map_texture_size),
+ m_shadow_map_colored ? m_texture_format_color : m_texture_format,
+ true);
+ }
+
+ if (!shadowMapClientMapFuture && m_map_shadow_update_frames > 1) {
+ shadowMapClientMapFuture = getSMTexture(
+ std::string("shadow_clientmap_bb_") + itos(m_shadow_map_texture_size),
+ m_shadow_map_colored ? m_texture_format_color : m_texture_format,
+ true);
+ }
+
+ if (m_shadow_map_colored && !shadowMapTextureColors) {
+ shadowMapTextureColors = getSMTexture(
+ std::string("shadow_colored_") + itos(m_shadow_map_texture_size),
+ m_shadow_map_colored ? m_texture_format_color : m_texture_format,
+ true);
+ }
+
+ // The merge all shadowmaps texture
+ if (!shadowMapTextureFinal) {
+ video::ECOLOR_FORMAT frt;
+ if (m_shadow_map_texture_32bit) {
+ if (m_shadow_map_colored)
+ frt = video::ECOLOR_FORMAT::ECF_A32B32G32R32F;
+ else
+ frt = video::ECOLOR_FORMAT::ECF_R32F;
+ } else {
+ if (m_shadow_map_colored)
+ frt = video::ECOLOR_FORMAT::ECF_A16B16G16R16F;
+ else
+ frt = video::ECOLOR_FORMAT::ECF_R16F;
+ }
+ shadowMapTextureFinal = getSMTexture(
+ std::string("shadowmap_final_") + itos(m_shadow_map_texture_size),
+ frt, true);
+ }
+
+ if (!m_shadow_node_array.empty() && !m_light_list.empty()) {
+ bool reset_sm_texture = false;
+
+ // detect if SM should be regenerated
+ for (DirectionalLight &light : m_light_list) {
+ if (light.should_update_map_shadow) {
+ light.should_update_map_shadow = false;
+ m_current_frame = 0;
+ reset_sm_texture = true;
+ }
+ }
+
+ video::ITexture* shadowMapTargetTexture = shadowMapClientMapFuture;
+ if (shadowMapTargetTexture == nullptr)
+ shadowMapTargetTexture = shadowMapClientMap;
+
+ // Update SM incrementally:
+ for (DirectionalLight &light : m_light_list) {
+ // Static shader values.
+ m_shadow_depth_cb->MapRes = (f32)m_shadow_map_texture_size;
+ m_shadow_depth_cb->MaxFar = (f32)m_shadow_map_max_distance * BS;
+
+ // set the Render Target
+ // right now we can only render in usual RTT, not
+ // Depth texture is available in irrlicth maybe we
+ // should put some gl* fn here
+
+
+ if (m_current_frame < m_map_shadow_update_frames) {
+ m_driver->setRenderTarget(shadowMapTargetTexture, reset_sm_texture, true,
+ video::SColor(255, 255, 255, 255));
+ renderShadowMap(shadowMapTargetTexture, light);
+
+ // Render transparent part in one pass.
+ // This is also handled in ClientMap.
+ if (m_current_frame == m_map_shadow_update_frames - 1) {
+ if (m_shadow_map_colored) {
+ m_driver->setRenderTarget(0, false, false);
+ m_driver->setRenderTarget(shadowMapTextureColors,
+ true, false, video::SColor(255, 255, 255, 255));
+ }
+ renderShadowMap(shadowMapTextureColors, light,
+ scene::ESNRP_TRANSPARENT);
+ }
+ m_driver->setRenderTarget(0, false, false);
+ }
+
+ reset_sm_texture = false;
+ } // end for lights
+
+ // move to the next section
+ if (m_current_frame <= m_map_shadow_update_frames)
+ ++m_current_frame;
+
+ // pass finished, swap textures and commit light changes
+ if (m_current_frame == m_map_shadow_update_frames) {
+ if (shadowMapClientMapFuture != nullptr)
+ std::swap(shadowMapClientMapFuture, shadowMapClientMap);
+
+ // Let all lights know that maps are updated
+ for (DirectionalLight &light : m_light_list)
+ light.commitFrustum();
+ }
+ }
+}
+
+void ShadowRenderer::update(video::ITexture *outputTarget)
+{
+ if (!m_shadows_enabled || m_smgr->getActiveCamera() == nullptr) {
+ return;
+ }
+
+ updateSMTextures();
+
+ if (!m_shadow_node_array.empty() && !m_light_list.empty()) {
+
+ for (DirectionalLight &light : m_light_list) {
+ // Static shader values.
+ m_shadow_depth_cb->MapRes = (f32)m_shadow_map_texture_size;
+ m_shadow_depth_cb->MaxFar = (f32)m_shadow_map_max_distance * BS;
+
+ // render shadows for the n0n-map objects.
+ m_driver->setRenderTarget(shadowMapTextureDynamicObjects, true,
+ true, video::SColor(255, 255, 255, 255));
+ renderShadowObjects(shadowMapTextureDynamicObjects, light);
+ // clear the Render Target
+ m_driver->setRenderTarget(0, false, false);
+
+ // in order to avoid too many map shadow renders,
+ // we should make a second pass to mix clientmap shadows and
+ // entities shadows :(
+ m_screen_quad->getMaterial().setTexture(0, shadowMapClientMap);
+ // dynamic objs shadow texture.
+ if (m_shadow_map_colored)
+ m_screen_quad->getMaterial().setTexture(1, shadowMapTextureColors);
+ m_screen_quad->getMaterial().setTexture(2, shadowMapTextureDynamicObjects);
+
+ m_driver->setRenderTarget(shadowMapTextureFinal, false, false,
+ video::SColor(255, 255, 255, 255));
+ m_screen_quad->render(m_driver);
+ m_driver->setRenderTarget(0, false, false);
+
+ } // end for lights
+ }
+}
+
+void ShadowRenderer::drawDebug()
+{
+ /* this code just shows shadows textures in screen and in ONLY for debugging*/
+ #if 0
+ // this is debug, ignore for now.
+ m_driver->draw2DImage(shadowMapTextureFinal,
+ core::rect<s32>(0, 50, 128, 128 + 50),
+ core::rect<s32>({0, 0}, shadowMapTextureFinal->getSize()));
+
+ m_driver->draw2DImage(shadowMapClientMap,
+ core::rect<s32>(0, 50 + 128, 128, 128 + 50 + 128),
+ core::rect<s32>({0, 0}, shadowMapTextureFinal->getSize()));
+ m_driver->draw2DImage(shadowMapTextureDynamicObjects,
+ core::rect<s32>(0, 128 + 50 + 128, 128,
+ 128 + 50 + 128 + 128),
+ core::rect<s32>({0, 0}, shadowMapTextureDynamicObjects->getSize()));
+
+ if (m_shadow_map_colored) {
+
+ m_driver->draw2DImage(shadowMapTextureColors,
+ core::rect<s32>(128,128 + 50 + 128 + 128,
+ 128 + 128, 128 + 50 + 128 + 128 + 128),
+ core::rect<s32>({0, 0}, shadowMapTextureColors->getSize()));
+ }
+ #endif
+}
+
+
+video::ITexture *ShadowRenderer::getSMTexture(const std::string &shadow_map_name,
+ video::ECOLOR_FORMAT texture_format, bool force_creation)
+{
+ if (force_creation) {
+ return m_driver->addRenderTargetTexture(
+ core::dimension2du(m_shadow_map_texture_size,
+ m_shadow_map_texture_size),
+ shadow_map_name.c_str(), texture_format);
+ }
+
+ return m_driver->getTexture(shadow_map_name.c_str());
+}
+
+void ShadowRenderer::renderShadowMap(video::ITexture *target,
+ DirectionalLight &light, scene::E_SCENE_NODE_RENDER_PASS pass)
+{
+ m_driver->setTransform(video::ETS_VIEW, light.getFutureViewMatrix());
+ m_driver->setTransform(video::ETS_PROJECTION, light.getFutureProjectionMatrix());
+
+ // Operate on the client map
+ for (const auto &shadow_node : m_shadow_node_array) {
+ if (strcmp(shadow_node.node->getName(), "ClientMap") != 0)
+ continue;
+
+ ClientMap *map_node = static_cast<ClientMap *>(shadow_node.node);
+
+ video::SMaterial material;
+ if (map_node->getMaterialCount() > 0) {
+ // we only want the first material, which is the one with the albedo info
+ material = map_node->getMaterial(0);
+ }
+
+ material.BackfaceCulling = false;
+ material.FrontfaceCulling = true;
+ material.PolygonOffsetFactor = 4.0f;
+ material.PolygonOffsetDirection = video::EPO_BACK;
+ //material.PolygonOffsetDepthBias = 1.0f/4.0f;
+ //material.PolygonOffsetSlopeScale = -1.f;
+
+ if (m_shadow_map_colored && pass != scene::ESNRP_SOLID) {
+ material.MaterialType = (video::E_MATERIAL_TYPE) depth_shader_trans;
+ }
+ else {
+ material.MaterialType = (video::E_MATERIAL_TYPE) depth_shader;
+ material.BlendOperation = video::EBO_MIN;
+ }
+
+ // FIXME: I don't think this is needed here
+ map_node->OnAnimate(m_device->getTimer()->getTime());
+
+ m_driver->setTransform(video::ETS_WORLD,
+ map_node->getAbsoluteTransformation());
+
+ map_node->renderMapShadows(m_driver, material, pass, m_current_frame, m_map_shadow_update_frames);
+ break;
+ }
+}
+
+void ShadowRenderer::renderShadowObjects(
+ video::ITexture *target, DirectionalLight &light)
+{
+ m_driver->setTransform(video::ETS_VIEW, light.getViewMatrix());
+ m_driver->setTransform(video::ETS_PROJECTION, light.getProjectionMatrix());
+
+ for (const auto &shadow_node : m_shadow_node_array) {
+ // we only take care of the shadow casters
+ if (shadow_node.shadowMode == ESM_RECEIVE ||
+ strcmp(shadow_node.node->getName(), "ClientMap") == 0)
+ continue;
+
+ // render other objects
+ u32 n_node_materials = shadow_node.node->getMaterialCount();
+ std::vector<s32> BufferMaterialList;
+ std::vector<std::pair<bool, bool>> BufferMaterialCullingList;
+ std::vector<video::E_BLEND_OPERATION> BufferBlendOperationList;
+ BufferMaterialList.reserve(n_node_materials);
+ BufferMaterialCullingList.reserve(n_node_materials);
+ BufferBlendOperationList.reserve(n_node_materials);
+
+ // backup materialtype for each material
+ // (aka shader)
+ // and replace it by our "depth" shader
+ for (u32 m = 0; m < n_node_materials; m++) {
+ auto &current_mat = shadow_node.node->getMaterial(m);
+
+ BufferMaterialList.push_back(current_mat.MaterialType);
+ current_mat.MaterialType =
+ (video::E_MATERIAL_TYPE)depth_shader_entities;
+
+ BufferMaterialCullingList.emplace_back(
+ (bool)current_mat.BackfaceCulling, (bool)current_mat.FrontfaceCulling);
+ BufferBlendOperationList.push_back(current_mat.BlendOperation);
+
+ current_mat.BackfaceCulling = true;
+ current_mat.FrontfaceCulling = false;
+ current_mat.PolygonOffsetFactor = 1.0f/2048.0f;
+ current_mat.PolygonOffsetDirection = video::EPO_BACK;
+ //current_mat.PolygonOffsetDepthBias = 1.0 * 2.8e-6;
+ //current_mat.PolygonOffsetSlopeScale = -1.f;
+ }
+
+ m_driver->setTransform(video::ETS_WORLD,
+ shadow_node.node->getAbsoluteTransformation());
+ shadow_node.node->render();
+
+ // restore the material.
+
+ for (u32 m = 0; m < n_node_materials; m++) {
+ auto &current_mat = shadow_node.node->getMaterial(m);
+
+ current_mat.MaterialType = (video::E_MATERIAL_TYPE) BufferMaterialList[m];
+
+ current_mat.BackfaceCulling = BufferMaterialCullingList[m].first;
+ current_mat.FrontfaceCulling = BufferMaterialCullingList[m].second;
+ current_mat.BlendOperation = BufferBlendOperationList[m];
+ }
+
+ } // end for caster shadow nodes
+}
+
+void ShadowRenderer::mixShadowsQuad()
+{
+}
+
+/*
+ * @Liso's disclaimer ;) This function loads the Shadow Mapping Shaders.
+ * I used a custom loader because I couldn't figure out how to use the base
+ * Shaders system with custom IShaderConstantSetCallBack without messing up the
+ * code too much. If anyone knows how to integrate this with the standard MT
+ * shaders, please feel free to change it.
+ */
+
+void ShadowRenderer::createShaders()
+{
+ video::IGPUProgrammingServices *gpu = m_driver->getGPUProgrammingServices();
+
+ if (depth_shader == -1) {
+ std::string depth_shader_vs = getShaderPath("shadow_shaders", "pass1_vertex.glsl");
+ if (depth_shader_vs.empty()) {
+ m_shadows_enabled = false;
+ errorstream << "Error shadow mapping vs shader not found." << std::endl;
+ return;
+ }
+ std::string depth_shader_fs = getShaderPath("shadow_shaders", "pass1_fragment.glsl");
+ if (depth_shader_fs.empty()) {
+ m_shadows_enabled = false;
+ errorstream << "Error shadow mapping fs shader not found." << std::endl;
+ return;
+ }
+ m_shadow_depth_cb = new ShadowDepthShaderCB();
+
+ depth_shader = gpu->addHighLevelShaderMaterial(
+ readShaderFile(depth_shader_vs).c_str(), "vertexMain",
+ video::EVST_VS_1_1,
+ readShaderFile(depth_shader_fs).c_str(), "pixelMain",
+ video::EPST_PS_1_2, m_shadow_depth_cb, video::EMT_ONETEXTURE_BLEND);
+
+ if (depth_shader == -1) {
+ // upsi, something went wrong loading shader.
+ delete m_shadow_depth_cb;
+ m_shadows_enabled = false;
+ errorstream << "Error compiling shadow mapping shader." << std::endl;
+ return;
+ }
+
+ // HACK, TODO: investigate this better
+ // Grab the material renderer once more so minetest doesn't crash
+ // on exit
+ m_driver->getMaterialRenderer(depth_shader)->grab();
+ }
+
+ // This creates a clone of depth_shader with base material set to EMT_SOLID,
+ // because entities won't render shadows with base material EMP_ONETEXTURE_BLEND
+ if (depth_shader_entities == -1) {
+ std::string depth_shader_vs = getShaderPath("shadow_shaders", "pass1_vertex.glsl");
+ if (depth_shader_vs.empty()) {
+ m_shadows_enabled = false;
+ errorstream << "Error shadow mapping vs shader not found." << std::endl;
+ return;
+ }
+ std::string depth_shader_fs = getShaderPath("shadow_shaders", "pass1_fragment.glsl");
+ if (depth_shader_fs.empty()) {
+ m_shadows_enabled = false;
+ errorstream << "Error shadow mapping fs shader not found." << std::endl;
+ return;
+ }
+
+ depth_shader_entities = gpu->addHighLevelShaderMaterial(
+ readShaderFile(depth_shader_vs).c_str(), "vertexMain",
+ video::EVST_VS_1_1,
+ readShaderFile(depth_shader_fs).c_str(), "pixelMain",
+ video::EPST_PS_1_2, m_shadow_depth_cb);
+
+ if (depth_shader_entities == -1) {
+ // upsi, something went wrong loading shader.
+ m_shadows_enabled = false;
+ errorstream << "Error compiling shadow mapping shader (dynamic)." << std::endl;
+ return;
+ }
+
+ // HACK, TODO: investigate this better
+ // Grab the material renderer once more so minetest doesn't crash
+ // on exit
+ m_driver->getMaterialRenderer(depth_shader_entities)->grab();
+ }
+
+ if (mixcsm_shader == -1) {
+ std::string depth_shader_vs = getShaderPath("shadow_shaders", "pass2_vertex.glsl");
+ if (depth_shader_vs.empty()) {
+ m_shadows_enabled = false;
+ errorstream << "Error cascade shadow mapping fs shader not found." << std::endl;
+ return;
+ }
+
+ std::string depth_shader_fs = getShaderPath("shadow_shaders", "pass2_fragment.glsl");
+ if (depth_shader_fs.empty()) {
+ m_shadows_enabled = false;
+ errorstream << "Error cascade shadow mapping fs shader not found." << std::endl;
+ return;
+ }
+ m_shadow_mix_cb = new shadowScreenQuadCB();
+ m_screen_quad = new shadowScreenQuad();
+ mixcsm_shader = gpu->addHighLevelShaderMaterial(
+ readShaderFile(depth_shader_vs).c_str(), "vertexMain",
+ video::EVST_VS_1_1,
+ readShaderFile(depth_shader_fs).c_str(), "pixelMain",
+ video::EPST_PS_1_2, m_shadow_mix_cb);
+
+ m_screen_quad->getMaterial().MaterialType =
+ (video::E_MATERIAL_TYPE)mixcsm_shader;
+
+ if (mixcsm_shader == -1) {
+ // upsi, something went wrong loading shader.
+ delete m_shadow_mix_cb;
+ delete m_screen_quad;
+ m_shadows_enabled = false;
+ errorstream << "Error compiling cascade shadow mapping shader." << std::endl;
+ return;
+ }
+
+ // HACK, TODO: investigate this better
+ // Grab the material renderer once more so minetest doesn't crash
+ // on exit
+ m_driver->getMaterialRenderer(mixcsm_shader)->grab();
+ }
+
+ if (m_shadow_map_colored && depth_shader_trans == -1) {
+ std::string depth_shader_vs = getShaderPath("shadow_shaders", "pass1_trans_vertex.glsl");
+ if (depth_shader_vs.empty()) {
+ m_shadows_enabled = false;
+ errorstream << "Error shadow mapping vs shader not found." << std::endl;
+ return;
+ }
+ std::string depth_shader_fs = getShaderPath("shadow_shaders", "pass1_trans_fragment.glsl");
+ if (depth_shader_fs.empty()) {
+ m_shadows_enabled = false;
+ errorstream << "Error shadow mapping fs shader not found." << std::endl;
+ return;
+ }
+ m_shadow_depth_trans_cb = new ShadowDepthShaderCB();
+
+ depth_shader_trans = gpu->addHighLevelShaderMaterial(
+ readShaderFile(depth_shader_vs).c_str(), "vertexMain",
+ video::EVST_VS_1_1,
+ readShaderFile(depth_shader_fs).c_str(), "pixelMain",
+ video::EPST_PS_1_2, m_shadow_depth_trans_cb);
+
+ if (depth_shader_trans == -1) {
+ // upsi, something went wrong loading shader.
+ delete m_shadow_depth_trans_cb;
+ m_shadow_map_colored = false;
+ m_shadows_enabled = false;
+ errorstream << "Error compiling colored shadow mapping shader." << std::endl;
+ return;
+ }
+
+ // HACK, TODO: investigate this better
+ // Grab the material renderer once more so minetest doesn't crash
+ // on exit
+ m_driver->getMaterialRenderer(depth_shader_trans)->grab();
+ }
+}
+
+std::string ShadowRenderer::readShaderFile(const std::string &path)
+{
+ std::string prefix;
+ if (m_shadow_map_colored)
+ prefix.append("#define COLORED_SHADOWS 1\n");
+
+ std::string content;
+ fs::ReadFile(path, content);
+
+ return prefix + content;
+}
diff --git a/src/client/shadows/dynamicshadowsrender.h b/src/client/shadows/dynamicshadowsrender.h
new file mode 100644
index 000000000..e4b3c3e22
--- /dev/null
+++ b/src/client/shadows/dynamicshadowsrender.h
@@ -0,0 +1,147 @@
+/*
+Minetest
+Copyright (C) 2021 Liso <anlismon@gmail.com>
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU Lesser General Public License as published by
+the Free Software Foundation; either version 2.1 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU Lesser General Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public License along
+with this program; if not, write to the Free Software Foundation, Inc.,
+51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+
+#pragma once
+
+#include <string>
+#include <vector>
+#include "irrlichttypes_extrabloated.h"
+#include "client/shadows/dynamicshadows.h"
+
+class ShadowDepthShaderCB;
+class shadowScreenQuad;
+class shadowScreenQuadCB;
+
+enum E_SHADOW_MODE : u8
+{
+ ESM_RECEIVE = 0,
+ ESM_BOTH,
+};
+
+struct NodeToApply
+{
+ NodeToApply(scene::ISceneNode *n,
+ E_SHADOW_MODE m = E_SHADOW_MODE::ESM_BOTH) :
+ node(n),
+ shadowMode(m){};
+ bool operator<(const NodeToApply &other) const { return node < other.node; };
+
+ scene::ISceneNode *node;
+
+ E_SHADOW_MODE shadowMode{E_SHADOW_MODE::ESM_BOTH};
+ bool dirty{false};
+};
+
+class ShadowRenderer
+{
+public:
+ ShadowRenderer(IrrlichtDevice *device, Client *client);
+
+ ~ShadowRenderer();
+
+ void initialize();
+
+ /// Adds a directional light shadow map (Usually just one (the sun) except in
+ /// Tattoine ).
+ size_t addDirectionalLight();
+ DirectionalLight &getDirectionalLight(u32 index = 0);
+ size_t getDirectionalLightCount() const;
+ f32 getMaxShadowFar() const;
+
+ /// Adds a shadow to the scene node.
+ /// The shadow mode can be ESM_BOTH, or ESM_RECEIVE.
+ /// ESM_BOTH casts and receives shadows
+ /// ESM_RECEIVE only receives but does not cast shadows.
+ ///
+ void addNodeToShadowList(scene::ISceneNode *node,
+ E_SHADOW_MODE shadowMode = ESM_BOTH);
+ void removeNodeFromShadowList(scene::ISceneNode *node);
+
+ void update(video::ITexture *outputTarget = nullptr);
+ void drawDebug();
+
+ video::ITexture *get_texture()
+ {
+ return shadowMapTextureFinal;
+ }
+
+
+ bool is_active() const { return m_shadows_enabled; }
+ void setTimeOfDay(float isDay) { m_time_day = isDay; };
+
+ s32 getShadowSamples() const { return m_shadow_samples; }
+ float getShadowStrength() const { return m_shadow_strength; }
+ float getTimeOfDay() const { return m_time_day; }
+
+private:
+ video::ITexture *getSMTexture(const std::string &shadow_map_name,
+ video::ECOLOR_FORMAT texture_format,
+ bool force_creation = false);
+
+ void renderShadowMap(video::ITexture *target, DirectionalLight &light,
+ scene::E_SCENE_NODE_RENDER_PASS pass =
+ scene::ESNRP_SOLID);
+ void renderShadowObjects(video::ITexture *target, DirectionalLight &light);
+ void mixShadowsQuad();
+ void updateSMTextures();
+
+ // a bunch of variables
+ IrrlichtDevice *m_device{nullptr};
+ scene::ISceneManager *m_smgr{nullptr};
+ video::IVideoDriver *m_driver{nullptr};
+ Client *m_client{nullptr};
+ video::ITexture *shadowMapClientMap{nullptr};
+ video::ITexture *shadowMapClientMapFuture{nullptr};
+ video::ITexture *shadowMapTextureFinal{nullptr};
+ video::ITexture *shadowMapTextureDynamicObjects{nullptr};
+ video::ITexture *shadowMapTextureColors{nullptr};
+
+ std::vector<DirectionalLight> m_light_list;
+ std::vector<NodeToApply> m_shadow_node_array;
+
+ float m_shadow_strength;
+ float m_shadow_map_max_distance;
+ float m_shadow_map_texture_size;
+ float m_time_day{0.0f};
+ int m_shadow_samples;
+ bool m_shadow_map_texture_32bit;
+ bool m_shadows_enabled;
+ bool m_shadow_map_colored;
+ u8 m_map_shadow_update_frames; /* Use this number of frames to update map shaodw */
+ u8 m_current_frame{0}; /* Current frame */
+
+ video::ECOLOR_FORMAT m_texture_format{video::ECOLOR_FORMAT::ECF_R16F};
+ video::ECOLOR_FORMAT m_texture_format_color{video::ECOLOR_FORMAT::ECF_R16G16};
+
+ // Shadow Shader stuff
+
+ void createShaders();
+ std::string readShaderFile(const std::string &path);
+
+ s32 depth_shader{-1};
+ s32 depth_shader_entities{-1};
+ s32 depth_shader_trans{-1};
+ s32 mixcsm_shader{-1};
+
+ ShadowDepthShaderCB *m_shadow_depth_cb{nullptr};
+ ShadowDepthShaderCB *m_shadow_depth_trans_cb{nullptr};
+
+ shadowScreenQuad *m_screen_quad{nullptr};
+ shadowScreenQuadCB *m_shadow_mix_cb{nullptr};
+};
diff --git a/src/client/shadows/shadowsScreenQuad.cpp b/src/client/shadows/shadowsScreenQuad.cpp
new file mode 100644
index 000000000..5f6d38157
--- /dev/null
+++ b/src/client/shadows/shadowsScreenQuad.cpp
@@ -0,0 +1,61 @@
+/*
+Minetest
+Copyright (C) 2021 Liso <anlismon@gmail.com>
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU Lesser General Public License as published by
+the Free Software Foundation; either version 2.1 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU Lesser General Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public License along
+with this program; if not, write to the Free Software Foundation, Inc.,
+51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+
+#include "shadowsScreenQuad.h"
+
+shadowScreenQuad::shadowScreenQuad()
+{
+ Material.Wireframe = false;
+ Material.Lighting = false;
+
+ video::SColor color(0x0);
+ Vertices[0] = video::S3DVertex(
+ -1.0f, -1.0f, 0.0f, 0, 0, 1, color, 0.0f, 1.0f);
+ Vertices[1] = video::S3DVertex(
+ -1.0f, 1.0f, 0.0f, 0, 0, 1, color, 0.0f, 0.0f);
+ Vertices[2] = video::S3DVertex(
+ 1.0f, 1.0f, 0.0f, 0, 0, 1, color, 1.0f, 0.0f);
+ Vertices[3] = video::S3DVertex(
+ 1.0f, -1.0f, 0.0f, 0, 0, 1, color, 1.0f, 1.0f);
+ Vertices[4] = video::S3DVertex(
+ -1.0f, -1.0f, 0.0f, 0, 0, 1, color, 0.0f, 1.0f);
+ Vertices[5] = video::S3DVertex(
+ 1.0f, 1.0f, 0.0f, 0, 0, 1, color, 1.0f, 0.0f);
+}
+
+void shadowScreenQuad::render(video::IVideoDriver *driver)
+{
+ u16 indices[6] = {0, 1, 2, 3, 4, 5};
+ driver->setMaterial(Material);
+ driver->setTransform(video::ETS_WORLD, core::matrix4());
+ driver->drawIndexedTriangleList(&Vertices[0], 6, &indices[0], 2);
+}
+
+void shadowScreenQuadCB::OnSetConstants(
+ video::IMaterialRendererServices *services, s32 userData)
+{
+ s32 TextureId = 0;
+ m_sm_client_map_setting.set(&TextureId, services);
+
+ TextureId = 1;
+ m_sm_client_map_trans_setting.set(&TextureId, services);
+
+ TextureId = 2;
+ m_sm_dynamic_sampler_setting.set(&TextureId, services);
+}
diff --git a/src/client/shadows/shadowsScreenQuad.h b/src/client/shadows/shadowsScreenQuad.h
new file mode 100644
index 000000000..c18be9a2b
--- /dev/null
+++ b/src/client/shadows/shadowsScreenQuad.h
@@ -0,0 +1,54 @@
+/*
+Minetest
+Copyright (C) 2021 Liso <anlismon@gmail.com>
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU Lesser General Public License as published by
+the Free Software Foundation; either version 2.1 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU Lesser General Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public License along
+with this program; if not, write to the Free Software Foundation, Inc.,
+51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+
+#pragma once
+#include "irrlichttypes_extrabloated.h"
+#include <IMaterialRendererServices.h>
+#include <IShaderConstantSetCallBack.h>
+#include "client/shader.h"
+
+class shadowScreenQuad
+{
+public:
+ shadowScreenQuad();
+
+ void render(video::IVideoDriver *driver);
+ video::SMaterial &getMaterial() { return Material; }
+
+private:
+ video::S3DVertex Vertices[6];
+ video::SMaterial Material;
+};
+
+class shadowScreenQuadCB : public video::IShaderConstantSetCallBack
+{
+public:
+ shadowScreenQuadCB() :
+ m_sm_client_map_setting("ShadowMapClientMap"),
+ m_sm_client_map_trans_setting("ShadowMapClientMapTraslucent"),
+ m_sm_dynamic_sampler_setting("ShadowMapSamplerdynamic")
+ {}
+
+ virtual void OnSetConstants(video::IMaterialRendererServices *services,
+ s32 userData);
+private:
+ CachedPixelShaderSetting<s32> m_sm_client_map_setting;
+ CachedPixelShaderSetting<s32> m_sm_client_map_trans_setting;
+ CachedPixelShaderSetting<s32> m_sm_dynamic_sampler_setting;
+};
diff --git a/src/unittest/test_player.cpp b/src/client/shadows/shadowsshadercallbacks.cpp
index 6990b4016..65a63f49c 100644
--- a/src/unittest/test_player.cpp
+++ b/src/client/shadows/shadowsshadercallbacks.cpp
@@ -1,6 +1,6 @@
/*
Minetest
-Copyright (C) 2010-2016 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
+Copyright (C) 2021 Liso <anlismon@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
@@ -17,23 +17,20 @@ with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
-#include "test.h"
+#include "client/shadows/shadowsshadercallbacks.h"
-#include "exceptions.h"
-#include "remoteplayer.h"
-#include "server.h"
-
-class TestPlayer : public TestBase
+void ShadowDepthShaderCB::OnSetConstants(
+ video::IMaterialRendererServices *services, s32 userData)
{
-public:
- TestPlayer() { TestManager::registerTestModule(this); }
- const char *getName() { return "TestPlayer"; }
-
- void runTests(IGameDef *gamedef);
-};
+ video::IVideoDriver *driver = services->getVideoDriver();
-static TestPlayer g_test_instance;
+ core::matrix4 lightMVP = driver->getTransform(video::ETS_PROJECTION);
+ lightMVP *= driver->getTransform(video::ETS_VIEW);
+ lightMVP *= driver->getTransform(video::ETS_WORLD);
-void TestPlayer::runTests(IGameDef *gamedef)
-{
+ m_light_mvp_setting.set(lightMVP.pointer(), services);
+ m_map_resolution_setting.set(&MapRes, services);
+ m_max_far_setting.set(&MaxFar, services);
+ s32 TextureId = 0;
+ m_color_map_sampler_setting.set(&TextureId, services);
}
diff --git a/src/client/shadows/shadowsshadercallbacks.h b/src/client/shadows/shadowsshadercallbacks.h
new file mode 100644
index 000000000..3549567c3
--- /dev/null
+++ b/src/client/shadows/shadowsshadercallbacks.h
@@ -0,0 +1,48 @@
+/*
+Minetest
+Copyright (C) 2021 Liso <anlismon@gmail.com>
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU Lesser General Public License as published by
+the Free Software Foundation; either version 2.1 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU Lesser General Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public License along
+with this program; if not, write to the Free Software Foundation, Inc.,
+51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+
+#pragma once
+#include "irrlichttypes_extrabloated.h"
+#include <IMaterialRendererServices.h>
+#include <IShaderConstantSetCallBack.h>
+#include "client/shader.h"
+
+class ShadowDepthShaderCB : public video::IShaderConstantSetCallBack
+{
+public:
+ ShadowDepthShaderCB() :
+ m_light_mvp_setting("LightMVP"),
+ m_map_resolution_setting("MapResolution"),
+ m_max_far_setting("MaxFar"),
+ m_color_map_sampler_setting("ColorMapSampler")
+ {}
+
+ void OnSetMaterial(const video::SMaterial &material) override {}
+
+ void OnSetConstants(video::IMaterialRendererServices *services,
+ s32 userData) override;
+
+ f32 MaxFar{2048.0f}, MapRes{1024.0f};
+
+private:
+ CachedVertexShaderSetting<f32, 16> m_light_mvp_setting;
+ CachedVertexShaderSetting<f32> m_map_resolution_setting;
+ CachedVertexShaderSetting<f32> m_max_far_setting;
+ CachedPixelShaderSetting<s32> m_color_map_sampler_setting;
+};
diff --git a/src/client/sky.cpp b/src/client/sky.cpp
index 2512a0e23..7fe90c6cd 100644
--- a/src/client/sky.cpp
+++ b/src/client/sky.cpp
@@ -18,50 +18,65 @@ with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
+#include <cmath>
#include "sky.h"
-#include "ITexture.h"
-#include "IVideoDriver.h"
-#include "ISceneManager.h"
-#include "ICameraSceneNode.h"
-#include "S3DVertex.h"
+#include <ITexture.h>
+#include <IVideoDriver.h>
+#include <ISceneManager.h>
+#include <ICameraSceneNode.h>
+#include <S3DVertex.h>
#include "client/tile.h"
#include "noise.h" // easeCurve
#include "profiler.h"
#include "util/numeric.h"
-#include <cmath>
#include "client/renderingengine.h"
#include "settings.h"
#include "camera.h" // CameraModes
-#include "config.h"
+
using namespace irr::core;
static video::SMaterial baseMaterial()
{
video::SMaterial mat;
mat.Lighting = false;
-#if ENABLE_GLES
+#if IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR > 8
mat.ZBuffer = video::ECFN_DISABLED;
+ mat.ZWriteEnable = video::EZW_OFF;
#else
+ mat.ZWriteEnable = false;
mat.ZBuffer = video::ECFN_NEVER;
#endif
- mat.ZWriteEnable = false;
mat.AntiAliasing = 0;
mat.TextureLayer[0].TextureWrapU = video::ETC_CLAMP_TO_EDGE;
mat.TextureLayer[0].TextureWrapV = video::ETC_CLAMP_TO_EDGE;
mat.BackfaceCulling = false;
return mat;
-};
+}
-Sky::Sky(s32 id, ITextureSource *tsrc, IShaderSource *ssrc) :
- scene::ISceneNode(RenderingEngine::get_scene_manager()->getRootSceneNode(),
- RenderingEngine::get_scene_manager(), id)
+static inline void disableTextureFiltering(video::SMaterial &mat)
{
+ mat.setFlag(video::E_MATERIAL_FLAG::EMF_BILINEAR_FILTER, false);
+ mat.setFlag(video::E_MATERIAL_FLAG::EMF_TRILINEAR_FILTER, false);
+ mat.setFlag(video::E_MATERIAL_FLAG::EMF_ANISOTROPIC_FILTER, false);
+}
+
+Sky::Sky(s32 id, RenderingEngine *rendering_engine, ITextureSource *tsrc, IShaderSource *ssrc) :
+ scene::ISceneNode(rendering_engine->get_scene_manager()->getRootSceneNode(),
+ rendering_engine->get_scene_manager(), id)
+{
+ m_seed = (u64)myrand() << 32 | myrand();
+
setAutomaticCulling(scene::EAC_OFF);
m_box.MaxEdge.set(0, 0, 0);
m_box.MinEdge.set(0, 0, 0);
m_enable_shaders = g_settings->getBool("enable_shaders");
+ m_sky_params = SkyboxDefaults::getSkyDefaults();
+ m_sun_params = SkyboxDefaults::getSunDefaults();
+ m_moon_params = SkyboxDefaults::getMoonDefaults();
+ m_star_params = SkyboxDefaults::getStarDefaults();
+
// Create materials
m_materials[0] = baseMaterial();
@@ -70,57 +85,30 @@ Sky::Sky(s32 id, ITextureSource *tsrc, IShaderSource *ssrc) :
m_materials[0].ColorMaterial = video::ECM_NONE;
m_materials[1] = baseMaterial();
- //m_materials[1].MaterialType = video::EMT_TRANSPARENT_VERTEX_ALPHA;
m_materials[1].MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
m_materials[2] = baseMaterial();
m_materials[2].setTexture(0, tsrc->getTextureForMesh("sunrisebg.png"));
m_materials[2].MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
- //m_materials[2].MaterialType = video::EMT_TRANSPARENT_ADD_COLOR;
-
- // Ensures that sun and moon textures and tonemaps are correct.
- setSkyDefaults();
- m_sun_texture = tsrc->isKnownSourceImage(m_sun_params.texture) ?
- tsrc->getTextureForMesh(m_sun_params.texture) : nullptr;
- m_moon_texture = tsrc->isKnownSourceImage(m_moon_params.texture) ?
- tsrc->getTextureForMesh(m_moon_params.texture) : nullptr;
- m_sun_tonemap = tsrc->isKnownSourceImage(m_sun_params.tonemap) ?
- tsrc->getTexture(m_sun_params.tonemap) : nullptr;
- m_moon_tonemap = tsrc->isKnownSourceImage(m_moon_params.tonemap) ?
- tsrc->getTexture(m_moon_params.tonemap) : nullptr;
- if (m_sun_texture) {
- m_materials[3] = baseMaterial();
- m_materials[3].setTexture(0, m_sun_texture);
- m_materials[3].MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
- // Disables texture filtering
- m_materials[3].setFlag(video::E_MATERIAL_FLAG::EMF_BILINEAR_FILTER, false);
- m_materials[3].setFlag(video::E_MATERIAL_FLAG::EMF_TRILINEAR_FILTER, false);
- m_materials[3].setFlag(video::E_MATERIAL_FLAG::EMF_ANISOTROPIC_FILTER, false);
- // Use tonemaps if available
- if (m_sun_tonemap)
- m_materials[3].Lighting = true;
- }
- if (m_moon_texture) {
- m_materials[4] = baseMaterial();
- m_materials[4].setTexture(0, m_moon_texture);
- m_materials[4].MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
- // Disables texture filtering
- m_materials[4].setFlag(video::E_MATERIAL_FLAG::EMF_BILINEAR_FILTER, false);
- m_materials[4].setFlag(video::E_MATERIAL_FLAG::EMF_TRILINEAR_FILTER, false);
- m_materials[4].setFlag(video::E_MATERIAL_FLAG::EMF_ANISOTROPIC_FILTER, false);
- // Use tonemaps if available
- if (m_moon_tonemap)
- m_materials[4].Lighting = true;
- }
+ setSunTexture(m_sun_params.texture, m_sun_params.tonemap, tsrc);
+
+ setMoonTexture(m_moon_params.texture, m_moon_params.tonemap, tsrc);
for (int i = 5; i < 11; i++) {
m_materials[i] = baseMaterial();
m_materials[i].Lighting = true;
m_materials[i].MaterialType = video::EMT_SOLID;
}
+
m_directional_colored_fog = g_settings->getBool("directional_colored_fog");
- setStarCount(1000, true);
+
+ if (false && g_settings->getBool("enable_dynamic_shadows")) {
+ float val = g_settings->getFloat("shadow_sky_body_orbit_tilt");
+ m_sky_body_orbit_tilt = rangelim(val, 0.0f, 60.0f);
+ }
+
+ setStarCount(1000);
}
void Sky::OnRegisterSceneNode()
@@ -172,17 +160,7 @@ void Sky::render()
video::SColorf mooncolor_f(0.50, 0.57, 0.65, 1);
video::SColorf mooncolor2_f(0.85, 0.875, 0.9, 1);
- float nightlength = 0.415;
- float wn = nightlength / 2;
- float wicked_time_of_day = 0;
- if (m_time_of_day > wn && m_time_of_day < 1.0 - wn)
- wicked_time_of_day = (m_time_of_day - wn) / (1.0 - wn * 2) * 0.5 + 0.25;
- else if (m_time_of_day < 0.5)
- wicked_time_of_day = m_time_of_day / wn * 0.25;
- else
- wicked_time_of_day = 1.0 - ((1.0 - m_time_of_day) / wn * 0.25);
- /*std::cerr<<"time_of_day="<<m_time_of_day<<" -> "
- <<"wicked_time_of_day="<<wicked_time_of_day<<std::endl;*/
+ float wicked_time_of_day = getWickedTimeOfDay(m_time_of_day);
video::SColor suncolor = suncolor_f.toSColor();
video::SColor suncolor2 = suncolor2_f.toSColor();
@@ -386,20 +364,6 @@ void Sky::update(float time_of_day, float time_brightness,
bool is_dawn = (time_brightness >= 0.20 && time_brightness < 0.35);
- /*
- Development colours
-
- video::SColorf bgcolor_bright_normal_f(170. / 255, 200. / 255, 230. / 255, 1.0);
- video::SColorf bgcolor_bright_dawn_f(0.666, 200. / 255 * 0.7, 230. / 255 * 0.5, 1.0);
- video::SColorf bgcolor_bright_dawn_f(0.666, 0.549, 0.220, 1.0);
- video::SColorf bgcolor_bright_dawn_f(0.666 * 1.2, 0.549 * 1.0, 0.220 * 1.0, 1.0);
- video::SColorf bgcolor_bright_dawn_f(0.666 * 1.2, 0.549 * 1.0, 0.220 * 1.2, 1.0);
-
- video::SColorf cloudcolor_bright_dawn_f(1.0, 0.591, 0.4);
- video::SColorf cloudcolor_bright_dawn_f(1.0, 0.65, 0.44);
- video::SColorf cloudcolor_bright_dawn_f(1.0, 0.7, 0.5);
- */
-
video::SColorf bgcolor_bright_normal_f = m_sky_params.sky_color.day_horizon;
video::SColorf bgcolor_bright_indoor_f = m_sky_params.sky_color.indoors;
video::SColorf bgcolor_bright_dawn_f = m_sky_params.sky_color.dawn_horizon;
@@ -736,10 +700,15 @@ void Sky::place_sky_body(
* day_position: turn the body around the Z axis, to place it depending of the time of the day
*/
{
+ v3f centrum(0, 0, -1);
+ centrum.rotateXZBy(horizon_position);
+ centrum.rotateXYBy(day_position);
+ centrum.rotateYZBy(m_sky_body_orbit_tilt);
for (video::S3DVertex &vertex : vertices) {
// Body is directed to -Z (south) by default
vertex.Pos.rotateXZBy(horizon_position);
vertex.Pos.rotateXYBy(day_position);
+ vertex.Pos.Z += centrum.Z;
}
}
@@ -749,33 +718,29 @@ void Sky::setSunTexture(const std::string &sun_texture,
// Ignore matching textures (with modifiers) entirely,
// but lets at least update the tonemap before hand.
m_sun_params.tonemap = sun_tonemap;
- m_sun_tonemap = tsrc->isKnownSourceImage(m_sun_params.tonemap) ?
- tsrc->getTexture(m_sun_params.tonemap) : nullptr;
+ m_sun_tonemap = tsrc->isKnownSourceImage(sun_tonemap) ?
+ tsrc->getTexture(sun_tonemap) : nullptr;
m_materials[3].Lighting = !!m_sun_tonemap;
- if (m_sun_params.texture == sun_texture)
+ if (m_sun_params.texture == sun_texture && !m_first_update)
return;
m_sun_params.texture = sun_texture;
- if (sun_texture != "") {
- // We want to ensure the texture exists first.
- m_sun_texture = tsrc->getTextureForMesh(m_sun_params.texture);
-
- if (m_sun_texture) {
- m_materials[3] = baseMaterial();
- m_materials[3].setTexture(0, m_sun_texture);
- m_materials[3].MaterialType = video::
- EMT_TRANSPARENT_ALPHA_CHANNEL;
- // Disables texture filtering
- m_materials[3].setFlag(
- video::E_MATERIAL_FLAG::EMF_BILINEAR_FILTER, false);
- m_materials[3].setFlag(
- video::E_MATERIAL_FLAG::EMF_TRILINEAR_FILTER, false);
- m_materials[3].setFlag(
- video::E_MATERIAL_FLAG::EMF_ANISOTROPIC_FILTER, false);
- }
- } else {
- m_sun_texture = nullptr;
+ m_sun_texture = nullptr;
+ if (sun_texture == "sun.png") {
+ // Dumb compatibility fix: sun.png transparently falls back to no texture
+ m_sun_texture = tsrc->isKnownSourceImage(sun_texture) ?
+ tsrc->getTexture(sun_texture) : nullptr;
+ } else if (!sun_texture.empty()) {
+ m_sun_texture = tsrc->getTextureForMesh(sun_texture);
+ }
+
+ if (m_sun_texture) {
+ m_materials[3] = baseMaterial();
+ m_materials[3].setTexture(0, m_sun_texture);
+ m_materials[3].MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
+ disableTextureFiltering(m_materials[3]);
+ m_materials[3].Lighting = !!m_sun_tonemap;
}
}
@@ -797,42 +762,37 @@ void Sky::setMoonTexture(const std::string &moon_texture,
// Ignore matching textures (with modifiers) entirely,
// but lets at least update the tonemap before hand.
m_moon_params.tonemap = moon_tonemap;
- m_moon_tonemap = tsrc->isKnownSourceImage(m_moon_params.tonemap) ?
- tsrc->getTexture(m_moon_params.tonemap) : nullptr;
+ m_moon_tonemap = tsrc->isKnownSourceImage(moon_tonemap) ?
+ tsrc->getTexture(moon_tonemap) : nullptr;
m_materials[4].Lighting = !!m_moon_tonemap;
- if (m_moon_params.texture == moon_texture)
+ if (m_moon_params.texture == moon_texture && !m_first_update)
return;
m_moon_params.texture = moon_texture;
- if (moon_texture != "") {
- // We want to ensure the texture exists first.
- m_moon_texture = tsrc->getTextureForMesh(m_moon_params.texture);
-
- if (m_moon_texture) {
- m_materials[4] = baseMaterial();
- m_materials[4].setTexture(0, m_moon_texture);
- m_materials[4].MaterialType = video::
- EMT_TRANSPARENT_ALPHA_CHANNEL;
- // Disables texture filtering
- m_materials[4].setFlag(
- video::E_MATERIAL_FLAG::EMF_BILINEAR_FILTER, false);
- m_materials[4].setFlag(
- video::E_MATERIAL_FLAG::EMF_TRILINEAR_FILTER, false);
- m_materials[4].setFlag(
- video::E_MATERIAL_FLAG::EMF_ANISOTROPIC_FILTER, false);
- }
- } else {
- m_moon_texture = nullptr;
+ m_moon_texture = nullptr;
+ if (moon_texture == "moon.png") {
+ // Dumb compatibility fix: moon.png transparently falls back to no texture
+ m_moon_texture = tsrc->isKnownSourceImage(moon_texture) ?
+ tsrc->getTexture(moon_texture) : nullptr;
+ } else if (!moon_texture.empty()) {
+ m_moon_texture = tsrc->getTextureForMesh(moon_texture);
+ }
+
+ if (m_moon_texture) {
+ m_materials[4] = baseMaterial();
+ m_materials[4].setTexture(0, m_moon_texture);
+ m_materials[4].MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
+ disableTextureFiltering(m_materials[4]);
+ m_materials[4].Lighting = !!m_moon_tonemap;
}
}
-void Sky::setStarCount(u16 star_count, bool force_update)
+void Sky::setStarCount(u16 star_count)
{
// Allow force updating star count at game init.
- if (m_star_params.count != star_count || force_update) {
+ if (m_star_params.count != star_count || m_first_update) {
m_star_params.count = star_count;
- m_seed = (u64)myrand() << 32 | myrand();
updateStars();
}
}
@@ -920,12 +880,16 @@ void Sky::addTextureToSkybox(const std::string &texture, int material_id,
m_materials[material_id+5].MaterialType = video::EMT_SOLID;
}
-// To be called once at game init to setup default values.
-void Sky::setSkyDefaults()
+float getWickedTimeOfDay(float time_of_day)
{
- SkyboxDefaults sky_defaults;
- m_sky_params.sky_color = sky_defaults.getSkyColorDefaults();
- m_sun_params = sky_defaults.getSunDefaults();
- m_moon_params = sky_defaults.getMoonDefaults();
- m_star_params = sky_defaults.getStarDefaults();
+ float nightlength = 0.415f;
+ float wn = nightlength / 2;
+ float wicked_time_of_day = 0;
+ if (time_of_day > wn && time_of_day < 1.0f - wn)
+ wicked_time_of_day = (time_of_day - wn) / (1.0f - wn * 2) * 0.5f + 0.25f;
+ else if (time_of_day < 0.5f)
+ wicked_time_of_day = time_of_day / wn * 0.25f;
+ else
+ wicked_time_of_day = 1.0f - ((1.0f - time_of_day) / wn * 0.25f);
+ return wicked_time_of_day;
}
diff --git a/src/client/sky.h b/src/client/sky.h
index dc7da5021..3dc057b70 100644
--- a/src/client/sky.h
+++ b/src/client/sky.h
@@ -17,6 +17,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
+#pragma once
+
#include "irrlichttypes_extrabloated.h"
#include <ISceneNode.h>
#include <array>
@@ -25,8 +27,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "shader.h"
#include "skyparams.h"
-#pragma once
-
#define SKY_MATERIAL_COUNT 12
class ITextureSource;
@@ -36,7 +36,7 @@ class Sky : public scene::ISceneNode
{
public:
//! constructor
- Sky(s32 id, ITextureSource *tsrc, IShaderSource *ssrc);
+ Sky(s32 id, RenderingEngine *rendering_engine, ITextureSource *tsrc, IShaderSource *ssrc);
virtual void OnRegisterSceneNode();
@@ -77,7 +77,7 @@ public:
void setMoonScale(f32 moon_scale) { m_moon_params.scale = moon_scale; }
void setStarsVisible(bool stars_visible) { m_star_params.visible = stars_visible; }
- void setStarCount(u16 star_count, bool force_update);
+ void setStarCount(u16 star_count);
void setStarColor(video::SColor star_color) { m_star_params.starcolor = star_color; }
void setStarScale(f32 star_scale) { m_star_params.scale = star_scale; updateStars(); }
@@ -105,6 +105,8 @@ public:
ITextureSource *tsrc);
const video::SColorf &getCurrentStarColor() const { return m_star_color; }
+ float getSkyBodyOrbitTilt() const { return m_sky_body_orbit_tilt; }
+
private:
aabb3f m_box;
video::SMaterial m_materials[SKY_MATERIAL_COUNT];
@@ -148,7 +150,7 @@ private:
bool m_visible = true;
// Used when m_visible=false
video::SColor m_fallback_bg_color = video::SColor(255, 255, 255, 255);
- bool m_first_update = true;
+ bool m_first_update = true; // Set before the sky is updated for the first time
float m_time_of_day;
float m_time_brightness;
bool m_sunlight_seen;
@@ -159,6 +161,7 @@ private:
bool m_directional_colored_fog;
bool m_in_clouds = true; // Prevent duplicating bools to remember old values
bool m_enable_shaders = false;
+ float m_sky_body_orbit_tilt = 0.0f;
video::SColorf m_bgcolor_bright_f = video::SColorf(1.0f, 1.0f, 1.0f, 1.0f);
video::SColorf m_skycolor_bright_f = video::SColorf(1.0f, 1.0f, 1.0f, 1.0f);
@@ -203,5 +206,8 @@ private:
void draw_stars(video::IVideoDriver *driver, float wicked_time_of_day);
void place_sky_body(std::array<video::S3DVertex, 4> &vertices,
float horizon_position, float day_position);
- void setSkyDefaults();
};
+
+// calculates value for sky body positions for the given observed time of day
+// this is used to draw both Sun/Moon and shadows
+float getWickedTimeOfDay(float time_of_day);
diff --git a/src/client/sound_openal.cpp b/src/client/sound_openal.cpp
index f4e61f93e..0eda8842b 100644
--- a/src/client/sound_openal.cpp
+++ b/src/client/sound_openal.cpp
@@ -362,6 +362,14 @@ public:
for (auto &buffer : m_buffers) {
for (SoundBuffer *sb : buffer.second) {
+ alDeleteBuffers(1, &sb->buffer_id);
+
+ ALenum error = alGetError();
+ if (error != AL_NO_ERROR) {
+ warningstream << "Audio: Failed to free stream for "
+ << buffer.first << ": " << alErrorString(error) << std::endl;
+ }
+
delete sb;
}
buffer.second.clear();
@@ -671,8 +679,8 @@ public:
alSourcei(sound->source_id, AL_SOURCE_RELATIVE, false);
alSource3f(sound->source_id, AL_POSITION, pos.X, pos.Y, pos.Z);
- alSource3f(sound->source_id, AL_VELOCITY, 0, 0, 0);
- alSourcef(sound->source_id, AL_REFERENCE_DISTANCE, 30.0);
+ alSource3f(sound->source_id, AL_VELOCITY, 0.0f, 0.0f, 0.0f);
+ alSourcef(sound->source_id, AL_REFERENCE_DISTANCE, 10.0f);
}
bool updateSoundGain(int id, float gain)
diff --git a/src/client/tile.cpp b/src/client/tile.cpp
index f2639757e..da03ff5c8 100644
--- a/src/client/tile.cpp
+++ b/src/client/tile.cpp
@@ -33,15 +33,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "imagefilters.h"
#include "guiscalingfilter.h"
#include "renderingengine.h"
-
-
-#if ENABLE_GLES
-#ifdef _IRR_COMPILE_WITH_OGLES1_
-#include <GLES/gl.h>
-#else
-#include <GLES2/gl2.h>
-#endif
-#endif
+#include "util/base64.h"
/*
A cache from texture name to texture path
@@ -90,12 +82,8 @@ static bool replace_ext(std::string &path, const char *ext)
std::string getImagePath(std::string path)
{
// A NULL-ended list of possible image extensions
- const char *extensions[] = {
- "png", "jpg", "bmp", "tga",
- "pcx", "ppm", "psd", "wal", "rgb",
- NULL
- };
- // If there is no extension, add one
+ const char *extensions[] = { "png", "jpg", "bmp", "tga", NULL };
+ // If there is no extension, assume PNG
if (removeStringEnd(path, extensions).empty())
path = path + ".png";
// Check paths until something is found to exist
@@ -427,6 +415,7 @@ private:
std::unordered_map<std::string, Palette> m_palettes;
// Cached settings needed for making textures from meshes
+ bool m_setting_mipmap;
bool m_setting_trilinear_filter;
bool m_setting_bilinear_filter;
};
@@ -447,6 +436,7 @@ TextureSource::TextureSource()
// Cache some settings
// Note: Since this is only done once, the game must be restarted
// for these settings to take effect
+ m_setting_mipmap = g_settings->getBool("mip_map");
m_setting_trilinear_filter = g_settings->getBool("trilinear_filter");
m_setting_bilinear_filter = g_settings->getBool("bilinear_filter");
}
@@ -667,7 +657,7 @@ video::ITexture* TextureSource::getTexture(const std::string &name, u32 *id)
video::ITexture* TextureSource::getTextureForMesh(const std::string &name, u32 *id)
{
static thread_local bool filter_needed =
- g_settings->getBool("texture_clean_transparent") ||
+ g_settings->getBool("texture_clean_transparent") || m_setting_mipmap ||
((m_setting_trilinear_filter || m_setting_bilinear_filter) &&
g_settings->getS32("texture_min_size") > 1);
// Avoid duplicating texture if it won't actually change
@@ -773,6 +763,9 @@ void TextureSource::rebuildImagesAndTextures()
// Recreate textures
for (TextureInfo &ti : m_textureinfo_cache) {
+ if (ti.name.empty())
+ continue; // Skip dummy entry
+
video::IImage *img = generateImage(ti.name);
#if ENABLE_GLES
img = Align2Npot2(img, driver);
@@ -837,17 +830,16 @@ static video::IImage *createInventoryCubeImage(
image = scaled;
}
sanity_check(image->getPitch() == 4 * size);
- return reinterpret_cast<u32 *>(image->lock());
+ return reinterpret_cast<u32 *>(image->getData());
};
auto free_image = [] (video::IImage *image) -> void {
- image->unlock();
image->drop();
};
video::IImage *result = driver->createImage(video::ECF_A8R8G8B8, {cube_size, cube_size});
sanity_check(result->getPitch() == 4 * cube_size);
result->fill(video::SColor(0x00000000u));
- u32 *target = reinterpret_cast<u32 *>(result->lock());
+ u32 *target = reinterpret_cast<u32 *>(result->getData());
// Draws single cube face
// `shade_factor` is face brightness, in range [0.0, 1.0]
@@ -906,7 +898,6 @@ static video::IImage *createInventoryCubeImage(
{0, 5}, {1, 5},
});
- result->unlock();
return result;
}
@@ -1013,42 +1004,19 @@ video::IImage* TextureSource::generateImage(const std::string &name)
#if ENABLE_GLES
-
-static inline u16 get_GL_major_version()
-{
- const GLubyte *gl_version = glGetString(GL_VERSION);
- return (u16) (gl_version[0] - '0');
-}
-
-/**
- * Check if hardware requires npot2 aligned textures
- * @return true if alignment NOT(!) requires, false otherwise
- */
-
-bool hasNPotSupport()
-{
- // Only GLES2 is trusted to correctly report npot support
- // Note: we cache the boolean result, the GL context will never change.
- static const bool supported = get_GL_major_version() > 1 &&
- glGetString(GL_EXTENSIONS) &&
- strstr((char *)glGetString(GL_EXTENSIONS), "GL_OES_texture_npot");
- return supported;
-}
-
/**
* Check and align image to npot2 if required by hardware
* @param image image to check for npot2 alignment
* @param driver driver to use for image operations
* @return image or copy of image aligned to npot2
*/
-
-video::IImage * Align2Npot2(video::IImage * image,
- video::IVideoDriver* driver)
+video::IImage *Align2Npot2(video::IImage *image,
+ video::IVideoDriver *driver)
{
if (image == NULL)
return image;
- if (hasNPotSupport())
+ if (driver->queryFeature(video::EVDF_TEXTURE_NPOT))
return image;
core::dimension2d<u32> dim = image->getDimension();
@@ -1092,6 +1060,45 @@ static std::string unescape_string(const std::string &str, const char esc = '\\'
return out;
}
+void blitBaseImage(video::IImage* &src, video::IImage* &dst)
+{
+ //infostream<<"Blitting "<<part_of_name<<" on base"<<std::endl;
+ // Size of the copied area
+ core::dimension2d<u32> dim = src->getDimension();
+ //core::dimension2d<u32> dim(16,16);
+ // Position to copy the blitted to in the base image
+ core::position2d<s32> pos_to(0,0);
+ // Position to copy the blitted from in the blitted image
+ core::position2d<s32> pos_from(0,0);
+ // Blit
+ /*image->copyToWithAlpha(baseimg, pos_to,
+ core::rect<s32>(pos_from, dim),
+ video::SColor(255,255,255,255),
+ NULL);*/
+
+ core::dimension2d<u32> dim_dst = dst->getDimension();
+ if (dim == dim_dst) {
+ blit_with_alpha(src, dst, pos_from, pos_to, dim);
+ } else if (dim.Width * dim.Height < dim_dst.Width * dim_dst.Height) {
+ // Upscale overlying image
+ video::IImage *scaled_image = RenderingEngine::get_video_driver()->
+ createImage(video::ECF_A8R8G8B8, dim_dst);
+ src->copyToScaling(scaled_image);
+
+ blit_with_alpha(scaled_image, dst, pos_from, pos_to, dim_dst);
+ scaled_image->drop();
+ } else {
+ // Upscale base image
+ video::IImage *scaled_base = RenderingEngine::get_video_driver()->
+ createImage(video::ECF_A8R8G8B8, dim);
+ dst->copyToScaling(scaled_base);
+ dst->drop();
+ dst = scaled_base;
+
+ blit_with_alpha(src, dst, pos_from, pos_to, dim);
+ }
+}
+
bool TextureSource::generateImagePart(std::string part_of_name,
video::IImage *& baseimg)
{
@@ -1155,41 +1162,7 @@ bool TextureSource::generateImagePart(std::string part_of_name,
// Else blit on base.
else
{
- //infostream<<"Blitting "<<part_of_name<<" on base"<<std::endl;
- // Size of the copied area
- core::dimension2d<u32> dim = image->getDimension();
- //core::dimension2d<u32> dim(16,16);
- // Position to copy the blitted to in the base image
- core::position2d<s32> pos_to(0,0);
- // Position to copy the blitted from in the blitted image
- core::position2d<s32> pos_from(0,0);
- // Blit
- /*image->copyToWithAlpha(baseimg, pos_to,
- core::rect<s32>(pos_from, dim),
- video::SColor(255,255,255,255),
- NULL);*/
-
- core::dimension2d<u32> dim_dst = baseimg->getDimension();
- if (dim == dim_dst) {
- blit_with_alpha(image, baseimg, pos_from, pos_to, dim);
- } else if (dim.Width * dim.Height < dim_dst.Width * dim_dst.Height) {
- // Upscale overlying image
- video::IImage *scaled_image = RenderingEngine::get_video_driver()->
- createImage(video::ECF_A8R8G8B8, dim_dst);
- image->copyToScaling(scaled_image);
-
- blit_with_alpha(scaled_image, baseimg, pos_from, pos_to, dim_dst);
- scaled_image->drop();
- } else {
- // Upscale base image
- video::IImage *scaled_base = RenderingEngine::get_video_driver()->
- createImage(video::ECF_A8R8G8B8, dim);
- baseimg->copyToScaling(scaled_base);
- baseimg->drop();
- baseimg = scaled_base;
-
- blit_with_alpha(image, baseimg, pos_from, pos_to, dim);
- }
+ blitBaseImage(image, baseimg);
}
//cleanup
image->drop();
@@ -1638,8 +1611,8 @@ bool TextureSource::generateImagePart(std::string part_of_name,
return false;
}
- // Apply the "clean transparent" filter, if configured.
- if (g_settings->getBool("texture_clean_transparent"))
+ // Apply the "clean transparent" filter, if needed
+ if (m_setting_mipmap || g_settings->getBool("texture_clean_transparent"))
imageCleanTransparent(baseimg, 127);
/* Upscale textures to user's requested minimum size. This is a trick to make
@@ -1817,6 +1790,43 @@ bool TextureSource::generateImagePart(std::string part_of_name,
baseimg->drop();
baseimg = img;
}
+ /*
+ [png:base64
+ Decodes a PNG image in base64 form.
+ Use minetest.encode_png and minetest.encode_base64
+ to produce a valid string.
+ */
+ else if (str_starts_with(part_of_name, "[png:")) {
+ Strfnd sf(part_of_name);
+ sf.next(":");
+ std::string png;
+ {
+ std::string blob = sf.next("");
+ if (!base64_is_valid(blob)) {
+ errorstream << "generateImagePart(): "
+ << "malformed base64 in '[png'"
+ << std::endl;
+ return false;
+ }
+ png = base64_decode(blob);
+ }
+
+ auto *device = RenderingEngine::get_raw_device();
+ auto *fs = device->getFileSystem();
+ auto *vd = device->getVideoDriver();
+ auto *memfile = fs->createMemoryReadFile(png.data(), png.size(), "__temp_png");
+ video::IImage* pngimg = vd->createImageFromFile(memfile);
+ memfile->drop();
+
+ if (baseimg) {
+ blitBaseImage(pngimg, baseimg);
+ } else {
+ core::dimension2d<u32> dim = pngimg->getDimension();
+ baseimg = driver->createImage(video::ECF_A8R8G8B8, dim);
+ pngimg->copyTo(baseimg);
+ }
+ pngimg->drop();
+ }
else
{
errorstream << "generateImagePart(): Invalid "
@@ -2219,6 +2229,48 @@ video::ITexture* TextureSource::getNormalTexture(const std::string &name)
return NULL;
}
+namespace {
+ // For more colourspace transformations, see for example
+ // https://github.com/tobspr/GLSL-Color-Spaces/blob/master/ColorSpaces.inc.glsl
+
+ inline float linear_to_srgb_component(float v)
+ {
+ if (v > 0.0031308f)
+ return 1.055f * powf(v, 1.0f / 2.4f) - 0.055f;
+ return 12.92f * v;
+ }
+ inline float srgb_to_linear_component(float v)
+ {
+ if (v > 0.04045f)
+ return powf((v + 0.055f) / 1.055f, 2.4f);
+ return v / 12.92f;
+ }
+
+ v3f srgb_to_linear(const video::SColor &col_srgb)
+ {
+ v3f col(col_srgb.getRed(), col_srgb.getGreen(), col_srgb.getBlue());
+ col /= 255.0f;
+ col.X = srgb_to_linear_component(col.X);
+ col.Y = srgb_to_linear_component(col.Y);
+ col.Z = srgb_to_linear_component(col.Z);
+ return col;
+ }
+
+ video::SColor linear_to_srgb(const v3f &col_linear)
+ {
+ v3f col;
+ col.X = linear_to_srgb_component(col_linear.X);
+ col.Y = linear_to_srgb_component(col_linear.Y);
+ col.Z = linear_to_srgb_component(col_linear.Z);
+ col *= 255.0f;
+ col.X = core::clamp<float>(col.X, 0.0f, 255.0f);
+ col.Y = core::clamp<float>(col.Y, 0.0f, 255.0f);
+ col.Z = core::clamp<float>(col.Z, 0.0f, 255.0f);
+ return video::SColor(0xff, myround(col.X), myround(col.Y),
+ myround(col.Z));
+ }
+}
+
video::SColor TextureSource::getTextureAverageColor(const std::string &name)
{
video::IVideoDriver *driver = RenderingEngine::get_video_driver();
@@ -2233,9 +2285,7 @@ video::SColor TextureSource::getTextureAverageColor(const std::string &name)
return c;
u32 total = 0;
- u32 tR = 0;
- u32 tG = 0;
- u32 tB = 0;
+ v3f col_acc(0, 0, 0);
core::dimension2d<u32> dim = image->getDimension();
u16 step = 1;
if (dim.Width > 16)
@@ -2245,17 +2295,14 @@ video::SColor TextureSource::getTextureAverageColor(const std::string &name)
c = image->getPixel(x,y);
if (c.getAlpha() > 0) {
total++;
- tR += c.getRed();
- tG += c.getGreen();
- tB += c.getBlue();
+ col_acc += srgb_to_linear(c);
}
}
}
image->drop();
if (total > 0) {
- c.setRed(tR / total);
- c.setGreen(tG / total);
- c.setBlue(tB / total);
+ col_acc /= total;
+ c = linear_to_srgb(col_acc);
}
c.setAlpha(255);
return c;
diff --git a/src/client/tile.h b/src/client/tile.h
index 49c46f749..fcdc46460 100644
--- a/src/client/tile.h
+++ b/src/client/tile.h
@@ -134,8 +134,7 @@ public:
IWritableTextureSource *createTextureSource();
#if ENABLE_GLES
-bool hasNPotSupport();
-video::IImage * Align2Npot2(video::IImage * image, irr::video::IVideoDriver* driver);
+video::IImage *Align2Npot2(video::IImage *image, video::IVideoDriver *driver);
#endif
enum MaterialType{
diff --git a/src/client/wieldmesh.cpp b/src/client/wieldmesh.cpp
index ad583210a..0a4cb3b86 100644
--- a/src/client/wieldmesh.cpp
+++ b/src/client/wieldmesh.cpp
@@ -33,6 +33,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "util/numeric.h"
#include <map>
#include <IMeshManipulator.h>
+#include "client/renderingengine.h"
#define WIELD_SCALE_FACTOR 30.0
#define WIELD_SCALE_FACTOR_EXTRUDED 40.0
@@ -220,11 +221,18 @@ WieldMeshSceneNode::WieldMeshSceneNode(scene::ISceneManager *mgr, s32 id, bool l
m_meshnode->setReadOnlyMaterials(false);
m_meshnode->setVisible(false);
dummymesh->drop(); // m_meshnode grabbed it
+
+ m_shadow = RenderingEngine::get_shadow_renderer();
}
WieldMeshSceneNode::~WieldMeshSceneNode()
{
sanity_check(g_extrusion_mesh_cache);
+
+ // Remove node from shadow casters. m_shadow might be an invalid pointer!
+ if (auto shadow = RenderingEngine::get_shadow_renderer())
+ shadow->removeNodeFromShadowList(m_meshnode);
+
if (g_extrusion_mesh_cache->drop())
g_extrusion_mesh_cache = nullptr;
}
@@ -294,32 +302,36 @@ void WieldMeshSceneNode::setExtruded(const std::string &imagename,
}
material.setFlag(video::EMF_ANISOTROPIC_FILTER, m_anisotropic_filter);
// mipmaps cause "thin black line" artifacts
-#if (IRRLICHT_VERSION_MAJOR >= 1 && IRRLICHT_VERSION_MINOR >= 8) || IRRLICHT_VERSION_MAJOR >= 2
material.setFlag(video::EMF_USE_MIP_MAPS, false);
-#endif
if (m_enable_shaders) {
material.setTexture(2, tsrc->getShaderFlagsTexture(false));
}
}
}
-scene::SMesh *createSpecialNodeMesh(Client *client, content_t id, std::vector<ItemPartColor> *colors, const ContentFeatures &f)
+static scene::SMesh *createSpecialNodeMesh(Client *client, MapNode n,
+ std::vector<ItemPartColor> *colors, const ContentFeatures &f)
{
MeshMakeData mesh_make_data(client, false);
MeshCollector collector;
mesh_make_data.setSmoothLighting(false);
- MapblockMeshGenerator gen(&mesh_make_data, &collector);
- u8 param2 = 0;
- if (f.param_type_2 == CPT2_WALLMOUNTED ||
+ MapblockMeshGenerator gen(&mesh_make_data, &collector,
+ client->getSceneManager()->getMeshManipulator());
+
+ if (n.getParam2()) {
+ // keep it
+ } else if (f.param_type_2 == CPT2_WALLMOUNTED ||
f.param_type_2 == CPT2_COLORED_WALLMOUNTED) {
- if (f.drawtype == NDT_TORCHLIKE)
- param2 = 1;
- else if (f.drawtype == NDT_SIGNLIKE ||
+ if (f.drawtype == NDT_TORCHLIKE ||
+ f.drawtype == NDT_SIGNLIKE ||
f.drawtype == NDT_NODEBOX ||
- f.drawtype == NDT_MESH)
- param2 = 4;
+ f.drawtype == NDT_MESH) {
+ n.setParam2(4);
+ }
+ } else if (f.drawtype == NDT_SIGNLIKE || f.drawtype == NDT_TORCHLIKE) {
+ n.setParam2(1);
}
- gen.renderSingle(id, param2);
+ gen.renderSingle(n.getContent(), n.getParam2());
colors->clear();
scene::SMesh *mesh = new scene::SMesh();
@@ -392,7 +404,6 @@ void WieldMeshSceneNode::setItem(const ItemStack &item, Client *client, bool che
case NDT_TORCHLIKE:
case NDT_RAILLIKE:
case NDT_PLANTLIKE:
- case NDT_PLANTLIKE_ROOTED:
case NDT_FLOWINGLIQUID: {
v3f wscale = def.wield_scale;
if (f.drawtype == NDT_FLOWINGLIQUID)
@@ -408,14 +419,26 @@ void WieldMeshSceneNode::setItem(const ItemStack &item, Client *client, bool che
m_colors.emplace_back(l1.has_color, l1.color);
break;
}
+ case NDT_PLANTLIKE_ROOTED: {
+ setExtruded(tsrc->getTextureName(f.special_tiles[0].layers[0].texture_id),
+ "", def.wield_scale, tsrc,
+ f.special_tiles[0].layers[0].animation_frame_count);
+ // Add color
+ const TileLayer &l0 = f.special_tiles[0].layers[0];
+ m_colors.emplace_back(l0.has_color, l0.color);
+ break;
+ }
case NDT_NORMAL:
case NDT_ALLFACES:
case NDT_LIQUID:
setCube(f, def.wield_scale);
break;
- default:
+ default: {
// Render non-trivial drawtypes like the actual node
- mesh = createSpecialNodeMesh(client, id, &m_colors, f);
+ MapNode n(id);
+ n.setParam2(def.place_param2);
+
+ mesh = createSpecialNodeMesh(client, n, &m_colors, f);
changeToMesh(mesh);
mesh->drop();
m_meshnode->setScale(
@@ -423,6 +446,7 @@ void WieldMeshSceneNode::setItem(const ItemStack &item, Client *client, bool che
/ (BS * f.visual_scale));
break;
}
+ }
u32 material_count = m_meshnode->getMaterialCount();
for (u32 i = 0; i < material_count; ++i) {
@@ -434,9 +458,14 @@ void WieldMeshSceneNode::setItem(const ItemStack &item, Client *client, bool che
material.setFlag(video::EMF_TRILINEAR_FILTER, m_trilinear_filter);
}
return;
- } else if (!def.inventory_image.empty()) {
- setExtruded(def.inventory_image, def.inventory_overlay, def.wield_scale,
- tsrc, 1);
+ } else {
+ if (!def.inventory_image.empty()) {
+ setExtruded(def.inventory_image, def.inventory_overlay, def.wield_scale,
+ tsrc, 1);
+ } else {
+ setExtruded("no_texture.png", "", def.wield_scale, tsrc, 1);
+ }
+
m_colors.emplace_back();
// overlay is white, if present
m_colors.emplace_back(true, video::SColor(0xFFFFFFFF));
@@ -511,6 +540,10 @@ void WieldMeshSceneNode::changeToMesh(scene::IMesh *mesh)
// need to normalize normals when lighting is enabled (because of setScale())
m_meshnode->setMaterialFlag(video::EMF_NORMALIZE_NORMALS, m_lighting);
m_meshnode->setVisible(true);
+
+ // Add mesh to shadow caster
+ if (m_shadow)
+ m_shadow->addNodeToShadowList(m_meshnode);
}
void getItemMesh(Client *client, const ItemStack &item, ItemMesh *result)
@@ -523,7 +556,7 @@ void getItemMesh(Client *client, const ItemStack &item, ItemMesh *result)
content_t id = ndef->getId(def.name);
FATAL_ERROR_IF(!g_extrusion_mesh_cache, "Extrusion mesh cache is not yet initialized");
-
+
scene::SMesh *mesh = nullptr;
// Shading is on by default
@@ -585,12 +618,16 @@ void getItemMesh(Client *client, const ItemStack &item, ItemMesh *result)
result->buffer_colors.emplace_back(l0.has_color, l0.color);
break;
}
- default:
+ default: {
// Render non-trivial drawtypes like the actual node
- mesh = createSpecialNodeMesh(client, id, &result->buffer_colors, f);
+ MapNode n(id);
+ n.setParam2(def.place_param2);
+
+ mesh = createSpecialNodeMesh(client, n, &result->buffer_colors, f);
scaleMesh(mesh, v3f(0.12, 0.12, 0.12));
break;
}
+ }
u32 mc = mesh->getMeshBufferCount();
for (u32 i = 0; i < mc; ++i) {
diff --git a/src/client/wieldmesh.h b/src/client/wieldmesh.h
index 933097230..d1eeb64f5 100644
--- a/src/client/wieldmesh.h
+++ b/src/client/wieldmesh.h
@@ -27,6 +27,7 @@ struct ItemStack;
class Client;
class ITextureSource;
struct ContentFeatures;
+class ShadowRenderer;
/*!
* Holds color information of an item mesh's buffer.
@@ -124,6 +125,8 @@ private:
// so this variable is just required so we can implement
// getBoundingBox() and is set to an empty box.
aabb3f m_bounding_box;
+
+ ShadowRenderer *m_shadow;
};
void getItemMesh(Client *client, const ItemStack &item, ItemMesh *result);
diff --git a/src/clientiface.cpp b/src/clientiface.cpp
index f35dcd0eb..a1c3e1187 100644
--- a/src/clientiface.cpp
+++ b/src/clientiface.cpp
@@ -714,31 +714,6 @@ void ClientInterface::sendToAll(NetworkPacket *pkt)
}
}
-void ClientInterface::sendToAllCompat(NetworkPacket *pkt, NetworkPacket *legacypkt,
- u16 min_proto_ver)
-{
- RecursiveMutexAutoLock clientslock(m_clients_mutex);
- for (auto &client_it : m_clients) {
- RemoteClient *client = client_it.second;
- NetworkPacket *pkt_to_send = nullptr;
-
- if (client->net_proto_version >= min_proto_ver) {
- pkt_to_send = pkt;
- } else if (client->net_proto_version != 0) {
- pkt_to_send = legacypkt;
- } else {
- warningstream << "Client with unhandled version to handle: '"
- << client->net_proto_version << "'";
- continue;
- }
-
- m_con->Send(client->peer_id,
- clientCommandFactoryTable[pkt_to_send->getCommand()].channel,
- pkt_to_send,
- clientCommandFactoryTable[pkt_to_send->getCommand()].reliable);
- }
-}
-
RemoteClient* ClientInterface::getClientNoEx(session_t peer_id, ClientState state_min)
{
RecursiveMutexAutoLock clientslock(m_clients_mutex);
diff --git a/src/clientiface.h b/src/clientiface.h
index cc5292b71..b1591ddb0 100644
--- a/src/clientiface.h
+++ b/src/clientiface.h
@@ -31,6 +31,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <list>
#include <vector>
#include <set>
+#include <memory>
#include <mutex>
class MapBlock;
@@ -464,7 +465,6 @@ public:
/* send to all clients */
void sendToAll(NetworkPacket *pkt);
- void sendToAllCompat(NetworkPacket *pkt, NetworkPacket *legacypkt, u16 min_proto_ver);
/* delete a client */
void DeleteClient(session_t peer_id);
diff --git a/src/cmake_config.h.in b/src/cmake_config.h.in
index cfcee4b58..cf436d6dc 100644
--- a/src/cmake_config.h.in
+++ b/src/cmake_config.h.in
@@ -18,7 +18,6 @@
#cmakedefine01 USE_GETTEXT
#cmakedefine01 USE_CURL
#cmakedefine01 USE_SOUND
-#cmakedefine01 USE_FREETYPE
#cmakedefine01 USE_CURSES
#cmakedefine01 USE_LEVELDB
#cmakedefine01 USE_LUAJIT
diff --git a/src/constants.h b/src/constants.h
index 3cc3af094..b9d4f8d70 100644
--- a/src/constants.h
+++ b/src/constants.h
@@ -64,7 +64,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
// I really don't want to make every algorithm to check if it's going near
// the limit or not, so this is lower.
// This is the maximum value the setting map_generation_limit can be
-#define MAX_MAP_GENERATION_LIMIT (31000)
+#define MAX_MAP_GENERATION_LIMIT (31007)
// Size of node in floating-point units
// The original idea behind this is to disallow plain casts between
@@ -111,4 +111,3 @@ with this program; if not, write to the Free Software Foundation, Inc.,
*/
#define TTF_DEFAULT_FONT_SIZE (16)
-#define DEFAULT_FONT_SIZE (10)
diff --git a/src/content/mods.cpp b/src/content/mods.cpp
index 95ab0290a..455506967 100644
--- a/src/content/mods.cpp
+++ b/src/content/mods.cpp
@@ -22,12 +22,37 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <json/json.h>
#include <algorithm>
#include "content/mods.h"
+#include "database/database.h"
#include "filesys.h"
#include "log.h"
#include "content/subgames.h"
#include "settings.h"
#include "porting.h"
#include "convert_json.h"
+#include "script/common/c_internal.h"
+
+void ModSpec::checkAndLog() const
+{
+ if (!string_allowed(name, MODNAME_ALLOWED_CHARS)) {
+ throw ModError("Error loading mod \"" + name +
+ "\": Mod name does not follow naming conventions: "
+ "Only characters [a-z0-9_] are allowed.");
+ }
+
+ // Log deprecation messages
+ auto handling_mode = get_deprecated_handling_mode();
+ if (!deprecation_msgs.empty() && handling_mode != DeprecatedHandlingMode::Ignore) {
+ std::ostringstream os;
+ os << "Mod " << name << " at " << path << ":" << std::endl;
+ for (auto msg : deprecation_msgs)
+ os << "\t" << msg << std::endl;
+
+ if (handling_mode == DeprecatedHandlingMode::Error)
+ throw ModError(os.str());
+ else
+ warningstream << os.str();
+ }
+}
bool parseDependsString(std::string &dep, std::unordered_set<char> &symbols)
{
@@ -47,17 +72,6 @@ bool parseDependsString(std::string &dep, std::unordered_set<char> &symbols)
void parseModContents(ModSpec &spec)
{
// NOTE: this function works in mutual recursion with getModsInPath
- Settings info;
- info.readConfigFile((spec.path + DIR_DELIM + "mod.conf").c_str());
-
- if (info.exists("name"))
- spec.name = info.get("name");
-
- if (info.exists("author"))
- spec.author = info.get("author");
-
- if (info.exists("release"))
- spec.release = info.getS32("release");
spec.depends.clear();
spec.optdepends.clear();
@@ -78,6 +92,20 @@ void parseModContents(ModSpec &spec)
spec.modpack_content = getModsInPath(spec.path, true);
} else {
+ Settings info;
+ info.readConfigFile((spec.path + DIR_DELIM + "mod.conf").c_str());
+
+ if (info.exists("name"))
+ spec.name = info.get("name");
+ else
+ spec.deprecation_msgs.push_back("Mods not having a mod.conf file with the name is deprecated.");
+
+ if (info.exists("author"))
+ spec.author = info.get("author");
+
+ if (info.exists("release"))
+ spec.release = info.getS32("release");
+
// Attempt to load dependencies from mod.conf
bool mod_conf_has_depends = false;
if (info.exists("depends")) {
@@ -109,6 +137,10 @@ void parseModContents(ModSpec &spec)
std::vector<std::string> dependencies;
std::ifstream is((spec.path + DIR_DELIM + "depends.txt").c_str());
+
+ if (is.good())
+ spec.deprecation_msgs.push_back("depends.txt is deprecated, please use mod.conf instead.");
+
while (is.good()) {
std::string dep;
std::getline(is, dep);
@@ -127,14 +159,10 @@ void parseModContents(ModSpec &spec)
}
}
- if (info.exists("description")) {
+ if (info.exists("description"))
spec.desc = info.get("description");
- } else {
- std::ifstream is((spec.path + DIR_DELIM + "description.txt")
- .c_str());
- spec.desc = std::string((std::istreambuf_iterator<char>(is)),
- std::istreambuf_iterator<char>());
- }
+ else if (fs::ReadFile(spec.path + DIR_DELIM + "description.txt", spec.desc))
+ spec.deprecation_msgs.push_back("description.txt is deprecated, please use mod.conf instead.");
}
}
@@ -395,83 +423,29 @@ ClientModConfiguration::ClientModConfiguration(const std::string &path) :
}
#endif
-ModMetadata::ModMetadata(const std::string &mod_name) : m_mod_name(mod_name)
+ModMetadata::ModMetadata(const std::string &mod_name, ModMetadataDatabase *database):
+ m_mod_name(mod_name), m_database(database)
{
+ m_database->getModEntries(m_mod_name, &m_stringvars);
}
void ModMetadata::clear()
{
+ for (const auto &pair : m_stringvars) {
+ m_database->removeModEntry(m_mod_name, pair.first);
+ }
Metadata::clear();
- m_modified = true;
}
-bool ModMetadata::save(const std::string &root_path)
+bool ModMetadata::setString(const std::string &name, const std::string &var)
{
- Json::Value json;
- for (StringMap::const_iterator it = m_stringvars.begin();
- it != m_stringvars.end(); ++it) {
- json[it->first] = it->second;
- }
-
- if (!fs::PathExists(root_path)) {
- if (!fs::CreateAllDirs(root_path)) {
- errorstream << "ModMetadata[" << m_mod_name
- << "]: Unable to save. '" << root_path
- << "' tree cannot be created." << std::endl;
- return false;
+ if (Metadata::setString(name, var)) {
+ if (var.empty()) {
+ m_database->removeModEntry(m_mod_name, name);
+ } else {
+ m_database->setModEntry(m_mod_name, name, var);
}
- } else if (!fs::IsDir(root_path)) {
- errorstream << "ModMetadata[" << m_mod_name << "]: Unable to save. '"
- << root_path << "' is not a directory." << std::endl;
- return false;
+ return true;
}
-
- bool w_ok = fs::safeWriteToFile(
- root_path + DIR_DELIM + m_mod_name, fastWriteJson(json));
-
- if (w_ok) {
- m_modified = false;
- } else {
- errorstream << "ModMetadata[" << m_mod_name << "]: failed write file."
- << std::endl;
- }
- return w_ok;
-}
-
-bool ModMetadata::load(const std::string &root_path)
-{
- m_stringvars.clear();
-
- std::ifstream is((root_path + DIR_DELIM + m_mod_name).c_str(),
- std::ios_base::binary);
- if (!is.good()) {
- return false;
- }
-
- Json::Value root;
- Json::CharReaderBuilder builder;
- builder.settings_["collectComments"] = false;
- std::string errs;
-
- if (!Json::parseFromStream(builder, is, &root, &errs)) {
- errorstream << "ModMetadata[" << m_mod_name
- << "]: failed read data "
- "(Json decoding failure). Message: "
- << errs << std::endl;
- return false;
- }
-
- const Json::Value::Members attr_list = root.getMemberNames();
- for (const auto &it : attr_list) {
- Json::Value attr_value = root[it];
- m_stringvars[it] = attr_value.asString();
- }
-
- return true;
-}
-
-bool ModMetadata::setString(const std::string &name, const std::string &var)
-{
- m_modified = Metadata::setString(name, var);
- return m_modified;
+ return false;
}
diff --git a/src/content/mods.h b/src/content/mods.h
index b3500fbc8..dd3b6e0e6 100644
--- a/src/content/mods.h
+++ b/src/content/mods.h
@@ -31,6 +31,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "config.h"
#include "metadata.h"
+class ModMetadataDatabase;
+
#define MODNAME_ALLOWED_CHARS "abcdefghijklmnopqrstuvwxyz0123456789_"
struct ModSpec
@@ -49,6 +51,9 @@ struct ModSpec
bool part_of_modpack = false;
bool is_modpack = false;
+ // For logging purposes
+ std::vector<const char *> deprecation_msgs;
+
// if modpack:
std::map<std::string, ModSpec> modpack_content;
ModSpec(const std::string &name = "", const std::string &path = "") :
@@ -59,6 +64,8 @@ struct ModSpec
name(name), path(path), part_of_modpack(part_of_modpack)
{
}
+
+ void checkAndLog() const;
};
// Retrieves depends, optdepends, is_modpack and modpack_content
@@ -144,20 +151,16 @@ class ModMetadata : public Metadata
{
public:
ModMetadata() = delete;
- ModMetadata(const std::string &mod_name);
+ ModMetadata(const std::string &mod_name, ModMetadataDatabase *database);
~ModMetadata() = default;
virtual void clear();
- bool save(const std::string &root_path);
- bool load(const std::string &root_path);
-
- bool isModified() const { return m_modified; }
const std::string &getModName() const { return m_mod_name; }
virtual bool setString(const std::string &name, const std::string &var);
private:
std::string m_mod_name;
- bool m_modified = false;
+ ModMetadataDatabase *m_database;
};
diff --git a/src/content/subgames.cpp b/src/content/subgames.cpp
index e9dc609b0..62e82e0e4 100644
--- a/src/content/subgames.cpp
+++ b/src/content/subgames.cpp
@@ -24,7 +24,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "log.h"
#include "util/strfnd.h"
#include "defaultsettings.h" // for set_default_settings
-#include "mapgen/mapgen.h" // for MapgenParams
+#include "map_settings_manager.h"
#include "util/string.h"
#ifndef SERVER
@@ -113,6 +113,10 @@ SubgameSpec findSubgame(const std::string &id)
if (user != share || user_game)
mods_paths.insert(user + DIR_DELIM + "mods");
+ for (const std::string &mod_path : getEnvModPaths()) {
+ mods_paths.insert(mod_path);
+ }
+
// Get meta
std::string conf_path = game_path + DIR_DELIM + "game.conf";
Settings conf;
@@ -354,6 +358,7 @@ void loadGameConfAndInitWorld(const std::string &path, const std::string &name,
conf.set("backend", "sqlite3");
conf.set("player_backend", "sqlite3");
conf.set("auth_backend", "sqlite3");
+ conf.set("mod_storage_backend", "sqlite3");
conf.setBool("creative_mode", g_settings->getBool("creative_mode"));
conf.setBool("enable_damage", g_settings->getBool("enable_damage"));
@@ -365,22 +370,25 @@ void loadGameConfAndInitWorld(const std::string &path, const std::string &name,
// Create map_meta.txt if does not already exist
std::string map_meta_path = final_path + DIR_DELIM + "map_meta.txt";
if (!fs::PathExists(map_meta_path)) {
- verbosestream << "Creating map_meta.txt (" << map_meta_path << ")"
- << std::endl;
- std::ostringstream oss(std::ios_base::binary);
-
- Settings conf;
- MapgenParams params;
+ MapSettingsManager mgr(map_meta_path);
- params.readParams(g_settings);
- params.writeParams(&conf);
- conf.writeLines(oss);
- oss << "[end_of_params]\n";
+ mgr.setMapSetting("seed", g_settings->get("fixed_map_seed"));
- fs::safeWriteToFile(map_meta_path, oss.str());
+ mgr.makeMapgenParams();
+ mgr.saveMapMeta();
}
// The Settings object is no longer needed for created worlds
if (new_game_settings)
delete game_settings;
}
+
+std::vector<std::string> getEnvModPaths()
+{
+ const char *c_mod_path = getenv("MINETEST_MOD_PATH");
+ std::vector<std::string> paths;
+ Strfnd search_paths(c_mod_path ? c_mod_path : "");
+ while (!search_paths.at_end())
+ paths.push_back(search_paths.next(PATH_DELIM));
+ return paths;
+}
diff --git a/src/content/subgames.h b/src/content/subgames.h
index 60392639b..4a50803e8 100644
--- a/src/content/subgames.h
+++ b/src/content/subgames.h
@@ -58,6 +58,8 @@ SubgameSpec findWorldSubgame(const std::string &world_path);
std::set<std::string> getAvailableGameIds();
std::vector<SubgameSpec> getAvailableGames();
+// Get the list of paths to mods in the environment variable $MINETEST_MOD_PATH
+std::vector<std::string> getEnvModPaths();
bool getWorldExists(const std::string &world_path);
//! Try to get the displayed name of a world
diff --git a/src/convert_json.cpp b/src/convert_json.cpp
index c774aa002..686113fa8 100644
--- a/src/convert_json.cpp
+++ b/src/convert_json.cpp
@@ -17,63 +17,23 @@ with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
-#include <vector>
#include <iostream>
#include <sstream>
+#include <memory>
#include "convert_json.h"
-#include "content/mods.h"
-#include "config.h"
-#include "log.h"
-#include "settings.h"
-#include "httpfetch.h"
-#include "porting.h"
-Json::Value fetchJsonValue(const std::string &url,
- std::vector<std::string> *extra_headers)
+void fastWriteJson(const Json::Value &value, std::ostream &to)
{
- HTTPFetchRequest fetch_request;
- HTTPFetchResult fetch_result;
- fetch_request.url = url;
- fetch_request.caller = HTTPFETCH_SYNC;
-
- if (extra_headers != NULL)
- fetch_request.extra_headers = *extra_headers;
-
- httpfetch_sync(fetch_request, fetch_result);
-
- if (!fetch_result.succeeded) {
- return Json::Value();
- }
- Json::Value root;
- std::istringstream stream(fetch_result.data);
-
- Json::CharReaderBuilder builder;
- builder.settings_["collectComments"] = false;
- std::string errs;
-
- if (!Json::parseFromStream(builder, stream, &root, &errs)) {
- errorstream << "URL: " << url << std::endl;
- errorstream << "Failed to parse json data " << errs << std::endl;
- if (fetch_result.data.size() > 100) {
- errorstream << "Data (" << fetch_result.data.size()
- << " bytes) printed to warningstream." << std::endl;
- warningstream << "data: \"" << fetch_result.data << "\"" << std::endl;
- } else {
- errorstream << "data: \"" << fetch_result.data << "\"" << std::endl;
- }
- return Json::Value();
- }
-
- return root;
+ Json::StreamWriterBuilder builder;
+ builder["indentation"] = "";
+ std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
+ writer->write(value, &to);
}
std::string fastWriteJson(const Json::Value &value)
{
std::ostringstream oss;
- Json::StreamWriterBuilder builder;
- builder["indentation"] = "";
- std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
- writer->write(value, &oss);
+ fastWriteJson(value, oss);
return oss.str();
}
diff --git a/src/convert_json.h b/src/convert_json.h
index d8825acdc..d1d487e77 100644
--- a/src/convert_json.h
+++ b/src/convert_json.h
@@ -20,8 +20,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#pragma once
#include <json/json.h>
+#include <ostream>
-Json::Value fetchJsonValue(const std::string &url,
- std::vector<std::string> *extra_headers);
+void fastWriteJson(const Json::Value &value, std::ostream &to);
std::string fastWriteJson(const Json::Value &value);
diff --git a/src/database/database-dummy.cpp b/src/database/database-dummy.cpp
index b56f341c5..629b2fb04 100644
--- a/src/database/database-dummy.cpp
+++ b/src/database/database-dummy.cpp
@@ -80,3 +80,41 @@ void Database_Dummy::listPlayers(std::vector<std::string> &res)
res.emplace_back(player);
}
}
+
+bool Database_Dummy::getModEntries(const std::string &modname, StringMap *storage)
+{
+ const auto mod_pair = m_mod_meta_database.find(modname);
+ if (mod_pair != m_mod_meta_database.cend()) {
+ for (const auto &pair : mod_pair->second) {
+ (*storage)[pair.first] = pair.second;
+ }
+ }
+ return true;
+}
+
+bool Database_Dummy::setModEntry(const std::string &modname,
+ const std::string &key, const std::string &value)
+{
+ auto mod_pair = m_mod_meta_database.find(modname);
+ if (mod_pair == m_mod_meta_database.end()) {
+ m_mod_meta_database[modname] = StringMap({{key, value}});
+ } else {
+ mod_pair->second[key] = value;
+ }
+ return true;
+}
+
+bool Database_Dummy::removeModEntry(const std::string &modname, const std::string &key)
+{
+ auto mod_pair = m_mod_meta_database.find(modname);
+ if (mod_pair != m_mod_meta_database.end())
+ return mod_pair->second.erase(key) > 0;
+ return false;
+}
+
+void Database_Dummy::listMods(std::vector<std::string> *res)
+{
+ for (const auto &pair : m_mod_meta_database) {
+ res->push_back(pair.first);
+ }
+}
diff --git a/src/database/database-dummy.h b/src/database/database-dummy.h
index b69919f84..44b9e8d68 100644
--- a/src/database/database-dummy.h
+++ b/src/database/database-dummy.h
@@ -24,7 +24,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "database.h"
#include "irrlichttypes.h"
-class Database_Dummy : public MapDatabase, public PlayerDatabase
+class Database_Dummy : public MapDatabase, public PlayerDatabase, public ModMetadataDatabase
{
public:
bool saveBlock(const v3s16 &pos, const std::string &data);
@@ -37,10 +37,17 @@ public:
bool removePlayer(const std::string &name);
void listPlayers(std::vector<std::string> &res);
+ bool getModEntries(const std::string &modname, StringMap *storage);
+ bool setModEntry(const std::string &modname,
+ const std::string &key, const std::string &value);
+ bool removeModEntry(const std::string &modname, const std::string &key);
+ void listMods(std::vector<std::string> *res);
+
void beginSave() {}
void endSave() {}
private:
std::map<s64, std::string> m_database;
std::set<std::string> m_player_database;
+ std::unordered_map<std::string, StringMap> m_mod_meta_database;
};
diff --git a/src/database/database-files.cpp b/src/database/database-files.cpp
index d9d113b4e..7c0dbac28 100644
--- a/src/database/database-files.cpp
+++ b/src/database/database-files.cpp
@@ -18,7 +18,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
*/
#include <cassert>
-#include <json/json.h>
#include "convert_json.h"
#include "database-files.h"
#include "remoteplayer.h"
@@ -376,3 +375,127 @@ bool AuthDatabaseFiles::writeAuthFile()
}
return true;
}
+
+ModMetadataDatabaseFiles::ModMetadataDatabaseFiles(const std::string &savedir):
+ m_storage_dir(savedir + DIR_DELIM + "mod_storage")
+{
+}
+
+bool ModMetadataDatabaseFiles::getModEntries(const std::string &modname, StringMap *storage)
+{
+ Json::Value *meta = getOrCreateJson(modname);
+ if (!meta)
+ return false;
+
+ const Json::Value::Members attr_list = meta->getMemberNames();
+ for (const auto &it : attr_list) {
+ Json::Value attr_value = (*meta)[it];
+ (*storage)[it] = attr_value.asString();
+ }
+
+ return true;
+}
+
+bool ModMetadataDatabaseFiles::setModEntry(const std::string &modname,
+ const std::string &key, const std::string &value)
+{
+ Json::Value *meta = getOrCreateJson(modname);
+ if (!meta)
+ return false;
+
+ (*meta)[key] = Json::Value(value);
+ m_modified.insert(modname);
+
+ return true;
+}
+
+bool ModMetadataDatabaseFiles::removeModEntry(const std::string &modname,
+ const std::string &key)
+{
+ Json::Value *meta = getOrCreateJson(modname);
+ if (!meta)
+ return false;
+
+ Json::Value removed;
+ if (meta->removeMember(key, &removed)) {
+ m_modified.insert(modname);
+ return true;
+ }
+ return false;
+}
+
+void ModMetadataDatabaseFiles::beginSave()
+{
+}
+
+void ModMetadataDatabaseFiles::endSave()
+{
+ if (m_modified.empty())
+ return;
+
+ if (!fs::CreateAllDirs(m_storage_dir)) {
+ errorstream << "ModMetadataDatabaseFiles: Unable to save. '"
+ << m_storage_dir << "' cannot be created." << std::endl;
+ return;
+ }
+ if (!fs::IsDir(m_storage_dir)) {
+ errorstream << "ModMetadataDatabaseFiles: Unable to save. '"
+ << m_storage_dir << "' is not a directory." << std::endl;
+ return;
+ }
+
+ for (auto it = m_modified.begin(); it != m_modified.end();) {
+ const std::string &modname = *it;
+
+ const Json::Value &json = m_mod_meta[modname];
+
+ if (!fs::safeWriteToFile(m_storage_dir + DIR_DELIM + modname, fastWriteJson(json))) {
+ errorstream << "ModMetadataDatabaseFiles[" << modname
+ << "]: failed to write file." << std::endl;
+ ++it;
+ continue;
+ }
+
+ it = m_modified.erase(it);
+ }
+}
+
+void ModMetadataDatabaseFiles::listMods(std::vector<std::string> *res)
+{
+ // List in-memory metadata first.
+ for (const auto &pair : m_mod_meta) {
+ res->push_back(pair.first);
+ }
+
+ // List other metadata present in the filesystem.
+ for (const auto &entry : fs::GetDirListing(m_storage_dir)) {
+ if (!entry.dir && m_mod_meta.count(entry.name) == 0)
+ res->push_back(entry.name);
+ }
+}
+
+Json::Value *ModMetadataDatabaseFiles::getOrCreateJson(const std::string &modname)
+{
+ auto found = m_mod_meta.find(modname);
+ if (found != m_mod_meta.end())
+ return &found->second;
+
+ Json::Value meta(Json::objectValue);
+
+ std::string path = m_storage_dir + DIR_DELIM + modname;
+ if (fs::PathExists(path)) {
+ std::ifstream is(path.c_str(), std::ios_base::binary);
+
+ Json::CharReaderBuilder builder;
+ builder.settings_["collectComments"] = false;
+ std::string errs;
+
+ if (!Json::parseFromStream(builder, is, &meta, &errs)) {
+ errorstream << "ModMetadataDatabaseFiles[" << modname
+ << "]: failed to decode data: " << errs << std::endl;
+ return nullptr;
+ }
+ }
+
+ return &(m_mod_meta[modname] = meta);
+}
diff --git a/src/database/database-files.h b/src/database/database-files.h
index e647a2e24..962e4d7bb 100644
--- a/src/database/database-files.h
+++ b/src/database/database-files.h
@@ -25,6 +25,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "database.h"
#include <unordered_map>
+#include <unordered_set>
+#include <json/json.h>
class PlayerDatabaseFiles : public PlayerDatabase
{
@@ -69,3 +71,27 @@ private:
bool readAuthFile();
bool writeAuthFile();
};
+
+class ModMetadataDatabaseFiles : public ModMetadataDatabase
+{
+public:
+ ModMetadataDatabaseFiles(const std::string &savedir);
+ virtual ~ModMetadataDatabaseFiles() = default;
+
+ virtual bool getModEntries(const std::string &modname, StringMap *storage);
+ virtual bool setModEntry(const std::string &modname,
+ const std::string &key, const std::string &value);
+ virtual bool removeModEntry(const std::string &modname, const std::string &key);
+ virtual void listMods(std::vector<std::string> *res);
+
+ virtual void beginSave();
+ virtual void endSave();
+
+private:
+ Json::Value *getOrCreateJson(const std::string &modname);
+ bool writeJson(const std::string &modname, const Json::Value &json);
+
+ std::string m_storage_dir;
+ std::unordered_map<std::string, Json::Value> m_mod_meta;
+ std::unordered_set<std::string> m_modified;
+};
diff --git a/src/database/database-leveldb.cpp b/src/database/database-leveldb.cpp
index 73cd63f6d..39f4c8442 100644
--- a/src/database/database-leveldb.cpp
+++ b/src/database/database-leveldb.cpp
@@ -70,11 +70,11 @@ bool Database_LevelDB::saveBlock(const v3s16 &pos, const std::string &data)
void Database_LevelDB::loadBlock(const v3s16 &pos, std::string *block)
{
- std::string datastr;
leveldb::Status status = m_database->Get(leveldb::ReadOptions(),
- i64tos(getBlockAsInteger(pos)), &datastr);
+ i64tos(getBlockAsInteger(pos)), block);
- *block = (status.ok()) ? datastr : "";
+ if (!status.ok())
+ block->clear();
}
bool Database_LevelDB::deleteBlock(const v3s16 &pos)
@@ -131,7 +131,7 @@ void PlayerDatabaseLevelDB::savePlayer(RemotePlayer *player)
std::string (long) serialized_inventory
*/
- std::ostringstream os;
+ std::ostringstream os(std::ios_base::binary);
writeU8(os, 1);
PlayerSAO *sao = player->getPlayerSAO();
@@ -142,7 +142,7 @@ void PlayerDatabaseLevelDB::savePlayer(RemotePlayer *player)
writeF32(os, sao->getRotation().Y);
writeU16(os, sao->getBreath());
- StringMap stringvars = sao->getMeta().getStrings();
+ const auto &stringvars = sao->getMeta().getStrings();
writeU32(os, stringvars.size());
for (const auto &it : stringvars) {
os << serializeString16(it.first);
@@ -170,7 +170,7 @@ bool PlayerDatabaseLevelDB::loadPlayer(RemotePlayer *player, PlayerSAO *sao)
player->getName(), &raw);
if (!s.ok())
return false;
- std::istringstream is(raw);
+ std::istringstream is(raw, std::ios_base::binary);
if (readU8(is) > 1)
return false;
@@ -230,7 +230,7 @@ bool AuthDatabaseLevelDB::getAuth(const std::string &name, AuthEntry &res)
leveldb::Status s = m_database->Get(leveldb::ReadOptions(), name, &raw);
if (!s.ok())
return false;
- std::istringstream is(raw);
+ std::istringstream is(raw, std::ios_base::binary);
/*
u8 version = 1
@@ -262,7 +262,7 @@ bool AuthDatabaseLevelDB::getAuth(const std::string &name, AuthEntry &res)
bool AuthDatabaseLevelDB::saveAuth(const AuthEntry &authEntry)
{
- std::ostringstream os;
+ std::ostringstream os(std::ios_base::binary);
writeU8(os, 1);
os << serializeString16(authEntry.password);
diff --git a/src/database/database-postgresql.cpp b/src/database/database-postgresql.cpp
index e1bb39928..3469f4242 100644
--- a/src/database/database-postgresql.cpp
+++ b/src/database/database-postgresql.cpp
@@ -39,20 +39,24 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "remoteplayer.h"
#include "server/player_sao.h"
-Database_PostgreSQL::Database_PostgreSQL(const std::string &connect_string) :
+Database_PostgreSQL::Database_PostgreSQL(const std::string &connect_string,
+ const char *type) :
m_connect_string(connect_string)
{
if (m_connect_string.empty()) {
- throw SettingNotFoundException(
- "Set pgsql_connection string in world.mt to "
+ // Use given type to reference the exact setting in the error message
+ std::string s = type;
+ std::string msg =
+ "Set pgsql" + s + "_connection string in world.mt to "
"use the postgresql backend\n"
"Notes:\n"
- "pgsql_connection has the following form: \n"
- "\tpgsql_connection = host=127.0.0.1 port=5432 user=mt_user "
- "password=mt_password dbname=minetest_world\n"
+ "pgsql" + s + "_connection has the following form: \n"
+ "\tpgsql" + s + "_connection = host=127.0.0.1 port=5432 "
+ "user=mt_user password=mt_password dbname=minetest" + s + "\n"
"mt_user should have CREATE TABLE, INSERT, SELECT, UPDATE and "
- "DELETE rights on the database.\n"
- "Don't create mt_user as a SUPERUSER!");
+ "DELETE rights on the database. "
+ "Don't create mt_user as a SUPERUSER!";
+ throw SettingNotFoundException(msg);
}
}
@@ -166,7 +170,7 @@ void Database_PostgreSQL::rollback()
}
MapDatabasePostgreSQL::MapDatabasePostgreSQL(const std::string &connect_string):
- Database_PostgreSQL(connect_string),
+ Database_PostgreSQL(connect_string, ""),
MapDatabase()
{
connectToDatabase();
@@ -270,10 +274,10 @@ void MapDatabasePostgreSQL::loadBlock(const v3s16 &pos, std::string *block)
PGresult *results = execPrepared("read_block", ARRLEN(args), args,
argLen, argFmt, false);
- *block = "";
-
if (PQntuples(results))
- *block = std::string(PQgetvalue(results, 0, 0), PQgetlength(results, 0, 0));
+ block->assign(PQgetvalue(results, 0, 0), PQgetlength(results, 0, 0));
+ else
+ block->clear();
PQclear(results);
}
@@ -315,7 +319,7 @@ void MapDatabasePostgreSQL::listAllLoadableBlocks(std::vector<v3s16> &dst)
* Player Database
*/
PlayerDatabasePostgreSQL::PlayerDatabasePostgreSQL(const std::string &connect_string):
- Database_PostgreSQL(connect_string),
+ Database_PostgreSQL(connect_string, "_player"),
PlayerDatabase()
{
connectToDatabase();
@@ -492,6 +496,7 @@ void PlayerDatabasePostgreSQL::savePlayer(RemotePlayer *player)
execPrepared("remove_player_inventory_items", 1, rmvalues);
std::vector<const InventoryList*> inventory_lists = sao->getInventory()->getLists();
+ std::ostringstream oss;
for (u16 i = 0; i < inventory_lists.size(); i++) {
const InventoryList* list = inventory_lists[i];
const std::string &name = list->getName();
@@ -508,9 +513,10 @@ void PlayerDatabasePostgreSQL::savePlayer(RemotePlayer *player)
execPrepared("add_player_inventory", 5, inv_values);
for (u32 j = 0; j < list->getSize(); j++) {
- std::ostringstream os;
- list->getItem(j).serialize(os);
- std::string itemStr = os.str(), slotId = itos(j);
+ oss.str("");
+ oss.clear();
+ list->getItem(j).serialize(oss);
+ std::string itemStr = oss.str(), slotId = itos(j);
const char* invitem_values[] = {
player->getName(),
@@ -637,7 +643,8 @@ void PlayerDatabasePostgreSQL::listPlayers(std::vector<std::string> &res)
}
AuthDatabasePostgreSQL::AuthDatabasePostgreSQL(const std::string &connect_string) :
- Database_PostgreSQL(connect_string), AuthDatabase()
+ Database_PostgreSQL(connect_string, "_auth"),
+ AuthDatabase()
{
connectToDatabase();
}
diff --git a/src/database/database-postgresql.h b/src/database/database-postgresql.h
index f47deda33..81b4a2b10 100644
--- a/src/database/database-postgresql.h
+++ b/src/database/database-postgresql.h
@@ -29,7 +29,7 @@ class Settings;
class Database_PostgreSQL: public Database
{
public:
- Database_PostgreSQL(const std::string &connect_string);
+ Database_PostgreSQL(const std::string &connect_string, const char *type);
~Database_PostgreSQL();
void beginSave();
diff --git a/src/database/database-redis.cpp b/src/database/database-redis.cpp
index 096ea504d..5ffff67b7 100644
--- a/src/database/database-redis.cpp
+++ b/src/database/database-redis.cpp
@@ -127,8 +127,7 @@ void Database_Redis::loadBlock(const v3s16 &pos, std::string *block)
switch (reply->type) {
case REDIS_REPLY_STRING: {
- *block = std::string(reply->str, reply->len);
- // std::string copies the memory so this won't cause any problems
+ block->assign(reply->str, reply->len);
freeReplyObject(reply);
return;
}
@@ -141,8 +140,7 @@ void Database_Redis::loadBlock(const v3s16 &pos, std::string *block)
"Redis command 'HGET %s %s' errored: ") + errstr);
}
case REDIS_REPLY_NIL: {
- *block = "";
- // block not found in database
+ block->clear();
freeReplyObject(reply);
return;
}
diff --git a/src/database/database-sqlite3.cpp b/src/database/database-sqlite3.cpp
index 4560743b9..1e63ae9d8 100644
--- a/src/database/database-sqlite3.cpp
+++ b/src/database/database-sqlite3.cpp
@@ -228,11 +228,7 @@ void MapDatabaseSQLite3::createDatabase()
void MapDatabaseSQLite3::initStatements()
{
PREPARE_STATEMENT(read, "SELECT `data` FROM `blocks` WHERE `pos` = ? LIMIT 1");
-#ifdef __ANDROID__
- PREPARE_STATEMENT(write, "INSERT INTO `blocks` (`pos`, `data`) VALUES (?, ?)");
-#else
PREPARE_STATEMENT(write, "REPLACE INTO `blocks` (`pos`, `data`) VALUES (?, ?)");
-#endif
PREPARE_STATEMENT(delete, "DELETE FROM `blocks` WHERE `pos` = ?");
PREPARE_STATEMENT(list, "SELECT `pos` FROM `blocks`");
@@ -265,19 +261,6 @@ bool MapDatabaseSQLite3::saveBlock(const v3s16 &pos, const std::string &data)
{
verifyDatabase();
-#ifdef __ANDROID__
- /**
- * Note: For some unknown reason SQLite3 fails to REPLACE blocks on Android,
- * deleting them and then inserting works.
- */
- bindPos(m_stmt_read, pos);
-
- if (sqlite3_step(m_stmt_read) == SQLITE_ROW) {
- deleteBlock(pos);
- }
- sqlite3_reset(m_stmt_read);
-#endif
-
bindPos(m_stmt_write, pos);
SQLOK(sqlite3_bind_blob(m_stmt_write, 2, data.data(), data.size(), NULL),
"Internal error: failed to bind query at " __FILE__ ":" TOSTRING(__LINE__));
@@ -302,7 +285,10 @@ void MapDatabaseSQLite3::loadBlock(const v3s16 &pos, std::string *block)
const char *data = (const char *) sqlite3_column_blob(m_stmt_read, 0);
size_t len = sqlite3_column_bytes(m_stmt_read, 0);
- *block = (data) ? std::string(data, len) : "";
+ if (data)
+ block->assign(data, len);
+ else
+ block->clear();
sqlite3_step(m_stmt_read);
// We should never get more than 1 row, so ok to reset
@@ -491,6 +477,7 @@ void PlayerDatabaseSQLite3::savePlayer(RemotePlayer *player)
sqlite3_reset(m_stmt_player_remove_inventory_items);
std::vector<const InventoryList*> inventory_lists = sao->getInventory()->getLists();
+ std::ostringstream oss;
for (u16 i = 0; i < inventory_lists.size(); i++) {
const InventoryList* list = inventory_lists[i];
@@ -503,9 +490,10 @@ void PlayerDatabaseSQLite3::savePlayer(RemotePlayer *player)
sqlite3_reset(m_stmt_player_add_inventory);
for (u32 j = 0; j < list->getSize(); j++) {
- std::ostringstream os;
- list->getItem(j).serialize(os);
- std::string itemStr = os.str();
+ oss.str("");
+ oss.clear();
+ list->getItem(j).serialize(oss);
+ std::string itemStr = oss.str();
str_to_sqlite(m_stmt_player_add_inventory_items, 1, player->getName());
int_to_sqlite(m_stmt_player_add_inventory_items, 2, i);
@@ -774,3 +762,108 @@ void AuthDatabaseSQLite3::writePrivileges(const AuthEntry &authEntry)
sqlite3_reset(m_stmt_write_privs);
}
}
+
+ModMetadataDatabaseSQLite3::ModMetadataDatabaseSQLite3(const std::string &savedir):
+ Database_SQLite3(savedir, "mod_storage"), ModMetadataDatabase()
+{
+}
+
+ModMetadataDatabaseSQLite3::~ModMetadataDatabaseSQLite3()
+{
+ FINALIZE_STATEMENT(m_stmt_remove)
+ FINALIZE_STATEMENT(m_stmt_set)
+ FINALIZE_STATEMENT(m_stmt_get)
+}
+
+void ModMetadataDatabaseSQLite3::createDatabase()
+{
+ assert(m_database); // Pre-condition
+
+ SQLOK(sqlite3_exec(m_database,
+ "CREATE TABLE IF NOT EXISTS `entries` (\n"
+ " `modname` TEXT NOT NULL,\n"
+ " `key` BLOB NOT NULL,\n"
+ " `value` BLOB NOT NULL,\n"
+ " PRIMARY KEY (`modname`, `key`)\n"
+ ");\n",
+ NULL, NULL, NULL),
+ "Failed to create database table");
+}
+
+void ModMetadataDatabaseSQLite3::initStatements()
+{
+ PREPARE_STATEMENT(get, "SELECT `key`, `value` FROM `entries` WHERE `modname` = ?");
+ PREPARE_STATEMENT(set,
+ "REPLACE INTO `entries` (`modname`, `key`, `value`) VALUES (?, ?, ?)");
+ PREPARE_STATEMENT(remove, "DELETE FROM `entries` WHERE `modname` = ? AND `key` = ?");
+}
+
+bool ModMetadataDatabaseSQLite3::getModEntries(const std::string &modname, StringMap *storage)
+{
+ verifyDatabase();
+
+ str_to_sqlite(m_stmt_get, 1, modname);
+ while (sqlite3_step(m_stmt_get) == SQLITE_ROW) {
+ const char *key_data = (const char *) sqlite3_column_blob(m_stmt_get, 0);
+ size_t key_len = sqlite3_column_bytes(m_stmt_get, 0);
+ const char *value_data = (const char *) sqlite3_column_blob(m_stmt_get, 1);
+ size_t value_len = sqlite3_column_bytes(m_stmt_get, 1);
+ (*storage)[std::string(key_data, key_len)] = std::string(value_data, value_len);
+ }
+ sqlite3_vrfy(sqlite3_errcode(m_database), SQLITE_DONE);
+
+ sqlite3_reset(m_stmt_get);
+
+ return true;
+}
+
+bool ModMetadataDatabaseSQLite3::setModEntry(const std::string &modname,
+ const std::string &key, const std::string &value)
+{
+ verifyDatabase();
+
+ str_to_sqlite(m_stmt_set, 1, modname);
+ SQLOK(sqlite3_bind_blob(m_stmt_set, 2, key.data(), key.size(), NULL),
+ "Internal error: failed to bind query at " __FILE__ ":" TOSTRING(__LINE__));
+ SQLOK(sqlite3_bind_blob(m_stmt_set, 3, value.data(), value.size(), NULL),
+ "Internal error: failed to bind query at " __FILE__ ":" TOSTRING(__LINE__));
+ SQLRES(sqlite3_step(m_stmt_set), SQLITE_DONE, "Failed to set mod entry")
+
+ sqlite3_reset(m_stmt_set);
+
+ return true;
+}
+
+bool ModMetadataDatabaseSQLite3::removeModEntry(const std::string &modname,
+ const std::string &key)
+{
+ verifyDatabase();
+
+ str_to_sqlite(m_stmt_remove, 1, modname);
+ SQLOK(sqlite3_bind_blob(m_stmt_remove, 2, key.data(), key.size(), NULL),
+ "Internal error: failed to bind query at " __FILE__ ":" TOSTRING(__LINE__));
+ sqlite3_vrfy(sqlite3_step(m_stmt_remove), SQLITE_DONE);
+ int changes = sqlite3_changes(m_database);
+
+ sqlite3_reset(m_stmt_remove);
+
+ return changes > 0;
+}
+
+void ModMetadataDatabaseSQLite3::listMods(std::vector<std::string> *res)
+{
+ verifyDatabase();
+
+ char *errmsg;
+ int status = sqlite3_exec(m_database,
+ "SELECT `modname` FROM `entries` GROUP BY `modname`;",
+ [](void *res_vp, int n_col, char **cols, char **col_names) -> int {
+ ((decltype(res)) res_vp)->emplace_back(cols[0]);
+ return 0;
+ }, (void *) res, &errmsg);
+ if (status != SQLITE_OK) {
+ DatabaseException e(std::string("Error trying to list mods with metadata: ") + errmsg);
+ sqlite3_free(errmsg);
+ throw e;
+ }
+}
diff --git a/src/database/database-sqlite3.h b/src/database/database-sqlite3.h
index d7202a918..5e3d7c96c 100644
--- a/src/database/database-sqlite3.h
+++ b/src/database/database-sqlite3.h
@@ -232,3 +232,28 @@ private:
sqlite3_stmt *m_stmt_delete_privs = nullptr;
sqlite3_stmt *m_stmt_last_insert_rowid = nullptr;
};
+
+class ModMetadataDatabaseSQLite3 : private Database_SQLite3, public ModMetadataDatabase
+{
+public:
+ ModMetadataDatabaseSQLite3(const std::string &savedir);
+ virtual ~ModMetadataDatabaseSQLite3();
+
+ virtual bool getModEntries(const std::string &modname, StringMap *storage);
+ virtual bool setModEntry(const std::string &modname,
+ const std::string &key, const std::string &value);
+ virtual bool removeModEntry(const std::string &modname, const std::string &key);
+ virtual void listMods(std::vector<std::string> *res);
+
+ virtual void beginSave() { Database_SQLite3::beginSave(); }
+ virtual void endSave() { Database_SQLite3::endSave(); }
+
+protected:
+ virtual void createDatabase();
+ virtual void initStatements();
+
+private:
+ sqlite3_stmt *m_stmt_get = nullptr;
+ sqlite3_stmt *m_stmt_set = nullptr;
+ sqlite3_stmt *m_stmt_remove = nullptr;
+};
diff --git a/src/database/database.h b/src/database/database.h
index b7d551935..fbb5befea 100644
--- a/src/database/database.h
+++ b/src/database/database.h
@@ -25,6 +25,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "irr_v3d.h"
#include "irrlichttypes.h"
#include "util/basic_macros.h"
+#include "util/string.h"
class Database
{
@@ -84,3 +85,15 @@ public:
virtual void listNames(std::vector<std::string> &res) = 0;
virtual void reload() = 0;
};
+
+class ModMetadataDatabase : public Database
+{
+public:
+ virtual ~ModMetadataDatabase() = default;
+
+ virtual bool getModEntries(const std::string &modname, StringMap *storage) = 0;
+ virtual bool setModEntry(const std::string &modname,
+ const std::string &key, const std::string &value) = 0;
+ virtual bool removeModEntry(const std::string &modname, const std::string &key) = 0;
+ virtual void listMods(std::vector<std::string> *res) = 0;
+};
diff --git a/src/defaultsettings.cpp b/src/defaultsettings.cpp
index cda953082..600fc65f3 100644
--- a/src/defaultsettings.cpp
+++ b/src/defaultsettings.cpp
@@ -56,7 +56,7 @@ void set_default_settings()
settings->setDefault("client_unload_unused_data_timeout", "600");
settings->setDefault("client_mapblock_limit", "7500");
settings->setDefault("enable_build_where_you_stand", "false");
- settings->setDefault("curl_timeout", "5000");
+ settings->setDefault("curl_timeout", "20000");
settings->setDefault("curl_parallel_limit", "8");
settings->setDefault("curl_file_download_timeout", "300000");
settings->setDefault("curl_verify_cert", "true");
@@ -65,6 +65,8 @@ void set_default_settings()
settings->setDefault("max_out_chat_queue_size", "20");
settings->setDefault("pause_on_lost_focus", "false");
settings->setDefault("enable_register_confirmation", "true");
+ settings->setDefault("clickable_chat_weblinks", "false");
+ settings->setDefault("chat_weblink_color", "#8888FF");
// Keymap
settings->setDefault("remote_port", "30000");
@@ -80,7 +82,7 @@ void set_default_settings()
settings->setDefault("keymap_drop", "KEY_KEY_Q");
settings->setDefault("keymap_zoom", "KEY_KEY_Z");
settings->setDefault("keymap_inventory", "KEY_KEY_I");
- settings->setDefault("keymap_special1", "KEY_KEY_E");
+ settings->setDefault("keymap_aux1", "KEY_KEY_E");
settings->setDefault("keymap_chat", "KEY_KEY_T");
settings->setDefault("keymap_cmd", "/");
settings->setDefault("keymap_cmd_local", ".");
@@ -97,6 +99,7 @@ void set_default_settings()
settings->setDefault("keymap_increase_volume", "");
settings->setDefault("keymap_decrease_volume", "");
settings->setDefault("keymap_cinematic", "");
+ settings->setDefault("keymap_toggle_block_bounds", "");
settings->setDefault("keymap_toggle_hud", "KEY_F1");
settings->setDefault("keymap_toggle_chat", "KEY_F2");
settings->setDefault("keymap_toggle_fog", "KEY_F3");
@@ -176,12 +179,12 @@ void set_default_settings()
settings->setDefault("screen_h", "600");
settings->setDefault("autosave_screensize", "true");
settings->setDefault("fullscreen", "false");
- settings->setDefault("fullscreen_bpp", "24");
settings->setDefault("vsync", "false");
settings->setDefault("fov", "72");
settings->setDefault("leaves_style", "fancy");
settings->setDefault("connected_glass", "false");
settings->setDefault("smooth_lighting", "true");
+ settings->setDefault("performance_tradeoffs", "false");
settings->setDefault("lighting_alpha", "0.0");
settings->setDefault("lighting_beta", "1.5");
settings->setDefault("display_gamma", "1.0");
@@ -261,6 +264,18 @@ void set_default_settings()
settings->setDefault("enable_waving_leaves", "false");
settings->setDefault("enable_waving_plants", "false");
+ // Effects Shadows
+ settings->setDefault("enable_dynamic_shadows", "false");
+ settings->setDefault("shadow_strength", "0.2");
+ settings->setDefault("shadow_map_max_distance", "200.0");
+ settings->setDefault("shadow_map_texture_size", "2048");
+ settings->setDefault("shadow_map_texture_32bit", "true");
+ settings->setDefault("shadow_map_color", "false");
+ settings->setDefault("shadow_filters", "1");
+ settings->setDefault("shadow_poisson_filter", "true");
+ settings->setDefault("shadow_update_frames", "8");
+ settings->setDefault("shadow_soft_radius", "1.0");
+ settings->setDefault("shadow_sky_body_orbit_tilt", "0.0");
// Input
settings->setDefault("invert_mouse", "false");
@@ -271,7 +286,7 @@ void set_default_settings()
settings->setDefault("aux1_descends", "false");
settings->setDefault("doubletap_jump", "false");
settings->setDefault("always_fly_fast", "true");
-#ifdef __ANDROID__
+#ifdef HAVE_TOUCHSCREENGUI
settings->setDefault("autojump", "true");
#else
settings->setDefault("autojump", "false");
@@ -288,8 +303,7 @@ void set_default_settings()
settings->setDefault("main_menu_path", "");
settings->setDefault("serverlist_file", "favoriteservers.json");
-#if USE_FREETYPE
- settings->setDefault("freetype", "true");
+ // General font settings
settings->setDefault("font_path", porting::getDataPath("fonts" DIR_DELIM "Arimo-Regular.ttf"));
settings->setDefault("font_path_italic", porting::getDataPath("fonts" DIR_DELIM "Arimo-Italic.ttf"));
settings->setDefault("font_path_bold", porting::getDataPath("fonts" DIR_DELIM "Arimo-Bold.ttf"));
@@ -298,26 +312,15 @@ void set_default_settings()
settings->setDefault("font_italic", "false");
settings->setDefault("font_shadow", "1");
settings->setDefault("font_shadow_alpha", "127");
+ settings->setDefault("font_size_divisible_by", "1");
settings->setDefault("mono_font_path", porting::getDataPath("fonts" DIR_DELIM "Cousine-Regular.ttf"));
settings->setDefault("mono_font_path_italic", porting::getDataPath("fonts" DIR_DELIM "Cousine-Italic.ttf"));
settings->setDefault("mono_font_path_bold", porting::getDataPath("fonts" DIR_DELIM "Cousine-Bold.ttf"));
settings->setDefault("mono_font_path_bold_italic", porting::getDataPath("fonts" DIR_DELIM "Cousine-BoldItalic.ttf"));
+ settings->setDefault("mono_font_size_divisible_by", "1");
settings->setDefault("fallback_font_path", porting::getDataPath("fonts" DIR_DELIM "DroidSansFallbackFull.ttf"));
- settings->setDefault("fallback_font_shadow", "1");
- settings->setDefault("fallback_font_shadow_alpha", "128");
-
std::string font_size_str = std::to_string(TTF_DEFAULT_FONT_SIZE);
-
- settings->setDefault("fallback_font_size", font_size_str);
-#else
- settings->setDefault("freetype", "false");
- settings->setDefault("font_path", porting::getDataPath("fonts" DIR_DELIM "mono_dejavu_sans"));
- settings->setDefault("mono_font_path", porting::getDataPath("fonts" DIR_DELIM "mono_dejavu_sans"));
-
- std::string font_size_str = std::to_string(DEFAULT_FONT_SIZE);
-#endif
- // General font settings
settings->setDefault("font_size", font_size_str);
settings->setDefault("mono_font_size", font_size_str);
settings->setDefault("chat_font_size", "0"); // Default "font_size"
@@ -389,7 +392,7 @@ void set_default_settings()
settings->setDefault("chat_message_limit_per_10sec", "8.0");
settings->setDefault("chat_message_limit_trigger_kick", "50");
settings->setDefault("sqlite_synchronous", "2");
- settings->setDefault("map_compression_level_disk", "3");
+ settings->setDefault("map_compression_level_disk", "-1");
settings->setDefault("map_compression_level_net", "-1");
settings->setDefault("full_block_send_enable_min_time_from_building", "2.0");
settings->setDefault("dedicated_server_step", "0.09");
@@ -432,7 +435,7 @@ void set_default_settings()
// Mapgen
settings->setDefault("mg_name", "v7");
settings->setDefault("water_level", "1");
- settings->setDefault("mapgen_limit", "31000");
+ settings->setDefault("mapgen_limit", "31007");
settings->setDefault("chunksize", "5");
settings->setDefault("fixed_map_seed", "");
settings->setDefault("max_block_generate_distance", "10");
@@ -446,9 +449,9 @@ void set_default_settings()
settings->setDefault("server_name", "");
settings->setDefault("server_description", "");
- settings->setDefault("high_precision_fpu", "true");
settings->setDefault("enable_console", "false");
settings->setDefault("screen_dpi", "72");
+ settings->setDefault("display_density_factor", "1");
// Altered settings for macOS
#if defined(__MACH__) && defined(__APPLE__)
@@ -456,16 +459,19 @@ void set_default_settings()
settings->setDefault("fps_max", "0");
#endif
+#ifdef HAVE_TOUCHSCREENGUI
+ settings->setDefault("touchtarget", "true");
+ settings->setDefault("touchscreen_threshold","20");
+ settings->setDefault("fixed_virtual_joystick", "false");
+ settings->setDefault("virtual_joystick_triggers_aux1", "false");
+#endif
// Altered settings for Android
#ifdef __ANDROID__
settings->setDefault("screen_w", "0");
settings->setDefault("screen_h", "0");
settings->setDefault("fullscreen", "true");
- settings->setDefault("touchtarget", "true");
- settings->setDefault("touchscreen_threshold","20");
- settings->setDefault("fixed_virtual_joystick", "false");
- settings->setDefault("virtual_joystick_triggers_aux", "false");
settings->setDefault("smooth_lighting", "false");
+ settings->setDefault("performance_tradeoffs", "true");
settings->setDefault("max_simultaneous_block_sends_per_client", "10");
settings->setDefault("emergequeue_limit_diskonly", "16");
settings->setDefault("emergequeue_limit_generate", "16");
@@ -476,7 +482,7 @@ void set_default_settings()
settings->setDefault("max_objects_per_block", "20");
settings->setDefault("sqlite_synchronous", "1");
settings->setDefault("map_compression_level_disk", "-1");
- settings->setDefault("map_compression_level_net", "3");
+ settings->setDefault("map_compression_level_net", "-1");
settings->setDefault("server_map_save_interval", "15");
settings->setDefault("client_mapblock_limit", "1000");
settings->setDefault("active_block_range", "2");
diff --git a/src/emerge.cpp b/src/emerge.cpp
index e0dc5628e..55ae99caf 100644
--- a/src/emerge.cpp
+++ b/src/emerge.cpp
@@ -113,13 +113,15 @@ EmergeParams::~EmergeParams()
{
infostream << "EmergeParams: destroying " << this << std::endl;
// Delete everything that was cloned on creation of EmergeParams
+ delete biomegen;
delete biomemgr;
delete oremgr;
delete decomgr;
delete schemmgr;
}
-EmergeParams::EmergeParams(EmergeManager *parent, const BiomeManager *biomemgr,
+EmergeParams::EmergeParams(EmergeManager *parent, const BiomeGen *biomegen,
+ const BiomeManager *biomemgr,
const OreManager *oremgr, const DecorationManager *decomgr,
const SchematicManager *schemmgr) :
ndef(parent->ndef),
@@ -129,6 +131,7 @@ EmergeParams::EmergeParams(EmergeManager *parent, const BiomeManager *biomemgr,
biomemgr(biomemgr->clone()), oremgr(oremgr->clone()),
decomgr(decomgr->clone()), schemmgr(schemmgr->clone())
{
+ this->biomegen = biomegen->clone(this->biomemgr);
}
////
@@ -143,6 +146,10 @@ EmergeManager::EmergeManager(Server *server)
this->decomgr = new DecorationManager(server);
this->schemmgr = new SchematicManager(server);
+ // initialized later
+ this->mgparams = nullptr;
+ this->biomegen = nullptr;
+
// Note that accesses to this variable are not synchronized.
// This is because the *only* thread ever starting or stopping
// EmergeThreads should be the ServerThread.
@@ -158,20 +165,17 @@ EmergeManager::EmergeManager(Server *server)
if (nthreads < 1)
nthreads = 1;
- m_qlimit_total = g_settings->getU16("emergequeue_limit_total");
+ m_qlimit_total = g_settings->getU32("emergequeue_limit_total");
// FIXME: these fallback values are probably not good
- if (!g_settings->getU16NoEx("emergequeue_limit_diskonly", m_qlimit_diskonly))
+ if (!g_settings->getU32NoEx("emergequeue_limit_diskonly", m_qlimit_diskonly))
m_qlimit_diskonly = nthreads * 5 + 1;
- if (!g_settings->getU16NoEx("emergequeue_limit_generate", m_qlimit_generate))
+ if (!g_settings->getU32NoEx("emergequeue_limit_generate", m_qlimit_generate))
m_qlimit_generate = nthreads + 1;
// don't trust user input for something very important like this
- if (m_qlimit_total < 1)
- m_qlimit_total = 1;
- if (m_qlimit_diskonly < 1)
- m_qlimit_diskonly = 1;
- if (m_qlimit_generate < 1)
- m_qlimit_generate = 1;
+ m_qlimit_total = rangelim(m_qlimit_total, 1, 1000000);
+ m_qlimit_diskonly = rangelim(m_qlimit_diskonly, 1, 1000000);
+ m_qlimit_generate = rangelim(m_qlimit_generate, 1, 1000000);
for (s16 i = 0; i < nthreads; i++)
m_threads.push_back(new EmergeThread(server, i));
@@ -240,9 +244,12 @@ void EmergeManager::initMapgens(MapgenParams *params)
mgparams = params;
+ v3s16 csize = v3s16(1, 1, 1) * (params->chunksize * MAP_BLOCKSIZE);
+ biomegen = biomemgr->createBiomeGen(BIOMEGEN_ORIGINAL, params->bparams, csize);
+
for (u32 i = 0; i != m_threads.size(); i++) {
- EmergeParams *p = new EmergeParams(
- this, biomemgr, oremgr, decomgr, schemmgr);
+ EmergeParams *p = new EmergeParams(this, biomegen,
+ biomemgr, oremgr, decomgr, schemmgr);
infostream << "EmergeManager: Created params " << p
<< " for thread " << i << std::endl;
m_mapgens.push_back(Mapgen::createMapgen(params->mgtype, params, p));
@@ -348,18 +355,19 @@ bool EmergeManager::enqueueBlockEmergeEx(
}
+bool EmergeManager::isBlockInQueue(v3s16 pos)
+{
+ MutexAutoLock queuelock(m_queue_mutex);
+ return m_blocks_enqueued.find(pos) != m_blocks_enqueued.end();
+}
+
+
//
// Mapgen-related helper functions
//
// TODO(hmmmm): Move this to ServerMap
-v3s16 EmergeManager::getContainingChunk(v3s16 blockpos)
-{
- return getContainingChunk(blockpos, mgparams->chunksize);
-}
-
-// TODO(hmmmm): Move this to ServerMap
v3s16 EmergeManager::getContainingChunk(v3s16 blockpos, s16 chunksize)
{
s16 coff = -chunksize / 2;
@@ -382,17 +390,6 @@ int EmergeManager::getSpawnLevelAtPoint(v2s16 p)
}
-int EmergeManager::getGroundLevelAtPoint(v2s16 p)
-{
- if (m_mapgens.empty() || !m_mapgens[0]) {
- errorstream << "EmergeManager: getGroundLevelAtPoint() called"
- " before mapgen init" << std::endl;
- return 0;
- }
-
- return m_mapgens[0]->getGroundLevelAtPoint(p);
-}
-
// TODO(hmmmm): Move this to ServerMap
bool EmergeManager::isBlockUnderground(v3s16 blockpos)
{
@@ -408,14 +405,14 @@ bool EmergeManager::pushBlockEmergeData(
void *callback_param,
bool *entry_already_exists)
{
- u16 &count_peer = m_peer_queue_count[peer_requested];
+ u32 &count_peer = m_peer_queue_count[peer_requested];
if ((flags & BLOCK_EMERGE_FORCE_QUEUE) == 0) {
if (m_blocks_enqueued.size() >= m_qlimit_total)
return false;
if (peer_requested != PEER_ID_INEXISTENT) {
- u16 qlimit_peer = (flags & BLOCK_EMERGE_ALLOW_GEN) ?
+ u32 qlimit_peer = (flags & BLOCK_EMERGE_ALLOW_GEN) ?
m_qlimit_generate : m_qlimit_diskonly;
if (count_peer >= qlimit_peer)
return false;
@@ -450,20 +447,18 @@ bool EmergeManager::pushBlockEmergeData(
bool EmergeManager::popBlockEmergeData(v3s16 pos, BlockEmergeData *bedata)
{
- std::map<v3s16, BlockEmergeData>::iterator it;
- std::unordered_map<u16, u16>::iterator it2;
-
- it = m_blocks_enqueued.find(pos);
+ auto it = m_blocks_enqueued.find(pos);
if (it == m_blocks_enqueued.end())
return false;
*bedata = it->second;
- it2 = m_peer_queue_count.find(bedata->peer_requested);
+ auto it2 = m_peer_queue_count.find(bedata->peer_requested);
if (it2 == m_peer_queue_count.end())
return false;
- u16 &count_peer = it2->second;
+ u32 &count_peer = it2->second;
+
assert(count_peer != 0);
count_peer--;
@@ -635,15 +630,17 @@ MapBlock *EmergeThread::finishGen(v3s16 pos, BlockMakeData *bmdata,
m_server->getScriptIface()->environment_OnGenerated(
minp, maxp, m_mapgen->blockseed);
} catch (LuaError &e) {
- m_server->setAsyncFatalError("Lua: finishGen" + std::string(e.what()));
+ m_server->setAsyncFatalError(e);
}
+ EMERGE_DBG_OUT("ended up with: " << analyze_block(block));
+
/*
- Clear generate notifier events
+ Clear mapgen state
*/
+ assert(!m_mapgen->generating);
m_mapgen->gennotify.clearEvents();
-
- EMERGE_DBG_OUT("ended up with: " << analyze_block(block));
+ m_mapgen->vm = nullptr;
/*
Activate the block
@@ -659,19 +656,19 @@ void *EmergeThread::run()
BEGIN_DEBUG_EXCEPTION_HANDLER
v3s16 pos;
+ std::map<v3s16, MapBlock *> modified_blocks;
- m_map = (ServerMap *)&(m_server->m_env->getMap());
+ m_map = &m_server->m_env->getServerMap();
m_emerge = m_server->m_emerge;
m_mapgen = m_emerge->m_mapgens[id];
enable_mapgen_debug_info = m_emerge->enable_mapgen_debug_info;
try {
while (!stopRequested()) {
- std::map<v3s16, MapBlock *> modified_blocks;
BlockEmergeData bedata;
BlockMakeData bmdata;
EmergeAction action;
- MapBlock *block;
+ MapBlock *block = nullptr;
if (!popBlockEmerge(&pos, &bedata)) {
m_queue_event.wait();
@@ -694,6 +691,8 @@ void *EmergeThread::run()
}
block = finishGen(pos, &bmdata, &modified_blocks);
+ if (!block)
+ action = EMERGE_ERRORED;
}
runCompletionCallbacks(pos, action, bedata.callbacks);
@@ -703,6 +702,7 @@ void *EmergeThread::run()
if (!modified_blocks.empty())
m_server->SetBlocksNotSent(modified_blocks);
+ modified_blocks.clear();
}
} catch (VersionMismatchException &e) {
std::ostringstream err;
@@ -724,6 +724,8 @@ void *EmergeThread::run()
m_server->setAsyncFatalError(err.str());
}
+ cancelPendingItems();
+
END_DEBUG_EXCEPTION_HANDLER
return NULL;
}
diff --git a/src/emerge.h b/src/emerge.h
index da845e243..61e7bda63 100644
--- a/src/emerge.h
+++ b/src/emerge.h
@@ -99,13 +99,15 @@ public:
u32 gen_notify_on;
const std::set<u32> *gen_notify_on_deco_ids; // shared
+ BiomeGen *biomegen;
BiomeManager *biomemgr;
OreManager *oremgr;
DecorationManager *decomgr;
SchematicManager *schemmgr;
private:
- EmergeParams(EmergeManager *parent, const BiomeManager *biomemgr,
+ EmergeParams(EmergeManager *parent, const BiomeGen *biomegen,
+ const BiomeManager *biomemgr,
const OreManager *oremgr, const DecorationManager *decomgr,
const SchematicManager *schemmgr);
};
@@ -140,6 +142,8 @@ public:
~EmergeManager();
DISABLE_CLASS_COPY(EmergeManager);
+ const BiomeGen *getBiomeGen() const { return biomegen; }
+
// no usage restrictions
const BiomeManager *getBiomeManager() const { return biomemgr; }
const OreManager *getOreManager() const { return oremgr; }
@@ -170,13 +174,12 @@ public:
EmergeCompletionCallback callback,
void *callback_param);
- v3s16 getContainingChunk(v3s16 blockpos);
+ bool isBlockInQueue(v3s16 pos);
Mapgen *getCurrentMapgen();
// Mapgen helpers methods
int getSpawnLevelAtPoint(v2s16 p);
- int getGroundLevelAtPoint(v2s16 p);
bool isBlockUnderground(v3s16 blockpos);
static v3s16 getContainingChunk(v3s16 blockpos, s16 chunksize);
@@ -188,14 +191,15 @@ private:
std::mutex m_queue_mutex;
std::map<v3s16, BlockEmergeData> m_blocks_enqueued;
- std::unordered_map<u16, u16> m_peer_queue_count;
+ std::unordered_map<u16, u32> m_peer_queue_count;
- u16 m_qlimit_total;
- u16 m_qlimit_diskonly;
- u16 m_qlimit_generate;
+ u32 m_qlimit_total;
+ u32 m_qlimit_diskonly;
+ u32 m_qlimit_generate;
// Managers of various map generation-related components
// Note that each Mapgen gets a copy(!) of these to work with
+ BiomeGen *biomegen;
BiomeManager *biomemgr;
OreManager *oremgr;
DecorationManager *decomgr;
diff --git a/src/environment.cpp b/src/environment.cpp
index 06f2b8bf9..b04f77557 100644
--- a/src/environment.cpp
+++ b/src/environment.cpp
@@ -169,6 +169,12 @@ void Environment::continueRaycast(RaycastState *state, PointedThing *result)
new_nodes.MaxEdge.Z = new_nodes.MinEdge.Z;
}
+ if (new_nodes.MaxEdge.X == S16_MAX ||
+ new_nodes.MaxEdge.Y == S16_MAX ||
+ new_nodes.MaxEdge.Z == S16_MAX) {
+ break; // About to go out of bounds
+ }
+
// For each untested node
for (s16 x = new_nodes.MinEdge.X; x <= new_nodes.MaxEdge.X; x++)
for (s16 y = new_nodes.MinEdge.Y; y <= new_nodes.MaxEdge.Y; y++)
diff --git a/src/filesys.cpp b/src/filesys.cpp
index 5ffb4506e..ea00def6a 100644
--- a/src/filesys.cpp
+++ b/src/filesys.cpp
@@ -21,21 +21,33 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "util/string.h"
#include <iostream>
#include <cstdio>
+#include <cstdlib>
#include <cstring>
#include <cerrno>
#include <fstream>
#include "log.h"
#include "config.h"
#include "porting.h"
+#ifndef SERVER
+#include "irr_ptr.h"
+#endif
namespace fs
{
-#ifdef _WIN32 // WINDOWS
+#ifdef _WIN32
+
+/***********
+ * Windows *
+ ***********/
+#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0501
+#endif
#include <windows.h>
#include <shlwapi.h>
+#include <io.h>
+#include <direct.h>
std::vector<DirListNode> GetDirListing(const std::string &pathstring)
{
@@ -176,16 +188,34 @@ std::string TempPath()
errorstream<<"GetTempPath failed, error = "<<GetLastError()<<std::endl;
return "";
}
- std::vector<char> buf(bufsize);
+ std::string buf;
+ buf.resize(bufsize);
DWORD len = GetTempPath(bufsize, &buf[0]);
if(len == 0 || len > bufsize){
errorstream<<"GetTempPath failed, error = "<<GetLastError()<<std::endl;
return "";
}
- return std::string(buf.begin(), buf.begin() + len);
+ buf.resize(len);
+ return buf;
+}
+
+std::string CreateTempFile()
+{
+ std::string path = TempPath() + DIR_DELIM "MT_XXXXXX";
+ _mktemp_s(&path[0], path.size() + 1); // modifies path
+ HANDLE file = CreateFile(path.c_str(), GENERIC_WRITE, 0, nullptr,
+ CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
+ if (file == INVALID_HANDLE_VALUE)
+ return "";
+ CloseHandle(file);
+ return path;
}
-#else // POSIX
+#else
+
+/*********
+ * POSIX *
+ *********/
#include <sys/types.h>
#include <dirent.h>
@@ -364,8 +394,22 @@ std::string TempPath()
#endif
}
+std::string CreateTempFile()
+{
+ std::string path = TempPath() + DIR_DELIM "MT_XXXXXX";
+ int fd = mkstemp(&path[0]); // modifies path
+ if (fd == -1)
+ return "";
+ close(fd);
+ return path;
+}
+
#endif
+/****************************
+ * portable implementations *
+ ****************************/
+
void GetRecursiveDirs(std::vector<std::string> &dirs, const std::string &dir)
{
static const std::set<char> chars_to_ignore = { '_', '.' };
@@ -517,6 +561,30 @@ bool CopyDir(const std::string &source, const std::string &target)
return false;
}
+bool MoveDir(const std::string &source, const std::string &target)
+{
+ infostream << "Moving \"" << source << "\" to \"" << target << "\"" << std::endl;
+
+ // If target exists as empty folder delete, otherwise error
+ if (fs::PathExists(target)) {
+ if (rmdir(target.c_str()) != 0) {
+ errorstream << "MoveDir: target \"" << target
+ << "\" exists as file or non-empty folder" << std::endl;
+ return false;
+ }
+ }
+
+ // Try renaming first which is instant
+ if (fs::Rename(source, target))
+ return true;
+
+ infostream << "MoveDir: rename not possible, will copy instead" << std::endl;
+ bool retval = fs::CopyDir(source, target);
+ if (retval)
+ retval &= fs::RecursiveDelete(source);
+ return retval;
+}
+
bool PathStartsWith(const std::string &path, const std::string &prefix)
{
size_t pathsize = path.size();
@@ -727,6 +795,67 @@ bool safeWriteToFile(const std::string &path, const std::string &content)
return true;
}
+#ifndef SERVER
+bool extractZipFile(io::IFileSystem *fs, const char *filename, const std::string &destination)
+{
+ // Be careful here not to touch the global file hierarchy in Irrlicht
+ // since this function needs to be thread-safe!
+
+ io::IArchiveLoader *zip_loader = nullptr;
+ for (u32 i = 0; i < fs->getArchiveLoaderCount(); i++) {
+ if (fs->getArchiveLoader(i)->isALoadableFileFormat(io::EFAT_ZIP)) {
+ zip_loader = fs->getArchiveLoader(i);
+ break;
+ }
+ }
+ if (!zip_loader) {
+ warningstream << "fs::extractZipFile(): Irrlicht said it doesn't support ZIPs." << std::endl;
+ return false;
+ }
+
+ irr_ptr<io::IFileArchive> opened_zip(zip_loader->createArchive(filename, false, false));
+ const io::IFileList* files_in_zip = opened_zip->getFileList();
+
+ for (u32 i = 0; i < files_in_zip->getFileCount(); i++) {
+ std::string fullpath = destination + DIR_DELIM;
+ fullpath += files_in_zip->getFullFileName(i).c_str();
+ std::string fullpath_dir = fs::RemoveLastPathComponent(fullpath);
+
+ if (files_in_zip->isDirectory(i))
+ continue; // ignore, we create dirs as necessary
+
+ if (!fs::PathExists(fullpath_dir) && !fs::CreateAllDirs(fullpath_dir))
+ return false;
+
+ irr_ptr<io::IReadFile> toread(opened_zip->createAndOpenFile(i));
+
+ std::ofstream os(fullpath.c_str(), std::ios::binary);
+ if (!os.good())
+ return false;
+
+ char buffer[4096];
+ long total_read = 0;
+
+ while (total_read < toread->getSize()) {
+ long bytes_read = toread->read(buffer, sizeof(buffer));
+ bool error = true;
+ if (bytes_read != 0) {
+ os.write(buffer, bytes_read);
+ error = os.fail();
+ }
+ if (error) {
+ os.close();
+ remove(fullpath.c_str());
+ return false;
+ }
+ total_read += bytes_read;
+ }
+ }
+
+ return true;
+}
+#endif
+
bool ReadFile(const std::string &path, std::string &out)
{
std::ifstream is(path, std::ios::binary | std::ios::ate);
@@ -739,7 +868,7 @@ bool ReadFile(const std::string &path, std::string &out)
is.seekg(0);
is.read(&out[0], size);
- return true;
+ return !is.fail();
}
bool Rename(const std::string &from, const std::string &to)
diff --git a/src/filesys.h b/src/filesys.h
index cfbfa02bf..3fa2524c3 100644
--- a/src/filesys.h
+++ b/src/filesys.h
@@ -24,18 +24,22 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <vector>
#include "exceptions.h"
-#ifdef _WIN32 // WINDOWS
+#ifdef _WIN32
#define DIR_DELIM "\\"
#define DIR_DELIM_CHAR '\\'
#define FILESYS_CASE_INSENSITIVE true
#define PATH_DELIM ";"
-#else // POSIX
+#else
#define DIR_DELIM "/"
#define DIR_DELIM_CHAR '/'
#define FILESYS_CASE_INSENSITIVE false
#define PATH_DELIM ":"
#endif
+namespace irr { namespace io {
+class IFileSystem;
+}}
+
namespace fs
{
@@ -67,6 +71,10 @@ bool DeleteSingleFileOrEmptyDirectory(const std::string &path);
// Returns path to temp directory, can return "" on error
std::string TempPath();
+// Returns path to securely-created temporary file (will already exist when this function returns)
+// can return "" on error
+std::string CreateTempFile();
+
/* Returns a list of subdirectories, including the path itself, but excluding
hidden directories (whose names start with . or _)
*/
@@ -98,6 +106,10 @@ bool CopyFileContents(const std::string &source, const std::string &target);
// Omits files and subdirectories that start with a period
bool CopyDir(const std::string &source, const std::string &target);
+// Move directory and all subdirectories
+// Behavior with files/subdirs that start with a period is undefined
+bool MoveDir(const std::string &source, const std::string &target);
+
// Check if one path is prefix of another
// For example, "/tmp" is a prefix of "/tmp" and "/tmp/file" but not "/tmp2"
// Ignores case differences and '/' vs. '\\' on Windows
@@ -125,6 +137,10 @@ const char *GetFilenameFromPath(const char *path);
bool safeWriteToFile(const std::string &path, const std::string &content);
+#ifndef SERVER
+bool extractZipFile(irr::io::IFileSystem *fs, const char *filename, const std::string &destination);
+#endif
+
bool ReadFile(const std::string &path, std::string &out);
bool Rename(const std::string &from, const std::string &to);
diff --git a/src/gamedef.h b/src/gamedef.h
index bc0ee14c3..8a9246da2 100644
--- a/src/gamedef.h
+++ b/src/gamedef.h
@@ -33,6 +33,7 @@ class EmergeManager;
class Camera;
class ModChannel;
class ModMetadata;
+class ModMetadataDatabase;
namespace irr { namespace scene {
class IAnimatedMesh;
@@ -70,9 +71,9 @@ public:
virtual const std::vector<ModSpec> &getMods() const = 0;
virtual const ModSpec* getModSpec(const std::string &modname) const = 0;
virtual std::string getWorldPath() const { return ""; }
- virtual std::string getModStoragePath() const = 0;
virtual bool registerModStorage(ModMetadata *storage) = 0;
virtual void unregisterModStorage(const std::string &name) = 0;
+ virtual ModMetadataDatabase *getModStorageDatabase() = 0;
virtual bool joinModChannel(const std::string &channel) = 0;
virtual bool leaveModChannel(const std::string &channel) = 0;
diff --git a/src/gettext.cpp b/src/gettext.cpp
index 6818004df..de042cf35 100644
--- a/src/gettext.cpp
+++ b/src/gettext.cpp
@@ -127,6 +127,10 @@ void init_gettext(const char *path, const std::string &configured_language,
// Add user specified locale to environment
setenv("LANGUAGE", configured_language.c_str(), 1);
+#ifdef __ANDROID__
+ setenv("LANG", configured_language.c_str(), 1);
+#endif
+
// Reload locale with changed environment
setlocale(LC_ALL, "");
#elif defined(_MSC_VER)
diff --git a/src/gettext.h b/src/gettext.h
index 42b375d86..6225fef93 100644
--- a/src/gettext.h
+++ b/src/gettext.h
@@ -21,6 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "config.h" // for USE_GETTEXT
#include <string>
+#include "porting.h"
#if USE_GETTEXT
#include <libintl.h>
@@ -47,8 +48,7 @@ void init_gettext(const char *path, const std::string &configured_language,
extern wchar_t *utf8_to_wide_c(const char *str);
-// You must free the returned string!
-// The returned string is allocated using new
+// The returned string must be freed using delete[]
inline const wchar_t *wgettext(const char *str)
{
// We must check here that is not an empty string to avoid trying to translate it
@@ -59,3 +59,49 @@ inline std::string strgettext(const std::string &text)
{
return text.empty() ? "" : gettext(text.c_str());
}
+
+/**
+ * Returns translated string with format args applied
+ *
+ * @tparam Args Template parameter for format args
+ * @param src Translation source string
+ * @param args Variable format args
+ * @return translated string
+ */
+template <typename ...Args>
+inline std::wstring fwgettext(const char *src, Args&&... args)
+{
+ wchar_t buf[255];
+ const wchar_t* str = wgettext(src);
+ swprintf(buf, sizeof(buf) / sizeof(wchar_t), str, std::forward<Args>(args)...);
+ delete[] str;
+ return std::wstring(buf);
+}
+
+/**
+ * Returns translated string with format args applied
+ *
+ * @tparam Args Template parameter for format args
+ * @param format Translation source string
+ * @param args Variable format args
+ * @return translated string.
+ */
+template <typename ...Args>
+inline std::string fmtgettext(const char *format, Args&&... args)
+{
+ std::string buf;
+ std::size_t buf_size = 256;
+ buf.resize(buf_size);
+
+ format = gettext(format);
+
+ int len = porting::mt_snprintf(&buf[0], buf_size, format, std::forward<Args>(args)...);
+ if (len <= 0) throw std::runtime_error("gettext format error: " + std::string(format));
+ if ((size_t)len >= buf.size()) {
+ buf.resize(len+1); // extra null byte
+ porting::mt_snprintf(&buf[0], buf.size(), format, std::forward<Args>(args)...);
+ }
+ buf.resize(len); // remove null bytes
+
+ return buf;
+}
diff --git a/src/gui/CMakeLists.txt b/src/gui/CMakeLists.txt
index fdd36914a..513b13e8e 100644
--- a/src/gui/CMakeLists.txt
+++ b/src/gui/CMakeLists.txt
@@ -1,3 +1,8 @@
+set(extra_gui_SRCS "")
+if(ENABLE_TOUCH)
+ set(extra_gui_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/touchscreengui.cpp)
+endif()
+
set(gui_SRCS
${CMAKE_CURRENT_SOURCE_DIR}/guiAnimatedImage.cpp
${CMAKE_CURRENT_SOURCE_DIR}/guiBackgroundImage.cpp
@@ -23,8 +28,8 @@ set(gui_SRCS
${CMAKE_CURRENT_SOURCE_DIR}/guiTable.cpp
${CMAKE_CURRENT_SOURCE_DIR}/guiHyperText.cpp
${CMAKE_CURRENT_SOURCE_DIR}/guiVolumeChange.cpp
- ${CMAKE_CURRENT_SOURCE_DIR}/intlGUIEditBox.cpp
${CMAKE_CURRENT_SOURCE_DIR}/modalMenu.cpp
${CMAKE_CURRENT_SOURCE_DIR}/profilergraph.cpp
+ ${extra_gui_SRCS}
PARENT_SCOPE
)
diff --git a/src/gui/guiButton.cpp b/src/gui/guiButton.cpp
index b98e5de82..ba95b81c3 100644
--- a/src/gui/guiButton.cpp
+++ b/src/gui/guiButton.cpp
@@ -506,6 +506,13 @@ video::SColor GUIButton::getOverrideColor() const
return OverrideColor;
}
+#if IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR > 8
+video::SColor GUIButton::getActiveColor() const
+{
+ return video::SColor(0,0,0,0); // unused?
+}
+#endif
+
void GUIButton::enableOverrideColor(bool enable)
{
OverrideColorEnabled = enable;
@@ -625,85 +632,6 @@ bool GUIButton::isDrawingBorder() const
}
-//! Writes attributes of the element.
-void GUIButton::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options=0) const
-{
- IGUIButton::serializeAttributes(out,options);
-
- out->addBool ("PushButton", IsPushButton );
- if (IsPushButton)
- out->addBool("Pressed", Pressed);
-
- for ( u32 i=0; i<(u32)EGBIS_COUNT; ++i )
- {
- if ( ButtonImages[i].Texture )
- {
- core::stringc name( GUIButtonImageStateNames[i] );
- out->addTexture(name.c_str(), ButtonImages[i].Texture);
- name += "Rect";
- out->addRect(name.c_str(), ButtonImages[i].SourceRect);
- }
- }
-
- out->addBool ("UseAlphaChannel", UseAlphaChannel);
- out->addBool ("Border", DrawBorder);
- out->addBool ("ScaleImage", ScaleImage);
-
- for ( u32 i=0; i<(u32)EGBS_COUNT; ++i )
- {
- if ( ButtonSprites[i].Index >= 0 )
- {
- core::stringc nameIndex( GUIButtonStateNames[i] );
- nameIndex += "Index";
- out->addInt(nameIndex.c_str(), ButtonSprites[i].Index );
-
- core::stringc nameColor( GUIButtonStateNames[i] );
- nameColor += "Color";
- out->addColor(nameColor.c_str(), ButtonSprites[i].Color );
-
- core::stringc nameLoop( GUIButtonStateNames[i] );
- nameLoop += "Loop";
- out->addBool(nameLoop.c_str(), ButtonSprites[i].Loop );
-
- core::stringc nameScale( GUIButtonStateNames[i] );
- nameScale += "Scale";
- out->addBool(nameScale.c_str(), ButtonSprites[i].Scale );
- }
- }
-
- // out->addString ("OverrideFont", OverrideFont);
-}
-
-
-//! Reads attributes of the element
-void GUIButton::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options=0)
-{
- IGUIButton::deserializeAttributes(in,options);
-
- IsPushButton = in->getAttributeAsBool("PushButton");
- Pressed = IsPushButton ? in->getAttributeAsBool("Pressed") : false;
-
- core::rect<s32> rec = in->getAttributeAsRect("ImageRect");
- if (rec.isValid())
- setImage( in->getAttributeAsTexture("Image"), rec);
- else
- setImage( in->getAttributeAsTexture("Image") );
-
- rec = in->getAttributeAsRect("PressedImageRect");
- if (rec.isValid())
- setPressedImage( in->getAttributeAsTexture("PressedImage"), rec);
- else
- setPressedImage( in->getAttributeAsTexture("PressedImage") );
-
- setDrawBorder(in->getAttributeAsBool("Border"));
- setUseAlphaChannel(in->getAttributeAsBool("UseAlphaChannel"));
- setScaleImage(in->getAttributeAsBool("ScaleImage"));
-
- // setOverrideFont(in->getAttributeAsString("OverrideFont"));
-
- updateAbsolutePosition();
-}
-
// PATCH
GUIButton* GUIButton::addButton(IGUIEnvironment *environment,
const core::rect<s32>& rectangle, ISimpleTextureSource *tsrc,
diff --git a/src/gui/guiButton.h b/src/gui/guiButton.h
index 4e1b04aac..ee9bb6f21 100644
--- a/src/gui/guiButton.h
+++ b/src/gui/guiButton.h
@@ -69,6 +69,12 @@ using namespace irr;
class ISimpleTextureSource;
+#if (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR <= 8)
+#define OVERRIDE_19
+#else
+#define OVERRIDE_19 override
+#endif
+
class GUIButton : public gui::IGUIButton
{
public:
@@ -97,22 +103,27 @@ public:
virtual gui::IGUIFont* getActiveFont() const override;
//! Sets another color for the button text.
- virtual void setOverrideColor(video::SColor color);
+ virtual void setOverrideColor(video::SColor color) OVERRIDE_19;
//! Gets the override color
- virtual video::SColor getOverrideColor(void) const;
+ virtual video::SColor getOverrideColor(void) const OVERRIDE_19;
+
+ #if IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR > 8
+ //! Gets the currently used text color
+ virtual video::SColor getActiveColor() const override;
+ #endif
//! Sets if the button text should use the override color or the color in the gui skin.
- virtual void enableOverrideColor(bool enable);
+ virtual void enableOverrideColor(bool enable) OVERRIDE_19;
//! Checks if an override color is enabled
- virtual bool isOverrideColorEnabled(void) const;
+ virtual bool isOverrideColorEnabled(void) const OVERRIDE_19;
// PATCH
//! Sets an image which should be displayed on the button when it is in the given state.
virtual void setImage(gui::EGUI_BUTTON_IMAGE_STATE state,
video::ITexture* image=nullptr,
- const core::rect<s32>& sourceRect=core::rect<s32>(0,0,0,0));
+ const core::rect<s32>& sourceRect=core::rect<s32>(0,0,0,0)) OVERRIDE_19;
//! Sets an image which should be displayed on the button when it is in normal state.
virtual void setImage(video::ITexture* image=nullptr) override;
@@ -141,7 +152,7 @@ public:
*/
virtual void setSprite(gui::EGUI_BUTTON_STATE state, s32 index,
video::SColor color=video::SColor(255,255,255,255),
- bool loop=false, bool scale=false);
+ bool loop=false, bool scale=false) OVERRIDE_19;
#if (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR <= 8)
void setSprite(gui::EGUI_BUTTON_STATE state, s32 index, video::SColor color, bool loop) override {
@@ -150,16 +161,16 @@ public:
#endif
//! Get the sprite-index for the given state or -1 when no sprite is set
- virtual s32 getSpriteIndex(gui::EGUI_BUTTON_STATE state) const;
+ virtual s32 getSpriteIndex(gui::EGUI_BUTTON_STATE state) const OVERRIDE_19;
//! Get the sprite color for the given state. Color is only used when a sprite is set.
- virtual video::SColor getSpriteColor(gui::EGUI_BUTTON_STATE state) const;
+ virtual video::SColor getSpriteColor(gui::EGUI_BUTTON_STATE state) const OVERRIDE_19;
//! Returns if the sprite in the given state does loop
- virtual bool getSpriteLoop(gui::EGUI_BUTTON_STATE state) const;
+ virtual bool getSpriteLoop(gui::EGUI_BUTTON_STATE state) const OVERRIDE_19;
//! Returns if the sprite in the given state is scaled
- virtual bool getSpriteScale(gui::EGUI_BUTTON_STATE state) const;
+ virtual bool getSpriteScale(gui::EGUI_BUTTON_STATE state) const OVERRIDE_19;
//! Sets if the button should behave like a push button. Which means it
//! can be in two states: Normal or Pressed. With a click on the button,
@@ -199,25 +210,17 @@ public:
virtual bool isScalingImage() const override;
//! Get if the shift key was pressed in last EGET_BUTTON_CLICKED event
- virtual bool getClickShiftState() const
+ virtual bool getClickShiftState() const OVERRIDE_19
{
return ClickShiftState;
}
//! Get if the control key was pressed in last EGET_BUTTON_CLICKED event
- virtual bool getClickControlState() const
+ virtual bool getClickControlState() const OVERRIDE_19
{
return ClickControlState;
}
- //! Writes attributes of the element.
- virtual void serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options) const override;
-
- //! Reads attributes of the element
- virtual void deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options) override;
-
-
-
void setColor(video::SColor color);
// PATCH
//! Set element properties from a StyleSpec corresponding to the button state
diff --git a/src/gui/guiChatConsole.cpp b/src/gui/guiChatConsole.cpp
index ef471106d..01e10ea2e 100644
--- a/src/gui/guiChatConsole.cpp
+++ b/src/gui/guiChatConsole.cpp
@@ -17,6 +17,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
+#include "IrrCompileConfig.h"
#include "guiChatConsole.h"
#include "chat.h"
#include "client/client.h"
@@ -29,17 +30,18 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "client/fontengine.h"
#include "log.h"
#include "gettext.h"
+#include "irrlicht_changes/CGUITTFont.h"
#include <string>
-#if USE_FREETYPE
- #include "irrlicht_changes/CGUITTFont.h"
-#endif
-
inline u32 clamp_u8(s32 value)
{
return (u32) MYMIN(MYMAX(value, 0), 255);
}
+inline bool isInCtrlKeys(const irr::EKEY_CODE& kc)
+{
+ return kc == KEY_LCONTROL || kc == KEY_RCONTROL || kc == KEY_CONTROL;
+}
GUIChatConsole::GUIChatConsole(
gui::IGUIEnvironment* env,
@@ -90,6 +92,10 @@ GUIChatConsole::GUIChatConsole(
// set default cursor options
setCursor(true, true, 2.0, 0.1);
+
+ // track ctrl keys for mouse event
+ m_is_ctrl_down = false;
+ m_cache_clickable_chat_weblinks = g_settings->getBool("clickable_chat_weblinks");
}
GUIChatConsole::~GUIChatConsole()
@@ -319,20 +325,16 @@ void GUIChatConsole::drawText()
core::rect<s32> destrect(
x, y, x + m_fontsize.X * fragment.text.size(), y + m_fontsize.Y);
-#if USE_FREETYPE
if (m_font->getType() == irr::gui::EGFT_CUSTOM) {
- // Draw colored text if FreeType is enabled
- irr::gui::CGUITTFont *tmp = dynamic_cast<irr::gui::CGUITTFont *>(m_font);
+ // Draw colored text if possible
+ gui::CGUITTFont *tmp = static_cast<gui::CGUITTFont*>(m_font);
tmp->draw(
fragment.text,
destrect,
- video::SColor(255, 255, 255, 255),
false,
false,
&AbsoluteClippingRect);
- } else
-#endif
- {
+ } else {
// Otherwise use standard text
m_font->draw(
fragment.text.c_str(),
@@ -405,8 +407,21 @@ bool GUIChatConsole::OnEvent(const SEvent& event)
ChatPrompt &prompt = m_chat_backend->getPrompt();
- if(event.EventType == EET_KEY_INPUT_EVENT && event.KeyInput.PressedDown)
+ if (event.EventType == EET_KEY_INPUT_EVENT && !event.KeyInput.PressedDown)
+ {
+ // CTRL up
+ if (isInCtrlKeys(event.KeyInput.Key))
+ {
+ m_is_ctrl_down = false;
+ }
+ }
+ else if(event.EventType == EET_KEY_INPUT_EVENT && event.KeyInput.PressedDown)
{
+ // CTRL down
+ if (isInCtrlKeys(event.KeyInput.Key)) {
+ m_is_ctrl_down = true;
+ }
+
// Key input
if (KeyPress(event.KeyInput) == getKeySetting("keymap_console")) {
closeConsole();
@@ -559,8 +574,7 @@ bool GUIChatConsole::OnEvent(const SEvent& event)
const c8 *text = os_operator->getTextFromClipboard();
if (!text)
return true;
- std::basic_string<unsigned char> str((const unsigned char*)text);
- prompt.input(std::wstring(str.begin(), str.end()));
+ prompt.input(utf8_to_wide(text));
return true;
}
else if(event.KeyInput.Key == KEY_KEY_X && event.KeyInput.Control)
@@ -607,24 +621,38 @@ bool GUIChatConsole::OnEvent(const SEvent& event)
prompt.nickCompletion(names, backwards);
return true;
} else if (!iswcntrl(event.KeyInput.Char) && !event.KeyInput.Control) {
- #if defined(__linux__) && (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 9)
- wchar_t wc = L'_';
- mbtowc( &wc, (char *) &event.KeyInput.Char, sizeof(event.KeyInput.Char) );
- prompt.input(wc);
- #else
- prompt.input(event.KeyInput.Char);
- #endif
+ prompt.input(event.KeyInput.Char);
return true;
}
}
else if(event.EventType == EET_MOUSE_INPUT_EVENT)
{
- if(event.MouseInput.Event == EMIE_MOUSE_WHEEL)
+ if (event.MouseInput.Event == EMIE_MOUSE_WHEEL)
{
s32 rows = myround(-3.0 * event.MouseInput.Wheel);
m_chat_backend->scroll(rows);
}
+ // Middle click or ctrl-click opens weblink, if enabled in config
+ else if(m_cache_clickable_chat_weblinks && (
+ event.MouseInput.Event == EMIE_MMOUSE_PRESSED_DOWN ||
+ (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN && m_is_ctrl_down)
+ ))
+ {
+ // If clicked within console output region
+ if (event.MouseInput.Y / m_fontsize.Y < (m_height / m_fontsize.Y) - 1 )
+ {
+ // Translate pixel position to font position
+ middleClick(event.MouseInput.X / m_fontsize.X, event.MouseInput.Y / m_fontsize.Y);
+ }
+ }
}
+#if (IRRLICHT_VERSION_MT_REVISION >= 2)
+ else if(event.EventType == EET_STRING_INPUT_EVENT)
+ {
+ prompt.input(std::wstring(event.StringInput.Str->c_str()));
+ return true;
+ }
+#endif
return Parent ? Parent->OnEvent(event) : false;
}
@@ -639,3 +667,62 @@ void GUIChatConsole::setVisible(bool visible)
}
}
+void GUIChatConsole::middleClick(s32 col, s32 row)
+{
+ // Prevent accidental rapid clicking
+ static u64 s_oldtime = 0;
+ u64 newtime = porting::getTimeMs();
+
+ // 0.6 seconds should suffice
+ if (newtime - s_oldtime < 600)
+ return;
+ s_oldtime = newtime;
+
+ const std::vector<ChatFormattedFragment> &
+ frags = m_chat_backend->getConsoleBuffer().getFormattedLine(row).fragments;
+ std::string weblink = ""; // from frag meta
+
+ // Identify targetted fragment, if exists
+ int indx = frags.size() - 1;
+ if (indx < 0) {
+ // Invalid row, frags is empty
+ return;
+ }
+ // Scan from right to left, offset by 1 font space because left margin
+ while (indx > -1 && (u32)col < frags[indx].column + 1) {
+ --indx;
+ }
+ if (indx > -1) {
+ weblink = frags[indx].weblink;
+ // Note if(indx < 0) then a frag somehow had a corrupt column field
+ }
+
+ /*
+ // Debug help. Please keep this in case adjustments are made later.
+ std::string ws;
+ ws = "Middleclick: (" + std::to_string(col) + ',' + std::to_string(row) + ')' + " frags:";
+ // show all frags <position>(<length>) for the clicked row
+ for (u32 i=0;i<frags.size();++i) {
+ if (indx == int(i))
+ // tag the actual clicked frag
+ ws += '*';
+ ws += std::to_string(frags.at(i).column) + '('
+ + std::to_string(frags.at(i).text.size()) + "),";
+ }
+ actionstream << ws << std::endl;
+ */
+
+ // User notification
+ if (weblink.size() != 0) {
+ std::ostringstream msg;
+ msg << " * ";
+ if (porting::open_url(weblink)) {
+ msg << gettext("Opening webpage");
+ }
+ else {
+ msg << gettext("Failed to open webpage");
+ }
+ msg << " '" << weblink << "'";
+ m_chat_backend->addUnparsedMessage(utf8_to_wide(msg.str()));
+ }
+}
diff --git a/src/gui/guiChatConsole.h b/src/gui/guiChatConsole.h
index 896342ab0..32628f0d8 100644
--- a/src/gui/guiChatConsole.h
+++ b/src/gui/guiChatConsole.h
@@ -72,6 +72,8 @@ public:
virtual void setVisible(bool visible);
+ virtual bool acceptsIME() { return true; }
+
private:
void reformatConsole();
void recalculateConsolePosition();
@@ -82,6 +84,9 @@ private:
void drawText();
void drawPrompt();
+ // If clicked fragment has a web url, send it to the system default web browser
+ void middleClick(s32 col, s32 row);
+
private:
ChatBackend* m_chat_backend;
Client* m_client;
@@ -124,4 +129,9 @@ private:
// font
gui::IGUIFont *m_font = nullptr;
v2u32 m_fontsize;
+
+ // Enable clickable chat weblinks
+ bool m_cache_clickable_chat_weblinks;
+ // Track if a ctrl key is currently held down
+ bool m_is_ctrl_down;
};
diff --git a/src/gui/guiConfirmRegistration.cpp b/src/gui/guiConfirmRegistration.cpp
index 4a798c39b..b8887a4af 100644
--- a/src/gui/guiConfirmRegistration.cpp
+++ b/src/gui/guiConfirmRegistration.cpp
@@ -25,9 +25,13 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <IGUIButton.h>
#include <IGUIStaticText.h>
#include <IGUIFont.h>
-#include "intlGUIEditBox.h"
+#include "guiEditBoxWithScrollbar.h"
#include "porting.h"
+#ifdef HAVE_TOUCHSCREENGUI
+ #include "client/renderingengine.h"
+#endif
+
#include "gettext.h"
// Continuing from guiPasswordChange.cpp
@@ -45,7 +49,7 @@ GUIConfirmRegistration::GUIConfirmRegistration(gui::IGUIEnvironment *env,
m_client(client), m_playername(playername), m_password(password),
m_aborted(aborted), m_tsrc(tsrc)
{
-#ifdef __ANDROID__
+#ifdef HAVE_TOUCHSCREENGUI
m_touchscreen_visible = false;
#endif
}
@@ -73,8 +77,8 @@ void GUIConfirmRegistration::regenerateGui(v2u32 screensize)
/*
Calculate new sizes and positions
*/
-#ifdef __ANDROID__
- const float s = m_gui_scale * porting::getDisplayDensity() / 2;
+#ifdef HAVE_TOUCHSCREENGUI
+ const float s = m_gui_scale * RenderingEngine::getDisplayDensity() / 2;
#else
const float s = m_gui_scale;
#endif
@@ -109,10 +113,9 @@ void GUIConfirmRegistration::regenerateGui(v2u32 screensize)
porting::mt_snprintf(info_text_buf, sizeof(info_text_buf),
info_text_template.c_str(), m_playername.c_str());
- wchar_t *info_text_buf_wide = utf8_to_wide_c(info_text_buf);
- gui::IGUIEditBox *e = new gui::intlGUIEditBox(info_text_buf_wide, true,
- Environment, this, ID_intotext, rect2, false, true);
- delete[] info_text_buf_wide;
+ std::wstring info_text_w = utf8_to_wide(info_text_buf);
+ gui::IGUIEditBox *e = new GUIEditBoxWithScrollBar(info_text_w.c_str(),
+ true, Environment, this, ID_intotext, rect2, false, true);
e->drop();
e->setMultiLine(true);
e->setWordWrap(true);
diff --git a/src/gui/guiEditBox.cpp b/src/gui/guiEditBox.cpp
index 79979dbc3..4a0f5013d 100644
--- a/src/gui/guiEditBox.cpp
+++ b/src/gui/guiEditBox.cpp
@@ -19,11 +19,13 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "guiEditBox.h"
+#include "IrrCompileConfig.h"
#include "IGUISkin.h"
#include "IGUIEnvironment.h"
#include "IGUIFont.h"
#include "porting.h"
+#include "util/string.h"
GUIEditBox::~GUIEditBox()
{
@@ -208,35 +210,19 @@ bool GUIEditBox::OnEvent(const SEvent &event)
}
}
break;
- case EET_KEY_INPUT_EVENT: {
-#if (defined(__linux__) || defined(__FreeBSD__)) || defined(__DragonFly__)
- // ################################################################
- // ValkaTR:
- // This part is the difference from the original intlGUIEditBox
- // It converts UTF-8 character into a UCS-2 (wchar_t)
- wchar_t wc = L'_';
- mbtowc(&wc, (char *)&event.KeyInput.Char,
- sizeof(event.KeyInput.Char));
-
- // printf( "char: %lc (%u) \r\n", wc, wc );
-
- SEvent irrevent(event);
- irrevent.KeyInput.Char = wc;
- // ################################################################
-
- if (processKey(irrevent))
- return true;
-#else
+ case EET_KEY_INPUT_EVENT:
if (processKey(event))
return true;
-#endif // defined(linux)
-
break;
- }
case EET_MOUSE_INPUT_EVENT:
if (processMouse(event))
return true;
break;
+#if (IRRLICHT_VERSION_MT_REVISION >= 2)
+ case EET_STRING_INPUT_EVENT:
+ inputString(*event.StringInput.Str);
+ return true;
+#endif
default:
break;
}
@@ -247,10 +233,6 @@ bool GUIEditBox::OnEvent(const SEvent &event)
bool GUIEditBox::processKey(const SEvent &event)
{
- if (!m_writable) {
- return false;
- }
-
if (!event.KeyInput.PressedDown)
return false;
@@ -536,8 +518,7 @@ void GUIEditBox::onKeyControlC(const SEvent &event)
const s32 realmbgn = m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end;
const s32 realmend = m_mark_begin < m_mark_end ? m_mark_end : m_mark_begin;
- core::stringc s;
- s = Text.subString(realmbgn, realmend - realmbgn).c_str();
+ std::string s = stringw_to_utf8(Text.subString(realmbgn, realmend - realmbgn));
m_operator->copyToClipboard(s.c_str());
}
@@ -546,6 +527,9 @@ bool GUIEditBox::onKeyControlX(const SEvent &event, s32 &mark_begin, s32 &mark_e
// First copy to clipboard
onKeyControlC(event);
+ if (!m_writable)
+ return false;
+
if (m_passwordbox || !m_operator || m_mark_begin == m_mark_end)
return false;
@@ -571,7 +555,7 @@ bool GUIEditBox::onKeyControlX(const SEvent &event, s32 &mark_begin, s32 &mark_e
bool GUIEditBox::onKeyControlV(const SEvent &event, s32 &mark_begin, s32 &mark_end)
{
- if (!isEnabled())
+ if (!isEnabled() || !m_writable)
return false;
// paste from the clipboard
@@ -583,29 +567,28 @@ bool GUIEditBox::onKeyControlV(const SEvent &event, s32 &mark_begin, s32 &mark_e
// add new character
if (const c8 *p = m_operator->getTextFromClipboard()) {
+ core::stringw inserted_text = utf8_to_stringw(p);
if (m_mark_begin == m_mark_end) {
// insert text
core::stringw s = Text.subString(0, m_cursor_pos);
- s.append(p);
+ s.append(inserted_text);
s.append(Text.subString(
m_cursor_pos, Text.size() - m_cursor_pos));
if (!m_max || s.size() <= m_max) {
Text = s;
- s = p;
- m_cursor_pos += s.size();
+ m_cursor_pos += inserted_text.size();
}
} else {
// replace text
core::stringw s = Text.subString(0, realmbgn);
- s.append(p);
+ s.append(inserted_text);
s.append(Text.subString(realmend, Text.size() - realmend));
if (!m_max || s.size() <= m_max) {
Text = s;
- s = p;
- m_cursor_pos = realmbgn + s.size();
+ m_cursor_pos = realmbgn + inserted_text.size();
}
}
}
@@ -617,7 +600,7 @@ bool GUIEditBox::onKeyControlV(const SEvent &event, s32 &mark_begin, s32 &mark_e
bool GUIEditBox::onKeyBack(const SEvent &event, s32 &mark_begin, s32 &mark_end)
{
- if (!isEnabled() || Text.empty())
+ if (!isEnabled() || Text.empty() || !m_writable)
return false;
core::stringw s;
@@ -655,7 +638,7 @@ bool GUIEditBox::onKeyBack(const SEvent &event, s32 &mark_begin, s32 &mark_end)
bool GUIEditBox::onKeyDelete(const SEvent &event, s32 &mark_begin, s32 &mark_end)
{
- if (!isEnabled() || Text.empty())
+ if (!isEnabled() || Text.empty() || !m_writable)
return false;
core::stringw s;
@@ -691,39 +674,44 @@ bool GUIEditBox::onKeyDelete(const SEvent &event, s32 &mark_begin, s32 &mark_end
void GUIEditBox::inputChar(wchar_t c)
{
- if (!isEnabled() || !m_writable)
+ if (c == 0)
return;
+ core::stringw s(&c, 1);
+ inputString(s);
+}
- if (c != 0) {
- if (Text.size() < m_max || m_max == 0) {
- core::stringw s;
-
- if (m_mark_begin != m_mark_end) {
- // clang-format off
- // replace marked text
- s32 real_begin = m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end;
- s32 real_end = m_mark_begin < m_mark_end ? m_mark_end : m_mark_begin;
-
- s = Text.subString(0, real_begin);
- s.append(c);
- s.append(Text.subString(real_end, Text.size() - real_end));
- Text = s;
- m_cursor_pos = real_begin + 1;
- // clang-format on
- } else {
- // add new character
- s = Text.subString(0, m_cursor_pos);
- s.append(c);
- s.append(Text.subString(m_cursor_pos,
- Text.size() - m_cursor_pos));
- Text = s;
- ++m_cursor_pos;
- }
+void GUIEditBox::inputString(const core::stringw &str)
+{
+ if (!isEnabled() || !m_writable)
+ return;
- m_blink_start_time = porting::getTimeMs();
- setTextMarkers(0, 0);
+ u32 len = str.size();
+ if (Text.size()+len <= m_max || m_max == 0) {
+ core::stringw s;
+ if (m_mark_begin != m_mark_end) {
+ // replace marked text
+ s32 real_begin = m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end;
+ s32 real_end = m_mark_begin < m_mark_end ? m_mark_end : m_mark_begin;
+
+ s = Text.subString(0, real_begin);
+ s.append(str);
+ s.append(Text.subString(real_end, Text.size() - real_end));
+ Text = s;
+ m_cursor_pos = real_begin + len;
+ } else {
+ // append string
+ s = Text.subString(0, m_cursor_pos);
+ s.append(str);
+ s.append(Text.subString(m_cursor_pos,
+ Text.size() - m_cursor_pos));
+ Text = s;
+ m_cursor_pos += len;
}
+
+ m_blink_start_time = porting::getTimeMs();
+ setTextMarkers(0, 0);
}
+
breakText();
sendGuiEvent(EGET_EDITBOX_CHANGED);
calculateScrollPos();
@@ -858,54 +846,3 @@ void GUIEditBox::updateVScrollBar()
}
}
}
-
-void GUIEditBox::deserializeAttributes(
- io::IAttributes *in, io::SAttributeReadWriteOptions *options = 0)
-{
- IGUIEditBox::deserializeAttributes(in, options);
-
- setOverrideColor(in->getAttributeAsColor("OverrideColor"));
- enableOverrideColor(in->getAttributeAsBool("OverrideColorEnabled"));
- setMax(in->getAttributeAsInt("MaxChars"));
- setWordWrap(in->getAttributeAsBool("WordWrap"));
- setMultiLine(in->getAttributeAsBool("MultiLine"));
- setAutoScroll(in->getAttributeAsBool("AutoScroll"));
- core::stringw ch = in->getAttributeAsStringW("PasswordChar");
-
- if (ch.empty())
- setPasswordBox(in->getAttributeAsBool("PasswordBox"));
- else
- setPasswordBox(in->getAttributeAsBool("PasswordBox"), ch[0]);
-
- setTextAlignment((EGUI_ALIGNMENT)in->getAttributeAsEnumeration(
- "HTextAlign", GUIAlignmentNames),
- (EGUI_ALIGNMENT)in->getAttributeAsEnumeration(
- "VTextAlign", GUIAlignmentNames));
-
- setWritable(in->getAttributeAsBool("Writable"));
- // setOverrideFont(in->getAttributeAsFont("OverrideFont"));
-}
-
-//! Writes attributes of the element.
-void GUIEditBox::serializeAttributes(
- io::IAttributes *out, io::SAttributeReadWriteOptions *options = 0) const
-{
- // IGUIEditBox::serializeAttributes(out,options);
-
- out->addBool("OverrideColorEnabled", m_override_color_enabled);
- out->addColor("OverrideColor", m_override_color);
- // out->addFont("OverrideFont",m_override_font);
- out->addInt("MaxChars", m_max);
- out->addBool("WordWrap", m_word_wrap);
- out->addBool("MultiLine", m_multiline);
- out->addBool("AutoScroll", m_autoscroll);
- out->addBool("PasswordBox", m_passwordbox);
- core::stringw ch = L" ";
- ch[0] = m_passwordchar;
- out->addString("PasswordChar", ch.c_str());
- out->addEnum("HTextAlign", m_halign, GUIAlignmentNames);
- out->addEnum("VTextAlign", m_valign, GUIAlignmentNames);
- out->addBool("Writable", m_writable);
-
- IGUIEditBox::serializeAttributes(out, options);
-}
diff --git a/src/gui/guiEditBox.h b/src/gui/guiEditBox.h
index c616d75d1..4c7413f54 100644
--- a/src/gui/guiEditBox.h
+++ b/src/gui/guiEditBox.h
@@ -130,13 +130,7 @@ public:
//! called if an event happened.
virtual bool OnEvent(const SEvent &event);
- //! Writes attributes of the element.
- virtual void serializeAttributes(io::IAttributes *out,
- io::SAttributeReadWriteOptions *options) const;
-
- //! Reads attributes of the element
- virtual void deserializeAttributes(
- io::IAttributes *in, io::SAttributeReadWriteOptions *options);
+ virtual bool acceptsIME() { return isEnabled() && m_writable; };
protected:
virtual void breakText() = 0;
@@ -156,6 +150,7 @@ protected:
virtual s32 getCursorPos(s32 x, s32 y) = 0;
bool processKey(const SEvent &event);
+ virtual void inputString(const core::stringw &str);
virtual void inputChar(wchar_t c);
//! returns the line number that the cursor is on
diff --git a/src/gui/guiEditBoxWithScrollbar.cpp b/src/gui/guiEditBoxWithScrollbar.cpp
index c72070787..1b7f7832a 100644
--- a/src/gui/guiEditBoxWithScrollbar.cpp
+++ b/src/gui/guiEditBoxWithScrollbar.cpp
@@ -620,6 +620,17 @@ void GUIEditBoxWithScrollBar::createVScrollBar()
if (Environment)
skin = Environment->getSkin();
+ s32 fontHeight = 1;
+
+ if (m_override_font) {
+ fontHeight = m_override_font->getDimension(L"Ay").Height;
+ } else {
+ IGUIFont *font;
+ if (skin && (font = skin->getFont())) {
+ fontHeight = font->getDimension(L"Ay").Height;
+ }
+ }
+
m_scrollbar_width = skin ? skin->getSize(gui::EGDS_SCROLLBAR_SIZE) : 16;
irr::core::rect<s32> scrollbarrect = m_frame_rect;
@@ -628,8 +639,8 @@ void GUIEditBoxWithScrollBar::createVScrollBar()
scrollbarrect, false, true);
m_vscrollbar->setVisible(false);
- m_vscrollbar->setSmallStep(1);
- m_vscrollbar->setLargeStep(1);
+ m_vscrollbar->setSmallStep(3 * fontHeight);
+ m_vscrollbar->setLargeStep(10 * fontHeight);
}
@@ -641,26 +652,6 @@ void GUIEditBoxWithScrollBar::setBackgroundColor(const video::SColor &bg_color)
m_bg_color_used = true;
}
-//! Writes attributes of the element.
-void GUIEditBoxWithScrollBar::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options = 0) const
-{
- out->addBool("Border", m_border);
- out->addBool("Background", m_background);
- // out->addFont("OverrideFont", OverrideFont);
-
- GUIEditBox::serializeAttributes(out, options);
-}
-
-
-//! Reads attributes of the element
-void GUIEditBoxWithScrollBar::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options = 0)
-{
- GUIEditBox::deserializeAttributes(in, options);
-
- setDrawBorder(in->getAttributeAsBool("Border"));
- setDrawBackground(in->getAttributeAsBool("Background"));
-}
-
bool GUIEditBoxWithScrollBar::isDrawBackgroundEnabled() const { return false; }
bool GUIEditBoxWithScrollBar::isDrawBorderEnabled() const { return false; }
void GUIEditBoxWithScrollBar::setCursorChar(const wchar_t cursorChar) { }
diff --git a/src/gui/guiEditBoxWithScrollbar.h b/src/gui/guiEditBoxWithScrollbar.h
index 3f7450dcb..cea482fc2 100644
--- a/src/gui/guiEditBoxWithScrollbar.h
+++ b/src/gui/guiEditBoxWithScrollbar.h
@@ -31,12 +31,6 @@ public:
//! Change the background color
virtual void setBackgroundColor(const video::SColor &bg_color);
- //! Writes attributes of the element.
- virtual void serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options) const;
-
- //! Reads attributes of the element
- virtual void deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options);
-
virtual bool isDrawBackgroundEnabled() const;
virtual bool isDrawBorderEnabled() const;
virtual void setCursorChar(const wchar_t cursorChar);
diff --git a/src/gui/guiEngine.cpp b/src/gui/guiEngine.cpp
index 93463ad70..b65b31304 100644
--- a/src/gui/guiEngine.cpp
+++ b/src/gui/guiEngine.cpp
@@ -104,16 +104,22 @@ void MenuMusicFetcher::fetchSounds(const std::string &name,
if(m_fetched.count(name))
return;
m_fetched.insert(name);
- std::string base;
- base = porting::path_share + DIR_DELIM + "sounds";
- dst_paths.insert(base + DIR_DELIM + name + ".ogg");
- int i;
- for(i=0; i<10; i++)
- dst_paths.insert(base + DIR_DELIM + name + "."+itos(i)+".ogg");
- base = porting::path_user + DIR_DELIM + "sounds";
- dst_paths.insert(base + DIR_DELIM + name + ".ogg");
- for(i=0; i<10; i++)
- dst_paths.insert(base + DIR_DELIM + name + "."+itos(i)+".ogg");
+ std::vector<fs::DirListNode> list;
+ // Reusable local function
+ auto add_paths = [&dst_paths](const std::string name, const std::string base = "") {
+ dst_paths.insert(base + name + ".ogg");
+ for (int i = 0; i < 10; i++)
+ dst_paths.insert(base + name + "." + itos(i) + ".ogg");
+ };
+ // Allow full paths
+ if (name.find(DIR_DELIM_CHAR) != std::string::npos) {
+ add_paths(name);
+ } else {
+ std::string share_prefix = porting::path_share + DIR_DELIM;
+ add_paths(name, share_prefix + "sounds" + DIR_DELIM);
+ std::string user_prefix = porting::path_user + DIR_DELIM;
+ add_paths(name, user_prefix + "sounds" + DIR_DELIM);
+ }
}
/******************************************************************************/
@@ -121,12 +127,14 @@ void MenuMusicFetcher::fetchSounds(const std::string &name,
/******************************************************************************/
GUIEngine::GUIEngine(JoystickController *joystick,
gui::IGUIElement *parent,
+ RenderingEngine *rendering_engine,
IMenuManager *menumgr,
MainMenuData *data,
bool &kill) :
+ m_rendering_engine(rendering_engine),
m_parent(parent),
m_menumanager(menumgr),
- m_smgr(RenderingEngine::get_scene_manager()),
+ m_smgr(rendering_engine->get_scene_manager()),
m_data(data),
m_kill(kill)
{
@@ -138,7 +146,7 @@ GUIEngine::GUIEngine(JoystickController *joystick,
m_buttonhandler = new TextDestGuiEngine(this);
//create texture source
- m_texture_source = new MenuTextureSource(RenderingEngine::get_video_driver());
+ m_texture_source = new MenuTextureSource(rendering_engine->get_video_driver());
//create soundmanager
MenuMusicFetcher soundfetcher;
@@ -156,7 +164,7 @@ GUIEngine::GUIEngine(JoystickController *joystick,
g_fontengine->getTextHeight());
rect += v2s32(4, 0);
- m_irr_toplefttext = gui::StaticText::add(RenderingEngine::get_gui_env(),
+ m_irr_toplefttext = gui::StaticText::add(rendering_engine->get_gui_env(),
m_toplefttext, rect, false, true, 0, -1);
//create formspecsource
@@ -168,6 +176,7 @@ GUIEngine::GUIEngine(JoystickController *joystick,
-1,
m_menumanager,
NULL /* &client */,
+ m_rendering_engine->get_gui_env(),
m_texture_source,
m_sound_manager,
m_formspecgui,
@@ -232,7 +241,7 @@ void GUIEngine::run()
{
// Always create clouds because they may or may not be
// needed based on the game selected
- video::IVideoDriver *driver = RenderingEngine::get_video_driver();
+ video::IVideoDriver *driver = m_rendering_engine->get_video_driver();
cloudInit();
@@ -259,10 +268,10 @@ void GUIEngine::run()
fog_pixelfog, fog_rangefog);
}
- while (RenderingEngine::run() && (!m_startgame) && (!m_kill)) {
+ while (m_rendering_engine->run() && (!m_startgame) && (!m_kill)) {
const irr::core::dimension2d<u32> &current_screen_size =
- RenderingEngine::get_video_driver()->getScreenSize();
+ m_rendering_engine->get_video_driver()->getScreenSize();
// Verify if window size has changed and save it if it's the case
// Ensure evaluating settings->getBool after verifying screensize
// First condition is cheaper
@@ -293,11 +302,11 @@ void GUIEngine::run()
drawHeader(driver);
drawFooter(driver);
- RenderingEngine::get_gui_env()->drawAll();
+ m_rendering_engine->get_gui_env()->drawAll();
driver->endScene();
- IrrlichtDevice *device = RenderingEngine::get_raw_device();
+ IrrlichtDevice *device = m_rendering_engine->get_raw_device();
u32 frametime_min = 1000 / (device->isWindowFocused()
? g_settings->getFloat("fps_max")
: g_settings->getFloat("fps_max_unfocused"));
@@ -330,7 +339,7 @@ GUIEngine::~GUIEngine()
//clean up texture pointers
for (image_definition &texture : m_textures) {
if (texture.texture)
- RenderingEngine::get_video_driver()->removeTexture(texture.texture);
+ m_rendering_engine->get_video_driver()->removeTexture(texture.texture);
}
delete m_texture_source;
@@ -350,13 +359,13 @@ void GUIEngine::cloudInit()
v3f(0,0,0), v3f(0, 60, 100));
m_cloud.camera->setFarValue(10000);
- m_cloud.lasttime = RenderingEngine::get_timer_time();
+ m_cloud.lasttime = m_rendering_engine->get_timer_time();
}
/******************************************************************************/
void GUIEngine::cloudPreProcess()
{
- u32 time = RenderingEngine::get_timer_time();
+ u32 time = m_rendering_engine->get_timer_time();
if(time > m_cloud.lasttime)
m_cloud.dtime = (time - m_cloud.lasttime) / 1000.0;
@@ -377,7 +386,7 @@ void GUIEngine::cloudPostProcess(u32 frametime_min, IrrlichtDevice *device)
u32 busytime_u32;
// not using getRealTime is necessary for wine
- u32 time = RenderingEngine::get_timer_time();
+ u32 time = m_rendering_engine->get_timer_time();
if(time > m_cloud.lasttime)
busytime_u32 = time - m_cloud.lasttime;
else
@@ -434,9 +443,22 @@ void GUIEngine::drawBackground(video::IVideoDriver *driver)
return;
}
+ // Chop background image to the smaller screen dimension
+ v2u32 bg_size = screensize;
+ v2f32 scale(
+ (f32) bg_size.X / sourcesize.X,
+ (f32) bg_size.Y / sourcesize.Y);
+ if (scale.X < scale.Y)
+ bg_size.X = (int) (scale.Y * sourcesize.X);
+ else
+ bg_size.Y = (int) (scale.X * sourcesize.Y);
+ v2s32 offset = v2s32(
+ (s32) screensize.X - (s32) bg_size.X,
+ (s32) screensize.Y - (s32) bg_size.Y
+ ) / 2;
/* Draw background texture */
draw2DImageFilterScaled(driver, texture,
- core::rect<s32>(0, 0, screensize.X, screensize.Y),
+ core::rect<s32>(offset.X, offset.Y, bg_size.X + offset.X, bg_size.Y + offset.Y),
core::rect<s32>(0, 0, sourcesize.X, sourcesize.Y),
NULL, NULL, true);
}
@@ -528,7 +550,7 @@ void GUIEngine::drawFooter(video::IVideoDriver *driver)
bool GUIEngine::setTexture(texture_layer layer, const std::string &texturepath,
bool tile_image, unsigned int minsize)
{
- video::IVideoDriver *driver = RenderingEngine::get_video_driver();
+ video::IVideoDriver *driver = m_rendering_engine->get_video_driver();
if (m_textures[layer].texture) {
driver->removeTexture(m_textures[layer].texture);
@@ -595,7 +617,7 @@ void GUIEngine::updateTopLeftTextSize()
rect += v2s32(4, 0);
m_irr_toplefttext->remove();
- m_irr_toplefttext = gui::StaticText::add(RenderingEngine::get_gui_env(),
+ m_irr_toplefttext = gui::StaticText::add(m_rendering_engine->get_gui_env(),
m_toplefttext, rect, false, true, 0, -1);
}
@@ -611,10 +633,3 @@ void GUIEngine::stopSound(s32 handle)
{
m_sound_manager->stopSound(handle);
}
-
-/******************************************************************************/
-unsigned int GUIEngine::queueAsync(const std::string &serialized_func,
- const std::string &serialized_params)
-{
- return m_script->queueAsync(serialized_func, serialized_params);
-}
diff --git a/src/gui/guiEngine.h b/src/gui/guiEngine.h
index eef1ad8aa..d7e6485ef 100644
--- a/src/gui/guiEngine.h
+++ b/src/gui/guiEngine.h
@@ -50,6 +50,7 @@ struct image_definition {
/* forward declarations */
/******************************************************************************/
class GUIEngine;
+class RenderingEngine;
class MainMenuScripting;
class Clouds;
struct MainMenuData;
@@ -150,6 +151,7 @@ public:
*/
GUIEngine(JoystickController *joystick,
gui::IGUIElement *parent,
+ RenderingEngine *rendering_engine,
IMenuManager *menumgr,
MainMenuData *data,
bool &kill);
@@ -173,10 +175,6 @@ public:
return m_scriptdir;
}
- /** pass async callback to scriptengine **/
- unsigned int queueAsync(const std::string &serialized_fct,
- const std::string &serialized_params);
-
private:
/** find and run the main menu script */
@@ -188,6 +186,7 @@ private:
/** update size of topleftext element */
void updateTopLeftTextSize();
+ RenderingEngine *m_rendering_engine = nullptr;
/** parent gui element */
gui::IGUIElement *m_parent = nullptr;
/** manager to add menus to */
diff --git a/src/gui/guiFormSpecMenu.cpp b/src/gui/guiFormSpecMenu.cpp
index 5aa6dc9ae..85bd04900 100644
--- a/src/gui/guiFormSpecMenu.cpp
+++ b/src/gui/guiFormSpecMenu.cpp
@@ -65,7 +65,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "guiInventoryList.h"
#include "guiItemImage.h"
#include "guiScrollContainer.h"
-#include "intlGUIEditBox.h"
#include "guiHyperText.h"
#include "guiScene.h"
@@ -82,6 +81,13 @@ with this program; if not, write to the Free Software Foundation, Inc.,
" specified: \"" << parts[b] << "\"" << std::endl; \
return; \
}
+
+#define MY_CHECKCLIENT(a) \
+ if (!m_client) { \
+ errorstream << "Attempted to use element " << a << " with m_client == nullptr." << std::endl; \
+ return; \
+ }
+
/*
GUIFormSpecMenu
*/
@@ -97,10 +103,10 @@ inline u32 clamp_u8(s32 value)
GUIFormSpecMenu::GUIFormSpecMenu(JoystickController *joystick,
gui::IGUIElement *parent, s32 id, IMenuManager *menumgr,
- Client *client, ISimpleTextureSource *tsrc, ISoundManager *sound_manager,
- IFormSource *fsrc, TextDest *tdst,
+ Client *client, gui::IGUIEnvironment *guienv, ISimpleTextureSource *tsrc,
+ ISoundManager *sound_manager, IFormSource *fsrc, TextDest *tdst,
const std::string &formspecPrepend, bool remap_dbl_click):
- GUIModalMenu(RenderingEngine::get_gui_env(), parent, id, menumgr, remap_dbl_click),
+ GUIModalMenu(guienv, parent, id, menumgr, remap_dbl_click),
m_invmgr(client),
m_tsrc(tsrc),
m_sound_manager(sound_manager),
@@ -146,12 +152,12 @@ GUIFormSpecMenu::~GUIFormSpecMenu()
}
void GUIFormSpecMenu::create(GUIFormSpecMenu *&cur_formspec, Client *client,
- JoystickController *joystick, IFormSource *fs_src, TextDest *txt_dest,
- const std::string &formspecPrepend, ISoundManager *sound_manager)
+ gui::IGUIEnvironment *guienv, JoystickController *joystick, IFormSource *fs_src,
+ TextDest *txt_dest, const std::string &formspecPrepend, ISoundManager *sound_manager)
{
if (cur_formspec == nullptr) {
cur_formspec = new GUIFormSpecMenu(joystick, guiroot, -1, &g_menumgr,
- client, client->getTextureSource(), sound_manager, fs_src,
+ client, guienv, client->getTextureSource(), sound_manager, fs_src,
txt_dest, formspecPrepend);
cur_formspec->doPause = false;
@@ -295,8 +301,20 @@ v2s32 GUIFormSpecMenu::getRealCoordinateGeometry(const std::vector<std::string>
return v2s32(stof(v_geom[0]) * imgsize.X, stof(v_geom[1]) * imgsize.Y);
}
+bool GUIFormSpecMenu::precheckElement(const std::string &name, const std::string &element,
+ size_t args_min, size_t args_max, std::vector<std::string> &parts)
+{
+ parts = split(element, ';');
+ if (parts.size() >= args_min && (parts.size() <= args_max || m_formspec_version > FORMSPEC_API_VERSION))
+ return true;
+
+ errorstream << "Invalid " << name << " element(" << parts.size() << "): '" << element << "'" << std::endl;
+ return false;
+}
+
void GUIFormSpecMenu::parseSize(parserData* data, const std::string &element)
{
+ // Note: do not use precheckElement due to "," separator.
std::vector<std::string> parts = split(element,',');
if (((parts.size() == 2) || parts.size() == 3) ||
@@ -309,7 +327,7 @@ void GUIFormSpecMenu::parseSize(parserData* data, const std::string &element)
data->invsize.Y = MYMAX(0, stof(parts[1]));
lockSize(false);
-#ifndef __ANDROID__
+#ifndef HAVE_TOUCHSCREENGUI
if (parts.size() == 3) {
if (parts[2] == "true") {
lockSize(true,v2u32(800,600));
@@ -350,14 +368,9 @@ void GUIFormSpecMenu::parseContainerEnd(parserData* data)
void GUIFormSpecMenu::parseScrollContainer(parserData *data, const std::string &element)
{
- std::vector<std::string> parts = split(element, ';');
-
- if (parts.size() < 4 ||
- (parts.size() > 5 && m_formspec_version <= FORMSPEC_API_VERSION)) {
- errorstream << "Invalid scroll_container start element (" << parts.size()
- << "): '" << element << "'" << std::endl;
+ std::vector<std::string> parts;
+ if (!precheckElement("scroll_container start", element, 4, 5, parts))
return;
- }
std::vector<std::string> v_pos = split(parts[0], ',');
std::vector<std::string> v_geom = split(parts[1], ',');
@@ -446,105 +459,95 @@ void GUIFormSpecMenu::parseScrollContainerEnd(parserData *data)
void GUIFormSpecMenu::parseList(parserData *data, const std::string &element)
{
- if (m_client == 0) {
- warningstream<<"invalid use of 'list' with m_client==0"<<std::endl;
- return;
- }
+ MY_CHECKCLIENT("list");
- std::vector<std::string> parts = split(element,';');
+ std::vector<std::string> parts;
+ if (!precheckElement("list", element, 4, 5, parts))
+ return;
- if (((parts.size() == 4) || (parts.size() == 5)) ||
- ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
- {
- std::string location = parts[0];
- std::string listname = parts[1];
- std::vector<std::string> v_pos = split(parts[2],',');
- std::vector<std::string> v_geom = split(parts[3],',');
- std::string startindex;
- if (parts.size() == 5)
- startindex = parts[4];
+ std::string location = parts[0];
+ std::string listname = parts[1];
+ std::vector<std::string> v_pos = split(parts[2],',');
+ std::vector<std::string> v_geom = split(parts[3],',');
+ std::string startindex;
+ if (parts.size() == 5)
+ startindex = parts[4];
- MY_CHECKPOS("list",2);
- MY_CHECKGEOM("list",3);
+ MY_CHECKPOS("list",2);
+ MY_CHECKGEOM("list",3);
- InventoryLocation loc;
+ InventoryLocation loc;
- if (location == "context" || location == "current_name")
- loc = m_current_inventory_location;
- else
- loc.deSerialize(location);
+ if (location == "context" || location == "current_name")
+ loc = m_current_inventory_location;
+ else
+ loc.deSerialize(location);
- v2s32 geom;
- geom.X = stoi(v_geom[0]);
- geom.Y = stoi(v_geom[1]);
+ v2s32 geom;
+ geom.X = stoi(v_geom[0]);
+ geom.Y = stoi(v_geom[1]);
- s32 start_i = 0;
- if (!startindex.empty())
- start_i = stoi(startindex);
+ s32 start_i = 0;
+ if (!startindex.empty())
+ start_i = stoi(startindex);
- if (geom.X < 0 || geom.Y < 0 || start_i < 0) {
- errorstream << "Invalid list element: '" << element << "'" << std::endl;
- return;
- }
+ if (geom.X < 0 || geom.Y < 0 || start_i < 0) {
+ errorstream << "Invalid list element: '" << element << "'" << std::endl;
+ return;
+ }
- if (!data->explicit_size)
- warningstream << "invalid use of list without a size[] element" << std::endl;
+ if (!data->explicit_size)
+ warningstream << "invalid use of list without a size[] element" << std::endl;
- FieldSpec spec(
- "",
- L"",
- L"",
- 258 + m_fields.size(),
- 3
- );
+ FieldSpec spec(
+ "",
+ L"",
+ L"",
+ 258 + m_fields.size(),
+ 3
+ );
- auto style = getDefaultStyleForElement("list", spec.fname);
+ auto style = getDefaultStyleForElement("list", spec.fname);
- v2f32 slot_scale = style.getVector2f(StyleSpec::SIZE, v2f32(0, 0));
- v2f32 slot_size(
- slot_scale.X <= 0 ? imgsize.X : std::max<f32>(slot_scale.X * imgsize.X, 1),
- slot_scale.Y <= 0 ? imgsize.Y : std::max<f32>(slot_scale.Y * imgsize.Y, 1)
- );
+ v2f32 slot_scale = style.getVector2f(StyleSpec::SIZE, v2f32(0, 0));
+ v2f32 slot_size(
+ slot_scale.X <= 0 ? imgsize.X : std::max<f32>(slot_scale.X * imgsize.X, 1),
+ slot_scale.Y <= 0 ? imgsize.Y : std::max<f32>(slot_scale.Y * imgsize.Y, 1)
+ );
- v2f32 slot_spacing = style.getVector2f(StyleSpec::SPACING, v2f32(-1, -1));
- v2f32 default_spacing = data->real_coordinates ?
- v2f32(imgsize.X * 0.25f, imgsize.Y * 0.25f) :
- v2f32(spacing.X - imgsize.X, spacing.Y - imgsize.Y);
+ v2f32 slot_spacing = style.getVector2f(StyleSpec::SPACING, v2f32(-1, -1));
+ v2f32 default_spacing = data->real_coordinates ?
+ v2f32(imgsize.X * 0.25f, imgsize.Y * 0.25f) :
+ v2f32(spacing.X - imgsize.X, spacing.Y - imgsize.Y);
- slot_spacing.X = slot_spacing.X < 0 ? default_spacing.X :
- imgsize.X * slot_spacing.X;
- slot_spacing.Y = slot_spacing.Y < 0 ? default_spacing.Y :
- imgsize.Y * slot_spacing.Y;
+ slot_spacing.X = slot_spacing.X < 0 ? default_spacing.X :
+ imgsize.X * slot_spacing.X;
+ slot_spacing.Y = slot_spacing.Y < 0 ? default_spacing.Y :
+ imgsize.Y * slot_spacing.Y;
- slot_spacing += slot_size;
+ slot_spacing += slot_size;
- v2s32 pos = data->real_coordinates ? getRealCoordinateBasePos(v_pos) :
- getElementBasePos(&v_pos);
+ v2s32 pos = data->real_coordinates ? getRealCoordinateBasePos(v_pos) :
+ getElementBasePos(&v_pos);
- core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y,
- pos.X + (geom.X - 1) * slot_spacing.X + slot_size.X,
- pos.Y + (geom.Y - 1) * slot_spacing.Y + slot_size.Y);
+ core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y,
+ pos.X + (geom.X - 1) * slot_spacing.X + slot_size.X,
+ pos.Y + (geom.Y - 1) * slot_spacing.Y + slot_size.Y);
- GUIInventoryList *e = new GUIInventoryList(Environment, data->current_parent,
- spec.fid, rect, m_invmgr, loc, listname, geom, start_i,
- v2s32(slot_size.X, slot_size.Y), slot_spacing, this,
- data->inventorylist_options, m_font);
+ GUIInventoryList *e = new GUIInventoryList(Environment, data->current_parent,
+ spec.fid, rect, m_invmgr, loc, listname, geom, start_i,
+ v2s32(slot_size.X, slot_size.Y), slot_spacing, this,
+ data->inventorylist_options, m_font);
- e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
+ e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
- m_inventorylists.push_back(e);
- m_fields.push_back(spec);
- return;
- }
- errorstream<< "Invalid list element(" << parts.size() << "): '" << element << "'" << std::endl;
+ m_inventorylists.push_back(e);
+ m_fields.push_back(spec);
}
void GUIFormSpecMenu::parseListRing(parserData *data, const std::string &element)
{
- if (m_client == 0) {
- errorstream << "WARNING: invalid use of 'listring' with m_client==0" << std::endl;
- return;
- }
+ MY_CHECKCLIENT("listring");
std::vector<std::string> parts = split(element, ';');
@@ -579,157 +582,150 @@ void GUIFormSpecMenu::parseListRing(parserData *data, const std::string &element
void GUIFormSpecMenu::parseCheckbox(parserData* data, const std::string &element)
{
- std::vector<std::string> parts = split(element,';');
-
- if (((parts.size() >= 3) && (parts.size() <= 4)) ||
- ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
- {
- std::vector<std::string> v_pos = split(parts[0],',');
- std::string name = parts[1];
- std::string label = parts[2];
- std::string selected;
+ std::vector<std::string> parts;
+ if (!precheckElement("checkbox", element, 3, 4, parts))
+ return;
- if (parts.size() >= 4)
- selected = parts[3];
+ std::vector<std::string> v_pos = split(parts[0],',');
+ std::string name = parts[1];
+ std::string label = parts[2];
+ std::string selected;
- MY_CHECKPOS("checkbox",0);
+ if (parts.size() >= 4)
+ selected = parts[3];
- bool fselected = false;
+ MY_CHECKPOS("checkbox",0);
- if (selected == "true")
- fselected = true;
+ bool fselected = false;
- std::wstring wlabel = translate_string(utf8_to_wide(unescape_string(label)));
- const core::dimension2d<u32> label_size = m_font->getDimension(wlabel.c_str());
- s32 cb_size = Environment->getSkin()->getSize(gui::EGDS_CHECK_BOX_WIDTH);
- s32 y_center = (std::max(label_size.Height, (u32)cb_size) + 1) / 2;
+ if (selected == "true")
+ fselected = true;
- v2s32 pos;
- core::rect<s32> rect;
+ std::wstring wlabel = translate_string(utf8_to_wide(unescape_string(label)));
+ const core::dimension2d<u32> label_size = m_font->getDimension(wlabel.c_str());
+ s32 cb_size = Environment->getSkin()->getSize(gui::EGDS_CHECK_BOX_WIDTH);
+ s32 y_center = (std::max(label_size.Height, (u32)cb_size) + 1) / 2;
- if (data->real_coordinates) {
- pos = getRealCoordinateBasePos(v_pos);
+ v2s32 pos;
+ core::rect<s32> rect;
- rect = core::rect<s32>(
- pos.X,
- pos.Y - y_center,
- pos.X + label_size.Width + cb_size + 7,
- pos.Y + y_center
- );
- } else {
- pos = getElementBasePos(&v_pos);
- rect = core::rect<s32>(
- pos.X,
- pos.Y + imgsize.Y / 2 - y_center,
- pos.X + label_size.Width + cb_size + 7,
- pos.Y + imgsize.Y / 2 + y_center
- );
- }
+ if (data->real_coordinates) {
+ pos = getRealCoordinateBasePos(v_pos);
- FieldSpec spec(
- name,
- wlabel, //Needed for displaying text on MSVC
- wlabel,
- 258+m_fields.size()
+ rect = core::rect<s32>(
+ pos.X,
+ pos.Y - y_center,
+ pos.X + label_size.Width + cb_size + 7,
+ pos.Y + y_center
+ );
+ } else {
+ pos = getElementBasePos(&v_pos);
+ rect = core::rect<s32>(
+ pos.X,
+ pos.Y + imgsize.Y / 2 - y_center,
+ pos.X + label_size.Width + cb_size + 7,
+ pos.Y + imgsize.Y / 2 + y_center
);
+ }
- spec.ftype = f_CheckBox;
+ FieldSpec spec(
+ name,
+ wlabel, //Needed for displaying text on MSVC
+ wlabel,
+ 258+m_fields.size()
+ );
- gui::IGUICheckBox *e = Environment->addCheckBox(fselected, rect,
- data->current_parent, spec.fid, spec.flabel.c_str());
+ spec.ftype = f_CheckBox;
- auto style = getDefaultStyleForElement("checkbox", name);
+ gui::IGUICheckBox *e = Environment->addCheckBox(fselected, rect,
+ data->current_parent, spec.fid, spec.flabel.c_str());
- spec.sound = style.get(StyleSpec::Property::SOUND, "");
+ auto style = getDefaultStyleForElement("checkbox", name);
- e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
+ spec.sound = style.get(StyleSpec::Property::SOUND, "");
- if (spec.fname == m_focused_element) {
- Environment->setFocus(e);
- }
+ e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
- e->grab();
- m_checkboxes.emplace_back(spec, e);
- m_fields.push_back(spec);
- return;
+ if (spec.fname == m_focused_element) {
+ Environment->setFocus(e);
}
- errorstream<< "Invalid checkbox element(" << parts.size() << "): '" << element << "'" << std::endl;
+
+ e->grab();
+ m_checkboxes.emplace_back(spec, e);
+ m_fields.push_back(spec);
}
void GUIFormSpecMenu::parseScrollBar(parserData* data, const std::string &element)
{
- std::vector<std::string> parts = split(element,';');
+ std::vector<std::string> parts;
+ if (!precheckElement("scrollbar", element, 5, 5, parts))
+ return;
- if (parts.size() >= 5) {
- std::vector<std::string> v_pos = split(parts[0],',');
- std::vector<std::string> v_geom = split(parts[1],',');
- std::string name = parts[3];
- std::string value = parts[4];
+ std::vector<std::string> v_pos = split(parts[0],',');
+ std::vector<std::string> v_geom = split(parts[1],',');
+ std::string name = parts[3];
+ std::string value = parts[4];
- MY_CHECKPOS("scrollbar",0);
- MY_CHECKGEOM("scrollbar",1);
+ MY_CHECKPOS("scrollbar",0);
+ MY_CHECKGEOM("scrollbar",1);
- v2s32 pos;
- v2s32 dim;
+ v2s32 pos;
+ v2s32 dim;
- if (data->real_coordinates) {
- pos = getRealCoordinateBasePos(v_pos);
- dim = getRealCoordinateGeometry(v_geom);
- } else {
- pos = getElementBasePos(&v_pos);
- dim.X = stof(v_geom[0]) * spacing.X;
- dim.Y = stof(v_geom[1]) * spacing.Y;
- }
+ if (data->real_coordinates) {
+ pos = getRealCoordinateBasePos(v_pos);
+ dim = getRealCoordinateGeometry(v_geom);
+ } else {
+ pos = getElementBasePos(&v_pos);
+ dim.X = stof(v_geom[0]) * spacing.X;
+ dim.Y = stof(v_geom[1]) * spacing.Y;
+ }
- core::rect<s32> rect =
- core::rect<s32>(pos.X, pos.Y, pos.X + dim.X, pos.Y + dim.Y);
+ core::rect<s32> rect =
+ core::rect<s32>(pos.X, pos.Y, pos.X + dim.X, pos.Y + dim.Y);
- FieldSpec spec(
- name,
- L"",
- L"",
- 258+m_fields.size()
- );
+ FieldSpec spec(
+ name,
+ L"",
+ L"",
+ 258+m_fields.size()
+ );
- bool is_horizontal = true;
+ bool is_horizontal = true;
- if (parts[2] == "vertical")
- is_horizontal = false;
+ if (parts[2] == "vertical")
+ is_horizontal = false;
- spec.ftype = f_ScrollBar;
- spec.send = true;
- GUIScrollBar *e = new GUIScrollBar(Environment, data->current_parent,
- spec.fid, rect, is_horizontal, true);
+ spec.ftype = f_ScrollBar;
+ spec.send = true;
+ GUIScrollBar *e = new GUIScrollBar(Environment, data->current_parent,
+ spec.fid, rect, is_horizontal, true);
- auto style = getDefaultStyleForElement("scrollbar", name);
- e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
- e->setArrowsVisible(data->scrollbar_options.arrow_visiblity);
+ auto style = getDefaultStyleForElement("scrollbar", name);
+ e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
+ e->setArrowsVisible(data->scrollbar_options.arrow_visiblity);
- s32 max = data->scrollbar_options.max;
- s32 min = data->scrollbar_options.min;
+ s32 max = data->scrollbar_options.max;
+ s32 min = data->scrollbar_options.min;
- e->setMax(max);
- e->setMin(min);
+ e->setMax(max);
+ e->setMin(min);
- e->setPos(stoi(parts[4]));
+ e->setPos(stoi(parts[4]));
- e->setSmallStep(data->scrollbar_options.small_step);
- e->setLargeStep(data->scrollbar_options.large_step);
+ e->setSmallStep(data->scrollbar_options.small_step);
+ e->setLargeStep(data->scrollbar_options.large_step);
- s32 scrollbar_size = is_horizontal ? dim.X : dim.Y;
+ s32 scrollbar_size = is_horizontal ? dim.X : dim.Y;
- e->setPageSize(scrollbar_size * (max - min + 1) / data->scrollbar_options.thumb_size);
+ e->setPageSize(scrollbar_size * (max - min + 1) / data->scrollbar_options.thumb_size);
- if (spec.fname == m_focused_element) {
- Environment->setFocus(e);
- }
-
- m_scrollbars.emplace_back(spec,e);
- m_fields.push_back(spec);
- return;
+ if (spec.fname == m_focused_element) {
+ Environment->setFocus(e);
}
- errorstream << "Invalid scrollbar element(" << parts.size() << "): '" << element
- << "'" << std::endl;
+
+ m_scrollbars.emplace_back(spec,e);
+ m_fields.push_back(spec);
}
void GUIFormSpecMenu::parseScrollBarOptions(parserData* data, const std::string &element)
@@ -787,11 +783,11 @@ void GUIFormSpecMenu::parseScrollBarOptions(parserData* data, const std::string
void GUIFormSpecMenu::parseImage(parserData* data, const std::string &element)
{
- std::vector<std::string> parts = split(element,';');
+ std::vector<std::string> parts;
+ if (!precheckElement("image", element, 2, 3, parts))
+ return;
- if ((parts.size() == 3) ||
- ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
- {
+ if (parts.size() >= 3) {
std::vector<std::string> v_pos = split(parts[0],',');
std::vector<std::string> v_geom = split(parts[1],',');
std::string name = unescape_string(parts[2]);
@@ -843,54 +839,47 @@ void GUIFormSpecMenu::parseImage(parserData* data, const std::string &element)
return;
}
- if (parts.size() == 2) {
- std::vector<std::string> v_pos = split(parts[0],',');
- std::string name = unescape_string(parts[1]);
-
- MY_CHECKPOS("image", 0);
+ // Else: 2 arguments in "parts"
- v2s32 pos = getElementBasePos(&v_pos);
+ std::vector<std::string> v_pos = split(parts[0],',');
+ std::string name = unescape_string(parts[1]);
- if (!data->explicit_size)
- warningstream<<"invalid use of image without a size[] element"<<std::endl;
+ MY_CHECKPOS("image", 0);
- video::ITexture *texture = m_tsrc->getTexture(name);
- if (!texture) {
- errorstream << "GUIFormSpecMenu::parseImage() Unable to load texture:"
- << std::endl << "\t" << name << std::endl;
- return;
- }
+ v2s32 pos = getElementBasePos(&v_pos);
- FieldSpec spec(
- name,
- L"",
- L"",
- 258 + m_fields.size()
- );
- gui::IGUIImage *e = Environment->addImage(texture, pos, true,
- data->current_parent, spec.fid, 0);
- auto style = getDefaultStyleForElement("image", spec.fname);
- e->setNotClipped(style.getBool(StyleSpec::NOCLIP, m_formspec_version < 3));
- m_fields.push_back(spec);
+ if (!data->explicit_size)
+ warningstream<<"invalid use of image without a size[] element"<<std::endl;
- // images should let events through
- e->grab();
- m_clickthrough_elements.push_back(e);
+ video::ITexture *texture = m_tsrc->getTexture(name);
+ if (!texture) {
+ errorstream << "GUIFormSpecMenu::parseImage() Unable to load texture:"
+ << std::endl << "\t" << name << std::endl;
return;
}
- errorstream<< "Invalid image element(" << parts.size() << "): '" << element << "'" << std::endl;
+
+ FieldSpec spec(
+ name,
+ L"",
+ L"",
+ 258 + m_fields.size()
+ );
+ gui::IGUIImage *e = Environment->addImage(texture, pos, true,
+ data->current_parent, spec.fid, 0);
+ auto style = getDefaultStyleForElement("image", spec.fname);
+ e->setNotClipped(style.getBool(StyleSpec::NOCLIP, m_formspec_version < 3));
+ m_fields.push_back(spec);
+
+ // images should let events through
+ e->grab();
+ m_clickthrough_elements.push_back(e);
}
void GUIFormSpecMenu::parseAnimatedImage(parserData *data, const std::string &element)
{
- std::vector<std::string> parts = split(element, ';');
-
- if (parts.size() != 6 && parts.size() != 7 &&
- !(parts.size() > 7 && m_formspec_version > FORMSPEC_API_VERSION)) {
- errorstream << "Invalid animated_image element(" << parts.size()
- << "): '" << element << "'" << std::endl;
+ std::vector<std::string> parts;
+ if (!precheckElement("animated_image", element, 6, 7, parts))
return;
- }
std::vector<std::string> v_pos = split(parts[0], ',');
std::vector<std::string> v_geom = split(parts[1], ',');
@@ -945,218 +934,207 @@ void GUIFormSpecMenu::parseAnimatedImage(parserData *data, const std::string &el
void GUIFormSpecMenu::parseItemImage(parserData* data, const std::string &element)
{
- std::vector<std::string> parts = split(element,';');
+ std::vector<std::string> parts;
+ if (!precheckElement("item_image", element, 3, 3, parts))
+ return;
- if ((parts.size() == 3) ||
- ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
- {
- std::vector<std::string> v_pos = split(parts[0],',');
- std::vector<std::string> v_geom = split(parts[1],',');
- std::string name = parts[2];
+ std::vector<std::string> v_pos = split(parts[0],',');
+ std::vector<std::string> v_geom = split(parts[1],',');
+ std::string name = parts[2];
- MY_CHECKPOS("itemimage",0);
- MY_CHECKGEOM("itemimage",1);
+ MY_CHECKPOS("item_image",0);
+ MY_CHECKGEOM("item_image",1);
- v2s32 pos;
- v2s32 geom;
+ v2s32 pos;
+ v2s32 geom;
- if (data->real_coordinates) {
- pos = getRealCoordinateBasePos(v_pos);
- geom = getRealCoordinateGeometry(v_geom);
- } else {
- pos = getElementBasePos(&v_pos);
- geom.X = stof(v_geom[0]) * (float)imgsize.X;
- geom.Y = stof(v_geom[1]) * (float)imgsize.Y;
- }
+ if (data->real_coordinates) {
+ pos = getRealCoordinateBasePos(v_pos);
+ geom = getRealCoordinateGeometry(v_geom);
+ } else {
+ pos = getElementBasePos(&v_pos);
+ geom.X = stof(v_geom[0]) * (float)imgsize.X;
+ geom.Y = stof(v_geom[1]) * (float)imgsize.Y;
+ }
- if(!data->explicit_size)
- warningstream<<"invalid use of item_image without a size[] element"<<std::endl;
+ if(!data->explicit_size)
+ warningstream<<"invalid use of item_image without a size[] element"<<std::endl;
- FieldSpec spec(
- "",
- L"",
- L"",
- 258 + m_fields.size(),
- 2
- );
- spec.ftype = f_ItemImage;
+ FieldSpec spec(
+ "",
+ L"",
+ L"",
+ 258 + m_fields.size(),
+ 2
+ );
+ spec.ftype = f_ItemImage;
- GUIItemImage *e = new GUIItemImage(Environment, data->current_parent, spec.fid,
- core::rect<s32>(pos, pos + geom), name, m_font, m_client);
- auto style = getDefaultStyleForElement("item_image", spec.fname);
- e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
+ GUIItemImage *e = new GUIItemImage(Environment, data->current_parent, spec.fid,
+ core::rect<s32>(pos, pos + geom), name, m_font, m_client);
+ auto style = getDefaultStyleForElement("item_image", spec.fname);
+ e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
- // item images should let events through
- m_clickthrough_elements.push_back(e);
+ // item images should let events through
+ m_clickthrough_elements.push_back(e);
- m_fields.push_back(spec);
- return;
- }
- errorstream<< "Invalid ItemImage element(" << parts.size() << "): '" << element << "'" << std::endl;
+ m_fields.push_back(spec);
}
void GUIFormSpecMenu::parseButton(parserData* data, const std::string &element,
const std::string &type)
{
- std::vector<std::string> parts = split(element,';');
-
- if ((parts.size() == 4) ||
- ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
- {
- std::vector<std::string> v_pos = split(parts[0],',');
- std::vector<std::string> v_geom = split(parts[1],',');
- std::string name = parts[2];
- std::string label = parts[3];
+ std::vector<std::string> parts;
+ if (!precheckElement("button", element, 4, 4, parts))
+ return;
- MY_CHECKPOS("button",0);
- MY_CHECKGEOM("button",1);
+ std::vector<std::string> v_pos = split(parts[0],',');
+ std::vector<std::string> v_geom = split(parts[1],',');
+ std::string name = parts[2];
+ std::string label = parts[3];
- v2s32 pos;
- v2s32 geom;
- core::rect<s32> rect;
+ MY_CHECKPOS("button",0);
+ MY_CHECKGEOM("button",1);
- if (data->real_coordinates) {
- pos = getRealCoordinateBasePos(v_pos);
- geom = getRealCoordinateGeometry(v_geom);
- rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X,
- pos.Y+geom.Y);
- } else {
- pos = getElementBasePos(&v_pos);
- geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X);
- pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
+ v2s32 pos;
+ v2s32 geom;
+ core::rect<s32> rect;
- rect = core::rect<s32>(pos.X, pos.Y - m_btn_height,
- pos.X + geom.X, pos.Y + m_btn_height);
- }
+ if (data->real_coordinates) {
+ pos = getRealCoordinateBasePos(v_pos);
+ geom = getRealCoordinateGeometry(v_geom);
+ rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X,
+ pos.Y+geom.Y);
+ } else {
+ pos = getElementBasePos(&v_pos);
+ geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X);
+ pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
- if(!data->explicit_size)
- warningstream<<"invalid use of button without a size[] element"<<std::endl;
+ rect = core::rect<s32>(pos.X, pos.Y - m_btn_height,
+ pos.X + geom.X, pos.Y + m_btn_height);
+ }
- std::wstring wlabel = translate_string(utf8_to_wide(unescape_string(label)));
+ if(!data->explicit_size)
+ warningstream<<"invalid use of button without a size[] element"<<std::endl;
- FieldSpec spec(
- name,
- wlabel,
- L"",
- 258 + m_fields.size()
- );
- spec.ftype = f_Button;
- if(type == "button_exit")
- spec.is_exit = true;
+ std::wstring wlabel = translate_string(utf8_to_wide(unescape_string(label)));
- GUIButton *e = GUIButton::addButton(Environment, rect, m_tsrc,
- data->current_parent, spec.fid, spec.flabel.c_str());
+ FieldSpec spec(
+ name,
+ wlabel,
+ L"",
+ 258 + m_fields.size()
+ );
+ spec.ftype = f_Button;
+ if(type == "button_exit")
+ spec.is_exit = true;
- auto style = getStyleForElement(type, name, (type != "button") ? "button" : "");
+ GUIButton *e = GUIButton::addButton(Environment, rect, m_tsrc,
+ data->current_parent, spec.fid, spec.flabel.c_str());
- spec.sound = style[StyleSpec::STATE_DEFAULT].get(StyleSpec::Property::SOUND, "");
+ auto style = getStyleForElement(type, name, (type != "button") ? "button" : "");
- e->setStyles(style);
+ spec.sound = style[StyleSpec::STATE_DEFAULT].get(StyleSpec::Property::SOUND, "");
- if (spec.fname == m_focused_element) {
- Environment->setFocus(e);
- }
+ e->setStyles(style);
- m_fields.push_back(spec);
- return;
+ if (spec.fname == m_focused_element) {
+ Environment->setFocus(e);
}
- errorstream<< "Invalid button element(" << parts.size() << "): '" << element << "'" << std::endl;
+
+ m_fields.push_back(spec);
}
void GUIFormSpecMenu::parseBackground(parserData* data, const std::string &element)
{
- std::vector<std::string> parts = split(element,';');
+ std::vector<std::string> parts;
+ if (!precheckElement("background", element, 3, 5, parts))
+ return;
- if ((parts.size() >= 3 && parts.size() <= 5) ||
- (parts.size() > 5 && m_formspec_version > FORMSPEC_API_VERSION)) {
- std::vector<std::string> v_pos = split(parts[0],',');
- std::vector<std::string> v_geom = split(parts[1],',');
- std::string name = unescape_string(parts[2]);
+ std::vector<std::string> v_pos = split(parts[0],',');
+ std::vector<std::string> v_geom = split(parts[1],',');
+ std::string name = unescape_string(parts[2]);
- MY_CHECKPOS("background",0);
- MY_CHECKGEOM("background",1);
+ MY_CHECKPOS("background",0);
+ MY_CHECKGEOM("background",1);
- v2s32 pos;
- v2s32 geom;
+ v2s32 pos;
+ v2s32 geom;
- if (data->real_coordinates) {
- pos = getRealCoordinateBasePos(v_pos);
- geom = getRealCoordinateGeometry(v_geom);
- } else {
- pos = getElementBasePos(&v_pos);
- pos.X -= (spacing.X - (float)imgsize.X) / 2;
- pos.Y -= (spacing.Y - (float)imgsize.Y) / 2;
+ if (data->real_coordinates) {
+ pos = getRealCoordinateBasePos(v_pos);
+ geom = getRealCoordinateGeometry(v_geom);
+ } else {
+ pos = getElementBasePos(&v_pos);
+ pos.X -= (spacing.X - (float)imgsize.X) / 2;
+ pos.Y -= (spacing.Y - (float)imgsize.Y) / 2;
- geom.X = stof(v_geom[0]) * spacing.X;
- geom.Y = stof(v_geom[1]) * spacing.Y;
- }
+ geom.X = stof(v_geom[0]) * spacing.X;
+ geom.Y = stof(v_geom[1]) * spacing.Y;
+ }
- bool clip = false;
- if (parts.size() >= 4 && is_yes(parts[3])) {
- if (data->real_coordinates) {
- pos = getRealCoordinateBasePos(v_pos) * -1;
- geom = v2s32(0, 0);
- } else {
- pos.X = stoi(v_pos[0]); //acts as offset
- pos.Y = stoi(v_pos[1]);
- }
- clip = true;
+ bool clip = false;
+ if (parts.size() >= 4 && is_yes(parts[3])) {
+ if (data->real_coordinates) {
+ pos = getRealCoordinateBasePos(v_pos) * -1;
+ geom = v2s32(0, 0);
+ } else {
+ pos.X = stoi(v_pos[0]); //acts as offset
+ pos.Y = stoi(v_pos[1]);
}
+ clip = true;
+ }
- core::rect<s32> middle;
- if (parts.size() >= 5) {
- std::vector<std::string> v_middle = split(parts[4], ',');
- if (v_middle.size() == 1) {
- s32 x = stoi(v_middle[0]);
- middle.UpperLeftCorner = core::vector2di(x, x);
- middle.LowerRightCorner = core::vector2di(-x, -x);
- } else if (v_middle.size() == 2) {
- s32 x = stoi(v_middle[0]);
- s32 y = stoi(v_middle[1]);
- middle.UpperLeftCorner = core::vector2di(x, y);
- middle.LowerRightCorner = core::vector2di(-x, -y);
- // `-x` is interpreted as `w - x`
- } else if (v_middle.size() == 4) {
- middle.UpperLeftCorner = core::vector2di(stoi(v_middle[0]), stoi(v_middle[1]));
- middle.LowerRightCorner = core::vector2di(stoi(v_middle[2]), stoi(v_middle[3]));
- } else {
- warningstream << "Invalid rectangle given to middle param of background[] element" << std::endl;
- }
+ core::rect<s32> middle;
+ if (parts.size() >= 5) {
+ std::vector<std::string> v_middle = split(parts[4], ',');
+ if (v_middle.size() == 1) {
+ s32 x = stoi(v_middle[0]);
+ middle.UpperLeftCorner = core::vector2di(x, x);
+ middle.LowerRightCorner = core::vector2di(-x, -x);
+ } else if (v_middle.size() == 2) {
+ s32 x = stoi(v_middle[0]);
+ s32 y = stoi(v_middle[1]);
+ middle.UpperLeftCorner = core::vector2di(x, y);
+ middle.LowerRightCorner = core::vector2di(-x, -y);
+ // `-x` is interpreted as `w - x`
+ } else if (v_middle.size() == 4) {
+ middle.UpperLeftCorner = core::vector2di(stoi(v_middle[0]), stoi(v_middle[1]));
+ middle.LowerRightCorner = core::vector2di(stoi(v_middle[2]), stoi(v_middle[3]));
+ } else {
+ warningstream << "Invalid rectangle given to middle param of background[] element" << std::endl;
}
+ }
- if (!data->explicit_size && !clip)
- warningstream << "invalid use of unclipped background without a size[] element" << std::endl;
+ if (!data->explicit_size && !clip)
+ warningstream << "invalid use of unclipped background without a size[] element" << std::endl;
- FieldSpec spec(
- name,
- L"",
- L"",
- 258 + m_fields.size()
- );
+ FieldSpec spec(
+ name,
+ L"",
+ L"",
+ 258 + m_fields.size()
+ );
- core::rect<s32> rect;
- if (!clip) {
- // no auto_clip => position like normal image
- rect = core::rect<s32>(pos, pos + geom);
- } else {
- // it will be auto-clipped when drawing
- rect = core::rect<s32>(-pos, pos);
- }
+ core::rect<s32> rect;
+ if (!clip) {
+ // no auto_clip => position like normal image
+ rect = core::rect<s32>(pos, pos + geom);
+ } else {
+ // it will be auto-clipped when drawing
+ rect = core::rect<s32>(-pos, pos);
+ }
- GUIBackgroundImage *e = new GUIBackgroundImage(Environment, this, spec.fid,
- rect, name, middle, m_tsrc, clip);
+ GUIBackgroundImage *e = new GUIBackgroundImage(Environment, this, spec.fid,
+ rect, name, middle, m_tsrc, clip);
- FATAL_ERROR_IF(!e, "Failed to create background formspec element");
+ FATAL_ERROR_IF(!e, "Failed to create background formspec element");
- e->setNotClipped(true);
+ e->setNotClipped(true);
- e->setVisible(false); // the element is drawn manually before all others
+ e->setVisible(false); // the element is drawn manually before all others
- m_backgrounds.push_back(e);
- m_fields.push_back(spec);
- return;
- }
- errorstream<< "Invalid background element(" << parts.size() << "): '" << element << "'" << std::endl;
+ m_backgrounds.push_back(e);
+ m_fields.push_back(spec);
}
void GUIFormSpecMenu::parseTableOptions(parserData* data, const std::string &element)
@@ -1193,338 +1171,320 @@ void GUIFormSpecMenu::parseTableColumns(parserData* data, const std::string &ele
void GUIFormSpecMenu::parseTable(parserData* data, const std::string &element)
{
- std::vector<std::string> parts = split(element,';');
+ std::vector<std::string> parts;
+ if (!precheckElement("table", element, 4, 5, parts))
+ return;
- if (((parts.size() == 4) || (parts.size() == 5)) ||
- ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
- {
- std::vector<std::string> v_pos = split(parts[0],',');
- std::vector<std::string> v_geom = split(parts[1],',');
- std::string name = parts[2];
- std::vector<std::string> items = split(parts[3],',');
- std::string str_initial_selection;
- std::string str_transparent = "false";
+ std::vector<std::string> v_pos = split(parts[0],',');
+ std::vector<std::string> v_geom = split(parts[1],',');
+ std::string name = parts[2];
+ std::vector<std::string> items = split(parts[3],',');
+ std::string str_initial_selection;
+ std::string str_transparent = "false";
- if (parts.size() >= 5)
- str_initial_selection = parts[4];
+ if (parts.size() >= 5)
+ str_initial_selection = parts[4];
- MY_CHECKPOS("table",0);
- MY_CHECKGEOM("table",1);
+ MY_CHECKPOS("table",0);
+ MY_CHECKGEOM("table",1);
- v2s32 pos;
- v2s32 geom;
+ v2s32 pos;
+ v2s32 geom;
- if (data->real_coordinates) {
- pos = getRealCoordinateBasePos(v_pos);
- geom = getRealCoordinateGeometry(v_geom);
- } else {
- pos = getElementBasePos(&v_pos);
- geom.X = stof(v_geom[0]) * spacing.X;
- geom.Y = stof(v_geom[1]) * spacing.Y;
- }
+ if (data->real_coordinates) {
+ pos = getRealCoordinateBasePos(v_pos);
+ geom = getRealCoordinateGeometry(v_geom);
+ } else {
+ pos = getElementBasePos(&v_pos);
+ geom.X = stof(v_geom[0]) * spacing.X;
+ geom.Y = stof(v_geom[1]) * spacing.Y;
+ }
- core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
+ core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
- FieldSpec spec(
- name,
- L"",
- L"",
- 258 + m_fields.size()
- );
+ FieldSpec spec(
+ name,
+ L"",
+ L"",
+ 258 + m_fields.size()
+ );
- spec.ftype = f_Table;
+ spec.ftype = f_Table;
- for (std::string &item : items) {
- item = wide_to_utf8(unescape_translate(utf8_to_wide(unescape_string(item))));
- }
+ for (std::string &item : items) {
+ item = wide_to_utf8(unescape_translate(utf8_to_wide(unescape_string(item))));
+ }
- //now really show table
- GUITable *e = new GUITable(Environment, data->current_parent, spec.fid,
- rect, m_tsrc);
+ //now really show table
+ GUITable *e = new GUITable(Environment, data->current_parent, spec.fid,
+ rect, m_tsrc);
- if (spec.fname == m_focused_element) {
- Environment->setFocus(e);
- }
+ if (spec.fname == m_focused_element) {
+ Environment->setFocus(e);
+ }
- e->setTable(data->table_options, data->table_columns, items);
+ e->setTable(data->table_options, data->table_columns, items);
- if (data->table_dyndata.find(name) != data->table_dyndata.end()) {
- e->setDynamicData(data->table_dyndata[name]);
- }
+ if (data->table_dyndata.find(name) != data->table_dyndata.end()) {
+ e->setDynamicData(data->table_dyndata[name]);
+ }
- if (!str_initial_selection.empty() && str_initial_selection != "0")
- e->setSelected(stoi(str_initial_selection));
+ if (!str_initial_selection.empty() && str_initial_selection != "0")
+ e->setSelected(stoi(str_initial_selection));
- auto style = getDefaultStyleForElement("table", name);
- e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
- e->setOverrideFont(style.getFont());
+ auto style = getDefaultStyleForElement("table", name);
+ e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
+ e->setOverrideFont(style.getFont());
- m_tables.emplace_back(spec, e);
- m_fields.push_back(spec);
- return;
- }
- errorstream<< "Invalid table element(" << parts.size() << "): '" << element << "'" << std::endl;
+ m_tables.emplace_back(spec, e);
+ m_fields.push_back(spec);
}
void GUIFormSpecMenu::parseTextList(parserData* data, const std::string &element)
{
- std::vector<std::string> parts = split(element,';');
+ std::vector<std::string> parts;
+ if (!precheckElement("textlist", element, 4, 6, parts))
+ return;
- if (((parts.size() == 4) || (parts.size() == 5) || (parts.size() == 6)) ||
- ((parts.size() > 6) && (m_formspec_version > FORMSPEC_API_VERSION)))
- {
- std::vector<std::string> v_pos = split(parts[0],',');
- std::vector<std::string> v_geom = split(parts[1],',');
- std::string name = parts[2];
- std::vector<std::string> items = split(parts[3],',');
- std::string str_initial_selection;
- std::string str_transparent = "false";
+ std::vector<std::string> v_pos = split(parts[0],',');
+ std::vector<std::string> v_geom = split(parts[1],',');
+ std::string name = parts[2];
+ std::vector<std::string> items = split(parts[3],',');
+ std::string str_initial_selection;
+ std::string str_transparent = "false";
- if (parts.size() >= 5)
- str_initial_selection = parts[4];
+ if (parts.size() >= 5)
+ str_initial_selection = parts[4];
- if (parts.size() >= 6)
- str_transparent = parts[5];
+ if (parts.size() >= 6)
+ str_transparent = parts[5];
- MY_CHECKPOS("textlist",0);
- MY_CHECKGEOM("textlist",1);
+ MY_CHECKPOS("textlist",0);
+ MY_CHECKGEOM("textlist",1);
- v2s32 pos;
- v2s32 geom;
+ v2s32 pos;
+ v2s32 geom;
- if (data->real_coordinates) {
- pos = getRealCoordinateBasePos(v_pos);
- geom = getRealCoordinateGeometry(v_geom);
- } else {
- pos = getElementBasePos(&v_pos);
- geom.X = stof(v_geom[0]) * spacing.X;
- geom.Y = stof(v_geom[1]) * spacing.Y;
- }
+ if (data->real_coordinates) {
+ pos = getRealCoordinateBasePos(v_pos);
+ geom = getRealCoordinateGeometry(v_geom);
+ } else {
+ pos = getElementBasePos(&v_pos);
+ geom.X = stof(v_geom[0]) * spacing.X;
+ geom.Y = stof(v_geom[1]) * spacing.Y;
+ }
- core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
+ core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
- FieldSpec spec(
- name,
- L"",
- L"",
- 258 + m_fields.size()
- );
+ FieldSpec spec(
+ name,
+ L"",
+ L"",
+ 258 + m_fields.size()
+ );
- spec.ftype = f_Table;
+ spec.ftype = f_Table;
- for (std::string &item : items) {
- item = wide_to_utf8(unescape_translate(utf8_to_wide(unescape_string(item))));
- }
+ for (std::string &item : items) {
+ item = wide_to_utf8(unescape_translate(utf8_to_wide(unescape_string(item))));
+ }
- //now really show list
- GUITable *e = new GUITable(Environment, data->current_parent, spec.fid,
- rect, m_tsrc);
+ //now really show list
+ GUITable *e = new GUITable(Environment, data->current_parent, spec.fid,
+ rect, m_tsrc);
- if (spec.fname == m_focused_element) {
- Environment->setFocus(e);
- }
+ if (spec.fname == m_focused_element) {
+ Environment->setFocus(e);
+ }
- e->setTextList(items, is_yes(str_transparent));
+ e->setTextList(items, is_yes(str_transparent));
- if (data->table_dyndata.find(name) != data->table_dyndata.end()) {
- e->setDynamicData(data->table_dyndata[name]);
- }
+ if (data->table_dyndata.find(name) != data->table_dyndata.end()) {
+ e->setDynamicData(data->table_dyndata[name]);
+ }
- if (!str_initial_selection.empty() && str_initial_selection != "0")
- e->setSelected(stoi(str_initial_selection));
+ if (!str_initial_selection.empty() && str_initial_selection != "0")
+ e->setSelected(stoi(str_initial_selection));
- auto style = getDefaultStyleForElement("textlist", name);
- e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
- e->setOverrideFont(style.getFont());
+ auto style = getDefaultStyleForElement("textlist", name);
+ e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
+ e->setOverrideFont(style.getFont());
- m_tables.emplace_back(spec, e);
- m_fields.push_back(spec);
- return;
- }
- errorstream<< "Invalid textlist element(" << parts.size() << "): '" << element << "'" << std::endl;
+ m_tables.emplace_back(spec, e);
+ m_fields.push_back(spec);
}
void GUIFormSpecMenu::parseDropDown(parserData* data, const std::string &element)
{
- std::vector<std::string> parts = split(element, ';');
-
- if (parts.size() == 5 || parts.size() == 6 ||
- (parts.size() > 6 && m_formspec_version > FORMSPEC_API_VERSION))
- {
- std::vector<std::string> v_pos = split(parts[0], ',');
- std::string name = parts[2];
- std::vector<std::string> items = split(parts[3], ',');
- std::string str_initial_selection = parts[4];
+ std::vector<std::string> parts;
+ if (!precheckElement("dropdown", element, 5, 6, parts))
+ return;
- if (parts.size() >= 6 && is_yes(parts[5]))
- m_dropdown_index_event[name] = true;
+ std::vector<std::string> v_pos = split(parts[0], ',');
+ std::string name = parts[2];
+ std::vector<std::string> items = split(parts[3], ',');
+ std::string str_initial_selection = parts[4];
- MY_CHECKPOS("dropdown",0);
+ if (parts.size() >= 6 && is_yes(parts[5]))
+ m_dropdown_index_event[name] = true;
- v2s32 pos;
- v2s32 geom;
- core::rect<s32> rect;
+ MY_CHECKPOS("dropdown",0);
- if (data->real_coordinates) {
- std::vector<std::string> v_geom = split(parts[1],',');
+ v2s32 pos;
+ v2s32 geom;
+ core::rect<s32> rect;
- if (v_geom.size() == 1)
- v_geom.emplace_back("1");
+ if (data->real_coordinates) {
+ std::vector<std::string> v_geom = split(parts[1],',');
- MY_CHECKGEOM("dropdown",1);
+ if (v_geom.size() == 1)
+ v_geom.emplace_back("1");
- pos = getRealCoordinateBasePos(v_pos);
- geom = getRealCoordinateGeometry(v_geom);
- rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
- } else {
- pos = getElementBasePos(&v_pos);
+ MY_CHECKGEOM("dropdown",1);
- s32 width = stof(parts[1]) * spacing.Y;
+ pos = getRealCoordinateBasePos(v_pos);
+ geom = getRealCoordinateGeometry(v_geom);
+ rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
+ } else {
+ pos = getElementBasePos(&v_pos);
- rect = core::rect<s32>(pos.X, pos.Y,
- pos.X + width, pos.Y + (m_btn_height * 2));
- }
+ s32 width = stof(parts[1]) * spacing.Y;
- FieldSpec spec(
- name,
- L"",
- L"",
- 258 + m_fields.size()
- );
+ rect = core::rect<s32>(pos.X, pos.Y,
+ pos.X + width, pos.Y + (m_btn_height * 2));
+ }
- spec.ftype = f_DropDown;
- spec.send = true;
+ FieldSpec spec(
+ name,
+ L"",
+ L"",
+ 258 + m_fields.size()
+ );
- //now really show list
- gui::IGUIComboBox *e = Environment->addComboBox(rect, data->current_parent,
- spec.fid);
+ spec.ftype = f_DropDown;
+ spec.send = true;
- if (spec.fname == m_focused_element) {
- Environment->setFocus(e);
- }
+ //now really show list
+ gui::IGUIComboBox *e = Environment->addComboBox(rect, data->current_parent,
+ spec.fid);
- for (const std::string &item : items) {
- e->addItem(unescape_translate(unescape_string(
- utf8_to_wide(item))).c_str());
- }
+ if (spec.fname == m_focused_element) {
+ Environment->setFocus(e);
+ }
- if (!str_initial_selection.empty())
- e->setSelected(stoi(str_initial_selection)-1);
+ for (const std::string &item : items) {
+ e->addItem(unescape_translate(unescape_string(
+ utf8_to_wide(item))).c_str());
+ }
- auto style = getDefaultStyleForElement("dropdown", name);
+ if (!str_initial_selection.empty())
+ e->setSelected(stoi(str_initial_selection)-1);
- spec.sound = style.get(StyleSpec::Property::SOUND, "");
+ auto style = getDefaultStyleForElement("dropdown", name);
- e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
+ spec.sound = style.get(StyleSpec::Property::SOUND, "");
- m_fields.push_back(spec);
+ e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
- m_dropdowns.emplace_back(spec, std::vector<std::string>());
- std::vector<std::string> &values = m_dropdowns.back().second;
- for (const std::string &item : items) {
- values.push_back(unescape_string(item));
- }
+ m_fields.push_back(spec);
- return;
+ m_dropdowns.emplace_back(spec, std::vector<std::string>());
+ std::vector<std::string> &values = m_dropdowns.back().second;
+ for (const std::string &item : items) {
+ values.push_back(unescape_string(item));
}
- errorstream << "Invalid dropdown element(" << parts.size() << "): '" << element
- << "'" << std::endl;
}
void GUIFormSpecMenu::parseFieldCloseOnEnter(parserData *data, const std::string &element)
{
- std::vector<std::string> parts = split(element,';');
- if (parts.size() == 2 ||
- (parts.size() > 2 && m_formspec_version > FORMSPEC_API_VERSION)) {
- field_close_on_enter[parts[0]] = is_yes(parts[1]);
- }
+ std::vector<std::string> parts;
+ if (!precheckElement("field_close_on_enter", element, 2, 2, parts))
+ return;
+
+ field_close_on_enter[parts[0]] = is_yes(parts[1]);
}
void GUIFormSpecMenu::parsePwdField(parserData* data, const std::string &element)
{
- std::vector<std::string> parts = split(element,';');
-
- if (parts.size() == 4 ||
- (parts.size() > 4 && m_formspec_version > FORMSPEC_API_VERSION))
- {
- std::vector<std::string> v_pos = split(parts[0],',');
- std::vector<std::string> v_geom = split(parts[1],',');
- std::string name = parts[2];
- std::string label = parts[3];
-
- MY_CHECKPOS("pwdfield",0);
- MY_CHECKGEOM("pwdfield",1);
+ std::vector<std::string> parts;
+ if (!precheckElement("pwdfield", element, 4, 4, parts))
+ return;
- v2s32 pos;
- v2s32 geom;
+ std::vector<std::string> v_pos = split(parts[0],',');
+ std::vector<std::string> v_geom = split(parts[1],',');
+ std::string name = parts[2];
+ std::string label = parts[3];
- if (data->real_coordinates) {
- pos = getRealCoordinateBasePos(v_pos);
- geom = getRealCoordinateGeometry(v_geom);
- } else {
- pos = getElementBasePos(&v_pos);
- pos -= padding;
+ MY_CHECKPOS("pwdfield",0);
+ MY_CHECKGEOM("pwdfield",1);
- geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X);
+ v2s32 pos;
+ v2s32 geom;
- pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
- pos.Y -= m_btn_height;
- geom.Y = m_btn_height*2;
- }
+ if (data->real_coordinates) {
+ pos = getRealCoordinateBasePos(v_pos);
+ geom = getRealCoordinateGeometry(v_geom);
+ } else {
+ pos = getElementBasePos(&v_pos);
+ pos -= padding;
- core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
+ geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X);
- std::wstring wlabel = translate_string(utf8_to_wide(unescape_string(label)));
+ pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
+ pos.Y -= m_btn_height;
+ geom.Y = m_btn_height*2;
+ }
- FieldSpec spec(
- name,
- wlabel,
- L"",
- 258 + m_fields.size(),
- 0,
- ECI_IBEAM
- );
+ core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
- spec.send = true;
- gui::IGUIEditBox *e = Environment->addEditBox(0, rect, true,
- data->current_parent, spec.fid);
+ std::wstring wlabel = translate_string(utf8_to_wide(unescape_string(label)));
- if (spec.fname == m_focused_element) {
- Environment->setFocus(e);
- }
+ FieldSpec spec(
+ name,
+ wlabel,
+ L"",
+ 258 + m_fields.size(),
+ 0,
+ ECI_IBEAM
+ );
- if (label.length() >= 1) {
- int font_height = g_fontengine->getTextHeight();
- rect.UpperLeftCorner.Y -= font_height;
- rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
- gui::StaticText::add(Environment, spec.flabel.c_str(), rect, false, true,
- data->current_parent, 0);
- }
+ spec.send = true;
+ gui::IGUIEditBox *e = Environment->addEditBox(0, rect, true,
+ data->current_parent, spec.fid);
- e->setPasswordBox(true,L'*');
+ if (spec.fname == m_focused_element) {
+ Environment->setFocus(e);
+ }
- auto style = getDefaultStyleForElement("pwdfield", name, "field");
- e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
- e->setDrawBorder(style.getBool(StyleSpec::BORDER, true));
- e->setOverrideColor(style.getColor(StyleSpec::TEXTCOLOR, video::SColor(0xFFFFFFFF)));
- e->setOverrideFont(style.getFont());
+ if (label.length() >= 1) {
+ int font_height = g_fontengine->getTextHeight();
+ rect.UpperLeftCorner.Y -= font_height;
+ rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
+ gui::StaticText::add(Environment, spec.flabel.c_str(), rect, false, true,
+ data->current_parent, 0);
+ }
- irr::SEvent evt;
- evt.EventType = EET_KEY_INPUT_EVENT;
- evt.KeyInput.Key = KEY_END;
- evt.KeyInput.Char = 0;
- evt.KeyInput.Control = false;
- evt.KeyInput.Shift = false;
- evt.KeyInput.PressedDown = true;
- e->OnEvent(evt);
+ e->setPasswordBox(true,L'*');
- // Note: Before 5.2.0 "parts.size() >= 5" resulted in a
- // warning referring to field_close_on_enter[]!
+ auto style = getDefaultStyleForElement("pwdfield", name, "field");
+ e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
+ e->setDrawBorder(style.getBool(StyleSpec::BORDER, true));
+ e->setOverrideColor(style.getColor(StyleSpec::TEXTCOLOR, video::SColor(0xFFFFFFFF)));
+ e->setOverrideFont(style.getFont());
+
+ irr::SEvent evt;
+ evt.EventType = EET_KEY_INPUT_EVENT;
+ evt.KeyInput.Key = KEY_END;
+ evt.KeyInput.Char = 0;
+ evt.KeyInput.Control = false;
+ evt.KeyInput.Shift = false;
+ evt.KeyInput.PressedDown = true;
+ e->OnEvent(evt);
+
+ // Note: Before 5.2.0 "parts.size() >= 5" resulted in a
+ // warning referring to field_close_on_enter[]!
- m_fields.push_back(spec);
- return;
- }
- errorstream<< "Invalid pwdfield element(" << parts.size() << "): '" << element << "'" << std::endl;
+ m_fields.push_back(spec);
}
void GUIFormSpecMenu::createTextField(parserData *data, FieldSpec &spec,
@@ -1547,21 +1507,13 @@ void GUIFormSpecMenu::createTextField(parserData *data, FieldSpec &spec,
}
gui::IGUIEditBox *e = nullptr;
- static constexpr bool use_intl_edit_box = USE_FREETYPE &&
- IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 9;
-
- if (use_intl_edit_box && g_settings->getBool("freetype")) {
- e = new gui::intlGUIEditBox(spec.fdefault.c_str(), true, Environment,
- data->current_parent, spec.fid, rect, is_editable, is_multiline);
- } else {
- if (is_multiline) {
- e = new GUIEditBoxWithScrollBar(spec.fdefault.c_str(), true, Environment,
- data->current_parent, spec.fid, rect, is_editable, true);
- } else if (is_editable) {
- e = Environment->addEditBox(spec.fdefault.c_str(), rect, true,
- data->current_parent, spec.fid);
- e->grab();
- }
+ if (is_multiline) {
+ e = new GUIEditBoxWithScrollBar(spec.fdefault.c_str(), true, Environment,
+ data->current_parent, spec.fid, rect, is_editable, true);
+ } else if (is_editable) {
+ e = Environment->addEditBox(spec.fdefault.c_str(), rect, true,
+ data->current_parent, spec.fid);
+ e->grab();
}
auto style = getDefaultStyleForElement(is_multiline ? "textarea" : "field", spec.fname);
@@ -1586,11 +1538,10 @@ void GUIFormSpecMenu::createTextField(parserData *data, FieldSpec &spec,
}
e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
- e->setDrawBorder(style.getBool(StyleSpec::BORDER, true));
e->setOverrideColor(style.getColor(StyleSpec::TEXTCOLOR, video::SColor(0xFFFFFFFF)));
- if (style.get(StyleSpec::BGCOLOR, "") == "transparent") {
- e->setDrawBackground(false);
- }
+ bool border = style.getBool(StyleSpec::BORDER, true);
+ e->setDrawBorder(border);
+ e->setDrawBackground(border);
e->setOverrideFont(style.getFont());
e->drop();
@@ -1720,30 +1671,26 @@ void GUIFormSpecMenu::parseTextArea(parserData* data, std::vector<std::string>&
void GUIFormSpecMenu::parseField(parserData* data, const std::string &element,
const std::string &type)
{
- std::vector<std::string> parts = split(element,';');
+ std::vector<std::string> parts;
+ if (!precheckElement(type, element, 3, 5, parts))
+ return;
if (parts.size() == 3 || parts.size() == 4) {
- parseSimpleField(data,parts);
+ parseSimpleField(data, parts);
return;
}
- if ((parts.size() == 5) ||
- ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
- {
- parseTextArea(data,parts,type);
- return;
- }
- errorstream<< "Invalid field element(" << parts.size() << "): '" << element << "'" << std::endl;
+ // Else: >= 5 arguments in "parts"
+ parseTextArea(data, parts, type);
}
void GUIFormSpecMenu::parseHyperText(parserData *data, const std::string &element)
{
- std::vector<std::string> parts = split(element, ';');
+ MY_CHECKCLIENT("list");
- if (parts.size() != 4 && m_formspec_version < FORMSPEC_API_VERSION) {
- errorstream << "Invalid text element(" << parts.size() << "): '" << element << "'" << std::endl;
+ std::vector<std::string> parts;
+ if (!precheckElement("hypertext", element, 4, 4, parts))
return;
- }
std::vector<std::string> v_pos = split(parts[0], ',');
std::vector<std::string> v_geom = split(parts[1], ',');
@@ -1794,539 +1741,521 @@ void GUIFormSpecMenu::parseHyperText(parserData *data, const std::string &elemen
void GUIFormSpecMenu::parseLabel(parserData* data, const std::string &element)
{
- std::vector<std::string> parts = split(element,';');
+ std::vector<std::string> parts;
+ if (!precheckElement("label", element, 2, 2, parts))
+ return;
- if ((parts.size() == 2) ||
- ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION)))
- {
- std::vector<std::string> v_pos = split(parts[0],',');
- std::string text = parts[1];
+ std::vector<std::string> v_pos = split(parts[0],',');
+ std::string text = parts[1];
- MY_CHECKPOS("label",0);
+ MY_CHECKPOS("label",0);
- if(!data->explicit_size)
- warningstream<<"invalid use of label without a size[] element"<<std::endl;
+ if(!data->explicit_size)
+ warningstream<<"invalid use of label without a size[] element"<<std::endl;
- std::vector<std::string> lines = split(text, '\n');
+ std::vector<std::string> lines = split(text, '\n');
- auto style = getDefaultStyleForElement("label", "");
- gui::IGUIFont *font = style.getFont();
- if (!font)
- font = m_font;
+ auto style = getDefaultStyleForElement("label", "");
+ gui::IGUIFont *font = style.getFont();
+ if (!font)
+ font = m_font;
- for (unsigned int i = 0; i != lines.size(); i++) {
- std::wstring wlabel_colors = translate_string(
- utf8_to_wide(unescape_string(lines[i])));
- // Without color escapes to get the font dimensions
- std::wstring wlabel_plain = unescape_enriched(wlabel_colors);
+ for (unsigned int i = 0; i != lines.size(); i++) {
+ std::wstring wlabel_colors = translate_string(
+ utf8_to_wide(unescape_string(lines[i])));
+ // Without color escapes to get the font dimensions
+ std::wstring wlabel_plain = unescape_enriched(wlabel_colors);
- core::rect<s32> rect;
+ core::rect<s32> rect;
- if (data->real_coordinates) {
- // Lines are spaced at the distance of 1/2 imgsize.
- // This alows lines that line up with the new elements
- // easily without sacrificing good line distance. If
- // it was one whole imgsize, it would have too much
- // spacing.
- v2s32 pos = getRealCoordinateBasePos(v_pos);
+ if (data->real_coordinates) {
+ // Lines are spaced at the distance of 1/2 imgsize.
+ // This alows lines that line up with the new elements
+ // easily without sacrificing good line distance. If
+ // it was one whole imgsize, it would have too much
+ // spacing.
+ v2s32 pos = getRealCoordinateBasePos(v_pos);
- // Labels are positioned by their center, not their top.
- pos.Y += (((float) imgsize.Y) / -2) + (((float) imgsize.Y) * i / 2);
+ // Labels are positioned by their center, not their top.
+ pos.Y += (((float) imgsize.Y) / -2) + (((float) imgsize.Y) * i / 2);
- rect = core::rect<s32>(
- pos.X, pos.Y,
- pos.X + font->getDimension(wlabel_plain.c_str()).Width,
- pos.Y + imgsize.Y);
+ rect = core::rect<s32>(
+ pos.X, pos.Y,
+ pos.X + font->getDimension(wlabel_plain.c_str()).Width,
+ pos.Y + imgsize.Y);
- } else {
- // Lines are spaced at the nominal distance of
- // 2/5 inventory slot, even if the font doesn't
- // quite match that. This provides consistent
- // form layout, at the expense of sometimes
- // having sub-optimal spacing for the font.
- // We multiply by 2 and then divide by 5, rather
- // than multiply by 0.4, to get exact results
- // in the integer cases: 0.4 is not exactly
- // representable in binary floating point.
-
- v2s32 pos = getElementBasePos(nullptr);
- pos.X += stof(v_pos[0]) * spacing.X;
- pos.Y += (stof(v_pos[1]) + 7.0f / 30.0f) * spacing.Y;
-
- pos.Y += ((float) i) * spacing.Y * 2.0 / 5.0;
-
- rect = core::rect<s32>(
- pos.X, pos.Y - m_btn_height,
- pos.X + font->getDimension(wlabel_plain.c_str()).Width,
- pos.Y + m_btn_height);
- }
+ } else {
+ // Lines are spaced at the nominal distance of
+ // 2/5 inventory slot, even if the font doesn't
+ // quite match that. This provides consistent
+ // form layout, at the expense of sometimes
+ // having sub-optimal spacing for the font.
+ // We multiply by 2 and then divide by 5, rather
+ // than multiply by 0.4, to get exact results
+ // in the integer cases: 0.4 is not exactly
+ // representable in binary floating point.
+
+ v2s32 pos = getElementBasePos(nullptr);
+ pos.X += stof(v_pos[0]) * spacing.X;
+ pos.Y += (stof(v_pos[1]) + 7.0f / 30.0f) * spacing.Y;
+
+ pos.Y += ((float) i) * spacing.Y * 2.0 / 5.0;
- FieldSpec spec(
- "",
- wlabel_colors,
- L"",
- 258 + m_fields.size(),
- 4
- );
- gui::IGUIStaticText *e = gui::StaticText::add(Environment,
- spec.flabel.c_str(), rect, false, false, data->current_parent,
- spec.fid);
- e->setTextAlignment(gui::EGUIA_UPPERLEFT, gui::EGUIA_CENTER);
+ rect = core::rect<s32>(
+ pos.X, pos.Y - m_btn_height,
+ pos.X + font->getDimension(wlabel_plain.c_str()).Width,
+ pos.Y + m_btn_height);
+ }
- e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
- e->setOverrideColor(style.getColor(StyleSpec::TEXTCOLOR, video::SColor(0xFFFFFFFF)));
- e->setOverrideFont(font);
+ FieldSpec spec(
+ "",
+ wlabel_colors,
+ L"",
+ 258 + m_fields.size(),
+ 4
+ );
+ gui::IGUIStaticText *e = gui::StaticText::add(Environment,
+ spec.flabel.c_str(), rect, false, false, data->current_parent,
+ spec.fid);
+ e->setTextAlignment(gui::EGUIA_UPPERLEFT, gui::EGUIA_CENTER);
- m_fields.push_back(spec);
+ e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
+ e->setOverrideColor(style.getColor(StyleSpec::TEXTCOLOR, video::SColor(0xFFFFFFFF)));
+ e->setOverrideFont(font);
- // labels should let events through
- e->grab();
- m_clickthrough_elements.push_back(e);
- }
+ m_fields.push_back(spec);
- return;
+ // labels should let events through
+ e->grab();
+ m_clickthrough_elements.push_back(e);
}
- errorstream << "Invalid label element(" << parts.size() << "): '" << element
- << "'" << std::endl;
}
void GUIFormSpecMenu::parseVertLabel(parserData* data, const std::string &element)
{
- std::vector<std::string> parts = split(element,';');
+ std::vector<std::string> parts;
+ if (!precheckElement("vertlabel", element, 2, 2, parts))
+ return;
- if ((parts.size() == 2) ||
- ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION)))
- {
- std::vector<std::string> v_pos = split(parts[0],',');
- std::wstring text = unescape_translate(
- unescape_string(utf8_to_wide(parts[1])));
+ std::vector<std::string> v_pos = split(parts[0],',');
+ std::wstring text = unescape_translate(
+ unescape_string(utf8_to_wide(parts[1])));
- MY_CHECKPOS("vertlabel",1);
+ MY_CHECKPOS("vertlabel",1);
- auto style = getDefaultStyleForElement("vertlabel", "", "label");
- gui::IGUIFont *font = style.getFont();
- if (!font)
- font = m_font;
+ auto style = getDefaultStyleForElement("vertlabel", "", "label");
+ gui::IGUIFont *font = style.getFont();
+ if (!font)
+ font = m_font;
- v2s32 pos;
- core::rect<s32> rect;
+ v2s32 pos;
+ core::rect<s32> rect;
- if (data->real_coordinates) {
- pos = getRealCoordinateBasePos(v_pos);
+ if (data->real_coordinates) {
+ pos = getRealCoordinateBasePos(v_pos);
- // Vertlabels are positioned by center, not left.
- pos.X -= imgsize.X / 2;
+ // Vertlabels are positioned by center, not left.
+ pos.X -= imgsize.X / 2;
- // We use text.length + 1 because without it, the rect
- // isn't quite tall enough and cuts off the text.
- rect = core::rect<s32>(pos.X, pos.Y,
- pos.X + imgsize.X,
- pos.Y + font_line_height(font) *
- (text.length() + 1));
+ // We use text.length + 1 because without it, the rect
+ // isn't quite tall enough and cuts off the text.
+ rect = core::rect<s32>(pos.X, pos.Y,
+ pos.X + imgsize.X,
+ pos.Y + font_line_height(font) *
+ (text.length() + 1));
- } else {
- pos = getElementBasePos(&v_pos);
+ } else {
+ pos = getElementBasePos(&v_pos);
- // As above, the length must be one longer. The width of
- // the rect (15 pixels) seems rather arbitrary, but
- // changing it might break something.
- rect = core::rect<s32>(
- pos.X, pos.Y+((imgsize.Y/2) - m_btn_height),
- pos.X+15, pos.Y +
- font_line_height(font) *
- (text.length() + 1) +
- ((imgsize.Y/2) - m_btn_height));
- }
+ // As above, the length must be one longer. The width of
+ // the rect (15 pixels) seems rather arbitrary, but
+ // changing it might break something.
+ rect = core::rect<s32>(
+ pos.X, pos.Y+((imgsize.Y/2) - m_btn_height),
+ pos.X+15, pos.Y +
+ font_line_height(font) *
+ (text.length() + 1) +
+ ((imgsize.Y/2) - m_btn_height));
+ }
- if(!data->explicit_size)
- warningstream<<"invalid use of label without a size[] element"<<std::endl;
+ if(!data->explicit_size)
+ warningstream<<"invalid use of label without a size[] element"<<std::endl;
- std::wstring label;
+ std::wstring label;
- for (wchar_t i : text) {
- label += i;
- label += L"\n";
- }
+ for (wchar_t i : text) {
+ label += i;
+ label += L"\n";
+ }
- FieldSpec spec(
- "",
- label,
- L"",
- 258 + m_fields.size()
- );
- gui::IGUIStaticText *e = gui::StaticText::add(Environment, spec.flabel.c_str(),
- rect, false, false, data->current_parent, spec.fid);
- e->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
+ FieldSpec spec(
+ "",
+ label,
+ L"",
+ 258 + m_fields.size()
+ );
+ gui::IGUIStaticText *e = gui::StaticText::add(Environment, spec.flabel.c_str(),
+ rect, false, false, data->current_parent, spec.fid);
+ e->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
- e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
- e->setOverrideColor(style.getColor(StyleSpec::TEXTCOLOR, video::SColor(0xFFFFFFFF)));
- e->setOverrideFont(font);
+ e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
+ e->setOverrideColor(style.getColor(StyleSpec::TEXTCOLOR, video::SColor(0xFFFFFFFF)));
+ e->setOverrideFont(font);
- m_fields.push_back(spec);
+ m_fields.push_back(spec);
- // vertlabels should let events through
- e->grab();
- m_clickthrough_elements.push_back(e);
- return;
- }
- errorstream<< "Invalid vertlabel element(" << parts.size() << "): '" << element << "'" << std::endl;
+ // vertlabels should let events through
+ e->grab();
+ m_clickthrough_elements.push_back(e);
}
void GUIFormSpecMenu::parseImageButton(parserData* data, const std::string &element,
const std::string &type)
{
- std::vector<std::string> parts = split(element,';');
-
- if ((((parts.size() >= 5) && (parts.size() <= 8)) && (parts.size() != 6)) ||
- ((parts.size() > 8) && (m_formspec_version > FORMSPEC_API_VERSION)))
- {
- std::vector<std::string> v_pos = split(parts[0],',');
- std::vector<std::string> v_geom = split(parts[1],',');
- std::string image_name = parts[2];
- std::string name = parts[3];
- std::string label = parts[4];
+ std::vector<std::string> parts;
+ if (!precheckElement("image_button", element, 5, 8, parts))
+ return;
- MY_CHECKPOS("imagebutton",0);
- MY_CHECKGEOM("imagebutton",1);
+ if (parts.size() == 6) {
+ // Invalid argument count.
+ errorstream << "Invalid image_button element(" << parts.size() << "): '" << element << "'" << std::endl;
+ return;
+ }
- std::string pressed_image_name;
+ std::vector<std::string> v_pos = split(parts[0],',');
+ std::vector<std::string> v_geom = split(parts[1],',');
+ std::string image_name = parts[2];
+ std::string name = parts[3];
+ std::string label = parts[4];
- if (parts.size() >= 8) {
- pressed_image_name = parts[7];
- }
+ MY_CHECKPOS("image_button",0);
+ MY_CHECKGEOM("image_button",1);
- v2s32 pos;
- v2s32 geom;
+ std::string pressed_image_name;
- if (data->real_coordinates) {
- pos = getRealCoordinateBasePos(v_pos);
- geom = getRealCoordinateGeometry(v_geom);
- } else {
- pos = getElementBasePos(&v_pos);
- geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X);
- geom.Y = (stof(v_geom[1]) * spacing.Y) - (spacing.Y - imgsize.Y);
- }
+ if (parts.size() >= 8) {
+ pressed_image_name = parts[7];
+ }
- core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X,
- pos.Y+geom.Y);
+ v2s32 pos;
+ v2s32 geom;
- if (!data->explicit_size)
- warningstream<<"invalid use of image_button without a size[] element"<<std::endl;
+ if (data->real_coordinates) {
+ pos = getRealCoordinateBasePos(v_pos);
+ geom = getRealCoordinateGeometry(v_geom);
+ } else {
+ pos = getElementBasePos(&v_pos);
+ geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X);
+ geom.Y = (stof(v_geom[1]) * spacing.Y) - (spacing.Y - imgsize.Y);
+ }
- image_name = unescape_string(image_name);
- pressed_image_name = unescape_string(pressed_image_name);
+ core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X,
+ pos.Y+geom.Y);
- std::wstring wlabel = utf8_to_wide(unescape_string(label));
+ if (!data->explicit_size)
+ warningstream<<"invalid use of image_button without a size[] element"<<std::endl;
- FieldSpec spec(
- name,
- wlabel,
- utf8_to_wide(image_name),
- 258 + m_fields.size()
- );
- spec.ftype = f_Button;
- if (type == "image_button_exit")
- spec.is_exit = true;
+ image_name = unescape_string(image_name);
+ pressed_image_name = unescape_string(pressed_image_name);
- GUIButtonImage *e = GUIButtonImage::addButton(Environment, rect, m_tsrc,
- data->current_parent, spec.fid, spec.flabel.c_str());
+ std::wstring wlabel = utf8_to_wide(unescape_string(label));
- if (spec.fname == m_focused_element) {
- Environment->setFocus(e);
- }
+ FieldSpec spec(
+ name,
+ wlabel,
+ utf8_to_wide(image_name),
+ 258 + m_fields.size()
+ );
+ spec.ftype = f_Button;
+ if (type == "image_button_exit")
+ spec.is_exit = true;
- auto style = getStyleForElement("image_button", spec.fname);
+ GUIButtonImage *e = GUIButtonImage::addButton(Environment, rect, m_tsrc,
+ data->current_parent, spec.fid, spec.flabel.c_str());
- spec.sound = style[StyleSpec::STATE_DEFAULT].get(StyleSpec::Property::SOUND, "");
+ if (spec.fname == m_focused_element) {
+ Environment->setFocus(e);
+ }
- // Override style properties with values specified directly in the element
- if (!image_name.empty())
- style[StyleSpec::STATE_DEFAULT].set(StyleSpec::FGIMG, image_name);
+ auto style = getStyleForElement("image_button", spec.fname);
- if (!pressed_image_name.empty())
- style[StyleSpec::STATE_PRESSED].set(StyleSpec::FGIMG, pressed_image_name);
+ spec.sound = style[StyleSpec::STATE_DEFAULT].get(StyleSpec::Property::SOUND, "");
- if (parts.size() >= 7) {
- style[StyleSpec::STATE_DEFAULT].set(StyleSpec::NOCLIP, parts[5]);
- style[StyleSpec::STATE_DEFAULT].set(StyleSpec::BORDER, parts[6]);
- }
+ // Override style properties with values specified directly in the element
+ if (!image_name.empty())
+ style[StyleSpec::STATE_DEFAULT].set(StyleSpec::FGIMG, image_name);
- e->setStyles(style);
- e->setScaleImage(true);
+ if (!pressed_image_name.empty())
+ style[StyleSpec::STATE_PRESSED].set(StyleSpec::FGIMG, pressed_image_name);
- m_fields.push_back(spec);
- return;
+ if (parts.size() >= 7) {
+ style[StyleSpec::STATE_DEFAULT].set(StyleSpec::NOCLIP, parts[5]);
+ style[StyleSpec::STATE_DEFAULT].set(StyleSpec::BORDER, parts[6]);
}
- errorstream<< "Invalid imagebutton element(" << parts.size() << "): '" << element << "'" << std::endl;
+ e->setStyles(style);
+ e->setScaleImage(true);
+
+ m_fields.push_back(spec);
}
void GUIFormSpecMenu::parseTabHeader(parserData* data, const std::string &element)
{
- std::vector<std::string> parts = split(element, ';');
-
- if (((parts.size() == 4) || (parts.size() == 6)) || (parts.size() == 7 &&
- data->real_coordinates) || ((parts.size() > 6) &&
- (m_formspec_version > FORMSPEC_API_VERSION)))
- {
- std::vector<std::string> v_pos = split(parts[0],',');
+ std::vector<std::string> parts;
+ if (!precheckElement("tabheader", element, 4, 7, parts))
+ return;
- // If we're using real coordinates, add an extra field for height.
- // Width is not here because tabs are the width of the text, and
- // there's no reason to change that.
- unsigned int i = 0;
- std::vector<std::string> v_geom = {"1", "1"}; // Dummy width and height
- bool auto_width = true;
- if (parts.size() == 7) {
- i++;
+ // Length 7: Additional "height" parameter after "pos". Only valid with real_coordinates.
+ // Note: New arguments for the "height" syntax cannot be added without breaking older clients.
+ if (parts.size() == 5 || (parts.size() == 7 && !data->real_coordinates)) {
+ errorstream << "Invalid tabheader element(" << parts.size() << "): '"
+ << element << "'" << std::endl;
+ return;
+ }
- v_geom = split(parts[1], ',');
- if (v_geom.size() == 1)
- v_geom.insert(v_geom.begin(), "1"); // Dummy value
- else
- auto_width = false;
- }
+ std::vector<std::string> v_pos = split(parts[0],',');
- std::string name = parts[i+1];
- std::vector<std::string> buttons = split(parts[i+2], ',');
- std::string str_index = parts[i+3];
- bool show_background = true;
- bool show_border = true;
- int tab_index = stoi(str_index) - 1;
+ // If we're using real coordinates, add an extra field for height.
+ // Width is not here because tabs are the width of the text, and
+ // there's no reason to change that.
+ unsigned int i = 0;
+ std::vector<std::string> v_geom = {"1", "1"}; // Dummy width and height
+ bool auto_width = true;
+ if (parts.size() == 7) {
+ i++;
+
+ v_geom = split(parts[1], ',');
+ if (v_geom.size() == 1)
+ v_geom.insert(v_geom.begin(), "1"); // Dummy value
+ else
+ auto_width = false;
+ }
- MY_CHECKPOS("tabheader", 0);
+ std::string name = parts[i+1];
+ std::vector<std::string> buttons = split(parts[i+2], ',');
+ std::string str_index = parts[i+3];
+ bool show_background = true;
+ bool show_border = true;
+ int tab_index = stoi(str_index) - 1;
- if (parts.size() == 6 + i) {
- if (parts[4+i] == "true")
- show_background = false;
- if (parts[5+i] == "false")
- show_border = false;
- }
+ MY_CHECKPOS("tabheader", 0);
- FieldSpec spec(
- name,
- L"",
- L"",
- 258 + m_fields.size()
- );
+ if (parts.size() == 6 + i) {
+ if (parts[4+i] == "true")
+ show_background = false;
+ if (parts[5+i] == "false")
+ show_border = false;
+ }
- spec.ftype = f_TabHeader;
+ FieldSpec spec(
+ name,
+ L"",
+ L"",
+ 258 + m_fields.size()
+ );
- v2s32 pos;
- v2s32 geom;
+ spec.ftype = f_TabHeader;
- if (data->real_coordinates) {
- pos = getRealCoordinateBasePos(v_pos);
+ v2s32 pos;
+ v2s32 geom;
- geom = getRealCoordinateGeometry(v_geom);
- // Set default height
- if (parts.size() <= 6)
- geom.Y = m_btn_height * 2;
- pos.Y -= geom.Y; // TabHeader base pos is the bottom, not the top.
- if (auto_width)
- geom.X = DesiredRect.getWidth(); // Set automatic width
-
- MY_CHECKGEOM("tabheader", 1);
- } else {
- v2f32 pos_f = pos_offset * spacing;
- pos_f.X += stof(v_pos[0]) * spacing.X;
- pos_f.Y += stof(v_pos[1]) * spacing.Y - m_btn_height * 2;
- pos = v2s32(pos_f.X, pos_f.Y);
+ if (data->real_coordinates) {
+ pos = getRealCoordinateBasePos(v_pos);
+ geom = getRealCoordinateGeometry(v_geom);
+ // Set default height
+ if (parts.size() <= 6)
geom.Y = m_btn_height * 2;
- geom.X = DesiredRect.getWidth();
- }
+ pos.Y -= geom.Y; // TabHeader base pos is the bottom, not the top.
+ if (auto_width)
+ geom.X = DesiredRect.getWidth(); // Set automatic width
- core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X,
- pos.Y+geom.Y);
+ MY_CHECKGEOM("tabheader", 1);
+ } else {
+ v2f32 pos_f = pos_offset * spacing;
+ pos_f.X += stof(v_pos[0]) * spacing.X;
+ pos_f.Y += stof(v_pos[1]) * spacing.Y - m_btn_height * 2;
+ pos = v2s32(pos_f.X, pos_f.Y);
- gui::IGUITabControl *e = Environment->addTabControl(rect,
- data->current_parent, show_background, show_border, spec.fid);
- e->setAlignment(irr::gui::EGUIA_UPPERLEFT, irr::gui::EGUIA_UPPERLEFT,
- irr::gui::EGUIA_UPPERLEFT, irr::gui::EGUIA_LOWERRIGHT);
- e->setTabHeight(geom.Y);
+ geom.Y = m_btn_height * 2;
+ geom.X = DesiredRect.getWidth();
+ }
- auto style = getDefaultStyleForElement("tabheader", name);
+ core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X,
+ pos.Y+geom.Y);
- spec.sound = style.get(StyleSpec::Property::SOUND, "");
+ gui::IGUITabControl *e = Environment->addTabControl(rect,
+ data->current_parent, show_background, show_border, spec.fid);
+ e->setAlignment(irr::gui::EGUIA_UPPERLEFT, irr::gui::EGUIA_UPPERLEFT,
+ irr::gui::EGUIA_UPPERLEFT, irr::gui::EGUIA_LOWERRIGHT);
+ e->setTabHeight(geom.Y);
- e->setNotClipped(style.getBool(StyleSpec::NOCLIP, true));
+ auto style = getDefaultStyleForElement("tabheader", name);
- for (const std::string &button : buttons) {
- auto tab = e->addTab(unescape_translate(unescape_string(
- utf8_to_wide(button))).c_str(), -1);
- if (style.isNotDefault(StyleSpec::BGCOLOR))
- tab->setBackgroundColor(style.getColor(StyleSpec::BGCOLOR));
+ spec.sound = style.get(StyleSpec::Property::SOUND, "");
- tab->setTextColor(style.getColor(StyleSpec::TEXTCOLOR, video::SColor(0xFFFFFFFF)));
- }
+ e->setNotClipped(style.getBool(StyleSpec::NOCLIP, true));
- if ((tab_index >= 0) &&
- (buttons.size() < INT_MAX) &&
- (tab_index < (int) buttons.size()))
- e->setActiveTab(tab_index);
+ for (const std::string &button : buttons) {
+ auto tab = e->addTab(unescape_translate(unescape_string(
+ utf8_to_wide(button))).c_str(), -1);
+ if (style.isNotDefault(StyleSpec::BGCOLOR))
+ tab->setBackgroundColor(style.getColor(StyleSpec::BGCOLOR));
- m_fields.push_back(spec);
- return;
+ tab->setTextColor(style.getColor(StyleSpec::TEXTCOLOR, video::SColor(0xFFFFFFFF)));
}
- errorstream << "Invalid TabHeader element(" << parts.size() << "): '"
- << element << "'" << std::endl;
+
+ if ((tab_index >= 0) &&
+ (buttons.size() < INT_MAX) &&
+ (tab_index < (int) buttons.size()))
+ e->setActiveTab(tab_index);
+
+ m_fields.push_back(spec);
}
void GUIFormSpecMenu::parseItemImageButton(parserData* data, const std::string &element)
{
- if (m_client == 0) {
- warningstream << "invalid use of item_image_button with m_client==0"
- << std::endl;
- return;
- }
+ MY_CHECKCLIENT("item_image_button");
- std::vector<std::string> parts = split(element,';');
+ std::vector<std::string> parts;
+ if (!precheckElement("item_image_button", element, 5, 5, parts))
+ return;
- if ((parts.size() == 5) ||
- ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
- {
- std::vector<std::string> v_pos = split(parts[0],',');
- std::vector<std::string> v_geom = split(parts[1],',');
- std::string item_name = parts[2];
- std::string name = parts[3];
- std::string label = parts[4];
+ std::vector<std::string> v_pos = split(parts[0],',');
+ std::vector<std::string> v_geom = split(parts[1],',');
+ std::string item_name = parts[2];
+ std::string name = parts[3];
+ std::string label = parts[4];
- label = unescape_string(label);
- item_name = unescape_string(item_name);
+ label = unescape_string(label);
+ item_name = unescape_string(item_name);
- MY_CHECKPOS("itemimagebutton",0);
- MY_CHECKGEOM("itemimagebutton",1);
+ MY_CHECKPOS("item_image_button",0);
+ MY_CHECKGEOM("item_image_button",1);
- v2s32 pos;
- v2s32 geom;
-
- if (data->real_coordinates) {
- pos = getRealCoordinateBasePos(v_pos);
- geom = getRealCoordinateGeometry(v_geom);
- } else {
- pos = getElementBasePos(&v_pos);
- geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X);
- geom.Y = (stof(v_geom[1]) * spacing.Y) - (spacing.Y - imgsize.Y);
- }
+ v2s32 pos;
+ v2s32 geom;
- core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
+ if (data->real_coordinates) {
+ pos = getRealCoordinateBasePos(v_pos);
+ geom = getRealCoordinateGeometry(v_geom);
+ } else {
+ pos = getElementBasePos(&v_pos);
+ geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X);
+ geom.Y = (stof(v_geom[1]) * spacing.Y) - (spacing.Y - imgsize.Y);
+ }
- if(!data->explicit_size)
- warningstream<<"invalid use of item_image_button without a size[] element"<<std::endl;
+ core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
- IItemDefManager *idef = m_client->idef();
- ItemStack item;
- item.deSerialize(item_name, idef);
+ if(!data->explicit_size)
+ warningstream<<"invalid use of item_image_button without a size[] element"<<std::endl;
- m_tooltips[name] =
- TooltipSpec(utf8_to_wide(item.getDefinition(idef).description),
- m_default_tooltip_bgcolor,
- m_default_tooltip_color);
+ IItemDefManager *idef = m_client->idef();
+ ItemStack item;
+ item.deSerialize(item_name, idef);
- // the spec for the button
- FieldSpec spec_btn(
- name,
- utf8_to_wide(label),
- utf8_to_wide(item_name),
- 258 + m_fields.size(),
- 2
- );
+ m_tooltips[name] =
+ TooltipSpec(utf8_to_wide(item.getDefinition(idef).description),
+ m_default_tooltip_bgcolor,
+ m_default_tooltip_color);
- GUIButtonItemImage *e_btn = GUIButtonItemImage::addButton(Environment,
- rect, m_tsrc, data->current_parent, spec_btn.fid, spec_btn.flabel.c_str(),
- item_name, m_client);
+ // the spec for the button
+ FieldSpec spec_btn(
+ name,
+ utf8_to_wide(label),
+ utf8_to_wide(item_name),
+ 258 + m_fields.size(),
+ 2
+ );
- auto style = getStyleForElement("item_image_button", spec_btn.fname, "image_button");
+ GUIButtonItemImage *e_btn = GUIButtonItemImage::addButton(Environment,
+ rect, m_tsrc, data->current_parent, spec_btn.fid, spec_btn.flabel.c_str(),
+ item_name, m_client);
- spec_btn.sound = style[StyleSpec::STATE_DEFAULT].get(StyleSpec::Property::SOUND, "");
+ auto style = getStyleForElement("item_image_button", spec_btn.fname, "image_button");
- e_btn->setStyles(style);
+ spec_btn.sound = style[StyleSpec::STATE_DEFAULT].get(StyleSpec::Property::SOUND, "");
- if (spec_btn.fname == m_focused_element) {
- Environment->setFocus(e_btn);
- }
+ e_btn->setStyles(style);
- spec_btn.ftype = f_Button;
- rect += data->basepos-padding;
- spec_btn.rect = rect;
- m_fields.push_back(spec_btn);
- return;
+ if (spec_btn.fname == m_focused_element) {
+ Environment->setFocus(e_btn);
}
- errorstream<< "Invalid ItemImagebutton element(" << parts.size() << "): '" << element << "'" << std::endl;
+
+ spec_btn.ftype = f_Button;
+ rect += data->basepos-padding;
+ spec_btn.rect = rect;
+ m_fields.push_back(spec_btn);
}
void GUIFormSpecMenu::parseBox(parserData* data, const std::string &element)
{
- std::vector<std::string> parts = split(element, ';');
+ std::vector<std::string> parts;
+ if (!precheckElement("box", element, 3, 3, parts))
+ return;
- if ((parts.size() == 3) ||
- ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
- {
- std::vector<std::string> v_pos = split(parts[0], ',');
- std::vector<std::string> v_geom = split(parts[1], ',');
+ std::vector<std::string> v_pos = split(parts[0], ',');
+ std::vector<std::string> v_geom = split(parts[1], ',');
- MY_CHECKPOS("box", 0);
- MY_CHECKGEOM("box", 1);
+ MY_CHECKPOS("box", 0);
+ MY_CHECKGEOM("box", 1);
- v2s32 pos;
- v2s32 geom;
+ v2s32 pos;
+ v2s32 geom;
- if (data->real_coordinates) {
- pos = getRealCoordinateBasePos(v_pos);
- geom = getRealCoordinateGeometry(v_geom);
- } else {
- pos = getElementBasePos(&v_pos);
- geom.X = stof(v_geom[0]) * spacing.X;
- geom.Y = stof(v_geom[1]) * spacing.Y;
- }
+ if (data->real_coordinates) {
+ pos = getRealCoordinateBasePos(v_pos);
+ geom = getRealCoordinateGeometry(v_geom);
+ } else {
+ pos = getElementBasePos(&v_pos);
+ geom.X = stof(v_geom[0]) * spacing.X;
+ geom.Y = stof(v_geom[1]) * spacing.Y;
+ }
- FieldSpec spec(
- "",
- L"",
- L"",
- 258 + m_fields.size(),
- -2
- );
- spec.ftype = f_Box;
+ FieldSpec spec(
+ "",
+ L"",
+ L"",
+ 258 + m_fields.size(),
+ -2
+ );
+ spec.ftype = f_Box;
- auto style = getDefaultStyleForElement("box", spec.fname);
+ auto style = getDefaultStyleForElement("box", spec.fname);
- video::SColor tmp_color;
- std::array<video::SColor, 4> colors;
- std::array<video::SColor, 4> bordercolors = {0x0, 0x0, 0x0, 0x0};
- std::array<s32, 4> borderwidths = {0, 0, 0, 0};
+ video::SColor tmp_color;
+ std::array<video::SColor, 4> colors;
+ std::array<video::SColor, 4> bordercolors = {0x0, 0x0, 0x0, 0x0};
+ std::array<s32, 4> borderwidths = {0, 0, 0, 0};
- if (parseColorString(parts[2], tmp_color, true, 0x8C)) {
- colors = {tmp_color, tmp_color, tmp_color, tmp_color};
- } else {
- colors = style.getColorArray(StyleSpec::COLORS, {0x0, 0x0, 0x0, 0x0});
- bordercolors = style.getColorArray(StyleSpec::BORDERCOLORS,
- {0x0, 0x0, 0x0, 0x0});
- borderwidths = style.getIntArray(StyleSpec::BORDERWIDTHS, {0, 0, 0, 0});
- }
+ if (parseColorString(parts[2], tmp_color, true, 0x8C)) {
+ colors = {tmp_color, tmp_color, tmp_color, tmp_color};
+ } else {
+ colors = style.getColorArray(StyleSpec::COLORS, {0x0, 0x0, 0x0, 0x0});
+ bordercolors = style.getColorArray(StyleSpec::BORDERCOLORS,
+ {0x0, 0x0, 0x0, 0x0});
+ borderwidths = style.getIntArray(StyleSpec::BORDERWIDTHS, {0, 0, 0, 0});
+ }
- core::rect<s32> rect(pos, pos + geom);
+ core::rect<s32> rect(pos, pos + geom);
- GUIBox *e = new GUIBox(Environment, data->current_parent, spec.fid, rect,
- colors, bordercolors, borderwidths);
- e->setNotClipped(style.getBool(StyleSpec::NOCLIP, m_formspec_version < 3));
- e->drop();
+ GUIBox *e = new GUIBox(Environment, data->current_parent, spec.fid, rect,
+ colors, bordercolors, borderwidths);
+ e->setNotClipped(style.getBool(StyleSpec::NOCLIP, m_formspec_version < 3));
+ e->drop();
- m_fields.push_back(spec);
- return;
- }
- errorstream << "Invalid Box element(" << parts.size() << "): '" << element
- << "'" << std::endl;
+ m_fields.push_back(spec);
}
void GUIFormSpecMenu::parseBackgroundColor(parserData* data, const std::string &element)
{
- std::vector<std::string> parts = split(element,';');
+ std::vector<std::string> parts;
+ if (!precheckElement("bgcolor", element, 1, 3, parts))
+ return;
+
const u32 parameter_count = parts.size();
- if ((parameter_count > 2 && m_formspec_version < 3) ||
- (parameter_count > 3 && m_formspec_version <= FORMSPEC_API_VERSION)) {
+ if (parameter_count > 2 && m_formspec_version < 3) {
errorstream << "Invalid bgcolor element(" << parameter_count << "): '"
<< element << "'" << std::endl;
return;
@@ -2357,49 +2286,51 @@ void GUIFormSpecMenu::parseBackgroundColor(parserData* data, const std::string &
void GUIFormSpecMenu::parseListColors(parserData* data, const std::string &element)
{
- std::vector<std::string> parts = split(element,';');
+ std::vector<std::string> parts;
+ // Legacy Note: If clients older than 5.5.0-dev are supplied with additional arguments,
+ // the tooltip colors will be ignored.
+ if (!precheckElement("listcolors", element, 2, 5, parts))
+ return;
- if (((parts.size() == 2) || (parts.size() == 3) || (parts.size() == 5)) ||
- ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
- {
- parseColorString(parts[0], data->inventorylist_options.slotbg_n, false);
- parseColorString(parts[1], data->inventorylist_options.slotbg_h, false);
+ if (parts.size() == 4) {
+ // Invalid argument combination
+ errorstream << "Invalid listcolors element(" << parts.size() << "): '"
+ << element << "'" << std::endl;
+ return;
+ }
- if (parts.size() >= 3) {
- if (parseColorString(parts[2], data->inventorylist_options.slotbordercolor,
- false)) {
- data->inventorylist_options.slotborder = true;
- }
- }
- if (parts.size() == 5) {
- video::SColor tmp_color;
+ parseColorString(parts[0], data->inventorylist_options.slotbg_n, false);
+ parseColorString(parts[1], data->inventorylist_options.slotbg_h, false);
- if (parseColorString(parts[3], tmp_color, false))
- m_default_tooltip_bgcolor = tmp_color;
- if (parseColorString(parts[4], tmp_color, false))
- m_default_tooltip_color = tmp_color;
+ if (parts.size() >= 3) {
+ if (parseColorString(parts[2], data->inventorylist_options.slotbordercolor,
+ false)) {
+ data->inventorylist_options.slotborder = true;
}
+ }
+ if (parts.size() >= 5) {
+ video::SColor tmp_color;
- // update all already parsed inventorylists
- for (GUIInventoryList *e : m_inventorylists) {
- e->setSlotBGColors(data->inventorylist_options.slotbg_n,
- data->inventorylist_options.slotbg_h);
- e->setSlotBorders(data->inventorylist_options.slotborder,
- data->inventorylist_options.slotbordercolor);
- }
- return;
+ if (parseColorString(parts[3], tmp_color, false))
+ m_default_tooltip_bgcolor = tmp_color;
+ if (parseColorString(parts[4], tmp_color, false))
+ m_default_tooltip_color = tmp_color;
+ }
+
+ // update all already parsed inventorylists
+ for (GUIInventoryList *e : m_inventorylists) {
+ e->setSlotBGColors(data->inventorylist_options.slotbg_n,
+ data->inventorylist_options.slotbg_h);
+ e->setSlotBorders(data->inventorylist_options.slotborder,
+ data->inventorylist_options.slotbordercolor);
}
- errorstream<< "Invalid listcolors element(" << parts.size() << "): '" << element << "'" << std::endl;
}
void GUIFormSpecMenu::parseTooltip(parserData* data, const std::string &element)
{
- std::vector<std::string> parts = split(element,';');
- if (parts.size() < 2) {
- errorstream << "Invalid tooltip element(" << parts.size() << "): '"
- << element << "'" << std::endl;
+ std::vector<std::string> parts;
+ if (!precheckElement("tooltip", element, 2, 5, parts))
return;
- }
// Get mode and check size
bool rect_mode = parts[0].find(',') != std::string::npos;
@@ -2539,11 +2470,16 @@ bool GUIFormSpecMenu::parsePositionDirect(parserData *data, const std::string &e
void GUIFormSpecMenu::parsePosition(parserData *data, const std::string &element)
{
- std::vector<std::string> parts = split(element, ',');
+ std::vector<std::string> parts = split(element, ';');
- if (parts.size() == 2) {
- data->offset.X = stof(parts[0]);
- data->offset.Y = stof(parts[1]);
+ if (parts.size() == 1 ||
+ (parts.size() > 1 && m_formspec_version > FORMSPEC_API_VERSION)) {
+ std::vector<std::string> v_geom = split(parts[0], ',');
+
+ MY_CHECKGEOM("position", 0);
+
+ data->offset.X = stof(v_geom[0]);
+ data->offset.Y = stof(v_geom[1]);
return;
}
@@ -2573,11 +2509,16 @@ bool GUIFormSpecMenu::parseAnchorDirect(parserData *data, const std::string &ele
void GUIFormSpecMenu::parseAnchor(parserData *data, const std::string &element)
{
- std::vector<std::string> parts = split(element, ',');
+ std::vector<std::string> parts = split(element, ';');
- if (parts.size() == 2) {
- data->anchor.X = stof(parts[0]);
- data->anchor.Y = stof(parts[1]);
+ if (parts.size() == 1 ||
+ (parts.size() > 1 && m_formspec_version > FORMSPEC_API_VERSION)) {
+ std::vector<std::string> v_geom = split(parts[0], ',');
+
+ MY_CHECKGEOM("anchor", 0);
+
+ data->anchor.X = stof(v_geom[0]);
+ data->anchor.Y = stof(v_geom[1]);
return;
}
@@ -2585,6 +2526,46 @@ void GUIFormSpecMenu::parseAnchor(parserData *data, const std::string &element)
<< "'" << std::endl;
}
+bool GUIFormSpecMenu::parsePaddingDirect(parserData *data, const std::string &element)
+{
+ if (element.empty())
+ return false;
+
+ std::vector<std::string> parts = split(element, '[');
+
+ if (parts.size() != 2)
+ return false;
+
+ std::string type = trim(parts[0]);
+ std::string description = trim(parts[1]);
+
+ if (type != "padding")
+ return false;
+
+ parsePadding(data, description);
+
+ return true;
+}
+
+void GUIFormSpecMenu::parsePadding(parserData *data, const std::string &element)
+{
+ std::vector<std::string> parts = split(element, ';');
+
+ if (parts.size() == 1 ||
+ (parts.size() > 1 && m_formspec_version > FORMSPEC_API_VERSION)) {
+ std::vector<std::string> v_geom = split(parts[0], ',');
+
+ MY_CHECKGEOM("padding", 0);
+
+ data->padding.X = stof(v_geom[0]);
+ data->padding.Y = stof(v_geom[1]);
+ return;
+ }
+
+ errorstream << "Invalid padding element (" << parts.size() << "): '" << element
+ << "'" << std::endl;
+}
+
bool GUIFormSpecMenu::parseStyle(parserData *data, const std::string &element, bool style_type)
{
std::vector<std::string> parts = split(element, ';');
@@ -2723,39 +2704,29 @@ bool GUIFormSpecMenu::parseStyle(parserData *data, const std::string &element, b
void GUIFormSpecMenu::parseSetFocus(const std::string &element)
{
- std::vector<std::string> parts = split(element, ';');
-
- if (parts.size() <= 2 ||
- (parts.size() > 2 && m_formspec_version > FORMSPEC_API_VERSION))
- {
- if (m_is_form_regenerated)
- return; // Never focus on resizing
-
- bool force_focus = parts.size() >= 2 && is_yes(parts[1]);
- if (force_focus || m_text_dst->m_formname != m_last_formname)
- setFocus(parts[0]);
-
+ std::vector<std::string> parts;
+ if (!precheckElement("set_focus", element, 1, 2, parts))
return;
- }
- errorstream << "Invalid set_focus element (" << parts.size() << "): '" << element
- << "'" << std::endl;
+ if (m_is_form_regenerated)
+ return; // Never focus on resizing
+
+ bool force_focus = parts.size() >= 2 && is_yes(parts[1]);
+ if (force_focus || m_text_dst->m_formname != m_last_formname)
+ setFocus(parts[0]);
}
void GUIFormSpecMenu::parseModel(parserData *data, const std::string &element)
{
- std::vector<std::string> parts = split(element, ';');
+ MY_CHECKCLIENT("model");
- if (parts.size() < 5 || (parts.size() > 9 &&
- m_formspec_version <= FORMSPEC_API_VERSION)) {
- errorstream << "Invalid model element (" << parts.size() << "): '" << element
- << "'" << std::endl;
+ std::vector<std::string> parts;
+ if (!precheckElement("model", element, 5, 10, parts))
return;
- }
// Avoid length checks by resizing
- if (parts.size() < 9)
- parts.resize(9);
+ if (parts.size() < 10)
+ parts.resize(10);
std::vector<std::string> v_pos = split(parts[0], ',');
std::vector<std::string> v_geom = split(parts[1], ',');
@@ -2766,6 +2737,7 @@ void GUIFormSpecMenu::parseModel(parserData *data, const std::string &element)
bool inf_rotation = is_yes(parts[6]);
bool mousectrl = is_yes(parts[7]) || parts[7].empty(); // default true
std::vector<std::string> frame_loop = split(parts[8], ',');
+ std::string speed = unescape_string(parts[9]);
MY_CHECKPOS("model", 0);
MY_CHECKGEOM("model", 1);
@@ -2802,7 +2774,7 @@ void GUIFormSpecMenu::parseModel(parserData *data, const std::string &element)
core::rect<s32> rect(pos, pos + geom);
- GUIScene *e = new GUIScene(Environment, RenderingEngine::get_scene_manager(),
+ GUIScene *e = new GUIScene(Environment, m_client->getSceneManager(),
data->current_parent, rect, spec.fid);
auto meshnode = e->setMesh(mesh);
@@ -2825,6 +2797,7 @@ void GUIFormSpecMenu::parseModel(parserData *data, const std::string &element)
}
e->setFrameLoop(frame_loop_begin, frame_loop_end);
+ e->setAnimationSpeed(stof(speed));
auto style = getStyleForElement("model", spec.fname);
e->setStyles(style);
@@ -3099,6 +3072,7 @@ void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
mydata.screensize = screensize;
mydata.offset = v2f32(0.5f, 0.5f);
mydata.anchor = v2f32(0.5f, 0.5f);
+ mydata.padding = v2f32(0.05f, 0.05f);
mydata.simple_field_count = 0;
// Base position of contents of form
@@ -3201,7 +3175,14 @@ void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
}
}
- /* "no_prepend" element is always after "position" (or "size" element) if it used */
+ /* "padding" element is always after "anchor" and previous if it is used */
+ for (; i < elements.size(); i++) {
+ if (!parsePaddingDirect(&mydata, elements[i])) {
+ break;
+ }
+ }
+
+ /* "no_prepend" element is always after "padding" and previous if it used */
bool enable_prepends = true;
for (; i < elements.size(); i++) {
if (elements[i].empty())
@@ -3266,11 +3247,9 @@ void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
double fitx_imgsize;
double fity_imgsize;
- // Pad the screensize with 5% of the screensize on all sides to ensure
- // that even the largest formspecs don't touch the screen borders.
v2f padded_screensize(
- mydata.screensize.X * 0.9f,
- mydata.screensize.Y * 0.9f
+ mydata.screensize.X * (1.0f - mydata.padding.X * 2.0f),
+ mydata.screensize.Y * (1.0f - mydata.padding.Y * 2.0f)
);
if (mydata.real_coordinates) {
@@ -3286,13 +3265,15 @@ void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
((15.0 / 13.0) * (0.85 + mydata.invsize.Y));
}
-#ifdef __ANDROID__
+ s32 min_screen_dim = std::min(padded_screensize.X, padded_screensize.Y);
+
+#ifdef HAVE_TOUCHSCREENGUI
// In Android, the preferred imgsize should be larger to accommodate the
// smaller screensize.
- double prefer_imgsize = padded_screensize.Y / 10 * gui_scaling;
+ double prefer_imgsize = min_screen_dim / 10 * gui_scaling;
#else
// Desktop computers have more space, so try to fit 15 coordinates.
- double prefer_imgsize = padded_screensize.Y / 15 * gui_scaling;
+ double prefer_imgsize = min_screen_dim / 15 * gui_scaling;
#endif
// Try to use the preferred imgsize, but if that's bigger than the maximum
// size, use the maximum size.
@@ -3749,7 +3730,7 @@ void GUIFormSpecMenu::showTooltip(const std::wstring &text,
v2u32 screenSize = Environment->getVideoDriver()->getScreenSize();
int tooltip_offset_x = m_btn_height;
int tooltip_offset_y = m_btn_height;
-#ifdef __ANDROID__
+#ifdef HAVE_TOUCHSCREENGUI
tooltip_offset_x *= 3;
tooltip_offset_y = 0;
if (m_pointer.X > (s32)screenSize.X / 2)
@@ -3941,9 +3922,7 @@ void GUIFormSpecMenu::acceptInput(FormspecQuitMode quitmode)
}
if (e != 0) {
- std::stringstream ss;
- ss << (e->getActiveTab() +1);
- fields[name] = ss.str();
+ fields[name] = itos(e->getActiveTab() + 1);
}
} else if (s.ftype == f_CheckBox) {
// No dynamic cast possible due to some distributions shipped
@@ -3969,12 +3948,10 @@ void GUIFormSpecMenu::acceptInput(FormspecQuitMode quitmode)
e = static_cast<GUIScrollBar *>(element);
if (e) {
- std::stringstream os;
- os << e->getPos();
if (s.fdefault == L"Changed")
- fields[name] = "CHG:" + os.str();
+ fields[name] = "CHG:" + itos(e->getPos());
else
- fields[name] = "VAL:" + os.str();
+ fields[name] = "VAL:" + itos(e->getPos());
}
} else if (s.ftype == f_AnimatedImage) {
// No dynamic cast possible due to some distributions shipped
diff --git a/src/gui/guiFormSpecMenu.h b/src/gui/guiFormSpecMenu.h
index d658aba7b..0b4d3879d 100644
--- a/src/gui/guiFormSpecMenu.h
+++ b/src/gui/guiFormSpecMenu.h
@@ -152,6 +152,7 @@ public:
gui::IGUIElement* parent, s32 id,
IMenuManager *menumgr,
Client *client,
+ gui::IGUIEnvironment *guienv,
ISimpleTextureSource *tsrc,
ISoundManager *sound_manager,
IFormSource* fs_src,
@@ -162,8 +163,9 @@ public:
~GUIFormSpecMenu();
static void create(GUIFormSpecMenu *&cur_formspec, Client *client,
- JoystickController *joystick, IFormSource *fs_src, TextDest *txt_dest,
- const std::string &formspecPrepend, ISoundManager *sound_manager);
+ gui::IGUIEnvironment *guienv, JoystickController *joystick, IFormSource *fs_src,
+ TextDest *txt_dest, const std::string &formspecPrepend,
+ ISoundManager *sound_manager);
void setFormSpec(const std::string &formspec_string,
const InventoryLocation &current_inventory_location)
@@ -227,7 +229,7 @@ public:
return m_selected_item;
}
- const u16 getSelectedAmount() const
+ u16 getSelectedAmount() const
{
return m_selected_amount;
}
@@ -277,6 +279,8 @@ protected:
v2s32 getElementBasePos(const std::vector<std::string> *v_pos);
v2s32 getRealCoordinateBasePos(const std::vector<std::string> &v_pos);
v2s32 getRealCoordinateGeometry(const std::vector<std::string> &v_geom);
+ bool precheckElement(const std::string &name, const std::string &element,
+ size_t args_min, size_t args_max, std::vector<std::string> &parts);
std::unordered_map<std::string, std::vector<StyleSpec>> theme_by_type;
std::unordered_map<std::string, std::vector<StyleSpec>> theme_by_name;
@@ -364,6 +368,7 @@ private:
v2s32 size;
v2f32 offset;
v2f32 anchor;
+ v2f32 padding;
core::rect<s32> rect;
v2s32 basepos;
v2u32 screensize;
@@ -445,6 +450,8 @@ private:
void parsePosition(parserData *data, const std::string &element);
bool parseAnchorDirect(parserData *data, const std::string &element);
void parseAnchor(parserData *data, const std::string &element);
+ bool parsePaddingDirect(parserData *data, const std::string &element);
+ void parsePadding(parserData *data, const std::string &element);
bool parseStyle(parserData *data, const std::string &element, bool style_type);
void parseSetFocus(const std::string &element);
void parseModel(parserData *data, const std::string &element);
diff --git a/src/gui/guiHyperText.cpp b/src/gui/guiHyperText.cpp
index ccfdcb81d..40450ce5f 100644
--- a/src/gui/guiHyperText.cpp
+++ b/src/gui/guiHyperText.cpp
@@ -17,31 +17,26 @@ with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
-#include "IGUIEnvironment.h"
-#include "IGUIElement.h"
+#include "guiHyperText.h"
#include "guiScrollBar.h"
-#include "IGUIFont.h"
-#include <vector>
-#include <list>
-#include <unordered_map>
-using namespace irr::gui;
#include "client/fontengine.h"
-#include <SColor.h>
#include "client/tile.h"
#include "IVideoDriver.h"
#include "client/client.h"
#include "client/renderingengine.h"
#include "hud.h"
-#include "guiHyperText.h"
#include "util/string.h"
+#include "irrlicht_changes/CGUITTFont.h"
-bool check_color(const std::string &str)
+using namespace irr::gui;
+
+static bool check_color(const std::string &str)
{
irr::video::SColor color;
return parseColorString(str, color, false);
}
-bool check_integer(const std::string &str)
+static bool check_integer(const std::string &str)
{
if (str.empty())
return false;
@@ -616,12 +611,10 @@ TextDrawer::TextDrawer(const wchar_t *text, Client *client,
if (e.font) {
e.dim.Width = e.font->getDimension(e.text.c_str()).Width;
e.dim.Height = e.font->getDimension(L"Yy").Height;
-#if USE_FREETYPE
if (e.font->getType() == irr::gui::EGFT_CUSTOM) {
- e.baseline = e.dim.Height - 1 -
- ((irr::gui::CGUITTFont *)e.font)->getAscender() / 64;
+ CGUITTFont *tmp = static_cast<CGUITTFont*>(e.font);
+ e.baseline = e.dim.Height - 1 - tmp->getAscender() / 64;
}
-#endif
} else {
e.dim = {0, 0};
}
diff --git a/src/gui/guiHyperText.h b/src/gui/guiHyperText.h
index 5b936262e..04c664df5 100644
--- a/src/gui/guiHyperText.h
+++ b/src/gui/guiHyperText.h
@@ -19,16 +19,17 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#pragma once
-#include "config.h" // for USE_FREETYPE
+#include <vector>
+#include <list>
+#include <unordered_map>
+#include <string>
+#include "irrlichttypes_extrabloated.h"
using namespace irr;
class ISimpleTextureSource;
class Client;
-
-#if USE_FREETYPE
-#include "irrlicht_changes/CGUITTFont.h"
-#endif
+class GUIScrollBar;
class ParsedText
{
diff --git a/src/gui/guiKeyChangeMenu.cpp b/src/gui/guiKeyChangeMenu.cpp
index 4dcb47779..29d5138f0 100644
--- a/src/gui/guiKeyChangeMenu.cpp
+++ b/src/gui/guiKeyChangeMenu.cpp
@@ -46,7 +46,7 @@ enum
GUI_ID_KEY_BACKWARD_BUTTON,
GUI_ID_KEY_LEFT_BUTTON,
GUI_ID_KEY_RIGHT_BUTTON,
- GUI_ID_KEY_USE_BUTTON,
+ GUI_ID_KEY_AUX1_BUTTON,
GUI_ID_KEY_FLY_BUTTON,
GUI_ID_KEY_FAST_BUTTON,
GUI_ID_KEY_JUMP_BUTTON,
@@ -70,6 +70,7 @@ enum
GUI_ID_KEY_MINIMAP_BUTTON,
GUI_ID_KEY_SCREENSHOT_BUTTON,
GUI_ID_KEY_CHATLOG_BUTTON,
+ GUI_ID_KEY_BLOCK_BOUNDS_BUTTON,
GUI_ID_KEY_HUD_BUTTON,
GUI_ID_KEY_FOG_BUTTON,
GUI_ID_KEY_DEC_RANGE_BUTTON,
@@ -177,7 +178,7 @@ void GUIKeyChangeMenu::regenerateGui(v2u32 screensize)
{
core::rect<s32> rect(0, 0, option_w, 30 * s);
rect += topleft + v2s32(option_x, option_y);
- const wchar_t *text = wgettext("\"Special\" = climb down");
+ const wchar_t *text = wgettext("\"Aux1\" = climb down");
Environment->addCheckBox(g_settings->getBool("aux1_descends"), rect, this,
GUI_ID_CB_AUX1_DESCENDS, text);
delete[] text;
@@ -412,37 +413,38 @@ void GUIKeyChangeMenu::add_key(int id, const wchar_t *button_name, const std::st
void GUIKeyChangeMenu::init_keys()
{
- this->add_key(GUI_ID_KEY_FORWARD_BUTTON, wgettext("Forward"), "keymap_forward");
- this->add_key(GUI_ID_KEY_BACKWARD_BUTTON, wgettext("Backward"), "keymap_backward");
- this->add_key(GUI_ID_KEY_LEFT_BUTTON, wgettext("Left"), "keymap_left");
- this->add_key(GUI_ID_KEY_RIGHT_BUTTON, wgettext("Right"), "keymap_right");
- this->add_key(GUI_ID_KEY_USE_BUTTON, wgettext("Special"), "keymap_special1");
- this->add_key(GUI_ID_KEY_JUMP_BUTTON, wgettext("Jump"), "keymap_jump");
- this->add_key(GUI_ID_KEY_SNEAK_BUTTON, wgettext("Sneak"), "keymap_sneak");
- this->add_key(GUI_ID_KEY_DROP_BUTTON, wgettext("Drop"), "keymap_drop");
- this->add_key(GUI_ID_KEY_INVENTORY_BUTTON, wgettext("Inventory"), "keymap_inventory");
- this->add_key(GUI_ID_KEY_HOTBAR_PREV_BUTTON,wgettext("Prev. item"), "keymap_hotbar_previous");
- this->add_key(GUI_ID_KEY_HOTBAR_NEXT_BUTTON,wgettext("Next item"), "keymap_hotbar_next");
- this->add_key(GUI_ID_KEY_ZOOM_BUTTON, wgettext("Zoom"), "keymap_zoom");
- this->add_key(GUI_ID_KEY_CAMERA_BUTTON, wgettext("Change camera"), "keymap_camera_mode");
- this->add_key(GUI_ID_KEY_MINIMAP_BUTTON, wgettext("Toggle minimap"), "keymap_minimap");
- this->add_key(GUI_ID_KEY_FLY_BUTTON, wgettext("Toggle fly"), "keymap_freemove");
- this->add_key(GUI_ID_KEY_PITCH_MOVE, wgettext("Toggle pitchmove"), "keymap_pitchmove");
- this->add_key(GUI_ID_KEY_FAST_BUTTON, wgettext("Toggle fast"), "keymap_fastmove");
- this->add_key(GUI_ID_KEY_NOCLIP_BUTTON, wgettext("Toggle noclip"), "keymap_noclip");
- this->add_key(GUI_ID_KEY_MUTE_BUTTON, wgettext("Mute"), "keymap_mute");
- this->add_key(GUI_ID_KEY_DEC_VOLUME_BUTTON,wgettext("Dec. volume"), "keymap_decrease_volume");
- this->add_key(GUI_ID_KEY_INC_VOLUME_BUTTON,wgettext("Inc. volume"), "keymap_increase_volume");
- this->add_key(GUI_ID_KEY_AUTOFWD_BUTTON, wgettext("Autoforward"), "keymap_autoforward");
- this->add_key(GUI_ID_KEY_CHAT_BUTTON, wgettext("Chat"), "keymap_chat");
- this->add_key(GUI_ID_KEY_SCREENSHOT_BUTTON,wgettext("Screenshot"), "keymap_screenshot");
- this->add_key(GUI_ID_KEY_RANGE_BUTTON, wgettext("Range select"), "keymap_rangeselect");
- this->add_key(GUI_ID_KEY_DEC_RANGE_BUTTON, wgettext("Dec. range"), "keymap_decrease_viewing_range_min");
- this->add_key(GUI_ID_KEY_INC_RANGE_BUTTON, wgettext("Inc. range"), "keymap_increase_viewing_range_min");
- this->add_key(GUI_ID_KEY_CONSOLE_BUTTON, wgettext("Console"), "keymap_console");
- this->add_key(GUI_ID_KEY_CMD_BUTTON, wgettext("Command"), "keymap_cmd");
- this->add_key(GUI_ID_KEY_CMD_LOCAL_BUTTON, wgettext("Local command"), "keymap_cmd_local");
- this->add_key(GUI_ID_KEY_HUD_BUTTON, wgettext("Toggle HUD"), "keymap_toggle_hud");
- this->add_key(GUI_ID_KEY_CHATLOG_BUTTON, wgettext("Toggle chat log"), "keymap_toggle_chat");
- this->add_key(GUI_ID_KEY_FOG_BUTTON, wgettext("Toggle fog"), "keymap_toggle_fog");
+ this->add_key(GUI_ID_KEY_FORWARD_BUTTON, wgettext("Forward"), "keymap_forward");
+ this->add_key(GUI_ID_KEY_BACKWARD_BUTTON, wgettext("Backward"), "keymap_backward");
+ this->add_key(GUI_ID_KEY_LEFT_BUTTON, wgettext("Left"), "keymap_left");
+ this->add_key(GUI_ID_KEY_RIGHT_BUTTON, wgettext("Right"), "keymap_right");
+ this->add_key(GUI_ID_KEY_AUX1_BUTTON, wgettext("Aux1"), "keymap_aux1");
+ this->add_key(GUI_ID_KEY_JUMP_BUTTON, wgettext("Jump"), "keymap_jump");
+ this->add_key(GUI_ID_KEY_SNEAK_BUTTON, wgettext("Sneak"), "keymap_sneak");
+ this->add_key(GUI_ID_KEY_DROP_BUTTON, wgettext("Drop"), "keymap_drop");
+ this->add_key(GUI_ID_KEY_INVENTORY_BUTTON, wgettext("Inventory"), "keymap_inventory");
+ this->add_key(GUI_ID_KEY_HOTBAR_PREV_BUTTON, wgettext("Prev. item"), "keymap_hotbar_previous");
+ this->add_key(GUI_ID_KEY_HOTBAR_NEXT_BUTTON, wgettext("Next item"), "keymap_hotbar_next");
+ this->add_key(GUI_ID_KEY_ZOOM_BUTTON, wgettext("Zoom"), "keymap_zoom");
+ this->add_key(GUI_ID_KEY_CAMERA_BUTTON, wgettext("Change camera"), "keymap_camera_mode");
+ this->add_key(GUI_ID_KEY_MINIMAP_BUTTON, wgettext("Toggle minimap"), "keymap_minimap");
+ this->add_key(GUI_ID_KEY_FLY_BUTTON, wgettext("Toggle fly"), "keymap_freemove");
+ this->add_key(GUI_ID_KEY_PITCH_MOVE, wgettext("Toggle pitchmove"), "keymap_pitchmove");
+ this->add_key(GUI_ID_KEY_FAST_BUTTON, wgettext("Toggle fast"), "keymap_fastmove");
+ this->add_key(GUI_ID_KEY_NOCLIP_BUTTON, wgettext("Toggle noclip"), "keymap_noclip");
+ this->add_key(GUI_ID_KEY_MUTE_BUTTON, wgettext("Mute"), "keymap_mute");
+ this->add_key(GUI_ID_KEY_DEC_VOLUME_BUTTON, wgettext("Dec. volume"), "keymap_decrease_volume");
+ this->add_key(GUI_ID_KEY_INC_VOLUME_BUTTON, wgettext("Inc. volume"), "keymap_increase_volume");
+ this->add_key(GUI_ID_KEY_AUTOFWD_BUTTON, wgettext("Autoforward"), "keymap_autoforward");
+ this->add_key(GUI_ID_KEY_CHAT_BUTTON, wgettext("Chat"), "keymap_chat");
+ this->add_key(GUI_ID_KEY_SCREENSHOT_BUTTON, wgettext("Screenshot"), "keymap_screenshot");
+ this->add_key(GUI_ID_KEY_RANGE_BUTTON, wgettext("Range select"), "keymap_rangeselect");
+ this->add_key(GUI_ID_KEY_DEC_RANGE_BUTTON, wgettext("Dec. range"), "keymap_decrease_viewing_range_min");
+ this->add_key(GUI_ID_KEY_INC_RANGE_BUTTON, wgettext("Inc. range"), "keymap_increase_viewing_range_min");
+ this->add_key(GUI_ID_KEY_CONSOLE_BUTTON, wgettext("Console"), "keymap_console");
+ this->add_key(GUI_ID_KEY_CMD_BUTTON, wgettext("Command"), "keymap_cmd");
+ this->add_key(GUI_ID_KEY_CMD_LOCAL_BUTTON, wgettext("Local command"), "keymap_cmd_local");
+ this->add_key(GUI_ID_KEY_BLOCK_BOUNDS_BUTTON, wgettext("Block bounds"), "keymap_toggle_block_bounds");
+ this->add_key(GUI_ID_KEY_HUD_BUTTON, wgettext("Toggle HUD"), "keymap_toggle_hud");
+ this->add_key(GUI_ID_KEY_CHATLOG_BUTTON, wgettext("Toggle chat log"), "keymap_toggle_chat");
+ this->add_key(GUI_ID_KEY_FOG_BUTTON, wgettext("Toggle fog"), "keymap_toggle_fog");
}
diff --git a/src/gui/guiPasswordChange.cpp b/src/gui/guiPasswordChange.cpp
index 74cd62f5b..c983260f6 100644
--- a/src/gui/guiPasswordChange.cpp
+++ b/src/gui/guiPasswordChange.cpp
@@ -25,6 +25,10 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <IGUIStaticText.h>
#include <IGUIFont.h>
+#ifdef HAVE_TOUCHSCREENGUI
+ #include "client/renderingengine.h"
+#endif
+
#include "porting.h"
#include "gettext.h"
@@ -79,8 +83,8 @@ void GUIPasswordChange::regenerateGui(v2u32 screensize)
/*
Calculate new sizes and positions
*/
-#ifdef __ANDROID__
- const float s = m_gui_scale * porting::getDisplayDensity() / 2;
+#ifdef HAVE_TOUCHSCREENGUI
+ const float s = m_gui_scale * RenderingEngine::getDisplayDensity() / 2;
#else
const float s = m_gui_scale;
#endif
diff --git a/src/gui/guiScene.cpp b/src/gui/guiScene.cpp
index 5f4c50b91..ee2556b03 100644
--- a/src/gui/guiScene.cpp
+++ b/src/gui/guiScene.cpp
@@ -21,7 +21,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <SViewFrustum.h>
#include <IAnimatedMeshSceneNode.h>
-#include <ILightSceneNode.h>
#include "porting.h"
GUIScene::GUIScene(gui::IGUIEnvironment *env, scene::ISceneManager *smgr,
@@ -34,9 +33,6 @@ GUIScene::GUIScene(gui::IGUIEnvironment *env, scene::ISceneManager *smgr,
m_cam = m_smgr->addCameraSceneNode(0, v3f(0.f, 0.f, -100.f), v3f(0.f));
m_cam->setFOV(30.f * core::DEGTORAD);
- scene::ILightSceneNode *light = m_smgr->addLightSceneNode(m_cam);
- light->setRadius(1000.f);
-
m_smgr->getParameters()->setAttribute(scene::ALLOW_ZWRITE_ON_TRANSPARENT, true);
}
@@ -60,6 +56,7 @@ scene::IAnimatedMeshSceneNode *GUIScene::setMesh(scene::IAnimatedMesh *mesh)
m_mesh = m_smgr->addAnimatedMeshSceneNode(mesh);
m_mesh->setPosition(-m_mesh->getBoundingBox().getCenter());
m_mesh->animateJoints();
+
return m_mesh;
}
@@ -73,10 +70,13 @@ void GUIScene::setTexture(u32 idx, video::ITexture *texture)
material.setFlag(video::EMF_FOG_ENABLE, true);
material.setFlag(video::EMF_BILINEAR_FILTER, false);
material.setFlag(video::EMF_BACK_FACE_CULLING, false);
+ material.setFlag(video::EMF_ZWRITE_ENABLE, true);
}
void GUIScene::draw()
{
+ m_driver->clearBuffers(video::ECBF_DEPTH);
+
// Control rotation speed based on time
u64 new_time = porting::getTimeMs();
u64 dtime_ms = 0;
@@ -161,6 +161,14 @@ void GUIScene::setFrameLoop(s32 begin, s32 end)
m_mesh->setFrameLoop(begin, end);
}
+/**
+ * Sets the animation speed (FPS) for the mesh
+ */
+void GUIScene::setAnimationSpeed(f32 speed)
+{
+ m_mesh->setAnimationSpeed(speed);
+}
+
/* Camera control functions */
inline void GUIScene::calcOptimalDistance()
diff --git a/src/gui/guiScene.h b/src/gui/guiScene.h
index 08eb7f350..0f5f3a891 100644
--- a/src/gui/guiScene.h
+++ b/src/gui/guiScene.h
@@ -37,6 +37,7 @@ public:
void setTexture(u32 idx, video::ITexture *texture);
void setBackgroundColor(const video::SColor &color) noexcept { m_bgcolor = color; };
void setFrameLoop(s32 begin, s32 end);
+ void setAnimationSpeed(f32 speed);
void enableMouseControl(bool enable) noexcept { m_mouse_ctrl = enable; };
void setRotation(v2f rot) noexcept { m_custom_rot = rot; };
void enableContinuousRotation(bool enable) noexcept { m_inf_rot = enable; };
diff --git a/src/gui/guiSkin.cpp b/src/gui/guiSkin.cpp
index e09209bd9..ca692f6cb 100644
--- a/src/gui/guiSkin.cpp
+++ b/src/gui/guiSkin.cpp
@@ -1024,48 +1024,6 @@ void GUISkin::draw2DRectangle(IGUIElement* element,
}
-//! Writes attributes of the object.
-//! Implement this to expose the attributes of your scene node animator for
-//! scripting languages, editors, debuggers or xml serialization purposes.
-void GUISkin::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options) const
-{
- u32 i;
- for (i=0; i<EGDC_COUNT; ++i)
- out->addColor(GUISkinColorNames[i], Colors[i]);
-
- for (i=0; i<EGDS_COUNT; ++i)
- out->addInt(GUISkinSizeNames[i], Sizes[i]);
-
- for (i=0; i<EGDT_COUNT; ++i)
- out->addString(GUISkinTextNames[i], Texts[i].c_str());
-
- for (i=0; i<EGDI_COUNT; ++i)
- out->addInt(GUISkinIconNames[i], Icons[i]);
-}
-
-
-//! Reads attributes of the object.
-//! Implement this to set the attributes of your scene node animator for
-//! scripting languages, editors, debuggers or xml deserialization purposes.
-void GUISkin::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options)
-{
- // TODO: This is not nice code for downward compatibility, whenever new values are added and users
- // load an old skin the corresponding values will be set to 0.
- u32 i;
- for (i=0; i<EGDC_COUNT; ++i)
- Colors[i] = in->getAttributeAsColor(GUISkinColorNames[i]);
-
- for (i=0; i<EGDS_COUNT; ++i)
- Sizes[i] = in->getAttributeAsInt(GUISkinSizeNames[i]);
-
- for (i=0; i<EGDT_COUNT; ++i)
- Texts[i] = in->getAttributeAsStringW(GUISkinTextNames[i]);
-
- for (i=0; i<EGDI_COUNT; ++i)
- Icons[i] = in->getAttributeAsInt(GUISkinIconNames[i]);
-}
-
-
//! gets the colors
// PATCH
void GUISkin::getColors(video::SColor* colors)
diff --git a/src/gui/guiSkin.h b/src/gui/guiSkin.h
index bbb900f9f..fa9b27bdd 100644
--- a/src/gui/guiSkin.h
+++ b/src/gui/guiSkin.h
@@ -290,16 +290,6 @@ namespace gui
//! get the type of this skin
virtual EGUI_SKIN_TYPE getType() const;
- //! Writes attributes of the object.
- //! Implement this to expose the attributes of your scene node animator for
- //! scripting languages, editors, debuggers or xml serialization purposes.
- virtual void serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options=0) const;
-
- //! Reads attributes of the object.
- //! Implement this to set the attributes of your scene node animator for
- //! scripting languages, editors, debuggers or xml deserialization purposes.
- virtual void deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options=0);
-
//! gets the colors
virtual void getColors(video::SColor* colors); // ::PATCH:
diff --git a/src/gui/guiTable.cpp b/src/gui/guiTable.cpp
index cab2e19fd..79ae1aea3 100644
--- a/src/gui/guiTable.cpp
+++ b/src/gui/guiTable.cpp
@@ -77,9 +77,10 @@ GUITable::GUITable(gui::IGUIEnvironment *env,
setTabStop(true);
setTabOrder(-1);
updateAbsolutePosition();
+#ifdef HAVE_TOUCHSCREENGUI
+ float density = 1; // dp scaling is applied by the skin
+#else
float density = RenderingEngine::getDisplayDensity();
-#ifdef __ANDROID__
- density = 1; // dp scaling is applied by the skin
#endif
core::rect<s32> relative_rect = m_scrollbar->getRelativePosition();
s32 width = (relative_rect.getWidth() / (2.0 / 3.0)) * density *
diff --git a/src/gui/guiVolumeChange.cpp b/src/gui/guiVolumeChange.cpp
index f17cfa986..61ab758a1 100644
--- a/src/gui/guiVolumeChange.cpp
+++ b/src/gui/guiVolumeChange.cpp
@@ -93,11 +93,12 @@ void GUIVolumeChange::regenerateGui(v2u32 screensize)
core::rect<s32> rect(0, 0, 160 * s, 20 * s);
rect = rect + v2s32(size.X / 2 - 80 * s, size.Y / 2 - 70 * s);
- const wchar_t *text = wgettext("Sound Volume: ");
+ wchar_t text[100];
+ const wchar_t *str = wgettext("Sound Volume: %d%%");
+ swprintf(text, sizeof(text) / sizeof(wchar_t), str, volume);
+ delete[] str;
core::stringw volume_text = text;
- delete [] text;
- volume_text += core::stringw(volume) + core::stringw("%");
Environment->addStaticText(volume_text.c_str(), rect, false,
true, this, ID_soundText);
}
@@ -183,11 +184,13 @@ bool GUIVolumeChange::OnEvent(const SEvent& event)
g_settings->setFloat("sound_volume", (float) pos / 100);
gui::IGUIElement *e = getElementFromId(ID_soundText);
- const wchar_t *text = wgettext("Sound Volume: ");
+ wchar_t text[100];
+ const wchar_t *str = wgettext("Sound Volume: %d%%");
+ swprintf(text, sizeof(text) / sizeof(wchar_t), str, pos);
+ delete[] str;
+
core::stringw volume_text = text;
- delete [] text;
- volume_text += core::stringw(pos) + core::stringw("%");
e->setText(volume_text.c_str());
return true;
}
diff --git a/src/gui/intlGUIEditBox.cpp b/src/gui/intlGUIEditBox.cpp
deleted file mode 100644
index 0f09ea746..000000000
--- a/src/gui/intlGUIEditBox.cpp
+++ /dev/null
@@ -1,626 +0,0 @@
-// 11.11.2011 11:11 ValkaTR
-//
-// This is a copy of intlGUIEditBox from the irrlicht, but with a
-// fix in the OnEvent function, which doesn't allowed input of
-// other keyboard layouts than latin-1
-//
-// Characters like: ä ö ü õ ы й ю я ъ № € ° ...
-//
-// This fix is only needed for linux, because of a bug
-// in the CIrrDeviceLinux.cpp:1014-1015 of the irrlicht
-//
-// Also locale in the programm should not be changed to
-// a "C", "POSIX" or whatever, it should be set to "",
-// or XLookupString will return nothing for the international
-// characters.
-//
-// From the "man setlocale":
-//
-// On startup of the main program, the portable "C" locale
-// is selected as default. A program may be made
-// portable to all locales by calling:
-//
-// setlocale(LC_ALL, "");
-//
-// after program initialization....
-//
-
-// Copyright (C) 2002-2013 Nikolaus Gebhardt
-// This file is part of the "Irrlicht Engine".
-// For conditions of distribution and use, see copyright notice in irrlicht.h
-
-#include <util/numeric.h>
-#include "intlGUIEditBox.h"
-
-#include "IGUISkin.h"
-#include "IGUIEnvironment.h"
-#include "IGUIFont.h"
-#include "IVideoDriver.h"
-//#include "irrlicht/os.cpp"
-#include "porting.h"
-//#include "Keycodes.h"
-#include "log.h"
-
-/*
- todo:
- optional scrollbars
- ctrl+left/right to select word
- double click/ctrl click: word select + drag to select whole words, triple click to select line
- optional? dragging selected text
- numerical
-*/
-
-namespace irr
-{
-namespace gui
-{
-
-//! constructor
-intlGUIEditBox::intlGUIEditBox(const wchar_t* text, bool border,
- IGUIEnvironment* environment, IGUIElement* parent, s32 id,
- const core::rect<s32>& rectangle, bool writable, bool has_vscrollbar)
- : GUIEditBox(environment, parent, id, rectangle, border, writable)
-{
- #ifdef _DEBUG
- setDebugName("intlintlGUIEditBox");
- #endif
-
- Text = text;
-
- if (Environment)
- m_operator = Environment->getOSOperator();
-
- if (m_operator)
- m_operator->grab();
-
- // this element can be tabbed to
- setTabStop(true);
- setTabOrder(-1);
-
- IGUISkin *skin = 0;
- if (Environment)
- skin = Environment->getSkin();
- if (m_border && skin)
- {
- m_frame_rect.UpperLeftCorner.X += skin->getSize(EGDS_TEXT_DISTANCE_X)+1;
- m_frame_rect.UpperLeftCorner.Y += skin->getSize(EGDS_TEXT_DISTANCE_Y)+1;
- m_frame_rect.LowerRightCorner.X -= skin->getSize(EGDS_TEXT_DISTANCE_X)+1;
- m_frame_rect.LowerRightCorner.Y -= skin->getSize(EGDS_TEXT_DISTANCE_Y)+1;
- }
-
- if (skin && has_vscrollbar) {
- m_scrollbar_width = skin->getSize(gui::EGDS_SCROLLBAR_SIZE);
-
- if (m_scrollbar_width > 0) {
- createVScrollBar();
- }
- }
-
- breakText();
-
- calculateScrollPos();
- setWritable(writable);
-}
-
-//! Sets whether to draw the background
-void intlGUIEditBox::setDrawBackground(bool draw)
-{
-}
-
-void intlGUIEditBox::updateAbsolutePosition()
-{
- core::rect<s32> oldAbsoluteRect(AbsoluteRect);
- IGUIElement::updateAbsolutePosition();
- if ( oldAbsoluteRect != AbsoluteRect )
- {
- breakText();
- }
-}
-
-
-//! draws the element and its children
-void intlGUIEditBox::draw()
-{
- if (!IsVisible)
- return;
-
- const bool focus = Environment->hasFocus(this);
-
- IGUISkin* skin = Environment->getSkin();
- if (!skin)
- return;
-
- m_frame_rect = AbsoluteRect;
-
- // draw the border
-
- if (m_border)
- {
- if (m_writable) {
- skin->draw3DSunkenPane(this, skin->getColor(EGDC_WINDOW),
- false, true, m_frame_rect, &AbsoluteClippingRect);
- }
-
- m_frame_rect.UpperLeftCorner.X += skin->getSize(EGDS_TEXT_DISTANCE_X)+1;
- m_frame_rect.UpperLeftCorner.Y += skin->getSize(EGDS_TEXT_DISTANCE_Y)+1;
- m_frame_rect.LowerRightCorner.X -= skin->getSize(EGDS_TEXT_DISTANCE_X)+1;
- m_frame_rect.LowerRightCorner.Y -= skin->getSize(EGDS_TEXT_DISTANCE_Y)+1;
- }
-
- updateVScrollBar();
- core::rect<s32> localClipRect = m_frame_rect;
- localClipRect.clipAgainst(AbsoluteClippingRect);
-
- // draw the text
-
- IGUIFont* font = m_override_font;
- if (!m_override_font)
- font = skin->getFont();
-
- s32 cursorLine = 0;
- s32 charcursorpos = 0;
-
- if (font)
- {
- if (m_last_break_font != font)
- {
- breakText();
- }
-
- // calculate cursor pos
-
- core::stringw *txtLine = &Text;
- s32 startPos = 0;
-
- core::stringw s, s2;
-
- // get mark position
- const bool ml = (!m_passwordbox && (m_word_wrap || m_multiline));
- const s32 realmbgn = m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end;
- const s32 realmend = m_mark_begin < m_mark_end ? m_mark_end : m_mark_begin;
- const s32 hlineStart = ml ? getLineFromPos(realmbgn) : 0;
- const s32 hlineCount = ml ? getLineFromPos(realmend) - hlineStart + 1 : 1;
- const s32 lineCount = ml ? m_broken_text.size() : 1;
-
- // Save the override color information.
- // Then, alter it if the edit box is disabled.
- const bool prevOver = m_override_color_enabled;
- const video::SColor prevColor = m_override_color;
-
- if (!Text.empty()) {
- if (!IsEnabled && !m_override_color_enabled)
- {
- m_override_color_enabled = true;
- m_override_color = skin->getColor(EGDC_GRAY_TEXT);
- }
-
- for (s32 i=0; i < lineCount; ++i)
- {
- setTextRect(i);
-
- // clipping test - don't draw anything outside the visible area
- core::rect<s32> c = localClipRect;
- c.clipAgainst(m_current_text_rect);
- if (!c.isValid())
- continue;
-
- // get current line
- if (m_passwordbox)
- {
- if (m_broken_text.size() != 1)
- {
- m_broken_text.clear();
- m_broken_text.emplace_back();
- }
- if (m_broken_text[0].size() != Text.size())
- {
- m_broken_text[0] = Text;
- for (u32 q = 0; q < Text.size(); ++q)
- {
- m_broken_text[0] [q] = m_passwordchar;
- }
- }
- txtLine = &m_broken_text[0];
- startPos = 0;
- }
- else
- {
- txtLine = ml ? &m_broken_text[i] : &Text;
- startPos = ml ? m_broken_text_positions[i] : 0;
- }
-
-
- // draw normal text
- font->draw(txtLine->c_str(), m_current_text_rect,
- m_override_color_enabled ? m_override_color : skin->getColor(EGDC_BUTTON_TEXT),
- false, true, &localClipRect);
-
- // draw mark and marked text
- if (focus && m_mark_begin != m_mark_end && i >= hlineStart && i < hlineStart + hlineCount)
- {
-
- s32 mbegin = 0, mend = 0;
- s32 lineStartPos = 0, lineEndPos = txtLine->size();
-
- if (i == hlineStart)
- {
- // highlight start is on this line
- s = txtLine->subString(0, realmbgn - startPos);
- mbegin = font->getDimension(s.c_str()).Width;
-
- // deal with kerning
- mbegin += font->getKerningWidth(
- &((*txtLine)[realmbgn - startPos]),
- realmbgn - startPos > 0 ? &((*txtLine)[realmbgn - startPos - 1]) : 0);
-
- lineStartPos = realmbgn - startPos;
- }
- if (i == hlineStart + hlineCount - 1)
- {
- // highlight end is on this line
- s2 = txtLine->subString(0, realmend - startPos);
- mend = font->getDimension(s2.c_str()).Width;
- lineEndPos = (s32)s2.size();
- }
- else
- mend = font->getDimension(txtLine->c_str()).Width;
-
- m_current_text_rect.UpperLeftCorner.X += mbegin;
- m_current_text_rect.LowerRightCorner.X = m_current_text_rect.UpperLeftCorner.X + mend - mbegin;
-
- // draw mark
- skin->draw2DRectangle(this, skin->getColor(EGDC_HIGH_LIGHT), m_current_text_rect, &localClipRect);
-
- // draw marked text
- s = txtLine->subString(lineStartPos, lineEndPos - lineStartPos);
-
- if (!s.empty())
- font->draw(s.c_str(), m_current_text_rect,
- m_override_color_enabled ? m_override_color : skin->getColor(EGDC_HIGH_LIGHT_TEXT),
- false, true, &localClipRect);
-
- }
- }
-
- // Return the override color information to its previous settings.
- m_override_color_enabled = prevOver;
- m_override_color = prevColor;
- }
-
- // draw cursor
-
- if (m_word_wrap || m_multiline)
- {
- cursorLine = getLineFromPos(m_cursor_pos);
- txtLine = &m_broken_text[cursorLine];
- startPos = m_broken_text_positions[cursorLine];
- }
- s = txtLine->subString(0,m_cursor_pos-startPos);
- charcursorpos = font->getDimension(s.c_str()).Width +
- font->getKerningWidth(L"_", m_cursor_pos-startPos > 0 ? &((*txtLine)[m_cursor_pos-startPos-1]) : 0);
-
- if (m_writable) {
- if (focus && (porting::getTimeMs() - m_blink_start_time) % 700 < 350) {
- setTextRect(cursorLine);
- m_current_text_rect.UpperLeftCorner.X += charcursorpos;
-
- font->draw(L"_", m_current_text_rect,
- m_override_color_enabled ? m_override_color : skin->getColor(EGDC_BUTTON_TEXT),
- false, true, &localClipRect);
- }
- }
- }
-
- // draw children
- IGUIElement::draw();
-}
-
-
-s32 intlGUIEditBox::getCursorPos(s32 x, s32 y)
-{
- IGUIFont* font = getActiveFont();
-
- const u32 lineCount = (m_word_wrap || m_multiline) ? m_broken_text.size() : 1;
-
- core::stringw *txtLine = NULL;
- s32 startPos = 0;
- u32 curr_line_idx = 0;
- x += 3;
-
- for (; curr_line_idx < lineCount; ++curr_line_idx) {
- setTextRect(curr_line_idx);
- if (curr_line_idx == 0 && y < m_current_text_rect.UpperLeftCorner.Y)
- y = m_current_text_rect.UpperLeftCorner.Y;
- if (curr_line_idx == lineCount - 1 && y > m_current_text_rect.LowerRightCorner.Y)
- y = m_current_text_rect.LowerRightCorner.Y;
-
- // is it inside this region?
- if (y >= m_current_text_rect.UpperLeftCorner.Y && y <= m_current_text_rect.LowerRightCorner.Y) {
- // we've found the clicked line
- txtLine = (m_word_wrap || m_multiline) ? &m_broken_text[curr_line_idx] : &Text;
- startPos = (m_word_wrap || m_multiline) ? m_broken_text_positions[curr_line_idx] : 0;
- break;
- }
- }
-
- if (x < m_current_text_rect.UpperLeftCorner.X)
- x = m_current_text_rect.UpperLeftCorner.X;
- else if (x > m_current_text_rect.LowerRightCorner.X)
- x = m_current_text_rect.LowerRightCorner.X;
-
- s32 idx = font->getCharacterFromPos(txtLine->c_str(), x - m_current_text_rect.UpperLeftCorner.X);
- // Special handling for last line, if we are on limits, add 1 extra shift because idx
- // will be the last char, not null char of the wstring
- if (curr_line_idx == lineCount - 1 && x == m_current_text_rect.LowerRightCorner.X)
- idx++;
-
- return rangelim(idx + startPos, 0, S32_MAX);
-}
-
-
-//! Breaks the single text line.
-void intlGUIEditBox::breakText()
-{
- IGUISkin* skin = Environment->getSkin();
-
- if ((!m_word_wrap && !m_multiline) || !skin)
- return;
-
- m_broken_text.clear(); // need to reallocate :/
- m_broken_text_positions.clear();
-
- IGUIFont* font = m_override_font;
- if (!m_override_font)
- font = skin->getFont();
-
- if (!font)
- return;
-
- m_last_break_font = font;
-
- core::stringw line;
- core::stringw word;
- core::stringw whitespace;
- s32 lastLineStart = 0;
- s32 size = Text.size();
- s32 length = 0;
- s32 elWidth = RelativeRect.getWidth() - m_scrollbar_width - 10;
- wchar_t c;
-
- for (s32 i=0; i<size; ++i)
- {
- c = Text[i];
- bool lineBreak = false;
-
- if (c == L'\r') // Mac or Windows breaks
- {
- lineBreak = true;
- c = ' ';
- if (Text[i+1] == L'\n') // Windows breaks
- {
- Text.erase(i+1);
- --size;
- }
- }
- else if (c == L'\n') // Unix breaks
- {
- lineBreak = true;
- c = ' ';
- }
-
- // don't break if we're not a multi-line edit box
- if (!m_multiline)
- lineBreak = false;
-
- if (c == L' ' || c == 0 || i == (size-1))
- {
- if (!word.empty()) {
- // here comes the next whitespace, look if
- // we can break the last word to the next line.
- s32 whitelgth = font->getDimension(whitespace.c_str()).Width;
- s32 worldlgth = font->getDimension(word.c_str()).Width;
-
- if (m_word_wrap && length + worldlgth + whitelgth > elWidth)
- {
- // break to next line
- length = worldlgth;
- m_broken_text.push_back(line);
- m_broken_text_positions.push_back(lastLineStart);
- lastLineStart = i - (s32)word.size();
- line = word;
- }
- else
- {
- // add word to line
- line += whitespace;
- line += word;
- length += whitelgth + worldlgth;
- }
-
- word = L"";
- whitespace = L"";
- }
-
- whitespace += c;
-
- // compute line break
- if (lineBreak)
- {
- line += whitespace;
- line += word;
- m_broken_text.push_back(line);
- m_broken_text_positions.push_back(lastLineStart);
- lastLineStart = i+1;
- line = L"";
- word = L"";
- whitespace = L"";
- length = 0;
- }
- }
- else
- {
- // yippee this is a word..
- word += c;
- }
- }
-
- line += whitespace;
- line += word;
- m_broken_text.push_back(line);
- m_broken_text_positions.push_back(lastLineStart);
-}
-
-
-void intlGUIEditBox::setTextRect(s32 line)
-{
- core::dimension2du d;
-
- IGUISkin* skin = Environment->getSkin();
- if (!skin)
- return;
-
- IGUIFont* font = m_override_font ? m_override_font : skin->getFont();
-
- if (!font)
- return;
-
- // get text dimension
- const u32 lineCount = (m_word_wrap || m_multiline) ? m_broken_text.size() : 1;
- if (m_word_wrap || m_multiline)
- {
- d = font->getDimension(m_broken_text[line].c_str());
- }
- else
- {
- d = font->getDimension(Text.c_str());
- d.Height = AbsoluteRect.getHeight();
- }
- d.Height += font->getKerningHeight();
-
- // justification
- switch (m_halign)
- {
- case EGUIA_CENTER:
- // align to h centre
- m_current_text_rect.UpperLeftCorner.X = (m_frame_rect.getWidth()/2) - (d.Width/2);
- m_current_text_rect.LowerRightCorner.X = (m_frame_rect.getWidth()/2) + (d.Width/2);
- break;
- case EGUIA_LOWERRIGHT:
- // align to right edge
- m_current_text_rect.UpperLeftCorner.X = m_frame_rect.getWidth() - d.Width;
- m_current_text_rect.LowerRightCorner.X = m_frame_rect.getWidth();
- break;
- default:
- // align to left edge
- m_current_text_rect.UpperLeftCorner.X = 0;
- m_current_text_rect.LowerRightCorner.X = d.Width;
-
- }
-
- switch (m_valign)
- {
- case EGUIA_CENTER:
- // align to v centre
- m_current_text_rect.UpperLeftCorner.Y =
- (m_frame_rect.getHeight()/2) - (lineCount*d.Height)/2 + d.Height*line;
- break;
- case EGUIA_LOWERRIGHT:
- // align to bottom edge
- m_current_text_rect.UpperLeftCorner.Y =
- m_frame_rect.getHeight() - lineCount*d.Height + d.Height*line;
- break;
- default:
- // align to top edge
- m_current_text_rect.UpperLeftCorner.Y = d.Height*line;
- break;
- }
-
- m_current_text_rect.UpperLeftCorner.X -= m_hscroll_pos;
- m_current_text_rect.LowerRightCorner.X -= m_hscroll_pos;
- m_current_text_rect.UpperLeftCorner.Y -= m_vscroll_pos;
- m_current_text_rect.LowerRightCorner.Y = m_current_text_rect.UpperLeftCorner.Y + d.Height;
-
- m_current_text_rect += m_frame_rect.UpperLeftCorner;
-
-}
-
-void intlGUIEditBox::calculateScrollPos()
-{
- if (!m_autoscroll)
- return;
-
- // calculate horizontal scroll position
- s32 cursLine = getLineFromPos(m_cursor_pos);
- setTextRect(cursLine);
-
- // don't do horizontal scrolling when wordwrap is enabled.
- if (!m_word_wrap)
- {
- // get cursor position
- IGUISkin* skin = Environment->getSkin();
- if (!skin)
- return;
- IGUIFont* font = m_override_font ? m_override_font : skin->getFont();
- if (!font)
- return;
-
- core::stringw *txtLine = m_multiline ? &m_broken_text[cursLine] : &Text;
- s32 cPos = m_multiline ? m_cursor_pos - m_broken_text_positions[cursLine] : m_cursor_pos;
-
- s32 cStart = m_current_text_rect.UpperLeftCorner.X + m_hscroll_pos +
- font->getDimension(txtLine->subString(0, cPos).c_str()).Width;
-
- s32 cEnd = cStart + font->getDimension(L"_ ").Width;
-
- if (m_frame_rect.LowerRightCorner.X < cEnd)
- m_hscroll_pos = cEnd - m_frame_rect.LowerRightCorner.X;
- else if (m_frame_rect.UpperLeftCorner.X > cStart)
- m_hscroll_pos = cStart - m_frame_rect.UpperLeftCorner.X;
- else
- m_hscroll_pos = 0;
-
- // todo: adjust scrollbar
- }
-
- if (!m_word_wrap && !m_multiline)
- return;
-
- // vertical scroll position
- if (m_frame_rect.LowerRightCorner.Y < m_current_text_rect.LowerRightCorner.Y)
- m_vscroll_pos += m_current_text_rect.LowerRightCorner.Y - m_frame_rect.LowerRightCorner.Y; // scrolling downwards
- else if (m_frame_rect.UpperLeftCorner.Y > m_current_text_rect.UpperLeftCorner.Y)
- m_vscroll_pos += m_current_text_rect.UpperLeftCorner.Y - m_frame_rect.UpperLeftCorner.Y; // scrolling upwards
-
- // todo: adjust scrollbar
- if (m_vscrollbar)
- m_vscrollbar->setPos(m_vscroll_pos);
-}
-
-
-//! Create a vertical scrollbar
-void intlGUIEditBox::createVScrollBar()
-{
- s32 fontHeight = 1;
-
- if (m_override_font) {
- fontHeight = m_override_font->getDimension(L"").Height;
- } else {
- if (IGUISkin* skin = Environment->getSkin()) {
- if (IGUIFont* font = skin->getFont()) {
- fontHeight = font->getDimension(L"").Height;
- }
- }
- }
-
- irr::core::rect<s32> scrollbarrect = m_frame_rect;
- scrollbarrect.UpperLeftCorner.X += m_frame_rect.getWidth() - m_scrollbar_width;
- m_vscrollbar = new GUIScrollBar(Environment, getParent(), -1,
- scrollbarrect, false, true);
-
- m_vscrollbar->setVisible(false);
- m_vscrollbar->setSmallStep(3 * fontHeight);
- m_vscrollbar->setLargeStep(10 * fontHeight);
-}
-
-} // end namespace gui
-} // end namespace irr
diff --git a/src/gui/intlGUIEditBox.h b/src/gui/intlGUIEditBox.h
deleted file mode 100644
index 007fe1c93..000000000
--- a/src/gui/intlGUIEditBox.h
+++ /dev/null
@@ -1,68 +0,0 @@
-// Copyright (C) 2002-2013 Nikolaus Gebhardt
-// This file is part of the "Irrlicht Engine".
-// For conditions of distribution and use, see copyright notice in irrlicht.h
-
-#pragma once
-
-#include "IrrCompileConfig.h"
-//#ifdef _IRR_COMPILE_WITH_GUI_
-
-#include "guiEditBox.h"
-#include "irrArray.h"
-#include "IOSOperator.h"
-
-namespace irr
-{
-namespace gui
-{
- class intlGUIEditBox : public GUIEditBox
- {
- public:
-
- //! constructor
- intlGUIEditBox(const wchar_t* text, bool border, IGUIEnvironment* environment,
- IGUIElement* parent, s32 id, const core::rect<s32>& rectangle,
- bool writable = true, bool has_vscrollbar = false);
-
- //! destructor
- virtual ~intlGUIEditBox() {}
-
- //! Sets whether to draw the background
- virtual void setDrawBackground(bool draw);
-
- virtual bool isDrawBackgroundEnabled() const { return true; }
-
- //! draws the element and its children
- virtual void draw();
-
- //! Updates the absolute position, splits text if required
- virtual void updateAbsolutePosition();
-
- virtual void setCursorChar(const wchar_t cursorChar) {}
-
- virtual wchar_t getCursorChar() const { return L'|'; }
-
- virtual void setCursorBlinkTime(u32 timeMs) {}
-
- virtual u32 getCursorBlinkTime() const { return 500; }
-
- protected:
- //! Breaks the single text line.
- virtual void breakText();
- //! sets the area of the given line
- virtual void setTextRect(s32 line);
-
- //! calculates the current scroll position
- void calculateScrollPos();
-
- s32 getCursorPos(s32 x, s32 y);
-
- //! Create a vertical scrollbar
- void createVScrollBar();
- };
-
-
-} // end namespace gui
-} // end namespace irr
-
-//#endif // _IRR_COMPILE_WITH_GUI_
diff --git a/src/gui/modalMenu.cpp b/src/gui/modalMenu.cpp
index 0d3fb55f0..56a5d2cb9 100644
--- a/src/gui/modalMenu.cpp
+++ b/src/gui/modalMenu.cpp
@@ -26,6 +26,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#ifdef HAVE_TOUCHSCREENGUI
#include "touchscreengui.h"
+#include "client/renderingengine.h"
#endif
// clang-format off
@@ -40,8 +41,8 @@ GUIModalMenu::GUIModalMenu(gui::IGUIEnvironment* env, gui::IGUIElement* parent,
m_remap_dbl_click(remap_dbl_click)
{
m_gui_scale = g_settings->getFloat("gui_scaling");
-#ifdef __ANDROID__
- float d = porting::getDisplayDensity();
+#ifdef HAVE_TOUCHSCREENGUI
+ float d = RenderingEngine::getDisplayDensity();
m_gui_scale *= 1.1 - 0.3 * d + 0.2 * d * d;
#endif
setVisible(true);
@@ -183,7 +184,7 @@ static bool isChild(gui::IGUIElement *tocheck, gui::IGUIElement *parent)
return false;
}
-#ifdef __ANDROID__
+#ifdef HAVE_TOUCHSCREENGUI
bool GUIModalMenu::simulateMouseEvent(
gui::IGUIElement *target, ETOUCH_INPUT_EVENT touch_event)
@@ -217,6 +218,8 @@ bool GUIModalMenu::simulateMouseEvent(
void GUIModalMenu::enter(gui::IGUIElement *hovered)
{
+ if (!hovered)
+ return;
sanity_check(!m_hovered);
m_hovered.grab(hovered);
SEvent gui_event{};
@@ -268,7 +271,7 @@ bool GUIModalMenu::preprocessEvent(const SEvent &event)
std::string label = wide_to_utf8(getLabelByID(hovered->getID()));
if (label.empty())
label = "text";
- message += gettext(label) + ":";
+ message += strgettext(label) + ":";
// single line text input
int type = 2;
@@ -286,7 +289,9 @@ bool GUIModalMenu::preprocessEvent(const SEvent &event)
return retval;
}
}
+#endif
+#ifdef HAVE_TOUCHSCREENGUI
if (event.EventType == EET_TOUCH_INPUT_EVENT) {
irr_ptr<GUIModalMenu> holder;
holder.grab(this); // keep this alive until return (it might be dropped downstream [?])
diff --git a/src/gui/modalMenu.h b/src/gui/modalMenu.h
index ed0da3205..06e78f06b 100644
--- a/src/gui/modalMenu.h
+++ b/src/gui/modalMenu.h
@@ -75,10 +75,10 @@ protected:
v2u32 m_screensize_old;
float m_gui_scale;
#ifdef __ANDROID__
- v2s32 m_down_pos;
std::string m_jni_field_name;
#endif
#ifdef HAVE_TOUCHSCREENGUI
+ v2s32 m_down_pos;
bool m_touchscreen_visible = true;
#endif
@@ -102,7 +102,7 @@ private:
// wants to launch other menus
bool m_allow_focus_removal = false;
-#ifdef __ANDROID__
+#ifdef HAVE_TOUCHSCREENGUI
irr_ptr<gui::IGUIElement> m_hovered;
bool simulateMouseEvent(gui::IGUIElement *target, ETOUCH_INPUT_EVENT touch_event);
diff --git a/src/gui/profilergraph.cpp b/src/gui/profilergraph.cpp
index b29285e2f..f71ef3799 100644
--- a/src/gui/profilergraph.cpp
+++ b/src/gui/profilergraph.cpp
@@ -94,20 +94,29 @@ void ProfilerGraph::draw(s32 x_left, s32 y_bottom, video::IVideoDriver *driver,
show_min = 0;
}
- s32 texth = 15;
+ const s32 texth = 15;
char buf[10];
- porting::mt_snprintf(buf, sizeof(buf), "%.3g", show_max);
+ if (floorf(show_max) == show_max)
+ porting::mt_snprintf(buf, sizeof(buf), "%.5g", show_max);
+ else
+ porting::mt_snprintf(buf, sizeof(buf), "%.3g", show_max);
font->draw(utf8_to_wide(buf).c_str(),
core::rect<s32>(textx, y - graphh, textx2,
y - graphh + texth),
meta.color);
- porting::mt_snprintf(buf, sizeof(buf), "%.3g", show_min);
+
+ if (floorf(show_min) == show_min)
+ porting::mt_snprintf(buf, sizeof(buf), "%.5g", show_min);
+ else
+ porting::mt_snprintf(buf, sizeof(buf), "%.3g", show_min);
font->draw(utf8_to_wide(buf).c_str(),
core::rect<s32>(textx, y - texth, textx2, y), meta.color);
+
font->draw(utf8_to_wide(id).c_str(),
core::rect<s32>(textx, y - graphh / 2 - texth / 2, textx2,
y - graphh / 2 + texth / 2),
meta.color);
+
s32 graph1y = y;
s32 graph1h = graphh;
bool relativegraph = (show_min != 0 && show_min != show_max);
diff --git a/src/gui/touchscreengui.cpp b/src/gui/touchscreengui.cpp
index e1a971462..ebe1a6325 100644
--- a/src/gui/touchscreengui.cpp
+++ b/src/gui/touchscreengui.cpp
@@ -28,19 +28,18 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "util/numeric.h"
#include "porting.h"
#include "client/guiscalingfilter.h"
+#include "client/renderingengine.h"
#include <iostream>
#include <algorithm>
-#include <ISceneCollisionManager.h>
-
using namespace irr::core;
const char **button_imagenames = (const char *[]) {
"jump_btn.png",
"down.png",
"zoom.png",
- "aux_btn.png"
+ "aux1_btn.png"
};
const char **joystick_imagenames = (const char *[]) {
@@ -80,8 +79,8 @@ static irr::EKEY_CODE id2keycode(touch_gui_button_id id)
case zoom_id:
key = "zoom";
break;
- case special1_id:
- key = "special1";
+ case aux1_id:
+ key = "aux1";
break;
case fly_id:
key = "freemove";
@@ -425,10 +424,10 @@ TouchScreenGUI::TouchScreenGUI(IrrlichtDevice *device, IEventReceiver *receiver)
m_touchscreen_threshold = g_settings->getU16("touchscreen_threshold");
m_fixed_joystick = g_settings->getBool("fixed_virtual_joystick");
- m_joystick_triggers_special1 = g_settings->getBool("virtual_joystick_triggers_aux");
+ m_joystick_triggers_aux1 = g_settings->getBool("virtual_joystick_triggers_aux1");
m_screensize = m_device->getVideoDriver()->getScreenSize();
button_size = MYMIN(m_screensize.Y / 4.5f,
- porting::getDisplayDensity() *
+ RenderingEngine::getDisplayDensity() *
g_settings->getFloat("hud_scaling") * 65.0f);
}
@@ -521,9 +520,9 @@ void TouchScreenGUI::init(ISimpleTextureSource *tsrc)
m_screensize.Y - (3 * button_size)),
L"z", false);
- // init special1/aux button
- if (!m_joystick_triggers_special1)
- initButton(special1_id,
+ // init aux1 button
+ if (!m_joystick_triggers_aux1)
+ initButton(aux1_id,
rect<s32>(m_screensize.X - (1.25 * button_size),
m_screensize.Y - (2.5 * button_size),
m_screensize.X - (0.25 * button_size),
@@ -670,9 +669,9 @@ void TouchScreenGUI::handleReleaseEvent(size_t evt_id)
if (button != after_last_element_id) {
// handle button events
handleButtonEvent(button, evt_id, false);
- } else if (evt_id == m_move_id) {
+ } else if (m_has_move_id && evt_id == m_move_id) {
// handle the point used for moving view
- m_move_id = -1;
+ m_has_move_id = false;
// if this pointer issued a mouse event issue symmetric release here
if (m_move_sent_as_mouse_event) {
@@ -694,8 +693,8 @@ void TouchScreenGUI::handleReleaseEvent(size_t evt_id)
}
// handle joystick
- else if (evt_id == m_joystick_id) {
- m_joystick_id = -1;
+ else if (m_has_joystick_id && evt_id == m_joystick_id) {
+ m_has_joystick_id = false;
// reset joystick
for (unsigned int i = 0; i < 4; i++)
@@ -778,7 +777,8 @@ void TouchScreenGUI::translateEvent(const SEvent &event)
if ((m_fixed_joystick && dxj * dxj + dyj * dyj <= button_size * button_size * 1.5 * 1.5) ||
(!m_fixed_joystick && event.TouchInput.X < m_screensize.X / 3.0f)) {
// If we don't already have a starting point for joystick make this the one.
- if (m_joystick_id == -1) {
+ if (!m_has_joystick_id) {
+ m_has_joystick_id = true;
m_joystick_id = event.TouchInput.ID;
m_joystick_has_really_moved = false;
@@ -798,7 +798,8 @@ void TouchScreenGUI::translateEvent(const SEvent &event)
}
} else {
// If we don't already have a moving point make this the moving one.
- if (m_move_id == -1) {
+ if (!m_has_move_id) {
+ m_has_move_id = true;
m_move_id = event.TouchInput.ID;
m_move_has_really_moved = false;
m_move_downtime = porting::getTimeMs();
@@ -821,7 +822,7 @@ void TouchScreenGUI::translateEvent(const SEvent &event)
v2s32(event.TouchInput.X, event.TouchInput.Y))
return;
- if (m_move_id != -1) {
+ if (m_has_move_id) {
if ((event.TouchInput.ID == m_move_id) &&
(!m_move_sent_as_mouse_event)) {
@@ -864,7 +865,7 @@ void TouchScreenGUI::translateEvent(const SEvent &event)
}
}
- if (m_joystick_id != -1 && event.TouchInput.ID == m_joystick_id) {
+ if (m_has_joystick_id && event.TouchInput.ID == m_joystick_id) {
s32 X = event.TouchInput.X;
s32 Y = event.TouchInput.Y;
@@ -923,7 +924,7 @@ void TouchScreenGUI::translateEvent(const SEvent &event)
}
if (distance > button_size) {
- m_joystick_status[j_special1] = true;
+ m_joystick_status[j_aux1] = true;
// move joystick "button"
s32 ndx = button_size * dx / distance - button_size / 2.0f;
s32 ndy = button_size * dy / distance - button_size / 2.0f;
@@ -943,7 +944,7 @@ void TouchScreenGUI::translateEvent(const SEvent &event)
}
}
- if (m_move_id == -1 && m_joystick_id == -1)
+ if (!m_has_move_id && !m_has_joystick_id)
handleChangedButton(event);
}
}
@@ -1039,7 +1040,7 @@ bool TouchScreenGUI::doubleTapDetection()
void TouchScreenGUI::applyJoystickStatus()
{
for (unsigned int i = 0; i < 5; i++) {
- if (i == 4 && !m_joystick_triggers_special1)
+ if (i == 4 && !m_joystick_triggers_aux1)
continue;
SEvent translated{};
@@ -1088,7 +1089,7 @@ void TouchScreenGUI::step(float dtime)
button.repeatcounter += dtime;
// in case we're moving around digging does not happen
- if (m_move_id != -1)
+ if (m_has_move_id)
m_move_has_really_moved = true;
if (button.repeatcounter < button.repeatdelay)
@@ -1116,7 +1117,7 @@ void TouchScreenGUI::step(float dtime)
}
// if a new placed pointer isn't moved for some time start digging
- if ((m_move_id != -1) &&
+ if (m_has_move_id &&
(!m_move_has_really_moved) &&
(!m_move_sent_as_mouse_event)) {
diff --git a/src/gui/touchscreengui.h b/src/gui/touchscreengui.h
index 0349624fa..6b36c0d59 100644
--- a/src/gui/touchscreengui.h
+++ b/src/gui/touchscreengui.h
@@ -39,7 +39,7 @@ typedef enum
jump_id = 0,
crunch_id,
zoom_id,
- special1_id,
+ aux1_id,
after_last_element_id,
settings_starter_id,
rare_controls_starter_id,
@@ -69,7 +69,7 @@ typedef enum
j_backward,
j_left,
j_right,
- j_special1
+ j_aux1
} touch_gui_joystick_move_id;
typedef enum
@@ -217,7 +217,7 @@ private:
// forward, backward, left, right
touch_gui_button_id m_joystick_names[5] = {
- forward_id, backward_id, left_id, right_id, special1_id};
+ forward_id, backward_id, left_id, right_id, aux1_id};
bool m_joystick_status[5] = {false, false, false, false, false};
/*
@@ -228,16 +228,18 @@ private:
*/
line3d<f32> m_shootline;
- int m_move_id = -1;
+ bool m_has_move_id = false;
+ size_t m_move_id;
bool m_move_has_really_moved = false;
u64 m_move_downtime = 0;
bool m_move_sent_as_mouse_event = false;
v2s32 m_move_downlocation = v2s32(-10000, -10000);
- int m_joystick_id = -1;
+ bool m_has_joystick_id = false;
+ size_t m_joystick_id;
bool m_joystick_has_really_moved = false;
bool m_fixed_joystick = false;
- bool m_joystick_triggers_special1 = false;
+ bool m_joystick_triggers_aux1 = false;
button_info *m_joystick_btn_off = nullptr;
button_info *m_joystick_btn_bg = nullptr;
button_info *m_joystick_btn_center = nullptr;
diff --git a/src/httpfetch.cpp b/src/httpfetch.cpp
index 65202ce3e..16f0791c9 100644
--- a/src/httpfetch.cpp
+++ b/src/httpfetch.cpp
@@ -22,7 +22,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <iostream>
#include <sstream>
#include <list>
-#include <map>
+#include <unordered_map>
#include <cerrno>
#include <mutex>
#include "network/socket.h" // for select()
@@ -37,13 +37,14 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "settings.h"
#include "noise.h"
-std::mutex g_httpfetch_mutex;
-std::map<unsigned long, std::queue<HTTPFetchResult> > g_httpfetch_results;
-PcgRandom g_callerid_randomness;
+static std::mutex g_httpfetch_mutex;
+static std::unordered_map<u64, std::queue<HTTPFetchResult>>
+ g_httpfetch_results;
+static PcgRandom g_callerid_randomness;
HTTPFetchRequest::HTTPFetchRequest() :
timeout(g_settings->getS32("curl_timeout")),
- connect_timeout(timeout),
+ connect_timeout(10 * 1000),
useragent(std::string(PROJECT_NAME_C "/") + g_version_hash + " (" + porting::get_sysinfo() + ")")
{
}
@@ -51,24 +52,22 @@ HTTPFetchRequest::HTTPFetchRequest() :
static void httpfetch_deliver_result(const HTTPFetchResult &fetch_result)
{
- unsigned long caller = fetch_result.caller;
+ u64 caller = fetch_result.caller;
if (caller != HTTPFETCH_DISCARD) {
MutexAutoLock lock(g_httpfetch_mutex);
- g_httpfetch_results[caller].push(fetch_result);
+ g_httpfetch_results[caller].emplace(fetch_result);
}
}
-static void httpfetch_request_clear(unsigned long caller);
+static void httpfetch_request_clear(u64 caller);
-unsigned long httpfetch_caller_alloc()
+u64 httpfetch_caller_alloc()
{
MutexAutoLock lock(g_httpfetch_mutex);
- // Check each caller ID except HTTPFETCH_DISCARD
- const unsigned long discard = HTTPFETCH_DISCARD;
- for (unsigned long caller = discard + 1; caller != discard; ++caller) {
- std::map<unsigned long, std::queue<HTTPFetchResult> >::iterator
- it = g_httpfetch_results.find(caller);
+ // Check each caller ID except reserved ones
+ for (u64 caller = HTTPFETCH_CID_START; caller != 0; ++caller) {
+ auto it = g_httpfetch_results.find(caller);
if (it == g_httpfetch_results.end()) {
verbosestream << "httpfetch_caller_alloc: allocating "
<< caller << std::endl;
@@ -79,18 +78,17 @@ unsigned long httpfetch_caller_alloc()
}
FATAL_ERROR("httpfetch_caller_alloc: ran out of caller IDs");
- return discard;
}
-unsigned long httpfetch_caller_alloc_secure()
+u64 httpfetch_caller_alloc_secure()
{
MutexAutoLock lock(g_httpfetch_mutex);
// Generate random caller IDs and make sure they're not
- // already used or equal to HTTPFETCH_DISCARD
+ // already used or reserved.
// Give up after 100 tries to prevent infinite loop
- u8 tries = 100;
- unsigned long caller;
+ size_t tries = 100;
+ u64 caller;
do {
caller = (((u64) g_callerid_randomness.next()) << 32) |
@@ -100,7 +98,8 @@ unsigned long httpfetch_caller_alloc_secure()
FATAL_ERROR("httpfetch_caller_alloc_secure: ran out of caller IDs");
return HTTPFETCH_DISCARD;
}
- } while (g_httpfetch_results.find(caller) != g_httpfetch_results.end());
+ } while (caller >= HTTPFETCH_CID_START &&
+ g_httpfetch_results.find(caller) != g_httpfetch_results.end());
verbosestream << "httpfetch_caller_alloc_secure: allocating "
<< caller << std::endl;
@@ -110,7 +109,7 @@ unsigned long httpfetch_caller_alloc_secure()
return caller;
}
-void httpfetch_caller_free(unsigned long caller)
+void httpfetch_caller_free(u64 caller)
{
verbosestream<<"httpfetch_caller_free: freeing "
<<caller<<std::endl;
@@ -122,13 +121,12 @@ void httpfetch_caller_free(unsigned long caller)
}
}
-bool httpfetch_async_get(unsigned long caller, HTTPFetchResult &fetch_result)
+bool httpfetch_async_get(u64 caller, HTTPFetchResult &fetch_result)
{
MutexAutoLock lock(g_httpfetch_mutex);
// Check that caller exists
- std::map<unsigned long, std::queue<HTTPFetchResult> >::iterator
- it = g_httpfetch_results.find(caller);
+ auto it = g_httpfetch_results.find(caller);
if (it == g_httpfetch_results.end())
return false;
@@ -138,7 +136,7 @@ bool httpfetch_async_get(unsigned long caller, HTTPFetchResult &fetch_result)
return false;
// Pop first result
- fetch_result = caller_results.front();
+ fetch_result = std::move(caller_results.front());
caller_results.pop();
return true;
}
@@ -243,7 +241,6 @@ HTTPFetchOngoing::HTTPFetchOngoing(const HTTPFetchRequest &request_,
// Set static cURL options
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
- curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 3);
curl_easy_setopt(curl, CURLOPT_ENCODING, "gzip");
@@ -394,10 +391,17 @@ const HTTPFetchResult * HTTPFetchOngoing::complete(CURLcode res)
}
if (res != CURLE_OK) {
- errorstream << request.url << " not found ("
- << curl_easy_strerror(res) << ")"
- << " (response code " << result.response_code << ")"
+ errorstream << "HTTPFetch for " << request.url << " failed ("
+ << curl_easy_strerror(res) << ")" << std::endl;
+ } else if (result.response_code >= 400) {
+ errorstream << "HTTPFetch for " << request.url
+ << " returned response code " << result.response_code
<< std::endl;
+ if (result.caller == HTTPFETCH_PRINT_ERR && !result.data.empty()) {
+ errorstream << "Response body:" << std::endl;
+ safe_print_string(errorstream, result.data);
+ errorstream << std::endl;
+ }
}
return &result;
@@ -475,7 +479,7 @@ public:
m_requests.push_back(req);
}
- void requestClear(unsigned long caller, Event *event)
+ void requestClear(u64 caller, Event *event)
{
Request req;
req.type = RT_CLEAR;
@@ -506,7 +510,7 @@ protected:
}
else if (req.type == RT_CLEAR) {
- unsigned long caller = req.fetch_request.caller;
+ u64 caller = req.fetch_request.caller;
// Abort all ongoing fetches for the caller
for (std::vector<HTTPFetchOngoing*>::iterator
@@ -762,10 +766,12 @@ void httpfetch_cleanup()
{
verbosestream<<"httpfetch_cleanup: cleaning up"<<std::endl;
- g_httpfetch_thread->stop();
- g_httpfetch_thread->requestWakeUp();
- g_httpfetch_thread->wait();
- delete g_httpfetch_thread;
+ if (g_httpfetch_thread) {
+ g_httpfetch_thread->stop();
+ g_httpfetch_thread->requestWakeUp();
+ g_httpfetch_thread->wait();
+ delete g_httpfetch_thread;
+ }
curl_global_cleanup();
}
@@ -777,7 +783,7 @@ void httpfetch_async(const HTTPFetchRequest &fetch_request)
g_httpfetch_thread->start();
}
-static void httpfetch_request_clear(unsigned long caller)
+static void httpfetch_request_clear(u64 caller)
{
if (g_httpfetch_thread->isRunning()) {
Event event;
@@ -826,7 +832,7 @@ void httpfetch_async(const HTTPFetchRequest &fetch_request)
httpfetch_deliver_result(fetch_result);
}
-static void httpfetch_request_clear(unsigned long caller)
+static void httpfetch_request_clear(u64 caller)
{
}
diff --git a/src/httpfetch.h b/src/httpfetch.h
index 3b9f17f0a..a4901e63b 100644
--- a/src/httpfetch.h
+++ b/src/httpfetch.h
@@ -23,10 +23,17 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "util/string.h"
#include "config.h"
-// Can be used in place of "caller" in asynchronous transfers to discard result
-// (used as default value of "caller")
+// These can be used in place of "caller" in to specify special handling.
+// Discard result (used as default value of "caller").
#define HTTPFETCH_DISCARD 0
+// Indicates that the result should not be discarded when performing a
+// synchronous request (since a real caller ID is not needed for synchronous
+// requests because the result does not have to be retrieved later).
#define HTTPFETCH_SYNC 1
+// Print response body to console if the server returns an error code.
+#define HTTPFETCH_PRINT_ERR 2
+// Start of regular allocated caller IDs.
+#define HTTPFETCH_CID_START 3
// Methods
enum HttpMethod : u8
@@ -43,11 +50,11 @@ struct HTTPFetchRequest
// Identifies the caller (for asynchronous requests)
// Ignored by httpfetch_sync
- unsigned long caller = HTTPFETCH_DISCARD;
+ u64 caller = HTTPFETCH_DISCARD;
// Some number that identifies the request
// (when the same caller issues multiple httpfetch_async calls)
- unsigned long request_id = 0;
+ u64 request_id = 0;
// Timeout for the whole transfer, in milliseconds
long timeout;
@@ -85,8 +92,8 @@ struct HTTPFetchResult
long response_code = 0;
std::string data = "";
// The caller and request_id from the corresponding HTTPFetchRequest.
- unsigned long caller = HTTPFETCH_DISCARD;
- unsigned long request_id = 0;
+ u64 caller = HTTPFETCH_DISCARD;
+ u64 request_id = 0;
HTTPFetchResult() = default;
@@ -107,19 +114,19 @@ void httpfetch_async(const HTTPFetchRequest &fetch_request);
// If any fetch for the given caller ID is complete, removes it from the
// result queue, sets the fetch result and returns true. Otherwise returns false.
-bool httpfetch_async_get(unsigned long caller, HTTPFetchResult &fetch_result);
+bool httpfetch_async_get(u64 caller, HTTPFetchResult &fetch_result);
// Allocates a caller ID for httpfetch_async
// Not required if you want to set caller = HTTPFETCH_DISCARD
-unsigned long httpfetch_caller_alloc();
+u64 httpfetch_caller_alloc();
// Allocates a non-predictable caller ID for httpfetch_async
-unsigned long httpfetch_caller_alloc_secure();
+u64 httpfetch_caller_alloc_secure();
// Frees a caller ID allocated with httpfetch_caller_alloc
// Note: This can be expensive, because the httpfetch thread is told
// to stop any ongoing fetches for the given caller.
-void httpfetch_caller_free(unsigned long caller);
+void httpfetch_caller_free(u64 caller);
// Performs a synchronous HTTP request. This blocks and therefore should
// only be used from background threads.
diff --git a/src/hud.cpp b/src/hud.cpp
index 1791e04df..e4ad7940f 100644
--- a/src/hud.cpp
+++ b/src/hud.cpp
@@ -50,6 +50,7 @@ const struct EnumString es_HudElementStat[] =
{HUD_STAT_SIZE, "size"},
{HUD_STAT_Z_INDEX, "z_index"},
{HUD_STAT_TEXT2, "text2"},
+ {HUD_STAT_STYLE, "style"},
{0, NULL},
};
diff --git a/src/hud.h b/src/hud.h
index a0613ae98..769966688 100644
--- a/src/hud.h
+++ b/src/hud.h
@@ -33,6 +33,10 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#define HUD_CORNER_LOWER 1
#define HUD_CORNER_CENTER 2
+#define HUD_STYLE_BOLD 1
+#define HUD_STYLE_ITALIC 2
+#define HUD_STYLE_MONO 4
+
// Note that these visibility flags do not determine if the hud items are
// actually drawn, but rather, whether to draw the item should the rest
// of the game state permit it.
@@ -78,6 +82,7 @@ enum HudElementStat {
HUD_STAT_SIZE,
HUD_STAT_Z_INDEX,
HUD_STAT_TEXT2,
+ HUD_STAT_STYLE,
};
enum HudCompassDir {
@@ -102,6 +107,7 @@ struct HudElement {
v2s32 size;
s16 z_index = 0;
std::string text2;
+ u32 style;
};
extern const EnumString es_HudElementType[];
diff --git a/src/inventory.cpp b/src/inventory.cpp
index fc1aaf371..d14b12f9d 100644
--- a/src/inventory.cpp
+++ b/src/inventory.cpp
@@ -460,7 +460,6 @@ void InventoryList::deSerialize(std::istream &is)
std::getline(is, line, '\n');
std::istringstream iss(line);
- //iss.imbue(std::locale("C"));
std::string name;
std::getline(iss, name, ' ');
@@ -561,11 +560,6 @@ u32 InventoryList::getUsedSlots() const
return num;
}
-u32 InventoryList::getFreeSlots() const
-{
- return getSize() - getUsedSlots();
-}
-
const ItemStack& InventoryList::getItem(u32 i) const
{
assert(i < m_size); // Pre-condition
@@ -938,19 +932,16 @@ void Inventory::deSerialize(std::istream &is)
InventoryList * Inventory::addList(const std::string &name, u32 size)
{
setModified();
+
+ // Remove existing lists
s32 i = getListIndex(name);
- if(i != -1)
- {
- if(m_lists[i]->getSize() != size)
- {
- delete m_lists[i];
- m_lists[i] = new InventoryList(name, size, m_itemdef);
- m_lists[i]->setModified();
- }
+ if (i != -1) {
+ delete m_lists[i];
+ m_lists[i] = new InventoryList(name, size, m_itemdef);
+ m_lists[i]->setModified();
return m_lists[i];
}
-
//don't create list with invalid name
if (name.find(' ') != std::string::npos)
return nullptr;
@@ -999,7 +990,7 @@ const InventoryList *Inventory::getList(const std::string &name) const
return m_lists[i];
}
-const s32 Inventory::getListIndex(const std::string &name) const
+s32 Inventory::getListIndex(const std::string &name) const
{
for(u32 i=0; i<m_lists.size(); i++)
{
diff --git a/src/inventory.h b/src/inventory.h
index f36bc57cf..eb063d4ad 100644
--- a/src/inventory.h
+++ b/src/inventory.h
@@ -211,7 +211,6 @@ public:
u32 getWidth() const;
// Count used slots
u32 getUsedSlots() const;
- u32 getFreeSlots() const;
// Get reference to item
const ItemStack& getItem(u32 i) const;
@@ -298,6 +297,7 @@ public:
void serialize(std::ostream &os, bool incremental = false) const;
void deSerialize(std::istream &is);
+ // Creates a new list if none exists or truncates existing lists
InventoryList * addList(const std::string &name, u32 size);
InventoryList * getList(const std::string &name);
const InventoryList * getList(const std::string &name) const;
@@ -335,7 +335,7 @@ public:
}
private:
// -1 if not found
- const s32 getListIndex(const std::string &name) const;
+ s32 getListIndex(const std::string &name) const;
std::vector<InventoryList*> m_lists;
IItemDefManager *m_itemdef;
diff --git a/src/inventorymanager.cpp b/src/inventorymanager.cpp
index 1e81c1dbc..a159bf786 100644
--- a/src/inventorymanager.cpp
+++ b/src/inventorymanager.cpp
@@ -273,7 +273,7 @@ void IMoveAction::apply(InventoryManager *mgr, ServerActiveObject *player, IGame
}
if (!list_to) {
infostream << "IMoveAction::apply(): FAIL: destination list not found: "
- << "to_inv=\""<<to_inv.dump() << "\""
+ << "to_inv=\"" << to_inv.dump() << "\""
<< ", to_list=\"" << to_list << "\"" << std::endl;
return;
}
@@ -322,12 +322,20 @@ void IMoveAction::apply(InventoryManager *mgr, ServerActiveObject *player, IGame
return;
}
- if ((u16)to_i > list_to->getSize()) {
+ if (from_i < 0 || list_from->getSize() <= (u32) from_i) {
+ infostream << "IMoveAction::apply(): FAIL: source index out of bounds: "
+ << "size of from_list=\"" << list_from->getSize() << "\""
+ << ", from_index=\"" << from_i << "\"" << std::endl;
+ return;
+ }
+
+ if (to_i < 0 || list_to->getSize() <= (u32) to_i) {
infostream << "IMoveAction::apply(): FAIL: destination index out of bounds: "
- << "to_i=" << to_i
- << ", size=" << list_to->getSize() << std::endl;
+ << "size of to_list=\"" << list_to->getSize() << "\""
+ << ", to_index=\"" << to_i << "\"" << std::endl;
return;
}
+
/*
Do not handle rollback if both inventories are that of the same player
*/
diff --git a/src/irrlicht_changes/CGUITTFont.cpp b/src/irrlicht_changes/CGUITTFont.cpp
index bd4e700de..e785ea837 100644
--- a/src/irrlicht_changes/CGUITTFont.cpp
+++ b/src/irrlicht_changes/CGUITTFont.cpp
@@ -103,7 +103,7 @@ video::IImage* SGUITTGlyph::createGlyphImage(const FT_Bitmap& bits, video::IVide
// Load the monochrome data in.
const u32 image_pitch = image->getPitch() / sizeof(u16);
- u16* image_data = (u16*)image->lock();
+ u16* image_data = (u16*)image->getData();
u8* glyph_data = bits.buffer;
for (s32 y = 0; y < (s32)bits.rows; ++y)
@@ -119,7 +119,6 @@ video::IImage* SGUITTGlyph::createGlyphImage(const FT_Bitmap& bits, video::IVide
}
image_data += image_pitch;
}
- image->unlock();
break;
}
@@ -133,7 +132,7 @@ video::IImage* SGUITTGlyph::createGlyphImage(const FT_Bitmap& bits, video::IVide
// Load the grayscale data in.
const float gray_count = static_cast<float>(bits.num_grays);
const u32 image_pitch = image->getPitch() / sizeof(u32);
- u32* image_data = (u32*)image->lock();
+ u32* image_data = (u32*)image->getData();
u8* glyph_data = bits.buffer;
for (s32 y = 0; y < (s32)bits.rows; ++y)
{
@@ -145,7 +144,6 @@ video::IImage* SGUITTGlyph::createGlyphImage(const FT_Bitmap& bits, video::IVide
}
glyph_data += bits.pitch;
}
- image->unlock();
break;
}
default:
@@ -277,7 +275,8 @@ CGUITTFont* CGUITTFont::create(IrrlichtDevice *device, const io::path& filename,
//! Constructor.
CGUITTFont::CGUITTFont(IGUIEnvironment *env)
: use_monochrome(false), use_transparency(true), use_hinting(true), use_auto_hinting(true),
-batch_load_size(1), Device(0), Environment(env), Driver(0), GlobalKerningWidth(0), GlobalKerningHeight(0)
+batch_load_size(1), Device(0), Environment(env), Driver(0), GlobalKerningWidth(0), GlobalKerningHeight(0),
+shadow_offset(0), shadow_alpha(0), fallback(0)
{
#ifdef _DEBUG
setDebugName("CGUITTFont");
@@ -548,12 +547,12 @@ void CGUITTFont::setFontHinting(const bool enable, const bool enable_auto_hintin
void CGUITTFont::draw(const core::stringw& text, const core::rect<s32>& position, video::SColor color, bool hcenter, bool vcenter, const core::rect<s32>* clip)
{
- draw(EnrichedString(std::wstring(text.c_str()), color), position, color, hcenter, vcenter, clip);
+ draw(EnrichedString(std::wstring(text.c_str()), color), position, hcenter, vcenter, clip);
}
-void CGUITTFont::draw(const EnrichedString &text, const core::rect<s32>& position, video::SColor color, bool hcenter, bool vcenter, const core::rect<s32>* clip)
+void CGUITTFont::draw(const EnrichedString &text, const core::rect<s32>& position, bool hcenter, bool vcenter, const core::rect<s32>* clip)
{
- std::vector<video::SColor> colors = text.getColors();
+ const std::vector<video::SColor> &colors = text.getColors();
if (!Driver)
return;
@@ -563,6 +562,7 @@ void CGUITTFont::draw(const EnrichedString &text, const core::rect<s32>& positio
{
Glyph_Pages[i]->render_positions.clear();
Glyph_Pages[i]->render_source_rects.clear();
+ Glyph_Pages[i]->render_colors.clear();
}
// Set up some variables.
@@ -591,7 +591,6 @@ void CGUITTFont::draw(const EnrichedString &text, const core::rect<s32>& positio
u32 n;
uchar32_t previousChar = 0;
core::ustring::const_iterator iter(utext);
- std::vector<video::SColor> applied_colors;
while (!iter.atEnd())
{
uchar32_t currentChar = *iter;
@@ -637,12 +636,36 @@ void CGUITTFont::draw(const EnrichedString &text, const core::rect<s32>& positio
CGUITTGlyphPage* const page = Glyph_Pages[glyph.glyph_page];
page->render_positions.push_back(core::position2di(offset.X + offx, offset.Y + offy));
page->render_source_rects.push_back(glyph.source_rect);
+ if (iter.getPos() < colors.size())
+ page->render_colors.push_back(colors[iter.getPos()]);
+ else
+ page->render_colors.push_back(video::SColor(255,255,255,255));
Render_Map.set(glyph.glyph_page, page);
- u32 current_color = iter.getPos();
- if (current_color < colors.size())
- applied_colors.push_back(colors[current_color]);
}
- offset.X += getWidthFromCharacter(currentChar);
+ if (n > 0)
+ {
+ offset.X += getWidthFromCharacter(currentChar);
+ }
+ else if (fallback != 0)
+ {
+ // Let the fallback font draw it, this isn't super efficient but hopefully that doesn't matter
+ wchar_t l1[] = { (wchar_t) currentChar, 0 }, l2 = (wchar_t) previousChar;
+
+ if (visible)
+ {
+ // Apply kerning.
+ offset.X += fallback->getKerningWidth(l1, &l2);
+ offset.Y += fallback->getKerningHeight();
+
+ u32 current_color = iter.getPos();
+ fallback->draw(core::stringw(l1),
+ core::rect<s32>({offset.X-1, offset.Y-1}, position.LowerRightCorner), // ???
+ current_color < colors.size() ? colors[current_color] : video::SColor(255, 255, 255, 255),
+ false, false, clip);
+ }
+
+ offset.X += fallback->getDimension(l1).Width;
+ }
previousChar = currentChar;
++iter;
@@ -666,16 +689,24 @@ void CGUITTFont::draw(const EnrichedString &text, const core::rect<s32>& positio
for (size_t i = 0; i < page->render_positions.size(); ++i)
page->render_positions[i] -= core::vector2di(shadow_offset, shadow_offset);
}
+ // render runs of matching color in batch
+ size_t ibegin;
+ video::SColor colprev;
for (size_t i = 0; i < page->render_positions.size(); ++i) {
- irr::video::SColor col;
- if (!applied_colors.empty()) {
- col = applied_colors[i < applied_colors.size() ? i : 0];
- } else {
- col = irr::video::SColor(255, 255, 255, 255);
- }
+ ibegin = i;
+ colprev = page->render_colors[i];
+ do
+ ++i;
+ while (i < page->render_positions.size() && page->render_colors[i] == colprev);
+ core::array<core::vector2di> tmp_positions;
+ core::array<core::recti> tmp_source_rects;
+ tmp_positions.set_pointer(&page->render_positions[ibegin], i - ibegin, false, false); // no copy
+ tmp_source_rects.set_pointer(&page->render_source_rects[ibegin], i - ibegin, false, false);
+ --i;
+
if (!use_transparency)
- col.color |= 0xff000000;
- Driver->draw2DImage(page->texture, page->render_positions[i], page->render_source_rects[i], clip, col, true);
+ colprev.color |= 0xff000000;
+ Driver->draw2DImageBatch(page->texture, tmp_positions, tmp_source_rects, clip, colprev, true);
}
}
}
@@ -768,6 +799,12 @@ inline u32 CGUITTFont::getWidthFromCharacter(uchar32_t c) const
int w = Glyphs[n-1].advance.x / 64;
return w;
}
+ if (fallback != 0)
+ {
+ wchar_t s[] = { (wchar_t) c, 0 };
+ return fallback->getDimension(s).Width;
+ }
+
if (c >= 0x2000)
return (font_metrics.ascender / 64);
else return (font_metrics.ascender / 64) / 2;
@@ -791,6 +828,12 @@ inline u32 CGUITTFont::getHeightFromCharacter(uchar32_t c) const
s32 height = (font_metrics.ascender / 64) - Glyphs[n-1].offset.Y + Glyphs[n-1].source_rect.getHeight();
return height;
}
+ if (fallback != 0)
+ {
+ wchar_t s[] = { (wchar_t) c, 0 };
+ return fallback->getDimension(s).Height;
+ }
+
if (c >= 0x2000)
return (font_metrics.ascender / 64);
else return (font_metrics.ascender / 64) / 2;
@@ -806,9 +849,9 @@ u32 CGUITTFont::getGlyphIndexByChar(uchar32_t c) const
// Get the glyph.
u32 glyph = FT_Get_Char_Index(tt_face, c);
- // Check for a valid glyph. If it is invalid, attempt to use the replacement character.
+ // Check for a valid glyph.
if (glyph == 0)
- glyph = FT_Get_Char_Index(tt_face, core::unicode::UTF_REPLACEMENT_CHARACTER);
+ return 0;
// If our glyph is already loaded, don't bother doing any batch loading code.
if (glyph != 0 && Glyphs[glyph - 1].isLoaded)
@@ -924,13 +967,26 @@ core::vector2di CGUITTFont::getKerning(const uchar32_t thisLetter, const uchar32
core::vector2di ret(GlobalKerningWidth, GlobalKerningHeight);
+ u32 n = getGlyphIndexByChar(thisLetter);
+
+ // If we don't have this glyph, ask fallback font
+ if (n == 0)
+ {
+ if (fallback != 0) {
+ wchar_t l1 = (wchar_t) thisLetter, l2 = (wchar_t) previousLetter;
+ ret.X = fallback->getKerningWidth(&l1, &l2);
+ ret.Y = fallback->getKerningHeight();
+ }
+ return ret;
+ }
+
// If we don't have kerning, no point in continuing.
if (!FT_HAS_KERNING(tt_face))
return ret;
// Get the kerning information.
FT_Vector v;
- FT_Get_Kerning(tt_face, getGlyphIndexByChar(previousLetter), getGlyphIndexByChar(thisLetter), FT_KERNING_DEFAULT, &v);
+ FT_Get_Kerning(tt_face, getGlyphIndexByChar(previousLetter), n, FT_KERNING_DEFAULT, &v);
// If we have a scalable font, the return value will be in font points.
if (FT_IS_SCALABLE(tt_face))
@@ -962,6 +1018,9 @@ void CGUITTFont::setInvisibleCharacters(const core::ustring& s)
video::IImage* CGUITTFont::createTextureFromChar(const uchar32_t& ch)
{
u32 n = getGlyphIndexByChar(ch);
+ if (n == 0)
+ n = getGlyphIndexByChar((uchar32_t) core::unicode::UTF_REPLACEMENT_CHARACTER);
+
const SGUITTGlyph& glyph = Glyphs[n-1];
CGUITTGlyphPage* page = Glyph_Pages[glyph.glyph_page];
@@ -971,11 +1030,7 @@ video::IImage* CGUITTFont::createTextureFromChar(const uchar32_t& ch)
video::ITexture* tex = page->texture;
// Acquire a read-only lock of the corresponding page texture.
- #if IRRLICHT_VERSION_MAJOR==1 && IRRLICHT_VERSION_MINOR>=8
void* ptr = tex->lock(video::ETLM_READ_ONLY);
- #else
- void* ptr = tex->lock(true);
- #endif
video::ECOLOR_FORMAT format = tex->getColorFormat();
core::dimension2du tex_size = tex->getOriginalSize();
@@ -1132,11 +1187,7 @@ core::array<scene::ISceneNode*> CGUITTFont::addTextSceneNode(const wchar_t* text
// Now we copy planes corresponding to the letter size.
IMeshManipulator* mani = smgr->getMeshManipulator();
IMesh* meshcopy = mani->createMeshCopy(shared_plane_ptr_);
- #if IRRLICHT_VERSION_MAJOR==1 && IRRLICHT_VERSION_MINOR>=8
mani->scale(meshcopy, vector3df((f32)letter_size.Width, (f32)letter_size.Height, 1));
- #else
- mani->scaleMesh(meshcopy, vector3df((f32)letter_size.Width, (f32)letter_size.Height, 1));
- #endif
ISceneNode* current_node = smgr->addMeshSceneNode(meshcopy, parent, -1, current_pos);
meshcopy->drop();
@@ -1149,6 +1200,8 @@ core::array<scene::ISceneNode*> CGUITTFont::addTextSceneNode(const wchar_t* text
container.push_back(current_node);
}
offset.X += getWidthFromCharacter(current_char);
+ // Note that fallback font handling is missing here (Minetest never uses this)
+
previous_char = current_char;
++text;
}
diff --git a/src/irrlicht_changes/CGUITTFont.h b/src/irrlicht_changes/CGUITTFont.h
index 310f74f67..7b04ae828 100644
--- a/src/irrlicht_changes/CGUITTFont.h
+++ b/src/irrlicht_changes/CGUITTFont.h
@@ -34,7 +34,7 @@
#include <irrlicht.h>
#include <ft2build.h>
#include <vector>
-#include "irrUString.h"
+#include <irrUString.h>
#include "util/enriched_string.h"
#include FT_FREETYPE_H
@@ -199,6 +199,7 @@ namespace gui
core::array<core::vector2di> render_positions;
core::array<core::recti> render_source_rects;
+ core::array<video::SColor> render_colors;
private:
core::array<const SGUITTGlyph*> glyph_to_be_paged;
@@ -269,8 +270,8 @@ namespace gui
video::SColor color, bool hcenter=false, bool vcenter=false,
const core::rect<s32>* clip=0);
- virtual void draw(const EnrichedString& text, const core::rect<s32>& position,
- video::SColor color, bool hcenter=false, bool vcenter=false,
+ void draw(const EnrichedString& text, const core::rect<s32>& position,
+ bool hcenter=false, bool vcenter=false,
const core::rect<s32>* clip=0);
//! Returns the dimension of a character produced by this font.
@@ -313,6 +314,9 @@ namespace gui
//! Get the last glyph page's index.
u32 getLastGlyphPageIndex() const { return Glyph_Pages.size() - 1; }
+ //! Set font that should be used for glyphs not present in ours
+ void setFallback(gui::IGUIFont* font) { fallback = font; }
+
//! Create corresponding character's software image copy from the font,
//! so you can use this data just like any ordinary video::IImage.
//! \param ch The character you need
@@ -387,6 +391,8 @@ namespace gui
core::ustring Invisible;
u32 shadow_offset;
u32 shadow_alpha;
+
+ gui::IGUIFont* fallback;
};
} // end namespace gui
diff --git a/src/irrlicht_changes/CMakeLists.txt b/src/irrlicht_changes/CMakeLists.txt
index d2f66ab77..19f431af3 100644
--- a/src/irrlicht_changes/CMakeLists.txt
+++ b/src/irrlicht_changes/CMakeLists.txt
@@ -1,14 +1,9 @@
if (BUILD_CLIENT)
set(client_irrlicht_changes_SRCS
${CMAKE_CURRENT_SOURCE_DIR}/static_text.cpp
+ ${CMAKE_CURRENT_SOURCE_DIR}/CGUITTFont.cpp
)
- if (ENABLE_FREETYPE)
- set(client_irrlicht_changes_SRCS ${client_irrlicht_changes_SRCS}
- ${CMAKE_CURRENT_SOURCE_DIR}/CGUITTFont.cpp
- )
- endif()
-
# CMake require us to set a local scope and then parent scope
# Else the last set win in parent scope
set(client_irrlicht_changes_SRCS ${client_irrlicht_changes_SRCS} PARENT_SCOPE)
diff --git a/src/irrlicht_changes/irrUString.h b/src/irrlicht_changes/irrUString.h
deleted file mode 100644
index 09172ee6d..000000000
--- a/src/irrlicht_changes/irrUString.h
+++ /dev/null
@@ -1,3891 +0,0 @@
-/*
- Basic Unicode string class for Irrlicht.
- Copyright (c) 2009-2011 John Norman
-
- This software is provided 'as-is', without any express or implied
- warranty. In no event will the authors be held liable for any
- damages arising from the use of this software.
-
- Permission is granted to anyone to use this software for any
- purpose, including commercial applications, and to alter it and
- redistribute it freely, subject to the following restrictions:
-
- 1. The origin of this software must not be misrepresented; you
- must not claim that you wrote the original software. If you use
- this software in a product, an acknowledgment in the product
- documentation would be appreciated but is not required.
-
- 2. Altered source versions must be plainly marked as such, and
- must not be misrepresented as being the original software.
-
- 3. This notice may not be removed or altered from any source
- distribution.
-
- The original version of this class can be located at:
- http://irrlicht.suckerfreegames.com/
-
- John Norman
- john@suckerfreegames.com
-*/
-
-#pragma once
-
-#if (__cplusplus > 199711L) || (_MSC_VER >= 1600) || defined(__GXX_EXPERIMENTAL_CXX0X__)
-# define USTRING_CPP0X
-# if defined(__GXX_EXPERIMENTAL_CXX0X__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 5)))
-# define USTRING_CPP0X_NEWLITERALS
-# endif
-#endif
-
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-#include <cstddef>
-
-#ifdef _WIN32
-#define __BYTE_ORDER 0
-#define __LITTLE_ENDIAN 0
-#define __BIG_ENDIAN 1
-#elif defined(__MACH__) && defined(__APPLE__)
-#include <machine/endian.h>
-#elif defined(__FreeBSD__) || defined(__DragonFly__)
-#include <sys/endian.h>
-#else
-#include <endian.h>
-#endif
-
-#ifdef USTRING_CPP0X
-# include <utility>
-#endif
-
-#ifndef USTRING_NO_STL
-# include <string>
-# include <iterator>
-# include <ostream>
-#endif
-
-#include "irrTypes.h"
-#include "irrAllocator.h"
-#include "irrArray.h"
-#include "irrMath.h"
-#include "irrString.h"
-#include "path.h"
-
-//! UTF-16 surrogate start values.
-static const irr::u16 UTF16_HI_SURROGATE = 0xD800;
-static const irr::u16 UTF16_LO_SURROGATE = 0xDC00;
-
-//! Is a UTF-16 code point a surrogate?
-#define UTF16_IS_SURROGATE(c) (((c) & 0xF800) == 0xD800)
-#define UTF16_IS_SURROGATE_HI(c) (((c) & 0xFC00) == 0xD800)
-#define UTF16_IS_SURROGATE_LO(c) (((c) & 0xFC00) == 0xDC00)
-
-
-namespace irr
-{
-
- // Define our character types.
-#ifdef USTRING_CPP0X_NEWLITERALS // C++0x
- typedef char32_t uchar32_t;
- typedef char16_t uchar16_t;
- typedef char uchar8_t;
-#else
- typedef u32 uchar32_t;
- typedef u16 uchar16_t;
- typedef u8 uchar8_t;
-#endif
-
-namespace core
-{
-
-namespace unicode
-{
-
-//! The unicode replacement character. Used to replace invalid characters.
-const irr::u16 UTF_REPLACEMENT_CHARACTER = 0xFFFD;
-
-//! Convert a UTF-16 surrogate pair into a UTF-32 character.
-//! \param high The high value of the pair.
-//! \param low The low value of the pair.
-//! \return The UTF-32 character expressed by the surrogate pair.
-inline uchar32_t toUTF32(uchar16_t high, uchar16_t low)
-{
- // Convert the surrogate pair into a single UTF-32 character.
- uchar32_t x = ((high & ((1 << 6) -1)) << 10) | (low & ((1 << 10) -1));
- uchar32_t wu = ((high >> 6) & ((1 << 5) - 1)) + 1;
- return (wu << 16) | x;
-}
-
-//! Swaps the endianness of a 16-bit value.
-//! \return The new value.
-inline uchar16_t swapEndian16(const uchar16_t& c)
-{
- return ((c >> 8) & 0x00FF) | ((c << 8) & 0xFF00);
-}
-
-//! Swaps the endianness of a 32-bit value.
-//! \return The new value.
-inline uchar32_t swapEndian32(const uchar32_t& c)
-{
- return ((c >> 24) & 0x000000FF) |
- ((c >> 8) & 0x0000FF00) |
- ((c << 8) & 0x00FF0000) |
- ((c << 24) & 0xFF000000);
-}
-
-//! The Unicode byte order mark.
-const u16 BOM = 0xFEFF;
-
-//! The size of the Unicode byte order mark in terms of the Unicode character size.
-const u8 BOM_UTF8_LEN = 3;
-const u8 BOM_UTF16_LEN = 1;
-const u8 BOM_UTF32_LEN = 1;
-
-//! Unicode byte order marks for file operations.
-const u8 BOM_ENCODE_UTF8[3] = { 0xEF, 0xBB, 0xBF };
-const u8 BOM_ENCODE_UTF16_BE[2] = { 0xFE, 0xFF };
-const u8 BOM_ENCODE_UTF16_LE[2] = { 0xFF, 0xFE };
-const u8 BOM_ENCODE_UTF32_BE[4] = { 0x00, 0x00, 0xFE, 0xFF };
-const u8 BOM_ENCODE_UTF32_LE[4] = { 0xFF, 0xFE, 0x00, 0x00 };
-
-//! The size in bytes of the Unicode byte marks for file operations.
-const u8 BOM_ENCODE_UTF8_LEN = 3;
-const u8 BOM_ENCODE_UTF16_LEN = 2;
-const u8 BOM_ENCODE_UTF32_LEN = 4;
-
-//! Unicode encoding type.
-enum EUTF_ENCODE
-{
- EUTFE_NONE = 0,
- EUTFE_UTF8,
- EUTFE_UTF16,
- EUTFE_UTF16_LE,
- EUTFE_UTF16_BE,
- EUTFE_UTF32,
- EUTFE_UTF32_LE,
- EUTFE_UTF32_BE
-};
-
-//! Unicode endianness.
-enum EUTF_ENDIAN
-{
- EUTFEE_NATIVE = 0,
- EUTFEE_LITTLE,
- EUTFEE_BIG
-};
-
-//! Returns the specified unicode byte order mark in a byte array.
-//! The byte order mark is the first few bytes in a text file that signifies its encoding.
-/** \param mode The Unicode encoding method that we want to get the byte order mark for.
- If EUTFE_UTF16 or EUTFE_UTF32 is passed, it uses the native system endianness. **/
-//! \return An array that contains a byte order mark.
-inline core::array<u8> getUnicodeBOM(EUTF_ENCODE mode)
-{
-#define COPY_ARRAY(source, size) \
- memcpy(ret.pointer(), source, size); \
- ret.set_used(size)
-
- core::array<u8> ret(4);
- switch (mode)
- {
- case EUTFE_UTF8:
- COPY_ARRAY(BOM_ENCODE_UTF8, BOM_ENCODE_UTF8_LEN);
- break;
- case EUTFE_UTF16:
- #ifdef __BIG_ENDIAN__
- COPY_ARRAY(BOM_ENCODE_UTF16_BE, BOM_ENCODE_UTF16_LEN);
- #else
- COPY_ARRAY(BOM_ENCODE_UTF16_LE, BOM_ENCODE_UTF16_LEN);
- #endif
- break;
- case EUTFE_UTF16_BE:
- COPY_ARRAY(BOM_ENCODE_UTF16_BE, BOM_ENCODE_UTF16_LEN);
- break;
- case EUTFE_UTF16_LE:
- COPY_ARRAY(BOM_ENCODE_UTF16_LE, BOM_ENCODE_UTF16_LEN);
- break;
- case EUTFE_UTF32:
- #ifdef __BIG_ENDIAN__
- COPY_ARRAY(BOM_ENCODE_UTF32_BE, BOM_ENCODE_UTF32_LEN);
- #else
- COPY_ARRAY(BOM_ENCODE_UTF32_LE, BOM_ENCODE_UTF32_LEN);
- #endif
- break;
- case EUTFE_UTF32_BE:
- COPY_ARRAY(BOM_ENCODE_UTF32_BE, BOM_ENCODE_UTF32_LEN);
- break;
- case EUTFE_UTF32_LE:
- COPY_ARRAY(BOM_ENCODE_UTF32_LE, BOM_ENCODE_UTF32_LEN);
- break;
- case EUTFE_NONE:
- // TODO sapier: fixed warning only,
- // don't know if something needs to be done here
- break;
- }
- return ret;
-
-#undef COPY_ARRAY
-}
-
-//! Detects if the given data stream starts with a unicode BOM.
-//! \param data The data stream to check.
-//! \return The unicode BOM associated with the data stream, or EUTFE_NONE if none was found.
-inline EUTF_ENCODE determineUnicodeBOM(const char* data)
-{
- if (memcmp(data, BOM_ENCODE_UTF8, 3) == 0) return EUTFE_UTF8;
- if (memcmp(data, BOM_ENCODE_UTF16_BE, 2) == 0) return EUTFE_UTF16_BE;
- if (memcmp(data, BOM_ENCODE_UTF16_LE, 2) == 0) return EUTFE_UTF16_LE;
- if (memcmp(data, BOM_ENCODE_UTF32_BE, 4) == 0) return EUTFE_UTF32_BE;
- if (memcmp(data, BOM_ENCODE_UTF32_LE, 4) == 0) return EUTFE_UTF32_LE;
- return EUTFE_NONE;
-}
-
-} // end namespace unicode
-
-
-//! UTF-16 string class.
-template <typename TAlloc = irrAllocator<uchar16_t> >
-class ustring16
-{
-public:
-
- ///------------------///
- /// iterator classes ///
- ///------------------///
-
- //! Access an element in a unicode string, allowing one to change it.
- class _ustring16_iterator_access
- {
- public:
- _ustring16_iterator_access(const ustring16<TAlloc>* s, u32 p) : ref(s), pos(p) {}
-
- //! Allow the class to be interpreted as a single UTF-32 character.
- operator uchar32_t() const
- {
- return _get();
- }
-
- //! Allow one to change the character in the unicode string.
- //! \param c The new character to use.
- //! \return Myself.
- _ustring16_iterator_access& operator=(const uchar32_t c)
- {
- _set(c);
- return *this;
- }
-
- //! Increments the value by 1.
- //! \return Myself.
- _ustring16_iterator_access& operator++()
- {
- _set(_get() + 1);
- return *this;
- }
-
- //! Increments the value by 1, returning the old value.
- //! \return A unicode character.
- uchar32_t operator++(int)
- {
- uchar32_t old = _get();
- _set(old + 1);
- return old;
- }
-
- //! Decrements the value by 1.
- //! \return Myself.
- _ustring16_iterator_access& operator--()
- {
- _set(_get() - 1);
- return *this;
- }
-
- //! Decrements the value by 1, returning the old value.
- //! \return A unicode character.
- uchar32_t operator--(int)
- {
- uchar32_t old = _get();
- _set(old - 1);
- return old;
- }
-
- //! Adds to the value by a specified amount.
- //! \param val The amount to add to this character.
- //! \return Myself.
- _ustring16_iterator_access& operator+=(int val)
- {
- _set(_get() + val);
- return *this;
- }
-
- //! Subtracts from the value by a specified amount.
- //! \param val The amount to subtract from this character.
- //! \return Myself.
- _ustring16_iterator_access& operator-=(int val)
- {
- _set(_get() - val);
- return *this;
- }
-
- //! Multiples the value by a specified amount.
- //! \param val The amount to multiply this character by.
- //! \return Myself.
- _ustring16_iterator_access& operator*=(int val)
- {
- _set(_get() * val);
- return *this;
- }
-
- //! Divides the value by a specified amount.
- //! \param val The amount to divide this character by.
- //! \return Myself.
- _ustring16_iterator_access& operator/=(int val)
- {
- _set(_get() / val);
- return *this;
- }
-
- //! Modulos the value by a specified amount.
- //! \param val The amount to modulo this character by.
- //! \return Myself.
- _ustring16_iterator_access& operator%=(int val)
- {
- _set(_get() % val);
- return *this;
- }
-
- //! Adds to the value by a specified amount.
- //! \param val The amount to add to this character.
- //! \return A unicode character.
- uchar32_t operator+(int val) const
- {
- return _get() + val;
- }
-
- //! Subtracts from the value by a specified amount.
- //! \param val The amount to subtract from this character.
- //! \return A unicode character.
- uchar32_t operator-(int val) const
- {
- return _get() - val;
- }
-
- //! Multiplies the value by a specified amount.
- //! \param val The amount to multiply this character by.
- //! \return A unicode character.
- uchar32_t operator*(int val) const
- {
- return _get() * val;
- }
-
- //! Divides the value by a specified amount.
- //! \param val The amount to divide this character by.
- //! \return A unicode character.
- uchar32_t operator/(int val) const
- {
- return _get() / val;
- }
-
- //! Modulos the value by a specified amount.
- //! \param val The amount to modulo this character by.
- //! \return A unicode character.
- uchar32_t operator%(int val) const
- {
- return _get() % val;
- }
-
- private:
- //! Gets a uchar32_t from our current position.
- uchar32_t _get() const
- {
- const uchar16_t* a = ref->c_str();
- if (!UTF16_IS_SURROGATE(a[pos]))
- return static_cast<uchar32_t>(a[pos]);
- else
- {
- if (pos + 1 >= ref->size_raw())
- return 0;
-
- return unicode::toUTF32(a[pos], a[pos + 1]);
- }
- }
-
- //! Sets a uchar32_t at our current position.
- void _set(uchar32_t c)
- {
- ustring16<TAlloc>* ref2 = const_cast<ustring16<TAlloc>*>(ref);
- const uchar16_t* a = ref2->c_str();
- if (c > 0xFFFF)
- {
- // c will be multibyte, so split it up into the high and low surrogate pairs.
- uchar16_t x = static_cast<uchar16_t>(c);
- uchar16_t vh = UTF16_HI_SURROGATE | ((((c >> 16) & ((1 << 5) - 1)) - 1) << 6) | (x >> 10);
- uchar16_t vl = UTF16_LO_SURROGATE | (x & ((1 << 10) - 1));
-
- // If the previous position was a surrogate pair, just replace them. Else, insert the low pair.
- if (UTF16_IS_SURROGATE_HI(a[pos]) && pos + 1 != ref2->size_raw())
- ref2->replace_raw(vl, static_cast<u32>(pos) + 1);
- else ref2->insert_raw(vl, static_cast<u32>(pos) + 1);
-
- ref2->replace_raw(vh, static_cast<u32>(pos));
- }
- else
- {
- // c will be a single byte.
- uchar16_t vh = static_cast<uchar16_t>(c);
-
- // If the previous position was a surrogate pair, remove the extra byte.
- if (UTF16_IS_SURROGATE_HI(a[pos]))
- ref2->erase_raw(static_cast<u32>(pos) + 1);
-
- ref2->replace_raw(vh, static_cast<u32>(pos));
- }
- }
-
- const ustring16<TAlloc>* ref;
- u32 pos;
- };
- typedef typename ustring16<TAlloc>::_ustring16_iterator_access access;
-
-
- //! Iterator to iterate through a UTF-16 string.
-#ifndef USTRING_NO_STL
- class _ustring16_const_iterator : public std::iterator<
- std::bidirectional_iterator_tag, // iterator_category
- access, // value_type
- ptrdiff_t, // difference_type
- const access, // pointer
- const access // reference
- >
-#else
- class _ustring16_const_iterator
-#endif
- {
- public:
- typedef _ustring16_const_iterator _Iter;
- typedef std::iterator<std::bidirectional_iterator_tag, access, ptrdiff_t, const access, const access> _Base;
- typedef const access const_pointer;
- typedef const access const_reference;
-
-#ifndef USTRING_NO_STL
- typedef typename _Base::value_type value_type;
- typedef typename _Base::difference_type difference_type;
- typedef typename _Base::difference_type distance_type;
- typedef typename _Base::pointer pointer;
- typedef const_reference reference;
-#else
- typedef access value_type;
- typedef u32 difference_type;
- typedef u32 distance_type;
- typedef const_pointer pointer;
- typedef const_reference reference;
-#endif
-
- //! Constructors.
- _ustring16_const_iterator(const _Iter& i) : ref(i.ref), pos(i.pos) {}
- _ustring16_const_iterator(const ustring16<TAlloc>& s) : ref(&s), pos(0) {}
- _ustring16_const_iterator(const ustring16<TAlloc>& s, const u32 p) : ref(&s), pos(0)
- {
- if (ref->size_raw() == 0 || p == 0)
- return;
-
- // Go to the appropriate position.
- u32 i = p;
- u32 sr = ref->size_raw();
- const uchar16_t* a = ref->c_str();
- while (i != 0 && pos < sr)
- {
- if (UTF16_IS_SURROGATE_HI(a[pos]))
- pos += 2;
- else ++pos;
- --i;
- }
- }
-
- //! Test for equalness.
- bool operator==(const _Iter& iter) const
- {
- if (ref == iter.ref && pos == iter.pos)
- return true;
- return false;
- }
-
- //! Test for unequalness.
- bool operator!=(const _Iter& iter) const
- {
- if (ref != iter.ref || pos != iter.pos)
- return true;
- return false;
- }
-
- //! Switch to the next full character in the string.
- _Iter& operator++()
- { // ++iterator
- if (pos == ref->size_raw()) return *this;
- const uchar16_t* a = ref->c_str();
- if (UTF16_IS_SURROGATE_HI(a[pos]))
- pos += 2; // TODO: check for valid low surrogate?
- else ++pos;
- if (pos > ref->size_raw()) pos = ref->size_raw();
- return *this;
- }
-
- //! Switch to the next full character in the string, returning the previous position.
- _Iter operator++(int)
- { // iterator++
- _Iter _tmp(*this);
- ++*this;
- return _tmp;
- }
-
- //! Switch to the previous full character in the string.
- _Iter& operator--()
- { // --iterator
- if (pos == 0) return *this;
- const uchar16_t* a = ref->c_str();
- --pos;
- if (UTF16_IS_SURROGATE_LO(a[pos]) && pos != 0) // low surrogate, go back one more.
- --pos;
- return *this;
- }
-
- //! Switch to the previous full character in the string, returning the previous position.
- _Iter operator--(int)
- { // iterator--
- _Iter _tmp(*this);
- --*this;
- return _tmp;
- }
-
- //! Advance a specified number of full characters in the string.
- //! \return Myself.
- _Iter& operator+=(const difference_type v)
- {
- if (v == 0) return *this;
- if (v < 0) return operator-=(v * -1);
-
- if (pos >= ref->size_raw())
- return *this;
-
- // Go to the appropriate position.
- // TODO: Don't force u32 on an x64 OS. Make it agnostic.
- u32 i = (u32)v;
- u32 sr = ref->size_raw();
- const uchar16_t* a = ref->c_str();
- while (i != 0 && pos < sr)
- {
- if (UTF16_IS_SURROGATE_HI(a[pos]))
- pos += 2;
- else ++pos;
- --i;
- }
- if (pos > sr)
- pos = sr;
-
- return *this;
- }
-
- //! Go back a specified number of full characters in the string.
- //! \return Myself.
- _Iter& operator-=(const difference_type v)
- {
- if (v == 0) return *this;
- if (v > 0) return operator+=(v * -1);
-
- if (pos == 0)
- return *this;
-
- // Go to the appropriate position.
- // TODO: Don't force u32 on an x64 OS. Make it agnostic.
- u32 i = (u32)v;
- const uchar16_t* a = ref->c_str();
- while (i != 0 && pos != 0)
- {
- --pos;
- if (UTF16_IS_SURROGATE_LO(a[pos]) != 0 && pos != 0)
- --pos;
- --i;
- }
-
- return *this;
- }
-
- //! Return a new iterator that is a variable number of full characters forward from the current position.
- _Iter operator+(const difference_type v) const
- {
- _Iter ret(*this);
- ret += v;
- return ret;
- }
-
- //! Return a new iterator that is a variable number of full characters backward from the current position.
- _Iter operator-(const difference_type v) const
- {
- _Iter ret(*this);
- ret -= v;
- return ret;
- }
-
- //! Returns the distance between two iterators.
- difference_type operator-(const _Iter& iter) const
- {
- // Make sure we reference the same object!
- if (ref != iter.ref)
- return difference_type();
-
- _Iter i = iter;
- difference_type ret;
-
- // Walk up.
- if (pos > i.pos)
- {
- while (pos > i.pos)
- {
- ++i;
- ++ret;
- }
- return ret;
- }
-
- // Walk down.
- while (pos < i.pos)
- {
- --i;
- --ret;
- }
- return ret;
- }
-
- //! Accesses the full character at the iterator's position.
- const_reference operator*() const
- {
- if (pos >= ref->size_raw())
- {
- const uchar16_t* a = ref->c_str();
- u32 p = ref->size_raw();
- if (UTF16_IS_SURROGATE_LO(a[p]))
- --p;
- reference ret(ref, p);
- return ret;
- }
- const_reference ret(ref, pos);
- return ret;
- }
-
- //! Accesses the full character at the iterator's position.
- reference operator*()
- {
- if (pos >= ref->size_raw())
- {
- const uchar16_t* a = ref->c_str();
- u32 p = ref->size_raw();
- if (UTF16_IS_SURROGATE_LO(a[p]))
- --p;
- reference ret(ref, p);
- return ret;
- }
- reference ret(ref, pos);
- return ret;
- }
-
- //! Accesses the full character at the iterator's position.
- const_pointer operator->() const
- {
- return operator*();
- }
-
- //! Accesses the full character at the iterator's position.
- pointer operator->()
- {
- return operator*();
- }
-
- //! Is the iterator at the start of the string?
- bool atStart() const
- {
- return pos == 0;
- }
-
- //! Is the iterator at the end of the string?
- bool atEnd() const
- {
- const uchar16_t* a = ref->c_str();
- if (UTF16_IS_SURROGATE(a[pos]))
- return (pos + 1) >= ref->size_raw();
- else return pos >= ref->size_raw();
- }
-
- //! Moves the iterator to the start of the string.
- void toStart()
- {
- pos = 0;
- }
-
- //! Moves the iterator to the end of the string.
- void toEnd()
- {
- pos = ref->size_raw();
- }
-
- //! Returns the iterator's position.
- //! \return The iterator's position.
- u32 getPos() const
- {
- return pos;
- }
-
- protected:
- const ustring16<TAlloc>* ref;
- u32 pos;
- };
-
- //! Iterator to iterate through a UTF-16 string.
- class _ustring16_iterator : public _ustring16_const_iterator
- {
- public:
- typedef _ustring16_iterator _Iter;
- typedef _ustring16_const_iterator _Base;
- typedef typename _Base::const_pointer const_pointer;
- typedef typename _Base::const_reference const_reference;
-
-
- typedef typename _Base::value_type value_type;
- typedef typename _Base::difference_type difference_type;
- typedef typename _Base::distance_type distance_type;
- typedef access pointer;
- typedef access reference;
-
- using _Base::pos;
- using _Base::ref;
-
- //! Constructors.
- _ustring16_iterator(const _Iter& i) : _ustring16_const_iterator(i) {}
- _ustring16_iterator(const ustring16<TAlloc>& s) : _ustring16_const_iterator(s) {}
- _ustring16_iterator(const ustring16<TAlloc>& s, const u32 p) : _ustring16_const_iterator(s, p) {}
-
- //! Accesses the full character at the iterator's position.
- reference operator*() const
- {
- if (pos >= ref->size_raw())
- {
- const uchar16_t* a = ref->c_str();
- u32 p = ref->size_raw();
- if (UTF16_IS_SURROGATE_LO(a[p]))
- --p;
- reference ret(ref, p);
- return ret;
- }
- reference ret(ref, pos);
- return ret;
- }
-
- //! Accesses the full character at the iterator's position.
- reference operator*()
- {
- if (pos >= ref->size_raw())
- {
- const uchar16_t* a = ref->c_str();
- u32 p = ref->size_raw();
- if (UTF16_IS_SURROGATE_LO(a[p]))
- --p;
- reference ret(ref, p);
- return ret;
- }
- reference ret(ref, pos);
- return ret;
- }
-
- //! Accesses the full character at the iterator's position.
- pointer operator->() const
- {
- return operator*();
- }
-
- //! Accesses the full character at the iterator's position.
- pointer operator->()
- {
- return operator*();
- }
- };
-
- typedef typename ustring16<TAlloc>::_ustring16_iterator iterator;
- typedef typename ustring16<TAlloc>::_ustring16_const_iterator const_iterator;
-
- ///----------------------///
- /// end iterator classes ///
- ///----------------------///
-
- //! Default constructor
- ustring16()
- : array(0), allocated(1), used(0)
- {
-#if __BYTE_ORDER == __BIG_ENDIAN
- encoding = unicode::EUTFE_UTF16_BE;
-#else
- encoding = unicode::EUTFE_UTF16_LE;
-#endif
- array = allocator.allocate(1); // new u16[1];
- array[0] = 0x0;
- }
-
-
- //! Constructor
- ustring16(const ustring16<TAlloc>& other)
- : array(0), allocated(0), used(0)
- {
-#if __BYTE_ORDER == __BIG_ENDIAN
- encoding = unicode::EUTFE_UTF16_BE;
-#else
- encoding = unicode::EUTFE_UTF16_LE;
-#endif
- *this = other;
- }
-
-
- //! Constructor from other string types
- template <class B, class A>
- ustring16(const string<B, A>& other)
- : array(0), allocated(0), used(0)
- {
-#if __BYTE_ORDER == __BIG_ENDIAN
- encoding = unicode::EUTFE_UTF16_BE;
-#else
- encoding = unicode::EUTFE_UTF16_LE;
-#endif
- *this = other;
- }
-
-
-#ifndef USTRING_NO_STL
- //! Constructor from std::string
- template <class B, class A, typename Alloc>
- ustring16(const std::basic_string<B, A, Alloc>& other)
- : array(0), allocated(0), used(0)
- {
-#if __BYTE_ORDER == __BIG_ENDIAN
- encoding = unicode::EUTFE_UTF16_BE;
-#else
- encoding = unicode::EUTFE_UTF16_LE;
-#endif
- *this = other.c_str();
- }
-
-
- //! Constructor from iterator.
- template <typename Itr>
- ustring16(Itr first, Itr last)
- : array(0), allocated(0), used(0)
- {
-#if __BYTE_ORDER == __BIG_ENDIAN
- encoding = unicode::EUTFE_UTF16_BE;
-#else
- encoding = unicode::EUTFE_UTF16_LE;
-#endif
- reserve(std::distance(first, last));
- array[used] = 0;
-
- for (; first != last; ++first)
- append((uchar32_t)*first);
- }
-#endif
-
-
-#ifndef USTRING_CPP0X_NEWLITERALS
- //! Constructor for copying a character string from a pointer.
- ustring16(const char* const c)
- : array(0), allocated(0), used(0)
- {
-#if __BYTE_ORDER == __BIG_ENDIAN
- encoding = unicode::EUTFE_UTF16_BE;
-#else
- encoding = unicode::EUTFE_UTF16_LE;
-#endif
-
- loadDataStream(c, strlen(c));
- //append((uchar8_t*)c);
- }
-
-
- //! Constructor for copying a character string from a pointer with a given length.
- ustring16(const char* const c, u32 length)
- : array(0), allocated(0), used(0)
- {
-#if __BYTE_ORDER == __BIG_ENDIAN
- encoding = unicode::EUTFE_UTF16_BE;
-#else
- encoding = unicode::EUTFE_UTF16_LE;
-#endif
-
- loadDataStream(c, length);
- }
-#endif
-
-
- //! Constructor for copying a UTF-8 string from a pointer.
- ustring16(const uchar8_t* const c)
- : array(0), allocated(0), used(0)
- {
-#if __BYTE_ORDER == __BIG_ENDIAN
- encoding = unicode::EUTFE_UTF16_BE;
-#else
- encoding = unicode::EUTFE_UTF16_LE;
-#endif
-
- append(c);
- }
-
-
- //! Constructor for copying a UTF-8 string from a single char.
- ustring16(const char c)
- : array(0), allocated(0), used(0)
- {
-#if __BYTE_ORDER == __BIG_ENDIAN
- encoding = unicode::EUTFE_UTF16_BE;
-#else
- encoding = unicode::EUTFE_UTF16_LE;
-#endif
-
- append((uchar32_t)c);
- }
-
-
- //! Constructor for copying a UTF-8 string from a pointer with a given length.
- ustring16(const uchar8_t* const c, u32 length)
- : array(0), allocated(0), used(0)
- {
-#if __BYTE_ORDER == __BIG_ENDIAN
- encoding = unicode::EUTFE_UTF16_BE;
-#else
- encoding = unicode::EUTFE_UTF16_LE;
-#endif
-
- append(c, length);
- }
-
-
- //! Constructor for copying a UTF-16 string from a pointer.
- ustring16(const uchar16_t* const c)
- : array(0), allocated(0), used(0)
- {
-#if __BYTE_ORDER == __BIG_ENDIAN
- encoding = unicode::EUTFE_UTF16_BE;
-#else
- encoding = unicode::EUTFE_UTF16_LE;
-#endif
-
- append(c);
- }
-
-
- //! Constructor for copying a UTF-16 string from a pointer with a given length
- ustring16(const uchar16_t* const c, u32 length)
- : array(0), allocated(0), used(0)
- {
-#if __BYTE_ORDER == __BIG_ENDIAN
- encoding = unicode::EUTFE_UTF16_BE;
-#else
- encoding = unicode::EUTFE_UTF16_LE;
-#endif
-
- append(c, length);
- }
-
-
- //! Constructor for copying a UTF-32 string from a pointer.
- ustring16(const uchar32_t* const c)
- : array(0), allocated(0), used(0)
- {
-#if __BYTE_ORDER == __BIG_ENDIAN
- encoding = unicode::EUTFE_UTF16_BE;
-#else
- encoding = unicode::EUTFE_UTF16_LE;
-#endif
-
- append(c);
- }
-
-
- //! Constructor for copying a UTF-32 from a pointer with a given length.
- ustring16(const uchar32_t* const c, u32 length)
- : array(0), allocated(0), used(0)
- {
-#if __BYTE_ORDER == __BIG_ENDIAN
- encoding = unicode::EUTFE_UTF16_BE;
-#else
- encoding = unicode::EUTFE_UTF16_LE;
-#endif
-
- append(c, length);
- }
-
-
- //! Constructor for copying a wchar_t string from a pointer.
- ustring16(const wchar_t* const c)
- : array(0), allocated(0), used(0)
- {
-#if __BYTE_ORDER == __BIG_ENDIAN
- encoding = unicode::EUTFE_UTF16_BE;
-#else
- encoding = unicode::EUTFE_UTF16_LE;
-#endif
-
- if (sizeof(wchar_t) == 4)
- append(reinterpret_cast<const uchar32_t* const>(c));
- else if (sizeof(wchar_t) == 2)
- append(reinterpret_cast<const uchar16_t* const>(c));
- else if (sizeof(wchar_t) == 1)
- append(reinterpret_cast<const uchar8_t* const>(c));
- }
-
-
- //! Constructor for copying a wchar_t string from a pointer with a given length.
- ustring16(const wchar_t* const c, u32 length)
- : array(0), allocated(0), used(0)
- {
-#if __BYTE_ORDER == __BIG_ENDIAN
- encoding = unicode::EUTFE_UTF16_BE;
-#else
- encoding = unicode::EUTFE_UTF16_LE;
-#endif
-
- if (sizeof(wchar_t) == 4)
- append(reinterpret_cast<const uchar32_t* const>(c), length);
- else if (sizeof(wchar_t) == 2)
- append(reinterpret_cast<const uchar16_t* const>(c), length);
- else if (sizeof(wchar_t) == 1)
- append(reinterpret_cast<const uchar8_t* const>(c), length);
- }
-
-
-#ifdef USTRING_CPP0X
- //! Constructor for moving a ustring16
- ustring16(ustring16<TAlloc>&& other)
- : array(other.array), encoding(other.encoding), allocated(other.allocated), used(other.used)
- {
- //std::cout << "MOVE constructor" << std::endl;
- other.array = 0;
- other.allocated = 0;
- other.used = 0;
- }
-#endif
-
-
- //! Destructor
- ~ustring16()
- {
- allocator.deallocate(array); // delete [] array;
- }
-
-
- //! Assignment operator
- ustring16& operator=(const ustring16<TAlloc>& other)
- {
- if (this == &other)
- return *this;
-
- used = other.size_raw();
- if (used >= allocated)
- {
- allocator.deallocate(array); // delete [] array;
- allocated = used + 1;
- array = allocator.allocate(used + 1); //new u16[used];
- }
-
- const uchar16_t* p = other.c_str();
- for (u32 i=0; i<=used; ++i, ++p)
- array[i] = *p;
-
- array[used] = 0;
-
- // Validate our new UTF-16 string.
- validate();
-
- return *this;
- }
-
-
-#ifdef USTRING_CPP0X
- //! Move assignment operator
- ustring16& operator=(ustring16<TAlloc>&& other)
- {
- if (this != &other)
- {
- //std::cout << "MOVE operator=" << std::endl;
- allocator.deallocate(array);
-
- array = other.array;
- allocated = other.allocated;
- encoding = other.encoding;
- used = other.used;
- other.array = 0;
- other.used = 0;
- }
- return *this;
- }
-#endif
-
-
- //! Assignment operator for other string types
- template <class B, class A>
- ustring16<TAlloc>& operator=(const string<B, A>& other)
- {
- *this = other.c_str();
- return *this;
- }
-
-
- //! Assignment operator for UTF-8 strings
- ustring16<TAlloc>& operator=(const uchar8_t* const c)
- {
- if (!array)
- {
- array = allocator.allocate(1); //new u16[1];
- allocated = 1;
- }
- used = 0;
- array[used] = 0x0;
- if (!c) return *this;
-
- //! Append our string now.
- append(c);
- return *this;
- }
-
-
- //! Assignment operator for UTF-16 strings
- ustring16<TAlloc>& operator=(const uchar16_t* const c)
- {
- if (!array)
- {
- array = allocator.allocate(1); //new u16[1];
- allocated = 1;
- }
- used = 0;
- array[used] = 0x0;
- if (!c) return *this;
-
- //! Append our string now.
- append(c);
- return *this;
- }
-
-
- //! Assignment operator for UTF-32 strings
- ustring16<TAlloc>& operator=(const uchar32_t* const c)
- {
- if (!array)
- {
- array = allocator.allocate(1); //new u16[1];
- allocated = 1;
- }
- used = 0;
- array[used] = 0x0;
- if (!c) return *this;
-
- //! Append our string now.
- append(c);
- return *this;
- }
-
-
- //! Assignment operator for wchar_t strings.
- /** Note that this assumes that a correct unicode string is stored in the wchar_t string.
- Since wchar_t changes depending on its platform, it could either be a UTF-8, -16, or -32 string.
- This function assumes you are storing the correct unicode encoding inside the wchar_t string. **/
- ustring16<TAlloc>& operator=(const wchar_t* const c)
- {
- if (sizeof(wchar_t) == 4)
- *this = reinterpret_cast<const uchar32_t* const>(c);
- else if (sizeof(wchar_t) == 2)
- *this = reinterpret_cast<const uchar16_t* const>(c);
- else if (sizeof(wchar_t) == 1)
- *this = reinterpret_cast<const uchar8_t* const>(c);
-
- return *this;
- }
-
-
- //! Assignment operator for other strings.
- /** Note that this assumes that a correct unicode string is stored in the string. **/
- template <class B>
- ustring16<TAlloc>& operator=(const B* const c)
- {
- if (sizeof(B) == 4)
- *this = reinterpret_cast<const uchar32_t* const>(c);
- else if (sizeof(B) == 2)
- *this = reinterpret_cast<const uchar16_t* const>(c);
- else if (sizeof(B) == 1)
- *this = reinterpret_cast<const uchar8_t* const>(c);
-
- return *this;
- }
-
-
- //! Direct access operator
- access operator [](const u32 index)
- {
- _IRR_DEBUG_BREAK_IF(index>=size()) // bad index
- iterator iter(*this, index);
- return iter.operator*();
- }
-
-
- //! Direct access operator
- const access operator [](const u32 index) const
- {
- _IRR_DEBUG_BREAK_IF(index>=size()) // bad index
- const_iterator iter(*this, index);
- return iter.operator*();
- }
-
-
- //! Equality operator
- bool operator ==(const uchar16_t* const str) const
- {
- if (!str)
- return false;
-
- u32 i;
- for(i=0; array[i] && str[i]; ++i)
- if (array[i] != str[i])
- return false;
-
- return !array[i] && !str[i];
- }
-
-
- //! Equality operator
- bool operator ==(const ustring16<TAlloc>& other) const
- {
- for(u32 i=0; array[i] && other.array[i]; ++i)
- if (array[i] != other.array[i])
- return false;
-
- return used == other.used;
- }
-
-
- //! Is smaller comparator
- bool operator <(const ustring16<TAlloc>& other) const
- {
- for(u32 i=0; array[i] && other.array[i]; ++i)
- {
- s32 diff = array[i] - other.array[i];
- if ( diff )
- return diff < 0;
- }
-
- return used < other.used;
- }
-
-
- //! Inequality operator
- bool operator !=(const uchar16_t* const str) const
- {
- return !(*this == str);
- }
-
-
- //! Inequality operator
- bool operator !=(const ustring16<TAlloc>& other) const
- {
- return !(*this == other);
- }
-
-
- //! Returns the length of a ustring16 in full characters.
- //! \return Length of a ustring16 in full characters.
- u32 size() const
- {
- const_iterator i(*this, 0);
- u32 pos = 0;
- while (!i.atEnd())
- {
- ++i;
- ++pos;
- }
- return pos;
- }
-
-
- //! Informs if the ustring is empty or not.
- //! \return True if the ustring is empty, false if not.
- bool empty() const
- {
- return (size_raw() == 0);
- }
-
-
- //! Returns a pointer to the raw UTF-16 string data.
- //! \return pointer to C-style NUL terminated array of UTF-16 code points.
- const uchar16_t* c_str() const
- {
- return array;
- }
-
-
- //! Compares the first n characters of this string with another.
- //! \param other Other string to compare to.
- //! \param n Number of characters to compare.
- //! \return True if the n first characters of both strings are equal.
- bool equalsn(const ustring16<TAlloc>& other, u32 n) const
- {
- u32 i;
- const uchar16_t* oa = other.c_str();
- for(i=0; i < n && array[i] && oa[i]; ++i)
- if (array[i] != oa[i])
- return false;
-
- // if one (or both) of the strings was smaller then they
- // are only equal if they have the same length
- return (i == n) || (used == other.used);
- }
-
-
- //! Compares the first n characters of this string with another.
- //! \param str Other string to compare to.
- //! \param n Number of characters to compare.
- //! \return True if the n first characters of both strings are equal.
- bool equalsn(const uchar16_t* const str, u32 n) const
- {
- if (!str)
- return false;
- u32 i;
- for(i=0; i < n && array[i] && str[i]; ++i)
- if (array[i] != str[i])
- return false;
-
- // if one (or both) of the strings was smaller then they
- // are only equal if they have the same length
- return (i == n) || (array[i] == 0 && str[i] == 0);
- }
-
-
- //! Appends a character to this ustring16
- //! \param character The character to append.
- //! \return A reference to our current string.
- ustring16<TAlloc>& append(uchar32_t character)
- {
- if (used + 2 >= allocated)
- reallocate(used + 2);
-
- if (character > 0xFFFF)
- {
- used += 2;
-
- // character will be multibyte, so split it up into a surrogate pair.
- uchar16_t x = static_cast<uchar16_t>(character);
- uchar16_t vh = UTF16_HI_SURROGATE | ((((character >> 16) & ((1 << 5) - 1)) - 1) << 6) | (x >> 10);
- uchar16_t vl = UTF16_LO_SURROGATE | (x & ((1 << 10) - 1));
- array[used-2] = vh;
- array[used-1] = vl;
- }
- else
- {
- ++used;
- array[used-1] = character;
- }
- array[used] = 0;
-
- return *this;
- }
-
-
- //! Appends a UTF-8 string to this ustring16
- //! \param other The UTF-8 string to append.
- //! \param length The length of the string to append.
- //! \return A reference to our current string.
- ustring16<TAlloc>& append(const uchar8_t* const other, u32 length=0xffffffff)
- {
- if (!other)
- return *this;
-
- // Determine if the string is long enough for a BOM.
- u32 len = 0;
- const uchar8_t* p = other;
- do
- {
- ++len;
- } while (*p++ && len < unicode::BOM_ENCODE_UTF8_LEN);
-
- // Check for BOM.
- unicode::EUTF_ENCODE c_bom = unicode::EUTFE_NONE;
- if (len == unicode::BOM_ENCODE_UTF8_LEN)
- {
- if (memcmp(other, unicode::BOM_ENCODE_UTF8, unicode::BOM_ENCODE_UTF8_LEN) == 0)
- c_bom = unicode::EUTFE_UTF8;
- }
-
- // If a BOM was found, don't include it in the string.
- const uchar8_t* c2 = other;
- if (c_bom != unicode::EUTFE_NONE)
- {
- c2 = other + unicode::BOM_UTF8_LEN;
- length -= unicode::BOM_UTF8_LEN;
- }
-
- // Calculate the size of the string to read in.
- len = 0;
- p = c2;
- do
- {
- ++len;
- } while(*p++ && len < length);
- if (len > length)
- len = length;
-
- // If we need to grow the array, do it now.
- if (used + len >= allocated)
- reallocate(used + (len * 2));
- u32 start = used;
-
- // Convert UTF-8 to UTF-16.
- u32 pos = start;
- for (u32 l = 0; l<len;)
- {
- ++used;
- if (((c2[l] >> 6) & 0x03) == 0x02)
- { // Invalid continuation byte.
- array[pos++] = unicode::UTF_REPLACEMENT_CHARACTER;
- ++l;
- }
- else if (c2[l] == 0xC0 || c2[l] == 0xC1)
- { // Invalid byte - overlong encoding.
- array[pos++] = unicode::UTF_REPLACEMENT_CHARACTER;
- ++l;
- }
- else if ((c2[l] & 0xF8) == 0xF0)
- { // 4 bytes UTF-8, 2 bytes UTF-16.
- // Check for a full string.
- if ((l + 3) >= len)
- {
- array[pos++] = unicode::UTF_REPLACEMENT_CHARACTER;
- l += 3;
- break;
- }
-
- // Validate.
- bool valid = true;
- u8 l2 = 0;
- if (valid && (((c2[l+1] >> 6) & 0x03) == 0x02)) ++l2; else valid = false;
- if (valid && (((c2[l+2] >> 6) & 0x03) == 0x02)) ++l2; else valid = false;
- if (valid && (((c2[l+3] >> 6) & 0x03) == 0x02)) ++l2; else valid = false;
- if (!valid)
- {
- array[pos++] = unicode::UTF_REPLACEMENT_CHARACTER;
- l += l2;
- continue;
- }
-
- // Decode.
- uchar8_t b1 = ((c2[l] & 0x7) << 2) | ((c2[l+1] >> 4) & 0x3);
- uchar8_t b2 = ((c2[l+1] & 0xF) << 4) | ((c2[l+2] >> 2) & 0xF);
- uchar8_t b3 = ((c2[l+2] & 0x3) << 6) | (c2[l+3] & 0x3F);
- uchar32_t v = b3 | ((uchar32_t)b2 << 8) | ((uchar32_t)b1 << 16);
-
- // Split v up into a surrogate pair.
- uchar16_t x = static_cast<uchar16_t>(v);
- uchar16_t vh = UTF16_HI_SURROGATE | ((((v >> 16) & ((1 << 5) - 1)) - 1) << 6) | (x >> 10);
- uchar16_t vl = UTF16_LO_SURROGATE | (x & ((1 << 10) - 1));
-
- array[pos++] = vh;
- array[pos++] = vl;
- l += 4;
- ++used; // Using two shorts this time, so increase used by 1.
- }
- else if ((c2[l] & 0xF0) == 0xE0)
- { // 3 bytes UTF-8, 1 byte UTF-16.
- // Check for a full string.
- if ((l + 2) >= len)
- {
- array[pos++] = unicode::UTF_REPLACEMENT_CHARACTER;
- l += 2;
- break;
- }
-
- // Validate.
- bool valid = true;
- u8 l2 = 0;
- if (valid && (((c2[l+1] >> 6) & 0x03) == 0x02)) ++l2; else valid = false;
- if (valid && (((c2[l+2] >> 6) & 0x03) == 0x02)) ++l2; else valid = false;
- if (!valid)
- {
- array[pos++] = unicode::UTF_REPLACEMENT_CHARACTER;
- l += l2;
- continue;
- }
-
- // Decode.
- uchar8_t b1 = ((c2[l] & 0xF) << 4) | ((c2[l+1] >> 2) & 0xF);
- uchar8_t b2 = ((c2[l+1] & 0x3) << 6) | (c2[l+2] & 0x3F);
- uchar16_t ch = b2 | ((uchar16_t)b1 << 8);
- array[pos++] = ch;
- l += 3;
- }
- else if ((c2[l] & 0xE0) == 0xC0)
- { // 2 bytes UTF-8, 1 byte UTF-16.
- // Check for a full string.
- if ((l + 1) >= len)
- {
- array[pos++] = unicode::UTF_REPLACEMENT_CHARACTER;
- l += 1;
- break;
- }
-
- // Validate.
- if (((c2[l+1] >> 6) & 0x03) != 0x02)
- {
- array[pos++] = unicode::UTF_REPLACEMENT_CHARACTER;
- ++l;
- continue;
- }
-
- // Decode.
- uchar8_t b1 = (c2[l] >> 2) & 0x7;
- uchar8_t b2 = ((c2[l] & 0x3) << 6) | (c2[l+1] & 0x3F);
- uchar16_t ch = b2 | ((uchar16_t)b1 << 8);
- array[pos++] = ch;
- l += 2;
- }
- else
- { // 1 byte UTF-8, 1 byte UTF-16.
- // Validate.
- if (c2[l] > 0x7F)
- { // Values above 0xF4 are restricted and aren't used. By now, anything above 0x7F is invalid.
- array[pos++] = unicode::UTF_REPLACEMENT_CHARACTER;
- }
- else array[pos++] = static_cast<uchar16_t>(c2[l]);
- ++l;
- }
- }
- array[used] = 0;
-
- // Validate our new UTF-16 string.
- validate();
-
- return *this;
- }
-
-
- //! Appends a UTF-16 string to this ustring16
- //! \param other The UTF-16 string to append.
- //! \param length The length of the string to append.
- //! \return A reference to our current string.
- ustring16<TAlloc>& append(const uchar16_t* const other, u32 length=0xffffffff)
- {
- if (!other)
- return *this;
-
- // Determine if the string is long enough for a BOM.
- u32 len = 0;
- const uchar16_t* p = other;
- do
- {
- ++len;
- } while (*p++ && len < unicode::BOM_ENCODE_UTF16_LEN);
-
- // Check for the BOM to determine the string's endianness.
- unicode::EUTF_ENDIAN c_end = unicode::EUTFEE_NATIVE;
- if (memcmp(other, unicode::BOM_ENCODE_UTF16_LE, unicode::BOM_ENCODE_UTF16_LEN) == 0)
- c_end = unicode::EUTFEE_LITTLE;
- else if (memcmp(other, unicode::BOM_ENCODE_UTF16_BE, unicode::BOM_ENCODE_UTF16_LEN) == 0)
- c_end = unicode::EUTFEE_BIG;
-
- // If a BOM was found, don't include it in the string.
- const uchar16_t* c2 = other;
- if (c_end != unicode::EUTFEE_NATIVE)
- {
- c2 = other + unicode::BOM_UTF16_LEN;
- length -= unicode::BOM_UTF16_LEN;
- }
-
- // Calculate the size of the string to read in.
- len = 0;
- p = c2;
- do
- {
- ++len;
- } while(*p++ && len < length);
- if (len > length)
- len = length;
-
- // If we need to grow the size of the array, do it now.
- if (used + len >= allocated)
- reallocate(used + (len * 2));
- u32 start = used;
- used += len;
-
- // Copy the string now.
- unicode::EUTF_ENDIAN m_end = getEndianness();
- for (u32 l = start; l < start + len; ++l)
- {
- array[l] = (uchar16_t)c2[l];
- if (c_end != unicode::EUTFEE_NATIVE && c_end != m_end)
- array[l] = unicode::swapEndian16(array[l]);
- }
-
- array[used] = 0;
-
- // Validate our new UTF-16 string.
- validate();
- return *this;
- }
-
-
- //! Appends a UTF-32 string to this ustring16
- //! \param other The UTF-32 string to append.
- //! \param length The length of the string to append.
- //! \return A reference to our current string.
- ustring16<TAlloc>& append(const uchar32_t* const other, u32 length=0xffffffff)
- {
- if (!other)
- return *this;
-
- // Check for the BOM to determine the string's endianness.
- unicode::EUTF_ENDIAN c_end = unicode::EUTFEE_NATIVE;
- if (memcmp(other, unicode::BOM_ENCODE_UTF32_LE, unicode::BOM_ENCODE_UTF32_LEN) == 0)
- c_end = unicode::EUTFEE_LITTLE;
- else if (memcmp(other, unicode::BOM_ENCODE_UTF32_BE, unicode::BOM_ENCODE_UTF32_LEN) == 0)
- c_end = unicode::EUTFEE_BIG;
-
- // If a BOM was found, don't include it in the string.
- const uchar32_t* c2 = other;
- if (c_end != unicode::EUTFEE_NATIVE)
- {
- c2 = other + unicode::BOM_UTF32_LEN;
- length -= unicode::BOM_UTF32_LEN;
- }
-
- // Calculate the size of the string to read in.
- u32 len = 0;
- const uchar32_t* p = c2;
- do
- {
- ++len;
- } while(*p++ && len < length);
- if (len > length)
- len = length;
-
- // If we need to grow the size of the array, do it now.
- // In case all of the UTF-32 string is split into surrogate pairs, do len * 2.
- if (used + (len * 2) >= allocated)
- reallocate(used + ((len * 2) * 2));
- u32 start = used;
-
- // Convert UTF-32 to UTF-16.
- unicode::EUTF_ENDIAN m_end = getEndianness();
- u32 pos = start;
- for (u32 l = 0; l<len; ++l)
- {
- ++used;
-
- uchar32_t ch = c2[l];
- if (c_end != unicode::EUTFEE_NATIVE && c_end != m_end)
- ch = unicode::swapEndian32(ch);
-
- if (ch > 0xFFFF)
- {
- // Split ch up into a surrogate pair as it is over 16 bits long.
- uchar16_t x = static_cast<uchar16_t>(ch);
- uchar16_t vh = UTF16_HI_SURROGATE | ((((ch >> 16) & ((1 << 5) - 1)) - 1) << 6) | (x >> 10);
- uchar16_t vl = UTF16_LO_SURROGATE | (x & ((1 << 10) - 1));
- array[pos++] = vh;
- array[pos++] = vl;
- ++used; // Using two shorts, so increased used again.
- }
- else if (ch >= 0xD800 && ch <= 0xDFFF)
- {
- // Between possible UTF-16 surrogates (invalid!)
- array[pos++] = unicode::UTF_REPLACEMENT_CHARACTER;
- }
- else array[pos++] = static_cast<uchar16_t>(ch);
- }
- array[used] = 0;
-
- // Validate our new UTF-16 string.
- validate();
-
- return *this;
- }
-
-
- //! Appends a ustring16 to this ustring16
- //! \param other The string to append to this one.
- //! \return A reference to our current string.
- ustring16<TAlloc>& append(const ustring16<TAlloc>& other)
- {
- const uchar16_t* oa = other.c_str();
-
- u32 len = other.size_raw();
-
- if (used + len >= allocated)
- reallocate(used + len);
-
- for (u32 l=0; l<len; ++l)
- array[used+l] = oa[l];
-
- used += len;
- array[used] = 0;
-
- return *this;
- }
-
-
- //! Appends a certain amount of characters of a ustring16 to this ustring16.
- //! \param other The string to append to this one.
- //! \param length How many characters of the other string to add to this one.
- //! \return A reference to our current string.
- ustring16<TAlloc>& append(const ustring16<TAlloc>& other, u32 length)
- {
- if (other.size() == 0)
- return *this;
-
- if (other.size() < length)
- {
- append(other);
- return *this;
- }
-
- if (used + length * 2 >= allocated)
- reallocate(used + length * 2);
-
- const_iterator iter(other, 0);
- u32 l = length;
- while (!iter.atEnd() && l)
- {
- uchar32_t c = *iter;
- append(c);
- ++iter;
- --l;
- }
-
- return *this;
- }
-
-
- //! Reserves some memory.
- //! \param count The amount of characters to reserve.
- void reserve(u32 count)
- {
- if (count < allocated)
- return;
-
- reallocate(count);
- }
-
-
- //! Finds first occurrence of character.
- //! \param c The character to search for.
- //! \return Position where the character has been found, or -1 if not found.
- s32 findFirst(uchar32_t c) const
- {
- const_iterator i(*this, 0);
-
- s32 pos = 0;
- while (!i.atEnd())
- {
- uchar32_t t = *i;
- if (c == t)
- return pos;
- ++pos;
- ++i;
- }
-
- return -1;
- }
-
- //! Finds first occurrence of a character of a list.
- //! \param c A list of characters to find. For example if the method should find the first occurrence of 'a' or 'b', this parameter should be "ab".
- //! \param count The amount of characters in the list. Usually, this should be strlen(c).
- //! \return Position where one of the characters has been found, or -1 if not found.
- s32 findFirstChar(const uchar32_t* const c, u32 count=1) const
- {
- if (!c || !count)
- return -1;
-
- const_iterator i(*this, 0);
-
- s32 pos = 0;
- while (!i.atEnd())
- {
- uchar32_t t = *i;
- for (u32 j=0; j<count; ++j)
- if (t == c[j])
- return pos;
- ++pos;
- ++i;
- }
-
- return -1;
- }
-
-
- //! Finds first position of a character not in a given list.
- //! \param c A list of characters to NOT find. For example if the method should find the first occurrence of a character not 'a' or 'b', this parameter should be "ab".
- //! \param count The amount of characters in the list. Usually, this should be strlen(c).
- //! \return Position where the character has been found, or -1 if not found.
- s32 findFirstCharNotInList(const uchar32_t* const c, u32 count=1) const
- {
- if (!c || !count)
- return -1;
-
- const_iterator i(*this, 0);
-
- s32 pos = 0;
- while (!i.atEnd())
- {
- uchar32_t t = *i;
- u32 j;
- for (j=0; j<count; ++j)
- if (t == c[j])
- break;
-
- if (j==count)
- return pos;
- ++pos;
- ++i;
- }
-
- return -1;
- }
-
- //! Finds last position of a character not in a given list.
- //! \param c A list of characters to NOT find. For example if the method should find the first occurrence of a character not 'a' or 'b', this parameter should be "ab".
- //! \param count The amount of characters in the list. Usually, this should be strlen(c).
- //! \return Position where the character has been found, or -1 if not found.
- s32 findLastCharNotInList(const uchar32_t* const c, u32 count=1) const
- {
- if (!c || !count)
- return -1;
-
- const_iterator i(end());
- --i;
-
- s32 pos = size() - 1;
- while (!i.atStart())
- {
- uchar32_t t = *i;
- u32 j;
- for (j=0; j<count; ++j)
- if (t == c[j])
- break;
-
- if (j==count)
- return pos;
- --pos;
- --i;
- }
-
- return -1;
- }
-
- //! Finds next occurrence of character.
- //! \param c The character to search for.
- //! \param startPos The position in the string to start searching.
- //! \return Position where the character has been found, or -1 if not found.
- s32 findNext(uchar32_t c, u32 startPos) const
- {
- const_iterator i(*this, startPos);
-
- s32 pos = startPos;
- while (!i.atEnd())
- {
- uchar32_t t = *i;
- if (t == c)
- return pos;
- ++pos;
- ++i;
- }
-
- return -1;
- }
-
-
- //! Finds last occurrence of character.
- //! \param c The character to search for.
- //! \param start The start position of the reverse search ( default = -1, on end ).
- //! \return Position where the character has been found, or -1 if not found.
- s32 findLast(uchar32_t c, s32 start = -1) const
- {
- u32 s = size();
- start = core::clamp ( start < 0 ? (s32)s : start, 0, (s32)s ) - 1;
-
- const_iterator i(*this, start);
- u32 pos = start;
- while (!i.atStart())
- {
- uchar32_t t = *i;
- if (t == c)
- return pos;
- --pos;
- --i;
- }
-
- return -1;
- }
-
- //! Finds last occurrence of a character in a list.
- //! \param c A list of strings to find. For example if the method should find the last occurrence of 'a' or 'b', this parameter should be "ab".
- //! \param count The amount of characters in the list. Usually, this should be strlen(c).
- //! \return Position where one of the characters has been found, or -1 if not found.
- s32 findLastChar(const uchar32_t* const c, u32 count=1) const
- {
- if (!c || !count)
- return -1;
-
- const_iterator i(end());
- --i;
-
- s32 pos = size();
- while (!i.atStart())
- {
- uchar32_t t = *i;
- for (u32 j=0; j<count; ++j)
- if (t == c[j])
- return pos;
- --pos;
- --i;
- }
-
- return -1;
- }
-
-
- //! Finds another ustring16 in this ustring16.
- //! \param str The string to find.
- //! \param start The start position of the search.
- //! \return Positions where the ustring16 has been found, or -1 if not found.
- s32 find(const ustring16<TAlloc>& str, const u32 start = 0) const
- {
- u32 my_size = size();
- u32 their_size = str.size();
-
- if (their_size == 0 || my_size - start < their_size)
- return -1;
-
- const_iterator i(*this, start);
-
- s32 pos = start;
- while (!i.atEnd())
- {
- const_iterator i2(i);
- const_iterator j(str, 0);
- uchar32_t t1 = (uchar32_t)*i2;
- uchar32_t t2 = (uchar32_t)*j;
- while (t1 == t2)
- {
- ++i2;
- ++j;
- if (j.atEnd())
- return pos;
- t1 = (uchar32_t)*i2;
- t2 = (uchar32_t)*j;
- }
- ++i;
- ++pos;
- }
-
- return -1;
- }
-
-
- //! Finds another ustring16 in this ustring16.
- //! \param str The string to find.
- //! \param start The start position of the search.
- //! \return Positions where the string has been found, or -1 if not found.
- s32 find_raw(const ustring16<TAlloc>& str, const u32 start = 0) const
- {
- const uchar16_t* data = str.c_str();
- if (data && *data)
- {
- u32 len = 0;
-
- while (data[len])
- ++len;
-
- if (len > used)
- return -1;
-
- for (u32 i=start; i<=used-len; ++i)
- {
- u32 j=0;
-
- while(data[j] && array[i+j] == data[j])
- ++j;
-
- if (!data[j])
- return i;
- }
- }
-
- return -1;
- }
-
-
- //! Returns a substring.
- //! \param begin: Start of substring.
- //! \param length: Length of substring.
- //! \return A reference to our current string.
- ustring16<TAlloc> subString(u32 begin, s32 length) const
- {
- u32 len = size();
- // if start after ustring16
- // or no proper substring length
- if ((length <= 0) || (begin>=len))
- return ustring16<TAlloc>("");
- // clamp length to maximal value
- if ((length+begin) > len)
- length = len-begin;
-
- ustring16<TAlloc> o;
- o.reserve((length+1) * 2);
-
- const_iterator i(*this, begin);
- while (!i.atEnd() && length)
- {
- o.append(*i);
- ++i;
- --length;
- }
-
- return o;
- }
-
-
- //! Appends a character to this ustring16.
- //! \param c Character to append.
- //! \return A reference to our current string.
- ustring16<TAlloc>& operator += (char c)
- {
- append((uchar32_t)c);
- return *this;
- }
-
-
- //! Appends a character to this ustring16.
- //! \param c Character to append.
- //! \return A reference to our current string.
- ustring16<TAlloc>& operator += (uchar32_t c)
- {
- append(c);
- return *this;
- }
-
-
- //! Appends a number to this ustring16.
- //! \param c Number to append.
- //! \return A reference to our current string.
- ustring16<TAlloc>& operator += (short c)
- {
- append(core::stringc(c));
- return *this;
- }
-
-
- //! Appends a number to this ustring16.
- //! \param c Number to append.
- //! \return A reference to our current string.
- ustring16<TAlloc>& operator += (unsigned short c)
- {
- append(core::stringc(c));
- return *this;
- }
-
-
-#ifdef USTRING_CPP0X_NEWLITERALS
- //! Appends a number to this ustring16.
- //! \param c Number to append.
- //! \return A reference to our current string.
- ustring16<TAlloc>& operator += (int c)
- {
- append(core::stringc(c));
- return *this;
- }
-
-
- //! Appends a number to this ustring16.
- //! \param c Number to append.
- //! \return A reference to our current string.
- ustring16<TAlloc>& operator += (unsigned int c)
- {
- append(core::stringc(c));
- return *this;
- }
-#endif
-
-
- //! Appends a number to this ustring16.
- //! \param c Number to append.
- //! \return A reference to our current string.
- ustring16<TAlloc>& operator += (long c)
- {
- append(core::stringc(c));
- return *this;
- }
-
-
- //! Appends a number to this ustring16.
- //! \param c Number to append.
- //! \return A reference to our current string.
- ustring16<TAlloc>& operator += (unsigned long c)
- {
- append(core::stringc(c));
- return *this;
- }
-
-
- //! Appends a number to this ustring16.
- //! \param c Number to append.
- //! \return A reference to our current string.
- ustring16<TAlloc>& operator += (double c)
- {
- append(core::stringc(c));
- return *this;
- }
-
-
- //! Appends a char ustring16 to this ustring16.
- //! \param c Char ustring16 to append.
- //! \return A reference to our current string.
- ustring16<TAlloc>& operator += (const uchar16_t* const c)
- {
- append(c);
- return *this;
- }
-
-
- //! Appends a ustring16 to this ustring16.
- //! \param other ustring16 to append.
- //! \return A reference to our current string.
- ustring16<TAlloc>& operator += (const ustring16<TAlloc>& other)
- {
- append(other);
- return *this;
- }
-
-
- //! Replaces all characters of a given type with another one.
- //! \param toReplace Character to replace.
- //! \param replaceWith Character replacing the old one.
- //! \return A reference to our current string.
- ustring16<TAlloc>& replace(uchar32_t toReplace, uchar32_t replaceWith)
- {
- iterator i(*this, 0);
- while (!i.atEnd())
- {
- typename ustring16<TAlloc>::access a = *i;
- if ((uchar32_t)a == toReplace)
- a = replaceWith;
- ++i;
- }
- return *this;
- }
-
-
- //! Replaces all instances of a string with another one.
- //! \param toReplace The string to replace.
- //! \param replaceWith The string replacing the old one.
- //! \return A reference to our current string.
- ustring16<TAlloc>& replace(const ustring16<TAlloc>& toReplace, const ustring16<TAlloc>& replaceWith)
- {
- if (toReplace.size() == 0)
- return *this;
-
- const uchar16_t* other = toReplace.c_str();
- const uchar16_t* replace = replaceWith.c_str();
- const u32 other_size = toReplace.size_raw();
- const u32 replace_size = replaceWith.size_raw();
-
- // Determine the delta. The algorithm will change depending on the delta.
- s32 delta = replace_size - other_size;
-
- // A character for character replace. The string will not shrink or grow.
- if (delta == 0)
- {
- s32 pos = 0;
- while ((pos = find_raw(other, pos)) != -1)
- {
- for (u32 i = 0; i < replace_size; ++i)
- array[pos + i] = replace[i];
- ++pos;
- }
- return *this;
- }
-
- // We are going to be removing some characters. The string will shrink.
- if (delta < 0)
- {
- u32 i = 0;
- for (u32 pos = 0; pos <= used; ++i, ++pos)
- {
- // Is this potentially a match?
- if (array[pos] == *other)
- {
- // Check to see if we have a match.
- u32 j;
- for (j = 0; j < other_size; ++j)
- {
- if (array[pos + j] != other[j])
- break;
- }
-
- // If we have a match, replace characters.
- if (j == other_size)
- {
- for (j = 0; j < replace_size; ++j)
- array[i + j] = replace[j];
- i += replace_size - 1;
- pos += other_size - 1;
- continue;
- }
- }
-
- // No match found, just copy characters.
- array[i - 1] = array[pos];
- }
- array[i] = 0;
- used = i;
-
- return *this;
- }
-
- // We are going to be adding characters, so the string size will increase.
- // Count the number of times toReplace exists in the string so we can allocate the new size.
- u32 find_count = 0;
- s32 pos = 0;
- while ((pos = find_raw(other, pos)) != -1)
- {
- ++find_count;
- ++pos;
- }
-
- // Re-allocate the string now, if needed.
- u32 len = delta * find_count;
- if (used + len >= allocated)
- reallocate(used + len);
-
- // Start replacing.
- pos = 0;
- while ((pos = find_raw(other, pos)) != -1)
- {
- uchar16_t* start = array + pos + other_size - 1;
- uchar16_t* ptr = array + used;
- uchar16_t* end = array + used + delta;
-
- // Shift characters to make room for the string.
- while (ptr != start)
- {
- *end = *ptr;
- --ptr;
- --end;
- }
-
- // Add the new string now.
- for (u32 i = 0; i < replace_size; ++i)
- array[pos + i] = replace[i];
-
- pos += replace_size;
- used += delta;
- }
-
- // Terminate the string and return ourself.
- array[used] = 0;
- return *this;
- }
-
-
- //! Removes characters from a ustring16..
- //! \param c The character to remove.
- //! \return A reference to our current string.
- ustring16<TAlloc>& remove(uchar32_t c)
- {
- u32 pos = 0;
- u32 found = 0;
- u32 len = (c > 0xFFFF ? 2 : 1); // Remove characters equal to the size of c as a UTF-16 character.
- for (u32 i=0; i<=used; ++i)
- {
- uchar32_t uc32 = 0;
- if (!UTF16_IS_SURROGATE_HI(array[i]))
- uc32 |= array[i];
- else if (i + 1 <= used)
- {
- // Convert the surrogate pair into a single UTF-32 character.
- uc32 = unicode::toUTF32(array[i], array[i + 1]);
- }
- u32 len2 = (uc32 > 0xFFFF ? 2 : 1);
-
- if (uc32 == c)
- {
- found += len;
- continue;
- }
-
- array[pos++] = array[i];
- if (len2 == 2)
- array[pos++] = array[++i];
- }
- used -= found;
- array[used] = 0;
- return *this;
- }
-
-
- //! Removes a ustring16 from the ustring16.
- //! \param toRemove The string to remove.
- //! \return A reference to our current string.
- ustring16<TAlloc>& remove(const ustring16<TAlloc>& toRemove)
- {
- u32 size = toRemove.size_raw();
- if (size == 0) return *this;
-
- const uchar16_t* tra = toRemove.c_str();
- u32 pos = 0;
- u32 found = 0;
- for (u32 i=0; i<=used; ++i)
- {
- u32 j = 0;
- while (j < size)
- {
- if (array[i + j] != tra[j])
- break;
- ++j;
- }
- if (j == size)
- {
- found += size;
- i += size - 1;
- continue;
- }
-
- array[pos++] = array[i];
- }
- used -= found;
- array[used] = 0;
- return *this;
- }
-
-
- //! Removes characters from the ustring16.
- //! \param characters The characters to remove.
- //! \return A reference to our current string.
- ustring16<TAlloc>& removeChars(const ustring16<TAlloc>& characters)
- {
- if (characters.size_raw() == 0)
- return *this;
-
- u32 pos = 0;
- u32 found = 0;
- const_iterator iter(characters);
- for (u32 i=0; i<=used; ++i)
- {
- uchar32_t uc32 = 0;
- if (!UTF16_IS_SURROGATE_HI(array[i]))
- uc32 |= array[i];
- else if (i + 1 <= used)
- {
- // Convert the surrogate pair into a single UTF-32 character.
- uc32 = unicode::toUTF32(array[i], array[i+1]);
- }
- u32 len2 = (uc32 > 0xFFFF ? 2 : 1);
-
- bool cont = false;
- iter.toStart();
- while (!iter.atEnd())
- {
- uchar32_t c = *iter;
- if (uc32 == c)
- {
- found += (c > 0xFFFF ? 2 : 1); // Remove characters equal to the size of c as a UTF-16 character.
- ++i;
- cont = true;
- break;
- }
- ++iter;
- }
- if (cont) continue;
-
- array[pos++] = array[i];
- if (len2 == 2)
- array[pos++] = array[++i];
- }
- used -= found;
- array[used] = 0;
- return *this;
- }
-
-
- //! Trims the ustring16.
- //! Removes the specified characters (by default, Latin-1 whitespace) from the begining and the end of the ustring16.
- //! \param whitespace The characters that are to be considered as whitespace.
- //! \return A reference to our current string.
- ustring16<TAlloc>& trim(const ustring16<TAlloc>& whitespace = " \t\n\r")
- {
- core::array<uchar32_t> utf32white = whitespace.toUTF32();
-
- // find start and end of the substring without the specified characters
- const s32 begin = findFirstCharNotInList(utf32white.const_pointer(), whitespace.used + 1);
- if (begin == -1)
- return (*this="");
-
- const s32 end = findLastCharNotInList(utf32white.const_pointer(), whitespace.used + 1);
-
- return (*this = subString(begin, (end +1) - begin));
- }
-
-
- //! Erases a character from the ustring16.
- //! May be slow, because all elements following after the erased element have to be copied.
- //! \param index Index of element to be erased.
- //! \return A reference to our current string.
- ustring16<TAlloc>& erase(u32 index)
- {
- _IRR_DEBUG_BREAK_IF(index>used) // access violation
-
- iterator i(*this, index);
-
- uchar32_t t = *i;
- u32 len = (t > 0xFFFF ? 2 : 1);
-
- for (u32 j = static_cast<u32>(i.getPos()) + len; j <= used; ++j)
- array[j - len] = array[j];
-
- used -= len;
- array[used] = 0;
-
- return *this;
- }
-
-
- //! Validate the existing ustring16, checking for valid surrogate pairs and checking for proper termination.
- //! \return A reference to our current string.
- ustring16<TAlloc>& validate()
- {
- // Validate all unicode characters.
- for (u32 i=0; i<allocated; ++i)
- {
- // Terminate on existing null.
- if (array[i] == 0)
- {
- used = i;
- return *this;
- }
- if (UTF16_IS_SURROGATE(array[i]))
- {
- if (((i+1) >= allocated) || UTF16_IS_SURROGATE_LO(array[i]))
- array[i] = unicode::UTF_REPLACEMENT_CHARACTER;
- else if (UTF16_IS_SURROGATE_HI(array[i]) && !UTF16_IS_SURROGATE_LO(array[i+1]))
- array[i] = unicode::UTF_REPLACEMENT_CHARACTER;
- ++i;
- }
- if (array[i] >= 0xFDD0 && array[i] <= 0xFDEF)
- array[i] = unicode::UTF_REPLACEMENT_CHARACTER;
- }
-
- // terminate
- used = 0;
- if (allocated > 0)
- {
- used = allocated - 1;
- array[used] = 0;
- }
- return *this;
- }
-
-
- //! Gets the last char of the ustring16, or 0.
- //! \return The last char of the ustring16, or 0.
- uchar32_t lastChar() const
- {
- if (used < 1)
- return 0;
-
- if (UTF16_IS_SURROGATE_LO(array[used-1]))
- {
- // Make sure we have a paired surrogate.
- if (used < 2)
- return 0;
-
- // Check for an invalid surrogate.
- if (!UTF16_IS_SURROGATE_HI(array[used-2]))
- return 0;
-
- // Convert the surrogate pair into a single UTF-32 character.
- return unicode::toUTF32(array[used-2], array[used-1]);
- }
- else
- {
- return array[used-1];
- }
- }
-
-
- //! Split the ustring16 into parts.
- /** This method will split a ustring16 at certain delimiter characters
- into the container passed in as reference. The type of the container
- has to be given as template parameter. It must provide a push_back and
- a size method.
- \param ret The result container
- \param c C-style ustring16 of delimiter characters
- \param count Number of delimiter characters
- \param ignoreEmptyTokens Flag to avoid empty substrings in the result
- container. If two delimiters occur without a character in between, an
- empty substring would be placed in the result. If this flag is set,
- only non-empty strings are stored.
- \param keepSeparators Flag which allows to add the separator to the
- result ustring16. If this flag is true, the concatenation of the
- substrings results in the original ustring16. Otherwise, only the
- characters between the delimiters are returned.
- \return The number of resulting substrings
- */
- template<class container>
- u32 split(container& ret, const uchar32_t* const c, u32 count=1, bool ignoreEmptyTokens=true, bool keepSeparators=false) const
- {
- if (!c)
- return 0;
-
- const_iterator i(*this);
- const u32 oldSize=ret.size();
- u32 pos = 0;
- u32 lastpos = 0;
- u32 lastpospos = 0;
- bool lastWasSeparator = false;
- while (!i.atEnd())
- {
- uchar32_t ch = *i;
- bool foundSeparator = false;
- for (u32 j=0; j<count; ++j)
- {
- if (ch == c[j])
- {
- if ((!ignoreEmptyTokens || pos - lastpos != 0) &&
- !lastWasSeparator)
- ret.push_back(ustring16<TAlloc>(&array[lastpospos], pos - lastpos));
- foundSeparator = true;
- lastpos = (keepSeparators ? pos : pos + 1);
- lastpospos = (keepSeparators ? i.getPos() : i.getPos() + 1);
- break;
- }
- }
- lastWasSeparator = foundSeparator;
- ++pos;
- ++i;
- }
- u32 s = size() + 1;
- if (s > lastpos)
- ret.push_back(ustring16<TAlloc>(&array[lastpospos], s - lastpos));
- return ret.size()-oldSize;
- }
-
-
- //! Split the ustring16 into parts.
- /** This method will split a ustring16 at certain delimiter characters
- into the container passed in as reference. The type of the container
- has to be given as template parameter. It must provide a push_back and
- a size method.
- \param ret The result container
- \param c A unicode string of delimiter characters
- \param ignoreEmptyTokens Flag to avoid empty substrings in the result
- container. If two delimiters occur without a character in between, an
- empty substring would be placed in the result. If this flag is set,
- only non-empty strings are stored.
- \param keepSeparators Flag which allows to add the separator to the
- result ustring16. If this flag is true, the concatenation of the
- substrings results in the original ustring16. Otherwise, only the
- characters between the delimiters are returned.
- \return The number of resulting substrings
- */
- template<class container>
- u32 split(container& ret, const ustring16<TAlloc>& c, bool ignoreEmptyTokens=true, bool keepSeparators=false) const
- {
- core::array<uchar32_t> v = c.toUTF32();
- return split(ret, v.pointer(), v.size(), ignoreEmptyTokens, keepSeparators);
- }
-
-
- //! Gets the size of the allocated memory buffer for the string.
- //! \return The size of the allocated memory buffer.
- u32 capacity() const
- {
- return allocated;
- }
-
-
- //! Returns the raw number of UTF-16 code points in the string which includes the individual surrogates.
- //! \return The raw number of UTF-16 code points, excluding the trialing NUL.
- u32 size_raw() const
- {
- return used;
- }
-
-
- //! Inserts a character into the string.
- //! \param c The character to insert.
- //! \param pos The position to insert the character.
- //! \return A reference to our current string.
- ustring16<TAlloc>& insert(uchar32_t c, u32 pos)
- {
- u8 len = (c > 0xFFFF ? 2 : 1);
-
- if (used + len >= allocated)
- reallocate(used + len);
-
- used += len;
-
- iterator iter(*this, pos);
- for (u32 i = used - 2; i > iter.getPos(); --i)
- array[i] = array[i - len];
-
- if (c > 0xFFFF)
- {
- // c will be multibyte, so split it up into a surrogate pair.
- uchar16_t x = static_cast<uchar16_t>(c);
- uchar16_t vh = UTF16_HI_SURROGATE | ((((c >> 16) & ((1 << 5) - 1)) - 1) << 6) | (x >> 10);
- uchar16_t vl = UTF16_LO_SURROGATE | (x & ((1 << 10) - 1));
- array[iter.getPos()] = vh;
- array[iter.getPos()+1] = vl;
- }
- else
- {
- array[iter.getPos()] = static_cast<uchar16_t>(c);
- }
- array[used] = 0;
- return *this;
- }
-
-
- //! Inserts a string into the string.
- //! \param c The string to insert.
- //! \param pos The position to insert the string.
- //! \return A reference to our current string.
- ustring16<TAlloc>& insert(const ustring16<TAlloc>& c, u32 pos)
- {
- u32 len = c.size_raw();
- if (len == 0) return *this;
-
- if (used + len >= allocated)
- reallocate(used + len);
-
- used += len;
-
- iterator iter(*this, pos);
- for (u32 i = used - 2; i > iter.getPos() + len; --i)
- array[i] = array[i - len];
-
- const uchar16_t* s = c.c_str();
- for (u32 i = 0; i < len; ++i)
- {
- array[pos++] = *s;
- ++s;
- }
-
- array[used] = 0;
- return *this;
- }
-
-
- //! Inserts a character into the string.
- //! \param c The character to insert.
- //! \param pos The position to insert the character.
- //! \return A reference to our current string.
- ustring16<TAlloc>& insert_raw(uchar16_t c, u32 pos)
- {
- if (used + 1 >= allocated)
- reallocate(used + 1);
-
- ++used;
-
- for (u32 i = used - 1; i > pos; --i)
- array[i] = array[i - 1];
-
- array[pos] = c;
- array[used] = 0;
- return *this;
- }
-
-
- //! Removes a character from string.
- //! \param pos Position of the character to remove.
- //! \return A reference to our current string.
- ustring16<TAlloc>& erase_raw(u32 pos)
- {
- for (u32 i=pos; i<=used; ++i)
- {
- array[i] = array[i + 1];
- }
- --used;
- array[used] = 0;
- return *this;
- }
-
-
- //! Replaces a character in the string.
- //! \param c The new character.
- //! \param pos The position of the character to replace.
- //! \return A reference to our current string.
- ustring16<TAlloc>& replace_raw(uchar16_t c, u32 pos)
- {
- array[pos] = c;
- return *this;
- }
-
-
- //! Returns an iterator to the beginning of the string.
- //! \return An iterator to the beginning of the string.
- iterator begin()
- {
- iterator i(*this, 0);
- return i;
- }
-
-
- //! Returns an iterator to the beginning of the string.
- //! \return An iterator to the beginning of the string.
- const_iterator begin() const
- {
- const_iterator i(*this, 0);
- return i;
- }
-
-
- //! Returns an iterator to the beginning of the string.
- //! \return An iterator to the beginning of the string.
- const_iterator cbegin() const
- {
- const_iterator i(*this, 0);
- return i;
- }
-
-
- //! Returns an iterator to the end of the string.
- //! \return An iterator to the end of the string.
- iterator end()
- {
- iterator i(*this, 0);
- i.toEnd();
- return i;
- }
-
-
- //! Returns an iterator to the end of the string.
- //! \return An iterator to the end of the string.
- const_iterator end() const
- {
- const_iterator i(*this, 0);
- i.toEnd();
- return i;
- }
-
-
- //! Returns an iterator to the end of the string.
- //! \return An iterator to the end of the string.
- const_iterator cend() const
- {
- const_iterator i(*this, 0);
- i.toEnd();
- return i;
- }
-
-
- //! Converts the string to a UTF-8 encoded string.
- //! \param addBOM If true, the proper unicode byte-order mark will be prefixed to the string.
- //! \return A string containing the UTF-8 encoded string.
- core::string<uchar8_t> toUTF8_s(const bool addBOM = false) const
- {
- core::string<uchar8_t> ret;
- ret.reserve(used * 4 + (addBOM ? unicode::BOM_UTF8_LEN : 0) + 1);
- const_iterator iter(*this, 0);
-
- // Add the byte order mark if the user wants it.
- if (addBOM)
- {
- ret.append(unicode::BOM_ENCODE_UTF8[0]);
- ret.append(unicode::BOM_ENCODE_UTF8[1]);
- ret.append(unicode::BOM_ENCODE_UTF8[2]);
- }
-
- while (!iter.atEnd())
- {
- uchar32_t c = *iter;
- if (c > 0xFFFF)
- { // 4 bytes
- uchar8_t b1 = (0x1E << 3) | ((c >> 18) & 0x7);
- uchar8_t b2 = (0x2 << 6) | ((c >> 12) & 0x3F);
- uchar8_t b3 = (0x2 << 6) | ((c >> 6) & 0x3F);
- uchar8_t b4 = (0x2 << 6) | (c & 0x3F);
- ret.append(b1);
- ret.append(b2);
- ret.append(b3);
- ret.append(b4);
- }
- else if (c > 0x7FF)
- { // 3 bytes
- uchar8_t b1 = (0xE << 4) | ((c >> 12) & 0xF);
- uchar8_t b2 = (0x2 << 6) | ((c >> 6) & 0x3F);
- uchar8_t b3 = (0x2 << 6) | (c & 0x3F);
- ret.append(b1);
- ret.append(b2);
- ret.append(b3);
- }
- else if (c > 0x7F)
- { // 2 bytes
- uchar8_t b1 = (0x6 << 5) | ((c >> 6) & 0x1F);
- uchar8_t b2 = (0x2 << 6) | (c & 0x3F);
- ret.append(b1);
- ret.append(b2);
- }
- else
- { // 1 byte
- ret.append(static_cast<uchar8_t>(c));
- }
- ++iter;
- }
- return ret;
- }
-
-
- //! Converts the string to a UTF-8 encoded string array.
- //! \param addBOM If true, the proper unicode byte-order mark will be prefixed to the string.
- //! \return An array containing the UTF-8 encoded string.
- core::array<uchar8_t> toUTF8(const bool addBOM = false) const
- {
- core::array<uchar8_t> ret(used * 4 + (addBOM ? unicode::BOM_UTF8_LEN : 0) + 1);
- const_iterator iter(*this, 0);
-
- // Add the byte order mark if the user wants it.
- if (addBOM)
- {
- ret.push_back(unicode::BOM_ENCODE_UTF8[0]);
- ret.push_back(unicode::BOM_ENCODE_UTF8[1]);
- ret.push_back(unicode::BOM_ENCODE_UTF8[2]);
- }
-
- while (!iter.atEnd())
- {
- uchar32_t c = *iter;
- if (c > 0xFFFF)
- { // 4 bytes
- uchar8_t b1 = (0x1E << 3) | ((c >> 18) & 0x7);
- uchar8_t b2 = (0x2 << 6) | ((c >> 12) & 0x3F);
- uchar8_t b3 = (0x2 << 6) | ((c >> 6) & 0x3F);
- uchar8_t b4 = (0x2 << 6) | (c & 0x3F);
- ret.push_back(b1);
- ret.push_back(b2);
- ret.push_back(b3);
- ret.push_back(b4);
- }
- else if (c > 0x7FF)
- { // 3 bytes
- uchar8_t b1 = (0xE << 4) | ((c >> 12) & 0xF);
- uchar8_t b2 = (0x2 << 6) | ((c >> 6) & 0x3F);
- uchar8_t b3 = (0x2 << 6) | (c & 0x3F);
- ret.push_back(b1);
- ret.push_back(b2);
- ret.push_back(b3);
- }
- else if (c > 0x7F)
- { // 2 bytes
- uchar8_t b1 = (0x6 << 5) | ((c >> 6) & 0x1F);
- uchar8_t b2 = (0x2 << 6) | (c & 0x3F);
- ret.push_back(b1);
- ret.push_back(b2);
- }
- else
- { // 1 byte
- ret.push_back(static_cast<uchar8_t>(c));
- }
- ++iter;
- }
- ret.push_back(0);
- return ret;
- }
-
-
-#ifdef USTRING_CPP0X_NEWLITERALS // C++0x
- //! Converts the string to a UTF-16 encoded string.
- //! \param endian The desired endianness of the string.
- //! \param addBOM If true, the proper unicode byte-order mark will be prefixed to the string.
- //! \return A string containing the UTF-16 encoded string.
- core::string<char16_t> toUTF16_s(const unicode::EUTF_ENDIAN endian = unicode::EUTFEE_NATIVE, const bool addBOM = false) const
- {
- core::string<char16_t> ret;
- ret.reserve(used + (addBOM ? unicode::BOM_UTF16_LEN : 0) + 1);
-
- // Add the BOM if specified.
- if (addBOM)
- {
- if (endian == unicode::EUTFEE_NATIVE)
- ret[0] = unicode::BOM;
- else if (endian == unicode::EUTFEE_LITTLE)
- {
- uchar8_t* ptr8 = reinterpret_cast<uchar8_t*>(&ret[0]);
- *ptr8++ = unicode::BOM_ENCODE_UTF16_LE[0];
- *ptr8 = unicode::BOM_ENCODE_UTF16_LE[1];
- }
- else
- {
- uchar8_t* ptr8 = reinterpret_cast<uchar8_t*>(&ret[0]);
- *ptr8++ = unicode::BOM_ENCODE_UTF16_BE[0];
- *ptr8 = unicode::BOM_ENCODE_UTF16_BE[1];
- }
- }
-
- ret.append(array);
- if (endian != unicode::EUTFEE_NATIVE && getEndianness() != endian)
- {
- char16_t* ptr = ret.c_str();
- for (u32 i = 0; i < ret.size(); ++i)
- *ptr++ = unicode::swapEndian16(*ptr);
- }
- return ret;
- }
-#endif
-
-
- //! Converts the string to a UTF-16 encoded string array.
- //! Unfortunately, no toUTF16_s() version exists due to limitations with Irrlicht's string class.
- //! \param endian The desired endianness of the string.
- //! \param addBOM If true, the proper unicode byte-order mark will be prefixed to the string.
- //! \return An array containing the UTF-16 encoded string.
- core::array<uchar16_t> toUTF16(const unicode::EUTF_ENDIAN endian = unicode::EUTFEE_NATIVE, const bool addBOM = false) const
- {
- core::array<uchar16_t> ret(used + (addBOM ? unicode::BOM_UTF16_LEN : 0) + 1);
- uchar16_t* ptr = ret.pointer();
-
- // Add the BOM if specified.
- if (addBOM)
- {
- if (endian == unicode::EUTFEE_NATIVE)
- *ptr = unicode::BOM;
- else if (endian == unicode::EUTFEE_LITTLE)
- {
- uchar8_t* ptr8 = reinterpret_cast<uchar8_t*>(ptr);
- *ptr8++ = unicode::BOM_ENCODE_UTF16_LE[0];
- *ptr8 = unicode::BOM_ENCODE_UTF16_LE[1];
- }
- else
- {
- uchar8_t* ptr8 = reinterpret_cast<uchar8_t*>(ptr);
- *ptr8++ = unicode::BOM_ENCODE_UTF16_BE[0];
- *ptr8 = unicode::BOM_ENCODE_UTF16_BE[1];
- }
- ++ptr;
- }
-
- memcpy((void*)ptr, (void*)array, used * sizeof(uchar16_t));
- if (endian != unicode::EUTFEE_NATIVE && getEndianness() != endian)
- {
- for (u32 i = 0; i <= used; ++i)
- ptr[i] = unicode::swapEndian16(ptr[i]);
- }
- ret.set_used(used + (addBOM ? unicode::BOM_UTF16_LEN : 0));
- ret.push_back(0);
- return ret;
- }
-
-
-#ifdef USTRING_CPP0X_NEWLITERALS // C++0x
- //! Converts the string to a UTF-32 encoded string.
- //! \param endian The desired endianness of the string.
- //! \param addBOM If true, the proper unicode byte-order mark will be prefixed to the string.
- //! \return A string containing the UTF-32 encoded string.
- core::string<char32_t> toUTF32_s(const unicode::EUTF_ENDIAN endian = unicode::EUTFEE_NATIVE, const bool addBOM = false) const
- {
- core::string<char32_t> ret;
- ret.reserve(size() + 1 + (addBOM ? unicode::BOM_UTF32_LEN : 0));
- const_iterator iter(*this, 0);
-
- // Add the BOM if specified.
- if (addBOM)
- {
- if (endian == unicode::EUTFEE_NATIVE)
- ret.append(unicode::BOM);
- else
- {
- union
- {
- uchar32_t full;
- u8 chunk[4];
- } t;
-
- if (endian == unicode::EUTFEE_LITTLE)
- {
- t.chunk[0] = unicode::BOM_ENCODE_UTF32_LE[0];
- t.chunk[1] = unicode::BOM_ENCODE_UTF32_LE[1];
- t.chunk[2] = unicode::BOM_ENCODE_UTF32_LE[2];
- t.chunk[3] = unicode::BOM_ENCODE_UTF32_LE[3];
- }
- else
- {
- t.chunk[0] = unicode::BOM_ENCODE_UTF32_BE[0];
- t.chunk[1] = unicode::BOM_ENCODE_UTF32_BE[1];
- t.chunk[2] = unicode::BOM_ENCODE_UTF32_BE[2];
- t.chunk[3] = unicode::BOM_ENCODE_UTF32_BE[3];
- }
- ret.append(t.full);
- }
- }
-
- while (!iter.atEnd())
- {
- uchar32_t c = *iter;
- if (endian != unicode::EUTFEE_NATIVE && getEndianness() != endian)
- c = unicode::swapEndian32(c);
- ret.append(c);
- ++iter;
- }
- return ret;
- }
-#endif
-
-
- //! Converts the string to a UTF-32 encoded string array.
- //! Unfortunately, no toUTF32_s() version exists due to limitations with Irrlicht's string class.
- //! \param endian The desired endianness of the string.
- //! \param addBOM If true, the proper unicode byte-order mark will be prefixed to the string.
- //! \return An array containing the UTF-32 encoded string.
- core::array<uchar32_t> toUTF32(const unicode::EUTF_ENDIAN endian = unicode::EUTFEE_NATIVE, const bool addBOM = false) const
- {
- core::array<uchar32_t> ret(size() + (addBOM ? unicode::BOM_UTF32_LEN : 0) + 1);
- const_iterator iter(*this, 0);
-
- // Add the BOM if specified.
- if (addBOM)
- {
- if (endian == unicode::EUTFEE_NATIVE)
- ret.push_back(unicode::BOM);
- else
- {
- union
- {
- uchar32_t full;
- u8 chunk[4];
- } t;
-
- if (endian == unicode::EUTFEE_LITTLE)
- {
- t.chunk[0] = unicode::BOM_ENCODE_UTF32_LE[0];
- t.chunk[1] = unicode::BOM_ENCODE_UTF32_LE[1];
- t.chunk[2] = unicode::BOM_ENCODE_UTF32_LE[2];
- t.chunk[3] = unicode::BOM_ENCODE_UTF32_LE[3];
- }
- else
- {
- t.chunk[0] = unicode::BOM_ENCODE_UTF32_BE[0];
- t.chunk[1] = unicode::BOM_ENCODE_UTF32_BE[1];
- t.chunk[2] = unicode::BOM_ENCODE_UTF32_BE[2];
- t.chunk[3] = unicode::BOM_ENCODE_UTF32_BE[3];
- }
- ret.push_back(t.full);
- }
- }
- ret.push_back(0);
-
- while (!iter.atEnd())
- {
- uchar32_t c = *iter;
- if (endian != unicode::EUTFEE_NATIVE && getEndianness() != endian)
- c = unicode::swapEndian32(c);
- ret.push_back(c);
- ++iter;
- }
- return ret;
- }
-
-
- //! Converts the string to a wchar_t encoded string.
- /** The size of a wchar_t changes depending on the platform. This function will store a
- correct UTF-8, -16, or -32 encoded string depending on the size of a wchar_t. **/
- //! \param endian The desired endianness of the string.
- //! \param addBOM If true, the proper unicode byte-order mark will be prefixed to the string.
- //! \return A string containing the wchar_t encoded string.
- core::string<wchar_t> toWCHAR_s(const unicode::EUTF_ENDIAN endian = unicode::EUTFEE_NATIVE, const bool addBOM = false) const
- {
- if (sizeof(wchar_t) == 4)
- {
- core::array<uchar32_t> a(toUTF32(endian, addBOM));
- core::stringw ret(a.pointer());
- return ret;
- }
- else if (sizeof(wchar_t) == 2)
- {
- if (endian == unicode::EUTFEE_NATIVE && addBOM == false)
- {
- core::stringw ret(array);
- return ret;
- }
- else
- {
- core::array<uchar16_t> a(toUTF16(endian, addBOM));
- core::stringw ret(a.pointer());
- return ret;
- }
- }
- else if (sizeof(wchar_t) == 1)
- {
- core::array<uchar8_t> a(toUTF8(addBOM));
- core::stringw ret(a.pointer());
- return ret;
- }
-
- // Shouldn't happen.
- return core::stringw();
- }
-
-
- //! Converts the string to a wchar_t encoded string array.
- /** The size of a wchar_t changes depending on the platform. This function will store a
- correct UTF-8, -16, or -32 encoded string depending on the size of a wchar_t. **/
- //! \param endian The desired endianness of the string.
- //! \param addBOM If true, the proper unicode byte-order mark will be prefixed to the string.
- //! \return An array containing the wchar_t encoded string.
- core::array<wchar_t> toWCHAR(const unicode::EUTF_ENDIAN endian = unicode::EUTFEE_NATIVE, const bool addBOM = false) const
- {
- if (sizeof(wchar_t) == 4)
- {
- core::array<uchar32_t> a(toUTF32(endian, addBOM));
- core::array<wchar_t> ret(a.size());
- ret.set_used(a.size());
- memcpy((void*)ret.pointer(), (void*)a.pointer(), a.size() * sizeof(uchar32_t));
- return ret;
- }
- if (sizeof(wchar_t) == 2)
- {
- if (endian == unicode::EUTFEE_NATIVE && addBOM == false)
- {
- core::array<wchar_t> ret(used);
- ret.set_used(used);
- memcpy((void*)ret.pointer(), (void*)array, used * sizeof(uchar16_t));
- return ret;
- }
- else
- {
- core::array<uchar16_t> a(toUTF16(endian, addBOM));
- core::array<wchar_t> ret(a.size());
- ret.set_used(a.size());
- memcpy((void*)ret.pointer(), (void*)a.pointer(), a.size() * sizeof(uchar16_t));
- return ret;
- }
- }
- if (sizeof(wchar_t) == 1)
- {
- core::array<uchar8_t> a(toUTF8(addBOM));
- core::array<wchar_t> ret(a.size());
- ret.set_used(a.size());
- memcpy((void*)ret.pointer(), (void*)a.pointer(), a.size() * sizeof(uchar8_t));
- return ret;
- }
-
- // Shouldn't happen.
- return core::array<wchar_t>();
- }
-
- //! Converts the string to a properly encoded io::path string.
- //! \param endian The desired endianness of the string.
- //! \param addBOM If true, the proper unicode byte-order mark will be prefixed to the string.
- //! \return An io::path string containing the properly encoded string.
- io::path toPATH_s(const unicode::EUTF_ENDIAN endian = unicode::EUTFEE_NATIVE, const bool addBOM = false) const
- {
-#if defined(_IRR_WCHAR_FILESYSTEM)
- return toWCHAR_s(endian, addBOM);
-#else
- return toUTF8_s(addBOM);
-#endif
- }
-
- //! Loads an unknown stream of data.
- //! Will attempt to determine if the stream is unicode data. Useful for loading from files.
- //! \param data The data stream to load from.
- //! \param data_size The length of the data string.
- //! \return A reference to our current string.
- ustring16<TAlloc>& loadDataStream(const char* data, size_t data_size)
- {
- // Clear our string.
- *this = "";
- if (!data)
- return *this;
-
- unicode::EUTF_ENCODE e = unicode::determineUnicodeBOM(data);
- switch (e)
- {
- default:
- case unicode::EUTFE_UTF8:
- append((uchar8_t*)data, data_size);
- break;
-
- case unicode::EUTFE_UTF16:
- case unicode::EUTFE_UTF16_BE:
- case unicode::EUTFE_UTF16_LE:
- append((uchar16_t*)data, data_size / 2);
- break;
-
- case unicode::EUTFE_UTF32:
- case unicode::EUTFE_UTF32_BE:
- case unicode::EUTFE_UTF32_LE:
- append((uchar32_t*)data, data_size / 4);
- break;
- }
-
- return *this;
- }
-
- //! Gets the encoding of the Unicode string this class contains.
- //! \return An enum describing the current encoding of this string.
- const unicode::EUTF_ENCODE getEncoding() const
- {
- return encoding;
- }
-
- //! Gets the endianness of the Unicode string this class contains.
- //! \return An enum describing the endianness of this string.
- const unicode::EUTF_ENDIAN getEndianness() const
- {
- if (encoding == unicode::EUTFE_UTF16_LE ||
- encoding == unicode::EUTFE_UTF32_LE)
- return unicode::EUTFEE_LITTLE;
- else return unicode::EUTFEE_BIG;
- }
-
-private:
-
- //! Reallocate the string, making it bigger or smaller.
- //! \param new_size The new size of the string.
- void reallocate(u32 new_size)
- {
- uchar16_t* old_array = array;
-
- array = allocator.allocate(new_size + 1); //new u16[new_size];
- allocated = new_size + 1;
- if (old_array == 0) return;
-
- u32 amount = used < new_size ? used : new_size;
- for (u32 i=0; i<=amount; ++i)
- array[i] = old_array[i];
-
- if (allocated <= used)
- used = allocated - 1;
-
- array[used] = 0;
-
- allocator.deallocate(old_array); // delete [] old_array;
- }
-
- //--- member variables
-
- uchar16_t* array;
- unicode::EUTF_ENCODE encoding;
- u32 allocated;
- u32 used;
- TAlloc allocator;
- //irrAllocator<uchar16_t> allocator;
-};
-
-typedef ustring16<irrAllocator<uchar16_t> > ustring;
-
-
-//! Appends two ustring16s.
-template <typename TAlloc>
-inline ustring16<TAlloc> operator+(const ustring16<TAlloc>& left, const ustring16<TAlloc>& right)
-{
- ustring16<TAlloc> ret(left);
- ret += right;
- return ret;
-}
-
-
-//! Appends a ustring16 and a null-terminated unicode string.
-template <typename TAlloc, class B>
-inline ustring16<TAlloc> operator+(const ustring16<TAlloc>& left, const B* const right)
-{
- ustring16<TAlloc> ret(left);
- ret += right;
- return ret;
-}
-
-
-//! Appends a ustring16 and a null-terminated unicode string.
-template <class B, typename TAlloc>
-inline ustring16<TAlloc> operator+(const B* const left, const ustring16<TAlloc>& right)
-{
- ustring16<TAlloc> ret(left);
- ret += right;
- return ret;
-}
-
-
-//! Appends a ustring16 and an Irrlicht string.
-template <typename TAlloc, typename B, typename BAlloc>
-inline ustring16<TAlloc> operator+(const ustring16<TAlloc>& left, const string<B, BAlloc>& right)
-{
- ustring16<TAlloc> ret(left);
- ret += right;
- return ret;
-}
-
-
-//! Appends a ustring16 and an Irrlicht string.
-template <typename TAlloc, typename B, typename BAlloc>
-inline ustring16<TAlloc> operator+(const string<B, BAlloc>& left, const ustring16<TAlloc>& right)
-{
- ustring16<TAlloc> ret(left);
- ret += right;
- return ret;
-}
-
-
-//! Appends a ustring16 and a std::basic_string.
-template <typename TAlloc, typename B, typename A, typename BAlloc>
-inline ustring16<TAlloc> operator+(const ustring16<TAlloc>& left, const std::basic_string<B, A, BAlloc>& right)
-{
- ustring16<TAlloc> ret(left);
- ret += right;
- return ret;
-}
-
-
-//! Appends a ustring16 and a std::basic_string.
-template <typename TAlloc, typename B, typename A, typename BAlloc>
-inline ustring16<TAlloc> operator+(const std::basic_string<B, A, BAlloc>& left, const ustring16<TAlloc>& right)
-{
- ustring16<TAlloc> ret(left);
- ret += right;
- return ret;
-}
-
-
-//! Appends a ustring16 and a char.
-template <typename TAlloc>
-inline ustring16<TAlloc> operator+(const ustring16<TAlloc>& left, const char right)
-{
- ustring16<TAlloc> ret(left);
- ret += right;
- return ret;
-}
-
-
-//! Appends a ustring16 and a char.
-template <typename TAlloc>
-inline ustring16<TAlloc> operator+(const char left, const ustring16<TAlloc>& right)
-{
- ustring16<TAlloc> ret(left);
- ret += right;
- return ret;
-}
-
-
-#ifdef USTRING_CPP0X_NEWLITERALS
-//! Appends a ustring16 and a uchar32_t.
-template <typename TAlloc>
-inline ustring16<TAlloc> operator+(const ustring16<TAlloc>& left, const uchar32_t right)
-{
- ustring16<TAlloc> ret(left);
- ret += right;
- return ret;
-}
-
-
-//! Appends a ustring16 and a uchar32_t.
-template <typename TAlloc>
-inline ustring16<TAlloc> operator+(const uchar32_t left, const ustring16<TAlloc>& right)
-{
- ustring16<TAlloc> ret(left);
- ret += right;
- return ret;
-}
-#endif
-
-
-//! Appends a ustring16 and a short.
-template <typename TAlloc>
-inline ustring16<TAlloc> operator+(const ustring16<TAlloc>& left, const short right)
-{
- ustring16<TAlloc> ret(left);
- ret += core::stringc(right);
- return ret;
-}
-
-
-//! Appends a ustring16 and a short.
-template <typename TAlloc>
-inline ustring16<TAlloc> operator+(const short left, const ustring16<TAlloc>& right)
-{
- ustring16<TAlloc> ret((core::stringc(left)));
- ret += right;
- return ret;
-}
-
-
-//! Appends a ustring16 and an unsigned short.
-template <typename TAlloc>
-inline ustring16<TAlloc> operator+(const ustring16<TAlloc>& left, const unsigned short right)
-{
- ustring16<TAlloc> ret(left);
- ret += core::stringc(right);
- return ret;
-}
-
-
-//! Appends a ustring16 and an unsigned short.
-template <typename TAlloc>
-inline ustring16<TAlloc> operator+(const unsigned short left, const ustring16<TAlloc>& right)
-{
- ustring16<TAlloc> ret((core::stringc(left)));
- ret += right;
- return ret;
-}
-
-
-//! Appends a ustring16 and an int.
-template <typename TAlloc>
-inline ustring16<TAlloc> operator+(const ustring16<TAlloc>& left, const int right)
-{
- ustring16<TAlloc> ret(left);
- ret += core::stringc(right);
- return ret;
-}
-
-
-//! Appends a ustring16 and an int.
-template <typename TAlloc>
-inline ustring16<TAlloc> operator+(const int left, const ustring16<TAlloc>& right)
-{
- ustring16<TAlloc> ret((core::stringc(left)));
- ret += right;
- return ret;
-}
-
-
-//! Appends a ustring16 and an unsigned int.
-template <typename TAlloc>
-inline ustring16<TAlloc> operator+(const ustring16<TAlloc>& left, const unsigned int right)
-{
- ustring16<TAlloc> ret(left);
- ret += core::stringc(right);
- return ret;
-}
-
-
-//! Appends a ustring16 and an unsigned int.
-template <typename TAlloc>
-inline ustring16<TAlloc> operator+(const unsigned int left, const ustring16<TAlloc>& right)
-{
- ustring16<TAlloc> ret((core::stringc(left)));
- ret += right;
- return ret;
-}
-
-
-//! Appends a ustring16 and a long.
-template <typename TAlloc>
-inline ustring16<TAlloc> operator+(const ustring16<TAlloc>& left, const long right)
-{
- ustring16<TAlloc> ret(left);
- ret += core::stringc(right);
- return ret;
-}
-
-
-//! Appends a ustring16 and a long.
-template <typename TAlloc>
-inline ustring16<TAlloc> operator+(const long left, const ustring16<TAlloc>& right)
-{
- ustring16<TAlloc> ret((core::stringc(left)));
- ret += right;
- return ret;
-}
-
-
-//! Appends a ustring16 and an unsigned long.
-template <typename TAlloc>
-inline ustring16<TAlloc> operator+(const ustring16<TAlloc>& left, const unsigned long right)
-{
- ustring16<TAlloc> ret(left);
- ret += core::stringc(right);
- return ret;
-}
-
-
-//! Appends a ustring16 and an unsigned long.
-template <typename TAlloc>
-inline ustring16<TAlloc> operator+(const unsigned long left, const ustring16<TAlloc>& right)
-{
- ustring16<TAlloc> ret((core::stringc(left)));
- ret += right;
- return ret;
-}
-
-
-//! Appends a ustring16 and a float.
-template <typename TAlloc>
-inline ustring16<TAlloc> operator+(const ustring16<TAlloc>& left, const float right)
-{
- ustring16<TAlloc> ret(left);
- ret += core::stringc(right);
- return ret;
-}
-
-
-//! Appends a ustring16 and a float.
-template <typename TAlloc>
-inline ustring16<TAlloc> operator+(const float left, const ustring16<TAlloc>& right)
-{
- ustring16<TAlloc> ret((core::stringc(left)));
- ret += right;
- return ret;
-}
-
-
-//! Appends a ustring16 and a double.
-template <typename TAlloc>
-inline ustring16<TAlloc> operator+(const ustring16<TAlloc>& left, const double right)
-{
- ustring16<TAlloc> ret(left);
- ret += core::stringc(right);
- return ret;
-}
-
-
-//! Appends a ustring16 and a double.
-template <typename TAlloc>
-inline ustring16<TAlloc> operator+(const double left, const ustring16<TAlloc>& right)
-{
- ustring16<TAlloc> ret((core::stringc(left)));
- ret += right;
- return ret;
-}
-
-
-#ifdef USTRING_CPP0X
-//! Appends two ustring16s.
-template <typename TAlloc>
-inline ustring16<TAlloc>&& operator+(const ustring16<TAlloc>& left, ustring16<TAlloc>&& right)
-{
- //std::cout << "MOVE operator+(&, &&)" << std::endl;
- right.insert(left, 0);
- return std::move(right);
-}
-
-
-//! Appends two ustring16s.
-template <typename TAlloc>
-inline ustring16<TAlloc>&& operator+(ustring16<TAlloc>&& left, const ustring16<TAlloc>& right)
-{
- //std::cout << "MOVE operator+(&&, &)" << std::endl;
- left.append(right);
- return std::move(left);
-}
-
-
-//! Appends two ustring16s.
-template <typename TAlloc>
-inline ustring16<TAlloc>&& operator+(ustring16<TAlloc>&& left, ustring16<TAlloc>&& right)
-{
- //std::cout << "MOVE operator+(&&, &&)" << std::endl;
- if ((right.size_raw() <= left.capacity() - left.size_raw()) ||
- (right.capacity() - right.size_raw() < left.size_raw()))
- {
- left.append(right);
- return std::move(left);
- }
- else
- {
- right.insert(left, 0);
- return std::move(right);
- }
-}
-
-
-//! Appends a ustring16 and a null-terminated unicode string.
-template <typename TAlloc, class B>
-inline ustring16<TAlloc>&& operator+(ustring16<TAlloc>&& left, const B* const right)
-{
- //std::cout << "MOVE operator+(&&, B*)" << std::endl;
- left.append(right);
- return std::move(left);
-}
-
-
-//! Appends a ustring16 and a null-terminated unicode string.
-template <class B, typename TAlloc>
-inline ustring16<TAlloc>&& operator+(const B* const left, ustring16<TAlloc>&& right)
-{
- //std::cout << "MOVE operator+(B*, &&)" << std::endl;
- right.insert(left, 0);
- return std::move(right);
-}
-
-
-//! Appends a ustring16 and an Irrlicht string.
-template <typename TAlloc, typename B, typename BAlloc>
-inline ustring16<TAlloc>&& operator+(const string<B, BAlloc>& left, ustring16<TAlloc>&& right)
-{
- //std::cout << "MOVE operator+(&, &&)" << std::endl;
- right.insert(left, 0);
- return std::move(right);
-}
-
-
-//! Appends a ustring16 and an Irrlicht string.
-template <typename TAlloc, typename B, typename BAlloc>
-inline ustring16<TAlloc>&& operator+(ustring16<TAlloc>&& left, const string<B, BAlloc>& right)
-{
- //std::cout << "MOVE operator+(&&, &)" << std::endl;
- left.append(right);
- return std::move(left);
-}
-
-
-//! Appends a ustring16 and a std::basic_string.
-template <typename TAlloc, typename B, typename A, typename BAlloc>
-inline ustring16<TAlloc>&& operator+(const std::basic_string<B, A, BAlloc>& left, ustring16<TAlloc>&& right)
-{
- //std::cout << "MOVE operator+(&, &&)" << std::endl;
- right.insert(core::ustring16<TAlloc>(left), 0);
- return std::move(right);
-}
-
-
-//! Appends a ustring16 and a std::basic_string.
-template <typename TAlloc, typename B, typename A, typename BAlloc>
-inline ustring16<TAlloc>&& operator+(ustring16<TAlloc>&& left, const std::basic_string<B, A, BAlloc>& right)
-{
- //std::cout << "MOVE operator+(&&, &)" << std::endl;
- left.append(right);
- return std::move(left);
-}
-
-
-//! Appends a ustring16 and a char.
-template <typename TAlloc>
-inline ustring16<TAlloc> operator+(ustring16<TAlloc>&& left, const char right)
-{
- left.append((uchar32_t)right);
- return std::move(left);
-}
-
-
-//! Appends a ustring16 and a char.
-template <typename TAlloc>
-inline ustring16<TAlloc> operator+(const char left, ustring16<TAlloc>&& right)
-{
- right.insert((uchar32_t)left, 0);
- return std::move(right);
-}
-
-
-#ifdef USTRING_CPP0X_NEWLITERALS
-//! Appends a ustring16 and a uchar32_t.
-template <typename TAlloc>
-inline ustring16<TAlloc> operator+(ustring16<TAlloc>&& left, const uchar32_t right)
-{
- left.append(right);
- return std::move(left);
-}
-
-
-//! Appends a ustring16 and a uchar32_t.
-template <typename TAlloc>
-inline ustring16<TAlloc> operator+(const uchar32_t left, ustring16<TAlloc>&& right)
-{
- right.insert(left, 0);
- return std::move(right);
-}
-#endif
-
-
-//! Appends a ustring16 and a short.
-template <typename TAlloc>
-inline ustring16<TAlloc> operator+(ustring16<TAlloc>&& left, const short right)
-{
- left.append(core::stringc(right));
- return std::move(left);
-}
-
-
-//! Appends a ustring16 and a short.
-template <typename TAlloc>
-inline ustring16<TAlloc> operator+(const short left, ustring16<TAlloc>&& right)
-{
- right.insert(core::stringc(left), 0);
- return std::move(right);
-}
-
-
-//! Appends a ustring16 and an unsigned short.
-template <typename TAlloc>
-inline ustring16<TAlloc> operator+(ustring16<TAlloc>&& left, const unsigned short right)
-{
- left.append(core::stringc(right));
- return std::move(left);
-}
-
-
-//! Appends a ustring16 and an unsigned short.
-template <typename TAlloc>
-inline ustring16<TAlloc> operator+(const unsigned short left, ustring16<TAlloc>&& right)
-{
- right.insert(core::stringc(left), 0);
- return std::move(right);
-}
-
-
-//! Appends a ustring16 and an int.
-template <typename TAlloc>
-inline ustring16<TAlloc> operator+(ustring16<TAlloc>&& left, const int right)
-{
- left.append(core::stringc(right));
- return std::move(left);
-}
-
-
-//! Appends a ustring16 and an int.
-template <typename TAlloc>
-inline ustring16<TAlloc> operator+(const int left, ustring16<TAlloc>&& right)
-{
- right.insert(core::stringc(left), 0);
- return std::move(right);
-}
-
-
-//! Appends a ustring16 and an unsigned int.
-template <typename TAlloc>
-inline ustring16<TAlloc> operator+(ustring16<TAlloc>&& left, const unsigned int right)
-{
- left.append(core::stringc(right));
- return std::move(left);
-}
-
-
-//! Appends a ustring16 and an unsigned int.
-template <typename TAlloc>
-inline ustring16<TAlloc> operator+(const unsigned int left, ustring16<TAlloc>&& right)
-{
- right.insert(core::stringc(left), 0);
- return std::move(right);
-}
-
-
-//! Appends a ustring16 and a long.
-template <typename TAlloc>
-inline ustring16<TAlloc> operator+(ustring16<TAlloc>&& left, const long right)
-{
- left.append(core::stringc(right));
- return std::move(left);
-}
-
-
-//! Appends a ustring16 and a long.
-template <typename TAlloc>
-inline ustring16<TAlloc> operator+(const long left, ustring16<TAlloc>&& right)
-{
- right.insert(core::stringc(left), 0);
- return std::move(right);
-}
-
-
-//! Appends a ustring16 and an unsigned long.
-template <typename TAlloc>
-inline ustring16<TAlloc> operator+(ustring16<TAlloc>&& left, const unsigned long right)
-{
- left.append(core::stringc(right));
- return std::move(left);
-}
-
-
-//! Appends a ustring16 and an unsigned long.
-template <typename TAlloc>
-inline ustring16<TAlloc> operator+(const unsigned long left, ustring16<TAlloc>&& right)
-{
- right.insert(core::stringc(left), 0);
- return std::move(right);
-}
-
-
-//! Appends a ustring16 and a float.
-template <typename TAlloc>
-inline ustring16<TAlloc> operator+(ustring16<TAlloc>&& left, const float right)
-{
- left.append(core::stringc(right));
- return std::move(left);
-}
-
-
-//! Appends a ustring16 and a float.
-template <typename TAlloc>
-inline ustring16<TAlloc> operator+(const float left, ustring16<TAlloc>&& right)
-{
- right.insert(core::stringc(left), 0);
- return std::move(right);
-}
-
-
-//! Appends a ustring16 and a double.
-template <typename TAlloc>
-inline ustring16<TAlloc> operator+(ustring16<TAlloc>&& left, const double right)
-{
- left.append(core::stringc(right));
- return std::move(left);
-}
-
-
-//! Appends a ustring16 and a double.
-template <typename TAlloc>
-inline ustring16<TAlloc> operator+(const double left, ustring16<TAlloc>&& right)
-{
- right.insert(core::stringc(left), 0);
- return std::move(right);
-}
-#endif
-
-
-#ifndef USTRING_NO_STL
-//! Writes a ustring16 to an ostream.
-template <typename TAlloc>
-inline std::ostream& operator<<(std::ostream& out, const ustring16<TAlloc>& in)
-{
- out << in.toUTF8_s().c_str();
- return out;
-}
-
-//! Writes a ustring16 to a wostream.
-template <typename TAlloc>
-inline std::wostream& operator<<(std::wostream& out, const ustring16<TAlloc>& in)
-{
- out << in.toWCHAR_s().c_str();
- return out;
-}
-#endif
-
-
-#ifndef USTRING_NO_STL
-
-namespace unicode
-{
-
-//! Hashing algorithm for hashing a ustring. Used for things like unordered_maps.
-//! Algorithm taken from std::hash<std::string>.
-class hash : public std::unary_function<core::ustring, size_t>
-{
- public:
- size_t operator()(const core::ustring& s) const
- {
- size_t ret = 2166136261U;
- size_t index = 0;
- size_t stride = 1 + s.size_raw() / 10;
-
- core::ustring::const_iterator i = s.begin();
- while (i != s.end())
- {
- // TODO: Don't force u32 on an x64 OS. Make it agnostic.
- ret = 16777619U * ret ^ (size_t)s[(u32)index];
- index += stride;
- i += stride;
- }
- return (ret);
- }
-};
-
-} // end namespace unicode
-
-#endif
-
-} // end namespace core
-} // end namespace irr
diff --git a/src/irrlicht_changes/static_text.cpp b/src/irrlicht_changes/static_text.cpp
index bf61cd64e..baf0ea626 100644
--- a/src/irrlicht_changes/static_text.cpp
+++ b/src/irrlicht_changes/static_text.cpp
@@ -12,17 +12,12 @@
#include <rect.h>
#include <SColor.h>
-#if USE_FREETYPE
- #include "CGUITTFont.h"
-#endif
-
+#include "CGUITTFont.h"
#include "util/string.h"
namespace irr
{
-#if USE_FREETYPE
-
namespace gui
{
//! constructor
@@ -108,19 +103,12 @@ void StaticText::draw()
font->getDimension(str.c_str()).Width;
}
- //str = colorizeText(BrokenText[i].c_str(), colors, previous_color);
- //if (!colors.empty())
- // previous_color = colors[colors.size() - 1];
-
-#if USE_FREETYPE
if (font->getType() == irr::gui::EGFT_CUSTOM) {
- irr::gui::CGUITTFont *tmp = static_cast<irr::gui::CGUITTFont*>(font);
+ CGUITTFont *tmp = static_cast<CGUITTFont*>(font);
tmp->draw(str,
- r, previous_color, // FIXME
- HAlign == EGUIA_CENTER, VAlign == EGUIA_CENTER,
+ r, HAlign == EGUIA_CENTER, VAlign == EGUIA_CENTER,
(RestrainTextInside ? &AbsoluteClippingRect : NULL));
} else
-#endif
{
// Draw non-colored text
font->draw(str.c_str(),
@@ -246,15 +234,17 @@ void StaticText::setTextAlignment(EGUI_ALIGNMENT horizontal, EGUI_ALIGNMENT vert
}
-#if IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR <= 7
-const video::SColor& StaticText::getOverrideColor() const
-#else
video::SColor StaticText::getOverrideColor() const
-#endif
{
return ColoredText.getDefaultColor();
}
+#if IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR > 8
+video::SColor StaticText::getActiveColor() const
+{
+ return getOverrideColor();
+}
+#endif
//! Sets if the static text should use the overide color or the
//! color in the gui skin.
@@ -591,54 +581,8 @@ s32 StaticText::getTextWidth() const
}
-//! Writes attributes of the element.
-//! Implement this to expose the attributes of your element for
-//! scripting languages, editors, debuggers or xml serialization purposes.
-void StaticText::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options=0) const
-{
- IGUIStaticText::serializeAttributes(out,options);
-
- out->addBool ("Border", Border);
- out->addBool ("OverrideColorEnabled",true);
- out->addBool ("OverrideBGColorEnabled",ColoredText.hasBackground());
- out->addBool ("WordWrap", WordWrap);
- out->addBool ("Background", Background);
- out->addBool ("RightToLeft", RightToLeft);
- out->addBool ("RestrainTextInside", RestrainTextInside);
- out->addColor ("OverrideColor", ColoredText.getDefaultColor());
- out->addColor ("BGColor", ColoredText.getBackground());
- out->addEnum ("HTextAlign", HAlign, GUIAlignmentNames);
- out->addEnum ("VTextAlign", VAlign, GUIAlignmentNames);
-
- // out->addFont ("OverrideFont", OverrideFont);
-}
-
-
-//! Reads attributes of the element
-void StaticText::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options=0)
-{
- IGUIStaticText::deserializeAttributes(in,options);
-
- Border = in->getAttributeAsBool("Border");
- setWordWrap(in->getAttributeAsBool("WordWrap"));
- Background = in->getAttributeAsBool("Background");
- RightToLeft = in->getAttributeAsBool("RightToLeft");
- RestrainTextInside = in->getAttributeAsBool("RestrainTextInside");
- if (in->getAttributeAsBool("OverrideColorEnabled"))
- ColoredText.setDefaultColor(in->getAttributeAsColor("OverrideColor"));
- if (in->getAttributeAsBool("OverrideBGColorEnabled"))
- ColoredText.setBackground(in->getAttributeAsColor("BGColor"));
-
- setTextAlignment( (EGUI_ALIGNMENT) in->getAttributeAsEnumeration("HTextAlign", GUIAlignmentNames),
- (EGUI_ALIGNMENT) in->getAttributeAsEnumeration("VTextAlign", GUIAlignmentNames));
-
- // OverrideFont = in->getAttributeAsFont("OverrideFont");
-}
-
} // end namespace gui
-#endif // USE_FREETYPE
-
} // end namespace irr
diff --git a/src/irrlicht_changes/static_text.h b/src/irrlicht_changes/static_text.h
index 1f111ea56..74ef62008 100644
--- a/src/irrlicht_changes/static_text.h
+++ b/src/irrlicht_changes/static_text.h
@@ -20,7 +20,6 @@
#include "config.h"
#include <IGUIEnvironment.h>
-#if USE_FREETYPE
namespace irr
{
@@ -134,10 +133,11 @@ namespace gui
virtual void setTextAlignment(EGUI_ALIGNMENT horizontal, EGUI_ALIGNMENT vertical);
//! Gets the override color
- #if IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR <= 7
- virtual const video::SColor& getOverrideColor() const;
- #else
virtual video::SColor getOverrideColor() const;
+
+ #if IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR > 8
+ //! Gets the currently used text color
+ virtual video::SColor getActiveColor() const;
#endif
//! Sets if the static text should use the overide color or the
@@ -183,12 +183,6 @@ namespace gui
//! Checks if the text should be interpreted as right-to-left text
virtual bool isRightToLeft() const;
- //! Writes attributes of the element.
- virtual void serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options) const;
-
- //! Reads attributes of the element
- virtual void deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options);
-
virtual bool hasType(EGUI_ELEMENT_TYPE t) const {
return (t == EGUIET_ENRICHED_STATIC_TEXT) || (t == EGUIET_STATIC_TEXT);
};
@@ -235,41 +229,6 @@ inline void setStaticText(irr::gui::IGUIStaticText *static_text, const EnrichedS
}
}
-#else // USE_FREETYPE
-
-namespace irr
-{
-namespace gui
-{
-
-class StaticText
-{
-public:
- static irr::gui::IGUIStaticText *add(
- irr::gui::IGUIEnvironment *guienv,
- const EnrichedString &text,
- const core::rect< s32 > &rectangle,
- bool border = false,
- bool wordWrap = true,
- irr::gui::IGUIElement *parent = NULL,
- s32 id = -1,
- bool fillBackground = false)
- {
- return guienv->addStaticText(text.c_str(), rectangle, border, wordWrap, parent, id, fillBackground);
- }
-};
-
-} // end namespace gui
-
-} // end namespace irr
-
-inline void setStaticText(irr::gui::IGUIStaticText *static_text, const EnrichedString &text)
-{
- static_text->setText(text.c_str());
-}
-
-#endif
-
inline void setStaticText(irr::gui::IGUIStaticText *static_text, const wchar_t *text)
{
setStaticText(static_text, EnrichedString(text, static_text->getOverrideColor()));
diff --git a/src/irrlichttypes.h b/src/irrlichttypes.h
index 794776b26..93c2d105b 100644
--- a/src/irrlichttypes.h
+++ b/src/irrlichttypes.h
@@ -19,16 +19,11 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#pragma once
-/* Ensure that <stdint.h> is included before <irrTypes.h>, unless building on
- * MSVC, to address an irrlicht issue: https://sourceforge.net/p/irrlicht/bugs/433/
- *
- * TODO: Decide whether or not we support non-compliant C++ compilers like old
- * versions of MSCV. If we do not then <stdint.h> can always be included
- * regardless of the compiler.
+/*
+ * IrrlichtMt already includes stdint.h in irrTypes.h. This works everywhere
+ * we need it to (including recent MSVC), so should be fine here too.
*/
-#ifndef _MSC_VER
-# include <cstdint>
-#endif
+#include <cstdint>
#include <irrTypes.h>
@@ -36,19 +31,6 @@ using namespace irr;
namespace irr {
-// Irrlicht 1.8+ defines 64bit unsigned symbol in irrTypes.h
-#if (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 8)
-#ifdef _MSC_VER
- // Windows
- typedef long long s64;
- typedef unsigned long long u64;
-#else
- // Posix
- typedef int64_t s64;
- typedef uint64_t u64;
-#endif
-#endif
-
#if (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR >= 9)
namespace core {
template <typename T>
diff --git a/src/itemdef.cpp b/src/itemdef.cpp
index 5fb1e4c47..d79d6b263 100644
--- a/src/itemdef.cpp
+++ b/src/itemdef.cpp
@@ -71,13 +71,11 @@ ItemDefinition& ItemDefinition::operator=(const ItemDefinition &def)
stack_max = def.stack_max;
usable = def.usable;
liquids_pointable = def.liquids_pointable;
- if(def.tool_capabilities)
- {
- tool_capabilities = new ToolCapabilities(
- *def.tool_capabilities);
- }
+ if (def.tool_capabilities)
+ tool_capabilities = new ToolCapabilities(*def.tool_capabilities);
groups = def.groups;
node_placement_prediction = def.node_placement_prediction;
+ place_param2 = def.place_param2;
sound_place = def.sound_place;
sound_place_failed = def.sound_place_failed;
range = def.range;
@@ -120,8 +118,8 @@ void ItemDefinition::reset()
sound_place = SimpleSoundSpec();
sound_place_failed = SimpleSoundSpec();
range = -1;
-
node_placement_prediction = "";
+ place_param2 = 0;
}
void ItemDefinition::serialize(std::ostream &os, u16 protocol_version) const
@@ -166,6 +164,8 @@ void ItemDefinition::serialize(std::ostream &os, u16 protocol_version) const
os << serializeString16(wield_overlay);
os << serializeString16(short_description);
+
+ os << place_param2;
}
void ItemDefinition::deSerialize(std::istream &is)
@@ -219,6 +219,8 @@ void ItemDefinition::deSerialize(std::istream &is)
// block to not need to increase the version.
try {
short_description = deSerializeString16(is);
+
+ place_param2 = readU8(is); // 0 if missing
} catch(SerializationError &e) {};
}
diff --git a/src/itemdef.h b/src/itemdef.h
index ebf0d3527..3e302840f 100644
--- a/src/itemdef.h
+++ b/src/itemdef.h
@@ -86,6 +86,7 @@ struct ItemDefinition
// Server will update the precise end result a moment later.
// "" = no prediction
std::string node_placement_prediction;
+ u8 place_param2;
/*
Some helpful methods
diff --git a/src/itemstackmetadata.cpp b/src/itemstackmetadata.cpp
index 7a26fbb0e..529e0149f 100644
--- a/src/itemstackmetadata.cpp
+++ b/src/itemstackmetadata.cpp
@@ -60,7 +60,7 @@ bool ItemStackMetadata::setString(const std::string &name, const std::string &va
void ItemStackMetadata::serialize(std::ostream &os) const
{
- std::ostringstream os2;
+ std::ostringstream os2(std::ios_base::binary);
os2 << DESERIALIZE_START;
for (const auto &stringvar : m_stringvars) {
if (!stringvar.first.empty() || !stringvar.second.empty())
diff --git a/src/main.cpp b/src/main.cpp
index 39b441d2c..ca95ef874 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -38,6 +38,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "player.h"
#include "porting.h"
#include "network/socket.h"
+#include "mapblock.h"
#if USE_CURSES
#include "terminal_chat_console.h"
#endif
@@ -60,11 +61,8 @@ extern "C" {
#endif
}
-#if !defined(SERVER) && \
- (IRRLICHT_VERSION_MAJOR == 1) && \
- (IRRLICHT_VERSION_MINOR == 8) && \
- (IRRLICHT_VERSION_REVISION == 2)
- #error "Irrlicht 1.8.2 is known to be broken - please update Irrlicht to version >= 1.8.3"
+#if !defined(__cpp_rtti) || !defined(__cpp_exceptions)
+#error Minetest cannot be built without exceptions or RTTI
#endif
#define DEBUGFILE "debug.txt"
@@ -91,6 +89,7 @@ static void list_worlds(bool print_name, bool print_path);
static bool setup_log_params(const Settings &cmd_args);
static bool create_userdata_path();
static bool init_common(const Settings &cmd_args, int argc, char *argv[]);
+static void uninit_common();
static void startup_message();
static bool read_config_file(const Settings &cmd_args);
static void init_log_streams(const Settings &cmd_args);
@@ -110,6 +109,7 @@ static bool determine_subgame(GameParams *game_params);
static bool run_dedicated_server(const GameParams &game_params, const Settings &cmd_args);
static bool migrate_map_database(const GameParams &game_params, const Settings &cmd_args);
+static bool recompress_map_database(const GameParams &game_params, const Settings &cmd_args, const Address &addr);
/**********************************************************************/
@@ -201,6 +201,7 @@ int main(int argc, char *argv[])
errorstream << "Unittest support is not enabled in this binary. "
<< "If you want to enable it, compile project with BUILD_UNITTESTS=1 flag."
<< std::endl;
+ return 1;
#endif
}
#endif
@@ -225,8 +226,7 @@ int main(int argc, char *argv[])
return run_dedicated_server(game_params, cmd_args) ? 0 : 1;
#ifndef SERVER
- ClientLauncher launcher;
- retval = launcher.run(game_params, cmd_args) ? 0 : 1;
+ retval = ClientLauncher().run(game_params, cmd_args) ? 0 : 1;
#else
retval = 0;
#endif
@@ -237,9 +237,6 @@ int main(int argc, char *argv[])
print_modified_quicktune_values();
- // Stop httpfetch thread (if started)
- httpfetch_cleanup();
-
END_DEBUG_EXCEPTION_HANDLER
return retval;
@@ -302,11 +299,13 @@ static void set_allowed_options(OptionList *allowed_options)
_("Migrate from current players backend to another (Only works when using minetestserver or with --server)"))));
allowed_options->insert(std::make_pair("migrate-auth", ValueSpec(VALUETYPE_STRING,
_("Migrate from current auth backend to another (Only works when using minetestserver or with --server)"))));
+ allowed_options->insert(std::make_pair("migrate-mod-storage", ValueSpec(VALUETYPE_STRING,
+ _("Migrate from current mod storage backend to another (Only works when using minetestserver or with --server)"))));
allowed_options->insert(std::make_pair("terminal", ValueSpec(VALUETYPE_FLAG,
_("Feature an interactive terminal (Only works when using minetestserver or with --server)"))));
+ allowed_options->insert(std::make_pair("recompress", ValueSpec(VALUETYPE_FLAG,
+ _("Recompress the blocks of the given map database."))));
#ifndef SERVER
- allowed_options->insert(std::make_pair("videomodes", ValueSpec(VALUETYPE_FLAG,
- _("Show available video modes"))));
allowed_options->insert(std::make_pair("speedtests", ValueSpec(VALUETYPE_FLAG,
_("Run speed tests"))));
allowed_options->insert(std::make_pair("address", ValueSpec(VALUETYPE_STRING,
@@ -489,13 +488,14 @@ static bool init_common(const Settings &cmd_args, int argc, char *argv[])
startup_message();
set_default_settings();
- // Initialize sockets
sockets_init();
- atexit(sockets_cleanup);
// Initialize g_settings
Settings::createLayer(SL_GLOBAL);
+ // Set cleanup callback(s) to run at process exit
+ atexit(uninit_common);
+
if (!read_config_file(cmd_args))
return false;
@@ -514,6 +514,17 @@ static bool init_common(const Settings &cmd_args, int argc, char *argv[])
return true;
}
+static void uninit_common()
+{
+ httpfetch_cleanup();
+
+ sockets_cleanup();
+
+ // It'd actually be okay to leak these but we want to please valgrind...
+ for (int i = 0; i < (int)SL_TOTAL_COUNT; i++)
+ delete Settings::getLayer((SettingsLayer)i);
+}
+
static void startup_message()
{
infostream << PROJECT_NAME << " " << _("with")
@@ -867,7 +878,7 @@ static bool run_dedicated_server(const GameParams &game_params, const Settings &
return false;
}
- // Database migration
+ // Database migration/compression
if (cmd_args.exists("migrate"))
return migrate_map_database(game_params, cmd_args);
@@ -877,6 +888,12 @@ static bool run_dedicated_server(const GameParams &game_params, const Settings &
if (cmd_args.exists("migrate-auth"))
return ServerEnvironment::migrateAuthDatabase(game_params, cmd_args);
+ if (cmd_args.exists("migrate-mod-storage"))
+ return Server::migrateModStorageDatabase(game_params, cmd_args);
+
+ if (cmd_args.getFlag("recompress"))
+ return recompress_map_database(game_params, cmd_args, bind_addr);
+
if (cmd_args.exists("terminal")) {
#if USE_CURSES
bool name_ok = true;
@@ -1026,3 +1043,67 @@ static bool migrate_map_database(const GameParams &game_params, const Settings &
return true;
}
+
+static bool recompress_map_database(const GameParams &game_params, const Settings &cmd_args, const Address &addr)
+{
+ Settings world_mt;
+ const std::string world_mt_path = game_params.world_path + DIR_DELIM + "world.mt";
+
+ if (!world_mt.readConfigFile(world_mt_path.c_str())) {
+ errorstream << "Cannot read world.mt at " << world_mt_path << std::endl;
+ return false;
+ }
+ const std::string &backend = world_mt.get("backend");
+ Server server(game_params.world_path, game_params.game_spec, false, addr, false);
+ MapDatabase *db = ServerMap::createDatabase(backend, game_params.world_path, world_mt);
+
+ u32 count = 0;
+ u64 last_update_time = 0;
+ bool &kill = *porting::signal_handler_killstatus();
+ const u8 serialize_as_ver = SER_FMT_VER_HIGHEST_WRITE;
+
+ // This is ok because the server doesn't actually run
+ std::vector<v3s16> blocks;
+ db->listAllLoadableBlocks(blocks);
+ db->beginSave();
+ std::istringstream iss(std::ios_base::binary);
+ std::ostringstream oss(std::ios_base::binary);
+ for (auto it = blocks.begin(); it != blocks.end(); ++it) {
+ if (kill) return false;
+
+ std::string data;
+ db->loadBlock(*it, &data);
+ if (data.empty()) {
+ errorstream << "Failed to load block " << PP(*it) << std::endl;
+ return false;
+ }
+
+ iss.str(data);
+ iss.clear();
+
+ MapBlock mb(nullptr, v3s16(0,0,0), &server);
+ u8 ver = readU8(iss);
+ mb.deSerialize(iss, ver, true);
+
+ oss.str("");
+ oss.clear();
+ writeU8(oss, serialize_as_ver);
+ mb.serialize(oss, serialize_as_ver, true, -1);
+
+ db->saveBlock(*it, oss.str());
+
+ count++;
+ if (count % 0xFF == 0 && porting::getTimeS() - last_update_time >= 1) {
+ std::cerr << " Recompressed " << count << " blocks, "
+ << (100.0f * count / blocks.size()) << "% completed.\r";
+ db->endSave();
+ db->beginSave();
+ last_update_time = porting::getTimeS();
+ }
+ }
+ std::cerr << std::endl;
+ db->endSave();
+
+ actionstream << "Done, " << count << " blocks were recompressed." << std::endl;
+ return true;
+}
diff --git a/src/map.cpp b/src/map.cpp
index 7c59edbaa..a11bbb96a 100644
--- a/src/map.cpp
+++ b/src/map.cpp
@@ -139,13 +139,6 @@ MapBlock * Map::getBlockNoCreate(v3s16 p3d)
return block;
}
-bool Map::isNodeUnderground(v3s16 p)
-{
- v3s16 blockpos = getNodeBlockPos(p);
- MapBlock *block = getBlockNoCreateNoEx(blockpos);
- return block && block->getIsUnderground();
-}
-
bool Map::isValidPosition(v3s16 p)
{
v3s16 blockpos = getNodeBlockPos(p);
@@ -829,7 +822,7 @@ void Map::transformLiquids(std::map<v3s16, MapBlock*> &modified_blocks,
m_transforming_liquid.push_back(iter);
voxalgo::update_lighting_nodes(this, changed_nodes, modified_blocks);
-
+ env->getScriptIface()->on_liquid_transformed(changed_nodes);
/* ----------------------------------------------------------------------
* Manage the queue so that it does not grow indefinately
@@ -1451,11 +1444,7 @@ MapSector *ServerMap::createSector(v2s16 p2d)
/*
Do not create over max mapgen limit
*/
- const s16 max_limit_bp = MAX_MAP_GENERATION_LIMIT / MAP_BLOCKSIZE;
- if (p2d.X < -max_limit_bp ||
- p2d.X > max_limit_bp ||
- p2d.Y < -max_limit_bp ||
- p2d.Y > max_limit_bp)
+ if (blockpos_over_max_limit(v3s16(p2d.X, 0, p2d.Y)))
throw InvalidPositionException("createSector(): pos. over max mapgen limit");
/*
@@ -1464,9 +1453,6 @@ MapSector *ServerMap::createSector(v2s16 p2d)
sector = new MapSector(this, p2d, m_gamedef);
- // Sector position on map in nodes
- //v2s16 nodepos2d = p2d * MAP_BLOCKSIZE;
-
/*
Insert to container
*/
@@ -1549,6 +1535,11 @@ MapBlock *ServerMap::getBlockOrEmerge(v3s16 p3d)
return block;
}
+bool ServerMap::isBlockInQueue(v3s16 pos)
+{
+ return m_emerge && m_emerge->isBlockInQueue(pos);
+}
+
// N.B. This requires no synchronization, since data will not be modified unless
// the VoxelManipulator being updated belongs to the same thread.
void ServerMap::updateVManip(v3s16 pos)
diff --git a/src/map.h b/src/map.h
index e68795c4a..fe580b20f 100644
--- a/src/map.h
+++ b/src/map.h
@@ -167,9 +167,6 @@ public:
inline const NodeDefManager * getNodeDefManager() { return m_nodedef; }
- // Returns InvalidPositionException if not found
- bool isNodeUnderground(v3s16 p);
-
bool isValidPosition(v3s16 p);
// throws InvalidPositionException if not found
@@ -365,6 +362,8 @@ public:
*/
MapBlock *getBlockOrEmerge(v3s16 p3d);
+ bool isBlockInQueue(v3s16 pos);
+
/*
Database functions
*/
diff --git a/src/map_settings_manager.cpp b/src/map_settings_manager.cpp
index 99e3cb0e6..c75483edb 100644
--- a/src/map_settings_manager.cpp
+++ b/src/map_settings_manager.cpp
@@ -26,15 +26,24 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "map_settings_manager.h"
MapSettingsManager::MapSettingsManager(const std::string &map_meta_path):
- m_map_meta_path(map_meta_path)
+ m_map_meta_path(map_meta_path),
+ m_hierarchy(g_settings)
{
- m_map_settings = Settings::createLayer(SL_MAP, "[end_of_params]");
- Mapgen::setDefaultSettings(Settings::getLayer(SL_DEFAULTS));
+ /*
+ * We build our own hierarchy which falls back to the global one.
+ * It looks as follows: (lowest prio first)
+ * 0: whatever is picked up from g_settings (incl. engine defaults)
+ * 1: defaults set by scripts (override_meta = false)
+ * 2: settings present in map_meta.txt or overriden by scripts
+ */
+ m_defaults = new Settings("", &m_hierarchy, 1);
+ m_map_settings = new Settings("[end_of_params]", &m_hierarchy, 2);
}
MapSettingsManager::~MapSettingsManager()
{
+ delete m_defaults;
delete m_map_settings;
delete mapgen_params;
}
@@ -43,15 +52,7 @@ MapSettingsManager::~MapSettingsManager()
bool MapSettingsManager::getMapSetting(
const std::string &name, std::string *value_out)
{
- // Get from map_meta.txt, then try from all other sources
- if (m_map_settings->getNoEx(name, *value_out))
- return true;
-
- // Compatibility kludge
- if (name == "seed")
- return Settings::getLayer(SL_GLOBAL)->getNoEx("fixed_map_seed", *value_out);
-
- return false;
+ return m_map_settings->getNoEx(name, *value_out);
}
@@ -72,7 +73,7 @@ bool MapSettingsManager::setMapSetting(
if (override_meta)
m_map_settings->set(name, value);
else
- Settings::getLayer(SL_GLOBAL)->set(name, value);
+ m_defaults->set(name, value);
return true;
}
@@ -87,7 +88,7 @@ bool MapSettingsManager::setMapSettingNoiseParams(
if (override_meta)
m_map_settings->setNoiseParams(name, *value);
else
- Settings::getLayer(SL_GLOBAL)->setNoiseParams(name, *value);
+ m_defaults->setNoiseParams(name, *value);
return true;
}
@@ -146,15 +147,8 @@ MapgenParams *MapSettingsManager::makeMapgenParams()
if (mapgen_params)
return mapgen_params;
- assert(m_map_settings != NULL);
-
- // At this point, we have (in order of precedence):
- // 1). SL_MAP containing map_meta.txt settings or
- // explicit overrides from scripts
- // 2). SL_GLOBAL containing all user-specified config file
- // settings
- // 3). SL_DEFAULTS containing any low-priority settings from
- // scripts, e.g. mods using Lua as an enhanced config file)
+ assert(m_map_settings);
+ assert(m_defaults);
// Now, get the mapgen type so we can create the appropriate MapgenParams
std::string mg_name;
diff --git a/src/map_settings_manager.h b/src/map_settings_manager.h
index 9258d3032..fa271268d 100644
--- a/src/map_settings_manager.h
+++ b/src/map_settings_manager.h
@@ -20,8 +20,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#pragma once
#include <string>
+#include "settings.h"
-class Settings;
struct NoiseParams;
struct MapgenParams;
@@ -70,6 +70,8 @@ public:
private:
std::string m_map_meta_path;
- // TODO: Rename to "m_settings"
+
+ SettingsHierarchy m_hierarchy;
+ Settings *m_defaults;
Settings *m_map_settings;
};
diff --git a/src/mapblock.cpp b/src/mapblock.cpp
index 0ca71e643..e3a6caa19 100644
--- a/src/mapblock.cpp
+++ b/src/mapblock.cpp
@@ -218,31 +218,6 @@ void MapBlock::expireDayNightDiff()
m_day_night_differs_expired = true;
}
-s16 MapBlock::getGroundLevel(v2s16 p2d)
-{
- if(isDummy())
- return -3;
- try
- {
- s16 y = MAP_BLOCKSIZE-1;
- for(; y>=0; y--)
- {
- MapNode n = getNodeRef(p2d.X, y, p2d.Y);
- if (m_gamedef->ndef()->get(n).walkable) {
- if(y == MAP_BLOCKSIZE-1)
- return -2;
-
- return y;
- }
- }
- return -1;
- }
- catch(InvalidPositionException &e)
- {
- return -3;
- }
-}
-
/*
Serialization
*/
@@ -355,7 +330,7 @@ static void correctBlockNodeIds(const NameIdMapping *nimap, MapNode *nodes,
}
}
-void MapBlock::serialize(std::ostream &os, u8 version, bool disk, int compression_level)
+void MapBlock::serialize(std::ostream &os_compressed, u8 version, bool disk, int compression_level)
{
if(!ser_ver_supported(version))
throw VersionMismatchException("ERROR: MapBlock format not supported");
@@ -365,6 +340,9 @@ void MapBlock::serialize(std::ostream &os, u8 version, bool disk, int compressio
FATAL_ERROR_IF(version < SER_FMT_VER_LOWEST_WRITE, "Serialisation version error");
+ std::ostringstream os_raw(std::ios_base::binary);
+ std::ostream &os = version >= 29 ? os_raw : os_compressed;
+
// First byte
u8 flags = 0;
if(is_underground)
@@ -382,37 +360,52 @@ void MapBlock::serialize(std::ostream &os, u8 version, bool disk, int compressio
Bulk node data
*/
NameIdMapping nimap;
- if(disk)
+ SharedBuffer<u8> buf;
+ const u8 content_width = 2;
+ const u8 params_width = 2;
+ if(disk)
{
MapNode *tmp_nodes = new MapNode[nodecount];
- for(u32 i=0; i<nodecount; i++)
- tmp_nodes[i] = data[i];
+ memcpy(tmp_nodes, data, nodecount * sizeof(MapNode));
getBlockNodeIdMapping(&nimap, tmp_nodes, m_gamedef->ndef());
- u8 content_width = 2;
- u8 params_width = 2;
- writeU8(os, content_width);
- writeU8(os, params_width);
- MapNode::serializeBulk(os, version, tmp_nodes, nodecount,
- content_width, params_width, compression_level);
+ buf = MapNode::serializeBulk(version, tmp_nodes, nodecount,
+ content_width, params_width);
delete[] tmp_nodes;
+
+ // write timestamp and node/id mapping first
+ if (version >= 29) {
+ writeU32(os, getTimestamp());
+
+ nimap.serialize(os);
+ }
}
else
{
- u8 content_width = 2;
- u8 params_width = 2;
- writeU8(os, content_width);
- writeU8(os, params_width);
- MapNode::serializeBulk(os, version, data, nodecount,
- content_width, params_width, compression_level);
+ buf = MapNode::serializeBulk(version, data, nodecount,
+ content_width, params_width);
+ }
+
+ writeU8(os, content_width);
+ writeU8(os, params_width);
+ if (version >= 29) {
+ os.write(reinterpret_cast<char*>(*buf), buf.getSize());
+ } else {
+ // prior to 29 node data was compressed individually
+ compress(buf, os, version, compression_level);
}
/*
Node metadata
*/
- std::ostringstream oss(std::ios_base::binary);
- m_node_metadata.serialize(oss, version, disk);
- compressZlib(oss.str(), os, compression_level);
+ if (version >= 29) {
+ m_node_metadata.serialize(os, version, disk);
+ } else {
+ // use os_raw from above to avoid allocating another stream object
+ m_node_metadata.serialize(os_raw, version, disk);
+ // prior to 29 node data was compressed individually
+ compress(os_raw.str(), os, version, compression_level);
+ }
/*
Data that goes to disk, but not the network
@@ -427,17 +420,24 @@ void MapBlock::serialize(std::ostream &os, u8 version, bool disk, int compressio
// Static objects
m_static_objects.serialize(os);
- // Timestamp
- writeU32(os, getTimestamp());
+ if(version < 29){
+ // Timestamp
+ writeU32(os, getTimestamp());
- // Write block-specific node definition id mapping
- nimap.serialize(os);
+ // Write block-specific node definition id mapping
+ nimap.serialize(os);
+ }
if(version >= 25){
// Node timers
m_node_timers.serialize(os, version);
}
}
+
+ if (version >= 29) {
+ // now compress the whole thing
+ compress(os_raw.str(), os_compressed, version, compression_level);
+ }
}
void MapBlock::serializeNetworkSpecific(std::ostream &os)
@@ -449,7 +449,7 @@ void MapBlock::serializeNetworkSpecific(std::ostream &os)
writeU8(os, 2); // version
}
-void MapBlock::deSerialize(std::istream &is, u8 version, bool disk)
+void MapBlock::deSerialize(std::istream &in_compressed, u8 version, bool disk)
{
if(!ser_ver_supported(version))
throw VersionMismatchException("ERROR: MapBlock format not supported");
@@ -460,10 +460,16 @@ void MapBlock::deSerialize(std::istream &is, u8 version, bool disk)
if(version <= 21)
{
- deSerialize_pre22(is, version, disk);
+ deSerialize_pre22(in_compressed, version, disk);
return;
}
+ // Decompress the whole block (version >= 29)
+ std::stringstream in_raw(std::ios_base::binary | std::ios_base::in | std::ios_base::out);
+ if (version >= 29)
+ decompress(in_compressed, in_raw, version);
+ std::istream &is = version >= 29 ? in_raw : in_compressed;
+
u8 flags = readU8(is);
is_underground = (flags & 0x01) != 0;
m_day_night_differs = (flags & 0x02) != 0;
@@ -473,9 +479,20 @@ void MapBlock::deSerialize(std::istream &is, u8 version, bool disk)
m_lighting_complete = readU16(is);
m_generated = (flags & 0x08) == 0;
- /*
- Bulk node data
- */
+ NameIdMapping nimap;
+ if (disk && version >= 29) {
+ // Timestamp
+ TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())
+ <<": Timestamp"<<std::endl);
+ setTimestampNoChangedFlag(readU32(is));
+ m_disk_timestamp = m_timestamp;
+
+ // Node/id mapping
+ TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())
+ <<": NameIdMapping"<<std::endl);
+ nimap.deSerialize(is);
+ }
+
TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())
<<": Bulk node data"<<std::endl);
u8 content_width = readU8(is);
@@ -484,29 +501,44 @@ void MapBlock::deSerialize(std::istream &is, u8 version, bool disk)
throw SerializationError("MapBlock::deSerialize(): invalid content_width");
if(params_width != 2)
throw SerializationError("MapBlock::deSerialize(): invalid params_width");
- MapNode::deSerializeBulk(is, version, data, nodecount,
+
+ /*
+ Bulk node data
+ */
+ if (version >= 29) {
+ MapNode::deSerializeBulk(is, version, data, nodecount,
+ content_width, params_width);
+ } else {
+ // use in_raw from above to avoid allocating another stream object
+ decompress(is, in_raw, version);
+ MapNode::deSerializeBulk(in_raw, version, data, nodecount,
content_width, params_width);
+ }
/*
NodeMetadata
*/
TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())
<<": Node metadata"<<std::endl);
- // Ignore errors
- try {
- std::ostringstream oss(std::ios_base::binary);
- decompressZlib(is, oss);
- std::istringstream iss(oss.str(), std::ios_base::binary);
- if (version >= 23)
- m_node_metadata.deSerialize(iss, m_gamedef->idef());
- else
- content_nodemeta_deserialize_legacy(iss,
- &m_node_metadata, &m_node_timers,
- m_gamedef->idef());
- } catch(SerializationError &e) {
- warningstream<<"MapBlock::deSerialize(): Ignoring an error"
- <<" while deserializing node metadata at ("
- <<PP(getPos())<<": "<<e.what()<<std::endl;
+ if (version >= 29) {
+ m_node_metadata.deSerialize(is, m_gamedef->idef());
+ } else {
+ try {
+ // reuse in_raw
+ in_raw.str("");
+ in_raw.clear();
+ decompress(is, in_raw, version);
+ if (version >= 23)
+ m_node_metadata.deSerialize(in_raw, m_gamedef->idef());
+ else
+ content_nodemeta_deserialize_legacy(in_raw,
+ &m_node_metadata, &m_node_timers,
+ m_gamedef->idef());
+ } catch(SerializationError &e) {
+ warningstream<<"MapBlock::deSerialize(): Ignoring an error"
+ <<" while deserializing node metadata at ("
+ <<PP(getPos())<<": "<<e.what()<<std::endl;
+ }
}
/*
@@ -530,17 +562,20 @@ void MapBlock::deSerialize(std::istream &is, u8 version, bool disk)
<<": Static objects"<<std::endl);
m_static_objects.deSerialize(is);
- // Timestamp
- TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())
- <<": Timestamp"<<std::endl);
- setTimestampNoChangedFlag(readU32(is));
- m_disk_timestamp = m_timestamp;
+ if(version < 29) {
+ // Timestamp
+ TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())
+ <<": Timestamp"<<std::endl);
+ setTimestampNoChangedFlag(readU32(is));
+ m_disk_timestamp = m_timestamp;
+
+ // Node/id mapping
+ TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())
+ <<": NameIdMapping"<<std::endl);
+ nimap.deSerialize(is);
+ }
// Dynamically re-set ids based on node names
- TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())
- <<": NameIdMapping"<<std::endl);
- NameIdMapping nimap;
- nimap.deSerialize(is);
correctBlockNodeIds(&nimap, data, m_gamedef);
if(version >= 25){
diff --git a/src/mapblock.h b/src/mapblock.h
index 7b82301e9..a86db7b70 100644
--- a/src/mapblock.h
+++ b/src/mapblock.h
@@ -140,7 +140,7 @@ public:
//// Flags
////
- inline bool isDummy()
+ inline bool isDummy() const
{
return !data;
}
@@ -364,20 +364,6 @@ public:
}
////
- //// Miscellaneous stuff
- ////
-
- /*
- Tries to measure ground level.
- Return value:
- -1 = only air
- -2 = only ground
- -3 = random fail
- 0...MAP_BLOCKSIZE-1 = ground level
- */
- s16 getGroundLevel(v2s16 p2d);
-
- ////
//// Timestamp (see m_timestamp)
////
@@ -473,7 +459,7 @@ public:
// These don't write or read version by itself
// Set disk to true for on-disk format, false for over-the-network format
// Precondition: version >= SER_FMT_VER_LOWEST_WRITE
- void serialize(std::ostream &os, u8 version, bool disk, int compression_level);
+ void serialize(std::ostream &result, u8 version, bool disk, int compression_level);
// If disk == true: In addition to doing other things, will add
// unknown blocks from id-name mapping to wndef
void deSerialize(std::istream &is, u8 version, bool disk);
@@ -615,7 +601,7 @@ typedef std::vector<MapBlock*> MapBlockVect;
inline bool objectpos_over_limit(v3f p)
{
- const float max_limit_bs = MAX_MAP_GENERATION_LIMIT * BS;
+ const float max_limit_bs = (MAX_MAP_GENERATION_LIMIT + 0.5f) * BS;
return p.X < -max_limit_bs ||
p.X > max_limit_bs ||
p.Y < -max_limit_bs ||
diff --git a/src/mapgen/mapgen.cpp b/src/mapgen/mapgen.cpp
index e0dfd2d71..d767bd264 100644
--- a/src/mapgen/mapgen.cpp
+++ b/src/mapgen/mapgen.cpp
@@ -595,7 +595,8 @@ MapgenBasic::MapgenBasic(int mapgenid, MapgenParams *params, EmergeParams *emerg
this->heightmap = new s16[csize.X * csize.Z];
//// Initialize biome generator
- biomegen = m_bmgr->createBiomeGen(BIOMEGEN_ORIGINAL, params->bparams, csize);
+ biomegen = emerge->biomegen;
+ biomegen->assertChunkSize(csize);
biomemap = biomegen->biomemap;
//// Look up some commonly used content
@@ -621,7 +622,6 @@ MapgenBasic::MapgenBasic(int mapgenid, MapgenParams *params, EmergeParams *emerg
MapgenBasic::~MapgenBasic()
{
- delete biomegen;
delete []heightmap;
delete m_emerge; // destroying EmergeParams is our responsibility
@@ -1018,10 +1018,11 @@ MapgenParams::~MapgenParams()
void MapgenParams::readParams(const Settings *settings)
{
- std::string seed_str;
- const char *seed_name = (settings == g_settings) ? "fixed_map_seed" : "seed";
+ // should always be used via MapSettingsManager
+ assert(settings != g_settings);
- if (settings->getNoEx(seed_name, seed_str)) {
+ std::string seed_str;
+ if (settings->getNoEx("seed", seed_str)) {
if (!seed_str.empty())
seed = read_seed(seed_str.c_str());
else
diff --git a/src/mapgen/mapgen_v6.cpp b/src/mapgen/mapgen_v6.cpp
index bce9cee81..a418acace 100644
--- a/src/mapgen/mapgen_v6.cpp
+++ b/src/mapgen/mapgen_v6.cpp
@@ -360,19 +360,6 @@ int MapgenV6::getSpawnLevelAtPoint(v2s16 p)
//////////////////////// Noise functions
-float MapgenV6::getMudAmount(v2s16 p)
-{
- int index = (p.Y - node_min.Z) * ystride + (p.X - node_min.X);
- return getMudAmount(index);
-}
-
-
-bool MapgenV6::getHaveBeach(v2s16 p)
-{
- int index = (p.Y - node_min.Z) * ystride + (p.X - node_min.X);
- return getHaveBeach(index);
-}
-
BiomeV6Type MapgenV6::getBiome(v2s16 p)
{
diff --git a/src/mapgen/mapgen_v6.h b/src/mapgen/mapgen_v6.h
index a6e6da8c6..b0eb67893 100644
--- a/src/mapgen/mapgen_v6.h
+++ b/src/mapgen/mapgen_v6.h
@@ -154,9 +154,7 @@ public:
float getHumidity(v2s16 p);
float getTreeAmount(v2s16 p);
bool getHaveAppleTree(v2s16 p);
- float getMudAmount(v2s16 p);
- virtual float getMudAmount(int index);
- bool getHaveBeach(v2s16 p);
+ float getMudAmount(int index);
bool getHaveBeach(int index);
BiomeV6Type getBiome(v2s16 p);
BiomeV6Type getBiome(int index, v2s16 p);
diff --git a/src/mapgen/mapgen_valleys.cpp b/src/mapgen/mapgen_valleys.cpp
index c4234857e..80a99b1f0 100644
--- a/src/mapgen/mapgen_valleys.cpp
+++ b/src/mapgen/mapgen_valleys.cpp
@@ -57,7 +57,8 @@ FlagDesc flagdesc_mapgen_valleys[] = {
MapgenValleys::MapgenValleys(MapgenValleysParams *params, EmergeParams *emerge)
: MapgenBasic(MAPGEN_VALLEYS, params, emerge)
{
- // NOTE: MapgenValleys has a hard dependency on BiomeGenOriginal
+ FATAL_ERROR_IF(biomegen->getType() != BIOMEGEN_ORIGINAL,
+ "MapgenValleys has a hard dependency on BiomeGenOriginal");
m_bgen = (BiomeGenOriginal *)biomegen;
spflags = params->spflags;
diff --git a/src/mapgen/mg_biome.cpp b/src/mapgen/mg_biome.cpp
index 610c38594..f08cc190f 100644
--- a/src/mapgen/mg_biome.cpp
+++ b/src/mapgen/mg_biome.cpp
@@ -101,71 +101,6 @@ BiomeManager *BiomeManager::clone() const
return mgr;
}
-
-// For BiomeGen type 'BiomeGenOriginal'
-float BiomeManager::getHeatAtPosOriginal(v3s16 pos, NoiseParams &np_heat,
- NoiseParams &np_heat_blend, u64 seed) const
-{
- return
- NoisePerlin2D(&np_heat, pos.X, pos.Z, seed) +
- NoisePerlin2D(&np_heat_blend, pos.X, pos.Z, seed);
-}
-
-
-// For BiomeGen type 'BiomeGenOriginal'
-float BiomeManager::getHumidityAtPosOriginal(v3s16 pos, NoiseParams &np_humidity,
- NoiseParams &np_humidity_blend, u64 seed) const
-{
- return
- NoisePerlin2D(&np_humidity, pos.X, pos.Z, seed) +
- NoisePerlin2D(&np_humidity_blend, pos.X, pos.Z, seed);
-}
-
-
-// For BiomeGen type 'BiomeGenOriginal'
-const Biome *BiomeManager::getBiomeFromNoiseOriginal(float heat,
- float humidity, v3s16 pos) const
-{
- Biome *biome_closest = nullptr;
- Biome *biome_closest_blend = nullptr;
- float dist_min = FLT_MAX;
- float dist_min_blend = FLT_MAX;
-
- for (size_t i = 1; i < getNumObjects(); i++) {
- Biome *b = (Biome *)getRaw(i);
- if (!b ||
- pos.Y < b->min_pos.Y || pos.Y > b->max_pos.Y + b->vertical_blend ||
- pos.X < b->min_pos.X || pos.X > b->max_pos.X ||
- pos.Z < b->min_pos.Z || pos.Z > b->max_pos.Z)
- continue;
-
- float d_heat = heat - b->heat_point;
- float d_humidity = humidity - b->humidity_point;
- float dist = (d_heat * d_heat) + (d_humidity * d_humidity);
-
- if (pos.Y <= b->max_pos.Y) { // Within y limits of biome b
- if (dist < dist_min) {
- dist_min = dist;
- biome_closest = b;
- }
- } else if (dist < dist_min_blend) { // Blend area above biome b
- dist_min_blend = dist;
- biome_closest_blend = b;
- }
- }
-
- const u64 seed = pos.Y + (heat + humidity) * 0.9f;
- PcgRandom rng(seed);
-
- if (biome_closest_blend && dist_min_blend <= dist_min &&
- rng.range(0, biome_closest_blend->vertical_blend) >=
- pos.Y - biome_closest_blend->max_pos.Y)
- return biome_closest_blend;
-
- return (biome_closest) ? biome_closest : (Biome *)getRaw(BIOME_NONE);
-}
-
-
////////////////////////////////////////////////////////////////////////////////
void BiomeParamsOriginal::readParams(const Settings *settings)
@@ -189,7 +124,7 @@ void BiomeParamsOriginal::writeParams(Settings *settings) const
////////////////////////////////////////////////////////////////////////////////
BiomeGenOriginal::BiomeGenOriginal(BiomeManager *biomemgr,
- BiomeParamsOriginal *params, v3s16 chunksize)
+ const BiomeParamsOriginal *params, v3s16 chunksize)
{
m_bmgr = biomemgr;
m_params = params;
@@ -224,17 +159,26 @@ BiomeGenOriginal::~BiomeGenOriginal()
delete noise_humidity_blend;
}
-// Only usable in a mapgen thread
-Biome *BiomeGenOriginal::calcBiomeAtPoint(v3s16 pos) const
+BiomeGen *BiomeGenOriginal::clone(BiomeManager *biomemgr) const
+{
+ return new BiomeGenOriginal(biomemgr, m_params, m_csize);
+}
+
+float BiomeGenOriginal::calcHeatAtPoint(v3s16 pos) const
{
- float heat =
- NoisePerlin2D(&m_params->np_heat, pos.X, pos.Z, m_params->seed) +
+ return NoisePerlin2D(&m_params->np_heat, pos.X, pos.Z, m_params->seed) +
NoisePerlin2D(&m_params->np_heat_blend, pos.X, pos.Z, m_params->seed);
- float humidity =
- NoisePerlin2D(&m_params->np_humidity, pos.X, pos.Z, m_params->seed) +
+}
+
+float BiomeGenOriginal::calcHumidityAtPoint(v3s16 pos) const
+{
+ return NoisePerlin2D(&m_params->np_humidity, pos.X, pos.Z, m_params->seed) +
NoisePerlin2D(&m_params->np_humidity_blend, pos.X, pos.Z, m_params->seed);
+}
- return calcBiomeFromNoise(heat, humidity, pos);
+Biome *BiomeGenOriginal::calcBiomeAtPoint(v3s16 pos) const
+{
+ return calcBiomeFromNoise(calcHeatAtPoint(pos), calcHumidityAtPoint(pos), pos);
}
diff --git a/src/mapgen/mg_biome.h b/src/mapgen/mg_biome.h
index be4cfea4d..c85afc3a0 100644
--- a/src/mapgen/mg_biome.h
+++ b/src/mapgen/mg_biome.h
@@ -97,6 +97,15 @@ public:
virtual BiomeGenType getType() const = 0;
+ // Clone this BiomeGen and set a the new BiomeManager to be used by the copy
+ virtual BiomeGen *clone(BiomeManager *biomemgr) const = 0;
+
+ // Check that the internal chunk size is what the mapgen expects, just to be sure.
+ inline void assertChunkSize(v3s16 expect) const
+ {
+ FATAL_ERROR_IF(m_csize != expect, "Chunk size mismatches");
+ }
+
// Calculates the biome at the exact position provided. This function can
// be called at any time, but may be less efficient than the latter methods,
// depending on implementation.
@@ -158,12 +167,18 @@ struct BiomeParamsOriginal : public BiomeParams {
class BiomeGenOriginal : public BiomeGen {
public:
BiomeGenOriginal(BiomeManager *biomemgr,
- BiomeParamsOriginal *params, v3s16 chunksize);
+ const BiomeParamsOriginal *params, v3s16 chunksize);
virtual ~BiomeGenOriginal();
BiomeGenType getType() const { return BIOMEGEN_ORIGINAL; }
+ BiomeGen *clone(BiomeManager *biomemgr) const;
+
+ // Slower, meant for Script API use
+ float calcHeatAtPoint(v3s16 pos) const;
+ float calcHumidityAtPoint(v3s16 pos) const;
Biome *calcBiomeAtPoint(v3s16 pos) const;
+
void calcBiomeNoise(v3s16 pmin);
biome_t *getBiomes(s16 *heightmap, v3s16 pmin);
@@ -176,7 +191,7 @@ public:
float *humidmap;
private:
- BiomeParamsOriginal *m_params;
+ const BiomeParamsOriginal *m_params;
Noise *noise_heat;
Noise *noise_humidity;
@@ -229,14 +244,6 @@ public:
virtual void clear();
- // For BiomeGen type 'BiomeGenOriginal'
- float getHeatAtPosOriginal(v3s16 pos, NoiseParams &np_heat,
- NoiseParams &np_heat_blend, u64 seed) const;
- float getHumidityAtPosOriginal(v3s16 pos, NoiseParams &np_humidity,
- NoiseParams &np_humidity_blend, u64 seed) const;
- const Biome *getBiomeFromNoiseOriginal(float heat, float humidity,
- v3s16 pos) const;
-
private:
BiomeManager() {};
diff --git a/src/mapgen/mg_ore.h b/src/mapgen/mg_ore.h
index a58fa9bfe..a757fa6d0 100644
--- a/src/mapgen/mg_ore.h
+++ b/src/mapgen/mg_ore.h
@@ -85,7 +85,7 @@ class OreScatter : public Ore {
public:
OreScatter() : Ore(false) {}
- ObjDef *clone() const;
+ ObjDef *clone() const override;
void generate(MMVManip *vm, int mapseed, u32 blockseed,
v3s16 nmin, v3s16 nmax, biome_t *biomemap) override;
@@ -95,7 +95,7 @@ class OreSheet : public Ore {
public:
OreSheet() : Ore(true) {}
- ObjDef *clone() const;
+ ObjDef *clone() const override;
u16 column_height_min;
u16 column_height_max;
@@ -107,7 +107,7 @@ public:
class OrePuff : public Ore {
public:
- ObjDef *clone() const;
+ ObjDef *clone() const override;
NoiseParams np_puff_top;
NoiseParams np_puff_bottom;
@@ -123,7 +123,7 @@ public:
class OreBlob : public Ore {
public:
- ObjDef *clone() const;
+ ObjDef *clone() const override;
OreBlob() : Ore(true) {}
void generate(MMVManip *vm, int mapseed, u32 blockseed,
@@ -132,7 +132,7 @@ public:
class OreVein : public Ore {
public:
- ObjDef *clone() const;
+ ObjDef *clone() const override;
float random_factor;
Noise *noise2 = nullptr;
@@ -147,7 +147,7 @@ public:
class OreStratum : public Ore {
public:
- ObjDef *clone() const;
+ ObjDef *clone() const override;
NoiseParams np_stratum_thickness;
Noise *noise_stratum_thickness = nullptr;
diff --git a/src/mapgen/mg_schematic.cpp b/src/mapgen/mg_schematic.cpp
index e70e97e48..b9ba70302 100644
--- a/src/mapgen/mg_schematic.cpp
+++ b/src/mapgen/mg_schematic.cpp
@@ -76,10 +76,6 @@ void SchematicManager::clear()
///////////////////////////////////////////////////////////////////////////////
-Schematic::Schematic()
-= default;
-
-
Schematic::~Schematic()
{
delete []schemdata;
@@ -108,13 +104,19 @@ ObjDef *Schematic::clone() const
void Schematic::resolveNodeNames()
{
+ c_nodes.clear();
getIdsFromNrBacklog(&c_nodes, true, CONTENT_AIR);
size_t bufsize = size.X * size.Y * size.Z;
for (size_t i = 0; i != bufsize; i++) {
content_t c_original = schemdata[i].getContent();
- content_t c_new = c_nodes[c_original];
- schemdata[i].setContent(c_new);
+ if (c_original >= c_nodes.size()) {
+ errorstream << "Corrupt schematic. name=\"" << name
+ << "\" at index " << i << std::endl;
+ c_original = 0;
+ }
+ // Unfold condensed ID layout to content_t
+ schemdata[i].setContent(c_nodes[c_original]);
}
}
@@ -279,8 +281,7 @@ void Schematic::placeOnMap(ServerMap *map, v3s16 p, u32 flags,
}
-bool Schematic::deserializeFromMts(std::istream *is,
- std::vector<std::string> *names)
+bool Schematic::deserializeFromMts(std::istream *is)
{
std::istream &ss = *is;
content_t cignore = CONTENT_IGNORE;
@@ -312,6 +313,8 @@ bool Schematic::deserializeFromMts(std::istream *is,
slice_probs[y] = (version >= 3) ? readU8(ss) : MTSCHEM_PROB_ALWAYS_OLD;
//// Read node names
+ NodeResolver::reset();
+
u16 nidmapcount = readU16(ss);
for (int i = 0; i != nidmapcount; i++) {
std::string name = deSerializeString16(ss);
@@ -324,16 +327,21 @@ bool Schematic::deserializeFromMts(std::istream *is,
have_cignore = true;
}
- names->push_back(name);
+ m_nodenames.push_back(name);
}
+ // Prepare for node resolver
+ m_nnlistsizes.push_back(m_nodenames.size());
+
//// Read node data
size_t nodecount = size.X * size.Y * size.Z;
delete []schemdata;
schemdata = new MapNode[nodecount];
- MapNode::deSerializeBulk(ss, SER_FMT_VER_HIGHEST_READ, schemdata,
+ std::stringstream d_ss(std::ios_base::binary | std::ios_base::in | std::ios_base::out);
+ decompress(ss, d_ss, MTSCHEM_MAPNODE_SER_FMT_VER);
+ MapNode::deSerializeBulk(d_ss, MTSCHEM_MAPNODE_SER_FMT_VER, schemdata,
nodecount, 2, 2);
// Fix probability values for nodes that were ignore; removed in v2
@@ -358,9 +366,11 @@ bool Schematic::deserializeFromMts(std::istream *is,
}
-bool Schematic::serializeToMts(std::ostream *os,
- const std::vector<std::string> &names) const
+bool Schematic::serializeToMts(std::ostream *os) const
{
+ // Nodes must not be resolved (-> condensed)
+ // checking here is not possible because "schemdata" might be temporary.
+
std::ostream &ss = *os;
writeU32(ss, MTSCHEM_FILE_SIGNATURE); // signature
@@ -370,20 +380,21 @@ bool Schematic::serializeToMts(std::ostream *os,
for (int y = 0; y != size.Y; y++) // Y slice probabilities
writeU8(ss, slice_probs[y]);
- writeU16(ss, names.size()); // name count
- for (size_t i = 0; i != names.size(); i++)
- ss << serializeString16(names[i]); // node names
+ writeU16(ss, m_nodenames.size()); // name count
+ for (size_t i = 0; i != m_nodenames.size(); i++) {
+ ss << serializeString16(m_nodenames[i]); // node names
+ }
// compressed bulk node data
- MapNode::serializeBulk(ss, SER_FMT_VER_HIGHEST_WRITE,
- schemdata, size.X * size.Y * size.Z, 2, 2, -1);
+ SharedBuffer<u8> buf = MapNode::serializeBulk(MTSCHEM_MAPNODE_SER_FMT_VER,
+ schemdata, size.X * size.Y * size.Z, 2, 2);
+ compress(buf, ss, MTSCHEM_MAPNODE_SER_FMT_VER);
return true;
}
-bool Schematic::serializeToLua(std::ostream *os,
- const std::vector<std::string> &names, bool use_comments,
+bool Schematic::serializeToLua(std::ostream *os, bool use_comments,
u32 indent_spaces) const
{
std::ostream &ss = *os;
@@ -392,6 +403,9 @@ bool Schematic::serializeToLua(std::ostream *os,
if (indent_spaces > 0)
indent.assign(indent_spaces, ' ');
+ bool resolve_done = isResolveDone();
+ FATAL_ERROR_IF(resolve_done && !m_ndef, "serializeToLua: NodeDefManager is required");
+
//// Write header
{
ss << "schematic = {" << std::endl;
@@ -436,9 +450,22 @@ bool Schematic::serializeToLua(std::ostream *os,
u8 probability = schemdata[i].param1 & MTSCHEM_PROB_MASK;
bool force_place = schemdata[i].param1 & MTSCHEM_FORCE_PLACE;
- ss << indent << indent << "{"
- << "name=\"" << names[schemdata[i].getContent()]
- << "\", prob=" << (u16)probability * 2
+ // After node resolving: real content_t, lookup using NodeDefManager
+ // Prior node resolving: condensed ID, lookup using m_nodenames
+ content_t c = schemdata[i].getContent();
+
+ ss << indent << indent << "{" << "name=\"";
+
+ if (!resolve_done) {
+ // Prior node resolving (eg. direct schematic load)
+ FATAL_ERROR_IF(c >= m_nodenames.size(), "Invalid node list");
+ ss << m_nodenames[c];
+ } else {
+ // After node resolving (eg. biome decoration)
+ ss << m_ndef->get(c).name;
+ }
+
+ ss << "\", prob=" << (u16)probability * 2
<< ", param2=" << (u16)schemdata[i].param2;
if (force_place)
@@ -467,25 +494,24 @@ bool Schematic::loadSchematicFromFile(const std::string &filename,
return false;
}
- size_t origsize = m_nodenames.size();
- if (!deserializeFromMts(&is, &m_nodenames))
- return false;
+ if (!m_ndef)
+ m_ndef = ndef;
- m_nnlistsizes.push_back(m_nodenames.size() - origsize);
+ if (!deserializeFromMts(&is))
+ return false;
name = filename;
if (replace_names) {
- for (size_t i = origsize; i < m_nodenames.size(); i++) {
- std::string &node_name = m_nodenames[i];
+ for (std::string &node_name : m_nodenames) {
StringMap::iterator it = replace_names->find(node_name);
if (it != replace_names->end())
node_name = it->second;
}
}
- if (ndef)
- ndef->pendNodeResolve(this);
+ if (m_ndef)
+ m_ndef->pendNodeResolve(this);
return true;
}
@@ -494,33 +520,26 @@ bool Schematic::loadSchematicFromFile(const std::string &filename,
bool Schematic::saveSchematicToFile(const std::string &filename,
const NodeDefManager *ndef)
{
- MapNode *orig_schemdata = schemdata;
- std::vector<std::string> ndef_nodenames;
- std::vector<std::string> *names;
+ Schematic *schem = this;
- if (m_resolve_done && ndef == NULL)
- ndef = m_ndef;
+ bool needs_condense = isResolveDone();
- if (ndef) {
- names = &ndef_nodenames;
+ if (!m_ndef)
+ m_ndef = ndef;
- u32 volume = size.X * size.Y * size.Z;
- schemdata = new MapNode[volume];
- for (u32 i = 0; i != volume; i++)
- schemdata[i] = orig_schemdata[i];
+ if (needs_condense) {
+ if (!m_ndef)
+ return false;
- generate_nodelist_and_update_ids(schemdata, volume, names, ndef);
- } else { // otherwise, use the names we have on hand in the list
- names = &m_nodenames;
+ schem = (Schematic *)this->clone();
+ schem->condenseContentIds();
}
std::ostringstream os(std::ios_base::binary);
- bool status = serializeToMts(&os, *names);
+ bool status = schem->serializeToMts(&os);
- if (ndef) {
- delete []schemdata;
- schemdata = orig_schemdata;
- }
+ if (needs_condense)
+ delete schem;
if (!status)
return false;
@@ -556,6 +575,10 @@ bool Schematic::getSchematicFromMap(Map *map, v3s16 p1, v3s16 p2)
}
delete vm;
+
+ // Reset and mark as complete
+ NodeResolver::reset(true);
+
return true;
}
@@ -578,32 +601,36 @@ void Schematic::applyProbabilities(v3s16 p0,
}
for (size_t i = 0; i != splist->size(); i++) {
- s16 y = (*splist)[i].first - p0.Y;
- slice_probs[y] = (*splist)[i].second;
+ s16 slice = (*splist)[i].first;
+ if (slice < size.Y)
+ slice_probs[slice] = (*splist)[i].second;
}
}
-void generate_nodelist_and_update_ids(MapNode *nodes, size_t nodecount,
- std::vector<std::string> *usednodes, const NodeDefManager *ndef)
+void Schematic::condenseContentIds()
{
std::unordered_map<content_t, content_t> nodeidmap;
content_t numids = 0;
+ // Reset node resolve fields
+ NodeResolver::reset();
+
+ size_t nodecount = size.X * size.Y * size.Z;
for (size_t i = 0; i != nodecount; i++) {
content_t id;
- content_t c = nodes[i].getContent();
+ content_t c = schemdata[i].getContent();
- std::unordered_map<content_t, content_t>::const_iterator it = nodeidmap.find(c);
+ auto it = nodeidmap.find(c);
if (it == nodeidmap.end()) {
id = numids;
numids++;
- usednodes->push_back(ndef->get(c).name);
- nodeidmap.insert(std::make_pair(c, id));
+ m_nodenames.push_back(m_ndef->get(c).name);
+ nodeidmap.emplace(std::make_pair(c, id));
} else {
id = it->second;
}
- nodes[i].setContent(id);
+ schemdata[i].setContent(id);
}
}
diff --git a/src/mapgen/mg_schematic.h b/src/mapgen/mg_schematic.h
index 6b31251b6..9189bb3a7 100644
--- a/src/mapgen/mg_schematic.h
+++ b/src/mapgen/mg_schematic.h
@@ -70,6 +70,7 @@ class Server;
#define MTSCHEM_FILE_SIGNATURE 0x4d54534d // 'MTSM'
#define MTSCHEM_FILE_VER_HIGHEST_READ 4
#define MTSCHEM_FILE_VER_HIGHEST_WRITE 4
+#define MTSCHEM_MAPNODE_SER_FMT_VER 28 // Fixed serialization version for schematics since these still need to use Zlib
#define MTSCHEM_PROB_MASK 0x7F
@@ -92,7 +93,7 @@ enum SchematicFormatType {
class Schematic : public ObjDef, public NodeResolver {
public:
- Schematic();
+ Schematic() = default;
virtual ~Schematic();
ObjDef *clone() const;
@@ -105,11 +106,9 @@ public:
const NodeDefManager *ndef);
bool getSchematicFromMap(Map *map, v3s16 p1, v3s16 p2);
- bool deserializeFromMts(std::istream *is, std::vector<std::string> *names);
- bool serializeToMts(std::ostream *os,
- const std::vector<std::string> &names) const;
- bool serializeToLua(std::ostream *os, const std::vector<std::string> &names,
- bool use_comments, u32 indent_spaces) const;
+ bool deserializeFromMts(std::istream *is);
+ bool serializeToMts(std::ostream *os) const;
+ bool serializeToLua(std::ostream *os, bool use_comments, u32 indent_spaces) const;
void blitToVManip(MMVManip *vm, v3s16 p, Rotation rot, bool force_place);
bool placeOnVManip(MMVManip *vm, v3s16 p, u32 flags, Rotation rot, bool force_place);
@@ -124,6 +123,10 @@ public:
v3s16 size;
MapNode *schemdata = nullptr;
u8 *slice_probs = nullptr;
+
+private:
+ // Counterpart to the node resolver: Condense content_t to a sequential "m_nodenames" list
+ void condenseContentIds();
};
class SchematicManager : public ObjDefManager {
@@ -151,5 +154,3 @@ private:
Server *m_server;
};
-void generate_nodelist_and_update_ids(MapNode *nodes, size_t nodecount,
- std::vector<std::string> *usednodes, const NodeDefManager *ndef);
diff --git a/src/mapnode.cpp b/src/mapnode.cpp
index 0551f3b6f..73bd620fb 100644
--- a/src/mapnode.cpp
+++ b/src/mapnode.cpp
@@ -159,8 +159,13 @@ u8 MapNode::getWallMounted(const NodeDefManager *nodemgr) const
{
const ContentFeatures &f = nodemgr->get(*this);
if (f.param_type_2 == CPT2_WALLMOUNTED ||
- f.param_type_2 == CPT2_COLORED_WALLMOUNTED)
+ f.param_type_2 == CPT2_COLORED_WALLMOUNTED) {
return getParam2() & 0x07;
+ } else if (f.drawtype == NDT_SIGNLIKE || f.drawtype == NDT_TORCHLIKE ||
+ f.drawtype == NDT_PLANTLIKE ||
+ f.drawtype == NDT_PLANTLIKE_ROOTED) {
+ return 1;
+ }
return 0;
}
@@ -177,6 +182,16 @@ v3s16 MapNode::getWallMountedDir(const NodeDefManager *nodemgr) const
}
}
+u8 MapNode::getDegRotate(const NodeDefManager *nodemgr) const
+{
+ const ContentFeatures &f = nodemgr->get(*this);
+ if (f.param_type_2 == CPT2_DEGROTATE)
+ return getParam2() % 240;
+ if (f.param_type_2 == CPT2_COLORED_DEGROTATE)
+ return 10 * ((getParam2() & 0x1F) % 24);
+ return 0;
+}
+
void MapNode::rotateAlongYAxis(const NodeDefManager *nodemgr, Rotation rot)
{
ContentParamType2 cpt2 = nodemgr->get(*this).param_type_2;
@@ -230,6 +245,17 @@ void MapNode::rotateAlongYAxis(const NodeDefManager *nodemgr, Rotation rot)
Rotation oldrot = wallmounted_to_rot[wmountface - 2];
param2 &= ~7;
param2 |= rot_to_wallmounted[(oldrot - rot) & 3];
+ } else if (cpt2 == CPT2_DEGROTATE) {
+ int angle = param2; // in 1.5°
+ angle += 60 * rot; // don’t do that on u8
+ angle %= 240;
+ param2 = angle;
+ } else if (cpt2 == CPT2_COLORED_DEGROTATE) {
+ int angle = param2 & 0x1F; // in 15°
+ int color = param2 & 0xE0;
+ angle += 6 * rot;
+ angle %= 24;
+ param2 = color | angle;
}
}
@@ -704,9 +730,10 @@ void MapNode::deSerialize(u8 *source, u8 version)
}
}
}
-void MapNode::serializeBulk(std::ostream &os, int version,
+
+SharedBuffer<u8> MapNode::serializeBulk(int version,
const MapNode *nodes, u32 nodecount,
- u8 content_width, u8 params_width, int compression_level)
+ u8 content_width, u8 params_width)
{
if (!ser_ver_supported(version))
throw VersionMismatchException("ERROR: MapNode format not supported");
@@ -720,8 +747,7 @@ void MapNode::serializeBulk(std::ostream &os, int version,
throw SerializationError("MapNode::serializeBulk: serialization to "
"version < 24 not possible");
- size_t databuf_size = nodecount * (content_width + params_width);
- u8 *databuf = new u8[databuf_size];
+ SharedBuffer<u8> databuf(nodecount * (content_width + params_width));
u32 start1 = content_width * nodecount;
u32 start2 = (content_width + 1) * nodecount;
@@ -732,14 +758,7 @@ void MapNode::serializeBulk(std::ostream &os, int version,
writeU8(&databuf[start1 + i], nodes[i].param1);
writeU8(&databuf[start2 + i], nodes[i].param2);
}
-
- /*
- Compress data to output stream
- */
-
- compressZlib(databuf, databuf_size, os, compression_level);
-
- delete [] databuf;
+ return databuf;
}
// Deserialize bulk node data
@@ -755,15 +774,10 @@ void MapNode::deSerializeBulk(std::istream &is, int version,
|| params_width != 2)
FATAL_ERROR("Deserialize bulk node data error");
- // Uncompress or read data
- u32 len = nodecount * (content_width + params_width);
- std::ostringstream os(std::ios_base::binary);
- decompressZlib(is, os);
- std::string s = os.str();
- if(s.size() != len)
- throw SerializationError("deSerializeBulkNodes: "
- "decompress resulted in invalid size");
- const u8 *databuf = reinterpret_cast<const u8*>(s.c_str());
+ // read data
+ const u32 len = nodecount * (content_width + params_width);
+ Buffer<u8> databuf(len);
+ is.read(reinterpret_cast<char*>(*databuf), len);
// Deserialize content
if(content_width == 1)
diff --git a/src/mapnode.h b/src/mapnode.h
index a9ae63ba3..afd3a96be 100644
--- a/src/mapnode.h
+++ b/src/mapnode.h
@@ -21,6 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "irrlichttypes_bloated.h"
#include "light.h"
+#include "util/pointer.h"
#include <string>
#include <vector>
@@ -240,6 +241,9 @@ struct MapNode
u8 getWallMounted(const NodeDefManager *nodemgr) const;
v3s16 getWallMountedDir(const NodeDefManager *nodemgr) const;
+ /// @returns Rotation in range 0–239 (in 1.5° steps)
+ u8 getDegRotate(const NodeDefManager *nodemgr) const;
+
void rotateAlongYAxis(const NodeDefManager *nodemgr, Rotation rot);
/*!
@@ -290,9 +294,9 @@ struct MapNode
// content_width = the number of bytes of content per node
// params_width = the number of bytes of params per node
// compressed = true to zlib-compress output
- static void serializeBulk(std::ostream &os, int version,
+ static SharedBuffer<u8> serializeBulk(int version,
const MapNode *nodes, u32 nodecount,
- u8 content_width, u8 params_width, int compression_level);
+ u8 content_width, u8 params_width);
static void deSerializeBulk(std::istream &is, int version,
MapNode *nodes, u32 nodecount,
u8 content_width, u8 params_width);
diff --git a/src/network/address.cpp b/src/network/address.cpp
index 05678aa62..90e561802 100644
--- a/src/network/address.cpp
+++ b/src/network/address.cpp
@@ -87,38 +87,31 @@ Address::Address(const IPv6AddressBytes *ipv6_bytes, u16 port)
setPort(port);
}
-// Equality (address family, address and port must be equal)
-bool Address::operator==(const Address &address)
+// Equality (address family, IP and port must be equal)
+bool Address::operator==(const Address &other)
{
- if (address.m_addr_family != m_addr_family || address.m_port != m_port)
+ if (other.m_addr_family != m_addr_family || other.m_port != m_port)
return false;
if (m_addr_family == AF_INET) {
- return m_address.ipv4.sin_addr.s_addr ==
- address.m_address.ipv4.sin_addr.s_addr;
+ return m_address.ipv4.s_addr == other.m_address.ipv4.s_addr;
}
if (m_addr_family == AF_INET6) {
- return memcmp(m_address.ipv6.sin6_addr.s6_addr,
- address.m_address.ipv6.sin6_addr.s6_addr, 16) == 0;
+ return memcmp(m_address.ipv6.s6_addr,
+ other.m_address.ipv6.s6_addr, 16) == 0;
}
return false;
}
-bool Address::operator!=(const Address &address)
-{
- return !(*this == address);
-}
-
void Address::Resolve(const char *name)
{
if (!name || name[0] == 0) {
- if (m_addr_family == AF_INET) {
- setAddress((u32)0);
- } else if (m_addr_family == AF_INET6) {
- setAddress((IPv6AddressBytes *)0);
- }
+ if (m_addr_family == AF_INET)
+ setAddress(static_cast<u32>(0));
+ else if (m_addr_family == AF_INET6)
+ setAddress(static_cast<IPv6AddressBytes*>(nullptr));
return;
}
@@ -126,9 +119,6 @@ void Address::Resolve(const char *name)
memset(&hints, 0, sizeof(hints));
// Setup hints
- hints.ai_socktype = 0;
- hints.ai_protocol = 0;
- hints.ai_flags = 0;
if (g_settings->getBool("enable_ipv6")) {
// AF_UNSPEC allows both IPv6 and IPv4 addresses to be returned
hints.ai_family = AF_UNSPEC;
@@ -145,14 +135,13 @@ void Address::Resolve(const char *name)
if (resolved->ai_family == AF_INET) {
struct sockaddr_in *t = (struct sockaddr_in *)resolved->ai_addr;
m_addr_family = AF_INET;
- m_address.ipv4 = *t;
+ m_address.ipv4 = t->sin_addr;
} else if (resolved->ai_family == AF_INET6) {
struct sockaddr_in6 *t = (struct sockaddr_in6 *)resolved->ai_addr;
m_addr_family = AF_INET6;
- m_address.ipv6 = *t;
+ m_address.ipv6 = t->sin6_addr;
} else {
- freeaddrinfo(resolved);
- throw ResolveError("");
+ m_addr_family = 0;
}
freeaddrinfo(resolved);
}
@@ -163,47 +152,37 @@ std::string Address::serializeString() const
// windows XP doesnt have inet_ntop, maybe use better func
#ifdef _WIN32
if (m_addr_family == AF_INET) {
- u8 a, b, c, d;
- u32 addr;
- addr = ntohl(m_address.ipv4.sin_addr.s_addr);
- a = (addr & 0xFF000000) >> 24;
- b = (addr & 0x00FF0000) >> 16;
- c = (addr & 0x0000FF00) >> 8;
- d = (addr & 0x000000FF);
- return itos(a) + "." + itos(b) + "." + itos(c) + "." + itos(d);
+ return inet_ntoa(m_address.ipv4);
} else if (m_addr_family == AF_INET6) {
std::ostringstream os;
+ os << std::hex;
for (int i = 0; i < 16; i += 2) {
- u16 section = (m_address.ipv6.sin6_addr.s6_addr[i] << 8) |
- (m_address.ipv6.sin6_addr.s6_addr[i + 1]);
- os << std::hex << section;
+ u16 section = (m_address.ipv6.s6_addr[i] << 8) |
+ (m_address.ipv6.s6_addr[i + 1]);
+ os << section;
if (i < 14)
os << ":";
}
return os.str();
- } else
- return std::string("");
+ } else {
+ return "";
+ }
#else
char str[INET6_ADDRSTRLEN];
- if (inet_ntop(m_addr_family,
- (m_addr_family == AF_INET)
- ? (void *)&(m_address.ipv4.sin_addr)
- : (void *)&(m_address.ipv6.sin6_addr),
- str, INET6_ADDRSTRLEN) == NULL) {
- return std::string("");
- }
- return std::string(str);
+ if (inet_ntop(m_addr_family, (void*) &m_address, str, sizeof(str)) == nullptr)
+ return "";
+ return str;
#endif
}
-struct sockaddr_in Address::getAddress() const
+struct in_addr Address::getAddress() const
{
- return m_address.ipv4; // NOTE: NO PORT INCLUDED, use getPort()
+ return m_address.ipv4;
}
-struct sockaddr_in6 Address::getAddress6() const
+struct in6_addr Address::getAddress6() const
{
- return m_address.ipv6; // NOTE: NO PORT INCLUDED, use getPort()
+ return m_address.ipv6;
}
u16 Address::getPort() const
@@ -211,52 +190,39 @@ u16 Address::getPort() const
return m_port;
}
-int Address::getFamily() const
-{
- return m_addr_family;
-}
-
-bool Address::isIPv6() const
-{
- return m_addr_family == AF_INET6;
-}
-
bool Address::isZero() const
{
if (m_addr_family == AF_INET) {
- return m_address.ipv4.sin_addr.s_addr == 0;
+ return m_address.ipv4.s_addr == 0;
}
if (m_addr_family == AF_INET6) {
static const char zero[16] = {0};
- return memcmp(m_address.ipv6.sin6_addr.s6_addr, zero, 16) == 0;
+ return memcmp(m_address.ipv6.s6_addr, zero, 16) == 0;
}
+
return false;
}
void Address::setAddress(u32 address)
{
m_addr_family = AF_INET;
- m_address.ipv4.sin_family = AF_INET;
- m_address.ipv4.sin_addr.s_addr = htonl(address);
+ m_address.ipv4.s_addr = htonl(address);
}
void Address::setAddress(u8 a, u8 b, u8 c, u8 d)
{
- m_addr_family = AF_INET;
- m_address.ipv4.sin_family = AF_INET;
- u32 addr = htonl((a << 24) | (b << 16) | (c << 8) | d);
- m_address.ipv4.sin_addr.s_addr = addr;
+ u32 addr = (a << 24) | (b << 16) | (c << 8) | d;
+ setAddress(addr);
}
void Address::setAddress(const IPv6AddressBytes *ipv6_bytes)
{
m_addr_family = AF_INET6;
- m_address.ipv6.sin6_family = AF_INET6;
if (ipv6_bytes)
- memcpy(m_address.ipv6.sin6_addr.s6_addr, ipv6_bytes->bytes, 16);
+ memcpy(m_address.ipv6.s6_addr, ipv6_bytes->bytes, 16);
else
- memset(m_address.ipv6.sin6_addr.s6_addr, 0, 16);
+ memset(m_address.ipv6.s6_addr, 0, 16);
}
void Address::setPort(u16 port)
@@ -268,23 +234,26 @@ void Address::print(std::ostream *s) const
{
if (m_addr_family == AF_INET6)
*s << "[" << serializeString() << "]:" << m_port;
- else
+ else if (m_addr_family == AF_INET)
*s << serializeString() << ":" << m_port;
+ else
+ *s << "(undefined)";
}
bool Address::isLocalhost() const
{
if (isIPv6()) {
- static const unsigned char localhost_bytes[] = {
+ static const u8 localhost_bytes[] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
- static const unsigned char mapped_ipv4_localhost[] = {
+ static const u8 mapped_ipv4_localhost[] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 0x7f, 0, 0, 0};
- auto addr = m_address.ipv6.sin6_addr.s6_addr;
+ auto addr = m_address.ipv6.s6_addr;
return memcmp(addr, localhost_bytes, 16) == 0 ||
memcmp(addr, mapped_ipv4_localhost, 13) == 0;
}
- return (m_address.ipv4.sin_addr.s_addr & 0xFF) == 0x7f;
+ auto addr = ntohl(m_address.ipv4.s_addr);
+ return (addr >> 24) == 0x7f;
}
diff --git a/src/network/address.h b/src/network/address.h
index 4329c84a8..c2f5f2eef 100644
--- a/src/network/address.h
+++ b/src/network/address.h
@@ -36,9 +36,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "irrlichttypes.h"
#include "networkexceptions.h"
-class IPv6AddressBytes
+struct IPv6AddressBytes
{
-public:
u8 bytes[16];
IPv6AddressBytes() { memset(bytes, 0, 16); }
};
@@ -50,30 +49,34 @@ public:
Address(u32 address, u16 port);
Address(u8 a, u8 b, u8 c, u8 d, u16 port);
Address(const IPv6AddressBytes *ipv6_bytes, u16 port);
+
bool operator==(const Address &address);
- bool operator!=(const Address &address);
+ bool operator!=(const Address &address) { return !(*this == address); }
+
+ struct in_addr getAddress() const;
+ struct in6_addr getAddress6() const;
+ u16 getPort() const;
+ int getFamily() const { return m_addr_family; }
+ bool isIPv6() const { return m_addr_family == AF_INET6; }
+ bool isZero() const;
+ void print(std::ostream *s) const;
+ std::string serializeString() const;
+ bool isLocalhost() const;
+
// Resolve() may throw ResolveError (address is unchanged in this case)
void Resolve(const char *name);
- struct sockaddr_in getAddress() const;
- unsigned short getPort() const;
+
void setAddress(u32 address);
void setAddress(u8 a, u8 b, u8 c, u8 d);
void setAddress(const IPv6AddressBytes *ipv6_bytes);
- struct sockaddr_in6 getAddress6() const;
- int getFamily() const;
- bool isIPv6() const;
- bool isZero() const;
- void setPort(unsigned short port);
- void print(std::ostream *s) const;
- std::string serializeString() const;
- bool isLocalhost() const;
+ void setPort(u16 port);
private:
- unsigned int m_addr_family = 0;
+ unsigned short m_addr_family = 0;
union
{
- struct sockaddr_in ipv4;
- struct sockaddr_in6 ipv6;
+ struct in_addr ipv4;
+ struct in6_addr ipv6;
} m_address;
u16 m_port = 0; // Port is separate from sockaddr structures
};
diff --git a/src/network/clientopcodes.cpp b/src/network/clientopcodes.cpp
index 55cfdd4dc..a98a5e7d1 100644
--- a/src/network/clientopcodes.cpp
+++ b/src/network/clientopcodes.cpp
@@ -204,7 +204,7 @@ const ServerCommandFactory serverCommandFactoryTable[TOSERVER_NUM_MSG_TYPES] =
null_command_factory, // 0x3e
null_command_factory, // 0x3f
{ "TOSERVER_REQUEST_MEDIA", 1, true }, // 0x40
- null_command_factory, // 0x41
+ { "TOSERVER_HAVE_MEDIA", 2, true }, // 0x41
null_command_factory, // 0x42
{ "TOSERVER_CLIENT_READY", 1, true }, // 0x43
null_command_factory, // 0x44
diff --git a/src/network/clientpackethandler.cpp b/src/network/clientpackethandler.cpp
index 44bd81dac..48ad60ac6 100644
--- a/src/network/clientpackethandler.cpp
+++ b/src/network/clientpackethandler.cpp
@@ -43,6 +43,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "tileanimation.h"
#include "gettext.h"
#include "skyparams.h"
+#include <memory>
void Client::handleCommand_Deprecated(NetworkPacket* pkt)
{
@@ -88,7 +89,7 @@ void Client::handleCommand_Hello(NetworkPacket* pkt)
// This is only neccessary though when we actually want to add casing support
if (m_chosen_auth_mech != AUTH_MECHANISM_NONE) {
- // we recieved a TOCLIENT_HELLO while auth was already going on
+ // we received a TOCLIENT_HELLO while auth was already going on
errorstream << "Client: TOCLIENT_HELLO while auth was already going on"
<< "(chosen_mech=" << m_chosen_auth_mech << ")." << std::endl;
if (m_chosen_auth_mech == AUTH_MECHANISM_SRP ||
@@ -156,7 +157,7 @@ void Client::handleCommand_AcceptSudoMode(NetworkPacket* pkt)
m_password = m_new_password;
- verbosestream << "Client: Recieved TOCLIENT_ACCEPT_SUDO_MODE." << std::endl;
+ verbosestream << "Client: Received TOCLIENT_ACCEPT_SUDO_MODE." << std::endl;
// send packet to actually set the password
startAuth(AUTH_MECHANISM_FIRST_SRP);
@@ -261,7 +262,7 @@ void Client::handleCommand_NodemetaChanged(NetworkPacket *pkt)
return;
std::istringstream is(pkt->readLongString(), std::ios::binary);
- std::stringstream sstr;
+ std::stringstream sstr(std::ios::binary | std::ios::in | std::ios::out);
decompressZlib(is, sstr);
NodeMetadataList meta_updates_list(false);
@@ -670,21 +671,19 @@ void Client::handleCommand_AnnounceMedia(NetworkPacket* pkt)
m_media_downloader->addFile(name, sha1_raw);
}
- try {
+ {
std::string str;
-
*pkt >> str;
Strfnd sf(str);
- while(!sf.at_end()) {
+ while (!sf.at_end()) {
std::string baseurl = trim(sf.next(","));
- if (!baseurl.empty())
+ if (!baseurl.empty()) {
+ m_remote_media_servers.emplace_back(baseurl);
m_media_downloader->addRemoteServer(baseurl);
+ }
}
}
- catch(SerializationError& e) {
- // not supported by server or turned off
- }
m_media_downloader->step(this);
}
@@ -716,31 +715,38 @@ void Client::handleCommand_Media(NetworkPacket* pkt)
if (num_files == 0)
return;
- if (!m_media_downloader || !m_media_downloader->isStarted()) {
- const char *problem = m_media_downloader ?
- "media has not been requested" :
- "all media has been received already";
- errorstream << "Client: Received media but "
- << problem << "! "
- << " bunch " << bunch_i << "/" << num_bunches
- << " files=" << num_files
- << " size=" << pkt->getSize() << std::endl;
- return;
- }
+ bool init_phase = m_media_downloader && m_media_downloader->isStarted();
- // Mesh update thread must be stopped while
- // updating content definitions
- sanity_check(!m_mesh_update_thread.isRunning());
+ if (init_phase) {
+ // Mesh update thread must be stopped while
+ // updating content definitions
+ sanity_check(!m_mesh_update_thread.isRunning());
+ }
- for (u32 i=0; i < num_files; i++) {
- std::string name;
+ for (u32 i = 0; i < num_files; i++) {
+ std::string name, data;
*pkt >> name;
+ data = pkt->readLongString();
- std::string data = pkt->readLongString();
-
- m_media_downloader->conventionalTransferDone(
- name, data, this);
+ bool ok = false;
+ if (init_phase) {
+ ok = m_media_downloader->conventionalTransferDone(name, data, this);
+ } else {
+ // Check pending dynamic transfers, one of them must be it
+ for (const auto &it : m_pending_media_downloads) {
+ if (it.second->conventionalTransferDone(name, data, this)) {
+ ok = true;
+ break;
+ }
+ }
+ }
+ if (!ok) {
+ errorstream << "Client: Received media \"" << name
+ << "\" but no downloads pending. " << num_bunches << " bunches, "
+ << num_files << " in this one. (init_phase=" << init_phase
+ << ")" << std::endl;
+ }
}
}
@@ -755,12 +761,11 @@ void Client::handleCommand_NodeDef(NetworkPacket* pkt)
// Decompress node definitions
std::istringstream tmp_is(pkt->readLongString(), std::ios::binary);
- std::ostringstream tmp_os;
+ std::stringstream tmp_os(std::ios::binary | std::ios::in | std::ios::out);
decompressZlib(tmp_is, tmp_os);
// Deserialize node definitions
- std::istringstream tmp_is2(tmp_os.str());
- m_nodedef->deSerialize(tmp_is2);
+ m_nodedef->deSerialize(tmp_os);
m_nodedef_received = true;
}
@@ -775,12 +780,11 @@ void Client::handleCommand_ItemDef(NetworkPacket* pkt)
// Decompress item definitions
std::istringstream tmp_is(pkt->readLongString(), std::ios::binary);
- std::ostringstream tmp_os;
+ std::stringstream tmp_os(std::ios::binary | std::ios::in | std::ios::out);
decompressZlib(tmp_is, tmp_os);
// Deserialize node definitions
- std::istringstream tmp_is2(tmp_os.str());
- m_itemdef->deSerialize(tmp_is2);
+ m_itemdef->deSerialize(tmp_os);
m_itemdef_received = true;
}
@@ -1041,9 +1045,6 @@ void Client::handleCommand_DeleteParticleSpawner(NetworkPacket* pkt)
void Client::handleCommand_HudAdd(NetworkPacket* pkt)
{
- std::string datastring(pkt->getString(0), pkt->getSize());
- std::istringstream is(datastring, std::ios_base::binary);
-
u32 server_id;
u8 type;
v2f pos;
@@ -1059,6 +1060,7 @@ void Client::handleCommand_HudAdd(NetworkPacket* pkt)
v2s32 size;
s16 z_index = 0;
std::string text2;
+ u32 style = 0;
*pkt >> server_id >> type >> pos >> name >> scale >> text >> number >> item
>> dir >> align >> offset;
@@ -1067,25 +1069,28 @@ void Client::handleCommand_HudAdd(NetworkPacket* pkt)
*pkt >> size;
*pkt >> z_index;
*pkt >> text2;
+ *pkt >> style;
} catch(PacketError &e) {};
ClientEvent *event = new ClientEvent();
- event->type = CE_HUDADD;
- event->hudadd.server_id = server_id;
- event->hudadd.type = type;
- event->hudadd.pos = new v2f(pos);
- event->hudadd.name = new std::string(name);
- event->hudadd.scale = new v2f(scale);
- event->hudadd.text = new std::string(text);
- event->hudadd.number = number;
- event->hudadd.item = item;
- event->hudadd.dir = dir;
- event->hudadd.align = new v2f(align);
- event->hudadd.offset = new v2f(offset);
- event->hudadd.world_pos = new v3f(world_pos);
- event->hudadd.size = new v2s32(size);
- event->hudadd.z_index = z_index;
- event->hudadd.text2 = new std::string(text2);
+ event->type = CE_HUDADD;
+ event->hudadd = new ClientEventHudAdd();
+ event->hudadd->server_id = server_id;
+ event->hudadd->type = type;
+ event->hudadd->pos = pos;
+ event->hudadd->name = name;
+ event->hudadd->scale = scale;
+ event->hudadd->text = text;
+ event->hudadd->number = number;
+ event->hudadd->item = item;
+ event->hudadd->dir = dir;
+ event->hudadd->align = align;
+ event->hudadd->offset = offset;
+ event->hudadd->world_pos = world_pos;
+ event->hudadd->size = size;
+ event->hudadd->z_index = z_index;
+ event->hudadd->text2 = text2;
+ event->hudadd->style = style;
m_client_event_queue.push(event);
}
@@ -1113,27 +1118,40 @@ void Client::handleCommand_HudChange(NetworkPacket* pkt)
*pkt >> server_id >> stat;
- if (stat == HUD_STAT_POS || stat == HUD_STAT_SCALE ||
- stat == HUD_STAT_ALIGN || stat == HUD_STAT_OFFSET)
- *pkt >> v2fdata;
- else if (stat == HUD_STAT_NAME || stat == HUD_STAT_TEXT || stat == HUD_STAT_TEXT2)
- *pkt >> sdata;
- else if (stat == HUD_STAT_WORLD_POS)
- *pkt >> v3fdata;
- else if (stat == HUD_STAT_SIZE )
- *pkt >> v2s32data;
- else
- *pkt >> intdata;
+ // Keep in sync with:server.cpp -> SendHUDChange
+ switch ((HudElementStat)stat) {
+ case HUD_STAT_POS:
+ case HUD_STAT_SCALE:
+ case HUD_STAT_ALIGN:
+ case HUD_STAT_OFFSET:
+ *pkt >> v2fdata;
+ break;
+ case HUD_STAT_NAME:
+ case HUD_STAT_TEXT:
+ case HUD_STAT_TEXT2:
+ *pkt >> sdata;
+ break;
+ case HUD_STAT_WORLD_POS:
+ *pkt >> v3fdata;
+ break;
+ case HUD_STAT_SIZE:
+ *pkt >> v2s32data;
+ break;
+ default:
+ *pkt >> intdata;
+ break;
+ }
ClientEvent *event = new ClientEvent();
- event->type = CE_HUDCHANGE;
- event->hudchange.id = server_id;
- event->hudchange.stat = (HudElementStat)stat;
- event->hudchange.v2fdata = new v2f(v2fdata);
- event->hudchange.v3fdata = new v3f(v3fdata);
- event->hudchange.sdata = new std::string(sdata);
- event->hudchange.data = intdata;
- event->hudchange.v2s32data = new v2s32(v2s32data);
+ event->type = CE_HUDCHANGE;
+ event->hudchange = new ClientEventHudChange();
+ event->hudchange->id = server_id;
+ event->hudchange->stat = static_cast<HudElementStat>(stat);
+ event->hudchange->v2fdata = v2fdata;
+ event->hudchange->v3fdata = v3fdata;
+ event->hudchange->sdata = sdata;
+ event->hudchange->data = intdata;
+ event->hudchange->v2s32data = v2s32data;
m_client_event_queue.push(event);
}
@@ -1219,19 +1237,17 @@ void Client::handleCommand_HudSetSky(NetworkPacket* pkt)
} catch (...) {}
// Use default skybox settings:
- SkyboxDefaults sky_defaults;
- SunParams sun = sky_defaults.getSunDefaults();
- MoonParams moon = sky_defaults.getMoonDefaults();
- StarParams stars = sky_defaults.getStarDefaults();
+ SunParams sun = SkyboxDefaults::getSunDefaults();
+ MoonParams moon = SkyboxDefaults::getMoonDefaults();
+ StarParams stars = SkyboxDefaults::getStarDefaults();
// Fix for "regular" skies, as color isn't kept:
if (skybox.type == "regular") {
- skybox.sky_color = sky_defaults.getSkyColorDefaults();
+ skybox.sky_color = SkyboxDefaults::getSkyColorDefaults();
skybox.fog_tint_type = "default";
skybox.fog_moon_tint = video::SColor(255, 255, 255, 255);
skybox.fog_sun_tint = video::SColor(255, 255, 255, 255);
- }
- else {
+ } else {
sun.visible = false;
sun.sunrise_visible = false;
moon.visible = false;
@@ -1381,6 +1397,8 @@ void Client::handleCommand_LocalPlayerAnimations(NetworkPacket* pkt)
*pkt >> player->local_animations[2];
*pkt >> player->local_animations[3];
*pkt >> player->local_animation_speed;
+
+ player->last_animation = -1;
}
void Client::handleCommand_EyeOffset(NetworkPacket* pkt)
@@ -1478,46 +1496,72 @@ void Client::handleCommand_PlayerSpeed(NetworkPacket *pkt)
void Client::handleCommand_MediaPush(NetworkPacket *pkt)
{
std::string raw_hash, filename, filedata;
+ u32 token;
bool cached;
*pkt >> raw_hash >> filename >> cached;
- filedata = pkt->readLongString();
+ if (m_proto_ver >= 40)
+ *pkt >> token;
+ else
+ filedata = pkt->readLongString();
- if (raw_hash.size() != 20 || filedata.empty() || filename.empty() ||
+ if (raw_hash.size() != 20 || filename.empty() ||
+ (m_proto_ver < 40 && filedata.empty()) ||
!string_allowed(filename, TEXTURENAME_ALLOWED_CHARS)) {
throw PacketError("Illegal filename, data or hash");
}
- verbosestream << "Server pushes media file \"" << filename << "\" with "
- << filedata.size() << " bytes of data (cached=" << cached
- << ")" << std::endl;
+ verbosestream << "Server pushes media file \"" << filename << "\" ";
+ if (filedata.empty())
+ verbosestream << "to be fetched ";
+ else
+ verbosestream << "with " << filedata.size() << " bytes ";
+ verbosestream << "(cached=" << cached << ")" << std::endl;
if (m_media_pushed_files.count(filename) != 0) {
- // Silently ignore for synchronization purposes
+ // Ignore (but acknowledge). Previously this was for sync purposes,
+ // but even in new versions media cannot be replaced at runtime.
+ if (m_proto_ver >= 40)
+ sendHaveMedia({ token });
return;
}
- // Compute and check checksum of data
- std::string computed_hash;
- {
- SHA1 ctx;
- ctx.addBytes(filedata.c_str(), filedata.size());
- unsigned char *buf = ctx.getDigest();
- computed_hash.assign((char*) buf, 20);
- free(buf);
- }
- if (raw_hash != computed_hash) {
- verbosestream << "Hash of file data mismatches, ignoring." << std::endl;
+ if (!filedata.empty()) {
+ // LEGACY CODEPATH
+ // Compute and check checksum of data
+ std::string computed_hash;
+ {
+ SHA1 ctx;
+ ctx.addBytes(filedata.c_str(), filedata.size());
+ unsigned char *buf = ctx.getDigest();
+ computed_hash.assign((char*) buf, 20);
+ free(buf);
+ }
+ if (raw_hash != computed_hash) {
+ verbosestream << "Hash of file data mismatches, ignoring." << std::endl;
+ return;
+ }
+
+ // Actually load media
+ loadMedia(filedata, filename, true);
+ m_media_pushed_files.insert(filename);
+
+ // Cache file for the next time when this client joins the same server
+ if (cached)
+ clientMediaUpdateCache(raw_hash, filedata);
return;
}
- // Actually load media
- loadMedia(filedata, filename, true);
m_media_pushed_files.insert(filename);
- // Cache file for the next time when this client joins the same server
- if (cached)
- clientMediaUpdateCache(raw_hash, filedata);
+ // create a downloader for this file
+ auto downloader(std::make_shared<SingleMediaDownloader>(cached));
+ m_pending_media_downloads.emplace_back(token, downloader);
+ downloader->addFile(filename, raw_hash);
+ for (const auto &baseurl : m_remote_media_servers)
+ downloader->addRemoteServer(baseurl);
+
+ downloader->step(this);
}
/*
diff --git a/src/network/connection.cpp b/src/network/connection.cpp
index a4970954f..2d3cf6e88 100644
--- a/src/network/connection.cpp
+++ b/src/network/connection.cpp
@@ -62,18 +62,27 @@ namespace con
#define PING_TIMEOUT 5.0
-BufferedPacket makePacket(Address &address, const SharedBuffer<u8> &data,
+u16 BufferedPacket::getSeqnum() const
+{
+ if (size() < BASE_HEADER_SIZE + 3)
+ return 0; // should never happen
+
+ return readU16(&data[BASE_HEADER_SIZE + 1]);
+}
+
+BufferedPacketPtr makePacket(Address &address, const SharedBuffer<u8> &data,
u32 protocol_id, session_t sender_peer_id, u8 channel)
{
u32 packet_size = data.getSize() + BASE_HEADER_SIZE;
- BufferedPacket p(packet_size);
- p.address = address;
- writeU32(&p.data[0], protocol_id);
- writeU16(&p.data[4], sender_peer_id);
- writeU8(&p.data[6], channel);
+ BufferedPacketPtr p(new BufferedPacket(packet_size));
+ p->address = address;
+
+ writeU32(&p->data[0], protocol_id);
+ writeU16(&p->data[4], sender_peer_id);
+ writeU8(&p->data[6], channel);
- memcpy(&p.data[BASE_HEADER_SIZE], *data, data.getSize());
+ memcpy(&p->data[BASE_HEADER_SIZE], *data, data.getSize());
return p;
}
@@ -169,9 +178,8 @@ void ReliablePacketBuffer::print()
MutexAutoLock listlock(m_list_mutex);
LOG(dout_con<<"Dump of ReliablePacketBuffer:" << std::endl);
unsigned int index = 0;
- for (BufferedPacket &bufferedPacket : m_list) {
- u16 s = readU16(&(bufferedPacket.data[BASE_HEADER_SIZE+1]));
- LOG(dout_con<<index<< ":" << s << std::endl);
+ for (BufferedPacketPtr &packet : m_list) {
+ LOG(dout_con<<index<< ":" << packet->getSeqnum() << std::endl);
index++;
}
}
@@ -188,16 +196,13 @@ u32 ReliablePacketBuffer::size()
return m_list.size();
}
-RPBSearchResult ReliablePacketBuffer::findPacket(u16 seqnum)
+RPBSearchResult ReliablePacketBuffer::findPacketNoLock(u16 seqnum)
{
- std::list<BufferedPacket>::iterator i = m_list.begin();
- for(; i != m_list.end(); ++i)
- {
- u16 s = readU16(&(i->data[BASE_HEADER_SIZE+1]));
- if (s == seqnum)
- break;
+ for (auto it = m_list.begin(); it != m_list.end(); ++it) {
+ if ((*it)->getSeqnum() == seqnum)
+ return it;
}
- return i;
+ return m_list.end();
}
bool ReliablePacketBuffer::getFirstSeqnum(u16& result)
@@ -205,54 +210,54 @@ bool ReliablePacketBuffer::getFirstSeqnum(u16& result)
MutexAutoLock listlock(m_list_mutex);
if (m_list.empty())
return false;
- const BufferedPacket &p = m_list.front();
- result = readU16(&p.data[BASE_HEADER_SIZE + 1]);
+ result = m_list.front()->getSeqnum();
return true;
}
-BufferedPacket ReliablePacketBuffer::popFirst()
+BufferedPacketPtr ReliablePacketBuffer::popFirst()
{
MutexAutoLock listlock(m_list_mutex);
if (m_list.empty())
throw NotFoundException("Buffer is empty");
- BufferedPacket p = std::move(m_list.front());
+
+ BufferedPacketPtr p(m_list.front());
m_list.pop_front();
if (m_list.empty()) {
m_oldest_non_answered_ack = 0;
} else {
- m_oldest_non_answered_ack =
- readU16(&m_list.front().data[BASE_HEADER_SIZE + 1]);
+ m_oldest_non_answered_ack = m_list.front()->getSeqnum();
}
return p;
}
-BufferedPacket ReliablePacketBuffer::popSeqnum(u16 seqnum)
+BufferedPacketPtr ReliablePacketBuffer::popSeqnum(u16 seqnum)
{
MutexAutoLock listlock(m_list_mutex);
- RPBSearchResult r = findPacket(seqnum);
- if (r == notFound()) {
+ RPBSearchResult r = findPacketNoLock(seqnum);
+ if (r == m_list.end()) {
LOG(dout_con<<"Sequence number: " << seqnum
<< " not found in reliable buffer"<<std::endl);
throw NotFoundException("seqnum not found in buffer");
}
- BufferedPacket p = std::move(*r);
+ BufferedPacketPtr p(*r);
m_list.erase(r);
if (m_list.empty()) {
m_oldest_non_answered_ack = 0;
} else {
- m_oldest_non_answered_ack =
- readU16(&m_list.front().data[BASE_HEADER_SIZE + 1]);
+ m_oldest_non_answered_ack = m_list.front()->getSeqnum();
}
return p;
}
-void ReliablePacketBuffer::insert(const BufferedPacket &p, u16 next_expected)
+void ReliablePacketBuffer::insert(BufferedPacketPtr &p_ptr, u16 next_expected)
{
MutexAutoLock listlock(m_list_mutex);
- if (p.data.getSize() < BASE_HEADER_SIZE + 3) {
+ const BufferedPacket &p = *p_ptr;
+
+ if (p.size() < BASE_HEADER_SIZE + 3) {
errorstream << "ReliablePacketBuffer::insert(): Invalid data size for "
"reliable packet" << std::endl;
return;
@@ -263,7 +268,7 @@ void ReliablePacketBuffer::insert(const BufferedPacket &p, u16 next_expected)
<< std::endl;
return;
}
- u16 seqnum = readU16(&p.data[BASE_HEADER_SIZE + 1]);
+ const u16 seqnum = p.getSeqnum();
if (!seqnum_in_window(seqnum, next_expected, MAX_RELIABLE_WINDOW_SIZE)) {
errorstream << "ReliablePacketBuffer::insert(): seqnum is outside of "
@@ -280,44 +285,44 @@ void ReliablePacketBuffer::insert(const BufferedPacket &p, u16 next_expected)
// Find the right place for the packet and insert it there
// If list is empty, just add it
- if (m_list.empty())
- {
- m_list.push_back(p);
+ if (m_list.empty()) {
+ m_list.push_back(p_ptr);
m_oldest_non_answered_ack = seqnum;
// Done.
return;
}
// Otherwise find the right place
- std::list<BufferedPacket>::iterator i = m_list.begin();
+ auto it = m_list.begin();
// Find the first packet in the list which has a higher seqnum
- u16 s = readU16(&(i->data[BASE_HEADER_SIZE+1]));
+ u16 s = (*it)->getSeqnum();
/* case seqnum is smaller then next_expected seqnum */
/* this is true e.g. on wrap around */
if (seqnum < next_expected) {
- while(((s < seqnum) || (s >= next_expected)) && (i != m_list.end())) {
- ++i;
- if (i != m_list.end())
- s = readU16(&(i->data[BASE_HEADER_SIZE+1]));
+ while(((s < seqnum) || (s >= next_expected)) && (it != m_list.end())) {
+ ++it;
+ if (it != m_list.end())
+ s = (*it)->getSeqnum();
}
}
/* non wrap around case (at least for incoming and next_expected */
else
{
- while(((s < seqnum) && (s >= next_expected)) && (i != m_list.end())) {
- ++i;
- if (i != m_list.end())
- s = readU16(&(i->data[BASE_HEADER_SIZE+1]));
+ while(((s < seqnum) && (s >= next_expected)) && (it != m_list.end())) {
+ ++it;
+ if (it != m_list.end())
+ s = (*it)->getSeqnum();
}
}
if (s == seqnum) {
/* nothing to do this seems to be a resent packet */
/* for paranoia reason data should be compared */
+ auto &i = *it;
if (
- (readU16(&(i->data[BASE_HEADER_SIZE+1])) != seqnum) ||
- (i->data.getSize() != p.data.getSize()) ||
+ (i->getSeqnum() != seqnum) ||
+ (i->size() != p.size()) ||
(i->address != p.address)
)
{
@@ -325,51 +330,52 @@ void ReliablePacketBuffer::insert(const BufferedPacket &p, u16 next_expected)
fprintf(stderr,
"Duplicated seqnum %d non matching packet detected:\n",
seqnum);
- fprintf(stderr, "Old: seqnum: %05d size: %04d, address: %s\n",
- readU16(&(i->data[BASE_HEADER_SIZE+1])),i->data.getSize(),
+ fprintf(stderr, "Old: seqnum: %05d size: %04zu, address: %s\n",
+ i->getSeqnum(), i->size(),
i->address.serializeString().c_str());
- fprintf(stderr, "New: seqnum: %05d size: %04u, address: %s\n",
- readU16(&(p.data[BASE_HEADER_SIZE+1])),p.data.getSize(),
+ fprintf(stderr, "New: seqnum: %05d size: %04zu, address: %s\n",
+ p.getSeqnum(), p.size(),
p.address.serializeString().c_str());
throw IncomingDataCorruption("duplicated packet isn't same as original one");
}
}
/* insert or push back */
- else if (i != m_list.end()) {
- m_list.insert(i, p);
+ else if (it != m_list.end()) {
+ m_list.insert(it, p_ptr);
} else {
- m_list.push_back(p);
+ m_list.push_back(p_ptr);
}
/* update last packet number */
- m_oldest_non_answered_ack = readU16(&m_list.front().data[BASE_HEADER_SIZE+1]);
+ m_oldest_non_answered_ack = m_list.front()->getSeqnum();
}
void ReliablePacketBuffer::incrementTimeouts(float dtime)
{
MutexAutoLock listlock(m_list_mutex);
- for (BufferedPacket &bufferedPacket : m_list) {
- bufferedPacket.time += dtime;
- bufferedPacket.totaltime += dtime;
+ for (auto &packet : m_list) {
+ packet->time += dtime;
+ packet->totaltime += dtime;
}
}
-std::list<BufferedPacket>
+std::list<ConstSharedPtr<BufferedPacket>>
ReliablePacketBuffer::getTimedOuts(float timeout, u32 max_packets)
{
MutexAutoLock listlock(m_list_mutex);
- std::list<BufferedPacket> timed_outs;
- for (BufferedPacket &bufferedPacket : m_list) {
- if (bufferedPacket.time >= timeout) {
- // caller will resend packet so reset time and increase counter
- bufferedPacket.time = 0.0f;
- bufferedPacket.resend_count++;
+ std::list<ConstSharedPtr<BufferedPacket>> timed_outs;
+ for (auto &packet : m_list) {
+ if (packet->time < timeout)
+ continue;
- timed_outs.push_back(bufferedPacket);
+ // caller will resend packet so reset time and increase counter
+ packet->time = 0.0f;
+ packet->resend_count++;
- if (timed_outs.size() >= max_packets)
- break;
- }
+ timed_outs.emplace_back(packet);
+
+ if (timed_outs.size() >= max_packets)
+ break;
}
return timed_outs;
}
@@ -428,11 +434,13 @@ IncomingSplitBuffer::~IncomingSplitBuffer()
}
}
-SharedBuffer<u8> IncomingSplitBuffer::insert(const BufferedPacket &p, bool reliable)
+SharedBuffer<u8> IncomingSplitBuffer::insert(BufferedPacketPtr &p_ptr, bool reliable)
{
MutexAutoLock listlock(m_map_mutex);
+ const BufferedPacket &p = *p_ptr;
+
u32 headersize = BASE_HEADER_SIZE + 7;
- if (p.data.getSize() < headersize) {
+ if (p.size() < headersize) {
errorstream << "Invalid data size for split packet" << std::endl;
return SharedBuffer<u8>();
}
@@ -473,7 +481,7 @@ SharedBuffer<u8> IncomingSplitBuffer::insert(const BufferedPacket &p, bool relia
<<std::endl);
// Cut chunk data out of packet
- u32 chunkdatasize = p.data.getSize() - headersize;
+ u32 chunkdatasize = p.size() - headersize;
SharedBuffer<u8> chunkdata(chunkdatasize);
memcpy(*chunkdata, &(p.data[headersize]), chunkdatasize);
@@ -520,14 +528,67 @@ void IncomingSplitBuffer::removeUnreliableTimedOuts(float dtime, float timeout)
ConnectionCommand
*/
-void ConnectionCommand::send(session_t peer_id_, u8 channelnum_, NetworkPacket *pkt,
- bool reliable_)
+ConnectionCommandPtr ConnectionCommand::create(ConnectionCommandType type)
+{
+ return ConnectionCommandPtr(new ConnectionCommand(type));
+}
+
+ConnectionCommandPtr ConnectionCommand::serve(Address address)
+{
+ auto c = create(CONNCMD_SERVE);
+ c->address = address;
+ return c;
+}
+
+ConnectionCommandPtr ConnectionCommand::connect(Address address)
{
- type = CONNCMD_SEND;
- peer_id = peer_id_;
- channelnum = channelnum_;
- data = pkt->oldForgePacket();
- reliable = reliable_;
+ auto c = create(CONNCMD_CONNECT);
+ c->address = address;
+ return c;
+}
+
+ConnectionCommandPtr ConnectionCommand::disconnect()
+{
+ return create(CONNCMD_DISCONNECT);
+}
+
+ConnectionCommandPtr ConnectionCommand::disconnect_peer(session_t peer_id)
+{
+ auto c = create(CONNCMD_DISCONNECT_PEER);
+ c->peer_id = peer_id;
+ return c;
+}
+
+ConnectionCommandPtr ConnectionCommand::send(session_t peer_id, u8 channelnum,
+ NetworkPacket *pkt, bool reliable)
+{
+ auto c = create(CONNCMD_SEND);
+ c->peer_id = peer_id;
+ c->channelnum = channelnum;
+ c->reliable = reliable;
+ c->data = pkt->oldForgePacket();
+ return c;
+}
+
+ConnectionCommandPtr ConnectionCommand::ack(session_t peer_id, u8 channelnum, const Buffer<u8> &data)
+{
+ auto c = create(CONCMD_ACK);
+ c->peer_id = peer_id;
+ c->channelnum = channelnum;
+ c->reliable = false;
+ data.copyTo(c->data);
+ return c;
+}
+
+ConnectionCommandPtr ConnectionCommand::createPeer(session_t peer_id, const Buffer<u8> &data)
+{
+ auto c = create(CONCMD_CREATE_PEER);
+ c->peer_id = peer_id;
+ c->channelnum = 0;
+ c->reliable = true;
+ c->raw = true;
+ data.copyTo(c->data);
+ return c;
}
/*
@@ -562,39 +623,38 @@ void Channel::setNextSplitSeqNum(u16 seqnum)
u16 Channel::getOutgoingSequenceNumber(bool& successful)
{
MutexAutoLock internal(m_internal_mutex);
+
u16 retval = next_outgoing_seqnum;
- u16 lowest_unacked_seqnumber;
+ successful = false;
/* shortcut if there ain't any packet in outgoing list */
- if (outgoing_reliables_sent.empty())
- {
+ if (outgoing_reliables_sent.empty()) {
+ successful = true;
next_outgoing_seqnum++;
return retval;
}
- if (outgoing_reliables_sent.getFirstSeqnum(lowest_unacked_seqnumber))
- {
+ u16 lowest_unacked_seqnumber;
+ if (outgoing_reliables_sent.getFirstSeqnum(lowest_unacked_seqnumber)) {
if (lowest_unacked_seqnumber < next_outgoing_seqnum) {
// ugly cast but this one is required in order to tell compiler we
// know about difference of two unsigned may be negative in general
// but we already made sure it won't happen in this case
- if (((u16)(next_outgoing_seqnum - lowest_unacked_seqnumber)) > window_size) {
- successful = false;
+ if (((u16)(next_outgoing_seqnum - lowest_unacked_seqnumber)) > m_window_size) {
return 0;
}
- }
- else {
+ } else {
// ugly cast but this one is required in order to tell compiler we
// know about difference of two unsigned may be negative in general
// but we already made sure it won't happen in this case
if ((next_outgoing_seqnum + (u16)(SEQNUM_MAX - lowest_unacked_seqnumber)) >
- window_size) {
- successful = false;
+ m_window_size) {
return 0;
}
}
}
+ successful = true;
next_outgoing_seqnum++;
return retval;
}
@@ -666,7 +726,7 @@ void Channel::UpdateTimers(float dtime)
//packet_too_late = current_packet_too_late;
packets_successful = current_packet_successful;
- if (current_bytes_transfered > (unsigned int) (window_size*512/2)) {
+ if (current_bytes_transfered > (unsigned int) (m_window_size*512/2)) {
reasonable_amount_of_data_transmitted = true;
}
current_packet_loss = 0;
@@ -681,37 +741,25 @@ void Channel::UpdateTimers(float dtime)
if (packets_successful > 0) {
successful_to_lost_ratio = packet_loss/packets_successful;
} else if (packet_loss > 0) {
- window_size = std::max(
- (window_size - 10),
- MIN_RELIABLE_WINDOW_SIZE);
+ setWindowSize(m_window_size - 10);
done = true;
}
if (!done) {
- if ((successful_to_lost_ratio < 0.01f) &&
- (window_size < MAX_RELIABLE_WINDOW_SIZE)) {
+ if (successful_to_lost_ratio < 0.01f) {
/* don't even think about increasing if we didn't even
* use major parts of our window */
if (reasonable_amount_of_data_transmitted)
- window_size = std::min(
- (window_size + 100),
- MAX_RELIABLE_WINDOW_SIZE);
- } else if ((successful_to_lost_ratio < 0.05f) &&
- (window_size < MAX_RELIABLE_WINDOW_SIZE)) {
+ setWindowSize(m_window_size + 100);
+ } else if (successful_to_lost_ratio < 0.05f) {
/* don't even think about increasing if we didn't even
* use major parts of our window */
if (reasonable_amount_of_data_transmitted)
- window_size = std::min(
- (window_size + 50),
- MAX_RELIABLE_WINDOW_SIZE);
+ setWindowSize(m_window_size + 50);
} else if (successful_to_lost_ratio > 0.15f) {
- window_size = std::max(
- (window_size - 100),
- MIN_RELIABLE_WINDOW_SIZE);
+ setWindowSize(m_window_size - 100);
} else if (successful_to_lost_ratio > 0.1f) {
- window_size = std::max(
- (window_size - 50),
- MIN_RELIABLE_WINDOW_SIZE);
+ setWindowSize(m_window_size - 50);
}
}
}
@@ -958,45 +1006,45 @@ bool UDPPeer::Ping(float dtime,SharedBuffer<u8>& data)
return false;
}
-void UDPPeer::PutReliableSendCommand(ConnectionCommand &c,
+void UDPPeer::PutReliableSendCommand(ConnectionCommandPtr &c,
unsigned int max_packet_size)
{
if (m_pending_disconnect)
return;
- Channel &chan = channels[c.channelnum];
+ Channel &chan = channels[c->channelnum];
if (chan.queued_commands.empty() &&
/* don't queue more packets then window size */
- (chan.queued_reliables.size() < chan.getWindowSize() / 2)) {
+ (chan.queued_reliables.size() + 1 < chan.getWindowSize() / 2)) {
LOG(dout_con<<m_connection->getDesc()
- <<" processing reliable command for peer id: " << c.peer_id
- <<" data size: " << c.data.getSize() << std::endl);
- if (!processReliableSendCommand(c,max_packet_size)) {
- chan.queued_commands.push_back(c);
- }
- }
- else {
+ <<" processing reliable command for peer id: " << c->peer_id
+ <<" data size: " << c->data.getSize() << std::endl);
+ if (processReliableSendCommand(c, max_packet_size))
+ return;
+ } else {
LOG(dout_con<<m_connection->getDesc()
- <<" Queueing reliable command for peer id: " << c.peer_id
- <<" data size: " << c.data.getSize() <<std::endl);
- chan.queued_commands.push_back(c);
- if (chan.queued_commands.size() >= chan.getWindowSize() / 2) {
+ <<" Queueing reliable command for peer id: " << c->peer_id
+ <<" data size: " << c->data.getSize() <<std::endl);
+
+ if (chan.queued_commands.size() + 1 >= chan.getWindowSize() / 2) {
LOG(derr_con << m_connection->getDesc()
- << "Possible packet stall to peer id: " << c.peer_id
+ << "Possible packet stall to peer id: " << c->peer_id
<< " queued_commands=" << chan.queued_commands.size()
<< std::endl);
}
}
+ chan.queued_commands.push_back(c);
}
bool UDPPeer::processReliableSendCommand(
- ConnectionCommand &c,
+ ConnectionCommandPtr &c_ptr,
unsigned int max_packet_size)
{
if (m_pending_disconnect)
return true;
+ const auto &c = *c_ptr;
Channel &chan = channels[c.channelnum];
u32 chunksize_max = max_packet_size
@@ -1015,9 +1063,9 @@ bool UDPPeer::processReliableSendCommand(
chan.setNextSplitSeqNum(split_sequence_number);
}
- bool have_sequence_number = true;
+ bool have_sequence_number = false;
bool have_initial_sequence_number = false;
- std::queue<BufferedPacket> toadd;
+ std::queue<BufferedPacketPtr> toadd;
volatile u16 initial_sequence_number = 0;
for (SharedBuffer<u8> &original : originals) {
@@ -1036,25 +1084,23 @@ bool UDPPeer::processReliableSendCommand(
SharedBuffer<u8> reliable = makeReliablePacket(original, seqnum);
// Add base headers and make a packet
- BufferedPacket p = con::makePacket(address, reliable,
+ BufferedPacketPtr p = con::makePacket(address, reliable,
m_connection->GetProtocolID(), m_connection->GetPeerID(),
c.channelnum);
- toadd.push(std::move(p));
+ toadd.push(p);
}
if (have_sequence_number) {
- volatile u16 pcount = 0;
while (!toadd.empty()) {
- BufferedPacket p = std::move(toadd.front());
+ BufferedPacketPtr p = toadd.front();
toadd.pop();
// LOG(dout_con<<connection->getDesc()
// << " queuing reliable packet for peer_id: " << c.peer_id
// << " channel: " << (c.channelnum&0xFF)
// << " seqnum: " << readU16(&p.data[BASE_HEADER_SIZE+1])
// << std::endl)
- chan.queued_reliables.push(std::move(p));
- pcount++;
+ chan.queued_reliables.push(p);
}
sanity_check(chan.queued_reliables.size() < 0xFFFF);
return true;
@@ -1063,6 +1109,7 @@ bool UDPPeer::processReliableSendCommand(
volatile u16 packets_available = toadd.size();
/* we didn't get a single sequence number no need to fill queue */
if (!have_initial_sequence_number) {
+ LOG(derr_con << m_connection->getDesc() << "Ran out of sequence numbers!" << std::endl);
return false;
}
@@ -1108,18 +1155,18 @@ void UDPPeer::RunCommandQueues(
(channel.queued_reliables.size() < maxtransfer) &&
(commands_processed < maxcommands)) {
try {
- ConnectionCommand c = channel.queued_commands.front();
+ ConnectionCommandPtr c = channel.queued_commands.front();
LOG(dout_con << m_connection->getDesc()
<< " processing queued reliable command " << std::endl);
// Packet is processed, remove it from queue
- if (processReliableSendCommand(c,max_packet_size)) {
+ if (processReliableSendCommand(c, max_packet_size)) {
channel.queued_commands.pop_front();
} else {
LOG(dout_con << m_connection->getDesc()
- << " Failed to queue packets for peer_id: " << c.peer_id
- << ", delaying sending of " << c.data.getSize()
+ << " Failed to queue packets for peer_id: " << c->peer_id
+ << ", delaying sending of " << c->data.getSize()
<< " bytes" << std::endl);
}
}
@@ -1142,7 +1189,7 @@ void UDPPeer::setNextSplitSequenceNumber(u8 channel, u16 seqnum)
channels[channel].setNextSplitSeqNum(seqnum);
}
-SharedBuffer<u8> UDPPeer::addSplitPacket(u8 channel, const BufferedPacket &toadd,
+SharedBuffer<u8> UDPPeer::addSplitPacket(u8 channel, BufferedPacketPtr &toadd,
bool reliable)
{
assert(channel < CHANNEL_COUNT); // Pre-condition
@@ -1150,6 +1197,63 @@ SharedBuffer<u8> UDPPeer::addSplitPacket(u8 channel, const BufferedPacket &toadd
}
/*
+ ConnectionEvent
+*/
+
+const char *ConnectionEvent::describe() const
+{
+ switch(type) {
+ case CONNEVENT_NONE:
+ return "CONNEVENT_NONE";
+ case CONNEVENT_DATA_RECEIVED:
+ return "CONNEVENT_DATA_RECEIVED";
+ case CONNEVENT_PEER_ADDED:
+ return "CONNEVENT_PEER_ADDED";
+ case CONNEVENT_PEER_REMOVED:
+ return "CONNEVENT_PEER_REMOVED";
+ case CONNEVENT_BIND_FAILED:
+ return "CONNEVENT_BIND_FAILED";
+ }
+ return "Invalid ConnectionEvent";
+}
+
+
+ConnectionEventPtr ConnectionEvent::create(ConnectionEventType type)
+{
+ return std::shared_ptr<ConnectionEvent>(new ConnectionEvent(type));
+}
+
+ConnectionEventPtr ConnectionEvent::dataReceived(session_t peer_id, const Buffer<u8> &data)
+{
+ auto e = create(CONNEVENT_DATA_RECEIVED);
+ e->peer_id = peer_id;
+ data.copyTo(e->data);
+ return e;
+}
+
+ConnectionEventPtr ConnectionEvent::peerAdded(session_t peer_id, Address address)
+{
+ auto e = create(CONNEVENT_PEER_ADDED);
+ e->peer_id = peer_id;
+ e->address = address;
+ return e;
+}
+
+ConnectionEventPtr ConnectionEvent::peerRemoved(session_t peer_id, bool is_timeout, Address address)
+{
+ auto e = create(CONNEVENT_PEER_REMOVED);
+ e->peer_id = peer_id;
+ e->timeout = is_timeout;
+ e->address = address;
+ return e;
+}
+
+ConnectionEventPtr ConnectionEvent::bindFailed()
+{
+ return create(CONNEVENT_BIND_FAILED);
+}
+
+/*
Connection
*/
@@ -1198,18 +1302,12 @@ Connection::~Connection()
/* Internal stuff */
-void Connection::putEvent(const ConnectionEvent &e)
+void Connection::putEvent(ConnectionEventPtr e)
{
- assert(e.type != CONNEVENT_NONE); // Pre-condition
+ assert(e->type != CONNEVENT_NONE); // Pre-condition
m_event_queue.push_back(e);
}
-void Connection::putEvent(ConnectionEvent &&e)
-{
- assert(e.type != CONNEVENT_NONE); // Pre-condition
- m_event_queue.push_back(std::move(e));
-}
-
void Connection::TriggerSend()
{
m_sendThread->Trigger();
@@ -1272,11 +1370,9 @@ bool Connection::deletePeer(session_t peer_id, bool timeout)
Address peer_address;
//any peer has a primary address this never fails!
peer->getAddress(MTP_PRIMARY, peer_address);
- // Create event
- ConnectionEvent e;
- e.peerRemoved(peer_id, timeout, peer_address);
- putEvent(e);
+ // Create event
+ putEvent(ConnectionEvent::peerRemoved(peer_id, timeout, peer_address));
peer->Drop();
return true;
@@ -1284,18 +1380,16 @@ bool Connection::deletePeer(session_t peer_id, bool timeout)
/* Interface */
-ConnectionEvent Connection::waitEvent(u32 timeout_ms)
+ConnectionEventPtr Connection::waitEvent(u32 timeout_ms)
{
try {
return m_event_queue.pop_front(timeout_ms);
} catch(ItemNotFoundException &ex) {
- ConnectionEvent e;
- e.type = CONNEVENT_NONE;
- return e;
+ return ConnectionEvent::create(CONNEVENT_NONE);
}
}
-void Connection::putCommand(const ConnectionCommand &c)
+void Connection::putCommand(ConnectionCommandPtr c)
{
if (!m_shutting_down) {
m_command_queue.push_back(c);
@@ -1303,26 +1397,14 @@ void Connection::putCommand(const ConnectionCommand &c)
}
}
-void Connection::putCommand(ConnectionCommand &&c)
-{
- if (!m_shutting_down) {
- m_command_queue.push_back(std::move(c));
- m_sendThread->Trigger();
- }
-}
-
void Connection::Serve(Address bind_addr)
{
- ConnectionCommand c;
- c.serve(bind_addr);
- putCommand(c);
+ putCommand(ConnectionCommand::serve(bind_addr));
}
void Connection::Connect(Address address)
{
- ConnectionCommand c;
- c.connect(address);
- putCommand(c);
+ putCommand(ConnectionCommand::connect(address));
}
bool Connection::Connected()
@@ -1344,9 +1426,7 @@ bool Connection::Connected()
void Connection::Disconnect()
{
- ConnectionCommand c;
- c.disconnect();
- putCommand(c);
+ putCommand(ConnectionCommand::disconnect());
}
bool Connection::Receive(NetworkPacket *pkt, u32 timeout)
@@ -1357,11 +1437,15 @@ bool Connection::Receive(NetworkPacket *pkt, u32 timeout)
This is not considered to be a problem (is it?)
*/
for(;;) {
- ConnectionEvent e = waitEvent(timeout);
- if (e.type != CONNEVENT_NONE)
+ ConnectionEventPtr e_ptr = waitEvent(timeout);
+ const ConnectionEvent &e = *e_ptr;
+
+ if (e.type != CONNEVENT_NONE) {
LOG(dout_con << getDesc() << ": Receive: got event: "
<< e.describe() << std::endl);
- switch(e.type) {
+ }
+
+ switch (e.type) {
case CONNEVENT_NONE:
return false;
case CONNEVENT_DATA_RECEIVED:
@@ -1409,10 +1493,7 @@ void Connection::Send(session_t peer_id, u8 channelnum,
{
assert(channelnum < CHANNEL_COUNT); // Pre-condition
- ConnectionCommand c;
-
- c.send(peer_id, channelnum, pkt, reliable);
- putCommand(std::move(c));
+ putCommand(ConnectionCommand::send(peer_id, channelnum, pkt, reliable));
}
Address Connection::GetPeerAddress(session_t peer_id)
@@ -1511,41 +1592,31 @@ u16 Connection::createPeer(Address& sender, MTProtocols protocol, int fd)
LOG(dout_con << getDesc()
<< "createPeer(): giving peer_id=" << peer_id_new << std::endl);
- ConnectionCommand cmd;
- Buffer<u8> reply(4);
- writeU8(&reply[0], PACKET_TYPE_CONTROL);
- writeU8(&reply[1], CONTROLTYPE_SET_PEER_ID);
- writeU16(&reply[2], peer_id_new);
- cmd.createPeer(peer_id_new,reply);
- putCommand(std::move(cmd));
+ {
+ Buffer<u8> reply(4);
+ writeU8(&reply[0], PACKET_TYPE_CONTROL);
+ writeU8(&reply[1], CONTROLTYPE_SET_PEER_ID);
+ writeU16(&reply[2], peer_id_new);
+ putCommand(ConnectionCommand::createPeer(peer_id_new, reply));
+ }
// Create peer addition event
- ConnectionEvent e;
- e.peerAdded(peer_id_new, sender);
- putEvent(e);
+ putEvent(ConnectionEvent::peerAdded(peer_id_new, sender));
// We're now talking to a valid peer_id
return peer_id_new;
}
-void Connection::PrintInfo(std::ostream &out)
-{
- m_info_mutex.lock();
- out<<getDesc()<<": ";
- m_info_mutex.unlock();
-}
-
const std::string Connection::getDesc()
{
+ MutexAutoLock _(m_info_mutex);
return std::string("con(")+
itos(m_udpSocket.GetHandle())+"/"+itos(m_peer_id)+")";
}
void Connection::DisconnectPeer(session_t peer_id)
{
- ConnectionCommand discon;
- discon.disconnect_peer(peer_id);
- putCommand(discon);
+ putCommand(ConnectionCommand::disconnect_peer(peer_id));
}
void Connection::sendAck(session_t peer_id, u8 channelnum, u16 seqnum)
@@ -1557,14 +1628,12 @@ void Connection::sendAck(session_t peer_id, u8 channelnum, u16 seqnum)
" channel: " << (channelnum & 0xFF) <<
" seqnum: " << seqnum << std::endl);
- ConnectionCommand c;
SharedBuffer<u8> ack(4);
writeU8(&ack[0], PACKET_TYPE_CONTROL);
writeU8(&ack[1], CONTROLTYPE_ACK);
writeU16(&ack[2], seqnum);
- c.ack(peer_id, channelnum, ack);
- putCommand(std::move(c));
+ putCommand(ConnectionCommand::ack(peer_id, channelnum, ack));
m_sendThread->Trigger();
}
diff --git a/src/network/connection.h b/src/network/connection.h
index 49bb65c3e..1afb4ae84 100644
--- a/src/network/connection.h
+++ b/src/network/connection.h
@@ -32,6 +32,95 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <vector>
#include <map>
+#define MAX_UDP_PEERS 65535
+
+/*
+=== NOTES ===
+
+A packet is sent through a channel to a peer with a basic header:
+ Header (7 bytes):
+ [0] u32 protocol_id
+ [4] session_t sender_peer_id
+ [6] u8 channel
+sender_peer_id:
+ Unique to each peer.
+ value 0 (PEER_ID_INEXISTENT) is reserved for making new connections
+ value 1 (PEER_ID_SERVER) is reserved for server
+ these constants are defined in constants.h
+channel:
+ Channel numbers have no intrinsic meaning. Currently only 0, 1, 2 exist.
+*/
+#define BASE_HEADER_SIZE 7
+#define CHANNEL_COUNT 3
+
+/*
+Packet types:
+
+CONTROL: This is a packet used by the protocol.
+- When this is processed, nothing is handed to the user.
+ Header (2 byte):
+ [0] u8 type
+ [1] u8 controltype
+controltype and data description:
+ CONTROLTYPE_ACK
+ [2] u16 seqnum
+ CONTROLTYPE_SET_PEER_ID
+ [2] session_t peer_id_new
+ CONTROLTYPE_PING
+ - There is no actual reply, but this can be sent in a reliable
+ packet to get a reply
+ CONTROLTYPE_DISCO
+*/
+enum ControlType : u8 {
+ CONTROLTYPE_ACK = 0,
+ CONTROLTYPE_SET_PEER_ID = 1,
+ CONTROLTYPE_PING = 2,
+ CONTROLTYPE_DISCO = 3,
+};
+
+/*
+ORIGINAL: This is a plain packet with no control and no error
+checking at all.
+- When this is processed, it is directly handed to the user.
+ Header (1 byte):
+ [0] u8 type
+*/
+//#define TYPE_ORIGINAL 1
+#define ORIGINAL_HEADER_SIZE 1
+
+/*
+SPLIT: These are sequences of packets forming one bigger piece of
+data.
+- When processed and all the packet_nums 0...packet_count-1 are
+ present (this should be buffered), the resulting data shall be
+ directly handed to the user.
+- If the data fails to come up in a reasonable time, the buffer shall
+ be silently discarded.
+- These can be sent as-is or atop of a RELIABLE packet stream.
+ Header (7 bytes):
+ [0] u8 type
+ [1] u16 seqnum
+ [3] u16 chunk_count
+ [5] u16 chunk_num
+*/
+//#define TYPE_SPLIT 2
+
+/*
+RELIABLE: Delivery of all RELIABLE packets shall be forced by ACKs,
+and they shall be delivered in the same order as sent. This is done
+with a buffer in the receiving and transmitting end.
+- When this is processed, the contents of each packet is recursively
+ processed as packets.
+ Header (3 bytes):
+ [0] u8 type
+ [1] u16 seqnum
+
+*/
+//#define TYPE_RELIABLE 3
+#define RELIABLE_HEADER_SIZE 3
+#define SEQNUM_INITIAL 65500
+#define SEQNUM_MAX 65535
+
class NetworkPacket;
namespace con
@@ -46,9 +135,13 @@ typedef enum MTProtocols {
MTP_MINETEST_RELIABLE_UDP
} MTProtocols;
-#define MAX_UDP_PEERS 65535
-
-#define SEQNUM_MAX 65535
+enum PacketType : u8 {
+ PACKET_TYPE_CONTROL = 0,
+ PACKET_TYPE_ORIGINAL = 1,
+ PACKET_TYPE_SPLIT = 2,
+ PACKET_TYPE_RELIABLE = 3,
+ PACKET_TYPE_MAX
+};
inline bool seqnum_higher(u16 totest, u16 base)
{
@@ -85,24 +178,40 @@ static inline float CALC_DTIME(u64 lasttime, u64 curtime)
return MYMAX(MYMIN(value,0.1),0.0);
}
-struct BufferedPacket
-{
- BufferedPacket(u8 *a_data, u32 a_size):
- data(a_data, a_size)
- {}
- BufferedPacket(u32 a_size):
- data(a_size)
- {}
- Buffer<u8> data; // Data of the packet, including headers
+/*
+ Struct for all kinds of packets. Includes following data:
+ BASE_HEADER
+ u8[] packet data (usually copied from SharedBuffer<u8>)
+*/
+struct BufferedPacket {
+ BufferedPacket(u32 a_size)
+ {
+ m_data.resize(a_size);
+ data = &m_data[0];
+ }
+
+ DISABLE_CLASS_COPY(BufferedPacket)
+
+ u16 getSeqnum() const;
+
+ inline const size_t size() const { return m_data.size(); }
+
+ u8 *data; // Direct memory access
float time = 0.0f; // Seconds from buffering the packet or re-sending
float totaltime = 0.0f; // Seconds from buffering the packet
u64 absolute_send_time = -1;
Address address; // Sender or destination
unsigned int resend_count = 0;
+
+private:
+ std::vector<u8> m_data; // Data of the packet, including headers
};
+typedef std::shared_ptr<BufferedPacket> BufferedPacketPtr;
+
+
// This adds the base headers to the data and makes a packet out of it
-BufferedPacket makePacket(Address &address, const SharedBuffer<u8> &data,
+BufferedPacketPtr makePacket(Address &address, const SharedBuffer<u8> &data,
u32 protocol_id, session_t sender_peer_id, u8 channel);
// Depending on size, make a TYPE_ORIGINAL or TYPE_SPLIT packet
@@ -137,100 +246,11 @@ private:
};
/*
-=== NOTES ===
-
-A packet is sent through a channel to a peer with a basic header:
- Header (7 bytes):
- [0] u32 protocol_id
- [4] session_t sender_peer_id
- [6] u8 channel
-sender_peer_id:
- Unique to each peer.
- value 0 (PEER_ID_INEXISTENT) is reserved for making new connections
- value 1 (PEER_ID_SERVER) is reserved for server
- these constants are defined in constants.h
-channel:
- Channel numbers have no intrinsic meaning. Currently only 0, 1, 2 exist.
-*/
-#define BASE_HEADER_SIZE 7
-#define CHANNEL_COUNT 3
-/*
-Packet types:
-
-CONTROL: This is a packet used by the protocol.
-- When this is processed, nothing is handed to the user.
- Header (2 byte):
- [0] u8 type
- [1] u8 controltype
-controltype and data description:
- CONTROLTYPE_ACK
- [2] u16 seqnum
- CONTROLTYPE_SET_PEER_ID
- [2] session_t peer_id_new
- CONTROLTYPE_PING
- - There is no actual reply, but this can be sent in a reliable
- packet to get a reply
- CONTROLTYPE_DISCO
-*/
-//#define TYPE_CONTROL 0
-#define CONTROLTYPE_ACK 0
-#define CONTROLTYPE_SET_PEER_ID 1
-#define CONTROLTYPE_PING 2
-#define CONTROLTYPE_DISCO 3
-
-/*
-ORIGINAL: This is a plain packet with no control and no error
-checking at all.
-- When this is processed, it is directly handed to the user.
- Header (1 byte):
- [0] u8 type
-*/
-//#define TYPE_ORIGINAL 1
-#define ORIGINAL_HEADER_SIZE 1
-/*
-SPLIT: These are sequences of packets forming one bigger piece of
-data.
-- When processed and all the packet_nums 0...packet_count-1 are
- present (this should be buffered), the resulting data shall be
- directly handed to the user.
-- If the data fails to come up in a reasonable time, the buffer shall
- be silently discarded.
-- These can be sent as-is or atop of a RELIABLE packet stream.
- Header (7 bytes):
- [0] u8 type
- [1] u16 seqnum
- [3] u16 chunk_count
- [5] u16 chunk_num
-*/
-//#define TYPE_SPLIT 2
-/*
-RELIABLE: Delivery of all RELIABLE packets shall be forced by ACKs,
-and they shall be delivered in the same order as sent. This is done
-with a buffer in the receiving and transmitting end.
-- When this is processed, the contents of each packet is recursively
- processed as packets.
- Header (3 bytes):
- [0] u8 type
- [1] u16 seqnum
-
-*/
-//#define TYPE_RELIABLE 3
-#define RELIABLE_HEADER_SIZE 3
-#define SEQNUM_INITIAL 65500
-
-enum PacketType: u8 {
- PACKET_TYPE_CONTROL = 0,
- PACKET_TYPE_ORIGINAL = 1,
- PACKET_TYPE_SPLIT = 2,
- PACKET_TYPE_RELIABLE = 3,
- PACKET_TYPE_MAX
-};
-/*
A buffer which stores reliable packets and sorts them internally
for fast access to the smallest one.
*/
-typedef std::list<BufferedPacket>::iterator RPBSearchResult;
+typedef std::list<BufferedPacketPtr>::iterator RPBSearchResult;
class ReliablePacketBuffer
{
@@ -239,12 +259,12 @@ public:
bool getFirstSeqnum(u16& result);
- BufferedPacket popFirst();
- BufferedPacket popSeqnum(u16 seqnum);
- void insert(const BufferedPacket &p, u16 next_expected);
+ BufferedPacketPtr popFirst();
+ BufferedPacketPtr popSeqnum(u16 seqnum);
+ void insert(BufferedPacketPtr &p_ptr, u16 next_expected);
void incrementTimeouts(float dtime);
- std::list<BufferedPacket> getTimedOuts(float timeout, u32 max_packets);
+ std::list<ConstSharedPtr<BufferedPacket>> getTimedOuts(float timeout, u32 max_packets);
void print();
bool empty();
@@ -252,10 +272,9 @@ public:
private:
- RPBSearchResult findPacket(u16 seqnum); // does not perform locking
- inline RPBSearchResult notFound() { return m_list.end(); }
+ RPBSearchResult findPacketNoLock(u16 seqnum);
- std::list<BufferedPacket> m_list;
+ std::list<BufferedPacketPtr> m_list;
u16 m_oldest_non_answered_ack;
@@ -274,7 +293,7 @@ public:
Returns a reference counted buffer of length != 0 when a full split
packet is constructed. If not, returns one of length 0.
*/
- SharedBuffer<u8> insert(const BufferedPacket &p, bool reliable);
+ SharedBuffer<u8> insert(BufferedPacketPtr &p_ptr, bool reliable);
void removeUnreliableTimedOuts(float dtime, float timeout);
@@ -285,25 +304,6 @@ private:
std::mutex m_map_mutex;
};
-struct OutgoingPacket
-{
- session_t peer_id;
- u8 channelnum;
- SharedBuffer<u8> data;
- bool reliable;
- bool ack;
-
- OutgoingPacket(session_t peer_id_, u8 channelnum_, const SharedBuffer<u8> &data_,
- bool reliable_,bool ack_=false):
- peer_id(peer_id_),
- channelnum(channelnum_),
- data(data_),
- reliable(reliable_),
- ack(ack_)
- {
- }
-};
-
enum ConnectionCommandType{
CONNCMD_NONE,
CONNCMD_SERVE,
@@ -316,9 +316,13 @@ enum ConnectionCommandType{
CONCMD_CREATE_PEER
};
+struct ConnectionCommand;
+typedef std::shared_ptr<ConnectionCommand> ConnectionCommandPtr;
+
+// This is very similar to ConnectionEvent
struct ConnectionCommand
{
- enum ConnectionCommandType type = CONNCMD_NONE;
+ const ConnectionCommandType type;
Address address;
session_t peer_id = PEER_ID_INEXISTENT;
u8 channelnum = 0;
@@ -326,48 +330,21 @@ struct ConnectionCommand
bool reliable = false;
bool raw = false;
- ConnectionCommand() = default;
+ DISABLE_CLASS_COPY(ConnectionCommand);
- void serve(Address address_)
- {
- type = CONNCMD_SERVE;
- address = address_;
- }
- void connect(Address address_)
- {
- type = CONNCMD_CONNECT;
- address = address_;
- }
- void disconnect()
- {
- type = CONNCMD_DISCONNECT;
- }
- void disconnect_peer(session_t peer_id_)
- {
- type = CONNCMD_DISCONNECT_PEER;
- peer_id = peer_id_;
- }
-
- void send(session_t peer_id_, u8 channelnum_, NetworkPacket *pkt, bool reliable_);
+ static ConnectionCommandPtr serve(Address address);
+ static ConnectionCommandPtr connect(Address address);
+ static ConnectionCommandPtr disconnect();
+ static ConnectionCommandPtr disconnect_peer(session_t peer_id);
+ static ConnectionCommandPtr send(session_t peer_id, u8 channelnum, NetworkPacket *pkt, bool reliable);
+ static ConnectionCommandPtr ack(session_t peer_id, u8 channelnum, const Buffer<u8> &data);
+ static ConnectionCommandPtr createPeer(session_t peer_id, const Buffer<u8> &data);
- void ack(session_t peer_id_, u8 channelnum_, const Buffer<u8> &data_)
- {
- type = CONCMD_ACK;
- peer_id = peer_id_;
- channelnum = channelnum_;
- data = data_;
- reliable = false;
- }
+private:
+ ConnectionCommand(ConnectionCommandType type_) :
+ type(type_) {}
- void createPeer(session_t peer_id_, const Buffer<u8> &data_)
- {
- type = CONCMD_CREATE_PEER;
- peer_id = peer_id_;
- data = data_;
- channelnum = 0;
- reliable = true;
- raw = true;
- }
+ static ConnectionCommandPtr create(ConnectionCommandType type);
};
/* maximum window size to use, 0xFFFF is theoretical maximum. don't think about
@@ -402,10 +379,10 @@ public:
ReliablePacketBuffer outgoing_reliables_sent;
//queued reliable packets
- std::queue<BufferedPacket> queued_reliables;
+ std::queue<BufferedPacketPtr> queued_reliables;
//queue commands prior splitting to packets
- std::deque<ConnectionCommand> queued_commands;
+ std::deque<ConnectionCommandPtr> queued_commands;
IncomingSplitBuffer incoming_splits;
@@ -420,34 +397,38 @@ public:
void UpdateTimers(float dtime);
- const float getCurrentDownloadRateKB()
+ float getCurrentDownloadRateKB()
{ MutexAutoLock lock(m_internal_mutex); return cur_kbps; };
- const float getMaxDownloadRateKB()
+ float getMaxDownloadRateKB()
{ MutexAutoLock lock(m_internal_mutex); return max_kbps; };
- const float getCurrentLossRateKB()
+ float getCurrentLossRateKB()
{ MutexAutoLock lock(m_internal_mutex); return cur_kbps_lost; };
- const float getMaxLossRateKB()
+ float getMaxLossRateKB()
{ MutexAutoLock lock(m_internal_mutex); return max_kbps_lost; };
- const float getCurrentIncomingRateKB()
+ float getCurrentIncomingRateKB()
{ MutexAutoLock lock(m_internal_mutex); return cur_incoming_kbps; };
- const float getMaxIncomingRateKB()
+ float getMaxIncomingRateKB()
{ MutexAutoLock lock(m_internal_mutex); return max_incoming_kbps; };
- const float getAvgDownloadRateKB()
+ float getAvgDownloadRateKB()
{ MutexAutoLock lock(m_internal_mutex); return avg_kbps; };
- const float getAvgLossRateKB()
+ float getAvgLossRateKB()
{ MutexAutoLock lock(m_internal_mutex); return avg_kbps_lost; };
- const float getAvgIncomingRateKB()
+ float getAvgIncomingRateKB()
{ MutexAutoLock lock(m_internal_mutex); return avg_incoming_kbps; };
- const unsigned int getWindowSize() const { return window_size; };
+ u16 getWindowSize() const { return m_window_size; };
+
+ void setWindowSize(long size)
+ {
+ m_window_size = (u16)rangelim(size, MIN_RELIABLE_WINDOW_SIZE, MAX_RELIABLE_WINDOW_SIZE);
+ }
- void setWindowSize(unsigned int size) { window_size = size; };
private:
std::mutex m_internal_mutex;
- int window_size = MIN_RELIABLE_WINDOW_SIZE;
+ u16 m_window_size = MIN_RELIABLE_WINDOW_SIZE;
u16 next_incoming_seqnum = SEQNUM_INITIAL;
@@ -510,7 +491,7 @@ class Peer {
public:
friend class PeerHelper;
- Peer(Address address_,u16 id_,Connection* connection) :
+ Peer(Address address_,session_t id_,Connection* connection) :
id(id_),
m_connection(connection),
address(address_),
@@ -524,11 +505,11 @@ class Peer {
};
// Unique id of the peer
- u16 id;
+ const session_t id;
void Drop();
- virtual void PutReliableSendCommand(ConnectionCommand &c,
+ virtual void PutReliableSendCommand(ConnectionCommandPtr &c,
unsigned int max_packet_size) {};
virtual bool getAddress(MTProtocols type, Address& toset) = 0;
@@ -545,7 +526,7 @@ class Peer {
virtual u16 getNextSplitSequenceNumber(u8 channel) { return 0; };
virtual void setNextSplitSequenceNumber(u8 channel, u16 seqnum) {};
- virtual SharedBuffer<u8> addSplitPacket(u8 channel, const BufferedPacket &toadd,
+ virtual SharedBuffer<u8> addSplitPacket(u8 channel, BufferedPacketPtr &toadd,
bool reliable)
{
errorstream << "Peer::addSplitPacket called,"
@@ -582,7 +563,7 @@ class Peer {
bool IncUseCount();
void DecUseCount();
- std::mutex m_exclusive_access_mutex;
+ mutable std::mutex m_exclusive_access_mutex;
bool m_pending_deletion = false;
@@ -630,7 +611,7 @@ public:
UDPPeer(u16 a_id, Address a_address, Connection* connection);
virtual ~UDPPeer() = default;
- void PutReliableSendCommand(ConnectionCommand &c,
+ void PutReliableSendCommand(ConnectionCommandPtr &c,
unsigned int max_packet_size);
bool getAddress(MTProtocols type, Address& toset);
@@ -638,7 +619,7 @@ public:
u16 getNextSplitSequenceNumber(u8 channel);
void setNextSplitSequenceNumber(u8 channel, u16 seqnum);
- SharedBuffer<u8> addSplitPacket(u8 channel, const BufferedPacket &toadd,
+ SharedBuffer<u8> addSplitPacket(u8 channel, BufferedPacketPtr &toadd,
bool reliable);
protected:
@@ -667,7 +648,7 @@ private:
float resend_timeout = 0.5;
bool processReliableSendCommand(
- ConnectionCommand &c,
+ ConnectionCommandPtr &c_ptr,
unsigned int max_packet_size);
};
@@ -675,7 +656,7 @@ private:
Connection
*/
-enum ConnectionEventType{
+enum ConnectionEventType {
CONNEVENT_NONE,
CONNEVENT_DATA_RECEIVED,
CONNEVENT_PEER_ADDED,
@@ -683,56 +664,32 @@ enum ConnectionEventType{
CONNEVENT_BIND_FAILED,
};
+struct ConnectionEvent;
+typedef std::shared_ptr<ConnectionEvent> ConnectionEventPtr;
+
+// This is very similar to ConnectionCommand
struct ConnectionEvent
{
- enum ConnectionEventType type = CONNEVENT_NONE;
+ const ConnectionEventType type;
session_t peer_id = 0;
Buffer<u8> data;
bool timeout = false;
Address address;
- ConnectionEvent() = default;
+ // We don't want to copy "data"
+ DISABLE_CLASS_COPY(ConnectionEvent);
- const char *describe() const
- {
- switch(type) {
- case CONNEVENT_NONE:
- return "CONNEVENT_NONE";
- case CONNEVENT_DATA_RECEIVED:
- return "CONNEVENT_DATA_RECEIVED";
- case CONNEVENT_PEER_ADDED:
- return "CONNEVENT_PEER_ADDED";
- case CONNEVENT_PEER_REMOVED:
- return "CONNEVENT_PEER_REMOVED";
- case CONNEVENT_BIND_FAILED:
- return "CONNEVENT_BIND_FAILED";
- }
- return "Invalid ConnectionEvent";
- }
+ static ConnectionEventPtr create(ConnectionEventType type);
+ static ConnectionEventPtr dataReceived(session_t peer_id, const Buffer<u8> &data);
+ static ConnectionEventPtr peerAdded(session_t peer_id, Address address);
+ static ConnectionEventPtr peerRemoved(session_t peer_id, bool is_timeout, Address address);
+ static ConnectionEventPtr bindFailed();
- void dataReceived(session_t peer_id_, const Buffer<u8> &data_)
- {
- type = CONNEVENT_DATA_RECEIVED;
- peer_id = peer_id_;
- data = data_;
- }
- void peerAdded(session_t peer_id_, Address address_)
- {
- type = CONNEVENT_PEER_ADDED;
- peer_id = peer_id_;
- address = address_;
- }
- void peerRemoved(session_t peer_id_, bool timeout_, Address address_)
- {
- type = CONNEVENT_PEER_REMOVED;
- peer_id = peer_id_;
- timeout = timeout_;
- address = address_;
- }
- void bindFailed()
- {
- type = CONNEVENT_BIND_FAILED;
- }
+ const char *describe() const;
+
+private:
+ ConnectionEvent(ConnectionEventType type_) :
+ type(type_) {}
};
class PeerHandler;
@@ -748,10 +705,9 @@ public:
~Connection();
/* Interface */
- ConnectionEvent waitEvent(u32 timeout_ms);
- // Warning: creates an unnecessary copy, prefer putCommand(T&&) if possible
- void putCommand(const ConnectionCommand &c);
- void putCommand(ConnectionCommand &&c);
+ ConnectionEventPtr waitEvent(u32 timeout_ms);
+
+ void putCommand(ConnectionCommandPtr c);
void SetTimeoutMs(u32 timeout) { m_bc_receive_timeout = timeout; }
void Serve(Address bind_addr);
@@ -765,7 +721,7 @@ public:
Address GetPeerAddress(session_t peer_id);
float getPeerStat(session_t peer_id, rtt_stat_type type);
float getLocalStat(rate_stat_type type);
- const u32 GetProtocolID() const { return m_protocol_id; };
+ u32 GetProtocolID() const { return m_protocol_id; };
const std::string getDesc();
void DisconnectPeer(session_t peer_id);
@@ -781,8 +737,6 @@ protected:
void sendAck(session_t peer_id, u8 channelnum, u16 seqnum);
- void PrintInfo(std::ostream &out);
-
std::vector<session_t> getPeerIDs()
{
MutexAutoLock peerlock(m_peers_mutex);
@@ -791,13 +745,11 @@ protected:
UDPSocket m_udpSocket;
// Command queue: user -> SendThread
- MutexedQueue<ConnectionCommand> m_command_queue;
+ MutexedQueue<ConnectionCommandPtr> m_command_queue;
bool Receive(NetworkPacket *pkt, u32 timeout);
- // Warning: creates an unnecessary copy, prefer putEvent(T&&) if possible
- void putEvent(const ConnectionEvent &e);
- void putEvent(ConnectionEvent &&e);
+ void putEvent(ConnectionEventPtr e);
void TriggerSend();
@@ -807,7 +759,7 @@ protected:
}
private:
// Event queue: ReceiveThread -> user
- MutexedQueue<ConnectionEvent> m_event_queue;
+ MutexedQueue<ConnectionEventPtr> m_event_queue;
session_t m_peer_id = 0;
u32 m_protocol_id;
@@ -819,7 +771,7 @@ private:
std::unique_ptr<ConnectionSendThread> m_sendThread;
std::unique_ptr<ConnectionReceiveThread> m_receiveThread;
- std::mutex m_info_mutex;
+ mutable std::mutex m_info_mutex;
// Backwards compatibility
PeerHandler *m_bc_peerhandler;
diff --git a/src/network/connectionthreads.cpp b/src/network/connectionthreads.cpp
index a306ced9b..dca065ae1 100644
--- a/src/network/connectionthreads.cpp
+++ b/src/network/connectionthreads.cpp
@@ -50,11 +50,11 @@ std::mutex log_conthread_mutex;
#define WINDOW_SIZE 5
-static session_t readPeerId(u8 *packetdata)
+static session_t readPeerId(const u8 *packetdata)
{
return readU16(&packetdata[4]);
}
-static u8 readChannel(u8 *packetdata)
+static u8 readChannel(const u8 *packetdata)
{
return readU8(&packetdata[6]);
}
@@ -114,9 +114,9 @@ void *ConnectionSendThread::run()
}
/* translate commands to packets */
- ConnectionCommand c = m_connection->m_command_queue.pop_frontNoEx(0);
- while (c.type != CONNCMD_NONE) {
- if (c.reliable)
+ auto c = m_connection->m_command_queue.pop_frontNoEx(0);
+ while (c && c->type != CONNCMD_NONE) {
+ if (c->reliable)
processReliableCommand(c);
else
processNonReliableCommand(c);
@@ -227,21 +227,21 @@ void ConnectionSendThread::runTimeouts(float dtime)
m_iteration_packets_avaialble -= timed_outs.size();
for (const auto &k : timed_outs) {
- u8 channelnum = readChannel(*k.data);
- u16 seqnum = readU16(&(k.data[BASE_HEADER_SIZE + 1]));
+ u8 channelnum = readChannel(k->data);
+ u16 seqnum = k->getSeqnum();
- channel.UpdateBytesLost(k.data.getSize());
+ channel.UpdateBytesLost(k->size());
LOG(derr_con << m_connection->getDesc()
<< "RE-SENDING timed-out RELIABLE to "
- << k.address.serializeString()
+ << k->address.serializeString()
<< "(t/o=" << resend_timeout << "): "
- << "count=" << k.resend_count
+ << "count=" << k->resend_count
<< ", channel=" << ((int) channelnum & 0xff)
<< ", seqnum=" << seqnum
<< std::endl);
- rawSend(k);
+ rawSend(k.get());
// do not handle rtt here as we can't decide if this packet was
// lost or really takes more time to transmit
@@ -274,25 +274,24 @@ void ConnectionSendThread::runTimeouts(float dtime)
}
}
-void ConnectionSendThread::rawSend(const BufferedPacket &packet)
+void ConnectionSendThread::rawSend(const BufferedPacket *p)
{
try {
- m_connection->m_udpSocket.Send(packet.address, *packet.data,
- packet.data.getSize());
+ m_connection->m_udpSocket.Send(p->address, p->data, p->size());
LOG(dout_con << m_connection->getDesc()
- << " rawSend: " << packet.data.getSize()
+ << " rawSend: " << p->size()
<< " bytes sent" << std::endl);
} catch (SendFailedException &e) {
LOG(derr_con << m_connection->getDesc()
<< "Connection::rawSend(): SendFailedException: "
- << packet.address.serializeString() << std::endl);
+ << p->address.serializeString() << std::endl);
}
}
-void ConnectionSendThread::sendAsPacketReliable(BufferedPacket &p, Channel *channel)
+void ConnectionSendThread::sendAsPacketReliable(BufferedPacketPtr &p, Channel *channel)
{
try {
- p.absolute_send_time = porting::getTimeMs();
+ p->absolute_send_time = porting::getTimeMs();
// Buffer the packet
channel->outgoing_reliables_sent.insert(p,
(channel->readOutgoingSequenceNumber() - MAX_RELIABLE_WINDOW_SIZE)
@@ -305,7 +304,7 @@ void ConnectionSendThread::sendAsPacketReliable(BufferedPacket &p, Channel *chan
}
// Send the packet
- rawSend(p);
+ rawSend(p.get());
}
bool ConnectionSendThread::rawSendAsPacket(session_t peer_id, u8 channelnum,
@@ -321,11 +320,10 @@ bool ConnectionSendThread::rawSendAsPacket(session_t peer_id, u8 channelnum,
Channel *channel = &(dynamic_cast<UDPPeer *>(&peer)->channels[channelnum]);
if (reliable) {
- bool have_sequence_number_for_raw_packet = true;
- u16 seqnum =
- channel->getOutgoingSequenceNumber(have_sequence_number_for_raw_packet);
+ bool have_seqnum = false;
+ const u16 seqnum = channel->getOutgoingSequenceNumber(have_seqnum);
- if (!have_sequence_number_for_raw_packet)
+ if (!have_seqnum)
return false;
SharedBuffer<u8> reliable = makeReliablePacket(data, seqnum);
@@ -333,13 +331,12 @@ bool ConnectionSendThread::rawSendAsPacket(session_t peer_id, u8 channelnum,
peer->getAddress(MTP_MINETEST_RELIABLE_UDP, peer_address);
// Add base headers and make a packet
- BufferedPacket p = con::makePacket(peer_address, reliable,
+ BufferedPacketPtr p = con::makePacket(peer_address, reliable,
m_connection->GetProtocolID(), m_connection->GetPeerID(),
channelnum);
// first check if our send window is already maxed out
- if (channel->outgoing_reliables_sent.size()
- < channel->getWindowSize()) {
+ if (channel->outgoing_reliables_sent.size() < channel->getWindowSize()) {
LOG(dout_con << m_connection->getDesc()
<< " INFO: sending a reliable packet to peer_id " << peer_id
<< " channel: " << (u32)channelnum
@@ -352,19 +349,19 @@ bool ConnectionSendThread::rawSendAsPacket(session_t peer_id, u8 channelnum,
<< " INFO: queueing reliable packet for peer_id: " << peer_id
<< " channel: " << (u32)channelnum
<< " seqnum: " << seqnum << std::endl);
- channel->queued_reliables.push(std::move(p));
+ channel->queued_reliables.push(p);
return false;
}
Address peer_address;
if (peer->getAddress(MTP_UDP, peer_address)) {
// Add base headers and make a packet
- BufferedPacket p = con::makePacket(peer_address, data,
+ BufferedPacketPtr p = con::makePacket(peer_address, data,
m_connection->GetProtocolID(), m_connection->GetPeerID(),
channelnum);
// Send the packet
- rawSend(p);
+ rawSend(p.get());
return true;
}
@@ -374,11 +371,11 @@ bool ConnectionSendThread::rawSendAsPacket(session_t peer_id, u8 channelnum,
return false;
}
-void ConnectionSendThread::processReliableCommand(ConnectionCommand &c)
+void ConnectionSendThread::processReliableCommand(ConnectionCommandPtr &c)
{
- assert(c.reliable); // Pre-condition
+ assert(c->reliable); // Pre-condition
- switch (c.type) {
+ switch (c->type) {
case CONNCMD_NONE:
LOG(dout_con << m_connection->getDesc()
<< "UDP processing reliable CONNCMD_NONE" << std::endl);
@@ -399,7 +396,7 @@ void ConnectionSendThread::processReliableCommand(ConnectionCommand &c)
case CONCMD_CREATE_PEER:
LOG(dout_con << m_connection->getDesc()
<< "UDP processing reliable CONCMD_CREATE_PEER" << std::endl);
- if (!rawSendAsPacket(c.peer_id, c.channelnum, c.data, c.reliable)) {
+ if (!rawSendAsPacket(c->peer_id, c->channelnum, c->data, c->reliable)) {
/* put to queue if we couldn't send it immediately */
sendReliable(c);
}
@@ -412,13 +409,14 @@ void ConnectionSendThread::processReliableCommand(ConnectionCommand &c)
FATAL_ERROR("Got command that shouldn't be reliable as reliable command");
default:
LOG(dout_con << m_connection->getDesc()
- << " Invalid reliable command type: " << c.type << std::endl);
+ << " Invalid reliable command type: " << c->type << std::endl);
}
}
-void ConnectionSendThread::processNonReliableCommand(ConnectionCommand &c)
+void ConnectionSendThread::processNonReliableCommand(ConnectionCommandPtr &c_ptr)
{
+ const ConnectionCommand &c = *c_ptr;
assert(!c.reliable); // Pre-condition
switch (c.type) {
@@ -480,9 +478,7 @@ void ConnectionSendThread::serve(Address bind_address)
}
catch (SocketException &e) {
// Create event
- ConnectionEvent ce;
- ce.bindFailed();
- m_connection->putEvent(ce);
+ m_connection->putEvent(ConnectionEvent::bindFailed());
}
}
@@ -495,9 +491,7 @@ void ConnectionSendThread::connect(Address address)
UDPPeer *peer = m_connection->createServerPeer(address);
// Create event
- ConnectionEvent e;
- e.peerAdded(peer->id, peer->address);
- m_connection->putEvent(e);
+ m_connection->putEvent(ConnectionEvent::peerAdded(peer->id, peer->address));
Address bind_addr;
@@ -586,9 +580,9 @@ void ConnectionSendThread::send(session_t peer_id, u8 channelnum,
}
}
-void ConnectionSendThread::sendReliable(ConnectionCommand &c)
+void ConnectionSendThread::sendReliable(ConnectionCommandPtr &c)
{
- PeerHelper peer = m_connection->getPeerNoEx(c.peer_id);
+ PeerHelper peer = m_connection->getPeerNoEx(c->peer_id);
if (!peer)
return;
@@ -604,7 +598,7 @@ void ConnectionSendThread::sendToAll(u8 channelnum, const SharedBuffer<u8> &data
}
}
-void ConnectionSendThread::sendToAllReliable(ConnectionCommand &c)
+void ConnectionSendThread::sendToAllReliable(ConnectionCommandPtr &c)
{
std::vector<session_t> peerids = m_connection->getPeerIDs();
@@ -663,8 +657,12 @@ void ConnectionSendThread::sendPackets(float dtime)
// first send queued reliable packets for all peers (if possible)
for (unsigned int i = 0; i < CHANNEL_COUNT; i++) {
Channel &channel = udpPeer->channels[i];
- u16 next_to_ack = 0;
+ // Reduces logging verbosity
+ if (channel.queued_reliables.empty())
+ continue;
+
+ u16 next_to_ack = 0;
channel.outgoing_reliables_sent.getFirstSeqnum(next_to_ack);
u16 next_to_receive = 0;
channel.incoming_reliables.getFirstSeqnum(next_to_receive);
@@ -694,13 +692,13 @@ void ConnectionSendThread::sendPackets(float dtime)
channel.outgoing_reliables_sent.size()
< channel.getWindowSize() &&
peer->m_increment_packets_remaining > 0) {
- BufferedPacket p = std::move(channel.queued_reliables.front());
+ BufferedPacketPtr p = channel.queued_reliables.front();
channel.queued_reliables.pop();
LOG(dout_con << m_connection->getDesc()
<< " INFO: sending a queued reliable packet "
<< " channel: " << i
- << ", seqnum: " << readU16(&p.data[BASE_HEADER_SIZE + 1])
+ << ", seqnum: " << p->getSeqnum()
<< std::endl);
sendAsPacketReliable(p, &channel);
@@ -881,17 +879,14 @@ void ConnectionReceiveThread::receive(SharedBuffer<u8> &packetdata,
try {
// First, see if there any buffered packets we can process now
if (packet_queued) {
- bool data_left = true;
session_t peer_id;
SharedBuffer<u8> resultdata;
- while (data_left) {
+ while (true) {
try {
- data_left = getFromBuffers(peer_id, resultdata);
- if (data_left) {
- ConnectionEvent e;
- e.dataReceived(peer_id, resultdata);
- m_connection->putEvent(std::move(e));
- }
+ if (!getFromBuffers(peer_id, resultdata))
+ break;
+
+ m_connection->putEvent(ConnectionEvent::dataReceived(peer_id, resultdata));
}
catch (ProcessedSilentlyException &e) {
/* try reading again */
@@ -908,7 +903,7 @@ void ConnectionReceiveThread::receive(SharedBuffer<u8> &packetdata,
return;
if ((received_size < BASE_HEADER_SIZE) ||
- (readU32(&packetdata[0]) != m_connection->GetProtocolID())) {
+ (readU32(&packetdata[0]) != m_connection->GetProtocolID())) {
LOG(derr_con << m_connection->getDesc()
<< "Receive(): Invalid incoming packet, "
<< "size: " << received_size
@@ -999,9 +994,7 @@ void ConnectionReceiveThread::receive(SharedBuffer<u8> &packetdata,
<< ", channel: " << (u32)channelnum << ", returned "
<< resultdata.getSize() << " bytes" << std::endl);
- ConnectionEvent e;
- e.dataReceived(peer_id, resultdata);
- m_connection->putEvent(std::move(e));
+ m_connection->putEvent(ConnectionEvent::dataReceived(peer_id, resultdata));
}
catch (ProcessedSilentlyException &e) {
}
@@ -1026,10 +1019,11 @@ bool ConnectionReceiveThread::getFromBuffers(session_t &peer_id, SharedBuffer<u8
if (!peer)
continue;
- if (dynamic_cast<UDPPeer *>(&peer) == 0)
+ UDPPeer *p = dynamic_cast<UDPPeer *>(&peer);
+ if (!p)
continue;
- for (Channel &channel : (dynamic_cast<UDPPeer *>(&peer))->channels) {
+ for (Channel &channel : p->channels) {
if (checkIncomingBuffers(&channel, peer_id, dst)) {
return true;
}
@@ -1042,32 +1036,34 @@ bool ConnectionReceiveThread::checkIncomingBuffers(Channel *channel,
session_t &peer_id, SharedBuffer<u8> &dst)
{
u16 firstseqnum = 0;
- if (channel->incoming_reliables.getFirstSeqnum(firstseqnum)) {
- if (firstseqnum == channel->readNextIncomingSeqNum()) {
- BufferedPacket p = channel->incoming_reliables.popFirst();
- peer_id = readPeerId(*p.data);
- u8 channelnum = readChannel(*p.data);
- u16 seqnum = readU16(&p.data[BASE_HEADER_SIZE + 1]);
+ if (!channel->incoming_reliables.getFirstSeqnum(firstseqnum))
+ return false;
- LOG(dout_con << m_connection->getDesc()
- << "UNBUFFERING TYPE_RELIABLE"
- << " seqnum=" << seqnum
- << " peer_id=" << peer_id
- << " channel=" << ((int) channelnum & 0xff)
- << std::endl);
+ if (firstseqnum != channel->readNextIncomingSeqNum())
+ return false;
- channel->incNextIncomingSeqNum();
+ BufferedPacketPtr p = channel->incoming_reliables.popFirst();
- u32 headers_size = BASE_HEADER_SIZE + RELIABLE_HEADER_SIZE;
- // Get out the inside packet and re-process it
- SharedBuffer<u8> payload(p.data.getSize() - headers_size);
- memcpy(*payload, &p.data[headers_size], payload.getSize());
+ peer_id = readPeerId(p->data); // Carried over to caller function
+ u8 channelnum = readChannel(p->data);
+ u16 seqnum = p->getSeqnum();
- dst = processPacket(channel, payload, peer_id, channelnum, true);
- return true;
- }
- }
- return false;
+ LOG(dout_con << m_connection->getDesc()
+ << "UNBUFFERING TYPE_RELIABLE"
+ << " seqnum=" << seqnum
+ << " peer_id=" << peer_id
+ << " channel=" << ((int) channelnum & 0xff)
+ << std::endl);
+
+ channel->incNextIncomingSeqNum();
+
+ u32 headers_size = BASE_HEADER_SIZE + RELIABLE_HEADER_SIZE;
+ // Get out the inside packet and re-process it
+ SharedBuffer<u8> payload(p->size() - headers_size);
+ memcpy(*payload, &p->data[headers_size], payload.getSize());
+
+ dst = processPacket(channel, payload, peer_id, channelnum, true);
+ return true;
}
SharedBuffer<u8> ConnectionReceiveThread::processPacket(Channel *channel,
@@ -1115,7 +1111,7 @@ SharedBuffer<u8> ConnectionReceiveThread::handlePacketType_Control(Channel *chan
if (packetdata.getSize() < 2)
throw InvalidIncomingDataException("packetdata.getSize() < 2");
- u8 controltype = readU8(&(packetdata[1]));
+ ControlType controltype = (ControlType)readU8(&(packetdata[1]));
if (controltype == CONTROLTYPE_ACK) {
assert(channel != NULL);
@@ -1131,7 +1127,7 @@ SharedBuffer<u8> ConnectionReceiveThread::handlePacketType_Control(Channel *chan
<< seqnum << " ]" << std::endl);
try {
- BufferedPacket p = channel->outgoing_reliables_sent.popSeqnum(seqnum);
+ BufferedPacketPtr p = channel->outgoing_reliables_sent.popSeqnum(seqnum);
// the rtt calculation will be a bit off for re-sent packets but that's okay
{
@@ -1140,14 +1136,14 @@ SharedBuffer<u8> ConnectionReceiveThread::handlePacketType_Control(Channel *chan
// a overflow is quite unlikely but as it'd result in major
// rtt miscalculation we handle it here
- if (current_time > p.absolute_send_time) {
- float rtt = (current_time - p.absolute_send_time) / 1000.0;
+ if (current_time > p->absolute_send_time) {
+ float rtt = (current_time - p->absolute_send_time) / 1000.0;
// Let peer calculate stuff according to it
// (avg_rtt and resend_timeout)
dynamic_cast<UDPPeer *>(peer)->reportRTT(rtt);
- } else if (p.totaltime > 0) {
- float rtt = p.totaltime;
+ } else if (p->totaltime > 0) {
+ float rtt = p->totaltime;
// Let peer calculate stuff according to it
// (avg_rtt and resend_timeout)
@@ -1156,7 +1152,7 @@ SharedBuffer<u8> ConnectionReceiveThread::handlePacketType_Control(Channel *chan
}
// put bytes for max bandwidth calculation
- channel->UpdateBytesSent(p.data.getSize(), 1);
+ channel->UpdateBytesSent(p->size(), 1);
if (channel->outgoing_reliables_sent.size() == 0)
m_connection->TriggerSend();
} catch (NotFoundException &e) {
@@ -1204,7 +1200,7 @@ SharedBuffer<u8> ConnectionReceiveThread::handlePacketType_Control(Channel *chan
throw ProcessedSilentlyException("Got a DISCO");
} else {
LOG(derr_con << m_connection->getDesc()
- << "INVALID TYPE_CONTROL: invalid controltype="
+ << "INVALID controltype="
<< ((int) controltype & 0xff) << std::endl);
throw InvalidIncomingDataException("Invalid control type");
}
@@ -1232,7 +1228,7 @@ SharedBuffer<u8> ConnectionReceiveThread::handlePacketType_Split(Channel *channe
if (peer->getAddress(MTP_UDP, peer_address)) {
// We have to create a packet again for buffering
// This isn't actually too bad an idea.
- BufferedPacket packet = makePacket(peer_address,
+ BufferedPacketPtr packet = con::makePacket(peer_address,
packetdata,
m_connection->GetProtocolID(),
peer->id,
@@ -1267,7 +1263,7 @@ SharedBuffer<u8> ConnectionReceiveThread::handlePacketType_Reliable(Channel *cha
if (packetdata.getSize() < RELIABLE_HEADER_SIZE)
throw InvalidIncomingDataException("packetdata.getSize() < RELIABLE_HEADER_SIZE");
- u16 seqnum = readU16(&packetdata[1]);
+ const u16 seqnum = readU16(&packetdata[1]);
bool is_future_packet = false;
bool is_old_packet = false;
@@ -1311,7 +1307,7 @@ SharedBuffer<u8> ConnectionReceiveThread::handlePacketType_Reliable(Channel *cha
// This one comes later, buffer it.
// Actually we have to make a packet to buffer one.
// Well, we have all the ingredients, so just do it.
- BufferedPacket packet = con::makePacket(
+ BufferedPacketPtr packet = con::makePacket(
peer_address,
packetdata,
m_connection->GetProtocolID(),
@@ -1328,9 +1324,7 @@ SharedBuffer<u8> ConnectionReceiveThread::handlePacketType_Reliable(Channel *cha
throw ProcessedQueued("Buffered future reliable packet");
} catch (AlreadyExistsException &e) {
} catch (IncomingDataCorruption &e) {
- ConnectionCommand discon;
- discon.disconnect_peer(peer->id);
- m_connection->putCommand(discon);
+ m_connection->putCommand(ConnectionCommand::disconnect_peer(peer->id));
LOG(derr_con << m_connection->getDesc()
<< "INVALID, TYPE_RELIABLE peer_id: " << peer->id
@@ -1351,7 +1345,7 @@ SharedBuffer<u8> ConnectionReceiveThread::handlePacketType_Reliable(Channel *cha
u16 queued_seqnum = 0;
if (channel->incoming_reliables.getFirstSeqnum(queued_seqnum)) {
if (queued_seqnum == seqnum) {
- BufferedPacket queued_packet = channel->incoming_reliables.popFirst();
+ BufferedPacketPtr queued_packet = channel->incoming_reliables.popFirst();
/** TODO find a way to verify the new against the old packet */
}
}
diff --git a/src/network/connectionthreads.h b/src/network/connectionthreads.h
index 612407c3b..c2e2dae12 100644
--- a/src/network/connectionthreads.h
+++ b/src/network/connectionthreads.h
@@ -29,6 +29,25 @@ namespace con
class Connection;
+struct OutgoingPacket
+{
+ session_t peer_id;
+ u8 channelnum;
+ SharedBuffer<u8> data;
+ bool reliable;
+ bool ack;
+
+ OutgoingPacket(session_t peer_id_, u8 channelnum_, const SharedBuffer<u8> &data_,
+ bool reliable_,bool ack_=false):
+ peer_id(peer_id_),
+ channelnum(channelnum_),
+ data(data_),
+ reliable(reliable_),
+ ack(ack_)
+ {
+ }
+};
+
class ConnectionSendThread : public Thread
{
@@ -51,27 +70,27 @@ public:
private:
void runTimeouts(float dtime);
- void rawSend(const BufferedPacket &packet);
+ void rawSend(const BufferedPacket *p);
bool rawSendAsPacket(session_t peer_id, u8 channelnum,
const SharedBuffer<u8> &data, bool reliable);
- void processReliableCommand(ConnectionCommand &c);
- void processNonReliableCommand(ConnectionCommand &c);
+ void processReliableCommand(ConnectionCommandPtr &c);
+ void processNonReliableCommand(ConnectionCommandPtr &c);
void serve(Address bind_address);
void connect(Address address);
void disconnect();
void disconnect_peer(session_t peer_id);
void send(session_t peer_id, u8 channelnum, const SharedBuffer<u8> &data);
- void sendReliable(ConnectionCommand &c);
+ void sendReliable(ConnectionCommandPtr &c);
void sendToAll(u8 channelnum, const SharedBuffer<u8> &data);
- void sendToAllReliable(ConnectionCommand &c);
+ void sendToAllReliable(ConnectionCommandPtr &c);
void sendPackets(float dtime);
void sendAsPacket(session_t peer_id, u8 channelnum, const SharedBuffer<u8> &data,
bool ack = false);
- void sendAsPacketReliable(BufferedPacket &p, Channel *channel);
+ void sendAsPacketReliable(BufferedPacketPtr &p, Channel *channel);
bool packetsQueued();
diff --git a/src/network/networkpacket.h b/src/network/networkpacket.h
index b1c44f055..b9c39f332 100644
--- a/src/network/networkpacket.h
+++ b/src/network/networkpacket.h
@@ -41,7 +41,7 @@ public:
u32 getSize() const { return m_datasize; }
session_t getPeerId() const { return m_peer_id; }
u16 getCommand() { return m_command; }
- const u32 getRemainingBytes() const { return m_datasize - m_read_offset; }
+ u32 getRemainingBytes() const { return m_datasize - m_read_offset; }
const char *getRemainingString() { return getString(m_read_offset); }
// Returns a c-string without copying.
diff --git a/src/network/networkprotocol.h b/src/network/networkprotocol.h
index 838bf0b2c..a5ff53216 100644
--- a/src/network/networkprotocol.h
+++ b/src/network/networkprotocol.h
@@ -205,9 +205,11 @@ with this program; if not, write to the Free Software Foundation, Inc.,
Updated set_sky packet
Adds new sun, moon and stars packets
Minimap modes
+ PROTOCOL VERSION 40:
+ TOCLIENT_MEDIA_PUSH changed, TOSERVER_HAVE_MEDIA added
*/
-#define LATEST_PROTOCOL_VERSION 39
+#define LATEST_PROTOCOL_VERSION 40
#define LATEST_PROTOCOL_VERSION_STRING TOSTRING(LATEST_PROTOCOL_VERSION)
// Server's supported network protocol range
@@ -227,7 +229,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
// base64-encoded SHA-1 (27+\0).
// See also: Formspec Version History in doc/lua_api.txt
-#define FORMSPEC_API_VERSION 4
+#define FORMSPEC_API_VERSION 5
#define TEXTURENAME_ALLOWED_CHARS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_.-"
@@ -315,9 +317,8 @@ enum ToClientCommand
/*
std::string raw_hash
std::string filename
+ u32 callback_token
bool should_be_cached
- u32 len
- char filedata[len]
*/
// (oops, there is some gap here)
@@ -936,7 +937,13 @@ enum ToServerCommand
}
*/
- TOSERVER_RECEIVED_MEDIA = 0x41, // Obsolete
+ TOSERVER_HAVE_MEDIA = 0x41,
+ /*
+ u8 number of callback tokens
+ for each:
+ u32 token
+ */
+
TOSERVER_BREATH = 0x42, // Obsolete
TOSERVER_CLIENT_READY = 0x43,
diff --git a/src/network/serveropcodes.cpp b/src/network/serveropcodes.cpp
index aea5d7174..44b65e8da 100644
--- a/src/network/serveropcodes.cpp
+++ b/src/network/serveropcodes.cpp
@@ -89,7 +89,7 @@ const ToServerCommandHandler toServerCommandTable[TOSERVER_NUM_MSG_TYPES] =
null_command_handler, // 0x3e
null_command_handler, // 0x3f
{ "TOSERVER_REQUEST_MEDIA", TOSERVER_STATE_STARTUP, &Server::handleCommand_RequestMedia }, // 0x40
- null_command_handler, // 0x41
+ { "TOSERVER_HAVE_MEDIA", TOSERVER_STATE_INGAME, &Server::handleCommand_HaveMedia }, // 0x41
null_command_handler, // 0x42
{ "TOSERVER_CLIENT_READY", TOSERVER_STATE_STARTUP, &Server::handleCommand_ClientReady }, // 0x43
null_command_handler, // 0x44
@@ -167,7 +167,7 @@ const ClientCommandFactory clientCommandFactoryTable[TOCLIENT_NUM_MSG_TYPES] =
{ "TOCLIENT_TIME_OF_DAY", 0, true }, // 0x29
{ "TOCLIENT_CSM_RESTRICTION_FLAGS", 0, true }, // 0x2A
{ "TOCLIENT_PLAYER_SPEED", 0, true }, // 0x2B
- { "TOCLIENT_MEDIA_PUSH", 0, true }, // 0x2C (sent over channel 1 too)
+ { "TOCLIENT_MEDIA_PUSH", 0, true }, // 0x2C (sent over channel 1 too if legacy)
null_command_factory, // 0x2D
null_command_factory, // 0x2E
{ "TOCLIENT_CHAT_MESSAGE", 0, true }, // 0x2F
diff --git a/src/network/serverpackethandler.cpp b/src/network/serverpackethandler.cpp
index fd5aed9d1..12dc24460 100644
--- a/src/network/serverpackethandler.cpp
+++ b/src/network/serverpackethandler.cpp
@@ -86,7 +86,7 @@ void Server::handleCommand_Init(NetworkPacket* pkt)
// Do not allow multiple players in simple singleplayer mode.
// This isn't a perfect way to do it, but will suffice for now
- if (m_simple_singleplayer_mode && m_clients.getClientIDs().size() > 1) {
+ if (m_simple_singleplayer_mode && !m_clients.getClientIDs().empty()) {
infostream << "Server: Not allowing another client (" << addr_s <<
") to connect in simple singleplayer mode" << std::endl;
DenyAccess(peer_id, SERVER_ACCESSDENIED_SINGLEPLAYER);
@@ -174,6 +174,16 @@ void Server::handleCommand_Init(NetworkPacket* pkt)
return;
}
+ RemotePlayer *player = m_env->getPlayer(playername);
+
+ // If player is already connected, cancel
+ if (player && player->getPeerId() != PEER_ID_INEXISTENT) {
+ actionstream << "Server: Player with name \"" << playername <<
+ "\" tried to connect, but player with same name is already connected" << std::endl;
+ DenyAccess(peer_id, SERVER_ACCESSDENIED_ALREADY_CONNECTED);
+ return;
+ }
+
m_clients.setPlayerName(peer_id, playername);
//TODO (later) case insensitivity
@@ -352,16 +362,15 @@ void Server::handleCommand_RequestMedia(NetworkPacket* pkt)
session_t peer_id = pkt->getPeerId();
infostream << "Sending " << numfiles << " files to " <<
getPlayerName(peer_id) << std::endl;
- verbosestream << "TOSERVER_REQUEST_MEDIA: " << std::endl;
+ verbosestream << "TOSERVER_REQUEST_MEDIA: requested file(s)" << std::endl;
for (u16 i = 0; i < numfiles; i++) {
std::string name;
*pkt >> name;
- tosend.push_back(name);
- verbosestream << "TOSERVER_REQUEST_MEDIA: requested file "
- << name << std::endl;
+ tosend.emplace_back(name);
+ verbosestream << " " << name << std::endl;
}
sendRequestedMedia(peer_id, tosend);
@@ -473,7 +482,6 @@ void Server::process_PlayerPos(RemotePlayer *player, PlayerSAO *playersao,
f32 yaw = (f32)f32yaw / 100.0f;
u32 keyPressed = 0;
- // default behavior (in case an old client doesn't send these)
f32 fov = 0;
u8 wanted_range = 0;
@@ -499,17 +507,7 @@ void Server::process_PlayerPos(RemotePlayer *player, PlayerSAO *playersao,
playersao->setFov(fov);
playersao->setWantedRange(wanted_range);
- player->keyPressed = keyPressed;
- player->control.up = (keyPressed & (0x1 << 0));
- player->control.down = (keyPressed & (0x1 << 1));
- player->control.left = (keyPressed & (0x1 << 2));
- player->control.right = (keyPressed & (0x1 << 3));
- player->control.jump = (keyPressed & (0x1 << 4));
- player->control.aux1 = (keyPressed & (0x1 << 5));
- player->control.sneak = (keyPressed & (0x1 << 6));
- player->control.dig = (keyPressed & (0x1 << 7));
- player->control.place = (keyPressed & (0x1 << 8));
- player->control.zoom = (keyPressed & (0x1 << 9));
+ player->control.unpackKeysPressed(keyPressed);
if (playersao->checkMovementCheat()) {
// Call callbacks
@@ -821,8 +819,7 @@ void Server::handleCommand_Damage(NetworkPacket* pkt)
<< std::endl;
PlayerHPChangeReason reason(PlayerHPChangeReason::FALL);
- playersao->setHP((s32)playersao->getHP() - (s32)damage, reason);
- SendPlayerHPOrDie(playersao, reason);
+ playersao->setHP((s32)playersao->getHP() - (s32)damage, reason, true);
}
}
@@ -916,6 +913,13 @@ bool Server::checkInteractDistance(RemotePlayer *player, const f32 d, const std:
return true;
}
+// Tiny helper to retrieve the selected item into an Optional
+static inline void getWieldedItem(const PlayerSAO *playersao, Optional<ItemStack> &ret)
+{
+ ret = ItemStack();
+ playersao->getWieldedItem(&(*ret));
+}
+
void Server::handleCommand_Interact(NetworkPacket *pkt)
{
/*
@@ -1107,11 +1111,8 @@ void Server::handleCommand_Interact(NetworkPacket *pkt)
float time_from_last_punch =
playersao->resetTimeFromLastPunch();
- u16 src_original_hp = pointed_object->getHP();
- u16 dst_origin_hp = playersao->getHP();
-
- u16 wear = pointed_object->punch(dir, &toolcap, playersao,
- time_from_last_punch);
+ u32 wear = pointed_object->punch(dir, &toolcap, playersao,
+ time_from_last_punch, tool_item.wear);
// Callback may have changed item, so get it again
playersao->getWieldedItem(&selected_item);
@@ -1119,18 +1120,6 @@ void Server::handleCommand_Interact(NetworkPacket *pkt)
if (changed)
playersao->setWieldedItem(selected_item);
- // If the object is a player and its HP changed
- if (src_original_hp != pointed_object->getHP() &&
- pointed_object->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
- SendPlayerHPOrDie((PlayerSAO *)pointed_object,
- PlayerHPChangeReason(PlayerHPChangeReason::PLAYER_PUNCH, playersao));
- }
-
- // If the puncher is a player and its HP changed
- if (dst_origin_hp != playersao->getHP())
- SendPlayerHPOrDie(playersao,
- PlayerHPChangeReason(PlayerHPChangeReason::PLAYER_PUNCH, pointed_object));
-
return;
} // action == INTERACT_START_DIGGING
@@ -1176,7 +1165,8 @@ void Server::handleCommand_Interact(NetworkPacket *pkt)
// Get diggability and expected digging time
DigParams params = getDigParams(m_nodedef->get(n).groups,
- &selected_item.getToolCapabilities(m_itemdef));
+ &selected_item.getToolCapabilities(m_itemdef),
+ selected_item.wear);
// If can't dig, try hand
if (!params.diggable) {
params = getDigParams(m_nodedef->get(n).groups,
@@ -1238,14 +1228,17 @@ void Server::handleCommand_Interact(NetworkPacket *pkt)
// Place block or right-click object
case INTERACT_PLACE: {
- ItemStack selected_item;
- playersao->getWieldedItem(&selected_item, nullptr);
+ Optional<ItemStack> selected_item;
+ getWieldedItem(playersao, selected_item);
// Reset build time counter
if (pointed.type == POINTEDTHING_NODE &&
- selected_item.getDefinition(m_itemdef).type == ITEM_NODE)
+ selected_item->getDefinition(m_itemdef).type == ITEM_NODE)
getClient(peer_id)->m_time_from_building = 0.0;
+ const bool had_prediction = !selected_item->getDefinition(m_itemdef).
+ node_placement_prediction.empty();
+
if (pointed.type == POINTEDTHING_OBJECT) {
// Right click object
@@ -1258,11 +1251,9 @@ void Server::handleCommand_Interact(NetworkPacket *pkt)
<< pointed_object->getDescription() << std::endl;
// Do stuff
- if (m_script->item_OnSecondaryUse(
- selected_item, playersao, pointed)) {
- if (playersao->setWieldedItem(selected_item)) {
+ if (m_script->item_OnSecondaryUse(selected_item, playersao, pointed)) {
+ if (selected_item.has_value() && playersao->setWieldedItem(*selected_item))
SendInventory(playersao, true);
- }
}
pointed_object->rightClick(playersao);
@@ -1270,7 +1261,7 @@ void Server::handleCommand_Interact(NetworkPacket *pkt)
// Placement was handled in lua
// Apply returned ItemStack
- if (playersao->setWieldedItem(selected_item))
+ if (selected_item.has_value() && playersao->setWieldedItem(*selected_item))
SendInventory(playersao, true);
}
@@ -1282,8 +1273,7 @@ void Server::handleCommand_Interact(NetworkPacket *pkt)
RemoteClient *client = getClient(peer_id);
v3s16 blockpos = getNodeBlockPos(pointed.node_abovesurface);
v3s16 blockpos2 = getNodeBlockPos(pointed.node_undersurface);
- if (!selected_item.getDefinition(m_itemdef
- ).node_placement_prediction.empty()) {
+ if (had_prediction) {
client->SetBlockNotSent(blockpos);
if (blockpos2 != blockpos)
client->SetBlockNotSent(blockpos2);
@@ -1297,15 +1287,15 @@ void Server::handleCommand_Interact(NetworkPacket *pkt)
} // action == INTERACT_PLACE
case INTERACT_USE: {
- ItemStack selected_item;
- playersao->getWieldedItem(&selected_item, nullptr);
+ Optional<ItemStack> selected_item;
+ getWieldedItem(playersao, selected_item);
- actionstream << player->getName() << " uses " << selected_item.name
+ actionstream << player->getName() << " uses " << selected_item->name
<< ", pointing at " << pointed.dump() << std::endl;
if (m_script->item_OnUse(selected_item, playersao, pointed)) {
// Apply returned ItemStack
- if (playersao->setWieldedItem(selected_item))
+ if (selected_item.has_value() && playersao->setWieldedItem(*selected_item))
SendInventory(playersao, true);
}
@@ -1314,16 +1304,17 @@ void Server::handleCommand_Interact(NetworkPacket *pkt)
// Rightclick air
case INTERACT_ACTIVATE: {
- ItemStack selected_item;
- playersao->getWieldedItem(&selected_item, nullptr);
+ Optional<ItemStack> selected_item;
+ getWieldedItem(playersao, selected_item);
actionstream << player->getName() << " activates "
- << selected_item.name << std::endl;
+ << selected_item->name << std::endl;
pointed.type = POINTEDTHING_NOTHING; // can only ever be NOTHING
if (m_script->item_OnSecondaryUse(selected_item, playersao, pointed)) {
- if (playersao->setWieldedItem(selected_item))
+ // Apply returned ItemStack
+ if (selected_item.has_value() && playersao->setWieldedItem(*selected_item))
SendInventory(playersao, true);
}
@@ -1811,3 +1802,30 @@ void Server::handleCommand_ModChannelMsg(NetworkPacket *pkt)
broadcastModChannelMessage(channel_name, channel_msg, peer_id);
}
+
+void Server::handleCommand_HaveMedia(NetworkPacket *pkt)
+{
+ std::vector<u32> tokens;
+ u8 numtokens;
+
+ *pkt >> numtokens;
+ for (u16 i = 0; i < numtokens; i++) {
+ u32 n;
+ *pkt >> n;
+ tokens.emplace_back(n);
+ }
+
+ const session_t peer_id = pkt->getPeerId();
+ auto player = m_env->getPlayer(peer_id);
+
+ for (const u32 token : tokens) {
+ auto it = m_pending_dyn_media.find(token);
+ if (it == m_pending_dyn_media.end())
+ continue;
+ if (it->second.waiting_players.count(peer_id)) {
+ it->second.waiting_players.erase(peer_id);
+ if (player)
+ getScriptIface()->on_dynamic_media_added(token, player->getName());
+ }
+ }
+}
diff --git a/src/network/socket.cpp b/src/network/socket.cpp
index 94a9f4180..0bb7ea234 100644
--- a/src/network/socket.cpp
+++ b/src/network/socket.cpp
@@ -23,14 +23,11 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <iostream>
#include <cstdlib>
#include <cstring>
-#include <cerrno>
-#include <sstream>
#include <iomanip>
#include "util/string.h"
#include "util/numeric.h"
#include "constants.h"
#include "debug.h"
-#include "settings.h"
#include "log.h"
#ifdef _WIN32
@@ -42,9 +39,10 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <winsock2.h>
#include <ws2tcpip.h>
#define LAST_SOCKET_ERR() WSAGetLastError()
-typedef SOCKET socket_t;
+#define SOCKET_ERR_STR(e) itos(e)
typedef int socklen_t;
#else
+#include <cerrno>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
@@ -53,7 +51,7 @@ typedef int socklen_t;
#include <unistd.h>
#include <arpa/inet.h>
#define LAST_SOCKET_ERR() (errno)
-typedef int socket_t;
+#define SOCKET_ERR_STR(e) strerror(e)
#endif
// Set to true to enable verbose debug output
@@ -113,7 +111,7 @@ bool UDPSocket::init(bool ipv6, bool noExceptions)
}
throw SocketException(std::string("Failed to create socket: error ") +
- itos(LAST_SOCKET_ERR()));
+ SOCKET_ERR_STR(LAST_SOCKET_ERR()));
}
setTimeoutMs(0);
@@ -153,40 +151,40 @@ void UDPSocket::Bind(Address addr)
}
if (addr.getFamily() != m_addr_family) {
- static const char *errmsg =
+ const char *errmsg =
"Socket and bind address families do not match";
errorstream << "Bind failed: " << errmsg << std::endl;
throw SocketException(errmsg);
}
+ int ret = 0;
+
if (m_addr_family == AF_INET6) {
struct sockaddr_in6 address;
memset(&address, 0, sizeof(address));
- address = addr.getAddress6();
address.sin6_family = AF_INET6;
+ address.sin6_addr = addr.getAddress6();
address.sin6_port = htons(addr.getPort());
- if (bind(m_handle, (const struct sockaddr *)&address,
- sizeof(struct sockaddr_in6)) < 0) {
- dstream << (int)m_handle << ": Bind failed: " << strerror(errno)
- << std::endl;
- throw SocketException("Failed to bind socket");
- }
+ ret = bind(m_handle, (const struct sockaddr *) &address,
+ sizeof(struct sockaddr_in6));
} else {
struct sockaddr_in address;
memset(&address, 0, sizeof(address));
- address = addr.getAddress();
address.sin_family = AF_INET;
+ address.sin_addr = addr.getAddress();
address.sin_port = htons(addr.getPort());
- if (bind(m_handle, (const struct sockaddr *)&address,
- sizeof(struct sockaddr_in)) < 0) {
- dstream << (int)m_handle << ": Bind failed: " << strerror(errno)
- << std::endl;
- throw SocketException("Failed to bind socket");
- }
+ ret = bind(m_handle, (const struct sockaddr *) &address,
+ sizeof(struct sockaddr_in));
+ }
+
+ if (ret < 0) {
+ dstream << (int)m_handle << ": Bind failed: "
+ << SOCKET_ERR_STR(LAST_SOCKET_ERR()) << std::endl;
+ throw SocketException("Failed to bind socket");
}
}
@@ -233,13 +231,19 @@ void UDPSocket::Send(const Address &destination, const void *data, int size)
int sent;
if (m_addr_family == AF_INET6) {
- struct sockaddr_in6 address = destination.getAddress6();
+ struct sockaddr_in6 address = {0};
+ address.sin6_family = AF_INET6;
+ address.sin6_addr = destination.getAddress6();
address.sin6_port = htons(destination.getPort());
+
sent = sendto(m_handle, (const char *)data, size, 0,
(struct sockaddr *)&address, sizeof(struct sockaddr_in6));
} else {
- struct sockaddr_in address = destination.getAddress();
+ struct sockaddr_in address = {0};
+ address.sin_family = AF_INET;
+ address.sin_addr = destination.getAddress();
address.sin_port = htons(destination.getPort());
+
sent = sendto(m_handle, (const char *)data, size, 0,
(struct sockaddr *)&address, sizeof(struct sockaddr_in));
}
@@ -267,9 +271,9 @@ int UDPSocket::Receive(Address &sender, void *data, int size)
return -1;
u16 address_port = ntohs(address.sin6_port);
- IPv6AddressBytes bytes;
- memcpy(bytes.bytes, address.sin6_addr.s6_addr, 16);
- sender = Address(&bytes, address_port);
+ const auto *bytes = reinterpret_cast<IPv6AddressBytes*>
+ (address.sin6_addr.s6_addr);
+ sender = Address(bytes, address_port);
} else {
struct sockaddr_in address;
memset(&address, 0, sizeof(address));
@@ -341,7 +345,12 @@ bool UDPSocket::WaitData(int timeout_ms)
if (result == 0)
return false;
- if (result < 0 && (errno == EINTR || errno == EBADF)) {
+ int e = LAST_SOCKET_ERR();
+#ifdef _WIN32
+ if (result < 0 && (e == WSAEINTR || e == WSAEBADF)) {
+#else
+ if (result < 0 && (e == EINTR || e == EBADF)) {
+#endif
// N.B. select() fails when sockets are destroyed on Connection's dtor
// with EBADF. Instead of doing tricky synchronization, allow this
// thread to exit but don't throw an exception.
@@ -349,18 +358,9 @@ bool UDPSocket::WaitData(int timeout_ms)
}
if (result < 0) {
- dstream << m_handle << ": Select failed: " << strerror(errno)
+ dstream << (int)m_handle << ": Select failed: " << SOCKET_ERR_STR(e)
<< std::endl;
-#ifdef _WIN32
- int e = WSAGetLastError();
- dstream << (int)m_handle << ": WSAGetLastError()=" << e << std::endl;
- if (e == 10004 /* WSAEINTR */ || e == 10009 /* WSAEBADF */) {
- infostream << "Ignoring WSAEINTR/WSAEBADF." << std::endl;
- return false;
- }
-#endif
-
throw SocketException("Select failed");
} else if (!FD_ISSET(m_handle, &readset)) {
// No data
diff --git a/src/network/socket.h b/src/network/socket.h
index e0e76f4c2..d34186b44 100644
--- a/src/network/socket.h
+++ b/src/network/socket.h
@@ -19,18 +19,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#pragma once
-#ifdef _WIN32
-#ifndef _WIN32_WINNT
-#define _WIN32_WINNT 0x0501
-#endif
-#include <windows.h>
-#include <winsock2.h>
-#include <ws2tcpip.h>
-#else
-#include <sys/socket.h>
-#include <netinet/in.h>
-#endif
-
#include <ostream>
#include <cstring>
#include "address.h"
@@ -53,8 +41,6 @@ public:
bool init(bool ipv6, bool noExceptions = false);
- // void Close();
- // bool IsOpen();
void Send(const Address &destination, const void *data, int size);
// Returns -1 if there is no data
int Receive(Address &sender, void *data, int size);
diff --git a/src/nodedef.cpp b/src/nodedef.cpp
index a3b6b18c1..8a5542837 100644
--- a/src/nodedef.cpp
+++ b/src/nodedef.cpp
@@ -207,7 +207,17 @@ void TileDef::serialize(std::ostream &os, u16 protocol_version) const
u8 version = 6;
writeU8(os, version);
- os << serializeString16(name);
+ if (protocol_version > 39) {
+ os << serializeString16(name);
+ } else {
+ // Before f018737, TextureSource::getTextureAverageColor did not handle
+ // missing textures. "[png" can be used as base texture, but is not known
+ // on older clients. Hence use "blank.png" to avoid this problem.
+ if (!name.empty() && name[0] == '[')
+ os << serializeString16("blank.png^" + name);
+ else
+ os << serializeString16(name);
+ }
animation.serialize(os, version);
bool has_scale = scale > 0;
u16 flags = 0;
@@ -403,6 +413,8 @@ void ContentFeatures::reset()
palette_name = "";
palette = NULL;
node_dig_prediction = "air";
+ move_resistance = 0;
+ liquid_move_physics = false;
}
void ContentFeatures::setAlphaFromLegacy(u8 legacy_alpha)
@@ -489,7 +501,16 @@ void ContentFeatures::serialize(std::ostream &os, u16 protocol_version) const
writeU32(os, damage_per_second);
// liquid
- writeU8(os, liquid_type);
+ LiquidType liquid_type_bc = liquid_type;
+ if (protocol_version <= 39) {
+ // Since commit 7f25823, liquid drawtypes can be used even with LIQUID_NONE
+ // solution: force liquid type accordingly to accepted values
+ if (drawtype == NDT_LIQUID)
+ liquid_type_bc = LIQUID_SOURCE;
+ else if (drawtype == NDT_FLOWINGLIQUID)
+ liquid_type_bc = LIQUID_FLOWING;
+ }
+ writeU8(os, liquid_type_bc);
os << serializeString16(liquid_alternative_flowing);
os << serializeString16(liquid_alternative_source);
writeU8(os, liquid_viscosity);
@@ -512,9 +533,12 @@ void ContentFeatures::serialize(std::ostream &os, u16 protocol_version) const
writeU8(os, legacy_facedir_simple);
writeU8(os, legacy_wallmounted);
+ // new attributes
os << serializeString16(node_dig_prediction);
writeU8(os, leveled_max);
writeU8(os, alpha);
+ writeU8(os, move_resistance);
+ writeU8(os, liquid_move_physics);
}
void ContentFeatures::deSerialize(std::istream &is)
@@ -584,9 +608,11 @@ void ContentFeatures::deSerialize(std::istream &is)
// liquid
liquid_type = (enum LiquidType) readU8(is);
+ liquid_move_physics = liquid_type != LIQUID_NONE;
liquid_alternative_flowing = deSerializeString16(is);
liquid_alternative_source = deSerializeString16(is);
liquid_viscosity = readU8(is);
+ move_resistance = liquid_viscosity; // set default move_resistance
liquid_renewable = readU8(is);
liquid_range = readU8(is);
drowning = readU8(is);
@@ -618,6 +644,16 @@ void ContentFeatures::deSerialize(std::istream &is)
if (is.eof())
throw SerializationError("");
alpha = static_cast<enum AlphaMode>(tmp);
+
+ tmp = readU8(is);
+ if (is.eof())
+ throw SerializationError("");
+ move_resistance = tmp;
+
+ tmp = readU8(is);
+ if (is.eof())
+ throw SerializationError("");
+ liquid_move_physics = tmp;
} catch(SerializationError &e) {};
}
@@ -779,8 +815,10 @@ void ContentFeatures::updateTextures(ITextureSource *tsrc, IShaderSource *shdsrc
TileDef tdef[6];
for (u32 j = 0; j < 6; j++) {
tdef[j] = tiledef[j];
- if (tdef[j].name.empty())
- tdef[j].name = "unknown_node.png";
+ if (tdef[j].name.empty()) {
+ tdef[j].name = "no_texture.png";
+ tdef[j].backface_culling = false;
+ }
}
// also the overlay tiles
TileDef tdef_overlay[6];
@@ -788,8 +826,9 @@ void ContentFeatures::updateTextures(ITextureSource *tsrc, IShaderSource *shdsrc
tdef_overlay[j] = tiledef_overlay[j];
// also the special tiles
TileDef tdef_spec[6];
- for (u32 j = 0; j < CF_SPECIAL_COUNT; j++)
+ for (u32 j = 0; j < CF_SPECIAL_COUNT; j++) {
tdef_spec[j] = tiledef_special[j];
+ }
bool is_liquid = false;
@@ -944,7 +983,8 @@ void ContentFeatures::updateTextures(ITextureSource *tsrc, IShaderSource *shdsrc
if (param_type_2 == CPT2_COLOR ||
param_type_2 == CPT2_COLORED_FACEDIR ||
- param_type_2 == CPT2_COLORED_WALLMOUNTED)
+ param_type_2 == CPT2_COLORED_WALLMOUNTED ||
+ param_type_2 == CPT2_COLORED_DEGROTATE)
palette = tsrc->getPalette(palette_name);
if (drawtype == NDT_MESH && !mesh.empty()) {
@@ -1034,6 +1074,10 @@ void NodeDefManager::clear()
{
ContentFeatures f;
f.name = "unknown";
+ TileDef unknownTile;
+ unknownTile.name = "unknown_node.png";
+ for (int t = 0; t < 6; t++)
+ f.tiledef[t] = unknownTile;
// Insert directly into containers
content_t c = CONTENT_UNKNOWN;
m_content_features[c] = f;
@@ -1434,9 +1478,7 @@ void NodeDefManager::applyTextureOverrides(const std::vector<TextureOverride> &o
}
}
-void NodeDefManager::updateTextures(IGameDef *gamedef,
- void (*progress_callback)(void *progress_args, u32 progress, u32 max_progress),
- void *progress_callback_args)
+void NodeDefManager::updateTextures(IGameDef *gamedef, void *progress_callback_args)
{
#ifndef SERVER
infostream << "NodeDefManager::updateTextures(): Updating "
@@ -1445,8 +1487,8 @@ void NodeDefManager::updateTextures(IGameDef *gamedef,
Client *client = (Client *)gamedef;
ITextureSource *tsrc = client->tsrc();
IShaderSource *shdsrc = client->getShaderSource();
- scene::IMeshManipulator *meshmanip =
- RenderingEngine::get_scene_manager()->getMeshManipulator();
+ auto smgr = client->getSceneManager();
+ scene::IMeshManipulator *meshmanip = smgr->getMeshManipulator();
TextureSettings tsettings;
tsettings.readSettings();
@@ -1455,7 +1497,7 @@ void NodeDefManager::updateTextures(IGameDef *gamedef,
for (u32 i = 0; i < size; i++) {
ContentFeatures *f = &(m_content_features[i]);
f->updateTextures(tsrc, shdsrc, meshmanip, client, tsettings);
- progress_callback(progress_callback_args, i, size);
+ client->showUpdateProgressTexture(progress_callback_args, i, size);
}
#endif
}
@@ -1675,8 +1717,7 @@ bool NodeDefManager::nodeboxConnects(MapNode from, MapNode to,
NodeResolver::NodeResolver()
{
- m_nodenames.reserve(16);
- m_nnlistsizes.reserve(4);
+ reset();
}
@@ -1779,3 +1820,16 @@ bool NodeResolver::getIdsFromNrBacklog(std::vector<content_t> *result_out,
return success;
}
+
+void NodeResolver::reset(bool resolve_done)
+{
+ m_nodenames.clear();
+ m_nodenames_idx = 0;
+ m_nnlistsizes.clear();
+ m_nnlistsizes_idx = 0;
+
+ m_resolve_done = resolve_done;
+
+ m_nodenames.reserve(16);
+ m_nnlistsizes.reserve(4);
+}
diff --git a/src/nodedef.h b/src/nodedef.h
index 7da0e0d07..ea50d4281 100644
--- a/src/nodedef.h
+++ b/src/nodedef.h
@@ -44,6 +44,9 @@ class ITextureSource;
class IShaderSource;
class IGameDef;
class NodeResolver;
+#if BUILD_UNITTESTS
+class TestSchematic;
+#endif
enum ContentParamType
{
@@ -64,7 +67,7 @@ enum ContentParamType2
CPT2_WALLMOUNTED,
// Block level like FLOWINGLIQUID
CPT2_LEVELED,
- // 2D rotation for things like plants
+ // 2D rotation
CPT2_DEGROTATE,
// Mesh options for plants
CPT2_MESHOPTIONS,
@@ -76,6 +79,8 @@ enum ContentParamType2
CPT2_COLORED_WALLMOUNTED,
// Glasslike framed drawtype internal liquid level, param2 values 0 to 63
CPT2_GLASSLIKE_LIQUID_LEVEL,
+ // 3 bits of palette index, then degrotate
+ CPT2_COLORED_DEGROTATE,
};
enum LiquidType
@@ -371,11 +376,15 @@ struct ContentFeatures
u32 damage_per_second;
// client dig prediction
std::string node_dig_prediction;
+ // how slow players move through
+ u8 move_resistance = 0;
// --- LIQUID PROPERTIES ---
// Whether the node is non-liquid, source liquid or flowing liquid
enum LiquidType liquid_type;
+ // If true, movement (e.g. of players) inside this node is liquid-like.
+ bool liquid_move_physics;
// If the content is liquid, this is the flowing version of the liquid.
std::string liquid_alternative_flowing;
content_t liquid_alternative_flowing_id;
@@ -655,9 +664,7 @@ public:
* total ContentFeatures.
* @param progress_cbk_args passed to the callback function
*/
- void updateTextures(IGameDef *gamedef,
- void (*progress_cbk)(void *progress_args, u32 progress, u32 max_progress),
- void *progress_cbk_args);
+ void updateTextures(IGameDef *gamedef, void *progress_cbk_args);
/*!
* Writes the content of this manager to the given output stream.
@@ -789,10 +796,13 @@ private:
NodeDefManager *createNodeDefManager();
+// NodeResolver: Queue for node names which are then translated
+// to content_t after the NodeDefManager was initialized
class NodeResolver {
public:
NodeResolver();
virtual ~NodeResolver();
+ // Callback which is run as soon NodeDefManager is ready
virtual void resolveNodeNames() = 0;
// required because this class is used as mixin for ObjDef
@@ -804,12 +814,31 @@ public:
bool getIdsFromNrBacklog(std::vector<content_t> *result_out,
bool all_required = false, content_t c_fallback = CONTENT_IGNORE);
- void nodeResolveInternal();
+ inline bool isResolveDone() const { return m_resolve_done; }
+ void reset(bool resolve_done = false);
- u32 m_nodenames_idx = 0;
- u32 m_nnlistsizes_idx = 0;
+ // Vector containing all node names in the resolve "queue"
std::vector<std::string> m_nodenames;
+ // Specifies the "set size" of node names which are to be processed
+ // this is used for getIdsFromNrBacklog
+ // TODO: replace or remove
std::vector<size_t> m_nnlistsizes;
+
+protected:
+ friend class NodeDefManager; // m_ndef
+
const NodeDefManager *m_ndef = nullptr;
+ // Index of the next "m_nodenames" entry to resolve
+ u32 m_nodenames_idx = 0;
+
+private:
+#if BUILD_UNITTESTS
+ // Unittest requires access to m_resolve_done
+ friend class TestSchematic;
+#endif
+ void nodeResolveInternal();
+
+ // Index of the next "m_nnlistsizes" entry to process
+ u32 m_nnlistsizes_idx = 0;
bool m_resolve_done = false;
};
diff --git a/src/nodemetadata.cpp b/src/nodemetadata.cpp
index f98732385..b5052c3b8 100644
--- a/src/nodemetadata.cpp
+++ b/src/nodemetadata.cpp
@@ -113,13 +113,13 @@ int NodeMetadata::countNonPrivate() const
*/
void NodeMetadataList::serialize(std::ostream &os, u8 blockver, bool disk,
- bool absolute_pos) const
+ bool absolute_pos, bool include_empty) const
{
/*
Version 0 is a placeholder for "nothing to see here; go away."
*/
- u16 count = countNonEmpty();
+ u16 count = include_empty ? m_data.size() : countNonEmpty();
if (count == 0) {
writeU8(os, 0); // version
return;
@@ -134,7 +134,7 @@ void NodeMetadataList::serialize(std::ostream &os, u8 blockver, bool disk,
i != m_data.end(); ++i) {
v3s16 p = i->first;
NodeMetadata *data = i->second;
- if (data->empty())
+ if (!include_empty && data->empty())
continue;
if (absolute_pos) {
diff --git a/src/nodemetadata.h b/src/nodemetadata.h
index c028caf88..4b5b4d887 100644
--- a/src/nodemetadata.h
+++ b/src/nodemetadata.h
@@ -82,7 +82,7 @@ public:
~NodeMetadataList();
void serialize(std::ostream &os, u8 blockver, bool disk = true,
- bool absolute_pos = false) const;
+ bool absolute_pos = false, bool include_empty = false) const;
void deSerialize(std::istream &is, IItemDefManager *item_def_mgr,
bool absolute_pos = false);
diff --git a/src/noise.cpp b/src/noise.cpp
index e16564b05..2f4de6855 100644
--- a/src/noise.cpp
+++ b/src/noise.cpp
@@ -312,51 +312,6 @@ float noise2d_perlin(float x, float y, s32 seed,
}
-float noise2d_perlin_abs(float x, float y, s32 seed,
- int octaves, float persistence, bool eased)
-{
- float a = 0;
- float f = 1.0;
- float g = 1.0;
- for (int i = 0; i < octaves; i++) {
- a += g * std::fabs(noise2d_gradient(x * f, y * f, seed + i, eased));
- f *= 2.0;
- g *= persistence;
- }
- return a;
-}
-
-
-float noise3d_perlin(float x, float y, float z, s32 seed,
- int octaves, float persistence, bool eased)
-{
- float a = 0;
- float f = 1.0;
- float g = 1.0;
- for (int i = 0; i < octaves; i++) {
- a += g * noise3d_gradient(x * f, y * f, z * f, seed + i, eased);
- f *= 2.0;
- g *= persistence;
- }
- return a;
-}
-
-
-float noise3d_perlin_abs(float x, float y, float z, s32 seed,
- int octaves, float persistence, bool eased)
-{
- float a = 0;
- float f = 1.0;
- float g = 1.0;
- for (int i = 0; i < octaves; i++) {
- a += g * std::fabs(noise3d_gradient(x * f, y * f, z * f, seed + i, eased));
- f *= 2.0;
- g *= persistence;
- }
- return a;
-}
-
-
float contour(float v)
{
v = std::fabs(v);
@@ -369,7 +324,7 @@ float contour(float v)
///////////////////////// [ New noise ] ////////////////////////////
-float NoisePerlin2D(NoiseParams *np, float x, float y, s32 seed)
+float NoisePerlin2D(const NoiseParams *np, float x, float y, s32 seed)
{
float a = 0;
float f = 1.0;
@@ -395,7 +350,7 @@ float NoisePerlin2D(NoiseParams *np, float x, float y, s32 seed)
}
-float NoisePerlin3D(NoiseParams *np, float x, float y, float z, s32 seed)
+float NoisePerlin3D(const NoiseParams *np, float x, float y, float z, s32 seed)
{
float a = 0;
float f = 1.0;
@@ -422,7 +377,7 @@ float NoisePerlin3D(NoiseParams *np, float x, float y, float z, s32 seed)
}
-Noise::Noise(NoiseParams *np_, s32 seed, u32 sx, u32 sy, u32 sz)
+Noise::Noise(const NoiseParams *np_, s32 seed, u32 sx, u32 sy, u32 sz)
{
np = *np_;
this->seed = seed;
diff --git a/src/noise.h b/src/noise.h
index 613879890..e4a9ed6c7 100644
--- a/src/noise.h
+++ b/src/noise.h
@@ -146,7 +146,7 @@ public:
float *persist_buf = nullptr;
float *result = nullptr;
- Noise(NoiseParams *np, s32 seed, u32 sx, u32 sy, u32 sz=1);
+ Noise(const NoiseParams *np, s32 seed, u32 sx, u32 sy, u32 sz=1);
~Noise();
void setSize(u32 sx, u32 sy, u32 sz=1);
@@ -192,8 +192,8 @@ private:
};
-float NoisePerlin2D(NoiseParams *np, float x, float y, s32 seed);
-float NoisePerlin3D(NoiseParams *np, float x, float y, float z, s32 seed);
+float NoisePerlin2D(const NoiseParams *np, float x, float y, s32 seed);
+float NoisePerlin3D(const NoiseParams *np, float x, float y, float z, s32 seed);
inline float NoisePerlin2D_PO(NoiseParams *np, float x, float xoff,
float y, float yoff, s32 seed)
@@ -224,15 +224,6 @@ float noise3d_gradient(float x, float y, float z, s32 seed, bool eased=false);
float noise2d_perlin(float x, float y, s32 seed,
int octaves, float persistence, bool eased=true);
-float noise2d_perlin_abs(float x, float y, s32 seed,
- int octaves, float persistence, bool eased=true);
-
-float noise3d_perlin(float x, float y, float z, s32 seed,
- int octaves, float persistence, bool eased=false);
-
-float noise3d_perlin_abs(float x, float y, float z, s32 seed,
- int octaves, float persistence, bool eased=false);
-
inline float easeCurve(float t)
{
return t * t * t * (t * (6.f * t - 15.f) + 10.f);
diff --git a/src/object_properties.cpp b/src/object_properties.cpp
index 2eebc27d6..c7f6becf0 100644
--- a/src/object_properties.cpp
+++ b/src/object_properties.cpp
@@ -28,7 +28,7 @@ static const video::SColor NULL_BGCOLOR{0, 1, 1, 1};
ObjectProperties::ObjectProperties()
{
- textures.emplace_back("unknown_object.png");
+ textures.emplace_back("no_texture.png");
colors.emplace_back(255,255,255,255);
}
@@ -83,6 +83,39 @@ std::string ObjectProperties::dump()
return os.str();
}
+bool ObjectProperties::validate()
+{
+ const char *func = "ObjectProperties::validate(): ";
+ bool ret = true;
+
+ // cf. where serializeString16 is used below
+ for (u32 i = 0; i < textures.size(); i++) {
+ if (textures[i].size() > U16_MAX) {
+ warningstream << func << "texture " << (i+1) << " has excessive length, "
+ "clearing it." << std::endl;
+ textures[i].clear();
+ ret = false;
+ }
+ }
+ if (nametag.length() > U16_MAX) {
+ warningstream << func << "nametag has excessive length, clearing it." << std::endl;
+ nametag.clear();
+ ret = false;
+ }
+ if (infotext.length() > U16_MAX) {
+ warningstream << func << "infotext has excessive length, clearing it." << std::endl;
+ infotext.clear();
+ ret = false;
+ }
+ if (wield_item.length() > U16_MAX) {
+ warningstream << func << "wield_item has excessive length, clearing it." << std::endl;
+ wield_item.clear();
+ ret = false;
+ }
+
+ return ret;
+}
+
void ObjectProperties::serialize(std::ostream &os) const
{
writeU8(os, 4); // PROTOCOL_VERSION >= 37
@@ -105,7 +138,6 @@ void ObjectProperties::serialize(std::ostream &os) const
writeU8(os, is_visible);
writeU8(os, makes_footstep_sound);
writeF32(os, automatic_rotate);
- // Added in protocol version 14
os << serializeString16(mesh);
writeU16(os, colors.size());
for (video::SColor color : colors) {
diff --git a/src/object_properties.h b/src/object_properties.h
index db28eebfd..79866a22c 100644
--- a/src/object_properties.h
+++ b/src/object_properties.h
@@ -68,6 +68,8 @@ struct ObjectProperties
ObjectProperties();
std::string dump();
+ // check limits of some important properties (strings) that'd cause exceptions later on
+ bool validate();
void serialize(std::ostream &os) const;
void deSerialize(std::istream &is);
};
diff --git a/src/player.cpp b/src/player.cpp
index d3ba5c2c2..347be30f1 100644
--- a/src/player.cpp
+++ b/src/player.cpp
@@ -19,6 +19,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "player.h"
+#include <cmath>
#include "threading/mutex_auto_lock.h"
#include "util/numeric.h"
#include "hud.h"
@@ -159,6 +160,64 @@ void Player::clearHud()
}
}
+#ifndef SERVER
+
+u32 PlayerControl::getKeysPressed() const
+{
+ u32 keypress_bits =
+ ( (u32)(jump & 1) << 4) |
+ ( (u32)(aux1 & 1) << 5) |
+ ( (u32)(sneak & 1) << 6) |
+ ( (u32)(dig & 1) << 7) |
+ ( (u32)(place & 1) << 8) |
+ ( (u32)(zoom & 1) << 9)
+ ;
+
+ // If any direction keys are pressed pass those through
+ if (direction_keys != 0)
+ {
+ keypress_bits |= direction_keys;
+ }
+ // Otherwise set direction keys based on joystick movement (for mod compatibility)
+ else if (isMoving())
+ {
+ float abs_d;
+
+ // (absolute value indicates forward / backward)
+ abs_d = abs(movement_direction);
+ if (abs_d < 3.0f / 8.0f * M_PI)
+ keypress_bits |= (u32)1; // Forward
+ if (abs_d > 5.0f / 8.0f * M_PI)
+ keypress_bits |= (u32)1 << 1; // Backward
+
+ // rotate entire coordinate system by 90 degree
+ abs_d = movement_direction + M_PI_2;
+ if (abs_d >= M_PI)
+ abs_d -= 2 * M_PI;
+ abs_d = abs(abs_d);
+ // (value now indicates left / right)
+ if (abs_d < 3.0f / 8.0f * M_PI)
+ keypress_bits |= (u32)1 << 2; // Left
+ if (abs_d > 5.0f / 8.0f * M_PI)
+ keypress_bits |= (u32)1 << 3; // Right
+ }
+
+ return keypress_bits;
+}
+
+#endif
+
+void PlayerControl::unpackKeysPressed(u32 keypress_bits)
+{
+ direction_keys = keypress_bits & 0xf;
+ jump = keypress_bits & (1 << 4);
+ aux1 = keypress_bits & (1 << 5);
+ sneak = keypress_bits & (1 << 6);
+ dig = keypress_bits & (1 << 7);
+ place = keypress_bits & (1 << 8);
+ zoom = keypress_bits & (1 << 9);
+}
+
void PlayerSettings::readGlobalSettings()
{
free_move = g_settings->getBool("free_move");
diff --git a/src/player.h b/src/player.h
index ec068a8b1..d769acdad 100644
--- a/src/player.h
+++ b/src/player.h
@@ -49,26 +49,18 @@ struct PlayerControl
PlayerControl() = default;
PlayerControl(
- bool a_up,
- bool a_down,
- bool a_left,
- bool a_right,
- bool a_jump,
- bool a_aux1,
- bool a_sneak,
+ bool a_up, bool a_down, bool a_left, bool a_right,
+ bool a_jump, bool a_aux1, bool a_sneak,
bool a_zoom,
- bool a_dig,
- bool a_place,
- float a_pitch,
- float a_yaw,
- float a_sidew_move_joystick_axis,
- float a_forw_move_joystick_axis
+ bool a_dig, bool a_place,
+ float a_pitch, float a_yaw,
+ float a_movement_speed, float a_movement_direction
)
{
- up = a_up;
- down = a_down;
- left = a_left;
- right = a_right;
+ // Encode direction keys into a single value so nobody uses it accidentally
+ // as movement_{speed,direction} is supposed to be the source of truth.
+ direction_keys = (a_up&1) | ((a_down&1) << 1) |
+ ((a_left&1) << 2) | ((a_right&1) << 3);
jump = a_jump;
aux1 = a_aux1;
sneak = a_sneak;
@@ -77,23 +69,31 @@ struct PlayerControl
place = a_place;
pitch = a_pitch;
yaw = a_yaw;
- sidew_move_joystick_axis = a_sidew_move_joystick_axis;
- forw_move_joystick_axis = a_forw_move_joystick_axis;
+ movement_speed = a_movement_speed;
+ movement_direction = a_movement_direction;
}
- bool up = false;
- bool down = false;
- bool left = false;
- bool right = false;
+
+#ifndef SERVER
+ // For client use
+ u32 getKeysPressed() const;
+ inline bool isMoving() const { return movement_speed > 0.001f; }
+#endif
+
+ // For server use
+ void unpackKeysPressed(u32 keypress_bits);
+
+ u8 direction_keys = 0;
bool jump = false;
bool aux1 = false;
bool sneak = false;
bool zoom = false;
bool dig = false;
bool place = false;
+ // Note: These four are NOT available on the server
float pitch = 0.0f;
float yaw = 0.0f;
- float sidew_move_joystick_axis = 0.0f;
- float forw_move_joystick_axis = 0.0f;
+ float movement_speed = 0.0f;
+ float movement_direction = 0.0f;
};
struct PlayerSettings
@@ -200,8 +200,6 @@ public:
return m_fov_override_spec;
}
- u32 keyPressed = 0;
-
HudElement* getHud(u32 id);
u32 addHud(HudElement* hud);
HudElement* removeHud(u32 id);
diff --git a/src/porting.cpp b/src/porting.cpp
index 4c87bddee..caf9e9be3 100644
--- a/src/porting.cpp
+++ b/src/porting.cpp
@@ -35,6 +35,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <algorithm>
#include <shlwapi.h>
#include <shellapi.h>
+ #include <mmsystem.h>
#endif
#if !defined(_WIN32)
#include <unistd.h>
@@ -69,6 +70,15 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <cstdarg>
#include <cstdio>
+#if !defined(SERVER) && defined(_WIN32)
+// On Windows export some driver-specific variables to encourage Minetest to be
+// executed on the discrete GPU in case of systems with two. Portability is fun.
+extern "C" {
+ __declspec(dllexport) DWORD NvOptimusEnablement = 1;
+ __declspec(dllexport) DWORD AmdPowerXpressRequestHighPerformance = 1;
+}
+#endif
+
namespace porting
{
@@ -766,6 +776,9 @@ bool open_directory(const std::string &path)
inline double get_perf_freq()
{
+ // Also use this opportunity to enable high-res timers
+ timeBeginPeriod(1);
+
LARGE_INTEGER freq;
QueryPerformanceFrequency(&freq);
return freq.QuadPart;
diff --git a/src/porting.h b/src/porting.h
index e4ebe36fd..93932e1d9 100644
--- a/src/porting.h
+++ b/src/porting.h
@@ -234,21 +234,21 @@ inline u64 getTimeMs()
{
struct timespec ts;
os_get_clock(&ts);
- return ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
+ return ((u64) ts.tv_sec) * 1000LL + ((u64) ts.tv_nsec) / 1000000LL;
}
inline u64 getTimeUs()
{
struct timespec ts;
os_get_clock(&ts);
- return ts.tv_sec * 1000000 + ts.tv_nsec / 1000;
+ return ((u64) ts.tv_sec) * 1000000LL + ((u64) ts.tv_nsec) / 1000LL;
}
inline u64 getTimeNs()
{
struct timespec ts;
os_get_clock(&ts);
- return ts.tv_sec * 1000000000 + ts.tv_nsec;
+ return ((u64) ts.tv_sec) * 1000000000LL + ((u64) ts.tv_nsec);
}
#endif
diff --git a/src/remoteplayer.cpp b/src/remoteplayer.cpp
index 925ad001b..20be7a8c8 100644
--- a/src/remoteplayer.cpp
+++ b/src/remoteplayer.cpp
@@ -31,6 +31,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
/*
RemotePlayer
*/
+
// static config cache for remoteplayer
bool RemotePlayer::m_setting_cache_loaded = false;
float RemotePlayer::m_setting_chat_message_limit_per_10sec = 0.0f;
@@ -46,6 +47,7 @@ RemotePlayer::RemotePlayer(const char *name, IItemDefManager *idef):
g_settings->getU16("chat_message_limit_trigger_kick");
RemotePlayer::m_setting_cache_loaded = true;
}
+
movement_acceleration_default = g_settings->getFloat("movement_acceleration_default") * BS;
movement_acceleration_air = g_settings->getFloat("movement_acceleration_air") * BS;
movement_acceleration_fast = g_settings->getFloat("movement_acceleration_fast") * BS;
@@ -59,32 +61,16 @@ RemotePlayer::RemotePlayer(const char *name, IItemDefManager *idef):
movement_liquid_sink = g_settings->getFloat("movement_liquid_sink") * BS;
movement_gravity = g_settings->getFloat("movement_gravity") * BS;
- // copy defaults
- m_cloud_params.density = 0.4f;
- m_cloud_params.color_bright = video::SColor(229, 240, 240, 255);
- m_cloud_params.color_ambient = video::SColor(255, 0, 0, 0);
- m_cloud_params.height = 120.0f;
- m_cloud_params.thickness = 16.0f;
- m_cloud_params.speed = v2f(0.0f, -2.0f);
-
// Skybox defaults:
-
- SkyboxDefaults sky_defaults;
-
- m_skybox_params.sky_color = sky_defaults.getSkyColorDefaults();
- m_skybox_params.type = "regular";
- m_skybox_params.clouds = true;
- m_skybox_params.fog_sun_tint = video::SColor(255, 244, 125, 29);
- m_skybox_params.fog_moon_tint = video::SColorf(0.5, 0.6, 0.8, 1).toSColor();
- m_skybox_params.fog_tint_type = "default";
-
- m_sun_params = sky_defaults.getSunDefaults();
- m_moon_params = sky_defaults.getMoonDefaults();
- m_star_params = sky_defaults.getStarDefaults();
+ m_cloud_params = SkyboxDefaults::getCloudDefaults();
+ m_skybox_params = SkyboxDefaults::getSkyDefaults();
+ m_sun_params = SkyboxDefaults::getSunDefaults();
+ m_moon_params = SkyboxDefaults::getMoonDefaults();
+ m_star_params = SkyboxDefaults::getStarDefaults();
}
-const RemotePlayerChatResult RemotePlayer::canSendChatMessage()
+RemotePlayerChatResult RemotePlayer::canSendChatMessage()
{
// Rate limit messages
u32 now = time(NULL);
diff --git a/src/remoteplayer.h b/src/remoteplayer.h
index 8d086fc5a..c8991480b 100644
--- a/src/remoteplayer.h
+++ b/src/remoteplayer.h
@@ -21,7 +21,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#pragma once
#include "player.h"
-#include "cloudparams.h"
#include "skyparams.h"
class PlayerSAO;
@@ -47,7 +46,7 @@ public:
PlayerSAO *getPlayerSAO() { return m_sao; }
void setPlayerSAO(PlayerSAO *sao) { m_sao = sao; }
- const RemotePlayerChatResult canSendChatMessage();
+ RemotePlayerChatResult canSendChatMessage();
void setHotbarItemcount(s32 hotbar_itemcount)
{
diff --git a/src/rollback.cpp b/src/rollback.cpp
index 3cd9c7ce7..33b7958b9 100644
--- a/src/rollback.cpp
+++ b/src/rollback.cpp
@@ -941,12 +941,6 @@ void RollbackManager::addAction(const RollbackAction & action)
}
}
-std::list<RollbackAction> RollbackManager::getEntriesSince(time_t first_time)
-{
- flush();
- return getActionsSince(first_time);
-}
-
std::list<RollbackAction> RollbackManager::getNodeActors(v3s16 pos, int range,
time_t seconds, int limit)
{
diff --git a/src/rollback.h b/src/rollback.h
index 1d9949d15..ff96e513f 100644
--- a/src/rollback.h
+++ b/src/rollback.h
@@ -46,7 +46,6 @@ public:
void flush();
void addAction(const RollbackAction & action);
- std::list<RollbackAction> getEntriesSince(time_t first_time);
std::list<RollbackAction> getNodeActors(v3s16 pos, int range,
time_t seconds, int limit);
std::list<RollbackAction> getRevertActions(
diff --git a/src/script/common/c_content.cpp b/src/script/common/c_content.cpp
index 6995f6b61..8a5a3fe71 100644
--- a/src/script/common/c_content.cpp
+++ b/src/script/common/c_content.cpp
@@ -119,6 +119,8 @@ void read_item_definition(lua_State* L, int index,
// "" = no prediction
getstringfield(L, index, "node_placement_prediction",
def.node_placement_prediction);
+
+ getintfield(L, index, "place_param2", def.place_param2);
}
/******************************************************************************/
@@ -198,8 +200,6 @@ void read_object_properties(lua_State *L, int index,
if (prop->hp_max < sao->getHP()) {
PlayerHPChangeReason reason(PlayerHPChangeReason::SET_HP);
sao->setHP(prop->hp_max, reason);
- if (sao->getType() == ACTIVEOBJECT_TYPE_PLAYER)
- sao->getEnv()->getGameDef()->SendPlayerHPOrDie((PlayerSAO *)sao, reason);
}
}
@@ -683,7 +683,8 @@ void read_content_features(lua_State *L, ContentFeatures &f, int index)
if (!f.palette_name.empty() &&
!(f.param_type_2 == CPT2_COLOR ||
f.param_type_2 == CPT2_COLORED_FACEDIR ||
- f.param_type_2 == CPT2_COLORED_WALLMOUNTED))
+ f.param_type_2 == CPT2_COLORED_WALLMOUNTED ||
+ f.param_type_2 == CPT2_COLORED_DEGROTATE))
warningstream << "Node " << f.name.c_str()
<< " has a palette, but not a suitable paramtype2." << std::endl;
@@ -718,6 +719,9 @@ void read_content_features(lua_State *L, ContentFeatures &f, int index)
// the slowest possible
f.liquid_viscosity = getintfield_default(L, index,
"liquid_viscosity", f.liquid_viscosity);
+ // If move_resistance is not set explicitly,
+ // move_resistance is equal to liquid_viscosity
+ f.move_resistance = f.liquid_viscosity;
f.liquid_range = getintfield_default(L, index,
"liquid_range", f.liquid_range);
f.leveled = getintfield_default(L, index, "leveled", f.leveled);
@@ -821,6 +825,21 @@ void read_content_features(lua_State *L, ContentFeatures &f, int index)
getstringfield(L, index, "node_dig_prediction",
f.node_dig_prediction);
+ // How much the node slows down players, ranging from 1 to 7,
+ // the higher, the slower.
+ f.move_resistance = getintfield_default(L, index,
+ "move_resistance", f.move_resistance);
+
+ // Whether e.g. players in this node will have liquid movement physics
+ lua_getfield(L, index, "liquid_move_physics");
+ if(lua_isboolean(L, -1)) {
+ f.liquid_move_physics = lua_toboolean(L, -1);
+ } else if(lua_isnil(L, -1)) {
+ f.liquid_move_physics = f.liquid_type != LIQUID_NONE;
+ } else {
+ errorstream << "Field \"liquid_move_physics\": Invalid type!" << std::endl;
+ }
+ lua_pop(L, 1);
}
void push_content_features(lua_State *L, const ContentFeatures &c)
@@ -948,6 +967,10 @@ void push_content_features(lua_State *L, const ContentFeatures &c)
lua_setfield(L, -2, "legacy_wallmounted");
lua_pushstring(L, c.node_dig_prediction.c_str());
lua_setfield(L, -2, "node_dig_prediction");
+ lua_pushnumber(L, c.move_resistance);
+ lua_setfield(L, -2, "move_resistance");
+ lua_pushboolean(L, c.liquid_move_physics);
+ lua_setfield(L, -2, "liquid_move_physics");
}
/******************************************************************************/
@@ -1347,26 +1370,28 @@ void read_inventory_list(lua_State *L, int tableindex,
{
if(tableindex < 0)
tableindex = lua_gettop(L) + 1 + tableindex;
+
// If nil, delete list
if(lua_isnil(L, tableindex)){
inv->deleteList(name);
return;
}
- // Otherwise set list
+
+ // Get Lua-specified items to insert into the list
std::vector<ItemStack> items = read_items(L, tableindex,srv);
- int listsize = (forcesize != -1) ? forcesize : items.size();
+ size_t listsize = (forcesize >= 0) ? forcesize : items.size();
+
+ // Create or resize/clear list
InventoryList *invlist = inv->addList(name, listsize);
- int index = 0;
- for(std::vector<ItemStack>::const_iterator
- i = items.begin(); i != items.end(); ++i){
- if(forcesize != -1 && index == forcesize)
- break;
- invlist->changeItem(index, *i);
- index++;
+ if (!invlist) {
+ luaL_error(L, "inventory list: cannot create list named '%s'", name);
+ return;
}
- while(forcesize != -1 && index < forcesize){
- invlist->deleteItem(index);
- index++;
+
+ for (size_t i = 0; i < items.size(); ++i) {
+ if (i == listsize)
+ break; // Truncate provided list of items
+ invlist->changeItem(i, items[i]);
}
}
@@ -1923,6 +1948,8 @@ void read_hud_element(lua_State *L, HudElement *elem)
elem->world_pos = lua_istable(L, -1) ? read_v3f(L, -1) : v3f();
lua_pop(L, 1);
+ elem->style = getintfield_default(L, 2, "style", 0);
+
/* check for known deprecated element usage */
if ((elem->type == HUD_ELEM_STATBAR) && (elem->size == v2s32()))
log_deprecated(L,"Deprecated usage of statbar without size!");
@@ -1977,17 +2004,22 @@ void push_hud_element(lua_State *L, HudElement *elem)
lua_pushstring(L, elem->text2.c_str());
lua_setfield(L, -2, "text2");
+
+ lua_pushinteger(L, elem->style);
+ lua_setfield(L, -2, "style");
}
-HudElementStat read_hud_change(lua_State *L, HudElement *elem, void **value)
+bool read_hud_change(lua_State *L, HudElementStat &stat, HudElement *elem, void **value)
{
- HudElementStat stat = HUD_STAT_NUMBER;
- std::string statstr;
- if (lua_isstring(L, 3)) {
+ std::string statstr = lua_tostring(L, 3);
+ {
int statint;
- statstr = lua_tostring(L, 3);
- stat = string_to_enum(es_HudElementStat, statint, statstr) ?
- (HudElementStat)statint : stat;
+ if (!string_to_enum(es_HudElementStat, statint, statstr)) {
+ script_log_unique(L, "Unknown HUD stat type: " + statstr, warningstream);
+ return false;
+ }
+
+ stat = (HudElementStat)statint;
}
switch (stat) {
@@ -2045,8 +2077,13 @@ HudElementStat read_hud_change(lua_State *L, HudElement *elem, void **value)
elem->text2 = luaL_checkstring(L, 4);
*value = &elem->text2;
break;
+ case HUD_STAT_STYLE:
+ elem->style = luaL_checknumber(L, 4);
+ *value = &elem->style;
+ break;
}
- return stat;
+
+ return true;
}
/******************************************************************************/
diff --git a/src/script/common/c_content.h b/src/script/common/c_content.h
index 29d576355..e762604a4 100644
--- a/src/script/common/c_content.h
+++ b/src/script/common/c_content.h
@@ -39,7 +39,9 @@ extern "C" {
#include "itemgroup.h"
#include "itemdef.h"
#include "c_types.h"
-#include "hud.h"
+// We do a explicit path include because by default c_content.h include src/client/hud.h
+// prior to the src/hud.h, which is not good on server only build
+#include "../../hud.h"
namespace Json { class Value; }
@@ -191,12 +193,12 @@ void read_json_value (lua_State *L, Json::Value &root,
void push_pointed_thing(lua_State *L, const PointedThing &pointed, bool csm =
false, bool hitpoint = false);
-void push_objectRef (lua_State *L, const u16 id);
+void push_objectRef (lua_State *L, const u16 id);
-void read_hud_element (lua_State *L, HudElement *elem);
+void read_hud_element (lua_State *L, HudElement *elem);
-void push_hud_element (lua_State *L, HudElement *elem);
+void push_hud_element (lua_State *L, HudElement *elem);
-HudElementStat read_hud_change (lua_State *L, HudElement *elem, void **value);
+bool read_hud_change (lua_State *L, HudElementStat &stat, HudElement *elem, void **value);
-void push_collision_move_result(lua_State *L, const collisionMoveResult &res);
+void push_collision_move_result(lua_State *L, const collisionMoveResult &res);
diff --git a/src/script/common/c_converter.cpp b/src/script/common/c_converter.cpp
index c00401b58..19734b913 100644
--- a/src/script/common/c_converter.cpp
+++ b/src/script/common/c_converter.cpp
@@ -51,12 +51,32 @@ if (value < F1000_MIN || value > F1000_MAX) { \
#define CHECK_POS_TAB(index) CHECK_TYPE(index, "position", LUA_TTABLE)
+/**
+ * A helper which sets (if available) the vector metatable from builtin as metatable
+ * for the table on top of the stack
+ */
+static void set_vector_metatable(lua_State *L)
+{
+ // get vector.metatable
+ lua_getglobal(L, "vector");
+ if (!lua_istable(L, -1)) {
+ // there is no global vector table
+ lua_pop(L, 1);
+ errorstream << "set_vector_metatable in c_converter.cpp: " <<
+ "missing global vector table" << std::endl;
+ return;
+ }
+ lua_getfield(L, -1, "metatable");
+ // set the metatable
+ lua_setmetatable(L, -3);
+ // pop vector global
+ lua_pop(L, 1);
+}
+
+
void push_float_string(lua_State *L, float value)
{
- std::stringstream ss;
- std::string str;
- ss << value;
- str = ss.str();
+ auto str = ftos(value);
lua_pushstring(L, str.c_str());
}
@@ -69,6 +89,7 @@ void push_v3f(lua_State *L, v3f p)
lua_setfield(L, -2, "y");
lua_pushnumber(L, p.Z);
lua_setfield(L, -2, "z");
+ set_vector_metatable(L);
}
void push_v2f(lua_State *L, v2f p)
@@ -281,6 +302,7 @@ void push_v3s16(lua_State *L, v3s16 p)
lua_setfield(L, -2, "y");
lua_pushinteger(L, p.Z);
lua_setfield(L, -2, "z");
+ set_vector_metatable(L);
}
v3s16 read_v3s16(lua_State *L, int index)
diff --git a/src/script/common/c_internal.cpp b/src/script/common/c_internal.cpp
index ad5f836c5..df82dba14 100644
--- a/src/script/common/c_internal.cpp
+++ b/src/script/common/c_internal.cpp
@@ -18,10 +18,12 @@ with this program; if not, write to the Free Software Foundation, Inc.,
*/
#include "common/c_internal.h"
+#include "util/numeric.h"
#include "debug.h"
#include "log.h"
#include "porting.h"
#include "settings.h"
+#include <algorithm> // std::find
std::string script_get_backtrace(lua_State *L)
{
@@ -99,60 +101,35 @@ void script_error(lua_State *L, int pcall_result, const char *mod, const char *f
throw LuaError(err_msg);
}
-// Push the list of callbacks (a lua table).
-// Then push nargs arguments.
-// Then call this function, which
-// - runs the callbacks
-// - replaces the table and arguments with the return value,
-// computed depending on mode
-void script_run_callbacks_f(lua_State *L, int nargs,
- RunCallbacksMode mode, const char *fxn)
-{
- FATAL_ERROR_IF(lua_gettop(L) < nargs + 1, "Not enough arguments");
-
- // Insert error handler
- PUSH_ERROR_HANDLER(L);
- int error_handler = lua_gettop(L) - nargs - 1;
- lua_insert(L, error_handler);
-
- // Insert run_callbacks between error handler and table
- lua_getglobal(L, "core");
- lua_getfield(L, -1, "run_callbacks");
- lua_remove(L, -2);
- lua_insert(L, error_handler + 1);
-
- // Insert mode after table
- lua_pushnumber(L, (int) mode);
- lua_insert(L, error_handler + 3);
-
- // Stack now looks like this:
- // ... <error handler> <run_callbacks> <table> <mode> <arg#1> <arg#2> ... <arg#n>
-
- int result = lua_pcall(L, nargs + 2, 1, error_handler);
- if (result != 0)
- script_error(L, result, NULL, fxn);
-
- lua_remove(L, error_handler);
-}
-
-static void script_log(lua_State *L, const std::string &message,
- std::ostream &log_to, bool do_error, int stack_depth)
+static void script_log_add_source(lua_State *L, std::string &message, int stack_depth)
{
lua_Debug ar;
- log_to << message << " ";
if (lua_getstack(L, stack_depth, &ar)) {
FATAL_ERROR_IF(!lua_getinfo(L, "Sl", &ar), "lua_getinfo() failed");
- log_to << "(at " << ar.short_src << ":" << ar.currentline << ")";
+ message.append(" (at " + std::string(ar.short_src) + ":"
+ + std::to_string(ar.currentline) + ")");
} else {
- log_to << "(at ?:?)";
+ message.append(" (at ?:?)");
}
- log_to << std::endl;
+}
- if (do_error)
- script_error(L, LUA_ERRRUN, NULL, NULL);
- else
- infostream << script_get_backtrace(L) << std::endl;
+bool script_log_unique(lua_State *L, std::string message, std::ostream &log_to,
+ int stack_depth)
+{
+ thread_local std::vector<u64> logged_messages;
+
+ script_log_add_source(L, message, stack_depth);
+ u64 hash = murmur_hash_64_ua(message.data(), message.length(), 0xBADBABE);
+
+ if (std::find(logged_messages.begin(), logged_messages.end(), hash)
+ == logged_messages.end()) {
+
+ logged_messages.emplace_back(hash);
+ log_to << message << std::endl;
+ return true;
+ }
+ return false;
}
DeprecatedHandlingMode get_deprecated_handling_mode()
@@ -174,9 +151,18 @@ DeprecatedHandlingMode get_deprecated_handling_mode()
return ret;
}
-void log_deprecated(lua_State *L, const std::string &message, int stack_depth)
+void log_deprecated(lua_State *L, std::string message, int stack_depth)
{
DeprecatedHandlingMode mode = get_deprecated_handling_mode();
- if (mode != DeprecatedHandlingMode::Ignore)
- script_log(L, message, warningstream, mode == DeprecatedHandlingMode::Error, stack_depth);
+ if (mode == DeprecatedHandlingMode::Ignore)
+ return;
+
+ script_log_add_source(L, message, stack_depth);
+ warningstream << message << std::endl;
+
+ if (mode == DeprecatedHandlingMode::Error)
+ script_error(L, LUA_ERRRUN, NULL, NULL);
+ else
+ infostream << script_get_backtrace(L) << std::endl;
}
+
diff --git a/src/script/common/c_internal.h b/src/script/common/c_internal.h
index 452c2dd5e..94cfd61fb 100644
--- a/src/script/common/c_internal.h
+++ b/src/script/common/c_internal.h
@@ -54,6 +54,8 @@ extern "C" {
#define CUSTOM_RIDX_GLOBALS_BACKUP (CUSTOM_RIDX_BASE + 1)
#define CUSTOM_RIDX_CURRENT_MOD_NAME (CUSTOM_RIDX_BASE + 2)
#define CUSTOM_RIDX_BACKTRACE (CUSTOM_RIDX_BASE + 3)
+#define CUSTOM_RIDX_HTTP_API_LUA (CUSTOM_RIDX_BASE + 4)
+
// Determine if CUSTOM_RIDX_SCRIPTAPI will hold a light or full userdata
#if defined(__aarch64__) && USE_LUAJIT
@@ -75,9 +77,6 @@ extern "C" {
} \
}
-#define script_run_callbacks(L, nargs, mode) \
- script_run_callbacks_f((L), (nargs), (mode), __FUNCTION__)
-
// What script_run_callbacks does with the return values of callbacks.
// Regardless of the mode, if only one callback is defined,
// its return value is the total return value.
@@ -108,13 +107,17 @@ enum RunCallbacksMode
// are converted by readParam<bool> to true or false, respectively.
};
+// Gets a backtrace of the current execution point
std::string script_get_backtrace(lua_State *L);
+// Wrapper for CFunction calls that converts C++ exceptions to Lua errors
int script_exception_wrapper(lua_State *L, lua_CFunction f);
+// Takes an error from lua_pcall and throws it as a LuaError
void script_error(lua_State *L, int pcall_result, const char *mod, const char *fxn);
-void script_run_callbacks_f(lua_State *L, int nargs,
- RunCallbacksMode mode, const char *fxn);
-enum class DeprecatedHandlingMode {
+bool script_log_unique(lua_State *L, std::string message, std::ostream &log_to,
+ int stack_depth = 1);
+
+enum DeprecatedHandlingMode {
Ignore,
Log,
Error
@@ -134,5 +137,4 @@ DeprecatedHandlingMode get_deprecated_handling_mode();
* @param message The deprecation method
* @param stack_depth How far on the stack to the first user function (ie: not builtin or core)
*/
-void log_deprecated(lua_State *L, const std::string &message,
- int stack_depth=1);
+void log_deprecated(lua_State *L, std::string message, int stack_depth = 1);
diff --git a/src/script/cpp_api/s_async.cpp b/src/script/cpp_api/s_async.cpp
index 0619b32c0..dacdcd75a 100644
--- a/src/script/cpp_api/s_async.cpp
+++ b/src/script/cpp_api/s_async.cpp
@@ -32,20 +32,19 @@ extern "C" {
#include "filesys.h"
#include "porting.h"
#include "common/c_internal.h"
+#include "lua_api/l_base.h"
/******************************************************************************/
AsyncEngine::~AsyncEngine()
{
-
// Request all threads to stop
for (AsyncWorkerThread *workerThread : workerThreads) {
workerThread->stop();
}
-
// Wake up all threads
- for (std::vector<AsyncWorkerThread *>::iterator it = workerThreads.begin();
- it != workerThreads.end(); ++it) {
+ for (auto it : workerThreads) {
+ (void)it;
jobQueueCounter.post();
}
@@ -68,6 +67,7 @@ AsyncEngine::~AsyncEngine()
/******************************************************************************/
void AsyncEngine::registerStateInitializer(StateInitializer func)
{
+ FATAL_ERROR_IF(initDone, "Initializer may not be registered after init");
stateInitializers.push_back(func);
}
@@ -85,36 +85,36 @@ void AsyncEngine::initialize(unsigned int numEngines)
}
/******************************************************************************/
-unsigned int AsyncEngine::queueAsyncJob(const std::string &func,
- const std::string &params)
+u32 AsyncEngine::queueAsyncJob(std::string &&func, std::string &&params,
+ const std::string &mod_origin)
{
jobQueueMutex.lock();
- LuaJobInfo toAdd;
- toAdd.id = jobIdCounter++;
- toAdd.serializedFunction = func;
- toAdd.serializedParams = params;
+ u32 jobId = jobIdCounter++;
- jobQueue.push_back(toAdd);
+ jobQueue.emplace_back();
+ auto &to_add = jobQueue.back();
+ to_add.id = jobId;
+ to_add.function = std::move(func);
+ to_add.params = std::move(params);
+ to_add.mod_origin = mod_origin;
jobQueueCounter.post();
-
jobQueueMutex.unlock();
-
- return toAdd.id;
+ return jobId;
}
/******************************************************************************/
-LuaJobInfo AsyncEngine::getJob()
+bool AsyncEngine::getJob(LuaJobInfo *job)
{
jobQueueCounter.wait();
jobQueueMutex.lock();
- LuaJobInfo retval;
+ bool retval = false;
if (!jobQueue.empty()) {
- retval = jobQueue.front();
+ *job = std::move(jobQueue.front());
jobQueue.pop_front();
- retval.valid = true;
+ retval = true;
}
jobQueueMutex.unlock();
@@ -122,10 +122,10 @@ LuaJobInfo AsyncEngine::getJob()
}
/******************************************************************************/
-void AsyncEngine::putJobResult(const LuaJobInfo &result)
+void AsyncEngine::putJobResult(LuaJobInfo &&result)
{
resultQueueMutex.lock();
- resultQueue.push_back(result);
+ resultQueue.emplace_back(std::move(result));
resultQueueMutex.unlock();
}
@@ -134,26 +134,30 @@ void AsyncEngine::step(lua_State *L)
{
int error_handler = PUSH_ERROR_HANDLER(L);
lua_getglobal(L, "core");
- resultQueueMutex.lock();
+
+ ScriptApiBase *script = ModApiBase::getScriptApiBase(L);
+
+ MutexAutoLock autolock(resultQueueMutex);
while (!resultQueue.empty()) {
- LuaJobInfo jobDone = resultQueue.front();
+ LuaJobInfo j = std::move(resultQueue.front());
resultQueue.pop_front();
lua_getfield(L, -1, "async_event_handler");
-
- if (lua_isnil(L, -1)) {
+ if (lua_isnil(L, -1))
FATAL_ERROR("Async event handler does not exist!");
- }
-
luaL_checktype(L, -1, LUA_TFUNCTION);
- lua_pushinteger(L, jobDone.id);
- lua_pushlstring(L, jobDone.serializedResult.data(),
- jobDone.serializedResult.size());
+ lua_pushinteger(L, j.id);
+ lua_pushlstring(L, j.result.data(), j.result.size());
- PCALL_RESL(L, lua_pcall(L, 2, 0, error_handler));
+ // Call handler
+ const char *origin = j.mod_origin.empty() ? nullptr : j.mod_origin.c_str();
+ script->setOriginDirect(origin);
+ int result = lua_pcall(L, 2, 0, error_handler);
+ if (result)
+ script_error(L, result, origin, "<async>");
}
- resultQueueMutex.unlock();
+
lua_pop(L, 2); // Pop core and error handler
}
@@ -168,8 +172,8 @@ void AsyncEngine::prepareEnvironment(lua_State* L, int top)
/******************************************************************************/
AsyncWorkerThread::AsyncWorkerThread(AsyncEngine* jobDispatcher,
const std::string &name) :
- Thread(name),
ScriptApiBase(ScriptingType::Async),
+ Thread(name),
jobDispatcher(jobDispatcher)
{
lua_State *L = getStack();
@@ -196,9 +200,9 @@ void* AsyncWorkerThread::run()
{
lua_State *L = getStack();
- std::string script = getServer()->getBuiltinLuaPath() + DIR_DELIM + "init.lua";
try {
- loadScript(script);
+ loadMod(getServer()->getBuiltinLuaPath() + DIR_DELIM + "init.lua",
+ BUILTIN_MOD_NAME);
} catch (const ModError &e) {
errorstream << "Execution of async base environment failed: "
<< e.what() << std::endl;
@@ -213,44 +217,44 @@ void* AsyncWorkerThread::run()
}
// Main loop
+ LuaJobInfo j;
while (!stopRequested()) {
// Wait for job
- LuaJobInfo toProcess = jobDispatcher->getJob();
-
- if (!toProcess.valid || stopRequested()) {
+ if (!jobDispatcher->getJob(&j) || stopRequested())
continue;
- }
lua_getfield(L, -1, "job_processor");
- if (lua_isnil(L, -1)) {
+ if (lua_isnil(L, -1))
FATAL_ERROR("Unable to get async job processor!");
- }
-
luaL_checktype(L, -1, LUA_TFUNCTION);
- // Call it
- lua_pushlstring(L,
- toProcess.serializedFunction.data(),
- toProcess.serializedFunction.size());
- lua_pushlstring(L,
- toProcess.serializedParams.data(),
- toProcess.serializedParams.size());
+ if (luaL_loadbuffer(L, j.function.data(), j.function.size(), "=(async)")) {
+ errorstream << "ASYNC WORKER: Unable to deserialize function" << std::endl;
+ lua_pushnil(L);
+ }
+ lua_pushlstring(L, j.params.data(), j.params.size());
+ // Call it
+ setOriginDirect(j.mod_origin.empty() ? nullptr : j.mod_origin.c_str());
int result = lua_pcall(L, 2, 1, error_handler);
if (result) {
- PCALL_RES(result);
- toProcess.serializedResult = "";
+ try {
+ scriptError(result, "<async>");
+ } catch (const ModError &e) {
+ errorstream << e.what() << std::endl;
+ }
} else {
// Fetch result
size_t length;
const char *retval = lua_tolstring(L, -1, &length);
- toProcess.serializedResult = std::string(retval, length);
+ j.result.assign(retval, length);
}
lua_pop(L, 1); // Pop retval
// Put job result
- jobDispatcher->putJobResult(toProcess);
+ if (!j.result.empty())
+ jobDispatcher->putJobResult(std::move(j));
}
lua_pop(L, 2); // Pop core and error handler
diff --git a/src/script/cpp_api/s_async.h b/src/script/cpp_api/s_async.h
index 99a4f891c..697cb0221 100644
--- a/src/script/cpp_api/s_async.h
+++ b/src/script/cpp_api/s_async.h
@@ -21,7 +21,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <vector>
#include <deque>
-#include <map>
#include "threading/semaphore.h"
#include "threading/thread.h"
@@ -39,26 +38,29 @@ struct LuaJobInfo
{
LuaJobInfo() = default;
- // Function to be called in async environment
- std::string serializedFunction = "";
- // Parameter to be passed to function
- std::string serializedParams = "";
- // Result of function call
- std::string serializedResult = "";
+ // Function to be called in async environment (from string.dump)
+ std::string function;
+ // Parameter to be passed to function (serialized)
+ std::string params;
+ // Result of function call (serialized)
+ std::string result;
+ // Name of the mod who invoked this call
+ std::string mod_origin;
// JobID used to identify a job and match it to callback
- unsigned int id = 0;
-
- bool valid = false;
+ u32 id;
};
// Asynchronous working environment
-class AsyncWorkerThread : public Thread, public ScriptApiBase {
+class AsyncWorkerThread : public Thread, virtual public ScriptApiBase {
+ friend class AsyncEngine;
public:
- AsyncWorkerThread(AsyncEngine* jobDispatcher, const std::string &name);
virtual ~AsyncWorkerThread();
void *run();
+protected:
+ AsyncWorkerThread(AsyncEngine* jobDispatcher, const std::string &name);
+
private:
AsyncEngine *jobDispatcher = nullptr;
};
@@ -89,7 +91,8 @@ public:
* @param params Serialized parameters
* @return jobid The job is queued
*/
- unsigned int queueAsyncJob(const std::string &func, const std::string &params);
+ u32 queueAsyncJob(std::string &&func, std::string &&params,
+ const std::string &mod_origin = "");
/**
* Engine step to process finished jobs
@@ -102,15 +105,16 @@ protected:
/**
* Get a Job from queue to be processed
* this function blocks until a job is ready
- * @return a job to be processed
+ * @param job a job to be processed
+ * @return whether a job was available
*/
- LuaJobInfo getJob();
+ bool getJob(LuaJobInfo *job);
/**
* Put a Job result back to result queue
* @param result result of completed job
*/
- void putJobResult(const LuaJobInfo &result);
+ void putJobResult(LuaJobInfo &&result);
/**
* Initialize environment with current registred functions
@@ -129,11 +133,10 @@ private:
std::vector<StateInitializer> stateInitializers;
// Internal counter to create job IDs
- unsigned int jobIdCounter = 0;
+ u32 jobIdCounter = 0;
// Mutex to protect job queue
std::mutex jobQueueMutex;
-
// Job queue
std::deque<LuaJobInfo> jobQueue;
diff --git a/src/script/cpp_api/s_base.cpp b/src/script/cpp_api/s_base.cpp
index f965975a3..f7b8a5102 100644
--- a/src/script/cpp_api/s_base.cpp
+++ b/src/script/cpp_api/s_base.cpp
@@ -37,6 +37,8 @@ extern "C" {
#include "lualib.h"
#if USE_LUAJIT
#include "luajit.h"
+#else
+ #include "bit.h"
#endif
}
@@ -88,6 +90,11 @@ ScriptApiBase::ScriptApiBase(ScriptingType type):
else
luaL_openlibs(m_luastack);
+ // Load bit library
+ lua_pushcfunction(m_luastack, luaopen_bit);
+ lua_pushstring(m_luastack, LUA_BITLIBNAME);
+ lua_call(m_luastack, 1, 0);
+
// Make the ScriptApiBase* accessible to ModApiBase
#if INDIRECT_SCRIPTAPI_RIDX
*(void **)(lua_newuserdata(m_luastack, sizeof(void *))) = this;
@@ -331,13 +338,9 @@ void ScriptApiBase::setOriginDirect(const char *origin)
void ScriptApiBase::setOriginFromTableRaw(int index, const char *fxn)
{
-#ifdef SCRIPTAPI_DEBUG
lua_State *L = getStack();
-
m_last_run_mod = lua_istable(L, index) ?
getstringfield_default(L, index, "mod_origin", "") : "";
- //printf(">>>> running %s for mod: %s\n", fxn, m_last_run_mod.c_str());
-#endif
}
/*
diff --git a/src/script/cpp_api/s_base.h b/src/script/cpp_api/s_base.h
index 86f7f7bac..244d81605 100644
--- a/src/script/cpp_api/s_base.h
+++ b/src/script/cpp_api/s_base.h
@@ -39,7 +39,6 @@ extern "C" {
#include "config.h"
#define SCRIPTAPI_LOCK_DEBUG
-#define SCRIPTAPI_DEBUG
// MUST be an invalid mod name so that mods can't
// use that name to bypass security!
@@ -108,7 +107,9 @@ public:
Client* getClient();
#endif
- std::string getOrigin() { return m_last_run_mod; }
+ // IMPORTANT: these cannot be used for any security-related uses, they exist
+ // only to enrich error messages
+ const std::string &getOrigin() { return m_last_run_mod; }
void setOriginDirect(const char *origin);
void setOriginFromTableRaw(int index, const char *fxn);
@@ -124,11 +125,23 @@ protected:
friend class ModApiEnvMod;
friend class LuaVoxelManip;
+ /*
+ Subtle edge case with coroutines: If for whatever reason you have a
+ method in a subclass that's called from existing lua_CFunction
+ (any of the l_*.cpp files) then make it static and take the lua_State*
+ as an argument. This is REQUIRED because getStack() will not return the
+ correct state if called inside coroutines.
+
+ Also note that src/script/common/ is the better place for such helpers.
+ */
lua_State* getStack()
{ return m_luastack; }
+ // Checks that stack size is sane
void realityCheck();
+ // Takes an error from lua_pcall and throws it as a LuaError
void scriptError(int result, const char *fxn);
+ // Dumps stack contents for debugging
void stackDump(std::ostream &o);
void setGameDef(IGameDef* gamedef) { m_gamedef = gamedef; }
diff --git a/src/script/cpp_api/s_client.cpp b/src/script/cpp_api/s_client.cpp
index f2cc9730b..c889fffa0 100644
--- a/src/script/cpp_api/s_client.cpp
+++ b/src/script/cpp_api/s_client.cpp
@@ -33,7 +33,11 @@ void ScriptApiClient::on_mods_loaded()
lua_getglobal(L, "core");
lua_getfield(L, -1, "registered_on_mods_loaded");
// Call callbacks
- runCallbacks(0, RUN_CALLBACKS_MODE_FIRST);
+ try {
+ runCallbacks(0, RUN_CALLBACKS_MODE_FIRST);
+ } catch (LuaError &e) {
+ getClient()->setFatalError(e);
+ }
}
void ScriptApiClient::on_shutdown()
@@ -44,7 +48,11 @@ void ScriptApiClient::on_shutdown()
lua_getglobal(L, "core");
lua_getfield(L, -1, "registered_on_shutdown");
// Call callbacks
- runCallbacks(0, RUN_CALLBACKS_MODE_FIRST);
+ try {
+ runCallbacks(0, RUN_CALLBACKS_MODE_FIRST);
+ } catch (LuaError &e) {
+ getClient()->setFatalError(e);
+ }
}
bool ScriptApiClient::on_sending_message(const std::string &message)
@@ -56,7 +64,12 @@ bool ScriptApiClient::on_sending_message(const std::string &message)
lua_getfield(L, -1, "registered_on_sending_chat_message");
// Call callbacks
lua_pushstring(L, message.c_str());
- runCallbacks(1, RUN_CALLBACKS_MODE_OR_SC);
+ try {
+ runCallbacks(1, RUN_CALLBACKS_MODE_OR_SC);
+ } catch (LuaError &e) {
+ getClient()->setFatalError(e);
+ return true;
+ }
return readParam<bool>(L, -1);
}
@@ -69,7 +82,12 @@ bool ScriptApiClient::on_receiving_message(const std::string &message)
lua_getfield(L, -1, "registered_on_receiving_chat_message");
// Call callbacks
lua_pushstring(L, message.c_str());
- runCallbacks(1, RUN_CALLBACKS_MODE_OR_SC);
+ try {
+ runCallbacks(1, RUN_CALLBACKS_MODE_OR_SC);
+ } catch (LuaError &e) {
+ getClient()->setFatalError(e);
+ return true;
+ }
return readParam<bool>(L, -1);
}
@@ -82,7 +100,11 @@ void ScriptApiClient::on_damage_taken(int32_t damage_amount)
lua_getfield(L, -1, "registered_on_damage_taken");
// Call callbacks
lua_pushinteger(L, damage_amount);
- runCallbacks(1, RUN_CALLBACKS_MODE_OR_SC);
+ try {
+ runCallbacks(1, RUN_CALLBACKS_MODE_OR_SC);
+ } catch (LuaError &e) {
+ getClient()->setFatalError(e);
+ }
}
void ScriptApiClient::on_hp_modification(int32_t newhp)
@@ -94,7 +116,11 @@ void ScriptApiClient::on_hp_modification(int32_t newhp)
lua_getfield(L, -1, "registered_on_hp_modification");
// Call callbacks
lua_pushinteger(L, newhp);
- runCallbacks(1, RUN_CALLBACKS_MODE_OR_SC);
+ try {
+ runCallbacks(1, RUN_CALLBACKS_MODE_OR_SC);
+ } catch (LuaError &e) {
+ getClient()->setFatalError(e);
+ }
}
void ScriptApiClient::on_death()
@@ -105,7 +131,11 @@ void ScriptApiClient::on_death()
lua_getglobal(L, "core");
lua_getfield(L, -1, "registered_on_death");
// Call callbacks
- runCallbacks(0, RUN_CALLBACKS_MODE_FIRST);
+ try {
+ runCallbacks(0, RUN_CALLBACKS_MODE_FIRST);
+ } catch (LuaError &e) {
+ getClient()->setFatalError(e);
+ }
}
void ScriptApiClient::environment_step(float dtime)
@@ -120,8 +150,7 @@ void ScriptApiClient::environment_step(float dtime)
try {
runCallbacks(1, RUN_CALLBACKS_MODE_FIRST);
} catch (LuaError &e) {
- getClient()->setFatalError(std::string("Client environment_step: ") + e.what() + "\n"
- + script_get_backtrace(L));
+ getClient()->setFatalError(e);
}
}
@@ -146,7 +175,11 @@ void ScriptApiClient::on_formspec_input(const std::string &formname,
lua_pushlstring(L, value.c_str(), value.size());
lua_settable(L, -3);
}
- runCallbacks(2, RUN_CALLBACKS_MODE_OR_SC);
+ try {
+ runCallbacks(2, RUN_CALLBACKS_MODE_OR_SC);
+ } catch (LuaError &e) {
+ getClient()->setFatalError(e);
+ }
}
bool ScriptApiClient::on_dignode(v3s16 p, MapNode node)
@@ -164,7 +197,12 @@ bool ScriptApiClient::on_dignode(v3s16 p, MapNode node)
pushnode(L, node, ndef);
// Call functions
- runCallbacks(2, RUN_CALLBACKS_MODE_OR);
+ try {
+ runCallbacks(2, RUN_CALLBACKS_MODE_OR);
+ } catch (LuaError &e) {
+ getClient()->setFatalError(e);
+ return true;
+ }
return lua_toboolean(L, -1);
}
@@ -183,7 +221,12 @@ bool ScriptApiClient::on_punchnode(v3s16 p, MapNode node)
pushnode(L, node, ndef);
// Call functions
- runCallbacks(2, RUN_CALLBACKS_MODE_OR);
+ try {
+ runCallbacks(2, RUN_CALLBACKS_MODE_OR);
+ } catch (LuaError &e) {
+ getClient()->setFatalError(e);
+ return true;
+ }
return readParam<bool>(L, -1);
}
@@ -200,7 +243,12 @@ bool ScriptApiClient::on_placenode(const PointedThing &pointed, const ItemDefini
push_item_definition(L, item);
// Call functions
- runCallbacks(2, RUN_CALLBACKS_MODE_OR);
+ try {
+ runCallbacks(2, RUN_CALLBACKS_MODE_OR);
+ } catch (LuaError &e) {
+ getClient()->setFatalError(e);
+ return true;
+ }
return readParam<bool>(L, -1);
}
@@ -217,7 +265,12 @@ bool ScriptApiClient::on_item_use(const ItemStack &item, const PointedThing &poi
push_pointed_thing(L, pointed, true);
// Call functions
- runCallbacks(2, RUN_CALLBACKS_MODE_OR);
+ try {
+ runCallbacks(2, RUN_CALLBACKS_MODE_OR);
+ } catch (LuaError &e) {
+ getClient()->setFatalError(e);
+ return true;
+ }
return readParam<bool>(L, -1);
}
@@ -238,7 +291,12 @@ bool ScriptApiClient::on_inventory_open(Inventory *inventory)
lua_rawset(L, -3);
}
- runCallbacks(1, RUN_CALLBACKS_MODE_OR);
+ try {
+ runCallbacks(1, RUN_CALLBACKS_MODE_OR);
+ } catch (LuaError &e) {
+ getClient()->setFatalError(e);
+ return true;
+ }
return readParam<bool>(L, -1);
}
diff --git a/src/script/cpp_api/s_entity.cpp b/src/script/cpp_api/s_entity.cpp
index 746f7013e..06337b9e8 100644
--- a/src/script/cpp_api/s_entity.cpp
+++ b/src/script/cpp_api/s_entity.cpp
@@ -240,7 +240,7 @@ void ScriptApiEntity::luaentity_Step(u16 id, float dtime,
// tool_capabilities, direction, damage)
bool ScriptApiEntity::luaentity_Punch(u16 id,
ServerActiveObject *puncher, float time_from_last_punch,
- const ToolCapabilities *toolcap, v3f dir, s16 damage)
+ const ToolCapabilities *toolcap, v3f dir, s32 damage)
{
SCRIPTAPI_PRECHECKHEADER
diff --git a/src/script/cpp_api/s_entity.h b/src/script/cpp_api/s_entity.h
index b52f6e447..7658ae922 100644
--- a/src/script/cpp_api/s_entity.h
+++ b/src/script/cpp_api/s_entity.h
@@ -42,7 +42,7 @@ public:
const collisionMoveResult *moveresult);
bool luaentity_Punch(u16 id,
ServerActiveObject *puncher, float time_from_last_punch,
- const ToolCapabilities *toolcap, v3f dir, s16 damage);
+ const ToolCapabilities *toolcap, v3f dir, s32 damage);
bool luaentity_on_death(u16 id, ServerActiveObject *killer);
void luaentity_Rightclick(u16 id, ServerActiveObject *clicker);
void luaentity_on_attach_child(u16 id, ServerActiveObject *child);
diff --git a/src/script/cpp_api/s_env.cpp b/src/script/cpp_api/s_env.cpp
index 8da5debaa..874c37b6e 100644
--- a/src/script/cpp_api/s_env.cpp
+++ b/src/script/cpp_api/s_env.cpp
@@ -25,6 +25,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "mapgen/mapgen.h"
#include "lua_api/l_env.h"
#include "server.h"
+#include "script/common/c_content.h"
+
void ScriptApiEnv::environment_OnGenerated(v3s16 minp, v3s16 maxp,
u32 blockseed)
@@ -51,13 +53,7 @@ void ScriptApiEnv::environment_Step(float dtime)
lua_getfield(L, -1, "registered_globalsteps");
// Call callbacks
lua_pushnumber(L, dtime);
- try {
- runCallbacks(1, RUN_CALLBACKS_MODE_FIRST);
- } catch (LuaError &e) {
- getServer()->setAsyncFatalError(
- std::string("environment_Step: ") + e.what() + "\n"
- + script_get_backtrace(L));
- }
+ runCallbacks(1, RUN_CALLBACKS_MODE_FIRST);
}
void ScriptApiEnv::player_event(ServerActiveObject *player, const std::string &type)
@@ -74,13 +70,7 @@ void ScriptApiEnv::player_event(ServerActiveObject *player, const std::string &t
// Call callbacks
objectrefGetOrCreate(L, player); // player
lua_pushstring(L,type.c_str()); // event type
- try {
- runCallbacks(2, RUN_CALLBACKS_MODE_FIRST);
- } catch (LuaError &e) {
- getServer()->setAsyncFatalError(
- std::string("player_event: ") + e.what() + "\n"
- + script_get_backtrace(L) );
- }
+ runCallbacks(2, RUN_CALLBACKS_MODE_FIRST);
}
void ScriptApiEnv::initializeEnvironment(ServerEnvironment *env)
@@ -150,13 +140,19 @@ void ScriptApiEnv::initializeEnvironment(ServerEnvironment *env)
bool simple_catch_up = true;
getboolfield(L, current_abm, "catch_up", simple_catch_up);
+
+ s16 min_y = INT16_MIN;
+ getintfield(L, current_abm, "min_y", min_y);
+
+ s16 max_y = INT16_MAX;
+ getintfield(L, current_abm, "max_y", max_y);
lua_getfield(L, current_abm, "action");
luaL_checktype(L, current_abm + 1, LUA_TFUNCTION);
lua_pop(L, 1);
LuaABM *abm = new LuaABM(L, id, trigger_contents, required_neighbors,
- trigger_interval, trigger_chance, simple_catch_up);
+ trigger_interval, trigger_chance, simple_catch_up, min_y, max_y);
env->addActiveBlockModifier(abm);
@@ -249,9 +245,8 @@ void ScriptApiEnv::on_emerge_area_completion(
try {
PCALL_RES(lua_pcall(L, 4, 0, error_handler));
} catch (LuaError &e) {
- server->setAsyncFatalError(
- std::string("on_emerge_area_completion: ") + e.what() + "\n"
- + script_get_backtrace(L));
+ // Note: don't throw here, we still need to run the cleanup code below
+ server->setAsyncFatalError(e);
}
lua_pop(L, 1); // Pop error handler
@@ -261,3 +256,35 @@ void ScriptApiEnv::on_emerge_area_completion(
luaL_unref(L, LUA_REGISTRYINDEX, state->args_ref);
}
}
+
+void ScriptApiEnv::on_liquid_transformed(
+ const std::vector<std::pair<v3s16, MapNode>> &list)
+{
+ SCRIPTAPI_PRECHECKHEADER
+
+ // Get core.registered_on_liquid_transformed
+ lua_getglobal(L, "core");
+ lua_getfield(L, -1, "registered_on_liquid_transformed");
+ luaL_checktype(L, -1, LUA_TTABLE);
+ lua_remove(L, -2);
+
+ // Skip converting list and calling hook if there are
+ // no registered callbacks.
+ if(lua_objlen(L, -1) < 1) return;
+
+ // Convert the list to a pos array and a node array for lua
+ int index = 1;
+ const NodeDefManager *ndef = getEnv()->getGameDef()->ndef();
+ lua_createtable(L, list.size(), 0);
+ lua_createtable(L, list.size(), 0);
+ for(std::pair<v3s16, MapNode> p : list) {
+ lua_pushnumber(L, index);
+ push_v3s16(L, p.first);
+ lua_rawset(L, -4);
+ lua_pushnumber(L, index++);
+ pushnode(L, p.second, ndef);
+ lua_rawset(L, -3);
+ }
+
+ runCallbacks(2, RUN_CALLBACKS_MODE_FIRST);
+}
diff --git a/src/script/cpp_api/s_env.h b/src/script/cpp_api/s_env.h
index 232a08aaf..090858f17 100644
--- a/src/script/cpp_api/s_env.h
+++ b/src/script/cpp_api/s_env.h
@@ -21,6 +21,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "cpp_api/s_base.h"
#include "irr_v3d.h"
+#include "mapnode.h"
+#include <vector>
class ServerEnvironment;
struct ScriptCallbackState;
@@ -41,5 +43,8 @@ public:
void on_emerge_area_completion(v3s16 blockpos, int action,
ScriptCallbackState *state);
+ // Called after liquid transform changes
+ void on_liquid_transformed(const std::vector<std::pair<v3s16, MapNode>> &list);
+
void initializeEnvironment(ServerEnvironment *env);
};
diff --git a/src/script/cpp_api/s_item.cpp b/src/script/cpp_api/s_item.cpp
index 24955cefc..b1916070e 100644
--- a/src/script/cpp_api/s_item.cpp
+++ b/src/script/cpp_api/s_item.cpp
@@ -29,6 +29,9 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "inventory.h"
#include "inventorymanager.h"
+#define WRAP_LUAERROR(e, detail) \
+ LuaError(std::string(__FUNCTION__) + ": " + (e).what() + ". " detail)
+
bool ScriptApiItem::item_OnDrop(ItemStack &item,
ServerActiveObject *dropper, v3f pos)
{
@@ -49,20 +52,21 @@ bool ScriptApiItem::item_OnDrop(ItemStack &item,
try {
item = read_item(L, -1, getServer()->idef());
} catch (LuaError &e) {
- throw LuaError(std::string(e.what()) + ". item=" + item.name);
+ throw WRAP_LUAERROR(e, "item=" + item.name);
}
}
lua_pop(L, 2); // Pop item and error handler
return true;
}
-bool ScriptApiItem::item_OnPlace(ItemStack &item,
+bool ScriptApiItem::item_OnPlace(Optional<ItemStack> &ret_item,
ServerActiveObject *placer, const PointedThing &pointed)
{
SCRIPTAPI_PRECHECKHEADER
int error_handler = PUSH_ERROR_HANDLER(L);
+ const ItemStack &item = *ret_item;
// Push callback function on stack
if (!getItemCallback(item.name.c_str(), "on_place"))
return false;
@@ -79,22 +83,25 @@ bool ScriptApiItem::item_OnPlace(ItemStack &item,
PCALL_RES(lua_pcall(L, 3, 1, error_handler));
if (!lua_isnil(L, -1)) {
try {
- item = read_item(L, -1, getServer()->idef());
+ ret_item = read_item(L, -1, getServer()->idef());
} catch (LuaError &e) {
- throw LuaError(std::string(e.what()) + ". item=" + item.name);
+ throw WRAP_LUAERROR(e, "item=" + item.name);
}
+ } else {
+ ret_item = nullopt;
}
lua_pop(L, 2); // Pop item and error handler
return true;
}
-bool ScriptApiItem::item_OnUse(ItemStack &item,
+bool ScriptApiItem::item_OnUse(Optional<ItemStack> &ret_item,
ServerActiveObject *user, const PointedThing &pointed)
{
SCRIPTAPI_PRECHECKHEADER
int error_handler = PUSH_ERROR_HANDLER(L);
+ const ItemStack &item = *ret_item;
// Push callback function on stack
if (!getItemCallback(item.name.c_str(), "on_use"))
return false;
@@ -106,22 +113,25 @@ bool ScriptApiItem::item_OnUse(ItemStack &item,
PCALL_RES(lua_pcall(L, 3, 1, error_handler));
if(!lua_isnil(L, -1)) {
try {
- item = read_item(L, -1, getServer()->idef());
+ ret_item = read_item(L, -1, getServer()->idef());
} catch (LuaError &e) {
- throw LuaError(std::string(e.what()) + ". item=" + item.name);
+ throw WRAP_LUAERROR(e, "item=" + item.name);
}
+ } else {
+ ret_item = nullopt;
}
lua_pop(L, 2); // Pop item and error handler
return true;
}
-bool ScriptApiItem::item_OnSecondaryUse(ItemStack &item,
+bool ScriptApiItem::item_OnSecondaryUse(Optional<ItemStack> &ret_item,
ServerActiveObject *user, const PointedThing &pointed)
{
SCRIPTAPI_PRECHECKHEADER
int error_handler = PUSH_ERROR_HANDLER(L);
+ const ItemStack &item = *ret_item;
if (!getItemCallback(item.name.c_str(), "on_secondary_use"))
return false;
@@ -131,10 +141,12 @@ bool ScriptApiItem::item_OnSecondaryUse(ItemStack &item,
PCALL_RES(lua_pcall(L, 3, 1, error_handler));
if (!lua_isnil(L, -1)) {
try {
- item = read_item(L, -1, getServer()->idef());
+ ret_item = read_item(L, -1, getServer()->idef());
} catch (LuaError &e) {
- throw LuaError(std::string(e.what()) + ". item=" + item.name);
+ throw WRAP_LUAERROR(e, "item=" + item.name);
}
+ } else {
+ ret_item = nullopt;
}
lua_pop(L, 2); // Pop item and error handler
return true;
@@ -165,7 +177,7 @@ bool ScriptApiItem::item_OnCraft(ItemStack &item, ServerActiveObject *user,
try {
item = read_item(L, -1, getServer()->idef());
} catch (LuaError &e) {
- throw LuaError(std::string(e.what()) + ". item=" + item.name);
+ throw WRAP_LUAERROR(e, "item=" + item.name);
}
}
lua_pop(L, 2); // Pop item and error handler
@@ -197,7 +209,7 @@ bool ScriptApiItem::item_CraftPredict(ItemStack &item, ServerActiveObject *user,
try {
item = read_item(L, -1, getServer()->idef());
} catch (LuaError &e) {
- throw LuaError(std::string(e.what()) + ". item=" + item.name);
+ throw WRAP_LUAERROR(e, "item=" + item.name);
}
}
lua_pop(L, 2); // Pop item and error handler
diff --git a/src/script/cpp_api/s_item.h b/src/script/cpp_api/s_item.h
index 25a3501f9..5015d8bd4 100644
--- a/src/script/cpp_api/s_item.h
+++ b/src/script/cpp_api/s_item.h
@@ -21,6 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "cpp_api/s_base.h"
#include "irr_v3d.h"
+#include "util/Optional.h"
struct PointedThing;
struct ItemStack;
@@ -35,13 +36,20 @@ class ScriptApiItem
: virtual public ScriptApiBase
{
public:
+ /*
+ * Functions with Optional<ItemStack> are for callbacks where Lua may
+ * want to prevent the engine from modifying the inventory after it's done.
+ * This has a longer backstory where on_use may need to empty the player's
+ * inventory without the engine interfering (see issue #6546).
+ */
+
bool item_OnDrop(ItemStack &item,
ServerActiveObject *dropper, v3f pos);
- bool item_OnPlace(ItemStack &item,
+ bool item_OnPlace(Optional<ItemStack> &item,
ServerActiveObject *placer, const PointedThing &pointed);
- bool item_OnUse(ItemStack &item,
+ bool item_OnUse(Optional<ItemStack> &item,
ServerActiveObject *user, const PointedThing &pointed);
- bool item_OnSecondaryUse(ItemStack &item,
+ bool item_OnSecondaryUse(Optional<ItemStack> &item,
ServerActiveObject *user, const PointedThing &pointed);
bool item_OnCraft(ItemStack &item, ServerActiveObject *user,
const InventoryList *old_craft_grid, const InventoryLocation &craft_inv);
diff --git a/src/script/cpp_api/s_mainmenu.h b/src/script/cpp_api/s_mainmenu.h
index aef36ce39..470577a29 100644
--- a/src/script/cpp_api/s_mainmenu.h
+++ b/src/script/cpp_api/s_mainmenu.h
@@ -38,7 +38,7 @@ public:
void handleMainMenuEvent(std::string text);
/**
- * process field data recieved from formspec
+ * process field data received from formspec
* @param fields data in field format
*/
void handleMainMenuButtons(const StringMap &fields);
diff --git a/src/script/cpp_api/s_node.cpp b/src/script/cpp_api/s_node.cpp
index f23fbfbde..029cb6308 100644
--- a/src/script/cpp_api/s_node.cpp
+++ b/src/script/cpp_api/s_node.cpp
@@ -65,6 +65,7 @@ struct EnumString ScriptApiNode::es_ContentParamType2[] =
{CPT2_COLORED_FACEDIR, "colorfacedir"},
{CPT2_COLORED_WALLMOUNTED, "colorwallmounted"},
{CPT2_GLASSLIKE_LIQUID_LEVEL, "glasslikeliquidlevel"},
+ {CPT2_COLORED_DEGROTATE, "colordegrotate"},
{0, NULL},
};
diff --git a/src/script/cpp_api/s_node.h b/src/script/cpp_api/s_node.h
index 3f771c838..3c6a8445b 100644
--- a/src/script/cpp_api/s_node.h
+++ b/src/script/cpp_api/s_node.h
@@ -53,6 +53,7 @@ public:
static struct EnumString es_ContentParamType[];
static struct EnumString es_ContentParamType2[];
static struct EnumString es_LiquidType[];
+ static struct EnumString es_LiquidMoveType[];
static struct EnumString es_NodeBoxType[];
static struct EnumString es_TextureAlphaMode[];
};
diff --git a/src/script/cpp_api/s_nodemeta.cpp b/src/script/cpp_api/s_nodemeta.cpp
index c081e9fc4..7ab3757f3 100644
--- a/src/script/cpp_api/s_nodemeta.cpp
+++ b/src/script/cpp_api/s_nodemeta.cpp
@@ -43,7 +43,7 @@ int ScriptApiNodemeta::nodemeta_inventory_AllowMove(
return 0;
// Push callback function on stack
- std::string nodename = ndef->get(node).name;
+ const auto &nodename = ndef->get(node).name;
if (!getItemCallback(nodename.c_str(), "allow_metadata_inventory_move", &ma.to_inv.p))
return count;
@@ -58,7 +58,7 @@ int ScriptApiNodemeta::nodemeta_inventory_AllowMove(
PCALL_RES(lua_pcall(L, 7, 1, error_handler));
if (!lua_isnumber(L, -1))
throw LuaError("allow_metadata_inventory_move should"
- " return a number, guilty node: " + nodename);
+ " return a number. node=" + nodename);
int num = luaL_checkinteger(L, -1);
lua_pop(L, 2); // Pop integer and error handler
return num;
@@ -81,7 +81,7 @@ int ScriptApiNodemeta::nodemeta_inventory_AllowPut(
return 0;
// Push callback function on stack
- std::string nodename = ndef->get(node).name;
+ const auto &nodename = ndef->get(node).name;
if (!getItemCallback(nodename.c_str(), "allow_metadata_inventory_put", &ma.to_inv.p))
return stack.count;
@@ -94,7 +94,7 @@ int ScriptApiNodemeta::nodemeta_inventory_AllowPut(
PCALL_RES(lua_pcall(L, 5, 1, error_handler));
if(!lua_isnumber(L, -1))
throw LuaError("allow_metadata_inventory_put should"
- " return a number, guilty node: " + nodename);
+ " return a number. node=" + nodename);
int num = luaL_checkinteger(L, -1);
lua_pop(L, 2); // Pop integer and error handler
return num;
@@ -117,7 +117,7 @@ int ScriptApiNodemeta::nodemeta_inventory_AllowTake(
return 0;
// Push callback function on stack
- std::string nodename = ndef->get(node).name;
+ const auto &nodename = ndef->get(node).name;
if (!getItemCallback(nodename.c_str(), "allow_metadata_inventory_take", &ma.from_inv.p))
return stack.count;
@@ -130,7 +130,7 @@ int ScriptApiNodemeta::nodemeta_inventory_AllowTake(
PCALL_RES(lua_pcall(L, 5, 1, error_handler));
if (!lua_isnumber(L, -1))
throw LuaError("allow_metadata_inventory_take should"
- " return a number, guilty node: " + nodename);
+ " return a number. node=" + nodename);
int num = luaL_checkinteger(L, -1);
lua_pop(L, 2); // Pop integer and error handler
return num;
@@ -153,7 +153,7 @@ void ScriptApiNodemeta::nodemeta_inventory_OnMove(
return;
// Push callback function on stack
- std::string nodename = ndef->get(node).name;
+ const auto &nodename = ndef->get(node).name;
if (!getItemCallback(nodename.c_str(), "on_metadata_inventory_move", &ma.from_inv.p))
return;
@@ -186,7 +186,7 @@ void ScriptApiNodemeta::nodemeta_inventory_OnPut(
return;
// Push callback function on stack
- std::string nodename = ndef->get(node).name;
+ const auto &nodename = ndef->get(node).name;
if (!getItemCallback(nodename.c_str(), "on_metadata_inventory_put", &ma.to_inv.p))
return;
@@ -217,7 +217,7 @@ void ScriptApiNodemeta::nodemeta_inventory_OnTake(
return;
// Push callback function on stack
- std::string nodename = ndef->get(node).name;
+ const auto &nodename = ndef->get(node).name;
if (!getItemCallback(nodename.c_str(), "on_metadata_inventory_take", &ma.from_inv.p))
return;
diff --git a/src/script/cpp_api/s_player.cpp b/src/script/cpp_api/s_player.cpp
index d3e6138dc..22b24f363 100644
--- a/src/script/cpp_api/s_player.cpp
+++ b/src/script/cpp_api/s_player.cpp
@@ -60,7 +60,7 @@ bool ScriptApiPlayer::on_punchplayer(ServerActiveObject *player,
float time_from_last_punch,
const ToolCapabilities *toolcap,
v3f dir,
- s16 damage)
+ s32 damage)
{
SCRIPTAPI_PRECHECKHEADER
// Get core.registered_on_punchplayers
diff --git a/src/script/cpp_api/s_player.h b/src/script/cpp_api/s_player.h
index c0f141862..e866aee46 100644
--- a/src/script/cpp_api/s_player.h
+++ b/src/script/cpp_api/s_player.h
@@ -46,7 +46,7 @@ public:
void on_cheat(ServerActiveObject *player, const std::string &cheat_type);
bool on_punchplayer(ServerActiveObject *player, ServerActiveObject *hitter,
float time_from_last_punch, const ToolCapabilities *toolcap,
- v3f dir, s16 damage);
+ v3f dir, s32 damage);
void on_rightclickplayer(ServerActiveObject *player, ServerActiveObject *clicker);
s32 on_player_hpchange(ServerActiveObject *player, s32 hp_change,
const PlayerHPChangeReason &reason);
diff --git a/src/script/cpp_api/s_security.cpp b/src/script/cpp_api/s_security.cpp
index 63058d7c3..a6c5114b2 100644
--- a/src/script/cpp_api/s_security.cpp
+++ b/src/script/cpp_api/s_security.cpp
@@ -18,7 +18,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
*/
#include "cpp_api/s_security.h"
-
+#include "lua_api/l_base.h"
#include "filesys.h"
#include "porting.h"
#include "server.h"
@@ -27,6 +27,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <cerrno>
#include <string>
+#include <algorithm>
#include <iostream>
@@ -45,6 +46,21 @@ static inline void copy_safe(lua_State *L, const char *list[], unsigned len, int
}
}
+static void shallow_copy_table(lua_State *L, int from=-2, int to=-1)
+{
+ if (from < 0) from = lua_gettop(L) + from + 1;
+ if (to < 0) to = lua_gettop(L) + to + 1;
+ lua_pushnil(L);
+ while (lua_next(L, from) != 0) {
+ assert(lua_type(L, -1) != LUA_TTABLE);
+ // duplicate key and value for lua_rawset
+ lua_pushvalue(L, -2);
+ lua_pushvalue(L, -2);
+ lua_rawset(L, to);
+ lua_pop(L, 1);
+ }
+}
+
// Pushes the original version of a library function on the stack, from the old version
static inline void push_original(lua_State *L, const char *lib, const char *func)
{
@@ -83,11 +99,15 @@ void ScriptApiSecurity::initializeSecurity()
"unpack",
"_VERSION",
"xpcall",
- // Completely safe libraries
+ };
+ static const char *whitelist_tables[] = {
+ // These libraries are completely safe BUT we need to duplicate their table
+ // to ensure the sandbox can't affect the insecure env
"coroutine",
"string",
"table",
"math",
+ "bit"
};
static const char *io_whitelist[] = {
"close",
@@ -101,21 +121,17 @@ void ScriptApiSecurity::initializeSecurity()
"date",
"difftime",
"getenv",
- "setlocale",
"time",
- "tmpname",
};
static const char *debug_whitelist[] = {
"gethook",
"traceback",
"getinfo",
"getmetatable",
- "setupvalue",
"setmetatable",
"upvalueid",
"sethook",
"debug",
- "setlocal",
};
static const char *package_whitelist[] = {
"config",
@@ -167,6 +183,17 @@ void ScriptApiSecurity::initializeSecurity()
lua_pop(L, 1);
+ // Copy safe libraries
+ for (const char *libname : whitelist_tables) {
+ lua_getfield(L, old_globals, libname);
+ lua_newtable(L);
+ shallow_copy_table(L);
+
+ lua_setglobal(L, libname);
+ lua_pop(L, 1);
+ }
+
+
// Copy safe IO functions
lua_getfield(L, old_globals, "io");
lua_newtable(L);
@@ -190,6 +217,7 @@ void ScriptApiSecurity::initializeSecurity()
// And replace unsafe ones
SECURE_API(os, remove);
SECURE_API(os, rename);
+ SECURE_API(os, setlocale);
lua_setglobal(L, "os");
lua_pop(L, 1); // Pop old OS
@@ -221,7 +249,25 @@ void ScriptApiSecurity::initializeSecurity()
lua_pop(L, 1); // Pop old jit
#endif
+ // Get rid of 'core' in the old globals, we don't want anyone thinking it's
+ // safe or even usable.
+ lua_pushnil(L);
+ lua_setfield(L, old_globals, "core");
+
lua_pop(L, 1); // Pop globals_backup
+
+
+ /*
+ * In addition to copying the tables in whitelist_tables, we also need to
+ * replace the string metatable. Otherwise old_globals.string would
+ * be accessible via getmetatable("").__index from inside the sandbox.
+ */
+ lua_pushliteral(L, "");
+ lua_newtable(L);
+ lua_getglobal(L, "string");
+ lua_setfield(L, -2, "__index");
+ lua_setmetatable(L, -2);
+ lua_pop(L, 1); // Pop empty string
}
void ScriptApiSecurity::initializeSecurityClient()
@@ -243,7 +289,7 @@ void ScriptApiSecurity::initializeSecurityClient()
"rawset",
"select",
"setfenv",
- // getmetatable can be used to escape the sandbox
+ // getmetatable can be used to escape the sandbox <- ???
"setmetatable",
"tonumber",
"tostring",
@@ -256,6 +302,7 @@ void ScriptApiSecurity::initializeSecurityClient()
"string",
"table",
"math",
+ "bit",
};
static const char *os_whitelist[] = {
"clock",
@@ -264,7 +311,7 @@ void ScriptApiSecurity::initializeSecurityClient()
"time"
};
static const char *debug_whitelist[] = {
- "getinfo",
+ "getinfo", // used by builtin and unset before mods load
"traceback"
};
@@ -496,15 +543,8 @@ bool ScriptApiSecurity::checkPath(lua_State *L, const char *path,
if (!removed.empty())
abs_path += DIR_DELIM + removed;
- // Get server from registry
- lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_SCRIPTAPI);
- ScriptApiBase *script;
-#if INDIRECT_SCRIPTAPI_RIDX
- script = (ScriptApiBase *) *(void**)(lua_touserdata(L, -1));
-#else
- script = (ScriptApiBase *) lua_touserdata(L, -1);
-#endif
- lua_pop(L, 1);
+ // Get gamedef from registry
+ ScriptApiBase *script = ModApiBase::getScriptApiBase(L);
const IGameDef *gamedef = script->getGameDef();
if (!gamedef)
return false;
@@ -569,6 +609,38 @@ bool ScriptApiSecurity::checkPath(lua_State *L, const char *path,
return false;
}
+bool ScriptApiSecurity::checkWhitelisted(lua_State *L, const std::string &setting)
+{
+ assert(str_starts_with(setting, "secure."));
+
+ // We have to make sure that this function is being called directly by
+ // a mod, otherwise a malicious mod could override this function and
+ // steal its return value.
+ lua_Debug info;
+
+ // Make sure there's only one item below this function on the stack...
+ if (lua_getstack(L, 2, &info))
+ return false;
+ FATAL_ERROR_IF(!lua_getstack(L, 1, &info), "lua_getstack() failed");
+ FATAL_ERROR_IF(!lua_getinfo(L, "S", &info), "lua_getinfo() failed");
+
+ // ...and that that item is the main file scope.
+ if (strcmp(info.what, "main") != 0)
+ return false;
+
+ // Mod must be listed in secure.http_mods or secure.trusted_mods
+ lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
+ if (!lua_isstring(L, -1))
+ return false;
+ std::string mod_name = readParam<std::string>(L, -1);
+
+ std::string value = g_settings->get(setting);
+ value.erase(std::remove(value.begin(), value.end(), ' '), value.end());
+ auto mod_list = str_split(value, ',');
+
+ return CONTAINS(mod_list, mod_name);
+}
+
int ScriptApiSecurity::sl_g_dofile(lua_State *L)
{
@@ -627,13 +699,7 @@ int ScriptApiSecurity::sl_g_load(lua_State *L)
int ScriptApiSecurity::sl_g_loadfile(lua_State *L)
{
#ifndef SERVER
- lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_SCRIPTAPI);
-#if INDIRECT_SCRIPTAPI_RIDX
- ScriptApiBase *script = (ScriptApiBase *) *(void**)(lua_touserdata(L, -1));
-#else
- ScriptApiBase *script = (ScriptApiBase *) lua_touserdata(L, -1);
-#endif
- lua_pop(L, 1);
+ ScriptApiBase *script = ModApiBase::getScriptApiBase(L);
// Client implementation
if (script->getType() == ScriptingType::Client) {
@@ -806,3 +872,20 @@ int ScriptApiSecurity::sl_os_remove(lua_State *L)
return 2;
}
+
+int ScriptApiSecurity::sl_os_setlocale(lua_State *L)
+{
+ const bool cat = lua_gettop(L) > 1;
+ // Don't allow changes
+ if (!lua_isnoneornil(L, 1)) {
+ lua_pushnil(L);
+ return 1;
+ }
+
+ push_original(L, "os", "setlocale");
+ lua_pushnil(L);
+ if (cat)
+ lua_pushvalue(L, 2);
+ lua_call(L, cat ? 2 : 1, 1);
+ return 1;
+}
diff --git a/src/script/cpp_api/s_security.h b/src/script/cpp_api/s_security.h
index 73e763548..880ce1638 100644
--- a/src/script/cpp_api/s_security.h
+++ b/src/script/cpp_api/s_security.h
@@ -40,11 +40,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
class ScriptApiSecurity : virtual public ScriptApiBase
{
public:
- int getThread(lua_State *L);
- // creates an empty Lua environment
- void createEmptyEnv(lua_State *L);
- // sets the enviroment to the table thats on top of the stack
- void setLuaEnv(lua_State *L, int thread);
// Sets up security on the ScriptApi's Lua state
void initializeSecurity();
void initializeSecurityClient();
@@ -57,8 +52,17 @@ public:
// Checks if mods are allowed to read (and optionally write) to the path
static bool checkPath(lua_State *L, const char *path, bool write_required,
bool *write_allowed=NULL);
+ // Check if mod is whitelisted in the given setting
+ // This additionally checks that the mod's main file scope is executing.
+ static bool checkWhitelisted(lua_State *L, const std::string &setting);
private:
+ int getThread(lua_State *L);
+ // sets the enviroment to the table thats on top of the stack
+ void setLuaEnv(lua_State *L, int thread);
+ // creates an empty Lua environment
+ void createEmptyEnv(lua_State *L);
+
// Syntax: "sl_" <Library name or 'g' (global)> '_' <Function name>
// (sl stands for Secure Lua)
@@ -75,4 +79,5 @@ private:
static int sl_os_rename(lua_State *L);
static int sl_os_remove(lua_State *L);
+ static int sl_os_setlocale(lua_State *L);
};
diff --git a/src/script/cpp_api/s_server.cpp b/src/script/cpp_api/s_server.cpp
index 96cb28b28..c255b0c71 100644
--- a/src/script/cpp_api/s_server.cpp
+++ b/src/script/cpp_api/s_server.cpp
@@ -20,6 +20,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "cpp_api/s_server.h"
#include "cpp_api/s_internal.h"
#include "common/c_converter.h"
+#include "util/numeric.h" // myrand
bool ScriptApiServer::getAuth(const std::string &playername,
std::string *dst_password,
@@ -196,3 +197,66 @@ std::string ScriptApiServer::formatChatMessage(const std::string &name,
return ret;
}
+
+u32 ScriptApiServer::allocateDynamicMediaCallback(lua_State *L, int f_idx)
+{
+ if (f_idx < 0)
+ f_idx = lua_gettop(L) + f_idx + 1;
+
+ lua_getglobal(L, "core");
+ lua_getfield(L, -1, "dynamic_media_callbacks");
+ luaL_checktype(L, -1, LUA_TTABLE);
+
+ // Find a randomly generated token that doesn't exist yet
+ int tries = 100;
+ u32 token;
+ while (1) {
+ token = myrand();
+ lua_rawgeti(L, -2, token);
+ bool is_free = lua_isnil(L, -1);
+ lua_pop(L, 1);
+ if (is_free)
+ break;
+ if (--tries < 0)
+ FATAL_ERROR("Ran out of callbacks IDs?!");
+ }
+
+ // core.dynamic_media_callbacks[token] = callback_func
+ lua_pushvalue(L, f_idx);
+ lua_rawseti(L, -2, token);
+
+ lua_pop(L, 2);
+
+ verbosestream << "allocateDynamicMediaCallback() = " << token << std::endl;
+ return token;
+}
+
+void ScriptApiServer::freeDynamicMediaCallback(u32 token)
+{
+ SCRIPTAPI_PRECHECKHEADER
+
+ verbosestream << "freeDynamicMediaCallback(" << token << ")" << std::endl;
+
+ // core.dynamic_media_callbacks[token] = nil
+ lua_getglobal(L, "core");
+ lua_getfield(L, -1, "dynamic_media_callbacks");
+ luaL_checktype(L, -1, LUA_TTABLE);
+ lua_pushnil(L);
+ lua_rawseti(L, -2, token);
+ lua_pop(L, 2);
+}
+
+void ScriptApiServer::on_dynamic_media_added(u32 token, const char *playername)
+{
+ SCRIPTAPI_PRECHECKHEADER
+
+ int error_handler = PUSH_ERROR_HANDLER(L);
+ lua_getglobal(L, "core");
+ lua_getfield(L, -1, "dynamic_media_callbacks");
+ luaL_checktype(L, -1, LUA_TTABLE);
+ lua_rawgeti(L, -1, token);
+ luaL_checktype(L, -1, LUA_TFUNCTION);
+
+ lua_pushstring(L, playername);
+ PCALL_RES(lua_pcall(L, 1, 0, error_handler));
+}
diff --git a/src/script/cpp_api/s_server.h b/src/script/cpp_api/s_server.h
index d8639cba7..58c8c0e48 100644
--- a/src/script/cpp_api/s_server.h
+++ b/src/script/cpp_api/s_server.h
@@ -49,6 +49,12 @@ public:
const std::string &password);
bool setPassword(const std::string &playername,
const std::string &password);
+
+ /* dynamic media handling */
+ static u32 allocateDynamicMediaCallback(lua_State *L, int f_idx);
+ void freeDynamicMediaCallback(u32 token);
+ void on_dynamic_media_added(u32 token, const char *playername);
+
private:
void getAuthHandler();
void readPrivileges(int index, std::set<std::string> &result);
diff --git a/src/script/lua_api/l_areastore.cpp b/src/script/lua_api/l_areastore.cpp
index 908c766b0..45724e604 100644
--- a/src/script/lua_api/l_areastore.cpp
+++ b/src/script/lua_api/l_areastore.cpp
@@ -372,7 +372,7 @@ void LuaAreaStore::Register(lua_State *L)
lua_pop(L, 1); // drop metatable
- luaL_openlib(L, 0, methods, 0); // fill methodtable
+ luaL_register(L, nullptr, methods); // fill methodtable
lua_pop(L, 1); // drop methodtable
// Can be created from Lua (AreaStore())
diff --git a/src/script/lua_api/l_auth.cpp b/src/script/lua_api/l_auth.cpp
index 0fc57ba3a..32d8a7411 100644
--- a/src/script/lua_api/l_auth.cpp
+++ b/src/script/lua_api/l_auth.cpp
@@ -32,8 +32,11 @@ AuthDatabase *ModApiAuth::getAuthDb(lua_State *L)
{
ServerEnvironment *server_environment =
dynamic_cast<ServerEnvironment *>(getEnv(L));
- if (!server_environment)
+ if (!server_environment) {
+ luaL_error(L, "Attempt to access an auth function but the auth"
+ " system is yet not initialized. This causes bugs.");
return nullptr;
+ }
return server_environment->getAuthDatabase();
}
diff --git a/src/script/lua_api/l_camera.cpp b/src/script/lua_api/l_camera.cpp
index 40251154c..d85d16283 100644
--- a/src/script/lua_api/l_camera.cpp
+++ b/src/script/lua_api/l_camera.cpp
@@ -219,7 +219,7 @@ void LuaCamera::Register(lua_State *L)
lua_pop(L, 1);
- luaL_openlib(L, 0, methods, 0);
+ luaL_register(L, nullptr, methods);
lua_pop(L, 1);
}
diff --git a/src/script/lua_api/l_env.cpp b/src/script/lua_api/l_env.cpp
index c75fc8dc7..18ee3a521 100644
--- a/src/script/lua_api/l_env.cpp
+++ b/src/script/lua_api/l_env.cpp
@@ -46,13 +46,22 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "client/client.h"
#endif
-struct EnumString ModApiEnvMod::es_ClearObjectsMode[] =
+const EnumString ModApiEnvMod::es_ClearObjectsMode[] =
{
{CLEAR_OBJECTS_MODE_FULL, "full"},
{CLEAR_OBJECTS_MODE_QUICK, "quick"},
{0, NULL},
};
+const EnumString ModApiEnvMod::es_BlockStatusType[] =
+{
+ {ServerEnvironment::BS_UNKNOWN, "unknown"},
+ {ServerEnvironment::BS_EMERGING, "emerging"},
+ {ServerEnvironment::BS_LOADED, "loaded"},
+ {ServerEnvironment::BS_ACTIVE, "active"},
+ {0, NULL},
+};
+
///////////////////////////////////////////////////////////////////////////////
@@ -228,7 +237,7 @@ void LuaRaycast::Register(lua_State *L)
lua_pop(L, 1);
- luaL_openlib(L, 0, methods, 0);
+ luaL_register(L, nullptr, methods);
lua_pop(L, 1);
lua_register(L, className, create_object);
@@ -468,7 +477,7 @@ int ModApiEnvMod::l_place_node(lua_State *L)
return 1;
}
// Create item to place
- ItemStack item(ndef->get(n).name, 1, 0, idef);
+ Optional<ItemStack> item = ItemStack(ndef->get(n).name, 1, 0, idef);
// Make pointed position
PointedThing pointed;
pointed.type = POINTEDTHING_NODE;
@@ -871,6 +880,21 @@ int ModApiEnvMod::l_find_node_near(lua_State *L)
return 0;
}
+static void checkArea(v3s16 &minp, v3s16 &maxp)
+{
+ auto volume = VoxelArea(minp, maxp).getVolume();
+ // Volume limit equal to 8 default mapchunks, (80 * 2) ^ 3 = 4,096,000
+ if (volume > 4096000) {
+ throw LuaError("Area volume exceeds allowed value of 4096000");
+ }
+
+ // Clamp to map range to avoid problems
+#define CLAMP(arg) core::clamp(arg, (s16)-MAX_MAP_GENERATION_LIMIT, (s16)MAX_MAP_GENERATION_LIMIT)
+ minp = v3s16(CLAMP(minp.X), CLAMP(minp.Y), CLAMP(minp.Z));
+ maxp = v3s16(CLAMP(maxp.X), CLAMP(maxp.Y), CLAMP(maxp.Z));
+#undef CLAMP
+}
+
// find_nodes_in_area(minp, maxp, nodenames, [grouped])
int ModApiEnvMod::l_find_nodes_in_area(lua_State *L)
{
@@ -890,13 +914,7 @@ int ModApiEnvMod::l_find_nodes_in_area(lua_State *L)
}
#endif
- v3s16 cube = maxp - minp + 1;
- // Volume limit equal to 8 default mapchunks, (80 * 2) ^ 3 = 4,096,000
- if ((u64)cube.X * (u64)cube.Y * (u64)cube.Z > 4096000) {
- luaL_error(L, "find_nodes_in_area(): area volume"
- " exceeds allowed value of 4096000");
- return 0;
- }
+ checkArea(minp, maxp);
std::vector<content_t> filter;
collectNodeIds(L, 3, ndef, filter);
@@ -1001,13 +1019,7 @@ int ModApiEnvMod::l_find_nodes_in_area_under_air(lua_State *L)
}
#endif
- v3s16 cube = maxp - minp + 1;
- // Volume limit equal to 8 default mapchunks, (80 * 2) ^ 3 = 4,096,000
- if ((u64)cube.X * (u64)cube.Y * (u64)cube.Z > 4096000) {
- luaL_error(L, "find_nodes_in_area_under_air(): area volume"
- " exceeds allowed value of 4096000");
- return 0;
- }
+ checkArea(minp, maxp);
std::vector<content_t> filter;
collectNodeIds(L, 3, ndef, filter);
@@ -1389,6 +1401,24 @@ int ModApiEnvMod::l_forceload_block(lua_State *L)
return 0;
}
+// compare_block_status(nodepos)
+int ModApiEnvMod::l_compare_block_status(lua_State *L)
+{
+ GET_ENV_PTR;
+
+ v3s16 nodepos = check_v3s16(L, 1);
+ std::string condition_s = luaL_checkstring(L, 2);
+ auto status = env->getBlockStatus(getNodeBlockPos(nodepos));
+
+ int condition_i = -1;
+ if (!string_to_enum(es_BlockStatusType, condition_i, condition_s))
+ return 0; // Unsupported
+
+ lua_pushboolean(L, status >= condition_i);
+ return 1;
+}
+
+
// forceload_free_block(blockpos)
// blockpos = {x=num, y=num, z=num}
int ModApiEnvMod::l_forceload_free_block(lua_State *L)
@@ -1462,6 +1492,7 @@ void ModApiEnvMod::Initialize(lua_State *L, int top)
API_FCT(transforming_liquid_add);
API_FCT(forceload_block);
API_FCT(forceload_free_block);
+ API_FCT(compare_block_status);
API_FCT(get_translated_string);
}
diff --git a/src/script/lua_api/l_env.h b/src/script/lua_api/l_env.h
index 42c2d64f8..67c76faae 100644
--- a/src/script/lua_api/l_env.h
+++ b/src/script/lua_api/l_env.h
@@ -195,6 +195,9 @@ private:
// stops forceloading a position
static int l_forceload_free_block(lua_State *L);
+ // compare_block_status(nodepos)
+ static int l_compare_block_status(lua_State *L);
+
// Get a string translated server side
static int l_get_translated_string(lua_State * L);
@@ -207,7 +210,8 @@ public:
static void Initialize(lua_State *L, int top);
static void InitializeClient(lua_State *L, int top);
- static struct EnumString es_ClearObjectsMode[];
+ static const EnumString es_ClearObjectsMode[];
+ static const EnumString es_BlockStatusType[];
};
class LuaABM : public ActiveBlockModifier {
@@ -219,17 +223,21 @@ private:
float m_trigger_interval;
u32 m_trigger_chance;
bool m_simple_catch_up;
+ s16 m_min_y;
+ s16 m_max_y;
public:
LuaABM(lua_State *L, int id,
const std::vector<std::string> &trigger_contents,
const std::vector<std::string> &required_neighbors,
- float trigger_interval, u32 trigger_chance, bool simple_catch_up):
+ float trigger_interval, u32 trigger_chance, bool simple_catch_up, s16 min_y, s16 max_y):
m_id(id),
m_trigger_contents(trigger_contents),
m_required_neighbors(required_neighbors),
m_trigger_interval(trigger_interval),
m_trigger_chance(trigger_chance),
- m_simple_catch_up(simple_catch_up)
+ m_simple_catch_up(simple_catch_up),
+ m_min_y(min_y),
+ m_max_y(max_y)
{
}
virtual const std::vector<std::string> &getTriggerContents() const
@@ -252,6 +260,14 @@ public:
{
return m_simple_catch_up;
}
+ virtual s16 getMinY()
+ {
+ return m_min_y;
+ }
+ virtual s16 getMaxY()
+ {
+ return m_max_y;
+ }
virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n,
u32 active_object_count, u32 active_object_count_wider);
};
diff --git a/src/script/lua_api/l_http.cpp b/src/script/lua_api/l_http.cpp
index 5ea3b3f99..bd359b3cc 100644
--- a/src/script/lua_api/l_http.cpp
+++ b/src/script/lua_api/l_http.cpp
@@ -21,14 +21,13 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "common/c_converter.h"
#include "common/c_content.h"
#include "lua_api/l_http.h"
+#include "cpp_api/s_security.h"
#include "httpfetch.h"
#include "settings.h"
#include "debug.h"
#include "log.h"
-#include <algorithm>
#include <iomanip>
-#include <cctype>
#define HTTP_API(name) \
lua_pushstring(L, #name); \
@@ -42,12 +41,10 @@ void ModApiHttp::read_http_fetch_request(lua_State *L, HTTPFetchRequest &req)
req.caller = httpfetch_caller_alloc_secure();
getstringfield(L, 1, "url", req.url);
- lua_getfield(L, 1, "user_agent");
- if (lua_isstring(L, -1))
- req.useragent = getstringfield_default(L, 1, "user_agent", "");
- lua_pop(L, 1);
+ getstringfield(L, 1, "user_agent", req.useragent);
req.multipart = getboolfield_default(L, 1, "multipart", false);
- req.timeout = getintfield_default(L, 1, "timeout", 3) * 1000;
+ if (getintfield(L, 1, "timeout", req.timeout))
+ req.timeout *= 1000;
lua_getfield(L, 1, "method");
if (lua_isstring(L, -1)) {
@@ -165,58 +162,40 @@ int ModApiHttp::l_http_fetch_async_get(lua_State *L)
return 1;
}
-int ModApiHttp::l_request_http_api(lua_State *L)
+int ModApiHttp::l_set_http_api_lua(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
- // We have to make sure that this function is being called directly by
- // a mod, otherwise a malicious mod could override this function and
- // steal its return value.
- lua_Debug info;
-
- // Make sure there's only one item below this function on the stack...
- if (lua_getstack(L, 2, &info)) {
- return 0;
- }
- FATAL_ERROR_IF(!lua_getstack(L, 1, &info), "lua_getstack() failed");
- FATAL_ERROR_IF(!lua_getinfo(L, "S", &info), "lua_getinfo() failed");
-
- // ...and that that item is the main file scope.
- if (strcmp(info.what, "main") != 0) {
- return 0;
- }
-
- // Mod must be listed in secure.http_mods or secure.trusted_mods
- lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
- if (!lua_isstring(L, -1)) {
- return 0;
- }
+ // This is called by builtin to give us a function that will later
+ // populate the http_api table with additional method(s).
+ // We need this because access to the HTTP api is security-relevant and
+ // any mod could just mess with a global variable.
+ luaL_checktype(L, 1, LUA_TFUNCTION);
+ lua_rawseti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_HTTP_API_LUA);
- std::string mod_name = readParam<std::string>(L, -1);
- std::string http_mods = g_settings->get("secure.http_mods");
- http_mods.erase(std::remove(http_mods.begin(), http_mods.end(), ' '), http_mods.end());
- std::vector<std::string> mod_list_http = str_split(http_mods, ',');
+ return 0;
+}
- std::string trusted_mods = g_settings->get("secure.trusted_mods");
- trusted_mods.erase(std::remove(trusted_mods.begin(), trusted_mods.end(), ' '), trusted_mods.end());
- std::vector<std::string> mod_list_trusted = str_split(trusted_mods, ',');
+int ModApiHttp::l_request_http_api(lua_State *L)
+{
+ NO_MAP_LOCK_REQUIRED;
- mod_list_http.insert(mod_list_http.end(), mod_list_trusted.begin(), mod_list_trusted.end());
- if (std::find(mod_list_http.begin(), mod_list_http.end(), mod_name) == mod_list_http.end()) {
+ if (!ScriptApiSecurity::checkWhitelisted(L, "secure.http_mods") &&
+ !ScriptApiSecurity::checkWhitelisted(L, "secure.trusted_mods")) {
lua_pushnil(L);
return 1;
}
- lua_getglobal(L, "core");
- lua_getfield(L, -1, "http_add_fetch");
+ lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_HTTP_API_LUA);
+ assert(lua_isfunction(L, -1));
lua_newtable(L);
HTTP_API(fetch_async);
HTTP_API(fetch_async_get);
// Stack now looks like this:
- // <core.http_add_fetch> <table with fetch_async, fetch_async_get>
- // Now call core.http_add_fetch to append .fetch(request, callback) to table
+ // <function> <table with fetch_async, fetch_async_get>
+ // Now call it to append .fetch(request, callback) to table
lua_call(L, 1, 1);
return 1;
@@ -249,6 +228,7 @@ void ModApiHttp::Initialize(lua_State *L, int top)
API_FCT(get_http_api);
} else {
API_FCT(request_http_api);
+ API_FCT(set_http_api_lua);
}
#endif
diff --git a/src/script/lua_api/l_http.h b/src/script/lua_api/l_http.h
index c3a2a5276..17fa283ba 100644
--- a/src/script/lua_api/l_http.h
+++ b/src/script/lua_api/l_http.h
@@ -41,6 +41,9 @@ private:
// http_fetch_async_get(handle)
static int l_http_fetch_async_get(lua_State *L);
+ // set_http_api_lua() [internal]
+ static int l_set_http_api_lua(lua_State *L);
+
// request_http_api()
static int l_request_http_api(lua_State *L);
diff --git a/src/script/lua_api/l_inventory.cpp b/src/script/lua_api/l_inventory.cpp
index 434d0a76c..b0a4ee194 100644
--- a/src/script/lua_api/l_inventory.cpp
+++ b/src/script/lua_api/l_inventory.cpp
@@ -421,19 +421,6 @@ void InvRef::create(lua_State *L, const InventoryLocation &loc)
luaL_getmetatable(L, className);
lua_setmetatable(L, -2);
}
-void InvRef::createPlayer(lua_State *L, RemotePlayer *player)
-{
- NO_MAP_LOCK_REQUIRED;
- InventoryLocation loc;
- loc.setPlayer(player->getName());
- create(L, loc);
-}
-void InvRef::createNodeMeta(lua_State *L, v3s16 p)
-{
- InventoryLocation loc;
- loc.setNodeMeta(p);
- create(L, loc);
-}
void InvRef::Register(lua_State *L)
{
@@ -456,7 +443,7 @@ void InvRef::Register(lua_State *L)
lua_pop(L, 1); // drop metatable
- luaL_openlib(L, 0, methods, 0); // fill methodtable
+ luaL_register(L, nullptr, methods); // fill methodtable
lua_pop(L, 1); // drop methodtable
// Cannot be created from Lua
diff --git a/src/script/lua_api/l_inventory.h b/src/script/lua_api/l_inventory.h
index 94f670c9d..6a75bac0f 100644
--- a/src/script/lua_api/l_inventory.h
+++ b/src/script/lua_api/l_inventory.h
@@ -111,8 +111,6 @@ public:
// Creates an InvRef and leaves it on top of stack
// Not callable from Lua; all references are created on the C side.
static void create(lua_State *L, const InventoryLocation &loc);
- static void createPlayer(lua_State *L, RemotePlayer *player);
- static void createNodeMeta(lua_State *L, v3s16 p);
static void Register(lua_State *L);
};
diff --git a/src/script/lua_api/l_item.cpp b/src/script/lua_api/l_item.cpp
index 9e0da4034..794d8a6e5 100644
--- a/src/script/lua_api/l_item.cpp
+++ b/src/script/lua_api/l_item.cpp
@@ -483,7 +483,7 @@ void LuaItemStack::Register(lua_State *L)
lua_pop(L, 1); // drop metatable
- luaL_openlib(L, 0, methods, 0); // fill methodtable
+ luaL_register(L, nullptr, methods); // fill methodtable
lua_pop(L, 1); // drop methodtable
// Can be created from Lua (ItemStack(itemstack or itemstring or table or nil))
diff --git a/src/script/lua_api/l_itemstackmeta.cpp b/src/script/lua_api/l_itemstackmeta.cpp
index d1ba1bda4..739fb9221 100644
--- a/src/script/lua_api/l_itemstackmeta.cpp
+++ b/src/script/lua_api/l_itemstackmeta.cpp
@@ -114,7 +114,7 @@ void ItemStackMetaRef::Register(lua_State *L)
lua_pop(L, 1); // drop metatable
- luaL_openlib(L, 0, methods, 0); // fill methodtable
+ luaL_register(L, nullptr, methods); // fill methodtable
lua_pop(L, 1); // drop methodtable
// Cannot be created from Lua
diff --git a/src/script/lua_api/l_localplayer.cpp b/src/script/lua_api/l_localplayer.cpp
index 33fa27c8b..2efb976c7 100644
--- a/src/script/lua_api/l_localplayer.cpp
+++ b/src/script/lua_api/l_localplayer.cpp
@@ -128,11 +128,11 @@ int LuaLocalPlayer::l_is_in_liquid_stable(lua_State *L)
return 1;
}
-int LuaLocalPlayer::l_get_liquid_viscosity(lua_State *L)
+int LuaLocalPlayer::l_get_move_resistance(lua_State *L)
{
LocalPlayer *player = getobject(L, 1);
- lua_pushinteger(L, player->liquid_viscosity);
+ lua_pushinteger(L, player->move_resistance);
return 1;
}
@@ -223,16 +223,22 @@ int LuaLocalPlayer::l_get_control(lua_State *L)
};
lua_createtable(L, 0, 12);
- set("up", c.up);
- set("down", c.down);
- set("left", c.left);
- set("right", c.right);
- set("jump", c.jump);
- set("aux1", c.aux1);
+ set("jump", c.jump);
+ set("aux1", c.aux1);
set("sneak", c.sneak);
- set("zoom", c.zoom);
- set("dig", c.dig);
+ set("zoom", c.zoom);
+ set("dig", c.dig);
set("place", c.place);
+ // Player movement in polar coordinates and non-binary speed
+ lua_pushnumber(L, c.movement_speed);
+ lua_setfield(L, -2, "movement_speed");
+ lua_pushnumber(L, c.movement_direction);
+ lua_setfield(L, -2, "movement_direction");
+ // Provide direction keys to ensure compatibility
+ set("up", c.direction_keys & (1 << 0));
+ set("down", c.direction_keys & (1 << 1));
+ set("left", c.direction_keys & (1 << 2));
+ set("right", c.direction_keys & (1 << 3));
return 1;
}
@@ -369,10 +375,11 @@ int LuaLocalPlayer::l_hud_change(lua_State *L)
if (!element)
return 0;
+ HudElementStat stat;
void *unused;
- read_hud_change(L, element, &unused);
+ bool ok = read_hud_change(L, stat, element, &unused);
- lua_pushboolean(L, true);
+ lua_pushboolean(L, ok);
return 1;
}
@@ -446,7 +453,7 @@ void LuaLocalPlayer::Register(lua_State *L)
lua_pop(L, 1); // Drop metatable
- luaL_openlib(L, 0, methods, 0); // fill methodtable
+ luaL_register(L, nullptr, methods); // fill methodtable
lua_pop(L, 1); // Drop methodtable
}
@@ -461,7 +468,6 @@ const luaL_Reg LuaLocalPlayer::methods[] = {
luamethod(LuaLocalPlayer, is_touching_ground),
luamethod(LuaLocalPlayer, is_in_liquid),
luamethod(LuaLocalPlayer, is_in_liquid_stable),
- luamethod(LuaLocalPlayer, get_liquid_viscosity),
luamethod(LuaLocalPlayer, is_climbing),
luamethod(LuaLocalPlayer, swimming_vertical),
luamethod(LuaLocalPlayer, get_physics_override),
@@ -483,5 +489,7 @@ const luaL_Reg LuaLocalPlayer::methods[] = {
luamethod(LuaLocalPlayer, hud_change),
luamethod(LuaLocalPlayer, hud_get),
+ luamethod(LuaLocalPlayer, get_move_resistance),
+
{0, 0}
};
diff --git a/src/script/lua_api/l_localplayer.h b/src/script/lua_api/l_localplayer.h
index 4413f2bdb..041545a49 100644
--- a/src/script/lua_api/l_localplayer.h
+++ b/src/script/lua_api/l_localplayer.h
@@ -51,7 +51,6 @@ private:
static int l_is_touching_ground(lua_State *L);
static int l_is_in_liquid(lua_State *L);
static int l_is_in_liquid_stable(lua_State *L);
- static int l_get_liquid_viscosity(lua_State *L);
static int l_is_climbing(lua_State *L);
static int l_swimming_vertical(lua_State *L);
@@ -96,6 +95,8 @@ private:
// hud_get(self, id)
static int l_hud_get(lua_State *L);
+ static int l_get_move_resistance(lua_State *L);
+
LocalPlayer *m_localplayer = nullptr;
public:
diff --git a/src/script/lua_api/l_mainmenu.cpp b/src/script/lua_api/l_mainmenu.cpp
index 3e9709bde..736ad022f 100644
--- a/src/script/lua_api/l_mainmenu.cpp
+++ b/src/script/lua_api/l_mainmenu.cpp
@@ -21,6 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "lua_api/l_internal.h"
#include "common/c_content.h"
#include "cpp_api/s_async.h"
+#include "scripting_mainmenu.h"
#include "gui/guiEngine.h"
#include "gui/guiMainMenu.h"
#include "gui/guiKeyChangeMenu.h"
@@ -34,9 +35,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "serverlist.h"
#include "mapgen/mapgen.h"
#include "settings.h"
-
-#include <IFileArchive.h>
-#include <IFileSystem.h>
+#include "client/client.h"
#include "client/renderingengine.h"
#include "network/networkprotocol.h"
@@ -399,7 +398,8 @@ int ModApiMainMenu::l_show_keys_menu(lua_State *L)
GUIEngine* engine = getGuiEngine(L);
sanity_check(engine != NULL);
- GUIKeyChangeMenu *kmenu = new GUIKeyChangeMenu(RenderingEngine::get_gui_env(),
+ GUIKeyChangeMenu *kmenu = new GUIKeyChangeMenu(
+ engine->m_rendering_engine->get_gui_env(),
engine->m_parent,
-1,
engine->m_menumanager,
@@ -414,25 +414,53 @@ int ModApiMainMenu::l_create_world(lua_State *L)
const char *name = luaL_checkstring(L, 1);
int gameidx = luaL_checkinteger(L,2) -1;
+ StringMap use_settings;
+ luaL_checktype(L, 3, LUA_TTABLE);
+ lua_pushnil(L);
+ while (lua_next(L, 3) != 0) {
+ // key at index -2 and value at index -1
+ use_settings[luaL_checkstring(L, -2)] = luaL_checkstring(L, -1);
+ lua_pop(L, 1);
+ }
+ lua_pop(L, 1);
+
std::string path = porting::path_user + DIR_DELIM
"worlds" + DIR_DELIM
+ sanitizeDirName(name, "world_");
std::vector<SubgameSpec> games = getAvailableGames();
+ if (gameidx < 0 || gameidx >= (int) games.size()) {
+ lua_pushstring(L, "Invalid game index");
+ return 1;
+ }
- if ((gameidx >= 0) &&
- (gameidx < (int) games.size())) {
+ // Set the settings for world creation
+ // this is a bad hack but the best we have right now..
+ StringMap backup;
+ for (auto it : use_settings) {
+ if (g_settings->existsLocal(it.first))
+ backup[it.first] = g_settings->get(it.first);
+ g_settings->set(it.first, it.second);
+ }
- // Create world if it doesn't exist
- try {
- loadGameConfAndInitWorld(path, name, games[gameidx], true);
- lua_pushnil(L);
- } catch (const BaseException &e) {
- lua_pushstring(L, (std::string("Failed to initialize world: ") + e.what()).c_str());
- }
- } else {
- lua_pushstring(L, "Invalid game index");
+ // Create world if it doesn't exist
+ try {
+ loadGameConfAndInitWorld(path, name, games[gameidx], true);
+ lua_pushnil(L);
+ } catch (const BaseException &e) {
+ auto err = std::string("Failed to initialize world: ") + e.what();
+ lua_pushstring(L, err.c_str());
+ }
+
+ // Restore previous settings
+ for (auto it : use_settings) {
+ auto it2 = backup.find(it.first);
+ if (it2 == backup.end())
+ g_settings->remove(it.first); // wasn't set before
+ else
+ g_settings->set(it.first, it2->second); // was set before
}
+
return 1;
}
@@ -503,6 +531,21 @@ int ModApiMainMenu::l_get_modpath(lua_State *L)
}
/******************************************************************************/
+int ModApiMainMenu::l_get_modpaths(lua_State *L)
+{
+ int index = 1;
+ lua_newtable(L);
+ ModApiMainMenu::l_get_modpath(L);
+ lua_rawseti(L, -2, index);
+ for (const std::string &component : getEnvModPaths()) {
+ index++;
+ lua_pushstring(L, component.c_str());
+ lua_rawseti(L, -2, index);
+ }
+ return 1;
+}
+
+/******************************************************************************/
int ModApiMainMenu::l_get_clientmodpath(lua_State *L)
{
std::string modpath = fs::RemoveRelativePathComponents(
@@ -548,7 +591,10 @@ int ModApiMainMenu::l_get_cache_path(lua_State *L)
/******************************************************************************/
int ModApiMainMenu::l_get_temp_path(lua_State *L)
{
- lua_pushstring(L, fs::TempPath().c_str());
+ if (lua_isnoneornil(L, 1) || !lua_toboolean(L, 1))
+ lua_pushstring(L, fs::TempPath().c_str());
+ else
+ lua_pushstring(L, fs::CreateTempFile().c_str());
return 1;
}
@@ -588,26 +634,24 @@ int ModApiMainMenu::l_copy_dir(lua_State *L)
const char *destination = luaL_checkstring(L, 2);
bool keep_source = true;
+ if (!lua_isnoneornil(L, 3))
+ keep_source = readParam<bool>(L, 3);
- if ((!lua_isnone(L,3)) &&
- (!lua_isnil(L,3))) {
- keep_source = readParam<bool>(L,3);
- }
-
- std::string absolute_destination = fs::RemoveRelativePathComponents(destination);
- std::string absolute_source = fs::RemoveRelativePathComponents(source);
-
- if ((ModApiMainMenu::mayModifyPath(absolute_destination))) {
- bool retval = fs::CopyDir(absolute_source,absolute_destination);
-
- if (retval && (!keep_source)) {
+ std::string abs_destination = fs::RemoveRelativePathComponents(destination);
+ std::string abs_source = fs::RemoveRelativePathComponents(source);
- retval &= fs::RecursiveDelete(absolute_source);
- }
- lua_pushboolean(L,retval);
+ if (!ModApiMainMenu::mayModifyPath(abs_destination) ||
+ (!keep_source && !ModApiMainMenu::mayModifyPath(abs_source))) {
+ lua_pushboolean(L, false);
return 1;
}
- lua_pushboolean(L,false);
+
+ bool retval;
+ if (keep_source)
+ retval = fs::CopyDir(abs_source, abs_destination);
+ else
+ retval = fs::MoveDir(abs_source, abs_destination);
+ lua_pushboolean(L, retval);
return 1;
}
@@ -629,75 +673,9 @@ int ModApiMainMenu::l_extract_zip(lua_State *L)
std::string absolute_destination = fs::RemoveRelativePathComponents(destination);
if (ModApiMainMenu::mayModifyPath(absolute_destination)) {
- fs::CreateAllDirs(absolute_destination);
-
- io::IFileSystem *fs = RenderingEngine::get_filesystem();
-
- if (!fs->addFileArchive(zipfile, false, false, io::EFAT_ZIP)) {
- lua_pushboolean(L,false);
- return 1;
- }
-
- sanity_check(fs->getFileArchiveCount() > 0);
-
- /**********************************************************************/
- /* WARNING this is not threadsafe!! */
- /**********************************************************************/
- io::IFileArchive* opened_zip =
- fs->getFileArchive(fs->getFileArchiveCount()-1);
-
- const io::IFileList* files_in_zip = opened_zip->getFileList();
-
- unsigned int number_of_files = files_in_zip->getFileCount();
-
- for (unsigned int i=0; i < number_of_files; i++) {
- std::string fullpath = destination;
- fullpath += DIR_DELIM;
- fullpath += files_in_zip->getFullFileName(i).c_str();
- std::string fullpath_dir = fs::RemoveLastPathComponent(fullpath);
-
- if (!files_in_zip->isDirectory(i)) {
- if (!fs::PathExists(fullpath_dir) && !fs::CreateAllDirs(fullpath_dir)) {
- fs->removeFileArchive(fs->getFileArchiveCount()-1);
- lua_pushboolean(L,false);
- return 1;
- }
-
- io::IReadFile* toread = opened_zip->createAndOpenFile(i);
-
- FILE *targetfile = fopen(fullpath.c_str(),"wb");
-
- if (targetfile == NULL) {
- fs->removeFileArchive(fs->getFileArchiveCount()-1);
- lua_pushboolean(L,false);
- return 1;
- }
-
- char read_buffer[1024];
- long total_read = 0;
-
- while (total_read < toread->getSize()) {
-
- unsigned int bytes_read =
- toread->read(read_buffer,sizeof(read_buffer));
- if ((bytes_read == 0 ) ||
- (fwrite(read_buffer, 1, bytes_read, targetfile) != bytes_read))
- {
- fclose(targetfile);
- fs->removeFileArchive(fs->getFileArchiveCount()-1);
- lua_pushboolean(L,false);
- return 1;
- }
- total_read += bytes_read;
- }
-
- fclose(targetfile);
- }
-
- }
-
- fs->removeFileArchive(fs->getFileArchiveCount()-1);
- lua_pushboolean(L,true);
+ auto fs = RenderingEngine::get_raw_device()->getFileSystem();
+ bool ok = fs::extractZipFile(fs, zipfile, destination);
+ lua_pushboolean(L, ok);
return 1;
}
@@ -763,7 +741,7 @@ int ModApiMainMenu::l_show_path_select_dialog(lua_State *L)
bool is_file_select = readParam<bool>(L, 3);
GUIFileSelectMenu* fileOpenMenu =
- new GUIFileSelectMenu(RenderingEngine::get_gui_env(),
+ new GUIFileSelectMenu(engine->m_rendering_engine->get_gui_env(),
engine->m_parent,
-1,
engine->m_menumanager,
@@ -804,13 +782,12 @@ int ModApiMainMenu::l_get_video_drivers(lua_State *L)
lua_newtable(L);
for (u32 i = 0; i != drivers.size(); i++) {
- const char *name = RenderingEngine::getVideoDriverName(drivers[i]);
- const char *fname = RenderingEngine::getVideoDriverFriendlyName(drivers[i]);
+ auto &info = RenderingEngine::getVideoDriverInfo(drivers[i]);
lua_newtable(L);
- lua_pushstring(L, name);
+ lua_pushstring(L, info.name.c_str());
lua_setfield(L, -2, "name");
- lua_pushstring(L, fname);
+ lua_pushstring(L, info.friendly_name.c_str());
lua_setfield(L, -2, "friendly_name");
lua_rawseti(L, -2, i + 1);
@@ -820,32 +797,11 @@ int ModApiMainMenu::l_get_video_drivers(lua_State *L)
}
/******************************************************************************/
-int ModApiMainMenu::l_get_video_modes(lua_State *L)
-{
- std::vector<core::vector3d<u32> > videomodes
- = RenderingEngine::getSupportedVideoModes();
-
- lua_newtable(L);
- for (u32 i = 0; i != videomodes.size(); i++) {
- lua_newtable(L);
- lua_pushnumber(L, videomodes[i].X);
- lua_setfield(L, -2, "w");
- lua_pushnumber(L, videomodes[i].Y);
- lua_setfield(L, -2, "h");
- lua_pushnumber(L, videomodes[i].Z);
- lua_setfield(L, -2, "depth");
-
- lua_rawseti(L, -2, i + 1);
- }
-
- return 1;
-}
-
-/******************************************************************************/
int ModApiMainMenu::l_gettext(lua_State *L)
{
- std::string text = strgettext(std::string(luaL_checkstring(L, 1)));
- lua_pushstring(L, text.c_str());
+ const char *srctext = luaL_checkstring(L, 1);
+ const char *text = *srctext ? gettext(srctext) : "";
+ lua_pushstring(L, text);
return 1;
}
@@ -859,15 +815,7 @@ int ModApiMainMenu::l_get_screen_info(lua_State *L)
lua_pushnumber(L,RenderingEngine::getDisplayDensity());
lua_settable(L, top);
- lua_pushstring(L,"display_width");
- lua_pushnumber(L,RenderingEngine::getDisplaySize().X);
- lua_settable(L, top);
-
- lua_pushstring(L,"display_height");
- lua_pushnumber(L,RenderingEngine::getDisplaySize().Y);
- lua_settable(L, top);
-
- const v2u32 &window_size = RenderingEngine::get_instance()->getWindowSize();
+ const v2u32 &window_size = RenderingEngine::getWindowSize();
lua_pushstring(L,"window_width");
lua_pushnumber(L, window_size.X);
lua_settable(L, top);
@@ -875,6 +823,10 @@ int ModApiMainMenu::l_get_screen_info(lua_State *L)
lua_pushstring(L,"window_height");
lua_pushnumber(L, window_size.Y);
lua_settable(L, top);
+
+ lua_pushstring(L, "render_info");
+ lua_pushstring(L, wide_to_utf8(RenderingEngine::get_video_driver()->getName()).c_str());
+ lua_settable(L, top);
return 1;
}
@@ -910,20 +862,20 @@ int ModApiMainMenu::l_open_dir(lua_State *L)
/******************************************************************************/
int ModApiMainMenu::l_do_async_callback(lua_State *L)
{
- GUIEngine* engine = getGuiEngine(L);
+ MainMenuScripting *script = getScriptApi<MainMenuScripting>(L);
size_t func_length, param_length;
const char* serialized_func_raw = luaL_checklstring(L, 1, &func_length);
-
const char* serialized_param_raw = luaL_checklstring(L, 2, &param_length);
sanity_check(serialized_func_raw != NULL);
sanity_check(serialized_param_raw != NULL);
- std::string serialized_func = std::string(serialized_func_raw, func_length);
- std::string serialized_param = std::string(serialized_param_raw, param_length);
+ u32 jobId = script->queueAsync(
+ std::string(serialized_func_raw, func_length),
+ std::string(serialized_param_raw, param_length));
- lua_pushinteger(L, engine->queueAsync(serialized_func, serialized_param));
+ lua_pushinteger(L, jobId);
return 1;
}
@@ -949,6 +901,7 @@ void ModApiMainMenu::Initialize(lua_State *L, int top)
API_FCT(get_mapgen_names);
API_FCT(get_user_path);
API_FCT(get_modpath);
+ API_FCT(get_modpaths);
API_FCT(get_clientmodpath);
API_FCT(get_gamepath);
API_FCT(get_texturepath);
@@ -966,7 +919,6 @@ void ModApiMainMenu::Initialize(lua_State *L, int top)
API_FCT(download_file);
API_FCT(gettext);
API_FCT(get_video_drivers);
- API_FCT(get_video_modes);
API_FCT(get_screen_info);
API_FCT(get_min_supp_proto);
API_FCT(get_max_supp_proto);
@@ -983,6 +935,7 @@ void ModApiMainMenu::InitializeAsync(lua_State *L, int top)
API_FCT(get_mapgen_names);
API_FCT(get_user_path);
API_FCT(get_modpath);
+ API_FCT(get_modpaths);
API_FCT(get_clientmodpath);
API_FCT(get_gamepath);
API_FCT(get_texturepath);
@@ -993,10 +946,10 @@ void ModApiMainMenu::InitializeAsync(lua_State *L, int top)
API_FCT(delete_dir);
API_FCT(copy_dir);
API_FCT(is_dir);
- //API_FCT(extract_zip); //TODO remove dependency to GuiEngine
+ API_FCT(extract_zip);
API_FCT(may_modify_path);
API_FCT(download_file);
API_FCT(get_min_supp_proto);
API_FCT(get_max_supp_proto);
- //API_FCT(gettext); (gettext lib isn't threadsafe)
+ API_FCT(gettext);
}
diff --git a/src/script/lua_api/l_mainmenu.h b/src/script/lua_api/l_mainmenu.h
index 33ac9e721..781185425 100644
--- a/src/script/lua_api/l_mainmenu.h
+++ b/src/script/lua_api/l_mainmenu.h
@@ -112,6 +112,8 @@ private:
static int l_get_modpath(lua_State *L);
+ static int l_get_modpaths(lua_State *L);
+
static int l_get_clientmodpath(lua_State *L);
static int l_get_gamepath(lua_State *L);
@@ -140,8 +142,6 @@ private:
static int l_get_video_drivers(lua_State *L);
- static int l_get_video_modes(lua_State *L);
-
//version compatibility
static int l_get_min_supp_proto(lua_State *L);
diff --git a/src/script/lua_api/l_mapgen.cpp b/src/script/lua_api/l_mapgen.cpp
index 12a497b1e..f173bd162 100644
--- a/src/script/lua_api/l_mapgen.cpp
+++ b/src/script/lua_api/l_mapgen.cpp
@@ -482,9 +482,7 @@ int ModApiMapgen::l_get_biome_id(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
- const char *biome_str = lua_tostring(L, 1);
- if (!biome_str)
- return 0;
+ const char *biome_str = luaL_checkstring(L, 1);
const BiomeManager *bmgr = getServer(L)->getEmergeManager()->getBiomeManager();
if (!bmgr)
@@ -527,30 +525,12 @@ int ModApiMapgen::l_get_heat(lua_State *L)
v3s16 pos = read_v3s16(L, 1);
- NoiseParams np_heat;
- NoiseParams np_heat_blend;
-
- MapSettingsManager *settingsmgr =
- getServer(L)->getEmergeManager()->map_settings_mgr;
-
- if (!settingsmgr->getMapSettingNoiseParams("mg_biome_np_heat",
- &np_heat) ||
- !settingsmgr->getMapSettingNoiseParams("mg_biome_np_heat_blend",
- &np_heat_blend))
- return 0;
+ const BiomeGen *biomegen = getServer(L)->getEmergeManager()->getBiomeGen();
- std::string value;
- if (!settingsmgr->getMapSetting("seed", &value))
- return 0;
- std::istringstream ss(value);
- u64 seed;
- ss >> seed;
-
- const BiomeManager *bmgr = getServer(L)->getEmergeManager()->getBiomeManager();
- if (!bmgr)
+ if (!biomegen || biomegen->getType() != BIOMEGEN_ORIGINAL)
return 0;
- float heat = bmgr->getHeatAtPosOriginal(pos, np_heat, np_heat_blend, seed);
+ float heat = ((BiomeGenOriginal*) biomegen)->calcHeatAtPoint(pos);
lua_pushnumber(L, heat);
@@ -566,31 +546,12 @@ int ModApiMapgen::l_get_humidity(lua_State *L)
v3s16 pos = read_v3s16(L, 1);
- NoiseParams np_humidity;
- NoiseParams np_humidity_blend;
+ const BiomeGen *biomegen = getServer(L)->getEmergeManager()->getBiomeGen();
- MapSettingsManager *settingsmgr =
- getServer(L)->getEmergeManager()->map_settings_mgr;
-
- if (!settingsmgr->getMapSettingNoiseParams("mg_biome_np_humidity",
- &np_humidity) ||
- !settingsmgr->getMapSettingNoiseParams("mg_biome_np_humidity_blend",
- &np_humidity_blend))
- return 0;
-
- std::string value;
- if (!settingsmgr->getMapSetting("seed", &value))
- return 0;
- std::istringstream ss(value);
- u64 seed;
- ss >> seed;
-
- const BiomeManager *bmgr = getServer(L)->getEmergeManager()->getBiomeManager();
- if (!bmgr)
+ if (!biomegen || biomegen->getType() != BIOMEGEN_ORIGINAL)
return 0;
- float humidity = bmgr->getHumidityAtPosOriginal(pos, np_humidity,
- np_humidity_blend, seed);
+ float humidity = ((BiomeGenOriginal*) biomegen)->calcHumidityAtPoint(pos);
lua_pushnumber(L, humidity);
@@ -606,45 +567,11 @@ int ModApiMapgen::l_get_biome_data(lua_State *L)
v3s16 pos = read_v3s16(L, 1);
- NoiseParams np_heat;
- NoiseParams np_heat_blend;
- NoiseParams np_humidity;
- NoiseParams np_humidity_blend;
-
- MapSettingsManager *settingsmgr =
- getServer(L)->getEmergeManager()->map_settings_mgr;
-
- if (!settingsmgr->getMapSettingNoiseParams("mg_biome_np_heat",
- &np_heat) ||
- !settingsmgr->getMapSettingNoiseParams("mg_biome_np_heat_blend",
- &np_heat_blend) ||
- !settingsmgr->getMapSettingNoiseParams("mg_biome_np_humidity",
- &np_humidity) ||
- !settingsmgr->getMapSettingNoiseParams("mg_biome_np_humidity_blend",
- &np_humidity_blend))
- return 0;
-
- std::string value;
- if (!settingsmgr->getMapSetting("seed", &value))
- return 0;
- std::istringstream ss(value);
- u64 seed;
- ss >> seed;
-
- const BiomeManager *bmgr = getServer(L)->getEmergeManager()->getBiomeManager();
- if (!bmgr)
+ const BiomeGen *biomegen = getServer(L)->getEmergeManager()->getBiomeGen();
+ if (!biomegen)
return 0;
- float heat = bmgr->getHeatAtPosOriginal(pos, np_heat, np_heat_blend, seed);
- if (!heat)
- return 0;
-
- float humidity = bmgr->getHumidityAtPosOriginal(pos, np_humidity,
- np_humidity_blend, seed);
- if (!humidity)
- return 0;
-
- const Biome *biome = bmgr->getBiomeFromNoiseOriginal(heat, humidity, pos);
+ const Biome *biome = biomegen->calcBiomeAtPoint(pos);
if (!biome || biome->index == OBJDEF_INVALID_INDEX)
return 0;
@@ -653,11 +580,16 @@ int ModApiMapgen::l_get_biome_data(lua_State *L)
lua_pushinteger(L, biome->index);
lua_setfield(L, -2, "biome");
- lua_pushnumber(L, heat);
- lua_setfield(L, -2, "heat");
+ if (biomegen->getType() == BIOMEGEN_ORIGINAL) {
+ float heat = ((BiomeGenOriginal*) biomegen)->calcHeatAtPoint(pos);
+ float humidity = ((BiomeGenOriginal*) biomegen)->calcHumidityAtPoint(pos);
- lua_pushnumber(L, humidity);
- lua_setfield(L, -2, "humidity");
+ lua_pushnumber(L, heat);
+ lua_setfield(L, -2, "heat");
+
+ lua_pushnumber(L, humidity);
+ lua_setfield(L, -2, "humidity");
+ }
return 1;
}
@@ -820,9 +752,7 @@ int ModApiMapgen::l_get_mapgen_params(lua_State *L)
lua_setfield(L, -2, "mgname");
settingsmgr->getMapSetting("seed", &value);
- std::istringstream ss(value);
- u64 seed;
- ss >> seed;
+ u64 seed = from_string<u64>(value);
lua_pushinteger(L, seed);
lua_setfield(L, -2, "seed");
@@ -1493,9 +1423,12 @@ int ModApiMapgen::l_generate_ores(lua_State *L)
NO_MAP_LOCK_REQUIRED;
EmergeManager *emerge = getServer(L)->getEmergeManager();
+ if (!emerge || !emerge->mgparams)
+ return 0;
Mapgen mg;
- mg.seed = emerge->mgparams->seed;
+ // Intentionally truncates to s32, see Mapgen::Mapgen()
+ mg.seed = (s32)emerge->mgparams->seed;
mg.vm = LuaVoxelManip::checkobject(L, 1)->vm;
mg.ndef = getServer(L)->getNodeDefManager();
@@ -1519,9 +1452,12 @@ int ModApiMapgen::l_generate_decorations(lua_State *L)
NO_MAP_LOCK_REQUIRED;
EmergeManager *emerge = getServer(L)->getEmergeManager();
+ if (!emerge || !emerge->mgparams)
+ return 0;
Mapgen mg;
- mg.seed = emerge->mgparams->seed;
+ // Intentionally truncates to s32, see Mapgen::Mapgen()
+ mg.seed = (s32)emerge->mgparams->seed;
mg.vm = LuaVoxelManip::checkobject(L, 1)->vm;
mg.ndef = getServer(L)->getNodeDefManager();
@@ -1734,11 +1670,10 @@ int ModApiMapgen::l_serialize_schematic(lua_State *L)
std::ostringstream os(std::ios_base::binary);
switch (schem_format) {
case SCHEM_FMT_MTS:
- schem->serializeToMts(&os, schem->m_nodenames);
+ schem->serializeToMts(&os);
break;
case SCHEM_FMT_LUA:
- schem->serializeToLua(&os, schem->m_nodenames,
- use_comments, indent_spaces);
+ schem->serializeToLua(&os, use_comments, indent_spaces);
break;
default:
return 0;
diff --git a/src/script/lua_api/l_metadata.cpp b/src/script/lua_api/l_metadata.cpp
index 21002e6a7..d00cb4daa 100644
--- a/src/script/lua_api/l_metadata.cpp
+++ b/src/script/lua_api/l_metadata.cpp
@@ -82,9 +82,10 @@ int MetaDataRef::l_get(lua_State *L)
std::string str;
if (meta->getStringToRef(name, str)) {
lua_pushlstring(L, str.c_str(), str.size());
- return 1;
+ } else {
+ lua_pushnil(L);
}
- return 0;
+ return 1;
}
// get_string(self, name)
diff --git a/src/script/lua_api/l_minimap.cpp b/src/script/lua_api/l_minimap.cpp
index 3bbb6e5e3..a135e0bd5 100644
--- a/src/script/lua_api/l_minimap.cpp
+++ b/src/script/lua_api/l_minimap.cpp
@@ -211,7 +211,7 @@ void LuaMinimap::Register(lua_State *L)
lua_pop(L, 1); // drop metatable
- luaL_openlib(L, 0, methods, 0); // fill methodtable
+ luaL_register(L, nullptr, methods); // fill methodtable
lua_pop(L, 1); // drop methodtable
}
diff --git a/src/script/lua_api/l_modchannels.cpp b/src/script/lua_api/l_modchannels.cpp
index 0485b276a..931c2749c 100644
--- a/src/script/lua_api/l_modchannels.cpp
+++ b/src/script/lua_api/l_modchannels.cpp
@@ -107,7 +107,7 @@ void ModChannelRef::Register(lua_State *L)
lua_pop(L, 1); // Drop metatable
- luaL_openlib(L, 0, methods, 0); // fill methodtable
+ luaL_register(L, nullptr, methods); // fill methodtable
lua_pop(L, 1); // Drop methodtable
}
diff --git a/src/script/lua_api/l_nodemeta.cpp b/src/script/lua_api/l_nodemeta.cpp
index 57052cb42..34760157d 100644
--- a/src/script/lua_api/l_nodemeta.cpp
+++ b/src/script/lua_api/l_nodemeta.cpp
@@ -89,7 +89,10 @@ int NodeMetaRef::l_get_inventory(lua_State *L)
NodeMetaRef *ref = checkobject(L, 1);
ref->getmeta(true); // try to ensure the metadata exists
- InvRef::createNodeMeta(L, ref->m_p);
+
+ InventoryLocation loc;
+ loc.setNodeMeta(ref->m_p);
+ InvRef::create(L, loc);
return 1;
}
@@ -234,7 +237,7 @@ void NodeMetaRef::RegisterCommon(lua_State *L)
void NodeMetaRef::Register(lua_State *L)
{
RegisterCommon(L);
- luaL_openlib(L, 0, methodsServer, 0); // fill methodtable
+ luaL_register(L, nullptr, methodsServer); // fill methodtable
lua_pop(L, 1); // drop methodtable
}
@@ -260,7 +263,7 @@ const luaL_Reg NodeMetaRef::methodsServer[] = {
void NodeMetaRef::RegisterClient(lua_State *L)
{
RegisterCommon(L);
- luaL_openlib(L, 0, methodsClient, 0); // fill methodtable
+ luaL_register(L, nullptr, methodsClient); // fill methodtable
lua_pop(L, 1); // drop methodtable
}
diff --git a/src/script/lua_api/l_nodetimer.cpp b/src/script/lua_api/l_nodetimer.cpp
index c2df52c05..8a302149f 100644
--- a/src/script/lua_api/l_nodetimer.cpp
+++ b/src/script/lua_api/l_nodetimer.cpp
@@ -122,7 +122,7 @@ void NodeTimerRef::Register(lua_State *L)
lua_pop(L, 1); // drop metatable
- luaL_openlib(L, 0, methods, 0); // fill methodtable
+ luaL_register(L, nullptr, methods); // fill methodtable
lua_pop(L, 1); // drop methodtable
// Cannot be created from Lua
diff --git a/src/script/lua_api/l_noise.cpp b/src/script/lua_api/l_noise.cpp
index e0861126a..f43ba837a 100644
--- a/src/script/lua_api/l_noise.cpp
+++ b/src/script/lua_api/l_noise.cpp
@@ -122,7 +122,7 @@ void LuaPerlinNoise::Register(lua_State *L)
lua_pop(L, 1);
- luaL_openlib(L, 0, methods, 0);
+ luaL_register(L, nullptr, methods);
lua_pop(L, 1);
lua_register(L, className, create_object);
@@ -380,7 +380,7 @@ void LuaPerlinNoiseMap::Register(lua_State *L)
lua_pop(L, 1);
- luaL_openlib(L, 0, methods, 0);
+ luaL_register(L, nullptr, methods);
lua_pop(L, 1);
lua_register(L, className, create_object);
@@ -485,7 +485,7 @@ void LuaPseudoRandom::Register(lua_State *L)
lua_pop(L, 1);
- luaL_openlib(L, 0, methods, 0);
+ luaL_register(L, nullptr, methods);
lua_pop(L, 1);
lua_register(L, className, create_object);
@@ -584,7 +584,7 @@ void LuaPcgRandom::Register(lua_State *L)
lua_pop(L, 1);
- luaL_openlib(L, 0, methods, 0);
+ luaL_register(L, nullptr, methods);
lua_pop(L, 1);
lua_register(L, className, create_object);
@@ -699,7 +699,7 @@ void LuaSecureRandom::Register(lua_State *L)
lua_pop(L, 1);
- luaL_openlib(L, 0, methods, 0);
+ luaL_register(L, nullptr, methods);
lua_pop(L, 1);
lua_register(L, className, create_object);
diff --git a/src/script/lua_api/l_object.cpp b/src/script/lua_api/l_object.cpp
index 8ae99b929..407b48db0 100644
--- a/src/script/lua_api/l_object.cpp
+++ b/src/script/lua_api/l_object.cpp
@@ -172,27 +172,11 @@ int ObjectRef::l_punch(lua_State *L)
float time_from_last_punch = readParam<float>(L, 3, 1000000.0f);
ToolCapabilities toolcap = read_tool_capabilities(L, 4);
v3f dir = readParam<v3f>(L, 5, sao->getBasePosition() - puncher->getBasePosition());
-
dir.normalize();
- u16 src_original_hp = sao->getHP();
- u16 dst_origin_hp = puncher->getHP();
- u16 wear = sao->punch(dir, &toolcap, puncher, time_from_last_punch);
+ u32 wear = sao->punch(dir, &toolcap, puncher, time_from_last_punch);
lua_pushnumber(L, wear);
- // If the punched is a player, and its HP changed
- if (src_original_hp != sao->getHP() &&
- sao->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
- getServer(L)->SendPlayerHPOrDie((PlayerSAO *)sao,
- PlayerHPChangeReason(PlayerHPChangeReason::PLAYER_PUNCH, puncher));
- }
-
- // If the puncher is a player, and its HP changed
- if (dst_origin_hp != puncher->getHP() &&
- puncher->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
- getServer(L)->SendPlayerHPOrDie((PlayerSAO *)puncher,
- PlayerHPChangeReason(PlayerHPChangeReason::PLAYER_PUNCH, sao));
- }
return 1;
}
@@ -238,8 +222,6 @@ int ObjectRef::l_set_hp(lua_State *L)
}
sao->setHP(hp, reason);
- if (sao->getType() == ACTIVEOBJECT_TYPE_PLAYER)
- getServer(L)->SendPlayerHPOrDie((PlayerSAO *)sao, reason);
if (reason.hasLuaReference())
luaL_unref(L, LUA_REGISTRYINDEX, reason.lua_reference);
return 0;
@@ -685,6 +667,7 @@ int ObjectRef::l_set_properties(lua_State *L)
return 0;
read_object_properties(L, 2, sao, prop, getServer(L)->idef());
+ prop->validate();
sao->notifyObjectPropertiesModified();
return 0;
}
@@ -752,6 +735,7 @@ int ObjectRef::l_set_nametag_attributes(lua_State *L)
std::string nametag = getstringfield_default(L, 2, "text", "");
prop->nametag = nametag;
+ prop->validate();
sao->notifyObjectPropertiesModified();
lua_pushboolean(L, true);
return 1;
@@ -1383,20 +1367,19 @@ int ObjectRef::l_get_player_control(lua_State *L)
NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
RemotePlayer *player = getplayer(ref);
- if (player == nullptr) {
- lua_pushlstring(L, "", 0);
- return 1;
- }
- const PlayerControl &control = player->getPlayerControl();
lua_newtable(L);
- lua_pushboolean(L, control.up);
+ if (player == nullptr)
+ return 1;
+
+ const PlayerControl &control = player->getPlayerControl();
+ lua_pushboolean(L, control.direction_keys & (1 << 0));
lua_setfield(L, -2, "up");
- lua_pushboolean(L, control.down);
+ lua_pushboolean(L, control.direction_keys & (1 << 1));
lua_setfield(L, -2, "down");
- lua_pushboolean(L, control.left);
+ lua_pushboolean(L, control.direction_keys & (1 << 2));
lua_setfield(L, -2, "left");
- lua_pushboolean(L, control.right);
+ lua_pushboolean(L, control.direction_keys & (1 << 3));
lua_setfield(L, -2, "right");
lua_pushboolean(L, control.jump);
lua_setfield(L, -2, "jump");
@@ -1425,11 +1408,25 @@ int ObjectRef::l_get_player_control_bits(lua_State *L)
ObjectRef *ref = checkobject(L, 1);
RemotePlayer *player = getplayer(ref);
if (player == nullptr) {
- lua_pushlstring(L, "", 0);
+ lua_pushinteger(L, 0);
return 1;
}
- lua_pushnumber(L, player->keyPressed);
+ const auto &c = player->getPlayerControl();
+
+ // This is very close to PlayerControl::getKeysPressed() but duplicated
+ // here so the encoding in the API is not inadvertedly changed.
+ u32 keypress_bits =
+ c.direction_keys |
+ ( (u32)(c.jump & 1) << 4) |
+ ( (u32)(c.aux1 & 1) << 5) |
+ ( (u32)(c.sneak & 1) << 6) |
+ ( (u32)(c.dig & 1) << 7) |
+ ( (u32)(c.place & 1) << 8) |
+ ( (u32)(c.zoom & 1) << 9)
+ ;
+
+ lua_pushinteger(L, keypress_bits);
return 1;
}
@@ -1553,12 +1550,14 @@ int ObjectRef::l_hud_change(lua_State *L)
if (elem == nullptr)
return 0;
+ HudElementStat stat;
void *value = nullptr;
- HudElementStat stat = read_hud_change(L, elem, &value);
+ bool ok = read_hud_change(L, stat, elem, &value);
- getServer(L)->hudChange(player, id, stat, value);
+ if (ok)
+ getServer(L)->hudChange(player, id, stat, value);
- lua_pushboolean(L, true);
+ lua_pushboolean(L, ok);
return 1;
}
@@ -1736,9 +1735,11 @@ int ObjectRef::l_set_sky(lua_State *L)
return 0;
SkyboxParams sky_params = player->getSkyParams();
- bool is_colorspec = is_color_table(L, 2);
- if (lua_istable(L, 2) && !is_colorspec) {
+ // reset if empty
+ if (lua_isnoneornil(L, 2) && lua_isnone(L, 3)) {
+ sky_params = SkyboxDefaults::getSkyDefaults();
+ } else if (lua_istable(L, 2) && !is_color_table(L, 2)) {
lua_getfield(L, 2, "base_color");
if (!lua_isnil(L, -1))
read_color(L, -1, &sky_params.bgcolor);
@@ -1762,17 +1763,11 @@ int ObjectRef::l_set_sky(lua_State *L)
}
lua_pop(L, 1);
- /*
- We want to avoid crashes, so we're checking even if we're not using them.
- However, we want to ensure that the skybox can be set to nil when
- using "regular" or "plain" skybox modes as textures aren't needed.
- */
-
- if (sky_params.textures.size() != 6 && sky_params.textures.size() > 0)
+ // Validate that we either have six or zero textures
+ if (sky_params.textures.size() != 6 && !sky_params.textures.empty())
throw LuaError("Skybox expects 6 textures!");
- sky_params.clouds = getboolfield_default(L, 2,
- "clouds", sky_params.clouds);
+ sky_params.clouds = getboolfield_default(L, 2, "clouds", sky_params.clouds);
lua_getfield(L, 2, "sky_color");
if (lua_istable(L, -1)) {
@@ -1820,7 +1815,7 @@ int ObjectRef::l_set_sky(lua_State *L)
sky_params.fog_tint_type = luaL_checkstring(L, -1);
lua_pop(L, 1);
- // Because we need to leave the "sky_color" table.
+ // pop "sky_color" table
lua_pop(L, 1);
}
} else {
@@ -1856,11 +1851,8 @@ int ObjectRef::l_set_sky(lua_State *L)
if (lua_istable(L, 4)) {
lua_pushnil(L);
while (lua_next(L, 4) != 0) {
- // Key at index -2, and value at index -1
- if (lua_isstring(L, -1))
- sky_params.textures.emplace_back(readParam<std::string>(L, -1));
- else
- sky_params.textures.emplace_back("");
+ // Key at index -2, and value at index -1
+ sky_params.textures.emplace_back(readParam<std::string>(L, -1));
// Remove the value, keep the key for the next iteration
lua_pop(L, 1);
}
@@ -1876,6 +1868,7 @@ int ObjectRef::l_set_sky(lua_State *L)
getServer(L)->setMoon(player, moon_params);
getServer(L)->setStars(player, star_params);
}
+
getServer(L)->setSky(player, sky_params);
lua_pushboolean(L, true);
return 1;
@@ -1951,21 +1944,20 @@ int ObjectRef::l_set_sun(lua_State *L)
if (player == nullptr)
return 0;
- luaL_checktype(L, 2, LUA_TTABLE);
SunParams sun_params = player->getSunParams();
- sun_params.visible = getboolfield_default(L, 2,
- "visible", sun_params.visible);
- sun_params.texture = getstringfield_default(L, 2,
- "texture", sun_params.texture);
- sun_params.tonemap = getstringfield_default(L, 2,
- "tonemap", sun_params.tonemap);
- sun_params.sunrise = getstringfield_default(L, 2,
- "sunrise", sun_params.sunrise);
- sun_params.sunrise_visible = getboolfield_default(L, 2,
- "sunrise_visible", sun_params.sunrise_visible);
- sun_params.scale = getfloatfield_default(L, 2,
- "scale", sun_params.scale);
+ // reset if empty
+ if (lua_isnoneornil(L, 2)) {
+ sun_params = SkyboxDefaults::getSunDefaults();
+ } else {
+ luaL_checktype(L, 2, LUA_TTABLE);
+ sun_params.visible = getboolfield_default(L, 2, "visible", sun_params.visible);
+ sun_params.texture = getstringfield_default(L, 2, "texture", sun_params.texture);
+ sun_params.tonemap = getstringfield_default(L, 2, "tonemap", sun_params.tonemap);
+ sun_params.sunrise = getstringfield_default(L, 2, "sunrise", sun_params.sunrise);
+ sun_params.sunrise_visible = getboolfield_default(L, 2, "sunrise_visible", sun_params.sunrise_visible);
+ sun_params.scale = getfloatfield_default(L, 2, "scale", sun_params.scale);
+ }
getServer(L)->setSun(player, sun_params);
lua_pushboolean(L, true);
@@ -2008,17 +2000,18 @@ int ObjectRef::l_set_moon(lua_State *L)
if (player == nullptr)
return 0;
- luaL_checktype(L, 2, LUA_TTABLE);
MoonParams moon_params = player->getMoonParams();
- moon_params.visible = getboolfield_default(L, 2,
- "visible", moon_params.visible);
- moon_params.texture = getstringfield_default(L, 2,
- "texture", moon_params.texture);
- moon_params.tonemap = getstringfield_default(L, 2,
- "tonemap", moon_params.tonemap);
- moon_params.scale = getfloatfield_default(L, 2,
- "scale", moon_params.scale);
+ // reset if empty
+ if (lua_isnoneornil(L, 2)) {
+ moon_params = SkyboxDefaults::getMoonDefaults();
+ } else {
+ luaL_checktype(L, 2, LUA_TTABLE);
+ moon_params.visible = getboolfield_default(L, 2, "visible", moon_params.visible);
+ moon_params.texture = getstringfield_default(L, 2, "texture", moon_params.texture);
+ moon_params.tonemap = getstringfield_default(L, 2, "tonemap", moon_params.tonemap);
+ moon_params.scale = getfloatfield_default(L, 2, "scale", moon_params.scale);
+ }
getServer(L)->setMoon(player, moon_params);
lua_pushboolean(L, true);
@@ -2057,21 +2050,24 @@ int ObjectRef::l_set_stars(lua_State *L)
if (player == nullptr)
return 0;
- luaL_checktype(L, 2, LUA_TTABLE);
StarParams star_params = player->getStarParams();
- star_params.visible = getboolfield_default(L, 2,
- "visible", star_params.visible);
- star_params.count = getintfield_default(L, 2,
- "count", star_params.count);
+ // reset if empty
+ if (lua_isnoneornil(L, 2)) {
+ star_params = SkyboxDefaults::getStarDefaults();
+ } else {
+ luaL_checktype(L, 2, LUA_TTABLE);
+ star_params.visible = getboolfield_default(L, 2, "visible", star_params.visible);
+ star_params.count = getintfield_default(L, 2, "count", star_params.count);
- lua_getfield(L, 2, "star_color");
- if (!lua_isnil(L, -1))
- read_color(L, -1, &star_params.starcolor);
- lua_pop(L, 1);
+ lua_getfield(L, 2, "star_color");
+ if (!lua_isnil(L, -1))
+ read_color(L, -1, &star_params.starcolor);
+ lua_pop(L, 1);
- star_params.scale = getfloatfield_default(L, 2,
- "scale", star_params.scale);
+ star_params.scale = getfloatfield_default(L, 2,
+ "scale", star_params.scale);
+ }
getServer(L)->setStars(player, star_params);
lua_pushboolean(L, true);
@@ -2110,31 +2106,36 @@ int ObjectRef::l_set_clouds(lua_State *L)
if (player == nullptr)
return 0;
- luaL_checktype(L, 2, LUA_TTABLE);
CloudParams cloud_params = player->getCloudParams();
- cloud_params.density = getfloatfield_default(L, 2, "density", cloud_params.density);
+ // reset if empty
+ if (lua_isnoneornil(L, 2)) {
+ cloud_params = SkyboxDefaults::getCloudDefaults();
+ } else {
+ luaL_checktype(L, 2, LUA_TTABLE);
+ cloud_params.density = getfloatfield_default(L, 2, "density", cloud_params.density);
- lua_getfield(L, 2, "color");
- if (!lua_isnil(L, -1))
- read_color(L, -1, &cloud_params.color_bright);
- lua_pop(L, 1);
- lua_getfield(L, 2, "ambient");
- if (!lua_isnil(L, -1))
- read_color(L, -1, &cloud_params.color_ambient);
- lua_pop(L, 1);
+ lua_getfield(L, 2, "color");
+ if (!lua_isnil(L, -1))
+ read_color(L, -1, &cloud_params.color_bright);
+ lua_pop(L, 1);
+ lua_getfield(L, 2, "ambient");
+ if (!lua_isnil(L, -1))
+ read_color(L, -1, &cloud_params.color_ambient);
+ lua_pop(L, 1);
- cloud_params.height = getfloatfield_default(L, 2, "height", cloud_params.height );
- cloud_params.thickness = getfloatfield_default(L, 2, "thickness", cloud_params.thickness);
+ cloud_params.height = getfloatfield_default(L, 2, "height", cloud_params.height);
+ cloud_params.thickness = getfloatfield_default(L, 2, "thickness", cloud_params.thickness);
- lua_getfield(L, 2, "speed");
- if (lua_istable(L, -1)) {
- v2f new_speed;
- new_speed.X = getfloatfield_default(L, -1, "x", 0);
- new_speed.Y = getfloatfield_default(L, -1, "z", 0);
- cloud_params.speed = new_speed;
+ lua_getfield(L, 2, "speed");
+ if (lua_istable(L, -1)) {
+ v2f new_speed;
+ new_speed.X = getfloatfield_default(L, -1, "x", 0);
+ new_speed.Y = getfloatfield_default(L, -1, "z", 0);
+ cloud_params.speed = new_speed;
+ }
+ lua_pop(L, 1);
}
- lua_pop(L, 1);
getServer(L)->setClouds(player, cloud_params);
lua_pushboolean(L, true);
@@ -2311,7 +2312,7 @@ void ObjectRef::Register(lua_State *L)
lua_pop(L, 1); // drop metatable
- luaL_openlib(L, 0, methods, 0); // fill methodtable
+ luaL_register(L, nullptr, methods); // fill methodtable
lua_pop(L, 1); // drop methodtable
}
diff --git a/src/script/lua_api/l_playermeta.cpp b/src/script/lua_api/l_playermeta.cpp
index 558672e38..2706c99df 100644
--- a/src/script/lua_api/l_playermeta.cpp
+++ b/src/script/lua_api/l_playermeta.cpp
@@ -97,7 +97,7 @@ void PlayerMetaRef::Register(lua_State *L)
lua_pop(L, 1); // drop metatable
- luaL_openlib(L, 0, methods, 0);
+ luaL_register(L, nullptr, methods);
lua_pop(L, 1);
// Cannot be created from Lua
diff --git a/src/script/lua_api/l_server.cpp b/src/script/lua_api/l_server.cpp
index bf5292521..88ab5e16b 100644
--- a/src/script/lua_api/l_server.cpp
+++ b/src/script/lua_api/l_server.cpp
@@ -23,6 +23,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "common/c_content.h"
#include "cpp_api/s_base.h"
#include "cpp_api/s_security.h"
+#include "scripting_server.h"
#include "server.h"
#include "environment.h"
#include "remoteplayer.h"
@@ -56,6 +57,17 @@ int ModApiServer::l_get_server_uptime(lua_State *L)
return 1;
}
+// get_server_max_lag()
+int ModApiServer::l_get_server_max_lag(lua_State *L)
+{
+ NO_MAP_LOCK_REQUIRED;
+ ServerEnvironment *s_env = dynamic_cast<ServerEnvironment *>(getEnv(L));
+ if (!s_env)
+ lua_pushnil(L);
+ else
+ lua_pushnumber(L, s_env->getMaxLagEstimate());
+ return 1;
+}
// print(text)
int ModApiServer::l_print(lua_State *L)
@@ -281,8 +293,10 @@ int ModApiServer::l_ban_player(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
- Server *server = getServer(L);
+ if (!getEnv(L))
+ throw LuaError("Can't ban player before server has started up");
+ Server *server = getServer(L);
const char *name = luaL_checkstring(L, 1);
RemotePlayer *player = server->getEnv().getPlayer(name);
if (!player) {
@@ -296,16 +310,20 @@ int ModApiServer::l_ban_player(lua_State *L)
return 1;
}
-// kick_player(name, [reason]) -> success
-int ModApiServer::l_kick_player(lua_State *L)
+// disconnect_player(name, [reason]) -> success
+int ModApiServer::l_disconnect_player(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
+
+ if (!getEnv(L))
+ throw LuaError("Can't kick player before server has started up");
+
const char *name = luaL_checkstring(L, 1);
- std::string message("Kicked");
+ std::string message;
if (lua_isstring(L, 2))
- message.append(": ").append(readParam<std::string>(L, 2));
+ message.append(readParam<std::string>(L, 2));
else
- message.append(".");
+ message.append("Disconnected.");
RemotePlayer *player = dynamic_cast<ServerEnvironment *>(getEnv(L))->getPlayer(name);
if (player == NULL) {
@@ -322,7 +340,8 @@ int ModApiServer::l_remove_player(lua_State *L)
NO_MAP_LOCK_REQUIRED;
std::string name = luaL_checkstring(L, 1);
ServerEnvironment *s_env = dynamic_cast<ServerEnvironment *>(getEnv(L));
- assert(s_env);
+ if (!s_env)
+ throw LuaError("Can't remove player before server has started up");
RemotePlayer *player = s_env->getPlayer(name.c_str());
if (!player)
@@ -452,29 +471,37 @@ int ModApiServer::l_sound_fade(lua_State *L)
}
// dynamic_add_media(filepath)
-int ModApiServer::l_dynamic_add_media_raw(lua_State *L)
+int ModApiServer::l_dynamic_add_media(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
if (!getEnv(L))
throw LuaError("Dynamic media cannot be added before server has started up");
+ Server *server = getServer(L);
- std::string filepath = readParam<std::string>(L, 1);
- CHECK_SECURE_PATH(L, filepath.c_str(), false);
+ std::string filepath;
+ std::string to_player;
+ bool ephemeral = false;
- std::vector<RemotePlayer*> sent_to;
- bool ok = getServer(L)->dynamicAddMedia(filepath, sent_to);
- if (ok) {
- // (see wrapper code in builtin)
- lua_createtable(L, sent_to.size(), 0);
- int i = 0;
- for (RemotePlayer *player : sent_to) {
- lua_pushstring(L, player->getName());
- lua_rawseti(L, -2, ++i);
- }
+ if (lua_istable(L, 1)) {
+ getstringfield(L, 1, "filepath", filepath);
+ getstringfield(L, 1, "to_player", to_player);
+ getboolfield(L, 1, "ephemeral", ephemeral);
} else {
- lua_pushboolean(L, false);
+ filepath = readParam<std::string>(L, 1);
}
+ if (filepath.empty())
+ luaL_typerror(L, 1, "non-empty string");
+ luaL_checktype(L, 2, LUA_TFUNCTION);
+
+ CHECK_SECURE_PATH(L, filepath.c_str(), false);
+
+ u32 token = server->getScriptIface()->allocateDynamicMediaCallback(L, 2);
+
+ bool ok = server->dynamicAddMedia(filepath, token, to_player, ephemeral);
+ if (!ok)
+ server->getScriptIface()->freeDynamicMediaCallback(token);
+ lua_pushboolean(L, ok);
return 1;
}
@@ -498,36 +525,12 @@ int ModApiServer::l_notify_authentication_modified(lua_State *L)
return 0;
}
-// get_last_run_mod()
-int ModApiServer::l_get_last_run_mod(lua_State *L)
-{
- NO_MAP_LOCK_REQUIRED;
- lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
- std::string current_mod = readParam<std::string>(L, -1, "");
- if (current_mod.empty()) {
- lua_pop(L, 1);
- lua_pushstring(L, getScriptApiBase(L)->getOrigin().c_str());
- }
- return 1;
-}
-
-// set_last_run_mod(modname)
-int ModApiServer::l_set_last_run_mod(lua_State *L)
-{
- NO_MAP_LOCK_REQUIRED;
-#ifdef SCRIPTAPI_DEBUG
- const char *mod = lua_tostring(L, 1);
- getScriptApiBase(L)->setOriginDirect(mod);
- //printf(">>>> last mod set from Lua: %s\n", mod);
-#endif
- return 0;
-}
-
void ModApiServer::Initialize(lua_State *L, int top)
{
API_FCT(request_shutdown);
API_FCT(get_server_status);
API_FCT(get_server_uptime);
+ API_FCT(get_server_max_lag);
API_FCT(get_worldpath);
API_FCT(is_singleplayer);
@@ -543,7 +546,7 @@ void ModApiServer::Initialize(lua_State *L, int top)
API_FCT(sound_play);
API_FCT(sound_stop);
API_FCT(sound_fade);
- API_FCT(dynamic_add_media_raw);
+ API_FCT(dynamic_add_media);
API_FCT(get_player_information);
API_FCT(get_player_privs);
@@ -551,11 +554,8 @@ void ModApiServer::Initialize(lua_State *L, int top)
API_FCT(get_ban_list);
API_FCT(get_ban_description);
API_FCT(ban_player);
- API_FCT(kick_player);
+ API_FCT(disconnect_player);
API_FCT(remove_player);
API_FCT(unban_player_or_ip);
API_FCT(notify_authentication_modified);
-
- API_FCT(get_last_run_mod);
- API_FCT(set_last_run_mod);
}
diff --git a/src/script/lua_api/l_server.h b/src/script/lua_api/l_server.h
index 2df180b17..f05c0b7c9 100644
--- a/src/script/lua_api/l_server.h
+++ b/src/script/lua_api/l_server.h
@@ -33,6 +33,9 @@ private:
// get_server_uptime()
static int l_get_server_uptime(lua_State *L);
+ // get_server_max_lag()
+ static int l_get_server_max_lag(lua_State *L);
+
// get_worldpath()
static int l_get_worldpath(lua_State *L);
@@ -71,7 +74,7 @@ private:
static int l_sound_fade(lua_State *L);
// dynamic_add_media(filepath)
- static int l_dynamic_add_media_raw(lua_State *L);
+ static int l_dynamic_add_media(lua_State *L);
// get_player_privs(name, text)
static int l_get_player_privs(lua_State *L);
@@ -94,8 +97,8 @@ private:
// unban_player_or_ip()
static int l_unban_player_or_ip(lua_State *L);
- // kick_player(name, [message]) -> success
- static int l_kick_player(lua_State *L);
+ // disconnect_player(name, [reason]) -> success
+ static int l_disconnect_player(lua_State *L);
// remove_player(name)
static int l_remove_player(lua_State *L);
@@ -103,12 +106,6 @@ private:
// notify_authentication_modified(name)
static int l_notify_authentication_modified(lua_State *L);
- // get_last_run_mod()
- static int l_get_last_run_mod(lua_State *L);
-
- // set_last_run_mod(modname)
- static int l_set_last_run_mod(lua_State *L);
-
public:
static void Initialize(lua_State *L, int top);
};
diff --git a/src/script/lua_api/l_settings.cpp b/src/script/lua_api/l_settings.cpp
index bcbaf15fa..14398dda2 100644
--- a/src/script/lua_api/l_settings.cpp
+++ b/src/script/lua_api/l_settings.cpp
@@ -20,18 +20,43 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "lua_api/l_settings.h"
#include "lua_api/l_internal.h"
#include "cpp_api/s_security.h"
+#include "threading/mutex_auto_lock.h"
#include "util/string.h" // FlagDesc
#include "settings.h"
#include "noise.h"
#include "log.h"
-#define SET_SECURITY_CHECK(L, name) \
- if (o->m_settings == g_settings && ScriptApiSecurity::isSecure(L) && \
- name.compare(0, 7, "secure.") == 0) { \
- throw LuaError("Attempt to set secure setting."); \
+/* This protects:
+ * 'secure.*' settings from being set
+ * some mapgen settings from being set
+ * (not security-criticial, just to avoid messing up user configs)
+ */
+#define CHECK_SETTING_SECURITY(L, name) \
+ if (o->m_settings == g_settings) { \
+ if (checkSettingSecurity(L, name) == -1) \
+ return 0; \
}
+static inline int checkSettingSecurity(lua_State* L, const std::string &name)
+{
+ if (ScriptApiSecurity::isSecure(L) && name.compare(0, 7, "secure.") == 0)
+ throw LuaError("Attempt to set secure setting.");
+
+ bool is_mainmenu = false;
+#ifndef SERVER
+ is_mainmenu = ModApiBase::getGuiEngine(L) != nullptr;
+#endif
+ if (!is_mainmenu && (name == "mg_name" || name == "mg_flags")) {
+ errorstream << "Tried to set global setting " << name << ", ignoring. "
+ "minetest.set_mapgen_setting() should be used instead." << std::endl;
+ infostream << script_get_backtrace(L) << std::endl;
+ return -1;
+ }
+
+ return 0;
+}
+
LuaSettings::LuaSettings(Settings *settings, const std::string &filename) :
m_settings(settings),
m_filename(filename)
@@ -129,6 +154,7 @@ int LuaSettings::l_get_np_group(lua_State *L)
return 1;
}
+// get_flags(self, key) -> table or nil
int LuaSettings::l_get_flags(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
@@ -161,7 +187,7 @@ int LuaSettings::l_set(lua_State* L)
std::string key = std::string(luaL_checkstring(L, 2));
const char* value = luaL_checkstring(L, 3);
- SET_SECURITY_CHECK(L, key);
+ CHECK_SETTING_SECURITY(L, key);
if (!o->m_settings->set(key, value))
throw LuaError("Invalid sequence found in setting parameters");
@@ -178,14 +204,14 @@ int LuaSettings::l_set_bool(lua_State* L)
std::string key = std::string(luaL_checkstring(L, 2));
bool value = readParam<bool>(L, 3);
- SET_SECURITY_CHECK(L, key);
+ CHECK_SETTING_SECURITY(L, key);
o->m_settings->setBool(key, value);
- return 1;
+ return 0;
}
-// set(self, key, value)
+// set_np_group(self, key, value)
int LuaSettings::l_set_np_group(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
@@ -195,7 +221,7 @@ int LuaSettings::l_set_np_group(lua_State *L)
NoiseParams value;
read_noiseparams(L, 3, &value);
- SET_SECURITY_CHECK(L, key);
+ CHECK_SETTING_SECURITY(L, key);
o->m_settings->setNoiseParams(key, value);
@@ -210,7 +236,7 @@ int LuaSettings::l_remove(lua_State* L)
std::string key = std::string(luaL_checkstring(L, 2));
- SET_SECURITY_CHECK(L, key);
+ CHECK_SETTING_SECURITY(L, key);
bool success = o->m_settings->remove(key);
lua_pushboolean(L, success);
@@ -253,20 +279,36 @@ int LuaSettings::l_write(lua_State* L)
return 1;
}
-// to_table(self) -> {[key1]=value1,...}
-int LuaSettings::l_to_table(lua_State* L)
+static void push_settings_table(lua_State *L, const Settings *settings)
{
- NO_MAP_LOCK_REQUIRED;
- LuaSettings* o = checkobject(L, 1);
-
- std::vector<std::string> keys = o->m_settings->getNames();
-
+ std::vector<std::string> keys = settings->getNames();
lua_newtable(L);
for (const std::string &key : keys) {
- lua_pushstring(L, o->m_settings->get(key).c_str());
+ std::string value;
+ Settings *group = nullptr;
+
+ if (settings->getNoEx(key, value)) {
+ lua_pushstring(L, value.c_str());
+ } else if (settings->getGroupNoEx(key, group)) {
+ // Recursively push tables
+ push_settings_table(L, group);
+ } else {
+ // Impossible case (multithreading) due to MutexAutoLock
+ continue;
+ }
+
lua_setfield(L, -2, key.c_str());
}
+}
+
+// to_table(self) -> {[key1]=value1,...}
+int LuaSettings::l_to_table(lua_State* L)
+{
+ NO_MAP_LOCK_REQUIRED;
+ LuaSettings* o = checkobject(L, 1);
+ MutexAutoLock(o->m_settings->m_mutex);
+ push_settings_table(L, o->m_settings);
return 1;
}
@@ -292,7 +334,7 @@ void LuaSettings::Register(lua_State* L)
lua_pop(L, 1); // drop metatable
- luaL_openlib(L, 0, methods, 0); // fill methodtable
+ luaL_register(L, nullptr, methods); // fill methodtable
lua_pop(L, 1); // drop methodtable
// Can be created from Lua (Settings(filename))
diff --git a/src/script/lua_api/l_storage.cpp b/src/script/lua_api/l_storage.cpp
index cba34fb63..b8f4347a8 100644
--- a/src/script/lua_api/l_storage.cpp
+++ b/src/script/lua_api/l_storage.cpp
@@ -32,19 +32,23 @@ int ModApiStorage::l_get_mod_storage(lua_State *L)
std::string mod_name = readParam<std::string>(L, -1);
- ModMetadata *store = new ModMetadata(mod_name);
+ ModMetadata *store = nullptr;
+
if (IGameDef *gamedef = getGameDef(L)) {
- store->load(gamedef->getModStoragePath());
- gamedef->registerModStorage(store);
+ store = new ModMetadata(mod_name, gamedef->getModStorageDatabase());
+ if (gamedef->registerModStorage(store)) {
+ StorageRef::create(L, store);
+ int object = lua_gettop(L);
+ lua_pushvalue(L, object);
+ return 1;
+ }
} else {
- delete store;
assert(false); // this should not happen
}
- StorageRef::create(L, store);
- int object = lua_gettop(L);
+ delete store;
- lua_pushvalue(L, object);
+ lua_pushnil(L);
return 1;
}
@@ -110,7 +114,7 @@ void StorageRef::Register(lua_State *L)
lua_pop(L, 1); // drop metatable
- luaL_openlib(L, 0, methods, 0); // fill methodtable
+ luaL_register(L, nullptr, methods); // fill methodtable
lua_pop(L, 1); // drop methodtable
}
diff --git a/src/script/lua_api/l_util.cpp b/src/script/lua_api/l_util.cpp
index 203a0dd28..b04f26fda 100644
--- a/src/script/lua_api/l_util.cpp
+++ b/src/script/lua_api/l_util.cpp
@@ -17,6 +17,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
+#include "irrlichttypes_extrabloated.h"
#include "lua_api/l_util.h"
#include "lua_api/l_internal.h"
#include "lua_api/l_settings.h"
@@ -39,8 +40,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "version.h"
#include "util/hex.h"
#include "util/sha1.h"
-#include <algorithm>
-
+#include "util/png.h"
+#include <cstdio>
// log([level,] text)
// Writes a line to the logger.
@@ -158,28 +159,33 @@ int ModApiUtil::l_write_json(lua_State *L)
return 1;
}
-// get_dig_params(groups, tool_capabilities)
+// get_dig_params(groups, tool_capabilities[, wear])
int ModApiUtil::l_get_dig_params(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
ItemGroupList groups;
read_groups(L, 1, groups);
ToolCapabilities tp = read_tool_capabilities(L, 2);
- push_dig_params(L, getDigParams(groups, &tp));
+ if (lua_isnoneornil(L, 3)) {
+ push_dig_params(L, getDigParams(groups, &tp));
+ } else {
+ u16 wear = readParam<int>(L, 3);
+ push_dig_params(L, getDigParams(groups, &tp, wear));
+ }
return 1;
}
-// get_hit_params(groups, tool_capabilities[, time_from_last_punch])
+// get_hit_params(groups, tool_capabilities[, time_from_last_punch, [, wear]])
int ModApiUtil::l_get_hit_params(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
std::unordered_map<std::string, int> groups;
read_groups(L, 1, groups);
ToolCapabilities tp = read_tool_capabilities(L, 2);
- if(lua_isnoneornil(L, 3))
- push_hit_params(L, getHitParams(groups, &tp));
- else
- push_hit_params(L, getHitParams(groups, &tp, readParam<float>(L, 3)));
+ float time_from_last_punch = readParam<float>(L, 3, 1000000);
+ int wear = readParam<int>(L, 4, 0);
+ push_hit_params(L, getHitParams(groups, &tp,
+ time_from_last_punch, wear));
return 1;
}
@@ -270,11 +276,11 @@ int ModApiUtil::l_compress(lua_State *L)
const char *data = luaL_checklstring(L, 1, &size);
int level = -1;
- if (!lua_isnone(L, 3) && !lua_isnil(L, 3))
- level = readParam<float>(L, 3);
+ if (!lua_isnoneornil(L, 3))
+ level = readParam<int>(L, 3);
- std::ostringstream os;
- compressZlib(std::string(data, size), os, level);
+ std::ostringstream os(std::ios_base::binary);
+ compressZlib(reinterpret_cast<const u8 *>(data), size, os, level);
std::string out = os.str();
@@ -290,8 +296,8 @@ int ModApiUtil::l_decompress(lua_State *L)
size_t size;
const char *data = luaL_checklstring(L, 1, &size);
- std::istringstream is(std::string(data, size));
- std::ostringstream os;
+ std::istringstream is(std::string(data, size), std::ios_base::binary);
+ std::ostringstream os(std::ios_base::binary);
decompressZlib(is, os);
std::string out = os.str();
@@ -342,6 +348,49 @@ int ModApiUtil::l_mkdir(lua_State *L)
return 1;
}
+// rmdir(path, recursive)
+int ModApiUtil::l_rmdir(lua_State *L)
+{
+ NO_MAP_LOCK_REQUIRED;
+ const char *path = luaL_checkstring(L, 1);
+ CHECK_SECURE_PATH(L, path, true);
+
+ bool recursive = readParam<bool>(L, 2, false);
+
+ if (recursive)
+ lua_pushboolean(L, fs::RecursiveDelete(path));
+ else
+ lua_pushboolean(L, fs::DeleteSingleFileOrEmptyDirectory(path));
+
+ return 1;
+}
+
+// cpdir(source, destination)
+int ModApiUtil::l_cpdir(lua_State *L)
+{
+ NO_MAP_LOCK_REQUIRED;
+ const char *source = luaL_checkstring(L, 1);
+ const char *destination = luaL_checkstring(L, 2);
+ CHECK_SECURE_PATH(L, source, false);
+ CHECK_SECURE_PATH(L, destination, true);
+
+ lua_pushboolean(L, fs::CopyDir(source, destination));
+ return 1;
+}
+
+// mpdir(source, destination)
+int ModApiUtil::l_mvdir(lua_State *L)
+{
+ NO_MAP_LOCK_REQUIRED;
+ const char *source = luaL_checkstring(L, 1);
+ const char *destination = luaL_checkstring(L, 2);
+ CHECK_SECURE_PATH(L, source, true);
+ CHECK_SECURE_PATH(L, destination, true);
+
+ lua_pushboolean(L, fs::MoveDir(source, destination));
+ return 1;
+}
+
// get_dir_list(path, is_dir)
int ModApiUtil::l_get_dir_list(lua_State *L)
{
@@ -394,36 +443,7 @@ int ModApiUtil::l_request_insecure_environment(lua_State *L)
return 1;
}
- // We have to make sure that this function is being called directly by
- // a mod, otherwise a malicious mod could override this function and
- // steal its return value.
- lua_Debug info;
- // Make sure there's only one item below this function on the stack...
- if (lua_getstack(L, 2, &info)) {
- return 0;
- }
- FATAL_ERROR_IF(!lua_getstack(L, 1, &info), "lua_getstack() failed");
- FATAL_ERROR_IF(!lua_getinfo(L, "S", &info), "lua_getinfo() failed");
- // ...and that that item is the main file scope.
- if (strcmp(info.what, "main") != 0) {
- return 0;
- }
-
- // Get mod name
- lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
- if (!lua_isstring(L, -1)) {
- return 0;
- }
-
- // Check secure.trusted_mods
- std::string mod_name = readParam<std::string>(L, -1);
- std::string trusted_mods = g_settings->get("secure.trusted_mods");
- trusted_mods.erase(std::remove_if(trusted_mods.begin(),
- trusted_mods.end(), static_cast<int(*)(int)>(&std::isspace)),
- trusted_mods.end());
- std::vector<std::string> mod_list = str_split(trusted_mods, ',');
- if (std::find(mod_list.begin(), mod_list.end(), mod_name) ==
- mod_list.end()) {
+ if (!ScriptApiSecurity::checkWhitelisted(L, "secure.trusted_mods")) {
return 0;
}
@@ -479,6 +499,84 @@ int ModApiUtil::l_sha1(lua_State *L)
return 1;
}
+// colorspec_to_colorstring(colorspec)
+int ModApiUtil::l_colorspec_to_colorstring(lua_State *L)
+{
+ NO_MAP_LOCK_REQUIRED;
+
+ video::SColor color(0);
+ if (read_color(L, 1, &color)) {
+ char colorstring[10];
+ snprintf(colorstring, 10, "#%02X%02X%02X%02X",
+ color.getRed(), color.getGreen(), color.getBlue(), color.getAlpha());
+ lua_pushstring(L, colorstring);
+ return 1;
+ }
+
+ return 0;
+}
+
+// colorspec_to_bytes(colorspec)
+int ModApiUtil::l_colorspec_to_bytes(lua_State *L)
+{
+ NO_MAP_LOCK_REQUIRED;
+
+ video::SColor color(0);
+ if (read_color(L, 1, &color)) {
+ u8 colorbytes[4] = {
+ (u8) color.getRed(),
+ (u8) color.getGreen(),
+ (u8) color.getBlue(),
+ (u8) color.getAlpha(),
+ };
+ lua_pushlstring(L, (const char*) colorbytes, 4);
+ return 1;
+ }
+
+ return 0;
+}
+
+// encode_png(w, h, data, level)
+int ModApiUtil::l_encode_png(lua_State *L)
+{
+ NO_MAP_LOCK_REQUIRED;
+
+ // The args are already pre-validated on the lua side.
+ u32 width = readParam<int>(L, 1);
+ u32 height = readParam<int>(L, 2);
+ const char *data = luaL_checklstring(L, 3, NULL);
+ s32 compression = readParam<int>(L, 4);
+
+ std::string out = encodePNG((const u8*)data, width, height, compression);
+
+ lua_pushlstring(L, out.data(), out.size());
+ return 1;
+}
+
+// get_last_run_mod()
+int ModApiUtil::l_get_last_run_mod(lua_State *L)
+{
+ NO_MAP_LOCK_REQUIRED;
+
+ lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
+ std::string current_mod = readParam<std::string>(L, -1, "");
+ if (current_mod.empty()) {
+ lua_pop(L, 1);
+ lua_pushstring(L, getScriptApiBase(L)->getOrigin().c_str());
+ }
+ return 1;
+}
+
+// set_last_run_mod(modname)
+int ModApiUtil::l_set_last_run_mod(lua_State *L)
+{
+ NO_MAP_LOCK_REQUIRED;
+
+ const char *mod = luaL_checkstring(L, 1);
+ getScriptApiBase(L)->setOriginDirect(mod);
+ return 0;
+}
+
void ModApiUtil::Initialize(lua_State *L, int top)
{
API_FCT(log);
@@ -503,6 +601,9 @@ void ModApiUtil::Initialize(lua_State *L, int top)
API_FCT(decompress);
API_FCT(mkdir);
+ API_FCT(rmdir);
+ API_FCT(cpdir);
+ API_FCT(mvdir);
API_FCT(get_dir_list);
API_FCT(safe_file_write);
@@ -513,6 +614,13 @@ void ModApiUtil::Initialize(lua_State *L, int top)
API_FCT(get_version);
API_FCT(sha1);
+ API_FCT(colorspec_to_colorstring);
+ API_FCT(colorspec_to_bytes);
+
+ API_FCT(encode_png);
+
+ API_FCT(get_last_run_mod);
+ API_FCT(set_last_run_mod);
LuaSettings::create(L, g_settings, g_settings_path);
lua_setfield(L, top, "settings");
@@ -537,6 +645,8 @@ void ModApiUtil::InitializeClient(lua_State *L, int top)
API_FCT(get_version);
API_FCT(sha1);
+ API_FCT(colorspec_to_colorstring);
+ API_FCT(colorspec_to_bytes);
}
void ModApiUtil::InitializeAsync(lua_State *L, int top)
@@ -557,6 +667,9 @@ void ModApiUtil::InitializeAsync(lua_State *L, int top)
API_FCT(decompress);
API_FCT(mkdir);
+ API_FCT(rmdir);
+ API_FCT(cpdir);
+ API_FCT(mvdir);
API_FCT(get_dir_list);
API_FCT(encode_base64);
@@ -564,8 +677,12 @@ void ModApiUtil::InitializeAsync(lua_State *L, int top)
API_FCT(get_version);
API_FCT(sha1);
+ API_FCT(colorspec_to_colorstring);
+ API_FCT(colorspec_to_bytes);
+
+ API_FCT(get_last_run_mod);
+ API_FCT(set_last_run_mod);
LuaSettings::create(L, g_settings, g_settings_path);
lua_setfield(L, top, "settings");
}
-
diff --git a/src/script/lua_api/l_util.h b/src/script/lua_api/l_util.h
index dbdd62b99..fcf8a1057 100644
--- a/src/script/lua_api/l_util.h
+++ b/src/script/lua_api/l_util.h
@@ -50,10 +50,10 @@ private:
// write_json(data[, styled])
static int l_write_json(lua_State *L);
- // get_dig_params(groups, tool_capabilities[, time_from_last_punch])
+ // get_dig_params(groups, tool_capabilities[, wear])
static int l_get_dig_params(lua_State *L);
- // get_hit_params(groups, tool_capabilities[, time_from_last_punch])
+ // get_hit_params(groups, tool_capabilities[, time_from_last_punch[, wear]])
static int l_get_hit_params(lua_State *L);
// check_password_entry(name, entry, password)
@@ -80,6 +80,15 @@ private:
// mkdir(path)
static int l_mkdir(lua_State *L);
+ // rmdir(path, recursive)
+ static int l_rmdir(lua_State *L);
+
+ // cpdir(source, destination, remove_source)
+ static int l_cpdir(lua_State *L);
+
+ // mvdir(source, destination)
+ static int l_mvdir(lua_State *L);
+
// get_dir_list(path, is_dir)
static int l_get_dir_list(lua_State *L);
@@ -101,6 +110,21 @@ private:
// sha1(string, raw)
static int l_sha1(lua_State *L);
+ // colorspec_to_colorstring(colorspec)
+ static int l_colorspec_to_colorstring(lua_State *L);
+
+ // colorspec_to_bytes(colorspec)
+ static int l_colorspec_to_bytes(lua_State *L);
+
+ // encode_png(w, h, data, level)
+ static int l_encode_png(lua_State *L);
+
+ // get_last_run_mod()
+ static int l_get_last_run_mod(lua_State *L);
+
+ // set_last_run_mod(modname)
+ static int l_set_last_run_mod(lua_State *L);
+
public:
static void Initialize(lua_State *L, int top);
static void InitializeAsync(lua_State *L, int top);
diff --git a/src/script/lua_api/l_vmanip.cpp b/src/script/lua_api/l_vmanip.cpp
index b99b1d98c..e040e545b 100644
--- a/src/script/lua_api/l_vmanip.cpp
+++ b/src/script/lua_api/l_vmanip.cpp
@@ -450,7 +450,7 @@ void LuaVoxelManip::Register(lua_State *L)
lua_pop(L, 1); // drop metatable
- luaL_openlib(L, 0, methods, 0); // fill methodtable
+ luaL_register(L, nullptr, methods); // fill methodtable
lua_pop(L, 1); // drop methodtable
// Can be created from Lua (VoxelManip())
diff --git a/src/script/scripting_mainmenu.cpp b/src/script/scripting_mainmenu.cpp
index b102a66a1..2a0cadb23 100644
--- a/src/script/scripting_mainmenu.cpp
+++ b/src/script/scripting_mainmenu.cpp
@@ -92,9 +92,9 @@ void MainMenuScripting::step()
}
/******************************************************************************/
-unsigned int MainMenuScripting::queueAsync(const std::string &serialized_func,
- const std::string &serialized_param)
+u32 MainMenuScripting::queueAsync(std::string &&serialized_func,
+ std::string &&serialized_param)
{
- return asyncEngine.queueAsyncJob(serialized_func, serialized_param);
+ return asyncEngine.queueAsyncJob(std::move(serialized_func), std::move(serialized_param));
}
diff --git a/src/script/scripting_mainmenu.h b/src/script/scripting_mainmenu.h
index 9e23bdc1b..3c329654a 100644
--- a/src/script/scripting_mainmenu.h
+++ b/src/script/scripting_mainmenu.h
@@ -38,8 +38,9 @@ public:
void step();
// Pass async events from engine to async threads
- unsigned int queueAsync(const std::string &serialized_func,
- const std::string &serialized_params);
+ u32 queueAsync(std::string &&serialized_func,
+ std::string &&serialized_param);
+
private:
void initializeModApi(lua_State *L, int top);
static void registerLuaClasses(lua_State *L, int top);
diff --git a/src/serialization.cpp b/src/serialization.cpp
index 310604f54..d4d7b5f6e 100644
--- a/src/serialization.cpp
+++ b/src/serialization.cpp
@@ -21,7 +21,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "util/serialize.h"
-#include "zlib.h"
+#include <zlib.h>
+#include <zstd.h>
/* report a zlib or i/o error */
void zerr(int ret)
@@ -197,27 +198,158 @@ void decompressZlib(std::istream &is, std::ostream &os, size_t limit)
inflateEnd(&z);
}
-void compress(const SharedBuffer<u8> &data, std::ostream &os, u8 version)
+struct ZSTD_Deleter {
+ void operator() (ZSTD_CStream* cstream) {
+ ZSTD_freeCStream(cstream);
+ }
+
+ void operator() (ZSTD_DStream* dstream) {
+ ZSTD_freeDStream(dstream);
+ }
+};
+
+#if defined(__MINGW32__) && !defined(__MINGW64__)
+/*
+ * This is exactly as dumb as it looks.
+ * Yes, this is a memory leak. No, we don't have better solution right now.
+ */
+template<typename T> class leaky_ptr
+{
+ T *value;
+public:
+ leaky_ptr(T *value) : value(value) {};
+ T *get() { return value; }
+};
+#endif
+
+void compressZstd(const u8 *data, size_t data_size, std::ostream &os, int level)
+{
+#if defined(__MINGW32__) && !defined(__MINGW64__)
+ // leaks one context per thread but doesn't crash :shrug:
+ thread_local leaky_ptr<ZSTD_CStream> stream(ZSTD_createCStream());
+#else
+ // reusing the context is recommended for performance
+ // it will destroyed when the thread ends
+ thread_local std::unique_ptr<ZSTD_CStream, ZSTD_Deleter> stream(ZSTD_createCStream());
+#endif
+
+
+ ZSTD_initCStream(stream.get(), level);
+
+ const size_t bufsize = 16384;
+ char output_buffer[bufsize];
+
+ ZSTD_inBuffer input = { data, data_size, 0 };
+ ZSTD_outBuffer output = { output_buffer, bufsize, 0 };
+
+ while (input.pos < input.size) {
+ size_t ret = ZSTD_compressStream(stream.get(), &output, &input);
+ if (ZSTD_isError(ret)) {
+ dstream << ZSTD_getErrorName(ret) << std::endl;
+ throw SerializationError("compressZstd: failed");
+ }
+ if (output.pos) {
+ os.write(output_buffer, output.pos);
+ output.pos = 0;
+ }
+ }
+
+ size_t ret;
+ do {
+ ret = ZSTD_endStream(stream.get(), &output);
+ if (ZSTD_isError(ret)) {
+ dstream << ZSTD_getErrorName(ret) << std::endl;
+ throw SerializationError("compressZstd: failed");
+ }
+ if (output.pos) {
+ os.write(output_buffer, output.pos);
+ output.pos = 0;
+ }
+ } while (ret != 0);
+
+}
+
+void compressZstd(const std::string &data, std::ostream &os, int level)
+{
+ compressZstd((u8*)data.c_str(), data.size(), os, level);
+}
+
+void decompressZstd(std::istream &is, std::ostream &os)
+{
+#if defined(__MINGW32__) && !defined(__MINGW64__)
+ // leaks one context per thread but doesn't crash :shrug:
+ thread_local leaky_ptr<ZSTD_DStream> stream(ZSTD_createDStream());
+#else
+ // reusing the context is recommended for performance
+ // it will destroyed when the thread ends
+ thread_local std::unique_ptr<ZSTD_DStream, ZSTD_Deleter> stream(ZSTD_createDStream());
+#endif
+
+ ZSTD_initDStream(stream.get());
+
+ const size_t bufsize = 16384;
+ char output_buffer[bufsize];
+ char input_buffer[bufsize];
+
+ ZSTD_outBuffer output = { output_buffer, bufsize, 0 };
+ ZSTD_inBuffer input = { input_buffer, 0, 0 };
+ size_t ret;
+ do
+ {
+ if (input.size == input.pos) {
+ is.read(input_buffer, bufsize);
+ input.size = is.gcount();
+ input.pos = 0;
+ }
+
+ ret = ZSTD_decompressStream(stream.get(), &output, &input);
+ if (ZSTD_isError(ret)) {
+ dstream << ZSTD_getErrorName(ret) << std::endl;
+ throw SerializationError("decompressZstd: failed");
+ }
+ if (output.pos) {
+ os.write(output_buffer, output.pos);
+ output.pos = 0;
+ }
+ } while (ret != 0);
+
+ // Unget all the data that ZSTD_decompressStream didn't take
+ is.clear(); // Just in case EOF is set
+ for (u32 i = 0; i < input.size - input.pos; i++) {
+ is.unget();
+ if (is.fail() || is.bad())
+ throw SerializationError("decompressZstd: unget failed");
+ }
+}
+
+void compress(u8 *data, u32 size, std::ostream &os, u8 version, int level)
{
+ if(version >= 29)
+ {
+ // map the zlib levels [0,9] to [1,10]. -1 becomes 0 which indicates the default (currently 3)
+ compressZstd(data, size, os, level + 1);
+ return;
+ }
+
if(version >= 11)
{
- compressZlib(*data ,data.getSize(), os);
+ compressZlib(data, size, os, level);
return;
}
- if(data.getSize() == 0)
+ if(size == 0)
return;
// Write length (u32)
u8 tmp[4];
- writeU32(tmp, data.getSize());
+ writeU32(tmp, size);
os.write((char*)tmp, 4);
// We will be writing 8-bit pairs of more_count and byte
u8 more_count = 0;
u8 current_byte = data[0];
- for(u32 i=1; i<data.getSize(); i++)
+ for(u32 i=1; i<size; i++)
{
if(
data[i] != current_byte
@@ -240,8 +372,24 @@ void compress(const SharedBuffer<u8> &data, std::ostream &os, u8 version)
os.write((char*)&current_byte, 1);
}
+void compress(const SharedBuffer<u8> &data, std::ostream &os, u8 version, int level)
+{
+ compress(*data, data.getSize(), os, version, level);
+}
+
+void compress(const std::string &data, std::ostream &os, u8 version, int level)
+{
+ compress((u8*)data.c_str(), data.size(), os, version, level);
+}
+
void decompress(std::istream &is, std::ostream &os, u8 version)
{
+ if(version >= 29)
+ {
+ decompressZstd(is, os);
+ return;
+ }
+
if(version >= 11)
{
decompressZlib(is, os);
diff --git a/src/serialization.h b/src/serialization.h
index f399983c4..e83a8c179 100644
--- a/src/serialization.h
+++ b/src/serialization.h
@@ -63,13 +63,14 @@ with this program; if not, write to the Free Software Foundation, Inc.,
26: Never written; read the same as 25
27: Added light spreading flags to blocks
28: Added "private" flag to NodeMetadata
+ 29: Switched compression to zstd, a bit of reorganization
*/
// This represents an uninitialized or invalid format
#define SER_FMT_VER_INVALID 255
// Highest supported serialization version
-#define SER_FMT_VER_HIGHEST_READ 28
+#define SER_FMT_VER_HIGHEST_READ 29
// Saved on disk version
-#define SER_FMT_VER_HIGHEST_WRITE 28
+#define SER_FMT_VER_HIGHEST_WRITE 29
// Lowest supported serialization version
#define SER_FMT_VER_LOWEST_READ 0
// Lowest serialization version for writing
@@ -89,7 +90,12 @@ void compressZlib(const u8 *data, size_t data_size, std::ostream &os, int level
void compressZlib(const std::string &data, std::ostream &os, int level = -1);
void decompressZlib(std::istream &is, std::ostream &os, size_t limit = 0);
+void compressZstd(const u8 *data, size_t data_size, std::ostream &os, int level = 0);
+void compressZstd(const std::string &data, std::ostream &os, int level = 0);
+void decompressZstd(std::istream &is, std::ostream &os);
+
// These choose between zlib and a self-made one according to version
-void compress(const SharedBuffer<u8> &data, std::ostream &os, u8 version);
-//void compress(const std::string &data, std::ostream &os, u8 version);
+void compress(const SharedBuffer<u8> &data, std::ostream &os, u8 version, int level = -1);
+void compress(const std::string &data, std::ostream &os, u8 version, int level = -1);
+void compress(u8 *data, u32 size, std::ostream &os, u8 version, int level = -1);
void decompress(std::istream &is, std::ostream &os, u8 version);
diff --git a/src/server.cpp b/src/server.cpp
index 81cdd1f8d..23a7dc5a0 100644
--- a/src/server.cpp
+++ b/src/server.cpp
@@ -66,6 +66,10 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "server/player_sao.h"
#include "server/serverinventorymgr.h"
#include "translation.h"
+#include "database/database-sqlite3.h"
+#include "database/database-files.h"
+#include "database/database-dummy.h"
+#include "gameparams.h"
class ClientNotFoundException : public BaseException
{
@@ -103,7 +107,13 @@ void *ServerThread::run()
* doesn't busy wait) and will process any remaining packets.
*/
- m_server->AsyncRunStep(true);
+ try {
+ m_server->AsyncRunStep(true);
+ } catch (con::ConnectionBindFailed &e) {
+ m_server->setAsyncFatalError(e.what());
+ } catch (LuaError &e) {
+ m_server->setAsyncFatalError(e);
+ }
while (!stopRequested()) {
try {
@@ -117,8 +127,7 @@ void *ServerThread::run()
} catch (con::ConnectionBindFailed &e) {
m_server->setAsyncFatalError(e.what());
} catch (LuaError &e) {
- m_server->setAsyncFatalError(
- "ServerThread::run Lua: " + std::string(e.what()));
+ m_server->setAsyncFatalError(e);
}
}
@@ -339,10 +348,15 @@ Server::~Server()
delete m_thread;
}
+ // Write any changes before deletion.
+ if (m_mod_storage_database)
+ m_mod_storage_database->endSave();
+
// Delete things in the reverse order of creation
delete m_emerge;
delete m_env;
delete m_rollback;
+ delete m_mod_storage_database;
delete m_banmanager;
delete m_itemdef;
delete m_nodedef;
@@ -388,6 +402,10 @@ void Server::init()
std::string ban_path = m_path_world + DIR_DELIM "ipban.txt";
m_banmanager = new BanManager(ban_path);
+ // Create mod storage database and begin a save for later
+ m_mod_storage_database = openModStorageDatabase(m_path_world);
+ m_mod_storage_database->beginSave();
+
m_modmgr = std::unique_ptr<ServerModManager>(new ServerModManager(m_path_world));
std::vector<ModSpec> unsatisfied_mods = m_modmgr->getUnsatisfiedMods();
// complain about mods with unsatisfied dependencies
@@ -494,16 +512,17 @@ void Server::start()
// ASCII art for the win!
std::cerr
- << " .__ __ __ " << std::endl
- << " _____ |__| ____ _____/ |_ ____ _______/ |_ " << std::endl
- << " / \\| |/ \\_/ __ \\ __\\/ __ \\ / ___/\\ __\\" << std::endl
- << "| Y Y \\ | | \\ ___/| | \\ ___/ \\___ \\ | | " << std::endl
- << "|__|_| /__|___| /\\___ >__| \\___ >____ > |__| " << std::endl
- << " \\/ \\/ \\/ \\/ \\/ " << std::endl;
+ << " __. __. __. " << std::endl
+ << " _____ |__| ____ _____ / |_ _____ _____ / |_ " << std::endl
+ << " / \\| |/ \\ / __ \\ _\\/ __ \\/ __> _\\" << std::endl
+ << "| Y Y \\ | | \\ ___/| | | ___/\\___ \\| | " << std::endl
+ << "|__|_| / |___| /\\______> | \\______>_____/| | " << std::endl
+ << " \\/ \\/ \\/ \\/ \\/ " << std::endl;
actionstream << "World at [" << m_path_world << "]" << std::endl;
actionstream << "Server for gameid=\"" << m_gamespec.id
- << "\" listening on " << m_bind_addr.serializeString() << ":"
- << m_bind_addr.getPort() << "." << std::endl;
+ << "\" listening on ";
+ m_bind_addr.print(&actionstream);
+ actionstream << "." << std::endl;
}
void Server::stop()
@@ -512,9 +531,7 @@ void Server::stop()
// Stop threads (set run=false first so both start stopping)
m_thread->stop();
- //m_emergethread.setRun(false);
m_thread->wait();
- //m_emergethread.stop();
infostream<<"Server: Threads stopped"<<std::endl;
}
@@ -665,6 +682,17 @@ void Server::AsyncRunStep(bool initial_step)
} else {
m_lag_gauge->increment(dtime/100);
}
+
+ {
+ float &counter = m_step_pending_dyn_media_timer;
+ counter += dtime;
+ if (counter >= 5.0f) {
+ stepPendingDynMediaCallbacks(counter);
+ counter = 0;
+ }
+ }
+
+
#if USE_CURL
// send masterserver announce
{
@@ -718,20 +746,12 @@ void Server::AsyncRunStep(bool initial_step)
}
m_clients.unlock();
- // Save mod storages if modified
+ // Write changes to the mod storage
m_mod_storage_save_timer -= dtime;
if (m_mod_storage_save_timer <= 0.0f) {
m_mod_storage_save_timer = g_settings->getFloat("server_map_save_interval");
- int n = 0;
- for (std::unordered_map<std::string, ModMetadata *>::const_iterator
- it = m_mod_storages.begin(); it != m_mod_storages.end(); ++it) {
- if (it->second->isModified()) {
- it->second->save(getModStoragePath());
- n++;
- }
- }
- if (n > 0)
- infostream << "Saved " << n << " modified mod storages." << std::endl;
+ m_mod_storage_database->endSave();
+ m_mod_storage_database->beginSave();
}
}
@@ -938,14 +958,14 @@ void Server::AsyncRunStep(bool initial_step)
}
/*
- Trigger emergethread (it somehow gets to a non-triggered but
- bysy state sometimes)
+ Trigger emerge thread
+ Doing this every 2s is left over from old code, unclear if this is still needed.
*/
{
float &counter = m_emergethread_trigger_timer;
- counter += dtime;
- if (counter >= 2.0) {
- counter = 0.0;
+ counter -= dtime;
+ if (counter <= 0.0f) {
+ counter = 2.0f;
m_emerge->startThreads();
}
@@ -1075,12 +1095,12 @@ PlayerSAO* Server::StageTwoClientInit(session_t peer_id)
// Send inventory
SendInventory(playersao, false);
- // Send HP or death screen
+ // Send HP
+ SendPlayerHP(playersao);
+
+ // Send death screen
if (playersao->isDead())
SendDeathscreen(peer_id, false, v3f(0,0,0));
- else
- SendPlayerHPOrDie(playersao,
- PlayerHPChangeReason(PlayerHPChangeReason::SET_HP));
// Send Breath
SendPlayerBreath(playersao);
@@ -1346,18 +1366,21 @@ void Server::SendMovement(session_t peer_id)
Send(&pkt);
}
-void Server::SendPlayerHPOrDie(PlayerSAO *playersao, const PlayerHPChangeReason &reason)
+void Server::HandlePlayerHPChange(PlayerSAO *playersao, const PlayerHPChangeReason &reason)
{
- if (playersao->isImmortal())
- return;
+ m_script->player_event(playersao, "health_changed");
+ SendPlayerHP(playersao);
+
+ // Send to other clients
+ playersao->sendPunchCommand();
- session_t peer_id = playersao->getPeerID();
- bool is_alive = !playersao->isDead();
+ if (playersao->isDead())
+ HandlePlayerDeath(playersao, reason);
+}
- if (is_alive)
- SendPlayerHP(peer_id);
- else
- DiePlayer(peer_id, reason);
+void Server::SendPlayerHP(PlayerSAO *playersao)
+{
+ SendHP(playersao->getPeerID(), playersao->getHP());
}
void Server::SendHP(session_t peer_id, u16 hp)
@@ -1638,7 +1661,7 @@ void Server::SendHUDAdd(session_t peer_id, u32 id, HudElement *form)
pkt << id << (u8) form->type << form->pos << form->name << form->scale
<< form->text << form->number << form->item << form->dir
<< form->align << form->offset << form->world_pos << form->size
- << form->z_index << form->text2;
+ << form->z_index << form->text2 << form->style;
Send(&pkt);
}
@@ -1673,10 +1696,7 @@ void Server::SendHUDChange(session_t peer_id, u32 id, HudElementStat stat, void
case HUD_STAT_SIZE:
pkt << *(v2s32 *) value;
break;
- case HUD_STAT_NUMBER:
- case HUD_STAT_ITEM:
- case HUD_STAT_DIR:
- default:
+ default: // all other types
pkt << *(u32 *) value;
break;
}
@@ -1794,18 +1814,6 @@ void Server::SendTimeOfDay(session_t peer_id, u16 time, f32 time_speed)
}
}
-void Server::SendPlayerHP(session_t peer_id)
-{
- PlayerSAO *playersao = getPlayerSAO(peer_id);
- assert(playersao);
-
- SendHP(peer_id, playersao->getHP());
- m_script->player_event(playersao,"health_changed");
-
- // Send to other clients
- playersao->sendPunchCommand();
-}
-
void Server::SendPlayerBreath(PlayerSAO *sao)
{
assert(sao);
@@ -2313,7 +2321,7 @@ void Server::sendMetadataChanged(const std::list<v3s16> &meta_updates, float far
// Send the meta changes
std::ostringstream os(std::ios::binary);
- meta_updates_list.serialize(os, client->net_proto_version, false, true);
+ meta_updates_list.serialize(os, client->serialization_version, false, true, true);
std::ostringstream oss(std::ios::binary);
compressZlib(os.str(), oss);
@@ -2442,9 +2450,8 @@ bool Server::addMediaFile(const std::string &filename,
// If name is not in a supported format, ignore it
const char *supported_ext[] = {
".png", ".jpg", ".bmp", ".tga",
- ".pcx", ".ppm", ".psd", ".wal", ".rgb",
".ogg",
- ".x", ".b3d", ".md2", ".obj",
+ ".x", ".b3d", ".obj",
// Custom translation file format
".tr",
NULL
@@ -2496,7 +2503,9 @@ void Server::fillMediaCache()
// Collect all media file paths
std::vector<std::string> paths;
- // The paths are ordered in descending priority
+
+ // ordered in descending priority
+ paths.push_back(getBuiltinLuaPath() + DIR_DELIM + "locale");
fs::GetRecursiveDirs(paths, porting::path_user + DIR_DELIM + "textures" + DIR_DELIM + "server");
fs::GetRecursiveDirs(paths, m_gamespec.path + DIR_DELIM + "textures");
m_modmgr->getModsMediaPaths(paths);
@@ -2530,6 +2539,8 @@ void Server::sendMediaAnnouncement(session_t peer_id, const std::string &lang_co
std::string lang_suffix;
lang_suffix.append(".").append(lang_code).append(".tr");
for (const auto &i : m_media) {
+ if (i.second.no_announce)
+ continue;
if (str_ends_with(i.first, ".tr") && !str_ends_with(i.first, lang_suffix))
continue;
media_sent++;
@@ -2538,6 +2549,8 @@ void Server::sendMediaAnnouncement(session_t peer_id, const std::string &lang_co
pkt << media_sent;
for (const auto &i : m_media) {
+ if (i.second.no_announce)
+ continue;
if (str_ends_with(i.first, ".tr") && !str_ends_with(i.first, lang_suffix))
continue;
pkt << i.first << i.second.sha1_digest;
@@ -2556,11 +2569,9 @@ struct SendableMedia
std::string path;
std::string data;
- SendableMedia(const std::string &name_="", const std::string &path_="",
- const std::string &data_=""):
- name(name_),
- path(path_),
- data(data_)
+ SendableMedia(const std::string &name, const std::string &path,
+ std::string &&data):
+ name(name), path(path), data(std::move(data))
{}
};
@@ -2587,40 +2598,19 @@ void Server::sendRequestedMedia(session_t peer_id,
continue;
}
- //TODO get path + name
- std::string tpath = m_media[name].path;
+ const auto &m = m_media[name];
// Read data
- std::ifstream fis(tpath.c_str(), std::ios_base::binary);
- if(!fis.good()){
- errorstream<<"Server::sendRequestedMedia(): Could not open \""
- <<tpath<<"\" for reading"<<std::endl;
- continue;
- }
- std::ostringstream tmp_os(std::ios_base::binary);
- bool bad = false;
- for(;;) {
- char buf[1024];
- fis.read(buf, 1024);
- std::streamsize len = fis.gcount();
- tmp_os.write(buf, len);
- file_size_bunch_total += len;
- if(fis.eof())
- break;
- if(!fis.good()) {
- bad = true;
- break;
- }
- }
- if (bad) {
- errorstream<<"Server::sendRequestedMedia(): Failed to read \""
- <<name<<"\""<<std::endl;
+ std::string data;
+ if (!fs::ReadFile(m.path, data)) {
+ errorstream << "Server::sendRequestedMedia(): Failed to read \""
+ << name << "\"" << std::endl;
continue;
}
- /*infostream<<"Server::sendRequestedMedia(): Loaded \""
- <<tname<<"\""<<std::endl;*/
+ file_size_bunch_total += data.size();
+
// Put in list
- file_bunches[file_bunches.size()-1].emplace_back(name, tpath, tmp_os.str());
+ file_bunches.back().emplace_back(name, m.path, std::move(data));
// Start next bunch if got enough data
if(file_size_bunch_total >= bytes_per_bunch) {
@@ -2663,6 +2653,33 @@ void Server::sendRequestedMedia(session_t peer_id,
}
}
+void Server::stepPendingDynMediaCallbacks(float dtime)
+{
+ MutexAutoLock lock(m_env_mutex);
+
+ for (auto it = m_pending_dyn_media.begin(); it != m_pending_dyn_media.end();) {
+ it->second.expiry_timer -= dtime;
+ bool del = it->second.waiting_players.empty() || it->second.expiry_timer < 0;
+
+ if (!del) {
+ it++;
+ continue;
+ }
+
+ const auto &name = it->second.filename;
+ if (!name.empty()) {
+ assert(m_media.count(name));
+ // if no_announce isn't set we're definitely deleting the wrong file!
+ sanity_check(m_media[name].no_announce);
+
+ fs::DeleteSingleFileOrEmptyDirectory(m_media[name].path);
+ m_media.erase(name);
+ }
+ getScriptIface()->freeDynamicMediaCallback(it->first);
+ it = m_pending_dyn_media.erase(it);
+ }
+}
+
void Server::SendMinimapModes(session_t peer_id,
std::vector<MinimapMode> &modes, size_t wanted_mode)
{
@@ -2725,23 +2742,18 @@ void Server::sendDetachedInventories(session_t peer_id, bool incremental)
Something random
*/
-void Server::DiePlayer(session_t peer_id, const PlayerHPChangeReason &reason)
+void Server::HandlePlayerDeath(PlayerSAO *playersao, const PlayerHPChangeReason &reason)
{
- PlayerSAO *playersao = getPlayerSAO(peer_id);
- assert(playersao);
-
infostream << "Server::DiePlayer(): Player "
<< playersao->getPlayer()->getName()
<< " dies" << std::endl;
- playersao->setHP(0, reason);
playersao->clearParentAttachment();
// Trigger scripted stuff
m_script->on_dieplayer(playersao, reason);
- SendPlayerHP(peer_id);
- SendDeathscreen(peer_id, false, v3f(0,0,0));
+ SendDeathscreen(playersao->getPeerID(), false, v3f(0,0,0));
}
void Server::RespawnPlayer(session_t peer_id)
@@ -2762,8 +2774,6 @@ void Server::RespawnPlayer(session_t peer_id)
// setPos will send the new position to client
playersao->setPos(findSpawnPos());
}
-
- SendPlayerHP(peer_id);
}
@@ -2999,6 +3009,9 @@ std::wstring Server::handleChat(const std::string &name,
}
auto message = trim(wide_to_utf8(wmessage));
+ if (message.empty())
+ return L"";
+
if (message.find_first_of("\n\r") != std::wstring::npos) {
return L"Newlines are not permitted in chat messages";
}
@@ -3095,15 +3108,18 @@ std::string Server::getStatusString()
std::ostringstream os(std::ios_base::binary);
os << "# Server: ";
// Version
- os << "version=" << g_version_string;
+ os << "version: " << g_version_string;
+ // Game
+ os << " | game: " << (m_gamespec.name.empty() ? m_gamespec.id : m_gamespec.name);
// Uptime
- os << ", uptime=" << m_uptime_counter->get();
+ os << " | uptime: " << duration_to_string((int) m_uptime_counter->get());
// Max lag estimate
- os << ", max_lag=" << (m_env ? m_env->getMaxLagEstimate() : 0);
+ os << " | max lag: " << std::setprecision(3);
+ os << (m_env ? m_env->getMaxLagEstimate() : 0) << "s";
// Information about clients
bool first = true;
- os << ", clients={";
+ os << " | clients: ";
if (m_env) {
std::vector<session_t> clients = m_clients.getClientIDs();
for (session_t client_id : clients) {
@@ -3120,7 +3136,6 @@ std::string Server::getStatusString()
os << name;
}
}
- os << "}";
if (m_env && !((ServerMap*)(&m_env->getMap()))->isSavingEnabled())
os << std::endl << "# Server: " << " WARNING: Map saving is disabled.";
@@ -3457,14 +3472,18 @@ void Server::deleteParticleSpawner(const std::string &playername, u32 id)
SendDeleteParticleSpawner(peer_id, id);
}
-bool Server::dynamicAddMedia(const std::string &filepath,
- std::vector<RemotePlayer*> &sent_to)
+bool Server::dynamicAddMedia(std::string filepath,
+ const u32 token, const std::string &to_player, bool ephemeral)
{
std::string filename = fs::GetFilenameFromPath(filepath.c_str());
- if (m_media.find(filename) != m_media.end()) {
- errorstream << "Server::dynamicAddMedia(): file \"" << filename
- << "\" already exists in media cache" << std::endl;
- return false;
+ auto it = m_media.find(filename);
+ if (it != m_media.end()) {
+ // Allow the same path to be "added" again in certain conditions
+ if (ephemeral || it->second.path != filepath) {
+ errorstream << "Server::dynamicAddMedia(): file \"" << filename
+ << "\" already exists in media cache" << std::endl;
+ return false;
+ }
}
// Load the file and add it to our media cache
@@ -3473,35 +3492,105 @@ bool Server::dynamicAddMedia(const std::string &filepath,
if (!ok)
return false;
+ if (ephemeral) {
+ // Create a copy of the file and swap out the path, this removes the
+ // requirement that mods keep the file accessible at the original path.
+ filepath = fs::CreateTempFile();
+ bool ok = ([&] () -> bool {
+ if (filepath.empty())
+ return false;
+ std::ofstream os(filepath.c_str(), std::ios::binary);
+ if (!os.good())
+ return false;
+ os << filedata;
+ os.close();
+ return !os.fail();
+ })();
+ if (!ok) {
+ errorstream << "Server: failed to create a copy of media file "
+ << "\"" << filename << "\"" << std::endl;
+ m_media.erase(filename);
+ return false;
+ }
+ verbosestream << "Server: \"" << filename << "\" temporarily copied to "
+ << filepath << std::endl;
+
+ m_media[filename].path = filepath;
+ m_media[filename].no_announce = true;
+ // stepPendingDynMediaCallbacks will clean this up later.
+ } else if (!to_player.empty()) {
+ m_media[filename].no_announce = true;
+ }
+
// Push file to existing clients
NetworkPacket pkt(TOCLIENT_MEDIA_PUSH, 0);
- pkt << raw_hash << filename << (bool) true;
- pkt.putLongString(filedata);
+ pkt << raw_hash << filename << (bool)ephemeral;
+
+ NetworkPacket legacy_pkt = pkt;
+
+ // Newer clients get asked to fetch the file (asynchronous)
+ pkt << token;
+ // Older clients have an awful hack that just throws the data at them
+ legacy_pkt.putLongString(filedata);
+ std::unordered_set<session_t> delivered, waiting;
m_clients.lock();
for (auto &pair : m_clients.getClientList()) {
- if (pair.second->getState() < CS_DefinitionsSent)
+ if (pair.second->getState() == CS_DefinitionsSent && !ephemeral) {
+ /*
+ If a client is in the DefinitionsSent state it is too late to
+ transfer the file via sendMediaAnnouncement() but at the same
+ time the client cannot accept a media push yet.
+ Short of artificially delaying the joining process there is no
+ way for the server to resolve this so we (currently) opt not to.
+ */
+ warningstream << "The media \"" << filename << "\" (dynamic) could "
+ "not be delivered to " << pair.second->getName()
+ << " due to a race condition." << std::endl;
continue;
- if (pair.second->net_proto_version < 39)
+ }
+ if (pair.second->getState() < CS_Active)
continue;
- if (auto player = m_env->getPlayer(pair.second->peer_id))
- sent_to.emplace_back(player);
- /*
- FIXME: this is a very awful hack
- The network layer only guarantees ordered delivery inside a channel.
- Since the very next packet could be one that uses the media, we have
- to push the media over ALL channels to ensure it is processed before
- it is used.
- In practice this means we have to send it twice:
- - channel 1 (HUD)
- - channel 0 (everything else: e.g. play_sound, object messages)
- */
- m_clients.send(pair.second->peer_id, 1, &pkt, true);
- m_clients.send(pair.second->peer_id, 0, &pkt, true);
+ const auto proto_ver = pair.second->net_proto_version;
+ if (proto_ver < 39)
+ continue;
+
+ const session_t peer_id = pair.second->peer_id;
+ if (!to_player.empty() && getPlayerName(peer_id) != to_player)
+ continue;
+
+ if (proto_ver < 40) {
+ delivered.emplace(peer_id);
+ /*
+ The network layer only guarantees ordered delivery inside a channel.
+ Since the very next packet could be one that uses the media, we have
+ to push the media over ALL channels to ensure it is processed before
+ it is used. In practice this means channels 1 and 0.
+ */
+ m_clients.send(peer_id, 1, &legacy_pkt, true);
+ m_clients.send(peer_id, 0, &legacy_pkt, true);
+ } else {
+ waiting.emplace(peer_id);
+ Send(peer_id, &pkt);
+ }
}
m_clients.unlock();
+ // Run callback for players that already had the file delivered (legacy-only)
+ for (session_t peer_id : delivered) {
+ if (auto player = m_env->getPlayer(peer_id))
+ getScriptIface()->on_dynamic_media_added(token, player->getName());
+ }
+
+ // Save all others in our pending state
+ auto &state = m_pending_dyn_media[token];
+ state.waiting_players = std::move(waiting);
+ // regardless of success throw away the callback after a while
+ state.expiry_timer = 60.0f;
+ if (ephemeral)
+ state.filename = filename;
+
return true;
}
@@ -3606,11 +3695,6 @@ std::string Server::getBuiltinLuaPath()
return porting::path_share + DIR_DELIM + "builtin";
}
-std::string Server::getModStoragePath() const
-{
- return m_path_world + DIR_DELIM + "mod_storage";
-}
-
v3f Server::findSpawnPos()
{
ServerMap &map = m_env->getServerMap();
@@ -3774,11 +3858,8 @@ bool Server::registerModStorage(ModMetadata *storage)
void Server::unregisterModStorage(const std::string &name)
{
std::unordered_map<std::string, ModMetadata *>::const_iterator it = m_mod_storages.find(name);
- if (it != m_mod_storages.end()) {
- // Save unconditionaly on unregistration
- it->second->save(getModStoragePath());
+ if (it != m_mod_storages.end())
m_mod_storages.erase(name);
- }
}
void dedicated_server_loop(Server &server, bool &kill)
@@ -3916,3 +3997,106 @@ Translations *Server::getTranslationLanguage(const std::string &lang_code)
return translations;
}
+
+ModMetadataDatabase *Server::openModStorageDatabase(const std::string &world_path)
+{
+ std::string world_mt_path = world_path + DIR_DELIM + "world.mt";
+ Settings world_mt;
+ if (!world_mt.readConfigFile(world_mt_path.c_str()))
+ throw BaseException("Cannot read world.mt!");
+
+ std::string backend = world_mt.exists("mod_storage_backend") ?
+ world_mt.get("mod_storage_backend") : "files";
+ if (backend == "files")
+ warningstream << "/!\\ You are using the old mod storage files backend. "
+ << "This backend is deprecated and may be removed in a future release /!\\"
+ << std::endl << "Switching to SQLite3 is advised, "
+ << "please read http://wiki.minetest.net/Database_backends." << std::endl;
+
+ return openModStorageDatabase(backend, world_path, world_mt);
+}
+
+ModMetadataDatabase *Server::openModStorageDatabase(const std::string &backend,
+ const std::string &world_path, const Settings &world_mt)
+{
+ if (backend == "sqlite3")
+ return new ModMetadataDatabaseSQLite3(world_path);
+
+ if (backend == "files")
+ return new ModMetadataDatabaseFiles(world_path);
+
+ if (backend == "dummy")
+ return new Database_Dummy();
+
+ throw BaseException("Mod storage database backend " + backend + " not supported");
+}
+
+bool Server::migrateModStorageDatabase(const GameParams &game_params, const Settings &cmd_args)
+{
+ std::string migrate_to = cmd_args.get("migrate-mod-storage");
+ Settings world_mt;
+ std::string world_mt_path = game_params.world_path + DIR_DELIM + "world.mt";
+ if (!world_mt.readConfigFile(world_mt_path.c_str())) {
+ errorstream << "Cannot read world.mt!" << std::endl;
+ return false;
+ }
+
+ std::string backend = world_mt.exists("mod_storage_backend") ?
+ world_mt.get("mod_storage_backend") : "files";
+ if (backend == migrate_to) {
+ errorstream << "Cannot migrate: new backend is same"
+ << " as the old one" << std::endl;
+ return false;
+ }
+
+ ModMetadataDatabase *srcdb = nullptr;
+ ModMetadataDatabase *dstdb = nullptr;
+
+ bool succeeded = false;
+
+ try {
+ srcdb = Server::openModStorageDatabase(backend, game_params.world_path, world_mt);
+ dstdb = Server::openModStorageDatabase(migrate_to, game_params.world_path, world_mt);
+
+ dstdb->beginSave();
+
+ std::vector<std::string> mod_list;
+ srcdb->listMods(&mod_list);
+ for (const std::string &modname : mod_list) {
+ StringMap meta;
+ srcdb->getModEntries(modname, &meta);
+ for (const auto &pair : meta) {
+ dstdb->setModEntry(modname, pair.first, pair.second);
+ }
+ }
+
+ dstdb->endSave();
+
+ succeeded = true;
+
+ actionstream << "Successfully migrated the metadata of "
+ << mod_list.size() << " mods" << std::endl;
+ world_mt.set("mod_storage_backend", migrate_to);
+ if (!world_mt.updateConfigFile(world_mt_path.c_str()))
+ errorstream << "Failed to update world.mt!" << std::endl;
+ else
+ actionstream << "world.mt updated" << std::endl;
+
+ } catch (BaseException &e) {
+ errorstream << "An error occurred during migration: " << e.what() << std::endl;
+ }
+
+ delete srcdb;
+ delete dstdb;
+
+ if (succeeded && backend == "files") {
+ // Back up files
+ const std::string storage_path = game_params.world_path + DIR_DELIM + "mod_storage";
+ const std::string backup_path = game_params.world_path + DIR_DELIM + "mod_storage.bak";
+ if (!fs::Rename(storage_path, backup_path))
+ warningstream << "After migration, " << storage_path
+ << " could not be renamed to " << backup_path << std::endl;
+ }
+
+ return succeeded;
+}
diff --git a/src/server.h b/src/server.h
index 9857215d0..2741b3157 100644
--- a/src/server.h
+++ b/src/server.h
@@ -43,6 +43,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <list>
#include <map>
#include <vector>
+#include <unordered_set>
class ChatEvent;
struct ChatEventChat;
@@ -81,12 +82,14 @@ enum ClientDeletionReason {
struct MediaInfo
{
std::string path;
- std::string sha1_digest;
+ std::string sha1_digest; // base64-encoded
+ bool no_announce; // true: not announced in TOCLIENT_ANNOUNCE_MEDIA (at player join)
MediaInfo(const std::string &path_="",
const std::string &sha1_digest_=""):
path(path_),
- sha1_digest(sha1_digest_)
+ sha1_digest(sha1_digest_),
+ no_announce(false)
{
}
};
@@ -197,6 +200,7 @@ public:
void handleCommand_FirstSrp(NetworkPacket* pkt);
void handleCommand_SrpBytesA(NetworkPacket* pkt);
void handleCommand_SrpBytesM(NetworkPacket* pkt);
+ void handleCommand_HaveMedia(NetworkPacket *pkt);
void ProcessData(NetworkPacket *pkt);
@@ -257,7 +261,8 @@ public:
void deleteParticleSpawner(const std::string &playername, u32 id);
- bool dynamicAddMedia(const std::string &filepath, std::vector<RemotePlayer*> &sent_to);
+ bool dynamicAddMedia(std::string filepath, u32 token,
+ const std::string &to_player, bool ephemeral);
ServerInventoryManager *getInventoryMgr() const { return m_inventory_mgr.get(); }
void sendDetachedInventory(Inventory *inventory, const std::string &name, session_t peer_id);
@@ -278,6 +283,7 @@ public:
virtual u16 allocateUnknownNodeId(const std::string &name);
IRollbackManager *getRollbackManager() { return m_rollback; }
virtual EmergeManager *getEmergeManager() { return m_emerge; }
+ virtual ModMetadataDatabase *getModStorageDatabase() { return m_mod_storage_database; }
IWritableItemDefManager* getWritableItemDefManager();
NodeDefManager* getWritableNodeDefManager();
@@ -288,13 +294,16 @@ public:
void getModNames(std::vector<std::string> &modlist);
std::string getBuiltinLuaPath();
virtual std::string getWorldPath() const { return m_path_world; }
- virtual std::string getModStoragePath() const;
inline bool isSingleplayer()
{ return m_simple_singleplayer_mode; }
inline void setAsyncFatalError(const std::string &error)
{ m_async_fatal_error.set(error); }
+ inline void setAsyncFatalError(const LuaError &e)
+ {
+ setAsyncFatalError(std::string("Lua: ") + e.what());
+ }
bool showFormspec(const char *name, const std::string &formspec, const std::string &formname);
Map & getMap() { return m_env->getMap(); }
@@ -341,7 +350,8 @@ public:
void printToConsoleOnly(const std::string &text);
- void SendPlayerHPOrDie(PlayerSAO *player, const PlayerHPChangeReason &reason);
+ void HandlePlayerHPChange(PlayerSAO *sao, const PlayerHPChangeReason &reason);
+ void SendPlayerHP(PlayerSAO *sao);
void SendPlayerBreath(PlayerSAO *sao);
void SendInventory(PlayerSAO *playerSAO, bool incremental);
void SendMovePlayer(session_t peer_id);
@@ -368,6 +378,14 @@ public:
// Get or load translations for a language
Translations *getTranslationLanguage(const std::string &lang_code);
+ static ModMetadataDatabase *openModStorageDatabase(const std::string &world_path);
+
+ static ModMetadataDatabase *openModStorageDatabase(const std::string &backend,
+ const std::string &world_path, const Settings &world_mt);
+
+ static bool migrateModStorageDatabase(const GameParams &game_params,
+ const Settings &cmd_args);
+
// Bind address
Address m_bind_addr;
@@ -395,6 +413,12 @@ private:
float m_timer = 0.0f;
};
+ struct PendingDynamicMediaCallback {
+ std::string filename; // only set if media entry and file is to be deleted
+ float expiry_timer;
+ std::unordered_set<session_t> waiting_players;
+ };
+
void init();
void SendMovement(session_t peer_id);
@@ -415,7 +439,6 @@ private:
virtual void SendChatMessage(session_t peer_id, const ChatMessage &message);
void SendTimeOfDay(session_t peer_id, u16 time, f32 time_speed);
- void SendPlayerHP(session_t peer_id);
void SendLocalPlayerAnimations(session_t peer_id, v2s32 animation_frames[4],
f32 animation_speed);
@@ -466,6 +489,7 @@ private:
void sendMediaAnnouncement(session_t peer_id, const std::string &lang_code);
void sendRequestedMedia(session_t peer_id,
const std::vector<std::string> &tosend);
+ void stepPendingDynMediaCallbacks(float dtime);
// Adds a ParticleSpawner on peer with peer_id (PEER_ID_INEXISTENT == all)
void SendAddParticleSpawner(session_t peer_id, u16 protocol_version,
@@ -486,7 +510,7 @@ private:
Something random
*/
- void DiePlayer(session_t peer_id, const PlayerHPChangeReason &reason);
+ void HandlePlayerDeath(PlayerSAO* sao, const PlayerHPChangeReason &reason);
void RespawnPlayer(session_t peer_id);
void DeleteClient(session_t peer_id, ClientDeletionReason reason);
void UpdateCrafting(RemotePlayer *player);
@@ -650,6 +674,10 @@ private:
// media files known to server
std::unordered_map<std::string, MediaInfo> m_media;
+ // pending dynamic media callbacks, clients inform the server when they have a file fetched
+ std::unordered_map<u32, PendingDynamicMediaCallback> m_pending_dyn_media;
+ float m_step_pending_dyn_media_timer = 0.0f;
+
/*
Sounds
*/
@@ -658,6 +686,7 @@ private:
s32 nextSoundId();
std::unordered_map<std::string, ModMetadata *> m_mod_storages;
+ ModMetadataDatabase *m_mod_storage_database = nullptr;
float m_mod_storage_save_timer = 10.0f;
// CSM restrictions byteflag
diff --git a/src/server/luaentity_sao.cpp b/src/server/luaentity_sao.cpp
index 3bcbe107b..82f6da231 100644
--- a/src/server/luaentity_sao.cpp
+++ b/src/server/luaentity_sao.cpp
@@ -108,7 +108,12 @@ void LuaEntitySAO::addedToEnvironment(u32 dtime_s)
m_env->getScriptIface()->
luaentity_Activate(m_id, m_init_state, dtime_s);
} else {
+ // It's an unknown object
+ // Use entitystring as infotext for debugging
m_prop.infotext = m_init_name;
+ // Set unknown object texture
+ m_prop.textures.clear();
+ m_prop.textures.emplace_back("unknown_object.png");
}
}
@@ -300,10 +305,11 @@ void LuaEntitySAO::getStaticData(std::string *result) const
*result = os.str();
}
-u16 LuaEntitySAO::punch(v3f dir,
+u32 LuaEntitySAO::punch(v3f dir,
const ToolCapabilities *toolcap,
ServerActiveObject *puncher,
- float time_from_last_punch)
+ float time_from_last_punch,
+ u16 initial_wear)
{
if (!m_registered) {
// Delete unknown LuaEntities when punched
@@ -321,7 +327,8 @@ u16 LuaEntitySAO::punch(v3f dir,
m_armor_groups,
toolcap,
&tool_item,
- time_from_last_punch);
+ time_from_last_punch,
+ initial_wear);
bool damage_handled = m_env->getScriptIface()->luaentity_Punch(m_id, puncher,
time_from_last_punch, toolcap, dir, result.did_punch ? result.damage : 0);
diff --git a/src/server/luaentity_sao.h b/src/server/luaentity_sao.h
index 6883ae1b9..87b664a8b 100644
--- a/src/server/luaentity_sao.h
+++ b/src/server/luaentity_sao.h
@@ -44,9 +44,10 @@ public:
bool isStaticAllowed() const { return m_prop.static_save; }
bool shouldUnload() const { return true; }
void getStaticData(std::string *result) const;
- u16 punch(v3f dir, const ToolCapabilities *toolcap = nullptr,
+ u32 punch(v3f dir, const ToolCapabilities *toolcap = nullptr,
ServerActiveObject *puncher = nullptr,
- float time_from_last_punch = 1000000.0f);
+ float time_from_last_punch = 1000000.0f,
+ u16 initial_wear = 0);
void rightClick(ServerActiveObject *clicker);
void setPos(const v3f &pos);
void moveTo(v3f pos, bool continuous);
diff --git a/src/server/mods.cpp b/src/server/mods.cpp
index 83fa12da9..609d8c346 100644
--- a/src/server/mods.cpp
+++ b/src/server/mods.cpp
@@ -61,12 +61,8 @@ void ServerModManager::loadMods(ServerScripting *script)
infostream << std::endl;
// Load and run "mod" scripts
for (const ModSpec &mod : m_sorted_mods) {
- if (!string_allowed(mod.name, MODNAME_ALLOWED_CHARS)) {
- throw ModError("Error loading mod \"" + mod.name +
- "\": Mod name does not follow naming "
- "conventions: "
- "Only characters [a-z0-9_] are allowed.");
- }
+ mod.checkAndLog();
+
std::string script_path = mod.path + DIR_DELIM + "init.lua";
auto t = porting::getTimeMs();
script->loadMod(script_path, mod.name);
diff --git a/src/server/player_sao.cpp b/src/server/player_sao.cpp
index 0d31f2e0b..d076d5783 100644
--- a/src/server/player_sao.cpp
+++ b/src/server/player_sao.cpp
@@ -167,7 +167,6 @@ void PlayerSAO::step(float dtime, bool send_recommended)
if (m_breath == 0) {
PlayerHPChangeReason reason(PlayerHPChangeReason::DROWNING);
setHP(m_hp - c.drowning, reason);
- m_env->getGameDef()->SendPlayerHPOrDie(this, reason);
}
}
}
@@ -216,7 +215,6 @@ void PlayerSAO::step(float dtime, bool send_recommended)
s32 newhp = (s32)m_hp - (s32)damage_per_second;
PlayerHPChangeReason reason(PlayerHPChangeReason::NODE_DAMAGE, nodename);
setHP(newhp, reason);
- m_env->getGameDef()->SendPlayerHPOrDie(this, reason);
}
}
@@ -411,10 +409,11 @@ void PlayerSAO::setLookPitchAndSend(const float pitch)
m_env->getGameDef()->SendMovePlayer(m_peer_id);
}
-u16 PlayerSAO::punch(v3f dir,
+u32 PlayerSAO::punch(v3f dir,
const ToolCapabilities *toolcap,
ServerActiveObject *puncher,
- float time_from_last_punch)
+ float time_from_last_punch,
+ u16 initial_wear)
{
if (!toolcap)
return 0;
@@ -432,7 +431,7 @@ u16 PlayerSAO::punch(v3f dir,
s32 old_hp = getHP();
HitParams hitparams = getHitParams(m_armor_groups, toolcap,
- time_from_last_punch);
+ time_from_last_punch, initial_wear);
PlayerSAO *playersao = m_player->getPlayerSAO();
@@ -464,33 +463,33 @@ void PlayerSAO::rightClick(ServerActiveObject *clicker)
m_env->getScriptIface()->on_rightclickplayer(this, clicker);
}
-void PlayerSAO::setHP(s32 hp, const PlayerHPChangeReason &reason)
+void PlayerSAO::setHP(s32 target_hp, const PlayerHPChangeReason &reason, bool from_client)
{
- if (hp == (s32)m_hp)
- return; // Nothing to do
+ target_hp = rangelim(target_hp, 0, U16_MAX);
- if (m_hp <= 0 && hp < (s32)m_hp)
- return; // Cannot take more damage
+ if (target_hp == m_hp)
+ return; // Nothing to do
- {
- s32 hp_change = m_env->getScriptIface()->on_player_hpchange(this, hp - m_hp, reason);
- if (hp_change == 0)
- return;
+ s32 hp_change = m_env->getScriptIface()->on_player_hpchange(this, target_hp - (s32)m_hp, reason);
- hp = m_hp + hp_change;
- }
+ s32 hp = (s32)m_hp + std::min(hp_change, U16_MAX); // Protection against s32 overflow
+ hp = rangelim(hp, 0, U16_MAX);
- s32 oldhp = m_hp;
- hp = rangelim(hp, 0, m_prop.hp_max);
+ if (hp > m_prop.hp_max)
+ hp = m_prop.hp_max;
- if (hp < oldhp && isImmortal())
- return; // Do not allow immortal players to be damaged
-
- m_hp = hp;
+ if (hp < m_hp && isImmortal())
+ hp = m_hp; // Do not allow immortal players to be damaged
// Update properties on death
- if ((hp == 0) != (oldhp == 0))
+ if ((hp == 0) != (m_hp == 0))
m_properties_sent = false;
+
+ if (hp != m_hp) {
+ m_hp = hp;
+ m_env->getGameDef()->HandlePlayerHPChange(this, reason);
+ } else if (from_client)
+ m_env->getGameDef()->SendPlayerHP(this);
}
void PlayerSAO::setBreath(const u16 breath, bool send)
diff --git a/src/server/player_sao.h b/src/server/player_sao.h
index 8e2d8803f..96d8f7189 100644
--- a/src/server/player_sao.h
+++ b/src/server/player_sao.h
@@ -109,10 +109,14 @@ public:
Interaction interface
*/
- u16 punch(v3f dir, const ToolCapabilities *toolcap, ServerActiveObject *puncher,
- float time_from_last_punch);
+ u32 punch(v3f dir, const ToolCapabilities *toolcap, ServerActiveObject *puncher,
+ float time_from_last_punch, u16 initial_wear = 0);
void rightClick(ServerActiveObject *clicker);
- void setHP(s32 hp, const PlayerHPChangeReason &reason);
+ void setHP(s32 hp, const PlayerHPChangeReason &reason) override
+ {
+ return setHP(hp, reason, false);
+ }
+ void setHP(s32 hp, const PlayerHPChangeReason &reason, bool from_client);
void setHPRaw(u16 hp) { m_hp = hp; }
u16 getBreath() const { return m_breath; }
void setBreath(const u16 breath, bool send = true);
diff --git a/src/server/serveractiveobject.h b/src/server/serveractiveobject.h
index 51f445914..5b0ee2d9b 100644
--- a/src/server/serveractiveobject.h
+++ b/src/server/serveractiveobject.h
@@ -145,11 +145,12 @@ public:
virtual bool shouldUnload() const
{ return true; }
- // Returns tool wear
- virtual u16 punch(v3f dir,
+ // Returns added tool wear
+ virtual u32 punch(v3f dir,
const ToolCapabilities *toolcap = nullptr,
ServerActiveObject *puncher = nullptr,
- float time_from_last_punch = 1000000.0f)
+ float time_from_last_punch = 1000000.0f,
+ u16 initial_wear = 0)
{ return 0; }
virtual void rightClick(ServerActiveObject *clicker)
{}
diff --git a/src/server/serverinventorymgr.cpp b/src/server/serverinventorymgr.cpp
index 2a80c9bbe..63d1645cb 100644
--- a/src/server/serverinventorymgr.cpp
+++ b/src/server/serverinventorymgr.cpp
@@ -39,24 +39,29 @@ ServerInventoryManager::~ServerInventoryManager()
Inventory *ServerInventoryManager::getInventory(const InventoryLocation &loc)
{
+ // No m_env check here: allow creation and modification of detached inventories
+
switch (loc.type) {
case InventoryLocation::UNDEFINED:
case InventoryLocation::CURRENT_PLAYER:
break;
case InventoryLocation::PLAYER: {
+ if (!m_env)
+ return nullptr;
+
RemotePlayer *player = m_env->getPlayer(loc.name.c_str());
if (!player)
return NULL;
+
PlayerSAO *playersao = player->getPlayerSAO();
- if (!playersao)
- return NULL;
- return playersao->getInventory();
+ return playersao ? playersao->getInventory() : nullptr;
} break;
case InventoryLocation::NODEMETA: {
+ if (!m_env)
+ return nullptr;
+
NodeMetadata *meta = m_env->getMap().getNodeMetadata(loc.p);
- if (!meta)
- return NULL;
- return meta->getInventory();
+ return meta ? meta->getInventory() : nullptr;
} break;
case InventoryLocation::DETACHED: {
auto it = m_detached_inventories.find(loc.name);
@@ -151,14 +156,15 @@ bool ServerInventoryManager::removeDetachedInventory(const std::string &name)
const std::string &owner = inv_it->second.owner;
if (!owner.empty()) {
- RemotePlayer *player = m_env->getPlayer(owner.c_str());
+ if (m_env) {
+ RemotePlayer *player = m_env->getPlayer(owner.c_str());
- if (player && player->getPeerId() != PEER_ID_INEXISTENT)
- m_env->getGameDef()->sendDetachedInventory(
- nullptr, name, player->getPeerId());
-
- } else {
- // Notify all players about the change
+ if (player && player->getPeerId() != PEER_ID_INEXISTENT)
+ m_env->getGameDef()->sendDetachedInventory(
+ nullptr, name, player->getPeerId());
+ }
+ } else if (m_env) {
+ // Notify all players about the change as soon ServerEnv exists
m_env->getGameDef()->sendDetachedInventory(
nullptr, name, PEER_ID_INEXISTENT);
}
diff --git a/src/server/unit_sao.cpp b/src/server/unit_sao.cpp
index 2371640ca..9a49b0f43 100644
--- a/src/server/unit_sao.cpp
+++ b/src/server/unit_sao.cpp
@@ -84,8 +84,11 @@ void UnitSAO::setBonePosition(const std::string &bone, v3f position, v3f rotatio
void UnitSAO::getBonePosition(const std::string &bone, v3f *position, v3f *rotation)
{
- *position = m_bone_position[bone].X;
- *rotation = m_bone_position[bone].Y;
+ auto it = m_bone_position.find(bone);
+ if (it != m_bone_position.end()) {
+ *position = it->second.X;
+ *rotation = it->second.Y;
+ }
}
// clang-format off
@@ -124,6 +127,19 @@ void UnitSAO::sendOutdatedData()
void UnitSAO::setAttachment(int parent_id, const std::string &bone, v3f position,
v3f rotation, bool force_visible)
{
+ auto *obj = parent_id ? m_env->getActiveObject(parent_id) : nullptr;
+ if (obj) {
+ // Do checks to avoid circular references
+ // The chain of wanted parent must not refer or contain "this"
+ for (obj = obj->getParent(); obj; obj = obj->getParent()) {
+ if (obj == this) {
+ warningstream << "Mod bug: Attempted to attach object " << m_id << " to parent "
+ << parent_id << " but former is an (in)direct parent of latter." << std::endl;
+ return;
+ }
+ }
+ }
+
// Attachments need to be handled on both the server and client.
// If we just attach on the server, we can only copy the position of the parent.
// Attachments are still sent to clients at an interval so players might see them
@@ -134,16 +150,21 @@ void UnitSAO::setAttachment(int parent_id, const std::string &bone, v3f position
int old_parent = m_attachment_parent_id;
m_attachment_parent_id = parent_id;
+
+ // The detach callbacks might call to setAttachment() again.
+ // Ensure the attachment params are applied after this callback is run.
+ if (parent_id != old_parent)
+ onDetach(old_parent);
+
+ m_attachment_parent_id = parent_id;
m_attachment_bone = bone;
m_attachment_position = position;
m_attachment_rotation = rotation;
m_force_visible = force_visible;
m_attachment_sent = false;
- if (parent_id != old_parent) {
- onDetach(old_parent);
+ if (parent_id != old_parent)
onAttach(parent_id);
- }
}
void UnitSAO::getAttachment(int *parent_id, std::string *bone, v3f *position,
diff --git a/src/serverenvironment.cpp b/src/serverenvironment.cpp
index 3d9ba132b..f3711652c 100644
--- a/src/serverenvironment.cpp
+++ b/src/serverenvironment.cpp
@@ -729,6 +729,8 @@ struct ActiveABM
int chance;
std::vector<content_t> required_neighbors;
bool check_required_neighbors; // false if required_neighbors is known to be empty
+ s16 min_y;
+ s16 max_y;
};
class ABMHandler
@@ -773,6 +775,9 @@ public:
} else {
aabm.chance = chance;
}
+ // y limits
+ aabm.min_y = abm->getMinY();
+ aabm.max_y = abm->getMaxY();
// Trigger neighbors
const std::vector<std::string> &required_neighbors_s =
@@ -885,6 +890,9 @@ public:
v3s16 p = p0 + block->getPosRelative();
for (ActiveABM &aabm : *m_aabms[c]) {
+ if ((p.Y < aabm.min_y) || (p.Y > aabm.max_y))
+ continue;
+
if (myrand() % aabm.chance != 0)
continue;
@@ -1542,6 +1550,21 @@ void ServerEnvironment::step(float dtime)
m_server->sendDetachedInventories(PEER_ID_INEXISTENT, true);
}
+ServerEnvironment::BlockStatus ServerEnvironment::getBlockStatus(v3s16 blockpos)
+{
+ if (m_active_blocks.contains(blockpos))
+ return BS_ACTIVE;
+
+ const MapBlock *block = m_map->getBlockNoCreateNoEx(blockpos);
+ if (block && !block->isDummy())
+ return BS_LOADED;
+
+ if (m_map->isBlockInQueue(blockpos))
+ return BS_EMERGING;
+
+ return BS_UNKNOWN;
+}
+
u32 ServerEnvironment::addParticleSpawner(float exptime)
{
// Timers with lifetime 0 do not expire
diff --git a/src/serverenvironment.h b/src/serverenvironment.h
index a11c814ed..8733c2dd2 100644
--- a/src/serverenvironment.h
+++ b/src/serverenvironment.h
@@ -67,6 +67,10 @@ public:
virtual u32 getTriggerChance() = 0;
// Whether to modify chance to simulate time lost by an unnattended block
virtual bool getSimpleCatchUp() = 0;
+ // get min Y for apply abm
+ virtual s16 getMinY() = 0;
+ // get max Y for apply abm
+ virtual s16 getMaxY() = 0;
// This is called usually at interval for 1/chance of the nodes
virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n){};
virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n,
@@ -342,7 +346,16 @@ public:
void reportMaxLagEstimate(float f) { m_max_lag_estimate = f; }
float getMaxLagEstimate() { return m_max_lag_estimate; }
- std::set<v3s16>* getForceloadedBlocks() { return &m_active_blocks.m_forceloaded_list; };
+ std::set<v3s16>* getForceloadedBlocks() { return &m_active_blocks.m_forceloaded_list; }
+
+ // Sorted by how ready a mapblock is
+ enum BlockStatus {
+ BS_UNKNOWN,
+ BS_EMERGING,
+ BS_LOADED,
+ BS_ACTIVE // always highest value
+ };
+ BlockStatus getBlockStatus(v3s16 blockpos);
// Sets the static object status all the active objects in the specified block
// This is only really needed for deleting blocks from the map
diff --git a/src/serverlist.cpp b/src/serverlist.cpp
index 3bcab3d58..29e3ac9a6 100644
--- a/src/serverlist.cpp
+++ b/src/serverlist.cpp
@@ -97,6 +97,7 @@ void sendAnnounce(AnnounceAction action,
}
HTTPFetchRequest fetch_request;
+ fetch_request.caller = HTTPFETCH_PRINT_ERR;
fetch_request.url = g_settings->get("serverlist_url") + std::string("/announce");
fetch_request.method = HTTP_POST;
fetch_request.fields["json"] = fastWriteJson(server);
diff --git a/src/settings.cpp b/src/settings.cpp
index cff393e5f..0e44ee0bc 100644
--- a/src/settings.cpp
+++ b/src/settings.cpp
@@ -33,35 +33,89 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <cctype>
#include <algorithm>
-Settings *g_settings = nullptr; // Populated in main()
+Settings *g_settings = nullptr;
+static SettingsHierarchy g_hierarchy;
std::string g_settings_path;
-Settings *Settings::s_layers[SL_TOTAL_COUNT] = {0}; // Zeroed by compiler
std::unordered_map<std::string, const FlagDesc *> Settings::s_flags;
+/* Settings hierarchy implementation */
-Settings *Settings::createLayer(SettingsLayer sl, const std::string &end_tag)
+SettingsHierarchy::SettingsHierarchy(Settings *fallback)
{
- if ((int)sl < 0 || sl >= SL_TOTAL_COUNT)
+ layers.push_back(fallback);
+}
+
+
+Settings *SettingsHierarchy::getLayer(int layer) const
+{
+ if (layer < 0 || layer >= (int)layers.size())
throw BaseException("Invalid settings layer");
+ return layers[layer];
+}
+
+
+Settings *SettingsHierarchy::getParent(int layer) const
+{
+ assert(layer >= 0 && layer < (int)layers.size());
+ // iterate towards the origin (0) to find the next fallback layer
+ for (int i = layer - 1; i >= 0; --i) {
+ if (layers[i])
+ return layers[i];
+ }
+
+ return nullptr;
+}
- Settings *&pos = s_layers[(size_t)sl];
+
+void SettingsHierarchy::onLayerCreated(int layer, Settings *obj)
+{
+ if (layer < 0)
+ throw BaseException("Invalid settings layer");
+ if ((int)layers.size() < layer + 1)
+ layers.resize(layer + 1);
+
+ Settings *&pos = layers[layer];
if (pos)
- throw BaseException("Setting layer " + std::to_string(sl) + " already exists");
+ throw BaseException("Setting layer " + itos(layer) + " already exists");
+
+ pos = obj;
+ // This feels bad
+ if (this == &g_hierarchy && layer == (int)SL_GLOBAL)
+ g_settings = obj;
+}
- pos = new Settings(end_tag);
- pos->m_settingslayer = sl;
- if (sl == SL_GLOBAL)
- g_settings = pos;
- return pos;
+void SettingsHierarchy::onLayerRemoved(int layer)
+{
+ assert(layer >= 0 && layer < (int)layers.size());
+ layers[layer] = nullptr;
+ if (this == &g_hierarchy && layer == (int)SL_GLOBAL)
+ g_settings = nullptr;
+}
+
+/* Settings implementation */
+
+Settings *Settings::createLayer(SettingsLayer sl, const std::string &end_tag)
+{
+ return new Settings(end_tag, &g_hierarchy, (int)sl);
}
Settings *Settings::getLayer(SettingsLayer sl)
{
- sanity_check((int)sl >= 0 && sl < SL_TOTAL_COUNT);
- return s_layers[(size_t)sl];
+ return g_hierarchy.getLayer(sl);
+}
+
+
+Settings::Settings(const std::string &end_tag, SettingsHierarchy *h,
+ int settings_layer) :
+ m_end_tag(end_tag),
+ m_hierarchy(h),
+ m_settingslayer(settings_layer)
+{
+ if (m_hierarchy)
+ m_hierarchy->onLayerCreated(m_settingslayer, this);
}
@@ -69,12 +123,8 @@ Settings::~Settings()
{
MutexAutoLock lock(m_mutex);
- if (m_settingslayer < SL_TOTAL_COUNT)
- s_layers[(size_t)m_settingslayer] = nullptr;
-
- // Compatibility
- if (m_settingslayer == SL_GLOBAL)
- g_settings = nullptr;
+ if (m_hierarchy)
+ m_hierarchy->onLayerRemoved(m_settingslayer);
clearNoLock();
}
@@ -86,8 +136,8 @@ Settings & Settings::operator = (const Settings &other)
return *this;
// TODO: Avoid copying Settings objects. Make this private.
- FATAL_ERROR_IF(m_settingslayer != SL_TOTAL_COUNT && other.m_settingslayer != SL_TOTAL_COUNT,
- ("Tried to copy unique Setting layer " + std::to_string(m_settingslayer)).c_str());
+ FATAL_ERROR_IF(m_hierarchy || other.m_hierarchy,
+ "Cannot copy or overwrite Settings object that belongs to a hierarchy");
MutexAutoLock lock(m_mutex);
MutexAutoLock lock2(other.m_mutex);
@@ -410,18 +460,7 @@ bool Settings::parseCommandLine(int argc, char *argv[],
Settings *Settings::getParent() const
{
- // If the Settings object is within the hierarchy structure,
- // iterate towards the origin (0) to find the next fallback layer
- if (m_settingslayer >= SL_TOTAL_COUNT)
- return nullptr;
-
- for (int i = (int)m_settingslayer - 1; i >= 0; --i) {
- if (s_layers[i])
- return s_layers[i];
- }
-
- // No parent
- return nullptr;
+ return m_hierarchy ? m_hierarchy->getParent(m_settingslayer) : nullptr;
}
@@ -497,11 +536,8 @@ float Settings::getFloat(const std::string &name) const
u64 Settings::getU64(const std::string &name) const
{
- u64 value = 0;
std::string s = get(name);
- std::istringstream ss(s);
- ss >> value;
- return value;
+ return from_string<u64>(s);
}
@@ -623,9 +659,7 @@ bool Settings::getNoiseParamsFromGroup(const std::string &name,
bool Settings::exists(const std::string &name) const
{
- MutexAutoLock lock(m_mutex);
-
- if (m_settings.find(name) != m_settings.end())
+ if (existsLocal(name))
return true;
if (auto parent = getParent())
return parent->exists(name);
@@ -633,6 +667,14 @@ bool Settings::exists(const std::string &name) const
}
+bool Settings::existsLocal(const std::string &name) const
+{
+ MutexAutoLock lock(m_mutex);
+
+ return m_settings.find(name) != m_settings.end();
+}
+
+
std::vector<std::string> Settings::getNames() const
{
MutexAutoLock lock(m_mutex);
@@ -715,6 +757,15 @@ bool Settings::getS16NoEx(const std::string &name, s16 &val) const
}
}
+bool Settings::getU32NoEx(const std::string &name, u32 &val) const
+{
+ try {
+ val = getU32(name);
+ return true;
+ } catch (SettingNotFoundException &e) {
+ return false;
+ }
+}
bool Settings::getS32NoEx(const std::string &name, s32 &val) const
{
@@ -823,6 +874,8 @@ bool Settings::set(const std::string &name, const std::string &value)
// TODO: Remove this function
bool Settings::setDefault(const std::string &name, const std::string &value)
{
+ FATAL_ERROR_IF(m_hierarchy != &g_hierarchy, "setDefault is only valid on "
+ "global settings");
return getLayer(SL_DEFAULTS)->set(name, value);
}
diff --git a/src/settings.h b/src/settings.h
index b5e859ee0..767d057f9 100644
--- a/src/settings.h
+++ b/src/settings.h
@@ -21,6 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "irrlichttypes_bloated.h"
#include "util/string.h"
+#include "util/basic_macros.h"
#include <string>
#include <list>
#include <set>
@@ -60,14 +61,36 @@ enum SettingsParseEvent {
SPE_MULTILINE,
};
+// Describes the global setting layers, SL_GLOBAL is where settings are read from
enum SettingsLayer {
SL_DEFAULTS,
SL_GAME,
SL_GLOBAL,
- SL_MAP,
SL_TOTAL_COUNT
};
+// Implements the hierarchy a settings object may be part of
+class SettingsHierarchy {
+public:
+ /*
+ * A settings object that may be part of another hierarchy can
+ * occupy the index 0 as a fallback. If not set you can use 0 on your own.
+ */
+ SettingsHierarchy(Settings *fallback = nullptr);
+
+ DISABLE_CLASS_COPY(SettingsHierarchy)
+
+ Settings *getLayer(int layer) const;
+
+private:
+ friend class Settings;
+ Settings *getParent(int layer) const;
+ void onLayerCreated(int layer, Settings *obj);
+ void onLayerRemoved(int layer);
+
+ std::vector<Settings*> layers;
+};
+
struct ValueSpec {
ValueSpec(ValueType a_type, const char *a_help=NULL)
{
@@ -100,13 +123,15 @@ typedef std::unordered_map<std::string, SettingsEntry> SettingEntries;
class Settings {
public:
+ /* These functions operate on the global hierarchy! */
static Settings *createLayer(SettingsLayer sl, const std::string &end_tag = "");
static Settings *getLayer(SettingsLayer sl);
- SettingsLayer getLayerType() const { return m_settingslayer; }
+ /**/
Settings(const std::string &end_tag = "") :
m_end_tag(end_tag)
{}
+ Settings(const std::string &end_tag, SettingsHierarchy *h, int settings_layer);
~Settings();
Settings & operator += (const Settings &other);
@@ -147,9 +172,12 @@ public:
bool getNoiseParamsFromValue(const std::string &name, NoiseParams &np) const;
bool getNoiseParamsFromGroup(const std::string &name, NoiseParams &np) const;
- // return all keys used
+ // return all keys used in this object
std::vector<std::string> getNames() const;
+ // check if setting exists anywhere in the hierarchy
bool exists(const std::string &name) const;
+ // check if setting exists in this object ("locally")
+ bool existsLocal(const std::string &name) const;
/***************************************
@@ -161,6 +189,7 @@ public:
bool getFlag(const std::string &name) const;
bool getU16NoEx(const std::string &name, u16 &val) const;
bool getS16NoEx(const std::string &name, s16 &val) const;
+ bool getU32NoEx(const std::string &name, u32 &val) const;
bool getS32NoEx(const std::string &name, s32 &val) const;
bool getU64NoEx(const std::string &name, u64 &val) const;
bool getFloatNoEx(const std::string &name, float &val) const;
@@ -200,9 +229,9 @@ public:
// remove a setting
bool remove(const std::string &name);
- /**************
- * Miscellany *
- **************/
+ /*****************
+ * Miscellaneous *
+ *****************/
void setDefault(const std::string &name, const FlagDesc *flagdesc, u32 flags);
const FlagDesc *getFlagDescFallback(const std::string &name) const;
@@ -214,6 +243,10 @@ public:
void removeSecureSettings();
+ // Returns the settings layer this object is.
+ // If within the global hierarchy you can cast this to enum SettingsLayer
+ inline int getLayer() const { return m_settingslayer; }
+
private:
/***********************
* Reading and writing *
@@ -239,6 +272,8 @@ private:
// Allow TestSettings to run sanity checks using private functions.
friend class TestSettings;
+ // For sane mutex locking when iterating
+ friend class LuaSettings;
void updateNoLock(const Settings &other);
void clearNoLock();
@@ -255,7 +290,8 @@ private:
// All methods that access m_settings/m_defaults directly should lock this.
mutable std::mutex m_mutex;
- static Settings *s_layers[SL_TOTAL_COUNT];
- SettingsLayer m_settingslayer = SL_TOTAL_COUNT;
+ SettingsHierarchy *m_hierarchy = nullptr;
+ int m_settingslayer = -1;
+
static std::unordered_map<std::string, const FlagDesc *> s_flags;
};
diff --git a/src/settings_translation_file.cpp b/src/settings_translation_file.cpp
index 317186e94..ebb7ba9be 100644
--- a/src/settings_translation_file.cpp
+++ b/src/settings_translation_file.cpp
@@ -11,7 +11,7 @@ fake_function() {
gettext("Pitch move mode");
gettext("If enabled, makes move directions relative to the player's pitch when flying or swimming.");
gettext("Fast movement");
- gettext("Fast movement (via the \"special\" key).\nThis requires the \"fast\" privilege on the server.");
+ gettext("Fast movement (via the \"Aux1\" key).\nThis requires the \"fast\" privilege on the server.");
gettext("Noclip");
gettext("If enabled together with fly mode, player is able to fly through solid nodes.\nThis requires the \"noclip\" privilege on the server.");
gettext("Cinematic mode");
@@ -24,12 +24,12 @@ fake_function() {
gettext("Invert vertical mouse movement.");
gettext("Mouse sensitivity");
gettext("Mouse sensitivity multiplier.");
- gettext("Special key for climbing/descending");
- gettext("If enabled, \"special\" key instead of \"sneak\" key is used for climbing down and\ndescending.");
+ gettext("Aux1 key for climbing/descending");
+ gettext("If enabled, \"Aux1\" key instead of \"Sneak\" key is used for climbing down and\ndescending.");
gettext("Double tap jump for fly");
gettext("Double-tapping the jump key toggles fly mode.");
gettext("Always fly and fast");
- gettext("If disabled, \"special\" key is used to fly fast if both fly and fast mode are\nenabled.");
+ gettext("If disabled, \"Aux1\" key is used to fly fast if both fly and fast mode are\nenabled.");
gettext("Place repetition interval");
gettext("The time in seconds it takes between repeated node placements when holding\nthe place button.");
gettext("Automatic jumping");
@@ -44,8 +44,8 @@ fake_function() {
gettext("The length in pixels it takes for touch screen interaction to start.");
gettext("Fixed virtual joystick");
gettext("(Android) Fixes the position of virtual joystick.\nIf disabled, virtual joystick will center to first-touch's position.");
- gettext("Virtual joystick triggers aux button");
- gettext("(Android) Use virtual joystick to trigger \"aux\" button.\nIf enabled, virtual joystick will also tap \"aux\" button when out of main circle.");
+ gettext("Virtual joystick triggers Aux1 button");
+ gettext("(Android) Use virtual joystick to trigger \"Aux1\" button.\nIf enabled, virtual joystick will also tap \"Aux1\" button when out of main circle.");
gettext("Enable joysticks");
gettext("Enable joysticks");
gettext("Joystick ID");
@@ -54,10 +54,10 @@ fake_function() {
gettext("The type of joystick");
gettext("Joystick button repetition interval");
gettext("The time in seconds it takes between repeated events\nwhen holding down a joystick button combination.");
- gettext("Joystick deadzone");
- gettext("The deadzone of the joystick");
+ gettext("Joystick dead zone");
+ gettext("The dead zone of the joystick");
gettext("Joystick frustum sensitivity");
- gettext("The sensitivity of the joystick axes for moving the\ningame view frustum around.");
+ gettext("The sensitivity of the joystick axes for moving the\nin-game view frustum around.");
gettext("Forward key");
gettext("Key for moving the player forward.\nSee http://irrlicht.sourceforge.net/docu/namespaceirr.html#a54da2a0e231901735e3da1b0edf72eb3");
gettext("Backward key");
@@ -76,7 +76,7 @@ fake_function() {
gettext("Key for placing.\nSee http://irrlicht.sourceforge.net/docu/namespaceirr.html#a54da2a0e231901735e3da1b0edf72eb3");
gettext("Inventory key");
gettext("Key for opening the inventory.\nSee http://irrlicht.sourceforge.net/docu/namespaceirr.html#a54da2a0e231901735e3da1b0edf72eb3");
- gettext("Special key");
+ gettext("Aux1 key");
gettext("Key for moving fast in fast mode.\nSee http://irrlicht.sourceforge.net/docu/namespaceirr.html#a54da2a0e231901735e3da1b0edf72eb3");
gettext("Chat key");
gettext("Key for opening the chat window.\nSee http://irrlicht.sourceforge.net/docu/namespaceirr.html#a54da2a0e231901735e3da1b0edf72eb3");
@@ -203,8 +203,8 @@ fake_function() {
gettext("Graphics");
gettext("In-Game");
gettext("Basic");
- gettext("Show nametag backgrounds by default");
- gettext("Whether nametag backgrounds should be shown by default.\nMods may still set a background.");
+ gettext("Show name tag backgrounds by default");
+ gettext("Whether name tag backgrounds should be shown by default.\nMods may still set a background.");
gettext("VBO");
gettext("Enable vertex buffer objects.\nThis should greatly improve graphics performance.");
gettext("Fog");
@@ -215,6 +215,8 @@ fake_function() {
gettext("Connects glass if supported by node.");
gettext("Smooth lighting");
gettext("Enable smooth lighting with simple ambient occlusion.\nDisable for speed or for different looks.");
+ gettext("Tradeoffs for performance");
+ gettext("Enables tradeoffs that reduce CPU load or increase rendering performance\nat the expense of minor visual glitches that do not impact game playability.");
gettext("Clouds");
gettext("Clouds are a client side effect.");
gettext("3D clouds");
@@ -225,7 +227,7 @@ fake_function() {
gettext("Adds particles when digging a node.");
gettext("Filtering");
gettext("Mipmapping");
- gettext("Use mip mapping to scale textures. May slightly increase performance,\nespecially when using a high resolution texture pack.\nGamma correct downscaling is not supported.");
+ gettext("Use mipmapping to scale textures. May slightly increase performance,\nespecially when using a high resolution texture pack.\nGamma correct downscaling is not supported.");
gettext("Anisotropic filtering");
gettext("Use anisotropic filtering when viewing at textures from an angle.");
gettext("Bilinear filtering");
@@ -233,9 +235,9 @@ fake_function() {
gettext("Trilinear filtering");
gettext("Use trilinear filtering when scaling textures.");
gettext("Clean transparent textures");
- gettext("Filtered textures can blend RGB values with fully-transparent neighbors,\nwhich PNG optimizers usually discard, sometimes resulting in a dark or\nlight edge to transparent textures. Apply this filter to clean that up\nat texture load time.");
+ gettext("Filtered textures can blend RGB values with fully-transparent neighbors,\nwhich PNG optimizers usually discard, often resulting in dark or\nlight edges to transparent textures. Apply a filter to clean that up\nat texture load time. This is automatically enabled if mipmapping is enabled.");
gettext("Minimum texture size");
- gettext("When using bilinear/trilinear/anisotropic filters, low-resolution textures\ncan be blurred, so automatically upscale them with nearest-neighbor\ninterpolation to preserve crisp pixels. This sets the minimum texture size\nfor the upscaled textures; higher values look sharper, but require more\nmemory. Powers of 2 are recommended. Setting this higher than 1 may not\nhave a visible effect unless bilinear/trilinear/anisotropic filtering is\nenabled.\nThis is also used as the base node texture size for world-aligned\ntexture autoscaling.");
+ gettext("When using bilinear/trilinear/anisotropic filters, low-resolution textures\ncan be blurred, so automatically upscale them with nearest-neighbor\ninterpolation to preserve crisp pixels. This sets the minimum texture size\nfor the upscaled textures; higher values look sharper, but require more\nmemory. Powers of 2 are recommended. This setting is ONLY applied if\nbilinear/trilinear/anisotropic filtering is enabled.\nThis is also used as the base node texture size for world-aligned\ntexture autoscaling.");
gettext("FSAA");
gettext("Use multi-sample antialiasing (MSAA) to smooth out block edges.\nThis algorithm smooths out the 3D viewport while keeping the image sharp,\nbut it doesn't affect the insides of textures\n(which is especially noticeable with transparent textures).\nVisible spaces appear between nodes when shaders are disabled.\nIf set to 0, MSAA is disabled.\nA restart is required after changing this option.");
gettext("Undersampling");
@@ -261,6 +263,29 @@ fake_function() {
gettext("Set to true to enable waving leaves.\nRequires shaders to be enabled.");
gettext("Waving plants");
gettext("Set to true to enable waving plants.\nRequires shaders to be enabled.");
+ gettext("Dynamic shadows");
+ gettext("Dynamic shadows");
+ gettext("Set to true to enable Shadow Mapping.\nRequires shaders to be enabled.");
+ gettext("Shadow strength");
+ gettext("Set the shadow strength.\nLower value means lighter shadows, higher value means darker shadows.");
+ gettext("Shadow map max distance in nodes to render shadows");
+ gettext("Maximum distance to render shadows.");
+ gettext("Shadow map texture size");
+ gettext("Texture size to render the shadow map on.\nThis must be a power of two.\nBigger numbers create better shadows but it is also more expensive.");
+ gettext("Shadow map texture in 32 bits");
+ gettext("Sets shadow texture quality to 32 bits.\nOn false, 16 bits texture will be used.\nThis can cause much more artifacts in the shadow.");
+ gettext("Poisson filtering");
+ gettext("Enable Poisson disk filtering.\nOn true uses Poisson disk to make \"soft shadows\". Otherwise uses PCF filtering.");
+ gettext("Shadow filter quality");
+ gettext("Define shadow filtering quality.\nThis simulates the soft shadows effect by applying a PCF or Poisson disk\nbut also uses more resources.");
+ gettext("Colored shadows");
+ gettext("Enable colored shadows.\nOn true translucent nodes cast colored shadows. This is expensive.");
+ gettext("Map shadows update frames");
+ gettext("Spread a complete update of shadow map over given amount of frames.\nHigher values might make shadows laggy, lower values\nwill consume more resources.\nMinimum value: 1; maximum value: 16");
+ gettext("Soft shadow radius");
+ gettext("Set the soft shadow radius size.\nLower values mean sharper shadows, bigger values mean softer shadows.\nMinimum value: 1.0; maximum value: 10.0");
+ gettext("Sky Body Orbit Tilt");
+ gettext("Set the tilt of Sun/Moon orbit in degrees.\nValue of 0 means no tilt / vertical orbit.\nMinimum value: 0.0; maximum value: 60.0");
gettext("Advanced");
gettext("Arm inertia");
gettext("Arm inertia, gives a more realistic movement of\nthe arm when the camera moves.");
@@ -275,15 +300,13 @@ fake_function() {
gettext("Near plane");
gettext("Camera 'near clipping plane' distance in nodes, between 0 and 0.25\nOnly works on GLES platforms. Most users will not need to change this.\nIncreasing can reduce artifacting on weaker GPUs.\n0.1 = Default, 0.25 = Good value for weaker tablets.");
gettext("Screen width");
- gettext("Width component of the initial window size.");
+ gettext("Width component of the initial window size. Ignored in fullscreen mode.");
gettext("Screen height");
- gettext("Height component of the initial window size.");
+ gettext("Height component of the initial window size. Ignored in fullscreen mode.");
gettext("Autosave screen size");
gettext("Save window size automatically when modified.");
gettext("Full screen");
gettext("Fullscreen mode.");
- gettext("Full screen BPP");
- gettext("Bits per pixel (aka color depth) in fullscreen mode.");
gettext("VSync");
gettext("Vertical screen synchronization.");
gettext("Field of view");
@@ -303,7 +326,7 @@ fake_function() {
gettext("Texture path");
gettext("Path to texture directory. All textures are first searched from here.");
gettext("Video driver");
- gettext("The rendering back-end for Irrlicht.\nA restart is required after changing this.\nNote: On Android, stick with OGLES1 if unsure! App may fail to start otherwise.\nOn other platforms, OpenGL is recommended.\nShaders are supported by OpenGL (desktop only) and OGLES2 (experimental)");
+ gettext("The rendering back-end.\nA restart is required after changing this.\nNote: On Android, stick with OGLES1 if unsure! App may fail to start otherwise.\nOn other platforms, OpenGL is recommended.\nShaders are supported by OpenGL (desktop only) and OGLES2 (experimental)");
gettext("Cloud radius");
gettext("Radius of cloud area stated in number of 64 node cloud squares.\nValues larger than 26 will start to produce sharp cutoffs at cloud area corners.");
gettext("View bobbing factor");
@@ -335,7 +358,7 @@ fake_function() {
gettext("Crosshair color");
gettext("Crosshair color (R,G,B).\nAlso controls the object crosshair color");
gettext("Crosshair alpha");
- gettext("Crosshair alpha (opaqueness, between 0 and 255).\nAlso controls the object crosshair color");
+ gettext("Crosshair alpha (opaqueness, between 0 and 255).\nThis also applies to the object crosshair.");
gettext("Recent Chat Messages");
gettext("Maximum number of recent chat messages to show");
gettext("Desynchronize block animation");
@@ -343,7 +366,7 @@ fake_function() {
gettext("Maximum hotbar width");
gettext("Maximum proportion of current window to be used for hotbar.\nUseful if there's something to be displayed right or left of hotbar.");
gettext("HUD scale factor");
- gettext("Modifies the size of the hudbar elements.");
+ gettext("Modifies the size of the HUD elements.");
gettext("Mesh cache");
gettext("Enables caching of facedir rotated meshes.");
gettext("Mapblock mesh generation delay");
@@ -385,8 +408,6 @@ fake_function() {
gettext("Delay showing tooltips, stated in milliseconds.");
gettext("Append item name");
gettext("Append item name to tooltip.");
- gettext("FreeType fonts");
- gettext("Whether FreeType fonts are used, requires FreeType support to be compiled in.\nIf disabled, bitmap and XML vectors fonts are used instead.");
gettext("Font bold by default");
gettext("Font italic by default");
gettext("Font shadow");
@@ -394,27 +415,25 @@ fake_function() {
gettext("Font shadow alpha");
gettext("Opaqueness (alpha) of the shadow behind the default font, between 0 and 255.");
gettext("Font size");
- gettext("Font size of the default font in point (pt).");
+ gettext("Font size of the default font where 1 unit = 1 pixel at 96 DPI");
+ gettext("Font size divisible by");
+ gettext("For pixel-style fonts that do not scale well, this ensures that font sizes used\nwith this font will always be divisible by this value, in pixels. For instance,\na pixel font 16 pixels tall should have this set to 16, so it will only ever be\nsized 16, 32, 48, etc., so a mod requesting a size of 25 will get 32.");
gettext("Regular font path");
- gettext("Path to the default font.\nIf “freetype” setting is enabled: Must be a TrueType font.\nIf “freetype” setting is disabled: Must be a bitmap or XML vectors font.\nThe fallback font will be used if the font cannot be loaded.");
+ gettext("Path to the default font. Must be a TrueType font.\nThe fallback font will be used if the font cannot be loaded.");
gettext("Bold font path");
gettext("Italic font path");
gettext("Bold and italic font path");
gettext("Monospace font size");
- gettext("Font size of the monospace font in point (pt).");
+ gettext("Font size of the monospace font where 1 unit = 1 pixel at 96 DPI");
+ gettext("Monospace font size divisible by");
+ gettext("For pixel-style fonts that do not scale well, this ensures that font sizes used\nwith this font will always be divisible by this value, in pixels. For instance,\na pixel font 16 pixels tall should have this set to 16, so it will only ever be\nsized 16, 32, 48, etc., so a mod requesting a size of 25 will get 32.");
gettext("Monospace font path");
- gettext("Path to the monospace font.\nIf “freetype” setting is enabled: Must be a TrueType font.\nIf “freetype” setting is disabled: Must be a bitmap or XML vectors font.\nThis font is used for e.g. the console and profiler screen.");
+ gettext("Path to the monospace font. Must be a TrueType font.\nThis font is used for e.g. the console and profiler screen.");
gettext("Bold monospace font path");
gettext("Italic monospace font path");
gettext("Bold and italic monospace font path");
- gettext("Fallback font size");
- gettext("Font size of the fallback font in point (pt).");
- gettext("Fallback font shadow");
- gettext("Shadow offset (in pixels) of the fallback font. If 0, then shadow will not be drawn.");
- gettext("Fallback font shadow alpha");
- gettext("Opaqueness (alpha) of the shadow behind the fallback font, between 0 and 255.");
gettext("Fallback font path");
- gettext("Path of the fallback font.\nIf “freetype” setting is enabled: Must be a TrueType font.\nIf “freetype” setting is disabled: Must be a bitmap or XML vectors font.\nThis font will be used for certain languages or if the default font is unavailable.");
+ gettext("Path of the fallback font. Must be a TrueType font.\nThis font will be used for certain languages or if the default font is unavailable.");
gettext("Chat font size");
gettext("Font size of the recent chat text and chat prompt in point (pt).\nValue 0 will use the default font size.");
gettext("Screenshot folder");
@@ -426,6 +445,8 @@ fake_function() {
gettext("Advanced");
gettext("DPI");
gettext("Adjust dpi configuration to your screen (non X11/Android only) e.g. for 4k screens.");
+ gettext("Display Density Scaling Factor");
+ gettext("Adjust the detected display density, used for scaling UI elements.");
gettext("Enable console window");
gettext("Windows systems only: Start Minetest with the command line window in the background.\nContains the same information as the file debug.txt (default name).");
gettext("Sound");
@@ -436,13 +457,17 @@ fake_function() {
gettext("Mute sound");
gettext("Whether to mute sounds. You can unmute sounds at any time, unless the\nsound system is disabled (enable_sound=false).\nIn-game, you can toggle the mute state with the mute key or by using the\npause menu.");
gettext("Client");
+ gettext("Chat weblinks");
+ gettext("Clickable weblinks (middle-click or Ctrl+left-click) enabled in chat console output.");
+ gettext("Weblink color");
+ gettext("Optional override for chat weblink color.");
gettext("Network");
gettext("Server address");
gettext("Address to connect to.\nLeave this blank to start a local server.\nNote that the address field in the main menu overrides this setting.");
gettext("Remote port");
gettext("Port to connect to (UDP).\nNote that the port field in the main menu overrides this setting.");
gettext("Prometheus listener address");
- gettext("Prometheus listener address.\nIf minetest is compiled with ENABLE_PROMETHEUS option enabled,\nenable metrics listener for Prometheus on that address.\nMetrics can be fetch on http://127.0.0.1:30000/metrics");
+ gettext("Prometheus listener address.\nIf Minetest is compiled with ENABLE_PROMETHEUS option enabled,\nenable metrics listener for Prometheus on that address.\nMetrics can be fetched on http://127.0.0.1:30000/metrics");
gettext("Saving map received from server");
gettext("Save the map received by the client on disk.");
gettext("Connect to external media server");
@@ -498,7 +523,7 @@ fake_function() {
gettext("Max. packets per iteration");
gettext("Maximum number of packets sent per send step, if you have a slow connection\ntry reducing it, but don't reduce it to a number below double of targeted\nclient number.");
gettext("Map Compression Level for Network Transfer");
- gettext("ZLib compression level to use when sending mapblocks to the client.\n-1 - Zlib's default compression level\n0 - no compresson, fastest\n9 - best compression, slowest\n(levels 1-3 use Zlib's \"fast\" method, 4-9 use the normal method)");
+ gettext("Compression level to use when sending mapblocks to the client.\n-1 - use default compression level\n0 - least compression, fastest\n9 - best compression, slowest");
gettext("Game");
gettext("Default game");
gettext("Default game when creating a new world.\nThis will be overridden when creating a world from the main menu.");
@@ -542,6 +567,8 @@ fake_function() {
gettext("If enabled, actions are recorded for rollback.\nThis option is only read when server starts.");
gettext("Chat message format");
gettext("Format of player chat messages. The following strings are valid placeholders:\n@name, @message, @timestamp (optional)");
+ gettext("Chat command time message threshold");
+ gettext("If the execution of a chat command takes longer than this specified time in\nseconds, add the time information to the chat command message");
gettext("Shutdown message");
gettext("A message to be displayed to all clients when the server shuts down.");
gettext("Crash message");
@@ -599,7 +626,7 @@ fake_function() {
gettext("Deprecated Lua API handling");
gettext("Handling for deprecated Lua API calls:\n- none: Do not log deprecated calls\n- log: mimic and log backtrace of deprecated call (default).\n- error: abort on usage of deprecated call (suggested for mod developers).");
gettext("Max. clearobjects extra blocks");
- gettext("Number of extra blocks that can be loaded by /clearobjects at once.\nThis is a trade-off between sqlite transaction overhead and\nmemory consumption (4096=100MB, as a rule of thumb).");
+ gettext("Number of extra blocks that can be loaded by /clearobjects at once.\nThis is a trade-off between SQLite transaction overhead and\nmemory consumption (4096=100MB, as a rule of thumb).");
gettext("Unload unused server data");
gettext("How much the server will wait before unloading unused mapblocks.\nHigher value is smoother, but will use more RAM.");
gettext("Maximum objects per block");
@@ -607,7 +634,7 @@ fake_function() {
gettext("Synchronous SQLite");
gettext("See https://www.sqlite.org/pragma.html#pragma_synchronous");
gettext("Map Compression Level for Disk Storage");
- gettext("ZLib compression level to use when saving mapblocks to disk.\n-1 - Zlib's default compression level\n0 - no compresson, fastest\n9 - best compression, slowest\n(levels 1-3 use Zlib's \"fast\" method, 4-9 use the normal method)");
+ gettext("Compression level to use when saving mapblocks to disk.\n-1 - use default compression level\n0 - least compression, fastest\n9 - best compression, slowest");
gettext("Dedicated server step");
gettext("Length of a server tick and the interval at which objects are generally updated over\nnetwork.");
gettext("Active block management interval");
@@ -656,8 +683,8 @@ fake_function() {
gettext("Instrument the action function of Active Block Modifiers on registration.");
gettext("Loading Block Modifiers");
gettext("Instrument the action function of Loading Block Modifiers on registration.");
- gettext("Chatcommands");
- gettext("Instrument chatcommands on registration.");
+ gettext("Chat commands");
+ gettext("Instrument chat commands on registration.");
gettext("Global callbacks");
gettext("Instrument global callback functions on registration.\n(anything you pass to a minetest.register_*() function)");
gettext("Advanced");
@@ -679,14 +706,12 @@ fake_function() {
gettext("IPv6");
gettext("Enable IPv6 support (for both client and server).\nRequired for IPv6 connections to work at all.");
gettext("Advanced");
- gettext("cURL timeout");
- gettext("Default timeout for cURL, stated in milliseconds.\nOnly has an effect if compiled with cURL.");
+ gettext("cURL interactive timeout");
+ gettext("Maximum time an interactive request (e.g. server list fetch) may take, stated in milliseconds.");
gettext("cURL parallel limit");
gettext("Limits number of parallel HTTP requests. Affects:\n- Media fetch if server uses remote_media setting.\n- Serverlist download and server announcement.\n- Downloads performed by main menu (e.g. mod manager).\nOnly has an effect if compiled with cURL.");
gettext("cURL file download timeout");
- gettext("Maximum time in ms a file download (e.g. a mod download) may take.");
- gettext("High-precision FPU");
- gettext("Makes DirectX work with LuaJIT. Disable if it causes troubles.");
+ gettext("Maximum time a file download (e.g. a mod download) may take, stated in milliseconds.");
gettext("Main menu script");
gettext("Replaces the default main menu with a custom one.");
gettext("Engine profiling data print interval");
@@ -701,7 +726,7 @@ fake_function() {
gettext("Map generation limit");
gettext("Limit of map generation, in nodes, in all 6 directions from (0, 0, 0).\nOnly mapchunks completely within the mapgen limit are generated.\nValue is stored per-world.");
gettext("Mapgen flags");
- gettext("Global map generation attributes.\nIn Mapgen v6 the 'decorations' flag controls all decorations except trees\nand junglegrass, in all other mapgens this flag controls all decorations.");
+ gettext("Global map generation attributes.\nIn Mapgen v6 the 'decorations' flag controls all decorations except trees\nand jungle grass, in all other mapgens this flag controls all decorations.");
gettext("Biome API temperature and humidity noise parameters");
gettext("Heat noise");
gettext("Temperature variation for biomes.");
diff --git a/src/skyparams.h b/src/skyparams.h
index 1de494d69..f7f694427 100644
--- a/src/skyparams.h
+++ b/src/skyparams.h
@@ -68,11 +68,36 @@ struct StarParams
f32 scale;
};
+struct CloudParams
+{
+ float density;
+ video::SColor color_bright;
+ video::SColor color_ambient;
+ float thickness;
+ float height;
+ v2f speed;
+};
+
// Utility class for setting default sky, sun, moon, stars values:
class SkyboxDefaults
{
public:
- const SkyColor getSkyColorDefaults()
+ SkyboxDefaults() = delete;
+
+ static const SkyboxParams getSkyDefaults()
+ {
+ SkyboxParams sky;
+ sky.bgcolor = video::SColor(255, 255, 255, 255);
+ sky.type = "regular";
+ sky.clouds = true;
+ sky.sky_color = getSkyColorDefaults();
+ sky.fog_sun_tint = video::SColor(255, 244, 125, 29);
+ sky.fog_moon_tint = video::SColorf(0.5, 0.6, 0.8, 1).toSColor();
+ sky.fog_tint_type = "default";
+ return sky;
+ }
+
+ static const SkyColor getSkyColorDefaults()
{
SkyColor sky;
// Horizon colors
@@ -87,7 +112,7 @@ public:
return sky;
}
- const SunParams getSunDefaults()
+ static const SunParams getSunDefaults()
{
SunParams sun;
sun.visible = true;
@@ -99,7 +124,7 @@ public:
return sun;
}
- const MoonParams getMoonDefaults()
+ static const MoonParams getMoonDefaults()
{
MoonParams moon;
moon.visible = true;
@@ -109,7 +134,7 @@ public:
return moon;
}
- const StarParams getStarDefaults()
+ static const StarParams getStarDefaults()
{
StarParams stars;
stars.visible = true;
@@ -118,4 +143,16 @@ public:
stars.scale = 1;
return stars;
}
+
+ static const CloudParams getCloudDefaults()
+ {
+ CloudParams clouds;
+ clouds.density = 0.4f;
+ clouds.color_bright = video::SColor(229, 240, 240, 255);
+ clouds.color_ambient = video::SColor(255, 0, 0, 0);
+ clouds.thickness = 16.0f;
+ clouds.height = 120;
+ clouds.speed = v2f(0.0f, -2.0f);
+ return clouds;
+ }
};
diff --git a/src/staticobject.cpp b/src/staticobject.cpp
index 86e455b9f..1160ec68f 100644
--- a/src/staticobject.cpp
+++ b/src/staticobject.cpp
@@ -37,6 +37,7 @@ void StaticObject::serialize(std::ostream &os)
// data
os<<serializeString16(data);
}
+
void StaticObject::deSerialize(std::istream &is, u8 version)
{
// type
@@ -49,6 +50,29 @@ void StaticObject::deSerialize(std::istream &is, u8 version)
void StaticObjectList::serialize(std::ostream &os)
{
+ // Check for problems first
+ auto problematic = [] (StaticObject &obj) -> bool {
+ if (obj.data.size() > U16_MAX) {
+ errorstream << "StaticObjectList::serialize(): "
+ "object has excessive static data (" << obj.data.size() <<
+ "), deleting it." << std::endl;
+ return true;
+ }
+ return false;
+ };
+ for (auto it = m_stored.begin(); it != m_stored.end(); ) {
+ if (problematic(*it))
+ it = m_stored.erase(it);
+ else
+ it++;
+ }
+ for (auto it = m_active.begin(); it != m_active.end(); ) {
+ if (problematic(it->second))
+ it = m_active.erase(it);
+ else
+ it++;
+ }
+
// version
u8 version = 0;
writeU8(os, version);
diff --git a/src/tool.cpp b/src/tool.cpp
index 90f4f9c12..075c6b3c5 100644
--- a/src/tool.cpp
+++ b/src/tool.cpp
@@ -23,6 +23,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "log.h"
#include "inventory.h"
#include "exceptions.h"
+#include "convert_json.h"
#include "util/serialize.h"
#include "util/numeric.h"
@@ -142,7 +143,7 @@ void ToolCapabilities::serializeJson(std::ostream &os) const
}
root["damage_groups"] = damage_groups_object;
- os << root;
+ fastWriteJson(root, os);
}
void ToolCapabilities::deserializeJson(std::istream &is)
@@ -182,9 +183,74 @@ void ToolCapabilities::deserializeJson(std::istream &is)
}
}
+static u32 calculateResultWear(const u32 uses, const u16 initial_wear)
+{
+ if (uses == 0) {
+ // Trivial case: Infinite uses
+ return 0;
+ }
+ /* Finite uses. This is not trivial,
+ as the maximum wear is not neatly evenly divisible by
+ most possible uses numbers. For example, for 128
+ uses, the calculation of wear is trivial, as
+ 65536 / 128 uses = 512 wear,
+ so the tool will get 512 wear 128 times in its lifetime.
+ But for a number like 130, this does not work:
+ 65536 / 130 uses = 504.123... wear.
+ Since wear must be an integer, we will get
+ 504*130 = 65520, which would lead to the wrong number
+ of uses.
+
+ Instead, we partition the "wear range" into blocks:
+ A block represents a single use and can be
+ of two possible sizes: normal and oversized.
+ A normal block is equal to floor(65536 / uses).
+ An oversized block is a normal block plus 1.
+ Then we determine how many oversized and normal
+ blocks we need and finally, whether we add
+ the normal wear or the oversized wear.
+
+ Example for 130 uses:
+ * Normal wear = 504
+ * Number of normal blocks = 114
+ * Oversized wear = 505
+ * Number of oversized blocks = 16
+
+ If we add everything together, we get:
+ 114*504 + 16*505 = 65536
+ */
+ u32 result_wear;
+ u32 wear_normal = ((U16_MAX+1) / uses);
+ // Will be non-zero if its not evenly divisible
+ u16 blocks_oversize = (U16_MAX+1) % uses;
+ // Whether to add one extra wear point in case
+ // of oversized wear.
+ u16 wear_extra = 0;
+ if (blocks_oversize > 0) {
+ u16 blocks_normal = uses - blocks_oversize;
+ /* When the wear has reached this value, we
+ know that wear_normal has been applied
+ for blocks_normal times, therefore,
+ only oversized blocks remain.
+ This also implies the raw tool wear number
+ increases a bit faster after this point,
+ but this should be barely noticable by the
+ player.
+ */
+ u16 wear_extra_at = blocks_normal * wear_normal;
+ if (initial_wear >= wear_extra_at) {
+ wear_extra = 1;
+ }
+ }
+ result_wear = wear_normal + wear_extra;
+ return result_wear;
+}
+
DigParams getDigParams(const ItemGroupList &groups,
- const ToolCapabilities *tp)
+ const ToolCapabilities *tp,
+ const u16 initial_wear)
{
+
// Group dig_immediate defaults to fixed time and no wear
if (tp->groupcaps.find("dig_immediate") == tp->groupcaps.cend()) {
switch (itemgroup_get(groups, "dig_immediate")) {
@@ -200,7 +266,7 @@ DigParams getDigParams(const ItemGroupList &groups,
// Values to be returned (with a bit of conversion)
bool result_diggable = false;
float result_time = 0.0;
- float result_wear = 0.0;
+ u32 result_wear = 0;
std::string result_main_group;
int level = itemgroup_get(groups, "level");
@@ -223,22 +289,24 @@ DigParams getDigParams(const ItemGroupList &groups,
if (!result_diggable || time < result_time) {
result_time = time;
result_diggable = true;
- if (cap.uses != 0)
- result_wear = 1.0 / cap.uses / pow(3.0, leveldiff);
- else
- result_wear = 0;
+ // The actual number of uses increases
+ // exponentially with leveldiff.
+ // If the levels are equal, real_uses equals cap.uses.
+ u32 real_uses = cap.uses * pow(3.0, leveldiff);
+ real_uses = MYMIN(real_uses, U16_MAX);
+ result_wear = calculateResultWear(real_uses, initial_wear);
result_main_group = groupname;
}
}
- u16 wear_i = U16_MAX * result_wear;
- return DigParams(result_diggable, result_time, wear_i, result_main_group);
+ return DigParams(result_diggable, result_time, result_wear, result_main_group);
}
HitParams getHitParams(const ItemGroupList &armor_groups,
- const ToolCapabilities *tp, float time_from_last_punch)
+ const ToolCapabilities *tp, float time_from_last_punch,
+ u16 initial_wear)
{
- s16 damage = 0;
+ s32 damage = 0;
float result_wear = 0.0f;
float punch_interval_multiplier =
rangelim(time_from_last_punch / tp->full_punch_interval, 0.0f, 1.0f);
@@ -248,10 +316,14 @@ HitParams getHitParams(const ItemGroupList &armor_groups,
damage += damageGroup.second * punch_interval_multiplier * armor / 100.0;
}
- if (tp->punch_attack_uses > 0)
- result_wear = 1.0f / tp->punch_attack_uses * punch_interval_multiplier;
+ if (tp->punch_attack_uses > 0) {
+ result_wear = calculateResultWear(tp->punch_attack_uses, initial_wear);
+ result_wear *= punch_interval_multiplier;
+ }
+ // Keep damage in sane bounds for simplicity
+ damage = rangelim(damage, -U16_MAX, U16_MAX);
- u16 wear_i = U16_MAX * result_wear;
+ u32 wear_i = (u32) result_wear;
return {damage, wear_i};
}
@@ -265,7 +337,8 @@ PunchDamageResult getPunchDamage(
const ItemGroupList &armor_groups,
const ToolCapabilities *toolcap,
const ItemStack *punchitem,
- float time_from_last_punch
+ float time_from_last_punch,
+ u16 initial_wear
){
bool do_hit = true;
{
@@ -285,7 +358,8 @@ PunchDamageResult getPunchDamage(
if(do_hit)
{
HitParams hitparams = getHitParams(armor_groups, toolcap,
- time_from_last_punch);
+ time_from_last_punch,
+ punchitem->wear);
result.did_punch = true;
result.wear = hitparams.wear;
result.damage = hitparams.hp;
diff --git a/src/tool.h b/src/tool.h
index 59dd501f5..8409f59af 100644
--- a/src/tool.h
+++ b/src/tool.h
@@ -88,10 +88,10 @@ struct DigParams
// Digging time in seconds
float time;
// Caused wear
- u16 wear;
+ u32 wear; // u32 because wear could be 65536 (single-use tool)
std::string main_group;
- DigParams(bool a_diggable = false, float a_time = 0.0f, u16 a_wear = 0,
+ DigParams(bool a_diggable = false, float a_time = 0.0f, u32 a_wear = 0,
const std::string &a_main_group = ""):
diggable(a_diggable),
time(a_time),
@@ -101,21 +101,24 @@ struct DigParams
};
DigParams getDigParams(const ItemGroupList &groups,
- const ToolCapabilities *tp);
+ const ToolCapabilities *tp,
+ const u16 initial_wear = 0);
struct HitParams
{
- s16 hp;
- u16 wear;
+ s32 hp;
+ // Caused wear
+ u32 wear; // u32 because wear could be 65536 (single-use weapon)
- HitParams(s16 hp_ = 0, u16 wear_ = 0):
+ HitParams(s32 hp_ = 0, u32 wear_ = 0):
hp(hp_),
wear(wear_)
{}
};
HitParams getHitParams(const ItemGroupList &armor_groups,
- const ToolCapabilities *tp, float time_from_last_punch);
+ const ToolCapabilities *tp, float time_from_last_punch,
+ u16 initial_wear = 0);
HitParams getHitParams(const ItemGroupList &armor_groups,
const ToolCapabilities *tp);
@@ -135,7 +138,8 @@ PunchDamageResult getPunchDamage(
const ItemGroupList &armor_groups,
const ToolCapabilities *toolcap,
const ItemStack *punchitem,
- float time_from_last_punch
+ float time_from_last_punch,
+ u16 initial_wear = 0
);
f32 getToolRange(const ItemDefinition &def_selected, const ItemDefinition &def_hand);
diff --git a/src/translation.cpp b/src/translation.cpp
index 55c958fa2..1e43b0894 100644
--- a/src/translation.cpp
+++ b/src/translation.cpp
@@ -64,7 +64,13 @@ void Translations::loadTranslation(const std::string &data)
line.resize(line.length() - 1);
if (str_starts_with(line, "# textdomain:")) {
- textdomain = utf8_to_wide(trim(str_split(line, ':')[1]));
+ auto parts = str_split(line, ':');
+ if (parts.size() < 2) {
+ errorstream << "Invalid textdomain translation line \"" << line
+ << "\"" << std::endl;
+ continue;
+ }
+ textdomain = utf8_to_wide(trim(parts[1]));
}
if (line.empty() || line[0] == '#')
continue;
diff --git a/src/unittest/CMakeLists.txt b/src/unittest/CMakeLists.txt
index 5703b8906..92f31ecac 100644
--- a/src/unittest/CMakeLists.txt
+++ b/src/unittest/CMakeLists.txt
@@ -11,14 +11,15 @@ set (UNITTEST_SRCS
${CMAKE_CURRENT_SOURCE_DIR}/test_filepath.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_inventory.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_irrptr.cpp
+ ${CMAKE_CURRENT_SOURCE_DIR}/test_map.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_map_settings_manager.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_mapnode.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_modchannels.cpp
+ ${CMAKE_CURRENT_SOURCE_DIR}/test_modmetadatadatabase.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_nodedef.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_noderesolver.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_noise.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_objdef.cpp
- ${CMAKE_CURRENT_SOURCE_DIR}/test_player.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_profiler.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_random.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_schematic.cpp
@@ -33,6 +34,7 @@ set (UNITTEST_SRCS
${CMAKE_CURRENT_SOURCE_DIR}/test_voxelarea.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_voxelalgorithms.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_voxelmanipulator.cpp
+ ${CMAKE_CURRENT_SOURCE_DIR}/test_gettext.cpp
PARENT_SCOPE)
set (UNITTEST_CLIENT_SRCS
@@ -44,6 +46,7 @@ set (UNITTEST_CLIENT_SRCS
set (TEST_WORLDDIR ${CMAKE_CURRENT_SOURCE_DIR}/test_world)
set (TEST_SUBGAME_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../../games/devtest)
+set (TEST_MOD_PATH ${CMAKE_CURRENT_SOURCE_DIR}/test_mod)
configure_file(
"${CMAKE_CURRENT_SOURCE_DIR}/test_config.h.in"
diff --git a/src/unittest/test.cpp b/src/unittest/test.cpp
index af324e1b1..f223d567e 100644
--- a/src/unittest/test.cpp
+++ b/src/unittest/test.cpp
@@ -25,6 +25,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "gamedef.h"
#include "modchannels.h"
#include "content/mods.h"
+#include "database/database-dummy.h"
#include "util/numeric.h"
#include "porting.h"
@@ -55,6 +56,7 @@ public:
scene::ISceneManager *getSceneManager() { return m_scenemgr; }
IRollbackManager *getRollbackManager() { return m_rollbackmgr; }
EmergeManager *getEmergeManager() { return m_emergemgr; }
+ ModMetadataDatabase *getModStorageDatabase() { return m_mod_storage_database; }
scene::IAnimatedMesh *getMesh(const std::string &filename) { return NULL; }
bool checkLocalPrivilege(const std::string &priv) { return false; }
@@ -68,7 +70,6 @@ public:
return testmodspec;
}
virtual const ModSpec* getModSpec(const std::string &modname) const { return NULL; }
- virtual std::string getModStoragePath() const { return "."; }
virtual bool registerModStorage(ModMetadata *meta) { return true; }
virtual void unregisterModStorage(const std::string &name) {}
bool joinModChannel(const std::string &channel);
@@ -89,11 +90,13 @@ private:
scene::ISceneManager *m_scenemgr = nullptr;
IRollbackManager *m_rollbackmgr = nullptr;
EmergeManager *m_emergemgr = nullptr;
+ ModMetadataDatabase *m_mod_storage_database = nullptr;
std::unique_ptr<ModChannelMgr> m_modchannel_mgr;
};
TestGameDef::TestGameDef() :
+ m_mod_storage_database(new Database_Dummy()),
m_modchannel_mgr(new ModChannelMgr())
{
m_itemdef = createItemDefManager();
@@ -107,6 +110,7 @@ TestGameDef::~TestGameDef()
{
delete m_itemdef;
delete m_nodedef;
+ delete m_mod_storage_database;
}
diff --git a/src/unittest/test_areastore.cpp b/src/unittest/test_areastore.cpp
index 691cd69d2..2af3ca90c 100644
--- a/src/unittest/test_areastore.cpp
+++ b/src/unittest/test_areastore.cpp
@@ -135,7 +135,7 @@ void TestAreaStore::testSerialization()
b.data = "Area BB";
store.insertArea(&b);
- std::ostringstream os;
+ std::ostringstream os(std::ios_base::binary);
store.serialize(os);
std::string str = os.str();
@@ -157,7 +157,7 @@ void TestAreaStore::testSerialization()
UASSERTEQ(const std::string &, str, str_wanted);
- std::istringstream is(str);
+ std::istringstream is(str, std::ios_base::binary);
store.deserialize(is);
// deserialize() doesn't clear the store
diff --git a/src/unittest/test_clientactiveobjectmgr.cpp b/src/unittest/test_clientactiveobjectmgr.cpp
index 4d2846c8d..2d508cf32 100644
--- a/src/unittest/test_clientactiveobjectmgr.cpp
+++ b/src/unittest/test_clientactiveobjectmgr.cpp
@@ -29,6 +29,7 @@ public:
TestClientActiveObject() : ClientActiveObject(0, nullptr, nullptr) {}
~TestClientActiveObject() = default;
ActiveObjectType getType() const { return ACTIVEOBJECT_TYPE_TEST; }
+ virtual void addToScene(ITextureSource *tsrc, scene::ISceneManager *smgr) {}
};
class TestClientActiveObjectMgr : public TestBase
diff --git a/src/unittest/test_compression.cpp b/src/unittest/test_compression.cpp
index dfcadd4b2..a96282f58 100644
--- a/src/unittest/test_compression.cpp
+++ b/src/unittest/test_compression.cpp
@@ -37,6 +37,7 @@ public:
void testRLECompression();
void testZlibCompression();
void testZlibLargeData();
+ void testZstdLargeData();
void testZlibLimit();
void _testZlibLimit(u32 size, u32 limit);
};
@@ -48,6 +49,7 @@ void TestCompression::runTests(IGameDef *gamedef)
TEST(testRLECompression);
TEST(testZlibCompression);
TEST(testZlibLargeData);
+ TEST(testZstdLargeData);
TEST(testZlibLimit);
}
@@ -111,7 +113,7 @@ void TestCompression::testZlibCompression()
fromdata[3]=1;
std::ostringstream os(std::ios_base::binary);
- compress(fromdata, os, SER_FMT_VER_HIGHEST_READ);
+ compressZlib(*fromdata, fromdata.getSize(), os);
std::string str_out = os.str();
@@ -124,7 +126,7 @@ void TestCompression::testZlibCompression()
std::istringstream is(str_out, std::ios_base::binary);
std::ostringstream os2(std::ios_base::binary);
- decompress(is, os2, SER_FMT_VER_HIGHEST_READ);
+ decompressZlib(is, os2);
std::string str_out2 = os2.str();
infostream << "decompress: ";
@@ -174,6 +176,42 @@ void TestCompression::testZlibLargeData()
}
}
+void TestCompression::testZstdLargeData()
+{
+ infostream << "Test: Testing zstd wrappers with a large amount "
+ "of pseudorandom data" << std::endl;
+
+ u32 size = 500000;
+ infostream << "Test: Input size of large compressZstd is "
+ << size << std::endl;
+
+ std::string data_in;
+ data_in.resize(size);
+ PseudoRandom pseudorandom(9420);
+ for (u32 i = 0; i < size; i++)
+ data_in[i] = pseudorandom.range(0, 255);
+
+ std::ostringstream os_compressed(std::ios::binary);
+ compressZstd(data_in, os_compressed, 0);
+ infostream << "Test: Output size of large compressZstd is "
+ << os_compressed.str().size()<<std::endl;
+
+ std::istringstream is_compressed(os_compressed.str(), std::ios::binary);
+ std::ostringstream os_decompressed(std::ios::binary);
+ decompressZstd(is_compressed, os_decompressed);
+ infostream << "Test: Output size of large decompressZstd is "
+ << os_decompressed.str().size() << std::endl;
+
+ std::string str_decompressed = os_decompressed.str();
+ UASSERTEQ(size_t, str_decompressed.size(), data_in.size());
+
+ for (u32 i = 0; i < size && i < str_decompressed.size(); i++) {
+ UTEST(str_decompressed[i] == data_in[i],
+ "index out[%i]=%i differs from in[%i]=%i",
+ i, str_decompressed[i], i, data_in[i]);
+ }
+}
+
void TestCompression::testZlibLimit()
{
// edge cases
diff --git a/src/unittest/test_config.h.in b/src/unittest/test_config.h.in
index 36850b00d..50d2398e4 100644
--- a/src/unittest/test_config.h.in
+++ b/src/unittest/test_config.h.in
@@ -4,3 +4,4 @@
#define TEST_WORLDDIR "@TEST_WORLDDIR@"
#define TEST_SUBGAME_PATH "@TEST_SUBGAME_PATH@"
+#define TEST_MOD_PATH "@TEST_MOD_PATH@"
diff --git a/src/unittest/test_connection.cpp b/src/unittest/test_connection.cpp
index 23b7e9105..04fea90d6 100644
--- a/src/unittest/test_connection.cpp
+++ b/src/unittest/test_connection.cpp
@@ -124,7 +124,7 @@ void TestConnection::testHelpers()
Address a(127,0,0,1, 10);
const u16 seqnum = 34352;
- con::BufferedPacket p1 = con::makePacket(a, data1,
+ con::BufferedPacketPtr p1 = con::makePacket(a, data1,
proto_id, peer_id, channel);
/*
We should now have a packet with this data:
@@ -135,10 +135,10 @@ void TestConnection::testHelpers()
Data:
[7] u8 data1[0]
*/
- UASSERT(readU32(&p1.data[0]) == proto_id);
- UASSERT(readU16(&p1.data[4]) == peer_id);
- UASSERT(readU8(&p1.data[6]) == channel);
- UASSERT(readU8(&p1.data[7]) == data1[0]);
+ UASSERT(readU32(&p1->data[0]) == proto_id);
+ UASSERT(readU16(&p1->data[4]) == peer_id);
+ UASSERT(readU8(&p1->data[6]) == channel);
+ UASSERT(readU8(&p1->data[7]) == data1[0]);
//infostream<<"initial data1[0]="<<((u32)data1[0]&0xff)<<std::endl;
diff --git a/src/unittest/test_gettext.cpp b/src/unittest/test_gettext.cpp
new file mode 100644
index 000000000..338a416d7
--- /dev/null
+++ b/src/unittest/test_gettext.cpp
@@ -0,0 +1,43 @@
+#include "test.h"
+#include "porting.h"
+#include "gettext.h"
+
+class TestGettext : public TestBase
+{
+public:
+ TestGettext() {
+ TestManager::registerTestModule(this);
+ }
+
+ const char *getName() { return "TestGettext"; }
+
+ void runTests(IGameDef *gamedef);
+
+ void testFmtgettext();
+};
+
+static TestGettext g_test_instance;
+
+void TestGettext::runTests(IGameDef *gamedef)
+{
+ TEST(testFmtgettext);
+}
+
+// Make sure updatepo.sh does not pick up the strings
+#define dummyname fmtgettext
+
+void TestGettext::testFmtgettext()
+{
+ std::string buf = dummyname("sample text %d", 12);
+ UASSERTEQ(std::string, buf, "sample text 12");
+
+ std::string src, expect;
+ src = "You are about to join this server with the name \"%s\".\n";
+ expect = "You are about to join this server with the name \"foo\".\n";
+ for (int i = 0; i < 20; i++) {
+ src.append("loooong text");
+ expect.append("loooong text");
+ }
+ buf = dummyname(src.c_str(), "foo");
+ UASSERTEQ(const std::string &, buf, expect);
+}
diff --git a/src/unittest/test_irrptr.cpp b/src/unittest/test_irrptr.cpp
index aa857ff46..3484f1514 100644
--- a/src/unittest/test_irrptr.cpp
+++ b/src/unittest/test_irrptr.cpp
@@ -91,6 +91,12 @@ void TestIrrPtr::testRefCounting()
obj->getReferenceCount());
}
+#if defined(__clang__)
+ #pragma GCC diagnostic push
+ #pragma GCC diagnostic ignored "-Wself-assign-overloaded"
+ #pragma GCC diagnostic ignored "-Wself-move"
+#endif
+
void TestIrrPtr::testSelfAssignment()
{
irr_ptr<IReferenceCounted> p1{new IReferenceCounted()};
@@ -129,3 +135,7 @@ void TestIrrPtr::testNullHandling()
UASSERT(!p2);
UASSERT(!p3);
}
+
+#if defined(__clang__)
+ #pragma GCC diagnostic pop
+#endif
diff --git a/src/unittest/test_map.cpp b/src/unittest/test_map.cpp
new file mode 100644
index 000000000..82e55e1aa
--- /dev/null
+++ b/src/unittest/test_map.cpp
@@ -0,0 +1,68 @@
+/*
+Minetest
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU Lesser General Public License as published by
+the Free Software Foundation; either version 2.1 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU Lesser General Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public License along
+with this program; if not, write to the Free Software Foundation, Inc.,
+51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+
+#include "test.h"
+
+#include <cstdio>
+#include "mapblock.h"
+
+class TestMap : public TestBase
+{
+public:
+ TestMap() { TestManager::registerTestModule(this); }
+ const char *getName() { return "TestMap"; }
+
+ void runTests(IGameDef *gamedef);
+
+ void testMaxMapgenLimit();
+};
+
+static TestMap g_test_instance;
+
+void TestMap::runTests(IGameDef *gamedef)
+{
+ TEST(testMaxMapgenLimit);
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+void TestMap::testMaxMapgenLimit()
+{
+ // limit must end on a mapblock boundary
+ UASSERTEQ(int, MAX_MAP_GENERATION_LIMIT % MAP_BLOCKSIZE, MAP_BLOCKSIZE - 1);
+
+ // objectpos_over_limit should do exactly this except the last node
+ // actually spans from LIMIT-0.5 to LIMIT+0.5
+ float limit_times_bs = MAX_MAP_GENERATION_LIMIT * BS;
+ UASSERT(objectpos_over_limit(v3f(limit_times_bs-BS/2)) == false);
+ UASSERT(objectpos_over_limit(v3f(limit_times_bs)) == false);
+ UASSERT(objectpos_over_limit(v3f(limit_times_bs+BS/2)) == false);
+ UASSERT(objectpos_over_limit(v3f(limit_times_bs+BS)) == true);
+
+ UASSERT(objectpos_over_limit(v3f(-limit_times_bs+BS/2)) == false);
+ UASSERT(objectpos_over_limit(v3f(-limit_times_bs)) == false);
+ UASSERT(objectpos_over_limit(v3f(-limit_times_bs-BS/2)) == false);
+ UASSERT(objectpos_over_limit(v3f(-limit_times_bs-BS)) == true);
+
+ // blockpos_over_max_limit
+ s16 limit_block = MAX_MAP_GENERATION_LIMIT / MAP_BLOCKSIZE;
+ UASSERT(blockpos_over_max_limit(v3s16(limit_block)) == false);
+ UASSERT(blockpos_over_max_limit(v3s16(limit_block+1)) == true);
+ UASSERT(blockpos_over_max_limit(v3s16(-limit_block)) == false);
+ UASSERT(blockpos_over_max_limit(v3s16(-limit_block-1)) == true);
+}
diff --git a/src/unittest/test_map_settings_manager.cpp b/src/unittest/test_map_settings_manager.cpp
index 81ca68705..17c31fe79 100644
--- a/src/unittest/test_map_settings_manager.cpp
+++ b/src/unittest/test_map_settings_manager.cpp
@@ -148,6 +148,11 @@ void TestMapSettingsManager::testMapSettingsManager()
check_noise_params(&dummy, &script_np_factor);
}
+ // The settings manager MUST leave user settings alone
+ mgr.setMapSetting("testname", "1");
+ mgr.setMapSetting("testname", "1", true);
+ UASSERT(!Settings::getLayer(SL_GLOBAL)->exists("testname"));
+
// Now make our Params and see if the values are correctly sourced
MapgenParams *params = mgr.makeMapgenParams();
UASSERT(params->mgtype == MAPGEN_V5);
diff --git a/src/unittest/test_mod/test_mod/init.lua b/src/unittest/test_mod/test_mod/init.lua
new file mode 100644
index 000000000..724a863f5
--- /dev/null
+++ b/src/unittest/test_mod/test_mod/init.lua
@@ -0,0 +1 @@
+-- deliberately empty
diff --git a/src/unittest/test_mod/test_mod/mod.conf b/src/unittest/test_mod/test_mod/mod.conf
new file mode 100644
index 000000000..56c64b2d8
--- /dev/null
+++ b/src/unittest/test_mod/test_mod/mod.conf
@@ -0,0 +1,2 @@
+name = test_mod
+description = A mod doing nothing, to test if MINETEST_MOD_PATH is recognised
diff --git a/src/unittest/test_modmetadatadatabase.cpp b/src/unittest/test_modmetadatadatabase.cpp
new file mode 100644
index 000000000..be97fae5e
--- /dev/null
+++ b/src/unittest/test_modmetadatadatabase.cpp
@@ -0,0 +1,253 @@
+/*
+Minetest
+Copyright (C) 2018 bendeutsch, Ben Deutsch <ben@bendeutsch.de>
+Copyright (C) 2021 TurkeyMcMac, Jude Melton-Houghton <jwmhjwmh@gmail.com>
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU Lesser General Public License as published by
+the Free Software Foundation; either version 2.1 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU Lesser General Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public License along
+with this program; if not, write to the Free Software Foundation, Inc.,
+51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+
+// This file is an edited copy of test_authdatabase.cpp
+
+#include "test.h"
+
+#include <algorithm>
+#include "database/database-files.h"
+#include "database/database-sqlite3.h"
+#include "filesys.h"
+
+namespace
+{
+// Anonymous namespace to create classes that are only
+// visible to this file
+//
+// These are helpers that return a *ModMetadataDatabase and
+// allow us to run the same tests on different databases and
+// database acquisition strategies.
+
+class ModMetadataDatabaseProvider
+{
+public:
+ virtual ~ModMetadataDatabaseProvider() = default;
+ virtual ModMetadataDatabase *getModMetadataDatabase() = 0;
+};
+
+class FixedProvider : public ModMetadataDatabaseProvider
+{
+public:
+ FixedProvider(ModMetadataDatabase *mod_meta_db) : mod_meta_db(mod_meta_db){};
+ virtual ~FixedProvider(){};
+ virtual ModMetadataDatabase *getModMetadataDatabase() { return mod_meta_db; };
+
+private:
+ ModMetadataDatabase *mod_meta_db;
+};
+
+class FilesProvider : public ModMetadataDatabaseProvider
+{
+public:
+ FilesProvider(const std::string &dir) : dir(dir){};
+ virtual ~FilesProvider()
+ {
+ if (mod_meta_db)
+ mod_meta_db->endSave();
+ delete mod_meta_db;
+ }
+ virtual ModMetadataDatabase *getModMetadataDatabase()
+ {
+ if (mod_meta_db)
+ mod_meta_db->endSave();
+ delete mod_meta_db;
+ mod_meta_db = new ModMetadataDatabaseFiles(dir);
+ mod_meta_db->beginSave();
+ return mod_meta_db;
+ };
+
+private:
+ std::string dir;
+ ModMetadataDatabase *mod_meta_db = nullptr;
+};
+
+class SQLite3Provider : public ModMetadataDatabaseProvider
+{
+public:
+ SQLite3Provider(const std::string &dir) : dir(dir){};
+ virtual ~SQLite3Provider()
+ {
+ if (mod_meta_db)
+ mod_meta_db->endSave();
+ delete mod_meta_db;
+ }
+ virtual ModMetadataDatabase *getModMetadataDatabase()
+ {
+ if (mod_meta_db)
+ mod_meta_db->endSave();
+ delete mod_meta_db;
+ mod_meta_db = new ModMetadataDatabaseSQLite3(dir);
+ mod_meta_db->beginSave();
+ return mod_meta_db;
+ };
+
+private:
+ std::string dir;
+ ModMetadataDatabase *mod_meta_db = nullptr;
+};
+}
+
+class TestModMetadataDatabase : public TestBase
+{
+public:
+ TestModMetadataDatabase() { TestManager::registerTestModule(this); }
+ const char *getName() { return "TestModMetadataDatabase"; }
+
+ void runTests(IGameDef *gamedef);
+ void runTestsForCurrentDB();
+
+ void testRecallFail();
+ void testCreate();
+ void testRecall();
+ void testChange();
+ void testRecallChanged();
+ void testListMods();
+ void testRemove();
+
+private:
+ ModMetadataDatabaseProvider *mod_meta_provider;
+};
+
+static TestModMetadataDatabase g_test_instance;
+
+void TestModMetadataDatabase::runTests(IGameDef *gamedef)
+{
+ // fixed directory, for persistence
+ thread_local const std::string test_dir = getTestTempDirectory();
+
+ // Each set of tests is run twice for each database type:
+ // one where we reuse the same ModMetadataDatabase object (to test local caching),
+ // and one where we create a new ModMetadataDatabase object for each call
+ // (to test actual persistence).
+
+ rawstream << "-------- Files database (same object)" << std::endl;
+
+ ModMetadataDatabase *mod_meta_db = new ModMetadataDatabaseFiles(test_dir);
+ mod_meta_provider = new FixedProvider(mod_meta_db);
+
+ runTestsForCurrentDB();
+
+ delete mod_meta_db;
+ delete mod_meta_provider;
+
+ // reset database
+ fs::RecursiveDelete(test_dir + DIR_DELIM + "mod_storage");
+
+ rawstream << "-------- Files database (new objects)" << std::endl;
+
+ mod_meta_provider = new FilesProvider(test_dir);
+
+ runTestsForCurrentDB();
+
+ delete mod_meta_provider;
+
+ rawstream << "-------- SQLite3 database (same object)" << std::endl;
+
+ mod_meta_db = new ModMetadataDatabaseSQLite3(test_dir);
+ mod_meta_provider = new FixedProvider(mod_meta_db);
+
+ runTestsForCurrentDB();
+
+ delete mod_meta_db;
+ delete mod_meta_provider;
+
+ // reset database
+ fs::DeleteSingleFileOrEmptyDirectory(test_dir + DIR_DELIM + "mod_storage.sqlite");
+
+ rawstream << "-------- SQLite3 database (new objects)" << std::endl;
+
+ mod_meta_provider = new SQLite3Provider(test_dir);
+
+ runTestsForCurrentDB();
+
+ delete mod_meta_provider;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+void TestModMetadataDatabase::runTestsForCurrentDB()
+{
+ TEST(testRecallFail);
+ TEST(testCreate);
+ TEST(testRecall);
+ TEST(testChange);
+ TEST(testRecallChanged);
+ TEST(testListMods);
+ TEST(testRemove);
+ TEST(testRecallFail);
+}
+
+void TestModMetadataDatabase::testRecallFail()
+{
+ ModMetadataDatabase *mod_meta_db = mod_meta_provider->getModMetadataDatabase();
+ StringMap recalled;
+ mod_meta_db->getModEntries("mod1", &recalled);
+ UASSERT(recalled.empty());
+}
+
+void TestModMetadataDatabase::testCreate()
+{
+ ModMetadataDatabase *mod_meta_db = mod_meta_provider->getModMetadataDatabase();
+ StringMap recalled;
+ UASSERT(mod_meta_db->setModEntry("mod1", "key1", "value1"));
+}
+
+void TestModMetadataDatabase::testRecall()
+{
+ ModMetadataDatabase *mod_meta_db = mod_meta_provider->getModMetadataDatabase();
+ StringMap recalled;
+ mod_meta_db->getModEntries("mod1", &recalled);
+ UASSERT(recalled.size() == 1);
+ UASSERT(recalled["key1"] == "value1");
+}
+
+void TestModMetadataDatabase::testChange()
+{
+ ModMetadataDatabase *mod_meta_db = mod_meta_provider->getModMetadataDatabase();
+ StringMap recalled;
+ UASSERT(mod_meta_db->setModEntry("mod1", "key1", "value2"));
+}
+
+void TestModMetadataDatabase::testRecallChanged()
+{
+ ModMetadataDatabase *mod_meta_db = mod_meta_provider->getModMetadataDatabase();
+ StringMap recalled;
+ mod_meta_db->getModEntries("mod1", &recalled);
+ UASSERT(recalled.size() == 1);
+ UASSERT(recalled["key1"] == "value2");
+}
+
+void TestModMetadataDatabase::testListMods()
+{
+ ModMetadataDatabase *mod_meta_db = mod_meta_provider->getModMetadataDatabase();
+ UASSERT(mod_meta_db->setModEntry("mod2", "key1", "value1"));
+ std::vector<std::string> mod_list;
+ mod_meta_db->listMods(&mod_list);
+ UASSERT(mod_list.size() == 2);
+ UASSERT(std::find(mod_list.cbegin(), mod_list.cend(), "mod1") != mod_list.cend());
+ UASSERT(std::find(mod_list.cbegin(), mod_list.cend(), "mod2") != mod_list.cend());
+}
+
+void TestModMetadataDatabase::testRemove()
+{
+ ModMetadataDatabase *mod_meta_db = mod_meta_provider->getModMetadataDatabase();
+ UASSERT(mod_meta_db->removeModEntry("mod1", "key1"));
+}
diff --git a/src/unittest/test_noderesolver.cpp b/src/unittest/test_noderesolver.cpp
index 28da43620..ed66093a9 100644
--- a/src/unittest/test_noderesolver.cpp
+++ b/src/unittest/test_noderesolver.cpp
@@ -56,6 +56,8 @@ void TestNodeResolver::runTests(IGameDef *gamedef)
class Foobar : public NodeResolver {
public:
+ friend class TestNodeResolver; // m_ndef
+
void resolveNodeNames();
content_t test_nr_node1;
diff --git a/src/unittest/test_schematic.cpp b/src/unittest/test_schematic.cpp
index da4ce50d2..d2f027eb4 100644
--- a/src/unittest/test_schematic.cpp
+++ b/src/unittest/test_schematic.cpp
@@ -66,13 +66,14 @@ void TestSchematic::testMtsSerializeDeserialize(const NodeDefManager *ndef)
std::stringstream ss(std::ios_base::binary |
std::ios_base::in | std::ios_base::out);
- std::vector<std::string> names;
- names.emplace_back("foo");
- names.emplace_back("bar");
- names.emplace_back("baz");
- names.emplace_back("qux");
-
- Schematic schem, schem2;
+ Schematic schem;
+ {
+ std::vector<std::string> &names = schem.m_nodenames;
+ names.emplace_back("foo");
+ names.emplace_back("bar");
+ names.emplace_back("baz");
+ names.emplace_back("qux");
+ }
schem.flags = 0;
schem.size = size;
@@ -83,18 +84,21 @@ void TestSchematic::testMtsSerializeDeserialize(const NodeDefManager *ndef)
for (s16 y = 0; y != size.Y; y++)
schem.slice_probs[y] = MTSCHEM_PROB_ALWAYS;
- UASSERT(schem.serializeToMts(&ss, names));
+ UASSERT(schem.serializeToMts(&ss));
ss.seekg(0);
- names.clear();
- UASSERT(schem2.deserializeFromMts(&ss, &names));
+ Schematic schem2;
+ UASSERT(schem2.deserializeFromMts(&ss));
- UASSERTEQ(size_t, names.size(), 4);
- UASSERTEQ(std::string, names[0], "foo");
- UASSERTEQ(std::string, names[1], "bar");
- UASSERTEQ(std::string, names[2], "baz");
- UASSERTEQ(std::string, names[3], "qux");
+ {
+ std::vector<std::string> &names = schem2.m_nodenames;
+ UASSERTEQ(size_t, names.size(), 4);
+ UASSERTEQ(std::string, names[0], "foo");
+ UASSERTEQ(std::string, names[1], "bar");
+ UASSERTEQ(std::string, names[2], "baz");
+ UASSERTEQ(std::string, names[3], "qux");
+ }
UASSERT(schem2.size == size);
for (size_t i = 0; i != volume; i++)
@@ -120,14 +124,14 @@ void TestSchematic::testLuaTableSerialize(const NodeDefManager *ndef)
for (s16 y = 0; y != size.Y; y++)
schem.slice_probs[y] = MTSCHEM_PROB_ALWAYS;
- std::vector<std::string> names;
+ std::vector<std::string> &names = schem.m_nodenames;
names.emplace_back("air");
names.emplace_back("default:lava_source");
names.emplace_back("default:glass");
std::ostringstream ss(std::ios_base::binary);
- UASSERT(schem.serializeToLua(&ss, names, false, 0));
+ UASSERT(schem.serializeToLua(&ss, false, 0));
UASSERTEQ(std::string, ss.str(), expected_lua_output);
}
@@ -159,6 +163,8 @@ void TestSchematic::testFileSerializeDeserialize(const NodeDefManager *ndef)
schem1.slice_probs[0] = 80;
schem1.slice_probs[1] = 160;
schem1.slice_probs[2] = 240;
+ // Node resolving happened manually.
+ schem1.m_resolve_done = true;
for (size_t i = 0; i != volume; i++) {
content_t c = content_map[test_schem2_data[i]];
diff --git a/src/unittest/test_servermodmanager.cpp b/src/unittest/test_servermodmanager.cpp
index e3edb0c32..4c473d8b5 100644
--- a/src/unittest/test_servermodmanager.cpp
+++ b/src/unittest/test_servermodmanager.cpp
@@ -48,14 +48,20 @@ static TestServerModManager g_test_instance;
void TestServerModManager::runTests(IGameDef *gamedef)
{
const char *saved_env_mt_subgame_path = getenv("MINETEST_SUBGAME_PATH");
+ const char *saved_env_mt_mod_path = getenv("MINETEST_MOD_PATH");
#ifdef WIN32
{
std::string subgame_path("MINETEST_SUBGAME_PATH=");
subgame_path.append(TEST_SUBGAME_PATH);
_putenv(subgame_path.c_str());
+
+ std::string mod_path("MINETEST_MOD_PATH=");
+ mod_path.append(TEST_MOD_PATH);
+ _putenv(mod_path.c_str());
}
#else
setenv("MINETEST_SUBGAME_PATH", TEST_SUBGAME_PATH, 1);
+ setenv("MINETEST_MOD_PATH", TEST_MOD_PATH, 1);
#endif
TEST(testCreation);
@@ -75,12 +81,21 @@ void TestServerModManager::runTests(IGameDef *gamedef)
if (saved_env_mt_subgame_path)
subgame_path.append(saved_env_mt_subgame_path);
_putenv(subgame_path.c_str());
+
+ std::string mod_path("MINETEST_MOD_PATH=");
+ if (saved_env_mt_mod_path)
+ mod_path.append(saved_env_mt_mod_path);
+ _putenv(mod_path.c_str());
}
#else
if (saved_env_mt_subgame_path)
setenv("MINETEST_SUBGAME_PATH", saved_env_mt_subgame_path, 1);
else
unsetenv("MINETEST_SUBGAME_PATH");
+ if (saved_env_mt_mod_path)
+ setenv("MINETEST_MOD_PATH", saved_env_mt_mod_path, 1);
+ else
+ unsetenv("MINETEST_MOD_PATH");
#endif
}
@@ -89,6 +104,7 @@ void TestServerModManager::testCreation()
std::string path = std::string(TEST_WORLDDIR) + DIR_DELIM + "world.mt";
Settings world_config;
world_config.set("gameid", "devtest");
+ world_config.set("load_mod_test_mod", "true");
UASSERTEQ(bool, world_config.updateConfigFile(path.c_str()), true);
ServerModManager sm(TEST_WORLDDIR);
}
@@ -119,16 +135,21 @@ void TestServerModManager::testGetMods()
UASSERTEQ(bool, mods.empty(), false);
// Ensure we found basenodes mod (part of devtest)
+ // and test_mod (for testing MINETEST_MOD_PATH).
bool default_found = false;
+ bool test_mod_found = false;
for (const auto &m : mods) {
if (m.name == "basenodes")
default_found = true;
+ if (m.name == "test_mod")
+ test_mod_found = true;
// Verify if paths are not empty
UASSERTEQ(bool, m.path.empty(), false);
}
UASSERTEQ(bool, default_found, true);
+ UASSERTEQ(bool, test_mod_found, true);
}
void TestServerModManager::testGetModspec()
diff --git a/src/unittest/test_socket.cpp b/src/unittest/test_socket.cpp
index 6d5cf334d..620021b59 100644
--- a/src/unittest/test_socket.cpp
+++ b/src/unittest/test_socket.cpp
@@ -97,11 +97,11 @@ void TestSocket::testIPv4Socket()
UASSERT(strncmp(sendbuffer, rcvbuffer, sizeof(sendbuffer)) == 0);
if (address != Address(0, 0, 0, 0, port)) {
- UASSERT(sender.getAddress().sin_addr.s_addr ==
- address.getAddress().sin_addr.s_addr);
+ UASSERT(sender.getAddress().s_addr ==
+ address.getAddress().s_addr);
} else {
- UASSERT(sender.getAddress().sin_addr.s_addr ==
- Address(127, 0, 0, 1, 0).getAddress().sin_addr.s_addr);
+ UASSERT(sender.getAddress().s_addr ==
+ Address(127, 0, 0, 1, 0).getAddress().s_addr);
}
}
@@ -128,7 +128,7 @@ void TestSocket::testIPv6Socket()
socket6.Bind(address6);
- try {
+ {
socket6.Send(Address(&bytes, port), sendbuffer, sizeof(sendbuffer));
sleep_ms(50);
@@ -142,10 +142,8 @@ void TestSocket::testIPv6Socket()
}
//FIXME: This fails on some systems
UASSERT(strncmp(sendbuffer, rcvbuffer, sizeof(sendbuffer)) == 0);
- UASSERT(memcmp(sender.getAddress6().sin6_addr.s6_addr,
- Address(&bytes, 0).getAddress6().sin6_addr.s6_addr, 16) == 0);
- } catch (SendFailedException &e) {
- errorstream << "IPv6 support enabled but not available!"
- << std::endl;
+
+ UASSERT(memcmp(sender.getAddress6().s6_addr,
+ Address(&bytes, 0).getAddress6().s6_addr, 16) == 0);
}
}
diff --git a/src/unittest/test_utilities.cpp b/src/unittest/test_utilities.cpp
index 93ba3f844..743fe4462 100644
--- a/src/unittest/test_utilities.cpp
+++ b/src/unittest/test_utilities.cpp
@@ -23,6 +23,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "util/enriched_string.h"
#include "util/numeric.h"
#include "util/string.h"
+#include "util/base64.h"
class TestUtilities : public TestBase {
public:
@@ -56,6 +57,7 @@ public:
void testMyround();
void testStringJoin();
void testEulerConversion();
+ void testBase64();
};
static TestUtilities g_test_instance;
@@ -87,6 +89,7 @@ void TestUtilities::runTests(IGameDef *gamedef)
TEST(testMyround);
TEST(testStringJoin);
TEST(testEulerConversion);
+ TEST(testBase64);
}
////////////////////////////////////////////////////////////////////////////////
@@ -389,9 +392,9 @@ void TestUtilities::testIsPowerOfTwo()
UASSERT(is_power_of_two(2) == true);
UASSERT(is_power_of_two(3) == false);
for (int exponent = 2; exponent <= 31; ++exponent) {
- UASSERT(is_power_of_two((1 << exponent) - 1) == false);
- UASSERT(is_power_of_two((1 << exponent)) == true);
- UASSERT(is_power_of_two((1 << exponent) + 1) == false);
+ UASSERT(is_power_of_two((1U << exponent) - 1) == false);
+ UASSERT(is_power_of_two((1U << exponent)) == true);
+ UASSERT(is_power_of_two((1U << exponent) + 1) == false);
}
UASSERT(is_power_of_two(U32_MAX) == false);
}
@@ -537,3 +540,93 @@ void TestUtilities::testEulerConversion()
setPitchYawRoll(m2, v2);
UASSERT(within(m1, m2, tolL));
}
+
+void TestUtilities::testBase64()
+{
+ // Test character set
+ UASSERT(base64_is_valid("ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+ "abcdefghijklmnopqrstuvwxyz"
+ "0123456789+/") == true);
+ UASSERT(base64_is_valid("/+9876543210"
+ "zyxwvutsrqponmlkjihgfedcba"
+ "ZYXWVUTSRQPONMLKJIHGFEDCBA") == true);
+ UASSERT(base64_is_valid("ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+ "abcdefghijklmnopqrstuvwxyz"
+ "0123456789+.") == false);
+ UASSERT(base64_is_valid("ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+ "abcdefghijklmnopqrstuvwxyz"
+ "0123456789 /") == false);
+
+ // Test empty string
+ UASSERT(base64_is_valid("") == true);
+
+ // Test different lengths, with and without padding,
+ // with correct and incorrect padding
+ UASSERT(base64_is_valid("A") == false);
+ UASSERT(base64_is_valid("AA") == true);
+ UASSERT(base64_is_valid("AAA") == true);
+ UASSERT(base64_is_valid("AAAA") == true);
+ UASSERT(base64_is_valid("AAAAA") == false);
+ UASSERT(base64_is_valid("AAAAAA") == true);
+ UASSERT(base64_is_valid("AAAAAAA") == true);
+ UASSERT(base64_is_valid("AAAAAAAA") == true);
+ UASSERT(base64_is_valid("A===") == false);
+ UASSERT(base64_is_valid("AA==") == true);
+ UASSERT(base64_is_valid("AAA=") == true);
+ UASSERT(base64_is_valid("AAAA") == true);
+ UASSERT(base64_is_valid("AAAA====") == false);
+ UASSERT(base64_is_valid("AAAAA===") == false);
+ UASSERT(base64_is_valid("AAAAAA==") == true);
+ UASSERT(base64_is_valid("AAAAAAA=") == true);
+ UASSERT(base64_is_valid("AAAAAAA==") == false);
+ UASSERT(base64_is_valid("AAAAAAA===") == false);
+ UASSERT(base64_is_valid("AAAAAAA====") == false);
+ UASSERT(base64_is_valid("AAAAAAAA") == true);
+ UASSERT(base64_is_valid("AAAAAAAA=") == false);
+ UASSERT(base64_is_valid("AAAAAAAA==") == false);
+ UASSERT(base64_is_valid("AAAAAAAA===") == false);
+ UASSERT(base64_is_valid("AAAAAAAA====") == false);
+
+ // Test if canonical encoding
+ // Last character limitations, length % 4 == 3
+ UASSERT(base64_is_valid("AAB") == false);
+ UASSERT(base64_is_valid("AAE") == true);
+ UASSERT(base64_is_valid("AAQ") == true);
+ UASSERT(base64_is_valid("AAB=") == false);
+ UASSERT(base64_is_valid("AAE=") == true);
+ UASSERT(base64_is_valid("AAQ=") == true);
+ UASSERT(base64_is_valid("AAAAAAB=") == false);
+ UASSERT(base64_is_valid("AAAAAAE=") == true);
+ UASSERT(base64_is_valid("AAAAAAQ=") == true);
+ // Last character limitations, length % 4 == 2
+ UASSERT(base64_is_valid("AB") == false);
+ UASSERT(base64_is_valid("AE") == false);
+ UASSERT(base64_is_valid("AQ") == true);
+ UASSERT(base64_is_valid("AB==") == false);
+ UASSERT(base64_is_valid("AE==") == false);
+ UASSERT(base64_is_valid("AQ==") == true);
+ UASSERT(base64_is_valid("AAAAAB==") == false);
+ UASSERT(base64_is_valid("AAAAAE==") == false);
+ UASSERT(base64_is_valid("AAAAAQ==") == true);
+
+ // Extraneous character present
+ UASSERT(base64_is_valid(".") == false);
+ UASSERT(base64_is_valid("A.") == false);
+ UASSERT(base64_is_valid("AA.") == false);
+ UASSERT(base64_is_valid("AAA.") == false);
+ UASSERT(base64_is_valid("AAAA.") == false);
+ UASSERT(base64_is_valid("AAAAA.") == false);
+ UASSERT(base64_is_valid("A.A") == false);
+ UASSERT(base64_is_valid("AA.A") == false);
+ UASSERT(base64_is_valid("AAA.A") == false);
+ UASSERT(base64_is_valid("AAAA.A") == false);
+ UASSERT(base64_is_valid("AAAAA.A") == false);
+ UASSERT(base64_is_valid("\xE1""AAA") == false);
+
+ // Padding in wrong position
+ UASSERT(base64_is_valid("A=A") == false);
+ UASSERT(base64_is_valid("AA=A") == false);
+ UASSERT(base64_is_valid("AAA=A") == false);
+ UASSERT(base64_is_valid("AAAA=A") == false);
+ UASSERT(base64_is_valid("AAAAA=A") == false);
+}
diff --git a/src/unittest/test_voxelarea.cpp b/src/unittest/test_voxelarea.cpp
index 6ec0412d5..9826d2ee7 100644
--- a/src/unittest/test_voxelarea.cpp
+++ b/src/unittest/test_voxelarea.cpp
@@ -30,6 +30,7 @@ public:
void test_addarea();
void test_pad();
+ void test_extent();
void test_volume();
void test_contains_voxelarea();
void test_contains_point();
@@ -65,6 +66,7 @@ void TestVoxelArea::runTests(IGameDef *gamedef)
{
TEST(test_addarea);
TEST(test_pad);
+ TEST(test_extent);
TEST(test_volume);
TEST(test_contains_voxelarea);
TEST(test_contains_point);
@@ -113,10 +115,22 @@ void TestVoxelArea::test_pad()
UASSERT(v1.MaxEdge == v3s16(-47, -9347, 969));
}
+void TestVoxelArea::test_extent()
+{
+ VoxelArea v1(v3s16(-1337, -547, -789), v3s16(-147, 447, 669));
+ UASSERT(v1.getExtent() == v3s16(1191, 995, 1459));
+
+ VoxelArea v2(v3s16(32493, -32507, 32753), v3s16(32508, -32492, 32768));
+ UASSERT(v2.getExtent() == v3s16(16, 16, 16));
+}
+
void TestVoxelArea::test_volume()
{
- VoxelArea v1(v3s16(-1337, 447, -789), v3s16(-147, -9547, 669));
- UASSERTEQ(s32, v1.getVolume(), -184657133);
+ VoxelArea v1(v3s16(-1337, -547, -789), v3s16(-147, 447, 669));
+ UASSERTEQ(s32, v1.getVolume(), 1728980655);
+
+ VoxelArea v2(v3s16(32493, -32507, 32753), v3s16(32508, -32492, 32768));
+ UASSERTEQ(s32, v2.getVolume(), 4096);
}
void TestVoxelArea::test_contains_voxelarea()
diff --git a/src/util/CMakeLists.txt b/src/util/CMakeLists.txt
index cd2e468d1..6bc97915f 100644
--- a/src/util/CMakeLists.txt
+++ b/src/util/CMakeLists.txt
@@ -15,4 +15,5 @@ set(UTIL_SRCS
${CMAKE_CURRENT_SOURCE_DIR}/string.cpp
${CMAKE_CURRENT_SOURCE_DIR}/srp.cpp
${CMAKE_CURRENT_SOURCE_DIR}/timetaker.cpp
+ ${CMAKE_CURRENT_SOURCE_DIR}/png.cpp
PARENT_SCOPE)
diff --git a/src/util/Optional.h b/src/util/Optional.h
index 9c2842b43..eda7fff89 100644
--- a/src/util/Optional.h
+++ b/src/util/Optional.h
@@ -19,6 +19,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#pragma once
+#include <utility>
#include "debug.h"
struct nullopt_t
@@ -43,18 +44,38 @@ class Optional
public:
Optional() noexcept {}
Optional(nullopt_t) noexcept {}
+
Optional(const T &value) noexcept : m_has_value(true), m_value(value) {}
+ Optional(T &&value) noexcept : m_has_value(true), m_value(std::move(value)) {}
+
Optional(const Optional<T> &other) noexcept :
m_has_value(other.m_has_value), m_value(other.m_value)
+ {}
+ Optional(Optional<T> &&other) noexcept :
+ m_has_value(other.m_has_value), m_value(std::move(other.m_value))
{
+ other.m_has_value = false;
}
- void operator=(nullopt_t) noexcept { m_has_value = false; }
+ Optional<T> &operator=(nullopt_t) noexcept { m_has_value = false; return *this; }
- void operator=(const Optional<T> &other) noexcept
+ Optional<T> &operator=(const Optional<T> &other) noexcept
{
+ if (&other == this)
+ return *this;
m_has_value = other.m_has_value;
m_value = other.m_value;
+ return *this;
+ }
+
+ Optional<T> &operator=(Optional<T> &&other) noexcept
+ {
+ if (&other == this)
+ return *this;
+ m_has_value = other.m_has_value;
+ m_value = std::move(other.m_value);
+ other.m_has_value = false;
+ return *this;
}
T &value()
@@ -71,6 +92,13 @@ public:
const T &value_or(const T &def) const { return m_has_value ? m_value : def; }
+ // Unchecked access consistent with std::optional
+ T* operator->() { return &m_value; }
+ const T* operator->() const { return &m_value; }
+
+ T& operator*() { return m_value; }
+ const T& operator*() const { return m_value; }
+
bool has_value() const noexcept { return m_has_value; }
explicit operator bool() const { return m_has_value; }
diff --git a/src/util/base64.cpp b/src/util/base64.cpp
index 6e1584410..0c2455222 100644
--- a/src/util/base64.cpp
+++ b/src/util/base64.cpp
@@ -33,18 +33,39 @@ static const std::string base64_chars =
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";
+static const std::string base64_chars_padding_1 = "AEIMQUYcgkosw048";
+static const std::string base64_chars_padding_2 = "AQgw";
static inline bool is_base64(unsigned char c)
{
- return isalnum(c) || c == '+' || c == '/' || c == '=';
+ return (c >= '0' && c <= '9')
+ || (c >= 'A' && c <= 'Z')
+ || (c >= 'a' && c <= 'z')
+ || c == '+' || c == '/';
}
bool base64_is_valid(std::string const& s)
{
- for (char i : s)
- if (!is_base64(i))
+ size_t i = 0;
+ for (; i < s.size(); ++i)
+ if (!is_base64(s[i]))
+ break;
+ unsigned char padding = 3 - ((i + 3) % 4);
+ if ((padding == 1 && base64_chars_padding_1.find(s[i - 1]) == std::string::npos)
+ || (padding == 2 && base64_chars_padding_2.find(s[i - 1]) == std::string::npos)
+ || padding == 3)
+ return false;
+ int actual_padding = s.size() - i;
+ // omission of padding characters is allowed
+ if (actual_padding == 0)
+ return true;
+
+ // remaining characters (max. 2) may only be padding
+ for (; i < s.size(); ++i)
+ if (s[i] != '=')
return false;
- return true;
+ // number of padding characters needs to match
+ return padding == actual_padding;
}
std::string base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len) {
diff --git a/src/util/numeric.cpp b/src/util/numeric.cpp
index 1af3f66be..702ddce95 100644
--- a/src/util/numeric.cpp
+++ b/src/util/numeric.cpp
@@ -106,10 +106,6 @@ u64 murmur_hash_64_ua(const void *key, int len, unsigned int seed)
bool isBlockInSight(v3s16 blockpos_b, v3f camera_pos, v3f camera_dir,
f32 camera_fov, f32 range, f32 *distance_ptr)
{
- // Maximum radius of a block. The magic number is
- // sqrt(3.0) / 2.0 in literal form.
- static constexpr const f32 block_max_radius = 0.866025403784f * MAP_BLOCKSIZE * BS;
-
v3s16 blockpos_nodes = blockpos_b * MAP_BLOCKSIZE;
// Block center position
@@ -123,7 +119,7 @@ bool isBlockInSight(v3s16 blockpos_b, v3f camera_pos, v3f camera_dir,
v3f blockpos_relative = blockpos - camera_pos;
// Total distance
- f32 d = MYMAX(0, blockpos_relative.getLength() - block_max_radius);
+ f32 d = MYMAX(0, blockpos_relative.getLength() - BLOCK_MAX_RADIUS);
if (distance_ptr)
*distance_ptr = d;
@@ -141,7 +137,7 @@ bool isBlockInSight(v3s16 blockpos_b, v3f camera_pos, v3f camera_dir,
// such that a block that has any portion visible with the
// current camera position will have the center visible at the
// adjusted postion
- f32 adjdist = block_max_radius / cos((M_PI - camera_fov) / 2);
+ f32 adjdist = BLOCK_MAX_RADIUS / cos((M_PI - camera_fov) / 2);
// Block position relative to adjusted camera
v3f blockpos_adj = blockpos - (camera_pos - camera_dir * adjdist);
@@ -163,7 +159,7 @@ bool isBlockInSight(v3s16 blockpos_b, v3f camera_pos, v3f camera_dir,
return true;
}
-s16 adjustDist(s16 dist, float zoom_fov)
+inline float adjustDist(float dist, float zoom_fov)
{
// 1.775 ~= 72 * PI / 180 * 1.4, the default FOV on the client.
// The heuristic threshold for zooming is half of that.
@@ -171,8 +167,13 @@ s16 adjustDist(s16 dist, float zoom_fov)
if (zoom_fov < 0.001f || zoom_fov > threshold_fov)
return dist;
- return std::round(dist * std::cbrt((1.0f - std::cos(threshold_fov)) /
- (1.0f - std::cos(zoom_fov / 2.0f))));
+ return dist * std::cbrt((1.0f - std::cos(threshold_fov)) /
+ (1.0f - std::cos(zoom_fov / 2.0f)));
+}
+
+s16 adjustDist(s16 dist, float zoom_fov)
+{
+ return std::round(adjustDist((float)dist, zoom_fov));
}
void setPitchYawRollRad(core::matrix4 &m, const v3f &rot)
diff --git a/src/util/numeric.h b/src/util/numeric.h
index 864ab7543..32a6f4312 100644
--- a/src/util/numeric.h
+++ b/src/util/numeric.h
@@ -20,6 +20,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#pragma once
#include "basic_macros.h"
+#include "constants.h"
#include "irrlichttypes.h"
#include "irr_v2d.h"
#include "irr_v3d.h"
@@ -36,6 +37,9 @@ with this program; if not, write to the Free Software Foundation, Inc.,
y = temp; \
} while (0)
+// Maximum radius of a block. The magic number is
+// sqrt(3.0) / 2.0 in literal form.
+static constexpr const f32 BLOCK_MAX_RADIUS = 0.866025403784f * MAP_BLOCKSIZE * BS;
inline s16 getContainerPos(s16 p, s16 d)
{
diff --git a/src/util/png.cpp b/src/util/png.cpp
new file mode 100755
index 000000000..698cbc9a5
--- /dev/null
+++ b/src/util/png.cpp
@@ -0,0 +1,68 @@
+/*
+Minetest
+Copyright (C) 2021 hecks
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU Lesser General Public License as published by
+the Free Software Foundation; either version 2.1 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU Lesser General Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public License along
+with this program; if not, write to the Free Software Foundation, Inc.,
+51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+
+#include "png.h"
+#include <string>
+#include <sstream>
+#include <zlib.h>
+#include <cassert>
+#include "util/serialize.h"
+#include "serialization.h"
+#include "irrlichttypes.h"
+
+static void writeChunk(std::ostringstream &target, const std::string &chunk_str)
+{
+ assert(chunk_str.size() >= 4);
+ assert(chunk_str.size() - 4 < U32_MAX);
+ writeU32(target, chunk_str.size() - 4); // Write length minus the identifier
+ target << chunk_str;
+ writeU32(target, crc32(0,(const u8*)chunk_str.data(), chunk_str.size()));
+}
+
+std::string encodePNG(const u8 *data, u32 width, u32 height, s32 compression)
+{
+ std::ostringstream file(std::ios::binary);
+ file << "\x89PNG\r\n\x1a\n";
+
+ {
+ std::ostringstream IHDR(std::ios::binary);
+ IHDR << "IHDR";
+ writeU32(IHDR, width);
+ writeU32(IHDR, height);
+ // 8 bpp, color type 6 (RGBA)
+ IHDR.write("\x08\x06\x00\x00\x00", 5);
+ writeChunk(file, IHDR.str());
+ }
+
+ {
+ std::ostringstream IDAT(std::ios::binary);
+ IDAT << "IDAT";
+ std::ostringstream scanlines(std::ios::binary);
+ for(u32 i = 0; i < height; i++) {
+ scanlines.write("\x00", 1); // Null predictor
+ scanlines.write((const char*) data + width * 4 * i, width * 4);
+ }
+ compressZlib(scanlines.str(), IDAT, compression);
+ writeChunk(file, IDAT.str());
+ }
+
+ file.write("\x00\x00\x00\x00IEND\xae\x42\x60\x82", 12);
+
+ return file.str();
+}
diff --git a/src/cloudparams.h b/src/util/png.h
index 88b5760ee..92387aef0 100644..100755
--- a/src/cloudparams.h
+++ b/src/util/png.h
@@ -1,6 +1,6 @@
/*
Minetest
-Copyright (C) 2017 bendeutsch, Ben Deutsch <ben@bendeutsch.de>
+Copyright (C) 2021 hecks
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
@@ -19,12 +19,9 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#pragma once
-struct CloudParams
-{
- float density;
- video::SColor color_bright;
- video::SColor color_ambient;
- float thickness;
- float height;
- v2f speed;
-};
+#include <string>
+#include "irrlichttypes.h"
+
+/* Simple PNG encoder. Encodes an RGBA image with no predictors.
+ Returns a binary string. */
+std::string encodePNG(const u8 *data, u32 width, u32 height, s32 compression);
diff --git a/src/util/pointer.h b/src/util/pointer.h
index 7fc5de551..245ac85bf 100644
--- a/src/util/pointer.h
+++ b/src/util/pointer.h
@@ -22,6 +22,21 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "irrlichttypes.h"
#include "debug.h" // For assert()
#include <cstring>
+#include <memory> // std::shared_ptr
+
+
+template<typename T> class ConstSharedPtr {
+public:
+ ConstSharedPtr(T *ptr) : ptr(ptr) {}
+ ConstSharedPtr(const std::shared_ptr<T> &ptr) : ptr(ptr) {}
+
+ const T* get() const noexcept { return ptr.get(); }
+ const T& operator*() const noexcept { return *ptr.get(); }
+ const T* operator->() const noexcept { return ptr.get(); }
+
+private:
+ std::shared_ptr<T> ptr;
+};
template <typename T>
class Buffer
@@ -40,17 +55,11 @@ public:
else
data = NULL;
}
- Buffer(const Buffer &buffer)
- {
- m_size = buffer.m_size;
- if(m_size != 0)
- {
- data = new T[buffer.m_size];
- memcpy(data, buffer.data, buffer.m_size);
- }
- else
- data = NULL;
- }
+
+ // Disable class copy
+ Buffer(const Buffer &) = delete;
+ Buffer &operator=(const Buffer &) = delete;
+
Buffer(Buffer &&buffer)
{
m_size = buffer.m_size;
@@ -81,21 +90,6 @@ public:
drop();
}
- Buffer& operator=(const Buffer &buffer)
- {
- if(this == &buffer)
- return *this;
- drop();
- m_size = buffer.m_size;
- if(m_size != 0)
- {
- data = new T[buffer.m_size];
- memcpy(data, buffer.data, buffer.m_size);
- }
- else
- data = NULL;
- return *this;
- }
Buffer& operator=(Buffer &&buffer)
{
if(this == &buffer)
@@ -113,6 +107,18 @@ public:
return *this;
}
+ void copyTo(Buffer &buffer) const
+ {
+ buffer.drop();
+ buffer.m_size = m_size;
+ if (m_size != 0) {
+ buffer.data = new T[m_size];
+ memcpy(buffer.data, data, m_size);
+ } else {
+ buffer.data = nullptr;
+ }
+ }
+
T & operator[](unsigned int i) const
{
return data[i];
diff --git a/src/util/serialize.cpp b/src/util/serialize.cpp
index d770101f2..281061229 100644
--- a/src/util/serialize.cpp
+++ b/src/util/serialize.cpp
@@ -248,7 +248,7 @@ std::string serializeJsonStringIfNeeded(const std::string &s)
std::string deSerializeJsonStringIfNeeded(std::istream &is)
{
- std::ostringstream tmp_os;
+ std::stringstream tmp_os(std::ios_base::binary | std::ios_base::in | std::ios_base::out);
bool expect_initial_quote = true;
bool is_json = false;
bool was_backslash = false;
@@ -280,8 +280,7 @@ std::string deSerializeJsonStringIfNeeded(std::istream &is)
expect_initial_quote = false;
}
if (is_json) {
- std::istringstream tmp_is(tmp_os.str(), std::ios::binary);
- return deSerializeJsonString(tmp_is);
+ return deSerializeJsonString(tmp_os);
}
return tmp_os.str();
diff --git a/src/util/string.cpp b/src/util/string.cpp
index 611ad35cb..bc4664997 100644
--- a/src/util/string.cpp
+++ b/src/util/string.cpp
@@ -30,7 +30,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <array>
#include <sstream>
#include <iomanip>
-#include <map>
+#include <unordered_map>
#ifndef _WIN32
#include <iconv.h>
@@ -39,15 +39,16 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <windows.h>
#endif
-#if defined(_ICONV_H_) && (defined(__FreeBSD__) || defined(__NetBSD__) || \
- defined(__OpenBSD__) || defined(__DragonFly__))
+#ifdef __NetBSD__
+ #include <sys/param.h>
+ #if __NetBSD_Version__ <= 999001500
+ #define BSD_ICONV_USED
+ #endif
+#elif defined(_ICONV_H_) && (defined(__FreeBSD__) || defined(__OpenBSD__) || \
+ defined(__DragonFly__))
#define BSD_ICONV_USED
#endif
-static bool parseHexColorString(const std::string &value, video::SColor &color,
- unsigned char default_alpha = 0xff);
-static bool parseNamedColorString(const std::string &value, video::SColor &color);
-
#ifndef _WIN32
static bool convert(const char *to, const char *from, char *outbuf,
@@ -83,6 +84,14 @@ static bool convert(const char *to, const char *from, char *outbuf,
#ifdef __ANDROID__
// On Android iconv disagrees how big a wchar_t is for whatever reason
const char *DEFAULT_ENCODING = "UTF-32LE";
+#elif defined(__NetBSD__)
+ // NetBSD does not allow "WCHAR_T" as a charset input to iconv.
+ #include <sys/endian.h>
+ #if BYTE_ORDER == BIG_ENDIAN
+ const char *DEFAULT_ENCODING = "UTF-32BE";
+ #else
+ const char *DEFAULT_ENCODING = "UTF-32LE";
+ #endif
#else
const char *DEFAULT_ENCODING = "WCHAR_T";
#endif
@@ -98,7 +107,7 @@ std::wstring utf8_to_wide(const std::string &input)
std::wstring out;
out.resize(outbuf_size / sizeof(wchar_t));
-#ifdef __ANDROID__
+#if defined(__ANDROID__) || defined(__NetBSD__)
SANITY_CHECK(sizeof(wchar_t) == 4);
#endif
@@ -324,29 +333,10 @@ u64 read_seed(const char *str)
return num;
}
-bool parseColorString(const std::string &value, video::SColor &color, bool quiet,
- unsigned char default_alpha)
-{
- bool success;
-
- if (value[0] == '#')
- success = parseHexColorString(value, color, default_alpha);
- else
- success = parseNamedColorString(value, color);
-
- if (!success && !quiet)
- errorstream << "Invalid color: \"" << value << "\"" << std::endl;
-
- return success;
-}
-
static bool parseHexColorString(const std::string &value, video::SColor &color,
unsigned char default_alpha)
{
- unsigned char components[] = { 0x00, 0x00, 0x00, default_alpha }; // R,G,B,A
-
- if (value[0] != '#')
- return false;
+ u8 components[] = {0x00, 0x00, 0x00, default_alpha}; // R,G,B,A
size_t len = value.size();
bool short_form;
@@ -358,198 +348,182 @@ static bool parseHexColorString(const std::string &value, video::SColor &color,
else
return false;
- bool success = true;
-
for (size_t pos = 1, cc = 0; pos < len; pos++, cc++) {
- assert(cc < sizeof components / sizeof components[0]);
if (short_form) {
- unsigned char d;
- if (!hex_digit_decode(value[pos], d)) {
- success = false;
- break;
- }
+ u8 d;
+ if (!hex_digit_decode(value[pos], d))
+ return false;
+
components[cc] = (d & 0xf) << 4 | (d & 0xf);
} else {
- unsigned char d1, d2;
+ u8 d1, d2;
if (!hex_digit_decode(value[pos], d1) ||
- !hex_digit_decode(value[pos+1], d2)) {
- success = false;
- break;
- }
+ !hex_digit_decode(value[pos+1], d2))
+ return false;
+
components[cc] = (d1 & 0xf) << 4 | (d2 & 0xf);
- pos++; // skip the second digit -- it's already used
+ pos++; // skip the second digit -- it's already used
}
}
- if (success) {
- color.setRed(components[0]);
- color.setGreen(components[1]);
- color.setBlue(components[2]);
- color.setAlpha(components[3]);
- }
+ color.setRed(components[0]);
+ color.setGreen(components[1]);
+ color.setBlue(components[2]);
+ color.setAlpha(components[3]);
- return success;
+ return true;
}
-struct ColorContainer {
- ColorContainer();
- std::map<const std::string, u32> colors;
+const static std::unordered_map<std::string, u32> s_named_colors = {
+ {"aliceblue", 0xf0f8ff},
+ {"antiquewhite", 0xfaebd7},
+ {"aqua", 0x00ffff},
+ {"aquamarine", 0x7fffd4},
+ {"azure", 0xf0ffff},
+ {"beige", 0xf5f5dc},
+ {"bisque", 0xffe4c4},
+ {"black", 00000000},
+ {"blanchedalmond", 0xffebcd},
+ {"blue", 0x0000ff},
+ {"blueviolet", 0x8a2be2},
+ {"brown", 0xa52a2a},
+ {"burlywood", 0xdeb887},
+ {"cadetblue", 0x5f9ea0},
+ {"chartreuse", 0x7fff00},
+ {"chocolate", 0xd2691e},
+ {"coral", 0xff7f50},
+ {"cornflowerblue", 0x6495ed},
+ {"cornsilk", 0xfff8dc},
+ {"crimson", 0xdc143c},
+ {"cyan", 0x00ffff},
+ {"darkblue", 0x00008b},
+ {"darkcyan", 0x008b8b},
+ {"darkgoldenrod", 0xb8860b},
+ {"darkgray", 0xa9a9a9},
+ {"darkgreen", 0x006400},
+ {"darkgrey", 0xa9a9a9},
+ {"darkkhaki", 0xbdb76b},
+ {"darkmagenta", 0x8b008b},
+ {"darkolivegreen", 0x556b2f},
+ {"darkorange", 0xff8c00},
+ {"darkorchid", 0x9932cc},
+ {"darkred", 0x8b0000},
+ {"darksalmon", 0xe9967a},
+ {"darkseagreen", 0x8fbc8f},
+ {"darkslateblue", 0x483d8b},
+ {"darkslategray", 0x2f4f4f},
+ {"darkslategrey", 0x2f4f4f},
+ {"darkturquoise", 0x00ced1},
+ {"darkviolet", 0x9400d3},
+ {"deeppink", 0xff1493},
+ {"deepskyblue", 0x00bfff},
+ {"dimgray", 0x696969},
+ {"dimgrey", 0x696969},
+ {"dodgerblue", 0x1e90ff},
+ {"firebrick", 0xb22222},
+ {"floralwhite", 0xfffaf0},
+ {"forestgreen", 0x228b22},
+ {"fuchsia", 0xff00ff},
+ {"gainsboro", 0xdcdcdc},
+ {"ghostwhite", 0xf8f8ff},
+ {"gold", 0xffd700},
+ {"goldenrod", 0xdaa520},
+ {"gray", 0x808080},
+ {"green", 0x008000},
+ {"greenyellow", 0xadff2f},
+ {"grey", 0x808080},
+ {"honeydew", 0xf0fff0},
+ {"hotpink", 0xff69b4},
+ {"indianred", 0xcd5c5c},
+ {"indigo", 0x4b0082},
+ {"ivory", 0xfffff0},
+ {"khaki", 0xf0e68c},
+ {"lavender", 0xe6e6fa},
+ {"lavenderblush", 0xfff0f5},
+ {"lawngreen", 0x7cfc00},
+ {"lemonchiffon", 0xfffacd},
+ {"lightblue", 0xadd8e6},
+ {"lightcoral", 0xf08080},
+ {"lightcyan", 0xe0ffff},
+ {"lightgoldenrodyellow", 0xfafad2},
+ {"lightgray", 0xd3d3d3},
+ {"lightgreen", 0x90ee90},
+ {"lightgrey", 0xd3d3d3},
+ {"lightpink", 0xffb6c1},
+ {"lightsalmon", 0xffa07a},
+ {"lightseagreen", 0x20b2aa},
+ {"lightskyblue", 0x87cefa},
+ {"lightslategray", 0x778899},
+ {"lightslategrey", 0x778899},
+ {"lightsteelblue", 0xb0c4de},
+ {"lightyellow", 0xffffe0},
+ {"lime", 0x00ff00},
+ {"limegreen", 0x32cd32},
+ {"linen", 0xfaf0e6},
+ {"magenta", 0xff00ff},
+ {"maroon", 0x800000},
+ {"mediumaquamarine", 0x66cdaa},
+ {"mediumblue", 0x0000cd},
+ {"mediumorchid", 0xba55d3},
+ {"mediumpurple", 0x9370db},
+ {"mediumseagreen", 0x3cb371},
+ {"mediumslateblue", 0x7b68ee},
+ {"mediumspringgreen", 0x00fa9a},
+ {"mediumturquoise", 0x48d1cc},
+ {"mediumvioletred", 0xc71585},
+ {"midnightblue", 0x191970},
+ {"mintcream", 0xf5fffa},
+ {"mistyrose", 0xffe4e1},
+ {"moccasin", 0xffe4b5},
+ {"navajowhite", 0xffdead},
+ {"navy", 0x000080},
+ {"oldlace", 0xfdf5e6},
+ {"olive", 0x808000},
+ {"olivedrab", 0x6b8e23},
+ {"orange", 0xffa500},
+ {"orangered", 0xff4500},
+ {"orchid", 0xda70d6},
+ {"palegoldenrod", 0xeee8aa},
+ {"palegreen", 0x98fb98},
+ {"paleturquoise", 0xafeeee},
+ {"palevioletred", 0xdb7093},
+ {"papayawhip", 0xffefd5},
+ {"peachpuff", 0xffdab9},
+ {"peru", 0xcd853f},
+ {"pink", 0xffc0cb},
+ {"plum", 0xdda0dd},
+ {"powderblue", 0xb0e0e6},
+ {"purple", 0x800080},
+ {"red", 0xff0000},
+ {"rosybrown", 0xbc8f8f},
+ {"royalblue", 0x4169e1},
+ {"saddlebrown", 0x8b4513},
+ {"salmon", 0xfa8072},
+ {"sandybrown", 0xf4a460},
+ {"seagreen", 0x2e8b57},
+ {"seashell", 0xfff5ee},
+ {"sienna", 0xa0522d},
+ {"silver", 0xc0c0c0},
+ {"skyblue", 0x87ceeb},
+ {"slateblue", 0x6a5acd},
+ {"slategray", 0x708090},
+ {"slategrey", 0x708090},
+ {"snow", 0xfffafa},
+ {"springgreen", 0x00ff7f},
+ {"steelblue", 0x4682b4},
+ {"tan", 0xd2b48c},
+ {"teal", 0x008080},
+ {"thistle", 0xd8bfd8},
+ {"tomato", 0xff6347},
+ {"turquoise", 0x40e0d0},
+ {"violet", 0xee82ee},
+ {"wheat", 0xf5deb3},
+ {"white", 0xffffff},
+ {"whitesmoke", 0xf5f5f5},
+ {"yellow", 0xffff00},
+ {"yellowgreen", 0x9acd32}
};
-ColorContainer::ColorContainer()
-{
- colors["aliceblue"] = 0xf0f8ff;
- colors["antiquewhite"] = 0xfaebd7;
- colors["aqua"] = 0x00ffff;
- colors["aquamarine"] = 0x7fffd4;
- colors["azure"] = 0xf0ffff;
- colors["beige"] = 0xf5f5dc;
- colors["bisque"] = 0xffe4c4;
- colors["black"] = 00000000;
- colors["blanchedalmond"] = 0xffebcd;
- colors["blue"] = 0x0000ff;
- colors["blueviolet"] = 0x8a2be2;
- colors["brown"] = 0xa52a2a;
- colors["burlywood"] = 0xdeb887;
- colors["cadetblue"] = 0x5f9ea0;
- colors["chartreuse"] = 0x7fff00;
- colors["chocolate"] = 0xd2691e;
- colors["coral"] = 0xff7f50;
- colors["cornflowerblue"] = 0x6495ed;
- colors["cornsilk"] = 0xfff8dc;
- colors["crimson"] = 0xdc143c;
- colors["cyan"] = 0x00ffff;
- colors["darkblue"] = 0x00008b;
- colors["darkcyan"] = 0x008b8b;
- colors["darkgoldenrod"] = 0xb8860b;
- colors["darkgray"] = 0xa9a9a9;
- colors["darkgreen"] = 0x006400;
- colors["darkgrey"] = 0xa9a9a9;
- colors["darkkhaki"] = 0xbdb76b;
- colors["darkmagenta"] = 0x8b008b;
- colors["darkolivegreen"] = 0x556b2f;
- colors["darkorange"] = 0xff8c00;
- colors["darkorchid"] = 0x9932cc;
- colors["darkred"] = 0x8b0000;
- colors["darksalmon"] = 0xe9967a;
- colors["darkseagreen"] = 0x8fbc8f;
- colors["darkslateblue"] = 0x483d8b;
- colors["darkslategray"] = 0x2f4f4f;
- colors["darkslategrey"] = 0x2f4f4f;
- colors["darkturquoise"] = 0x00ced1;
- colors["darkviolet"] = 0x9400d3;
- colors["deeppink"] = 0xff1493;
- colors["deepskyblue"] = 0x00bfff;
- colors["dimgray"] = 0x696969;
- colors["dimgrey"] = 0x696969;
- colors["dodgerblue"] = 0x1e90ff;
- colors["firebrick"] = 0xb22222;
- colors["floralwhite"] = 0xfffaf0;
- colors["forestgreen"] = 0x228b22;
- colors["fuchsia"] = 0xff00ff;
- colors["gainsboro"] = 0xdcdcdc;
- colors["ghostwhite"] = 0xf8f8ff;
- colors["gold"] = 0xffd700;
- colors["goldenrod"] = 0xdaa520;
- colors["gray"] = 0x808080;
- colors["green"] = 0x008000;
- colors["greenyellow"] = 0xadff2f;
- colors["grey"] = 0x808080;
- colors["honeydew"] = 0xf0fff0;
- colors["hotpink"] = 0xff69b4;
- colors["indianred"] = 0xcd5c5c;
- colors["indigo"] = 0x4b0082;
- colors["ivory"] = 0xfffff0;
- colors["khaki"] = 0xf0e68c;
- colors["lavender"] = 0xe6e6fa;
- colors["lavenderblush"] = 0xfff0f5;
- colors["lawngreen"] = 0x7cfc00;
- colors["lemonchiffon"] = 0xfffacd;
- colors["lightblue"] = 0xadd8e6;
- colors["lightcoral"] = 0xf08080;
- colors["lightcyan"] = 0xe0ffff;
- colors["lightgoldenrodyellow"] = 0xfafad2;
- colors["lightgray"] = 0xd3d3d3;
- colors["lightgreen"] = 0x90ee90;
- colors["lightgrey"] = 0xd3d3d3;
- colors["lightpink"] = 0xffb6c1;
- colors["lightsalmon"] = 0xffa07a;
- colors["lightseagreen"] = 0x20b2aa;
- colors["lightskyblue"] = 0x87cefa;
- colors["lightslategray"] = 0x778899;
- colors["lightslategrey"] = 0x778899;
- colors["lightsteelblue"] = 0xb0c4de;
- colors["lightyellow"] = 0xffffe0;
- colors["lime"] = 0x00ff00;
- colors["limegreen"] = 0x32cd32;
- colors["linen"] = 0xfaf0e6;
- colors["magenta"] = 0xff00ff;
- colors["maroon"] = 0x800000;
- colors["mediumaquamarine"] = 0x66cdaa;
- colors["mediumblue"] = 0x0000cd;
- colors["mediumorchid"] = 0xba55d3;
- colors["mediumpurple"] = 0x9370db;
- colors["mediumseagreen"] = 0x3cb371;
- colors["mediumslateblue"] = 0x7b68ee;
- colors["mediumspringgreen"] = 0x00fa9a;
- colors["mediumturquoise"] = 0x48d1cc;
- colors["mediumvioletred"] = 0xc71585;
- colors["midnightblue"] = 0x191970;
- colors["mintcream"] = 0xf5fffa;
- colors["mistyrose"] = 0xffe4e1;
- colors["moccasin"] = 0xffe4b5;
- colors["navajowhite"] = 0xffdead;
- colors["navy"] = 0x000080;
- colors["oldlace"] = 0xfdf5e6;
- colors["olive"] = 0x808000;
- colors["olivedrab"] = 0x6b8e23;
- colors["orange"] = 0xffa500;
- colors["orangered"] = 0xff4500;
- colors["orchid"] = 0xda70d6;
- colors["palegoldenrod"] = 0xeee8aa;
- colors["palegreen"] = 0x98fb98;
- colors["paleturquoise"] = 0xafeeee;
- colors["palevioletred"] = 0xdb7093;
- colors["papayawhip"] = 0xffefd5;
- colors["peachpuff"] = 0xffdab9;
- colors["peru"] = 0xcd853f;
- colors["pink"] = 0xffc0cb;
- colors["plum"] = 0xdda0dd;
- colors["powderblue"] = 0xb0e0e6;
- colors["purple"] = 0x800080;
- colors["red"] = 0xff0000;
- colors["rosybrown"] = 0xbc8f8f;
- colors["royalblue"] = 0x4169e1;
- colors["saddlebrown"] = 0x8b4513;
- colors["salmon"] = 0xfa8072;
- colors["sandybrown"] = 0xf4a460;
- colors["seagreen"] = 0x2e8b57;
- colors["seashell"] = 0xfff5ee;
- colors["sienna"] = 0xa0522d;
- colors["silver"] = 0xc0c0c0;
- colors["skyblue"] = 0x87ceeb;
- colors["slateblue"] = 0x6a5acd;
- colors["slategray"] = 0x708090;
- colors["slategrey"] = 0x708090;
- colors["snow"] = 0xfffafa;
- colors["springgreen"] = 0x00ff7f;
- colors["steelblue"] = 0x4682b4;
- colors["tan"] = 0xd2b48c;
- colors["teal"] = 0x008080;
- colors["thistle"] = 0xd8bfd8;
- colors["tomato"] = 0xff6347;
- colors["turquoise"] = 0x40e0d0;
- colors["violet"] = 0xee82ee;
- colors["wheat"] = 0xf5deb3;
- colors["white"] = 0xffffff;
- colors["whitesmoke"] = 0xf5f5f5;
- colors["yellow"] = 0xffff00;
- colors["yellowgreen"] = 0x9acd32;
-
-}
-
-static const ColorContainer named_colors;
-
static bool parseNamedColorString(const std::string &value, video::SColor &color)
{
std::string color_name;
@@ -570,9 +544,8 @@ static bool parseNamedColorString(const std::string &value, video::SColor &color
color_name = lowercase(color_name);
- std::map<const std::string, unsigned>::const_iterator it;
- it = named_colors.colors.find(color_name);
- if (it == named_colors.colors.end())
+ auto it = s_named_colors.find(color_name);
+ if (it == s_named_colors.end())
return false;
u32 color_temp = it->second;
@@ -580,21 +553,26 @@ static bool parseNamedColorString(const std::string &value, video::SColor &color
/* An empty string for alpha is ok (none of the color table entries
* have an alpha value either). Color strings without an alpha specified
* are interpreted as fully opaque
- *
- * For named colors the supplied alpha string (representing a hex value)
- * must be exactly two digits. For example: colorname#08
*/
if (!alpha_string.empty()) {
- if (alpha_string.length() != 2)
- return false;
-
- unsigned char d1, d2;
- if (!hex_digit_decode(alpha_string.at(0), d1)
- || !hex_digit_decode(alpha_string.at(1), d2))
+ if (alpha_string.size() == 1) {
+ u8 d;
+ if (!hex_digit_decode(alpha_string[0], d))
+ return false;
+
+ color_temp |= ((d & 0xf) << 4 | (d & 0xf)) << 24;
+ } else if (alpha_string.size() == 2) {
+ u8 d1, d2;
+ if (!hex_digit_decode(alpha_string[0], d1)
+ || !hex_digit_decode(alpha_string[1], d2))
+ return false;
+
+ color_temp |= ((d1 & 0xf) << 4 | (d2 & 0xf)) << 24;
+ } else {
return false;
- color_temp |= ((d1 & 0xf) << 4 | (d2 & 0xf)) << 24;
+ }
} else {
- color_temp |= 0xff << 24; // Fully opaque
+ color_temp |= 0xff << 24; // Fully opaque
}
color = video::SColor(color_temp);
@@ -602,6 +580,22 @@ static bool parseNamedColorString(const std::string &value, video::SColor &color
return true;
}
+bool parseColorString(const std::string &value, video::SColor &color, bool quiet,
+ unsigned char default_alpha)
+{
+ bool success;
+
+ if (value[0] == '#')
+ success = parseHexColorString(value, color, default_alpha);
+ else
+ success = parseNamedColorString(value, color);
+
+ if (!success && !quiet)
+ errorstream << "Invalid color: \"" << value << "\"" << std::endl;
+
+ return success;
+}
+
void str_replace(std::string &str, char from, char to)
{
std::replace(str.begin(), str.end(), from, to);
@@ -893,3 +887,19 @@ std::string sanitizeDirName(const std::string &str, const std::string &optional_
return wide_to_utf8(safe_name);
}
+
+
+void safe_print_string(std::ostream &os, const std::string &str)
+{
+ std::ostream::fmtflags flags = os.flags();
+ os << std::hex;
+ for (const char c : str) {
+ if (IS_ASCII_PRINTABLE_CHAR(c) || IS_UTF8_MULTB_START(c) ||
+ IS_UTF8_MULTB_INNER(c) || c == '\n' || c == '\t') {
+ os << c;
+ } else {
+ os << '<' << std::setw(2) << (int)c << '>';
+ }
+ }
+ os.setf(flags);
+}
diff --git a/src/util/string.h b/src/util/string.h
index d4afcaec8..8a9e83f22 100644
--- a/src/util/string.h
+++ b/src/util/string.h
@@ -661,24 +661,49 @@ inline const char *bool_to_cstr(bool val)
return val ? "true" : "false";
}
+/**
+ * Converts a duration in seconds to a pretty-printed duration in
+ * days, hours, minutes and seconds.
+ *
+ * @param sec duration in seconds
+ * @return pretty-printed duration
+ */
inline const std::string duration_to_string(int sec)
{
+ std::ostringstream ss;
+ const char *neg = "";
+ if (sec < 0) {
+ sec = -sec;
+ neg = "-";
+ }
+ int total_sec = sec;
int min = sec / 60;
sec %= 60;
int hour = min / 60;
min %= 60;
+ int day = hour / 24;
+ hour %= 24;
+
+ if (day > 0) {
+ ss << neg << day << "d";
+ if (hour > 0 || min > 0 || sec > 0)
+ ss << " ";
+ }
- std::stringstream ss;
if (hour > 0) {
- ss << hour << "h ";
+ ss << neg << hour << "h";
+ if (min > 0 || sec > 0)
+ ss << " ";
}
if (min > 0) {
- ss << min << "m ";
+ ss << neg << min << "min";
+ if (sec > 0)
+ ss << " ";
}
- if (sec > 0) {
- ss << sec << "s ";
+ if (sec > 0 || total_sec == 0) {
+ ss << neg << sec << "s";
}
return ss.str();
@@ -728,3 +753,11 @@ inline irr::core::stringw utf8_to_stringw(const std::string &input)
* 2. Remove 'unsafe' characters from the name by replacing them with '_'
*/
std::string sanitizeDirName(const std::string &str, const std::string &optional_prefix);
+
+/**
+ * Prints a sanitized version of a string without control characters.
+ * '\t' and '\n' are allowed, as are UTF-8 control characters (e.g. RTL).
+ * ASCII control characters are replaced with their hex encoding in angle
+ * brackets (e.g. "a\x1eb" -> "a<1e>b").
+ */
+void safe_print_string(std::ostream &os, const std::string &str);
diff --git a/src/version.cpp b/src/version.cpp
index c555f30af..f2aac37df 100644
--- a/src/version.cpp
+++ b/src/version.cpp
@@ -37,7 +37,6 @@ const char *g_build_info =
#ifndef SERVER
"USE_GETTEXT=" STR(USE_GETTEXT) "\n"
"USE_SOUND=" STR(USE_SOUND) "\n"
- "USE_FREETYPE=" STR(USE_FREETYPE) "\n"
#endif
"STATIC_SHAREDIR=" STR(STATIC_SHAREDIR)
#if USE_GETTEXT && defined(STATIC_LOCALEDIR)