Beispiel #1
0
        public async Task <PluginMetadata> LoadPlugin(string path, bool shouldAwait = true)
        {
            var pluginMetadata = new PluginMetadata
            {
                FileName = Path.GetFileName(path)
            };

            var options = new CompilerParameters();

            options.ReferencedAssemblies.Add(Assembly.GetEntryAssembly().Location);
            options.ReferencedAssemblies.Add("System.dll");
            options.ReferencedAssemblies.Add("System.Core.dll");
            options.ReferencedAssemblies.Add("System.Data.dll");
            options.ReferencedAssemblies.Add(ServerPath.GetBin("Newtonsoft.Json.dll"));
            options.GenerateInMemory = true;

            var c  = new Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider();
            var cr = shouldAwait ? await Task.Run(() => c.CompileAssemblyFromSource(options, File.ReadAllText(path))) : c.CompileAssemblyFromSource(options, File.ReadAllText(path));

            if (cr.Errors.HasErrors)
            {
                var sb = new StringBuilder();
                foreach (CompilerError err in cr.Errors)
                {
                    sb.Append($"{err.ErrorText}\nLine: {err.Line} - Column: {err.Column}\n\n");
                }
                pluginMetadata.Error = sb.ToString();
                Console.WriteLine(pluginMetadata.Error);
                return(pluginMetadata);
            }

            try
            {
                pluginMetadata.Type = shouldAwait ? await Task.Run(() => cr.CompiledAssembly.GetType($"WindowsGSM.Plugins.{Path.GetFileNameWithoutExtension(path)}")) : cr.CompiledAssembly.GetType($"WindowsGSM.Plugins.{Path.GetFileNameWithoutExtension(path)}");

                var plugin = GetPluginClass(pluginMetadata);
                pluginMetadata.FullName = $"{plugin.FullName} [{pluginMetadata.FileName}]";
                pluginMetadata.Plugin   = plugin.Plugin;
                try
                {
                    string      gameImage = ServerPath.GetPlugins(pluginMetadata.FileName, $"{Path.GetFileNameWithoutExtension(pluginMetadata.FileName)}.png");
                    ImageSource image     = new BitmapImage(new Uri(gameImage));
                    pluginMetadata.GameImage = gameImage;
                }
                catch
                {
                    pluginMetadata.GameImage = DefaultPluginImage;
                }
                try
                {
                    string      authorImage = ServerPath.GetPlugins(pluginMetadata.FileName, "author.png");
                    ImageSource image       = new BitmapImage(new Uri(authorImage));
                    pluginMetadata.AuthorImage = authorImage;
                }
                catch
                {
                    pluginMetadata.AuthorImage = DefaultUserImage;
                }
                pluginMetadata.IsLoaded = true;
            }
            catch (Exception e)
            {
                pluginMetadata.Error = e.Message;
                Console.WriteLine(pluginMetadata.Error);
                pluginMetadata.IsLoaded = false;
            }

            return(pluginMetadata);
        }