<commit_before>﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

public class PauseManager : MonoBehaviour
{
	public UnityEvent onPause, onUnPause;

	[SerializeField]
	private bool enableVigorousTesting;

	//Whitelisted items won't be affected by pause
	public AudioSource[] audioSourceWhitelist;
	public MonoBehaviour[] scriptWhitelist;

	private bool paused;
	private float timeScale;

	private List<AudioSource> pausedAudioSources;
	private List<MonoBehaviour> disabledScripts;

	private float pauseTimer = 0f;

	void Start ()
	{
		paused = false;
	}
	
	void Update ()
	{
		if (enableVigorousTesting && Input.GetKey(KeyCode.P))
			pauseTimer -= Time.fixedDeltaTime;
		if (Input.GetKeyDown(KeyCode.Escape) || pauseTimer < 0f)
		{
			if (!paused)
				pause();
			else
				unPause();
			pauseTimer = Random.Range(.1f, .2f);
		}
	}

	void pause()
	{
		timeScale = Time.timeScale;
		Time.timeScale = 0f;

		AudioSource[] audioSources = FindObjectsOfType(typeof(AudioSource)) as AudioSource[];
		pausedAudioSources = new List<AudioSource>();
		List<AudioSource> whitelistedAudioSources = new List<AudioSource>(audioSourceWhitelist);
		foreach (AudioSource source in audioSources)
		{
			if (!whitelistedAudioSources.Remove(source) && source.isPlaying)
			{
				source.Pause();
				pausedAudioSources.Add(source);
			}
		}

		MonoBehaviour[] scripts = FindObjectsOfType(typeof(MonoBehaviour)) as MonoBehaviour[];
		disabledScripts = new List<MonoBehaviour>();
		List<MonoBehaviour> whitelistedScripts = new List<MonoBehaviour>(scriptWhitelist);
		foreach( MonoBehaviour script in scripts)
		{
			if (!whitelistedScripts.Remove(script) && script.enabled && script != this)
			{
				script.enabled = false;
				disabledScripts.Add(script);
			}
		}

		onPause.Invoke();
		if (MicrogameController.instance != null)
			MicrogameController.instance.onPause.Invoke();

		paused = true;
	}

	void unPause()
	{
		Time.timeScale = timeScale;

		foreach (AudioSource source in pausedAudioSources)
		{
			if (source != null)
				source.UnPause();
		}

		foreach (MonoBehaviour script in disabledScripts)
		{
			if (script != null)
				script.enabled = true;
		}

		onUnPause.Invoke();
		if (MicrogameController.instance != null)
			MicrogameController.instance.onUnPause.Invoke();

		paused = false;
	}
}<commit_msg>Make microgame camera black on pause<commit_after>﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

public class PauseManager : MonoBehaviour
{
	public UnityEvent onPause, onUnPause;

	[SerializeField]
	private bool enableVigorousTesting;

	//Whitelisted items won't be affected by pause
	public AudioSource[] audioSourceWhitelist;
	public MonoBehaviour[] scriptWhitelist;

	private bool paused;
	private float timeScale;

	PauseData pauseData;
	private struct PauseData
	{
		public List<AudioSource> pausedAudioSources;
		public List<MonoBehaviour> disabledScripts;
		public int camCullingMask;
		public Color camColor;
	}

	private float pauseTimer = 0f;

	void Start ()
	{
		paused = false;
	}
	
	void Update ()
	{
		if (enableVigorousTesting && Input.GetKey(KeyCode.P))
			pauseTimer -= Time.fixedDeltaTime;
		if (Input.GetKeyDown(KeyCode.Escape) || pauseTimer < 0f)
		{
			if (!paused)
				pause();
			else
				unPause();
			pauseTimer = Random.Range(.1f, .2f);
		}
	}

	void pause()
	{
		timeScale = Time.timeScale;
		Time.timeScale = 0f;

		AudioSource[] audioSources = FindObjectsOfType(typeof(AudioSource)) as AudioSource[];
		pauseData.pausedAudioSources = new List<AudioSource>();
		List<AudioSource> whitelistedAudioSources = new List<AudioSource>(audioSourceWhitelist);
		foreach (AudioSource source in audioSources)
		{
			if (!whitelistedAudioSources.Remove(source) && source.isPlaying)
			{
				source.Pause();
				pauseData.pausedAudioSources.Add(source);
			}
		}

		MonoBehaviour[] scripts = FindObjectsOfType(typeof(MonoBehaviour)) as MonoBehaviour[];
		pauseData.disabledScripts = new List<MonoBehaviour>();
		List<MonoBehaviour> whitelistedScripts = new List<MonoBehaviour>(scriptWhitelist);
		foreach( MonoBehaviour script in scripts)
		{
			if (!whitelistedScripts.Remove(script) && script.enabled && script != this)
			{
				script.enabled = false;
				pauseData.disabledScripts.Add(script);
			}
		}

		onPause.Invoke();
		if (MicrogameController.instance != null)
		{
			MicrogameController.instance.onPause.Invoke();
			pauseData.camCullingMask = Camera.main.cullingMask;
			pauseData.camColor = Camera.main.backgroundColor;
			Camera.main.cullingMask = 0;
			Camera.main.backgroundColor = Color.black;
		}

		paused = true;
	}

	void unPause()
	{
		Time.timeScale = timeScale;

		foreach (AudioSource source in pauseData.pausedAudioSources)
		{
			if (source != null)
				source.UnPause();
		}

		foreach (MonoBehaviour script in pauseData.disabledScripts)
		{
			if (script != null)
				script.enabled = true;
		}

		onUnPause.Invoke();
		if (MicrogameController.instance != null)
		{
			Camera.main.cullingMask = pauseData.camCullingMask;
			Camera.main.backgroundColor = pauseData.camColor;
			MicrogameController.instance.onUnPause.Invoke();
		}

		paused = false;
	}
}
{'fraction_non_alphanumeric': 0.10810810810810811, 'fraction_numerical': 0.0021084914701935977, 'mean_word_length': 4.390495867768595, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 19, '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': '7373612', 'n_tokens_mistral': 1952, 'n_tokens_neox': 1670, 'n_words': 352}