using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using TriLib;
using UnityEngine;
using UnityEngine.Rendering;

public class ImportCM : MonoBehaviour
{
	private static ImportCM.BoneUse RemoveNoWeightBone(Dictionary<string, ImportCM.BoneUse> dic, Transform parent)
	{
		bool flag = false;
		if (parent.name == "Skirt")
		{
			return null;
		}
		ImportCM.BoneUse[] array = new ImportCM.BoneUse[parent.childCount];
		for (int i = 0; i < parent.childCount; i++)
		{
			ImportCM.BoneUse boneUse = array[i] = ImportCM.RemoveNoWeightBone(dic, parent.GetChild(i));
			flag |= (boneUse == null || boneUse.use);
		}
		foreach (ImportCM.BoneUse boneUse2 in array)
		{
			if (boneUse2 != null && boneUse2.bone != null && boneUse2.delete)
			{
				UnityEngine.Object.DestroyImmediate(boneUse2.bone.gameObject);
				boneUse2.bone = null;
			}
		}
		if (flag)
		{
			return null;
		}
		ImportCM.BoneUse boneUse2;
		if (dic.TryGetValue(parent.name, out boneUse2))
		{
			boneUse2.delete = (!boneUse2.use && !flag);
			return boneUse2;
		}
		return null;
	}

	public static GameObject LoadSkinMesh_R(string filename, TMorph morph, string slotname, TBodySkin bodyskin, int layer)
	{
		try
		{
			using (AFileBase afileBase = GameUty.FileOpen(filename, null))
			{
				if (ImportCM.m_skinTempFile == null)
				{
					ImportCM.m_skinTempFile = new byte[Math.Max(500000, afileBase.GetSize())];
				}
				else if (ImportCM.m_skinTempFile.Length < afileBase.GetSize())
				{
					ImportCM.m_skinTempFile = new byte[afileBase.GetSize()];
				}
				afileBase.Read(ref ImportCM.m_skinTempFile, afileBase.GetSize());
			}
		}
		catch (Exception ex)
		{
			NDebug.Assert("ファイルが開けませんでした。" + filename + "\n" + ex.Message, false);
		}
		BinaryReader binaryReader = new BinaryReader(new MemoryStream(ImportCM.m_skinTempFile), Encoding.UTF8);
		TBodySkin.OriVert oriVert = bodyskin.m_OriVert;
		GameObject gameObject = UnityEngine.Object.Instantiate(Resources.Load("seed")) as GameObject;
		gameObject.layer = layer;
		GameObject gameObject2 = null;
		Hashtable hashtable = new Hashtable();
		string text = binaryReader.ReadString();
		if (text != "CM3D2_MESH")
		{
			NDebug.Assert("LoadSkinMesh_R 例外 : ヘッダーファイルが不正です。" + text, false);
		}
		int num = binaryReader.ReadInt32();
		string str = binaryReader.ReadString();
		gameObject.name = "_SM_" + str;
		string b = binaryReader.ReadString();
		Dictionary<string, ImportCM.BoneUse> dictionary = new Dictionary<string, ImportCM.BoneUse>();
		int num2 = binaryReader.ReadInt32();
		List<GameObject> list = new List<GameObject>();
		for (int i = 0; i < num2; i++)
		{
			GameObject gameObject3 = UnityEngine.Object.Instantiate(Resources.Load("seed")) as GameObject;
			gameObject3.layer = layer;
			gameObject3.name = binaryReader.ReadString();
			list.Add(gameObject3);
			if (gameObject3.name == b)
			{
				gameObject2 = gameObject3;
			}
			hashtable[gameObject3.name] = gameObject3;
			dictionary[gameObject3.name] = new ImportCM.BoneUse(gameObject3.name, -1, gameObject3.transform);
			bool flag = binaryReader.ReadByte() != 0;
			if (flag)
			{
				GameObject gameObject4 = UnityEngine.Object.Instantiate(Resources.Load("seed")) as GameObject;
				gameObject4.name = gameObject3.name + "_SCL_";
				gameObject4.transform.parent = gameObject3.transform;
				hashtable[gameObject3.name + "&_SCL_"] = gameObject4;
				dictionary[gameObject4.name] = new ImportCM.BoneUse(gameObject4.name, -1, gameObject4.transform);
			}
		}
		for (int j = 0; j < num2; j++)
		{
			int num3 = binaryReader.ReadInt32();
			if (num3 >= 0)
			{
				list[j].transform.parent = list[num3].transform;
			}
			else
			{
				list[j].transform.parent = gameObject.transform;
			}
		}
		for (int k = 0; k < num2; k++)
		{
			Transform transform = list[k].transform;
			float x = binaryReader.ReadSingle();
			float y = binaryReader.ReadSingle();
			float z = binaryReader.ReadSingle();
			transform.localPosition = new Vector3(x, y, z);
			float x2 = binaryReader.ReadSingle();
			float y2 = binaryReader.ReadSingle();
			float z2 = binaryReader.ReadSingle();
			float w = binaryReader.ReadSingle();
			transform.localRotation = new Quaternion(x2, y2, z2, w);
			if (2001 <= num)
			{
				bool flag2 = binaryReader.ReadBoolean();
				if (flag2)
				{
					float x3 = binaryReader.ReadSingle();
					float y3 = binaryReader.ReadSingle();
					float z3 = binaryReader.ReadSingle();
					transform.localScale = new Vector3(x3, y3, z3);
				}
			}
		}
		int num4 = binaryReader.ReadInt32();
		int num5 = binaryReader.ReadInt32();
		int num6 = binaryReader.ReadInt32();
		oriVert.VCount = num4;
		oriVert.nSubMeshCount = num5;
		SkinnedMeshRenderer skinnedMeshRenderer = gameObject2.AddComponent<SkinnedMeshRenderer>();
		skinnedMeshRenderer.updateWhenOffscreen = true;
		skinnedMeshRenderer.skinnedMotionVectors = false;
		skinnedMeshRenderer.lightProbeUsage = LightProbeUsage.Off;
		skinnedMeshRenderer.reflectionProbeUsage = ReflectionProbeUsage.Off;
		skinnedMeshRenderer.motionVectorGenerationMode = MotionVectorGenerationMode.ForceNoMotion;
		if (!bodyskin.body.boMAN)
		{
			if (slotname == "head")
			{
				skinnedMeshRenderer.castShadows = false;
			}
			if (bodyskin.Category == "chikubi")
			{
				skinnedMeshRenderer.castShadows = false;
			}
			if (bodyskin.Category.IndexOf("seieki_") == 0)
			{
				skinnedMeshRenderer.castShadows = false;
			}
		}
		bodyskin.listDEL.Add(gameObject2);
		ImportCM.BoneUse[] array = new ImportCM.BoneUse[num6];
		Transform[] array2 = new Transform[num6];
		for (int l = 0; l < num6; l++)
		{
			string text2 = binaryReader.ReadString();
			if (!hashtable.ContainsKey(text2))
			{
				Debug.LogError("nullbone= " + text2);
			}
			else
			{
				GameObject gameObject5;
				if (hashtable.ContainsKey(text2 + "&_SCL_"))
				{
					gameObject5 = (GameObject)hashtable[text2 + "&_SCL_"];
				}
				else
				{
					gameObject5 = (GameObject)hashtable[text2];
				}
				array2[l] = gameObject5.transform;
				ImportCM.BoneUse[] array3 = array;
				int num7 = l;
				ImportCM.BoneUse boneUse = new ImportCM.BoneUse(gameObject5.name, l, gameObject5.transform);
				dictionary[gameObject5.name] = boneUse;
				array3[num7] = boneUse;
			}
		}
		skinnedMeshRenderer.bones = array2;
		Mesh mesh = new Mesh();
		skinnedMeshRenderer.sharedMesh = mesh;
		Mesh mesh2 = mesh;
		bodyskin.listDEL.Add(mesh2);
		Matrix4x4[] array4 = new Matrix4x4[num6];
		for (int m = 0; m < num6; m++)
		{
			for (int n = 0; n < 16; n++)
			{
				array4[m][n] = binaryReader.ReadSingle();
			}
		}
		mesh2.bindposes = array4;
		Vector3[] array5 = new Vector3[num4];
		Vector3[] array6 = new Vector3[num4];
		Vector2[] array7 = new Vector2[num4];
		BoneWeight[] array8 = new BoneWeight[num4];
		for (int num8 = 0; num8 < num4; num8++)
		{
			float num9 = binaryReader.ReadSingle();
			float num10 = binaryReader.ReadSingle();
			float new_z = binaryReader.ReadSingle();
			array5[num8].Set(num9, num10, new_z);
			num9 = binaryReader.ReadSingle();
			num10 = binaryReader.ReadSingle();
			new_z = binaryReader.ReadSingle();
			array6[num8].Set(num9, num10, new_z);
			num9 = binaryReader.ReadSingle();
			num10 = binaryReader.ReadSingle();
			array7[num8].Set(num9, num10);
		}
		mesh2.vertices = array5;
		mesh2.normals = array6;
		mesh2.uv = array7;
		oriVert.vOriVert = array5;
		oriVert.vOriNorm = array6;
		int num11 = binaryReader.ReadInt32();
		if (num11 > 0)
		{
			Vector4[] array9 = new Vector4[num11];
			for (int num12 = 0; num12 < num11; num12++)
			{
				float x4 = binaryReader.ReadSingle();
				float y4 = binaryReader.ReadSingle();
				float z4 = binaryReader.ReadSingle();
				float w2 = binaryReader.ReadSingle();
				array9[num12] = new Vector4(x4, y4, z4, w2);
			}
			mesh2.tangents = array9;
		}
		for (int num13 = 0; num13 < num4; num13++)
		{
			int num14 = (int)binaryReader.ReadUInt16();
			array8[num13].boneIndex0 = num14;
			int num15 = num14;
			num14 = (int)binaryReader.ReadUInt16();
			array8[num13].boneIndex1 = num14;
			int num16 = num14;
			num14 = (int)binaryReader.ReadUInt16();
			array8[num13].boneIndex2 = num14;
			int num17 = num14;
			num14 = (int)binaryReader.ReadUInt16();
			array8[num13].boneIndex3 = num14;
			int num18 = num14;
			array[num15].use = true;
			array[num16].use = true;
			array[num17].use = true;
			array[num18].use = true;
			array8[num13].weight0 = binaryReader.ReadSingle();
			array8[num13].weight1 = binaryReader.ReadSingle();
			array8[num13].weight2 = binaryReader.ReadSingle();
			array8[num13].weight3 = binaryReader.ReadSingle();
		}
		ImportCM.BoneUse boneUse2;
		if (dictionary.TryGetValue("Bip01", out boneUse2))
		{
			ImportCM.RemoveNoWeightBone(dictionary, boneUse2.bone);
		}
		mesh2.boneWeights = array8;
		mesh2.subMeshCount = num5;
		oriVert.bwWeight = array8;
		oriVert.nSubMeshCount = num5;
		oriVert.nSubMeshOriTri = new int[num5][];
		for (int num19 = 0; num19 < num5; num19++)
		{
			int num20 = binaryReader.ReadInt32();
			int[] array10 = new int[num20];
			for (int num21 = 0; num21 < num20; num21++)
			{
				array10[num21] = (int)binaryReader.ReadUInt16();
			}
			oriVert.nSubMeshOriTri[num19] = array10;
			mesh2.SetTriangles(array10, num19);
		}
		int num22 = binaryReader.ReadInt32();
		Material[] array11 = new Material[num22];
		for (int num23 = 0; num23 < num22; num23++)
		{
			Material material = ImportCM.ReadMaterial(binaryReader, bodyskin, null);
			array11[num23] = material;
		}
		skinnedMeshRenderer.materials = array11;
		for (;;)
		{
			string a = binaryReader.ReadString();
			if (a == "end")
			{
				break;
			}
			if (a == "morph")
			{
				morph.LoadMoprhData2(binaryReader);
			}
		}
		binaryReader.Close();
		return gameObject;
	}

	public static Material LoadMaterial(string f_strFileName, TBodySkin bodyskin, Material existmat = null)
	{
		try
		{
			using (AFileBase afileBase = GameUty.FileOpen(f_strFileName, null))
			{
				NDebug.Assert(afileBase.IsValid(), "LoadMaterial マテリアルコンテナが読めません。 :" + f_strFileName);
				if (ImportCM.m_matTempFile == null)
				{
					ImportCM.m_matTempFile = new byte[Math.Max(10000, afileBase.GetSize())];
				}
				else if (ImportCM.m_matTempFile.Length < afileBase.GetSize())
				{
					ImportCM.m_matTempFile = new byte[afileBase.GetSize()];
				}
				afileBase.Read(ref ImportCM.m_matTempFile, afileBase.GetSize());
			}
		}
		catch (Exception ex)
		{
			Debug.LogError(string.Concat(new string[]
			{
				"LoadMaterial マテリアルコンテナが読み込めませんでした。 : ",
				f_strFileName,
				" : ",
				ex.Message,
				" : StackTrace ：\n",
				ex.StackTrace
			}));
			throw ex;
		}
		BinaryReader binaryReader = new BinaryReader(new MemoryStream(ImportCM.m_matTempFile), Encoding.UTF8);
		string text = binaryReader.ReadString();
		if (text != "CM3D2_MATERIAL")
		{
			NDebug.Assert("ProcScriptBin 例外 : ヘッダーファイルが不正です。" + text, false);
		}
		int num = binaryReader.ReadInt32();
		string text2 = binaryReader.ReadString();
		Material result = ImportCM.ReadMaterial(binaryReader, bodyskin, existmat);
		binaryReader.Close();
		return result;
	}

	public static Material ReadMaterial(BinaryReader r, TBodySkin bodyskin = null, Material existmat = null)
	{
		if (ImportCM.m_hashPriorityMaterials == null)
		{
			ImportCM.m_hashPriorityMaterials = new Dictionary<int, KeyValuePair<string, float>>();
			string[] list = GameUty.FileSystem.GetList("prioritymaterial", AFileSystemBase.ListType.AllFile);
			if (list != null && 0 < list.Length)
			{
				for (int i = 0; i < list.Length; i++)
				{
					if (Path.GetExtension(list[i]) == ".pmat")
					{
						string text = list[i];
						using (AFileBase afileBase = GameUty.FileOpen(text, null))
						{
							NDebug.Assert(afileBase.IsValid(), text + "を開けませんでした");
							byte[] buffer = afileBase.ReadAll();
							using (BinaryReader binaryReader = new BinaryReader(new MemoryStream(buffer), Encoding.UTF8))
							{
								string a = binaryReader.ReadString();
								if (a != "CM3D2_PMATERIAL")
								{
									NDebug.Assert("ヘッダーエラー\n" + text, false);
								}
								int num = binaryReader.ReadInt32();
								int key = binaryReader.ReadInt32();
								string key2 = binaryReader.ReadString();
								float value = binaryReader.ReadSingle();
								NDebug.Assert(!ImportCM.m_hashPriorityMaterials.ContainsKey(key), "すでにハッシュが登録されています");
								ImportCM.m_hashPriorityMaterials.Add(key, new KeyValuePair<string, float>(key2, value));
							}
						}
					}
				}
			}
		}
		string name = r.ReadString();
		string text2 = r.ReadString();
		string text3 = r.ReadString();
		Material material2;
		if (existmat == null)
		{
			Material material;
			if (!ImportCM.m_dicCacheMaterial.TryGetValue(text3, out material))
			{
				string text4 = "DefMaterial/" + text3;
				material = (Resources.Load(text4, typeof(Material)) as Material);
				if (material == null)
				{
					NDebug.Assert("DefMaterialが見つかりません。" + text4, false);
				}
				ImportCM.m_dicCacheMaterial[text3] = material;
			}
			material2 = UnityEngine.Object.Instantiate<Material>(material);
			if (bodyskin != null)
			{
				bodyskin.listDEL.Add(material2);
			}
		}
		else
		{
			material2 = existmat;
			if (material2.shader.name != text2)
			{
				NDebug.Warning("マテリアル入れ替えエラー。違うシェーダーに入れようとしました。 " + text2 + " -> " + material2.shader.name);
			}
		}
		material2.name = name;
		int hashCode = material2.name.GetHashCode();
		if (ImportCM.m_hashPriorityMaterials != null && ImportCM.m_hashPriorityMaterials.ContainsKey(hashCode))
		{
			KeyValuePair<string, float> keyValuePair = ImportCM.m_hashPriorityMaterials[hashCode];
			if (keyValuePair.Key == material2.name)
			{
				material2.SetFloat("_SetManualRenderQueue", keyValuePair.Value);
				material2.renderQueue = (int)keyValuePair.Value;
			}
		}
		for (;;)
		{
			string text5 = r.ReadString();
			if (text5 == "end")
			{
				break;
			}
			string name2 = r.ReadString();
			if (text5 == "tex")
			{
				string a2 = r.ReadString();
				if (a2 == "null")
				{
					material2.SetTexture(name2, null);
				}
				else if (a2 == "tex2d")
				{
					string text6 = r.ReadString();
					string text7 = r.ReadString();
					Texture2D texture2D = ImportCM.CreateTexture(text6 + ".tex");
					texture2D.name = text6;
					texture2D.wrapMode = TextureWrapMode.Clamp;
					material2.SetTexture(name2, texture2D);
					if (bodyskin != null)
					{
						bodyskin.listDEL.Add(texture2D);
					}
					Vector2 value2;
					value2.x = r.ReadSingle();
					value2.y = r.ReadSingle();
					material2.SetTextureOffset(name2, value2);
					Vector2 value3;
					value3.x = r.ReadSingle();
					value3.y = r.ReadSingle();
					material2.SetTextureScale(name2, value3);
				}
				else if (a2 == "texRT")
				{
					string text8 = r.ReadString();
					string text9 = r.ReadString();
				}
			}
			else if (text5 == "col")
			{
				Color value4;
				value4.r = r.ReadSingle();
				value4.g = r.ReadSingle();
				value4.b = r.ReadSingle();
				value4.a = r.ReadSingle();
				material2.SetColor(name2, value4);
			}
			else if (text5 == "vec")
			{
				Vector4 value5;
				value5.x = r.ReadSingle();
				value5.y = r.ReadSingle();
				value5.z = r.ReadSingle();
				value5.w = r.ReadSingle();
				material2.SetVector(name2, value5);
			}
			else if (text5 == "f")
			{
				float value6 = r.ReadSingle();
				material2.SetFloat(name2, value6);
			}
			else
			{
				Debug.LogError("マテリアルが読み込めません。不正なマテリアルプロパティ型です " + text5);
			}
		}
		return material2;
	}

	public static GameObject LoadOnlyBone_R(byte[] bs)
	{
		GameObject gameObject = new GameObject();
		gameObject.layer = 10;
		BinaryReader binaryReader = new BinaryReader(new MemoryStream(bs));
		Hashtable hashtable = new Hashtable();
		string text = binaryReader.ReadString();
		if (text != "CM3D2_MESH")
		{
			NDebug.Assert("LoadOnlyBone_R 例外 : ヘッダーファイルが不正です。" + text, false);
		}
		int num = binaryReader.ReadInt32();
		string str = binaryReader.ReadString();
		gameObject.name = "_BO_" + str;
		string text2 = binaryReader.ReadString();
		int num2 = binaryReader.ReadInt32();
		List<GameObject> list = new List<GameObject>();
		for (int i = 0; i < num2; i++)
		{
			GameObject gameObject2 = UnityEngine.Object.Instantiate(Resources.Load("seed")) as GameObject;
			gameObject2.layer = 10;
			gameObject2.name = binaryReader.ReadString();
			list.Add(gameObject2);
			hashtable[gameObject2.name] = gameObject2;
			bool flag = binaryReader.ReadByte() != 0;
			if (flag)
			{
				GameObject gameObject3 = UnityEngine.Object.Instantiate(Resources.Load("seed")) as GameObject;
				gameObject2.layer = 10;
				gameObject3.name = gameObject2.name + "_SCL_";
				gameObject3.transform.parent = gameObject2.transform;
				hashtable[gameObject2.name + "&_SCL_"] = gameObject3;
			}
		}
		for (int j = 0; j < num2; j++)
		{
			int num3 = binaryReader.ReadInt32();
			if (num3 >= 0)
			{
				list[j].transform.parent = list[num3].transform;
			}
			else
			{
				list[j].transform.parent = gameObject.transform;
			}
		}
		for (int k = 0; k < num2; k++)
		{
			Transform transform = list[k].transform;
			float x = binaryReader.ReadSingle();
			float y = binaryReader.ReadSingle();
			float z = binaryReader.ReadSingle();
			transform.localPosition = new Vector3(x, y, z);
			float x2 = binaryReader.ReadSingle();
			float y2 = binaryReader.ReadSingle();
			float z2 = binaryReader.ReadSingle();
			float w = binaryReader.ReadSingle();
			transform.localRotation = new Quaternion(x2, y2, z2, w);
			if (2001 <= num)
			{
				bool flag2 = binaryReader.ReadBoolean();
				if (flag2)
				{
					float x3 = binaryReader.ReadSingle();
					float y3 = binaryReader.ReadSingle();
					float z3 = binaryReader.ReadSingle();
					transform.localScale = new Vector3(x3, y3, z3);
				}
			}
		}
		binaryReader.Close();
		gameObject.AddComponent(typeof(Animation));
		return gameObject;
	}

	public static Dictionary<string, ImportCM.OldSkirtBoneRev> LoadOldSkirtBoneRev(string f_strModelName)
	{
		Dictionary<string, ImportCM.OldSkirtBoneRev> dictionary = null;
		if (f_strModelName.Contains("dress020_onep"))
		{
			dictionary = new Dictionary<string, ImportCM.OldSkirtBoneRev>();
			for (int i = 1; i <= 12; i++)
			{
				dictionary.Add("Skirt" + i.ToString("00") + "_A_yure_skirt_h_", new ImportCM.OldSkirtBoneRev
				{
					bRecursiv = true,
					vRotRevInit = new Vector3((i != 7 && i != 8) ? 90f : -90f, 0f, 0f),
					vRotRevAfter = new Vector3((i != 7 && i != 8) ? -90f : 90f, 0f, 0f)
				});
			}
		}
		else if (f_strModelName.Contains("dress131_onep"))
		{
			dictionary = new Dictionary<string, ImportCM.OldSkirtBoneRev>();
			for (int j = 1; j <= 12; j++)
			{
				dictionary.Add("Skirt" + j.ToString("00") + "_A_yure_skirt_", new ImportCM.OldSkirtBoneRev
				{
					bRecursiv = true,
					vRotRevInit = new Vector3(0f, 0f, 0f),
					vRotRevAfter = new Vector3(180f, 0f, 0f)
				});
			}
		}
		return dictionary;
	}

	public static Texture2D CreateTexture(string f_strFileName)
	{
		return ImportCM.LoadTexture(GameUty.FileSystem, f_strFileName, true).CreateTexture2D();
	}

	public static Texture2D CreateTexture(AFileSystemBase f_fileSystem, string f_strFileName)
	{
		return ImportCM.LoadTexture(f_fileSystem, f_strFileName, true).CreateTexture2D();
	}

	public static TextureResource LoadTexture(AFileSystemBase f_fileSystem, string f_strFileName, bool usePoolBuffer)
	{
		try
		{
			using (AFileBase afileBase = GameUty.FileOpen(f_strFileName, f_fileSystem))
			{
				NDebug.Assert(afileBase.IsValid(), "LoadTexture テクスチャコンテナが読めません。 :" + f_strFileName);
				if (ImportCM.m_texTempFile == null)
				{
					ImportCM.m_texTempFile = new byte[Math.Max(500000, afileBase.GetSize())];
				}
				else if (ImportCM.m_texTempFile.Length < afileBase.GetSize())
				{
					ImportCM.m_texTempFile = new byte[afileBase.GetSize()];
				}
				afileBase.Read(ref ImportCM.m_texTempFile, afileBase.GetSize());
			}
		}
		catch (Exception ex)
		{
			Debug.LogError(string.Concat(new string[]
			{
				"LoadTexture テクスチャコンテナが読み込めませんでした。 : ",
				f_strFileName,
				" : ",
				ex.Message,
				" : StackTrace ：\n",
				ex.StackTrace
			}));
			throw ex;
		}
		BinaryReader binaryReader = new BinaryReader(new MemoryStream(ImportCM.m_texTempFile), Encoding.UTF8);
		string text = binaryReader.ReadString();
		if (text != "CM3D2_TEX")
		{
			NDebug.Assert("ProcScriptBin 例外 : ヘッダーファイルが不正です。" + text, false);
		}
		int num = binaryReader.ReadInt32();
		string text2 = binaryReader.ReadString();
		int width = 0;
		int height = 0;
		TextureFormat textureFormat = TextureFormat.ARGB32;
		Rect[] array = null;
		if (1010 <= num)
		{
			if (1011 <= num)
			{
				int num2 = binaryReader.ReadInt32();
				if (0 < num2)
				{
					array = new Rect[num2];
					for (int i = 0; i < num2; i++)
					{
						float x = binaryReader.ReadSingle();
						float y = binaryReader.ReadSingle();
						float width2 = binaryReader.ReadSingle();
						float height2 = binaryReader.ReadSingle();
						array[i] = new Rect(x, y, width2, height2);
					}
				}
			}
			width = binaryReader.ReadInt32();
			height = binaryReader.ReadInt32();
			textureFormat = (TextureFormat)binaryReader.ReadInt32();
		}
		int num3 = binaryReader.ReadInt32();
		byte[] array2;
		if (usePoolBuffer && textureFormat == TextureFormat.ARGB32)
		{
			if (ImportCM.m_texTempImg == null)
			{
				ImportCM.m_texTempImg = new byte[Math.Max(500000, num3)];
			}
			else if (ImportCM.m_texTempImg.Length < num3)
			{
				ImportCM.m_texTempImg = new byte[num3];
			}
			binaryReader.Read(ImportCM.m_texTempImg, 0, num3);
			array2 = ImportCM.m_texTempImg;
		}
		else
		{
			array2 = new byte[num3];
			binaryReader.Read(array2, 0, num3);
		}
		if (num == 1000)
		{
			width = ((int)array2[16] << 24 | (int)array2[17] << 16 | (int)array2[18] << 8 | (int)array2[19]);
			height = ((int)array2[20] << 24 | (int)array2[21] << 16 | (int)array2[22] << 8 | (int)array2[23]);
		}
		binaryReader.Close();
		return new TextureResource(width, height, textureFormat, array, array2);
	}

	public static AnimationClip LoadAniClip(string f_strFileName, bool load_l_mune_anime, bool load_r_mune_anime, bool no_chara = false)
	{
		try
		{
			using (AFileBase afileBase = GameUty.FileOpen(f_strFileName, null))
			{
				NDebug.Assert(afileBase.IsValid(), "LoadAniClip アニメーションファイルが開けません。 :" + f_strFileName);
				if (ImportCM.m_aniTempFile == null)
				{
					ImportCM.m_aniTempFile = new byte[Math.Max(500000, afileBase.GetSize())];
				}
				else if (ImportCM.m_aniTempFile.Length < afileBase.GetSize())
				{
					ImportCM.m_aniTempFile = new byte[afileBase.GetSize()];
				}
				afileBase.Read(ref ImportCM.m_aniTempFile, afileBase.GetSize());
			}
		}
		catch (Exception ex)
		{
			Debug.LogError(string.Concat(new string[]
			{
				"LoadAniClip アニメーションファイルが読み込めませんでした。 : ",
				f_strFileName,
				" : ",
				ex.Message,
				" : StackTrace ：\n",
				ex.StackTrace
			}));
			throw ex;
		}
		BinaryReader binaryReader = new BinaryReader(new MemoryStream(ImportCM.m_aniTempFile), Encoding.UTF8);
		string text = binaryReader.ReadString();
		if (text != "CM3D2_ANIM")
		{
			NDebug.Assert("LoadAniClip 例外 : ヘッダーファイルが不正です。" + text, false);
		}
		int num = binaryReader.ReadInt32();
		AnimationClip animationClip = new AnimationClip();
		if (animationClip != null)
		{
			animationClip.legacy = true;
		}
		string text2 = string.Empty;
		int num2 = 0;
		for (;;)
		{
			byte b = binaryReader.ReadByte();
			if (b == 0)
			{
				break;
			}
			if (b == 1)
			{
				text2 = binaryReader.ReadString();
				num2++;
			}
			else if (b >= 100)
			{
				int num3 = binaryReader.ReadInt32();
				Keyframe[] array = new Keyframe[num3];
				AnimationCurve animationCurve = new AnimationCurve();
				for (int i = 0; i < num3; i++)
				{
					array[i].time = binaryReader.ReadSingle();
					array[i].value = binaryReader.ReadSingle();
					array[i].inTangent = binaryReader.ReadSingle();
					array[i].outTangent = binaryReader.ReadSingle();
				}
				animationCurve.keys = array;
				bool flag = true;
				string text3 = text2 + "*";
				if (!no_chara)
				{
					if (b >= 104 && b <= 106 && !text3.Contains("Bip01*") && !text3.Contains("_IK_") && !text3.Contains("ManBip*") && !text3.Contains("Hip_L") && !text3.Contains("Hip_R"))
					{
						flag = false;
					}
					if (!load_l_mune_anime && text3.Contains("Mune_L"))
					{
						flag = false;
					}
					if (!load_r_mune_anime && text3.Contains("Mune_R"))
					{
						flag = false;
					}
				}
				if (flag)
				{
					animationClip.SetCurve(text2, AnmParse.proptypes[(int)(b - 100)], AnmParse.properties[(int)(b - 100)], animationCurve);
				}
			}
			else
			{
				Debug.LogError("com " + b);
			}
		}
		binaryReader.Close();
		return animationClip;
	}

	public static AnimationClip LoadAniClip(byte[] file_byte, bool load_l_mune_anime, bool load_r_mune_anime, bool no_chara = false)
	{
		if (ImportCM.m_aniTempFile == null)
		{
			ImportCM.m_aniTempFile = new byte[Math.Max(500000, file_byte.Length)];
		}
		else if (ImportCM.m_aniTempFile.Length < file_byte.Length)
		{
			ImportCM.m_aniTempFile = new byte[file_byte.Length];
		}
		Buffer.BlockCopy(file_byte, 0, ImportCM.m_aniTempFile, 0, file_byte.Length);
		BinaryReader binaryReader = new BinaryReader(new MemoryStream(ImportCM.m_aniTempFile), Encoding.UTF8);
		string text = binaryReader.ReadString();
		if (text != "CM3D2_ANIM")
		{
			NDebug.Assert("LoadAniClip 例外 : ヘッダーファイルが不正です。" + text, false);
		}
		int num = binaryReader.ReadInt32();
		AnimationClip animationClip = new AnimationClip();
		if (animationClip != null)
		{
			animationClip.legacy = true;
		}
		string text2 = string.Empty;
		int num2 = 0;
		for (;;)
		{
			byte b = binaryReader.ReadByte();
			if (b == 0)
			{
				break;
			}
			if (b == 1)
			{
				text2 = binaryReader.ReadString();
				num2++;
			}
			else if (b >= 100)
			{
				int num3 = binaryReader.ReadInt32();
				Keyframe[] array = new Keyframe[num3];
				AnimationCurve animationCurve = new AnimationCurve();
				for (int i = 0; i < num3; i++)
				{
					array[i].time = binaryReader.ReadSingle();
					array[i].value = binaryReader.ReadSingle();
					array[i].inTangent = binaryReader.ReadSingle();
					array[i].outTangent = binaryReader.ReadSingle();
				}
				animationCurve.keys = array;
				bool flag = true;
				string text3 = text2 + "*";
				if (!no_chara)
				{
					if (!no_chara && b >= 104 && b <= 106 && !text3.Contains("Bip01*") && !text3.Contains("_IK_") && !text3.Contains("ManBip*") && !text3.Contains("Hip_L") && !text3.Contains("Hip_R"))
					{
						flag = false;
					}
					if (!load_l_mune_anime && text3.Contains("Mune_L"))
					{
						flag = false;
					}
					if (!load_r_mune_anime && text3.Contains("Mune_R"))
					{
						flag = false;
					}
					if (flag)
					{
						animationClip.SetCurve(text2, AnmParse.proptypes[(int)(b - 100)], AnmParse.properties[(int)(b - 100)], animationCurve);
					}
				}
			}
			else
			{
				Debug.LogError("com " + b);
			}
		}
		binaryReader.Close();
		return animationClip;
	}

	public static AnimationClip LoadAniClipNative(AFileSystemBase fileSystem, string fileName, bool load_l_mune_anime, bool load_r_mune_anime, bool no_chara = false)
	{
		AnimationClip result = null;
		using (AFileBase afileBase = GameUty.FileOpen(fileName, fileSystem))
		{
			if (afileBase == null || !afileBase.IsValid())
			{
				Debug.LogError(fileName + " はありませんでした。");
				return null;
			}
			result = GameMain.Instance.AnmParse.LoadAnmClip(afileBase, load_l_mune_anime, load_r_mune_anime, no_chara);
		}
		return result;
	}

	public static AnimationClip LoadAniClipNative(byte[] file_byte, bool load_l_mune_anime, bool load_r_mune_anime, bool no_chara = false)
	{
		return GameMain.Instance.AnmParse.LoadAnmClip(file_byte, load_l_mune_anime, load_r_mune_anime, no_chara);
	}

	public static DynamicBone TryLoadDynamicBone(GameObject f_goRoot, Transform f_trHairRoot, string f_strFileName, TBody body, float[] f_aryDefStiffness)
	{
		try
		{
			using (AFileBase afileBase = GameUty.FileOpen(f_strFileName + ".phy", null))
			{
				if (afileBase == null || !afileBase.IsValid())
				{
					return null;
				}
				if (ImportCM.m_byPhyTempFile == null)
				{
					ImportCM.m_byPhyTempFile = new byte[Math.Max(50000, afileBase.GetSize())];
				}
				else if (ImportCM.m_byPhyTempFile.Length < afileBase.GetSize())
				{
					ImportCM.m_byPhyTempFile = new byte[afileBase.GetSize()];
				}
				afileBase.Read(ref ImportCM.m_byPhyTempFile, afileBase.GetSize());
			}
		}
		catch (Exception ex)
		{
			Debug.LogError(string.Concat(new string[]
			{
				"TryLoadDynamicBone 物理定義ファイルが読み込めませんでした。 : ",
				f_strFileName,
				".phy : ",
				ex.Message,
				" : StackTrace ：\n",
				ex.StackTrace
			}));
			throw ex;
		}
		BinaryReader binaryReader = new BinaryReader(new MemoryStream(ImportCM.m_byPhyTempFile), Encoding.UTF8);
		DynamicBone dynamicBone = f_goRoot.AddComponent<DynamicBone>();
		if (f_aryDefStiffness != null && f_aryDefStiffness.Length == 3)
		{
			dynamicBone.m_ValueFromBone[2, 0] = f_aryDefStiffness[0];
			dynamicBone.m_ValueFromBone[2, 1] = f_aryDefStiffness[1];
			dynamicBone.m_ValueFromBone[2, 2] = f_aryDefStiffness[2];
		}
		dynamicBone.m_Root = f_trHairRoot;
		dynamicBone.SerializeRead(binaryReader);
		dynamicBone.m_strNowReadPhysicsFileName = f_strFileName;
		dynamicBone.m_hitFloorPlane = body.m_hitFloorPlane;
		binaryReader.Close();
		return dynamicBone;
	}

	public static bool LoadDynamicCollilder(DynamicBone f_db, string f_strFileName, Transform f_trBodyBoneRoot, Maid f_maid)
	{
		try
		{
			using (AFileBase afileBase = GameUty.FileOpen(f_strFileName + ".col", null))
			{
				if (afileBase == null || !afileBase.IsValid())
				{
					Debug.LogError(f_strFileName + ".col が開けません。");
					return false;
				}
				if (ImportCM.m_byPhyTempFile == null)
				{
					ImportCM.m_byPhyTempFile = new byte[Math.Max(50000, afileBase.GetSize())];
				}
				else if (ImportCM.m_byPhyTempFile.Length < afileBase.GetSize())
				{
					ImportCM.m_byPhyTempFile = new byte[afileBase.GetSize()];
				}
				afileBase.Read(ref ImportCM.m_byPhyTempFile, afileBase.GetSize());
			}
		}
		catch (Exception ex)
		{
			Debug.LogError(string.Concat(new string[]
			{
				"LoadDynamicCollilder 物理Colliderファイルが読み込めませんでした。 : ",
				f_strFileName,
				".col : ",
				ex.Message,
				" : StackTrace ：\n",
				ex.StackTrace
			}));
			throw ex;
		}
		BinaryReader binaryReader = new BinaryReader(new MemoryStream(ImportCM.m_byPhyTempFile), Encoding.UTF8);
		f_db.SerializeReadCollider(binaryReader, f_trBodyBoneRoot, Path.GetFileNameWithoutExtension(f_strFileName), f_maid);
		binaryReader.Close();
		return true;
	}

	public static DynamicSkirtBone TryLoadDynamicSkirtBone(GameObject f_goRoot, Transform f_trSkirtRoot, string f_strFileName, TBody body)
	{
		try
		{
			using (AFileBase afileBase = GameUty.FileOpen(f_strFileName + ".psk", null))
			{
				if (afileBase == null || !afileBase.IsValid())
				{
					return null;
				}
				if (ImportCM.m_byPhyTempFile == null)
				{
					ImportCM.m_byPhyTempFile = new byte[Math.Max(50000, afileBase.GetSize())];
				}
				else if (ImportCM.m_byPhyTempFile.Length < afileBase.GetSize())
				{
					ImportCM.m_byPhyTempFile = new byte[afileBase.GetSize()];
				}
				afileBase.Read(ref ImportCM.m_byPhyTempFile, afileBase.GetSize());
			}
		}
		catch (Exception ex)
		{
			Debug.LogError(string.Concat(new string[]
			{
				"TryLoadSkirtDynamicBone スカート物理定義ファイルが読み込めませんでした。 : ",
				f_strFileName,
				".psk : ",
				ex.Message,
				" : StackTrace ：\n",
				ex.StackTrace
			}));
			throw ex;
		}
		BinaryReader binaryReader = new BinaryReader(new MemoryStream(ImportCM.m_byPhyTempFile), Encoding.UTF8);
		DynamicSkirtBone dynamicSkirtBone = f_goRoot.AddComponent<DynamicSkirtBone>();
		dynamicSkirtBone.SerializeRead(binaryReader);
		binaryReader.Close();
		return dynamicSkirtBone;
	}

	public static GameObject LoadGeneralModelFile(string f_strPathFileName)
	{
		GameObject gameObject = null;
		GameObject result;
		using (AssetLoader assetLoader = new AssetLoader())
		{
			try
			{
				AssetLoaderOptions assetLoaderOptions = AssetLoaderOptions.CreateInstance();
				assetLoaderOptions.AutoPlayAnimations = true;
				gameObject = assetLoader.LoadFromFile(f_strPathFileName, assetLoaderOptions, null);
			}
			catch (Exception ex)
			{
				Debug.LogError(ex.ToString());
			}
			result = gameObject;
		}
		return result;
	}

	private static byte[] m_skinTempFile = null;

	private static byte[] m_matTempFile = null;

	private static Dictionary<int, KeyValuePair<string, float>> m_hashPriorityMaterials = null;

	private static Dictionary<string, Material> m_dicCacheMaterial = new Dictionary<string, Material>();

	private static byte[] m_texTempFile = null;

	private static byte[] m_texTempImg = null;

	public static byte[] m_aniTempFile = null;

	public static byte[] m_byPhyTempFile = null;

	private class BoneUse
	{
		public BoneUse(string f_name, int f_idx, Transform f_bone)
		{
			this.name = f_name;
			this.idx = f_idx;
			this.bone = f_bone;
		}

		public bool use;

		public string name;

		public int idx;

		public Transform bone;

		public bool delete;
	}

	public class OldSkirtBoneRev
	{
		public bool bRecursiv = true;

		public Vector3 vRotRevInit;

		public Vector3 vRotRevAfter;
	}
}
