Ejemplo n.º 1
0
        private void frmScriptBuilder_Shown(object sender, EventArgs e)
        {
            if (!_isFirstTimeSetupComplete)
            {
                _studioFormShownEarly = true;
                Hide();
                return;
            }

            DialogResult result;

            if (_appSettings == null)
            {
                _appSettings = new ApplicationSettings().GetOrCreateApplicationSettings();
            }

            if (!_appSettings.ClientSettings.IsRestarting)
            {
                Program.SplashForm.Close();
                result = AddProject();
            }
            else
            {
                _appSettings.ClientSettings.IsRestarting = false;
                _appSettings.Save();

                frmProjectBuilder restartProjectBuilder = new frmProjectBuilder()
                {
                    ExistingProjectPath = ScriptProjectPath,
                    ExistingConfigPath  = Path.Combine(ScriptProjectPath, "project.obconfig"),
                    Action       = ProjectAction.OpenProject,
                    DialogResult = DialogResult.OK
                };
                result = AddProject(restartProjectBuilder);
            }

            if (result != DialogResult.Abort)
            {
                Notify("Welcome! Select a Command to get started!", Color.White);
            }
        }
Ejemplo n.º 2
0
        public void AddProject()
        {
            tvProject.Nodes.Clear();
            var projectBuilder = new frmProjectBuilder();

            projectBuilder.ShowDialog();

            //Close taskt if add project form is closed at startup
            if (projectBuilder.DialogResult == DialogResult.Cancel && _scriptProject == null)
            {
                Application.Exit();
                return;
            }

            //Create new taskt project
            else if (projectBuilder.CreateProject == true)
            {
                DialogResult result = CheckForUnsavedScripts();
                if (result == DialogResult.Cancel)
                {
                    return;
                }

                uiScriptTabControl.TabPages.Clear();
                _scriptProjectPath = projectBuilder.NewProjectPath;

                string                mainScriptPath      = Path.Combine(_scriptProjectPath, "Main.json");
                string                mainScriptName      = Path.GetFileNameWithoutExtension(mainScriptPath);
                UIListView            mainScriptActions   = NewLstScriptActions(mainScriptName);
                List <ScriptVariable> mainScriptVariables = new List <ScriptVariable>();
                List <ScriptElement>  mainScriptElements  = new List <ScriptElement>();
                ShowMessageCommand    helloWorldCommand   = new ShowMessageCommand();

                helloWorldCommand.v_Message = "Hello World";
                mainScriptActions.Items.Insert(0, CreateScriptCommandListViewItem(helloWorldCommand));

                //Begin saving as main.xml
                ClearSelectedListViewItems();

                try
                {
                    //Serialize main script
                    var mainScript = Script.SerializeScript(mainScriptActions.Items, mainScriptVariables, mainScriptElements,
                                                            mainScriptPath, projectBuilder.NewProjectName);
                    //Create new project
                    Project proj = new Project(projectBuilder.NewProjectName);
                    _mainFileName = proj.Main;
                    //Save new project
                    proj.SaveProject(mainScriptPath, mainScript, _mainFileName);
                    //Open new project
                    _scriptProject = Project.OpenProject(mainScriptPath);
                    //Open main script
                    OpenFile(mainScriptPath);
                    ScriptFilePath = mainScriptPath;
                    //Show success dialog
                    Notify("Project has been created successfully!");
                }
                catch (Exception ex)
                {
                    Notify("An Error Occured: " + ex.Message);
                }
            }

            //Open existing taskt project
            else if (projectBuilder.OpenProject == true)
            {
                DialogResult result = CheckForUnsavedScripts();
                if (result == DialogResult.Cancel)
                {
                    return;
                }

                try
                {
                    //Open project
                    _scriptProject = Project.OpenProject(projectBuilder.ExistingMainPath);
                    _mainFileName  = _scriptProject.Main;

                    if (Path.GetFileName(projectBuilder.ExistingMainPath) != _mainFileName)
                    {
                        throw new Exception("Attempted to open project from a script that isn't Main");
                    }

                    _scriptProjectPath = Path.GetDirectoryName(projectBuilder.ExistingMainPath);
                    uiScriptTabControl.TabPages.Clear();
                    //Open Main
                    OpenFile(projectBuilder.ExistingMainPath);
                    //show success dialog
                    Notify("Project has been opened successfully!");
                }
                catch (Exception ex)
                {
                    //show fail dialog
                    Notify("An Error Occured: " + ex.Message);
                    //Try adding project again
                    AddProject();
                    return;
                }
            }

            DirectoryInfo projectDirectoryInfo = new DirectoryInfo(_scriptProjectPath);
            TreeNode      projectNode          = new TreeNode(projectDirectoryInfo.Name);

            projectNode.Text = projectDirectoryInfo.Name;
            projectNode.Tag  = projectDirectoryInfo.FullName;
            projectNode.Nodes.Add("Empty");
            projectNode.ContextMenuStrip = cmsProjectMainFolderActions;
            tvProject.Nodes.Add(projectNode);
            projectNode.Expand();
        }
        public DialogResult AddProject()
        {
            tvProject.Nodes.Clear();
            var projectBuilder = new frmProjectBuilder();

            projectBuilder.ShowDialog();

            //close OpenBots if add project form is closed at startup
            if (projectBuilder.DialogResult == DialogResult.Cancel && ScriptProject == null)
            {
                Application.Exit();
                return(DialogResult.Abort);
            }

            //create new project
            else if (projectBuilder.Action == frmProjectBuilder.ProjectAction.CreateProject)
            {
                DialogResult result = CheckForUnsavedScripts();
                if (result == DialogResult.Cancel)
                {
                    return(DialogResult.Cancel);
                }

                uiScriptTabControl.TabPages.Clear();
                ScriptProjectPath = projectBuilder.NewProjectPath;

                //create new project
                ScriptProject = new Project(projectBuilder.NewProjectName, projectBuilder.NewProjectType);
                string configPath = Path.Combine(ScriptProjectPath, "project.obconfig");

                //create config file
                File.WriteAllText(configPath, JsonConvert.SerializeObject(ScriptProject));

                NotifySync("Loading package assemblies...", Color.White);

                var assemblyList = NugetPackageManager.LoadPackageAssemblies(configPath);
                _builder   = AppDomainSetupManager.LoadBuilder(assemblyList, _typeContext.GroupedTypes);
                AContainer = _builder.Build();

                string mainScriptPath = Path.Combine(ScriptProjectPath, ScriptProjectPath, ScriptProject.Main);
                string mainScriptName = Path.GetFileNameWithoutExtension(mainScriptPath);

                switch (ScriptProject.ProjectType)
                {
                case ProjectType.OpenBots:
                    CreateOpenBotsProject(mainScriptName, mainScriptPath);
                    break;

                case ProjectType.Python:
                case ProjectType.TagUI:
                case ProjectType.CSScript:
                    CreateTextEditorProject(mainScriptName, mainScriptPath);
                    break;
                }

                //show success dialog
                Notify("Project has been created successfully!", Color.White);
            }

            //open existing OpenBots project
            else if (projectBuilder.Action == frmProjectBuilder.ProjectAction.OpenProject)
            {
                DialogResult result = CheckForUnsavedScripts();
                if (result == DialogResult.Cancel)
                {
                    return(DialogResult.Cancel);
                }

                try
                {
                    //open project
                    string  existingConfigPath = projectBuilder.ExistingConfigPath;
                    Project project            = Project.OpenProject(existingConfigPath);

                    if (existingConfigPath.EndsWith(".config"))
                    {
                        existingConfigPath = existingConfigPath.Replace(".config", ".obconfig");
                    }

                    string mainFileName = project.Main;

                    string mainFilePath = Directory.GetFiles(projectBuilder.ExistingProjectPath, mainFileName, SearchOption.AllDirectories).FirstOrDefault();
                    if (mainFilePath == null)
                    {
                        throw new Exception("Main script not found");
                    }

                    NotifySync("Loading package assemblies...", Color.White);

                    var assemblyList = NugetPackageManager.LoadPackageAssemblies(existingConfigPath);
                    _builder   = AppDomainSetupManager.LoadBuilder(assemblyList, _typeContext.GroupedTypes);
                    AContainer = _builder.Build();

                    ScriptProject     = project;
                    _mainFileName     = mainFileName;
                    ScriptProjectPath = projectBuilder.ExistingProjectPath;
                    uiScriptTabControl.TabPages.Clear();

                    //open Main
                    switch (ScriptProject.ProjectType)
                    {
                    case ProjectType.OpenBots:
                        OpenOpenBotsFile(mainFilePath);
                        break;

                    case ProjectType.Python:
                    case ProjectType.TagUI:
                    case ProjectType.CSScript:
                        OpenTextEditorFile(mainFilePath, ScriptProject.ProjectType);
                        break;
                    }

                    //show success dialog
                    Notify("Project has been opened successfully!", Color.White);
                }
                catch (Exception ex)
                {
                    projectBuilder.Dispose();

                    //show fail dialog
                    Notify("An Error Occured: " + ex.Message, Color.Red);

                    //try adding project again
                    AddProject();
                    return(DialogResult.None);
                }
            }

            projectBuilder.Dispose();

            DirectoryInfo projectDirectoryInfo = new DirectoryInfo(ScriptProjectPath);
            TreeNode      projectNode          = new TreeNode(projectDirectoryInfo.Name);

            projectNode.Text = projectDirectoryInfo.Name;
            projectNode.Tag  = projectDirectoryInfo.FullName;
            projectNode.Nodes.Add("Empty");
            projectNode.ContextMenuStrip = cmsProjectMainFolderActions;
            tvProject.Nodes.Add(projectNode);
            projectNode.Expand();
            LoadCommands(this);

            //save to recent projects
            if (_appSettings.ClientSettings.RecentProjects == null)
            {
                _appSettings.ClientSettings.RecentProjects = new List <string>();
            }

            if (_appSettings.ClientSettings.RecentProjects.Contains(ScriptProjectPath))
            {
                _appSettings.ClientSettings.RecentProjects.Remove(ScriptProjectPath);
            }

            _appSettings.ClientSettings.RecentProjects.Insert(0, ScriptProjectPath);

            if (_appSettings.ClientSettings.RecentProjects.Count > 10)
            {
                _appSettings.ClientSettings.RecentProjects.RemoveAt(10);
            }

            _appSettings.Save(_appSettings);

            return(DialogResult.OK);
        }
        public void AddProject()
        {
            tvProject.Nodes.Clear();
            var projectBuilder = new frmProjectBuilder();

            projectBuilder.ShowDialog();

            //Close OpenBots if add project form is closed at startup
            if (projectBuilder.DialogResult == DialogResult.Cancel && ScriptProject == null)
            {
                Application.Exit();
                return;
            }

            //Create new OpenBots project
            else if (projectBuilder.CreateProject == true)
            {
                DialogResult result = CheckForUnsavedScripts();
                if (result == DialogResult.Cancel)
                {
                    return;
                }

                uiScriptTabControl.TabPages.Clear();
                ScriptProjectPath = projectBuilder.NewProjectPath;

                string                configPath          = Path.Combine(ScriptProjectPath, "project.config");
                string                mainScriptPath      = Path.Combine(ScriptProjectPath, "Main.json");
                string                mainScriptName      = Path.GetFileNameWithoutExtension(mainScriptPath);
                UIListView            mainScriptActions   = NewLstScriptActions(mainScriptName);
                List <ScriptVariable> mainScriptVariables = new List <ScriptVariable>();
                List <ScriptElement>  mainScriptElements  = new List <ScriptElement>();
                ShowMessageCommand    helloWorldCommand   = new ShowMessageCommand();

                helloWorldCommand.v_Message = "Hello World";
                mainScriptActions.Items.Insert(0, CreateScriptCommandListViewItem(helloWorldCommand));

                //Begin saving as main.xml
                ClearSelectedListViewItems();

                try
                {
                    //Serialize main script
                    var mainScript = Script.SerializeScript(mainScriptActions.Items, mainScriptVariables, mainScriptElements,
                                                            mainScriptPath);
                    //Create new project
                    ScriptProject = new Project(projectBuilder.NewProjectName);
                    _mainFileName = ScriptProject.Main;

                    //create config file
                    File.WriteAllText(configPath, JsonConvert.SerializeObject(ScriptProject));

                    OpenFile(mainScriptPath);
                    ScriptFilePath = mainScriptPath;

                    //Show success dialog
                    Notify("Project has been created successfully!", Color.White);
                }
                catch (Exception ex)
                {
                    Notify("An Error Occured: " + ex.Message, Color.Red);
                }
            }

            //Open existing OpenBots project
            else if (projectBuilder.OpenProject == true)
            {
                DialogResult result = CheckForUnsavedScripts();
                if (result == DialogResult.Cancel)
                {
                    return;
                }

                try
                {
                    //Open project
                    ScriptProject = Project.OpenProject(projectBuilder.ExistingConfigPath);
                    _mainFileName = ScriptProject.Main;

                    string mainFilePath = Directory.GetFiles(projectBuilder.ExistingProjectPath, _mainFileName, SearchOption.AllDirectories).FirstOrDefault();
                    if (mainFilePath == null)
                    {
                        throw new Exception("Main script not found");
                    }

                    ScriptProjectPath = projectBuilder.ExistingProjectPath;
                    uiScriptTabControl.TabPages.Clear();

                    //Open Main
                    OpenFile(mainFilePath);
                    //show success dialog
                    Notify("Project has been opened successfully!", Color.White);
                }
                catch (Exception ex)
                {
                    //show fail dialog
                    Notify("An Error Occured: " + ex.Message, Color.Red);
                    //Try adding project again
                    AddProject();
                    return;
                }
            }

            DirectoryInfo projectDirectoryInfo = new DirectoryInfo(ScriptProjectPath);
            TreeNode      projectNode          = new TreeNode(projectDirectoryInfo.Name);

            projectNode.Text = projectDirectoryInfo.Name;
            projectNode.Tag  = projectDirectoryInfo.FullName;
            projectNode.Nodes.Add("Empty");
            projectNode.ContextMenuStrip = cmsProjectMainFolderActions;
            tvProject.Nodes.Add(projectNode);
            projectNode.Expand();
        }