Advertisement
Guest User

Untitled

a guest
Jan 9th, 2014
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 6.78 KB | None | 0 0
  1. --- GitHub\Remote\KerbalMultiPlayer\KerbalMultiPlayer\KMPManager.cs 2014-01-09 04:06:45.051421200 -0500
  2. +++ github\KerbalMultiPlayer\KMPManager.cs  2014-01-09 03:07:00.848528300 -0500
  3. @@ -1644,6 +1644,108 @@
  4.                 }
  5.             }
  6.         }
  7. +
  8. +       /// <summary>
  9. +       /// Determines if a scenario module with the specified name exists already. Only checks the name.
  10. +       /// This is used to determine when the scenarios list was not cleared properly (creating a new game does not seem to clear the list - it only sets the modules'
  11. +       /// moduleRefs to null, which caused failure to sync on career servers, by creating another two modules and then attempting to write data into all four,
  12. +       /// and might have caused career mode on non-career servers if you weren't restarting KSP after exiting career servers).
  13. +       /// </summary>
  14. +       /// <param name="modName">The module name, such as "ResearchAndDevelopment"</param>
  15. +       /// <returns></returns>
  16. +       private bool HasModule(string modName)
  17. +       {
  18. +           Game g = HighLogic.CurrentGame;
  19. +           bool retval = false;
  20. +
  21. +           if (g == null)
  22. +           {
  23. +               Log.Warning("HasModule called when there is no current game.");
  24. +           }
  25. +           else if (g.scenarios.Count > 0)
  26. +           {
  27. +               for (int i = 0; i < g.scenarios.Count; i++)
  28. +               {
  29. +                   ProtoScenarioModule proto = g.scenarios[i];
  30. +                   if (proto != null)
  31. +                   {
  32. +                       if (proto.moduleName.CompareTo(modName) == 0)
  33. +                       {
  34. +                           retval = true;
  35. +                       }
  36. +                   }
  37. +                   else
  38. +                   {
  39. +                       Log.Warning("Null protoScenario found by HasModule!");
  40. +                   }
  41. +               }
  42. +           }
  43. +           return retval;
  44. +       }
  45. +      
  46. +       /// <summary>
  47. +       /// Log current game science, and log some debug information.
  48. +       /// Logs the science amount, or -1 if there is no current game, or no scenarios, or no R&D proto scenario module.
  49. +       /// This was used for debugging to figure out what and where things were going wrong with connecting after disconnecting (#578, #579).
  50. +       /// </summary>
  51. +       private void LogScience()
  52. +       {
  53. +           Game g = HighLogic.CurrentGame;
  54. +           if (g == null)
  55. +           {
  56. +               Log.Debug("No current game.");
  57. +               return;
  58. +           }
  59. +           Log.Debug("Game status=" + g.Status + " modes=" + g.Mode + " IsResumable=" + g.IsResumable() + " startScene=" + g.startScene+" NumScenarios="+g.scenarios.Count);
  60. +           float science = -1;
  61. +           if (g.scenarios.Count > 0)
  62. +           {
  63. +               for (int i = 0; i < g.scenarios.Count; i++)
  64. +               {
  65. +                   ProtoScenarioModule proto = g.scenarios[i];
  66. +                   if (proto != null)
  67. +                   {
  68. +                       Log.Debug("g.scenarios[" + i + "].moduleName=" + g.scenarios[i].moduleName + ", and moduleRef=" + (g.scenarios[i].moduleRef != null ? g.scenarios[i].moduleRef.ClassName : "null"));
  69. +                       if (proto.moduleRef != null && proto.moduleRef is ResearchAndDevelopment)
  70. +                       {
  71. +                           ResearchAndDevelopment rd = (ResearchAndDevelopment)proto.moduleRef;
  72. +                           if (science > -1)
  73. +                           {
  74. +                               //This was happening later on when there were four "scenarios" and the ones with null moduleRefs had somehow been replaced by actual moduleRefs.
  75. +                               //Upon trying to handle another science update when there were two valid R&D moduleRefs in scenarios,
  76. +                               //KSP/KMP exploded causing the desync which triggered the disconnect and message to restart KSP (the bug symptoms).
  77. +                               //The root cause, of course, was that scenarios was never cleared properly. That is fixed now.
  78. +                               Log.Error("More than one ResearchAndDevelopment scenario module in the game! Science was already " + science + ", now we've found another which says it is " + rd.Science + "!");
  79. +                           }
  80. +                           science = rd.Science;
  81. +                       }
  82. +                       else if (proto.moduleName.CompareTo("ResearchAndDevelopment") == 0)
  83. +                       {
  84. +                           //This was happening - after disconnecting from a career mode server, then making a new game to connect again,
  85. +                           //there would already be two "scenarios" - both with null moduleRefs -
  86. +                           //BEFORE we had tried to add two for career mode.
  87. +                           Log.Error("ProtoScenarioModule claims to be a ResearchAndDevelopment but contains no such thing! moduleRef is " + (proto.moduleRef != null ? proto.moduleRef.ClassName : "null"));
  88. +                       }
  89. +                   }
  90. +                   else
  91. +                   {
  92. +                       Log.Debug("Null protoScenario!");
  93. +                   }
  94. +               }
  95. +           }
  96. +           else
  97. +           {
  98. +               Log.Debug("No scenarios.");
  99. +           }
  100. +           if (science > -1)
  101. +           {
  102. +               Log.Debug("Science = " + science);
  103. +           }
  104. +           else if (g.scenarios.Count > 0)
  105. +           {
  106. +               Log.Debug("No ResearchAndDevelopment scenario modules.");
  107. +           }
  108. +       }
  109.        
  110.         private void handleScenarioUpdate(object obj)
  111.         {
  112. @@ -1664,7 +1766,7 @@
  113.                         try
  114.                         {
  115.                             proto.moduleRef.Load(update.getScenarioNode());
  116. -                       } catch { KMPClientMain.sendConnectionEndMessage("Error in handling scenario data. Please restart your client."); }
  117. +                       } catch (Exception e) { KMPClientMain.sendConnectionEndMessage("Error in handling scenario data. Please restart your client. "); Log.Debug(e.ToString());  }
  118.                         if (update.name == "ResearchAndDevelopment")
  119.                         {
  120.                             ResearchAndDevelopment rd = (ResearchAndDevelopment) proto.moduleRef;
  121. @@ -3517,7 +3619,7 @@
  122.  
  123.             if (forceQuit)
  124.             {
  125. -               Debug.Log("Force quit");
  126. +               Log.Debug("Force quit");
  127.                 forceQuit = false;
  128.                 gameRunning = false;
  129.                 if (HighLogic.LoadedScene != GameScenes.MAINMENU)
  130. @@ -4062,13 +4164,25 @@
  131.                     syncing = true;
  132.                    
  133.                     HighLogic.CurrentGame.Start();
  134. +                   //Determine if a R&D module already exists. If it does, erase scenarios.
  135. +                   if (HasModule("ResearchAndDevelopment"))
  136. +                   {
  137. +                       Log.Debug("Erasing scenario modules");
  138. +                       HighLogic.CurrentGame.scenarios.Clear();
  139. +                       //This is done because scenarios is not cleared properly even when a new game is started, and it was causing bugs in KMP.
  140. +                       //Instead of clearing scenarios, KSP appears to set the moduleRefs of each module to null, which is what was causing KMP bugs #578,
  141. +                       //and could be the cause of #579 (but closing KSP after disconnecting from a server, before connecting again, prevented it from happening,
  142. +                       //at least for #578).
  143. +                   }
  144.                    
  145.                     if (gameMode == 1)
  146.                     {
  147.                         var proto = HighLogic.CurrentGame.AddProtoScenarioModule(typeof(ResearchAndDevelopment), GameScenes.SPACECENTER, GameScenes.EDITOR, GameScenes.FLIGHT, GameScenes.TRACKSTATION, GameScenes.SPH);
  148. -                       proto.Load(ScenarioRunner.fetch);
  149. +                       proto.Load(ScenarioRunner.fetch);
  150. +                      
  151.                         proto = HighLogic.CurrentGame.AddProtoScenarioModule(typeof(ProgressTracking), GameScenes.SPACECENTER, GameScenes.FLIGHT, GameScenes.TRACKSTATION);
  152. -                       proto.Load(ScenarioRunner.fetch);
  153. +                       proto.Load(ScenarioRunner.fetch);
  154. +                      
  155.                         clearEditorPartList = true;
  156.                     }
  157.                    
  158. @@ -4244,7 +4358,7 @@
  159.                 GUILayout.EndVertical();
  160.             GUILayout.EndHorizontal();
  161.         }
  162. -      
  163. +
  164.         private void screenshotWindow(int windowID)
  165.         {
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement