aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShadowNinja <shadowninja@minetest.net>2013-12-18 16:35:55 -0500
committerShadowNinja <shadowninja@minetest.net>2013-12-18 16:35:55 -0500
commit49cec3f78240ed6310a9b5dd05ce09a79ed5a12e (patch)
tree38e8199e49906357939e74b6ed0b00f6f54de976
parent38d112033b3ba0ea0360fced334a279576aafc5d (diff)
downloadminetest-49cec3f78240ed6310a9b5dd05ce09a79ed5a12e.tar.gz
minetest-49cec3f78240ed6310a9b5dd05ce09a79ed5a12e.tar.bz2
minetest-49cec3f78240ed6310a9b5dd05ce09a79ed5a12e.zip
Handle LuaErrors in Lua -> C++ calls on LuaJIT
-rw-r--r--src/cmake_config.h.in1
-rw-r--r--src/config.h3
-rw-r--r--src/exceptions.h115
-rw-r--r--src/game.cpp10
-rw-r--r--src/script/common/c_content.cpp2
-rw-r--r--src/script/common/c_internal.cpp12
-rw-r--r--src/script/common/c_internal.h1
-rw-r--r--src/script/common/c_types.cpp8
-rw-r--r--src/script/common/c_types.h5
-rw-r--r--src/script/cpp_api/s_base.cpp11
-rw-r--r--src/script/cpp_api/s_inventory.cpp6
-rw-r--r--src/script/cpp_api/s_nodemeta.cpp6
-rw-r--r--src/script/cpp_api/s_server.cpp12
-rw-r--r--src/script/lua_api/l_base.h2
-rw-r--r--src/script/lua_api/l_craft.cpp28
-rw-r--r--src/script/lua_api/l_item.cpp4
-rw-r--r--src/script/lua_api/l_noise.cpp4
-rw-r--r--src/server.h17
18 files changed, 113 insertions, 134 deletions
diff --git a/src/cmake_config.h.in b/src/cmake_config.h.in
index 7cc2fb99e..75e448f79 100644
--- a/src/cmake_config.h.in
+++ b/src/cmake_config.h.in
@@ -12,6 +12,7 @@
#define CMAKE_USE_FREETYPE @USE_FREETYPE@
#define CMAKE_STATIC_SHAREDIR "@SHAREDIR@"
#define CMAKE_USE_LEVELDB @USE_LEVELDB@
+#define CMAKE_USE_LUAJIT @USE_LUAJIT@
#ifdef NDEBUG
#define CMAKE_BUILD_TYPE "Release"
diff --git a/src/config.h b/src/config.h
index 652c9008a..1c0aac4f2 100644
--- a/src/config.h
+++ b/src/config.h
@@ -14,6 +14,7 @@
#define USE_FREETYPE 0
#define STATIC_SHAREDIR ""
#define USE_LEVELDB 0
+#define USE_LUAJIT 0
#ifdef USE_CMAKE_CONFIG_H
#include "cmake_config.h"
@@ -33,6 +34,8 @@
#define STATIC_SHAREDIR CMAKE_STATIC_SHAREDIR
#undef USE_LEVELDB
#define USE_LEVELDB CMAKE_USE_LEVELDB
+ #undef USE_LUAJIT
+ #define USE_LUAJIT CMAKE_USE_LUAJIT
#endif
#endif
diff --git a/src/exceptions.h b/src/exceptions.h
index 085b42417..6fb97f3ed 100644
--- a/src/exceptions.h
+++ b/src/exceptions.h
@@ -21,132 +21,99 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#define EXCEPTIONS_HEADER
#include <exception>
+#include <string>
+
class BaseException : public std::exception
{
public:
- BaseException(const char *s)
+ BaseException(const std::string s) throw()
{
m_s = s;
}
+ ~BaseException() throw() {}
virtual const char * what() const throw()
{
- return m_s;
+ return m_s.c_str();
}
- const char *m_s;
+protected:
+ std::string m_s;
};
-class AsyncQueuedException : public BaseException
-{
+class AsyncQueuedException : public BaseException {
public:
- AsyncQueuedException(const char *s):
- BaseException(s)
- {}
+ AsyncQueuedException(std::string s): BaseException(s) {}
};
-class NotImplementedException : public BaseException
-{
+class NotImplementedException : public BaseException {
public:
- NotImplementedException(const char *s):
- BaseException(s)
- {}
+ NotImplementedException(std::string s): BaseException(s) {}
};
-class AlreadyExistsException : public BaseException
-{
+class AlreadyExistsException : public BaseException {
public:
- AlreadyExistsException(const char *s):
- BaseException(s)
- {}
+ AlreadyExistsException(std::string s): BaseException(s) {}
};
-class VersionMismatchException : public BaseException
-{
+class VersionMismatchException : public BaseException {
public:
- VersionMismatchException(const char *s):
- BaseException(s)
- {}
+ VersionMismatchException(std::string s): BaseException(s) {}
};
-class FileNotGoodException : public BaseException
-{
+class FileNotGoodException : public BaseException {
public:
- FileNotGoodException(const char *s):
- BaseException(s)
- {}
+ FileNotGoodException(std::string s): BaseException(s) {}
};
-class SerializationError : public BaseException
-{
+class SerializationError : public BaseException {
public:
- SerializationError(const char *s):
- BaseException(s)
- {}
+ SerializationError(std::string s): BaseException(s) {}
};
-class LoadError : public BaseException
-{
+class LoadError : public BaseException {
public:
- LoadError(const char *s):
- BaseException(s)
- {}
+ LoadError(std::string s): BaseException(s) {}
};
-class ContainerFullException : public BaseException
-{
+class ContainerFullException : public BaseException {
public:
- ContainerFullException(const char *s):
- BaseException(s)
- {}
+ ContainerFullException(std::string s): BaseException(s) {}
};
-class SettingNotFoundException : public BaseException
-{
+class SettingNotFoundException : public BaseException {
public:
- SettingNotFoundException(const char *s):
- BaseException(s)
- {}
+ SettingNotFoundException(std::string s): BaseException(s) {}
};
-class InvalidFilenameException : public BaseException
-{
+class InvalidFilenameException : public BaseException {
public:
- InvalidFilenameException(const char *s):
- BaseException(s)
- {}
+ InvalidFilenameException(std::string s): BaseException(s) {}
};
-class ProcessingLimitException : public BaseException
-{
+class ProcessingLimitException : public BaseException {
public:
- ProcessingLimitException(const char *s):
- BaseException(s)
- {}
+ ProcessingLimitException(std::string s): BaseException(s) {}
};
-class CommandLineError : public BaseException
-{
+class CommandLineError : public BaseException {
public:
- CommandLineError(const char *s):
- BaseException(s)
- {}
+ CommandLineError(std::string s): BaseException(s) {}
};
-class ItemNotFoundException : public BaseException
-{
+class ItemNotFoundException : public BaseException {
public:
- ItemNotFoundException(const char *s):
- BaseException(s)
- {}
+ ItemNotFoundException(std::string s): BaseException(s) {}
+};
+
+class ServerError : public BaseException {
+public:
+ ServerError(std::string s): BaseException(s) {}
};
// Only used on Windows (SEH)
-class FatalSystemException : public BaseException
-{
+class FatalSystemException : public BaseException {
public:
- FatalSystemException(const char *s):
- BaseException(s)
- {}
+ FatalSystemException(std::string s): BaseException(s) {}
};
/*
@@ -159,7 +126,7 @@ public:
InvalidPositionException():
BaseException("Somebody tried to get/set something in a nonexistent position.")
{}
- InvalidPositionException(const char *s):
+ InvalidPositionException(std::string s):
BaseException(s)
{}
};
diff --git a/src/game.cpp b/src/game.cpp
index cc5d3d81d..6f48870ad 100644
--- a/src/game.cpp
+++ b/src/game.cpp
@@ -3438,14 +3438,12 @@ void the_game(
L" running a different version of Minetest.";
errorstream<<wide_to_narrow(error_message)<<std::endl;
}
- catch(ServerError &e)
- {
+ catch(ServerError &e) {
error_message = narrow_to_wide(e.what());
- errorstream<<wide_to_narrow(error_message)<<std::endl;
+ errorstream << "ServerError: " << e.what() << std::endl;
}
- catch(ModError &e)
- {
- errorstream<<e.what()<<std::endl;
+ catch(ModError &e) {
+ errorstream << "ModError: " << e.what() << std::endl;
error_message = narrow_to_wide(e.what()) + wgettext("\nCheck debug.txt for details.");
}
diff --git a/src/script/common/c_content.cpp b/src/script/common/c_content.cpp
index d58479042..cf9f28d30 100644
--- a/src/script/common/c_content.cpp
+++ b/src/script/common/c_content.cpp
@@ -653,7 +653,7 @@ ItemStack read_item(lua_State* L, int index,Server* srv)
}
else
{
- throw LuaError(L, "Expecting itemstack, itemstring, table or nil");
+ throw LuaError(NULL, "Expecting itemstack, itemstring, table or nil");
}
}
diff --git a/src/script/common/c_internal.cpp b/src/script/common/c_internal.cpp
index 2866cfe86..90846676f 100644
--- a/src/script/common/c_internal.cpp
+++ b/src/script/common/c_internal.cpp
@@ -55,6 +55,18 @@ int script_error_handler(lua_State *L) {
return 1;
}
+int script_exception_wrapper(lua_State *L, lua_CFunction f)
+{
+ try {
+ return f(L); // Call wrapped function and return result.
+ } catch (const char *s) { // Catch and convert exceptions.
+ lua_pushstring(L, s);
+ } catch (LuaError& e) {
+ lua_pushstring(L, e.what());
+ }
+ return lua_error(L); // Rethrow as a Lua error.
+}
+
void script_error(lua_State *L)
{
const char *s = lua_tostring(L, -1);
diff --git a/src/script/common/c_internal.h b/src/script/common/c_internal.h
index eb6aa06e8..f3ef18d70 100644
--- a/src/script/common/c_internal.h
+++ b/src/script/common/c_internal.h
@@ -66,6 +66,7 @@ enum RunCallbacksMode
std::string script_get_backtrace(lua_State *L);
int script_error_handler(lua_State *L);
+int script_exception_wrapper(lua_State *L, lua_CFunction f);
void script_error(lua_State *L);
void script_run_callbacks(lua_State *L, int nargs,
RunCallbacksMode mode);
diff --git a/src/script/common/c_types.cpp b/src/script/common/c_types.cpp
index a6faf9819..6ffad1cb1 100644
--- a/src/script/common/c_types.cpp
+++ b/src/script/common/c_types.cpp
@@ -23,10 +23,12 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "common/c_internal.h"
#include "itemdef.h"
-LuaError::LuaError(lua_State *L, const std::string &s)
+LuaError::LuaError(lua_State *L, const std::string &s) :
+ ServerError(s)
{
- m_s = "LuaError: " + s;
- if (L) m_s += '\n' + script_get_backtrace(L);
+ if (L) {
+ m_s += '\n' + script_get_backtrace(L);
+ }
}
struct EnumString es_ItemType[] =
diff --git a/src/script/common/c_types.h b/src/script/common/c_types.h
index bc9f1cb96..709d4f34b 100644
--- a/src/script/common/c_types.h
+++ b/src/script/common/c_types.h
@@ -26,6 +26,8 @@ extern "C" {
#include <iostream>
+#include "exceptions.h"
+
struct EnumString
{
int num;
@@ -50,7 +52,7 @@ public:
}
};
-class LuaError : public std::exception
+class LuaError : public ServerError
{
public:
LuaError(lua_State *L, const std::string &s);
@@ -61,7 +63,6 @@ public:
{
return m_s.c_str();
}
- std::string m_s;
};
diff --git a/src/script/cpp_api/s_base.cpp b/src/script/cpp_api/s_base.cpp
index b957dc64f..898271743 100644
--- a/src/script/cpp_api/s_base.cpp
+++ b/src/script/cpp_api/s_base.cpp
@@ -29,6 +29,9 @@ with this program; if not, write to the Free Software Foundation, Inc.,
extern "C" {
#include "lualib.h"
+#if USE_LUAJIT
+ #include "luajit.h"
+#endif
}
#include <stdio.h>
@@ -73,6 +76,14 @@ ScriptApiBase::ScriptApiBase()
lua_pushlightuserdata(m_luastack, this);
lua_setfield(m_luastack, LUA_REGISTRYINDEX, "scriptapi");
+ // If we are using LuaJIT add a C++ wrapper function to catch
+ // exceptions thrown in Lua -> C++ calls
+#if USE_LUAJIT
+ lua_pushlightuserdata(m_luastack, (void*) script_exception_wrapper);
+ luaJIT_setmode(m_luastack, -1, LUAJIT_MODE_WRAPCFUNC | LUAJIT_MODE_ON);
+ lua_pop(m_luastack, 1);
+#endif
+
m_server = 0;
m_environment = 0;
m_guiengine = 0;
diff --git a/src/script/cpp_api/s_inventory.cpp b/src/script/cpp_api/s_inventory.cpp
index 4ee6e4be0..8542f0292 100644
--- a/src/script/cpp_api/s_inventory.cpp
+++ b/src/script/cpp_api/s_inventory.cpp
@@ -54,7 +54,7 @@ int ScriptApiDetached::detached_inventory_AllowMove(
if(lua_pcall(L, 7, 1, errorhandler))
scriptError();
if(!lua_isnumber(L, -1))
- throw LuaError(L, "allow_move should return a number");
+ throw LuaError(NULL, "allow_move should return a number");
int ret = luaL_checkinteger(L, -1);
lua_pop(L, 2); // Pop integer and error handler
return ret;
@@ -86,7 +86,7 @@ int ScriptApiDetached::detached_inventory_AllowPut(
if(lua_pcall(L, 5, 1, errorhandler))
scriptError();
if(!lua_isnumber(L, -1))
- throw LuaError(L, "allow_put should return a number");
+ throw LuaError(NULL, "allow_put should return a number");
int ret = luaL_checkinteger(L, -1);
lua_pop(L, 2); // Pop integer and error handler
return ret;
@@ -118,7 +118,7 @@ int ScriptApiDetached::detached_inventory_AllowTake(
if(lua_pcall(L, 5, 1, errorhandler))
scriptError();
if(!lua_isnumber(L, -1))
- throw LuaError(L, "allow_take should return a number");
+ throw LuaError(NULL, "allow_take should return a number");
int ret = luaL_checkinteger(L, -1);
lua_pop(L, 2); // Pop integer and error handler
return ret;
diff --git a/src/script/cpp_api/s_nodemeta.cpp b/src/script/cpp_api/s_nodemeta.cpp
index 1f04383f1..d8fd4dcfe 100644
--- a/src/script/cpp_api/s_nodemeta.cpp
+++ b/src/script/cpp_api/s_nodemeta.cpp
@@ -61,7 +61,7 @@ int ScriptApiNodemeta::nodemeta_inventory_AllowMove(v3s16 p,
scriptError();
lua_remove(L, errorhandler); // Remove error handler
if(!lua_isnumber(L, -1))
- throw LuaError(L, "allow_metadata_inventory_move should return a number");
+ throw LuaError(NULL, "allow_metadata_inventory_move should return a number");
int num = luaL_checkinteger(L, -1);
lua_pop(L, 1); // Pop integer
return num;
@@ -99,7 +99,7 @@ int ScriptApiNodemeta::nodemeta_inventory_AllowPut(v3s16 p,
scriptError();
lua_remove(L, errorhandler); // Remove error handler
if(!lua_isnumber(L, -1))
- throw LuaError(L, "allow_metadata_inventory_put should return a number");
+ throw LuaError(NULL, "allow_metadata_inventory_put should return a number");
int num = luaL_checkinteger(L, -1);
lua_pop(L, 1); // Pop integer
return num;
@@ -137,7 +137,7 @@ int ScriptApiNodemeta::nodemeta_inventory_AllowTake(v3s16 p,
scriptError();
lua_remove(L, errorhandler); // Remove error handler
if(!lua_isnumber(L, -1))
- throw LuaError(L, "allow_metadata_inventory_take should return a number");
+ throw LuaError(NULL, "allow_metadata_inventory_take should return a number");
int num = luaL_checkinteger(L, -1);
lua_pop(L, 1); // Pop integer
return num;
diff --git a/src/script/cpp_api/s_server.cpp b/src/script/cpp_api/s_server.cpp
index 4baf90636..98320b578 100644
--- a/src/script/cpp_api/s_server.cpp
+++ b/src/script/cpp_api/s_server.cpp
@@ -33,7 +33,7 @@ bool ScriptApiServer::getAuth(const std::string &playername,
getAuthHandler();
lua_getfield(L, -1, "get_auth");
if(lua_type(L, -1) != LUA_TFUNCTION)
- throw LuaError(L, "Authentication handler missing get_auth");
+ throw LuaError(NULL, "Authentication handler missing get_auth");
lua_pushstring(L, playername.c_str());
if(lua_pcall(L, 1, 1, errorhandler))
scriptError();
@@ -48,13 +48,13 @@ bool ScriptApiServer::getAuth(const std::string &playername,
std::string password;
bool found = getstringfield(L, -1, "password", password);
if(!found)
- throw LuaError(L, "Authentication handler didn't return password");
+ throw LuaError(NULL, "Authentication handler didn't return password");
if(dst_password)
*dst_password = password;
lua_getfield(L, -1, "privileges");
if(!lua_istable(L, -1))
- throw LuaError(L, "Authentication handler didn't return privilege table");
+ throw LuaError(NULL, "Authentication handler didn't return privilege table");
if(dst_privs)
readPrivileges(-1, *dst_privs);
lua_pop(L, 1);
@@ -74,7 +74,7 @@ void ScriptApiServer::getAuthHandler()
}
lua_remove(L, -2); // Remove minetest
if(lua_type(L, -1) != LUA_TTABLE)
- throw LuaError(L, "Authentication handler table not valid");
+ throw LuaError(NULL, "Authentication handler table not valid");
}
void ScriptApiServer::readPrivileges(int index, std::set<std::string> &result)
@@ -108,7 +108,7 @@ void ScriptApiServer::createAuth(const std::string &playername,
lua_getfield(L, -1, "create_auth");
lua_remove(L, -2); // Remove auth handler
if(lua_type(L, -1) != LUA_TFUNCTION)
- throw LuaError(L, "Authentication handler missing create_auth");
+ throw LuaError(NULL, "Authentication handler missing create_auth");
lua_pushstring(L, playername.c_str());
lua_pushstring(L, password.c_str());
if(lua_pcall(L, 2, 0, errorhandler))
@@ -128,7 +128,7 @@ bool ScriptApiServer::setPassword(const std::string &playername,
lua_getfield(L, -1, "set_password");
lua_remove(L, -2); // Remove auth handler
if(lua_type(L, -1) != LUA_TFUNCTION)
- throw LuaError(L, "Authentication handler missing set_password");
+ throw LuaError(NULL, "Authentication handler missing set_password");
lua_pushstring(L, playername.c_str());
lua_pushstring(L, password.c_str());
if(lua_pcall(L, 2, 1, errorhandler))
diff --git a/src/script/lua_api/l_base.h b/src/script/lua_api/l_base.h
index 808043bd4..694ce5a1e 100644
--- a/src/script/lua_api/l_base.h
+++ b/src/script/lua_api/l_base.h
@@ -48,7 +48,7 @@ protected:
ScriptApiBase *scriptIface = getScriptApiBase(L);
T *scriptIfaceDowncast = dynamic_cast<T*>(scriptIface);
if (!scriptIfaceDowncast) {
- throw LuaError(L, "Requested unavailable ScriptApi - core engine bug!");
+ throw LuaError(NULL, "Requested unavailable ScriptApi - core engine bug!");
}
return scriptIfaceDowncast;
}
diff --git a/src/script/lua_api/l_craft.cpp b/src/script/lua_api/l_craft.cpp
index 035abb78d..ef13aa82c 100644
--- a/src/script/lua_api/l_craft.cpp
+++ b/src/script/lua_api/l_craft.cpp
@@ -150,16 +150,16 @@ int ModApiCraft::l_register_craft(lua_State *L)
if(type == "shaped"){
std::string output = getstringfield_default(L, table, "output", "");
if(output == "")
- throw LuaError(L, "Crafting definition is missing an output");
+ throw LuaError(NULL, "Crafting definition is missing an output");
int width = 0;
std::vector<std::string> recipe;
lua_getfield(L, table, "recipe");
if(lua_isnil(L, -1))
- throw LuaError(L, "Crafting definition is missing a recipe"
+ throw LuaError(NULL, "Crafting definition is missing a recipe"
" (output=\"" + output + "\")");
if(!readCraftRecipeShaped(L, -1, width, recipe))
- throw LuaError(L, "Invalid crafting recipe"
+ throw LuaError(NULL, "Invalid crafting recipe"
" (output=\"" + output + "\")");
CraftReplacements replacements;
@@ -167,7 +167,7 @@ int ModApiCraft::l_register_craft(lua_State *L)
if(!lua_isnil(L, -1))
{
if(!readCraftReplacements(L, -1, replacements))
- throw LuaError(L, "Invalid replacements"
+ throw LuaError(NULL, "Invalid replacements"
" (output=\"" + output + "\")");
}
@@ -181,17 +181,17 @@ int ModApiCraft::l_register_craft(lua_State *L)
else if(type == "shapeless"){
std::string output = getstringfield_default(L, table, "output", "");
if(output == "")
- throw LuaError(L, "Crafting definition (shapeless)"
+ throw LuaError(NULL, "Crafting definition (shapeless)"
" is missing an output");
std::vector<std::string> recipe;
lua_getfield(L, table, "recipe");
if(lua_isnil(L, -1))
- throw LuaError(L, "Crafting definition (shapeless)"
+ throw LuaError(NULL, "Crafting definition (shapeless)"
" is missing a recipe"
" (output=\"" + output + "\")");
if(!readCraftRecipeShapeless(L, -1, recipe))
- throw LuaError(L, "Invalid crafting recipe"
+ throw LuaError(NULL, "Invalid crafting recipe"
" (output=\"" + output + "\")");
CraftReplacements replacements;
@@ -199,7 +199,7 @@ int ModApiCraft::l_register_craft(lua_State *L)
if(!lua_isnil(L, -1))
{
if(!readCraftReplacements(L, -1, replacements))
- throw LuaError(L, "Invalid replacements"
+ throw LuaError(NULL, "Invalid replacements"
" (output=\"" + output + "\")");
}
@@ -224,12 +224,12 @@ int ModApiCraft::l_register_craft(lua_State *L)
else if(type == "cooking"){
std::string output = getstringfield_default(L, table, "output", "");
if(output == "")
- throw LuaError(L, "Crafting definition (cooking)"
+ throw LuaError(NULL, "Crafting definition (cooking)"
" is missing an output");
std::string recipe = getstringfield_default(L, table, "recipe", "");
if(recipe == "")
- throw LuaError(L, "Crafting definition (cooking)"
+ throw LuaError(NULL, "Crafting definition (cooking)"
" is missing a recipe"
" (output=\"" + output + "\")");
@@ -240,7 +240,7 @@ int ModApiCraft::l_register_craft(lua_State *L)
if(!lua_isnil(L, -1))
{
if(!readCraftReplacements(L, -1, replacements))
- throw LuaError(L, "Invalid replacements"
+ throw LuaError(NULL, "Invalid replacements"
" (cooking output=\"" + output + "\")");
}
@@ -254,7 +254,7 @@ int ModApiCraft::l_register_craft(lua_State *L)
else if(type == "fuel"){
std::string recipe = getstringfield_default(L, table, "recipe", "");
if(recipe == "")
- throw LuaError(L, "Crafting definition (fuel)"
+ throw LuaError(NULL, "Crafting definition (fuel)"
" is missing a recipe");
float burntime = getfloatfield_default(L, table, "burntime", 1.0);
@@ -264,7 +264,7 @@ int ModApiCraft::l_register_craft(lua_State *L)
if(!lua_isnil(L, -1))
{
if(!readCraftReplacements(L, -1, replacements))
- throw LuaError(L, "Invalid replacements"
+ throw LuaError(NULL, "Invalid replacements"
" (fuel recipe=\"" + recipe + "\")");
}
@@ -274,7 +274,7 @@ int ModApiCraft::l_register_craft(lua_State *L)
}
else
{
- throw LuaError(L, "Unknown crafting definition type: \"" + type + "\"");
+ throw LuaError(NULL, "Unknown crafting definition type: \"" + type + "\"");
}
lua_pop(L, 1);
diff --git a/src/script/lua_api/l_item.cpp b/src/script/lua_api/l_item.cpp
index 512cd7398..4b5c89792 100644
--- a/src/script/lua_api/l_item.cpp
+++ b/src/script/lua_api/l_item.cpp
@@ -470,7 +470,7 @@ int ModApiItemMod::l_register_item_raw(lua_State *L)
name = lua_tostring(L, -1);
verbosestream<<"register_item_raw: "<<name<<std::endl;
} else {
- throw LuaError(L, "register_item_raw: name is not defined or not a string");
+ throw LuaError(NULL, "register_item_raw: name is not defined or not a string");
}
// Check if on_use is defined
@@ -500,7 +500,7 @@ int ModApiItemMod::l_register_item_raw(lua_State *L)
content_t id = ndef->set(f.name, f);
if(id > MAX_REGISTERED_CONTENT){
- throw LuaError(L, "Number of registerable nodes ("
+ throw LuaError(NULL, "Number of registerable nodes ("
+ itos(MAX_REGISTERED_CONTENT+1)
+ ") exceeded (" + name + ")");
}
diff --git a/src/script/lua_api/l_noise.cpp b/src/script/lua_api/l_noise.cpp
index 4b0c7932d..4ca9992af 100644
--- a/src/script/lua_api/l_noise.cpp
+++ b/src/script/lua_api/l_noise.cpp
@@ -330,10 +330,10 @@ int LuaPseudoRandom::l_next(lua_State *L)
max = luaL_checkinteger(L, 3);
if(max < min){
errorstream<<"PseudoRandom.next(): max="<<max<<" min="<<min<<std::endl;
- throw LuaError(L, "PseudoRandom.next(): max < min");
+ throw LuaError(NULL, "PseudoRandom.next(): max < min");
}
if(max - min != 32767 && max - min > 32767/5)
- throw LuaError(L, "PseudoRandom.next() max-min is not 32767"
+ throw LuaError(NULL, "PseudoRandom.next() max-min is not 32767"
" and is > 32768/5. This is disallowed due to"
" the bad random distribution the"
" implementation would otherwise make.");
diff --git a/src/server.h b/src/server.h
index e71a811ea..28678f59c 100644
--- a/src/server.h
+++ b/src/server.h
@@ -55,23 +55,6 @@ class ServerEnvironment;
struct SimpleSoundSpec;
-class ServerError : public std::exception
-{
-public:
- ServerError(const std::string &s)
- {
- m_s = "ServerError: ";
- m_s += s;
- }
- virtual ~ServerError() throw()
- {}
- virtual const char * what() const throw()
- {
- return m_s.c_str();
- }
- std::string m_s;
-};
-
/*
Some random functions
*/
an class="hl opt">->setVisible(false); m_scrollbar->setPos(0); setTabStop(true); setTabOrder(-1); updateAbsolutePosition(); 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 * g_settings->getFloat("gui_scaling"); m_scrollbar->setRelativePosition(core::rect<s32>( relative_rect.LowerRightCorner.X-width,relative_rect.UpperLeftCorner.Y, relative_rect.LowerRightCorner.X,relative_rect.LowerRightCorner.Y )); } GUITable::~GUITable() { for (GUITable::Row &row : m_rows) delete[] row.cells; if (m_font) m_font->drop(); m_scrollbar->remove(); } GUITable::Option GUITable::splitOption(const std::string &str) { size_t equal_pos = str.find('='); if (equal_pos == std::string::npos) return GUITable::Option(str, ""); return GUITable::Option(str.substr(0, equal_pos), str.substr(equal_pos + 1)); } void GUITable::setTextList(const std::vector<std::string> &content, bool transparent) { clear(); if (transparent) { m_background.setAlpha(0); m_border = false; } m_is_textlist = true; s32 empty_string_index = allocString(""); m_rows.resize(content.size()); for (s32 i = 0; i < (s32) content.size(); ++i) { Row *row = &m_rows[i]; row->cells = new Cell[1]; row->cellcount = 1; row->indent = 0; row->visible_index = i; m_visible_rows.push_back(i); Cell *cell = row->cells; cell->xmin = 0; cell->xmax = 0x7fff; // something large enough cell->xpos = 6; cell->content_type = COLUMN_TYPE_TEXT; cell->content_index = empty_string_index; cell->tooltip_index = empty_string_index; cell->color.set(255, 255, 255, 255); cell->color_defined = false; cell->reported_column = 1; // parse row content (color) const std::string &s = content[i]; if (s[0] == '#' && s[1] == '#') { // double # to escape cell->content_index = allocString(s.substr(2)); } else if (s[0] == '#' && s.size() >= 7 && parseColorString( s.substr(0,7), cell->color, false)) { // single # for color cell->color_defined = true; cell->content_index = allocString(s.substr(7)); } else { // no #, just text cell->content_index = allocString(s); } } allocationComplete(); // Clamp scroll bar position updateScrollBar(); } void GUITable::setTable(const TableOptions &options, const TableColumns &columns, std::vector<std::string> &content) { clear(); // Naming conventions: // i is always a row index, 0-based // j is always a column index, 0-based // k is another index, for example an option index // Handle a stupid error case... (issue #1187) if (columns.empty()) { TableColumn text_column; text_column.type = "text"; TableColumns new_columns; new_columns.push_back(text_column); setTable(options, new_columns, content); return; } // Handle table options video::SColor default_color(255, 255, 255, 255); s32 opendepth = 0; for (const Option &option : options) { const std::string &name = option.name; const std::string &value = option.value; if (name == "color") parseColorString(value, m_color, false); else if (name == "background") parseColorString(value, m_background, false); else if (name == "border") m_border = is_yes(value); else if (name == "highlight") parseColorString(value, m_highlight, false); else if (name == "highlight_text") parseColorString(value, m_highlight_text, false); else if (name == "opendepth") opendepth = stoi(value); else errorstream<<"Invalid table option: \""<<name<<"\"" <<" (value=\""<<value<<"\")"<<std::endl; } // Get number of columns and rows // note: error case columns.size() == 0 was handled above s32 colcount = columns.size(); assert(colcount >= 1); // rowcount = ceil(cellcount / colcount) but use integer arithmetic s32 rowcount = (content.size() + colcount - 1) / colcount; assert(rowcount >= 0); // Append empty strings to content if there is an incomplete row s32 cellcount = rowcount * colcount; while (content.size() < (u32) cellcount) content.emplace_back(""); // Create temporary rows (for processing columns) struct TempRow { // Current horizontal position (may different between rows due // to indent/tree columns, or text/image columns with width<0) s32 x; // Tree indentation level s32 indent; // Next cell: Index into m_strings or m_images s32 content_index; // Next cell: Width in pixels s32 content_width; // Vector of completed cells in this row std::vector<Cell> cells; // Stores colors and how long they last (maximum column index) std::vector<std::pair<video::SColor, s32> > colors; TempRow(): x(0), indent(0), content_index(0), content_width(0) {} }; TempRow *rows = new TempRow[rowcount]; // Get em width. Pedantically speaking, the width of "M" is not // necessarily the same as the em width, but whatever, close enough. s32 em = 6; if (m_font) em = m_font->getDimension(L"M").Width; s32 default_tooltip_index = allocString(""); std::map<s32, s32> active_image_indices; // Process content in column-major order for (s32 j = 0; j < colcount; ++j) { // Check column type ColumnType columntype = COLUMN_TYPE_TEXT; if (columns[j].type == "text") columntype = COLUMN_TYPE_TEXT; else if (columns[j].type == "image") columntype = COLUMN_TYPE_IMAGE; else if (columns[j].type == "color") columntype = COLUMN_TYPE_COLOR; else if (columns[j].type == "indent") columntype = COLUMN_TYPE_INDENT; else if (columns[j].type == "tree") columntype = COLUMN_TYPE_TREE; else errorstream<<"Invalid table column type: \"" <<columns[j].type<<"\""<<std::endl; // Process column options s32 padding = myround(0.5 * em); s32 tooltip_index = default_tooltip_index; s32 align = 0; s32 width = 0; s32 span = colcount; if (columntype == COLUMN_TYPE_INDENT) { padding = 0; // default indent padding } if (columntype == COLUMN_TYPE_INDENT || columntype == COLUMN_TYPE_TREE) { width = myround(em * 1.5); // default indent width } for (const Option &option : columns[j].options) { const std::string &name = option.name; const std::string &value = option.value; if (name == "padding") padding = myround(stof(value) * em); else if (name == "tooltip") tooltip_index = allocString(value); else if (name == "align" && value == "left") align = 0; else if (name == "align" && value == "center") align = 1; else if (name == "align" && value == "right") align = 2; else if (name == "align" && value == "inline") align = 3; else if (name == "width") width = myround(stof(value) * em); else if (name == "span" && columntype == COLUMN_TYPE_COLOR) span = stoi(value); else if (columntype == COLUMN_TYPE_IMAGE && !name.empty() && string_allowed(name, "0123456789")) { s32 content_index = allocImage(value); active_image_indices.insert(std::make_pair( stoi(name), content_index)); } else { errorstream<<"Invalid table column option: \""<<name<<"\"" <<" (value=\""<<value<<"\")"<<std::endl; } } // If current column type can use information from "color" columns, // find out which of those is currently active if (columntype == COLUMN_TYPE_TEXT) { for (s32 i = 0; i < rowcount; ++i) { TempRow *row = &rows[i]; while (!row->colors.empty() && row->colors.back().second < j) row->colors.pop_back(); } } // Make template for new cells Cell newcell; newcell.content_type = columntype; newcell.tooltip_index = tooltip_index; newcell.reported_column = j+1; if (columntype == COLUMN_TYPE_TEXT) { // Find right edge of column s32 xmax = 0; for (s32 i = 0; i < rowcount; ++i) { TempRow *row = &rows[i]; row->content_index = allocString(content[i * colcount + j]); const core::stringw &text = m_strings[row->content_index]; row->content_width = m_font ? m_font->getDimension(text.c_str()).Width : 0; row->content_width = MYMAX(row->content_width, width); s32 row_xmax = row->x + padding + row->content_width; xmax = MYMAX(xmax, row_xmax); } // Add a new cell (of text type) to each row for (s32 i = 0; i < rowcount; ++i) { newcell.xmin = rows[i].x + padding; alignContent(&newcell, xmax, rows[i].content_width, align); newcell.content_index = rows[i].content_index; newcell.color_defined = !rows[i].colors.empty(); if (newcell.color_defined) newcell.color = rows[i].colors.back().first; rows[i].cells.push_back(newcell); rows[i].x = newcell.xmax; } } else if (columntype == COLUMN_TYPE_IMAGE) { // Find right edge of column s32 xmax = 0; for (s32 i = 0; i < rowcount; ++i) { TempRow *row = &rows[i]; row->content_index = -1; // Find content_index. Image indices are defined in // column options so check active_image_indices. s32 image_index = stoi(content[i * colcount + j]); std::map<s32, s32>::iterator image_iter = active_image_indices.find(image_index); if (image_iter != active_image_indices.end()) row->content_index = image_iter->second; // Get texture object (might be NULL) video::ITexture *image = NULL; if (row->content_index >= 0) image = m_images[row->content_index]; // Get content width and update xmax row->content_width = image ? image->getOriginalSize().Width : 0; row->content_width = MYMAX(row->content_width, width); s32 row_xmax = row->x + padding + row->content_width; xmax = MYMAX(xmax, row_xmax); } // Add a new cell (of image type) to each row for (s32 i = 0; i < rowcount; ++i) { newcell.xmin = rows[i].x + padding; alignContent(&newcell, xmax, rows[i].content_width, align); newcell.content_index = rows[i].content_index; rows[i].cells.push_back(newcell); rows[i].x = newcell.xmax; } active_image_indices.clear(); } else if (columntype == COLUMN_TYPE_COLOR) { for (s32 i = 0; i < rowcount; ++i) { video::SColor cellcolor(255, 255, 255, 255); if (parseColorString(content[i * colcount + j], cellcolor, true)) rows[i].colors.emplace_back(cellcolor, j+span); } } else if (columntype == COLUMN_TYPE_INDENT || columntype == COLUMN_TYPE_TREE) { // For column type "tree", reserve additional space for +/- // Also enable special processing for treeview-type tables s32 content_width = 0; if (columntype == COLUMN_TYPE_TREE) { content_width = m_font ? m_font->getDimension(L"+").Width : 0; m_has_tree_column = true; } // Add a new cell (of indent or tree type) to each row for (s32 i = 0; i < rowcount; ++i) { TempRow *row = &rows[i]; s32 indentlevel = stoi(content[i * colcount + j]); indentlevel = MYMAX(indentlevel, 0); if (columntype == COLUMN_TYPE_TREE) row->indent = indentlevel; newcell.xmin = row->x + padding; newcell.xpos = newcell.xmin + indentlevel * width; newcell.xmax = newcell.xpos + content_width; newcell.content_index = 0; newcell.color_defined = !rows[i].colors.empty(); if (newcell.color_defined) newcell.color = rows[i].colors.back().first; row->cells.push_back(newcell); row->x = newcell.xmax; } } } // Copy temporary rows to not so temporary rows if (rowcount >= 1) { m_rows.resize(rowcount); for (s32 i = 0; i < rowcount; ++i) { Row *row = &m_rows[i]; row->cellcount = rows[i].cells.size(); row->cells = new Cell[row->cellcount]; memcpy((void*) row->cells, (void*) &rows[i].cells[0], row->cellcount * sizeof(Cell)); row->indent = rows[i].indent; row->visible_index = i; m_visible_rows.push_back(i); } } if (m_has_tree_column) { // Treeview: convert tree to indent cells on leaf rows for (s32 i = 0; i < rowcount; ++i) { if (i == rowcount-1 || m_rows[i].indent >= m_rows[i+1].indent) for (s32 j = 0; j < m_rows[i].cellcount; ++j) if (m_rows[i].cells[j].content_type == COLUMN_TYPE_TREE) m_rows[i].cells[j].content_type = COLUMN_TYPE_INDENT; } // Treeview: close rows according to opendepth option std::set<s32> opened_trees; for (s32 i = 0; i < rowcount; ++i) if (m_rows[i].indent < opendepth) opened_trees.insert(i); setOpenedTrees(opened_trees); } // Delete temporary information used only during setTable() delete[] rows; allocationComplete(); // Clamp scroll bar position updateScrollBar(); } void GUITable::clear() { // Clean up cells and rows for (GUITable::Row &row : m_rows) delete[] row.cells; m_rows.clear(); m_visible_rows.clear(); // Get colors from skin gui::IGUISkin *skin = Environment->getSkin(); m_color = skin->getColor(gui::EGDC_BUTTON_TEXT); m_background = skin->getColor(gui::EGDC_3D_HIGH_LIGHT); m_highlight = skin->getColor(gui::EGDC_HIGH_LIGHT); m_highlight_text = skin->getColor(gui::EGDC_HIGH_LIGHT_TEXT); // Reset members m_is_textlist = false; m_has_tree_column = false; m_selected = -1; m_sel_column = 0; m_sel_doubleclick = false; m_keynav_time = 0; m_keynav_buffer = L""; m_border = true; m_strings.clear(); m_images.clear(); m_alloc_strings.clear(); m_alloc_images.clear(); } std::string GUITable::checkEvent() { s32 sel = getSelected(); assert(sel >= 0); if (sel == 0) { return "INV"; } std::ostringstream os(std::ios::binary); if (m_sel_doubleclick) { os<<"DCL:"; m_sel_doubleclick = false; } else { os<<"CHG:"; } os<<sel; if (!m_is_textlist) { os<<":"<<m_sel_column; } return os.str(); } s32 GUITable::getSelected() const { if (m_selected < 0) return 0; assert(m_selected >= 0 && m_selected < (s32) m_visible_rows.size()); return m_visible_rows[m_selected] + 1; } void GUITable::setSelected(s32 index) { s32 old_selected = m_selected; m_selected = -1; m_sel_column = 0; m_sel_doubleclick = false; --index; // Switch from 1-based indexing to 0-based indexing s32 rowcount = m_rows.size(); if (rowcount == 0 || index < 0) { return; } if (index >= rowcount) { index = rowcount - 1; } // If the selected row is not visible, open its ancestors to make it visible bool selection_invisible = m_rows[index].visible_index < 0; if (selection_invisible) { std::set<s32> opened_trees; getOpenedTrees(opened_trees); s32 indent = m_rows[index].indent; for (s32 j = index - 1; j >= 0; --j) { if (m_rows[j].indent < indent) { opened_trees.insert(j); indent = m_rows[j].indent; } } setOpenedTrees(opened_trees); } if (index >= 0) { m_selected = m_rows[index].visible_index; assert(m_selected >= 0 && m_selected < (s32) m_visible_rows.size()); } if (m_selected != old_selected || selection_invisible) { autoScroll(); } } GUITable::DynamicData GUITable::getDynamicData() const { DynamicData dyndata; dyndata.selected = getSelected(); dyndata.scrollpos = m_scrollbar->getPos(); dyndata.keynav_time = m_keynav_time; dyndata.keynav_buffer = m_keynav_buffer; if (m_has_tree_column) getOpenedTrees(dyndata.opened_trees); return dyndata; } void GUITable::setDynamicData(const DynamicData &dyndata) { if (m_has_tree_column) setOpenedTrees(dyndata.opened_trees); m_keynav_time = dyndata.keynav_time; m_keynav_buffer = dyndata.keynav_buffer; setSelected(dyndata.selected); m_sel_column = 0; m_sel_doubleclick = false; m_scrollbar->setPos(dyndata.scrollpos); } const c8* GUITable::getTypeName() const { return "GUITable"; } void GUITable::updateAbsolutePosition() { IGUIElement::updateAbsolutePosition(); updateScrollBar(); } void GUITable::draw() { if (!IsVisible) return; gui::IGUISkin *skin = Environment->getSkin(); // draw background bool draw_background = m_background.getAlpha() > 0; if (m_border) skin->draw3DSunkenPane(this, m_background, true, draw_background, AbsoluteRect, &AbsoluteClippingRect); else if (draw_background) skin->draw2DRectangle(this, m_background, AbsoluteRect, &AbsoluteClippingRect); // get clipping rect core::rect<s32> client_clip(AbsoluteRect); client_clip.UpperLeftCorner.Y += 1; client_clip.UpperLeftCorner.X += 1; client_clip.LowerRightCorner.Y -= 1; client_clip.LowerRightCorner.X -= 1; if (m_scrollbar->isVisible()) { client_clip.LowerRightCorner.X = m_scrollbar->getAbsolutePosition().UpperLeftCorner.X; } client_clip.clipAgainst(AbsoluteClippingRect); // draw visible rows s32 scrollpos = m_scrollbar->getPos(); s32 row_min = scrollpos / m_rowheight; s32 row_max = (scrollpos + AbsoluteRect.getHeight() - 1) / m_rowheight + 1; row_max = MYMIN(row_max, (s32) m_visible_rows.size()); core::rect<s32> row_rect(AbsoluteRect); if (m_scrollbar->isVisible()) row_rect.LowerRightCorner.X -= skin->getSize(gui::EGDS_SCROLLBAR_SIZE); row_rect.UpperLeftCorner.Y += row_min * m_rowheight - scrollpos; row_rect.LowerRightCorner.Y = row_rect.UpperLeftCorner.Y + m_rowheight; for (s32 i = row_min; i < row_max; ++i) { Row *row = &m_rows[m_visible_rows[i]]; bool is_sel = i == m_selected; video::SColor color = m_color; if (is_sel) { skin->draw2DRectangle(this, m_highlight, row_rect, &client_clip); color = m_highlight_text; } for (s32 j = 0; j < row->cellcount; ++j) drawCell(&row->cells[j], color, row_rect, client_clip); row_rect.UpperLeftCorner.Y += m_rowheight; row_rect.LowerRightCorner.Y += m_rowheight; } // Draw children IGUIElement::draw(); } void GUITable::drawCell(const Cell *cell, video::SColor color, const core::rect<s32> &row_rect, const core::rect<s32> &client_clip) { if ((cell->content_type == COLUMN_TYPE_TEXT) || (cell->content_type == COLUMN_TYPE_TREE)) { core::rect<s32> text_rect = row_rect; text_rect.UpperLeftCorner.X = row_rect.UpperLeftCorner.X + cell->xpos; text_rect.LowerRightCorner.X = row_rect.UpperLeftCorner.X + cell->xmax; if (cell->color_defined) color = cell->color; if (m_font) { if (cell->content_type == COLUMN_TYPE_TEXT) m_font->draw(m_strings[cell->content_index], text_rect, color, false, true, &client_clip); else // tree m_font->draw(cell->content_index ? L"+" : L"-", text_rect, color, false, true, &client_clip); } } else if (cell->content_type == COLUMN_TYPE_IMAGE) { if (cell->content_index < 0) return; video::IVideoDriver *driver = Environment->getVideoDriver(); video::ITexture *image = m_images[cell->content_index]; if (image) { core::position2d<s32> dest_pos = row_rect.UpperLeftCorner; dest_pos.X += cell->xpos; core::rect<s32> source_rect( core::position2d<s32>(0, 0), image->getOriginalSize()); s32 imgh = source_rect.LowerRightCorner.Y; s32 rowh = row_rect.getHeight(); if (imgh < rowh) dest_pos.Y += (rowh - imgh) / 2; else source_rect.LowerRightCorner.Y = rowh; video::SColor color(255, 255, 255, 255); driver->draw2DImage(image, dest_pos, source_rect, &client_clip, color, true); } } } bool GUITable::OnEvent(const SEvent &event) { if (!isEnabled()) return IGUIElement::OnEvent(event); if (event.EventType == EET_KEY_INPUT_EVENT) { if (event.KeyInput.PressedDown && ( event.KeyInput.Key == KEY_DOWN || event.KeyInput.Key == KEY_UP || event.KeyInput.Key == KEY_HOME || event.KeyInput.Key == KEY_END || event.KeyInput.Key == KEY_NEXT || event.KeyInput.Key == KEY_PRIOR)) { s32 offset = 0; switch (event.KeyInput.Key) { case KEY_DOWN: offset = 1; break; case KEY_UP: offset = -1; break; case KEY_HOME: offset = - (s32) m_visible_rows.size(); break; case KEY_END: offset = m_visible_rows.size(); break; case KEY_NEXT: offset = AbsoluteRect.getHeight() / m_rowheight; break; case KEY_PRIOR: offset = - (s32) (AbsoluteRect.getHeight() / m_rowheight); break; default: break; } s32 old_selected = m_selected; s32 rowcount = m_visible_rows.size(); if (rowcount != 0) { m_selected = rangelim(m_selected + offset, 0, rowcount-1); autoScroll(); } if (m_selected != old_selected) sendTableEvent(0, false); return true; } if (event.KeyInput.PressedDown && ( event.KeyInput.Key == KEY_LEFT || event.KeyInput.Key == KEY_RIGHT)) { // Open/close subtree via keyboard if (m_selected >= 0) { int dir = event.KeyInput.Key == KEY_LEFT ? -1 : 1; toggleVisibleTree(m_selected, dir, true); } return true; } else if (!event.KeyInput.PressedDown && ( event.KeyInput.Key == KEY_RETURN || event.KeyInput.Key == KEY_SPACE)) { sendTableEvent(0, true); return true; } else if (event.KeyInput.Key == KEY_ESCAPE || event.KeyInput.Key == KEY_SPACE) { // pass to parent } else if (event.KeyInput.PressedDown && event.KeyInput.Char) { // change selection based on text as it is typed u64 now = porting::getTimeMs(); if (now - m_keynav_time >= 500) m_keynav_buffer = L""; m_keynav_time = now; // add to key buffer if not a key repeat if (!(m_keynav_buffer.size() == 1 && m_keynav_buffer[0] == event.KeyInput.Char)) { m_keynav_buffer.append(event.KeyInput.Char); } // find the selected item, starting at the current selection // don't change selection if the key buffer matches the current item s32 old_selected = m_selected; s32 start = MYMAX(m_selected, 0); s32 rowcount = m_visible_rows.size(); for (s32 k = 1; k < rowcount; ++k) { s32 current = start + k; if (current >= rowcount) current -= rowcount; if (doesRowStartWith(getRow(current), m_keynav_buffer)) { m_selected = current; break; } } autoScroll(); if (m_selected != old_selected) sendTableEvent(0, false); return true; } } if (event.EventType == EET_MOUSE_INPUT_EVENT) { core::position2d<s32> p(event.MouseInput.X, event.MouseInput.Y); if (event.MouseInput.Event == EMIE_MOUSE_WHEEL) { m_scrollbar->setPos(m_scrollbar->getPos() + (event.MouseInput.Wheel < 0 ? -3 : 3) * - (s32) m_rowheight / 2); return true; } // Find hovered row and cell bool really_hovering = false; s32 row_i = getRowAt(p.Y, really_hovering); const Cell *cell = NULL; if (really_hovering) { s32 cell_j = getCellAt(p.X, row_i); if (cell_j >= 0) cell = &(getRow(row_i)->cells[cell_j]); } // Update tooltip setToolTipText(cell ? m_strings[cell->tooltip_index].c_str() : L""); // Fix for #1567/#1806: // IGUIScrollBar passes double click events to its parent, // which we don't want. Detect this case and discard the event if (event.MouseInput.Event != EMIE_MOUSE_MOVED && m_scrollbar->isVisible() && m_scrollbar->isPointInside(p)) return true; if (event.MouseInput.isLeftPressed() && (isPointInside(p) || event.MouseInput.Event == EMIE_MOUSE_MOVED)) { s32 sel_column = 0; bool sel_doubleclick = (event.MouseInput.Event == EMIE_LMOUSE_DOUBLE_CLICK); bool plusminus_clicked = false; // For certain events (left click), report column // Also open/close subtrees when the +/- is clicked if (cell && ( event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN || event.MouseInput.Event == EMIE_LMOUSE_DOUBLE_CLICK || event.MouseInput.Event == EMIE_LMOUSE_TRIPLE_CLICK)) { sel_column = cell->reported_column; if (cell->content_type == COLUMN_TYPE_TREE) plusminus_clicked = true; } if (plusminus_clicked) { if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) { toggleVisibleTree(row_i, 0, false); } } else { // Normal selection s32 old_selected = m_selected; m_selected = row_i; autoScroll(); if (m_selected != old_selected || sel_column >= 1 || sel_doubleclick) { sendTableEvent(sel_column, sel_doubleclick); } // Treeview: double click opens/closes trees if (m_has_tree_column && sel_doubleclick) { toggleVisibleTree(m_selected, 0, false); } } } return true; } if (event.EventType == EET_GUI_EVENT && event.GUIEvent.EventType == gui::EGET_SCROLL_BAR_CHANGED && event.GUIEvent.Caller == m_scrollbar) { // Don't pass events from our scrollbar to the parent return true; } return IGUIElement::OnEvent(event); } /******************************************************************************/ /* GUITable helper functions */ /******************************************************************************/ s32 GUITable::allocString(const std::string &text) { std::map<std::string, s32>::iterator it = m_alloc_strings.find(text); if (it == m_alloc_strings.end()) { s32 id = m_strings.size(); std::wstring wtext = utf8_to_wide(text); m_strings.emplace_back(wtext.c_str()); m_alloc_strings.insert(std::make_pair(text, id)); return id; } return it->second; } s32 GUITable::allocImage(const std::string &imagename) { std::map<std::string, s32>::iterator it = m_alloc_images.find(imagename); if (it == m_alloc_images.end()) { s32 id = m_images.size(); m_images.push_back(m_tsrc->getTexture(imagename)); m_alloc_images.insert(std::make_pair(imagename, id)); return id; } return it->second; } void GUITable::allocationComplete() { // Called when done with creating rows and cells from table data, // i.e. when allocString and allocImage won't be called anymore m_alloc_strings.clear(); m_alloc_images.clear(); } const GUITable::Row* GUITable::getRow(s32 i) const { if (i >= 0 && i < (s32) m_visible_rows.size()) return &m_rows[m_visible_rows[i]]; return NULL; } bool GUITable::doesRowStartWith(const Row *row, const core::stringw &str) const { if (row == NULL) return false; for (s32 j = 0; j < row->cellcount; ++j) { Cell *cell = &row->cells[j]; if (cell->content_type == COLUMN_TYPE_TEXT) { const core::stringw &cellstr = m_strings[cell->content_index]; if (cellstr.size() >= str.size() && str.equals_ignore_case(cellstr.subString(0, str.size()))) return true; } } return false; } s32 GUITable::getRowAt(s32 y, bool &really_hovering) const { really_hovering = false; s32 rowcount = m_visible_rows.size(); if (rowcount == 0) return -1; // Use arithmetic to find row s32 rel_y = y - AbsoluteRect.UpperLeftCorner.Y - 1; s32 i = (rel_y + m_scrollbar->getPos()) / m_rowheight; if (i >= 0 && i < rowcount) { really_hovering = true; return i; } if (i < 0)