Exemplo n.º 1
0
        public void ReloadPlugin(bool actualyReload)
        {
            if (BPlugin != null)
            {
                BPlugin._OnClose(); //saving settings, closing opened threads (on plugin side)

                API.eRender        -= BPlugin._Render;
                API.eEntityAdded   -= BPlugin._EntityAdded;
                API.eEntityRemoved -= BPlugin._EntityRemoved;
                API.eClose         -= BPlugin._OnClose;
                API.eAreaChange    -= BPlugin._AreaChange;
                API.eInitialise    -= BPlugin._Initialise;
                BPlugin._OnPluginDestroyForHotReload();

                BPlugin = null;
                SettingPropertyDrawers.Clear();

                GC.Collect();
                GC.WaitForPendingFinalizers();
            }

            Assembly asmToLoad;
            var      debugCymboldFilePath = _dllPath.Replace(".dll", ".pdb");

            if (File.Exists(debugCymboldFilePath))
            {
                var dbgCymboldBytes = File.ReadAllBytes(debugCymboldFilePath);
                asmToLoad = Assembly.Load(File.ReadAllBytes(_dllPath), dbgCymboldBytes);
            }
            else
            {
                asmToLoad = Assembly.Load(File.ReadAllBytes(_dllPath));
            }

            if (asmToLoad == null)
            {
                State = PluginState.Reload_DllNotFound;
                return;
            }

            var pluginType = asmToLoad.GetType(_fullTypeName);

            if (pluginType == null)
            {
                State = PluginState.Reload_ClassNotFound;
                return;
            }

            //Spawning a new plugin class instance
            object pluginClassObj = null;

            try
            {
                pluginClassObj = Activator.CreateInstance(pluginType);
            }
            catch (Exception ex)
            {
                BasePlugin.LogMessage($"Error loading plugin {Path.GetFileNameWithoutExtension(_dllPath)}: " + ex.Message, 3);
                State = PluginState.ErrorClassInstance;
                return;
            }

            BPlugin = pluginClassObj as BasePlugin;
            BPlugin.InitPlugin(this);
            Settings = BPlugin._LoadSettings();

            if (!string.IsNullOrEmpty(BPlugin.PluginName))
            {
                PluginName = BPlugin.PluginName;
            }

            API.eRender        += BPlugin._Render;
            API.eEntityAdded   += BPlugin._EntityAdded;
            API.eEntityRemoved += BPlugin._EntityRemoved;
            API.eClose         += BPlugin._OnClose;
            API.eInitialise    += BPlugin._Initialise;
            API.eAreaChange    += BPlugin._AreaChange;

            BPlugin._Initialise();

            if (actualyReload)
            {
                BPlugin._AreaChange(GameController.Instance.Area);
            }

            foreach (var entity in GameController.Instance.EntityListWrapper.Entities.ToList())
            {
                BPlugin._EntityAdded(entity);
            }
        }