Skip to content

Commit

Permalink
Merge branch 'vfs_normalized_path_12' into 'master'
Browse files Browse the repository at this point in the history
Store Lua script path as VFS normalized (#8138)

See merge request OpenMW/openmw!4373
  • Loading branch information
psi29a committed Sep 16, 2024
2 parents 96ec3a8 + b4f77e8 commit b9cb028
Show file tree
Hide file tree
Showing 14 changed files with 203 additions and 123 deletions.
29 changes: 19 additions & 10 deletions apps/components_tests/fx/technique.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@

namespace
{
constexpr VFS::Path::NormalizedView techniquePropertiesPath("shaders/technique_properties.omwfx");

TestingOpenMW::VFSTestFile technique_properties(R"(
TestingOpenMW::VFSTestFile techniqueProperties(R"(
fragment main {}
vertex main {}
technique {
Expand All @@ -26,7 +27,9 @@ namespace
}
)");

TestingOpenMW::VFSTestFile rendertarget_properties{ R"(
constexpr VFS::Path::NormalizedView rendertargetPropertiesPath("shaders/rendertarget_properties.omwfx");

TestingOpenMW::VFSTestFile rendertargetProperties{ R"(
render_target rendertarget {
width_ratio = 0.5;
height_ratio = 0.5;
Expand All @@ -52,7 +55,9 @@ namespace
technique { passes = downsample2x, main; }
)" };

TestingOpenMW::VFSTestFile uniform_properties{ R"(
constexpr VFS::Path::NormalizedView uniformPropertiesPath("shaders/uniform_properties.omwfx");

TestingOpenMW::VFSTestFile uniformProperties{ R"(
uniform_vec4 uVec4 {
default = vec4(0,0,0,0);
min = vec4(0,1,0,0);
Expand All @@ -66,13 +71,17 @@ namespace
technique { passes = main; }
)" };

TestingOpenMW::VFSTestFile missing_sampler_source{ R"(
constexpr VFS::Path::NormalizedView missingSamplerSourcePath("shaders/missing_sampler_source.omwfx");

TestingOpenMW::VFSTestFile missingSamplerSource{ R"(
sampler_1d mysampler1d { }
fragment main { }
technique { passes = main; }
)" };

TestingOpenMW::VFSTestFile repeated_shared_block{ R"(
constexpr VFS::Path::NormalizedView repeatedSharedBlockPath("shaders/repeated_shared_block.omwfx");

TestingOpenMW::VFSTestFile repeatedSharedBlock{ R"(
shared {
float myfloat = 1.0;
}
Expand All @@ -92,11 +101,11 @@ namespace

TechniqueTest()
: mVFS(TestingOpenMW::createTestVFS({
{ "shaders/technique_properties.omwfx", &technique_properties },
{ "shaders/rendertarget_properties.omwfx", &rendertarget_properties },
{ "shaders/uniform_properties.omwfx", &uniform_properties },
{ "shaders/missing_sampler_source.omwfx", &missing_sampler_source },
{ "shaders/repeated_shared_block.omwfx", &repeated_shared_block },
{ techniquePropertiesPath, &techniqueProperties },
{ rendertargetPropertiesPath, &rendertargetProperties },
{ uniformPropertiesPath, &uniformProperties },
{ missingSamplerSourcePath, &missingSamplerSource },
{ repeatedSharedBlockPath, &repeatedSharedBlock },
}))
, mImageManager(mVFS.get(), 0)
{
Expand Down
20 changes: 10 additions & 10 deletions apps/components_tests/lua/test_configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ namespace
EXPECT_EQ(LuaUtil::scriptCfgToString(cfg.mScripts[1]), "PLAYER : my_mod/player.lua");
EXPECT_EQ(LuaUtil::scriptCfgToString(cfg.mScripts[2]), "CUSTOM : my_mod/some_other_script.lua");
EXPECT_EQ(LuaUtil::scriptCfgToString(cfg.mScripts[3]), "PLAYER NPC CREATURE : my_mod/some_other_script.lua");
EXPECT_EQ(LuaUtil::scriptCfgToString(cfg.mScripts[4]), ": my_mod/player.LUA");
EXPECT_EQ(LuaUtil::scriptCfgToString(cfg.mScripts[4]), ": my_mod/player.lua");
EXPECT_EQ(LuaUtil::scriptCfgToString(cfg.mScripts[5]), "CUSTOM CREATURE : my_mod/creature.lua");

LuaUtil::ScriptsConfiguration conf;
Expand All @@ -54,7 +54,7 @@ namespace
// cfg.mScripts[1] is overridden by cfg.mScripts[4]
// cfg.mScripts[2] is overridden by cfg.mScripts[3]
EXPECT_EQ(LuaUtil::scriptCfgToString(conf[1]), "PLAYER NPC CREATURE : my_mod/some_other_script.lua");
EXPECT_EQ(LuaUtil::scriptCfgToString(conf[2]), ": my_mod/player.LUA");
EXPECT_EQ(LuaUtil::scriptCfgToString(conf[2]), ": my_mod/player.lua");
EXPECT_EQ(LuaUtil::scriptCfgToString(conf[3]), "CUSTOM CREATURE : my_mod/creature.lua");

EXPECT_THAT(asVector(conf.getGlobalConf()), ElementsAre(Pair(0, "")));
Expand Down Expand Up @@ -89,7 +89,7 @@ namespace
{
ESM::LuaScriptsCfg cfg;
ESM::LuaScriptCfg& script1 = cfg.mScripts.emplace_back();
script1.mScriptPath = "Script1.lua";
script1.mScriptPath = VFS::Path::Normalized("Script1.lua");
script1.mInitializationData = "data1";
script1.mFlags = ESM::LuaScriptCfg::sPlayer;
script1.mTypes.push_back(ESM::REC_CREA);
Expand All @@ -98,12 +98,12 @@ namespace
script1.mRefs.push_back({ true, 2, 4, "" });

ESM::LuaScriptCfg& script2 = cfg.mScripts.emplace_back();
script2.mScriptPath = "Script2.lua";
script2.mScriptPath = VFS::Path::Normalized("Script2.lua");
script2.mFlags = ESM::LuaScriptCfg::sCustom;
script2.mTypes.push_back(ESM::REC_CONT);

ESM::LuaScriptCfg& script1Extra = cfg.mScripts.emplace_back();
script1Extra.mScriptPath = "script1.LUA";
script1Extra.mScriptPath = VFS::Path::Normalized("script1.LUA");
script1Extra.mFlags = ESM::LuaScriptCfg::sCustom | ESM::LuaScriptCfg::sMerge;
script1Extra.mTypes.push_back(ESM::REC_NPC_);
script1Extra.mRecords.push_back({ false, ESM::RefId::stringRefId("rat"), "" });
Expand All @@ -115,8 +115,8 @@ namespace
conf.init(cfg);
ASSERT_EQ(conf.size(), 2);
EXPECT_EQ(LuaUtil::scriptCfgToString(conf[0]),
"CUSTOM PLAYER CREATURE NPC : Script1.lua ; data 5 bytes ; 3 records ; 4 objects");
EXPECT_EQ(LuaUtil::scriptCfgToString(conf[1]), "CUSTOM CONTAINER : Script2.lua");
"CUSTOM PLAYER CREATURE NPC : script1.lua ; data 5 bytes ; 3 records ; 4 objects");
EXPECT_EQ(LuaUtil::scriptCfgToString(conf[1]), "CUSTOM CONTAINER : script2.lua");

EXPECT_THAT(asVector(conf.getPlayerConf()), ElementsAre(Pair(0, "data1")));
EXPECT_THAT(asVector(conf.getLocalConf(ESM::REC_CONT, ESM::RefId::stringRefId("something"), ESM::RefNum())),
Expand All @@ -139,7 +139,7 @@ namespace
ElementsAre(Pair(0, "data1"), Pair(1, "")));

ESM::LuaScriptCfg& script3 = cfg.mScripts.emplace_back();
script3.mScriptPath = "script1.lua";
script3.mScriptPath = VFS::Path::Normalized("script1.lua");
script3.mFlags = ESM::LuaScriptCfg::sGlobal;
EXPECT_ERROR(conf.init(cfg), "Flags mismatch for script1.lua");
}
Expand Down Expand Up @@ -168,13 +168,13 @@ namespace
}
{
ESM::LuaScriptCfg& script = cfg.mScripts.emplace_back();
script.mScriptPath = "test_global.lua";
script.mScriptPath = VFS::Path::Normalized("test_global.lua");
script.mFlags = ESM::LuaScriptCfg::sGlobal;
script.mInitializationData = luaData;
}
{
ESM::LuaScriptCfg& script = cfg.mScripts.emplace_back();
script.mScriptPath = "test_local.lua";
script.mScriptPath = VFS::Path::Normalized("test_local.lua");
script.mFlags = ESM::LuaScriptCfg::sMerge;
script.mTypes.push_back(ESM::REC_DOOR);
script.mTypes.push_back(ESM::REC_MISC);
Expand Down
19 changes: 13 additions & 6 deletions apps/components_tests/lua/test_l10n.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ namespace
return lua.safe_script("return " + luaCode).get<T>();
}

constexpr VFS::Path::NormalizedView test1EnPath("l10n/test1/en.yaml");
constexpr VFS::Path::NormalizedView test1EnUsPath("l10n/test1/en_us.yaml");
constexpr VFS::Path::NormalizedView test1DePath("l10n/test1/de.yaml");
constexpr VFS::Path::NormalizedView test2EnPath("l10n/test2/en.yaml");
constexpr VFS::Path::NormalizedView test3EnPath("l10n/test3/en.yaml");
constexpr VFS::Path::NormalizedView test3DePath("l10n/test3/de.yaml");

VFSTestFile invalidScript("not a script");
VFSTestFile incorrectScript(
"return { incorrectSection = {}, engineHandlers = { incorrectHandler = function() end } }");
Expand Down Expand Up @@ -67,12 +74,12 @@ you_have_arrows: "Arrows count: {count}"
struct LuaL10nTest : Test
{
std::unique_ptr<VFS::Manager> mVFS = createTestVFS({
{ "l10n/Test1/en.yaml", &test1En },
{ "l10n/Test1/en_US.yaml", &test1EnUS },
{ "l10n/Test1/de.yaml", &test1De },
{ "l10n/Test2/en.yaml", &test2En },
{ "l10n/Test3/en.yaml", &test1En },
{ "l10n/Test3/de.yaml", &test1De },
{ test1EnPath, &test1En },
{ test1EnUsPath, &test1EnUS },
{ test1DePath, &test1De },
{ test2EnPath, &test2En },
{ test3EnPath, &test1En },
{ test3DePath, &test1De },
});

LuaUtil::ScriptsConfiguration mCfg;
Expand Down
26 changes: 22 additions & 4 deletions apps/components_tests/lua/test_lua.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ namespace
{
using namespace testing;

constexpr VFS::Path::NormalizedView counterPath("aaa/counter.lua");

TestingOpenMW::VFSTestFile counterFile(R"X(
x = 42
return {
Expand All @@ -17,8 +19,12 @@ return {
}
)X");

constexpr VFS::Path::NormalizedView invalidPath("invalid.lua");

TestingOpenMW::VFSTestFile invalidScriptFile("Invalid script");

constexpr VFS::Path::NormalizedView testsPath("bbb/tests.lua");

TestingOpenMW::VFSTestFile testsFile(R"X(
return {
-- should work
Expand Down Expand Up @@ -51,6 +57,8 @@ return {
}
)X");

constexpr VFS::Path::NormalizedView metaIndexErrorPath("metaindexerror.lua");

TestingOpenMW::VFSTestFile metaIndexErrorFile(
"return setmetatable({}, { __index = function(t, key) error('meta index error') end })");

Expand All @@ -66,22 +74,32 @@ return {
return buf.str();
}

constexpr VFS::Path::NormalizedView bigPath("big.lua");

TestingOpenMW::VFSTestFile bigScriptFile(genBigScript());

constexpr VFS::Path::NormalizedView requireBigPath("requirebig.lua");

TestingOpenMW::VFSTestFile requireBigScriptFile("local x = require('big') ; return {x}");

struct LuaStateTest : Test
{
std::unique_ptr<VFS::Manager> mVFS = TestingOpenMW::createTestVFS({ { "aaa/counter.lua", &counterFile },
{ "bbb/tests.lua", &testsFile }, { "invalid.lua", &invalidScriptFile }, { "big.lua", &bigScriptFile },
{ "requireBig.lua", &requireBigScriptFile }, { "metaIndexError.lua", &metaIndexErrorFile } });
std::unique_ptr<VFS::Manager> mVFS = TestingOpenMW::createTestVFS({
{ counterPath, &counterFile },
{ testsPath, &testsFile },
{ invalidPath, &invalidScriptFile },
{ bigPath, &bigScriptFile },
{ requireBigPath, &requireBigScriptFile },
{ metaIndexErrorPath, &metaIndexErrorFile },
});

LuaUtil::ScriptsConfiguration mCfg;
LuaUtil::LuaState mLua{ mVFS.get(), &mCfg };
};

TEST_F(LuaStateTest, Sandbox)
{
const VFS::Path::Normalized path("aaa/counter.lua");
const VFS::Path::Normalized path(counterPath);
sol::table script1 = mLua.runInNewSandbox(path);

EXPECT_EQ(LuaUtil::call(script1["get"]).get<int>(), 42);
Expand Down
Loading

0 comments on commit b9cb028

Please sign in to comment.