6
6
#include < components/lua/asyncpackage.hpp>
7
7
#include < components/lua/luastate.hpp>
8
8
#include < components/lua/scriptscontainer.hpp>
9
+ #include < components/lua/scripttracker.hpp>
9
10
10
11
#include < components/testing/util.hpp>
11
12
@@ -135,6 +136,31 @@ return {
135
136
end,
136
137
},
137
138
}
139
+ )X" );
140
+
141
+ constexpr VFS::Path::NormalizedView unloadPath (" unload.lua" );
142
+
143
+ VFSTestFile unloadScript (R"X(
144
+ x = 0
145
+ y = 0
146
+ z = 0
147
+ return {
148
+ engineHandlers = {
149
+ onSave = function(state)
150
+ print('saving', x, y, z)
151
+ return {x = x, y = y}
152
+ end,
153
+ onLoad = function(state)
154
+ x, y = state.x, state.y
155
+ print('loaded', x, y, z)
156
+ end
157
+ },
158
+ eventHandlers = {
159
+ Set = function(eventData)
160
+ x, y, z = eventData.x, eventData.y, eventData.z
161
+ end
162
+ }
163
+ }
138
164
)X" );
139
165
140
166
struct LuaScriptsContainerTest : Test
@@ -151,6 +177,7 @@ return {
151
177
{ testInterfacePath, &interfaceScript },
152
178
{ overrideInterfacePath, &overrideInterfaceScript },
153
179
{ useInterfacePath, &useInterfaceScript },
180
+ { unloadPath, &unloadScript },
154
181
});
155
182
156
183
LuaUtil::ScriptsConfiguration mCfg ;
@@ -171,6 +198,7 @@ CUSTOM, NPC: loadSave2.lua
171
198
CUSTOM, PLAYER: testInterface.lua
172
199
CUSTOM, PLAYER: overrideInterface.lua
173
200
CUSTOM, PLAYER: useInterface.lua
201
+ CUSTOM: unload.lua
174
202
)X" );
175
203
mCfg .init (std::move (cfg));
176
204
}
@@ -511,4 +539,35 @@ CUSTOM, PLAYER: useInterface.lua
511
539
Log::sMinDebugLevel = level;
512
540
}
513
541
542
+ TEST_F (LuaScriptsContainerTest, Unload)
543
+ {
544
+ LuaUtil::ScriptTracker tracker;
545
+ LuaUtil::ScriptsContainer scripts1 (&mLua , " Test" , &tracker, false );
546
+
547
+ EXPECT_TRUE (scripts1.addCustomScript (*mCfg .findId (unloadPath)));
548
+ EXPECT_EQ (tracker.size (), 1 );
549
+
550
+ mLua .protectedCall ([&](LuaUtil::LuaView& lua) {
551
+ scripts1.receiveEvent (" Set" , LuaUtil::serialize (lua.sol ().create_table_with (" x" , 3 , " y" , 2 , " z" , 1 )));
552
+ testing::internal::CaptureStdout ();
553
+ for (int i = 0 ; i < 600 ; ++i)
554
+ tracker.unloadInactiveScripts (lua);
555
+ EXPECT_EQ (tracker.size (), 0 );
556
+ scripts1.receiveEvent (" Set" , LuaUtil::serialize (lua.sol ().create_table_with (" x" , 10 , " y" , 20 , " z" , 30 )));
557
+ EXPECT_EQ (internal::GetCapturedStdout (),
558
+ " Test[unload.lua]:\t saving\t 3\t 2\t 1\n "
559
+ " Test[unload.lua]:\t loaded\t 3\t 2\t 0\n " );
560
+ });
561
+ EXPECT_EQ (tracker.size (), 1 );
562
+ ESM::LuaScripts data;
563
+ scripts1.save (data);
564
+ EXPECT_EQ (tracker.size (), 1 );
565
+ mLua .protectedCall ([&](LuaUtil::LuaView& lua) {
566
+ for (int i = 0 ; i < 600 ; ++i)
567
+ tracker.unloadInactiveScripts (lua);
568
+ });
569
+ EXPECT_EQ (tracker.size (), 0 );
570
+ scripts1.load (data);
571
+ EXPECT_EQ (tracker.size (), 0 );
572
+ }
514
573
}
0 commit comments