TASEmulators / BizHawk

BizHawk is a multi-system emulator written in C#. BizHawk provides nice features for casual gamers such as full screen, and joypad support in addition to full rerecording and debugging tools for all system cores.
http://tasvideos.org/BizHawk.html
Other
2.14k stars 380 forks source link

[🐧] Lua in Mono megathread #2951

Closed YoshiRulz closed 1 year ago

YoshiRulz commented 2 years ago

Note: 2.9 has a new Lua engine, NLua+Lua, as the default.


If you're using 2.8 and getting a crash immediately upon opening the Lua Console like this:

System.NullReferenceException: Object reference not set to an instance of an object
at BizHawk.Client.EmuHawk.LuaConsole.AskSaveChanges () [0x00006] in <9a8bc2924d9d4ee0895a11a1f5b9e023>:0 
...

...it's probably because the Lua engine defaults to "Lua+LuaInterface", which usually crashes (the environment required for it to not crash is unclear). Change it to "NLua+KopiLua" in Config > Customize... > Advanced, or just grab a dev build.

Dsm0 commented 2 years ago

On a fresh build of Bizhawk, upon opening the Lua editor, bizhawk crashes with terminal output

warning: File "/usr/bin/mono-sgen-gdb.py" auto-loading has been declined by your `auto-load safe-path' set to "$debugdir:$datadir/auto-load".
To enable execution of this file add
    add-auto-load-safe-path /usr/bin/mono-sgen-gdb.py
line to your configuration file "/home/{my username}/.gdbinit".
To completely disable this security protection add
    set auto-load safe-path /
line to your configuration file "/home/will/.gdbinit".
For more information about this security protection see the
"Auto-loading safe path" section in the GDB manual.  E.g., run from the shell:
    info "(gdb)Auto-loading safe path"
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/usr/lib/libthread_db.so.1".
0x00007f126e972a2f in wait4 () from /usr/lib/libc.so.6

and then a bunch of thread dumps.

I don't know if this is a configuration error on my part, but if it isn't, and you'd like more details, I can open a new issue.

YoshiRulz commented 2 years ago

The actual error message:

* Assertion: should not be reached at metadata.c:3041

Looks like this was fixed by mono/mono#19434, and all Mono releases from 6.12.0.151 are unaffected. Unfortunately that version isn't available in the Manjaro repos so I'll have to wait. I built from source (see below) and confirmed that the fix works.

I can't offer you a workaround beyond deleting your config every time you launch EmuHawk, as that seems to avoid the crash somehow. edit: New, more helpful workaround: change Lua engine to "NLua+KopiLua". From 2.9 (https://github.com/TASEmulators/BizHawk/commit/41de03e31c2df9ea92f9f05df33a7e86d31eca7f) this will be the default on Linux.

imsamuka commented 2 years ago

In #1430 was cited that the "ugliness" was kinda fixed in 62f90f1. I don't know how bad it was before, but i think it is still pretty bad:

printscreen

It is at a unusable point, texts inside the gradient in status bar, and also bright text in bright background are so hard, or impossible to read. I'm sorry if i missed some configuration step or if it's my computer messing things up, but i searched for a solution without success...

If it is something easily fixable, please tell me how. I am really glad someone is trying to make the software available in Linux, thank you even if it hasn't a solution yet.

YoshiRulz commented 2 years ago

Fixing that should be as simple as adding more overrides to LinuxColorTable like in the commit you linked. Hit me up on Discord if you want help with that.

magpie514 commented 2 years ago

About the crash on mono < 6.12.151, Is there any other workaround besides deleting the config every time? I tried building mono (6.12.0.158, the libgdiplus dependency on gtest is a problem, set -Wno-error=maybe-uninitialized in CXXFLAGS) and setting up a parallel environment for this, but the error persists, although instead of a plain crash it gives this (displayed in a dialog).


Using OpenTK 3 for host input (keyboard + gamepads)
Method '<Module>:_getFiberPtrId ()' in assembly '/home/magpie/Downloads/BizHawk-2.7-linux-x64/dll/lua51.dll' contains native code that cannot be executed by Mono on this platform. The assembly was probably created using C++/CLI.

System.TypeInitializationException: The type initializer for '<Module>' threw an exception. ---> <CrtImplementationDetails>.ModuleLoadException: The C++ module failed to load.
 ---> System.MissingMethodException: Method contains unsupported native code assembly:<unknown assembly> type:<unknown type> member:(null)
  at (wrapper managed-to-native) <Module>._getFiberPtrId()
  at <Module>.<CrtImplementationDetails>.LanguageSupport._Initialize (<CrtImplementationDetails>.LanguageSupport* ) [0x00024] in <01e64c95deda41b0ac7590707eabc864>:0 
  at <Module>.<CrtImplementationDetails>.LanguageSupport.Initialize (<CrtImplementationDetails>.LanguageSupport* ) [0x00028] in <01e64c95deda41b0ac7590707eabc864>:0 
   --- End of inner exception stack trace ---
  at <Module>.<CrtImplementationDetails>.ThrowModuleLoadException (System.String errorMessage, System.Exception innerException) [0x00007] in <01e64c95deda41b0ac7590707eabc864>:0 
  at <Module>.<CrtImplementationDetails>.LanguageSupport.Initialize (<CrtImplementationDetails>.LanguageSupport* ) [0x00041] in <01e64c95deda41b0ac7590707eabc864>:0 
  at <Module>..cctor () [0x00008] in <01e64c95deda41b0ac7590707eabc864>:0 
   --- End of inner exception stack trace ---
  at NLua.Lua.init () [0x00000] in <33fe4f6b9b274d9dab88987da2521403>:0 
  at NLua.Lua..ctor () [0x00027] in <33fe4f6b9b274d9dab88987da2521403>:0 
  at BizHawk.Client.EmuHawk.Win32LuaLibraries..ctor (BizHawk.Client.Common.LuaFileList scriptList, BizHawk.Client.Common.LuaFunctionList registeredFuncList, BizHawk.Emulation.Common.IEmulatorServiceProvider serviceProvider, BizHawk.Client.EmuHawk.MainForm mainForm, BizHawk.Client.Common.DisplayManagerBase displayManager, BizHawk.Client.Common.InputManager inputManager, BizHawk.Client.Common.Config config, BizHawk.Emulation.Common.IEmulator emulator, BizHawk.Emulation.Common.IGameInfo game) [0x00000] in <2e9a6324a4084dc4bc01031a9fd36595>:0 
  at BizHawk.Client.EmuHawk.LuaConsole.Restart () [0x0012f] in <2e9a6324a4084dc4bc01031a9fd36595>:0 
  at BizHawk.Client.EmuHawk.ToolManager.Load[T] (System.Boolean focus, System.String toolPath) [0x00173] in <2e9a6324a4084dc4bc01031a9fd36595>:0 
  at BizHawk.Client.EmuHawk.MainForm.OpenLuaConsole () [0x00000] in <2e9a6324a4084dc4bc01031a9fd36595>:0 
  at BizHawk.Client.EmuHawk.MainForm.LuaConsoleMenuItem_Click (System.Object sender, System.EventArgs e) [0x00000] in <2e9a6324a4084dc4bc01031a9fd36595>:0 
  at System.Windows.Forms.ToolStripItem.OnClick (System.EventArgs e) [0x00019] in <1110f0149ba64c4fb0c8e7333cb663a5>:0 
  at System.Windows.Forms.ToolStripMenuItem.OnClick (System.EventArgs e) [0x00090] in <1110f0149ba64c4fb0c8e7333cb663a5>:0 
  at System.Windows.Forms.ToolStripMenuItem.HandleClick (System.Int32 mouse_clicks, System.EventArgs e) [0x00000] in <1110f0149ba64c4fb0c8e7333cb663a5>:0 
  at System.Windows.Forms.ToolStripItem.FireEvent (System.EventArgs e, System.Windows.Forms.ToolStripItemEventType met) [0x00054] in <1110f0149ba64c4fb0c8e7333cb663a5>:0 
  at (wrapper remoting-invoke-with-check) System.Windows.Forms.ToolStripItem.FireEvent(System.EventArgs,System.Windows.Forms.ToolStripItemEventType)
  at System.Windows.Forms.ToolStrip.OnMouseUp (System.Windows.Forms.MouseEventArgs mea) [0x00048] in <1110f0149ba64c4fb0c8e7333cb663a5>:0 
  at System.Windows.Forms.ToolStripDropDown.OnMouseUp (System.Windows.Forms.MouseEventArgs mea) [0x00000] in <1110f0149ba64c4fb0c8e7333cb663a5>:0 
  at System.Windows.Forms.Control.WmLButtonUp (System.Windows.Forms.Message& m) [0x00078] in <1110f0149ba64c4fb0c8e7333cb663a5>:0 
  at System.Windows.Forms.Control.WndProc (System.Windows.Forms.Message& m) [0x001b4] in <1110f0149ba64c4fb0c8e7333cb663a5>:0 
  at System.Windows.Forms.ScrollableControl.WndProc (System.Windows.Forms.Message& m) [0x00000] in <1110f0149ba64c4fb0c8e7333cb663a5>:0 
  at System.Windows.Forms.ToolStrip.WndProc (System.Windows.Forms.Message& m) [0x00000] in <1110f0149ba64c4fb0c8e7333cb663a5>:0 
  at System.Windows.Forms.ToolStripDropDown.WndProc (System.Windows.Forms.Message& m) [0x00017] in <1110f0149ba64c4fb0c8e7333cb663a5>:0 
  at System.Windows.Forms.Control+ControlWindowTarget.OnMessage (System.Windows.Forms.Message& m) [0x00000] in <1110f0149ba64c4fb0c8e7333cb663a5>:0 
  at System.Windows.Forms.Control+ControlNativeWindow.WndProc (System.Windows.Forms.Message& m) [0x0000b] in <1110f0149ba64c4fb0c8e7333cb663a5>:0 
  at System.Windows.Forms.NativeWindow.WndProc (System.IntPtr hWnd, System.Windows.Forms.Msg msg, System.IntPtr wParam, System.IntPtr lParam) [0x00085] in <1110f0149ba64c4fb0c8e7333cb663a5>:0 
BizHawk has completed its shutdown routines, killing process...

Seems more informative than the backtraces I was getting with the system-wide version of Mono, but hm. What can be done? It's so close to working (and it does indeed work when deleting config.ini) but only runs once before having to reconfigure all the emulator and keys, so it's not very useful, sadly. Do I need to build bizhawk with this Mono install for it to work? I don't mind that on principle, but it's going to be a giant amount of development packages just for one thing and that bothers me at a fundamental level (unless it's sure to work, then it's okay)

YoshiRulz commented 2 years ago

If you'd care to join me down the rabbit-hole, I've added Nix build scripts to the repo for both EmuHawk and the required version of Mono. That build is definitely immune from the crash. Unfortunately, I had to uninstall the older Mono and have broken my host OS such that I can't reinstall it, so I can't help look for another workaround.

magpie514 commented 2 years ago

Nix, huh? I've been meaning to look into it, but won't have time until the weekend. That said, I'll give it a spin then, maybe I can set up a builder VM.

bigbass1997 commented 2 years ago

If you'd care to join me down the rabbit-hole, I've added Nix build scripts to the repo for both EmuHawk and the required version of Mono. That build is definitely immune from the crash.

The necessary mono versions (6.12.0.151+) aren't available yet in the ubuntu package repos either. Never used nix before now, but I took a stab at it.

After installing nix, I cloned the bizhawk repo, and ran nix-build in the repo's root directory. However after running for awhile it failed the build. Here's the full output from nix, wasn't sure which parts were relevant to bizhawk/mono, so figured I'd include everything. Every following build attempt shows this.

YoshiRulz commented 2 years ago

Hmm I thought it might be unstable because it includes /.git, but it's the same after I pushed a commit... guess that was just a typo on my part. You could also build the cloned copy of the source with --arg useCWDAsSource true which obviously will bypass the hash check.

bigbass1997 commented 2 years ago

That seemed to get a little farther, but now there's another issue. CLI output here. And the full log mentioned in the error.

YoshiRulz commented 2 years ago

That's the same problem GitLab CI had on fresh clones, which I fixed by building BizHawk.Version first. I could add that to the Nix expression, or you could do it manually (in nix-shell to use the same SDK). But this is straying further off-topic, so if we could move to Discord that'd be appreciated.

edit: fixed in https://github.com/TASEmulators/BizHawk/commit/9448e56f0a8770c7631ff5be1c71246bb9c7a8c8

magpie514 commented 2 years ago

That's the same problem GitLab CI had on fresh clones, which I fixed by building BizHawk.Version first. I could add that to the Nix expression, or you could do it manually (in nix-shell to use the same SDK). But this is straying further off-topic, so if we could move to Discord that'd be appreciated.

Please post the results of that conversation if something comes up, I'm interested in setting up in a couple days.

magpie514 commented 2 years ago

I succeeded in building it. Wow, Nix is really interesting, now I really want to learn more about it. image You can see it opens fine when a configuration is already existing in there. Thank you very much, YoshiRulz, this was a very functional and educative solution. How does this work when updating, though? Do I just run the same commands after a git pull?

YoshiRulz commented 2 years ago

By the config.json I assume you copied it out of the Nix store or something? I actually got it working in the store, just run emuhawk-monort-2.7. It should be available in $PATH if you run nix-env -f default.nix -iA emuhawk from the repo (or do it declaratively—I haven't looked into that yet). As for updating, pass --arg useCWDAsSource true to nix-build/nix-env to make a dev build from the checked-out commit. By default it will grab the 2.7 source tarball instead. The dev build can be "installed" to your profile alongside 2.7, but only one dev build at a time, as they are all version 2.7.1-local.

magpie514 commented 2 years ago

Oh, I meant to say it ran (and exited) once, generated a config and then ran again fine, meaning the crash doesn't happen anymore with this method. My bad. This builds works perfectly as far as my basic tests of the Lua interface go. Thanks for the updating tips, I'll stick to this method as things go forward, until it can run with standard distro packages.

TauAkiou commented 2 years ago

I've been trying to get this working with no success for a few days now.

I've tried on two seperate systems, one running Fedora and one running Arch.

System mono provides similar results to @magpie514, with the same mono regression (crash on subsequent runs of the Lua Console)

Building mono from source (from latest master) exhibits similar behavior, though it creates an unhandled exception instead: Mono exception output

Running it through Nix has been unsuccessful on both Arch and Fedora, which seem to be attached to Nix: LibGL/driver failures on Fedora, and build failures on Arch. I don't feel like fighting with Nix, at least not right now.

Please let me know if any additional information is necessary.

YoshiRulz commented 2 years ago

I have a patch for debugging that NRE as another user had reported it (probably want to manually copy diff): https://gitlab.com/TASVideos/BizHawk/-/commit/8798cbcc7dc99b2270730fd4ff530380a268451d IIRC it will only ever print "... is null" in the case of a use-after-free, which the runtime shouldn't be doing in safe managed code.

I'm very interested in debugging Nix failures though. You can DM me the logs at YoshiRulz#4472 on Discord or wherever.

TauAkiou commented 2 years ago

it turns out that this is the same error that @magpie514 has, pretty much exactly.

This still happens on the latest git release & the latest nightly build from the official repos which is mono 6.13.0 and by most accounts should be a later version then the version of mono with this bug.

Even using the Preview build, which is 6.12.0.158 exhibits the same issue.

magpie514 commented 2 years ago

@TauAkiou I solved that with the Nix method, but I honestly can't tell why it's failing for you. For whatever it's worth I did the process on Ubuntu and using the nVidia GL setup. I only ran into a minor issue, I had to use --arg useCWDAsSource true on the cloned repo for it to build right. Can you try that on Arch since that's the one that failed to build?

TauAkiou commented 2 years ago

The issue with Nix on Arch is related to it not finding autoconf or libtool in the Nix environment, which more refers to a problem with setup on Arch or, more likely, me not knowing how Nix works particularly well and not setting it up properly.

What's more confusing, though is that it works perfectly fine on Fedora, but Fedora doesn't have OpenGL configured properly, and that's a separate issue.

I'm more confused as to why BizHawk is exhibiting the same problem despite being run on versions that shouldn't be vulnerable to the bug. My guess is that somehow, the metadata fixes either aren't working or aren't applied in these builds for some bizarre reason.

YoshiRulz commented 2 years ago

On Arch, are you using nix-build --pure? Is Nix up-to-date?

On Fedora, pull (to https://github.com/TASEmulators/BizHawk/commit/54e7bf25425bdf1da308c92eddd61f0b70778ae3) and try again. I borrowed a Fedora machine and added the missing paths to the wrapper script.

edit: btw we resolved this (mostly)

raphaelr commented 2 years ago

What is the intended value for the setting "Config -> Customize -> Advanced > Lua Core"? For me, "Lua + LuaInterface" was preselected, and it crashes. "NLua + KopiLua" doesn't crash.

vadosnaprimer commented 2 years ago

LuaInterface uses actual C Lua, while NLua reimplements it in C#.

YoshiRulz commented 2 years ago

The intended value is the default of Lua + LuaInterface. I didn't change it for Mono because it doesn't crash on my machine, though I've now had two reports of crashes. https://github.com/TASEmulators/BizHawk/blob/b20fdb8e8580780b87964be46ff11dac878bef14/src/BizHawk.Client.Common/config/Config.cs#L144

N00byKing commented 2 years ago

I've had issues with a script using luasockets (Specifically, it fails to find the library). Is that a known issue? Script in Question is here.

Still, thanks for getting this far already :D

ShneekeyTheLost commented 2 years ago

Apparently, BizHawk is having difficulty finding package.path and package.cpath on Debian distros, at least for lua-socket.

BizHawk's LUA console looks for it at usr/local/share/lua and usr/local/lib/lua however Ubuntu and similar distros put it in usr/share/lua and usr/lib/x86_64-linux-gnu/lua respectively.

This results, unsurprisingly, in failing to find the library (probably previous user's issue as well). I tried to include package.path and package.cpath directions as a quick workaround before calling , but unsurprisingly dynamic libraries are a no-go.

Other than that, since Ubuntu's repositories finally uploaded to use MONO >151, everything else seems to be working.

YoshiRulz commented 2 years ago

Are you able to use EmuHawk's socket implementation comm.socket* instead?

ShneekeyTheLost commented 2 years ago

Are you able to use EmuHawk's socket implementation comm.socket* instead?

How would I go about doing that?

YoshiRulz commented 2 years ago

Honestly not sure. I think the relevant functions are comm.socketServerSetIp, comm.socketServerIsConnected, comm.socketServerSetTimeout, and comm.socketServerResponse.

thechucklingatom commented 2 years ago

Hello, I am running into an issue when adding the ironmon tracking script. The error I get is Ironmon-Tracker (Gen 3): v0.6.2c NLua.Exceptions.LuaScriptException: [string "main"]:81: 'popen' not supported I am running an Arch linux build and running bizhawk from source through Rider. I have also run it through the provided EmuHawkMono.sh script. I have verified that lua exists and runs on the system. The io.popen("cd"):read() command runs through the lua field. I would be happy to do more research or even debug through Rider, but I am not sure where exactly to start.

YoshiRulz commented 2 years ago

The pacman-installed Lua is not used. Not sure why that function isn't supported, but a web search brings up os.execute as an alternative.

thechucklingatom commented 2 years ago

I'll look into that alternative. I didn't realize it doesn't use the system installation of lua. Maybe I'll poke around a bit in the lua it is packaged with and see what the difference might be.

phoneticsushi commented 1 year ago

Hi, I'm dusting off an old project that worked on BizHawk 2.6.1 for Windows but does not work on a recent Linux dev build (afccbe8a) - I've tracked it down to the comm.socketServerResponse() function.

From what I can glean from using this on the Windows 2.6.1 version,

That said, #2671 suggests that this function should ALWAYS block, so ¯\_(ツ)_/¯ what the intended functionality is or if it's changed in more recent versions.

FWIW, this has never been particularly clear, so if I'm doing something wrong here please correct me - this documentation says only "receives a message from the Socket server" with no mention if the "message" needs to be text, terminated somehow, follow a predefined protocol, etc.

Test script:

console.log(comm.socketServerResponse())

Expected behavior, given the above:

Actual behavior:

When the server sends a space character (not NULL, not newline, but space),

YoshiRulz commented 1 year ago

Since https://github.com/TASEmulators/BizHawk/commit/b1602dae06c6f7446f2d9ca3e6b81860638aaca5, which was before 2.6.2, EmuHawk does expect a certain format when receiving messages. The documentation was recently updated to reflect this: https://github.com/TASEmulators/BizHawk/commit/de1e7eef692e87dd3527ba7be44e4dab58f7138c. Please only use this Issue for differences in behaviour between Linux and Windows. edit: of the same code

phoneticsushi commented 1 year ago

Thanks - as of writing, that documentation isn't live and I was completely unaware of that change. Can confirm this works as described on Linux.

Thanks for the incredible work you put into this emulator!

mhdask commented 1 year ago

When trying to open LUA console i get following error output:

System.NullReferenceException: Object reference not set to an instance of an object
at BizHawk.Client.EmuHawk.LuaConsole.AskSaveChanges () [0x00006] in <9a8bc2924d9d4ee0895a11a1f5b9e023>:0 
at BizHawk.Client.EmuHawk.ToolManager+<>c.<AskSave>b__36_0 (BizHawk.Client.Common.IToolForm tool) [0x00000] in <9a8bc2924d9d4ee0895a11a1f5b9e023>:0 
at System.Linq.Enumerable+SelectListIterator`2[TSource,TResult].MoveNext () [0x00048] in <f3edbec5ed4044bd9bdecb89325d2a90>:0 
at System.Linq.Enumerable.All[TSource] (System.Collections.Generic.IEnumerable`1[T] source, System.Func`2[T,TResult] predicate) [0x00039] in <f3edbec5ed4044bd9bdecb89325d2a90>:0 
at BizHawk.Client.EmuHawk.ToolManager.AskSave () [0x00039] in <9a8bc2924d9d4ee0895a11a1f5b9e023>:0 
at BizHawk.Client.EmuHawk.MainForm.<.ctor>b__4_7 (System.Object o, System.ComponentModel.CancelEventArgs e) [0x00000] in <9a8bc2924d9d4ee0895a11a1f5b9e023>:0 
at System.Windows.Forms.Form.OnClosing (System.ComponentModel.CancelEventArgs e) [0x00019] in <9743d3af65374913941eb486c4f386f9>:0 
at System.Windows.Forms.Form.FireClosingEvents (System.Windows.Forms.CloseReason reason, System.Boolean cancel) [0x00007] in <9743d3af65374913941eb486c4f386f9>:0 
at (wrapper remoting-invoke-with-check) System.Windows.Forms.Form.FireClosingEvents(System.Windows.Forms.CloseReason,bool)
at System.Windows.Forms.Application.Exit (System.ComponentModel.CancelEventArgs e) [0x0002e] in <9743d3af65374913941eb486c4f386f9>:0 
at System.Windows.Forms.Application.Exit () [0x00005] in <9743d3af65374913941eb486c4f386f9>:0 
at System.Windows.Forms.Application.OnThreadException (System.Exception t) [0x0005e] in <9743d3af65374913941eb486c4f386f9>:0 
at System.Windows.Forms.NativeWindow.OnThreadException (System.Exception e) [0x00000] in <9743d3af65374913941eb486c4f386f9>:0 
at System.Windows.Forms.NativeWindow.WndProc (System.IntPtr hWnd, System.Windows.Forms.Msg msg, System.IntPtr wParam, System.IntPtr lParam) [0x00196] in <9743d3af65374913941eb486c4f386f9>:0 
at System.Windows.Forms.XplatUIX11.DispatchMessage (System.Windows.Forms.MSG& msg) [0x00000] in <9743d3af65374913941eb486c4f386f9>:0 
at System.Windows.Forms.XplatUIX11.DoEvents () [0x00071] in <9743d3af65374913941eb486c4f386f9>:0 
at System.Windows.Forms.XplatUI.DoEvents () [0x00000] in <9743d3af65374913941eb486c4f386f9>:0 
at System.Windows.Forms.Application.DoEvents () [0x00000] in <9743d3af65374913941eb486c4f386f9>:0 
at BizHawk.Client.EmuHawk.MainForm.CheckMessages () [0x00000] in <9a8bc2924d9d4ee0895a11a1f5b9e023>:0 
at BizHawk.Client.EmuHawk.MainForm.ProgramRunLoop () [0x001c3] in <9a8bc2924d9d4ee0895a11a1f5b9e023>:0 
at (wrapper remoting-invoke-with-check) BizHawk.Client.EmuHawk.MainForm.ProgramRunLoop()
at BizHawk.Client.EmuHawk.Program.SubMain (System.String[] args) [0x002cb] in <9a8bc2924d9d4ee0895a11a1f5b9e023>:0 

Anyone seen this before, and know what is going wrong? I can't seem to figure it out

YoshiRulz commented 1 year ago

Which Lua engine is selected? In 2.9 dev builds, the default was changed to "NLua+KopiLua", as "Lua+LuaInterface" tended to crash in exactly that way.

mhdask commented 1 year ago

Which Lua engine is selected? In 2.9 dev builds, the default was changed to "NLua+KopiLua", as "Lua+LuaInterface" tended to crash in exactly that way.

Thank you for the reply, I dont actually know what lua engine i have selected, how do i check, and where do i set other ones?

YoshiRulz commented 1 year ago

Config > Customize... > Advanced, or just grab a dev build.

mhdask commented 1 year ago

Config > Customize... > Advanced, or just grab a dev build.

That seemed to do the trick, thank you!

Zixiken commented 1 year ago

I'm new to bizhawk, and pretty unfamiliar with lua in general. I've been messing with the co-op lua script, and I've managed to get to the point of it trying to load luasocket which fails with this error:

NLua.Exceptions.LuaScriptException: error loading module 'socket.core' from file '/usr/lib64/lua/5.4/socket/core.so': /usr/lib64/lua/5.4/socket/core.so: undefined symbol: lua_gettop

Google seems to be suggesting this has something to do with compile flags bizhawk was compiled with, but I'm not sure. I do know I can use luasocket through the lua interpreter. I'm on gentoo, with mono-6.12.0.122.

CasualPokePlayer commented 1 year ago

On Linux, the Lua used will be the system lua (since 339915c013829bf1246f6f5d64d7b89fe9735733 / 2.9 rc3), so it would be the fault of the package manager here if the symbol was truly missing.

However, we do use that function internally, so if it was missing it would crash sooner. Perhaps there's something weird with that .so where it can't properly find the lua used?

EDIT: Searching through the internet seems to also point to possibly lua sockets being compiled with flags which give it a static linking requirement for liblua due to not actually explicitly linking against liblua, which would not work for us (we have to dynamically link), so that might also be an issue.

Zixiken commented 1 year ago

Hmm, looking at the files belonging to the lua package I'm only seeing a .so library, no .a. So I'm not sure it's possible that sockets statically linked? Regardless, I'm willing to try adding whatever link flags are necessary to ensure it compiles correctly, just not sure what those are.

I've been messing with strace, and I can see bizhawk opens and loads dlls/NLua.dll to memory, twice. Then, when I open the lua console, it loads /usr/lib64/liblua5.4.so to memory. Can you explain what part NLua.dll is playing here?

Continuing with strace, if I tell bizhawk to load the co-op script, it reads in related .lua files and eventually comes to /usr/lib64/lua/5.4/socket/core.so, which it maps to memory and then immediately unmaps and writes out the error. If I load the co-op script just with the interpreter on the command line, I see /usr/lib64/liblua5.4.so loaded to memory, the co-op .lua files are read, and /usr/lib64/lua/5.4/socket/core.so is mapped in the same fashion as bizhawk did. But then it continues on loading from that point until hitting a predictable error relating to missing bizhawk lua API. So it certainly seems that the system .so files know how to work together, and bizhawk does load them, but I guess it must be doing something different.

YoshiRulz commented 1 year ago

Is /usr/lib64/lua in LD_LIBRARY_PATH? Run mono EmuHawk.exe instead of using the launch script.

Zixiken commented 1 year ago

Is /usr/lib64/lua in LD_LIBRARY_PATH? Run mono EmuHawk.exe instead of using the launch script.

No change when run that way, and LD_LIBRARY_PATH is unset in my shell. But I did have to modify line 11 of EmuHawkMono.sh to make it gentoo-aware, bizhawk wouldn't start otherwise. Before: "fedora") libpath="/usr/lib64";;

After: "fedora"|"gentoo") libpath="/usr/lib64";;

Zixiken commented 1 year ago

Scratch that, it would start but crash when running a game.

CasualPokePlayer commented 1 year ago

Regardless, I'm willing to try adding whatever link flags are necessary to ensure it compiles correctly, just not sure what those are.

Add in -llua5.4 to the linker flags.

Zixiken commented 1 year ago

Add in -llua5.4 to the linker flags.

Alright, now I'm getting somewhere. I confirmed that link flag was missing by default when the libraries are linked, and confirmed that after adding the flag the output of ldd shows they now link to my system's /usr/lib64/liblua5.4.so.0. Running the co-op script in bizhawk now gets further, the error regarding luasocket is gone, and I had to adjust some of the script that was written with windows in mind. But I have a different error now, with the game script I'm trying to load in the co-op script. More specifically this is the offending line and error generated:

local game_code = string_from_byterange(memory.readbyterange(0x134, 9))

Error: NullHawk does not implement memory domains
NLua.Exceptions.LuaScriptException: A .NET exception occured in user-code
System.NotImplementedException: Error: NullHawk does not implement memory domains
  at BizHawk.Client.Common.MemoryApi.<get_Domain>g__LazyInit|12_0 () [0x00034] in <333d6f50a3904bd9a49c43cc2ba29b6b>:0 
  at BizHawk.Client.Common.MemoryApi.get_Domain () [0x00008] in <333d6f50a3904bd9a49c43cc2ba29b6b>:0 
  at BizHawk.Client.Common.MemoryApi.NamedDomainOrCurrent (System.String name) [0x0003c] in <333d6f50a3904bd9a49c43cc2ba29b6b>:0 
  at BizHawk.Client.Common.MemoryApi.ReadByteRange (System.Int64 addr, System.Int32 length, System.String domain) [0x00000] in <333d6f50a3904bd9a49c43cc2ba29b6b>:0 
  at BizHawk.Client.Common.MemoryLuaLibrary.ReadByteRange (System.Int64 addr, System.Int32 length, System.String domain) [0x00011] in <333d6f50a3904bd9a49c43cc2ba29b6b>:0 
  at (wrapper managed-to-native) System.Reflection.RuntimeMethodInfo.InternalInvoke(System.Reflection.RuntimeMethodInfo,object,object[],System.Exception&)
  at System.Reflection.RuntimeMethodInfo.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x0006a] in <a6a5ba8fc13a4797a32a4dc4ae25c772>:0

I'm not sure how to interpret this, is there any chance this will be implemented any time soon? Is there a better/other way the game script could be doing what it's trying to do? I do see that memory.readbyterange is deprecated, but I'm not sure if that's related.

CasualPokePlayer commented 1 year ago

"NullHawk" is the dummy "core" when no game is loaded. Load a game :P

YoshiRulz commented 1 year ago

NullHawk is the core used when no rom is loaded. sniped

I'm sure most of your problems would go away if you (or the script's author) used our provided socket library.