<commit_before>﻿using NUnit.Compatibility;
using Pear.InteractionEngine.Events;
using Pear.InteractionEngine.Properties;
using Pear.InteractionEngine.Utils;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEngine;
using UnityEngine.EventSystems;

namespace Pear.InteractionEngine.Interactions
{
	/// <summary>
	/// PIE's event system
	/// Almost a direction port from VTK
	/// https://github.com/thestonefox/VRTK/blob/25991cd6b9f935de07f2dcbd9ed1be82055e14f5/Assets/VRTK/Scripts/Internal/VRTK_EventSystem.cs
	/// </summary>
	public class Pear_EventSystem : EventSystem
	{
		// The previous event system used to keep track of state when enabling and disabling
		protected EventSystem _previousEventSystem;

		// The input module
		protected Pear_InputModule _inputModule;

		// Fields used to copy values
		private static readonly IEnumerable<FieldInfo> EVENT_SYSTEM_FIELD_INFOS = typeof(EventSystem).GetFields(BindingFlags.Public | BindingFlags.Instance);
		private static readonly PropertyInfo[] EVENT_SYSTEM_PROPERTY_INFOS = typeof(EventSystem).GetProperties(BindingFlags.Public | BindingFlags.Instance).Except(new[] { typeof(EventSystem).GetProperty("enabled") }).ToArray();
		private static readonly FieldInfo BASE_INPUT_MODULE_EVENT_SYSTEM_FIELD_INFO = typeof(BaseInputModule).GetField("m_EventSystem", BindingFlags.NonPublic | BindingFlags.Instance);

		protected override void Awake()
		{
			base.Awake();

			InitializeEvents();
		}

		/// <summary>
		/// Make sure each object that implements IEvent has the event object set
		/// </summary>
		private void InitializeEvents()
		{
			// Loop over every object
			// If it's an event, set it's event property
			UnityEngine.Object[] objectsInScene = Resources.FindObjectsOfTypeAll(typeof(MonoBehaviour));
			Type iEventType = typeof(IEvent<>);
			Type propertyType = typeof(Property<>);
			int eventCounter = 0;
			foreach (MonoBehaviour mono in objectsInScene)
			{
				Type eventType = mono.GetType().GetInterfaces().FirstOrDefault(i => i.GetTypeInfo().IsGenericType && i.GetGenericTypeDefinition() == iEventType);
				if (eventType != null)
				{
					// Create an instance of Property<T>
					Type eventValueType = eventType.GetGenericArguments()[0];
					Type eventPropertyType = propertyType.MakeGenericType(eventValueType);
					object eventPropertyValue = Activator.CreateInstance(eventPropertyType);

					// Set it on the script
					PropertyInfo eventProperty = eventType.GetProperty("Event");
					eventProperty.SetValue(mono, eventPropertyValue, null);
					eventCounter++;
				}
			}
		}

		protected override void OnEnable()
		{
			_previousEventSystem = current;
			if (_previousEventSystem != null)
			{
				_previousEventSystem.enabled = false;
				CopyValuesFrom(_previousEventSystem, this);
			}

			_inputModule = transform.GetOrAddComponent<Pear_InputModule>();
			base.OnEnable();
			StartCoroutine(SetEventSystemOfBaseInputModulesAfterFrameDelay(this));
		}

		protected override void OnDisable()
		{
			base.OnDisable();
			Destroy(_inputModule);

			if (_previousEventSystem != null)
			{
				_previousEventSystem.enabled = true;
				CopyValuesFrom(this, _previousEventSystem);
				SetEventSystemOfBaseInputModules(_previousEventSystem);<commit_msg>Allow anyone to initialize events on the event system<commit_after>﻿using NUnit.Compatibility;
using Pear.InteractionEngine.Events;
using Pear.InteractionEngine.Properties;
using Pear.InteractionEngine.Utils;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEngine;
using UnityEngine.EventSystems;

namespace Pear.InteractionEngine.Interactions
{
	/// <summary>
	/// PIE's event system
	/// Almost a direction port from VTK
	/// https://github.com/thestonefox/VRTK/blob/25991cd6b9f935de07f2dcbd9ed1be82055e14f5/Assets/VRTK/Scripts/Internal/VRTK_EventSystem.cs
	/// </summary>
	public class Pear_EventSystem : EventSystem
	{
		// The previous event system used to keep track of state when enabling and disabling
		protected EventSystem _previousEventSystem;

		// The input module
		protected Pear_InputModule _inputModule;

		// Fields used to copy values
		private static readonly IEnumerable<FieldInfo> EVENT_SYSTEM_FIELD_INFOS = typeof(EventSystem).GetFields(BindingFlags.Public | BindingFlags.Instance);
		private static readonly PropertyInfo[] EVENT_SYSTEM_PROPERTY_INFOS = typeof(EventSystem).GetProperties(BindingFlags.Public | BindingFlags.Instance).Except(new[] { typeof(EventSystem).GetProperty("enabled") }).ToArray();
		private static readonly FieldInfo BASE_INPUT_MODULE_EVENT_SYSTEM_FIELD_INFO = typeof(BaseInputModule).GetField("m_EventSystem", BindingFlags.NonPublic | BindingFlags.Instance);

		protected override void Awake()
		{
			base.Awake();

			InitializeEvents();
		}

		/// <summary>
		/// Make sure each object that implements IEvent has the event object set
		/// </summary>
		public static void InitializeEvents()
		{
			// Loop over every object
			// If it's an event, set it's event property
			UnityEngine.Object[] objectsInScene = Resources.FindObjectsOfTypeAll(typeof(MonoBehaviour));
			Type iEventType = typeof(IEvent<>);
			Type propertyType = typeof(Property<>);
			int eventCounter = 0;
			foreach (MonoBehaviour mono in objectsInScene)
			{
				Type eventType = mono.GetType().GetInterfaces().FirstOrDefault(i => i.GetTypeInfo().IsGenericType && i.GetGenericTypeDefinition() == iEventType);
				if (eventType != null)
				{
					// Create an instance of Property<T>
					Type eventValueType = eventType.GetGenericArguments()[0];
					Type eventPropertyType = propertyType.MakeGenericType(eventValueType);
					object eventPropertyValue = Activator.CreateInstance(eventPropertyType);

					// Set it on the script
					PropertyInfo eventProperty = eventType.GetProperty("Event");
					if (eventProperty.GetValue(mono, null) == null) {
						eventProperty.SetValue(mono, eventPropertyValue, null);
						eventCounter++;
					}
				}
			}
		}

		protected override void OnEnable()
		{
			_previousEventSystem = current;
			if (_previousEventSystem != null)
			{
				_previousEventSystem.enabled = false;
				CopyValuesFrom(_previousEventSystem, this);
			}

			_inputModule = transform.GetOrAddComponent<Pear_InputModule>();
			base.OnEnable();
			StartCoroutine(SetEventSystemOfBaseInputModulesAfterFrameDelay(this));
		}

		protected override void OnDisable()
		{
			base.OnDisable();
			Destroy(_inputModule);

			if (_previousEventSystem != null)
			{
				_previousEventSystem.enabled = true;
				CopyValuesFrom(this, _previousEventSystem);
				SetEventSystemOfBaseInputModules(_previousEventSystem);
{'fraction_non_alphanumeric': 0.09741609741609741, 'fraction_numerical': 0.007425007425007425, 'mean_word_length': 4.756410256410256, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 23, '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': '4319206', 'n_tokens_mistral': 2194, 'n_tokens_neox': 1877, 'n_words': 432}