<commit_before>﻿// Copyright (c) 2007-2016 ppy Pty Ltd <<EMAIL>>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu-framework/master/LICENCE

using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime;
using System.Threading;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.OpenGL;
using osu.Framework.Input;
using osu.Framework.Input.Handlers;
using osu.Framework.Statistics;
using osu.Framework.Threading;
using osu.Framework.Timing;
using OpenTK;
using System.Threading.Tasks;
using osu.Framework.Caching;
using osu.Framework.Extensions.IEnumerableExtensions;
using osu.Framework.Graphics.Sprites;
using OpenTK.Input;
using OpenTK.Graphics;

namespace osu.Framework.Platform
{
    public abstract class BasicGameHost : Container, IIpcHost
    {
        public static BasicGameHost Instance;

        public BasicGameWindow Window;

        private void setActive(bool isActive)
        {
            threads.ForEach(t => t.IsActive = isActive);

            if (isActive)
                Activated?.Invoke();
            else
                Deactivated?.Invoke();
        }

        public bool IsActive => InputThread.IsActive;

        public bool IsPrimaryInstance { get; protected set; } = true;

        public event Action Activated;
        public event Action Deactivated;
        public event Func<bool> Exiting;
        public event Action Exited;

        public event Action<IpcMessage> MessageReceived;

        protected void OnMessageReceived(IpcMessage message) => MessageReceived?.Invoke(message);

        public virtual Task SendMessage(IpcMessage message)
        {
            throw new NotImplementedException("This platform does not implement IPC.");
        }

        public virtual Clipboard GetClipboard() => null;

        public virtual BasicStorage Storage { get; protected set; } //public set currently required for visualtests setup.

        public override bool IsVisible => true;

        private GameThread[] threads;

        public GameThread DrawThread;
        public GameThread UpdateThread;
        public InputThread InputThread;

        private double maximumUpdateHz;

        public double MaximumUpdateHz
        {
            get
            {
                return maximumUpdateHz;
            }

            set
            {
                UpdateThread.ActiveHz = maximumUpdateHz = value;
            }
        }

        private double maximumDrawHz;

        public double MaximumDrawHz
        {
            get
            {
                return maximumDrawHz;
            }

            set
            {
                DrawThread.ActiveHz = maximumDrawHz = value;
            }
        }

        public double MaximumInactiveHz
        {
            get
            {
                return DrawThread.InactiveHz;
            }

            set
            {
                DrawThread.InactiveHz = value;
                UpdateThread.InactiveHz = value;
            }
        }

        protected internal PerformanceMonitor InputMonitor => InputThread.Monitor;
        protected internal PerformanceMonitor UpdateMonitor => UpdateThread.Monitor;
        protected internal PerformanceMonitor DrawMonitor => DrawThread.Monitor;

        private Cached<string> fullPathBacking = new Cached<string>();
        public string FullPath => fullPathBacking.EnsureValid() ? fullPathBacking.Value : fullPathBacking.Refresh(() =>
        {
            string codeBase = Assembly.GetExecutingAssembly().CodeBase;
            UriBuilder uri = new UriBuilder(codeBase);
            return Uri.UnescapeDataString(uri.Path);
        });

        private UserInputManager inputManager;

        protected override Container<Drawable> Content => inputManager;

        private string name;
        public override string Name => name;

        public DependencyContainer Dependencies { get; private set; } = new DependencyContainer();

        protected BasicGameHost(string gameName = @"")
        {
            Instance = this;

            Dependencies.Cache(this);
            name = gameName;

            threads = new[]
            {
                DrawThread = new GameThread(DrawFrame, @"Draw")
                {
                    OnThreadStart = DrawInitialize,
                },
                UpdateThread = new GameThread(UpdateFrame, @"Update")
                {
                    OnThreadStart = UpdateInitialize,
                    Monitor = { HandleGC = true }
                },
                InputThread = new InputThread(null, @"Input") //never gets started.
            };

            Clock = UpdateThread.Clock;

            MaximumUpdateHz = GameThread.DEFAULT_ACTIVE_HZ;
            MaximumDrawHz = (DisplayDevice.Default?.RefreshRate ?? 0) * 4;

            // Note, that RegisterCounters only has an effect for the first
            // BasicGameHost to be passed into it; i.e. the first BasicGameHost
            // to be instantiated.
            FrameStatistics.RegisterCounters(this);

            Environment.CurrentDirectory = System.IO.Path.GetDirectoryName(FullPath);

            setActive(true);

            AddInternal(inputManager = new UserInputManager(this));

            Dependencies.Cache(inputManager);
        }

        protected virtual void OnActivated() => Schedule(() => setActive(true));

        protected virtual void OnDeactivated() => Schedule(() => setActive(false));
<commit_msg>Add global exception handling.<commit_after>﻿// Copyright (c) 2007-2016 ppy Pty Ltd <<EMAIL>>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu-framework/master/LICENCE

using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime;
using System.Threading;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.OpenGL;
using osu.Framework.Input;
using osu.Framework.Input.Handlers;
using osu.Framework.Statistics;
using osu.Framework.Threading;
using osu.Framework.Timing;
using OpenTK;
using System.Threading.Tasks;
using osu.Framework.Caching;
using osu.Framework.Extensions.IEnumerableExtensions;
using osu.Framework.Graphics.Sprites;
using OpenTK.Input;
using OpenTK.Graphics;

namespace osu.Framework.Platform
{
    public abstract class BasicGameHost : Container, IIpcHost
    {
        public static BasicGameHost Instance;

        public BasicGameWindow Window;

        private void setActive(bool isActive)
        {
            threads.ForEach(t => t.IsActive = isActive);

            if (isActive)
                Activated?.Invoke();
            else
                Deactivated?.Invoke();
        }

        public bool IsActive => InputThread.IsActive;

        public bool IsPrimaryInstance { get; protected set; } = true;

        public event Action Activated;
        public event Action Deactivated;
        public event Func<bool> Exiting;
        public event Action Exited;

        public event Action<Exception> ExceptionThrown;

        public event Action<IpcMessage> MessageReceived;

        protected void OnMessageReceived(IpcMessage message) => MessageReceived?.Invoke(message);

        public virtual Task SendMessage(IpcMessage message)
        {
            throw new NotImplementedException("This platform does not implement IPC.");
        }

        public virtual Clipboard GetClipboard() => null;

        public virtual BasicStorage Storage { get; protected set; } //public set currently required for visualtests setup.

        public override bool IsVisible => true;

        private GameThread[] threads;

        public GameThread DrawThread;
        public GameThread UpdateThread;
        public InputThread InputThread;

        private double maximumUpdateHz;

        public double MaximumUpdateHz
        {
            get
            {
                return maximumUpdateHz;
            }

            set
            {
                UpdateThread.ActiveHz = maximumUpdateHz = value;
            }
        }

        private double maximumDrawHz;

        public double MaximumDrawHz
        {
            get
            {
                return maximumDrawHz;
            }

            set
            {
                DrawThread.ActiveHz = maximumDrawHz = value;
            }
        }

        public double MaximumInactiveHz
        {
            get
            {
                return DrawThread.InactiveHz;
            }

            set
            {
                DrawThread.InactiveHz = value;
                UpdateThread.InactiveHz = value;
            }
        }

        protected internal PerformanceMonitor InputMonitor => InputThread.Monitor;
        protected internal PerformanceMonitor UpdateMonitor => UpdateThread.Monitor;
        protected internal PerformanceMonitor DrawMonitor => DrawThread.Monitor;

        private Cached<string> fullPathBacking = new Cached<string>();
        public string FullPath => fullPathBacking.EnsureValid() ? fullPathBacking.Value : fullPathBacking.Refresh(() =>
        {
            string codeBase = Assembly.GetExecutingAssembly().CodeBase;
            UriBuilder uri = new UriBuilder(codeBase);
            return Uri.UnescapeDataString(uri.Path);
        });

        private UserInputManager inputManager;

        protected override Container<Drawable> Content => inputManager;

        private string name;
        public override string Name => name;

        public DependencyContainer Dependencies { get; private set; } = new DependencyContainer();

        protected BasicGameHost(string gameName = @"")
        {
            Instance = this;

            AppDomain.CurrentDomain.UnhandledException += exceptionHandler;

            Dependencies.Cache(this);
            name = gameName;

            threads = new[]
            {
                DrawThread = new GameThread(DrawFrame, @"Draw")
                {
                    OnThreadStart = DrawInitialize,
                },
                UpdateThread = new GameThread(UpdateFrame, @"Update")
                {
                    OnThreadStart = UpdateInitialize,
                    Monitor = { HandleGC = true }
                },
                InputThread = new InputThread(null, @"Input") //never gets started.
            };

            Clock = UpdateThread.Clock;

            MaximumUpdateHz = GameThread.DEFAULT_ACTIVE_HZ;
            MaximumDrawHz = (DisplayDevice.Default?.RefreshRate ?? 0) * 4;

            // Note, that RegisterCounters only has an effect for the first
            // BasicGameHost to be passed into it; i.e. the first BasicGameHost
            // to be instantiated.
            FrameStatistics.RegisterCounters(this);

            Environment.CurrentDirectory = System.IO.Path.GetDirectoryName(FullPath);

            setActive(true);

            AddInternal(inputManager = new UserInputManager(this));

            Dependencies.Cache(inputManager);
        }

        private void exceptionHandler(object sender, UnhandledExceptionEventArgs e)
        {
            var exception = e.ExceptionObject as Exception;

            if (ExceptionThrown != null)
                ExceptionThrown.Invoke(exception);
            else
                throw exception;
        }

        protected virtual void OnActivated() => Schedule(() => setActive(true));

        protected virtual void OnDeactivated() => Schedule(() => setActive(false));

{'fraction_non_alphanumeric': 0.07594504650567455, 'fraction_numerical': 0.0017066302585544842, 'mean_word_length': 2.060052219321149, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 50, 'https://': 2, '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': '4426661', 'n_tokens_mistral': 2893, 'n_tokens_neox': 2716, 'n_words': 737}