/// <summary> /// Adds in the vessels from the new seed and saves the new seed to file. /// </summary> /// <param name="oldSeed">The seed we are leaving.</param> /// <param name="newSeed">The seed we are going to.</param> public static void AddPersistenceVessels(string oldSeed, string newSeed) { if (newSeed == AstroUtils.KERBIN_SYSTEM_COORDS && !WarpDrivers.WarpDrive.needsPurge) { // Don't need to merge, we never left Kerbol return; } string persistence = FindPersistenceFile(); if (string.IsNullOrEmpty(persistence)) { Debugger.LogError("Could not find persistence file!"); return; } // Check to see if we already have a persistence file for this system if (SystemPersistenceExists(persistence, newSeed)) { // Load the game // We don't actually have to load the WHOLE game, just the vessels Debugger.Log("Loading existing system at " + newSeed); string path = Path.Combine(KSPUtil.ApplicationRootPath, "saves"); path = Path.Combine(path, HighLogic.SaveFolder); path = Path.Combine(path, AstroUtils.STAR_SYSTEM_FOLDER_NAME); path = Path.Combine(path, newSeed + AstroUtils.SEED_PERSISTENCE + AstroUtils.SFS); // Generate root node from persistence file ConfigNode root = ConfigNode.Load(path).GetNode("GAME"); if (root == null) { throw new PlanetRandomizerException("Could not load save file because the root node could not be found."); } // Find FLIGHTSTATE node in the root node ConfigNode flightStateNode = root.GetNode("FLIGHTSTATE"); // Generate new FlightState from the root FlightState flightState = new FlightState(flightStateNode, HighLogic.CurrentGame); // Load each ProtoVessel in the FlightState List <ProtoVessel> spawnedVessels = new List <ProtoVessel> (); List <Vessel> activeVessels = new List <Vessel> (); Debugger.Log(flightState.protoVessels.Count + " vessels in " + newSeed + "."); foreach (ProtoVessel proto in flightState.protoVessels) { Vessel vessel = VesselManager.LoadVessel(newSeed, proto); if (vessel == null) { Debugger.LogError(proto.vesselName + " was not spawned!"); } else { Debugger.Log(vessel.vesselName + " is now in seed " + newSeed + "."); spawnedVessels.Add(proto); activeVessels.Add(vessel); } } flightState.protoVessels = spawnedVessels; HighLogic.CurrentGame.flightState = flightState; FlightGlobals.Vessels.Clear(); FlightGlobals.Vessels.AddRange(activeVessels); if (newSeed == AstroUtils.KERBIN_SYSTEM_COORDS) { // We've now purged the system of all the "old" data WarpDrivers.WarpDrive.needsPurge = false; } } else { // Create a blank FlightState for the new system HighLogic.CurrentGame.flightState = new FlightState(); Debugger.LogWarning("Created a blank star system for seed " + newSeed); } Debugger.Log("All vessels have been merged."); SavePersistence(); }