Skip to content

Commit

Permalink
Fixed saving of profiles (not loading)
Browse files Browse the repository at this point in the history
 Some optimizations by reducing duplicate calls
 Following due to changes in KSP
  Changed the OnLoad in the ScienceAlertProfileManager to calling a CoRoutine so it can wait until the scene is ready
  Changed the OnVesselChange in the ScienceAlertProfileManager to calling a CoRoutine so it can wait until the OnLoad is complete
  • Loading branch information
linuxgurugamer committed Jan 1, 2019
1 parent ffdc162 commit ae53dc5
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 10 deletions.
7 changes: 7 additions & 0 deletions ChangeLog.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
1.9.5.2
Fixed saving of profiles (not loading)
Some optimizations by reducing duplicate calls
Following due to changes in KSP
Changed the OnLoad in the ScienceAlertProfileManager to calling a CoRoutine so it can wait until the scene is ready
Changed the OnVesselChange in the ScienceAlertProfileManager to calling a CoRoutine so it can wait until the OnLoad is complete

1.9.5.1
Fixed issue where tourists could EVA
Updated .version file
Expand Down
2 changes: 1 addition & 1 deletion GameData/ScienceAlert/ScienceAlert.version
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"MAJOR": 1,
"MINOR": 9,
"PATCH": 5,
"BUILD": 1
"BUILD": 2
},
"KSP_VERSION": {
"MAJOR": 1,
Expand Down
2 changes: 1 addition & 1 deletion ScienceAlert.version
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"MAJOR": 1,
"MINOR": 9,
"PATCH": 5,
"BUILD": 1
"BUILD": 2
},
"KSP_VERSION": {
"MAJOR": 1,
Expand Down
2 changes: 1 addition & 1 deletion Source/AssemblyVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@

using System.Reflection;

[assembly: AssemblyVersion("1.9.5.1")]
[assembly: AssemblyVersion("1.9.5.2")]
5 changes: 4 additions & 1 deletion Source/ScienceAlert.ProfileData/Profile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ public void OnSave(ConfigNode node)
ConfigNode.CreateConfigFromObject(this, 0, node);
foreach (KeyValuePair<string, ExperimentSettings> current in settings)
{
current.Value.OnSave(node.AddNode(new ConfigNode(current.Key)));
ConfigNode newNode = new ConfigNode(current.Key);
node.AddNode(newNode);
current.Value.OnSave(newNode);
}
Log.Debug("ALERT:Profile: OnSave config: {0}", node.ToString());
}
Expand Down Expand Up @@ -153,6 +155,7 @@ public Profile Clone()

public static Profile MakeDefault()
{
Log.Info("MakeDefault");
return new Profile("default");
}

Expand Down
45 changes: 39 additions & 6 deletions Source/ScienceAlert.ProfileData/ScienceAlertProfileManager.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using ReeperCommon;
using UnityEngine;

namespace ScienceAlert.ProfileData
{
Expand Down Expand Up @@ -124,8 +126,15 @@ private void SaveStoredProfiles()
private void OnVesselChange(Vessel vessel)
{
if (vessel == null) return;
if (!vesselProfiles.ContainsKey(vessel.id)) return;
if (vesselProfiles[vessel.id].modified) return;
StartCoroutine(WaitForLoad(vessel));
}
IEnumerator WaitForLoad(Vessel vessel)
{
while (!Ready)
yield return new WaitForSeconds(0.1f);

if (!vesselProfiles.ContainsKey(vessel.id)) yield break;
if (vesselProfiles[vessel.id].modified) yield break;

var stored = FindStoredProfile(vesselProfiles[vessel.id].name);
if (stored == null)
Expand Down Expand Up @@ -223,6 +232,17 @@ public override void OnLoad(ConfigNode node)
Ready = true;
return;
}
savedNode = node.CreateCopy();
StartCoroutine(WaitForFlightGlobals());
}
ConfigNode savedNode;

IEnumerator WaitForFlightGlobals()
{
while (!FlightGlobals.ready || (FlightGlobals.ActiveVessel == null))
yield return new WaitForSeconds(0.1f);
ConfigNode node = savedNode;

node = node.GetNode(PERSISTENT_NODE_NAME);
vesselProfiles = new VesselTable();
var guidStrings = node.nodes.DistinctNames();
Expand All @@ -232,7 +252,10 @@ public override void OnLoad(ConfigNode node)
try
{
Guid guid = new Guid(strGuid); // could throw an exception if string is malformed
if (!FlightGlobals.Vessels.Any(v => v.id == guid)) continue;


if (!FlightGlobals.fetch.vessels.Any(v => v.id == guid)) continue;

if (vesselProfiles.ContainsKey(guid)) continue;

ConfigNode profileNode = node.GetNode(strGuid);
Expand All @@ -242,9 +265,10 @@ public override void OnLoad(ConfigNode node)
vesselProfiles.Add(guid, p);
else
{
if (HaveStoredProfile(p.name))
Profile storedProfile = FindStoredProfile(p.name);
if (HaveStoredProfile(storedProfile))
{
vesselProfiles.Add(guid, FindStoredProfile(p.name).Clone());
vesselProfiles.Add(guid, storedProfile.Clone());
}
else
{
Expand Down Expand Up @@ -272,7 +296,11 @@ public override void OnSave(ConfigNode node)
try
{
if (FlightGlobals.Vessels.Any(v => v.id == kvp.Key))
kvp.Value.OnSave(node.AddNode(new ConfigNode(kvp.Key.ToString())));
{
ConfigNode newNode = new ConfigNode(kvp.Key.ToString());
node.AddNode(newNode);
kvp.Value.OnSave(newNode);
}
}
catch (Exception e)
{
Expand Down Expand Up @@ -414,6 +442,11 @@ public static bool HaveStoredProfile(string name)
return FindStoredProfile(name) != null;
}

public static bool HaveStoredProfile(Profile p)
{
return p != null;
}

private string FindVesselName(Guid guid)
{
Vessel vessel = FlightGlobals.Vessels.SingleOrDefault(v => v.id == guid);
Expand Down

0 comments on commit ae53dc5

Please sign in to comment.