<commit_before>﻿using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using osu_StreamCompanion.Code.Interfaces;

namespace osu_StreamCompanion.Code.Core.Maps
{
    
    public class Msn : IDisposable
    {
        private IntPtr m_hwnd;
        private Dictionary<string,string> _osuStatus= new Dictionary<string, string>(); 
        private static WNDCLASS lpWndClass;
        private List<IMsnGetter> _msnHandlers;
        private const string lpClassName = "MsnMsgrUIManager";
        public bool Suspend { get; set; }
        protected virtual void OnMSNStringChanged()
        {
            if (Suspend)
                return;
            Task.Factory.StartNew<int>(() =>
            {
                for (int i = 0; i < _msnHandlers.Count; i++)
                {
                    _msnHandlers[i].SetNewMsnString(new Dictionary<string, string>(_osuStatus));
                }
                return 1;
            });
        }

        public Msn(List<IMsnGetter> msnHandlers)
        {
            _msnHandlers = msnHandlers;
            lpWndClass = new WNDCLASS
            {
                lpszClassName = lpClassName,
                lpfnWndProc = new WndProc(this.CustomWndProc)
            };

            ushort num = RegisterClassW(ref lpWndClass);
            int num2 = Marshal.GetLastWin32Error();
            if ((num == 0) && (num2 != 0x582))
            {
                throw new Exception("Could not register window class");
            }
            this.m_hwnd = CreateWindowExW(0, lpClassName, string.Empty, 0, 0, 0, 0, 0, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
        }

        [DllImport("user32.dll", SetLastError = true)]
        private static extern IntPtr CreateWindowExW(uint dwExStyle, [MarshalAs(UnmanagedType.LPWStr)] string lpClassName, [MarshalAs(UnmanagedType.LPWStr)] string lpWindowName, uint dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, IntPtr hMenu, IntPtr hInstance, IntPtr lpParam);
        private IntPtr CustomWndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam)
        {
            if (msg == 0x4a)
            {
                COPYDATASTRUCT copydatastruct =
                    (COPYDATASTRUCT)Marshal.PtrToStructure(lParam, typeof(COPYDATASTRUCT));
                string str = Marshal.PtrToStringUni(copydatastruct.lpData, copydatastruct.cbData / 2);
                string[] separator = new string[] { @"\0" };
                string[] sourceArray = str.Split(separator, StringSplitOptions.None);
                if (sourceArray.Length > 8)
                {
                    _osuStatus["artist"] = sourceArray[5];
                    _osuStatus["title"] = sourceArray[4];
                    _osuStatus["diff"] = sourceArray[7];
                    _osuStatus["status"] = sourceArray[3].Split(new[] { ' ' }, 2)[0];

                    OnMSNStringChanged();
                }
            }
            return DefWindowProcW(hWnd, msg, wParam, lParam);
<commit_msg>Fix msn trying to get nonexistent data<commit_after>﻿using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using osu_StreamCompanion.Code.Interfaces;

namespace osu_StreamCompanion.Code.Core.Maps
{
    
    public class Msn : IDisposable
    {
        private IntPtr m_hwnd;
        private Dictionary<string,string> _osuStatus= new Dictionary<string, string>(); 
        private static WNDCLASS lpWndClass;
        private List<IMsnGetter> _msnHandlers;
        private const string lpClassName = "MsnMsgrUIManager";
        public bool Suspend { get; set; }
        protected virtual void OnMSNStringChanged()
        {
            if (Suspend)
                return;
            Task.Factory.StartNew<int>(() =>
            {
                for (int i = 0; i < _msnHandlers.Count; i++)
                {
                    _msnHandlers[i].SetNewMsnString(new Dictionary<string, string>(_osuStatus));
                }
                return 1;
            });
        }

        public Msn(List<IMsnGetter> msnHandlers)
        {
            _msnHandlers = msnHandlers;
            lpWndClass = new WNDCLASS
            {
                lpszClassName = lpClassName,
                lpfnWndProc = new WndProc(this.CustomWndProc)
            };

            ushort num = RegisterClassW(ref lpWndClass);
            int num2 = Marshal.GetLastWin32Error();
            if ((num == 0) && (num2 != 0x582))
            {
                throw new Exception("Could not register window class");
            }
            this.m_hwnd = CreateWindowExW(0, lpClassName, string.Empty, 0, 0, 0, 0, 0, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
        }

        [DllImport("user32.dll", SetLastError = true)]
        private static extern IntPtr CreateWindowExW(uint dwExStyle, [MarshalAs(UnmanagedType.LPWStr)] string lpClassName, [MarshalAs(UnmanagedType.LPWStr)] string lpWindowName, uint dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, IntPtr hMenu, IntPtr hInstance, IntPtr lpParam);
        private IntPtr CustomWndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam)
        {
            if (msg == 0x4a)
            {
                COPYDATASTRUCT copydatastruct =
                    (COPYDATASTRUCT)Marshal.PtrToStructure(lParam, typeof(COPYDATASTRUCT));

                var ptr = copydatastruct.lpData;
                if (ptr != IntPtr.Zero)
                {
                    string str = Marshal.PtrToStringUni(ptr, copydatastruct.cbData / 2);
                    string[] separator = new string[] { @"\0" };
                    string[] sourceArray = str.Split(separator, StringSplitOptions.None);
                    if (sourceArray.Length > 8)
                    {
                        _osuStatus["artist"] = sourceArray[5];
                        _osuStatus["title"] = sourceArray[4];
                        _osuStatus["diff"] = sourceArray[7];
                        _osuStatus["status"] = sourceArray[3].Split(new[] { ' ' }, 2)[0];

                        OnMSNStringChanged();
                    }
                }
            }
            return DefWindowProcW(hWnd, msg, wParam, lParam);

{'fraction_non_alphanumeric': 0.09729988816104809, 'fraction_numerical': 0.009586195877935773, 'mean_word_length': 1.8834638415476739, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 19, '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': '4727466', 'n_tokens_mistral': 1845, 'n_tokens_neox': 1758, 'n_words': 390}