mirror of
https://github.com/OpenMW/openmw.git
synced 2025-04-13 19:06:44 +00:00
Control active Lua scripts from openmw.cfg
This commit is contained in:
parent
8c6d303730
commit
87b5afb9bf
5 changed files with 52 additions and 5 deletions
|
@ -493,6 +493,11 @@ void OMW::Engine::addGroundcoverFile(const std::string& file)
|
||||||
mGroundcoverFiles.emplace_back(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)
|
void OMW::Engine::setSkipMenu (bool skipMenu, bool newGame)
|
||||||
{
|
{
|
||||||
mSkipMenu = skipMenu;
|
mSkipMenu = skipMenu;
|
||||||
|
@ -707,7 +712,7 @@ void OMW::Engine::prepareEngine (Settings::Manager & settings)
|
||||||
|
|
||||||
mViewer->addEventHandler(mScreenCaptureHandler);
|
mViewer->addEventHandler(mScreenCaptureHandler);
|
||||||
|
|
||||||
mLuaManager = new MWLua::LuaManager(mVFS.get());
|
mLuaManager = new MWLua::LuaManager(mVFS.get(), mLuaScriptListFiles);
|
||||||
mEnvironment.setLuaManager(mLuaManager);
|
mEnvironment.setLuaManager(mLuaManager);
|
||||||
|
|
||||||
// Create input and UI first to set up a bootstrapping environment for
|
// Create input and UI first to set up a bootstrapping environment for
|
||||||
|
|
|
@ -71,6 +71,7 @@ namespace OMW
|
||||||
std::string mCellName;
|
std::string mCellName;
|
||||||
std::vector<std::string> mContentFiles;
|
std::vector<std::string> mContentFiles;
|
||||||
std::vector<std::string> mGroundcoverFiles;
|
std::vector<std::string> mGroundcoverFiles;
|
||||||
|
std::vector<std::string> mLuaScriptListFiles;
|
||||||
bool mSkipMenu;
|
bool mSkipMenu;
|
||||||
bool mUseSound;
|
bool mUseSound;
|
||||||
bool mCompileAll;
|
bool mCompileAll;
|
||||||
|
@ -144,6 +145,7 @@ namespace OMW
|
||||||
*/
|
*/
|
||||||
void addContentFile(const std::string& file);
|
void addContentFile(const std::string& file);
|
||||||
void addGroundcoverFile(const std::string& file);
|
void addGroundcoverFile(const std::string& file);
|
||||||
|
void addLuaScriptListFile(const std::string& file);
|
||||||
|
|
||||||
/// Disable or enable all sounds
|
/// Disable or enable all sounds
|
||||||
void setSoundUsage(bool soundUsage);
|
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(), "")
|
("groundcover", bpo::value<Files::EscapeStringVector>()->default_value(Files::EscapeStringVector(), "")
|
||||||
->multitoken()->composing(), "groundcover content file(s): esm/esp, or omwgame/omwaddon")
|
->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)
|
("no-sound", bpo::value<bool>()->implicit_value(true)
|
||||||
->default_value(false), "disable all sounds")
|
->default_value(false), "disable all sounds")
|
||||||
|
|
||||||
|
@ -204,6 +207,10 @@ bool parseOptions (int argc, char** argv, OMW::Engine& engine, Files::Configurat
|
||||||
engine.addGroundcoverFile(file);
|
engine.addGroundcoverFile(file);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
StringsVector luaScriptLists = variables["lua-scripts"].as<Files::EscapeStringVector>().toStdStringVector();
|
||||||
|
for (const auto& file : luaScriptLists)
|
||||||
|
engine.addLuaScriptListFile(file);
|
||||||
|
|
||||||
// startup-settings
|
// startup-settings
|
||||||
engine.setCell(variables["start"].as<Files::EscapeHashString>().toStdString());
|
engine.setCell(variables["start"].as<Files::EscapeHashString>().toStdString());
|
||||||
engine.setSkipMenu (variables["skip-menu"].as<bool>(), variables["new-game"].as<bool>());
|
engine.setSkipMenu (variables["skip-menu"].as<bool>(), variables["new-game"].as<bool>());
|
||||||
|
|
|
@ -17,7 +17,7 @@
|
||||||
namespace MWLua
|
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();
|
Log(Debug::Info) << "Lua version: " << LuaUtil::getLuaVersion();
|
||||||
|
|
||||||
|
@ -48,13 +48,45 @@ namespace MWLua
|
||||||
mLua.addCommonPackage("openmw.core", initCorePackage(context));
|
mLua.addCommonPackage("openmw.core", initCorePackage(context));
|
||||||
mGlobalScripts.addPackage("openmw.world", initWorldPackage(context));
|
mGlobalScripts.addPackage("openmw.world", initWorldPackage(context));
|
||||||
mNearbyPackage = initNearbyPackage(localContext);
|
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()
|
void LuaManager::init()
|
||||||
{
|
{
|
||||||
mKeyPressEvents.clear();
|
mKeyPressEvents.clear();
|
||||||
if (mGlobalScripts.addNewScript("test.lua"))
|
for (const std::string& path : mGlobalScriptList)
|
||||||
Log(Debug::Info) << "Global script started: test.lua";
|
if (mGlobalScripts.addNewScript(path))
|
||||||
|
Log(Debug::Info) << "Global script started: " << path;
|
||||||
}
|
}
|
||||||
|
|
||||||
void LuaManager::update(bool paused, float dt)
|
void LuaManager::update(bool paused, float dt)
|
||||||
|
|
|
@ -21,7 +21,7 @@ namespace MWLua
|
||||||
class LuaManager : public MWBase::LuaManager
|
class LuaManager : public MWBase::LuaManager
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
LuaManager(const VFS::Manager* vfs);
|
LuaManager(const VFS::Manager* vfs, const std::vector<std::string>& globalScriptLists);
|
||||||
~LuaManager() {}
|
~LuaManager() {}
|
||||||
|
|
||||||
// Called by engine.cpp when environment is fully initialized.
|
// Called by engine.cpp when environment is fully initialized.
|
||||||
|
@ -64,6 +64,7 @@ namespace MWLua
|
||||||
LuaUtil::LuaState mLua;
|
LuaUtil::LuaState mLua;
|
||||||
sol::table mNearbyPackage;
|
sol::table mNearbyPackage;
|
||||||
|
|
||||||
|
std::vector<std::string> mGlobalScriptList;
|
||||||
GlobalScripts mGlobalScripts{&mLua};
|
GlobalScripts mGlobalScripts{&mLua};
|
||||||
std::set<LocalScripts*> mActiveLocalScripts;
|
std::set<LocalScripts*> mActiveLocalScripts;
|
||||||
WorldView mWorldView;
|
WorldView mWorldView;
|
||||||
|
|
Loading…
Reference in a new issue