MenuFileUtility.cs 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  1. using System;
  2. using System.Text;
  3. using System.IO;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using UnityEngine;
  8. using UnityEngine.Rendering;
  9. namespace COM3D2.MeidoPhotoStudio.Plugin
  10. {
  11. internal static class MenuFileUtility
  12. {
  13. public const string noCategory = "noCategory";
  14. public static string[] MenuCategories = new[] {
  15. noCategory, "acchat", "headset", "wear", "skirt", "onepiece", "mizugi", "bra", "panz", "stkg", "shoes",
  16. "acckami", "megane", "acchead", "acchana", "accmimi", "glove", "acckubi", "acckubiwa", "acckamisub",
  17. "accnip", "accude", "accheso", "accashi", "accsenaka", "accshippo", "accxxx"
  18. };
  19. private static readonly HashSet<string> accMpn = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase);
  20. private enum IMode
  21. {
  22. None, ItemChange, TexChange
  23. }
  24. public static event EventHandler MenuFilesReadyChange;
  25. public static bool MenuFilesReady { get; private set; } = false;
  26. static MenuFileUtility()
  27. {
  28. accMpn.UnionWith(MenuCategories.Skip(1));
  29. GameMain.Instance.StartCoroutine(CheckMenuDataBaseJob());
  30. }
  31. private static IEnumerator CheckMenuDataBaseJob()
  32. {
  33. if (MenuFilesReady) yield break;
  34. while (!GameMain.Instance.MenuDataBase.JobFinished()) yield return null;
  35. MenuFilesReady = true;
  36. MenuFilesReadyChange?.Invoke(null, EventArgs.Empty);
  37. yield break;
  38. }
  39. private static bool ProcScriptBin(byte[] menuBuf, ModelInfo modelInfo)
  40. {
  41. using (BinaryReader binaryReader = new BinaryReader(new MemoryStream(menuBuf), Encoding.UTF8))
  42. {
  43. string menuHeader = binaryReader.ReadString();
  44. NDebug.Assert(
  45. menuHeader == "CM3D2_MENU", "ProcScriptBin 例外 : ヘッダーファイルが不正です。" + menuHeader
  46. );
  47. binaryReader.ReadInt32(); // file version
  48. binaryReader.ReadString(); // txt path
  49. binaryReader.ReadString(); // name
  50. binaryReader.ReadString(); // category
  51. binaryReader.ReadString(); // description
  52. binaryReader.ReadInt32(); // idk (as long)
  53. string menuPropString = String.Empty;
  54. string menuPropStringTemp = String.Empty;
  55. // string tempString3 = String.Empty;
  56. // string slotName = String.Empty;
  57. try
  58. {
  59. while (true)
  60. {
  61. int numberOfProps = (int)binaryReader.ReadByte();
  62. menuPropStringTemp = menuPropString;
  63. menuPropString = String.Empty;
  64. if (numberOfProps != 0)
  65. {
  66. for (int i = 0; i < numberOfProps; i++)
  67. {
  68. menuPropString = $"{menuPropString}\"{binaryReader.ReadString()}\"";
  69. }
  70. if (menuPropString != string.Empty)
  71. {
  72. string header = UTY.GetStringCom(menuPropString);
  73. string[] menuProps = UTY.GetStringList(menuPropString);
  74. if (header == "end")
  75. {
  76. break;
  77. }
  78. else if (header == "マテリアル変更")
  79. {
  80. int matNo = int.Parse(menuProps[2]);
  81. string materialFile = menuProps[3];
  82. modelInfo.MaterialChanges.Add(new MaterialChange(matNo, materialFile));
  83. }
  84. else if (header == "additem")
  85. {
  86. modelInfo.ModelFile = menuProps[1];
  87. }
  88. }
  89. }
  90. else
  91. {
  92. break;
  93. }
  94. }
  95. }
  96. catch
  97. {
  98. return false;
  99. }
  100. }
  101. return true;
  102. }
  103. private static void ProcModScriptBin(byte[] cd, GameObject go)
  104. {
  105. BinaryReader binaryReader = new BinaryReader(new MemoryStream(cd), Encoding.UTF8);
  106. string str1 = binaryReader.ReadString();
  107. NDebug.Assert(str1 == "CM3D2_MOD", "ProcModScriptBin 例外 : ヘッダーファイルが不正です。" + str1);
  108. binaryReader.ReadInt32();
  109. binaryReader.ReadString();
  110. binaryReader.ReadString();
  111. binaryReader.ReadString();
  112. binaryReader.ReadString();
  113. binaryReader.ReadString();
  114. string mpnValue = binaryReader.ReadString();
  115. MPN mpn = MPN.null_mpn;
  116. try
  117. {
  118. mpn = (MPN)Enum.Parse(typeof(MPN), mpnValue);
  119. }
  120. catch { }
  121. if (mpn != MPN.null_mpn)
  122. {
  123. binaryReader.ReadString();
  124. }
  125. string s = binaryReader.ReadString();
  126. int num2 = binaryReader.ReadInt32();
  127. Dictionary<string, byte[]> dictionary = new Dictionary<string, byte[]>();
  128. for (int i = 0; i < num2; i++)
  129. {
  130. string key = binaryReader.ReadString();
  131. int count = binaryReader.ReadInt32();
  132. byte[] value = binaryReader.ReadBytes(count);
  133. dictionary.Add(key, value);
  134. }
  135. binaryReader.Close();
  136. using (StringReader stringReader = new StringReader(s))
  137. {
  138. IMode mode = IMode.None;
  139. string slotname = String.Empty;
  140. Material material = null;
  141. int num3 = 0;
  142. string line;
  143. bool change = false;
  144. while ((line = stringReader.ReadLine()) != null)
  145. {
  146. string[] array = line.Split(new[] { '\t', ' ' }, StringSplitOptions.RemoveEmptyEntries);
  147. if (array[0] == "アイテム変更" || array[0] == "マテリアル変更")
  148. {
  149. mode = IMode.ItemChange;
  150. }
  151. else if (array[0] == "テクスチャ変更")
  152. {
  153. mode = IMode.TexChange;
  154. }
  155. if (mode == IMode.ItemChange)
  156. {
  157. if (array[0] == "スロット名")
  158. {
  159. slotname = array[1];
  160. change = true;
  161. }
  162. if (change)
  163. {
  164. if (array[0] == "マテリアル番号")
  165. {
  166. num3 = int.Parse(array[1]);
  167. foreach (Transform transform in go.GetComponentsInChildren<Transform>(true))
  168. {
  169. Renderer component = transform.GetComponent<Renderer>();
  170. if (component != null && component.materials != null)
  171. {
  172. Material[] materials = component.materials;
  173. for (int k = 0; k < materials.Length; k++)
  174. {
  175. if (k == num3)
  176. {
  177. material = materials[k];
  178. break;
  179. }
  180. }
  181. }
  182. }
  183. }
  184. if (material != null)
  185. {
  186. if (array[0] == "テクスチャ設定")
  187. {
  188. ChangeTex(num3, array[1], array[2].ToLower(), dictionary, go);
  189. }
  190. else if (array[0] == "色設定")
  191. {
  192. material.SetColor(array[1],
  193. new Color(
  194. float.Parse(array[2]) / 255f,
  195. float.Parse(array[3]) / 255f,
  196. float.Parse(array[4]) / 255f,
  197. float.Parse(array[5]) / 255f
  198. )
  199. );
  200. }
  201. else if (array[0] == "数値設定")
  202. {
  203. material.SetFloat(array[1], float.Parse(array[2]));
  204. }
  205. }
  206. }
  207. }
  208. else if (mode == IMode.TexChange)
  209. {
  210. int matno = int.Parse(array[2]);
  211. ChangeTex(matno, array[3], array[4].ToLower(), dictionary, go);
  212. }
  213. }
  214. }
  215. }
  216. private static void ChangeTex(
  217. int matno, string prop, string filename, Dictionary<string, byte[]> matDict, GameObject go
  218. )
  219. {
  220. TextureResource textureResource = null;
  221. byte[] buf = matDict[filename.ToLowerInvariant()];
  222. textureResource = new TextureResource(2, 2, TextureFormat.ARGB32, null, buf);
  223. List<Renderer> list = new List<Renderer>(3);
  224. go.transform.GetComponentsInChildren<Renderer>(true, list);
  225. foreach (Renderer r in list)
  226. {
  227. if (r != null && r.material != null)
  228. {
  229. if (matno < r.materials.Length)
  230. {
  231. r.materials[matno].SetTexture(prop, null);
  232. Texture2D texture2D = textureResource.CreateTexture2D();
  233. texture2D.name = filename;
  234. r.materials[matno].SetTexture(prop, texture2D);
  235. }
  236. }
  237. }
  238. }
  239. private static GameObject LoadSkinMesh_R(string modelFileName, int layer)
  240. {
  241. byte[] buffer = null;
  242. using (AFileBase afileBase = GameUty.FileOpen(modelFileName, null))
  243. {
  244. if (afileBase.IsValid() && afileBase.GetSize() != 0)
  245. {
  246. buffer = afileBase.ReadAll();
  247. }
  248. else
  249. {
  250. Utility.LogError("invalid model");
  251. return null;
  252. }
  253. }
  254. using (BinaryReader binaryReader = new BinaryReader(new MemoryStream(buffer), Encoding.UTF8))
  255. {
  256. GameObject gameObject = UnityEngine.Object.Instantiate(Resources.Load("seed")) as GameObject;
  257. gameObject.layer = 1;
  258. GameObject gameObject2 = null;
  259. Hashtable hashtable = new Hashtable();
  260. string text = binaryReader.ReadString();
  261. if (text != "CM3D2_MESH")
  262. {
  263. NDebug.Assert("LoadSkinMesh_R 例外 : ヘッダーファイルが不正です。" + text, false);
  264. }
  265. int num = binaryReader.ReadInt32();
  266. string str = binaryReader.ReadString();
  267. gameObject.name = "_SM_" + str;
  268. string b = binaryReader.ReadString();
  269. int num2 = binaryReader.ReadInt32();
  270. List<GameObject> list = new List<GameObject>();
  271. for (int i = 0; i < num2; i++)
  272. {
  273. GameObject gameObject3 = UnityEngine.Object.Instantiate(Resources.Load("seed")) as GameObject;
  274. gameObject3.layer = layer;
  275. gameObject3.name = binaryReader.ReadString();
  276. list.Add(gameObject3);
  277. if (gameObject3.name == b)
  278. {
  279. gameObject2 = gameObject3;
  280. }
  281. hashtable[gameObject3.name] = gameObject3;
  282. bool flag = binaryReader.ReadByte() != 0;
  283. if (flag)
  284. {
  285. GameObject gameObject4 = UnityEngine.Object.Instantiate(Resources.Load("seed")) as GameObject;
  286. gameObject4.name = gameObject3.name + "_SCL_";
  287. gameObject4.transform.parent = gameObject3.transform;
  288. hashtable[gameObject3.name + "&_SCL_"] = gameObject4;
  289. }
  290. }
  291. for (int j = 0; j < num2; j++)
  292. {
  293. int num3 = binaryReader.ReadInt32();
  294. if (num3 >= 0)
  295. {
  296. list[j].transform.parent = list[num3].transform;
  297. }
  298. else
  299. {
  300. list[j].transform.parent = gameObject.transform;
  301. }
  302. }
  303. for (int k = 0; k < num2; k++)
  304. {
  305. Transform transform = list[k].transform;
  306. float x = binaryReader.ReadSingle();
  307. float y = binaryReader.ReadSingle();
  308. float z = binaryReader.ReadSingle();
  309. transform.localPosition = new Vector3(x, y, z);
  310. float x2 = binaryReader.ReadSingle();
  311. float y2 = binaryReader.ReadSingle();
  312. float z2 = binaryReader.ReadSingle();
  313. float w = binaryReader.ReadSingle();
  314. transform.localRotation = new Quaternion(x2, y2, z2, w);
  315. if (2001 <= num)
  316. {
  317. bool flag2 = binaryReader.ReadBoolean();
  318. if (flag2)
  319. {
  320. float x3 = binaryReader.ReadSingle();
  321. float y3 = binaryReader.ReadSingle();
  322. float z3 = binaryReader.ReadSingle();
  323. transform.localScale = new Vector3(x3, y3, z3);
  324. }
  325. }
  326. }
  327. int num4 = binaryReader.ReadInt32();
  328. int num5 = binaryReader.ReadInt32();
  329. int num6 = binaryReader.ReadInt32();
  330. SkinnedMeshRenderer skinnedMeshRenderer = gameObject2.AddComponent<SkinnedMeshRenderer>();
  331. skinnedMeshRenderer.updateWhenOffscreen = true;
  332. skinnedMeshRenderer.skinnedMotionVectors = false;
  333. skinnedMeshRenderer.lightProbeUsage = LightProbeUsage.Off;
  334. skinnedMeshRenderer.reflectionProbeUsage = ReflectionProbeUsage.Off;
  335. skinnedMeshRenderer.motionVectorGenerationMode = MotionVectorGenerationMode.ForceNoMotion;
  336. Transform[] array2 = new Transform[num6];
  337. for (int l = 0; l < num6; l++)
  338. {
  339. string text2 = binaryReader.ReadString();
  340. if (!hashtable.ContainsKey(text2))
  341. {
  342. Utility.LogError("nullbone= " + text2);
  343. }
  344. else
  345. {
  346. GameObject gameObject5;
  347. if (hashtable.ContainsKey(text2 + "&_SCL_"))
  348. {
  349. gameObject5 = (GameObject)hashtable[text2 + "&_SCL_"];
  350. }
  351. else
  352. {
  353. gameObject5 = (GameObject)hashtable[text2];
  354. }
  355. array2[l] = gameObject5.transform;
  356. }
  357. }
  358. skinnedMeshRenderer.bones = array2;
  359. Mesh mesh = new Mesh();
  360. skinnedMeshRenderer.sharedMesh = mesh;
  361. Mesh mesh2 = mesh;
  362. // bodyskin.listDEL.Add(mesh2);
  363. Matrix4x4[] array4 = new Matrix4x4[num6];
  364. for (int m = 0; m < num6; m++)
  365. {
  366. for (int n = 0; n < 16; n++)
  367. {
  368. array4[m][n] = binaryReader.ReadSingle();
  369. }
  370. }
  371. mesh2.bindposes = array4;
  372. Vector3[] array5 = new Vector3[num4];
  373. Vector3[] array6 = new Vector3[num4];
  374. Vector2[] array7 = new Vector2[num4];
  375. BoneWeight[] array8 = new BoneWeight[num4];
  376. for (int num8 = 0; num8 < num4; num8++)
  377. {
  378. float num9 = binaryReader.ReadSingle();
  379. float num10 = binaryReader.ReadSingle();
  380. float new_z = binaryReader.ReadSingle();
  381. array5[num8].Set(num9, num10, new_z);
  382. num9 = binaryReader.ReadSingle();
  383. num10 = binaryReader.ReadSingle();
  384. new_z = binaryReader.ReadSingle();
  385. array6[num8].Set(num9, num10, new_z);
  386. num9 = binaryReader.ReadSingle();
  387. num10 = binaryReader.ReadSingle();
  388. array7[num8].Set(num9, num10);
  389. }
  390. mesh2.vertices = array5;
  391. mesh2.normals = array6;
  392. mesh2.uv = array7;
  393. int num11 = binaryReader.ReadInt32();
  394. if (num11 > 0)
  395. {
  396. Vector4[] array9 = new Vector4[num11];
  397. for (int num12 = 0; num12 < num11; num12++)
  398. {
  399. float x4 = binaryReader.ReadSingle();
  400. float y4 = binaryReader.ReadSingle();
  401. float z4 = binaryReader.ReadSingle();
  402. float w2 = binaryReader.ReadSingle();
  403. array9[num12] = new Vector4(x4, y4, z4, w2);
  404. }
  405. mesh2.tangents = array9;
  406. }
  407. for (int num13 = 0; num13 < num4; num13++)
  408. {
  409. array8[num13].boneIndex0 = binaryReader.ReadUInt16();
  410. array8[num13].boneIndex1 = binaryReader.ReadUInt16();
  411. array8[num13].boneIndex2 = binaryReader.ReadUInt16();
  412. array8[num13].boneIndex3 = binaryReader.ReadUInt16();
  413. array8[num13].weight0 = binaryReader.ReadSingle();
  414. array8[num13].weight1 = binaryReader.ReadSingle();
  415. array8[num13].weight2 = binaryReader.ReadSingle();
  416. array8[num13].weight3 = binaryReader.ReadSingle();
  417. }
  418. mesh2.boneWeights = array8;
  419. mesh2.subMeshCount = num5;
  420. for (int num19 = 0; num19 < num5; num19++)
  421. {
  422. int num20 = binaryReader.ReadInt32();
  423. int[] array10 = new int[num20];
  424. for (int num21 = 0; num21 < num20; num21++)
  425. {
  426. array10[num21] = (int)binaryReader.ReadUInt16();
  427. }
  428. mesh2.SetTriangles(array10, num19);
  429. }
  430. int num22 = binaryReader.ReadInt32();
  431. Material[] array11 = new Material[num22];
  432. for (int num23 = 0; num23 < num22; num23++)
  433. {
  434. Material material = ImportCM.ReadMaterial(binaryReader);
  435. array11[num23] = material;
  436. }
  437. skinnedMeshRenderer.materials = array11;
  438. return gameObject;
  439. }
  440. }
  441. public static GameObject LoadModel(string menuFile)
  442. {
  443. return LoadModel(new ModItem { MenuFile = menuFile });
  444. }
  445. public static GameObject LoadModel(ModItem modItem)
  446. {
  447. byte[] menuBuffer = null;
  448. byte[] modMenuBuffer = null;
  449. if (modItem.IsOfficialMod)
  450. {
  451. using (FileStream fileStream = new FileStream(modItem.MenuFile, FileMode.Open))
  452. {
  453. if (fileStream == null || fileStream.Length == 0)
  454. {
  455. Utility.LogWarning($"Could not open mod menu file '{modItem.MenuFile}'");
  456. return null;
  457. }
  458. else
  459. {
  460. modMenuBuffer = new byte[fileStream.Length];
  461. fileStream.Read(modMenuBuffer, 0, (int)fileStream.Length);
  462. }
  463. }
  464. }
  465. string menu = modItem.IsOfficialMod ? modItem.BaseMenuFile : modItem.MenuFile;
  466. using (AFileBase afileBase = GameUty.FileOpen(menu))
  467. {
  468. if (afileBase == null || !afileBase.IsValid())
  469. {
  470. Utility.LogWarning($"Could not open menu file '{menu}'");
  471. return null;
  472. }
  473. else if (afileBase.GetSize() == 0)
  474. {
  475. Utility.LogWarning($"Mod menu is empty '{menu}'");
  476. return null;
  477. }
  478. else
  479. {
  480. menuBuffer = afileBase.ReadAll();
  481. }
  482. }
  483. ModelInfo modelInfo = new ModelInfo();
  484. if (ProcScriptBin(menuBuffer, modelInfo))
  485. {
  486. GameObject gameObject = null;
  487. try
  488. {
  489. gameObject = LoadSkinMesh_R(modelInfo.ModelFile, 1);
  490. }
  491. catch
  492. {
  493. Utility.LogWarning($"Could not load mesh for '{modItem.MenuFile}'");
  494. }
  495. if (gameObject != null)
  496. {
  497. foreach (MaterialChange matChange in modelInfo.MaterialChanges)
  498. {
  499. foreach (Transform transform in gameObject.transform.GetComponentsInChildren<Transform>(true))
  500. {
  501. Renderer renderer = transform.GetComponent<Renderer>();
  502. if (renderer != null && renderer.material != null
  503. && matChange.MaterialIndex < renderer.materials.Length
  504. )
  505. {
  506. renderer.materials[matChange.MaterialIndex] = ImportCM.LoadMaterial(
  507. matChange.MaterialFile, null, renderer.materials[matChange.MaterialIndex]
  508. );
  509. }
  510. }
  511. }
  512. if (modItem.IsOfficialMod)
  513. {
  514. ProcModScriptBin(modMenuBuffer, gameObject);
  515. }
  516. }
  517. return gameObject;
  518. }
  519. else
  520. {
  521. Utility.LogMessage($"Could not parse menu file '{modItem.MenuFile}'");
  522. return null;
  523. }
  524. }
  525. public static bool ParseNativeMenuFile(int menuIndex, ModItem modItem)
  526. {
  527. MenuDataBase menuDataBase = GameMain.Instance.MenuDataBase;
  528. menuDataBase.SetIndex(menuIndex);
  529. if (menuDataBase.GetBoDelOnly()) return false;
  530. modItem.Category = menuDataBase.GetCategoryMpnText();
  531. if (!accMpn.Contains(modItem.Category)) return false;
  532. modItem.MenuFile = menuDataBase.GetMenuFileName().ToLower();
  533. if (!ValidBG2MenuFile(modItem.MenuFile)) return false;
  534. modItem.Name = menuDataBase.GetMenuName();
  535. modItem.IconFile = menuDataBase.GetIconS();
  536. modItem.Priority = menuDataBase.GetPriority();
  537. return true;
  538. }
  539. public static bool ParseMenuFile(string menuFile, ModItem modItem)
  540. {
  541. if (!ValidBG2MenuFile(menuFile)) return false;
  542. byte[] buf = null;
  543. try
  544. {
  545. using (AFileBase afileBase = GameUty.FileOpen(menuFile))
  546. {
  547. if (afileBase == null || !afileBase.IsValid() || afileBase.GetSize() == 0) return false;
  548. buf = afileBase.ReadAll();
  549. }
  550. }
  551. catch
  552. {
  553. Utility.LogError($"Could not read menu file '{menuFile}'");
  554. return false;
  555. }
  556. using (BinaryReader binaryReader = new BinaryReader(new MemoryStream(buf), Encoding.UTF8))
  557. {
  558. string menuHeader = binaryReader.ReadString();
  559. NDebug.Assert(
  560. menuHeader == "CM3D2_MENU", "ProcScriptBin 例外 : ヘッダーファイルが不正です。" + menuHeader
  561. );
  562. binaryReader.ReadInt32(); // file version
  563. binaryReader.ReadString(); // txt path
  564. modItem.Name = binaryReader.ReadString(); // name
  565. modItem.Category = binaryReader.ReadString(); // category
  566. if (!accMpn.Contains(modItem.Category)) return false;
  567. binaryReader.ReadString(); // description
  568. binaryReader.ReadInt32(); // idk (as long)
  569. string menuPropString = String.Empty;
  570. string menuPropStringTemp = String.Empty;
  571. try
  572. {
  573. while (true)
  574. {
  575. int numberOfProps = (int)binaryReader.ReadByte();
  576. menuPropStringTemp = menuPropString;
  577. menuPropString = String.Empty;
  578. if (numberOfProps != 0)
  579. {
  580. for (int i = 0; i < numberOfProps; i++)
  581. {
  582. menuPropString = $"{menuPropString}\"{binaryReader.ReadString()}\"";
  583. }
  584. if (menuPropString != string.Empty)
  585. {
  586. string header = UTY.GetStringCom(menuPropString);
  587. string[] menuProps = UTY.GetStringList(menuPropString);
  588. if (header == "end")
  589. {
  590. break;
  591. }
  592. else if (header == "icons" || header == "icon")
  593. {
  594. modItem.IconFile = menuProps[1];
  595. break;
  596. }
  597. else if (header == "priority")
  598. {
  599. modItem.Priority = float.Parse(menuProps[1]);
  600. }
  601. }
  602. }
  603. else
  604. {
  605. break;
  606. }
  607. }
  608. }
  609. catch (Exception e)
  610. {
  611. Utility.LogWarning($"Could not parse menu file '{menuFile}' because {e.Message}");
  612. return false;
  613. }
  614. }
  615. return true;
  616. }
  617. public static bool ParseModMenuFile(string modMenuFile, ModItem modItem)
  618. {
  619. if (!ValidBG2MenuFile(modMenuFile)) return false;
  620. byte[] buf = null;
  621. try
  622. {
  623. using (FileStream fileStream = new FileStream(modMenuFile, FileMode.Open))
  624. {
  625. if (fileStream == null) return false;
  626. if (fileStream.Length == 0L)
  627. {
  628. Utility.LogError($"Mod menu file '{modMenuFile}' is empty");
  629. return false;
  630. }
  631. buf = new byte[fileStream.Length];
  632. fileStream.Read(buf, 0, (int)fileStream.Length);
  633. }
  634. }
  635. catch (Exception e)
  636. {
  637. Utility.LogError($"Could not read mod menu file '{modMenuFile} because {e.Message}'");
  638. return false;
  639. }
  640. if (buf == null) return false;
  641. using (BinaryReader binaryReader = new BinaryReader(new MemoryStream(buf), Encoding.UTF8))
  642. {
  643. try
  644. {
  645. if (binaryReader.ReadString() != "CM3D2_MOD") return false;
  646. else
  647. {
  648. binaryReader.ReadInt32();
  649. string iconName = binaryReader.ReadString();
  650. string baseItemPath = binaryReader.ReadString().Replace(":", " ");
  651. modItem.BaseMenuFile = Path.GetFileName(baseItemPath);
  652. modItem.Name = binaryReader.ReadString();
  653. modItem.Category = binaryReader.ReadString();
  654. if (!accMpn.Contains(modItem.Category)) return false;
  655. binaryReader.ReadString();
  656. string mpnValue = binaryReader.ReadString();
  657. MPN mpn = MPN.null_mpn;
  658. try
  659. {
  660. mpn = (MPN)Enum.Parse(typeof(MPN), mpnValue, true);
  661. }
  662. catch
  663. {
  664. return false;
  665. }
  666. if (mpn != MPN.null_mpn)
  667. {
  668. binaryReader.ReadString();
  669. }
  670. binaryReader.ReadString();
  671. int size = binaryReader.ReadInt32();
  672. for (int i = 0; i < size; i++)
  673. {
  674. string key = binaryReader.ReadString();
  675. int count = binaryReader.ReadInt32();
  676. byte[] data = binaryReader.ReadBytes(count);
  677. if (string.Equals(key, iconName, StringComparison.InvariantCultureIgnoreCase))
  678. {
  679. Texture2D tex = new Texture2D(1, 1, TextureFormat.RGBA32, false);
  680. tex.LoadImage(data);
  681. modItem.Icon = tex;
  682. break;
  683. }
  684. }
  685. }
  686. }
  687. catch (Exception e)
  688. {
  689. Utility.LogWarning($"Could not parse mod menu file '{modMenuFile}' because {e}");
  690. return false;
  691. }
  692. }
  693. return true;
  694. }
  695. public static bool ValidBG2MenuFile(ModItem modItem)
  696. {
  697. return accMpn.Contains(modItem.Category) && ValidBG2MenuFile(modItem.MenuFile);
  698. }
  699. public static bool ValidBG2MenuFile(string menu)
  700. {
  701. menu = Path.GetFileNameWithoutExtension(menu).ToLower();
  702. return !(menu.EndsWith("_del") || menu.Contains("zurashi") || menu.Contains("mekure")
  703. || menu.Contains("porori") || menu.Contains("moza") || menu.Contains("folder"));
  704. }
  705. public abstract class MenuItem
  706. {
  707. public string IconFile { get; set; }
  708. public Texture2D Icon { get; set; }
  709. }
  710. public class ModItem : MenuItem
  711. {
  712. public string MenuFile { get; set; }
  713. public string BaseMenuFile { get; set; }
  714. public string Name { get; set; }
  715. public string Category { get; set; }
  716. public float Priority { get; set; }
  717. public bool IsMod { get; set; }
  718. public bool IsOfficialMod { get; set; }
  719. public static ModItem Deserialize(BinaryReader binaryReader)
  720. {
  721. return new ModItem()
  722. {
  723. MenuFile = binaryReader.ReadNullableString(),
  724. BaseMenuFile = binaryReader.ReadNullableString(),
  725. IconFile = binaryReader.ReadNullableString(),
  726. Name = binaryReader.ReadNullableString(),
  727. Category = binaryReader.ReadNullableString(),
  728. Priority = float.Parse(binaryReader.ReadNullableString()),
  729. IsMod = binaryReader.ReadBoolean(),
  730. IsOfficialMod = binaryReader.ReadBoolean()
  731. };
  732. }
  733. public void Serialize(BinaryWriter binaryWriter)
  734. {
  735. if (IsOfficialMod) return;
  736. binaryWriter.WriteNullableString(MenuFile);
  737. binaryWriter.WriteNullableString(BaseMenuFile);
  738. binaryWriter.WriteNullableString(IconFile);
  739. binaryWriter.WriteNullableString(Name);
  740. binaryWriter.WriteNullableString(Category);
  741. binaryWriter.WriteNullableString(Priority.ToString());
  742. binaryWriter.Write(IsMod);
  743. binaryWriter.Write(IsOfficialMod);
  744. }
  745. }
  746. public class MyRoomItem : MenuItem
  747. {
  748. public int ID { get; set; }
  749. public string PrefabName { get; set; }
  750. }
  751. private class ModelInfo
  752. {
  753. public List<MaterialChange> MaterialChanges { get; set; } = new List<MaterialChange>();
  754. public string ModelFile { get; set; }
  755. }
  756. private struct MaterialChange
  757. {
  758. public int MaterialIndex { get; }
  759. public string MaterialFile { get; }
  760. public MaterialChange(int matno, string matf)
  761. {
  762. this.MaterialIndex = matno;
  763. this.MaterialFile = matf;
  764. }
  765. }
  766. }
  767. }