Skip to content

Commit fe48560

Browse files
author
Rene Damm
authored
FIX: Compile errors when using C# reserved identifiers in assets (#918).
1 parent fa27744 commit fe48560

File tree

6 files changed

+39
-34
lines changed

6 files changed

+39
-34
lines changed

Assets/Samples/SimpleDemo/SimpleControls.cs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
using UnityEngine.InputSystem;
77
using UnityEngine.InputSystem.Utilities;
88

9-
public class SimpleControls : IInputActionCollection, IDisposable
9+
public class @SimpleControls: IInputActionCollection, IDisposable
1010
{
1111
private InputActionAsset asset;
12-
public SimpleControls()
12+
public @SimpleControls()
1313
{
1414
asset = InputActionAsset.FromJson(@"{
1515
""name"": ""SimpleControls"",
@@ -207,8 +207,8 @@ public void Disable()
207207
private readonly InputAction m_gameplay_look;
208208
public struct GameplayActions
209209
{
210-
private SimpleControls m_Wrapper;
211-
public GameplayActions(SimpleControls wrapper) { m_Wrapper = wrapper; }
210+
private @SimpleControls m_Wrapper;
211+
public GameplayActions(@SimpleControls wrapper) { m_Wrapper = wrapper; }
212212
public InputAction @fire => m_Wrapper.m_gameplay_fire;
213213
public InputAction @move => m_Wrapper.m_gameplay_move;
214214
public InputAction @look => m_Wrapper.m_gameplay_look;
@@ -221,28 +221,28 @@ public void SetCallbacks(IGameplayActions instance)
221221
{
222222
if (m_Wrapper.m_GameplayActionsCallbackInterface != null)
223223
{
224-
fire.started -= m_Wrapper.m_GameplayActionsCallbackInterface.OnFire;
225-
fire.performed -= m_Wrapper.m_GameplayActionsCallbackInterface.OnFire;
226-
fire.canceled -= m_Wrapper.m_GameplayActionsCallbackInterface.OnFire;
227-
move.started -= m_Wrapper.m_GameplayActionsCallbackInterface.OnMove;
228-
move.performed -= m_Wrapper.m_GameplayActionsCallbackInterface.OnMove;
229-
move.canceled -= m_Wrapper.m_GameplayActionsCallbackInterface.OnMove;
230-
look.started -= m_Wrapper.m_GameplayActionsCallbackInterface.OnLook;
231-
look.performed -= m_Wrapper.m_GameplayActionsCallbackInterface.OnLook;
232-
look.canceled -= m_Wrapper.m_GameplayActionsCallbackInterface.OnLook;
224+
@fire.started -= m_Wrapper.m_GameplayActionsCallbackInterface.OnFire;
225+
@fire.performed -= m_Wrapper.m_GameplayActionsCallbackInterface.OnFire;
226+
@fire.canceled -= m_Wrapper.m_GameplayActionsCallbackInterface.OnFire;
227+
@move.started -= m_Wrapper.m_GameplayActionsCallbackInterface.OnMove;
228+
@move.performed -= m_Wrapper.m_GameplayActionsCallbackInterface.OnMove;
229+
@move.canceled -= m_Wrapper.m_GameplayActionsCallbackInterface.OnMove;
230+
@look.started -= m_Wrapper.m_GameplayActionsCallbackInterface.OnLook;
231+
@look.performed -= m_Wrapper.m_GameplayActionsCallbackInterface.OnLook;
232+
@look.canceled -= m_Wrapper.m_GameplayActionsCallbackInterface.OnLook;
233233
}
234234
m_Wrapper.m_GameplayActionsCallbackInterface = instance;
235235
if (instance != null)
236236
{
237-
fire.started += instance.OnFire;
238-
fire.performed += instance.OnFire;
239-
fire.canceled += instance.OnFire;
240-
move.started += instance.OnMove;
241-
move.performed += instance.OnMove;
242-
move.canceled += instance.OnMove;
243-
look.started += instance.OnLook;
244-
look.performed += instance.OnLook;
245-
look.canceled += instance.OnLook;
237+
@fire.started += instance.OnFire;
238+
@fire.performed += instance.OnFire;
239+
@fire.canceled += instance.OnFire;
240+
@move.started += instance.OnMove;
241+
@move.performed += instance.OnMove;
242+
@move.canceled += instance.OnMove;
243+
@look.started += instance.OnLook;
244+
@look.performed += instance.OnLook;
245+
@look.canceled += instance.OnLook;
246246
}
247247
}
248248
}

Assets/Tests/InputSystem/CoreTests_Editor.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1808,13 +1808,16 @@ public void Editor_ActionTree_CompositesAreShownWithNiceNames()
18081808
[TestCase("MyControls (2)", "MyNamespace", "", "MyNamespace.MyControls2")]
18091809
[TestCase("MyControls (2)", "MyNamespace", "MyClassName", "MyNamespace.MyClassName")]
18101810
[TestCase("MyControls", "", "MyClassName", "MyClassName")]
1811+
[TestCase("interface", "", "class", "class")] // Make sure we can deal with C# reserved keywords.
18111812
public void Editor_CanGenerateCodeWrapperForInputAsset(string assetName, string namespaceName, string className, string typeName)
18121813
{
18131814
var map1 = new InputActionMap("set1");
18141815
map1.AddAction("action1", binding: "/gamepad/leftStick");
18151816
map1.AddAction("action2", binding: "/gamepad/rightStick");
18161817
var map2 = new InputActionMap("set2");
18171818
map2.AddAction("action1", binding: "/gamepad/buttonSouth");
1819+
// Add an action that has a C# reserved keyword name.
1820+
map2.AddAction("return");
18181821
var asset = ScriptableObject.CreateInstance<InputActionAsset>();
18191822
asset.AddActionMap(map1);
18201823
asset.AddActionMap(map2);

Packages/com.unity.inputsystem/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ however, it has to be formatted properly to pass verification tests.
1919
#### Actions
2020

2121
- Fixed missing keyboard bindings in `DefaultInputActions.inputactions` for navigation in UI.
22+
- Fixed using C# reserved names in .inputactions assets leading to compile errors in generated C# classes (case 1189861).
2223
- Assigning a new `InputActionAsset` to a `InputSystemUIInputModule` will no longer look up action names globally but rather only look for actions that are located in action maps with the same name.
2324
* Previously, if you e.g. switched from one asset where the `point` action was bound to `UI/Point` to an asset that had no `UI` action map but did have an action called `Point` somewhere else, it would erroneously pick the most likely unrelated `Point` action for use by the UI.
2425
- Fixed missing custom editors for `AxisDeadzoneProcessor` and `StickDeadzoneProcessor` that link `min` and `max` values to input settings.

Packages/com.unity.inputsystem/InputSystem/Editor/AssetImporter/InputActionCodeGenerator.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,13 @@ public static string GenerateWrapperCode(InputActionAsset asset, Options options
8282
}
8383

8484
// Begin class.
85-
writer.WriteLine($"public class {options.className} : IInputActionCollection, IDisposable");
85+
writer.WriteLine($"public class @{options.className} : IInputActionCollection, IDisposable");
8686
writer.BeginBlock();
8787

8888
writer.WriteLine($"private InputActionAsset asset;");
8989

9090
// Default constructor.
91-
writer.WriteLine($"public {options.className}()");
91+
writer.WriteLine($"public @{options.className}()");
9292
writer.BeginBlock();
9393
writer.WriteLine($"asset = InputActionAsset.FromJson(@\"{asset.ToJson().Replace("\"", "\"\"")}\");");
9494

@@ -186,8 +186,8 @@ public static string GenerateWrapperCode(InputActionAsset asset, Options options
186186
writer.BeginBlock();
187187

188188
// Constructor.
189-
writer.WriteLine($"private {options.className} m_Wrapper;");
190-
writer.WriteLine($"public {mapTypeName}({options.className} wrapper) {{ m_Wrapper = wrapper; }}");
189+
writer.WriteLine($"private @{options.className} m_Wrapper;");
190+
writer.WriteLine($"public {mapTypeName}(@{options.className} wrapper) {{ m_Wrapper = wrapper; }}");
191191

192192
// Getter for each action.
193193
foreach (var action in map.actions)
@@ -223,9 +223,9 @@ public static string GenerateWrapperCode(InputActionAsset asset, Options options
223223
var actionName = CSharpCodeHelpers.MakeIdentifier(action.name);
224224
var actionTypeName = CSharpCodeHelpers.MakeTypeName(action.name);
225225

226-
writer.WriteLine($"{actionName}.started -= m_Wrapper.m_{mapTypeName}CallbackInterface.On{actionTypeName};");
227-
writer.WriteLine($"{actionName}.performed -= m_Wrapper.m_{mapTypeName}CallbackInterface.On{actionTypeName};");
228-
writer.WriteLine($"{actionName}.canceled -= m_Wrapper.m_{mapTypeName}CallbackInterface.On{actionTypeName};");
226+
writer.WriteLine($"@{actionName}.started -= m_Wrapper.m_{mapTypeName}CallbackInterface.On{actionTypeName};");
227+
writer.WriteLine($"@{actionName}.performed -= m_Wrapper.m_{mapTypeName}CallbackInterface.On{actionTypeName};");
228+
writer.WriteLine($"@{actionName}.canceled -= m_Wrapper.m_{mapTypeName}CallbackInterface.On{actionTypeName};");
229229
}
230230
writer.EndBlock();
231231

@@ -238,9 +238,9 @@ public static string GenerateWrapperCode(InputActionAsset asset, Options options
238238
var actionName = CSharpCodeHelpers.MakeIdentifier(action.name);
239239
var actionTypeName = CSharpCodeHelpers.MakeTypeName(action.name);
240240

241-
writer.WriteLine($"{actionName}.started += instance.On{actionTypeName};");
242-
writer.WriteLine($"{actionName}.performed += instance.On{actionTypeName};");
243-
writer.WriteLine($"{actionName}.canceled += instance.On{actionTypeName};");
241+
writer.WriteLine($"@{actionName}.started += instance.On{actionTypeName};");
242+
writer.WriteLine($"@{actionName}.performed += instance.On{actionTypeName};");
243+
writer.WriteLine($"@{actionName}.canceled += instance.On{actionTypeName};");
244244
}
245245
writer.EndBlock();
246246
writer.EndBlock();

Packages/com.unity.inputsystem/InputSystem/Editor/AssetImporter/InputActionImporter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace UnityEngine.InputSystem.Editor
2222
[ScriptedImporter(kVersion, InputActionAsset.Extension)]
2323
internal class InputActionImporter : ScriptedImporter
2424
{
25-
private const int kVersion = 7;
25+
private const int kVersion = 8;
2626

2727
private const string kActionIcon = "Packages/com.unity.inputsystem/InputSystem/Editor/Icons/InputAction.png";
2828
private const string kAssetIcon = "Packages/com.unity.inputsystem/InputSystem/Editor/Icons/InputActionAsset.png";

Packages/com.unity.inputsystem/InputSystem/Utilities/CSharpCodeHelpers.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,11 @@ public static bool IsEmptyOrProperNamespaceName(string name)
4040
return name.Split('.').All(IsProperIdentifier);
4141
}
4242

43+
////TODO: this one should add the @escape automatically so no other code has to worry
4344
public static string MakeIdentifier(string name, string suffix = "")
4445
{
4546
if (string.IsNullOrEmpty(name))
46-
throw new ArgumentNullException("name");
47+
throw new ArgumentNullException(nameof(name));
4748

4849
if (char.IsDigit(name[0]))
4950
name = "_" + name;

0 commit comments

Comments
 (0)