Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions kOS.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@
<Company></Company>
<Version>1.5.1.0</Version>
</PropertyGroup>
<PropertyGroup>
<KSPRoot>E:\Games\Kerbal Space Program RSS</KSPRoot>
</PropertyGroup>
Comment thread
AntonKuzin marked this conversation as resolved.
Outdated
</Project>
4 changes: 2 additions & 2 deletions src/kOS/AddOns/RemoteTech/RemoteTechAntennaModuleFields.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using kOS.Safe.Encapsulation;
using kOS.Safe.Encapsulation;
using kOS.Safe.Encapsulation.Suffixes;
using kOS.Safe.Exceptions;
using kOS.Suffixed;
Expand Down Expand Up @@ -113,7 +113,7 @@ protected override ListValue AllFieldNames()
// just print the guid if we can't figure out what it is
return new StringValue(guid.ToString());
}
return base.GetKSPFieldValue(suffixName);
return base.GetKSPFieldValue(suffixName, base.FieldIsVisible);
}

private Guid GetTargetGuid(Structure target)
Expand Down
55 changes: 44 additions & 11 deletions src/kOS/Suffixed/PartModuleField/PartModuleFields.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,27 @@ protected virtual ListValue AllFields(string formatter)
return returnValue;
}

/// <summary>
/// Return a list of all the strings of all KSPfields registered to this PartModule
/// which are currently NOT showing on the part's RMB menu.
/// </summary>
/// <returns>List of all the strings field names.</returns>
protected ListValue AllHiddenFields(string formatter)
{
var returnValue = new ListValue();

IEnumerable<BaseField> hiddenFields = partModule.Fields.Cast<BaseField>().Where((field) => !FieldIsVisible(field));

foreach (BaseField field in hiddenFields)
{
returnValue.Add(new StringValue(string.Format(formatter,
"get-only",
GetFieldName(field).ToLower(),
Utilities.Utils.KOSType(field.FieldInfo.FieldType))));
}
return returnValue;
}

/// <summary>
/// Return a list of all the strings of all KSPfields registered to this PartModule
/// which are currently showing on the part's RMB menu, without formating.
Expand All @@ -225,15 +246,26 @@ protected virtual ListValue AllFieldNames()
/// <returns>true if it is on the PartModule, false if it is not</returns>
public virtual BooleanValue HasField(StringValue fieldName)
{
return FieldIsVisible(GetField(fieldName));
return FieldIsVisible(GetField(fieldName, FieldIsVisible));
}

/// <summary>
/// Determine if the Partmodule has this KSPField on it, which is currently
/// NOT showing on the part's RMB menu:
/// </summary>
/// <param name="fieldName">The field to search for</param>
/// <returns>true if it is on the PartModule, false if it is not</returns>
public virtual BooleanValue HasHiddenField(StringValue fieldName)
{
return GetField(fieldName, field => !FieldIsVisible(field)) != null;
}

/// <summary>
/// Return the field itself that goes with the name (the BaseField, not the value).
/// </summary>
/// <param name="cookedGuiName">The case-insensitive guiName (or name if guiname is empty) of the field.</param>
/// <returns>a BaseField - a KSP type that can be used to get the value, or its GUI name or its reflection info.</returns>
protected BaseField GetField(string cookedGuiName)
protected BaseField GetField(string cookedGuiName, Func<BaseField, bool> visibilityFilterPredicate)
{
// Conceptually this should be a single hit using FirstOrDefault(), because there should only
// be one Field with the given GUI name. But Issue #2666 forced kOS to change it to an array of hits
Expand All @@ -251,7 +283,7 @@ protected BaseField GetField(string cookedGuiName)
// Issue #2666 is handled here. kOS should not return the invisible field when there's
// a visible one of the same name it could have picked instead. Only return an invisible
// field if there's no visible one to pick.
BaseField preferredMatch = allMatches.FirstOrDefault(field => FieldIsVisible(field));
BaseField preferredMatch = allMatches.FirstOrDefault(visibilityFilterPredicate);
return preferredMatch ?? allMatches.First();
}

Expand Down Expand Up @@ -413,18 +445,21 @@ private void InitializeSuffixesAfterConstruction()
AddSuffix("ALLACTIONS", new Suffix<ListValue>(() => AllActions("({0}) {1}, is {2}")));
AddSuffix("ALLACTIONNAMES", new Suffix<ListValue>(AllActionNames));
AddSuffix("HASACTION", new OneArgsSuffix<BooleanValue, StringValue>(HasAction));
AddSuffix("GETFIELD", new OneArgsSuffix<Structure, StringValue>(GetKSPFieldValue));
AddSuffix("GETFIELD", new OneArgsSuffix<Structure, StringValue>(argument => GetKSPFieldValue(argument, field => FieldIsVisible(field))));
AddSuffix("SETFIELD", new TwoArgsSuffix<StringValue, Structure>(SetKSPFieldValue));
AddSuffix("DOEVENT", new OneArgsSuffix<StringValue>(CallKSPEvent));
AddSuffix("DOACTION", new TwoArgsSuffix<StringValue, BooleanValue>(CallKSPAction));
AddSuffix("ALLHIDDENFIELDS", new Suffix<ListValue>(() => AllHiddenFields("({0}) {1}, is {2}")));
Comment thread
AntonKuzin marked this conversation as resolved.
AddSuffix("HASHIDDENFIELD", new OneArgsSuffix<BooleanValue, StringValue>(HasHiddenField));
AddSuffix("GETHIDDENFIELD", new OneArgsSuffix<Structure, StringValue>(argument => GetKSPFieldValue(argument, field => !FieldIsVisible(field))));
}

private static bool FieldIsVisible(BaseField field)
protected bool FieldIsVisible(BaseField field)
{
return (field != null) && (HighLogic.LoadedSceneIsEditor ? field.guiActiveEditor : field.guiActive);
}

private static bool EventIsVisible(BaseEvent evt)
private bool EventIsVisible(BaseEvent evt)
{
return (evt != null) && (
(HighLogic.LoadedSceneIsEditor ? evt.guiActiveEditor : evt.guiActive) &&
Expand All @@ -438,13 +473,11 @@ private static bool EventIsVisible(BaseEvent evt)
/// </summary>
/// <param name="suffixName"></param>
/// <returns></returns>
protected Structure GetKSPFieldValue(StringValue suffixName)
protected Structure GetKSPFieldValue(StringValue suffixName, Func<BaseField, bool> visibilityFilterPredicate)
{
BaseField field = GetField(suffixName);
BaseField field = GetField(suffixName, visibilityFilterPredicate);
if (field == null)
throw new KOSLookupFailException("FIELD", suffixName, this);
if (!FieldIsVisible(field))
throw new KOSLookupFailException("FIELD", suffixName, this, true);
Structure obj = FromPrimitiveWithAssert(field.GetValue(partModule));
return obj;
}
Expand All @@ -457,7 +490,7 @@ protected Structure GetKSPFieldValue(StringValue suffixName)
protected virtual void SetKSPFieldValue(StringValue suffixName, Structure newValue)
{
ThrowIfNotCPUVessel();
BaseField field = GetField(suffixName);
BaseField field = GetField(suffixName, FieldIsVisible);
if (field == null)
throw new KOSLookupFailException("FIELD", suffixName, this);
if (!FieldIsVisible(field))
Expand Down