mirror of
https://github.com/BeamMP/BeamMP-Server.git
synced 2026-06-17 14:12:25 +00:00
Add ability to switch into other lua states
This commit is contained in:
+3
-2
@@ -18,7 +18,7 @@ public:
|
|||||||
Commandline& Internal() { return mCommandline; }
|
Commandline& Internal() { return mCommandline; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void ChangeToLuaConsole();
|
void ChangeToLuaConsole(const std::string& LuaStateId);
|
||||||
void ChangeToRegularConsole();
|
void ChangeToRegularConsole();
|
||||||
|
|
||||||
Commandline mCommandline;
|
Commandline mCommandline;
|
||||||
@@ -27,5 +27,6 @@ private:
|
|||||||
TLuaEngine* mLuaEngine { nullptr };
|
TLuaEngine* mLuaEngine { nullptr };
|
||||||
bool mIsLuaConsole { false };
|
bool mIsLuaConsole { false };
|
||||||
bool mFirstTime { true };
|
bool mFirstTime { true };
|
||||||
const std::string mStateId = "BEAMMP_SERVER_CONSOLE";
|
std::string mStateId;
|
||||||
|
const std::string mDefaultStateId = "BEAMMP_SERVER_CONSOLE";
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -84,6 +84,7 @@ public:
|
|||||||
|
|
||||||
static void WaitForAll(std::vector<std::shared_ptr<TLuaResult>>& Results);
|
static void WaitForAll(std::vector<std::shared_ptr<TLuaResult>>& Results);
|
||||||
void ReportErrors(const std::vector<std::shared_ptr<TLuaResult> >& Results);
|
void ReportErrors(const std::vector<std::shared_ptr<TLuaResult> >& Results);
|
||||||
|
bool HasState(TLuaStateId StateId);
|
||||||
[[nodiscard]] std::shared_ptr<TLuaResult> EnqueueScript(TLuaStateId StateID, const TLuaChunk& Script);
|
[[nodiscard]] std::shared_ptr<TLuaResult> EnqueueScript(TLuaStateId StateID, const TLuaChunk& Script);
|
||||||
[[nodiscard]] std::shared_ptr<TLuaResult> EnqueueFunctionCall(TLuaStateId StateID, const std::string& FunctionName, const std::vector<TLuaArgTypes>& Args);
|
[[nodiscard]] std::shared_ptr<TLuaResult> EnqueueFunctionCall(TLuaStateId StateID, const std::string& FunctionName, const std::vector<TLuaArgTypes>& Args);
|
||||||
void EnsureStateExists(TLuaStateId StateId, const std::string& Name, bool DontCallOnInit = false);
|
void EnsureStateExists(TLuaStateId StateId, const std::string& Name, bool DontCallOnInit = false);
|
||||||
|
|||||||
+27
-6
@@ -2,6 +2,7 @@
|
|||||||
#include "Common.h"
|
#include "Common.h"
|
||||||
#include "Compat.h"
|
#include "Compat.h"
|
||||||
|
|
||||||
|
#include "CustomAssert.h"
|
||||||
#include "LuaAPI.h"
|
#include "LuaAPI.h"
|
||||||
#include "TLuaEngine.h"
|
#include "TLuaEngine.h"
|
||||||
|
|
||||||
@@ -91,10 +92,15 @@ void TConsole::BackupOldLog() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void TConsole::ChangeToLuaConsole() {
|
void TConsole::ChangeToLuaConsole(const std::string& LuaStateId) {
|
||||||
if (!mIsLuaConsole) {
|
if (!mIsLuaConsole) {
|
||||||
|
mStateId = LuaStateId;
|
||||||
mIsLuaConsole = true;
|
mIsLuaConsole = true;
|
||||||
Application::Console().WriteRaw("Entered Lua console. To exit, type `exit()`");
|
if (mStateId != mDefaultStateId) {
|
||||||
|
Application::Console().WriteRaw("Entered Lua console for state '" + mStateId + "'. To exit, type `exit()`");
|
||||||
|
} else {
|
||||||
|
Application::Console().WriteRaw("Entered Lua console. To exit, type `exit()`");
|
||||||
|
}
|
||||||
mCachedRegularHistory = mCommandline.history();
|
mCachedRegularHistory = mCommandline.history();
|
||||||
mCommandline.set_history(mCachedLuaHistory);
|
mCommandline.set_history(mCachedLuaHistory);
|
||||||
mCommandline.set_prompt("lua> ");
|
mCommandline.set_prompt("lua> ");
|
||||||
@@ -104,10 +110,15 @@ void TConsole::ChangeToLuaConsole() {
|
|||||||
void TConsole::ChangeToRegularConsole() {
|
void TConsole::ChangeToRegularConsole() {
|
||||||
if (mIsLuaConsole) {
|
if (mIsLuaConsole) {
|
||||||
mIsLuaConsole = false;
|
mIsLuaConsole = false;
|
||||||
Application::Console().WriteRaw("Left Lua console.");
|
if (mStateId != mDefaultStateId) {
|
||||||
|
Application::Console().WriteRaw("Left Lua console for state '" + mStateId + "'.");
|
||||||
|
} else {
|
||||||
|
Application::Console().WriteRaw("Left Lua console.");
|
||||||
|
}
|
||||||
mCachedLuaHistory = mCommandline.history();
|
mCachedLuaHistory = mCommandline.history();
|
||||||
mCommandline.set_history(mCachedRegularHistory);
|
mCommandline.set_history(mCachedRegularHistory);
|
||||||
mCommandline.set_prompt("> ");
|
mCommandline.set_prompt("> ");
|
||||||
|
mStateId = mDefaultStateId;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -144,8 +155,18 @@ TConsole::TConsole() {
|
|||||||
if (cmd == "exit") {
|
if (cmd == "exit") {
|
||||||
beammp_info("gracefully shutting down");
|
beammp_info("gracefully shutting down");
|
||||||
Application::GracefullyShutdown();
|
Application::GracefullyShutdown();
|
||||||
} else if (cmd == "lua") {
|
} else if (cmd.size() >= 3 && cmd.substr(0, 3) == "lua") {
|
||||||
ChangeToLuaConsole();
|
if (cmd.size() > 3) {
|
||||||
|
auto NewStateId = cmd.substr(4);
|
||||||
|
beammp_assert(!NewStateId.empty());
|
||||||
|
if (mLuaEngine->HasState(NewStateId)) {
|
||||||
|
ChangeToLuaConsole(NewStateId);
|
||||||
|
} else {
|
||||||
|
Application::Console().WriteRaw("Lua state '" + NewStateId + "' is not a known state. Didn't switch to Lua.");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
ChangeToLuaConsole(mDefaultStateId);
|
||||||
|
}
|
||||||
} else if (!cmd.empty()) {
|
} else if (!cmd.empty()) {
|
||||||
auto FutureIsNonNil =
|
auto FutureIsNonNil =
|
||||||
[](const std::shared_ptr<TLuaResult>& Future) {
|
[](const std::shared_ptr<TLuaResult>& Future) {
|
||||||
@@ -208,5 +229,5 @@ void TConsole::WriteRaw(const std::string& str) {
|
|||||||
|
|
||||||
void TConsole::InitializeLuaConsole(TLuaEngine& Engine) {
|
void TConsole::InitializeLuaConsole(TLuaEngine& Engine) {
|
||||||
mLuaEngine = &Engine;
|
mLuaEngine = &Engine;
|
||||||
Engine.EnsureStateExists(mStateId, "Console");
|
Engine.EnsureStateExists(mDefaultStateId, "Console");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -162,6 +162,11 @@ void TLuaEngine::ReportErrors(const std::vector<std::shared_ptr<TLuaResult>>& Re
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool TLuaEngine::HasState(TLuaStateId StateId) {
|
||||||
|
std::unique_lock Lock(mLuaStatesMutex);
|
||||||
|
return mLuaStates.find(StateId) != mLuaStates.end();
|
||||||
|
}
|
||||||
|
|
||||||
std::shared_ptr<TLuaResult> TLuaEngine::EnqueueScript(TLuaStateId StateID, const TLuaChunk& Script) {
|
std::shared_ptr<TLuaResult> TLuaEngine::EnqueueScript(TLuaStateId StateID, const TLuaChunk& Script) {
|
||||||
std::unique_lock Lock(mLuaStatesMutex);
|
std::unique_lock Lock(mLuaStatesMutex);
|
||||||
return mLuaStates.at(StateID)->EnqueueScript(Script);
|
return mLuaStates.at(StateID)->EnqueueScript(Script);
|
||||||
|
|||||||
Reference in New Issue
Block a user