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

public class ModCompile : MonoBehaviour
{
	private void Start()
	{
	}

	public static bool CompileMenuScript(string f_strInMenuTextPathFileName)
	{
		string text = Path.GetDirectoryName(f_strInMenuTextPathFileName) + "\\_compiled";
		if (!Directory.Exists(text))
		{
			Directory.CreateDirectory(text);
		}
		string path = text + "\\" + Path.GetFileNameWithoutExtension(f_strInMenuTextPathFileName);
		for (int i = 0; i < 99; i++)
		{
			string[] aryDefCategoryName = ModCompile.m_aryDefCategoryName;
			int num = i;
			MPN mpn = (MPN)i;
			aryDefCategoryName[num] = mpn.ToString().ToLower();
		}
		ModCompile.m_aryDefSlotName = new string[(TBody.m_strDefSlotName.Length - 1) / 3];
		for (int j = 0; j < TBody.m_strDefSlotName.Length - 1; j++)
		{
			try
			{
				if (j % 3 == 0)
				{
					ModCompile.m_aryDefSlotName[j / 3] = TBody.m_strDefSlotName[j].ToLower();
				}
			}
			catch (Exception ex)
			{
				NDebug.MessageBox("MOD Compile", ex.Message);
			}
		}
		string a = string.Empty;
		string text2 = string.Empty;
		string value = string.Empty;
		string value2 = string.Empty;
		Dictionary<int, string> dictionary = new Dictionary<int, string>();
		StreamReader streamReader = new StreamReader(f_strInMenuTextPathFileName, Encoding.UTF8);
		string text3;
		while ((text3 = streamReader.ReadLine()) != null)
		{
			if (!(text3 == string.Empty))
			{
				if (text3.IndexOf("/") != 0)
				{
					string a2 = UTY.GetStringCom(text3).ToLower();
					string[] stringList = UTY.GetStringList(text3);
					if (a2 == "name")
					{
						value = stringList[1];
					}
					else if (a2 == "setumei")
					{
						value2 = stringList[1];
					}
					else if (a2 == "メニューフォルダ")
					{
						a = stringList[1].ToLower();
					}
					else if (a2 == "additem")
					{
						if (stringList.Length != 3 && stringList.Length != 5 && stringList.Length != 6)
						{
							NDebug.MessageBox("MOD Compile", "AddItemは引数が3or5or6つ必要です。<AddItem> <モデルファイル名.model> <MPN> [[<アタッチ/ボーンにアタッチ>] <アタッチMPN> <アタッチ先位置名>]");
							return false;
						}
						string text4 = stringList[2].ToLower();
						string value3 = string.Empty;
						foreach (string text5 in ModCompile.m_aryDefSlotName)
						{
							if (text4 == text5)
							{
								value3 = text5;
								break;
							}
						}
						if (string.IsNullOrEmpty(value3))
						{
							NDebug.MessageBox("MOD Compile", "additem の " + text4 + " スロットはありません。" + f_strInMenuTextPathFileName);
						}
					}
					else if (a2 == "delitem")
					{
						if (stringList.Length >= 2)
						{
							string chk = stringList[1].ToLower();
							string value4 = Array.Find<string>(ModCompile.m_aryDefSlotName, (string s) => s == chk);
							if (string.IsNullOrEmpty(value4))
							{
								NDebug.MessageBox("MOD Compile", "delitem の " + chk + " カテゴリはありません。" + f_strInMenuTextPathFileName);
							}
						}
					}
					else if (a2 == "category")
					{
						text2 = stringList[1].ToLower();
						string chk = text2.ToLower();
						string text6 = Array.Find<string>(ModCompile.m_aryDefCategoryName, (string s) => s == chk);
						if (!string.IsNullOrEmpty(text6))
						{
							if (text2 != string.Empty && text6 != text2)
							{
								NDebug.MessageBox("MOD Compile", "category SaveItemName不明です。カテゴリ " + chk + " はありません。 " + f_strInMenuTextPathFileName);
							}
							text2 = text6;
						}
					}
					else if (a2 == "tex" || a2 == "テクスチャ変更")
					{
						int key = int.Parse(stringList[2]);
						string text7 = stringList[4];
						if (stringList.Length == 6)
						{
							string text8 = stringList[5];
							if (dictionary.ContainsKey(key))
							{
								if (dictionary[key] != text8)
								{
									NDebug.MessageBox("MOD Compile", string.Concat(new string[]
									{
										f_strInMenuTextPathFileName,
										"\n同じマテリアル番号が登場しましたが、過去のマテリアル番号と無限色タイプ名が違います。",
										text7,
										" にも ",
										dictionary[key],
										" を指定して下さい。"
									}));
									return false;
								}
							}
							else
							{
								dictionary.Add(key, text8);
							}
						}
						else if (dictionary.ContainsKey(key))
						{
							NDebug.MessageBox("MOD Compile", string.Concat(new string[]
							{
								f_strInMenuTextPathFileName,
								"\n同じマテリアル番号が登場しましたが、過去のマテリアル番号と無限色タイプ名が違います。",
								text7,
								" にも ",
								dictionary[key],
								" を指定して下さい。"
							}));
						}
					}
					else if (a2 == "アイテムパラメータ" && stringList.Length != 4)
					{
						NDebug.MessageBox("MOD Compile", "アイテムパラメータ 命令の引数が不正です。SlotNameを明示的に指定しする必要があります。アイテムパラメータ <スロット名> <変数名> <値> の順です。" + text3);
					}
				}
			}
		}
		if (a == string.Empty)
		{
			NDebug.MessageBox("MOD Compile", "メニューフォルダの指定がありません " + f_strInMenuTextPathFileName);
			a = Path.GetFileName(Path.GetDirectoryName(Path.GetDirectoryName(f_strInMenuTextPathFileName))).ToLower();
		}
		if (string.IsNullOrEmpty(text2))
		{
			NDebug.MessageBox("MOD Compile", "SaveItemNameが空です。 " + f_strInMenuTextPathFileName);
		}
		MemoryStream memoryStream = new MemoryStream();
		BinaryWriter binaryWriter = new BinaryWriter(memoryStream);
		StreamReader streamReader2 = new StreamReader(f_strInMenuTextPathFileName, Encoding.UTF8);
		short num2 = 0;
		while ((text3 = streamReader2.ReadLine()) != null)
		{
			num2 += 1;
			if (!(text3 == string.Empty))
			{
				if (text3.IndexOf("/") != 0)
				{
					string[] stringList2 = UTY.GetStringList(text3);
					byte b = (byte)stringList2.Length;
					if (b != 0)
					{
						binaryWriter.Write(b);
						for (int l = 0; l < (int)b; l++)
						{
							string text9 = stringList2[l];
							if (l == 0)
							{
								text9 = text9.ToLower();
							}
							if (text9.Contains("\\"))
							{
								NDebug.MessageBox("MOD Compile", string.Concat(new string[]
								{
									"致命的エラー)￥ではなくスラッシュを使用してください [",
									text9,
									"]        @",
									f_strInMenuTextPathFileName,
									"  LineNo=",
									num2.ToString()
								}));
							}
							binaryWriter.Write(text9);
						}
					}
				}
			}
		}
		binaryWriter.Write(0);
		streamReader2.Close();
		streamReader2.Dispose();
		MemoryStream memoryStream2 = new MemoryStream();
		BinaryWriter binaryWriter2 = new BinaryWriter(memoryStream2);
		binaryWriter2.Write("CM3D2_MENU");
		binaryWriter2.Write(ModCompile.FILE_FORMAT_VERSION);
		binaryWriter2.Write(f_strInMenuTextPathFileName.ToLower());
		binaryWriter2.Write(value);
		binaryWriter2.Write(text2);
		binaryWriter2.Write(value2);
		binaryWriter2.Write((int)memoryStream.Length);
		binaryWriter2.Write(memoryStream.ToArray());
		File.WriteAllBytes(path, memoryStream2.ToArray());
		memoryStream2.Close();
		memoryStream2.Dispose();
		return true;
	}

	public static void ConvMqoToModel(string f_strMqoFile)
	{
		ModCompile.<ConvMqoToModel>c__AnonStorey8 <ConvMqoToModel>c__AnonStorey = new ModCompile.<ConvMqoToModel>c__AnonStorey8();
		string text = Path.GetDirectoryName(f_strMqoFile) + "\\_compiled";
		if (!Directory.Exists(text))
		{
			Directory.CreateDirectory(text);
		}
		string path = text + "\\" + Path.GetFileNameWithoutExtension(f_strMqoFile) + ".model";
		StreamReader streamReader = new StreamReader(f_strMqoFile, Encoding.UTF8, false);
		short num = 0;
		SortedDictionary<string, int> sortedDictionary = new SortedDictionary<string, int>();
		int num2 = 0;
		float num3 = 1f;
		string text2 = string.Empty;
		List<string> list = new List<string>();
		<ConvMqoToModel>c__AnonStorey.listExObj = new List<ModCompile.ExObjIn>();
		ModCompile.ExObjIn exObjIn = null;
		string text3;
		while ((text3 = streamReader.ReadLine()) != null)
		{
			num += 1;
			if (!(text3 == string.Empty))
			{
				if (text3.IndexOf("/") != 0)
				{
					string[] stringList = UTY.GetStringList(text3);
					if (stringList.Length != 0)
					{
						if (stringList[0] == "IncludeXml")
						{
							if (Path.GetExtension(stringList[1]) == ".mqx")
							{
								text2 = stringList[1];
							}
						}
						else if (stringList[0] == "Object")
						{
							exObjIn = new ModCompile.ExObjIn(stringList[1]);
							<ConvMqoToModel>c__AnonStorey.listExObj.Add(exObjIn);
						}
						else if (stringList[0] == "uid")
						{
							if (exObjIn.nUid == 0)
							{
								exObjIn.nUid = int.Parse(stringList[1]);
							}
						}
						else if (stringList[0] == "depth")
						{
							exObjIn.nDepth = int.Parse(stringList[1]);
						}
						else if (stringList[0] == "rotation")
						{
							float x = float.Parse(stringList[1]);
							float y = float.Parse(stringList[2]);
							float z = float.Parse(stringList[3]);
							exObjIn.qRotLocal = Quaternion.Euler(x, y, z);
						}
						else if (stringList[0] == "translation")
						{
							float x2 = float.Parse(stringList[1]);
							float y2 = float.Parse(stringList[2]);
							float z2 = float.Parse(stringList[3]);
							exObjIn.vPosLocal = new Vector3(x2, y2, z2);
						}
						else if (stringList[0] == "vertex")
						{
							int num4 = int.Parse(stringList[1]);
							if (num4 != 0)
							{
								Vector3[] array = new Vector3[num4];
								for (int i2 = 0; i2 < num4; i2++)
								{
									string[] stringList2 = UTY.GetStringList(streamReader.ReadLine());
									float num5 = float.Parse(stringList2[0]) * num3;
									float z3 = float.Parse(stringList2[1]) * num3;
									float num6 = float.Parse(stringList2[2]) * num3;
									array[i2] = new Vector3(-num5, -num6, z3);
								}
								exObjIn.vVtxs = array;
							}
						}
						else if (stringList[0] == "face")
						{
							int num7 = int.Parse(stringList[1]);
							if (num7 != 0)
							{
								int[] array2 = new int[num7 * 3];
								int[] array3 = new int[num7];
								exObjIn.nFaceCount = num7;
								for (int j = 0; j < num7; j++)
								{
									string text4 = streamReader.ReadLine();
									text4 = text4.Replace('(', ' ');
									text4 = text4.Replace(')', ' ');
									string[] stringList3 = UTY.GetStringList(text4);
									if (stringList3[0] != "3")
									{
										NDebug.MessageBox("MOD Compile", "err)三角形以外のポリゴンがまざっている可能性があります");
										return;
									}
									if (stringList3.Length != 16)
									{
										NDebug.MessageBox("MOD Compile", "err) " + text4);
									}
									else
									{
										if (stringList3[1] != "V")
										{
											NDebug.MessageBox("MOD Compile", "err) 'V' このフォーマットには対応していません");
											return;
										}
										if (stringList3[5] != "M")
										{
											NDebug.MessageBox("MOD Compile", "err) 'M' このフォーマットには対応していません");
											return;
										}
										int num8 = int.Parse(stringList3[6]);
										array3[j] = num8;
										if (num2 < num8 + 1)
										{
											num2 = num8 + 1;
										}
										ModCompile.ExFace exFace = new ModCompile.ExFace();
										for (int k = 0; k < 3; k++)
										{
											exFace.idx[k] = int.Parse(stringList3[2 + k]);
										}
										for (int l = 0; l < 3; l++)
										{
											Vector2 vector = new Vector2(float.Parse(stringList3[10 + l * 2]), 1f - float.Parse(stringList3[10 + l * 2 + 1]));
											vector.y -= 1f;
											exFace.uv[l] = vector;
										}
										if (!exObjIn.dicFaces.ContainsKey(num8))
										{
											exObjIn.dicFaces.Add(num8, new List<ModCompile.ExFace>());
										}
										exObjIn.dicFaces[num8].Add(exFace);
									}
								}
							}
						}
						else if (stringList[0] == "Material")
						{
							int num9 = int.Parse(stringList[1]);
							if (num9 != 0)
							{
								for (int m = 0; m < num9; m++)
								{
									string[] stringList4 = UTY.GetStringList(streamReader.ReadLine());
									list.Add(stringList4[0]);
								}
							}
						}
					}
				}
			}
		}
		if (string.IsNullOrEmpty(text2))
		{
			NDebug.MessageBox("MOD Compile", "MQOにIncludeXmlがありません。");
			return;
		}
		streamReader.Close();
		streamReader.Dispose();
		ModCompile.ExObjIn exObjIn2 = <ConvMqoToModel>c__AnonStorey.listExObj.Find((ModCompile.ExObjIn o) => o.vVtxs != null && o.strName.Contains("[main]"));
		if (exObjIn2 == null)
		{
			NDebug.MessageBox("MOD Compile", "MQOのメインオブジェクトが見つかりません。[main]と名前の付いたオブジェクトが必要です。");
			return;
		}
		exObjIn2.strName = exObjIn2.strName.Substring(6);
		List<ModCompile.ExObjIn> list2 = <ConvMqoToModel>c__AnonStorey.listExObj.FindAll((ModCompile.ExObjIn o) => o.vVtxs != null && o.strName.Contains("[morph]"));
		list2.ForEach(delegate(ModCompile.ExObjIn a)
		{
			a.strName = a.strName.Substring(7);
		});
		ModCompile.ExObjIn exObjIn3 = <ConvMqoToModel>c__AnonStorey.listExObj[0];
		<ConvMqoToModel>c__AnonStorey.listExObj.RemoveAt(0);
		for (int n = 0; n < <ConvMqoToModel>c__AnonStorey.listExObj.Count; n++)
		{
			int nMyDepth = <ConvMqoToModel>c__AnonStorey.listExObj[n].nDepth;
			int num10 = <ConvMqoToModel>c__AnonStorey.listExObj.FindLastIndex(n, (ModCompile.ExObjIn a) => a.nDepth == nMyDepth - 1);
			if (num10 != -1)
			{
				<ConvMqoToModel>c__AnonStorey.listExObj[n].oParent = <ConvMqoToModel>c__AnonStorey.listExObj[num10];
			}
		}
		List<ModCompile.ExObjIn> list3 = <ConvMqoToModel>c__AnonStorey.listExObj.FindAll((ModCompile.ExObjIn a) => Array.FindAll<string>(ModCompile.scl_plus, (string b) => a.strName.Contains(b)).Length != 0);
		foreach (ModCompile.ExObjIn exObjIn4 in list3)
		{
			exObjIn4.nSclCount++;
		}
		exObjIn2.vSharedUVs = new Vector2[exObjIn2.vVtxs.Length];
		foreach (KeyValuePair<int, List<ModCompile.ExFace>> keyValuePair in exObjIn2.dicFaces)
		{
			foreach (ModCompile.ExFace exFace2 in keyValuePair.Value)
			{
				for (int num11 = 0; num11 < 3; num11++)
				{
					exObjIn2.vSharedUVs[exFace2.idx[num11]] = exFace2.uv[num11];
				}
			}
		}
		List<ModCompile.ExBone> list4 = new List<ModCompile.ExBone>();
		string path2 = Path.GetDirectoryName(f_strMqoFile) + "\\" + text2;
		XmlDocument xmlDocument = new XmlDocument();
		xmlDocument.Load(new StreamReader(path2, Encoding.UTF8, false));
		XmlNode xmlNode = xmlDocument.SelectSingleNode("/MetasequoiaDocument/Plugin.56A31D20.71F282AB/BoneSet");
		XmlNodeList childNodes = xmlNode.ChildNodes;
		for (int num12 = 0; num12 < childNodes.Count; num12++)
		{
			XmlNode xmlNode2 = childNodes[num12];
			ModCompile.ExBone exBone = new ModCompile.ExBone(int.Parse(xmlNode2.Attributes["id"].Value), xmlNode2.Attributes["name"].Value);
			exBone.vPosLocalS.x = float.Parse(xmlNode2.Attributes["rtX"].Value);
			exBone.vPosLocalS.y = float.Parse(xmlNode2.Attributes["rtY"].Value);
			exBone.vPosLocalS.z = float.Parse(xmlNode2.Attributes["rtZ"].Value);
			exBone.bDummy = (xmlNode2.Attributes["isDummy"].Value != "0");
			IEnumerator enumerator4 = xmlNode2.ChildNodes.GetEnumerator();
			try
			{
				while (enumerator4.MoveNext())
				{
					object obj = enumerator4.Current;
					XmlNode xmlNode3 = (XmlNode)obj;
					if (xmlNode3.Name == "P")
					{
						exBone.nParentId = int.Parse(xmlNode3.Attributes["id"].Value);
					}
					else if (xmlNode3.Name == "C")
					{
						exBone.listChildId.Add(int.Parse(xmlNode3.Attributes["id"].Value));
					}
					else if (xmlNode3.Name == "W")
					{
						ModCompile.ExWeight exWeight = new ModCompile.ExWeight();
						exWeight.nObjIdx = int.Parse(xmlNode3.Attributes["oi"].Value);
						exWeight.nVtxIdx = int.Parse(xmlNode3.Attributes["vi"].Value);
						exWeight.fWeight = float.Parse(xmlNode3.Attributes["w"].Value);
						exBone.listWeights.Add(exWeight);
					}
				}
			}
			finally
			{
				IDisposable disposable;
				if ((disposable = (enumerator4 as IDisposable)) != null)
				{
					disposable.Dispose();
				}
			}
			list4.Add(exBone);
		}
		using (List<ModCompile.ExBone>.Enumerator enumerator5 = list4.GetEnumerator())
		{
			while (enumerator5.MoveNext())
			{
				ModCompile.<ConvMqoToModel>c__AnonStorey4 <ConvMqoToModel>c__AnonStorey3 = new ModCompile.<ConvMqoToModel>c__AnonStorey4();
				<ConvMqoToModel>c__AnonStorey3.ebDummy = enumerator5.Current;
				if (<ConvMqoToModel>c__AnonStorey3.ebDummy.strName == "dummy")
				{
					ModCompile.<ConvMqoToModel>c__AnonStorey5 <ConvMqoToModel>c__AnonStorey4 = new ModCompile.<ConvMqoToModel>c__AnonStorey5();
					<ConvMqoToModel>c__AnonStorey4.<>f__ref$4 = <ConvMqoToModel>c__AnonStorey3;
					ModCompile.ExBone exBone2 = list4.Find((ModCompile.ExBone b) => b.nId == <ConvMqoToModel>c__AnonStorey4.<>f__ref$4.ebDummy.nParentId);
					List<int> listChildId = exBone2.listChildId;
					for (int num13 = 0; num13 < listChildId.Count; num13++)
					{
						if (listChildId[num13] == <ConvMqoToModel>c__AnonStorey3.ebDummy.nId)
						{
							listChildId[num13] = <ConvMqoToModel>c__AnonStorey3.ebDummy.listChildId[0];
						}
					}
					<ConvMqoToModel>c__AnonStorey4.listChildOfDummy = <ConvMqoToModel>c__AnonStorey3.ebDummy.listChildId;
					int nC;
					for (nC = 0; nC < <ConvMqoToModel>c__AnonStorey4.listChildOfDummy.Count; nC++)
					{
						List<ModCompile.ExBone> list5 = list4.FindAll((ModCompile.ExBone b) => b.nId == <ConvMqoToModel>c__AnonStorey4.listChildOfDummy[nC]);
						foreach (ModCompile.ExBone exBone3 in list5)
						{
							exBone3.nParentId = <ConvMqoToModel>c__AnonStorey3.ebDummy.nParentId;
						}
					}
				}
			}
		}
		string path3 = Path.GetDirectoryName(f_strMqoFile) + "\\" + Path.GetFileNameWithoutExtension(f_strMqoFile) + ".binpo.txt";
		StreamReader streamReader2 = new StreamReader(path3, Encoding.UTF8, false);
		if (streamReader2 == null)
		{
			NDebug.MessageBox("MOD Compile", "BindPoses[.binpo.txt]がありません。");
			return;
		}
		while ((text3 = streamReader2.ReadLine()) != null)
		{
			if (!(text3 == string.Empty))
			{
				if (text3.IndexOf("/") != 0)
				{
					string[] t = UTY.GetStringList(text3);
					if (t.Length != 0)
					{
						ModCompile.ExBone exBone4 = list4.Find((ModCompile.ExBone b) => b.strName == t[0]);
						if (exBone4 == null)
						{
							NDebug.MessageBox("MOD Compile", "BindPosesの中にボーン" + t[0] + "がみつかりません。");
							return;
						}
						exBone4.bUseBindPose = true;
						for (int num14 = 0; num14 < 16; num14++)
						{
							exBone4.matBindPose[num14] = float.Parse(t[1 + num14]);
						}
					}
				}
			}
		}
		streamReader2.Close();
		streamReader2.Dispose();
		list4.RemoveAll((ModCompile.ExBone b) => (b.bDummy || b.listWeights.Count == 0) && !b.bUseBindPose);
		exObjIn2.bwSharedBonwWeight = new BoneWeight[exObjIn2.vVtxs.Length];
		for (int num15 = 0; num15 < list4.Count; num15++)
		{
			ModCompile.ExBone exBone5 = list4[num15];
			for (int num16 = 0; num16 < exBone5.listWeights.Count; num16++)
			{
				ModCompile.ExWeight exWeight2 = exBone5.listWeights[num16];
				if (exWeight2.nObjIdx == exObjIn2.nUid)
				{
					try
					{
						BoneWeight boneWeight = exObjIn2.bwSharedBonwWeight[exWeight2.nVtxIdx - 1];
						if (boneWeight.weight0 < 0.001f)
						{
							boneWeight.boneIndex0 = num15;
							boneWeight.weight0 = exWeight2.fWeight / 100f;
						}
						else if (boneWeight.weight1 < 0.001f)
						{
							boneWeight.boneIndex1 = num15;
							boneWeight.weight1 = exWeight2.fWeight / 100f;
						}
						else if (boneWeight.weight2 < 0.001f)
						{
							boneWeight.boneIndex2 = num15;
							boneWeight.weight2 = exWeight2.fWeight / 100f;
						}
						else if (boneWeight.weight3 < 0.001f)
						{
							boneWeight.boneIndex3 = num15;
							boneWeight.weight3 = exWeight2.fWeight / 100f;
						}
						else
						{
							NDebug.MessageBox("MOD Compile", "頂点に対するボーンの数が4つを超えました。" + exBone5.strName);
						}
						exObjIn2.bwSharedBonwWeight[exWeight2.nVtxIdx - 1] = boneWeight;
					}
					catch (Exception ex)
					{
						NDebug.MessageBox("MOD Compile", ex.Message);
					}
				}
			}
		}
		Hashtable hashtable = new Hashtable();
		MemoryStream memoryStream = new MemoryStream();
		BinaryWriter binaryWriter = new BinaryWriter(memoryStream);
		binaryWriter.Write("CM3D2_MESH");
		binaryWriter.Write(1170);
		string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(f_strMqoFile);
		binaryWriter.Write(fileNameWithoutExtension);
		binaryWriter.Write(exObjIn2.strName);
		binaryWriter.Write(<ConvMqoToModel>c__AnonStorey.listExObj.Count);
		for (int num17 = 0; num17 < <ConvMqoToModel>c__AnonStorey.listExObj.Count; num17++)
		{
			ModCompile.ExObjIn exObjIn5 = <ConvMqoToModel>c__AnonStorey.listExObj[num17];
			if (hashtable.ContainsKey(exObjIn5.strName))
			{
				binaryWriter.Close();
				NDebug.MessageBox("MOD Compile", "Compile ERROR!重複ネームあり " + exObjIn5.strName + "\n" + f_strMqoFile);
				return;
			}
			hashtable[exObjIn5.strName] = num17;
			binaryWriter.Write(exObjIn5.strName);
			binaryWriter.Write((byte)exObjIn5.nSclCount);
		}
		int i;
		for (i = 0; i < <ConvMqoToModel>c__AnonStorey.listExObj.Count; i++)
		{
			int value = <ConvMqoToModel>c__AnonStorey.listExObj.FindIndex((ModCompile.ExObjIn o) => o == <ConvMqoToModel>c__AnonStorey.listExObj[i].oParent);
			binaryWriter.Write(value);
		}
		for (int num18 = 0; num18 < <ConvMqoToModel>c__AnonStorey.listExObj.Count; num18++)
		{
			ModCompile.ExObjIn exObjIn6 = <ConvMqoToModel>c__AnonStorey.listExObj[num18];
			binaryWriter.Write(exObjIn6.vPosLocal.x);
			binaryWriter.Write(exObjIn6.vPosLocal.y);
			binaryWriter.Write(exObjIn6.vPosLocal.z);
			binaryWriter.Write(exObjIn6.qRotLocal.x);
			binaryWriter.Write(exObjIn6.qRotLocal.y);
			binaryWriter.Write(exObjIn6.qRotLocal.z);
			binaryWriter.Write(exObjIn6.qRotLocal.w);
		}
		int num19 = exObjIn2.vVtxs.Length;
		binaryWriter.Write(num19);
		binaryWriter.Write(num2);
		int count = list4.Count;
		binaryWriter.Write(count);
		for (int num20 = 0; num20 < count; num20++)
		{
			binaryWriter.Write(list4[num20].strName);
		}
		for (int num21 = 0; num21 < count; num21++)
		{
			Matrix4x4 matBindPose = list4[num21].matBindPose;
			for (int num22 = 0; num22 < 16; num22++)
			{
				binaryWriter.Write(matBindPose[num22]);
			}
		}
		Mesh mesh = new Mesh();
		mesh.vertices = exObjIn2.vVtxs;
		mesh.subMeshCount = num2;
		List<int> list6 = new List<int>();
		foreach (KeyValuePair<int, List<ModCompile.ExFace>> keyValuePair2 in exObjIn2.dicFaces)
		{
			List<int> list7 = new List<int>();
			for (int num23 = 0; num23 < keyValuePair2.Value.Count; num23++)
			{
				for (int num24 = 0; num24 < 3; num24++)
				{
					list7.Add(keyValuePair2.Value[num23].idx[num24]);
				}
			}
			mesh.SetTriangles(list7.ToArray(), keyValuePair2.Key);
		}
		mesh.RecalculateNormals();
		Vector3[] vVtxs = exObjIn2.vVtxs;
		Vector3[] normals = mesh.normals;
		Vector2[] vSharedUVs = exObjIn2.vSharedUVs;
		BoneWeight[] bwSharedBonwWeight = exObjIn2.bwSharedBonwWeight;
		for (int num25 = 0; num25 < num19; num25++)
		{
			binaryWriter.Write(vVtxs[num25].x);
			binaryWriter.Write(vVtxs[num25].y);
			binaryWriter.Write(vVtxs[num25].z);
			binaryWriter.Write(normals[num25].x);
			binaryWriter.Write(normals[num25].y);
			binaryWriter.Write(normals[num25].z);
			binaryWriter.Write(vSharedUVs[num25].x);
			binaryWriter.Write(vSharedUVs[num25].y);
		}
		binaryWriter.Write(0);
		for (int num26 = 0; num26 < num19; num26++)
		{
			binaryWriter.Write((ushort)bwSharedBonwWeight[num26].boneIndex0);
			binaryWriter.Write((ushort)bwSharedBonwWeight[num26].boneIndex1);
			binaryWriter.Write((ushort)bwSharedBonwWeight[num26].boneIndex2);
			binaryWriter.Write((ushort)bwSharedBonwWeight[num26].boneIndex3);
			binaryWriter.Write(bwSharedBonwWeight[num26].weight0);
			binaryWriter.Write(bwSharedBonwWeight[num26].weight1);
			binaryWriter.Write(bwSharedBonwWeight[num26].weight2);
			binaryWriter.Write(bwSharedBonwWeight[num26].weight3);
		}
		for (int num27 = 0; num27 < num2; num27++)
		{
			List<ModCompile.ExFace> list8 = exObjIn2.dicFaces[num27];
			binaryWriter.Write(list8.Count * 3);
			for (int num28 = 0; num28 < list8.Count; num28++)
			{
				for (int num29 = 0; num29 < 3; num29++)
				{
					binaryWriter.Write((ushort)list8[num28].idx[num29]);
				}
			}
		}
		int count2 = exObjIn2.dicFaces.Count;
		binaryWriter.Write(count2);
		if (count2 != list.Count)
		{
			NDebug.MessageBox("MOD Compile", "マテリアル数が不正です。");
			return;
		}
		for (int num30 = 0; num30 < count2; num30++)
		{
			string text5 = Path.GetDirectoryName(f_strMqoFile) + "\\" + list[num30] + ".mate.txt";
			if (!File.Exists(text5))
			{
				NDebug.MessageBox("MOD Compile", "マテリアルが見つかりません。\n" + text5);
				return;
			}
			try
			{
				ModCompile.WriteMaterial(text5, binaryWriter);
			}
			catch (Exception ex2)
			{
				NDebug.MessageBox("MOD Compile", "マテリアルが不正です。" + text5 + "\n" + ex2.Message);
				return;
			}
		}
		foreach (ModCompile.ExObjIn exObjIn7 in list2)
		{
			Mesh mesh2 = new Mesh();
			mesh2.vertices = exObjIn7.vVtxs;
			mesh2.triangles = mesh.triangles;
			mesh2.RecalculateNormals();
			Vector3[] vVtxs2 = exObjIn7.vVtxs;
			Vector3[] normals2 = mesh2.normals;
			if (exObjIn7.vVtxs.Length != exObjIn2.vVtxs.Length)
			{
				NDebug.MessageBox("MOD Compile", "モーフターゲットと頂点数が異なります。" + exObjIn7.strName);
				return;
			}
			bool[] array4 = new bool[num19];
			binaryWriter.Write("morph");
			binaryWriter.Write(exObjIn7.strName.ToLower());
			int num31 = 0;
			for (int num32 = 0; num32 < num19; num32++)
			{
				float magnitude = (vVtxs2[num32] - vVtxs[num32]).magnitude;
				float magnitude2 = (normals2[num32] - normals[num32]).magnitude;
				if (magnitude > 0.0001f || magnitude2 > 0.01f)
				{
					num31++;
					array4[num32] = true;
				}
				else
				{
					array4[num32] = false;
				}
			}
			binaryWriter.Write(num31);
			for (int num33 = 0; num33 < num19; num33++)
			{
				if (array4[num33])
				{
					binaryWriter.Write((ushort)num33);
					binaryWriter.Write(vVtxs2[num33].x - vVtxs[num33].x);
					binaryWriter.Write(vVtxs2[num33].y - vVtxs[num33].y);
					binaryWriter.Write(vVtxs2[num33].z - vVtxs[num33].z);
					binaryWriter.Write(normals2[num33].x - normals[num33].x);
					binaryWriter.Write(normals2[num33].y - normals[num33].y);
					binaryWriter.Write(normals2[num33].z - normals[num33].z);
				}
			}
		}
		binaryWriter.Write("end");
		binaryWriter.Close();
		File.WriteAllBytes(path, memoryStream.ToArray());
		memoryStream.Close();
		memoryStream.Dispose();
	}

	private static void WriteMaterial(string f_strInMateTextFile, BinaryWriter w)
	{
		string value = string.Empty;
		string text = string.Empty;
		string text2 = string.Empty;
		List<ModCompile.MateTex2d> list = new List<ModCompile.MateTex2d>();
		List<ModCompile.MateCol> list2 = new List<ModCompile.MateCol>();
		List<ModCompile.MateVec4> list3 = new List<ModCompile.MateVec4>();
		List<ModCompile.MateFloat> list4 = new List<ModCompile.MateFloat>();
		StreamReader streamReader = new StreamReader(f_strInMateTextFile, Encoding.UTF8, false);
		string text3;
		while ((text3 = streamReader.ReadLine()) != null)
		{
			if (!(text3 == string.Empty))
			{
				if (text3.IndexOf("/") != 0)
				{
					string[] stringList = UTY.GetStringList(text3);
					if (stringList.Length != 0)
					{
						if (stringList[0] == "MaterialName")
						{
							value = stringList[1];
						}
						else if (stringList[0] == "ShaderName")
						{
							text = stringList[1];
						}
						else if (stringList[0] == "Shader")
						{
							text2 = stringList[1];
						}
						else if (stringList[0] == "tex2d")
						{
							list.Add(new ModCompile.MateTex2d(stringList[1], stringList[2], new Vector2(float.Parse(stringList[3]), float.Parse(stringList[4])), new Vector2(float.Parse(stringList[5]), float.Parse(stringList[6]))));
						}
						else if (stringList[0] == "col")
						{
							list2.Add(new ModCompile.MateCol(stringList[1], new Color(float.Parse(stringList[2]), float.Parse(stringList[3]), float.Parse(stringList[4]))));
						}
						else if (stringList[0] == "vec")
						{
							list3.Add(new ModCompile.MateVec4(stringList[1], new Vector4(float.Parse(stringList[2]), float.Parse(stringList[3]), float.Parse(stringList[4]), float.Parse(stringList[4]))));
						}
						else if (stringList[0] == "float")
						{
							list4.Add(new ModCompile.MateFloat(stringList[1], float.Parse(stringList[2])));
						}
					}
				}
			}
		}
		streamReader.Close();
		streamReader.Dispose();
		w.Write(value);
		w.Write(text);
		string text4 = string.Empty;
		int length = text.Length;
		for (int i = 0; i < length; i++)
		{
			if (text[i] != '/')
			{
				text4 += text[i].ToString();
			}
			else
			{
				text4 += "__";
			}
		}
		w.Write(text4);
		foreach (ModCompile.MateTex2d mateTex2d in list)
		{
			w.Write("tex");
			w.Write(mateTex2d.strPropName);
			w.Write("tex2d");
			w.Write(mateTex2d.strTexName);
			w.Write(mateTex2d.strTexName);
			w.Write(mateTex2d.vOffset.x);
			w.Write(mateTex2d.vOffset.y);
			w.Write(mateTex2d.vScale.x);
			w.Write(mateTex2d.vScale.y);
		}
		foreach (ModCompile.MateCol mateCol in list2)
		{
			w.Write("col");
			w.Write(mateCol.strPropName);
			Color col = mateCol.col;
			w.Write(col.r);
			w.Write(col.g);
			w.Write(col.b);
			w.Write(col.a);
		}
		foreach (ModCompile.MateVec4 mateVec in list3)
		{
			w.Write("vec");
			w.Write(mateVec.strPropName);
			Vector4 vVec = mateVec.vVec4;
			w.Write(vVec.x);
			w.Write(vVec.y);
			w.Write(vVec.z);
			w.Write(vVec.w);
		}
		foreach (ModCompile.MateFloat mateFloat in list4)
		{
			w.Write("f");
			w.Write(mateFloat.strPropName);
			w.Write(mateFloat.fValue);
		}
		w.Write("end");
	}

	public static void ExportTexture(string f_strInTexPathFileName)
	{
		string str = Path.GetDirectoryName(f_strInTexPathFileName) + "\\_compiled";
		string path = str + "\\" + Path.GetFileNameWithoutExtension(f_strInTexPathFileName) + ".tex";
		MemoryStream memoryStream = new MemoryStream();
		BinaryWriter binaryWriter = new BinaryWriter(memoryStream);
		byte[] array = File.ReadAllBytes(f_strInTexPathFileName);
		binaryWriter.Write("CM3D2_TEX");
		binaryWriter.Write(ModCompile.FILE_FORMAT_VERSION);
		binaryWriter.Write(f_strInTexPathFileName);
		binaryWriter.Write(array.Length);
		binaryWriter.Write(array);
		binaryWriter.Close();
		File.WriteAllBytes(path, memoryStream.ToArray());
		memoryStream.Close();
		memoryStream.Dispose();
	}

	public static bool ExportMaterial(string f_strInMatePathFileName)
	{
		string text = Path.GetDirectoryName(f_strInMatePathFileName) + "\\_compiled";
		if (!Directory.Exists(text))
		{
			Directory.CreateDirectory(text);
		}
		string path = text + "\\" + Path.GetFileNameWithoutExtension(f_strInMatePathFileName);
		MemoryStream memoryStream = new MemoryStream();
		BinaryWriter binaryWriter = new BinaryWriter(memoryStream);
		binaryWriter.Write("CM3D2_MATERIAL");
		binaryWriter.Write(ModCompile.FILE_FORMAT_VERSION);
		string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(f_strInMatePathFileName);
		binaryWriter.Write(fileNameWithoutExtension);
		try
		{
			ModCompile.WriteMaterial(f_strInMatePathFileName, binaryWriter);
		}
		catch (Exception ex)
		{
			NDebug.MessageBox("MOD Compile", "マテリアルが不正です。" + Path.GetFileName(f_strInMatePathFileName) + "\n" + ex.Message);
			return false;
		}
		File.WriteAllBytes(path, memoryStream.ToArray());
		memoryStream.Close();
		memoryStream.Dispose();
		return true;
	}

	private static string[] m_aryDefCategoryName = new string[99];

	private static string[] m_aryDefSlotName;

	private static int FILE_FORMAT_VERSION = 1000;

	private static string[] scl_plus = new string[]
	{
		"Pelvis",
		"Spine",
		"Thigh",
		"Calf",
		"Clavicle",
		"Neck"
	};

	private class ExWeight
	{
		public int nObjIdx;

		public int nVtxIdx;

		public float fWeight;
	}

	private class ExBone
	{
		public ExBone(int id, string name)
		{
			this.nId = id;
			this.strName = name;
		}

		public int nId;

		public string strName;

		public int nParentId;

		public List<int> listChildId = new List<int>();

		public ModCompile.ExBone bnParent;

		public ModCompile.ExBone[] aryChild;

		public Vector3 vPosLocalS;

		public Vector3 vPosLocalE;

		public Vector3 vPosWorldS;

		public bool bDummy;

		public Quaternion qRotLocal;

		public Vector3 vScale;

		public Matrix4x4 matBindPose = default(Matrix4x4);

		public bool bUseBindPose;

		public List<ModCompile.ExWeight> listWeights = new List<ModCompile.ExWeight>();
	}

	private class ExFace
	{
		public int[] idx = new int[3];

		public Vector2[] uv = new Vector2[3];
	}

	private class ExObjIn
	{
		public ExObjIn(string name)
		{
			this.strName = name;
		}

		public string strName;

		public ModCompile.ExObjIn oParent;

		public List<ModCompile.ExObjIn> listChild = new List<ModCompile.ExObjIn>();

		public Vector3 vPosLocal;

		public Quaternion qRotLocal;

		public Vector3[] vVtxs;

		public int nFaceCount;

		public int nUid;

		public int nDepth;

		public Dictionary<int, List<ModCompile.ExFace>> dicFaces = new Dictionary<int, List<ModCompile.ExFace>>();

		public int nSclCount;

		public Vector3[] vSharedNormals;

		public Vector2[] vSharedUVs;

		public BoneWeight[] bwSharedBonwWeight;
	}

	private class MateTex2d
	{
		public MateTex2d(string f_strPropName, string f_strTexName, Vector2 f_vOffset, Vector2 f_vScale)
		{
			this.strPropName = f_strPropName;
			this.strTexName = f_strTexName;
			this.vOffset = f_vOffset;
			this.vScale = f_vScale;
		}

		public string strPropName;

		public string strTexName;

		public Vector2 vOffset;

		public Vector2 vScale = new Vector2(1f, 1f);
	}

	private class MateCol
	{
		public MateCol(string f_strPropName, Color f_col)
		{
			this.strPropName = f_strPropName;
			this.col = f_col;
		}

		public string strPropName;

		public Color col;
	}

	private class MateVec4
	{
		public MateVec4(string f_strPropName, Vector4 f_v)
		{
			this.strPropName = f_strPropName;
			this.vVec4 = f_v;
		}

		public string strPropName;

		public Vector4 vVec4;
	}

	private class MateFloat
	{
		public MateFloat(string f_strPropName, float f_value)
		{
			this.strPropName = f_strPropName;
			this.fValue = f_value;
		}

		public string strPropName;

		public float fValue;
	}
}
