Skip to content

Commit

Permalink
Fix parameters in manialink actions returning last parameter two time… (
Browse files Browse the repository at this point in the history
  • Loading branch information
snixtho authored Nov 13, 2023
1 parent a83f85e commit 3284c6b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
26 changes: 9 additions & 17 deletions src/EvoSC.Manialinks/ManialinkActionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -336,22 +336,23 @@ public void RemoveRoute(string route)
/// <param name="previousComponent">The previous component from the recursive call.</param>
/// <param name="currentNode">The current working route node.</param>
/// <returns></returns>
private (IManialinkAction?, IMlRouteNode?) FindActionInternal(string[] nextComponents, string previousComponent, IMlRouteNode currentNode)
private (IManialinkAction?, IMlRouteNode?) FindActionInternal(string[] nextComponents, IMlRouteNode currentNode)
{
if (nextComponents.Length == 0 || currentNode.Children == null)
{
if (nextComponents.Length > 0)
if (nextComponents.Length > 1)
{
// reached end of current branch, but did not find the action
return (null, null);
}

// reached end of current branch and found an action
return (currentNode.Action,
new MlRouteNode(previousComponent) {Action = currentNode.Action, IsParameter = currentNode.IsParameter});
new MlRouteNode(nextComponents.First()) {Action = currentNode.Action, IsParameter = currentNode.IsParameter});
}

var currentComponent = nextComponents.First();
var nextComponent = nextComponents.Skip(1).First();
var pathNode = new MlRouteNode(currentComponent)
{
Children = new Dictionary<string, IMlRouteNode>(),
Expand All @@ -360,12 +361,12 @@ public void RemoveRoute(string route)

foreach (var child in currentNode.Children.Values)
{
if (!child.IsParameter && !child.Name.Equals(currentComponent, StringComparison.Ordinal))
if (!child.IsParameter && !child.Name.Equals(nextComponent, StringComparison.Ordinal))
{
continue;
}

var (manialinkAction, nextNode) = FindActionInternal(nextComponents[1..], currentComponent, child);
var (manialinkAction, nextNode) = FindActionInternal(nextComponents[1..], child);

if (nextNode != null)
{
Expand All @@ -390,25 +391,16 @@ public void RemoveRoute(string route)

lock (_rootNodeMutex)
{
foreach (var child in _rootNode.Children.Values)
foreach (var rootComponent in _rootNode.Children.Values)
{
var (manialinkAction, path) = FindActionInternal(routeComponents[1..], routeComponents[0], child);
var (manialinkAction, path) = FindActionInternal(routeComponents, rootComponent);

if (manialinkAction == null || path == null)
{
// action not found, try next root node
continue;
}

var pathNode = new MlRouteNode(routeComponents[0])
{
Children = new Dictionary<string, IMlRouteNode>(),
IsParameter = path.IsParameter
};

pathNode.Children.Add(path.Name, path);

return (manialinkAction, pathNode);
return (manialinkAction, path);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,14 @@ public void Test_Route_Registration_With_Action_Parameters()

Assert.NotNull(action);
Assert.NotNull(route);


Assert.NotNull(route.Children?.Values.FirstOrDefault()?.Children?.Values.FirstOrDefault()?.Children?.Values.FirstOrDefault());

Assert.True(route.Children.Values.First().Children.Values.First().IsParameter);
Assert.True(route.Children.Values.First().Children.Values.First().Children.Values.First().IsParameter);
Assert.Equal("1", route.Children.Values.First().Children.Values.First().Name);
Assert.Equal("2", route.Children.Values.First().Children.Values.First().Children.Values.First().Name);
}

[Fact]
Expand Down

0 comments on commit 3284c6b

Please sign in to comment.