Skip to content

Commit

Permalink
Fix #128: make agmemo system disregard whitespace around the = charac…
Browse files Browse the repository at this point in the history
…ter, and remove the tracking of agmemo per vesselmodule

-the description in the vesselmodule was assigned in the RPMComputer's Start method, so basically arbitrarily.
-Keeping the description only in the RPMComputer means that different parts on the same vessel could see differnt AG memos if they came from different craft files.
  • Loading branch information
JonnyOThan committed Aug 6, 2024
1 parent 9cbde4c commit e343c57
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 54 deletions.
5 changes: 2 additions & 3 deletions RasterPropMonitor/Core/RPMCEvaluators.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,9 @@ internal VariableEvaluator GetEvaluator(string input, out VariableUpdateType upd
updateType = VariableUpdateType.Constant;
if (uint.TryParse(input.Substring("AGMEMO".Length), out uint groupID))
{
RPMVesselComputer vesselComputer = RPMVesselComputer.Instance(vessel);
// if the memo contains a pipe character, the string changes depending on the state of the action group
string[] tokens;
if (vesselComputer.actionGroupMemo[groupID].IndexOf('|') > 1 && (tokens = vesselComputer.actionGroupMemo[groupID].Split('|')).Length == 2)
if (actionGroupMemo[groupID].IndexOf('|') > 1 && (tokens = actionGroupMemo[groupID].Split('|')).Length == 2)
{
return (RPMVesselComputer comp) =>
{
Expand All @@ -184,7 +183,7 @@ internal VariableEvaluator GetEvaluator(string input, out VariableUpdateType upd
}
else
{
return (RPMVesselComputer comp) => comp.actionGroupMemo[groupID];
return (RPMVesselComputer comp) => actionGroupMemo[groupID];
}
}
else
Expand Down
34 changes: 1 addition & 33 deletions RasterPropMonitor/Core/RPMVesselComputer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,7 @@ public partial class RPMVesselComputer : VesselModule
BaseAction.GetGroupIndex(KSPActionGroup.Custom08),
BaseAction.GetGroupIndex(KSPActionGroup.Custom09)
};
internal readonly string[] actionGroupMemo = {
"AG0",
"AG1",
"AG2",
"AG3",
"AG4",
"AG5",
"AG6",
"AG7",
"AG8",
"AG9"
};

private const float gee = 9.81f;
private readonly double upperAtmosphereLimit = Math.Log(100000.0);
#endregion
Expand Down Expand Up @@ -648,27 +637,6 @@ public void UpdateVariables()
#endregion

#region Interface Methods
/// <summary>
/// Initialize vessel description-based values.
/// </summary>
/// <param name="vesselDescription"></param>
internal void SetVesselDescription(string vesselDescription)
{
string[] descriptionStrings = vesselDescription.UnMangleConfigText().Split(JUtil.LineSeparator, StringSplitOptions.None);
for (int i = 0; i < descriptionStrings.Length; i++)
{
if (descriptionStrings[i].StartsWith("AG", StringComparison.Ordinal) && descriptionStrings[i][3] == '=')
{
uint groupID;
if (uint.TryParse(descriptionStrings[i][2].ToString(), out groupID))
{
actionGroupMemo[groupID] = descriptionStrings[i].Substring(4).Trim();
descriptionStrings[i] = string.Empty;
}
}
}
}

/// <summary>
/// Set the refresh rate (number of Update() calls per triggered update).
/// The lower of the current data rate and the new data rate is used.
Expand Down
41 changes: 23 additions & 18 deletions RasterPropMonitor/Core/RasterPropMonitorComputer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
****************************************************************************/
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using UnityEngine;
using UnityEngine.Diagnostics;
using UnityEngine.Profiling;
Expand All @@ -41,17 +42,26 @@ public partial class RasterPropMonitorComputer : PartModule
[KSPField]
public string triggeredEvents = string.Empty;

// Yes, it's a really braindead way of doing it, but I ran out of elegant ones,
// because nothing appears to work as documented -- IF it's documented.
// This one is sure to work and isn't THAT much of a performance drain, really.
// Pull requests welcome
// Vessel description storage and related code.
[KSPField(isPersistant = true)]
public string vesselDescription = string.Empty;
private string vesselDescriptionForDisplay = string.Empty;
private readonly string editorNewline = ((char)0x0a).ToString();
private static readonly string editorNewline = ((char)0x0a).ToString();
private string lastVesselDescription = string.Empty;

internal readonly string[] actionGroupMemo = {
"AG0",
"AG1",
"AG2",
"AG3",
"AG4",
"AG5",
"AG6",
"AG7",
"AG8",
"AG9"
};

internal List<string> storedStringsArray = new List<string>();

// Local variables
Expand Down Expand Up @@ -102,6 +112,8 @@ internal PeriodicRandomValue(int period_)
private ExternalVariableHandlers plugins = null;
internal Dictionary<string, Color32> overrideColors = new Dictionary<string, Color32>();

static readonly Regex x_agmemoRegex = new Regex("^AG([0-9])\\s*=\\s*(.*)\\s*");

public static RasterPropMonitorComputer FindFromProp(InternalProp prop)
{
var rpmc = prop.part.FindModuleImplementing<RasterPropMonitorComputer>();
Expand Down Expand Up @@ -365,25 +377,18 @@ public void Start()

plugins = new ExternalVariableHandlers(part);

RPMVesselComputer comp = RPMVesselComputer.Instance(vessel);
if (!string.IsNullOrEmpty(vesselDescription))
{
comp.SetVesselDescription(vesselDescription);
}

// Make sure we have the description strings parsed.
string[] descriptionStrings = vesselDescription.UnMangleConfigText().Split(JUtil.LineSeparator, StringSplitOptions.None);
for (int i = 0; i < descriptionStrings.Length; i++)
{
if (descriptionStrings[i].StartsWith("AG", StringComparison.Ordinal) && descriptionStrings[i][3] == '=')
var matches = x_agmemoRegex.Matches(descriptionStrings[i]);
if (matches.Count == 2 && uint.TryParse(matches[0].Value, out uint groupID) && groupID < actionGroupMemo.Length)
{
uint groupID;
if (uint.TryParse(descriptionStrings[i][2].ToString(), out groupID))
{
descriptionStrings[i] = string.Empty;
}
descriptionStrings[i] = string.Empty;
actionGroupMemo[groupID] = matches[1].Value;
}
}

vesselDescriptionForDisplay = string.Join(Environment.NewLine, descriptionStrings).MangleConfigText();
if (string.IsNullOrEmpty(vesselDescriptionForDisplay))
{
Expand Down Expand Up @@ -537,7 +542,7 @@ public void Update()
{
lastVesselDescription = s;
// For some unclear reason, the newline in this case is always 0A, rather than Environment.NewLine.
vesselDescription = s.Replace(editorNewline, "$$$");
vesselDescription = s.MangleConfigText();
}
}
else
Expand Down

0 comments on commit e343c57

Please sign in to comment.