private void FixedUpdate() { this.log.Clear(); VesselCommand availableCommand; if (ARConfiguration.RequireConnectionForControl && this.vessel != null) { availableCommand = this.vessel.CurrentCommand(); } else { availableCommand = VesselCommand.Crew; } log.AppendFormat("availableCommand: {0}\n\t" + "(availableCommand & VesselCommand.Crew) == VesselCommand.Crew: {1}\n\t" + "(availableCommand & VesselCommand.Probe) == VesselCommand.Probe: {2}\n\t" + "vessel.HasConnectedRelay(): {3}", (int)availableCommand, (availableCommand & VesselCommand.Crew) == VesselCommand.Crew, (availableCommand & VesselCommand.Probe) == VesselCommand.Probe, vessel.HasConnectedRelay() ); // If we are requiring a connection for control, the vessel does not have any adequately staffed pods, // and the vessel does not have any connected relays... if ( HighLogic.LoadedSceneIsFlight && ARConfiguration.RequireConnectionForControl && this.vessel != null && this.vessel.vesselType != VesselType.EVA && !( (availableCommand & VesselCommand.Crew) == VesselCommand.Crew || (availableCommand & VesselCommand.Probe) == VesselCommand.Probe && vessel.HasConnectedRelay() )) { // ...and if the controls are not currently locked... if (currentControlLock == ControlTypes.None) { // ...lock the controls. InputLockManager.SetControlLock(this.lockSet, this.lockID); } } // ...otherwise, if the controls are locked... else if (currentControlLock != ControlTypes.None) { // ...unlock the controls. InputLockManager.RemoveControlLock(this.lockID); } log.Print(); }
private void CloseFileIfOpen() { using (PooledDebugLogger logger = PooledDebugLogger.New(this)) { logger.AppendFormat("Cleaning up file {0}...", this.fileName); if (this.csvBytes != null && this.csvBytes.Count > 0) { logger.Append(" Writing remaining data..."); this.AsyncWriteData(); } logger.Append(" Waiting for writes to finish."); while (this.outstandingWrites > 0) { logger.Append('.'); System.Threading.Thread.Sleep(10); } if (this._outputFile != null) { this._outputFile.Close(); this._outputFile = null; logger.Append(" File closed."); } logger.Print(false); } }
public void OnDestroy() { using (PooledDebugLogger logger = PooledDebugLogger.New(this)) { logger.Append("Destroying..."); this.CloseFileIfOpen(); logger.Append(" Done."); logger.Print(false); } }
protected void LoadModulesOfType <U>() { using (PooledDebugLogger sb = PooledDebugLogger.New(this)) { sb.AppendLine("Loading modules..."); AssemblyLoader.LoadedAssembly assy; for (int aIdx = 0; aIdx < AssemblyLoader.loadedAssemblies.Count; aIdx++) { assy = AssemblyLoader.loadedAssemblies[aIdx]; Type[] loadedTypes = assy.assembly.GetExportedTypes(); Type loadedType; for (int tIdx = 0; tIdx < loadedTypes.Length; tIdx++) { loadedType = loadedTypes[tIdx]; if ( loadedType.IsInterface || loadedType.IsAbstract || !typeof(U).IsAssignableFrom(loadedType) || typeof(VOIDCore).IsAssignableFrom(loadedType)) { continue; } sb.AppendFormat("Checking IVOID_Module type {0}...", loadedType.Name); try { this.LoadModule(loadedType); sb.AppendLine("Success."); } catch (Exception ex) { sb.AppendFormat("Failed, caught {0}\n", ex.GetType().Name); #if DEBUG Debug.LogException(ex); #endif } } } this.LoadConfig(); this.modulesLoaded = true; sb.AppendFormat("Loaded {0} modules.\n", this.Modules.Count); sb.Print(); } }
public void FixedUpdate() { if ( HighLogic.LoadedSceneIsFlight && FlightGlobals.Vessels != null && this.vessel != null && this.dockingModule != null ) { #if DEBUG bool foundApproach = false; #endif PooledDebugLogger verboseLog = PooledDebugLogger.New(this); #if DEBUG try { #endif verboseLog.AppendFormat(" ({0}_{1}) on {2}", this.part.partInfo.name, this.part.craftID, this.vessel.vesselName); verboseLog.AppendFormat("\nChecking within acquireRangeSqr: {0}", this.acquireRangeSqr); verboseLog.AppendFormat("this.dockingModule: {0}\n", this.dockingModule == null ? "null" : this.dockingModule.ToString()); verboseLog.AppendFormat("this.dockingModule.state: {0}\n", this.dockingModule == null || this.dockingModule.state == null ? "null" :this.dockingModule.state.ToString()); // If we're already docked or pre-attached... if (this.dockingModule.state == "Docked" || this.dockingModule.state == "PreAttached") { // ...and if the timeout timer is running... if (this.timeoutTimer.IsRunning) { // ...reset the timeout timer this.timeoutTimer.Reset(); } // ...skip this check return; } // If the timeout timer is running, we found a match recently. If we haven't found a match in more than // five seconds, it's not recent anymore, so reset our size to default. if (this.timeoutTimer.IsRunning && this.timeoutTimer.ElapsedMilliseconds > 5000) { verboseLog.AppendFormat("\nNo target detected within 5 seconds, timing out."); if (this.dockingModule.state != "Docked") { verboseLog.AppendFormat("\nReverting to default nodeType: {0}", this.defaultSize); this.currentSize = this.defaultSize; } this.timeoutTimer.Reset(); } bool foundTargetNode = false; float closestNodeDistSqr = this.acquireRangeSqr * 4f; verboseLog.Append("Starting Vessels loop."); // Check all vessels for potential docking targets Vessel vessel; for (int vIdx = 0; vIdx < FlightGlobals.Vessels.Count; vIdx++) { vessel = FlightGlobals.Vessels[vIdx]; if (vessel == null) { verboseLog.Append("Skipping null vessel."); continue; } // Skip vessels that are just way too far away. if (this.vessel.sqrDistanceTo(vessel) > this.vesselFilterDistanceSqr) { verboseLog.AppendFormat("\nSkipping distant vessel {0} (sqrDistance {1})", vessel.vesselName, (vessel.GetWorldPos3D() - this.dockingModule.nodeTransform.position).sqrMagnitude ); continue; } verboseLog.AppendFormat("\nChecking nearby vessel {0} (sqrDistance {1})", vessel.vesselName, (vessel.GetWorldPos3D() - this.dockingModule.nodeTransform.position).sqrMagnitude ); // Since this vessel is not too far away, check all docking nodes on the vessel. IList <ModuleDockingNode> potentialNodes = vessel.getModulesOfType <ModuleDockingNode>(); ModuleDockingNode potentialTargetNode; for (int nIdx = 0; nIdx < potentialNodes.Count; nIdx++) { potentialTargetNode = potentialNodes[nIdx]; if (potentialTargetNode == null) { verboseLog.AppendFormat("\nSkipping potentialTargetNode at index {0} because it is null", nIdx); continue; } if (potentialTargetNode.part == null) { verboseLog.AppendFormat("\nSkipping potentialTargetNode at index {0} because its part is null", nIdx); continue; } if (potentialTargetNode.nodeTransform == null) { verboseLog.AppendFormat("\nSkipping potentialTargetNode at index {0} because its node transform is null", nIdx); continue; } verboseLog.AppendFormat("\nFound potentialTargetNode: {0}", potentialTargetNode); verboseLog.AppendFormat("\n\tpotentialTargetNode.state: {0}", potentialTargetNode.state); verboseLog.AppendFormat("\n\tpotentialTargetNode.nodeType: {0}", potentialTargetNode.nodeType); // We can't skip the current vessel, because it's possible to dock parts on a single vessel to // each other. Still, we can't dock a port to itself, so skip this part. if (potentialTargetNode.part == this.part) { verboseLog.AppendFormat("\nDiscarding potentialTargetNode: on this part."); continue; } // If this docking node is already docked, we can't dock to it, so skip it. if ( potentialTargetNode.state.Contains("Docked") || potentialTargetNode.state.Contains("PreAttached")) { verboseLog.Append("\nDiscarding potentialTargetNode: not ready."); continue; } float thisNodeDistSqr = (potentialTargetNode.nodeTransform.position - this.dockingModule.nodeTransform.position) .sqrMagnitude; verboseLog.AppendFormat( "\n\tChecking potentialTargetNode sqrDistance against the lesser of acquireRangeSqr and " + "closestNodeDistSqr ({0})", Mathf.Min(this.acquireRangeSqr * 4f, closestNodeDistSqr) ); // Only bother checking nodes closer than twice our acquire range. We have to check before we // get within acquire range to make sure Squad's code will catch us when we get there. if (thisNodeDistSqr <= Mathf.Min(this.acquireRangeSqr * 4f, closestNodeDistSqr)) { #if DEBUG foundApproach = true; #endif verboseLog.AppendFormat( "\n\tpotentialTargetNode is nearby ({0}), checking if adaptive.", thisNodeDistSqr); ModuleAdaptiveDockingNode targetAdaptiveNode = null; string targetSize; targetSize = string.Empty; // adapt to non-adaptive docking nodes if (this.validSizes.Contains(potentialTargetNode.nodeType)) { targetSize = potentialTargetNode.nodeType; this.currentSize = targetSize; } // Otherwise, look for another adaptive node. else { // Check the part for an AdaptiveDockingNode targetAdaptiveNode = potentialTargetNode.part .getFirstModuleOfType <ModuleAdaptiveDockingNode>(); } // If we've found an AdaptiveDockingNode... if (targetAdaptiveNode != null) { verboseLog.AppendFormat("\n\tpotentialTargetNode is adaptive."); verboseLog.AppendFormat("\n\tdefaultSize: {0}", targetAdaptiveNode.defaultSize); verboseLog.AppendFormat("\n\tvalidSizes: {0}", targetAdaptiveNode.validSizes); // ...and if we can become its largest (default) size... // to their default size. if (this.validSizes.Contains(targetAdaptiveNode.defaultSize)) { // ...target its default size. targetSize = targetAdaptiveNode.defaultSize; } // ...otherwise, look for a common size. else { string commonNodeType = GetGreatestCommonNodeType(this, targetAdaptiveNode); // ...if we didn't find a common size, stop processing this node if (commonNodeType == string.Empty) { verboseLog.AppendFormat("\n\tInvalid adaptive target: no common node types."); continue; } targetSize = commonNodeType; targetAdaptiveNode.currentSize = targetSize; verboseLog.AppendFormat( "\n\tTarget nodeType set to commonNodeType: {0}", targetSize); } } #if DEBUG else { verboseLog.AppendFormat("\n\tpotentialTargetNode is not adaptive."); verboseLog.AppendFormat("\n\tnodeType: {0}", potentialTargetNode.nodeType); } #endif // If we never found a target size, it's not a match, so stop processing. if (targetSize == string.Empty) { continue; } // ...otherwise, log this node as the closest and adapt to the target size. closestNodeDistSqr = thisNodeDistSqr; this.currentSize = targetSize; foundTargetNode = true; verboseLog.AppendFormat("\n\tLocal nodeType set to commonNodeType: {0}", targetSize); verboseLog.AppendFormat("\n\tFound suitable docking node."); verboseLog.AppendFormat("\n\ttargetSize: {0}", targetSize); verboseLog.AppendFormat("\n\tForward vector dot product: {0} (acquire minimum: {1})", Vector3.Dot(potentialTargetNode.nodeTransform.forward, this.dockingModule.nodeTransform.forward), this.dockingModule.acquireMinFwdDot ); verboseLog.AppendFormat("\n\tUp vector dot product: {0} (acquire minimum: {1})", Vector3.Dot(potentialTargetNode.nodeTransform.up, this.dockingModule.nodeTransform.up), this.dockingModule.acquireMinRollDot ); } else { verboseLog.AppendFormat( "\nDiscarding potentialTargetNode: too far away (thisNodeDistSqr: {0})", thisNodeDistSqr ); } } verboseLog.Append('\n'); } verboseLog.Append("\nFixedUpdate Finished."); if (foundTargetNode) { if (this.timeoutTimer.IsRunning) { this.timeoutTimer.Reset(); } this.timeoutTimer.Start(); } #if DEBUG } finally { // if (foundApproach) verboseLog.Print(); } #endif } }
// Runs on start. public override void OnStart(StartState state) { using (PooledDebugLogger log = PooledDebugLogger.New(this)) { #if DEBUG try { #endif log.AppendFormat("{0}: starting up", this.ToString()); // Start up the underlying PartModule stuff. base.OnStart(state); log.Append("\n\tbase started up"); ModuleReactionWheel prefabModule; // Fetch the reaction wheel module. if (this.part.tryGetFirstModuleOfType <ModuleReactionWheel>(out this.reactionWheelModule)) { log.AppendFormat("\n\tFound ModuleReactionWheel {0}", this.reactionWheelModule); if (PartLoader.getPartInfoByName(this.part.partInfo.name).partPrefab .tryGetFirstModuleOfType <ModuleReactionWheel>(out prefabModule)) { log.AppendFormat("\n\tFound prefab module {0}", prefabModule); TweakableTools.InitializeTweakable <ModuleTweakableReactionWheel>( this.Fields["RollTorque"].uiControlCurrent(), ref this.RollTorque, ref this.reactionWheelModule.RollTorque, prefabModule.RollTorque ); log.Append("\n\tRollTorque setup"); TweakableTools.InitializeTweakable <ModuleTweakableReactionWheel>( this.Fields["PitchTorque"].uiControlCurrent(), ref this.PitchTorque, ref this.reactionWheelModule.PitchTorque, prefabModule.PitchTorque ); log.Append("\n\tPitchTorque setup"); TweakableTools.InitializeTweakable <ModuleTweakableReactionWheel>( this.Fields["YawTorque"].uiControlCurrent(), ref this.YawTorque, ref this.reactionWheelModule.YawTorque, prefabModule.YawTorque ); log.Append("\n\tYawTorque setup"); } } var torqueGainCtl = this.Fields["TorqueGain"].uiControlCurrent(); if (torqueGainCtl is UI_FloatRange) { var torqueGainSlider = torqueGainCtl as UI_FloatRange; torqueGainSlider.maxValue = 1f; torqueGainSlider.stepIncrement = 0.025f; } log.Append("\n\tStarted!"); #if DEBUG } finally { log.Print(); } #endif } }
private void OnPreCull() { if (!HighLogic.LoadedSceneIsFlight || !MapView.MapIsEnabled || !ARConfiguration.PrettyLines) { this.Cleanup(!HighLogic.LoadedSceneIsFlight); return; } #if DEBUG || BENCH timer.Restart(); #endif try { log.Clear(); log.AppendFormat("OnPreCull.\n"); /* @ TODO: Fix * log.AppendFormat("\tMapView: Draw3DLines: {0}\n" + * "\tMapView.MapCamera.camera.fieldOfView: {1}\n" + * "\tMapView.MapCamera.Distance: {2}\n", * MapView.Draw3DLines, * MapView.MapCamera.camera.fieldOfView, * MapView.MapCamera.Distance * ); */ log.AppendLine("FlightGlobals ready and Vessels list not null."); IAntennaRelay relay; for (int i = 0; i < ARFlightController.UsefulRelays.Count; i++) { relay = ARFlightController.UsefulRelays[i]; if (relay == null) { log.AppendFormat("\n\tGot null relay, skipping"); continue; } log.AppendFormat("\n\tDrawing pretty lines for useful relay {0}", relay); #if DEBUG start = timer.ElapsedMilliseconds; #endif this.SetRelayVertices(relay); log.AppendFormat("\n\tSet relay vertices for {0} in {1}ms", relay, timer.ElapsedMilliseconds - start); } } catch (Exception ex) { this.LogError("Caught {0}: {1}\n{2}\n", ex.GetType().Name, ex.ToString(), ex.StackTrace.ToString()); this.Cleanup(false); } #if DEBUG finally { log.AppendFormat("\n\tOnPreCull finished in {0}ms\n", timer.ElapsedMilliseconds); log.Print(); } #endif #if BENCH ARMapRenderer.updateCount++; ARMapRenderer.updateTimer += (ulong)this.timer.ElapsedTicks; if (ARMapRenderer.updateCount >= (ulong)(8d / Time.smoothDeltaTime)) { ARMapRenderer.averager.AddItem((double)ARMapRenderer.updateTimer / (double)ARMapRenderer.updateCount); ARMapRenderer.updateTimer = 0u; ARMapRenderer.updateCount = 0u; ARMapRenderer.twiceAverageTime = (long)(ARMapRenderer.averager.Average * 2d); } if (this.timer.ElapsedTicks > ARMapRenderer.twiceAverageTime) { this.Log("PreCull took significant longer than usual ({0:S3}s vs {1:S3}s)", (double)this.timer.ElapsedTicks / (double)System.Diagnostics.Stopwatch.Frequency, ARMapRenderer.averager.Average / (double)System.Diagnostics.Stopwatch.Frequency ); } #endif }