mirror of
https://github.com/BeamMP/BeamMP-Server.git
synced 2026-06-17 14:12:25 +00:00
Lua: Implement more core functions
This commit is contained in:
@@ -100,6 +100,7 @@ private:
|
|||||||
sol::table Lua_GetPlayerIdentifiers(int ID);
|
sol::table Lua_GetPlayerIdentifiers(int ID);
|
||||||
sol::table Lua_GetPlayers();
|
sol::table Lua_GetPlayers();
|
||||||
std::string Lua_GetPlayerName(int ID);
|
std::string Lua_GetPlayerName(int ID);
|
||||||
|
sol::table Lua_GetPlayerVehicles(int ID);
|
||||||
|
|
||||||
std::string mName;
|
std::string mName;
|
||||||
std::atomic_bool& mShutdown;
|
std::atomic_bool& mShutdown;
|
||||||
|
|||||||
+29
-30
@@ -151,64 +151,63 @@ void LuaAPI::MP::RemoveVehicle(int PID, int VID) {
|
|||||||
c->DeleteCar(VID);
|
c->DeleteCar(VID);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/*
|
|
||||||
void LuaAPI::MP::Set(int ConfigID, sol::object NewValue) {
|
void LuaAPI::MP::Set(int ConfigID, sol::object NewValue) {
|
||||||
switch (ConfigID) {
|
switch (ConfigID) {
|
||||||
case 0: //debug
|
case 0: //debug
|
||||||
if (lua_isboolean(L, 2)) {
|
if (NewValue.is<bool>()) {
|
||||||
Application::Settings.DebugModeEnabled = NewValue.as<bool>();
|
Application::Settings.DebugModeEnabled = NewValue.as<bool>();
|
||||||
beammp_info("Set `Debug` to " + (Application::Settings.DebugModeEnabled ? "true" : "false"));
|
beammp_info(std::string("Set `Debug` to ") + (Application::Settings.DebugModeEnabled ? "true" : "false"));
|
||||||
} else
|
} else
|
||||||
SendError(Engine(), L, ("set invalid argument [2] expected boolean for ID : 0"));
|
beammp_lua_error("set invalid argument [2] expected boolean");
|
||||||
break;
|
break;
|
||||||
case 1: //private
|
case 1: //private
|
||||||
if (lua_isboolean(L, 2)) {
|
if (NewValue.is<bool>()) {
|
||||||
Application::Settings.Private = lua_toboolean(L, 2);
|
Application::Settings.Private = NewValue.as<bool>();
|
||||||
beammp_info("Set `Private` to ") + (Application::Settings.Private ? "true" : "false"));
|
beammp_info(std::string("Set `Private` to ") + (Application::Settings.Private ? "true" : "false"));
|
||||||
} else
|
} else
|
||||||
SendError(Engine(), L, ("set invalid argument [2] expected boolean for ID : 1"));
|
beammp_lua_error("set invalid argument [2] expected boolean");
|
||||||
break;
|
break;
|
||||||
case 2: //max cars
|
case 2: //max cars
|
||||||
if (lua_isnumber(L, 2)) {
|
if (NewValue.is<int>()) {
|
||||||
Application::Settings.MaxCars = int(lua_tointeger(L, 2));
|
Application::Settings.MaxCars = NewValue.as<int>();
|
||||||
beammp_info("Set `MaxCars` to ") + std::to_string(Application::Settings.MaxCars));
|
beammp_info(std::string("Set `MaxCars` to ") + std::to_string(Application::Settings.MaxCars));
|
||||||
} else
|
} else
|
||||||
SendError(Engine(), L, ("set invalid argument [2] expected number for ID : 2"));
|
beammp_lua_error("set invalid argument [2] expected integer");
|
||||||
break;
|
break;
|
||||||
case 3: //max players
|
case 3: //max players
|
||||||
if (lua_isnumber(L, 2)) {
|
if (NewValue.is<int>()) {
|
||||||
Application::Settings.MaxPlayers = int(lua_tointeger(L, 2));
|
Application::Settings.MaxPlayers = NewValue.as<int>();
|
||||||
beammp_info("Set `MaxPlayers` to ") + std::to_string(Application::Settings.MaxPlayers));
|
beammp_info(std::string("Set `MaxPlayers` to ") + std::to_string(Application::Settings.MaxPlayers));
|
||||||
} else
|
} else
|
||||||
SendError(Engine(), L, ("set invalid argument [2] expected number for ID : 3"));
|
beammp_lua_error("set invalid argument [2] expected integer");
|
||||||
break;
|
break;
|
||||||
case 4: //Map
|
case 4: //Map
|
||||||
if (lua_isstring(L, 2)) {
|
if (NewValue.is<std::string>()) {
|
||||||
Application::Settings.MapName = lua_tostring(L, 2);
|
Application::Settings.MapName = NewValue.as<std::string>();
|
||||||
beammp_info("Set `Map` to ") + Application::Settings.MapName);
|
beammp_info(std::string("Set `Map` to ") + Application::Settings.MapName);
|
||||||
} else
|
} else
|
||||||
SendError(Engine(), L, ("set invalid argument [2] expected string for ID : 4"));
|
beammp_lua_error("set invalid argument [2] expected string");
|
||||||
break;
|
break;
|
||||||
case 5: //Name
|
case 5: //Name
|
||||||
if (lua_isstring(L, 2)) {
|
if (NewValue.is<std::string>()) {
|
||||||
Application::Settings.ServerName = lua_tostring(L, 2);
|
Application::Settings.ServerName = NewValue.as<std::string>();
|
||||||
beammp_info("Set `Name` to ") + Application::Settings.ServerName);
|
beammp_info(std::string("Set `Name` to ") + Application::Settings.ServerName);
|
||||||
} else
|
} else
|
||||||
SendError(Engine(), L, ("set invalid argument [2] expected string for ID : 5"));
|
beammp_lua_error("set invalid argument [2] expected string");
|
||||||
break;
|
break;
|
||||||
case 6: //Desc
|
case 6: //Desc
|
||||||
if (lua_isstring(L, 2)) {
|
if (NewValue.is<std::string>()) {
|
||||||
Application::Settings.ServerDesc = lua_tostring(L, 2);
|
Application::Settings.ServerDesc = NewValue.as<std::string>();
|
||||||
beammp_info("Set `Description` to ") + Application::Settings.ServerDesc);
|
beammp_info(std::string("Set `Description` to ") + Application::Settings.ServerDesc);
|
||||||
} else
|
} else
|
||||||
SendError(Engine(), L, ("set invalid argument [2] expected string for ID : 6"));
|
beammp_lua_error("set invalid argument [2] expected string");
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
warn(("Invalid config ID : ") + std::to_string(C));
|
beammp_warn("Invalid config ID \"" + std::to_string(ConfigID) + "\". Use `MP.Settings.*` enum for this.");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|
||||||
void LuaAPI::MP::Sleep(size_t Ms) {
|
void LuaAPI::MP::Sleep(size_t Ms) {
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(Ms));
|
std::this_thread::sleep_for(std::chrono::milliseconds(Ms));
|
||||||
|
|||||||
+26
-2
@@ -227,6 +227,28 @@ std::string TLuaEngine::StateThreadData::Lua_GetPlayerName(int ID) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sol::table TLuaEngine::StateThreadData::Lua_GetPlayerVehicles(int ID) {
|
||||||
|
auto MaybeClient = GetClient(mEngine->Server(), ID);
|
||||||
|
if (MaybeClient && !MaybeClient.value().expired()) {
|
||||||
|
auto Client = MaybeClient.value().lock();
|
||||||
|
TClient::TSetOfVehicleData VehicleData;
|
||||||
|
{ // Vehicle Data Lock Scope
|
||||||
|
auto LockedData = Client->GetAllCars();
|
||||||
|
VehicleData = *LockedData.VehicleData;
|
||||||
|
} // End Vehicle Data Lock Scope
|
||||||
|
if (VehicleData.empty()) {
|
||||||
|
return sol::nil;
|
||||||
|
}
|
||||||
|
sol::state_view StateView(mState);
|
||||||
|
sol::table Result = StateView.create_table();
|
||||||
|
for (const auto& v : VehicleData) {
|
||||||
|
Result[v.ID()] = v.Data().substr(3);
|
||||||
|
}
|
||||||
|
return Result;
|
||||||
|
} else
|
||||||
|
return sol::nil;
|
||||||
|
}
|
||||||
|
|
||||||
TLuaEngine::StateThreadData::StateThreadData(const std::string& Name, std::atomic_bool& Shutdown, TLuaStateId StateId, TLuaEngine& Engine)
|
TLuaEngine::StateThreadData::StateThreadData(const std::string& Name, std::atomic_bool& Shutdown, TLuaStateId StateId, TLuaEngine& Engine)
|
||||||
: mName(Name)
|
: mName(Name)
|
||||||
, mShutdown(Shutdown)
|
, mShutdown(Shutdown)
|
||||||
@@ -271,7 +293,9 @@ TLuaEngine::StateThreadData::StateThreadData(const std::string& Name, std::atomi
|
|||||||
return Lua_GetPlayerName(ID);
|
return Lua_GetPlayerName(ID);
|
||||||
});
|
});
|
||||||
Table.set_function("RemoveVehicle", &LuaAPI::MP::RemoveVehicle);
|
Table.set_function("RemoveVehicle", &LuaAPI::MP::RemoveVehicle);
|
||||||
//Table.set_function("GetPlayerVehicles", &Lua_GetPlayerVehicles);
|
Table.set_function("GetPlayerVehicles", [&](int ID) -> sol::table {
|
||||||
|
return Lua_GetPlayerVehicles(ID);
|
||||||
|
}));
|
||||||
Table.set_function("SendChatMessage", &LuaAPI::MP::SendChatMessage);
|
Table.set_function("SendChatMessage", &LuaAPI::MP::SendChatMessage);
|
||||||
Table.set_function("GetPlayers", [&]() -> sol::table {
|
Table.set_function("GetPlayers", [&]() -> sol::table {
|
||||||
return Lua_GetPlayers();
|
return Lua_GetPlayers();
|
||||||
@@ -282,7 +306,7 @@ TLuaEngine::StateThreadData::StateThreadData(const std::string& Name, std::atomi
|
|||||||
return Lua_GetPlayerIdentifiers(ID);
|
return Lua_GetPlayerIdentifiers(ID);
|
||||||
});
|
});
|
||||||
Table.set_function("Sleep", &LuaAPI::MP::Sleep);
|
Table.set_function("Sleep", &LuaAPI::MP::Sleep);
|
||||||
//Table.set_function("Set", &LuaAPI::MP::Set);
|
Table.set_function("Set", &LuaAPI::MP::Set);
|
||||||
//Table.set_function("HttpsGET", &LuaAPI::MP::HttpsGET);
|
//Table.set_function("HttpsGET", &LuaAPI::MP::HttpsGET);
|
||||||
//Table.set_function("HttpsPOST", &LuaAPI::MP::HttpsPOST);
|
//Table.set_function("HttpsPOST", &LuaAPI::MP::HttpsPOST);
|
||||||
Table.create_named("Settings",
|
Table.create_named("Settings",
|
||||||
|
|||||||
Reference in New Issue
Block a user