linuxgurugamer / ksp-advanced-flybywire

Controller mod for Kerbal Space Program
MIT License
26 stars 11 forks source link

Mod breaks and presets get wiped after scene change #19

Closed DoubleDimensional closed 3 years ago

DoubleDimensional commented 4 years ago

Stock KSP 1.8.1 +Breaking Ground and Making History, Fresh Install Windows 10, Steam Logitech F310 Controller (X-Input Mode)

Mods: AFBW v1.8.3, ClickThroughBlocker v0.1.9.3, ToolbarController v0.1.9.4

The mod appears to load fine. After launching a plane onto the runway, the toolbar button appears, and allows you to select and edit presets. Custom presets can be made and seem to work fine as well. There is an issue with KAL-1000 controllers; they don't respond to throttle inputs when their play position is bound to it in action groups. This may happen with other axes, but I didn't check.

However, after any scene change (even reverting to launch) the mod completely breaks. The key and axis bindings of all presets are gone, though they do keep their names and configuration settings. Keyboard inputs are completely blocked unless the mod setting "AFBW input overrides SAS and other control inputs" is off. The debug console is constantly flooded with "NullReferenceException: Object reference not set to an instance of an object". Even after restarting the mod is still broken. The only fix I could find is reinstalling the mod each time.

I couldn't get ksp to generate an output_log.txt, but it did give a Player.log. I'm not sure if it's the same thing, but I looked through it and there's definitely a lot of errors. Player.log

Thanks for bringing this mod back btw, the stock control editing is awful.

antimork commented 4 years ago

I have the same here, KSP 1.8.1, both DLC, fresh install (with a heap of mods admittedly).

I first thought this was a problem with my AFBW setup brought forward from my old KSP install, but that doesn't seem to be the case - I recreated everything and it worked for a while, but after a load it is the same again. I should add that it seems specifically to be presets that are having problems - I can still calibrate the joystick, just not bind it to anything.

GitHub doesn't seem to want me to post the log at the moment, but it seem to be the same exception that DoubleDimension's log contains. ` Edit: The problem goes away if I restart KSP after deleting the config (PluginData). Still returns on a load/revert though.

bhaan commented 4 years ago

after any scene change (even reverting to launch) the mod completely breaks. The key and axis bindings of all presets are gone

The debug console is constantly flooded with "NullReferenceException: Object reference not set to an instance of an object"

I'm also experiencing these two issues. After looking into them, I've identified that the two symptoms are related. The null ref exceptions are a bug that is produced as a side effect of the first issue.

The loss of preset bindings is due to a xml serialization failure. After configuring controller presets within a flight scene and closing the preset editor window, the presets are serialized and written to this file: <ksp_dir>/GameData/ksp-advanced-flybywire/PluginData/advanced_flybywire_config_v21.xml and the contents appear similar to the following:

<presets>
  <ControllerPreset>
    <name>New Preset</name>
    <serializableDiscreteActionMap>
      <KeyValuePairOfDiscreteActionBitset />
      <KeyValuePairOfDiscreteActionBitset />
      <KeyValuePairOfDiscreteActionBitset />
      <KeyValuePairOfDiscreteActionBitset />
    </serializableDiscreteActionMap>
    <serialiazableContinuousActionMap>
      <KeyValuePairOfContinuousActionKeyValuePairOfBitsetInt32 />
      <KeyValuePairOfContinuousActionKeyValuePairOfBitsetInt32 />
      <KeyValuePairOfContinuousActionKeyValuePairOfBitsetInt32 />
      <KeyValuePairOfContinuousActionKeyValuePairOfBitsetInt32 />
    </serialiazableContinuousActionMap>
    <serializableContinuousActionInvMap>
      <KeyValuePairOfContinuousActionBoolean />
      <KeyValuePairOfContinuousActionBoolean />
      <KeyValuePairOfContinuousActionBoolean />
      <KeyValuePairOfContinuousActionBoolean />
    </serializableContinuousActionInvMap>
  </ControllerPreset>
</presets>

The issue is that the KeyValuePair xml entries should contain data for the button -> action mappings (example)

Therefore, when the flight scene is reloaded, the mod deserializes "empty" KeyValuePairs which causes the presets to be erased. And the Null Reference Exceptions result from the code failing to safely check for Null values of those deserialized objects.

After a little bit of research, I was able to identify the C# package System.Xml.Serialization does not support System.Collections.Generic.KeyValuePair<> types, as suggested in this blog post and referenced here and here

I have only tried to use this mod with the 1.8.X version of KSP, but I'm assuming this bug was introduced with the new version and the corresponding Unity Engine upgrade, which included a .Net version upgrade (I believe) that likely changed the behavior of the Xml Serializer. Otherwise, I have no idea how this mod ever worked.

I have a fix for this that I implemented and tested locally. I'll try to submit a pull request this weekend.

linuxgurugamer commented 4 years ago

This was working fine in 1.7, thank you for doing the research. I haven't had time to look at this, so this will be very helpful. I'll wait for the PR