Skip to content

FIX: Custom processors serialises enum by index rather than by value. #2164

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 35 commits into from
Jul 2, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
6504c53
Added test to cover the fix
AswinRajGopal Apr 7, 2025
e9cff62
Determine index from enum value and serialise accordingly for the asset.
AswinRajGopal Apr 7, 2025
7284150
Fix test failure after fix, had to focus the dropdown field before ch…
AswinRajGopal Apr 17, 2025
73890bd
Determine index from enum value and serialise accordingly for the asset.
AswinRajGopal Apr 7, 2025
9020c5e
Merge branch 'isxb-1482/fix-custom-processor-serializesbyIndex' of ht…
AswinRajGopal Apr 17, 2025
9819aab
Merge branch 'develop' into isxb-1482/fix-custom-processor-serializes…
AswinRajGopal Apr 17, 2025
43278ad
Update CHANGELOG.md
AswinRajGopal Apr 19, 2025
a06e9d3
Merge branch 'develop' into isxb-1482/fix-custom-processor-serializes…
Pauliusd01 Apr 23, 2025
580e931
Address PR feedbacks, changelog changed, test name modified, comments…
AswinRajGopal Apr 23, 2025
774190a
Address PR feedbacks, changelog changed, test name modified, comments…
AswinRajGopal Apr 23, 2025
6d50a6b
Merge branch 'isxb-1482/fix-custom-processor-serializesbyIndex' of ht…
AswinRajGopal Apr 23, 2025
7c7b927
Upgrade compatibility to fill the dropdown field with value
AswinRajGopal May 16, 2025
a521b32
Implement migrator for the old input action asset as we now serialize…
AswinRajGopal May 30, 2025
8c65e40
Removed additional version constant and used the existing k_Version t…
AswinRajGopal May 31, 2025
37d1dfb
Handle migration for LoadFromJson aswell and used NameAndParamerter p…
AswinRajGopal Jun 4, 2025
8bc3ef0
Remove log statements and unused header.
AswinRajGopal Jun 4, 2025
359f03d
Fix test failures and compiler errors.
AswinRajGopal Jun 5, 2025
b41abf8
Addressed PR comments, changed the migration logic a bit to deal with…
AswinRajGopal Jun 10, 2025
8d168c8
Removed empty lines, reverted kVersion to private.
AswinRajGopal Jun 10, 2025
a81282c
Removed empty lines, reverted kVersion to private.
AswinRajGopal Jun 10, 2025
b3f63c9
Merge branch 'isxb-1482/fix-custom-processor-serializesbyIndex' of ht…
AswinRajGopal Jun 10, 2025
40ce21e
Moved the optimization into migration function, assigned proper versi…
AswinRajGopal Jun 10, 2025
261cbd6
Fix test failure checking an empty json, omitt versioning.
AswinRajGopal Jun 10, 2025
42bb20c
Remove commented code and fix misplaced enums.
AswinRajGopal Jun 10, 2025
8acf7fe
Merge branch 'develop' into isxb-1482/fix-custom-processor-serializes…
ekcoh Jun 10, 2025
51994b7
Removed nullabe version fileds due to readability issues and boxing.
AswinRajGopal Jun 18, 2025
fbbacad
Patched the failing tests due the version introduction.
AswinRajGopal Jun 18, 2025
101dd7c
Merge branch 'isxb-1482/fix-custom-processor-serializesbyIndex' of ht…
AswinRajGopal Jun 18, 2025
1f6bd45
Revert "Patched the failing tests due the version introduction."
AswinRajGopal Jun 20, 2025
42807fd
Fix failing tests by introducing versioning.
AswinRajGopal Jun 20, 2025
34b0271
Merge branch 'develop' into isxb-1482/fix-custom-processor-serializes…
ekcoh Jun 25, 2025
a349aa5
Merge branch 'develop' into isxb-1482/fix-custom-processor-serializes…
ekcoh Jun 26, 2025
dc9452a
Merge branch 'develop' into isxb-1482/fix-custom-processor-serializes…
ritamerkl Jul 1, 2025
c38e483
Removing call to unity setup as wait for dirty for this test is not n…
AswinRajGopal Jul 2, 2025
d91a573
Merge branch 'develop' into isxb-1482/fix-custom-processor-serializes…
ritamerkl Jul 2, 2025
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
Prev Previous commit
Next Next commit
Implement migrator for the old input action asset as we now serialize…
… the enums by value in the processors.
  • Loading branch information
AswinRajGopal committed May 30, 2025
commit a521b32c52cfc49e0addfa1fd2aa88314eaba0f0
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ public string ToJson()
{
return JsonUtility.ToJson(new WriteFileJson
{
version = m_Version,
name = name,
maps = InputActionMap.WriteFileJson.FromMaps(m_ActionMaps).maps,
controlSchemes = InputControlScheme.SchemeJson.ToJson(m_ControlSchemes),
Expand Down Expand Up @@ -946,10 +947,12 @@ private void OnDestroy()
[NonSerialized] internal InputActionRebindingExtensions.ParameterOverride[] m_ParameterOverrides;

[NonSerialized] internal InputActionMap.DeviceArray m_Devices;
[SerializeField] internal int m_Version;

[Serializable]
internal struct WriteFileJson
{
public int version;
public string name;
public InputActionMap.WriteMapJson[] maps;
public InputControlScheme.SchemeJson[] controlSchemes;
Expand All @@ -965,12 +968,14 @@ internal struct WriteFileJsonNoName
[Serializable]
internal struct ReadFileJson
{
public int version;
public string name;
public InputActionMap.ReadMapJson[] maps;
public InputControlScheme.SchemeJson[] controlSchemes;

public void ToAsset(InputActionAsset asset)
{
asset.m_Version = version;
asset.name = name;
asset.m_ActionMaps = new InputActionMap.ReadFileJson {maps = maps}.ToMaps();
asset.m_ControlSchemes = InputControlScheme.SchemeJson.ToSchemes(controlSchemes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using UnityEditor;
#if UNITY_2020_2_OR_NEWER
using UnityEditor.AssetImporters;
Expand All @@ -29,6 +30,8 @@ namespace UnityEngine.InputSystem.Editor
internal class InputActionImporter : ScriptedImporter
{
private const int kVersion = 13;
// Bump this whenever you make a breaking change to the on-disk JSON format
private const int kJsonFormatVersion = 1;

[SerializeField] private bool m_GenerateWrapperCode;
[SerializeField] private string m_WrapperCodePath;
Expand Down Expand Up @@ -67,6 +70,19 @@ private static InputActionAsset CreateFromJson(AssetImportContext context)
// Attempt to parse JSON
asset.LoadFromJson(content);

// If this JSON was authored before we switched to enum-by-value, migrate it now
if (asset.m_Version < kJsonFormatVersion)
{
MigrateAllEnumParams(asset);
asset.m_Version = kJsonFormatVersion;

if (!EditorHelpers.WriteAsset(context.assetPath, asset.ToJson()))
context.LogImportError($"Could not write migrated JSON to '{context.assetPath}'");

EditorUtility.SetDirty(asset);
return null;
}

// Make sure action map names are unique within JSON file
var names = new HashSet<string>();
foreach (var map in asset.actionMaps)
Expand Down Expand Up @@ -117,6 +133,76 @@ private static InputActionAsset CreateFromJson(AssetImportContext context)
return asset;
}

static void MigrateAllEnumParams(InputActionAsset asset)
{
foreach (var map in asset.actionMaps)
{
foreach (var action in map.actions)
{
var raw = action.processors;
if (string.IsNullOrEmpty(raw))
continue;

var rebuilt = new List<string>();
foreach (var entry in raw.Split(';'))
{
var e = entry.Trim();
if (e.Length == 0)
{
rebuilt.Add(e);
continue;
}

var paren = e.IndexOf('(');
string procName = paren >= 0 ? e.Substring(0, paren) : e;
string args = paren >= 0
? e.Substring(paren + 1, e.Length - paren - 2)
: null;

var procType = InputSystem.TryGetProcessor(procName);
if (procType != null && args != null)
{
var patched = PatchEnumArgs(procType, args);
rebuilt.Add($"{procName}({patched})");
}
else
{
rebuilt.Add(e);
}
}

action.m_Processors = string.Join(";", rebuilt);
}
}
}

static string PatchEnumArgs(Type procType, string args)
{
var dict = args.Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries).Select(p => p.Split('=')).ToDictionary(kv => kv[0], kv => kv[1]);
var anyChanged = false;
var enumFields = procType.GetFields(BindingFlags.Public | BindingFlags.Instance).Where(f => f.FieldType.IsEnum);

foreach (var field in enumFields)
{
if (dict.TryGetValue(field.Name, out var raw) && int.TryParse(raw, out var ordinal))
{
var values = Enum.GetValues(field.FieldType).Cast<object>().ToArray();

if (ordinal >= 0 && ordinal < values.Length)
{
var real = Convert.ToInt32(values[ordinal]);
dict[field.Name] = real.ToString();
anyChanged = true;
}
}
}

if (!anyChanged)
return args;

return string.Join(",", dict.Select(kv => $"{kv.Key}={kv.Value}"));
}

public override void OnImportAsset(AssetImportContext ctx)
{
if (ctx == null)
Expand Down