Jump to content

[Answered] Strange problem with onVesselSwitching


Recommended Posts

I'm having a very strange problem with onVesselSwitching event.

So, to start, here is a class I use for logging:

namespace EvaFuel
{
    public static class Log
    {
        public enum LEVEL
        {
            OFF = 0,
            ERROR = 1,
            WARNING = 2,
            INFO = 3,
            DETAIL = 4,
            TRACE = 5
        };
        static string PREFIX = "EVAFuel: ";

        [ConditionalAttribute("DEBUG")]
        public static void Info(String msg)
        {
            if (IsLogable(LEVEL.INFO))
            {
                UnityEngine.Debug.Log(PREFIX + msg);
            }
        }

 

This is the beginning of the class with the problems:

    [KSPAddon(KSPAddon.Startup.SpaceCentre, true)]
    public class EvaFuelManager : MonoBehaviour
    {
        private void Start()
        {
            Log.Info("Start");
            DontDestroyOnLoad(this);
        }
        public void Awake()
        {
            Log.Info("Awake");
           
            GameEvents.onVesselSwitching.Add(this.onVesselSwitching);
        }

 

And here is where I'm having problems:

      void mylog(string s)
        {
            // Set a variable to the My Documents path.
            string mydocpath = @"R:\tmplog.txt";


            System.IO.StreamWriter sw = System.IO.File.AppendText(mydocpath);
            try
            {
                string logLine = System.String.Format(
                    "{0:G}: {1}.", System.DateTime.Now, s);
                sw.WriteLine(logLine);
            }
            finally
            {
                sw.Close();
            }
        }

public string resourceName {  get { return HighLogic.CurrentGame.Parameters.CustomParams<EVAFuelSettings>().EvaPropellantName; } }
  
        private void onVesselSwitching(Vessel from, Vessel to)
        {
            mylog("onVesselSwitching");
            Log.Info("onVesselSwitching");

            if (to == null || from == null)
            {
                Log.Info("to or from are null");
                return;
            }
            mylog("onVesselSwitching 1");
            Log.Info("onVesselSwitching 1");
            if (lastPart == null)
                return;
            mylog("lastPart not null: " + lastPart.partInfo.title);
            Log.Info("lastPart not null: " + lastPart.partInfo.title);

            mylog("onVesselSwitching 2");
            Log.Info("onVesselSwitching 2");
            mylog("onVesselSwitching: from: " + from.Parts.First().partInfo.title + "   to: " + to.Parts.First().partInfo.title);
            Log.Info("onVesselSwitching: from: " + from.Parts.First().partInfo.title + "   to: " + to.Parts.First().partInfo.title);
            Log.Info("lastPart == data.to");

            KerbalEVA kEVA = to.Parts.First().FindModuleImplementing<KerbalEVA>();           

            if (to.parts == null || to.parts.Count == 0)
                return;
            if (from.parts == null || from.parts.Count == 0)
                return;

            PartResource kerbalResource = null;
            PartResource shipResource = null;

  			// When the following return is here (or the following two lines are commented out) no errors.  
            return;
            kerbalResource = to.parts.First().Resources.Where(p => p.resourceName == resourceName).First();
            shipResource = from.parts.First().Resources.Where(p => p.resourceName == resourceName).First();


        }

 

What is strange is that I'm getting an error if the last two lines are there, but no error if commented out.  The strange part is that NONE of those logging statements are executed when the error occurs, but when the lines are commented out, the logging statements work.  Additional strangeness is that if I have a return in front of those lines, no error happens.

Originally, those two lines were using:

to.Parts
from.Parts

I added that "mylog" function because I was thinking that there may have been a race condition, and that writing directly to a file would at least show it's being called.

I even tried adding a try/catch around those two lines, but didn't make any difference.

When I run with the two lines disabled, I see the following:

EVAFuel: onVesselSwitching
 
(Filename: C:/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 42)

EVAFuel: to or from are null
 

When not disabled, the error:


Exception handling event onVesselSwitching in class EvaFuelManager:System.TypeLoadException: Could not load type 'System.Func`2' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
  at EventData`2[Vessel,Vessel].Fire (.Vessel data0, .Vessel data1) [0x00000] in <filename unknown>:0 
 
(Filename: C:/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 42)

TypeLoadException: Could not load type 'System.Func`2' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
  at EventData`2[Vessel,Vessel].Fire (.Vessel data0, .Vessel data1) [0x00000] in <filename unknown>:0 
UnityEngine.DebugLogHandler:Internal_LogException(Exception, Object)
UnityEngine.DebugLogHandler:LogException(Exception, Object)
UnityEngine.Logger:LogException(Exception, Object)
UnityEngine.Debug:LogException(Exception)
EventData`2:Fire(Vessel, Vessel)
FlightGlobals:setActiveVessel(Vessel, Boolean)
FlightGlobals:SetActiveVessel(Vessel)
FlightDriver:Start()

 

Edited by linuxgurugamer
Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...