MenuFileUtility.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. namespace COM3D2.MeidoPhotoStudio.Plugin
  9. {
  10. public static class MenuFileUtility
  11. {
  12. private static byte[] fileBuffer;
  13. public const string noCategory = "noCategory";
  14. public static readonly string[] MenuCategories =
  15. {
  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. public static event EventHandler MenuFilesReadyChange;
  22. public static bool MenuFilesReady { get; private set; }
  23. static MenuFileUtility()
  24. {
  25. accMpn.UnionWith(MenuCategories.Skip(1));
  26. GameMain.Instance.StartCoroutine(CheckMenuDataBaseJob());
  27. }
  28. private static IEnumerator CheckMenuDataBaseJob()
  29. {
  30. if (MenuFilesReady) yield break;
  31. while (!GameMain.Instance.MenuDataBase.JobFinished()) yield return null;
  32. MenuFilesReady = true;
  33. MenuFilesReadyChange?.Invoke(null, EventArgs.Empty);
  34. }
  35. private static ref byte[] GetFileBuffer(long size)
  36. {
  37. if (fileBuffer == null) fileBuffer = new byte[Math.Max(500000, size)];
  38. else if (fileBuffer.Length < size) fileBuffer = new byte[size];
  39. return ref fileBuffer;
  40. }
  41. public static byte[] ReadAFileBase(string filename)
  42. {
  43. using AFileBase aFileBase = GameUty.FileOpen(filename);
  44. if (aFileBase.IsValid() && aFileBase.GetSize() != 0)
  45. {
  46. ref byte[] buffer = ref GetFileBuffer(aFileBase.GetSize());
  47. aFileBase.Read(ref buffer, aFileBase.GetSize());
  48. return buffer;
  49. }
  50. Utility.LogError($"AFileBase '{filename}' is invalid");
  51. return null;
  52. }
  53. public static byte[] ReadOfficialMod(string filename)
  54. {
  55. using var fileStream = new FileStream(filename, FileMode.Open);
  56. if (fileStream.Length != 0)
  57. {
  58. ref byte[] buffer = ref GetFileBuffer(fileStream.Length);
  59. fileStream.Read(buffer, 0, (int) fileStream.Length);
  60. return buffer;
  61. }
  62. Utility.LogWarning($"Mod menu file '{filename}' is invalid");
  63. return null;
  64. }
  65. public static bool ParseNativeMenuFile(int menuIndex, ModItem modItem)
  66. {
  67. MenuDataBase menuDataBase = GameMain.Instance.MenuDataBase;
  68. menuDataBase.SetIndex(menuIndex);
  69. if (menuDataBase.GetBoDelOnly()) return false;
  70. modItem.Category = menuDataBase.GetCategoryMpnText();
  71. if (!accMpn.Contains(modItem.Category)) return false;
  72. modItem.MenuFile = menuDataBase.GetMenuFileName().ToLower();
  73. if (!ValidBG2MenuFile(modItem.MenuFile)) return false;
  74. modItem.Name = menuDataBase.GetMenuName();
  75. modItem.IconFile = menuDataBase.GetIconS();
  76. modItem.Priority = menuDataBase.GetPriority();
  77. return true;
  78. }
  79. public static void ParseMenuFile(string menuFile, ModItem modItem)
  80. {
  81. if (!ValidBG2MenuFile(menuFile)) return;
  82. byte[] buffer;
  83. try { buffer = ReadAFileBase(menuFile); }
  84. catch (Exception e)
  85. {
  86. Utility.LogError($"Could not read menu file '{menuFile}' because {e.Message}");
  87. return ;
  88. }
  89. try
  90. {
  91. using var binaryReader = new BinaryReader(new MemoryStream(buffer), Encoding.UTF8);
  92. if (binaryReader.ReadString() != "CM3D2_MENU") return;
  93. binaryReader.ReadInt32(); // file version
  94. binaryReader.ReadString(); // txt path
  95. modItem.Name = binaryReader.ReadString(); // name
  96. binaryReader.ReadString(); // category
  97. binaryReader.ReadString(); // description
  98. binaryReader.ReadInt32(); // idk (as long)
  99. while (true)
  100. {
  101. int numberOfProps = binaryReader.ReadByte();
  102. var menuPropString = string.Empty;
  103. if (numberOfProps == 0) break;
  104. for (var i = 0; i < numberOfProps; i++)
  105. {
  106. menuPropString = $"{menuPropString}\"{binaryReader.ReadString()}\"";
  107. }
  108. if (string.IsNullOrEmpty(menuPropString)) continue;
  109. var header = UTY.GetStringCom(menuPropString);
  110. string[] menuProps = UTY.GetStringList(menuPropString);
  111. if (header == "end") break;
  112. if (header == "category")
  113. {
  114. modItem.Category = menuProps[1];
  115. if (!accMpn.Contains(modItem.Category)) return;
  116. }
  117. else if (header == "icons" || header == "icon")
  118. {
  119. modItem.IconFile = menuProps[1];
  120. break;
  121. }
  122. else if (header == "priority") modItem.Priority = float.Parse(menuProps[1]);
  123. }
  124. }
  125. catch (Exception e)
  126. {
  127. Utility.LogWarning($"Could not parse menu file '{menuFile}' because {e.Message}");
  128. }
  129. }
  130. public static bool ParseModMenuFile(string modMenuFile, ModItem modItem)
  131. {
  132. if (!ValidBG2MenuFile(modMenuFile)) return false;
  133. byte[] modBuffer;
  134. try { modBuffer = ReadOfficialMod(modMenuFile); }
  135. catch (Exception e)
  136. {
  137. Utility.LogError($"Could not read mod menu file '{modMenuFile} because {e.Message}'");
  138. return false;
  139. }
  140. try
  141. {
  142. using var binaryReader = new BinaryReader(new MemoryStream(modBuffer), Encoding.UTF8);
  143. if (binaryReader.ReadString() != "CM3D2_MOD") return false;
  144. binaryReader.ReadInt32();
  145. var iconName = binaryReader.ReadString();
  146. var baseItemPath = binaryReader.ReadString().Replace(":", " ");
  147. modItem.BaseMenuFile = Path.GetFileName(baseItemPath);
  148. modItem.Name = binaryReader.ReadString();
  149. modItem.Category = binaryReader.ReadString();
  150. if (!accMpn.Contains(modItem.Category)) return false;
  151. binaryReader.ReadString();
  152. var mpnValue = binaryReader.ReadString();
  153. var mpn = MPN.null_mpn;
  154. try { mpn = (MPN) Enum.Parse(typeof(MPN), mpnValue, true); }
  155. catch { /* ignored */ }
  156. if (mpn != MPN.null_mpn) binaryReader.ReadString();
  157. binaryReader.ReadString();
  158. var entryCount = binaryReader.ReadInt32();
  159. for (var i = 0; i < entryCount; i++)
  160. {
  161. var key = binaryReader.ReadString();
  162. var count = binaryReader.ReadInt32();
  163. byte[] data = binaryReader.ReadBytes(count);
  164. if (!string.Equals(key, iconName, StringComparison.InvariantCultureIgnoreCase)) continue;
  165. var tex = new Texture2D(1, 1, TextureFormat.RGBA32, false);
  166. tex.LoadImage(data);
  167. modItem.Icon = tex;
  168. break;
  169. }
  170. }
  171. catch (Exception e)
  172. {
  173. Utility.LogWarning($"Could not parse mod menu file '{modMenuFile}' because {e}");
  174. return false;
  175. }
  176. return true;
  177. }
  178. public static bool ValidBG2MenuFile(ModItem modItem)
  179. => accMpn.Contains(modItem.Category) && ValidBG2MenuFile(modItem.MenuFile);
  180. public static bool ValidBG2MenuFile(string menu)
  181. {
  182. menu = Path.GetFileNameWithoutExtension(menu).ToLower();
  183. return !(menu.EndsWith("_del") || menu.Contains("zurashi") || menu.Contains("mekure")
  184. || menu.Contains("porori") || menu.Contains("moza") || menu.Contains("folder"));
  185. }
  186. }
  187. }