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

public class EditMod : MonoBehaviour
{
	private void Awake()
	{
	}

	private void OnGUI()
	{
		if (this.m_eModMode == EditMod.MOD_MODE.MOD_TEX)
		{
			GUI.TextField(new Rect(10f, 10f, 100f, 20f), this.m_mpn.ToString(), 255);
			if (GUI.Button(new Rect(120f, 10f, 100f, 20f), "ExportBase"))
			{
				this.ModExportBase();
			}
			this.strModPath = GUI.TextField(new Rect(240f, 10f, 400f, 20f), this.strModPath, 255);
			if (GUI.Button(new Rect(650f, 10f, 100f, 20f), "Compile"))
			{
				this.ModCompileDir(this.strModPath);
			}
		}
		else if (this.m_eModMode == EditMod.MOD_MODE.MOD_MQO)
		{
			this.strModPath = GUI.TextField(new Rect(390f, 10f, 400f, 20f), this.strModPath, 255);
			if (GUI.Button(new Rect(800f, 10f, 100f, 20f), "Compile"))
			{
				this.ModCompileDirMqo(this.strModPath);
			}
		}
	}

	private void ModExportBase()
	{
		Maid maid = GameMain.Instance.CharacterMgr.GetMaid(0);
		maid.ExportModBaseMenu(this.m_mpn);
	}

	private void ModExportMqo(string f_strOutFileName)
	{
		Maid maid = GameMain.Instance.CharacterMgr.GetMaid(0);
		maid.ExportModBaseMqo(this.m_mpn, f_strOutFileName);
		NDebug.MessageBox("MOD Export Complete", "MODの元ファイルを出力しました。");
	}

	private void ModCompileDir(string f_strModPath)
	{
		if (!Directory.Exists(f_strModPath))
		{
			NUty.WinMessageBox(NUty.GetWindowHandle(), "コンパイル対象のパスが見つかりません。", "MOD Compile Error", 0);
			return;
		}
		string[] files = Directory.GetFiles(f_strModPath, "mod_*.txt", SearchOption.TopDirectoryOnly);
		foreach (string f_strModMenu in files)
		{
			this.ModCompileMenu(f_strModMenu);
		}
	}

	private void ModCompileDirMqo(string f_strModPath)
	{
		if (!Directory.Exists(f_strModPath))
		{
			NUty.WinMessageBox(NUty.GetWindowHandle(), "コンパイル対象のパスが見つかりません。", "MQO Compile Error", 0);
			return;
		}
		string[] files = Directory.GetFiles(f_strModPath, "*.*", SearchOption.TopDirectoryOnly);
		foreach (string text in files)
		{
			if (text.Contains(".menu.txt"))
			{
				ModCompile.CompileMenuScript(text);
			}
			else if (text.Contains(".mqo"))
			{
				ModCompile.ConvMqoToModel(text);
			}
			else if (text.Contains(".png"))
			{
				ModCompile.ExportTexture(text);
			}
			else if (text.Contains(".mate.txt"))
			{
				ModCompile.ExportMaterial(text);
			}
		}
		NDebug.MessageBox("MOD Compile Complete", "MODファイルのコンパイルが終了しました。");
	}

	private bool ModCompileMenu(string f_strModMenu)
	{
		byte[] array = null;
		try
		{
			using (FileStream fileStream = new FileStream(f_strModMenu, FileMode.Open))
			{
				if (fileStream == null)
				{
					NUty.WinMessageBox(NUty.GetWindowHandle(), "コンパイル対象のメニューがありません。\n" + f_strModMenu, "MOD Compile Error", 0);
					return false;
				}
				array = new byte[fileStream.Length];
				fileStream.Read(array, 0, (int)fileStream.Length);
			}
		}
		catch (Exception ex)
		{
			NUty.WinMessageBox(NUty.GetWindowHandle(), "ModCompileMenu MODアイテムメニューファイルが読み込めませんでした。\n " + f_strModMenu + "\n" + ex.Message, "MOD Compile Error", 0);
			return false;
		}
		string text = string.Empty;
		string value = string.Empty;
		string text2 = string.Empty;
		string value2 = string.Empty;
		string value3 = string.Empty;
		string value4 = string.Empty;
		string value5 = string.Empty;
		string text3 = string.Empty;
		Dictionary<string, EditMod.TexSet> dictionary = new Dictionary<string, EditMod.TexSet>();
		string @string = Encoding.UTF8.GetString(array);
		using (StringReader stringReader = new StringReader(@string))
		{
			string text4;
			while ((text4 = stringReader.ReadLine()) != null)
			{
				string[] array2 = text4.Split(new char[]
				{
					'\t',
					' '
				}, StringSplitOptions.RemoveEmptyEntries);
				if (array2.Length != 0)
				{
					if (array2[0].IndexOf("/") != 0)
					{
						if (array2[0] == "基本アイテム")
						{
							text2 = array2[1];
						}
						else if (array2[0] == "アイテム名")
						{
							value2 = array2[1];
						}
						else if (array2[0] == "カテゴリ名")
						{
							value4 = array2[1];
						}
						else if (array2[0] == "説明")
						{
							value3 = array2[1];
						}
						else if (array2[0] == "アイコン")
						{
							array2[1] = array2[1].ToLower();
							value = array2[1];
							string text5 = Path.GetDirectoryName(f_strModMenu) + "\\";
							text5 += array2[1];
							FileStream fileStream2 = new FileStream(text5, FileMode.Open);
							if (fileStream2 == null)
							{
								NUty.WinMessageBox(NUty.GetWindowHandle(), "アイコンファイルが開けません。\n" + text5, "MOD Compile Error", 0);
								return false;
							}
							byte[] array3 = new byte[fileStream2.Length];
							fileStream2.Read(array3, 0, (int)fileStream2.Length);
							if (!dictionary.ContainsKey(array2[1].ToLower()))
							{
								dictionary.Add(array2[1].ToLower(), new EditMod.TexSet
								{
									nSize = (int)fileStream2.Length,
									byData = array3
								});
							}
							fileStream2.Close();
							fileStream2.Dispose();
						}
						else if (array2[0] == "色セット")
						{
							value5 = array2[1];
							if (array2.Length >= 3)
							{
								text3 = array2[2];
							}
						}
						else if (array2[0] == "テクスチャ設定")
						{
							array2[2] = array2[2].ToLower();
							string text6 = Path.GetDirectoryName(f_strModMenu) + "\\";
							text6 += array2[2];
							FileStream fileStream3 = new FileStream(text6, FileMode.Open);
							if (fileStream3 == null)
							{
								NUty.WinMessageBox(NUty.GetWindowHandle(), "テクスチャが開けません。\n" + text6, "MOD Compile Error", 0);
								return false;
							}
							byte[] array4 = new byte[fileStream3.Length];
							fileStream3.Read(array4, 0, (int)fileStream3.Length);
							if (!dictionary.ContainsKey(array2[2].ToLower()))
							{
								dictionary.Add(array2[2].ToLower(), new EditMod.TexSet
								{
									nSize = (int)fileStream3.Length,
									byData = array4
								});
							}
							fileStream3.Close();
							fileStream3.Dispose();
						}
						else if (array2[0] == "テクスチャ変更")
						{
							string text7 = array2[4].ToLower();
							string text8 = Path.GetDirectoryName(f_strModMenu) + "\\";
							string[] array5;
							if (text7.Contains("*"))
							{
								array5 = Directory.GetFiles(text8, text7, SearchOption.AllDirectories);
							}
							else
							{
								array5 = new string[]
								{
									text8 + text7
								};
							}
							foreach (string text9 in array5)
							{
								if (!File.Exists(text9))
								{
									NUty.WinMessageBox(NUty.GetWindowHandle(), "テクスチャが見つかりません。\n" + text9, "MOD Compile Error", 0);
									return false;
								}
								FileStream fileStream4 = new FileStream(text9, FileMode.Open);
								if (fileStream4 == null)
								{
									NUty.WinMessageBox(NUty.GetWindowHandle(), "テクスチャが開けません。\n" + text9, "MOD Compile Error", 0);
									return false;
								}
								byte[] array7 = new byte[fileStream4.Length];
								fileStream4.Read(array7, 0, (int)fileStream4.Length);
								if (!dictionary.ContainsKey(Path.GetFileName(text9).ToLower()))
								{
									dictionary.Add(Path.GetFileName(text9).ToLower(), new EditMod.TexSet
									{
										nSize = (int)fileStream4.Length,
										byData = array7
									});
								}
								fileStream4.Close();
								fileStream4.Dispose();
							}
						}
						foreach (string str in array2)
						{
							text = text + str + "\t";
						}
						text += "\n";
					}
				}
			}
		}
		MemoryStream memoryStream = new MemoryStream();
		BinaryWriter binaryWriter = new BinaryWriter(memoryStream);
		binaryWriter.Write("CM3D2_MOD");
		binaryWriter.Write(1170);
		binaryWriter.Write(value);
		binaryWriter.Write(text2.ToLower());
		binaryWriter.Write(value2);
		binaryWriter.Write(value4);
		binaryWriter.Write(value3);
		if (string.IsNullOrEmpty(value5))
		{
			binaryWriter.Write(MPN.null_mpn.ToString());
		}
		else
		{
			binaryWriter.Write(value5);
			binaryWriter.Write(text3.ToLower());
		}
		binaryWriter.Write(text);
		binaryWriter.Write(dictionary.Count);
		foreach (KeyValuePair<string, EditMod.TexSet> keyValuePair in dictionary)
		{
			binaryWriter.Write(keyValuePair.Key);
			binaryWriter.Write(keyValuePair.Value.byData.Length);
			binaryWriter.Write(keyValuePair.Value.byData);
		}
		string text10 = Path.GetDirectoryName(f_strModMenu) + "\\_compiled\\";
		Directory.CreateDirectory(text10);
		text10 += Path.GetFileNameWithoutExtension(f_strModMenu);
		text10 += ".mod";
		File.WriteAllBytes(text10, memoryStream.ToArray());
		memoryStream.Close();
		memoryStream.Dispose();
		memoryStream = null;
		NUty.WinMessageBox(NUty.GetWindowHandle(), "MODコンパイル完了\n" + text10, "MOD Compile Completed", 0);
		return true;
	}

	private void Update()
	{
		if (!this.bPush && Input.GetKey(KeyCode.M) && Input.GetKey(KeyCode.O) && Input.GetKey(KeyCode.D) && !Input.GetKey(KeyCode.Q))
		{
			if (this.m_eModMode != EditMod.MOD_MODE.MOD_TEX)
			{
				this.m_eModMode = EditMod.MOD_MODE.MOD_TEX;
			}
			else
			{
				this.m_eModMode = EditMod.MOD_MODE.MOD_NON;
			}
			this.bPush = true;
		}
		else if (!this.bPush && Input.GetKey(KeyCode.M) && Input.GetKey(KeyCode.O) && Input.GetKey(KeyCode.D) && Input.GetKey(KeyCode.Q))
		{
			if (this.m_eModMode != EditMod.MOD_MODE.MOD_MQO)
			{
				this.m_eModMode = EditMod.MOD_MODE.MOD_MQO;
			}
			else
			{
				this.m_eModMode = EditMod.MOD_MODE.MOD_NON;
			}
			this.bPush = true;
		}
		else if (!Input.GetKey(KeyCode.M) && !Input.GetKey(KeyCode.O) && !Input.GetKey(KeyCode.D) && !Input.GetKey(KeyCode.Q))
		{
			this.bPush = false;
		}
	}

	private string strModPath = string.Empty;

	private EditMod.MOD_MODE m_eModMode;

	public MPN m_mpn;

	private string m_strOutFileName = string.Empty;

	private bool bPush;

	private enum MOD_MODE
	{
		MOD_NON,
		MOD_TEX,
		MOD_MQO
	}

	private class TexSet
	{
		public int nSize;

		public byte[] byData;
	}
}
