Skip to content
Closed
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
197 changes: 162 additions & 35 deletions Packages/com.unity.inputsystem/InputSystem/Actions/InputActionAsset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1014,60 +1014,187 @@
{
if (parsedJson.version >= JsonVersion.Version1)
return;
if ((parsedJson.maps?.Length ?? 0) > 0 && (parsedJson.version) < JsonVersion.Version1)

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

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

Check warning on line 1029 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#L1029

Added line #L1029 was not covered by tests

for (int 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 processors = actionJson.processors;
if (string.IsNullOrEmpty(processors))
continue;

// Find top-level ';' token ranges without altering whitespace.
var tokenRanges = new List<(int start, int length)>();

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#L1039

Added line #L1039 was not covered by tests
{
var actionJson = mapJson.actions[ai];
var raw = actionJson.processors;
if (string.IsNullOrEmpty(raw))
int depth = 0, tokenStart = 0;
for (int i = 0; i < processors.Length; i++)
{
char c = processors[i];
if (c == '(')
depth++;
else if (c == ')')
depth = Math.Max(0, depth - 1);
else if (c == ';' && depth == 0)
{
int len = i - tokenStart;
if (len > 0)
tokenRanges.Add((tokenStart, len));

Check warning on line 1053 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-L1053

Added lines #L1041 - L1053 were not covered by tests

tokenStart = i + 1;
}
}
if (tokenStart < processors.Length)
{
int len = processors.Length - tokenStart;
if (len > 0)
tokenRanges.Add((tokenStart, len));
}
}

Check warning on line 1064 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#L1055-L1064

Added lines #L1055 - L1064 were not covered by tests

if (tokenRanges.Count == 0)
continue;

Check warning on line 1067 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#L1066-L1067

Added lines #L1066 - L1067 were not covered by tests

bool anyTokenChanged = false;
var rebuilt = new System.Text.StringBuilder(processors.Length + 16);
int cursor = 0;

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#L1069-L1071

Added lines #L1069 - L1071 were not covered by tests

foreach (var (start, length) in tokenRanges)
{
if (start > cursor)
rebuilt.Append(processors, cursor, start - cursor);

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#L1073-L1076

Added lines #L1073 - L1076 were not covered by tests

var tokenText = processors.Substring(start, length);
var trimmed = tokenText.Trim();

Check warning on line 1079 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-L1079

Added lines #L1078 - L1079 were not covered by tests

// If we can't parse, just write the original token back unchanged.
NameAndParameters nap;
try
{
nap = NameAndParameters.Parse(trimmed);
}
catch
{
rebuilt.Append(tokenText);
cursor = start + length;

Check warning on line 1090 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#L1084-L1090

Added lines #L1084 - L1090 were not covered by tests
continue;
}

var list = NameAndParameters.ParseMultiple(raw).ToList();
var rebuilt = new List<string>(list.Count);
foreach (var nap in list)
var procType = InputSystem.TryGetProcessor(nap.name);
if (procType == null || nap.parameters.Count == 0)

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#L1094-L1095

Added lines #L1094 - L1095 were not covered by tests
{
var procType = InputSystem.TryGetProcessor(nap.name);
if (nap.parameters.Count == 0 || procType == null)
{
rebuilt.Add(nap.ToString());
rebuilt.Append(tokenText);
cursor = start + length;
continue;

Check warning on line 1099 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-L1099

Added lines #L1097 - L1099 were not covered by tests
}

var enumFields = procType.GetFields(BindingFlags.Public | BindingFlags.Instance).Where(f => f.FieldType.IsEnum).ToArray();
if (enumFields.Length == 0)
{
rebuilt.Append(tokenText);
cursor = start + length;
continue;

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
}

int leadingWs = tokenText.Length - tokenText.TrimStart().Length;
int trailingWs = tokenText.Length - tokenText.TrimEnd().Length;
string core = tokenText.Substring(leadingWs, tokenText.Length - leadingWs - trailingWs);

Check warning on line 1112 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#L1110-L1112

Added lines #L1110 - L1112 were not covered by tests

bool changedThisToken = false;

Check warning on line 1114 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#L1114

Added line #L1114 was not covered by tests

foreach (var field in enumFields)
{
if (core.IndexOf(field.Name + "=", StringComparison.Ordinal) < 0)

Check warning on line 1118 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#L1116-L1118

Added lines #L1116 - L1118 were 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))
// Map ordinal -> underlying numeric values for the enum.
var values = Enum.GetValues(field.FieldType);
var numeric = new int[values.Length];
for (int i = 0; i < values.Length; i++)
numeric[i] = Convert.ToInt32(values.GetValue(i));

Check warning on line 1125 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#L1122-L1125

Added lines #L1122 - L1125 were not covered by tests

// Replace occurrences of "<Field>=<int>".
int search = 0;
while (true)

Check warning on line 1129 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-L1129

Added lines #L1128 - L1129 were not covered by tests
{
if (dict.TryGetValue(field.Name, out var ordS) && int.TryParse(ordS, out var ord))
int hit = core.IndexOf(field.Name + "=", search, StringComparison.Ordinal);
if (hit < 0) break;

Check warning on line 1132 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-L1132

Added lines #L1131 - L1132 were not covered by tests

int valStart = hit + field.Name.Length + 1;
int j = valStart;
bool neg = false;
if (j < core.Length && core[j] == '-')

Check warning on line 1137 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#L1134-L1137

Added lines #L1134 - L1137 were not covered by tests
{
var values = Enum.GetValues(field.FieldType).Cast<object>().ToArray();
if (ord >= 0 && ord < values.Length)
neg = true;
j++;
}

Check warning on line 1141 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#L1139-L1141

Added lines #L1139 - L1141 were not covered by tests

int valEnd = j;
while (valEnd < core.Length && char.IsDigit(core[valEnd]))
valEnd++;

Check warning on line 1145 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#L1143-L1145

Added lines #L1143 - L1145 were not covered by tests

if (valEnd == j)
{
search = valStart;
continue;

Check warning on line 1150 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#L1147-L1150

Added lines #L1147 - L1150 were not covered by tests
}

var numStr = core.Substring(valStart, valEnd - valStart);
if (int.TryParse(neg ? "-" + numStr : numStr, out var parsed))
{
if (parsed >= 0 && parsed < numeric.Length)

Check warning on line 1156 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#L1153-L1156

Added lines #L1153 - L1156 were not covered by tests
{
dict[field.Name] = Convert.ToInt32(values[ord]).ToString();
anyChanged = true;
int underlying = numeric[parsed];
if (underlying != parsed)
{
core = core.Substring(0, valStart) + underlying.ToString() + core.Substring(valEnd);
changedThisToken = true;
search = valStart + underlying.ToString().Length;
continue;

Check warning on line 1164 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#L1158-L1164

Added lines #L1158 - L1164 were not covered by tests
}
}
}
}

if (!anyChanged)
{
rebuilt.Add(nap.ToString());
}
else
{
var paramText = string.Join(",", dict.Select(kv => $"{kv.Key}={kv.Value}"));
rebuilt.Add($"{nap.name}({paramText})");
search = valEnd;

Check warning on line 1169 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#L1169

Added line #L1169 was not covered by tests
}
}

actionJson.processors = string.Join(";", rebuilt);
mapJson.actions[ai] = actionJson;
if (changedThisToken)
{
anyTokenChanged = true;
rebuilt.Append(tokenText.Substring(0, leadingWs));
rebuilt.Append(core);
rebuilt.Append(tokenText.Substring(tokenText.Length - trailingWs, trailingWs));
}

Check warning on line 1179 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#L1173-L1179

Added lines #L1173 - L1179 were not covered by tests
else
{
rebuilt.Append(tokenText);
}

Check warning on line 1183 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#L1181-L1183

Added lines #L1181 - L1183 were not covered by tests

cursor = start + length;

Check warning on line 1185 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#L1185

Added line #L1185 was not covered by tests
}
parsedJson.maps[mi] = mapJson;

if (cursor < processors.Length)
rebuilt.Append(processors, cursor, processors.Length - cursor);

Check warning on line 1189 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#L1188-L1189

Added lines #L1188 - L1189 were not covered by tests

if (anyTokenChanged)
actionJson.processors = rebuilt.ToString();

Check warning on line 1192 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#L1191-L1192

Added lines #L1191 - L1192 were not covered by tests

mapJson.actions[ai] = actionJson;

Check warning on line 1194 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#L1194

Added line #L1194 was not covered by tests
}

maps[mi] = mapJson;
}
// Bump the version so we never re-migrate
parsedJson.version = JsonVersion.Version1;
Expand Down
Loading