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

Understand why BetterEditorUndoRedo ended up screwing AttachedOnEditor indirectly #73

Closed
4 tasks done
Lisias opened this issue Feb 3, 2024 · 6 comments
Closed
4 tasks done
Assignees
Labels
task Not a problem, but something I must do and should not forget about
Milestone

Comments

@Lisias
Copy link
Contributor

Lisias commented Feb 3, 2024

Long history made short:

  • Install TweakScale/L 2.4.7.4 and KSPCF 1.34.0.0 with BetterEditorUndoRedo enabled
  • In the editor, place a central fuel tank
  • place another fuel tank attached in 4x symmetry to the side
  • pick up the part placed in symmetry
  • reattach it in a different location

See KSPModdingLibs/KSPCommunityFixes#191 and KSPCF Forum for the gory details.

This absolutely weird unfortunate interaction is already mitigated, but I'm very uncomfortable it had happened at all. due:

  • To the best of my knowledge, the KSPCF patch should not had caused something like that.
  • It mades clear that AttachedOnEditor is brittle.

The purpose of this issue is:

  • to try to figure out exactly WHY this is happening, i.e., why BetterEditorUndoRedo changed the runtime environment in a way that induced this misbehaviour
  • figure out why AttachedOnEditor had failed
  • propose a measure to make AttachedOnEditor more resilient with the least collateral damage (ideally none) possible.
@Lisias Lisias added the task Not a problem, but something I must do and should not forget about label Feb 3, 2024
@Lisias Lisias self-assigned this Feb 3, 2024
@Lisias
Copy link
Contributor Author

Lisias commented Feb 3, 2024

First things first.

I need to understand exactly what changes by having BetterEditorUndoRedo activated. So, yeah, MOAR TESTS.

The test session is:

  1. Firing KSP (with, and later, without KSPFC 1.34.0.0), and using a heavily instrumented debug compile for TweakScale 2.4.7.4
  2. Opening to a preexistent empty SandBox savegame
  3. Going to editor
  4. Taking a Kerbodyne S4-512 Fuel Tank as root part.
  5. Taking a Mk1 Liquid Fuel Fuselage and attaching it on the S4-512 with radial symmetry (I use 2 to keep the logging simpler)
  6. Detaching the Mk1 LF Fuselage and reataching it again in another place
  7. Noticing the misbehaviour (or its absence)
  8. Exiting Editor
  9. Quitting KSP.

This is the respective KSP logs from two Test Sessions (with and without KSPCF). Further analysis will follow.

@Lisias
Copy link
Contributor Author

Lisias commented Feb 4, 2024

This is the logs from the previous comment with only the "interesting bits" related to the problem at hands.

These excerpts start when you instantiate the S4-512 tank, and finishes when you exit Editor. So it logs the TweakScale (and AttachedOnEditor) points of view for the whole process.

Analysis of these logs reveals what I had already inferred: somehow, BetterEditorUndoRedo changed something on the expected Life Cycle of something inside Editor:

A DIFF between these two logs revealed (left is with BetterEditorUndoRedo enabled):

Screen Shot 2024-02-04 at 05 00 03

So, indeed, things are getting "out of order" on the process.

This doesn't necessarily means any wrongdoing by BetterEditorUndoRedo. It only means what should be obvious for some time: that mangling inside KSP's guts will change expected behaviour.

Such changing behaviours can lead to bugs due the change itself, or can reveal weakness on the affected code that could be had written in a more robust way. My current working theory is that AttachedOnEditor is on the later, because… well… KSPEvents should be handled as asynchronous message passing, not as synchronous procedure calls and apparently this is what I ended up doing.

I will cross check these findings with the ones on KSPModdingLibs/KSPCommunityFixes#191 just to satisfy my inherent stubbornness on identifying exactly where and how the problem is, but GotMachine's diagnosis appears to be right.

@Lisias
Copy link
Contributor Author

Lisias commented Apr 7, 2024

I choose to go grunt on the problem. I will send all the needed data on a BaseEventDetails data structure - this is going to run on Editor, not Flight. Performance is not an issue here.

@Lisias
Copy link
Contributor Author

Lisias commented Apr 8, 2024

Frak. It's not for any other reason than KSP is a pile of dog shit. Exceptions when fetching BaseEventDetails data are logged, but not raised to the caller.

My code just don't know when a Invalid Key exception happened.

Shit.

The following code

			Log.dbg("OnPartAttachmentNodesChanged for InstanceId {0:X}", instanceId);
			try
			{
				this.PreserveThisAttachmentNodes(data);
			}
			catch (Exception e)
			{
				Log.error(this, e);
				this.PreserveCurrentAttachmentNodes();
			}

Should be issuing a log like this:

[LOG 00:00:00.001] [KSP-Recall.AttachedOnEditor] TRACE: PreserveThisAttachmentNodes(BaseEventDetails) for <NO VESSEL>-miniFuselag
[ERR 00:00:00.002] [KSP-Recall.AttachedOnEditor] ERROR: Exception <yada yada yada>
[LOG 00:00:00.003] [KSP-Recall.AttachedOnEditor] TRACE: PreserveCurrentAttachmentNodes() for <NO VESSEL>-miniFuselag
[LOG 00:00:00.004] [KSP-Recall.Refunding.EditorHelper] TRACE: OnEditorShipModified Untitled Space Craft

But instead this is what I'm getting instead.

[LOG 21:38:10.475] [KSP-Recall.AttachedOnEditor] TRACE: PreserveThisAttachmentNodes(BaseEventDetails) for <NO VESSEL>-miniFuselag
[ERR 21:38:10.475] Invalid key: attachNodesCount

[LOG 21:38:10.477] [KSP-Recall.Refunding.EditorHelper] TRACE: OnEditorShipModified Untitled Space Craft

This is just maddening. You just can't trust anything on this code.

@Lisias
Copy link
Contributor Author

Lisias commented Apr 8, 2024

At least, UnityEngine.Vector3 is serializable to BaseEventDetails. Things would be pretty inconvenient if not.

Lisias added a commit that referenced this issue Apr 8, 2024
Lisias added a commit to TweakScale/TweakScale that referenced this issue Apr 8, 2024
@Lisias Lisias closed this as completed Apr 8, 2024
@Lisias
Copy link
Contributor Author

Lisias commented Apr 8, 2024

Implemented on commit: TweakScale/TweakScale@768fad9

=== == = EDIT

I mean... Commit 8fac9f1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
task Not a problem, but something I must do and should not forget about
Projects
None yet
Development

No branches or pull requests

1 participant