Example #1
0
        private async void Command_Decompile(MainWindow win)
        {
            OpenFileDialog ofd = new OpenFileDialog();

            ofd.Filter = "Sourcepawn Plugins (*.smx)|*.smx";
            ofd.Title  = Program.Translations.GetLanguage("ChDecomp");
            var result = ofd.ShowDialog();

            if (result.Value)
            {
                if (!string.IsNullOrWhiteSpace(ofd.FileName))
                {
                    FileInfo fInfo = new FileInfo(ofd.FileName);
                    if (fInfo.Exists)
                    {
                        ProgressDialogController task = null;
                        if (win != null)
                        {
                            task = await this.ShowProgressAsync(Program.Translations.GetLanguage("Decompiling"), fInfo.FullName, false, this.MetroDialogOptions);

                            MainWindow.ProcessUITasks();
                        }
                        string destFile = fInfo.FullName + ".sp";
                        File.WriteAllText(destFile, LysisDecompiler.Analyze(fInfo), Encoding.UTF8);
                        TryLoadSourceFile(destFile, true, false);
                        if (task != null)
                        {
                            await task.CloseAsync();
                        }
                    }
                }
            }
        }
Example #2
0
        private async void Compile_SPScripts(bool All = true)
        {
            if (InCompiling)
            {
                return;
            }
            Command_SaveAll();
            InCompiling = true;
            compiledFiles.Clear();
            compiledFileNames.Clear();
            nonUploadedFiles.Clear();
            var      c             = Program.Configs[Program.SelectedConfig];
            FileInfo spCompInfo    = null;
            bool     SpCompFound   = false;
            bool     PressedEscape = false;

            foreach (string dir in c.SMDirectories)
            {
                spCompInfo = new FileInfo(Path.Combine(dir, "spcomp.exe"));
                if (spCompInfo.Exists)
                {
                    SpCompFound = true;
                    break;
                }
            }
            if (SpCompFound)
            {
                List <string> filesToCompile = new List <string>();
                if (All)
                {
                    EditorElement[] editors = GetAllEditorElements();
                    if (editors == null)
                    {
                        InCompiling = false;
                        return;
                    }
                    for (int i = 0; i < editors.Length; ++i)
                    {
                        if (editors[i].CompileBox.IsChecked.Value)
                        {
                            filesToCompile.Add(editors[i].FullFilePath);
                        }
                    }
                }
                else
                {
                    EditorElement ee = GetCurrentEditorElement();
                    if (ee == null)
                    {
                        InCompiling = false;
                        return;
                    }

                    /*
                    ** I've struggled a bit here. Should i check, if the CompileBox is checked
                    ** and only compile if it's checked or should it be ignored and compiled anyway?
                    ** I decided, to compile anyway but give me feedback/opinions.
                    */
                    if (ee.FullFilePath.EndsWith(".sp"))
                    {
                        filesToCompile.Add(ee.FullFilePath);
                    }
                }
                int compileCount = filesToCompile.Count;
                if (compileCount > 0)
                {
                    ErrorResultGrid.Items.Clear();
                    var progressTask = await this.ShowProgressAsync(Program.Translations.Compiling, "", false, this.MetroDialogOptions);

                    progressTask.SetProgress(0.0);
                    StringBuilder stringOutput        = new StringBuilder();
                    Regex         errorFilterRegex    = new Regex(@"^(?<file>.+?)\((?<line>[0-9]+(\s*--\s*[0-9]+)?)\)\s*:\s*(?<type>[a-zA-Z]+\s+([a-zA-Z]+\s+)?[0-9]+)\s*:(?<details>.+)", RegexOptions.Compiled | RegexOptions.ExplicitCapture | RegexOptions.Multiline);
                    string        destinationFileName = null;
                    FileInfo      fileInfo            = null;
                    string        outFile             = null;
                    for (int i = 0; i < compileCount; ++i)
                    {
                        if (!InCompiling)                         //pressed escape
                        {
                            PressedEscape = true;
                            break;
                        }
                        string file = filesToCompile[i];
                        progressTask.SetMessage(file);
                        MainWindow.ProcessUITasks();
                        fileInfo = new FileInfo(file);
                        stringOutput.AppendLine(fileInfo.Name);
                        if (fileInfo.Exists)
                        {
                            using (Process process = new Process())
                            {
                                process.StartInfo.WorkingDirectory = fileInfo.DirectoryName;
                                process.StartInfo.UseShellExecute  = true;
                                process.StartInfo.WindowStyle      = ProcessWindowStyle.Hidden;
                                process.StartInfo.CreateNoWindow   = true;
                                process.StartInfo.FileName         = spCompInfo.FullName;
                                destinationFileName = ShortenScriptFileName(fileInfo.Name) + ".smx";
                                outFile             = Path.Combine(fileInfo.DirectoryName, destinationFileName);
                                if (File.Exists(outFile))
                                {
                                    File.Delete(outFile);
                                }
                                string errorFile = Environment.CurrentDirectory + @"\sourcepawn\errorfiles\error_" + Environment.TickCount.ToString() + "_" + file.GetHashCode().ToString("X") + "_" + i.ToString() + ".txt";
                                if (File.Exists(errorFile))
                                {
                                    File.Delete(errorFile);
                                }

                                StringBuilder includeDirectories = new StringBuilder();
                                foreach (string dir in c.SMDirectories)
                                {
                                    includeDirectories.Append(" -i=\"" + dir + "\"");
                                }

                                string includeStr = string.Empty;
                                includeStr = includeDirectories.ToString();

                                process.StartInfo.Arguments = "\"" + fileInfo.FullName + "\" -o=\"" + outFile + "\" -e=\"" + errorFile + "\"" + includeStr + " -O=" + c.OptimizeLevel.ToString() + " -v=" + c.VerboseLevel.ToString();
                                progressTask.SetProgress((((double)(i + 1)) - 0.5d) / ((double)compileCount));
                                string execResult = ExecuteCommandLine(c.PreCmd, fileInfo.DirectoryName, c.CopyDirectory, fileInfo.FullName, fileInfo.Name, outFile, destinationFileName);
                                if (!string.IsNullOrWhiteSpace(execResult))
                                {
                                    stringOutput.AppendLine(execResult.Trim(new char[] { '\n', '\r' }));
                                }
                                MainWindow.ProcessUITasks();
                                try
                                {
                                    process.Start();
                                    process.WaitForExit();
                                }
                                catch (Exception)
                                {
                                    InCompiling = false;
                                }
                                if (!InCompiling) //cannot await in catch
                                {
                                    await progressTask.CloseAsync();

                                    await this.ShowMessageAsync(Program.Translations.SPCompNotStarted, Program.Translations.Error, MessageDialogStyle.Affirmative, this.MetroDialogOptions);

                                    return;
                                }
                                if (File.Exists(errorFile))
                                {
                                    string errorStr = File.ReadAllText(errorFile);
                                    stringOutput.AppendLine(errorStr.Trim(new char[] { '\n', '\r' }));
                                    MatchCollection mc = errorFilterRegex.Matches(errorStr);
                                    for (int j = 0; j < mc.Count; ++j)
                                    {
                                        ErrorResultGrid.Items.Add(new ErrorDataGridRow()
                                        {
                                            file    = mc[j].Groups["file"].Value.Trim(),
                                            line    = mc[j].Groups["line"].Value.Trim(),
                                            type    = mc[j].Groups["type"].Value.Trim(),
                                            details = mc[j].Groups["details"].Value.Trim()
                                        });
                                    }
                                    File.Delete(errorFile);
                                }
                                stringOutput.AppendLine(Program.Translations.Done);
                                if (File.Exists(outFile))
                                {
                                    compiledFiles.Add(outFile);
                                    nonUploadedFiles.Add(outFile);
                                    compiledFileNames.Add(destinationFileName);
                                }
                                string execResult_Post = ExecuteCommandLine(c.PostCmd, fileInfo.DirectoryName, c.CopyDirectory, fileInfo.FullName, fileInfo.Name, outFile, destinationFileName);
                                if (!string.IsNullOrWhiteSpace(execResult_Post))
                                {
                                    stringOutput.AppendLine(execResult_Post.Trim(new char[] { '\n', '\r' }));
                                }
                                stringOutput.AppendLine();
                                progressTask.SetProgress(((double)(i + 1)) / ((double)compileCount));
                                MainWindow.ProcessUITasks();
                            }
                        }
                    }
                    if (!PressedEscape)
                    {
                        progressTask.SetProgress(1.0);
                        CompileOutput.Text = stringOutput.ToString();
                        if (c.AutoCopy)
                        {
                            Copy_Plugins(true);
                        }
                        if (CompileOutputRow.Height.Value < 11.0)
                        {
                            CompileOutputRow.Height = new GridLength(200.0);
                        }
                    }
                    await progressTask.CloseAsync();
                }
            }
            else
            {
                await this.ShowMessageAsync(Program.Translations.Error, Program.Translations.SPCompNotFound, MessageDialogStyle.Affirmative, this.MetroDialogOptions);
            }
            InCompiling = false;
        }