Skip to content
This repository has been archived by the owner on Jan 22, 2022. It is now read-only.

Commit

Permalink
Fix bug with public fields not receiving attributes in some cases
Browse files Browse the repository at this point in the history
- Fix bug when if you declare public fields as a comma seperated list like `float a, b, c;`, some field attributes would not propagate to all of the fields correctly
- Small fix to component name in one of the samples
  • Loading branch information
MerlinVR committed Mar 31, 2020
1 parent 8462a4f commit 593751b
Show file tree
Hide file tree
Showing 6 changed files with 340 additions and 321 deletions.
47 changes: 28 additions & 19 deletions Assets/UdonSharp/Editor/UdonSharpASTVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -742,27 +742,30 @@ public override void VisitFieldDeclaration(FieldDeclarationSyntax node)

bool isPublic = node.Modifiers.HasModifier("public") || fieldAttributes.Find(e => e is SerializeField) != null;

SymbolDefinition fieldSymbol = HandleVariableDeclaration(node.Declaration, isPublic ? SymbolDeclTypeFlags.Public : SymbolDeclTypeFlags.Private, fieldSyncMode);
FieldDefinition fieldDefinition = new FieldDefinition(fieldSymbol);
fieldDefinition.fieldAttributes = fieldAttributes;

if (fieldSymbol.IsUserDefinedType())
List<SymbolDefinition> fieldSymbols = HandleVariableDeclaration(node.Declaration, isPublic ? SymbolDeclTypeFlags.Public : SymbolDeclTypeFlags.Private, fieldSyncMode);
foreach (SymbolDefinition fieldSymbol in fieldSymbols)
{
System.Type fieldType = fieldSymbol.userCsType;
while (fieldType.IsArray)
fieldType = fieldType.GetElementType();
FieldDefinition fieldDefinition = new FieldDefinition(fieldSymbol);
fieldDefinition.fieldAttributes = fieldAttributes;

foreach (ClassDefinition classDefinition in visitorContext.externClassDefinitions)
if (fieldSymbol.IsUserDefinedType())
{
if (classDefinition.userClassType == fieldType)
System.Type fieldType = fieldSymbol.userCsType;
while (fieldType.IsArray)
fieldType = fieldType.GetElementType();

foreach (ClassDefinition classDefinition in visitorContext.externClassDefinitions)
{
fieldDefinition.userBehaviourSource = classDefinition.classScript;
break;
if (classDefinition.userClassType == fieldType)
{
fieldDefinition.userBehaviourSource = classDefinition.classScript;
break;
}
}
}
}

visitorContext.localFieldDefinitions.Add(fieldSymbol.symbolUniqueName, fieldDefinition);
visitorContext.localFieldDefinitions.Add(fieldSymbol.symbolUniqueName, fieldDefinition);
}
}

public override void VisitVariableDeclaration(VariableDeclarationSyntax node)
Expand All @@ -772,7 +775,7 @@ public override void VisitVariableDeclaration(VariableDeclarationSyntax node)
HandleVariableDeclaration(node, SymbolDeclTypeFlags.Local, UdonSyncMode.NotSynced);
}

public SymbolDefinition HandleVariableDeclaration(VariableDeclarationSyntax node, SymbolDeclTypeFlags symbolType, UdonSyncMode syncMode)
public List<SymbolDefinition> HandleVariableDeclaration(VariableDeclarationSyntax node, SymbolDeclTypeFlags symbolType, UdonSyncMode syncMode)
{
UpdateSyntaxNode(node);

Expand All @@ -794,9 +797,12 @@ public SymbolDefinition HandleVariableDeclaration(VariableDeclarationSyntax node
}

SymbolDefinition newSymbol = null;
List<SymbolDefinition> newSymbols = new List<SymbolDefinition>();

foreach (VariableDeclaratorSyntax variableDeclarator in node.Variables)
{
newSymbol = null;

string variableName = variableDeclarator.Identifier.ValueText;
bool createdSymbol = false;

Expand Down Expand Up @@ -841,13 +847,16 @@ public SymbolDefinition HandleVariableDeclaration(VariableDeclarationSyntax node
symbolCreationScope.SetToLocalSymbol(newSymbol);
}
}

if (newSymbol != null)
{
VerifySyncValidForType(newSymbol.symbolCsType, syncMode);
newSymbols.Add(newSymbol);
}
}

string udonTypeName = visitorContext.resolverContext.GetUdonTypeName(variableType);

if (newSymbol != null)
VerifySyncValidForType(newSymbol.symbolCsType, syncMode);

bool isUserDefinedType = UdonSharpUtils.IsUserDefinedType(variableType);

if (!visitorContext.resolverContext.ValidateUdonTypeName(udonTypeName, UdonReferenceType.Variable) &&
Expand All @@ -856,7 +865,7 @@ public SymbolDefinition HandleVariableDeclaration(VariableDeclarationSyntax node
!isUserDefinedType)
throw new System.NotSupportedException($"Udon does not support variables of type '{variableType.Name}' yet");

return newSymbol;
return newSymbols;
}

public override void VisitIdentifierName(IdentifierNameSyntax node)
Expand Down
2 changes: 1 addition & 1 deletion Assets/UdonSharp/Editor/UdonSharpProgramAsset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ protected override object DrawPublicVariableField(string symbol, object variable
fieldDefinitions.TryGetValue(symbol, out fieldDefinition);

EditorGUI.BeginChangeCheck();
object newValue = DrawFieldForType(null, symbol, (variableValue, variableType, fieldDefinition), fieldDefinition.fieldSymbol.userCsType, ref dirty, enabled);
object newValue = DrawFieldForType(null, symbol, (variableValue, variableType, fieldDefinition), fieldDefinition != null ? fieldDefinition.fieldSymbol.userCsType : null, ref dirty, enabled);

if (EditorGUI.EndChangeCheck())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ MonoBehaviour:
null\r\n\r\n.data_end\r\n\r\n \r\n # using UnityEngine;\r\n \r\n
\ # using VRC.SDK3.Components;\r\n \r\n # using VRC.SDKBase;\r\n
\ \r\n # using VRC.Udon;\r\n \r\n # namespace UdonSharp.Examples.Tutorials\r\n
\ \r\n # [AddComponentMenu(\"Udon Sharp/Tutorials/Spinning Cube
\ \r\n # [AddComponentMenu(\"Udon Sharp/Tutorials/Spinning Cubes
4 Signal\")]\r\n \r\n # public class SpinningCubes_4_Signal : UdonSharpBehaviour\r\n.code_start\r\n
\ \r\n # public float speed = 60f;\r\n.code_end\r\n"
assemblyError:
Expand Down Expand Up @@ -158,58 +158,59 @@ MonoBehaviour:
- startInstruction: 0
endInstruction: -1
startSourceChar: 89
endSourceChar: 185
endSourceChar: 289
line: 6
lineChar: 0
spanCodeSection: "namespace UdonSharp.Examples.Tutorials\r\n{\r\n /// <summary>\r\n
\ /// \r\n /// </summary>\r\n "
\ /// U# implementation of the fourth Udon spinning cube example (https://www.youtube.com/watch?v=HSpvrfWICeI)\r\n
\ /// </summary>\r\n "
- startInstruction: 0
endInstruction: -1
startSourceChar: 185
endSourceChar: 292
startSourceChar: 289
endSourceChar: 397
line: 11
lineChar: 4
spanCodeSection: "[AddComponentMenu(\"Udon Sharp/Tutorials/Spinning Cube 4 Signal\")]\r\n
\ public class SpinningCubes_4_Signal "
spanCodeSection: "[AddComponentMenu(\"Udon Sharp/Tutorials/Spinning Cubes 4
Signal\")]\r\n public class SpinningCubes_4_Signal "
- startInstruction: 0
endInstruction: -1
startSourceChar: 292
endSourceChar: 294
startSourceChar: 397
endSourceChar: 399
line: 12
lineChar: 40
spanCodeSection: ': '
- startInstruction: 0
endInstruction: -1
startSourceChar: 294
endSourceChar: 294
startSourceChar: 399
endSourceChar: 399
line: 12
lineChar: 42
spanCodeSection:
- startInstruction: 0
endInstruction: -1
startSourceChar: 294
endSourceChar: 330
startSourceChar: 399
endSourceChar: 435
line: 12
lineChar: 42
spanCodeSection: "UdonSharpBehaviour \r\n {\r\n "
- startInstruction: 0
endInstruction: -1
startSourceChar: 330
endSourceChar: 337
startSourceChar: 435
endSourceChar: 442
line: 14
lineChar: 8
spanCodeSection: 'public '
- startInstruction: 0
endInstruction: -1
startSourceChar: 337
endSourceChar: 337
startSourceChar: 442
endSourceChar: 442
line: 14
lineChar: 15
spanCodeSection:
- startInstruction: 0
endInstruction: 0
startSourceChar: 337
endSourceChar: 337
startSourceChar: 442
endSourceChar: 442
line: 14
lineChar: 15
spanCodeSection:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ MonoBehaviour:
\ \r\n # using UnityEngine;\r\n \r\n # using VRC.SDK3.Components;\r\n
\ \r\n # using VRC.SDKBase;\r\n \r\n # using VRC.Udon;\r\n
\ \r\n # namespace UdonSharp.Examples.Tutorials\r\n \r\n
\ # public class SpinningCubes_4_Spinner : UdonSharpBehaviour\r\n.code_start\r\n
\ # [AddComponentMenu(\"Udon Sharp/Tutorials/Spinning Cubes 4 Spinner\")]\r\n
\ \r\n # public class SpinningCubes_4_Spinner : UdonSharpBehaviour\r\n.code_start\r\n
\ \r\n # public SpinningCubes_4_Signal signal;\r\n \r\n #
private void Update()\r\n .export _update\r\n \r\n _update:\r\n \r\n
\ PUSH, __0_const_intnl_SystemUInt32\r\n PUSH, __0_intnl_returnTarget_UInt32\r\n
Expand Down Expand Up @@ -178,135 +179,137 @@ MonoBehaviour:
- startInstruction: 0
endInstruction: -1
startSourceChar: 89
endSourceChar: 185
endSourceChar: 289
line: 6
lineChar: 0
spanCodeSection: "namespace UdonSharp.Examples.Tutorials\r\n{\r\n /// <summary>\r\n
\ /// \r\n /// </summary>\r\n "
\ /// U# implementation of the fourth Udon spinning cube example (https://www.youtube.com/watch?v=HSpvrfWICeI)\r\n
\ /// </summary>\r\n "
- startInstruction: 0
endInstruction: -1
startSourceChar: 185
endSourceChar: 222
startSourceChar: 289
endSourceChar: 399
line: 11
lineChar: 4
spanCodeSection: 'public class SpinningCubes_4_Spinner '
spanCodeSection: "[AddComponentMenu(\"Udon Sharp/Tutorials/Spinning Cubes 4
Spinner\")]\r\n public class SpinningCubes_4_Spinner "
- startInstruction: 0
endInstruction: -1
startSourceChar: 222
endSourceChar: 224
line: 11
startSourceChar: 399
endSourceChar: 401
line: 12
lineChar: 41
spanCodeSection: ': '
- startInstruction: 0
endInstruction: -1
startSourceChar: 224
endSourceChar: 224
line: 11
startSourceChar: 401
endSourceChar: 401
line: 12
lineChar: 43
spanCodeSection:
- startInstruction: 0
endInstruction: -1
startSourceChar: 224
endSourceChar: 260
line: 11
startSourceChar: 401
endSourceChar: 437
line: 12
lineChar: 43
spanCodeSection: "UdonSharpBehaviour \r\n {\r\n "
- startInstruction: 0
endInstruction: -1
startSourceChar: 260
endSourceChar: 267
line: 13
startSourceChar: 437
endSourceChar: 444
line: 14
lineChar: 8
spanCodeSection: 'public '
- startInstruction: 0
endInstruction: -1
startSourceChar: 267
endSourceChar: 267
line: 13
startSourceChar: 444
endSourceChar: 444
line: 14
lineChar: 15
spanCodeSection:
- startInstruction: 0
endInstruction: -1
startSourceChar: 267
endSourceChar: 309
line: 13
startSourceChar: 444
endSourceChar: 486
line: 14
lineChar: 15
spanCodeSection: "SpinningCubes_4_Signal signal;\r\n\r\n "
- startInstruction: 0
endInstruction: 19
startSourceChar: 309
endSourceChar: 340
line: 15
startSourceChar: 486
endSourceChar: 517
line: 16
lineChar: 8
spanCodeSection: "private void Update()\r\n "
- startInstruction: 20
endInstruction: 19
startSourceChar: 340
endSourceChar: 355
line: 16
startSourceChar: 517
endSourceChar: 532
line: 17
lineChar: 8
spanCodeSection: "{\r\n "
- startInstruction: 20
endInstruction: 19
startSourceChar: 355
endSourceChar: 355
line: 17
startSourceChar: 532
endSourceChar: 532
line: 18
lineChar: 12
spanCodeSection:
- startInstruction: 20
endInstruction: 19
startSourceChar: 355
endSourceChar: 372
line: 17
startSourceChar: 532
endSourceChar: 549
line: 18
lineChar: 12
spanCodeSection: transform.Rotate(
- startInstruction: 20
endInstruction: 19
startSourceChar: 372
endSourceChar: 372
line: 17
startSourceChar: 549
endSourceChar: 549
line: 18
lineChar: 29
spanCodeSection:
- startInstruction: 20
endInstruction: 19
startSourceChar: 372
endSourceChar: 380
line: 17
startSourceChar: 549
endSourceChar: 557
line: 18
lineChar: 29
spanCodeSection: Vector3.
- startInstruction: 20
endInstruction: 35
startSourceChar: 380
endSourceChar: 384
line: 17
startSourceChar: 557
endSourceChar: 561
line: 18
lineChar: 37
spanCodeSection: 'up, '
- startInstruction: 36
endInstruction: 35
startSourceChar: 384
endSourceChar: 399
line: 17
startSourceChar: 561
endSourceChar: 576
line: 18
lineChar: 41
spanCodeSection: 'signal.speed * '
- startInstruction: 36
endInstruction: 35
startSourceChar: 399
endSourceChar: 399
line: 17
startSourceChar: 576
endSourceChar: 576
line: 18
lineChar: 56
spanCodeSection:
- startInstruction: 36
endInstruction: 35
startSourceChar: 399
endSourceChar: 404
line: 17
startSourceChar: 576
endSourceChar: 581
line: 18
lineChar: 56
spanCodeSection: Time.
- startInstruction: 36
endInstruction: 36
startSourceChar: 404
endSourceChar: 404
line: 17
startSourceChar: 581
endSourceChar: 581
line: 18
lineChar: 61
spanCodeSection:
serializationData:
Expand Down
Loading

0 comments on commit 593751b

Please sign in to comment.