Skip to content

Commit

Permalink
Merge pull request #1319 from Dunbaratu/fixes_1313_waypoint_message
Browse files Browse the repository at this point in the history
Fixes #1313 (waypoints).
  • Loading branch information
erendrake committed Dec 18, 2015
2 parents 026ceaa + ec422f7 commit f9fc4e0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
26 changes: 16 additions & 10 deletions src/kOS/Function/Suffixed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -472,24 +472,30 @@ public override void Execute(SharedObjects shared)
AssertArgBottomAndConsume(shared);

WaypointManager wpm = WaypointManager.Instance();
if (wpm == null) // When zero waypoints exist, there might not even be a waypoint manager.
{
ReturnValue = null;
// I don't like returning null here without the user being able to test for that, but
// we don't have another way to communicate "no such waypoint". We really need to address
// that problem once and for all.
return;
}

// If no contracts have been generated with waypoints in them,
// then sometimes the stock game's waypoint manager doesn't even
// exist yet either. (The base game seems not to instance one until the
// first time a contract with a waypoint is created).
if (wpm == null)
throw new KOSInvalidArgumentException("waypoint", "\""+pointName+"\"", "no waypoints exist");

string baseName;
int index;
bool hasGreek = WaypointValue.GreekToInteger(pointName, out index, out baseName);
if (hasGreek)
pointName = baseName;
Waypoint point = wpm.AllWaypoints().FirstOrDefault(
p => String.Equals(p.name, baseName,StringComparison.CurrentCultureIgnoreCase) && (!hasGreek || p.index == index));
p => String.Equals(p.name, pointName,StringComparison.CurrentCultureIgnoreCase) && (!hasGreek || p.index == index));

// We can't communicate the concept of a lookup fail to the script in a way it can catch (can't do
// nulls), so bomb out here:
if (point ==null)
throw new KOSInvalidArgumentException("waypoint", "\""+pointName+"\"", "no such waypoint");

ReturnValue = new WaypointValue(point, shared);
}
}
}

[Function("transferall")]
public class FunctionTransferAll : FunctionBase
Expand Down
16 changes: 9 additions & 7 deletions src/kOS/Suffixed/WaypointValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@ public WaypointValue(Waypoint wayPoint, SharedObjects shared)
WrappedWaypoint = wayPoint;
Shared = shared;
InitializeSuffixes();

// greekMap is static, so whichever waypoint instance's constructor happens to
// get called first will make it, and from then on other waypoints don't need to
// keep re-initializing it:
if (greekMap == null)
InitializeGreekMap();
}

private void InitializeSuffixes()
Expand Down Expand Up @@ -128,10 +122,18 @@ public string ToVerboseString()
/// </summary>
/// <param name="greekLetterName">string name to check. Case insensitively.</param>
/// <param name="index">integer position in alphabet. -1 if no match.</param>
/// <param name="baseName">the name after the greek suffix has been stripped off, if there is one.</param>
/// <param name="baseName">the name after the last term has been stripped off, if there are
/// space separated terms. Note that if the return value of this method is false, this
/// shouldn't be used and you should stick with the original full name.</param>
/// <returns>true if there was a greek letter suffix</returns>
public static bool GreekToInteger(string greekLetterName, out int index, out string baseName )
{
// greekMap is static, and we only need to populate it once in
// the lifetime of the KSP process. We'll do so the first time
// this method (the only one that uses it) gets called:
if (greekMap == null)
InitializeGreekMap();

// Get lastmost word (or whole string if there's no spaces):
int lastSpace = greekLetterName.LastIndexOf(' ');
string lastTerm;
Expand Down

0 comments on commit f9fc4e0

Please sign in to comment.