Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"for line in openedfile:lines() do" fails in 2.x #995

Closed
mugg91 opened this issue Sep 2, 2017 · 5 comments
Closed

"for line in openedfile:lines() do" fails in 2.x #995

mugg91 opened this issue Sep 2, 2017 · 5 comments
Labels
re: Lua API/scripting Relating to EmuHawk's Lua API (not the Lua Console) Repro: Fixed/added in 2.9

Comments

@mugg91
Copy link

mugg91 commented Sep 2, 2017

I explained my issue here: http://tasvideos.org/forum/viewtopic.php?p=458352#458352

If you open a file with io.open() and then run for line in openedfile:lines() do on it, it will output an error. It does not happen in the 1.x versions.

@Tastyfish
Copy link
Contributor

Tastyfish commented Sep 2, 2017

Yeah, seems like a bug in either the version of NLua we're using, or whatever lua it's interfacing to (the project seems to contain lua51.dll and KopiLua?) and for that matter also LuaInterface.dll, despite nlua being a fork of that. I actually have no idea what we're using in general for lua, other than that in theory it's supposed to work as 5.1 but file i/o is broken as heck; f:read("*l") also seems to be returning results with newlines, which it shouldn't, and crashes on EOF instead of returning nil.

Here's a really nasty hackjob fix I guess...

function io.lines(filename)
	local f = io.open(filename, "r")

	local iterator = function(f, prevLine)
		local success, line = pcall(function()
			return f:read("*l"):gsub("\n",""):gsub("\r","")
		end)
		if not success or line == nil then
			f:close()
			line = nil
		end
		return line
	end
	
	return iterator, f, ""
end

function GuiLoadSettings()
  for line in io.lines("config.cfg") do
    print(line)
  end
end

GuiLoadSettings()

while true do
  print("hello world")
  emu.frameadvance()
 
end

@mugg91
Copy link
Author

mugg91 commented Sep 3, 2017

Thank you, this works nicely. As a FYI, it will crash if the cfg file is missing but I adjusted my code and it works now with and without cfg file.

function doLine(filename)
	local f = io.open(filename, "r")

	local iterator = function(f, prevLine)
		if f==nil then
			return nil
		else
			local success, line = pcall(function()
				return f:read("*l"):gsub("\n",""):gsub("\r","")
			end)
			if not success or line == nil then
				f:close()
				line = nil
			end

			return line
		end
	end

	return iterator, f, ""
end

@zeromus
Copy link
Contributor

zeromus commented Sep 4, 2017

config > customize > advanced > lua+luainterface
kopilua's file IO is hopefully screwed. we need to remove it.
but we need you to use lua+luainterface without file IO workarounds first a bunch to confirm that the crashes are fixed.

@vadosnaprimer
Copy link
Contributor

Don't remove kopilua just yet, @FelipeLopes might need it.

@YoshiRulz YoshiRulz added the re: Lua API/scripting Relating to EmuHawk's Lua API (not the Lua Console) label Jan 29, 2019
@CasualPokePlayer
Copy link
Member

KopiLua was removed in 45fbdb4, KeraLua works fine here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
re: Lua API/scripting Relating to EmuHawk's Lua API (not the Lua Console) Repro: Fixed/added in 2.9
Projects
None yet
Development

No branches or pull requests

6 participants