mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-10-06 19:41:51 +00:00
Merge branch 'main' into volume-renderer
This commit is contained in:
commit
305f44d1c6
70 changed files with 802 additions and 580 deletions
|
|
@ -1,60 +1,92 @@
|
|||
#include "api_lua.hpp"
|
||||
|
||||
#include "io/io.hpp"
|
||||
#include "io/devices/MemoryDevice.hpp"
|
||||
#include "logic/scripting/scripting.hpp"
|
||||
#include "content/ContentControl.hpp"
|
||||
#include "devtools/Project.hpp"
|
||||
#include "engine/Engine.hpp"
|
||||
#include "engine/EnginePaths.hpp"
|
||||
#include "frontend/locale.hpp"
|
||||
#include "graphics/ui/elements/Menu.hpp"
|
||||
#include "graphics/ui/gui_util.hpp"
|
||||
#include "graphics/ui/GUI.hpp"
|
||||
#include "io/devices/MemoryDevice.hpp"
|
||||
#include "io/io.hpp"
|
||||
#include "io/settings_io.hpp"
|
||||
#include "logic/EngineController.hpp"
|
||||
#include "logic/LevelController.hpp"
|
||||
#include "logic/scripting/scripting.hpp"
|
||||
#include "network/Network.hpp"
|
||||
#include "util/platform.hpp"
|
||||
#include "util/stringutil.hpp"
|
||||
#include "window/Window.hpp"
|
||||
#include "world/Level.hpp"
|
||||
|
||||
using namespace scripting;
|
||||
|
||||
static int l_start_debug_instance(lua::State* L) {
|
||||
int port = lua::tointeger(L, 1);
|
||||
if (port == 0) {
|
||||
auto network = engine->getNetwork();
|
||||
if (network == nullptr) {
|
||||
throw std::runtime_error("project has no network permission");
|
||||
}
|
||||
port = network->findFreePort();
|
||||
if (port == -1) {
|
||||
throw std::runtime_error("could not find free port");
|
||||
}
|
||||
}
|
||||
const auto& paths = engine->getPaths();
|
||||
|
||||
std::vector<std::string> args {
|
||||
"--res", paths.getResourcesFolder().u8string(),
|
||||
"--dir", paths.getUserFilesFolder().u8string(),
|
||||
"--dbg-server", "tcp:" + std::to_string(port),
|
||||
};
|
||||
platform::new_engine_instance(std::move(args));
|
||||
return lua::pushinteger(L, port);
|
||||
/// @brief Check if content is loaded
|
||||
static int l_is_content_loaded(lua::State* L) {
|
||||
return lua::pushboolean(L, content != nullptr);
|
||||
}
|
||||
|
||||
static int l_focus(lua::State* L) {
|
||||
engine->getWindow().focus();
|
||||
/// @brief Load content
|
||||
static int l_load_content(lua::State* L) {
|
||||
content_control->loadContent();
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int l_create_memory_device(lua::State* L) {
|
||||
std::string name = lua::require_string(L, 1);
|
||||
if (io::get_device(name)) {
|
||||
/// @brief Reset content, excluding specified packs modules
|
||||
static int l_reset_content(lua::State* L) {
|
||||
if (level != nullptr) {
|
||||
throw std::runtime_error("world must be closed before");
|
||||
}
|
||||
std::vector<std::string> nonResetPacks;
|
||||
if (lua::istable(L, 1)) {
|
||||
int len = lua::objlen(L, 1);
|
||||
for (int i = 0; i < len; i++) {
|
||||
lua::rawgeti(L, i + 1, 1);
|
||||
nonResetPacks.emplace_back(lua::require_lstring(L, -1));
|
||||
lua::pop(L);
|
||||
}
|
||||
}
|
||||
content_control->resetContent(std::move(nonResetPacks));
|
||||
return 0;
|
||||
}
|
||||
|
||||
/// @brief Reconfigure packs
|
||||
/// @param addPacks An array of packs to add
|
||||
/// @param remPacks An array of packs to remove
|
||||
static int l_reconfig_packs(lua::State* L) {
|
||||
if (!lua::istable(L, 1)) {
|
||||
throw std::runtime_error("strings array expected as the first argument");
|
||||
}
|
||||
if (!lua::istable(L, 2)) {
|
||||
throw std::runtime_error("strings array expected as the second argument");
|
||||
}
|
||||
std::vector<std::string> addPacks;
|
||||
int addLen = lua::objlen(L, 1);
|
||||
for (int i = 0; i < addLen; i++) {
|
||||
lua::rawgeti(L, i + 1, 1);
|
||||
addPacks.emplace_back(lua::require_lstring(L, -1));
|
||||
lua::pop(L);
|
||||
}
|
||||
std::vector<std::string> remPacks;
|
||||
int remLen = lua::objlen(L, 2);
|
||||
for (int i = 0; i < remLen; i++) {
|
||||
lua::rawgeti(L, i + 1, 2);
|
||||
remPacks.emplace_back(lua::require_lstring(L, -1));
|
||||
lua::pop(L);
|
||||
}
|
||||
auto engineController = engine->getController();
|
||||
try {
|
||||
engineController->reconfigPacks(controller, addPacks, remPacks);
|
||||
} catch (const contentpack_error& err) {
|
||||
throw std::runtime_error(
|
||||
"entry-point '" + name + "' is already used"
|
||||
std::string(err.what()) + " [" + err.getPackId() + " ]"
|
||||
);
|
||||
}
|
||||
if (name.find(':') != std::string::npos) {
|
||||
throw std::runtime_error("invalid entry point name");
|
||||
}
|
||||
|
||||
io::set_device(name, std::make_unique<io::MemoryDevice>());
|
||||
return 0;
|
||||
}
|
||||
|
||||
/// @brief Get content sources list
|
||||
static int l_get_content_sources(lua::State* L) {
|
||||
const auto& sources = engine->getContentControl().getContentSources();
|
||||
lua::createtable(L, static_cast<int>(sources.size()), 0);
|
||||
|
|
@ -65,6 +97,7 @@ static int l_get_content_sources(lua::State* L) {
|
|||
return 1;
|
||||
}
|
||||
|
||||
/// @brief Set content sources list
|
||||
static int l_set_content_sources(lua::State* L) {
|
||||
if (!lua::istable(L, 1)) {
|
||||
throw std::runtime_error("table expected as argument 1");
|
||||
|
|
@ -80,18 +113,290 @@ static int l_set_content_sources(lua::State* L) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
/// @brief Reset content sources to defaults
|
||||
static int l_reset_content_sources(lua::State* L) {
|
||||
engine->getContentControl().resetContentSources();
|
||||
return 0;
|
||||
}
|
||||
|
||||
/// @brief Get a setting value
|
||||
/// @param name The name of the setting
|
||||
/// @return The value of the setting
|
||||
static int l_get_setting(lua::State* L) {
|
||||
auto name = lua::require_string(L, 1);
|
||||
const auto value = engine->getSettingsHandler().getValue(name);
|
||||
return lua::pushvalue(L, value);
|
||||
}
|
||||
|
||||
/// @brief Set a setting value
|
||||
/// @param name The name of the setting
|
||||
/// @param value The new value for the setting
|
||||
static int l_set_setting(lua::State* L) {
|
||||
auto name = lua::require_string(L, 1);
|
||||
const auto value = lua::tovalue(L, 2);
|
||||
engine->getSettingsHandler().setValue(name, value);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/// @brief Convert a setting value to a string
|
||||
/// @param name The name of the setting
|
||||
/// @return The string representation of the setting value
|
||||
static int l_str_setting(lua::State* L) {
|
||||
auto name = lua::require_string(L, 1);
|
||||
const auto string = engine->getSettingsHandler().toString(name);
|
||||
return lua::pushstring(L, string);
|
||||
}
|
||||
|
||||
/// @brief Get information about a setting
|
||||
/// @param name The name of the setting
|
||||
/// @return A table with information about the setting
|
||||
static int l_get_setting_info(lua::State* L) {
|
||||
auto name = lua::require_string(L, 1);
|
||||
auto setting = engine->getSettingsHandler().getSetting(name);
|
||||
lua::createtable(L, 0, 1);
|
||||
if (auto number = dynamic_cast<NumberSetting*>(setting)) {
|
||||
lua::pushnumber(L, number->getMin());
|
||||
lua::setfield(L, "min");
|
||||
lua::pushnumber(L, number->getMax());
|
||||
lua::setfield(L, "max");
|
||||
lua::pushnumber(L, number->getDefault());
|
||||
lua::setfield(L, "def");
|
||||
return 1;
|
||||
}
|
||||
if (auto integer = dynamic_cast<IntegerSetting*>(setting)) {
|
||||
lua::pushinteger(L, integer->getMin());
|
||||
lua::setfield(L, "min");
|
||||
lua::pushinteger(L, integer->getMax());
|
||||
lua::setfield(L, "max");
|
||||
lua::pushinteger(L, integer->getDefault());
|
||||
lua::setfield(L, "def");
|
||||
return 1;
|
||||
}
|
||||
if (auto boolean = dynamic_cast<FlagSetting*>(setting)) {
|
||||
lua::pushboolean(L, boolean->getDefault());
|
||||
lua::setfield(L, "def");
|
||||
return 1;
|
||||
}
|
||||
if (auto string = dynamic_cast<StringSetting*>(setting)) {
|
||||
lua::pushstring(L, string->getDefault());
|
||||
lua::setfield(L, "def");
|
||||
return 1;
|
||||
}
|
||||
lua::pop(L);
|
||||
throw std::runtime_error("unsupported setting type");
|
||||
}
|
||||
|
||||
/// @brief Open folder in file explorer
|
||||
static int l_open_folder(lua::State* L) {
|
||||
platform::open_folder(io::resolve(lua::require_string(L, 1)));
|
||||
return 0;
|
||||
}
|
||||
|
||||
/// @brief Open URL in default browser with confirmation dialog
|
||||
static int l_open_url(lua::State* L) {
|
||||
auto url = lua::require_string(L, 1);
|
||||
|
||||
std::wstring msg = langs::get(L"Are you sure you want to open the link:") +
|
||||
L"\n" + util::str2wstr_utf8(url) +
|
||||
std::wstring(L"?");
|
||||
|
||||
auto menu = engine->getGUI().getMenu();
|
||||
|
||||
guiutil::confirm(*engine, msg, [url, menu]() {
|
||||
platform::open_url(url);
|
||||
if (!menu->back()) {
|
||||
menu->reset();
|
||||
}
|
||||
});
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/// @brief Bring application window to focus
|
||||
static int l_focus(lua::State* L) {
|
||||
engine->getWindow().focus();
|
||||
return 0;
|
||||
}
|
||||
|
||||
/// @brief Set window title
|
||||
static int l_set_title(lua::State* L) {
|
||||
auto title = lua::require_string(L, 1);
|
||||
engine->getWindow().setTitle(title);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/// @brief Quit the game
|
||||
static int l_quit(lua::State*) {
|
||||
engine->quit();
|
||||
return 0;
|
||||
}
|
||||
|
||||
/// @brief Creating new world
|
||||
/// @param name Name world
|
||||
/// @param seed Seed world
|
||||
/// @param generator Type of generation
|
||||
static int l_new_world(lua::State* L) {
|
||||
auto name = lua::require_string(L, 1);
|
||||
auto seed = lua::require_string(L, 2);
|
||||
auto generator = lua::require_string(L, 3);
|
||||
int64_t localPlayer = 0;
|
||||
if (lua::gettop(L) >= 4) {
|
||||
localPlayer = lua::tointeger(L, 4);
|
||||
}
|
||||
if (level != nullptr) {
|
||||
throw std::runtime_error("world must be closed before");
|
||||
}
|
||||
auto controller = engine->getController();
|
||||
controller->setLocalPlayer(localPlayer);
|
||||
controller->createWorld(name, seed, generator);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/// @brief Open world
|
||||
/// @param name Name world
|
||||
static int l_open_world(lua::State* L) {
|
||||
auto name = lua::require_string(L, 1);
|
||||
if (level != nullptr) {
|
||||
throw std::runtime_error("world must be closed before");
|
||||
}
|
||||
auto controller = engine->getController();
|
||||
controller->setLocalPlayer(0);
|
||||
controller->openWorld(name, false);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/// @brief Reopen world
|
||||
static int l_reopen_world(lua::State*) {
|
||||
auto controller = engine->getController();
|
||||
if (level == nullptr) {
|
||||
throw std::runtime_error("no world open");
|
||||
}
|
||||
controller->reopenWorld(level->getWorld());
|
||||
return 0;
|
||||
}
|
||||
|
||||
/// @brief Save world
|
||||
static int l_save_world(lua::State* L) {
|
||||
if (controller == nullptr) {
|
||||
throw std::runtime_error("no world open");
|
||||
}
|
||||
controller->saveWorld();
|
||||
return 0;
|
||||
}
|
||||
|
||||
/// @brief Close world
|
||||
/// @param flag Save world (bool)
|
||||
static int l_close_world(lua::State* L) {
|
||||
if (controller == nullptr) {
|
||||
throw std::runtime_error("no world open");
|
||||
}
|
||||
controller->processBeforeQuit();
|
||||
bool save_world = lua::toboolean(L, 1);
|
||||
if (save_world) {
|
||||
controller->saveWorld();
|
||||
}
|
||||
engine->onWorldClosed();
|
||||
return 0;
|
||||
}
|
||||
|
||||
/// @brief Delete world
|
||||
/// @param name Name world
|
||||
static int l_delete_world(lua::State* L) {
|
||||
auto name = lua::require_string(L, 1);
|
||||
auto controller = engine->getController();
|
||||
controller->deleteWorld(name);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/// @brief Get engine version
|
||||
/// @return major, minor
|
||||
static int l_get_version(lua::State* L) {
|
||||
return lua::pushvec_stack(
|
||||
L, glm::vec2(ENGINE_VERSION_MAJOR, ENGINE_VERSION_MINOR)
|
||||
);
|
||||
}
|
||||
|
||||
/// @brief Create in-memory named IO device
|
||||
static int l_create_memory_device(lua::State* L) {
|
||||
std::string name = lua::require_string(L, 1);
|
||||
if (io::get_device(name)) {
|
||||
throw std::runtime_error(
|
||||
"entry-point '" + name + "' is already used"
|
||||
);
|
||||
}
|
||||
if (name.find(':') != std::string::npos) {
|
||||
throw std::runtime_error("invalid entry point name");
|
||||
}
|
||||
|
||||
io::set_device(name, std::make_unique<io::MemoryDevice>());
|
||||
return 0;
|
||||
}
|
||||
|
||||
/// @brief Start a new engine instance with debugging server
|
||||
static int l_start_debug_instance(lua::State* L) {
|
||||
if (!engine->getProject().permissions.has(Permissions::DEBUGGING)) {
|
||||
throw std::runtime_error("project has no debugging permission");
|
||||
}
|
||||
|
||||
int port = lua::tointeger(L, 1);
|
||||
if (port == 0) {
|
||||
auto network = engine->getNetwork();
|
||||
if (network == nullptr) {
|
||||
throw std::runtime_error("project has no network permission");
|
||||
}
|
||||
port = network->findFreePort();
|
||||
if (port == -1) {
|
||||
throw std::runtime_error("could not find free port");
|
||||
}
|
||||
}
|
||||
auto projectPath = lua::isstring(L, 2) ? lua::require_lstring(L, 2) : "";
|
||||
const auto& paths = engine->getPaths();
|
||||
|
||||
std::vector<std::string> args {
|
||||
"--res", paths.getResourcesFolder().u8string(),
|
||||
"--dir", paths.getUserFilesFolder().u8string(),
|
||||
"--dbg-server", "tcp:" + std::to_string(port),
|
||||
};
|
||||
if (!projectPath.empty()) {
|
||||
args.emplace_back("--project");
|
||||
args.emplace_back(io::resolve(std::string(projectPath)).string());
|
||||
}
|
||||
|
||||
platform::new_engine_instance(std::move(args));
|
||||
return lua::pushinteger(L, port);
|
||||
}
|
||||
|
||||
const luaL_Reg applib[] = {
|
||||
{"start_debug_instance", lua::wrap<l_start_debug_instance>},
|
||||
{"focus", lua::wrap<l_focus>},
|
||||
{"create_memory_device", lua::wrap<l_create_memory_device>},
|
||||
/// content
|
||||
{"is_content_loaded", lua::wrap<l_is_content_loaded>},
|
||||
{"load_content", lua::wrap<l_load_content>},
|
||||
{"reset_content", lua::wrap<l_reset_content>},
|
||||
{"reconfig_packs", lua::wrap<l_reconfig_packs>},
|
||||
{"get_content_sources", lua::wrap<l_get_content_sources>},
|
||||
{"set_content_sources", lua::wrap<l_set_content_sources>},
|
||||
{"reset_content_sources", lua::wrap<l_reset_content_sources>},
|
||||
// for other functions see libcore.cpp and stdlib.lua
|
||||
/// settings
|
||||
{"get_setting", lua::wrap<l_get_setting>},
|
||||
{"set_setting", lua::wrap<l_set_setting>},
|
||||
{"str_setting", lua::wrap<l_str_setting>},
|
||||
{"get_setting_info", lua::wrap<l_get_setting_info>},
|
||||
/// system applications
|
||||
{"open_folder", lua::wrap<l_open_folder>},
|
||||
{"open_url", lua::wrap<l_open_url>},
|
||||
/// window
|
||||
{"focus", lua::wrap<l_focus>},
|
||||
{"set_title", lua::wrap<l_set_title>},
|
||||
{"quit", lua::wrap<l_quit>},
|
||||
/// world
|
||||
{"new_world", lua::wrap<l_new_world>},
|
||||
{"open_world", lua::wrap<l_open_world>},
|
||||
{"reopen_world", lua::wrap<l_reopen_world>},
|
||||
{"save_world", lua::wrap<l_save_world>},
|
||||
{"close_world", lua::wrap<l_close_world>},
|
||||
{"delete_world", lua::wrap<l_delete_world>},
|
||||
/// other
|
||||
{"get_version", lua::wrap<l_get_version>},
|
||||
{"create_memory_device", lua::wrap<l_create_memory_device>},
|
||||
{"start_debug_instance", lua::wrap<l_start_debug_instance>},
|
||||
{nullptr, nullptr}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -476,8 +476,10 @@ static int l_raycast(lua::State* L) {
|
|||
auto start = lua::tovec<3>(L, 1);
|
||||
auto dir = lua::tovec<3>(L, 2);
|
||||
auto maxDistance = lua::tonumber(L, 3);
|
||||
bool includeNonSelectable = false;
|
||||
std::set<blockid_t> filteredBlocks {};
|
||||
if (lua::gettop(L) >= 5) {
|
||||
const int luaStackSize = lua::gettop(L);
|
||||
if (luaStackSize >= 5) {
|
||||
if (lua::istable(L, 5)) {
|
||||
int addLen = lua::objlen(L, 5);
|
||||
for (int i = 0; i < addLen; i++) {
|
||||
|
|
@ -493,6 +495,9 @@ static int l_raycast(lua::State* L) {
|
|||
throw std::runtime_error("table expected for filter");
|
||||
}
|
||||
}
|
||||
if (luaStackSize >= 6) {
|
||||
includeNonSelectable = lua::toboolean(L, 6);
|
||||
}
|
||||
glm::vec3 end;
|
||||
glm::ivec3 normal;
|
||||
glm::ivec3 iend;
|
||||
|
|
@ -504,9 +509,10 @@ static int l_raycast(lua::State* L) {
|
|||
end,
|
||||
normal,
|
||||
iend,
|
||||
filteredBlocks
|
||||
filteredBlocks,
|
||||
includeNonSelectable
|
||||
)) {
|
||||
if (lua::gettop(L) >= 4 && !lua::isnil(L, 4)) {
|
||||
if (luaStackSize >= 4 && !lua::isnil(L, 4)) {
|
||||
lua::pushvalue(L, 4);
|
||||
} else {
|
||||
lua::createtable(L, 0, 5);
|
||||
|
|
|
|||
|
|
@ -1,274 +1,9 @@
|
|||
#include <memory>
|
||||
#include <vector>
|
||||
#include "api_lua.hpp"
|
||||
|
||||
#include <sstream>
|
||||
|
||||
#include "api_lua.hpp"
|
||||
#include "constants.hpp"
|
||||
#include "assets/Assets.hpp"
|
||||
#include "content/Content.hpp"
|
||||
#include "content/ContentControl.hpp"
|
||||
#include "engine/Engine.hpp"
|
||||
#include "engine/EnginePaths.hpp"
|
||||
#include "io/io.hpp"
|
||||
#include "io/settings_io.hpp"
|
||||
#include "frontend/menu.hpp"
|
||||
#include "frontend/screens/MenuScreen.hpp"
|
||||
#include "graphics/core/Texture.hpp"
|
||||
#include "logic/EngineController.hpp"
|
||||
#include "logic/LevelController.hpp"
|
||||
#include "util/listutil.hpp"
|
||||
#include "util/platform.hpp"
|
||||
#include "world/Level.hpp"
|
||||
#include "world/generator/WorldGenerator.hpp"
|
||||
#include "util/platform.hpp"
|
||||
#include "frontend/locale.hpp"
|
||||
#include "graphics/ui/gui_util.hpp"
|
||||
#include "graphics/ui/GUI.hpp"
|
||||
#include "graphics/ui/elements/Menu.hpp"
|
||||
#include "window/Window.hpp"
|
||||
|
||||
using namespace scripting;
|
||||
|
||||
static int l_get_version(lua::State* L) {
|
||||
return lua::pushvec_stack(
|
||||
L, glm::vec2(ENGINE_VERSION_MAJOR, ENGINE_VERSION_MINOR)
|
||||
);
|
||||
}
|
||||
|
||||
static int l_load_content(lua::State* L) {
|
||||
content_control->loadContent();
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int l_reset_content(lua::State* L) {
|
||||
if (level != nullptr) {
|
||||
throw std::runtime_error("world must be closed before");
|
||||
}
|
||||
std::vector<std::string> nonResetPacks;
|
||||
if (lua::istable(L, 1)) {
|
||||
int len = lua::objlen(L, 1);
|
||||
for (int i = 0; i < len; i++) {
|
||||
lua::rawgeti(L, i + 1, 1);
|
||||
nonResetPacks.emplace_back(lua::require_lstring(L, -1));
|
||||
lua::pop(L);
|
||||
}
|
||||
}
|
||||
content_control->resetContent(std::move(nonResetPacks));
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int l_is_content_loaded(lua::State* L) {
|
||||
return lua::pushboolean(L, content != nullptr);
|
||||
}
|
||||
|
||||
/// @brief Creating new world
|
||||
/// @param name Name world
|
||||
/// @param seed Seed world
|
||||
/// @param generator Type of generation
|
||||
static int l_new_world(lua::State* L) {
|
||||
auto name = lua::require_string(L, 1);
|
||||
auto seed = lua::require_string(L, 2);
|
||||
auto generator = lua::require_string(L, 3);
|
||||
int64_t localPlayer = 0;
|
||||
if (lua::gettop(L) >= 4) {
|
||||
localPlayer = lua::tointeger(L, 4);
|
||||
}
|
||||
if (level != nullptr) {
|
||||
throw std::runtime_error("world must be closed before");
|
||||
}
|
||||
auto controller = engine->getController();
|
||||
controller->setLocalPlayer(localPlayer);
|
||||
controller->createWorld(name, seed, generator);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/// @brief Open world
|
||||
/// @param name Name world
|
||||
static int l_open_world(lua::State* L) {
|
||||
auto name = lua::require_string(L, 1);
|
||||
if (level != nullptr) {
|
||||
throw std::runtime_error("world must be closed before");
|
||||
}
|
||||
auto controller = engine->getController();
|
||||
controller->setLocalPlayer(0);
|
||||
controller->openWorld(name, false);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/// @brief Reopen world
|
||||
static int l_reopen_world(lua::State*) {
|
||||
auto controller = engine->getController();
|
||||
if (level == nullptr) {
|
||||
throw std::runtime_error("no world open");
|
||||
}
|
||||
controller->reopenWorld(level->getWorld());
|
||||
return 0;
|
||||
}
|
||||
|
||||
/// @brief Save world
|
||||
static int l_save_world(lua::State* L) {
|
||||
if (controller == nullptr) {
|
||||
throw std::runtime_error("no world open");
|
||||
}
|
||||
controller->saveWorld();
|
||||
return 0;
|
||||
}
|
||||
|
||||
/// @brief Close world
|
||||
/// @param flag Save world (bool)
|
||||
static int l_close_world(lua::State* L) {
|
||||
if (controller == nullptr) {
|
||||
throw std::runtime_error("no world open");
|
||||
}
|
||||
controller->processBeforeQuit();
|
||||
bool save_world = lua::toboolean(L, 1);
|
||||
if (save_world) {
|
||||
controller->saveWorld();
|
||||
}
|
||||
engine->onWorldClosed();
|
||||
return 0;
|
||||
}
|
||||
|
||||
/// @brief Delete world
|
||||
/// @param name Name world
|
||||
static int l_delete_world(lua::State* L) {
|
||||
auto name = lua::require_string(L, 1);
|
||||
auto controller = engine->getController();
|
||||
controller->deleteWorld(name);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/// @brief Reconfigure packs
|
||||
/// @param addPacks An array of packs to add
|
||||
/// @param remPacks An array of packs to remove
|
||||
static int l_reconfig_packs(lua::State* L) {
|
||||
if (!lua::istable(L, 1)) {
|
||||
throw std::runtime_error("strings array expected as the first argument");
|
||||
}
|
||||
if (!lua::istable(L, 2)) {
|
||||
throw std::runtime_error("strings array expected as the second argument");
|
||||
}
|
||||
std::vector<std::string> addPacks;
|
||||
int addLen = lua::objlen(L, 1);
|
||||
for (int i = 0; i < addLen; i++) {
|
||||
lua::rawgeti(L, i + 1, 1);
|
||||
addPacks.emplace_back(lua::require_lstring(L, -1));
|
||||
lua::pop(L);
|
||||
}
|
||||
std::vector<std::string> remPacks;
|
||||
int remLen = lua::objlen(L, 2);
|
||||
for (int i = 0; i < remLen; i++) {
|
||||
lua::rawgeti(L, i + 1, 2);
|
||||
remPacks.emplace_back(lua::require_lstring(L, -1));
|
||||
lua::pop(L);
|
||||
}
|
||||
auto engineController = engine->getController();
|
||||
try {
|
||||
engineController->reconfigPacks(controller, addPacks, remPacks);
|
||||
} catch (const contentpack_error& err) {
|
||||
throw std::runtime_error(
|
||||
std::string(err.what()) + " [" + err.getPackId() + " ]"
|
||||
);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/// @brief Get a setting value
|
||||
/// @param name The name of the setting
|
||||
/// @return The value of the setting
|
||||
static int l_get_setting(lua::State* L) {
|
||||
auto name = lua::require_string(L, 1);
|
||||
const auto value = engine->getSettingsHandler().getValue(name);
|
||||
return lua::pushvalue(L, value);
|
||||
}
|
||||
|
||||
/// @brief Set a setting value
|
||||
/// @param name The name of the setting
|
||||
/// @param value The new value for the setting
|
||||
static int l_set_setting(lua::State* L) {
|
||||
auto name = lua::require_string(L, 1);
|
||||
const auto value = lua::tovalue(L, 2);
|
||||
engine->getSettingsHandler().setValue(name, value);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/// @brief Convert a setting value to a string
|
||||
/// @param name The name of the setting
|
||||
/// @return The string representation of the setting value
|
||||
static int l_str_setting(lua::State* L) {
|
||||
auto name = lua::require_string(L, 1);
|
||||
const auto string = engine->getSettingsHandler().toString(name);
|
||||
return lua::pushstring(L, string);
|
||||
}
|
||||
|
||||
/// @brief Get information about a setting
|
||||
/// @param name The name of the setting
|
||||
/// @return A table with information about the setting
|
||||
static int l_get_setting_info(lua::State* L) {
|
||||
auto name = lua::require_string(L, 1);
|
||||
auto setting = engine->getSettingsHandler().getSetting(name);
|
||||
lua::createtable(L, 0, 1);
|
||||
if (auto number = dynamic_cast<NumberSetting*>(setting)) {
|
||||
lua::pushnumber(L, number->getMin());
|
||||
lua::setfield(L, "min");
|
||||
lua::pushnumber(L, number->getMax());
|
||||
lua::setfield(L, "max");
|
||||
lua::pushnumber(L, number->getDefault());
|
||||
lua::setfield(L, "def");
|
||||
return 1;
|
||||
}
|
||||
if (auto integer = dynamic_cast<IntegerSetting*>(setting)) {
|
||||
lua::pushinteger(L, integer->getMin());
|
||||
lua::setfield(L, "min");
|
||||
lua::pushinteger(L, integer->getMax());
|
||||
lua::setfield(L, "max");
|
||||
lua::pushinteger(L, integer->getDefault());
|
||||
lua::setfield(L, "def");
|
||||
return 1;
|
||||
}
|
||||
if (auto boolean = dynamic_cast<FlagSetting*>(setting)) {
|
||||
lua::pushboolean(L, boolean->getDefault());
|
||||
lua::setfield(L, "def");
|
||||
return 1;
|
||||
}
|
||||
if (auto string = dynamic_cast<StringSetting*>(setting)) {
|
||||
lua::pushstring(L, string->getDefault());
|
||||
lua::setfield(L, "def");
|
||||
return 1;
|
||||
}
|
||||
lua::pop(L);
|
||||
throw std::runtime_error("unsupported setting type");
|
||||
}
|
||||
|
||||
static int l_open_folder(lua::State* L) {
|
||||
platform::open_folder(io::resolve(lua::require_string(L, 1)));
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int l_open_url(lua::State* L) {
|
||||
auto url = lua::require_string(L, 1);
|
||||
|
||||
std::wstring msg = langs::get(L"Are you sure you want to open the link:") +
|
||||
L"\n" + util::str2wstr_utf8(url) +
|
||||
std::wstring(L"?");
|
||||
|
||||
auto menu = engine->getGUI().getMenu();
|
||||
|
||||
guiutil::confirm(*engine, msg, [url, menu]() {
|
||||
platform::open_url(url);
|
||||
if (!menu->back()) {
|
||||
menu->reset();
|
||||
}
|
||||
});
|
||||
return 0;
|
||||
}
|
||||
|
||||
/// @brief Quit the game
|
||||
static int l_quit(lua::State*) {
|
||||
engine->quit();
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int l_blank(lua::State*) {
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -300,33 +35,8 @@ static int l_capture_output(lua::State* L) {
|
|||
return 1;
|
||||
}
|
||||
|
||||
static int l_set_title(lua::State* L) {
|
||||
auto title = lua::require_string(L, 1);
|
||||
engine->getWindow().setTitle(title);
|
||||
return 0;
|
||||
}
|
||||
|
||||
const luaL_Reg corelib[] = {
|
||||
{"blank", lua::wrap<l_blank>},
|
||||
{"get_version", lua::wrap<l_get_version>},
|
||||
{"load_content", lua::wrap<l_load_content>},
|
||||
{"reset_content", lua::wrap<l_reset_content>},
|
||||
{"is_content_loaded", lua::wrap<l_is_content_loaded>},
|
||||
{"new_world", lua::wrap<l_new_world>},
|
||||
{"open_world", lua::wrap<l_open_world>},
|
||||
{"reopen_world", lua::wrap<l_reopen_world>},
|
||||
{"save_world", lua::wrap<l_save_world>},
|
||||
{"close_world", lua::wrap<l_close_world>},
|
||||
{"delete_world", lua::wrap<l_delete_world>},
|
||||
{"reconfig_packs", lua::wrap<l_reconfig_packs>},
|
||||
{"get_setting", lua::wrap<l_get_setting>},
|
||||
{"set_setting", lua::wrap<l_set_setting>},
|
||||
{"str_setting", lua::wrap<l_str_setting>},
|
||||
{"get_setting_info", lua::wrap<l_get_setting_info>},
|
||||
{"open_folder", lua::wrap<l_open_folder>},
|
||||
{"open_url", lua::wrap<l_open_url>},
|
||||
{"quit", lua::wrap<l_quit>},
|
||||
{"capture_output", lua::wrap<l_capture_output>},
|
||||
{"set_title", lua::wrap<l_set_title>},
|
||||
{nullptr, nullptr}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -141,8 +141,10 @@ static int l_raycast(lua::State* L) {
|
|||
auto dir = lua::tovec<3>(L, 2);
|
||||
auto maxDistance = lua::tonumber(L, 3);
|
||||
auto ignoreEntityId = lua::tointeger(L, 4);
|
||||
bool includeNonSelectable = false;
|
||||
std::set<blockid_t> filteredBlocks {};
|
||||
if (lua::gettop(L) >= 6) {
|
||||
const int luaStackSize = lua::gettop(L);
|
||||
if (luaStackSize >= 6) {
|
||||
if (lua::istable(L, 6)) {
|
||||
int addLen = lua::objlen(L, 6);
|
||||
for (int i = 0; i < addLen; i++) {
|
||||
|
|
@ -158,6 +160,9 @@ static int l_raycast(lua::State* L) {
|
|||
throw std::runtime_error("table expected for filter");
|
||||
}
|
||||
}
|
||||
if (luaStackSize >= 7) {
|
||||
includeNonSelectable = lua::toboolean(L, 6);
|
||||
}
|
||||
|
||||
glm::vec3 end;
|
||||
glm::ivec3 normal;
|
||||
|
|
@ -173,14 +178,15 @@ static int l_raycast(lua::State* L) {
|
|||
end,
|
||||
normal,
|
||||
iend,
|
||||
filteredBlocks
|
||||
filteredBlocks,
|
||||
includeNonSelectable
|
||||
)) {
|
||||
maxDistance = glm::distance(start, end);
|
||||
block = voxel->id;
|
||||
}
|
||||
if (auto ray =
|
||||
level->entities->rayCast(start, dir, maxDistance, ignoreEntityId)) {
|
||||
if (lua::gettop(L) >= 5 && !lua::isnil(L, 5)) {
|
||||
if (luaStackSize >= 5 && !lua::isnil(L, 5)) {
|
||||
lua::pushvalue(L, 5);
|
||||
} else {
|
||||
lua::createtable(L, 0, 6);
|
||||
|
|
|
|||
|
|
@ -1111,6 +1111,16 @@ static int l_gui_load_document(lua::State* L) {
|
|||
);
|
||||
auto document = documentPtr.get();
|
||||
engine->getAssets()->store(std::move(documentPtr), alias);
|
||||
|
||||
// namespace extension
|
||||
if (lua::istable(L, 4)) {
|
||||
if (lua::get_from(L, "table", "merge")) {
|
||||
lua::pushenv(L, *document->getEnvironment());
|
||||
lua::pushvalue(L, 4);
|
||||
lua::call(L, 2, 0);
|
||||
lua::pop(L);
|
||||
}
|
||||
}
|
||||
scripting::on_ui_open(document, {args});
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
#include "coders/json.hpp"
|
||||
#include "engine/Engine.hpp"
|
||||
#include "network/Network.hpp"
|
||||
#include "devtools/Project.hpp"
|
||||
|
||||
#include <variant>
|
||||
#include <utility>
|
||||
|
|
@ -439,7 +440,7 @@ static int l_is_nodelay(lua::State* L, network::Network& network) {
|
|||
return lua::pushboolean(L, false);
|
||||
}
|
||||
|
||||
static int l_pull_events(lua::State* L, network::Network& network) {
|
||||
static int l_pull_events(lua::State* L) {
|
||||
std::vector<NetworkEvent> local_queue;
|
||||
{
|
||||
std::lock_guard lock(events_queue_mutex);
|
||||
|
|
@ -528,7 +529,8 @@ int wrap(lua_State* L) {
|
|||
int result = 0;
|
||||
try {
|
||||
auto network = engine->getNetwork();
|
||||
if (network == nullptr) {
|
||||
const auto& permissions = engine->getProject().permissions;
|
||||
if (network == nullptr || !permissions.has(Permissions::NETWORK)) {
|
||||
throw std::runtime_error(
|
||||
"network subsystem is not available in the project"
|
||||
);
|
||||
|
|
@ -554,7 +556,7 @@ const luaL_Reg networklib[] = {
|
|||
{"get_total_download", wrap<l_get_total_download>},
|
||||
{"find_free_port", wrap<l_find_free_port>},
|
||||
{"is_available", lua::wrap<l_is_available>},
|
||||
{"__pull_events", wrap<l_pull_events>},
|
||||
{"__pull_events", lua::wrap<l_pull_events>},
|
||||
{"__open_tcp", wrap<l_open_tcp>},
|
||||
{"__open_udp", wrap<l_open_udp>},
|
||||
{"__closeserver", wrap<l_closeserver>},
|
||||
|
|
|
|||
|
|
@ -324,8 +324,10 @@ static int l_is_suspended(lua::State* L) {
|
|||
}
|
||||
|
||||
static int l_set_suspended(lua::State* L) {
|
||||
if (auto player = get_player(L, 1)) {
|
||||
player->setSuspended(lua::toboolean(L, 2));
|
||||
if (lua::toboolean(L, 2)) {
|
||||
level->players->suspend(lua::tointeger(L, 1));
|
||||
} else {
|
||||
level->players->resume(lua::tointeger(L, 1));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -34,7 +34,9 @@ static int l_get_intensity(lua::State* L) {
|
|||
static int l_set_intensity(lua::State* L) {
|
||||
size_t index = static_cast<size_t>(lua::tointeger(L, 1));
|
||||
float value = lua::tonumber(L, 2);
|
||||
post_processing->getEffect(index)->setIntensity(value);
|
||||
if (auto effect = post_processing->getEffect(index)) {
|
||||
effect->setIntensity(value);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -66,6 +66,8 @@ static void create_libs(State* L, StateType stateType) {
|
|||
|
||||
if (stateType == StateType::SCRIPT) {
|
||||
openlib(L, "app", applib);
|
||||
lua::getglobal(L, "app");
|
||||
lua::setglobal(L, "__vc_app");
|
||||
} else if (stateType == StateType::BASE) {
|
||||
openlib(L, "__vc_app", applib);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue