mamoniot / project-cybersyn

MIT License
43 stars 37 forks source link

Reproducible crash 1.2.15 - Switching no-signal combinator to depot mode #98

Open adlerd opened 1 year ago

adlerd commented 1 year ago

Error message:

Error while running event cybersyn::on_gui_selection_state_changed (ID 60)
__cybersyn__/scripts/gui.lua:95: attempt to index local 'signal' (a nil value)
stack traceback:
    __cybersyn__/scripts/gui.lua:95: in function 'handler'
    __flib__/gui-lite.lua:169: in function <__flib__/gui-lite.lua:152>

Repro steps: Place a train stop and a cybersyn combinator facing it. Open the GUI; it will be in station mode by default. Right-click the signal selection ("virtual-A" by default) to clear it. Then use the drop-down to switch the combinator to depot mode. The game will immediately crash to main menu with the above message.

NB. The reason this can come up is it's nice to clear out the signal for non-station combinators, so that there's not a spurious "A" on the combinator graphics in alt-mode (maybe plus the relevant game setting to make combinators show signals in alt-mode?).

adlerd commented 1 year ago

Suggested patch, not tested:

--- cybersyn/scripts/gui.lua
+++ cybersyn/scripts/gui.lua
@@ -91,8 +91,8 @@
        set_comb_operation(comb, MODE_DEPOT)
        --prevent the use of the each signal with depots
        local network = element.parent.parent.bottom.network--[[@as LuaGuiElement]]
        local signal = network.elem_value--[[@as SignalID]]
-       if signal.name == NETWORK_EACH then
+       if signal and (signal.name == NETWORK_EACH) then
            network.elem_value = nil
            set_comb_network_name(comb, nil)
        end
adlerd commented 1 year ago

Additional bug related to the same code (but probably not fixed with above patch):

Place train stop, combinator. Select "EACH" signal. Select "Depot" mode. Per the above code snippet, signal is cleared. Now open the dropdown again. The dropdown visually opens but then the game immediately crashes to menu with:

Error while running event cybersyn::on_gui_click (ID 1)
__cybersyn__/scripts/gui.lua:95: attempt to index local 'signal' (a nil value)
stack traceback:
    __cybersyn__/scripts/gui.lua:95: in function 'handler'
    __flib__/gui-lite.lua:169: in function <__flib__/gui-lite.lua:152>

This is perhaps worse because the user never cleared the signal, so maybe is less culpable in causing the crash ;)

download13 commented 1 year ago

Verified this fix works. I'll create a PR with the patch unless you want to @adlerd?