Пример #1
0
        private async Task <PluginHandler> GetPluginHandler(PluginInfo Plugin)
        {
            bool Online  = false;
            var VerName  = Plugin.Name + " Version";
            var ModeName = Plugin.Name + " Mode";

            if (Cache.ContainsKey(VerName))
            {
                Version LastVersion  = new Version(Plugin.LastVer);
                Version LocalVersion = new Version(Encoding.UTF8.GetString(Cache[VerName]));
                if (LastVersion > LocalVersion)
                {
                    Online = true;
                }
            }

            byte[] INI = await Download(Plugin.File, Online);

            var Handler = new PluginHandler();

            Handler.ExportPath = Ini.GetConfig("Plugin", "Export;Exp;export;exp", INI, true);
            Handler.ImportPath = Ini.GetConfig("Plugin", "Import;Imp;import;imp", INI, true);
            string Source = Ini.GetConfig("Plugin", "File;file;Archive;archive;Arc;arc", INI, false);

            if (string.IsNullOrWhiteSpace(Source))
            {
                Source = Path.GetFileNameWithoutExtension(Plugin.File);
            }

            int Mode = 0;

            byte[] Data;
            if (!Online && Cache.ContainsKey(ModeName))
            {
                Mode = int.Parse(Encoding.UTF8.GetString(Cache[ModeName]));
                switch (Mode)
                {
                case 0:
                    Data = await Download(Source + ".cs", Online, true);

                    break;

                case 1:
                    Data = await Download(Source + ".vb", Online, true);

                    break;

                case 2:
                    Data = await Download(Source + ".dll", Online, true);

                    break;

                default:
                    throw new Exception("Invalid Mode");
                }
            }
            else
            {
                if (ForceBinary)
                {
                    Mode = 2;
                    Data = await TryDownload(Source + ".dll", Online);
                }
                else
                {
                    Mode = 0;
                    Data = await TryDownload(Source + ".cs", Online);

                    if (Data == null)
                    {
                        Mode = 1;
                        Data = await TryDownload(Source + ".vb", Online);

                        if (Data == null)
                        {
                            Mode = 2;
                            Data = await Download(Source + ".dll", Online);
                        }
                    }
                }
                if (Data == null || Data.Length == 0)
                {
                    throw new FileLoadException("Unable to Download the Plugin: " + Source);
                }
            }
#if DebugPlugin
            bool Debug = true;
#else
            bool Debug = false;
#endif

            //Initialize Plugin
            Handler.InitializeWithScript = Ini.GetConfig("Plugin", "Initialize;InputOnCreate;initialize;inputoncreate", INI, false).ToLower() == "true";
            switch (Mode)
            {
            case 0:
                Handler.VM = new DotNetVM(Encoding.UTF8.GetString(Data), DotNetVM.Language.CSharp, null, Debug);
                break;

            case 1:
                Handler.VM = new DotNetVM(Encoding.UTF8.GetString(Data), DotNetVM.Language.VisualBasic, null, Debug);
                break;

            default:
                Handler.VM = new DotNetVM(Data);
                break;
            }

            Cache[VerName]  = Encoding.UTF8.GetBytes(Plugin.LastVer);
            Cache[ModeName] = Encoding.UTF8.GetBytes(Mode.ToString());

            return(Handler);
        }