<commit_before>﻿using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Windows.Forms;
using StreamCompanionTypes.DataTypes;
using StreamCompanionTypes.Interfaces;

namespace osuOverlay
{
    public class IngameOverlay : IPlugin, ISettingsProvider, ISaveRequester, IMapDataGetter, IDisposable
    {
        private ISettingsHandler _settings;
        private ISaver _saver;
        public bool Started { get; set; }
        public string SettingGroup { get; } = "General";
        private const string FilesFolder = "Dlls";
        private IngameOverlaySettings _overlaySettings;
        private ILogger _logger;

        private Thread _workerThread;
        private Process _currentOsuProcess;
        private bool _pauseProcessTracking;
        private bool _injectedAtleastOnce = false;


        public string Description { get; } = "";
        public string Name { get; } = nameof(IngameOverlay);
        public string Author { get; } = "Piotrekol";
        public string Url { get; } = "";
        public string UpdateUrl { get; } = "";


        public void Start(ILogger logger)
        {
            Started = true;
            _logger = logger;

            if (_settings.Get<bool>(PluginSettings.EnableIngameOverlay))
            {
                CopyFreeType();
                _workerThread = new Thread(WatchForProcessStart);
                _workerThread.Start();
            }
        }

        public void WatchForProcessStart()
        {
            try
            {
                while (true)
                {
                    
                    if (_currentOsuProcess == null || SafeHasExited(_currentOsuProcess))
                    {
                        _currentOsuProcess = null;
                        foreach (var process in Process.GetProcesses())
                        {
                            if (process.ProcessName == "osu!")
                            {
                                _currentOsuProcess = process;
                                break;
                            }
                        }
                        if (_currentOsuProcess != null)
                        {
                            if (Inject(!_injectedAtleastOnce))
                                _injectedAtleastOnce = true;
                        }
                    }

                    while (_pauseProcessTracking)
                    {
                        Thread.Sleep(1000);
                    }
                    Thread.Sleep(2000);
                }
            }
            catch (ThreadAbortException ex)
            {
            }
        }

        private void CopyFreeType()
        {
            var osuFolderDirectory = _settings.Get<string>(PluginSettings.MainOsuDirectory);
            if (Directory.Exists(osuFolderDirectory))
            {
                var newFreeTypeLocation = Path.Combine(osuFolderDirectory, "FreeType.dll");
                if (File.Exists(newFreeTypeLocation))
                    return;

                File.Copy(GetFullFreeTypeLocation(), newFreeTypeLocation);
            }
        }
        public bool SafeHasExited(Process process)
        {

            try
            {
                return process.HasExited;
            }
            catch
            {
                return true;
            }
        }

        private bool Inject(bool showErrors = false)
        {
            DllInjector dllInjector = DllInjector.GetInstance;
            var result = dllInjector.Inject("osu!", GetFullDllLocation());
            if (result != DllInjectionResult.Success)
            {
                string message = "";
                switch (result)
                {
                    case DllInjectionResult.GameProcessNotFound:
                        message = "osu! is not running yet - restart StreamCompanion after starting it!";
                        break;
                    case DllInjectionResult.DllNotFound:
                        message =
                            "Could not find osuOverlay file to add to osu!... this shouldn't happen, if it does(you see this message) please report this.";
                        break;
                    case DllInjectionResult.InjectionFailed:
                        message =
                            "Could not add overlay to osu! most likely SC doesn't have enough premissions - restart SC as administrator and try again. If that doesn't solve it - please report ";
                        break;
                }
                if (showErrors && result != DllInjectionResult.GameProcessNotFound)
                    MessageBox.Show(message, "StreamCompanion - ingameOverlay Error!", MessageBoxButtons.OK,
                        MessageBoxIcon.Error);
                _logger?.Log(message, LogLevel.Basic);
                return false;
            }

            return true;
        }

        private string GetFilesFolder() => Path.Combine(_saver.SaveDirectory, FilesFolder);

        private string GetFullDllLocation() => Path.Combine(GetFilesFolder(), "osuOverlay.dll");<commit_msg>Fix ingameOverlay plugin dlls location<commit_after>﻿using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Windows.Forms;
using StreamCompanionTypes.DataTypes;
using StreamCompanionTypes.Interfaces;

namespace osuOverlay
{
    public class IngameOverlay : IPlugin, ISettingsProvider, ISaveRequester, IMapDataGetter, IDisposable
    {
        private ISettingsHandler _settings;
        private ISaver _saver;
        public bool Started { get; set; }
        public string SettingGroup { get; } = "General";
        private IngameOverlaySettings _overlaySettings;
        private ILogger _logger;

        private Thread _workerThread;
        private Process _currentOsuProcess;
        private bool _pauseProcessTracking;
        private bool _injectedAtleastOnce = false;


        public string Description { get; } = "";
        public string Name { get; } = nameof(IngameOverlay);
        public string Author { get; } = "Piotrekol";
        public string Url { get; } = "";
        public string UpdateUrl { get; } = "";


        public void Start(ILogger logger)
        {
            Started = true;
            _logger = logger;

            if (_settings.Get<bool>(PluginSettings.EnableIngameOverlay))
            {
                CopyFreeType();
                _workerThread = new Thread(WatchForProcessStart);
                _workerThread.Start();
            }
        }

        public void WatchForProcessStart()
        {
            try
            {
                while (true)
                {

                    if (_currentOsuProcess == null || SafeHasExited(_currentOsuProcess))
                    {
                        _currentOsuProcess = null;
                        foreach (var process in Process.GetProcesses())
                        {
                            if (process.ProcessName == "osu!")
                            {
                                _currentOsuProcess = process;
                                break;
                            }
                        }
                        if (_currentOsuProcess != null)
                        {
                            if (Inject(!_injectedAtleastOnce))
                                _injectedAtleastOnce = true;
                        }
                    }

                    while (_pauseProcessTracking)
                    {
                        Thread.Sleep(1000);
                    }
                    Thread.Sleep(2000);
                }
            }
            catch (ThreadAbortException ex)
            {
            }
        }

        private void CopyFreeType()
        {
            var osuFolderDirectory = _settings.Get<string>(PluginSettings.MainOsuDirectory);
            if (Directory.Exists(osuFolderDirectory))
            {
                var newFreeTypeLocation = Path.Combine(osuFolderDirectory, "FreeType.dll");
                if (File.Exists(newFreeTypeLocation))
                    return;

                File.Copy(GetFullFreeTypeLocation(), newFreeTypeLocation);
            }
        }
        public bool SafeHasExited(Process process)
        {

            try
            {
                return process.HasExited;
            }
            catch
            {
                return true;
            }
        }

        private bool Inject(bool showErrors = false)
        {
            DllInjector dllInjector = DllInjector.GetInstance;
            var result = dllInjector.Inject("osu!", GetFullDllLocation());
            if (result != DllInjectionResult.Success)
            {
                string message = "";
                switch (result)
                {
                    case DllInjectionResult.GameProcessNotFound:
                        message = "osu! is not running yet - restart StreamCompanion after starting it!";
                        break;
                    case DllInjectionResult.DllNotFound:
                        message =
                            "Could not find osuOverlay file to add to osu!... this shouldn't happen, if it does(you see this message) please report this.";
                        break;
                    case DllInjectionResult.InjectionFailed:
                        message =
                            "Could not add overlay to osu! most likely SC doesn't have enough premissions - restart SC as administrator and try again. If that doesn't solve it - please report ";
                        break;
                }
                if (showErrors && result != DllInjectionResult.GameProcessNotFound)
                    MessageBox.Show(message, "StreamCompanion - ingameOverlay Error!", MessageBoxButtons.OK,
                        MessageBoxIcon.Error);
                _logger?.Log(message, LogLevel.Basic);
                return false;
            }

            return true;
        }

        private string GetFilesFolder() => Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Plugins", "Dlls");

        private string GetFullDllLocation() => Path.Combine(GetFilesFolder(), "osuOverlay.dll");
{'fraction_non_alphanumeric': 0.07105804052070079, 'fraction_numerical': 0.0015660174219438191, 'mean_word_length': 1.329154319580579, 'pattern_counts': {'":': 0, '<': 7, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5507601', 'n_tokens_mistral': 2445, 'n_tokens_neox': 2311, 'n_words': 559}