Skip to content
Open
Changes from all 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
129 changes: 97 additions & 32 deletions Packages/com.unity.inputsystem/InputSystem/Actions/InputActionAsset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEngine.InputSystem.Editor;
using System.Globalization;
using UnityEngine.InputSystem.Utilities;

////TODO: make the FindAction logic available on any IEnumerable<InputAction> and IInputActionCollection via extension methods
Expand Down Expand Up @@ -1014,60 +1014,125 @@
{
if (parsedJson.version >= JsonVersion.Version1)
return;
if ((parsedJson.maps?.Length ?? 0) > 0 && (parsedJson.version) < JsonVersion.Version1)

if (parsedJson.maps == null || parsedJson.maps.Length == 0)
{
parsedJson.version = JsonVersion.Version1;
return;
}

for (var mi = 0; mi < parsedJson.maps.Length; ++mi)
{
for (var mi = 0; mi < parsedJson.maps.Length; ++mi)
var mapJson = parsedJson.maps[mi];
if (mapJson.actions == null || mapJson.actions.Length == 0)
continue;

Check warning on line 1028 in Packages/com.unity.inputsystem/InputSystem/Actions/InputActionAsset.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Packages/com.unity.inputsystem/InputSystem/Actions/InputActionAsset.cs#L1028

Added line #L1028 was not covered by tests

for (var ai = 0; ai < mapJson.actions.Length; ++ai)
{
var mapJson = parsedJson.maps[mi];
for (var ai = 0; ai < mapJson.actions.Length; ++ai)
var actionJson = mapJson.actions[ai];
var raw = actionJson.processors;
if (string.IsNullOrEmpty(raw))
continue;

var parts = System.Text.RegularExpressions.Regex.Split(raw, @"\s*([,;])\s*");
if (parts.Length == 0)
continue;

Check warning on line 1039 in Packages/com.unity.inputsystem/InputSystem/Actions/InputActionAsset.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Packages/com.unity.inputsystem/InputSystem/Actions/InputActionAsset.cs#L1037-L1039

Added lines #L1037 - L1039 were not covered by tests

var tokens = new List<string>();
for (int i = 0; i < parts.Length; i += 2)
if (!string.IsNullOrEmpty(parts[i]))
tokens.Add(parts[i]);

Check warning on line 1044 in Packages/com.unity.inputsystem/InputSystem/Actions/InputActionAsset.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Packages/com.unity.inputsystem/InputSystem/Actions/InputActionAsset.cs#L1041-L1044

Added lines #L1041 - L1044 were not covered by tests

if (tokens.Count == 0)
continue;

Check warning on line 1047 in Packages/com.unity.inputsystem/InputSystem/Actions/InputActionAsset.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Packages/com.unity.inputsystem/InputSystem/Actions/InputActionAsset.cs#L1046-L1047

Added lines #L1046 - L1047 were not covered by tests

var parsed = new List<NameAndParameters>(tokens.Count);
foreach (var t in tokens)
parsed.Add(NameAndParameters.Parse(t));

Check warning on line 1051 in Packages/com.unity.inputsystem/InputSystem/Actions/InputActionAsset.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Packages/com.unity.inputsystem/InputSystem/Actions/InputActionAsset.cs#L1049-L1051

Added lines #L1049 - L1051 were not covered by tests

var rebuiltTokens = new List<string>(tokens.Count);
var anyProcessorChanged = false;

Check warning on line 1054 in Packages/com.unity.inputsystem/InputSystem/Actions/InputActionAsset.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Packages/com.unity.inputsystem/InputSystem/Actions/InputActionAsset.cs#L1053-L1054

Added lines #L1053 - L1054 were not covered by tests

for (int pi = 0; pi < parsed.Count; pi++)

Check warning on line 1056 in Packages/com.unity.inputsystem/InputSystem/Actions/InputActionAsset.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Packages/com.unity.inputsystem/InputSystem/Actions/InputActionAsset.cs#L1056

Added line #L1056 was not covered by tests
{
var actionJson = mapJson.actions[ai];
var raw = actionJson.processors;
if (string.IsNullOrEmpty(raw))
var nap = parsed[pi];

Check warning on line 1058 in Packages/com.unity.inputsystem/InputSystem/Actions/InputActionAsset.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Packages/com.unity.inputsystem/InputSystem/Actions/InputActionAsset.cs#L1058

Added line #L1058 was not covered by tests

var procType = InputSystem.TryGetProcessor(nap.name);
if (procType == null || nap.parameters.Count == 0)
{
rebuiltTokens.Add(tokens[pi]);

Check warning on line 1063 in Packages/com.unity.inputsystem/InputSystem/Actions/InputActionAsset.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Packages/com.unity.inputsystem/InputSystem/Actions/InputActionAsset.cs#L1060-L1063

Added lines #L1060 - L1063 were not covered by tests
continue;
}

var dict = new Dictionary<string, string>(nap.parameters.Count, System.StringComparer.OrdinalIgnoreCase);
foreach (var p in nap.parameters)
dict[p.name] = p.value.ToString();

Check warning on line 1069 in Packages/com.unity.inputsystem/InputSystem/Actions/InputActionAsset.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Packages/com.unity.inputsystem/InputSystem/Actions/InputActionAsset.cs#L1067-L1069

Added lines #L1067 - L1069 were not covered by tests

var changedThisProcessor = false;

Check warning on line 1071 in Packages/com.unity.inputsystem/InputSystem/Actions/InputActionAsset.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Packages/com.unity.inputsystem/InputSystem/Actions/InputActionAsset.cs#L1071

Added line #L1071 was not covered by tests

var list = NameAndParameters.ParseMultiple(raw).ToList();
var rebuilt = new List<string>(list.Count);
foreach (var nap in list)
foreach (var field in procType.GetFields(BindingFlags.Public | BindingFlags.Instance))

Check warning on line 1073 in Packages/com.unity.inputsystem/InputSystem/Actions/InputActionAsset.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Packages/com.unity.inputsystem/InputSystem/Actions/InputActionAsset.cs#L1073

Added line #L1073 was not covered by tests
{
var procType = InputSystem.TryGetProcessor(nap.name);
if (nap.parameters.Count == 0 || procType == null)
{
rebuilt.Add(nap.ToString());
if (!field.FieldType.IsEnum)
continue;

Check warning on line 1076 in Packages/com.unity.inputsystem/InputSystem/Actions/InputActionAsset.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Packages/com.unity.inputsystem/InputSystem/Actions/InputActionAsset.cs#L1075-L1076

Added lines #L1075 - L1076 were not covered by tests

if (!dict.TryGetValue(field.Name, out var rawVal))

Check warning on line 1078 in Packages/com.unity.inputsystem/InputSystem/Actions/InputActionAsset.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Packages/com.unity.inputsystem/InputSystem/Actions/InputActionAsset.cs#L1078

Added line #L1078 was not covered by tests
continue;
}

var dict = nap.parameters.ToDictionary(p => p.name, p => p.value.ToString());
var anyChanged = false;
foreach (var field in procType.GetFields(BindingFlags.Public | BindingFlags.Instance).Where(f => f.FieldType.IsEnum))
if (int.TryParse(rawVal, NumberStyles.Integer, CultureInfo.InvariantCulture, out var n))

Check warning on line 1081 in Packages/com.unity.inputsystem/InputSystem/Actions/InputActionAsset.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Packages/com.unity.inputsystem/InputSystem/Actions/InputActionAsset.cs#L1081

Added line #L1081 was not covered by tests
{
if (dict.TryGetValue(field.Name, out var ordS) && int.TryParse(ordS, out var ord))
var values = System.Enum.GetValues(field.FieldType);
var looksLikeOrdinal = n >= 0 && n < values.Length && !System.Enum.IsDefined(field.FieldType, n);
if (looksLikeOrdinal)

Check warning on line 1085 in Packages/com.unity.inputsystem/InputSystem/Actions/InputActionAsset.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Packages/com.unity.inputsystem/InputSystem/Actions/InputActionAsset.cs#L1083-L1085

Added lines #L1083 - L1085 were not covered by tests
{
var values = Enum.GetValues(field.FieldType).Cast<object>().ToArray();
if (ord >= 0 && ord < values.Length)
var underlying = Convert.ToInt32(values.GetValue(n));
if (underlying != n)

Check warning on line 1088 in Packages/com.unity.inputsystem/InputSystem/Actions/InputActionAsset.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Packages/com.unity.inputsystem/InputSystem/Actions/InputActionAsset.cs#L1087-L1088

Added lines #L1087 - L1088 were not covered by tests
{
dict[field.Name] = Convert.ToInt32(values[ord]).ToString();
anyChanged = true;
dict[field.Name] = underlying.ToString(CultureInfo.InvariantCulture);
changedThisProcessor = true;

Check warning on line 1091 in Packages/com.unity.inputsystem/InputSystem/Actions/InputActionAsset.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Packages/com.unity.inputsystem/InputSystem/Actions/InputActionAsset.cs#L1090-L1091

Added lines #L1090 - L1091 were not covered by tests
}
}
}
}

Check warning on line 1095 in Packages/com.unity.inputsystem/InputSystem/Actions/InputActionAsset.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Packages/com.unity.inputsystem/InputSystem/Actions/InputActionAsset.cs#L1095

Added line #L1095 was not covered by tests

if (!changedThisProcessor)
{
rebuiltTokens.Add(tokens[pi]);
}

Check warning on line 1100 in Packages/com.unity.inputsystem/InputSystem/Actions/InputActionAsset.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Packages/com.unity.inputsystem/InputSystem/Actions/InputActionAsset.cs#L1097-L1100

Added lines #L1097 - L1100 were not covered by tests
else
{
var ordered = nap.parameters.Select(p =>
{
var v = dict.TryGetValue(p.name, out var nv) ? nv : p.value.ToString();
return $"{p.name}={v}";
});

Check warning on line 1107 in Packages/com.unity.inputsystem/InputSystem/Actions/InputActionAsset.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Packages/com.unity.inputsystem/InputSystem/Actions/InputActionAsset.cs#L1102-L1107

Added lines #L1102 - L1107 were not covered by tests

var migrated = $"{nap.name}({string.Join(",", ordered)})";
rebuiltTokens.Add(migrated);
anyProcessorChanged = true;
}
}

Check warning on line 1113 in Packages/com.unity.inputsystem/InputSystem/Actions/InputActionAsset.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Packages/com.unity.inputsystem/InputSystem/Actions/InputActionAsset.cs#L1109-L1113

Added lines #L1109 - L1113 were not covered by tests

if (!anyChanged)
if (anyProcessorChanged)
{
var sb = new System.Text.StringBuilder(raw.Length + 16);
int tokenIndex = 0;
for (int partIndex = 0; partIndex < parts.Length; ++partIndex)
{
if ((partIndex % 2) == 0)

Check warning on line 1121 in Packages/com.unity.inputsystem/InputSystem/Actions/InputActionAsset.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Packages/com.unity.inputsystem/InputSystem/Actions/InputActionAsset.cs#L1115-L1121

Added lines #L1115 - L1121 were not covered by tests
{
rebuilt.Add(nap.ToString());
if (tokenIndex < rebuiltTokens.Count)
sb.Append(rebuiltTokens[tokenIndex++]);

Check warning on line 1124 in Packages/com.unity.inputsystem/InputSystem/Actions/InputActionAsset.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Packages/com.unity.inputsystem/InputSystem/Actions/InputActionAsset.cs#L1123-L1124

Added lines #L1123 - L1124 were not covered by tests
}
else
{
var paramText = string.Join(",", dict.Select(kv => $"{kv.Key}={kv.Value}"));
rebuilt.Add($"{nap.name}({paramText})");
sb.Append(parts[partIndex]);

Check warning on line 1128 in Packages/com.unity.inputsystem/InputSystem/Actions/InputActionAsset.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Packages/com.unity.inputsystem/InputSystem/Actions/InputActionAsset.cs#L1128

Added line #L1128 was not covered by tests
}
}

actionJson.processors = string.Join(";", rebuilt);
mapJson.actions[ai] = actionJson;
actionJson.processors = sb.ToString();

Check warning on line 1131 in Packages/com.unity.inputsystem/InputSystem/Actions/InputActionAsset.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Packages/com.unity.inputsystem/InputSystem/Actions/InputActionAsset.cs#L1131

Added line #L1131 was not covered by tests
}
parsedJson.maps[mi] = mapJson;
mapJson.actions[ai] = actionJson;

Check warning on line 1133 in Packages/com.unity.inputsystem/InputSystem/Actions/InputActionAsset.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Packages/com.unity.inputsystem/InputSystem/Actions/InputActionAsset.cs#L1133

Added line #L1133 was not covered by tests
}
parsedJson.maps[mi] = mapJson;
}
// Bump the version so we never re-migrate
parsedJson.version = JsonVersion.Version1;
Expand Down
Loading