Fixed RelayDataCost addition. Fixed persistent loads of base packet costs. Fixed some debug logging.
Fixed RelayDataCost addition. Fixed persistent loads of base packet costs. Fixed some debug logging.

--- a/ARFlightController.cs
+++ b/ARFlightController.cs
@@ -158,7 +158,6 @@
 				this.toolbarButton.Text = "AntennaRange";
 				this.toolbarButton.Visibility = new GameScenesVisibility(GameScenes.FLIGHT);
                 this.toolbarButton.OnClick += (e) => { this.buttonToggle(); };
-                
             }
 
 			GameEvents.onGameSceneLoadRequested.Add(this.onSceneChangeRequested);
@@ -335,8 +334,8 @@
 
 					log.AppendFormat("\nFinding nearest relay for active vessel relay {0}", relay);
 
+					relay.FindNearestRelay();
 					relay.RecalculateTransmissionRates();
-					relay.FindNearestRelay();
 				}
 
 				if (this.toolbarButton != null || this.appLauncherButton != null)

--- a/AntennaRange.csproj
+++ b/AntennaRange.csproj
@@ -3,8 +3,6 @@
   <PropertyGroup>
     <Configuration Condition=" '$(Configuration)' == '' ">Debug_win</Configuration>
     <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
-    <ProductVersion>8.0.30703</ProductVersion>
-    <SchemaVersion>2.0</SchemaVersion>
     <ProjectGuid>{B36F2C11-962E-4A75-9F41-61AD56D11493}</ProjectGuid>
     <OutputType>Library</OutputType>
     <RootNamespace>AntennaRange</RootNamespace>
@@ -103,13 +101,13 @@
     </Reference>
   </ItemGroup>
   <ItemGroup>
+    <None Include="GameData\AntennaRange\AntennaRange.cfg" />
+    <None Include="GameData\AntennaRange\ATM_AntennaRange.cfg" />
+  </ItemGroup>
+  <ItemGroup>
     <ProjectReference Include="..\ToadicusTools\ToadicusTools.csproj">
       <Project>{D48A5542-6655-4149-BC27-B27DF0466F1C}</Project>
       <Name>ToadicusTools</Name>
     </ProjectReference>
   </ItemGroup>
-  <ItemGroup>
-    <None Include="GameData\AntennaRange\AntennaRange.cfg" />
-    <None Include="GameData\AntennaRange\ATM_AntennaRange.cfg" />
-  </ItemGroup>
 </Project>

--- a/GameData/AntennaRange/AntennaRange.cfg
+++ b/GameData/AntennaRange/AntennaRange.cfg
@@ -48,6 +48,9 @@
 		simpleRange = 20500000
 		maxPowerFactor = 8
 		maxDataFactor = 4
+
+		basePacketSize = #$packetSize$
+		basePacketResourceCost = #$packetResourceCost$
 	}
 
 	MODULE
@@ -70,6 +73,9 @@
 		simpleRange = 18000000000
 		maxPowerFactor = 4
 		maxDataFactor = 8
+
+		basePacketSize = #$packetSize$
+		basePacketResourceCost = #$packetResourceCost$
 	}
 
 	MODULE
@@ -93,6 +99,9 @@
 		simpleRange = 56250000000
 		maxPowerFactor = 16
 		maxDataFactor = 2
+
+		basePacketSize = #$packetSize$
+		basePacketResourceCost = #$packetResourceCost$
 	}
 
 	MODULE
@@ -118,6 +127,9 @@
 		simpleRange = 25030376544
 		maxPowerFactor = 2.6180339887498948
 		maxDataFactor = 9
+
+		basePacketSize = #$packetSize$
+		basePacketResourceCost = #$packetResourceCost$
 	}
 
 	MODULE
@@ -151,6 +163,9 @@
 	packetSize = 1
 	packetResourceCost = 6.25
 
+	basePacketSize = #$packetSize$
+	baseResourceCost = #$packetResourceCost$
+
 	requiredResource = ElectricCharge
 }
 

--- a/ModuleLimitedDataTransmitter.cs
+++ b/ModuleLimitedDataTransmitter.cs
@@ -59,6 +59,12 @@
 		// Sometimes we will need to communicate errors; this is how we do it.
 		private ScreenMessage ErrorMsg;
 
+		[KSPField(isPersistant = false)]
+		public float basePacketSize;
+
+		[KSPField(isPersistant = false)]
+		public float basePacketResourceCost;
+
 		/// <summary>
 		/// When additive ranges are enabled, the distance from Kerbin at which the antenna will perform exactly as
 		/// prescribed by packetResourceCost and packetSize.
@@ -180,6 +186,7 @@
 			}
 		}
 
+		private RelayDataCost _currentLinkCost = new RelayDataCost();
 		/// <summary>
 		/// Gets the current link resource rate in EC/MiT.
 		/// </summary>
@@ -188,7 +195,9 @@
 		{
 			get
 			{
-				return new RelayDataCost(this.packetResourceCost, this.packetSize);
+				_currentLinkCost.PacketResourceCost = this.packetResourceCost;
+				_currentLinkCost.PacketSize = this.packetSize;
+				return _currentLinkCost;
 			}
 			set
 			{
@@ -503,7 +512,7 @@
 		/// <param name="state">State.</param>
 		public override void OnStart (StartState state)
 		{
-			this.BaseLinkCost = new RelayDataCost(base.packetResourceCost, base.packetSize);
+			this.BaseLinkCost = new RelayDataCost(this.basePacketResourceCost, this.basePacketSize);
 			this.RecalculateMaxRange();
 
 			base.OnStart (state);
@@ -534,7 +543,7 @@
 
 			base.OnLoad (node);
 
-			this.BaseLinkCost = new RelayDataCost(base.packetResourceCost, base.packetSize);
+			this.BaseLinkCost = new RelayDataCost(this.basePacketResourceCost, this.basePacketSize);
 			this.RecalculateMaxRange();
 		}
 
@@ -738,7 +747,14 @@
 			if (this.relay != null)
 			{
 				this.relay.RecalculateTransmissionRates();
-			}
+				this.LogDebug("Recalculated transmission rates in MLDT, cost is {0}", this.CurrentLinkCost);
+			}
+			#if DEBUG
+			else
+			{
+				this.LogDebug("Skipping recalculation; relay is null.");
+			}
+			#endif
 		}
 
 		/// <summary>
@@ -970,6 +986,8 @@
 				if (this.part != null && this.part.partInfo != null)
 				{
 					sb.Append(this.part.partInfo.title);
+					sb.Append('#');
+					sb.Append(this.part.flightID);
 				}
 				else
 				{

--- a/ProtoAntennaRelay.cs
+++ b/ProtoAntennaRelay.cs
@@ -218,6 +218,8 @@
 
 				if (this.protoPart != null && this.protoPart.pVesselRef != null)
 				{
+					sb.Append('#');
+					sb.Append(this.protoPart.flightID);
 					sb.AppendFormat(" on {0}", this.protoPart.pVesselRef.vesselName);
 				}
 

--- a/RelayDataCost.cs
+++ b/RelayDataCost.cs
@@ -65,7 +65,7 @@
 				lcd.PacketResourceCost *= mul;
 			}
 
-			return new RelayDataCost(gcd.PacketResourceCost + lcd.PacketResourceCost, gcd.PacketSize + lcd.PacketSize);
+			return new RelayDataCost(gcd.PacketResourceCost + lcd.PacketResourceCost, gcd.PacketSize);
 		}
 
 		/// <param name="only">RelayDataCost to be negated</param>

--- a/RelayExtensions.cs
+++ b/RelayExtensions.cs
@@ -343,7 +343,13 @@
 		[System.Diagnostics.Conditional("DEBUG")]
 		public static void LogDebug(this AntennaRelay relay, string format, params object[] args)
 		{
-			ToadicusTools.Logging.PostDebugMessage(string.Format("[{0}] {1}", relay.ToString(), format), args);
+			using (var sb = ToadicusTools.Text.PooledStringBuilder.Get())
+			{
+				sb.AppendFormat("[{0}] ", relay == null ? "NULL" : relay.ToString());
+				sb.AppendFormat(format, args);
+
+				ToadicusTools.Logging.PostDebugMessage(sb.ToString());
+			}
 		}
 
 		/// <summary>
@@ -352,7 +358,13 @@
 		[System.Diagnostics.Conditional("DEBUG")]
 		public static void LogDebug(this AntennaRelay relay, string msg)
 		{
-			ToadicusTools.Logging.PostDebugMessage("[{0}] {1}", relay.ToString(), msg);
+			using (var sb = ToadicusTools.Text.PooledStringBuilder.Get())
+			{
+				sb.AppendFormat("[{0}] ", relay == null ? "NULL" : relay.ToString());
+				sb.Append(msg);
+
+				ToadicusTools.Logging.PostDebugMessage(sb.ToString());
+			}
 		}
 	}