<commit_before>﻿using System;
using System.Windows.Forms.Integration;
using Eto.Drawing;
using Eto.Wpf.Forms;
using OpenSage.DataViewer.Controls;

namespace OpenSage.DataViewer.Windows.Controls
{
    internal sealed class GameControlHandler : WpfFrameworkElement<WindowsFormsHost, GameControl, Eto.Forms.Control.ICallback>, GameControl.IGameControl
    {
        private Game _game;

        public Func<IntPtr, Game> CreateGame { get; set; }

        public GameControlHandler()
        {
            var control = new System.Windows.Forms.Control
            {
                Width = 100,
                Height = 100
            };

            control.HandleCreated += (sender, e) =>
            {
                _game = CreateGame(control.Handle);
                System.Windows.Media.CompositionTarget.Rendering += OnRendering;
            };

            control.MouseDown += (sender, e) =>
            {
                control.Focus();
            };

            Control = new WindowsFormsHost
            {
                Child = control
            };
        }

        public override void OnUnLoad(EventArgs e)
        {
            System.Windows.Media.CompositionTarget.Rendering -= OnRendering;
            _game = null;

            base.OnUnLoad(e);
        }

        private void OnRendering(object sender, EventArgs e)
        {
            _game?.Tick();
        }

        public override Color BackgroundColor { get => Colors.Transparent; set { } }
    }
}<commit_msg>Attempt to fix crash when rendering after game has been disposed<commit_after>﻿using System;
using System.Windows.Forms.Integration;
using Eto.Drawing;
using Eto.Wpf.Forms;
using OpenSage.DataViewer.Controls;

namespace OpenSage.DataViewer.Windows.Controls
{
    internal sealed class GameControlHandler : WpfFrameworkElement<WindowsFormsHost, GameControl, Eto.Forms.Control.ICallback>, GameControl.IGameControl
    {
        private readonly object _lockObject = new object();
        private Game _game;

        public Func<IntPtr, Game> CreateGame { get; set; }

        public GameControlHandler()
        {
            var control = new System.Windows.Forms.Control
            {
                Width = 100,
                Height = 100
            };

            control.HandleCreated += (sender, e) =>
            {
                _game = CreateGame(control.Handle);
                System.Windows.Media.CompositionTarget.Rendering += OnRendering;
            };

            control.MouseDown += (sender, e) =>
            {
                control.Focus();
            };

            Control = new WindowsFormsHost
            {
                Child = control
            };
        }

        public override void OnUnLoad(EventArgs e)
        {
            lock (_lockObject)
            {
                System.Windows.Media.CompositionTarget.Rendering -= OnRendering;

                _game.Dispose();
                _game = null;
            }

            base.OnUnLoad(e);
        }

        private void OnRendering(object sender, EventArgs e)
        {
            lock (_lockObject)
            {
                _game?.Tick();
            }
        }

        public override Color BackgroundColor { get => Colors.Transparent; set { } }
    }
}
{'fraction_non_alphanumeric': 0.08687672070969715, 'fraction_numerical': 0.0036708473539308656, 'mean_word_length': 1.6139088729016786, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, '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': '3018735', 'n_tokens_mistral': 858, 'n_tokens_neox': 813, 'n_words': 179}