VOID_DataLogger: Switched most column values to VOID_Data fields.
VOID_DataLogger: Switched most column values to VOID_Data fields.

--- a/VOID_DataLogger.cs
+++ b/VOID_DataLogger.cs
@@ -56,6 +56,8 @@
 
 		protected System.Text.UTF8Encoding utf8Encoding;
 		protected FileStream _outputFile;
+
+		protected uint outstandingWrites;
 
 		protected List<string> csvList = new List<string>();
 
@@ -161,6 +163,7 @@
 			this.csv_log_interval_str = "0.5";
 
 			this.csvCollectTimer = 0;
+			this.outstandingWrites = 0;
 
 			this.WindowPos.x = Screen.width - 520;
 			this.WindowPos.y = 85;
@@ -288,6 +291,11 @@
 				this.AsyncWriteData();
 			}
 
+			while (this.outstandingWrites > 0)
+			{
+				System.Threading.Thread.Sleep(10);
+			}
+
 			if (this._outputFile != null)
 			{
 				logger.Append(" Closing File...");
@@ -298,11 +306,17 @@
 			logger.Print();
 		}
 
+		~VOID_DataLogger()
+		{
+			this.OnDestroy();
+		}
+
 		protected void AsyncWriteCallback(IAsyncResult result)
 		{
 			Tools.PostDebugMessage(this, "Got async callback, IsCompleted = {0}", result.IsCompleted);
 
 			this.outputFile.EndWrite(result);
+			this.outstandingWrites--;
 		}
 
 		private void AsyncWriteData()
@@ -323,7 +337,9 @@
 			WriteState state = new WriteState();
 
 			state.bytes = bytes.ToArray();
-
+			state.stream = this.outputFile;
+
+			this.outstandingWrites++;
 			var writeCallback = new AsyncCallback(this.AsyncWriteCallback);
 
 			this.outputFile.BeginWrite(state.bytes, 0, state.bytes.Length, writeCallback, state);
@@ -364,14 +380,13 @@
 			line.Append(vessel.missionTime.ToString("F3"));
 			line.Append(',');
 
+
 			//Altitude ASL
-			line.Append(vessel.orbit.altitude.ToString("F3"));
+			line.Append(VOID_Data.orbitAltitude.Value.ToString("F3"));
 			line.Append(',');
 
 			//Altitude (true)
-			double alt_true = vessel.orbit.altitude - vessel.terrainAltitude;
-			if (vessel.terrainAltitude < 0) alt_true = vessel.orbit.altitude;
-			line.Append(alt_true.ToString("F3"));
+			line.Append(VOID_Data.trueAltitude.Value.ToString("F3"));
 			line.Append(',');
 
 			// Surface Latitude
@@ -387,37 +402,35 @@
 			line.Append(',');
 
 			//Orbital velocity
-			line.Append(vessel.orbit.vel.magnitude.ToString("F3"));
+			line.Append(VOID_Data.orbitVelocity.Value.ToString("F3"));
 			line.Append(',');
 
 			//surface velocity
-			line.Append(vessel.srf_velocity.magnitude.ToString("F3"));
+			line.Append(VOID_Data.surfVelocity.Value.ToString("F3"));
 			line.Append(',');
 
 			//vertical speed
-			line.Append(vessel.verticalSpeed.ToString("F3"));
+			line.Append(VOID_Data.vertVelocity.Value.ToString("F3"));
 			line.Append(',');
 
 			//horizontal speed
-			line.Append(vessel.horizontalSrfSpeed.ToString("F3"));
+			line.Append(VOID_Data.horzVelocity.Value.ToString("F3"));
 			line.Append(',');
 
 			//gee force
-			line.Append(vessel.geeForce.ToString("F3"));
+			line.Append(VOID_Data.geeForce.Value.ToString("F3"));
 			line.Append(',');
 
 			//temperature
-			line.Append(vessel.flightIntegrator.getExternalTemperature().ToString("F2"));
+			line.Append(VOID_Data.temperature.Value.ToString("F2"));
 			line.Append(',');
 
 			//gravity
-			double r_vessel = vessel.mainBody.Radius + vessel.mainBody.GetAltitude(vessel.findWorldCenterOfMass());
-			double g_vessel = (VOID_Core.Constant_G * vessel.mainBody.Mass) / (r_vessel * r_vessel);
-			line.Append(g_vessel.ToString("F3"));
+			line.Append(VOID_Data.gravityAccel.Value.ToString("F3"));
 			line.Append(',');
 
 			//atm density
-			line.Append((vessel.atmDensity * 1000).ToString("F3"));
+			line.Append(Tools.MuMech_ToSI(VOID_Data.atmDensity.Value * 1000d, 3));
 			line.Append(',');
 
 			// Downrange Distance
@@ -433,7 +446,7 @@
 		private class WriteState
 		{
 			public byte[] bytes;
-			public KSP.IO.FileStream stream;
+			public FileStream stream;
 		}
 	}
 }