<commit_before>﻿using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows.Forms;
using StreamCompanionTypes.DataTypes;
using StreamCompanionTypes.Interfaces;

namespace MSNEventSource
{
    public class Msn : IDisposable, IPlugin, IOsuEventSource, IFirstRunControlProvider
    {
        private IntPtr m_hwnd;
        private Dictionary<string, string> _osuStatus = new Dictionary<string, string>();
        private static WNDCLASS lpWndClass;
        private ILogger _logger;
        private const string lpClassName = "MsnMsgrUIManager";
        public bool Suspend { get; set; }
        private string _lastMsnString = "";
        public bool Started { get; set; }
        public void Start(ILogger logger)
        {
            _logger = logger;
            Started = true;
        }

        public string Description { get; } = "Provides basic osu! events using old MSN communication that still exists in osu!";
        public string Name { get; } = nameof(Msn);
        public string Author { get; } = "Piotrekol";
        public string Url { get; } = "";
        public string UpdateUrl { get; } = "";
        public EventHandler<MapSearchArgs> NewOsuEvent { get; set; }

        protected virtual void OnMsnStringChanged()
        {
            if (Suspend)
                return;
            Task.Factory.StartNew<int>(() =>
            {
                _firstRunUserControl?.GotMsn(string.Format("{0} - {1}", _osuStatus["title"], _osuStatus["artist"]));

                var args = CreateArgs(_osuStatus);

                if(args != null)
                    NewOsuEvent?.Invoke(this, args);

                return 1;
            });
        }

        private MapSearchArgs CreateArgs(Dictionary<string, string> osuStatus)
        {
            OsuStatus status = osuStatus["status"] == "Listening" ? OsuStatus.Listening
                : osuStatus["status"] == "Playing" ? OsuStatus.Playing
                    : osuStatus["status"] == "Watching" ? OsuStatus.Watching
                        : osuStatus["status"] == "Editing" ? OsuStatus.Editing
                            : OsuStatus.Null;

            osuStatus["raw"] = string.Format("{0} - {1}", osuStatus["title"], osuStatus["artist"]);
            bool isFalsePlay;
            lock (this)
            {
                isFalsePlay = IsFalsePlay(osuStatus["raw"], status, _lastMsnString);
            }
            if (isFalsePlay)
            {
                _logger.Log(">ignoring second MSN string...", LogLevel.Advanced);
            }
            else
            {
                _lastMsnString = osuStatus["raw"];
                _logger.Log("", LogLevel.Advanced);
                string result = ">Got ";
                foreach (var v in osuStatus)
                {
                    if (v.Key != "raw") result = result + $"{v.Key}: \"{v.Value}\" ";
                }
                _logger.Log(result, LogLevel.Basic);

                var searchArgs = new MapSearchArgs("Msn")
                {
                    Artist = osuStatus["artist"] ?? "",
                    Title = osuStatus["title"] ?? "",
                    Diff = osuStatus["diff"] ?? "",
                    Raw = osuStatus["raw"] ?? "",
                    Status = status,

                };
                return searchArgs;<commit_msg>Fix: missing nullchecks in Msn plugin<commit_after>﻿using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows.Forms;
using StreamCompanionTypes.DataTypes;
using StreamCompanionTypes.Interfaces;

namespace MSNEventSource
{
    public class Msn : IDisposable, IPlugin, IOsuEventSource, IFirstRunControlProvider
    {
        private IntPtr m_hwnd;
        private Dictionary<string, string> _osuStatus = new Dictionary<string, string>();
        private static WNDCLASS lpWndClass;
        private ILogger _logger;
        private const string lpClassName = "MsnMsgrUIManager";
        public bool Suspend { get; set; }
        private string _lastMsnString = "";
        public bool Started { get; set; }
        public void Start(ILogger logger)
        {
            _logger = logger;
            Started = true;
        }

        public string Description { get; } = "Provides basic osu! events using old MSN communication that still exists in osu!";
        public string Name { get; } = nameof(Msn);
        public string Author { get; } = "Piotrekol";
        public string Url { get; } = "";
        public string UpdateUrl { get; } = "";
        public EventHandler<MapSearchArgs> NewOsuEvent { get; set; }

        protected virtual void OnMsnStringChanged()
        {
            if (Suspend)
                return;
            Task.Factory.StartNew<int>(() =>
            {
                _firstRunUserControl?.GotMsn(string.Format("{0} - {1}", _osuStatus["title"], _osuStatus["artist"]));

                var args = CreateArgs(_osuStatus);

                if(args != null)
                    NewOsuEvent?.Invoke(this, args);

                return 1;
            });
        }

        private MapSearchArgs CreateArgs(Dictionary<string, string> osuStatus)
        {
            OsuStatus status = osuStatus["status"] == "Listening" ? OsuStatus.Listening
                : osuStatus["status"] == "Playing" ? OsuStatus.Playing
                    : osuStatus["status"] == "Watching" ? OsuStatus.Watching
                        : osuStatus["status"] == "Editing" ? OsuStatus.Editing
                            : OsuStatus.Null;

            osuStatus["raw"] = string.Format("{0} - {1}", osuStatus["title"], osuStatus["artist"]);
            bool isFalsePlay;
            lock (this)
            {
                isFalsePlay = IsFalsePlay(osuStatus["raw"], status, _lastMsnString);
            }
            if (isFalsePlay)
            {
                _logger?.Log(">ignoring second MSN string...", LogLevel.Advanced);
            }
            else
            {
                _lastMsnString = osuStatus["raw"];
                _logger?.Log("", LogLevel.Advanced);
                string result = ">Got ";
                foreach (var v in osuStatus)
                {
                    if (v.Key != "raw") result = result + $"{v.Key}: \"{v.Value}\" ";
                }
                _logger?.Log(result, LogLevel.Basic);

                var searchArgs = new MapSearchArgs("Msn")
                {
                    Artist = osuStatus["artist"] ?? "",
                    Title = osuStatus["title"] ?? "",
                    Diff = osuStatus["diff"] ?? "",
                    Raw = osuStatus["raw"] ?? "",
                    Status = status,

                };
                return searchArgs;
{'fraction_non_alphanumeric': 0.11305755925217136, 'fraction_numerical': 0.0014721036360959812, 'mean_word_length': 1.7241379310344827, 'pattern_counts': {'":': 0, '<': 13, '<?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': '4527805', 'n_tokens_mistral': 1864, 'n_tokens_neox': 1779, 'n_words': 414}