Skip to content

Commit

Permalink
fix: Fixed the correct type not being found in MonoScript if the clas…
Browse files Browse the repository at this point in the history
…s was not the first in the file
  • Loading branch information
SolidAlloy committed Sep 26, 2021
1 parent 0547b4d commit 4703bb0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
34 changes: 23 additions & 11 deletions Editor/Extensions/MonoScriptExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,18 @@ public static class MonoScriptExtensions
/// <see cref="UnityEngine.Object"/> and generic classes (the file must be named by the "GenericClass`1.cs" template).
/// </summary>
/// <param name="script">The script to get the type from.</param>
/// <param name="className">A specific class name to search for.</param>
/// <returns>The <see cref="Type"/> of the class implemented by this script or <see langword="null"/>,
/// if the type was not found.</returns>
[PublicAPI, CanBeNull] public static Type GetClassType(this MonoScript script)
[PublicAPI, CanBeNull] public static Type GetClassType(this MonoScript script, string className = null)
{
Type simpleType = script.GetClass();
if (simpleType != null)
return simpleType;

string className = GetFirstClassFromText(script.text);
string foundClassName = string.IsNullOrEmpty(className) ? GetFirstClassFromText(script.text) : GetFirstClassWithName(script.text, className);

if (string.IsNullOrEmpty(className))
if (string.IsNullOrEmpty(foundClassName))
return null;

string assemblyName = script.GetAssemblyName();
Expand All @@ -51,7 +52,7 @@ public static class MonoScriptExtensions
}

string namespaceName = script.GetNamespaceName();
string fullTypeName = namespaceName == string.Empty ? className : $"{namespaceName}.{className}";
string fullTypeName = namespaceName == string.Empty ? foundClassName : $"{namespaceName}.{foundClassName}";

Type type = assembly.GetType(fullTypeName);
return type;
Expand All @@ -60,16 +61,27 @@ public static class MonoScriptExtensions
private static string GetFirstClassFromText(string text)
{
string className = _classRegex.Match(text).Value;
return GetProperClassName(className);
}

if (string.IsNullOrEmpty(className))
return className;
private static string GetFirstClassWithName(string text, string className)
{
string pattern = $@"(?<=(class )|(struct )){className}(\s)?(<.*?>)?(?=(\s|\n)*(:|{{))";
string foundClassName = Regex.Match(text, pattern).Value;
return GetProperClassName(foundClassName);
}

private static string GetProperClassName(string rawClassName)
{
if (string.IsNullOrEmpty(rawClassName))
return rawClassName;

if ( ! className.Contains("<"))
return className;
if ( ! rawClassName.Contains("<"))
return rawClassName;

int argsCount = className.CountChars(',') + 1;
int bracketIndex = className.IndexOf('<');
return $"{className.Substring(0, bracketIndex)}`{argsCount.ToString()}";
int argsCount = rawClassName.CountChars(',') + 1;
int bracketIndex = rawClassName.IndexOf('<');
return $"{rawClassName.Substring(0, bracketIndex)}`{argsCount.ToString()}";
}

/// <summary>Returns the assembly name of the class implemented by this script.</summary>
Expand Down
6 changes: 4 additions & 2 deletions Editor/Helpers/AssetSearch/AssetSearcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,14 @@ public static bool GetAssetDetails(Type type, [CanBeNull] out string GUID, out M
if (type.IsGenericType)
type = type.GetGenericTypeDefinition();

foreach (string guid in AssetDatabase.FindAssets($"t:MonoScript {type.Name.StripGenericSuffix()}"))
string typeNameWithoutSuffix = type.Name.StripGenericSuffix();

foreach (string guid in AssetDatabase.FindAssets($"t:MonoScript {typeNameWithoutSuffix}"))
{
string assetPath = AssetDatabase.GUIDToAssetPath(guid);
var asset = AssetDatabase.LoadAssetAtPath<MonoScript>(assetPath);

if (asset is null || asset.GetClassType() != type)
if (asset is null || asset.GetClassType(typeNameWithoutSuffix) != type)
continue;

GUID = guid;
Expand Down

0 comments on commit 4703bb0

Please sign in to comment.