add 'network' permission

This commit is contained in:
MihailRis 2025-12-07 01:26:48 +03:00 committed by ShiftyX1
parent 56d808e3e2
commit dd40780334
10 changed files with 67 additions and 27 deletions

View File

@ -1,3 +1,6 @@
# default project
name = "default"
base_packs = ["base"]
permissions = [
"network"
]

View File

@ -182,6 +182,9 @@ local function clean(iterable, checkFun, ...)
end
network.__process_events = function()
if not network.is_available() then
return
end
local CLIENT_CONNECTED = 1
local CONNECTED_TO_SERVER = 2
local DATAGRAM = 3

View File

@ -94,12 +94,17 @@ bool ClientConnection::alive() const {
static network::Server& create_tcp_server(
DebuggingServer& dbgServer, Engine& engine, int port
) {
auto& network = engine.getNetwork();
u64id_t serverId = network.openTcpServer(
auto network = engine.getNetwork();
if (network == nullptr) {
throw std::runtime_error(
"unable to create tcp server: project has no network permission"
);
}
u64id_t serverId = network->openTcpServer(
port,
[&network, &dbgServer](u64id_t sid, u64id_t id) {
auto& connection = dynamic_cast<network::ReadableConnection&>(
*network.getConnection(id, true)
*network->getConnection(id, true)
);
connection.setPrivate(true);
logger.info() << "connected client " << id << ": "
@ -108,7 +113,7 @@ static network::Server& create_tcp_server(
dbgServer.setClient(id);
}
);
auto& server = *network.getServer(serverId, true);
auto& server = *network->getServer(serverId, true);
server.setPrivate(true);
auto& tcpServer = dynamic_cast<network::TcpServer&>(server);
@ -302,8 +307,10 @@ void DebuggingServer::sendValue(
}
void DebuggingServer::setClient(u64id_t client) {
auto network = engine.getNetwork();
assert (network != nullptr);
connection =
std::make_unique<ClientConnection>(engine.getNetwork(), client);
std::make_unique<ClientConnection>(*network, client);
connectionEstablished = false;
}

View File

@ -14,6 +14,7 @@ namespace scripting {
struct Permissions {
static inline std::string WRITE_TO_USER = "write-to-user";
static inline std::string NETWORK = "network";
std::set<std::string> permissions;

View File

@ -138,7 +138,10 @@ void Engine::initialize(CoreParameters coreParameters) {
editor = std::make_unique<devtools::Editor>(*this);
cmd = std::make_unique<cmd::CommandsInterpreter>();
network = network::Network::create(settings.network);
if (project->permissions.has(Permissions::NETWORK)) {
network = network::Network::create(settings.network);
}
if (!params.debugServerString.empty()) {
try {
@ -233,7 +236,9 @@ void Engine::run() {
}
void Engine::postUpdate() {
network->update();
if (network) {
network->update();
}
postRunnables.run();
scripting::process_post_runnables();
@ -266,6 +271,8 @@ void Engine::nextFrame(bool waitForRefresh) {
}
void Engine::startPauseLoop() {
assert (network != nullptr);
bool initialCursorLocked = false;
if (!isHeadless()) {
initialCursorLocked = input->isCursorLocked();

View File

@ -3,13 +3,12 @@
#include "CoreParameters.hpp"
#include "PostRunnables.hpp"
#include "Time.hpp"
#include "delegates.hpp"
#include "settings.hpp"
#include "typedefs.hpp"
#include "util/ObjectsKeeper.hpp"
#include <memory>
#include <string>
#include <optional>
class Assets;
class ContentControl;
@ -161,8 +160,8 @@ public:
return *window;
}
network::Network& getNetwork() {
return *network;
network::Network* getNetwork() {
return network.get();
}
cmd::CommandsInterpreter& getCmd() {

View File

@ -59,6 +59,7 @@ std::shared_ptr<UINode> create_debug_panel(
Player& player,
bool allowDebugCheats
) {
auto network = engine.getNetwork();
auto& gui = engine.getGUI();
auto panel = std::make_shared<Panel>(
gui, glm::vec2(300, 200), glm::vec4(5.0f), 2.0f
@ -87,17 +88,18 @@ std::shared_ptr<UINode> create_debug_panel(
fpsMax = fps;
});
panel->listenInterval(1.0f, [&engine]() {
const auto& network = engine.getNetwork();
size_t totalDownload = network.getTotalDownload();
size_t totalUpload = network.getTotalUpload();
netSpeedString =
L"download: " + std::to_wstring(totalDownload - lastTotalDownload) +
L" B/s upload: " + std::to_wstring(totalUpload - lastTotalUpload) +
L" B/s";
lastTotalDownload = totalDownload;
lastTotalUpload = totalUpload;
});
if (network) {
panel->listenInterval(1.0f, [network]() {
size_t totalDownload = network->getTotalDownload();
size_t totalUpload = network->getTotalUpload();
netSpeedString =
L"download: " + std::to_wstring(totalDownload - lastTotalDownload) +
L" B/s upload: " + std::to_wstring(totalUpload - lastTotalUpload) +
L" B/s";
lastTotalDownload = totalDownload;
lastTotalUpload = totalUpload;
});
}
panel->add(create_label(gui, []() { return L"fps: "+fpsString;}));
@ -116,7 +118,9 @@ std::shared_ptr<UINode> create_debug_panel(
panel->add(create_label(gui, []() {
return L"lua-stack: " + std::to_wstring(scripting::get_values_on_stack());
}));
panel->add(create_label(gui, []() { return netSpeedString; }));
if (network) {
panel->add(create_label(gui, []() { return netSpeedString; }));
}
panel->add(create_label(gui, [&engine]() {
auto& settings = engine.getSettings();
bool culling = settings.graphics.frustumCulling.get();

View File

@ -15,7 +15,11 @@ using namespace scripting;
static int l_start_debug_instance(lua::State* L) {
int port = lua::tointeger(L, 1);
if (port == 0) {
port = engine->getNetwork().findFreePort();
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");
}

View File

@ -133,7 +133,7 @@ static int l_post(lua::State* L, network::Network& network) {
auto headers = read_headers(L, 3);
int currentRequestId = request_id++;
engine->getNetwork().post(
network.post(
url,
string,
[currentRequestId](std::vector<char> bytes) {
@ -240,7 +240,7 @@ static int l_recv(lua::State* L, network::Network& network) {
u64id_t id = lua::tointeger(L, 1);
int length = lua::tointeger(L, 2);
auto connection = engine->getNetwork().getConnection(id, false);
auto connection = network.getConnection(id, false);
if (connection == nullptr || connection->getTransportType() != network::TransportType::TCP) {
return 0;
@ -519,11 +519,21 @@ static int l_pull_events(lua::State* L, network::Network& network) {
return 1;
}
int l_is_available(lua::State* L) {
return engine->getNetwork() != nullptr;
}
template <int(*func)(lua::State*, network::Network&)>
int wrap(lua_State* L) {
int result = 0;
try {
result = func(L, engine->getNetwork());
auto network = engine->getNetwork();
if (network == nullptr) {
throw std::runtime_error(
"network subsystem is not available in the project"
);
}
result = func(L, *network);
}
// transform exception with description into lua_error
catch (std::exception& e) {
@ -543,6 +553,7 @@ const luaL_Reg networklib[] = {
{"get_total_upload", wrap<l_get_total_upload>},
{"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>},
{"__open_tcp", wrap<l_open_tcp>},
{"__open_udp", wrap<l_open_udp>},

View File

@ -194,5 +194,6 @@ void Network::update() {
}
std::unique_ptr<Network> Network::create(const NetworkSettings& settings) {
logger.info() << "initializing network";
return std::make_unique<Network>(network::create_curl_requests());
}