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

public class MaidColliderCollect : MonoBehaviour
{
	public string[] file_path_array
	{
		get
		{
			return this.file_path_array_;
		}
	}

	public static MaidColliderCollect AddColliderCollect(Maid maid)
	{
		if (maid == null)
		{
			NDebug.Assert("AddColliderCollect:メイドが居ません。", false);
		}
		if (maid.body0 == null || !maid.body0.isLoadedBody)
		{
			NDebug.Assert("AddColliderCollect:メイドのBodyが未だありません。", false);
		}
		MaidColliderCollect maidColliderCollect = maid.gameObject.GetComponent<MaidColliderCollect>();
		if (maidColliderCollect == null)
		{
			maidColliderCollect = maid.gameObject.AddComponent<MaidColliderCollect>();
		}
		return maidColliderCollect;
	}

	public static MaidColliderCollect AddCollider(Maid maid, MaidColliderCollect.ColliderType collider_type)
	{
		if (maid == null)
		{
			NDebug.Assert("AddColliderCollect:メイドが居ません。", false);
		}
		if (maid.body0 == null || !maid.body0.isLoadedBody)
		{
			NDebug.Assert("AddColliderCollect:メイドのBodyが未だありません。", false);
		}
		MaidColliderCollect maidColliderCollect = maid.gameObject.GetComponent<MaidColliderCollect>();
		if (maidColliderCollect == null)
		{
			maidColliderCollect = maid.gameObject.AddComponent<MaidColliderCollect>();
		}
		maidColliderCollect.AddCollider(collider_type);
		return maidColliderCollect;
	}

	public static void RemoveColliderAll(Maid maid)
	{
		if (maid == null)
		{
			return;
		}
		MaidColliderCollect component = maid.gameObject.GetComponent<MaidColliderCollect>();
		if (component != null)
		{
			for (int i = 0; i < 2; i++)
			{
				component.RemoveCollider((MaidColliderCollect.ColliderType)i);
			}
		}
	}

	public static void SuspendColliderAll(Maid maid, bool enable)
	{
		if (maid == null)
		{
			return;
		}
		MaidColliderCollect component = maid.gameObject.GetComponent<MaidColliderCollect>();
		if (component != null)
		{
			for (int i = 0; i < 2; i++)
			{
				component.SuspendCollider((MaidColliderCollect.ColliderType)i, enable);
			}
		}
	}

	public List<CapsuleCollider> AddCollider(MaidColliderCollect.ColliderType type)
	{
		this.RemoveCollider(type);
		List<CapsuleCollider> list = this.Read(type);
		this.collider_array_[(int)type] = list;
		return new List<CapsuleCollider>(list);
	}

	public void RemoveCollider(MaidColliderCollect.ColliderType type)
	{
		List<CapsuleCollider> list = this.collider_array_[(int)type];
		if (list != null)
		{
			for (int i = 0; i < list.Count; i++)
			{
				if (list[i] != null && list[i].gameObject != null)
				{
					UnityEngine.Object.Destroy(list[i].gameObject);
				}
			}
			list.Clear();
		}
	}

	public void SuspendCollider(MaidColliderCollect.ColliderType type, bool enable)
	{
		List<CapsuleCollider> list = this.collider_array_[(int)type];
		if (list != null)
		{
			for (int i = 0; i < list.Count; i++)
			{
				if (list[i] != null && list[i].gameObject != null)
				{
					list[i].enabled = enable;
				}
			}
		}
	}

	public List<CapsuleCollider> GetCollider(MaidColliderCollect.ColliderType type)
	{
		return this.collider_array_[(int)type];
	}

	public void SwitchCollider(MaidColliderCollect.ColliderType type)
	{
		for (int i = 0; i < 2; i++)
		{
			this.ActiveCollider(type, false);
			if (type != MaidColliderCollect.ColliderType.NON && i == (int)type)
			{
				this.ActiveCollider(type, true);
			}
		}
	}

	public void ActiveCollider(MaidColliderCollect.ColliderType type, bool active)
	{
		if (this.collider_array_[(int)type] != null)
		{
			foreach (CapsuleCollider capsuleCollider in this.collider_array_[(int)type])
			{
				capsuleCollider.gameObject.SetActive(active);
			}
		}
	}

	public List<CapsuleCollider> Read(MaidColliderCollect.ColliderType collider_type)
	{
		string path = this.file_path_array_[(int)collider_type];
		string str = this.add_obj_name_array_[(int)collider_type];
		int layer = this.layer_array_[(int)collider_type];
		List<CapsuleCollider> list = new List<CapsuleCollider>();
		path = Path.ChangeExtension(path, null);
		TextAsset textAsset = Resources.Load(path) as TextAsset;
		BinaryReader binaryReader = new BinaryReader(new MemoryStream(textAsset.bytes));
		Maid component = base.GetComponent<Maid>();
		Transform trBones = component.body0.m_trBones;
		int num = binaryReader.ReadInt32();
		for (int i = 0; i < num; i++)
		{
			string text = binaryReader.ReadString();
			int count = text.IndexOf("Bip01");
			text = text.Remove(0, count);
			Transform transform = trBones.Find(text);
			if (transform != null)
			{
				GameObject gameObject = new GameObject(str + transform.name);
				gameObject.layer = layer;
				gameObject.transform.SetParent(transform, false);
				CapsuleCollider capsuleCollider = this.ReadCapsuleCollider(binaryReader, gameObject);
				if (capsuleCollider != null)
				{
					list.Add(capsuleCollider);
				}
			}
		}
		binaryReader.Close();
		Resources.UnloadAsset(textAsset);
		return list;
	}

	public void Write(MaidColliderCollect.ColliderType collider_type)
	{
		string path = Path.Combine(Application.dataPath, "Resources/" + this.file_path_array_[(int)collider_type]);
		Dictionary<string, CapsuleCollider> dictionary = this.FindCapsuleCollider();
		Dictionary<string, CapsuleCollider> dictionary2 = new Dictionary<string, CapsuleCollider>();
		foreach (KeyValuePair<string, CapsuleCollider> keyValuePair in dictionary)
		{
			string key = keyValuePair.Key;
			for (int i = 0; i < 2; i++)
			{
				int num = keyValuePair.Key.IndexOf(this.add_obj_name_array_[i]);
				if (0 < num)
				{
					key = keyValuePair.Key.Remove(num - 1);
					break;
				}
			}
			dictionary2.Add(key, keyValuePair.Value);
		}
		MemoryStream memoryStream = new MemoryStream();
		BinaryWriter binaryWriter = new BinaryWriter(memoryStream);
		binaryWriter.Write(dictionary.Count);
		foreach (KeyValuePair<string, CapsuleCollider> keyValuePair2 in dictionary)
		{
			binaryWriter.Write(keyValuePair2.Key);
			this.WriterCapsuleCollider(binaryWriter, keyValuePair2.Value);
		}
		File.WriteAllBytes(path, memoryStream.ToArray());
	}

	private CapsuleCollider ReadCapsuleCollider(BinaryReader reader, GameObject add_collider_object)
	{
		CapsuleCollider capsuleCollider = null;
		Vector3 zero = Vector3.zero;
		zero.x = reader.ReadSingle();
		zero.y = reader.ReadSingle();
		zero.z = reader.ReadSingle();
		int direction = reader.ReadInt32();
		float height = reader.ReadSingle();
		float radius = reader.ReadSingle();
		if (add_collider_object != null && add_collider_object.GetComponent<CapsuleCollider>() == null)
		{
			capsuleCollider = add_collider_object.AddComponent<CapsuleCollider>();
			capsuleCollider.center = zero;
			capsuleCollider.direction = direction;
			capsuleCollider.height = height;
			capsuleCollider.radius = radius;
			capsuleCollider.isTrigger = true;
			capsuleCollider.enabled = false;
			capsuleCollider.enabled = true;
		}
		return capsuleCollider;
	}

	private void WriterCapsuleCollider(BinaryWriter writer, CapsuleCollider collider)
	{
		writer.Write(collider.center.x);
		writer.Write(collider.center.y);
		writer.Write(collider.center.z);
		writer.Write(collider.direction);
		writer.Write(collider.height);
		writer.Write(collider.radius);
	}

	private Dictionary<string, CapsuleCollider> FindCapsuleCollider()
	{
		Action<Transform, string, Dictionary<string, CapsuleCollider>> FindFunc = null;
		FindFunc = delegate(Transform target, string path, Dictionary<string, CapsuleCollider> path_dic)
		{
			path += target.name;
			CapsuleCollider component = target.GetComponent<CapsuleCollider>();
			if (component != null)
			{
				path_dic.Add(path, component);
			}
			for (int j = 0; j < target.childCount; j++)
			{
				FindFunc(target.GetChild(j), path + "/", path_dic);
			}
		};
		Dictionary<string, CapsuleCollider> dictionary = new Dictionary<string, CapsuleCollider>();
		for (int i = 0; i < base.transform.childCount; i++)
		{
			FindFunc(base.transform.GetChild(i), string.Empty, dictionary);
		}
		return dictionary;
	}

	private string[] add_obj_name_array_ = new string[]
	{
		"OvrGrabHit_",
		"OvrTouchHit_"
	};

	private string[] file_path_array_ = new string[]
	{
		"System/maid_collider.bytes",
		"System/maid_collider_touch.bytes"
	};

	private int[] layer_array_ = new int[]
	{
		17,
		19
	};

	private List<CapsuleCollider>[] collider_array_ = new List<CapsuleCollider>[2];

	public enum ColliderType
	{
		NON = -1,
		Grab,
		Touch,
		MAX
	}
}
