KSP-KOS / KOS

Fully programmable autopilot mod for KSP. Originally By Nivekk
Other
697 stars 230 forks source link

ProgramBuilder.ReplaceLabels Cannot Add Label #2760

Open bigmcr opened 4 years ago

bigmcr commented 4 years ago

On my latest installation of kOS, I've been running into an intermittent problem when I run scripts. Use a custom command line interface that I built. Using it, I run a variety of scripts. Approximately 75% of the time, I run into problems when I try to run a script. KSP freezes, and after about 15 seconds kOS gives me a "ProgramBuilder.ReplaceLabels" error. Environment: kOS version 1.2.1.0 KSP version 1.9.1.2788 Mods: 34 mods (not counting kOS) installed. Largest are OPM, Astronomer's Visual Enhancements and PlanetShine.

Screenshot below. kOs Cannot Add Label

nuggreat commented 4 years ago

We need the source code at the very least to even being trying to run down this problem.

bigmcr commented 4 years ago

https://github.com/bigmcr/kos-scripts has all of my code. I've had this problem most often when running the "exec" script, but the latest was from "circ". Note that I don't think the problem is with the various scripts I'm running, as they run just fine quite often. I think the problem is somewhere with the transition from my "loop" script to the various scripts that I'm calling manually.

nuggreat commented 4 years ago

Looking at your code I see nothing wrong at first glance. So the problem is likely internal to kOS if you could work out a reduced version of your script and the exact the steps for reproduction that would help speed up finding the problem.

Dunbaratu commented 4 years ago

Loop.ksm, line 412 seems to be trying to run a KSM file, like so:

IF (argList:LENGTH = 1) RUNPATH(inputStringList[0] + ".ksm", argList[0]).

The problem probably isn't caused by Loop.ksm line 412, but rather by something in whatever ksm file this RUNPATH ends up trying to run.

Do you already know what inputStringList[0] is supposed to be at the point where this error message comes out? (i.e. which KSM file should I be looking at.) I could take the time to trace through your code but it's probably faster to ask because you probably already know.

ProgramBuilder.ReplaceLabels() is part of what happens when program instructions from a compiled file get appended into memory at the end of the already existing instructions from other programs. It happens after compiling, but before running, so even though a KSM file is already compiled, ReplaceLabels() still has to be done on it just before it runs it.

One very quick thing to try - I assume you already tried it but I'll mention it just in case - is to delete that KSM file it's trying to run, and regenerate it from the KS source (re-run COMPILE), just to be 100% sure the KSM you are running is a recent new one that matches your current version of kOS. Some version updates in kOS end up invalidating older KSM files and requiring they be regenerated. If I could turn back time and redesign one thing about KSM it would be to have included a version number in the header so kOS could auto-detect if the version is out of date, but I didn't do that, alas.

bigmcr commented 4 years ago

I know what inputStringList[0] is in this specific instance, but not for all the times that I have had this issue. In the specific case of the screenshot I included above, inputStringList[0] was "circ", so you should look at the circularization script I made. However, I've had this error pop up most often on "exec", the maneuver node execution script. Note that the first non-PARAMETER line in both of those scripts is CLEARSCREEN, which didn't execute. That indicates that the called script didn't run at all, or at least not much. Attached are the KSM files of both circ and exec that were on the archive.

That section of code (line 396 through 436) is responsible for recognizing, compiling and running other scripts. Line 396 checks to see if the first argument passed is the name of an existing script on the current volume, wherever that is. 397 through 402 converts the typed commands from STRING format to a LIST containing the appropriate formatted data (BOOL, scalar, string, etc.). 403 through 407 prints all arguments on the terminal. In the screenshot I included, this is the "Argument apo has the value of apo and is of type String". 408 through 418 assumes that the loop is running locally. It therefore runs previously-compiled KSM files that had already been copied to the active CORE. It prints to the terminal the script name and number of arguments "Running circ with 1 arguments" in the screenshot. Line 411 logs the same information to a text file on the archive. Finally, it calls the script with the given number of arguments, up to 6 arguments. Line 418 through 433 is very similar, but it assumes the loop is running off the archive. It therefore compiles the script into a KSM file again (in the folder "KSM Files" on the archive), then runs the newly created KSM file with the appropriate number of arguments. Each step is reported on the terminal and printed to the text file on the archive.

This is a new installation and I was aware of potential cross-version issues with KSM files, so I made sure to delete all previous versions of KSM files. I'm confident that the issue isn't caused by non-compatible KSM files.

KSM Files.zip

bigmcr commented 4 years ago

Given your comment about ReplaceLabels, @Dunbaratu , I suppose I could resolve this by simply not compiling all scripts to KSM files. That's an ugly option, but it would work.