// Methods for accessing the TestFlight modules on a given part public static string GetFullPartName(Part part) { string baseName = part.name; if (part.Modules == null) { return(baseName); } // New query system // Find the active core ITestFlightCore core = TestFlightUtil.GetCore(part); if (core == null) { return(baseName); } // Look if it has an alias and use that if present string query = core.Configuration; if (query.Contains(":")) { return(query.Split(new char[1] { ':' })[1]); } // Otherwise use part.name else { return(baseName); } }
public void OnEnable() { if (core == null) { core = TestFlightUtil.GetCore(this.part, Configuration); } }
public static string GetPartTitle(Part part) { string baseName = part.partInfo.title; if (part.Modules == null) { return(baseName); } // Find the active core ITestFlightCore core = TestFlightUtil.GetCore(part); if (core == null) { return(baseName); } if (String.IsNullOrEmpty(core.Title)) { return(baseName); } else { return(core.Title); } }
public virtual float DoRepair() { Failed = false; ITestFlightCore core = TestFlightUtil.GetCore(this.part, Configuration); if (core != null) core.ModifyFlightData(duRepair, true); return 0; }
public override void OnStart(StartState state) { base.OnStart(state); if (core == null) { core = TestFlightUtil.GetCore(this.part, Configuration); } }
IEnumerator GetCore() { while (core == null) { core = TestFlightUtil.GetCore(this.part); yield return(null); } }
/// <summary> /// Triggers the failure controlled by the failure module /// </summary> public virtual void DoFailure() { Failed = true; ITestFlightCore core = TestFlightUtil.GetCore(this.part, Configuration); if (core != null) { core.ModifyFlightData(duFail, true); FlightLogger.eventLog.Add(String.Format("[{0}] {1} failed: {2}", KSPUtil.PrintTimeCompact((int)Math.Floor(this.vessel.missionTime), false), core.Title, failureTitle)); } }
/// <summary> /// Triggers the failure controlled by the failure module /// </summary> public virtual void DoFailure() { Failed = true; ITestFlightCore core = TestFlightUtil.GetCore(this.part, Configuration); if (core != null) { core.ModifyFlightData(duFail, true); } }
public override void OnStart(StartState state) { base.OnStart(state); core = TestFlightUtil.GetCore(this.part); if (core == null) { StartCoroutine("GetCore"); } }
public static void Log(string message, Part loggingPart) { ITestFlightCore core = TestFlightUtil.GetCore(loggingPart); bool debug = false; if (core != null) { debug = core.DebugEnabled; } TestFlightUtil.Log(message, debug); }
/// <summary> /// Triggers the failure controlled by the failure module /// </summary> public virtual void DoFailure() { Failed = true; ITestFlightCore core = TestFlightUtil.GetCore(this.part); if (core != null) { core.ModifyFlightData(duFail, true); } FlightLogger.eventLog.Add(String.Format("[{0}] {1} failed: {2}", TestFlightUtil.FormatTime(this.vessel.missionTime), TestFlightUtil.GetPartTitle(this.part), failureTitle)); }
public virtual float DoRepair() { Failed = false; ITestFlightCore core = TestFlightUtil.GetCore(this.part); if (core != null) { core.ModifyFlightData(duRepair, true); } return(0); }
public override void OnStart(StartState state) { base.OnStart(state); if (core == null) { core = TestFlightUtil.GetCore(this.part, Configuration); } if (core != null) { Startup(); } verboseDebugging = core.DebugEnabled; }
// INTERNAL methods IEnumerator Attach() { while (this.part == null || this.part.partInfo == null || this.part.partInfo.partPrefab == null || this.part.Modules == null) { yield return(null); } while (core == null) { core = TestFlightUtil.GetCore(this.part, Configuration); yield return(null); } Startup(); }
IEnumerator Attach() { while (this.part == null || this.part.partInfo == null || this.part.partInfo.partPrefab == null || this.part.Modules == null) { yield return(null); } while (core == null) { core = TestFlightUtil.GetCore(this.part, Configuration); if (core != null) { Log(String.Format("Attached to Core({0})", core.Configuration)); } yield return(null); } Startup(); }
public bool AddInteropValue(string name, bool value, string owner) { name = name.ToLower().Trim(); if (!RemoveInteropValue(name, owner)) { return(false); } InteropValue opValue = new InteropValue(); opValue.owner = owner; opValue.value = String.Format("{0:D}", value); opValue.valueType = InteropValueType.BOOL; knownInterops.Add(name, opValue); TestFlightUtil.UpdatePartConfigs(this.part); return(true); }
public void ClearInteropValues(string owner) { List <String> keysToDelete = new List <string>(); foreach (string key in knownInterops.Keys) { if (knownInterops[key].owner == owner) { keysToDelete.Add(key); } } if (keysToDelete.Count > 0) { foreach (string key in keysToDelete) { knownInterops.Remove(key); } } TestFlightUtil.UpdatePartConfigs(this.part); }
protected void LoadDataFromPrefab() { Log("Loading data from prefab"); Part prefab = this.part.partInfo.partPrefab; foreach (PartModule pm in prefab.Modules) { TestFlightReliabilityBase modulePrefab = pm as TestFlightReliabilityBase; // As of v1.3 this is simpler because we don't have scope or reliability bodies if (modulePrefab != null && TestFlightUtil.EvaluateQuery(modulePrefab.Configuration, this.part)) { Log("Found matching prefab"); if (modulePrefab.reliabilityCurve != null && modulePrefab.reliabilityCurve.maxTime > 0) { Log(String.Format("Found reliabilityCurve with data point between {0:F2} and {1:F2}. Loading curve from prefab", modulePrefab.reliabilityCurve.minTime, modulePrefab.reliabilityCurve.maxTime)); reliabilityCurve = modulePrefab.reliabilityCurve; return; } } } }
public bool AddInteropValue(string name, string value, string owner) { name = name.ToLower().Trim(); if (!RemoveInteropValue(name, owner)) { return(false); } InteropValue opValue = new InteropValue(); opValue.owner = owner; opValue.value = value; opValue.valueType = InteropValueType.STRING; knownInterops.Add(name, opValue); Log(String.Format("Added new interop, {0} = {1}, for {2}", name, value, owner)); TestFlightUtil.UpdatePartConfigs(this.part); return(true); }
public bool RemoveInteropValue(string name, string owner) { name = name.ToLower().Trim(); if (knownInterops == null) { knownInterops = new Dictionary <string, InteropValue>(); } if (!knownInterops.ContainsKey(name)) { return(true); } InteropValue opValue = knownInterops[name]; if (opValue.owner != owner) { return(false); } knownInterops.Remove(name); TestFlightUtil.UpdatePartConfigs(this.part); return(true); }
public virtual List <string> GetTestFlightInfo(float reliabilityAtTime) { List <string> infoStrings = new List <string>(); if (core == null) { Log("Core is null"); return(infoStrings); } if (reliabilityCurve == null) { Log("Curve is null"); return(infoStrings); } double currentFailRate = core.GetBaseFailureRate(); double maxFailRate = GetBaseFailureRate(reliabilityCurve.maxTime); infoStrings.Add("<b>Base Reliability</b>"); infoStrings.Add(String.Format("<b>Current Reliability</b>: {0:P1} at full burn, {1} <b>MTBF</b>", TestFlightUtil.FailureRateToReliability(currentFailRate, reliabilityAtTime), core.FailureRateToMTBFString(currentFailRate, TestFlightUtil.MTBFUnits.SECONDS, 999))); infoStrings.Add(String.Format("<b>Maximum Reliability</b>: {0:P1} at full burn, {1} <b>MTBF</b>", TestFlightUtil.FailureRateToReliability(maxFailRate, reliabilityAtTime), core.FailureRateToMTBFString(maxFailRate, TestFlightUtil.MTBFUnits.SECONDS, 999))); return(infoStrings); }
protected void Log(string message) { message = String.Format("TestFlightFailure({0}[{1}]): {2}", TestFlightUtil.GetFullPartName(this.part), Configuration, message); TestFlightUtil.Log(message, this.part); }
internal void Log(string message) { message = String.Format("TestFlightInterop({0}): {1}", TestFlightUtil.GetFullPartName(this.part), message); TestFlightUtil.Log(message, this.part); }
// Simply converts the failure rate to a MTBF number, without any string formatting public double FailureRateToMTBF(double failureRate, TestFlightUtil.MTBFUnits units) { failureRate = Math.Max(failureRate, TestFlightUtil.MIN_FAILURE_RATE); double mtbfSeconds = 1.0f / failureRate; switch (units) { case TestFlightUtil.MTBFUnits.SECONDS: return mtbfSeconds; case TestFlightUtil.MTBFUnits.MINUTES: return mtbfSeconds / 60; case TestFlightUtil.MTBFUnits.HOURS: return mtbfSeconds / 60 / 60; case TestFlightUtil.MTBFUnits.DAYS: return mtbfSeconds / 60 / 60 / 24; case TestFlightUtil.MTBFUnits.YEARS: return mtbfSeconds / 60 / 60 / 24 / 365; default: return mtbfSeconds; } }
internal static string UnitStringForMTBFUnitShort(TestFlightUtil.MTBFUnits units) { switch (units) { case TestFlightUtil.MTBFUnits.SECONDS: return "s"; case TestFlightUtil.MTBFUnits.MINUTES: return "m"; case TestFlightUtil.MTBFUnits.HOURS: return "h"; case TestFlightUtil.MTBFUnits.DAYS: return "d"; case TestFlightUtil.MTBFUnits.YEARS: return "y"; default: return "-"; } }
public static bool EvaluateBlock(string block, Part part) { block = block.ToLower(); // The meat of the evaluation is done here if (block.Contains(" ")) { string[] parts = block.Split(new char[1] { ' ' }); if (parts.Length < 3) { return(false); } string qualifier = parts[0]; string op = parts[1]; string term = parts[2]; string term1 = ""; string term2 = ""; if (term.Contains("-")) { term1 = term.Split(new char[1] { '-' })[0]; term2 = term.Split(new char[1] { '-' })[1]; } // try to get the interop value for this operator ITestFlightInterop interop = TestFlightUtil.GetInteropModule(part); if (interop == null) { return(false); } var val = interop.GetInterop(qualifier); if (val.valueType == InteropValueType.INVALID) { return(false); } switch (op) { case "=": switch (val.valueType) { case InteropValueType.BOOL: return(bool.Parse(val.value) == bool.Parse(term)); case InteropValueType.FLOAT: return(Math.Abs(float.Parse(val.value) - float.Parse(term)) < .0001); case InteropValueType.INT: return(int.Parse(val.value) == int.Parse(term)); case InteropValueType.STRING: return(string.Equals(val.value, term, StringComparison.InvariantCultureIgnoreCase)); } break; case "!=": switch (val.valueType) { case InteropValueType.BOOL: return(bool.Parse(val.value) != bool.Parse(term)); case InteropValueType.FLOAT: return(Math.Abs(float.Parse(val.value) - float.Parse(term)) > .0001); case InteropValueType.INT: return(int.Parse(val.value) != int.Parse(term)); case InteropValueType.STRING: return(!string.Equals(val.value, term, StringComparison.InvariantCultureIgnoreCase)); } break; case "<": switch (val.valueType) { case InteropValueType.FLOAT: return(float.Parse(val.value) < float.Parse(term)); case InteropValueType.INT: return(int.Parse(val.value) < int.Parse(term)); } break; case ">": switch (val.valueType) { case InteropValueType.FLOAT: return(float.Parse(val.value) > float.Parse(term)); case InteropValueType.INT: return(int.Parse(val.value) > int.Parse(term)); } break; case "<=": switch (val.valueType) { case InteropValueType.FLOAT: return(float.Parse(val.value) <= float.Parse(term)); case InteropValueType.INT: return(int.Parse(val.value) <= int.Parse(term)); } break; case ">=": switch (val.valueType) { case InteropValueType.FLOAT: return(float.Parse(val.value) >= float.Parse(term)); case InteropValueType.INT: return(int.Parse(val.value) >= int.Parse(term)); } break; case "<>": switch (val.valueType) { case InteropValueType.FLOAT: return(float.Parse(val.value) > float.Parse(term1) && float.Parse(val.value) < float.Parse(term2)); case InteropValueType.INT: return(int.Parse(val.value) > int.Parse(term1) && int.Parse(val.value) < int.Parse(term1)); } break; case "<=>": switch (val.valueType) { case InteropValueType.FLOAT: return(float.Parse(val.value) >= float.Parse(term1) && float.Parse(val.value) <= float.Parse(term2)); case InteropValueType.INT: return(int.Parse(val.value) >= int.Parse(term1) && int.Parse(val.value) <= int.Parse(term1)); } break; } return(false); } else { return(false); } }
public void FailureRateToMTBFString(double failureRate, TestFlightUtil.MTBFUnits units, bool shortForm, int maximum, out string output) { if (sb == null) sb = new StringBuilder(256); sb.Length = 0; var currentUnit = (int)TestFlightUtil.MTBFUnits.SECONDS; var mtbf = FailureRateToMTBF(failureRate, (TestFlightUtil.MTBFUnits)currentUnit); while (mtbf > maximum) { currentUnit++; mtbf = FailureRateToMTBF(failureRate, (TestFlightUtil.MTBFUnits)currentUnit); if ((TestFlightUtil.MTBFUnits)currentUnit == TestFlightUtil.MTBFUnits.INVALID) break; } if (shortForm) { output = sb.AppendFormat("{0:F2}{1}", mtbf, UnitStringForMTBFUnitShort((TestFlightUtil.MTBFUnits) currentUnit)).ToString(); } else { output = sb.AppendFormat("{0:F2}{1}", mtbf, UnitStringForMTBFUnit((TestFlightUtil.MTBFUnits)currentUnit)).ToString(); } }
internal String UnitStringForMTBFUnit(TestFlightUtil.MTBFUnits units) { switch (units) { case TestFlightUtil.MTBFUnits.SECONDS: return "seconds"; case TestFlightUtil.MTBFUnits.MINUTES: return "minutes"; case TestFlightUtil.MTBFUnits.HOURS: return "hours"; case TestFlightUtil.MTBFUnits.DAYS: return "days"; case TestFlightUtil.MTBFUnits.YEARS: return "years"; default: return "invalid"; } }
protected void Log(string message) { message = String.Format("FlightDataRecorder({0}[{1}]): {2}", Configuration, Configuration, message); TestFlightUtil.Log(message, this.part); }
internal void Log(string message) { message = String.Format("TestFlightReliability({0}[{1}]): {2}", TestFlightUtil.GetFullPartName(this.part), Configuration, message); TestFlightUtil.Log(message, this.part); }
// Simply converts the failure rate to a MTBF number, without any string formatting public float FailureRateToMTBF(float failureRate, TestFlightUtil.MTBFUnits units) { failureRate = Mathf.Max((float)failureRate, (float)TestFlightUtil.MIN_FAILURE_RATE); float mtbfSeconds = 1.0f / failureRate; switch (units) { case TestFlightUtil.MTBFUnits.SECONDS: return mtbfSeconds; case TestFlightUtil.MTBFUnits.MINUTES: return mtbfSeconds / 60; case TestFlightUtil.MTBFUnits.HOURS: return mtbfSeconds / 60 / 60; case TestFlightUtil.MTBFUnits.DAYS: return mtbfSeconds / 60 / 60 / 24; case TestFlightUtil.MTBFUnits.YEARS: return mtbfSeconds / 60 / 60 / 24 / 365; default: return mtbfSeconds; } }
// simply converts the failure rate into a MTBF string. Convenience method // Returned string will be of the format "123 units" // units should be one of: // seconds, hours, days, years, public String FailureRateToMTBFString(float failureRate, TestFlightUtil.MTBFUnits units) { return FailureRateToMTBFString(failureRate, units, false, int.MaxValue); }
public String FailureRateToMTBFString(float failureRate, TestFlightUtil.MTBFUnits units, int maximum) { return FailureRateToMTBFString(failureRate, units, false, maximum); }
// Short version of MTBFString uses a single letter to denote (s)econds, (m)inutes, (h)ours, (d)ays, (y)ears // The returned string will be of the format "12.00s" or "0.20d" public String FailureRateToMTBFString(float failureRate, TestFlightUtil.MTBFUnits units, bool shortForm) { return FailureRateToMTBFString(failureRate, units, shortForm, int.MaxValue); }
public override void OnAwake() { base.OnAwake(); AddInteropValue("kspPartName", TestFlightUtil.GetPartName(this.part), "TestFlight"); }
protected static bool EvaluateBlock(string block, Part part) { block = block.ToLower(); // The meat of the evaluation is done here if (block.Contains(" ")) { string[] parts = block.Split(new char[1] { ' ' }); if (parts.Length < 3) { return(false); } string qualifier = parts[0]; string op = parts[1]; string term = parts[2]; string term1 = ""; string term2 = ""; if (term.Contains("-")) { term1 = term.Split(new char[1] { '-' })[0]; term2 = term.Split(new char[1] { '-' })[1]; } // try to get the interop value for this operator ITestFlightInterop interop = TestFlightUtil.GetInteropModule(part); if (interop == null) { return(false); } InteropValue val; val = interop.GetInterop(qualifier); if (val.valueType == InteropValueType.INVALID) { return(false); } switch (op) { case "=": switch (val.valueType) { case InteropValueType.BOOL: if (bool.Parse(val.value) == bool.Parse(term)) { return(true); } else { return(false); } case InteropValueType.FLOAT: if (float.Parse(val.value) == float.Parse(term)) { return(true); } else { return(false); } case InteropValueType.INT: if (int.Parse(val.value) == int.Parse(term)) { return(true); } else { return(false); } case InteropValueType.STRING: if (val.value.ToLower() == term) { return(true); } else { return(false); } } break; case "!=": switch (val.valueType) { case InteropValueType.BOOL: if (bool.Parse(val.value) != bool.Parse(term)) { return(true); } else { return(false); } case InteropValueType.FLOAT: if (float.Parse(val.value) != float.Parse(term)) { return(true); } else { return(false); } case InteropValueType.INT: if (int.Parse(val.value) != int.Parse(term)) { return(true); } else { return(false); } case InteropValueType.STRING: if (val.value.ToLower() != term) { return(true); } else { return(false); } } break; case "<": switch (val.valueType) { case InteropValueType.FLOAT: if (float.Parse(val.value) < float.Parse(term)) { return(true); } else { return(false); } case InteropValueType.INT: if (int.Parse(val.value) < int.Parse(term)) { return(true); } else { return(false); } } break; case ">": switch (val.valueType) { case InteropValueType.FLOAT: if (float.Parse(val.value) > float.Parse(term)) { return(true); } else { return(false); } case InteropValueType.INT: if (int.Parse(val.value) > int.Parse(term)) { return(true); } else { return(false); } } break; case "<=": switch (val.valueType) { case InteropValueType.FLOAT: if (float.Parse(val.value) <= float.Parse(term)) { return(true); } else { return(false); } case InteropValueType.INT: if (int.Parse(val.value) <= int.Parse(term)) { return(true); } else { return(false); } } break; case ">=": switch (val.valueType) { case InteropValueType.FLOAT: if (float.Parse(val.value) >= float.Parse(term)) { return(true); } else { return(false); } case InteropValueType.INT: if (int.Parse(val.value) >= int.Parse(term)) { return(true); } else { return(false); } } break; case "<>": switch (val.valueType) { case InteropValueType.FLOAT: if (float.Parse(val.value) > float.Parse(term1) && float.Parse(val.value) < float.Parse(term2)) { return(true); } else { return(false); } case InteropValueType.INT: if (int.Parse(val.value) > int.Parse(term1) && int.Parse(val.value) < int.Parse(term1)) { return(true); } else { return(false); } } break; case "<=>": switch (val.valueType) { case InteropValueType.FLOAT: if (float.Parse(val.value) >= float.Parse(term1) && float.Parse(val.value) <= float.Parse(term2)) { return(true); } else { return(false); } case InteropValueType.INT: if (int.Parse(val.value) >= int.Parse(term1) && int.Parse(val.value) <= int.Parse(term1)) { return(true); } else { return(false); } } break; } return(false); } else { // if there are no "parts" to this block, then it must be just a simple part name or an alias if (block == part.name.ToLower()) { return(true); } if (block == TestFlightUtil.GetFullPartName(part).ToLower()) { return(true); } return(false); } }
protected void Log(string message) { message = String.Format("TestFlightReliability({0}[{1}]): {2}", Configuration, Configuration, message); TestFlightUtil.Log(message, this.part); }
public String FailureRateToMTBFString(float failureRate, TestFlightUtil.MTBFUnits units, bool shortForm, int maximum) { int currentUnit = (int)TestFlightUtil.MTBFUnits.SECONDS; float mtbf = FailureRateToMTBF(failureRate, (TestFlightUtil.MTBFUnits)currentUnit); while (mtbf > maximum) { currentUnit++; mtbf = FailureRateToMTBF(failureRate, (TestFlightUtil.MTBFUnits)currentUnit); if ((TestFlightUtil.MTBFUnits)currentUnit == TestFlightUtil.MTBFUnits.INVALID) break; } if (shortForm) { return String.Format("{0:F2}{1}", mtbf, UnitStringForMTBFUnit((TestFlightUtil.MTBFUnits)currentUnit)[0]); } else { return String.Format("{0:F2} {1}", mtbf, UnitStringForMTBFUnit((TestFlightUtil.MTBFUnits)currentUnit)); } }