<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.Threading.Tasks;
using osu.Framework.Platform;
using OpenTK;
using osu.Framework.Desktop.Input;
using osu.Framework.Input;

namespace osu.Framework.Desktop.Platform
{
    public abstract class DesktopGameHost : BasicGameHost
    {
        private TcpIpcProvider IpcProvider;
        private Task IpcTask;

        public DesktopGameHost(string gameName = @"", bool bindIPCPort = false) : base(gameName)
        {
            if (bindIPCPort)
            {
                IpcProvider = new TcpIpcProvider();
                IsPrimaryInstance = IpcProvider.Bind();
                if (IsPrimaryInstance)
                {
                    IpcProvider.MessageReceived += msg => OnMessageReceived(msg);
                    IpcTask = IpcProvider.Start();
                }
            }
        }

        public override TextInputSource GetTextInput() => Window == null ? null : new GameWindowTextInput(Window);

        protected override void LoadGame(BaseGame game)<commit_msg>Add support for automatically reloading framework on a change to the executable assembly.<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.IO;
using System.Threading;
using System.Threading.Tasks;
using osu.Framework.Platform;
using OpenTK;
using osu.Framework.Desktop.Input;
using osu.Framework.Input;

namespace osu.Framework.Desktop.Platform
{
    public abstract class DesktopGameHost : BasicGameHost
    {
        private TcpIpcProvider IpcProvider;
        private Task IpcTask;

        public DesktopGameHost(string gameName = @"", bool bindIPCPort = false) : base(gameName)
        {
            foreach (string a in Environment.GetCommandLineArgs())
            {
                switch (a)
                {
                    case @"--reload-on-change":
                        ensureShadowCopy();
                        break;
                }
            }

            if (bindIPCPort)
            {
                IpcProvider = new TcpIpcProvider();
                IsPrimaryInstance = IpcProvider.Bind();
                if (IsPrimaryInstance)
                {
                    IpcProvider.MessageReceived += msg => OnMessageReceived(msg);
                    IpcTask = IpcProvider.Start();
                }
            }
        }

        /// <summary>
        /// Copy ourselves to a temporary path and watch for updates to the original assembly.
        /// </summary>
        private void ensureShadowCopy()
        {
            string exe = System.Reflection.Assembly.GetEntryAssembly().Location;
            if (exe.Contains(@"_shadow"))
            {
                //we are already running a shadow copy. monitor the original executable path for changes.
                exe = exe.Replace(@"_shadow", @"");

                DateTime originalTime = new FileInfo(exe).LastWriteTimeUtc;

                Task.Run(() =>
                {
                    while (new FileInfo(exe).LastWriteTimeUtc == originalTime)
                        Thread.Sleep(1000);

                    Process.Start(exe, @"--reload-on-change");
                    Environment.Exit(0);
                });

                return;
            }

            string shadowExe = exe.Replace(@".exe", @"_shadow.exe");

            int attempts = 5;
            while (attempts-- > 0)
            {
                try
                {
                    File.Copy(exe, shadowExe, true);
                    break;
                }
                catch
                {
                    Thread.Sleep(200);
                }
            }

            Process.Start(shadowExe, @"--reload-on-change");
            Environment.Exit(0);
        }

        public override TextInputSource GetTextInput() => Window == null ? null : new GameWindowTextInput(Window);

        protected override void LoadGame(BaseGame game)
{'fraction_non_alphanumeric': 0.09383249340052796, 'fraction_numerical': 0.0064794816414686825, 'mean_word_length': 1.6346396965865992, 'pattern_counts': {'":': 1, '<': 9, '<?xml version=': 0, '>': 15, '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': '7314087', 'n_tokens_mistral': 1115, 'n_tokens_neox': 1009, 'n_words': 248}