MenuFileUtility.cs 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  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) => LoadModel(new ModItem(menuFile));
  442. public static GameObject LoadModel(ModItem modItem)
  443. {
  444. byte[] menuBuffer = null;
  445. byte[] modMenuBuffer = null;
  446. if (modItem.IsOfficialMod)
  447. {
  448. using (FileStream fileStream = new FileStream(modItem.MenuFile, FileMode.Open))
  449. {
  450. if (fileStream == null || fileStream.Length == 0)
  451. {
  452. Utility.LogWarning($"Could not open mod menu file '{modItem.MenuFile}'");
  453. return null;
  454. }
  455. else
  456. {
  457. modMenuBuffer = new byte[fileStream.Length];
  458. fileStream.Read(modMenuBuffer, 0, (int)fileStream.Length);
  459. }
  460. }
  461. }
  462. string menu = modItem.IsOfficialMod ? modItem.BaseMenuFile : modItem.MenuFile;
  463. using (AFileBase afileBase = GameUty.FileOpen(menu))
  464. {
  465. if (afileBase == null || !afileBase.IsValid())
  466. {
  467. Utility.LogWarning($"Could not open menu file '{menu}'");
  468. return null;
  469. }
  470. else if (afileBase.GetSize() == 0)
  471. {
  472. Utility.LogWarning($"Mod menu is empty '{menu}'");
  473. return null;
  474. }
  475. else
  476. {
  477. menuBuffer = afileBase.ReadAll();
  478. }
  479. }
  480. ModelInfo modelInfo = new ModelInfo();
  481. if (ProcScriptBin(menuBuffer, modelInfo))
  482. {
  483. GameObject gameObject = null;
  484. try
  485. {
  486. gameObject = LoadSkinMesh_R(modelInfo.ModelFile, 1);
  487. }
  488. catch
  489. {
  490. Utility.LogWarning($"Could not load mesh for '{modItem.MenuFile}'");
  491. }
  492. if (gameObject != null)
  493. {
  494. foreach (MaterialChange matChange in modelInfo.MaterialChanges)
  495. {
  496. foreach (Transform transform in gameObject.transform.GetComponentsInChildren<Transform>(true))
  497. {
  498. Renderer renderer = transform.GetComponent<Renderer>();
  499. if (renderer != null && renderer.material != null
  500. && matChange.MaterialIndex < renderer.materials.Length
  501. )
  502. {
  503. renderer.materials[matChange.MaterialIndex] = ImportCM.LoadMaterial(
  504. matChange.MaterialFile, null, renderer.materials[matChange.MaterialIndex]
  505. );
  506. }
  507. }
  508. }
  509. if (modItem.IsOfficialMod)
  510. {
  511. ProcModScriptBin(modMenuBuffer, gameObject);
  512. }
  513. }
  514. return gameObject;
  515. }
  516. else
  517. {
  518. Utility.LogMessage($"Could not parse menu file '{modItem.MenuFile}'");
  519. return null;
  520. }
  521. }
  522. public static bool ParseNativeMenuFile(int menuIndex, ModItem modItem)
  523. {
  524. MenuDataBase menuDataBase = GameMain.Instance.MenuDataBase;
  525. menuDataBase.SetIndex(menuIndex);
  526. if (menuDataBase.GetBoDelOnly()) return false;
  527. modItem.Category = menuDataBase.GetCategoryMpnText();
  528. if (!accMpn.Contains(modItem.Category)) return false;
  529. modItem.MenuFile = menuDataBase.GetMenuFileName().ToLower();
  530. if (!ValidBG2MenuFile(modItem.MenuFile)) return false;
  531. modItem.Name = menuDataBase.GetMenuName();
  532. modItem.IconFile = menuDataBase.GetIconS();
  533. modItem.Priority = menuDataBase.GetPriority();
  534. return true;
  535. }
  536. public static bool ParseMenuFile(string menuFile, ModItem modItem)
  537. {
  538. if (!ValidBG2MenuFile(menuFile)) return false;
  539. byte[] buf = null;
  540. try
  541. {
  542. using (AFileBase afileBase = GameUty.FileOpen(menuFile))
  543. {
  544. if (afileBase == null || !afileBase.IsValid() || afileBase.GetSize() == 0) return false;
  545. buf = afileBase.ReadAll();
  546. }
  547. }
  548. catch
  549. {
  550. Utility.LogError($"Could not read menu file '{menuFile}'");
  551. return false;
  552. }
  553. using (BinaryReader binaryReader = new BinaryReader(new MemoryStream(buf), Encoding.UTF8))
  554. {
  555. string menuHeader = binaryReader.ReadString();
  556. NDebug.Assert(
  557. menuHeader == "CM3D2_MENU", "ProcScriptBin 例外 : ヘッダーファイルが不正です。" + menuHeader
  558. );
  559. binaryReader.ReadInt32(); // file version
  560. binaryReader.ReadString(); // txt path
  561. modItem.Name = binaryReader.ReadString(); // name
  562. binaryReader.ReadString(); // category
  563. binaryReader.ReadString(); // description
  564. binaryReader.ReadInt32(); // idk (as long)
  565. string menuPropString = String.Empty;
  566. string menuPropStringTemp = String.Empty;
  567. try
  568. {
  569. while (true)
  570. {
  571. int numberOfProps = (int)binaryReader.ReadByte();
  572. menuPropStringTemp = menuPropString;
  573. menuPropString = String.Empty;
  574. if (numberOfProps != 0)
  575. {
  576. for (int i = 0; i < numberOfProps; i++)
  577. {
  578. menuPropString = $"{menuPropString}\"{binaryReader.ReadString()}\"";
  579. }
  580. if (menuPropString != string.Empty)
  581. {
  582. string header = UTY.GetStringCom(menuPropString);
  583. string[] menuProps = UTY.GetStringList(menuPropString);
  584. if (header == "end")
  585. {
  586. break;
  587. }
  588. else if (header == "category")
  589. {
  590. modItem.Category = menuProps[1];
  591. if (!accMpn.Contains(modItem.Category)) return false;
  592. }
  593. else if (header == "icons" || header == "icon")
  594. {
  595. modItem.IconFile = menuProps[1];
  596. break;
  597. }
  598. else if (header == "priority")
  599. {
  600. modItem.Priority = float.Parse(menuProps[1]);
  601. }
  602. }
  603. }
  604. else
  605. {
  606. break;
  607. }
  608. }
  609. }
  610. catch (Exception e)
  611. {
  612. Utility.LogWarning($"Could not parse menu file '{menuFile}' because {e.Message}");
  613. return false;
  614. }
  615. }
  616. return true;
  617. }
  618. public static bool ParseModMenuFile(string modMenuFile, ModItem modItem)
  619. {
  620. if (!ValidBG2MenuFile(modMenuFile)) return false;
  621. byte[] buf = null;
  622. try
  623. {
  624. using (FileStream fileStream = new FileStream(modMenuFile, FileMode.Open))
  625. {
  626. if (fileStream == null) return false;
  627. if (fileStream.Length == 0L)
  628. {
  629. Utility.LogError($"Mod menu file '{modMenuFile}' is empty");
  630. return false;
  631. }
  632. buf = new byte[fileStream.Length];
  633. fileStream.Read(buf, 0, (int)fileStream.Length);
  634. }
  635. }
  636. catch (Exception e)
  637. {
  638. Utility.LogError($"Could not read mod menu file '{modMenuFile} because {e.Message}'");
  639. return false;
  640. }
  641. if (buf == null) return false;
  642. using (BinaryReader binaryReader = new BinaryReader(new MemoryStream(buf), Encoding.UTF8))
  643. {
  644. try
  645. {
  646. if (binaryReader.ReadString() != "CM3D2_MOD") return false;
  647. else
  648. {
  649. binaryReader.ReadInt32();
  650. string iconName = binaryReader.ReadString();
  651. string baseItemPath = binaryReader.ReadString().Replace(":", " ");
  652. modItem.BaseMenuFile = Path.GetFileName(baseItemPath);
  653. modItem.Name = binaryReader.ReadString();
  654. modItem.Category = binaryReader.ReadString();
  655. if (!accMpn.Contains(modItem.Category)) return false;
  656. binaryReader.ReadString();
  657. string mpnValue = binaryReader.ReadString();
  658. MPN mpn = MPN.null_mpn;
  659. try
  660. {
  661. mpn = (MPN)Enum.Parse(typeof(MPN), mpnValue, true);
  662. }
  663. catch
  664. {
  665. return false;
  666. }
  667. if (mpn != MPN.null_mpn)
  668. {
  669. binaryReader.ReadString();
  670. }
  671. binaryReader.ReadString();
  672. int size = binaryReader.ReadInt32();
  673. for (int i = 0; i < size; i++)
  674. {
  675. string key = binaryReader.ReadString();
  676. int count = binaryReader.ReadInt32();
  677. byte[] data = binaryReader.ReadBytes(count);
  678. if (string.Equals(key, iconName, StringComparison.InvariantCultureIgnoreCase))
  679. {
  680. Texture2D tex = new Texture2D(1, 1, TextureFormat.RGBA32, false);
  681. tex.LoadImage(data);
  682. modItem.Icon = tex;
  683. break;
  684. }
  685. }
  686. }
  687. }
  688. catch (Exception e)
  689. {
  690. Utility.LogWarning($"Could not parse mod menu file '{modMenuFile}' because {e}");
  691. return false;
  692. }
  693. }
  694. return true;
  695. }
  696. public static bool ValidBG2MenuFile(ModItem modItem)
  697. {
  698. return accMpn.Contains(modItem.Category) && ValidBG2MenuFile(modItem.MenuFile);
  699. }
  700. public static bool ValidBG2MenuFile(string menu)
  701. {
  702. menu = Path.GetFileNameWithoutExtension(menu).ToLower();
  703. return !(menu.EndsWith("_del") || menu.Contains("zurashi") || menu.Contains("mekure")
  704. || menu.Contains("porori") || menu.Contains("moza") || menu.Contains("folder"));
  705. }
  706. public abstract class MenuItem
  707. {
  708. public string IconFile { get; set; }
  709. public Texture2D Icon { get; set; }
  710. }
  711. public class ModItem : MenuItem
  712. {
  713. public string MenuFile { get; set; }
  714. public string BaseMenuFile { get; set; }
  715. public string Name { get; set; }
  716. public string Category { get; set; }
  717. public float Priority { get; set; }
  718. public bool IsMod { get; set; }
  719. public bool IsOfficialMod { get; set; }
  720. public static ModItem OfficialMod(string menuFile) => new ModItem()
  721. {
  722. MenuFile = menuFile,
  723. IsMod = true,
  724. IsOfficialMod = true,
  725. Priority = 1000f
  726. };
  727. public static ModItem Mod(string menuFile) => new ModItem()
  728. {
  729. MenuFile = menuFile,
  730. IsMod = true
  731. };
  732. public ModItem() { }
  733. public ModItem(string menuFile) => MenuFile = menuFile;
  734. public override string ToString()
  735. {
  736. return IsOfficialMod ? $"{Path.GetFileName(MenuFile)}#{BaseMenuFile}" : MenuFile;
  737. }
  738. public static ModItem Deserialize(BinaryReader binaryReader)
  739. {
  740. return new ModItem()
  741. {
  742. MenuFile = binaryReader.ReadNullableString(),
  743. BaseMenuFile = binaryReader.ReadNullableString(),
  744. IconFile = binaryReader.ReadNullableString(),
  745. Name = binaryReader.ReadNullableString(),
  746. Category = binaryReader.ReadNullableString(),
  747. Priority = float.Parse(binaryReader.ReadNullableString()),
  748. IsMod = binaryReader.ReadBoolean(),
  749. IsOfficialMod = binaryReader.ReadBoolean()
  750. };
  751. }
  752. public void Serialize(BinaryWriter binaryWriter)
  753. {
  754. if (IsOfficialMod) return;
  755. binaryWriter.WriteNullableString(MenuFile);
  756. binaryWriter.WriteNullableString(BaseMenuFile);
  757. binaryWriter.WriteNullableString(IconFile);
  758. binaryWriter.WriteNullableString(Name);
  759. binaryWriter.WriteNullableString(Category);
  760. binaryWriter.WriteNullableString(Priority.ToString());
  761. binaryWriter.Write(IsMod);
  762. binaryWriter.Write(IsOfficialMod);
  763. }
  764. }
  765. public class MyRoomItem : MenuItem
  766. {
  767. public int ID { get; set; }
  768. public string PrefabName { get; set; }
  769. public override string ToString() => $"MYR_{ID}#{PrefabName}";
  770. }
  771. private class ModelInfo
  772. {
  773. public List<MaterialChange> MaterialChanges { get; set; } = new List<MaterialChange>();
  774. public string ModelFile { get; set; }
  775. }
  776. private struct MaterialChange
  777. {
  778. public int MaterialIndex { get; }
  779. public string MaterialFile { get; }
  780. public MaterialChange(int matno, string matf)
  781. {
  782. this.MaterialIndex = matno;
  783. this.MaterialFile = matf;
  784. }
  785. }
  786. }
  787. }