/// <summary>
        /// Add a plugin to the list
        /// </summary>
        /// <param name="plugin">The plugin to add</param>
        public void AddPlugin(PluginBaseClass plugin)
        {
            try {
                bool existsAlready = false;
                for (var i = 0; i < allPlugins.Count; i++)
                {
                    if (allPlugins[i].pluginName.Equals(plugin.pluginName))
                    {
                        SetPluginActiveState(allPlugins[i], false);
                        allPlugins[i] = plugin;
                        existsAlready = true;
                        break;
                    }
                }

                if (!existsAlready)
                {
                    allPlugins.Add(plugin);
                }
            }
            catch (Exception e) {
                Debug.Log("could not load Plugin: " + plugin.pluginName);
                Debug.Log(e);
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="plugin">the plugin to check for dependencies</param>
        /// <returns>success</returns>
        private bool ResolveDependencies(PluginBaseClass plugin)
        {
            List <string> missingDependencies = new List <string>();

            if (plugin.dependencies != null)
            {
                foreach (var dependency in plugin.dependencies)
                {
                    bool depMet = true;
                    foreach (var plugin1 in allPlugins)
                    {
                        if (plugin1.pluginName.Equals(dependency))
                        {
                            depMet = ResolveDependencies(plugin1);
                            break;
                        }
                    }

                    if (!depMet)
                    {
                        missingDependencies.Add(dependency);
                    }
                }
            }

            if (missingDependencies.Count != 0)
            {
                Debug.Log("Failed to load " + plugin.assetName + " (" + plugin.pluginName + ") because of missing dependencies.\n"
                          + plugin.assetName + " requires: " + missingDependencies);
                return(false);
            }

            return(true);
        }
Пример #3
0
 private void Start()
 {
     pluginBaseClass = GetComponent <PluginBaseClass>();
 }
        /// <summary>
        /// Enables or disables plugins properly
        /// </summary>
        /// <param name="plugin">the plugin to deal with</param>
        /// <param name="state">true = enable, false = disable</param>
        public void SetPluginActiveState(PluginBaseClass plugin, bool state)
        {
            if (state)               //enable

            // if dependencies are not met, don't load plugin
            {
                if (!ResolveDependencies(plugin))
                {
                    return;
                }

                switch (plugin.type)
                {
                case AssetType.Environment:
                    loadedEnvironments.Add(plugin.gameObject);
                    break;

                case AssetType.Gamemode:
                    var gamemode = core.SimpleInstantiate(plugin.gameObject).GetComponent <Gamemode>();
                    loadedGamemodes.Add(gamemode);
                    loadedPlugins.Add(gamemode);
                    gamemode.Init(core);
                    break;

                case AssetType.Misc:
                    var pl = core.SimpleInstantiate(plugin.gameObject).GetComponent <PluginBaseClass>();
                    miscPlugins.Add(pl);
                    loadedPlugins.Add(pl);
                    pl.Init(core);
                    break;

                case AssetType.TargetObject:
                    loadedTargetObjects.Add(plugin.GetComponentInChildren <TargetObject>());
                    break;

                case AssetType.VisualTrackedObject:
                    loadedTrackedObjects.Add(plugin.GetComponentInChildren <GenericTrackedObject>());
                    break;
                }
            }
            else                 //disable
            {
                plugin.StopPlugin();
                switch (plugin.type)
                {
                case AssetType.Gamemode:
                    break;

                case AssetType.TargetObject:
                    break;

                case AssetType.Environment:
                    break;

                case AssetType.VisualTrackedObject:
                    break;

                case AssetType.Misc:
                    Object.Destroy(plugin.gameObject);
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }
        }
        public void LoadPluginsFromFolder(string path)
        {
            List <PluginBaseClass> pluginsOut = new List <PluginBaseClass>();

            if (Util.EnsureDirectoryIntegrity(path, true))
            {
                string platformDir = "";
#if (UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN)
                platformDir = "win64";
#elif (UNITY_STANDALONE_LINUX || UNITY_EDITOR_LINUX)
                platformDir = "lin64";
#endif
                string[] pluginPaths = Directory.GetFiles(path);
                foreach (var pluginPath in pluginPaths)
                {
#if UNITY_EDITOR
                    if (pluginPath.Contains(".meta"))
                    {
                        continue;
                    }
#endif
                    try {
                        Byte[] data = new byte[] { };

                        //Open the stream and read it back.
                        // Using DotNetZip

                        /*using (FileStream fs = new FileStream(pluginPath, FileMode.Open)) {
                         *      MemoryStream ms = new MemoryStream();
                         *      using (ZipFile zip = ZipFile.Read(fs)) {
                         *              foreach (ZipEntry entry in zip)
                         *              {
                         *                      if (entry.FileName.Contains(platformDir)) {
                         *                              entry.Extract(ms);
                         *                              data = ms.ToArray();
                         *                      }
                         *              }
                         *      }
                         * }*/

                        // Using System.IO.Compression
                        var file = File.OpenRead(pluginPath);
                        var zip  = new ZipArchive(file, ZipArchiveMode.Read);
                        foreach (var entry in zip.Entries)
                        {
                            if (entry.FullName.Contains(platformDir))
                            {
                                var stream = entry.Open();
                                data = ReadStreamFully(stream);
                            }
                        }
                        AssetBundle assetBundle = AssetBundle.LoadFromMemory(data);
                        string[]    assetNames  = assetBundle.GetAllAssetNames();
                        string      assetName   = "";
                        foreach (var asset in assetNames)
                        {
                            if (asset.Contains("plugin") && asset.Contains(".prefab"))
                            {
                                assetName = asset;
                                break;
                            }
                        }
                        GameObject      assetObject = assetBundle.LoadAsset <GameObject>(assetName);
                        PluginBaseClass plugin      = assetObject.GetComponentInChildren <PluginBaseClass>();

                        pluginsOut.Add(plugin);
                    }
                    catch (Exception e) {
                        Debug.Log("Could not load Plugin from file");
                        Debug.Log(e);
                    }
                }
            }

            AddPlugins(pluginsOut.ToArray());
        }