mirror of https://github.com/OpenMW/openmw.git
Fixes and refactoring
parent
25cc884c17
commit
702eb19271
@ -0,0 +1,59 @@
|
|||||||
|
#include "gmock/gmock.h"
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
|
#include <components/lua/omwscriptsparser.hpp>
|
||||||
|
|
||||||
|
#include "testing_util.hpp"
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
using namespace testing;
|
||||||
|
|
||||||
|
TestFile file1(
|
||||||
|
"#comment.lua\n"
|
||||||
|
"\n"
|
||||||
|
"script1.lua\n"
|
||||||
|
"some mod/Some Script.lua"
|
||||||
|
);
|
||||||
|
TestFile file2(
|
||||||
|
"#comment.lua\r\n"
|
||||||
|
"\r\n"
|
||||||
|
"script2.lua\r\n"
|
||||||
|
"some other mod/Some Script.lua\r"
|
||||||
|
);
|
||||||
|
TestFile emptyFile("");
|
||||||
|
TestFile invalidFile("Invalid file");
|
||||||
|
|
||||||
|
struct OMWScriptsParserTest : Test
|
||||||
|
{
|
||||||
|
std::unique_ptr<VFS::Manager> mVFS = createTestVFS({
|
||||||
|
{"file1.omwscripts", &file1},
|
||||||
|
{"file2.omwscripts", &file2},
|
||||||
|
{"empty.omwscripts", &emptyFile},
|
||||||
|
{"invalid.lua", &file1},
|
||||||
|
{"invalid.omwscripts", &invalidFile},
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
TEST_F(OMWScriptsParserTest, Basic)
|
||||||
|
{
|
||||||
|
internal::CaptureStdout();
|
||||||
|
std::vector<std::string> res = LuaUtil::parseOMWScriptsFiles(
|
||||||
|
mVFS.get(), {"file2.omwscripts", "empty.omwscripts", "file1.omwscripts"});
|
||||||
|
EXPECT_EQ(internal::GetCapturedStdout(), "");
|
||||||
|
EXPECT_THAT(res, ElementsAre("script2.lua", "some other mod/Some Script.lua",
|
||||||
|
"script1.lua", "some mod/Some Script.lua"));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(OMWScriptsParserTest, InvalidFiles)
|
||||||
|
{
|
||||||
|
internal::CaptureStdout();
|
||||||
|
std::vector<std::string> res = LuaUtil::parseOMWScriptsFiles(
|
||||||
|
mVFS.get(), {"invalid.lua", "invalid.omwscripts"});
|
||||||
|
EXPECT_EQ(internal::GetCapturedStdout(),
|
||||||
|
"Script list should have suffix '.omwscripts', got: 'invalid.lua'\n"
|
||||||
|
"Lua script should have suffix '.lua', got: 'Invalid file'\n");
|
||||||
|
EXPECT_THAT(res, ElementsAre());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,44 @@
|
|||||||
|
#include "omwscriptsparser.hpp"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
#include <components/debug/debuglog.hpp>
|
||||||
|
|
||||||
|
std::vector<std::string> LuaUtil::parseOMWScriptsFiles(const VFS::Manager* vfs, const std::vector<std::string>& scriptLists)
|
||||||
|
{
|
||||||
|
auto endsWith = [](std::string_view s, std::string_view suffix)
|
||||||
|
{
|
||||||
|
return s.size() >= suffix.size() && std::equal(suffix.rbegin(), suffix.rend(), s.rbegin());
|
||||||
|
};
|
||||||
|
std::vector<std::string> res;
|
||||||
|
for (const std::string& scriptListFile : scriptLists)
|
||||||
|
{
|
||||||
|
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(std::min(pos + 1, view.size()));
|
||||||
|
if (!line.empty() && line.back() == '\r')
|
||||||
|
line = line.substr(0, pos - 1);
|
||||||
|
// Lines starting with '#' are comments.
|
||||||
|
// TODO: Maybe make the parser more robust. It is a bit inconsistent that 'path/#to/file.lua'
|
||||||
|
// is a valid path, but '#path/to/file.lua' is considered as a comment and ignored.
|
||||||
|
if (line.empty() || line[0] == '#')
|
||||||
|
continue;
|
||||||
|
if (endsWith(line, ".lua"))
|
||||||
|
res.push_back(std::string(line));
|
||||||
|
else
|
||||||
|
Log(Debug::Error) << "Lua script should have suffix '.lua', got: '" << line.substr(0, 300) << "'";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
#ifndef COMPONENTS_LUA_OMWSCRIPTSPARSER_H
|
||||||
|
#define COMPONENTS_LUA_OMWSCRIPTSPARSER_H
|
||||||
|
|
||||||
|
#include <components/vfs/manager.hpp>
|
||||||
|
|
||||||
|
namespace LuaUtil
|
||||||
|
{
|
||||||
|
|
||||||
|
// Parses list of `*.omwscripts` files.
|
||||||
|
std::vector<std::string> parseOMWScriptsFiles(const VFS::Manager* vfs, const std::vector<std::string>& scriptLists);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // COMPONENTS_LUA_OMWSCRIPTSPARSER_H
|
Loading…
Reference in New Issue