← 输入框|github.com|刷新
Skip to content

Add API to check whether a mod export has been registered #255

Open

Description

@SnipUndercover

Description

Hello. I made ModInteropImportGenerator, a Roslyn source generator which allows mods to define mod imports using the standard method definition syntax. Feel free to check its README to see how it's used.

To help with troubleshooting, the sourcegen generates detailed exception messages if something goes wrong, e.g. the import did not complete successfully, or an import has been called before it was imported.
If none of the methods import successfully, it is currently not possible to tell whether this is because the dependency is not present, the import name was typed incorrectly, or none of the import methods' names and signatures match those of the export methods.
It would be great if an API existed to be able to differentiate between these situations to provide tailored troubleshooting steps.

I believe the simplest way to go about this would be to add a IsExportRegistered method like so:

@@ -9,6 +9,7 @@ namespace MonoMod.ModInterop
     {

         private static HashSet<Type> Registered = new HashSet<Type>();
+        private static HashSet<string> RegisteredExportNames = new HashSet<string>();

         private static Dictionary<string, List<MethodInfo>> Methods = new Dictionary<string, List<MethodInfo>>();
         private static List<FieldInfo> Fields = new List<FieldInfo>();
@@ -77,7 +78,10 @@ namespace MonoMod.ModInterop
                 throw new MemberAccessException("Utility must be public static");
             var name = method.Name;
             if (!string.IsNullOrEmpty(prefix))
+            {
+                RegisteredExportNames.Add(prefix);
                 name = prefix + "." + name;
+            }

             if (!Methods.TryGetValue(name, out var methods))
                 Methods[name] = methods = new List<MethodInfo>();
@@ -104,5 +108,9 @@ namespace MonoMod.ModInterop
             return field.Name;
         }

+        public static bool IsExportRegistered(string exportName)
+        {
+            return RegisteredExportNames.Contains(exportName) || Methods.ContainsKey(exportName);
+        }
     }
 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions