Skip to content

Commit

Permalink
More robust handling for VRCSDK3 components
Browse files Browse the repository at this point in the history
- Now look through all the potential component types in VRCSDK3 and see if they have a mapping back to a base type. If they do, replace any instances of the base type with the VRCSDK3 type. This is fairly slow initially since it needs to look through all assemblies and should probably be looked at to speed up compiles a little.
- Make the RefreshProgramImpl handle when the program didn't assemble
  • Loading branch information
MerlinVR committed Feb 24, 2020
1 parent 0f23fb3 commit eede251
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 20 deletions.
7 changes: 6 additions & 1 deletion Assets/UdonSharp/Editor/UdonSharpProgramAsset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,12 @@ protected override void DrawProgramSourceGUI(UdonBehaviour udonBehaviour, ref bo

protected override void RefreshProgramImpl()
{
if (sourceCsScript != null && !EditorApplication.isCompiling)
bool hasAssemblyError = typeof(UdonAssemblyProgramAsset).GetField("assemblyError", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(this) != null;

if (sourceCsScript != null &&
!EditorApplication.isCompiling &&
!EditorApplication.isUpdating &&
!hasAssemblyError)
CompileCsProgram();
}

Expand Down
61 changes: 42 additions & 19 deletions Assets/UdonSharp/Editor/UdonSharpResolverContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -262,30 +262,45 @@ public System.Type ResolveExternType(string qualifiedTypeName)
return null;
}

public string SanitizeTypeName(string typeName)
{
return typeName.Replace(",", "")
.Replace(".", "")
.Replace("[]", "Array")
.Replace("&", "Ref")
.Replace("+", "");
}
private static Dictionary<System.Type, System.Type> inheritedTypeMap = null;

private string ReplaceSpecialCaseVRCComponents(string originalString)
private Dictionary<System.Type, System.Type> GetInheritedTypeMap()
{
originalString = originalString.Replace("VRCUdonUdonBehaviour", "VRCUdonCommonInterfacesIUdonEventReceiver");
if (inheritedTypeMap != null)
return inheritedTypeMap;

inheritedTypeMap = new Dictionary<System.Type, System.Type>();

if (originalString.StartsWith("VRCSDKBase"))
IEnumerable<System.Type> typeList = System.AppDomain.CurrentDomain.GetAssemblies().SelectMany(t => t.GetTypes()).Where(t => t != null && t.Namespace != null && t.Namespace.StartsWith("VRC.SDK3.Components"));

foreach (System.Type childType in typeList)
{
if (!(originalString.StartsWith("VRCSDKBaseVRCPlayerApi") ||
originalString.StartsWith("VRCSDKBaseNetworking") ||
originalString.StartsWith("VRCSDKBaseInputManager")))
if (childType.BaseType != null && childType.BaseType.Namespace.StartsWith("VRC.SDKBase"))
{
return originalString.Replace("VRCSDKBase", "VRCSDK3Components").Replace("_", "");
inheritedTypeMap.Add(childType.BaseType, childType);
}
}

return originalString;
return inheritedTypeMap;
}

private System.Type RemapBaseType(System.Type type)
{
var typeMap = GetInheritedTypeMap();

if (typeMap.ContainsKey(type))
return typeMap[type];

return type;
}

public string SanitizeTypeName(string typeName)
{
return typeName.Replace(",", "")
.Replace(".", "")
.Replace("[]", "Array")
.Replace("&", "Ref")
.Replace("+", "");
}

/// <summary>
Expand All @@ -296,6 +311,8 @@ private string ReplaceSpecialCaseVRCComponents(string originalString)
/// or null if it is not a valid Udon type.</returns>
public string GetUdonTypeName(System.Type externType)
{
externType = RemapBaseType(externType);

string externTypeName = externType.GetNameWithoutGenericArity();
string typeNamespace = externType.Namespace;

Expand All @@ -318,7 +335,7 @@ public string GetUdonTypeName(System.Type externType)
if (externTypeName == "T" || externTypeName == "T[]")
typeNamespace = "";

string fullTypeName = ReplaceSpecialCaseVRCComponents(SanitizeTypeName($"{typeNamespace}.{externTypeName}"));
string fullTypeName = SanitizeTypeName($"{typeNamespace}.{externTypeName}");

foreach (System.Type genericType in externType.GetGenericArguments())
{
Expand All @@ -331,6 +348,8 @@ public string GetUdonTypeName(System.Type externType)
fullTypeName = "ListT";
}

fullTypeName = fullTypeName.Replace("VRCUdonUdonBehaviour", "VRCUdonCommonInterfacesIUdonEventReceiver");

return fullTypeName;
}

Expand All @@ -351,7 +370,9 @@ public string GetUdonMethodName(MethodBase externMethod, bool validate = true, L
methodSourceType = genericArguments.First();
}

string functionNamespace = ReplaceSpecialCaseVRCComponents(SanitizeTypeName(methodSourceType.FullName));
methodSourceType = RemapBaseType(methodSourceType);

string functionNamespace = SanitizeTypeName(methodSourceType.FullName).Replace("VRCUdonUdonBehaviour", "VRCUdonCommonInterfacesIUdonEventReceiver");

string methodName = $"__{externMethod.Name.Trim('_').TrimStart('.')}";
ParameterInfo[] methodParams = externMethod.GetParameters();
Expand Down Expand Up @@ -400,7 +421,9 @@ public string GetUdonMethodName(MethodBase externMethod, bool validate = true, L

public string GetUdonFieldAccessorName(FieldInfo externField, FieldAccessorType accessorType, bool validate = true)
{
string functionNamespace = ReplaceSpecialCaseVRCComponents(SanitizeTypeName(externField.DeclaringType.FullName));
System.Type fieldType = RemapBaseType(externField.DeclaringType);

string functionNamespace = SanitizeTypeName(fieldType.FullName).Replace("VRCUdonUdonBehaviour", "VRCUdonCommonInterfacesIUdonEventReceiver");
string methodName = $"__{(accessorType == FieldAccessorType.Get ? "get" : "set")}_{externField.Name.Trim('_')}";

string paramStr = $"__{GetUdonTypeName(externField.FieldType)}";
Expand Down

0 comments on commit eede251

Please sign in to comment.