mirror of
https://gitlab.com/OpenMW/openmw.git
synced 2025-02-21 09:39:56 +00:00
Control active Lua scripts from openmw.cfg
This commit is contained in:
parent
8c6d303730
commit
87b5afb9bf
@ -493,6 +493,11 @@ void OMW::Engine::addGroundcoverFile(const std::string& file)
|
||||
mGroundcoverFiles.emplace_back(file);
|
||||
}
|
||||
|
||||
void OMW::Engine::addLuaScriptListFile(const std::string& file)
|
||||
{
|
||||
mLuaScriptListFiles.push_back(file);
|
||||
}
|
||||
|
||||
void OMW::Engine::setSkipMenu (bool skipMenu, bool newGame)
|
||||
{
|
||||
mSkipMenu = skipMenu;
|
||||
@ -707,7 +712,7 @@ void OMW::Engine::prepareEngine (Settings::Manager & settings)
|
||||
|
||||
mViewer->addEventHandler(mScreenCaptureHandler);
|
||||
|
||||
mLuaManager = new MWLua::LuaManager(mVFS.get());
|
||||
mLuaManager = new MWLua::LuaManager(mVFS.get(), mLuaScriptListFiles);
|
||||
mEnvironment.setLuaManager(mLuaManager);
|
||||
|
||||
// Create input and UI first to set up a bootstrapping environment for
|
||||
|
@ -71,6 +71,7 @@ namespace OMW
|
||||
std::string mCellName;
|
||||
std::vector<std::string> mContentFiles;
|
||||
std::vector<std::string> mGroundcoverFiles;
|
||||
std::vector<std::string> mLuaScriptListFiles;
|
||||
bool mSkipMenu;
|
||||
bool mUseSound;
|
||||
bool mCompileAll;
|
||||
@ -144,6 +145,7 @@ namespace OMW
|
||||
*/
|
||||
void addContentFile(const std::string& file);
|
||||
void addGroundcoverFile(const std::string& file);
|
||||
void addLuaScriptListFile(const std::string& file);
|
||||
|
||||
/// Disable or enable all sounds
|
||||
void setSoundUsage(bool soundUsage);
|
||||
|
@ -65,6 +65,9 @@ bool parseOptions (int argc, char** argv, OMW::Engine& engine, Files::Configurat
|
||||
("groundcover", bpo::value<Files::EscapeStringVector>()->default_value(Files::EscapeStringVector(), "")
|
||||
->multitoken()->composing(), "groundcover content file(s): esm/esp, or omwgame/omwaddon")
|
||||
|
||||
("lua-scripts", bpo::value<Files::EscapeStringVector>()->default_value(Files::EscapeStringVector(), "")
|
||||
->multitoken()->composing(), "file(s) with a list of global Lua scripts: omwscripts")
|
||||
|
||||
("no-sound", bpo::value<bool>()->implicit_value(true)
|
||||
->default_value(false), "disable all sounds")
|
||||
|
||||
@ -204,6 +207,10 @@ bool parseOptions (int argc, char** argv, OMW::Engine& engine, Files::Configurat
|
||||
engine.addGroundcoverFile(file);
|
||||
}
|
||||
|
||||
StringsVector luaScriptLists = variables["lua-scripts"].as<Files::EscapeStringVector>().toStdStringVector();
|
||||
for (const auto& file : luaScriptLists)
|
||||
engine.addLuaScriptListFile(file);
|
||||
|
||||
// startup-settings
|
||||
engine.setCell(variables["start"].as<Files::EscapeHashString>().toStdString());
|
||||
engine.setSkipMenu (variables["skip-menu"].as<bool>(), variables["new-game"].as<bool>());
|
||||
|
@ -17,7 +17,7 @@
|
||||
namespace MWLua
|
||||
{
|
||||
|
||||
LuaManager::LuaManager(const VFS::Manager* vfs) : mLua(vfs)
|
||||
LuaManager::LuaManager(const VFS::Manager* vfs, const std::vector<std::string>& globalScriptLists) : mLua(vfs)
|
||||
{
|
||||
Log(Debug::Info) << "Lua version: " << LuaUtil::getLuaVersion();
|
||||
|
||||
@ -48,13 +48,45 @@ namespace MWLua
|
||||
mLua.addCommonPackage("openmw.core", initCorePackage(context));
|
||||
mGlobalScripts.addPackage("openmw.world", initWorldPackage(context));
|
||||
mNearbyPackage = initNearbyPackage(localContext);
|
||||
|
||||
auto endsWith = [](std::string_view s, std::string_view suffix)
|
||||
{
|
||||
return s.size() >= suffix.size() && std::equal(suffix.rbegin(), suffix.rend(), s.rbegin());
|
||||
};
|
||||
for (const std::string& scriptListFile : globalScriptLists)
|
||||
{
|
||||
if (!endsWith(scriptListFile, ".omwscripts"))
|
||||
{
|
||||
Log(Debug::Error) << "Script list should have suffix '.omwscripts', got: '" << scriptListFile << "'";
|
||||
continue;
|
||||
}
|
||||
std::string content(std::istreambuf_iterator<char>(*vfs->get(scriptListFile)), {});
|
||||
std::string_view view(content);
|
||||
while (!view.empty())
|
||||
{
|
||||
size_t pos = 0;
|
||||
while (pos < view.size() && view[pos] != '\n')
|
||||
pos++;
|
||||
std::string_view line = view.substr(0, pos);
|
||||
view = view.substr(pos + 1);
|
||||
if (line.empty() || line[0] == '#')
|
||||
continue;
|
||||
if (line.back() == '\r')
|
||||
line = line.substr(0, pos - 1);
|
||||
if (endsWith(line, ".lua"))
|
||||
mGlobalScriptList.push_back(std::string(line));
|
||||
else
|
||||
Log(Debug::Error) << "Lua script should have suffix '.lua', got: '" << line.substr(0, 300) << "'";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void LuaManager::init()
|
||||
{
|
||||
mKeyPressEvents.clear();
|
||||
if (mGlobalScripts.addNewScript("test.lua"))
|
||||
Log(Debug::Info) << "Global script started: test.lua";
|
||||
for (const std::string& path : mGlobalScriptList)
|
||||
if (mGlobalScripts.addNewScript(path))
|
||||
Log(Debug::Info) << "Global script started: " << path;
|
||||
}
|
||||
|
||||
void LuaManager::update(bool paused, float dt)
|
||||
|
@ -21,7 +21,7 @@ namespace MWLua
|
||||
class LuaManager : public MWBase::LuaManager
|
||||
{
|
||||
public:
|
||||
LuaManager(const VFS::Manager* vfs);
|
||||
LuaManager(const VFS::Manager* vfs, const std::vector<std::string>& globalScriptLists);
|
||||
~LuaManager() {}
|
||||
|
||||
// Called by engine.cpp when environment is fully initialized.
|
||||
@ -64,6 +64,7 @@ namespace MWLua
|
||||
LuaUtil::LuaState mLua;
|
||||
sol::table mNearbyPackage;
|
||||
|
||||
std::vector<std::string> mGlobalScriptList;
|
||||
GlobalScripts mGlobalScripts{&mLua};
|
||||
std::set<LocalScripts*> mActiveLocalScripts;
|
||||
WorldView mWorldView;
|
||||
|
Loading…
x
Reference in New Issue
Block a user