MenuFileUtility.cs 34 KB

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