diff --git a/apps/openmw/mwlua/vfsbindings.cpp b/apps/openmw/mwlua/vfsbindings.cpp index 39b612acb8c..3186db26ca7 100644 --- a/apps/openmw/mwlua/vfsbindings.cpp +++ b/apps/openmw/mwlua/vfsbindings.cpp @@ -144,7 +144,11 @@ namespace MWLua values.push_back(sol::make_object(lua, msg)); } else - values.push_back(sol::make_object(lua, self.mFilePtr->tellg())); + { + // tellg returns std::streampos which is not required to be a numeric type. It is required to be + // convertible to std::streamoff which is a number + values.push_back(sol::make_object(lua, self.mFilePtr->tellg())); + } } catch (std::exception& e) { @@ -205,7 +209,7 @@ namespace MWLua }); }; - handle["close"] = [](lua_State* L, FileHandle& self) { + handle["close"] = [](sol::this_state lua, FileHandle& self) { sol::variadic_results values; try { @@ -214,16 +218,16 @@ namespace MWLua { auto msg = "Can not close file '" + self.mFileName + "': file handle is still opened."; values.push_back(sol::nil); - values.push_back(sol::make_object(L, msg)); + values.push_back(sol::make_object(lua, msg)); } else - values.push_back(sol::make_object(L, true)); + values.push_back(sol::make_object(lua, true)); } catch (std::exception& e) { auto msg = "Can not close file '" + self.mFileName + "': " + std::string(e.what()); values.push_back(sol::nil); - values.push_back(sol::make_object(L, msg)); + values.push_back(sol::make_object(lua, msg)); } return values; diff --git a/scripts/data/integration_tests/test_lua_api/test.lua b/scripts/data/integration_tests/test_lua_api/test.lua index fe4cd1d24f3..ac45a2cc11c 100644 --- a/scripts/data/integration_tests/test_lua_api/test.lua +++ b/scripts/data/integration_tests/test_lua_api/test.lua @@ -3,6 +3,7 @@ local core = require('openmw.core') local async = require('openmw.async') local util = require('openmw.util') local types = require('openmw.types') +local vfs = require('openmw.vfs') local world = require('openmw.world') local function testTimers() @@ -224,6 +225,42 @@ local function initPlayer() coroutine.yield() end +local function testVFS() + local file = 'test_vfs_dir/lines.txt' + testing.expectEqual(vfs.fileExists(file), true, 'lines.txt should exist') + testing.expectEqual(vfs.fileExists('test_vfs_dir/nosuchfile'), false, 'nosuchfile should not exist') + + local getLine = vfs.lines(file) + for _,v in pairs({ '1', '2', '', '4' }) do + testing.expectEqual(getLine(), v) + end + testing.expectEqual(getLine(), nil, 'All lines should have been read') + local ok = pcall(function() + vfs.lines('test_vfs_dir/nosuchfile') + end) + testing.expectEqual(ok, false, 'Should not be able to read lines from nonexistent file') + + local getPath = vfs.pathsWithPrefix('test_vfs_dir/') + testing.expectEqual(getPath(), file) + testing.expectEqual(getPath(), nil, 'All paths should have been read') + + local handle = vfs.open(file) + testing.expectEqual(vfs.type(handle), 'file', 'File should be open') + testing.expectEqual(handle.fileName, file) + + local n1, n2, _, l3, l4 = handle:read("*n", "*number", "*l", "*line", "*l") + testing.expectEqual(n1, 1) + testing.expectEqual(n2, 2) + testing.expectEqual(l3, '') + testing.expectEqual(l4, '4') + + testing.expectEqual(handle:seek('set', 0), 0, 'Reading should happen from the start of the file') + testing.expectEqual(handle:read("*a"), '1\n2\n\n4') + + testing.expectEqual(handle:close(), true, 'File should be closeable') + testing.expectEqual(vfs.type(handle), 'closed file', 'File should be closed') +end + tests = { {'timers', testTimers}, {'rotating player with controls.yawChange should change rotation', function() @@ -283,6 +320,7 @@ tests = { world.createObject('basic_dagger1h', 1):moveInto(player) testing.runLocalTest(player, 'playerWeaponAttack') end}, + {'vfs', testVFS}, } return { diff --git a/scripts/data/integration_tests/test_lua_api/test_vfs_dir/lines.txt b/scripts/data/integration_tests/test_lua_api/test_vfs_dir/lines.txt new file mode 100644 index 00000000000..ab3afc02378 --- /dev/null +++ b/scripts/data/integration_tests/test_lua_api/test_vfs_dir/lines.txt @@ -0,0 +1,4 @@ +1 +2 + +4 \ No newline at end of file