<commit_before>﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.IO;
using System.Diagnostics;

namespace ItemCollage
{
    public partial class Form1 : Form
    {
        IDictionary<GlobalHotkey, Action> hotkeys;
        List<Image> items = new List<Image>();

        public Form1()
        {
            InitializeComponent();

            // Initialise hotkeys
            hotkeys = new Dictionary<GlobalHotkey, Action> {
                { new GlobalHotkey(Constants.NOMOD, Keys.F1, this), GrabItem }
            };

            // register hotkeys
            foreach (var hk in hotkeys)
                hk.Key.Register();

            UpdateLabel();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            GrabItem();
        }

        private void GrabItem()
        {
            Stopwatch sw = new Stopwatch();

            var cursorPos = Cursor.Position;

            sw.Start();
            this.Opacity = 0;
            var screen = TakeScreenshot(ref cursorPos);
            this.Opacity = 1;

#if DEBUG
            sw.Stop();
            // save picture for future testing
            var baseName = string.Format("itemat-{0:yyyy-MM-dd-HH-mm-ss}-P{1}-{2}",
                DateTime.UtcNow, cursorPos.X, cursorPos.Y);
            var fileName = baseName + ".in.png";
            var picFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
            var testFolder = Path.Combine(picFolder, "ExtractTest");
            if (!Directory.Exists(testFolder)) Directory.CreateDirectory(testFolder);
            var file = Path.Combine(testFolder, fileName);
            screen.Save(file);
            sw.Start();
#endif

            var ie = new ItemExtractor(screen, cursorPos);
            var item = ie.ExtractItem();
            sw.Stop();
            Debug.Print("Time for extraction: " + sw.Elapsed.ToString());

            if (item == null)
            {
                label1.Text = "No item found";
                return;
            }

            items.Add(item);
            pictureBox1.Image = item;
            Clipboard.SetImage(item);

#if DEBUG
            var outfile = Path.Combine(testFolder, baseName + ".out.png");
            item.Save(outfile);
#endif

            UpdateLabel();
        }

        private void UpdateLabel()
        {
            label1.Text = string.Format("{0} item{1} copied.", items.Count,
                items.Count != 1 ? "s" : "");
        }

        protected override void WndProc(ref Message m)
        {
            if (m.Msg == Constants.WM_HOTKEY_MSG_ID)
            {
                var key = (Keys)((int)m.LParam >> 16);
                foreach (var hotkey in hotkeys.Where(hk => hk.Key.Key == key))
                    hotkey.Value();
            }
            base.WndProc(ref m);
        }

        private Bitmap TakeScreenshot(ref Point p)
        {
            var bounds = Screen.FromPoint(p).Bounds;
            p.Offset(-bounds.X, -bounds.Y);
            Bitmap bmp = new Bitmap(bounds.Width, bounds.Height, PixelFormat.Format24bppRgb);
            var g = Graphics.FromImage(bmp);
            g.CopyFromScreen(bounds.X, bounds.Y, 0, 0, new Size(bounds.Width, bounds.Height));
            return bmp;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            foreach (var hk in hotkeys)<commit_msg>Add some very basic update checking<commit_after>﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Text;
using System.Windows.Forms;

namespace ItemCollage
{
    public partial class Form1 : Form
    {
        const string UPDATE_URL =
            "https://raw.github.com/ygra/Diablo3ItemCollage/master/version";
        const string DOWNLOAD_URL =
            "https://github.com/ygra/Diablo3ItemCollage/downloads";

        IDictionary<GlobalHotkey, Action> hotkeys;
        List<Image> items = new List<Image>();

        public Form1()
        {
            InitializeComponent();

            // Initialise hotkeys
            hotkeys = new Dictionary<GlobalHotkey, Action> {
                { new GlobalHotkey(Constants.NOMOD, Keys.F1, this), GrabItem }
            };

            // register hotkeys
            foreach (var hk in hotkeys)
                hk.Key.Register();

            UpdateLabel();

            CheckForUpdates();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            GrabItem();
        }

        private void GrabItem()
        {
            Stopwatch sw = new Stopwatch();

            var cursorPos = Cursor.Position;

            sw.Start();
            this.Opacity = 0;
            var screen = TakeScreenshot(ref cursorPos);
            this.Opacity = 1;

#if DEBUG
            sw.Stop();
            // save picture for future testing
            var baseName = string.Format("itemat-{0:yyyy-MM-dd-HH-mm-ss}-P{1}-{2}",
                DateTime.UtcNow, cursorPos.X, cursorPos.Y);
            var fileName = baseName + ".in.png";
            var picFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
            var testFolder = Path.Combine(picFolder, "ExtractTest");
            if (!Directory.Exists(testFolder)) Directory.CreateDirectory(testFolder);
            var file = Path.Combine(testFolder, fileName);
            screen.Save(file);
            sw.Start();
#endif

            var ie = new ItemExtractor(screen, cursorPos);
            var item = ie.ExtractItem();
            sw.Stop();
            Debug.Print("Time for extraction: " + sw.Elapsed.ToString());

            if (item == null)
            {
                label1.Text = "No item found";
                return;
            }

            items.Add(item);
            pictureBox1.Image = item;
            Clipboard.SetImage(item);

#if DEBUG
            var outfile = Path.Combine(testFolder, baseName + ".out.png");
            item.Save(outfile);
#endif

            UpdateLabel();
        }

        private void UpdateLabel()
        {
            label1.Text = string.Format("{0} item{1} copied.", items.Count,
                items.Count != 1 ? "s" : "");
        }

        private void CheckForUpdates()
        {
            var client = new WebClient();
            client.Encoding = Encoding.UTF8;
            client.DownloadStringCompleted +=
                delegate(object s, DownloadStringCompletedEventArgs e)
                {
                    if (e.Error != null)
                        CheckUpdateError(e.Error);
                    else
                        CompareVersions(e.Result);
                };

            client.DownloadStringAsync(new Uri(UPDATE_URL));
        }

        private void CheckUpdateError(Exception error)
        {
            MessageBox.Show("Checking for updates failed:\n" + error.Message);
        }

        private void CompareVersions(string data)
        {
            var local = Assembly.GetExecutingAssembly().GetName().Version;
            var remote = new Version(data);

            if (remote > local)
            {
                var res = MessageBox.Show(string.Format(
                    "New version {0} available, download now?", remote),
                    "Diablo3ItemCollage", MessageBoxButtons.YesNo);

                if (res == DialogResult.Yes)
                    Process.Start(DOWNLOAD_URL);
            }
        }

        protected override void WndProc(ref Message m)
        {
            if (m.Msg == Constants.WM_HOTKEY_MSG_ID)
            {
                var key = (Keys)((int)m.LParam >> 16);
                foreach (var hotkey in hotkeys.Where(hk => hk.Key.Key == key))
                    hotkey.Value();
            }
            base.WndProc(ref m);
        }

        private Bitmap TakeScreenshot(ref Point p)
        {
            var bounds = Screen.FromPoint(p).Bounds;
            p.Offset(-bounds.X, -bounds.Y);
            Bitmap bmp = new Bitmap(bounds.Width, bounds.Height, PixelFormat.Format24bppRgb);
            var g = Graphics.FromImage(bmp);
            g.CopyFromScreen(bounds.X, bounds.Y, 0, 0, new Size(bounds.Width, bounds.Height));
            return bmp;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            foreach (var hk in hotkeys)
{'fraction_non_alphanumeric': 0.10545119280857439, 'fraction_numerical': 0.005647113057508355, 'mean_word_length': 1.6652334152334152, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 18, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1511994', 'n_tokens_mistral': 2406, 'n_tokens_neox': 2303, 'n_words': 532}