using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DesktopDuplication : MonoBehaviour
{
	public bool CreateDesktopDuplication(string display_device_name)
	{
		NDebug.Assert(this.class_pointer_ == IntPtr.Zero, "CreateDesktopDuplication二重に作成しようとしました。");
		if (!DesktopDuplication.class_pointer_cache_.TryGetValue(display_device_name, out this.class_pointer_))
		{
			this.class_pointer_ = DLLDesktopDuplication.CreateDesktopDuplication(display_device_name);
			if (this.class_pointer_ == IntPtr.Zero)
			{
				return false;
			}
			DesktopDuplication.class_pointer_cache_.Add(display_device_name, this.class_pointer_);
		}
		this.render_event_func_ptr_ = DLLDesktopDuplication.GetDesktopDuplicationRenderEventFunc();
		this.render_event_id_ = DLLDesktopDuplication.GetDesktopDuplicationRenderID(this.class_pointer_);
		this.routine_ = this.CallPluginAtEndOfFrames();
		NDebug.Assert(this.render_coroutine_ == null, "CreateDesktopDuplication Coroutine 二重に作成しようとしました。");
		this.render_coroutine_ = base.StartCoroutine(this.routine_);
		return true;
	}

	private void OnEnable()
	{
		if (this.routine_ != null && this.render_coroutine_ == null)
		{
			this.render_coroutine_ = base.StartCoroutine(this.routine_);
		}
	}

	private void OnDisable()
	{
		if (this.routine_ != null && this.render_coroutine_ != null)
		{
			base.StopCoroutine(this.render_coroutine_);
			this.render_coroutine_ = null;
		}
	}

	private void OnDestroy()
	{
		if (this.routine_ != null && this.render_coroutine_ != null)
		{
			base.StopCoroutine(this.render_coroutine_);
			this.render_coroutine_ = null;
		}
		if (this.is_valid)
		{
		}
		this.class_pointer_ = IntPtr.Zero;
	}

	public void OnApplicationQuit()
	{
		foreach (KeyValuePair<string, IntPtr> keyValuePair in DesktopDuplication.class_pointer_cache_)
		{
			if (keyValuePair.Value != IntPtr.Zero)
			{
				DLLDesktopDuplication.DeleteDesktopDuplication(keyValuePair.Value);
			}
		}
		DesktopDuplication.class_pointer_cache_.Clear();
		this.class_pointer_ = IntPtr.Zero;
	}

	public Texture2D render_texture
	{
		get
		{
			return this.render_tex_;
		}
		set
		{
			if (!this.is_valid)
			{
				return;
			}
			this.render_tex_ = value;
			if (this.render_tex_ != null)
			{
				DLLDesktopDuplication.SetDesktopDuplicationDrawTex(this.class_pointer_, this.render_tex_.GetNativeTexturePtr());
			}
		}
	}

	public int width
	{
		get
		{
			return (!this.is_valid) ? 0 : DLLDesktopDuplication.GetDesktopDuplicationRenderWidth(this.class_pointer_);
		}
	}

	public int height
	{
		get
		{
			return (!this.is_valid) ? 0 : DLLDesktopDuplication.GetDesktopDuplicationRenderHeight(this.class_pointer_);
		}
	}

	public bool is_mouse_pointer_visible
	{
		get
		{
			return this.is_valid && DLLDesktopDuplication.GetDesktopDuplicationIsMousePointerVisible(this.class_pointer_) != 0;
		}
	}

	public int mouse_pointer_x
	{
		get
		{
			return (!this.is_valid) ? 0 : DLLDesktopDuplication.GetDesktopDuplicationGetMousePointerX(this.class_pointer_);
		}
	}

	public int mouse_pointer_y
	{
		get
		{
			return (!this.is_valid) ? 0 : DLLDesktopDuplication.GetDesktopDuplicationGetMousePointerY(this.class_pointer_);
		}
	}

	public bool is_valid
	{
		get
		{
			return this.class_pointer_ != IntPtr.Zero;
		}
	}

	private IEnumerator CallPluginAtEndOfFrames()
	{
		for (;;)
		{
			yield return new WaitForEndOfFrame();
			if (this.is_valid && this.render_texture != null)
			{
				GL.IssuePluginEvent(this.render_event_func_ptr_, this.render_event_id_);
			}
		}
		yield break;
	}

	private static Dictionary<string, IntPtr> class_pointer_cache_ = new Dictionary<string, IntPtr>();

	private IntPtr class_pointer_ = IntPtr.Zero;

	private Texture2D render_tex_;

	private int render_event_id_ = -1;

	private IEnumerator routine_;

	private Coroutine render_coroutine_;

	private IntPtr render_event_func_ptr_;
}
