EditMod.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. using UnityEngine;
  6. public class EditMod : MonoBehaviour
  7. {
  8. private void Awake()
  9. {
  10. }
  11. private void OnGUI()
  12. {
  13. if (this.m_eModMode == EditMod.MOD_MODE.MOD_TEX)
  14. {
  15. GUI.TextField(new Rect(10f, 10f, 100f, 20f), this.m_mpn.ToString(), 255);
  16. if (GUI.Button(new Rect(120f, 10f, 100f, 20f), "ExportBase"))
  17. {
  18. this.ModExportBase();
  19. }
  20. this.strModPath = GUI.TextField(new Rect(240f, 10f, 400f, 20f), this.strModPath, 255);
  21. if (GUI.Button(new Rect(650f, 10f, 100f, 20f), "Compile"))
  22. {
  23. this.ModCompileDir(this.strModPath);
  24. }
  25. }
  26. else if (this.m_eModMode == EditMod.MOD_MODE.MOD_MQO)
  27. {
  28. this.strModPath = GUI.TextField(new Rect(390f, 10f, 400f, 20f), this.strModPath, 255);
  29. if (GUI.Button(new Rect(800f, 10f, 100f, 20f), "Compile"))
  30. {
  31. this.ModCompileDirMqo(this.strModPath);
  32. }
  33. }
  34. }
  35. private void ModExportBase()
  36. {
  37. Maid maid = GameMain.Instance.CharacterMgr.GetMaid(0);
  38. maid.ExportModBaseMenu(this.m_mpn);
  39. }
  40. private void ModExportMqo(string f_strOutFileName)
  41. {
  42. Maid maid = GameMain.Instance.CharacterMgr.GetMaid(0);
  43. maid.ExportModBaseMqo(this.m_mpn, f_strOutFileName);
  44. NDebug.MessageBox("MOD Export Complete", "MODの元ファイルを出力しました。");
  45. }
  46. private void ModCompileDir(string f_strModPath)
  47. {
  48. if (!Directory.Exists(f_strModPath))
  49. {
  50. NUty.WinMessageBox(NUty.GetWindowHandle(), "コンパイル対象のパスが見つかりません。", "MOD Compile Error", 0);
  51. return;
  52. }
  53. string[] files = Directory.GetFiles(f_strModPath, "mod_*.txt", SearchOption.TopDirectoryOnly);
  54. foreach (string f_strModMenu in files)
  55. {
  56. this.ModCompileMenu(f_strModMenu);
  57. }
  58. }
  59. private void ModCompileDirMqo(string f_strModPath)
  60. {
  61. if (!Directory.Exists(f_strModPath))
  62. {
  63. NUty.WinMessageBox(NUty.GetWindowHandle(), "コンパイル対象のパスが見つかりません。", "MQO Compile Error", 0);
  64. return;
  65. }
  66. string[] files = Directory.GetFiles(f_strModPath, "*.*", SearchOption.TopDirectoryOnly);
  67. foreach (string text in files)
  68. {
  69. if (text.Contains(".menu.txt"))
  70. {
  71. ModCompile.CompileMenuScript(text);
  72. }
  73. else if (text.Contains(".mqo"))
  74. {
  75. ModCompile.ConvMqoToModel(text);
  76. }
  77. else if (text.Contains(".png"))
  78. {
  79. ModCompile.ExportTexture(text);
  80. }
  81. else if (text.Contains(".mate.txt"))
  82. {
  83. ModCompile.ExportMaterial(text);
  84. }
  85. }
  86. NDebug.MessageBox("MOD Compile Complete", "MODファイルのコンパイルが終了しました。");
  87. }
  88. private bool ModCompileMenu(string f_strModMenu)
  89. {
  90. byte[] array = null;
  91. try
  92. {
  93. using (FileStream fileStream = new FileStream(f_strModMenu, FileMode.Open))
  94. {
  95. if (fileStream == null)
  96. {
  97. NUty.WinMessageBox(NUty.GetWindowHandle(), "コンパイル対象のメニューがありません。\n" + f_strModMenu, "MOD Compile Error", 0);
  98. return false;
  99. }
  100. array = new byte[fileStream.Length];
  101. fileStream.Read(array, 0, (int)fileStream.Length);
  102. }
  103. }
  104. catch (Exception ex)
  105. {
  106. NUty.WinMessageBox(NUty.GetWindowHandle(), "ModCompileMenu MODアイテムメニューファイルが読み込めませんでした。\n " + f_strModMenu + "\n" + ex.Message, "MOD Compile Error", 0);
  107. return false;
  108. }
  109. string text = string.Empty;
  110. string value = string.Empty;
  111. string text2 = string.Empty;
  112. string value2 = string.Empty;
  113. string value3 = string.Empty;
  114. string value4 = string.Empty;
  115. string value5 = string.Empty;
  116. string text3 = string.Empty;
  117. Dictionary<string, EditMod.TexSet> dictionary = new Dictionary<string, EditMod.TexSet>();
  118. string @string = Encoding.UTF8.GetString(array);
  119. using (StringReader stringReader = new StringReader(@string))
  120. {
  121. string text4;
  122. while ((text4 = stringReader.ReadLine()) != null)
  123. {
  124. string[] array2 = text4.Split(new char[]
  125. {
  126. '\t',
  127. ' '
  128. }, StringSplitOptions.RemoveEmptyEntries);
  129. if (array2.Length != 0)
  130. {
  131. if (array2[0].IndexOf("/") != 0)
  132. {
  133. if (array2[0] == "基本アイテム")
  134. {
  135. text2 = array2[1];
  136. }
  137. else if (array2[0] == "アイテム名")
  138. {
  139. value2 = array2[1];
  140. }
  141. else if (array2[0] == "カテゴリ名")
  142. {
  143. value4 = array2[1];
  144. }
  145. else if (array2[0] == "説明")
  146. {
  147. value3 = array2[1];
  148. }
  149. else if (array2[0] == "アイコン")
  150. {
  151. array2[1] = array2[1].ToLower();
  152. value = array2[1];
  153. string text5 = Path.GetDirectoryName(f_strModMenu) + "\\";
  154. text5 += array2[1];
  155. try
  156. {
  157. FileStream fileStream2 = new FileStream(text5, FileMode.Open);
  158. if (fileStream2 == null)
  159. {
  160. NUty.WinMessageBox(NUty.GetWindowHandle(), "アイコンファイルが開けません。\n" + text5, "MOD Compile Error", 0);
  161. return false;
  162. }
  163. byte[] array3 = new byte[fileStream2.Length];
  164. fileStream2.Read(array3, 0, (int)fileStream2.Length);
  165. if (!dictionary.ContainsKey(array2[1].ToLower()))
  166. {
  167. dictionary.Add(array2[1].ToLower(), new EditMod.TexSet
  168. {
  169. nSize = (int)fileStream2.Length,
  170. byData = array3
  171. });
  172. }
  173. fileStream2.Close();
  174. fileStream2.Dispose();
  175. }
  176. catch (Exception ex2)
  177. {
  178. NDebug.MessageBox("指定のアイコンファイルが見つかりませんでした。\n" + array2[1], "MOD Compile Error");
  179. return false;
  180. }
  181. }
  182. else if (array2[0] == "色セット")
  183. {
  184. value5 = array2[1];
  185. if (array2.Length >= 3)
  186. {
  187. text3 = array2[2];
  188. }
  189. }
  190. else if (array2[0] == "テクスチャ設定")
  191. {
  192. array2[2] = array2[2].ToLower();
  193. string text6 = Path.GetDirectoryName(f_strModMenu) + "\\";
  194. text6 += array2[2];
  195. try
  196. {
  197. FileStream fileStream3 = new FileStream(text6, FileMode.Open);
  198. if (fileStream3 == null)
  199. {
  200. NUty.WinMessageBox(NUty.GetWindowHandle(), "テクスチャが開けません。\n" + text6, "MOD Compile Error", 0);
  201. return false;
  202. }
  203. byte[] array4 = new byte[fileStream3.Length];
  204. fileStream3.Read(array4, 0, (int)fileStream3.Length);
  205. if (!dictionary.ContainsKey(array2[2].ToLower()))
  206. {
  207. dictionary.Add(array2[2].ToLower(), new EditMod.TexSet
  208. {
  209. nSize = (int)fileStream3.Length,
  210. byData = array4
  211. });
  212. }
  213. fileStream3.Close();
  214. fileStream3.Dispose();
  215. }
  216. catch (Exception ex3)
  217. {
  218. array2[2] = Path.ChangeExtension(array2[2], ".tex");
  219. string text7 = array2[2].ToLower();
  220. if (!dictionary.ContainsKey(text7))
  221. {
  222. dictionary.Add(text7, new EditMod.TexSet
  223. {
  224. nSize = EditMod.dummyImg.Length,
  225. byData = EditMod.dummyImg
  226. });
  227. Debug.Log("画像ファイルが付近に見つからなかったので、内部のファイルシステムから呼び出すことにします。" + text7);
  228. }
  229. }
  230. }
  231. else if (array2[0] == "テクスチャ変更")
  232. {
  233. string text8 = array2[4].ToLower();
  234. string text9 = Path.GetDirectoryName(f_strModMenu) + "\\";
  235. string[] array5;
  236. if (text8.Contains("*"))
  237. {
  238. array5 = Directory.GetFiles(text9, text8, SearchOption.AllDirectories);
  239. }
  240. else
  241. {
  242. array5 = new string[]
  243. {
  244. text9 + text8
  245. };
  246. }
  247. foreach (string text10 in array5)
  248. {
  249. try
  250. {
  251. FileStream fileStream4 = new FileStream(text10, FileMode.Open);
  252. if (fileStream4 == null)
  253. {
  254. NUty.WinMessageBox(NUty.GetWindowHandle(), "テクスチャが開けません。\n" + text10, "MOD Compile Error", 0);
  255. return false;
  256. }
  257. byte[] array7 = new byte[fileStream4.Length];
  258. fileStream4.Read(array7, 0, (int)fileStream4.Length);
  259. if (!dictionary.ContainsKey(Path.GetFileName(text10).ToLower()))
  260. {
  261. dictionary.Add(Path.GetFileName(text10).ToLower(), new EditMod.TexSet
  262. {
  263. nSize = (int)fileStream4.Length,
  264. byData = array7
  265. });
  266. }
  267. fileStream4.Close();
  268. fileStream4.Dispose();
  269. }
  270. catch (Exception ex4)
  271. {
  272. string path = Path.GetFileName(text10);
  273. path = Path.ChangeExtension(path, ".tex");
  274. string text11 = text8.ToLower();
  275. if (!dictionary.ContainsKey(text11))
  276. {
  277. dictionary.Add(text11, new EditMod.TexSet
  278. {
  279. nSize = EditMod.dummyImg.Length,
  280. byData = EditMod.dummyImg
  281. });
  282. Debug.Log("画像ファイルが付近に見つからなかったので、内部のファイルシステムから呼び出すことにします。" + text11);
  283. }
  284. }
  285. }
  286. }
  287. foreach (string str in array2)
  288. {
  289. text = text + str + "\t";
  290. }
  291. text += "\n";
  292. }
  293. }
  294. }
  295. }
  296. MemoryStream memoryStream = new MemoryStream();
  297. BinaryWriter binaryWriter = new BinaryWriter(memoryStream);
  298. binaryWriter.Write("CM3D2_MOD");
  299. binaryWriter.Write(1290);
  300. binaryWriter.Write(value);
  301. binaryWriter.Write(text2.ToLower());
  302. binaryWriter.Write(value2);
  303. binaryWriter.Write(value4);
  304. binaryWriter.Write(value3);
  305. if (string.IsNullOrEmpty(value5))
  306. {
  307. binaryWriter.Write(MPN.null_mpn.ToString());
  308. }
  309. else
  310. {
  311. binaryWriter.Write(value5);
  312. binaryWriter.Write(text3.ToLower());
  313. }
  314. binaryWriter.Write(text);
  315. binaryWriter.Write(dictionary.Count);
  316. foreach (KeyValuePair<string, EditMod.TexSet> keyValuePair in dictionary)
  317. {
  318. binaryWriter.Write(keyValuePair.Key);
  319. binaryWriter.Write(keyValuePair.Value.byData.Length);
  320. binaryWriter.Write(keyValuePair.Value.byData);
  321. }
  322. string text12 = Path.GetDirectoryName(f_strModMenu) + "\\_compiled\\";
  323. Directory.CreateDirectory(text12);
  324. text12 += Path.GetFileNameWithoutExtension(f_strModMenu);
  325. text12 += ".mod";
  326. File.WriteAllBytes(text12, memoryStream.ToArray());
  327. memoryStream.Close();
  328. memoryStream.Dispose();
  329. memoryStream = null;
  330. NUty.WinMessageBox(NUty.GetWindowHandle(), "MODコンパイル完了\n" + text12, "MOD Compile Completed", 0);
  331. return true;
  332. }
  333. private void Update()
  334. {
  335. if (!this.bPush && Input.GetKey(KeyCode.M) && Input.GetKey(KeyCode.O) && Input.GetKey(KeyCode.D) && !Input.GetKey(KeyCode.Q))
  336. {
  337. if (this.m_eModMode != EditMod.MOD_MODE.MOD_TEX)
  338. {
  339. this.m_eModMode = EditMod.MOD_MODE.MOD_TEX;
  340. }
  341. else
  342. {
  343. this.m_eModMode = EditMod.MOD_MODE.MOD_NON;
  344. }
  345. this.bPush = true;
  346. }
  347. else if (!this.bPush && Input.GetKey(KeyCode.M) && Input.GetKey(KeyCode.O) && Input.GetKey(KeyCode.D) && Input.GetKey(KeyCode.Q))
  348. {
  349. if (this.m_eModMode != EditMod.MOD_MODE.MOD_MQO)
  350. {
  351. this.m_eModMode = EditMod.MOD_MODE.MOD_MQO;
  352. }
  353. else
  354. {
  355. this.m_eModMode = EditMod.MOD_MODE.MOD_NON;
  356. }
  357. this.bPush = true;
  358. }
  359. else if (!Input.GetKey(KeyCode.M) && !Input.GetKey(KeyCode.O) && !Input.GetKey(KeyCode.D) && !Input.GetKey(KeyCode.Q))
  360. {
  361. this.bPush = false;
  362. }
  363. }
  364. private string strModPath = string.Empty;
  365. private EditMod.MOD_MODE m_eModMode;
  366. public MPN m_mpn;
  367. private string m_strOutFileName = string.Empty;
  368. public static readonly byte[] dummyImg = new byte[]
  369. {
  370. 137,
  371. 80,
  372. 78,
  373. 71,
  374. 13,
  375. 10,
  376. 26,
  377. 10,
  378. 0,
  379. 0,
  380. 0,
  381. 13,
  382. 73,
  383. 72,
  384. 68,
  385. 82,
  386. 0,
  387. 0,
  388. 0,
  389. 1,
  390. 0,
  391. 0,
  392. 0,
  393. 1,
  394. 8,
  395. 2,
  396. 0,
  397. 0,
  398. 0,
  399. 144,
  400. 119,
  401. 83,
  402. 222,
  403. 0,
  404. 0,
  405. 0,
  406. 12,
  407. 73,
  408. 68,
  409. 65,
  410. 84,
  411. 8,
  412. 153,
  413. 99,
  414. 248,
  415. byte.MaxValue,
  416. byte.MaxValue,
  417. 63,
  418. 0,
  419. 5,
  420. 254,
  421. 2,
  422. 254,
  423. 88,
  424. 242,
  425. 107,
  426. 14,
  427. 0,
  428. 0,
  429. 0,
  430. 0,
  431. 73,
  432. 69,
  433. 78,
  434. 68,
  435. 174,
  436. 66,
  437. 96,
  438. 130,
  439. 110,
  440. 101,
  441. 105
  442. };
  443. private bool bPush;
  444. private enum MOD_MODE
  445. {
  446. MOD_NON,
  447. MOD_TEX,
  448. MOD_MQO
  449. }
  450. private class TexSet
  451. {
  452. public int nSize;
  453. public byte[] byData;
  454. }
  455. }