From 519b6eabbbadc5ed626025820bbb8f99acca7112 Mon Sep 17 00:00:00 2001 From: Steve Halliwell Date: Mon, 31 Aug 2020 10:39:15 +1000 Subject: [PATCH] Improve InvokeMethod type find and error log InvokeMethod now explicitily searches all assemblies to find matching fully qualified name. InvokeMethodEditor now logs error if targeted type cannot be found, rather than null ref error --- Assets/Fungus/Scripts/Commands/InvokeMethod.cs | 13 ++++++++----- Assets/Fungus/Scripts/Editor/InvokeMethodEditor.cs | 12 +++++++++++- .../FirstPerson/FirstPersonDemo.unity | 2 +- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/Assets/Fungus/Scripts/Commands/InvokeMethod.cs b/Assets/Fungus/Scripts/Commands/InvokeMethod.cs index 22328be5..ffa10d5a 100644 --- a/Assets/Fungus/Scripts/Commands/InvokeMethod.cs +++ b/Assets/Fungus/Scripts/Commands/InvokeMethod.cs @@ -8,6 +8,7 @@ using System.Collections.Generic; using System; using UnityEngine.Events; using MarkerMetro.Unity.WinLegacy.Reflection; +using System.Linq; namespace Fungus { @@ -403,14 +404,16 @@ namespace Fungus { static Dictionary types = new Dictionary(); - public static System.Type GetType(string typeName) + public static System.Type GetType(string AssemblyQualifiedNameTypeName) { - if (types.ContainsKey(typeName)) - return types[typeName]; + if (types.ContainsKey(AssemblyQualifiedNameTypeName) && types[AssemblyQualifiedNameTypeName] != null) + return types[AssemblyQualifiedNameTypeName]; - types[typeName] = System.Type.GetType(typeName); + types[AssemblyQualifiedNameTypeName] = AppDomain.CurrentDomain.GetAssemblies(). + SelectMany(x => x.GetTypes()) + .FirstOrDefault(x => x.AssemblyQualifiedName == AssemblyQualifiedNameTypeName); - return types[typeName]; + return types[AssemblyQualifiedNameTypeName]; } } } diff --git a/Assets/Fungus/Scripts/Editor/InvokeMethodEditor.cs b/Assets/Fungus/Scripts/Editor/InvokeMethodEditor.cs index 86420cef..feda95ec 100644 --- a/Assets/Fungus/Scripts/Editor/InvokeMethodEditor.cs +++ b/Assets/Fungus/Scripts/Editor/InvokeMethodEditor.cs @@ -84,7 +84,17 @@ namespace Fungus.EditorUtils var saveReturnValueProp = objTarget.FindProperty("saveReturnValue"); var returnValueKeyProp = objTarget.FindProperty("returnValueVariableKey"); - var objComponent = gameObject.GetComponent(ReflectionHelper.GetType(component)); + var t = ReflectionHelper.GetType(component); + Component objComponent = null; + if (t != null) + { + objComponent = gameObject.GetComponent(t); + } + else + { + Debug.LogError("InvokeMethod command has attempted to locate a type that cannot be found: " + component); + } + var bindingFlags = BindingFlags.Default | BindingFlags.Public | BindingFlags.Instance; if (!showInheritedProp.boolValue) diff --git a/Assets/FungusExamples/FirstPerson/FirstPersonDemo.unity b/Assets/FungusExamples/FirstPerson/FirstPersonDemo.unity index 28ca771f..14fb21ab 100644 --- a/Assets/FungusExamples/FirstPerson/FirstPersonDemo.unity +++ b/Assets/FungusExamples/FirstPerson/FirstPersonDemo.unity @@ -795,7 +795,7 @@ MonoBehaviour: indentLevel: 0 description: targetObject: {fileID: 1377282257} - targetComponentAssemblyName: Fungus.Examples.LookingAtDoor, Assembly-CSharp, Version=0.0.0.0, + targetComponentAssemblyName: Fungus.Examples.LookingAtDoor, FungusExamples, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null targetComponentFullname: UnityEngine.Component[] targetComponentText: LookingAtDoor