Exemple #1
0
        // Free all loaded libraries
        void UnloadAll()
        {
            Debug.Log("Unload All");

            if (NimGame.UnityPluginUnload != null)
            {
                NimGame.UnityPluginUnload();
            }
            else
            {
                Debug.Log("Unload Func = null");
            }

            foreach (var kvp in _loadedPlugins)
            {
                bool result = DynLibInterface.UnloadPlugin(kvp.Value);
            }
            _loadedPlugins.Clear();
        }
Exemple #2
0
        // Load all plugins with 'PluginAttr'
        // Load all functions with 'PluginFunctionAttr'
        void LoadAll()
        {
            // TODO: Could loop over just Assembly-CSharp.dll in most cases?

            // Loop over all assemblies
            var assemblies = AppDomain.CurrentDomain.GetAssemblies();

            foreach (var assembly in assemblies)
            {
                // Loop over all types
                foreach (var type in assembly.GetTypes())
                {
                    // Get custom attributes for type
                    var typeAttributes = type.GetCustomAttributes(typeof(PluginAttr), true);
                    if (typeAttributes.Length > 0)
                    {
                        Debug.Assert(typeAttributes.Length == 1); // should not be possible

                        var typeAttribute = typeAttributes[0] as PluginAttr;

                        var    pluginName   = typeAttribute.pluginName;
                        IntPtr pluginHandle = IntPtr.Zero;
                        if (!_loadedPlugins.TryGetValue(pluginName, out pluginHandle))
                        {
                            var pluginPath = _path + pluginName + DynLibInterface.EXT;
                            pluginHandle = DynLibInterface.LoadPlugin(pluginPath);
                            Debug.Log("LoadPlugin " + pluginPath);
                            if (pluginHandle == IntPtr.Zero)
                            {
                                throw new System.Exception("Failed to load plugin [" + pluginPath + "]");
                            }

                            _loadedPlugins.Add(pluginName, pluginHandle);
                        }

                        // Loop over fields in type
                        var fields = type.GetFields(System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public);
                        foreach (var field in fields)
                        {
                            // Get custom attributes for field
                            var fieldAttributes = field.GetCustomAttributes(typeof(PluginFunctionAttr), true);
                            if (fieldAttributes.Length > 0)
                            {
                                Debug.Assert(fieldAttributes.Length == 1); // should not be possible

                                // Get PluginFunctionAttr attribute
                                var fieldAttribute = fieldAttributes[0] as PluginFunctionAttr;
                                var functionName   = fieldAttribute.functionName;

                                // Get function pointer
                                var fnPtr = DynLibInterface.GetPluginProcAddress(pluginHandle, functionName);
                                if (fnPtr == IntPtr.Zero)
                                {
                                    Debug.LogError(string.Format("Failed to find function [{0}] in plugin [{1}].", functionName, pluginName));
                                    continue;
                                }

                                // Get delegate pointer
                                var fnDelegate = Marshal.GetDelegateForFunctionPointer(fnPtr, field.FieldType);

                                // Set static field value
                                field.SetValue(null, fnDelegate);
                            }
                        }
                    }
                }
            }
            var interf = GetUnityInterfacesPtr();

            if (interf == IntPtr.Zero)
            {
                Debug.Log("Fail to GetUnityInterfacesPtr");
            }
            else
            {
                NimGame.UnityPluginLoad(GetUnityInterfacesPtr());
            }
        }