public void InitializeEngineInstance()
 {
     AssembliesList    = NamespaceMethods.GetAssemblies(ImportedNamespaces);
     NamespacesList    = NamespaceMethods.GetNamespacesList(ImportedNamespaces);
     EngineScript      = CSharpScript.Create("", ScriptOptions.Default.WithReferences(AssembliesList).WithImports(NamespacesList));
     EngineScriptState = null;
 }
Beispiel #2
0
        private void lbxImportedNamespaces_KeyDown(object sender, KeyEventArgs e)
        {
            ListBox listBox = (ListBox)sender;

            if (e.KeyCode == Keys.Delete)
            {
                List <string> removaList = new List <string>();
                foreach (var item in listBox.SelectedItems)
                {
                    var pair = (KeyValuePair <string, List <AssemblyReference> >)item;
                    removaList.Add(pair.Key);
                }

                removaList.ForEach(x => ScriptContext.ImportedNamespaces.Remove(x));
                var importedNameSpacesBinding = new BindingSource(ScriptContext.ImportedNamespaces, null);
                lbxImportedNamespaces.DataSource = importedNameSpacesBinding;

                TypeMethods.GenerateAllVariableTypes(NamespaceMethods.GetAssemblies(ScriptContext.ImportedNamespaces), TypeContext.GroupedTypes);
                ScriptContext.LoadCompilerObjects();

                //marks the script as unsaved with changes
                if (uiScriptTabControl.SelectedTab != null && !uiScriptTabControl.SelectedTab.Text.Contains(" *"))
                {
                    uiScriptTabControl.SelectedTab.Text += " *";
                }
            }
            else
            {
                dgvVariablesArguments_KeyDown(null, e);
            }
        }
        public static ContainerBuilder LoadBuilder(List <string> assemblyPaths, Dictionary <string, List <Type> > groupedTypes, Dictionary <string, List <AssemblyReference> > allNamespaces,
                                                   Dictionary <string, List <AssemblyReference> > importedNamespaces)
        {
            List <Assembly> existingAssemblies = new List <Assembly>();

            foreach (var path in assemblyPaths)
            {
                try
                {
                    var name = AssemblyName.GetAssemblyName(path).Name;

                    var assemblies       = AppDomain.CurrentDomain.GetAssemblies();
                    var existingAssembly = assemblies.Where(x => x.GetName().Name == name &&
                                                            x.GetName().Version.ToString() == AssemblyName.GetAssemblyName(path).Version.ToString())
                                           .FirstOrDefault();

                    if (existingAssembly == null && name != "RestSharp" && name != "WebDriver" && name != "OpenBots.Core")
                    {
                        //has to be LoadFile because package manager can't update/uninstall assemblies if LoadFrom
                        var assembly = Assembly.LoadFrom(path);
                        existingAssemblies.Add(assembly);
                    }
                    else if (existingAssembly != null)
                    {
                        existingAssemblies.Add(existingAssembly);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }
            }

            //TODO: limit types to one in loaded assemblies. Previously getting all assemblies instead of just the ones in existingAssemblies because mscorlib was missing
            TypeMethods.GenerateAllNamespaces(AppDomain.CurrentDomain.GetAssemblies().ToList(), allNamespaces);
            TypeMethods.GenerateAllVariableTypes(NamespaceMethods.GetAssemblies(importedNamespaces), groupedTypes);


            //if no commands have been loaded, at least include OpenBots.Core to access the BrokenCodeCommand
            if (existingAssemblies.Count == 0)
            {
                existingAssemblies.Add(Assembly.GetAssembly(typeof(BrokenCodeCommentCommand)));
            }

            ContainerBuilder builder = new ContainerBuilder();

            builder.RegisterAssemblyTypes(existingAssemblies.ToArray())
            .Where(t => t.IsAssignableTo <ScriptCommand>())
            .Named <ScriptCommand>(t => t.Name)
            .AsImplementedInterfaces();
            return(builder);
        }
        public async Task InitializeEngineInstanceTest(string scriptFilePath)
        {
            //different fix for type deserialization. Makes opening files slow but can be used if the other fix fails
            string  scriptFileText = File.ReadAllText(scriptFilePath);
            dynamic scriptFileObj  = JsonConvert.DeserializeObject(scriptFileText);

            ImportedNamespaces = JsonConvert.DeserializeObject <Dictionary <string, List <AssemblyReference> > >(JsonConvert.SerializeObject(scriptFileObj["ImportedNamespaces"]));
            AssembliesList     = NamespaceMethods.GetAssemblies(ImportedNamespaces);
            NamespacesList     = NamespaceMethods.GetNamespacesList(ImportedNamespaces);
            EngineScript       = CSharpScript.Create("", ScriptOptions.Default.WithReferences(AssembliesList).WithImports(NamespacesList));
            EngineScriptState  = await EngineScript.RunAsync();

            EngineScriptState = await EngineScriptState.ContinueWithAsync($"string {GenerateGuidPlaceHolder()} = \"\";");
        }
Beispiel #5
0
        private void cbxAllNamespaces_SelectionChangeCommitted(object sender, EventArgs e)
        {
            var pair = (KeyValuePair <string, List <AssemblyReference> >)cbxAllNamespaces.SelectedItem;

            if (!ScriptContext.ImportedNamespaces.ContainsKey(pair.Key))
            {
                ScriptContext.ImportedNamespaces.Add(pair.Key, pair.Value);
                var importedNameSpacesBinding = new BindingSource(ScriptContext.ImportedNamespaces, null);
                lbxImportedNamespaces.DataSource = importedNameSpacesBinding;

                TypeMethods.GenerateAllVariableTypes(NamespaceMethods.GetAssemblies(ScriptContext.ImportedNamespaces), TypeContext.GroupedTypes);
                ScriptContext.LoadCompilerObjects();

                //marks the script as unsaved with changes
                if (uiScriptTabControl.SelectedTab != null && !uiScriptTabControl.SelectedTab.Text.Contains(" *"))
                {
                    uiScriptTabControl.SelectedTab.Text += " *";
                }
            }
        }
Beispiel #6
0
        private void ResetVariableArgumentBindings()
        {
            dgvVariables.DataSource = new BindingList <ScriptVariable>(ScriptContext.Variables);
            dgvArguments.DataSource = new BindingList <ScriptArgument>(ScriptContext.Arguments);

            TypeMethods.GenerateAllVariableTypes(NamespaceMethods.GetAssemblies(ScriptContext.ImportedNamespaces), TypeContext.GroupedTypes);

            var defaultTypesBinding = new BindingSource(TypeContext.DefaultTypes, null);

            variableType.DataSource = defaultTypesBinding;
            argumentType.DataSource = defaultTypesBinding;

            var importedNameSpacesBinding = new BindingSource(ScriptContext.ImportedNamespaces, null);

            lbxImportedNamespaces.DataSource = importedNameSpacesBinding;

            var allNameSpacesBinding = new BindingSource(AllNamespaces, null);

            cbxAllNamespaces.DataSource = allNameSpacesBinding;
        }
Beispiel #7
0
        public void LoadCompilerObjects()
        {
            AssembliesList = NamespaceMethods.GetAssemblies(ImportedNamespaces);
            NamespacesList = NamespaceMethods.GetNamespacesList(ImportedNamespaces);

            DefaultCompilationOptions = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary).WithOverflowChecks(true)
                                        .WithOptimizationLevel(OptimizationLevel.Release)
                                        .WithUsings(NamespacesList);

            DefaultReferences = AssembliesList.Select(x => (MetadataReference)MetadataReference.CreateFromFile(x.Location)).ToList();

            //Intellisense
            AssembliesList.AddRange(MefHostServices.DefaultAssemblies);
            _workspace = new AdhocWorkspace(MefHostServices.Create(MefHostServices.DefaultAssemblies));

            _scriptProjectInfo = ProjectInfo.Create(ProjectId.CreateNewId(), VersionStamp.Create(), "Script", "Script", LanguageNames.CSharp, isSubmission: true)
                                 .WithMetadataReferences(DefaultReferences).WithCompilationOptions(DefaultCompilationOptions);

            ActiveTextBox = null;
            LoadIntellisenseScript();
        }