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

public class EditItemTextureCache : MonoBehaviour
{
	private void OnDestroy()
	{
		foreach (KeyValuePair<int, Texture2D> keyValuePair in this.loadTex)
		{
			UnityEngine.Object.Destroy(keyValuePair.Value);
		}
	}

	private IEnumerator ProcLoad()
	{
		float endTime = Time.realtimeSinceStartup + 0.003f;
		while (0 < this.preloadItems.Count)
		{
			int rid = this.preloadItems[0];
			if (!this.loadTex.ContainsKey(rid))
			{
				try
				{
					this.loadTex.Add(rid, ImportCM.CreateTexture(this.ridToFileNameDic[rid]));
				}
				catch (Exception)
				{
				}
				this.preloadItems.RemoveAt(0);
				if (endTime <= Time.realtimeSinceStartup)
				{
					yield return null;
					endTime = Time.realtimeSinceStartup + 0.003f;
				}
			}
			else
			{
				this.preloadItems.RemoveAt(0);
			}
		}
		this.loadProcessOperation = false;
		yield break;
	}

	public Texture2D GetTexter(int rid)
	{
		Texture2D texture2D = null;
		if (!this.loadTex.TryGetValue(rid, out texture2D))
		{
			texture2D = ImportCM.CreateTexture((!this.IsRegister(rid)) ? "notex.tex" : this.ridToFileNameDic[rid]);
			this.loadTex.Add(rid, texture2D);
		}
		return texture2D;
	}

	public bool IsRegister(int rid)
	{
		return this.ridToFileNameDic.ContainsKey(rid);
	}

	public void PreLoadRegister(int rid, string fileName)
	{
		if (!this.loadTex.ContainsKey(rid))
		{
			this.preloadItems.Add(rid);
			this.ridToFileNameDic[rid] = fileName;
		}
	}

	public void Update()
	{
		if (!this.loadProcessOperation && 0 < this.preloadItems.Count)
		{
			this.loadProcessOperation = true;
			base.StartCoroutine(this.ProcLoad());
		}
	}

	private Dictionary<int, Texture2D> loadTex = new Dictionary<int, Texture2D>();

	private List<int> preloadItems = new List<int>();

	private Dictionary<int, string> ridToFileNameDic = new Dictionary<int, string>();

	private bool loadProcessOperation;
}
