<commit_before>﻿using FlaUI.Core;
using FlaUI.Core.AutomationElements;
using FlaUI.Core.AutomationElements.Infrastructure;
using FlaUI.Core.Definitions;
using FlaUI.Core.Input;
using FlaUI.Core.Shapes;
using FlaUI.Core.Tools;
using FlaUI.UIA3;
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Management;
using System.Text.RegularExpressions;
using VcEngineAutomation.Extensions;
using VcEngineAutomation.Models;
using VcEngineAutomation.Panels;
using VcEngineAutomation.Ribbons;
using VcEngineAutomation.Utils;
using VcEngineAutomation.Windows;

namespace VcEngineAutomation
{
    public class VcEngine
    {
        private readonly Lazy<AutomationElement> viewPort;
        private readonly Lazy<AutomationElement> quickAccessToolBar;
        // Hardcoded for now until other ways to get locale for application
        private readonly Lazy<CultureInfo> appCultureInfo = new Lazy<CultureInfo>(() => CultureInfo.GetCultureInfoByIetfLanguageTag("en-US"));
        private readonly Lazy<Button> lazyUndoButton;
        private readonly Lazy<Button> lazyRedoButton;

        public static string DefaultInstallationPath => Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "Visual Components", "Visual Components Professional");

        public DockedTabRetriever TabRetriever { get; }
        public Application Application { get; }
        public AutomationBase Automation { get; }

        public bool IsR8 { get; }
        public bool IsR7 { get; }
        public bool IsR7OrAbove { get; }
        public bool IsR6 { get; }
        public bool IsR5 { get;  }
        public Window MainWindow { get; }
        public Ribbon Ribbon { get; }
        public string MainWindowName { get; }
        public AutomationElement QuickAccessToolbar => quickAccessToolBar.Value;
        public ApplicationMenu ApplicationMenu { get; }
        public Options Options { get; }
        public Camera Camera { get; }
        public Visual3DToolbar Visual3DToolbar { get; }
        public PropertiesPanel PropertiesPanel => Retry.WhileException(() => new PropertiesPanel(this, false), TimeSpan.FromSeconds(5), TimeSpan.FromMilliseconds(200));
        public PropertiesPanel DrawingPropertiesPanel => Retry.WhileException(() => new PropertiesPanel(this, true), TimeSpan.FromSeconds(5), TimeSpan.FromMilliseconds(200));
        public ECataloguePanel ECataloguePanel { get; }
        public OutputPanel OutputPanel { get; }
        public AutomationElement ViewPort => viewPort.Value;
        public World World { get; }
        public CultureInfo CultureInfo => appCultureInfo.Value;

        public VcEngine(Application application, AutomationBase automation)
        {
            Application = application;
            Automation = automation;

            var fileVersionInfo = GetVersionInfo(Process.GetProcessById(application.ProcessId));
            IsR5 = fileVersionInfo.ProductVersion.StartsWith("4.0.2");
            IsR6 = fileVersionInfo.FileVersion.StartsWith("4.0.3");
            IsR7 = fileVersionInfo.FileVersion.StartsWith("4.0.4");
            IsR8 = fileVersionInfo.FileVersion.StartsWith("4.0.5");
            IsR7OrAbove = fileVersionInfo.FileMajorPart >= 4 
                && fileVersionInfo.FileMinorPart >= 0 
                && fileVersionInfo.FileBuildPart >= 4;

            Console.WriteLine("VcEngine Version: " + (IsR5 ? "R5" : IsR6 ? "R6" : IsR7 ? "R7" : IsR8 ? "R8" : fileVersionInfo.FileVersion));
            MainWindowName = fileVersionInfo.FileDescription;

            Console.WriteLine("Waiting for application main window");
            MainWindow = Retry.WhileException(() => Application.GetMainWindow(automation), TimeSpan.FromMinutes(2), TimeSpan.FromMilliseconds(200));
            TabRetriever = new DockedTabRetriever(MainWindow);

            Console.WriteLine("Waiting for main ribbon");
            Tab mainTab = Retry.WhileException(() => MainWindow.FindFirstDescendant(cf => cf.ByAutomationId("XamRibbonTabs")).AsTab(), TimeSpan.FromMinutes(2));

            viewPort = new Lazy<AutomationElement>(() => MainWindow.FindFirstDescendant(cf => cf.ByAutomationId(IsR8 ? "Viewport" : "viewportContentPane")));
            quickAccessToolBar = new Lazy<AutomationElement>(() => MainWindow.FindFirstDescendant(cf => cf.ByClassName("QuickAccessToolbar")));
            World = new World(this);
            Ribbon = new Ribbon(this, MainWindow, mainTab);
            ApplicationMenu = new ApplicationMenu(this);
            Visual3DToolbar = new Visual3DToolbar(this);
            Options = new Options(ApplicationMenu);
            Camera = new Camera(this);<commit_msg>Updated support for R8 special version (4.0.6)<commit_after>﻿using FlaUI.Core;
using FlaUI.Core.AutomationElements;
using FlaUI.Core.AutomationElements.Infrastructure;
using FlaUI.Core.Definitions;
using FlaUI.Core.Input;
using FlaUI.Core.Shapes;
using FlaUI.Core.Tools;
using FlaUI.UIA3;
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Management;
using System.Text.RegularExpressions;
using VcEngineAutomation.Extensions;
using VcEngineAutomation.Models;
using VcEngineAutomation.Panels;
using VcEngineAutomation.Ribbons;
using VcEngineAutomation.Utils;
using VcEngineAutomation.Windows;

namespace VcEngineAutomation
{
    public class VcEngine
    {
        private readonly Lazy<AutomationElement> viewPort;
        private readonly Lazy<AutomationElement> quickAccessToolBar;
        // Hardcoded for now until other ways to get locale for application
        private readonly Lazy<CultureInfo> appCultureInfo = new Lazy<CultureInfo>(() => CultureInfo.GetCultureInfoByIetfLanguageTag("en-US"));
        private readonly Lazy<Button> lazyUndoButton;
        private readonly Lazy<Button> lazyRedoButton;

        public static string DefaultInstallationPath => Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "Visual Components", "Visual Components Professional");

        public DockedTabRetriever TabRetriever { get; }
        public Application Application { get; }
        public AutomationBase Automation { get; }

        public bool IsR9 { get; }
        public bool IsR9OrAbove { get; }
        public bool IsR8 { get; }
        public bool IsR8OrAbove { get; }
        public bool IsR7 { get; }
        public bool IsR7OrAbove { get; }
        public bool IsR6 { get; }
        public bool IsR5 { get;  }
        public string Version { get; }
        public Window MainWindow { get; }
        public Ribbon Ribbon { get; }
        public string MainWindowName { get; }
        public AutomationElement QuickAccessToolbar => quickAccessToolBar.Value;
        public ApplicationMenu ApplicationMenu { get; }
        public Options Options { get; }
        public Camera Camera { get; }
        public Visual3DToolbar Visual3DToolbar { get; }
        public PropertiesPanel PropertiesPanel => Retry.WhileException(() => new PropertiesPanel(this, false), TimeSpan.FromSeconds(5), TimeSpan.FromMilliseconds(200));
        public PropertiesPanel DrawingPropertiesPanel => Retry.WhileException(() => new PropertiesPanel(this, true), TimeSpan.FromSeconds(5), TimeSpan.FromMilliseconds(200));
        public ECataloguePanel ECataloguePanel { get; }
        public OutputPanel OutputPanel { get; }
        public AutomationElement ViewPort => viewPort.Value;
        public World World { get; }
        public CultureInfo CultureInfo => appCultureInfo.Value;

        public VcEngine(Application application, AutomationBase automation)
        {
            Application = application;
            Automation = automation;

            var fileVersionInfo = GetVersionInfo(Process.GetProcessById(application.ProcessId));
            Version = fileVersionInfo.FileVersion;
            IsR7OrAbove = fileVersionInfo.FileMajorPart >= 4
                          && fileVersionInfo.FileMinorPart >= 0
                          && fileVersionInfo.FileBuildPart >= 4;
            IsR8OrAbove = fileVersionInfo.FileMajorPart >= 4
                          && fileVersionInfo.FileMinorPart >= 0
                          && fileVersionInfo.FileBuildPart >= 5;
            IsR9OrAbove = fileVersionInfo.FileMajorPart >= 4
                          && fileVersionInfo.FileMinorPart >= 1;
            IsR5 = fileVersionInfo.ProductVersion.StartsWith("4.0.2");
            IsR6 = Version.StartsWith("4.0.3");
            IsR7 = Version.StartsWith("4.0.4");
            IsR8 = Version.StartsWith("4.0.5") || Version.StartsWith("4.0.6");
            IsR9 = Version.StartsWith("4.1.0");

            Console.WriteLine($"VcEngine Version: {Version}");
            MainWindowName = fileVersionInfo.FileDescription;

            Console.WriteLine("Waiting for application main window");
            MainWindow = Retry.WhileException(() => Application.GetMainWindow(automation), TimeSpan.FromMinutes(2), TimeSpan.FromMilliseconds(200));
            TabRetriever = new DockedTabRetriever(MainWindow);

            Console.WriteLine("Waiting for main ribbon");
            Tab mainTab = Retry.WhileException(() => MainWindow.FindFirstDescendant(cf => cf.ByAutomationId("XamRibbonTabs")).AsTab(), TimeSpan.FromMinutes(2));

            viewPort = new Lazy<AutomationElement>(() => MainWindow.FindFirstDescendant(cf => cf.ByAutomationId(IsR8OrAbove ? "Viewport" : "viewportContentPane")));
            quickAccessToolBar = new Lazy<AutomationElement>(() => MainWindow.FindFirstDescendant(cf => cf.ByClassName("QuickAccessToolbar")));
            World = new World(this);
            Ribbon = new Ribbon(this, MainWindow, mainTab);
            ApplicationMenu = new ApplicationMenu(this);
            Visual3DToolbar = new Visual3DToolbar(this);
            Options = new Options(ApplicationMenu);
            Camera = new Camera(this);
{'fraction_non_alphanumeric': 0.09385340419131656, 'fraction_numerical': 0.01173167552391457, 'mean_word_length': 3.2715203426124195, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 62, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4021791', 'n_tokens_mistral': 2713, 'n_tokens_neox': 2653, 'n_words': 610}