<commit_before>﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CreviceApp
{
    using Microsoft.CodeAnalysis.Scripting;
    using Microsoft.CodeAnalysis.CSharp.Scripting;
    using WinAPI.WindowsHookEx;

    public class MouseGestureForm : Form
    {
        protected Core.FSM.GestureMachine GestureMachine { get; private set; }

        private readonly LowLevelMouseHook mouseHook;
        protected readonly AppGlobal Global;

        // Designer needs this dummy constuctor.
        public MouseGestureForm() : this(new AppGlobal())
        {

        }

        public MouseGestureForm(AppGlobal Global)
        {
            this.mouseHook = new LowLevelMouseHook(MouseProc);
            this.Global = Global;
        }

        // %USERPROFILE%\\AppData\\Roaming\\Crevice\\CreviceApp
        public string DefaultUserDirectory
        {
            get
            {
                return Path.Combine(
                    Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
                    Application.CompanyName,
                    Application.ProductName);
            }
        }

        // Parent directory of UserScriptFile.
        public string UserDirectory
        {
            get
            {
                return Directory.GetParent(UserScriptFile).FullName;
            }
        }
        
        public string UserScriptFile
        {
            get
            {
                var scriptPath = Global.CLIOption.ScriptFile;
                if (Path.IsPathRooted(scriptPath))
                {
                    return scriptPath;
                }
                var uri = new Uri(new Uri(DefaultUserDirectory + "\\"), scriptPath);
                return uri.LocalPath;
            }
        }

        private string GetUserScriptCode()
        {
            var scriptFile = UserScriptFile;
            var dir = Directory.GetParent(scriptFile);
            if (!dir.Exists)
            {
                dir.Create();
            }
            if (!File.Exists(scriptFile))
            {
                File.WriteAllText(scriptFile, Encoding.UTF8.GetString(Properties.Resources.DefaultUserScript), Encoding.UTF8);
            }
            return File.ReadAllText(scriptFile, Encoding.UTF8);
        }

        private string GetUserScriptCacheFile()
        {
            return UserScriptFile + ".cache";
        }

        private Script ParseScript(string userScript)
        {
            var stopwatch = new Stopwatch();
            Verbose.Print("Parsing UserScript...");
            stopwatch.Start();
            var script = CSharpScript.Create(
                userScript,
                ScriptOptions.Default
                    .WithSourceResolver(ScriptSourceResolver.Default.WithBaseDirectory(UserDirectory))
                    .WithMetadataResolver(ScriptMetadataResolver.Default.WithBaseDirectory(UserDirectory))
                    .WithReferences("microlib")                   // microlib.dll
                    .WithReferences("System")                     // System.dll
                    .WithReferences("System.Core")                // System.Core.dll
                    .WithReferences("Microsoft.CSharp")           // Microsoft.CSharp.dll
                    .WithReferences(Assembly.GetEntryAssembly()), // CreviceApp.exe
                                                                  // todo dynamic type
                globalsType: typeof(Core.UserScriptExecutionContext));
            stopwatch.Stop();
            Verbose.Print("UserScript parse finished. ({0})", stopwatch.Elapsed);
            return script;
        }

        private UserScriptAssembly.Cache CompileUserScript(UserScriptAssembly usa, string userScriptCode, Script userScript)
        {
            var stopwatch = new Stopwatch();
            Verbose.Print("Compiling UserScript...");
            stopwatch.Start();
            var compilation = userScript.GetCompilation();
            stopwatch.Stop();
            Verbose.Print("UserScript compilation finished. ({0})", stopwatch.Elapsed);

            var peStream = new MemoryStream();
            var pdbStream = new MemoryStream();
            Verbose.Print("Genarating UserScriptAssembly...");
            stopwatch.Restart();
            compilation.Emit(peStream, pdbStream);
            stopwatch.Stop();
            Verbose.Print("UserScriptAssembly generation finished. ({0})", stopwatch.Elapsed);
            return usa.CreateCache(userScriptCode, peStream.GetBuffer(), pdbStream.GetBuffer());
        }

        private IEnumerable<Core.GestureDefinition> EvaluateUserScript(Script userScript, Core.UserScriptExecutionContext ctx)
        {
            var stopwatch = new Stopwatch();
            Verbose.Print("Compiling UserScript...");
            stopwatch.Start();
            var diagnotstics = userScript.Compile();
            stopwatch.Stop();
            Verbose.Print("UserScript compilation finished. ({ 0})", stopwatch.Elapsed);
            foreach (var dg in diagnotstics.Select((v, i) => new { v, i }))
            {
                Verbose.Print("Diagnotstics[{0}]: {1}", dg.i, dg.v.ToString());
            }
            Verbose.Print("Evaluating UserScript...");
            stopwatch.Restart();
            userScript.RunAsync(ctx).Wait();
            stopwatch.Stop();
            Verbose.Print("UserScript evaluation finished. ({0})", stopwatch.Elapsed);
            return ctx.GetGestureDefinition();
        }

        private IEnumerable<Core.GestureDefinition> EvaluateUserScriptAssembly(UserScriptAssembly.Cache cache, Core.UserScriptExecutionContext ctx)
        {
            var stopwatch = new Stopwatch();
            Verbose.Print("Loading UserScriptAssembly...");
            stopwatch.Start();
            var assembly = Assembly.Load(cache.pe, cache.pdb);
            stopwatch.Stop();
            Verbose.Print("UserScriptAssembly loading finished. ({0})", stopwatch.Elapsed);
            Verbose.Print("Evaluating UserScriptAssembly...");
            stopwatch.Restart();
            var type = assembly.GetType("Submission#0");
            var factory = type.GetMethod("<Factory>");<commit_msg>Fix wrong format expression.  #7<commit_after>﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CreviceApp
{
    using Microsoft.CodeAnalysis.Scripting;
    using Microsoft.CodeAnalysis.CSharp.Scripting;
    using WinAPI.WindowsHookEx;

    public class MouseGestureForm : Form
    {
        protected Core.FSM.GestureMachine GestureMachine { get; private set; }

        private readonly LowLevelMouseHook mouseHook;
        protected readonly AppGlobal Global;

        // Designer needs this dummy constuctor.
        public MouseGestureForm() : this(new AppGlobal())
        {

        }

        public MouseGestureForm(AppGlobal Global)
        {
            this.mouseHook = new LowLevelMouseHook(MouseProc);
            this.Global = Global;
        }

        // %USERPROFILE%\\AppData\\Roaming\\Crevice\\CreviceApp
        public string DefaultUserDirectory
        {
            get
            {
                return Path.Combine(
                    Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
                    Application.CompanyName,
                    Application.ProductName);
            }
        }

        // Parent directory of UserScriptFile.
        public string UserDirectory
        {
            get
            {
                return Directory.GetParent(UserScriptFile).FullName;
            }
        }
        
        public string UserScriptFile
        {
            get
            {
                var scriptPath = Global.CLIOption.ScriptFile;
                if (Path.IsPathRooted(scriptPath))
                {
                    return scriptPath;
                }
                var uri = new Uri(new Uri(DefaultUserDirectory + "\\"), scriptPath);
                return uri.LocalPath;
            }
        }

        private string GetUserScriptCode()
        {
            var scriptFile = UserScriptFile;
            var dir = Directory.GetParent(scriptFile);
            if (!dir.Exists)
            {
                dir.Create();
            }
            if (!File.Exists(scriptFile))
            {
                File.WriteAllText(scriptFile, Encoding.UTF8.GetString(Properties.Resources.DefaultUserScript), Encoding.UTF8);
            }
            return File.ReadAllText(scriptFile, Encoding.UTF8);
        }

        private string GetUserScriptCacheFile()
        {
            return UserScriptFile + ".cache";
        }

        private Script ParseScript(string userScript)
        {
            var stopwatch = new Stopwatch();
            Verbose.Print("Parsing UserScript...");
            stopwatch.Start();
            var script = CSharpScript.Create(
                userScript,
                ScriptOptions.Default
                    .WithSourceResolver(ScriptSourceResolver.Default.WithBaseDirectory(UserDirectory))
                    .WithMetadataResolver(ScriptMetadataResolver.Default.WithBaseDirectory(UserDirectory))
                    .WithReferences("microlib")                   // microlib.dll
                    .WithReferences("System")                     // System.dll
                    .WithReferences("System.Core")                // System.Core.dll
                    .WithReferences("Microsoft.CSharp")           // Microsoft.CSharp.dll
                    .WithReferences(Assembly.GetEntryAssembly()), // CreviceApp.exe
                                                                  // todo dynamic type
                globalsType: typeof(Core.UserScriptExecutionContext));
            stopwatch.Stop();
            Verbose.Print("UserScript parse finished. ({0})", stopwatch.Elapsed);
            return script;
        }

        private UserScriptAssembly.Cache CompileUserScript(UserScriptAssembly usa, string userScriptCode, Script userScript)
        {
            var stopwatch = new Stopwatch();
            Verbose.Print("Compiling UserScript...");
            stopwatch.Start();
            var compilation = userScript.GetCompilation();
            stopwatch.Stop();
            Verbose.Print("UserScript compilation finished. ({0})", stopwatch.Elapsed);

            var peStream = new MemoryStream();
            var pdbStream = new MemoryStream();
            Verbose.Print("Genarating UserScriptAssembly...");
            stopwatch.Restart();
            compilation.Emit(peStream, pdbStream);
            stopwatch.Stop();
            Verbose.Print("UserScriptAssembly generation finished. ({0})", stopwatch.Elapsed);
            return usa.CreateCache(userScriptCode, peStream.GetBuffer(), pdbStream.GetBuffer());
        }

        private IEnumerable<Core.GestureDefinition> EvaluateUserScript(Script userScript, Core.UserScriptExecutionContext ctx)
        {
            var stopwatch = new Stopwatch();
            Verbose.Print("Compiling UserScript...");
            stopwatch.Start();
            var diagnotstics = userScript.Compile();
            stopwatch.Stop();
            Verbose.Print("UserScript compilation finished. ({0})", stopwatch.Elapsed);
            foreach (var dg in diagnotstics.Select((v, i) => new { v, i }))
            {
                Verbose.Print("Diagnotstics[{0}]: {1}", dg.i, dg.v.ToString());
            }
            Verbose.Print("Evaluating UserScript...");
            stopwatch.Restart();
            userScript.RunAsync(ctx).Wait();
            stopwatch.Stop();
            Verbose.Print("UserScript evaluation finished. ({0})", stopwatch.Elapsed);
            return ctx.GetGestureDefinition();
        }

        private IEnumerable<Core.GestureDefinition> EvaluateUserScriptAssembly(UserScriptAssembly.Cache cache, Core.UserScriptExecutionContext ctx)
        {
            var stopwatch = new Stopwatch();
            Verbose.Print("Loading UserScriptAssembly...");
            stopwatch.Start();
            var assembly = Assembly.Load(cache.pe, cache.pdb);
            stopwatch.Stop();
            Verbose.Print("UserScriptAssembly loading finished. ({0})", stopwatch.Elapsed);
            Verbose.Print("Evaluating UserScriptAssembly...");
            stopwatch.Restart();
            var type = assembly.GetType("Submission#0");
            var factory = type.GetMethod("<Factory>");
{'fraction_non_alphanumeric': 0.09655920644761315, 'fraction_numerical': 0.0019373837569745815, 'mean_word_length': 1.8164556962025316, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5504233', 'n_tokens_mistral': 3197, 'n_tokens_neox': 3067, 'n_words': 605}