Constants.cs 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Xml.Linq;
  6. using MyRoomCustom;
  7. using Newtonsoft.Json;
  8. using UnityEngine;
  9. using wf;
  10. using static MeidoPhotoStudio.Plugin.MenuFileUtility;
  11. namespace MeidoPhotoStudio.Plugin;
  12. public static class Constants
  13. {
  14. public const string CustomPoseDirectory = "Custom Poses";
  15. public const string CustomHandDirectory = "Hand Presets";
  16. public const string CustomFaceDirectory = "Face Presets";
  17. public const string SceneDirectory = "Scenes";
  18. public const string KankyoDirectory = "Environments";
  19. public const string ConfigDirectory = "MeidoPhotoStudio";
  20. public const string TranslationDirectory = "Translations";
  21. public const string DatabaseDirectory = "Database";
  22. public static readonly string CustomPosePath;
  23. public static readonly string CustomHandPath;
  24. public static readonly string CustomFacePath;
  25. public static readonly string ScenesPath;
  26. public static readonly string KankyoPath;
  27. public static readonly string ConfigPath;
  28. public static readonly string DatabasePath;
  29. // TODO: Some of these IDs aren't used to use them or drop them.
  30. public static readonly int MainWindowID = 765;
  31. public static readonly int MessageWindowID = 961;
  32. public static readonly int SceneManagerWindowID = 876;
  33. public static readonly int SceneManagerModalID = 283;
  34. public static readonly int DropdownWindowID = 777;
  35. public static readonly List<string> PoseGroupList = new();
  36. public static readonly Dictionary<string, List<string>> PoseDict = new();
  37. public static readonly List<string> CustomPoseGroupList = new();
  38. public static readonly Dictionary<string, List<string>> CustomPoseDict = new();
  39. public static readonly List<string> CustomHandGroupList = new();
  40. public static readonly Dictionary<string, List<string>> CustomHandDict = new();
  41. public static readonly List<string> FaceGroupList = new();
  42. public static readonly Dictionary<string, List<string>> FaceDict = new();
  43. public static readonly List<string> CustomFaceGroupList = new();
  44. public static readonly Dictionary<string, List<string>> CustomFaceDict = new();
  45. public static readonly List<string> BGList = new();
  46. public static readonly List<KeyValuePair<string, string>> MyRoomCustomBGList = new();
  47. public static readonly List<string> DoguCategories = new();
  48. public static readonly Dictionary<string, List<string>> DoguDict = new();
  49. public static readonly List<string> MyRoomPropCategories = new();
  50. public static readonly Dictionary<string, List<MyRoomItem>> MyRoomPropDict = new();
  51. public static readonly Dictionary<string, List<ModItem>> ModPropDict =
  52. new(StringComparer.InvariantCultureIgnoreCase);
  53. public static readonly List<string> SceneDirectoryList = new();
  54. public static readonly List<string> KankyoDirectoryList = new();
  55. public static readonly List<MpnAttachProp> MpnAttachPropList = new();
  56. public static readonly Dictionary<DoguCategory, string> CustomDoguCategories =
  57. new()
  58. {
  59. [DoguCategory.Other] = "other",
  60. [DoguCategory.Mob] = "mob",
  61. [DoguCategory.Desk] = "desk",
  62. [DoguCategory.HandItem] = "handItem",
  63. [DoguCategory.BGSmall] = "bgSmall",
  64. };
  65. private static bool beginHandItemInit;
  66. private static bool beginMpnAttachInit;
  67. static Constants()
  68. {
  69. ConfigPath = Path.Combine(BepInEx.Paths.ConfigPath, ConfigDirectory);
  70. var presetPath = Path.Combine(ConfigPath, "Presets");
  71. CustomPosePath = Path.Combine(presetPath, CustomPoseDirectory);
  72. CustomHandPath = Path.Combine(presetPath, CustomHandDirectory);
  73. CustomFacePath = Path.Combine(presetPath, CustomFaceDirectory);
  74. ScenesPath = Path.Combine(ConfigPath, SceneDirectory);
  75. KankyoPath = Path.Combine(ConfigPath, KankyoDirectory);
  76. DatabasePath = Path.Combine(ConfigPath, DatabaseDirectory);
  77. var directories =
  78. new[] { CustomPosePath, CustomHandPath, ScenesPath, KankyoPath, ConfigPath, CustomFacePath, DatabasePath };
  79. foreach (var directory in directories)
  80. if (!Directory.Exists(directory))
  81. Directory.CreateDirectory(directory);
  82. }
  83. public static event EventHandler<MenuFilesEventArgs> MenuFilesChange;
  84. public static event EventHandler<PresetChangeEventArgs> CustomPoseChange;
  85. public static event EventHandler<PresetChangeEventArgs> CustomHandChange;
  86. public static event EventHandler<PresetChangeEventArgs> CustomFaceChange;
  87. public enum Window
  88. {
  89. Call,
  90. Pose,
  91. Face,
  92. BG,
  93. BG2,
  94. Main,
  95. Message,
  96. Save,
  97. SaveModal,
  98. Settings,
  99. }
  100. public enum Scene
  101. {
  102. Daily = 3,
  103. Edit = 5,
  104. }
  105. public enum DoguCategory
  106. {
  107. Other,
  108. Mob,
  109. Desk,
  110. HandItem,
  111. BGSmall,
  112. }
  113. public static int MyRoomCustomBGIndex { get; private set; } = -1;
  114. public static bool HandItemsInitialized { get; private set; }
  115. public static bool MpnAttachInitialized { get; private set; }
  116. public static bool MenuFilesInitialized { get; private set; }
  117. public static void Initialize()
  118. {
  119. InitializeSceneDirectories();
  120. InitializeKankyoDirectories();
  121. InitializePoses();
  122. InitializeHandPresets();
  123. InitializeFaceBlends();
  124. InitializeBGs();
  125. InitializeDogu();
  126. InitializeMyRoomProps();
  127. InitializeMpnAttachProps();
  128. }
  129. public static void AddFacePreset(Dictionary<string, float> faceData, string filename, string directory)
  130. {
  131. filename = Utility.SanitizePathPortion(filename);
  132. directory = Utility.SanitizePathPortion(directory);
  133. if (string.IsNullOrEmpty(filename))
  134. filename = "face_preset";
  135. if (directory.Equals(CustomFaceDirectory, StringComparison.InvariantCultureIgnoreCase))
  136. directory = string.Empty;
  137. directory = Path.Combine(CustomFacePath, directory);
  138. if (!Directory.Exists(directory))
  139. Directory.CreateDirectory(directory);
  140. var fullPath = Path.Combine(directory, filename);
  141. if (File.Exists($"{fullPath}.xml"))
  142. fullPath += $"_{Utility.Timestamp}";
  143. fullPath = Path.GetFullPath($"{fullPath}.xml");
  144. if (!fullPath.StartsWith(CustomFacePath))
  145. {
  146. Utility.LogError($"Could not save face preset! Path is invalid: '{fullPath}'");
  147. return;
  148. }
  149. var rootElement = new XElement("FaceData");
  150. foreach (var kvp in faceData)
  151. rootElement.Add(new XElement("elm", kvp.Value.ToString("G9"), new XAttribute("name", kvp.Key)));
  152. var fullDocument = new XDocument(
  153. new XDeclaration("1.0", "utf-8", "true"),
  154. new XComment("MeidoPhotoStudio Face Preset"),
  155. rootElement);
  156. fullDocument.Save(fullPath);
  157. var fileInfo = new FileInfo(fullPath);
  158. var category = fileInfo.Directory.Name;
  159. var faceGroup = CustomFaceGroupList.Find(
  160. group => string.Equals(category, group, StringComparison.InvariantCultureIgnoreCase));
  161. if (string.IsNullOrEmpty(faceGroup))
  162. {
  163. CustomFaceGroupList.Add(category);
  164. CustomFaceDict[category] = new();
  165. CustomFaceGroupList.Sort((a, b) => KeepAtTop(a, b, CustomFaceDirectory));
  166. }
  167. else
  168. {
  169. category = faceGroup;
  170. }
  171. CustomFaceDict[category].Add(fullPath);
  172. CustomFaceDict[category].Sort(WindowsLogicalComparer.StrCmpLogicalW);
  173. CustomFaceChange?.Invoke(null, new(fullPath, category));
  174. }
  175. public static void AddPose(byte[] anmBinary, string filename, string directory)
  176. {
  177. // TODO: Consider writing a file system monitor
  178. filename = Utility.SanitizePathPortion(filename);
  179. directory = Utility.SanitizePathPortion(directory);
  180. if (string.IsNullOrEmpty(filename))
  181. filename = "custom_pose";
  182. if (directory.Equals(CustomPoseDirectory, StringComparison.InvariantCultureIgnoreCase))
  183. directory = string.Empty;
  184. directory = Path.Combine(CustomPosePath, directory);
  185. if (!Directory.Exists(directory))
  186. Directory.CreateDirectory(directory);
  187. var fullPath = Path.Combine(directory, filename);
  188. if (File.Exists($"{fullPath}.anm"))
  189. fullPath += $"_{Utility.Timestamp}";
  190. fullPath = Path.GetFullPath($"{fullPath}.anm");
  191. if (!fullPath.StartsWith(CustomPosePath))
  192. {
  193. Utility.LogError($"Could not save pose! Path is invalid: '{fullPath}'");
  194. return;
  195. }
  196. File.WriteAllBytes(fullPath, anmBinary);
  197. var fileInfo = new FileInfo(fullPath);
  198. var category = fileInfo.Directory.Name;
  199. var poseGroup = CustomPoseGroupList.Find(
  200. group => string.Equals(category, group, StringComparison.InvariantCultureIgnoreCase));
  201. if (string.IsNullOrEmpty(poseGroup))
  202. {
  203. CustomPoseGroupList.Add(category);
  204. CustomPoseDict[category] = new();
  205. CustomPoseGroupList.Sort((a, b) => KeepAtTop(a, b, CustomPoseDirectory));
  206. }
  207. else
  208. {
  209. category = poseGroup;
  210. }
  211. CustomPoseDict[category].Add(fullPath);
  212. CustomPoseDict[category].Sort(WindowsLogicalComparer.StrCmpLogicalW);
  213. CustomPoseChange?.Invoke(null, new(fullPath, category));
  214. }
  215. public static void AddHand(byte[] handBinary, bool right, string filename, string directory)
  216. {
  217. filename = Utility.SanitizePathPortion(filename);
  218. directory = Utility.SanitizePathPortion(directory);
  219. if (string.IsNullOrEmpty(filename))
  220. filename = "custom_hand";
  221. if (directory.Equals(CustomHandDirectory, StringComparison.InvariantCultureIgnoreCase))
  222. directory = string.Empty;
  223. directory = Path.Combine(CustomHandPath, directory);
  224. if (!Directory.Exists(directory))
  225. Directory.CreateDirectory(directory);
  226. var fullPath = Path.Combine(directory, filename);
  227. if (File.Exists($"{fullPath}.xml"))
  228. fullPath += $"_{Utility.Timestamp}";
  229. fullPath = Path.GetFullPath($"{fullPath}.xml");
  230. if (!fullPath.StartsWith(CustomHandPath))
  231. {
  232. Utility.LogError($"Could not save hand! Path is invalid: '{fullPath}'");
  233. return;
  234. }
  235. // TODO: This does not actually do what I think it does.
  236. var gameVersion = Misc.GAME_VERSION; // get game version from user's Assembly-CSharp
  237. var finalXml = new XDocument(
  238. new XDeclaration("1.0", "utf-8", "true"),
  239. new XComment("CM3D2 FingerData"),
  240. new XElement(
  241. "FingerData",
  242. new XElement("GameVersion", gameVersion),
  243. new XElement("RightData", right),
  244. new XElement("BinaryData", Convert.ToBase64String(handBinary))));
  245. finalXml.Save(fullPath);
  246. var fileInfo = new FileInfo(fullPath);
  247. var category = fileInfo.Directory.Name;
  248. var handGroup = CustomHandGroupList.Find(
  249. group => string.Equals(category, group, StringComparison.InvariantCultureIgnoreCase));
  250. if (string.IsNullOrEmpty(handGroup))
  251. {
  252. CustomHandGroupList.Add(category);
  253. CustomHandDict[category] = new();
  254. CustomHandGroupList.Sort((a, b) => KeepAtTop(a, b, CustomHandDirectory));
  255. }
  256. else
  257. {
  258. category = handGroup;
  259. }
  260. CustomHandDict[category].Add(fullPath);
  261. CustomHandDict[category].Sort(WindowsLogicalComparer.StrCmpLogicalW);
  262. CustomHandChange?.Invoke(null, new(fullPath, category));
  263. }
  264. public static void InitializeSceneDirectories()
  265. {
  266. SceneDirectoryList.Clear();
  267. SceneDirectoryList.Add(SceneDirectory);
  268. foreach (var directory in Directory.GetDirectories(ScenesPath))
  269. SceneDirectoryList.Add(new DirectoryInfo(directory).Name);
  270. SceneDirectoryList.Sort((a, b) => KeepAtTop(a, b, SceneDirectory));
  271. }
  272. public static void InitializeKankyoDirectories()
  273. {
  274. KankyoDirectoryList.Clear();
  275. KankyoDirectoryList.Add(KankyoDirectory);
  276. foreach (var directory in Directory.GetDirectories(KankyoPath))
  277. KankyoDirectoryList.Add(new DirectoryInfo(directory).Name);
  278. KankyoDirectoryList.Sort((a, b) => KeepAtTop(a, b, KankyoDirectory));
  279. }
  280. public static void InitializePoses()
  281. {
  282. // Load Poses
  283. var poseListPath = Path.Combine(DatabasePath, "mm_pose_list.json");
  284. try
  285. {
  286. var poseListJson = File.ReadAllText(poseListPath);
  287. foreach (var poseList in JsonConvert.DeserializeObject<List<SerializePoseList>>(poseListJson))
  288. {
  289. PoseDict[poseList.UIName] = poseList.PoseList;
  290. PoseGroupList.Add(poseList.UIName);
  291. }
  292. }
  293. catch (IOException e)
  294. {
  295. Utility.LogError($"Could not open pose database because {e.Message}");
  296. Utility.LogMessage("Pose list will be serverely limited.");
  297. AddDefaultPose();
  298. }
  299. catch (Exception e)
  300. {
  301. Utility.LogError($"Could not parse pose database because {e.Message}");
  302. AddDefaultPose();
  303. }
  304. // Get Other poses that'll go into Normal, Normal 2 and Ero 2
  305. var com3d2MotionList = GameUty.FileSystem.GetList("motion", AFileSystemBase.ListType.AllFile);
  306. if (com3d2MotionList?.Length > 0)
  307. {
  308. var poseSet = new HashSet<string>();
  309. foreach (var poses in PoseDict.Values)
  310. poseSet.UnionWith(poses);
  311. var newCategories = new[] { "normal2", "ero2" };
  312. foreach (var category in newCategories)
  313. if (!PoseDict.ContainsKey(category))
  314. PoseDict[category] = new();
  315. // TODO: Try to group these poses into more than "normal2" and "ero2"
  316. foreach (var path in com3d2MotionList)
  317. {
  318. if (Path.GetExtension(path) is not ".anm")
  319. continue;
  320. var file = Path.GetFileNameWithoutExtension(path);
  321. if (poseSet.Contains(file))
  322. continue;
  323. if (file.StartsWith("edit_"))
  324. {
  325. PoseDict["normal"].Add(file);
  326. }
  327. else if (file is not ("dance_cm3d2_001_zoukin" or "dance_cm3d2_001_mop" or "aruki_1_idougo_f"
  328. or "sleep2" or "stand_akire2") && !file.EndsWith("_3_") && !file.EndsWith("_5_")
  329. && !file.StartsWith("vr_") && !file.StartsWith("dance_mc") && !file.Contains("_kubi_")
  330. && !file.Contains("a01_") && !file.Contains("b01_") && !file.Contains("b02_")
  331. && !file.EndsWith("_m2") && !file.EndsWith("_m2_once_") && !file.StartsWith("h_")
  332. && !file.StartsWith("event_") && !file.StartsWith("man_") && !file.EndsWith("_m")
  333. && !file.Contains("_m_") && !file.Contains("_man_"))
  334. {
  335. if (path.Contains(@"\sex\"))
  336. PoseDict["ero2"].Add(file);
  337. else
  338. PoseDict["normal2"].Add(file);
  339. }
  340. }
  341. foreach (var category in newCategories)
  342. {
  343. if (PoseDict[category].Count > 0)
  344. {
  345. if (!PoseGroupList.Contains(category))
  346. PoseGroupList.Add(category);
  347. }
  348. else
  349. {
  350. PoseDict.Remove(category);
  351. }
  352. }
  353. }
  354. InitializeCustomPoses();
  355. static void AddDefaultPose()
  356. {
  357. if (!PoseDict.ContainsKey("normal"))
  358. PoseDict["normal"] = new()
  359. {
  360. "maid_stand01",
  361. };
  362. if (!PoseGroupList.Contains("normal"))
  363. PoseGroupList.Insert(0, "normal");
  364. }
  365. }
  366. public static void InitializeCustomPoses()
  367. {
  368. CustomPoseGroupList.Clear();
  369. CustomPoseDict.Clear();
  370. CustomPoseGroupList.Add(CustomPoseDirectory);
  371. CustomPoseDict[CustomPoseDirectory] = new();
  372. GetPoses(CustomPosePath);
  373. foreach (var directory in Directory.GetDirectories(CustomPosePath))
  374. GetPoses(directory);
  375. CustomPoseGroupList.Sort((a, b) => KeepAtTop(a, b, CustomPoseDirectory));
  376. CustomPoseChange?.Invoke(null, PresetChangeEventArgs.Empty);
  377. static void GetPoses(string directory)
  378. {
  379. var poseList = Directory.GetFiles(directory)
  380. .Where(file => Path.GetExtension(file) is ".anm");
  381. if (!poseList.Any())
  382. return;
  383. var poseGroupName = new DirectoryInfo(directory).Name;
  384. if (poseGroupName is not CustomPoseDirectory)
  385. CustomPoseGroupList.Add(poseGroupName);
  386. CustomPoseDict[poseGroupName] = poseList.ToList();
  387. CustomPoseDict[poseGroupName].Sort(WindowsLogicalComparer.StrCmpLogicalW);
  388. }
  389. }
  390. public static void InitializeHandPresets()
  391. {
  392. CustomHandGroupList.Clear();
  393. CustomHandDict.Clear();
  394. CustomHandGroupList.Add(CustomHandDirectory);
  395. CustomHandDict[CustomHandDirectory] = new();
  396. GetPresets(CustomHandPath);
  397. foreach (var directory in Directory.GetDirectories(CustomHandPath))
  398. GetPresets(directory);
  399. CustomHandGroupList.Sort((a, b) => KeepAtTop(a, b, CustomHandDirectory));
  400. CustomHandChange?.Invoke(null, PresetChangeEventArgs.Empty);
  401. static void GetPresets(string directory)
  402. {
  403. var presetList = Directory.GetFiles(directory)
  404. .Where(file => Path.GetExtension(file) is ".xml");
  405. if (!presetList.Any())
  406. return;
  407. var presetCategory = new DirectoryInfo(directory).Name;
  408. if (presetCategory is not CustomHandDirectory)
  409. CustomHandGroupList.Add(presetCategory);
  410. CustomHandDict[presetCategory] = presetList.ToList();
  411. CustomHandDict[presetCategory].Sort(WindowsLogicalComparer.StrCmpLogicalW);
  412. }
  413. }
  414. public static void InitializeFaceBlends()
  415. {
  416. PhotoFaceData.Create();
  417. FaceGroupList.AddRange(PhotoFaceData.popup_category_list.Select(kvp => kvp.Key));
  418. foreach (var kvp in PhotoFaceData.category_list)
  419. FaceDict[kvp.Key] = kvp.Value.ConvertAll(data => data.setting_name);
  420. InitializeCustomFaceBlends();
  421. }
  422. public static void InitializeCustomFaceBlends()
  423. {
  424. CustomFaceGroupList.Clear();
  425. CustomFaceDict.Clear();
  426. CustomFaceGroupList.Add(CustomFaceDirectory);
  427. CustomFaceDict[CustomFaceDirectory] = new();
  428. GetFacePresets(CustomFacePath);
  429. foreach (var directory in Directory.GetDirectories(CustomFacePath))
  430. GetFacePresets(directory);
  431. CustomFaceGroupList.Sort((a, b) => KeepAtTop(a, b, CustomFaceDirectory));
  432. CustomFaceChange?.Invoke(null, PresetChangeEventArgs.Empty);
  433. static void GetFacePresets(string directory)
  434. {
  435. IEnumerable<string> presetList = Directory.GetFiles(directory)
  436. .Where(file => Path.GetExtension(file) is ".xml").ToList();
  437. if (!presetList.Any())
  438. return;
  439. var faceGroupName = new DirectoryInfo(directory).Name;
  440. if (faceGroupName is not CustomFaceDirectory)
  441. CustomFaceGroupList.Add(faceGroupName);
  442. CustomFaceDict[faceGroupName] = presetList.ToList();
  443. CustomFaceDict[faceGroupName].Sort(WindowsLogicalComparer.StrCmpLogicalW);
  444. }
  445. }
  446. public static void InitializeBGs()
  447. {
  448. // Load BGs
  449. PhotoBGData.Create();
  450. // COM3D2 BGs
  451. foreach (var bgData in PhotoBGData.data)
  452. {
  453. if (string.IsNullOrEmpty(bgData.create_prefab_name))
  454. continue;
  455. var bg = bgData.create_prefab_name;
  456. BGList.Add(bg);
  457. }
  458. // CM3D2 BGs
  459. if (GameUty.IsEnabledCompatibilityMode)
  460. {
  461. using var csvParser = OpenCsvParser("phot_bg_list.nei", GameUty.FileSystemOld);
  462. for (var cell_y = 1; cell_y < csvParser.max_cell_y; cell_y++)
  463. {
  464. if (!csvParser.IsCellToExistData(3, cell_y))
  465. continue;
  466. var bg = csvParser.GetCellAsString(3, cell_y);
  467. BGList.Add(bg);
  468. }
  469. }
  470. // Set index regardless of there being myRoom bgs or not
  471. MyRoomCustomBGIndex = BGList.Count;
  472. var saveDataDict = CreativeRoomManager.GetSaveDataDic();
  473. if (saveDataDict is not null)
  474. MyRoomCustomBGList.AddRange(saveDataDict);
  475. }
  476. public static void InitializeDogu()
  477. {
  478. foreach (var customCategory in CustomDoguCategories.Values)
  479. DoguDict[customCategory] = new();
  480. InitializeDeskItems();
  481. InitializePhotoBGItems();
  482. InitializeOtherDogu();
  483. InitializeHandItems();
  484. foreach (var category in PhotoBGObjectData.popup_category_list.Select(kvp => kvp.Key))
  485. {
  486. if (category is "マイオブジェクト")
  487. continue;
  488. DoguCategories.Add(category);
  489. }
  490. foreach (DoguCategory category in Enum.GetValues(typeof(DoguCategory)))
  491. DoguCategories.Add(CustomDoguCategories[category]);
  492. }
  493. public static List<ModItem> GetModPropList(string category)
  494. {
  495. if (!PropManager.ModItemsOnly && !MenuFilesReady)
  496. {
  497. Utility.LogMessage("Menu files are not ready yet");
  498. return null;
  499. }
  500. if (!MenuFilesInitialized)
  501. InitializeModProps();
  502. if (!ModPropDict.ContainsKey(category))
  503. return null;
  504. var selectedList = ModPropDict[category];
  505. if (!selectedList[0].Icon)
  506. {
  507. selectedList.Sort((a, b) =>
  508. {
  509. var res = a.Priority.CompareTo(b.Priority);
  510. if (res is 0)
  511. res = string.Compare(a.Name, b.Name);
  512. return res;
  513. });
  514. var previousMenuFile = string.Empty;
  515. selectedList.RemoveAll(item =>
  516. {
  517. if (item.Icon)
  518. return false;
  519. Texture2D icon;
  520. var iconFile = item.IconFile;
  521. if (string.IsNullOrEmpty(iconFile))
  522. {
  523. // TODO: Remove '{iconFile}' since it will not add anymore information.
  524. Utility.LogWarning($"Could not find icon '{iconFile}' for menu '{item.MenuFile}");
  525. return true;
  526. }
  527. try
  528. {
  529. icon = ImportCM.CreateTexture(iconFile);
  530. }
  531. catch
  532. {
  533. try
  534. {
  535. icon = ImportCM.CreateTexture($"tex\\{iconFile}");
  536. }
  537. catch
  538. {
  539. Utility.LogWarning($"Could not load '{iconFile}' for menu '{item.MenuFile}");
  540. return true;
  541. }
  542. }
  543. item.Icon = icon;
  544. return false;
  545. });
  546. }
  547. return selectedList;
  548. }
  549. private static void InitializeOtherDogu()
  550. {
  551. DoguDict[CustomDoguCategories[DoguCategory.BGSmall]] = BGList;
  552. DoguDict[CustomDoguCategories[DoguCategory.Mob]].AddRange(
  553. new[]
  554. {
  555. "Mob_Man_Stand001", "Mob_Man_Stand002", "Mob_Man_Stand003", "Mob_Man_Sit001", "Mob_Man_Sit002",
  556. "Mob_Man_Sit003", "Mob_Girl_Stand001", "Mob_Girl_Stand002", "Mob_Girl_Stand003", "Mob_Girl_Sit001",
  557. "Mob_Girl_Sit002", "Mob_Girl_Sit003",
  558. });
  559. var otherDoguList = DoguDict[CustomDoguCategories[DoguCategory.Other]];
  560. // bg object extend
  561. var doguHashSet = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase);
  562. doguHashSet.UnionWith(BGList);
  563. try
  564. {
  565. var ignoreListPath = Path.Combine(DatabasePath, "bg_ignore_list.json");
  566. var ignoreListJson = File.ReadAllText(ignoreListPath);
  567. var ignoreList = JsonConvert.DeserializeObject<string[]>(ignoreListJson);
  568. doguHashSet.UnionWith(ignoreList);
  569. }
  570. catch (IOException e)
  571. {
  572. Utility.LogWarning($"Could not open ignored BG database because {e.Message}");
  573. }
  574. catch
  575. {
  576. // Ignored.
  577. }
  578. foreach (var doguList in DoguDict.Values)
  579. doguHashSet.UnionWith(doguList);
  580. foreach (var path in GameUty.FileSystem.GetList("bg", AFileSystemBase.ListType.AllFile))
  581. {
  582. if (Path.GetExtension(path) is not ".asset_bg" || path.Contains("myroomcustomize"))
  583. continue;
  584. var file = Path.GetFileNameWithoutExtension(path);
  585. if (doguHashSet.Contains(file) || file.EndsWith("_hit"))
  586. continue;
  587. otherDoguList.Add(file);
  588. doguHashSet.Add(file);
  589. }
  590. // Get cherry picked dogu that I can't find in the game files
  591. try
  592. {
  593. var doguExtendPath = Path.Combine(DatabasePath, "extra_dogu.json");
  594. var doguExtendJson = File.ReadAllText(doguExtendPath);
  595. otherDoguList.AddRange(JsonConvert.DeserializeObject<IEnumerable<string>>(doguExtendJson));
  596. }
  597. catch (IOException e)
  598. {
  599. Utility.LogError($"Could not open extra prop database because {e.Message}");
  600. }
  601. catch (Exception e)
  602. {
  603. Utility.LogError($"Could not parse extra prop database because {e.Message}");
  604. }
  605. foreach (var path in GameUty.FileSystemOld.GetList("bg", AFileSystemBase.ListType.AllFile))
  606. {
  607. if (Path.GetExtension(path) is not ".asset_bg")
  608. continue;
  609. var file = Path.GetFileNameWithoutExtension(path);
  610. if (!doguHashSet.Contains(file) && !file.EndsWith("_not_optimisation"))
  611. otherDoguList.Add(file);
  612. }
  613. }
  614. private static void InitializeDeskItems()
  615. {
  616. var enabledIDs = new HashSet<int>();
  617. CsvCommonIdManager.ReadEnabledIdList(
  618. CsvCommonIdManager.FileSystemType.Normal, true, "desk_item_enabled_id", ref enabledIDs);
  619. CsvCommonIdManager.ReadEnabledIdList(
  620. CsvCommonIdManager.FileSystemType.Old, true, "desk_item_enabled_id", ref enabledIDs);
  621. var com3d2DeskDogu = DoguDict[CustomDoguCategories[DoguCategory.Desk]];
  622. GetDeskItems(GameUty.FileSystem);
  623. void GetDeskItems(AFileSystemBase fs)
  624. {
  625. using var csvParser = OpenCsvParser("desk_item_detail.nei", fs);
  626. for (var cell_y = 1; cell_y < csvParser.max_cell_y; cell_y++)
  627. {
  628. if (!csvParser.IsCellToExistData(0, cell_y))
  629. continue;
  630. var cell = csvParser.GetCellAsInteger(0, cell_y);
  631. if (!enabledIDs.Contains(cell))
  632. continue;
  633. var dogu = string.Empty;
  634. if (csvParser.IsCellToExistData(3, cell_y))
  635. dogu = csvParser.GetCellAsString(3, cell_y);
  636. else if (csvParser.IsCellToExistData(4, cell_y))
  637. dogu = csvParser.GetCellAsString(4, cell_y);
  638. if (!string.IsNullOrEmpty(dogu))
  639. com3d2DeskDogu.Add(dogu);
  640. }
  641. }
  642. }
  643. private static void InitializePhotoBGItems()
  644. {
  645. PhotoBGObjectData.Create();
  646. var photoBGObjectList = PhotoBGObjectData.data;
  647. var doguCategories = new List<string>();
  648. var addedCategories = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase);
  649. foreach (var photoBGObject in photoBGObjectList)
  650. {
  651. var category = photoBGObject.category;
  652. if (!addedCategories.Contains(category))
  653. {
  654. addedCategories.Add(category);
  655. doguCategories.Add(category);
  656. }
  657. if (!DoguDict.ContainsKey(category))
  658. DoguDict[category] = new();
  659. var dogu = string.Empty;
  660. if (!string.IsNullOrEmpty(photoBGObject.create_prefab_name))
  661. dogu = photoBGObject.create_prefab_name;
  662. else if (!string.IsNullOrEmpty(photoBGObject.create_asset_bundle_name))
  663. dogu = photoBGObject.create_asset_bundle_name;
  664. else if (!string.IsNullOrEmpty(photoBGObject.direct_file))
  665. dogu = photoBGObject.direct_file;
  666. if (!string.IsNullOrEmpty(dogu))
  667. DoguDict[category].Add(dogu);
  668. }
  669. DoguDict["パーティクル"].AddRange(
  670. new[]
  671. {
  672. "Particle/pLineY", "Particle/pLineP02", "Particle/pHeart01",
  673. "Particle/pLine_act2", "Particle/pstarY_act3",
  674. });
  675. }
  676. private static void InitializeHandItems()
  677. {
  678. if (HandItemsInitialized)
  679. return;
  680. if (!MenuFilesReady)
  681. {
  682. if (!beginHandItemInit)
  683. MenuFilesReadyChange += (_, _) =>
  684. InitializeHandItems();
  685. beginHandItemInit = true;
  686. return;
  687. }
  688. var menuDataBase = GameMain.Instance.MenuDataBase;
  689. var doguHashSet = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase);
  690. doguHashSet.UnionWith(BGList);
  691. try
  692. {
  693. var ignoreListPath = Path.Combine(DatabasePath, "bg_ignore_list.json");
  694. var ignoreListJson = File.ReadAllText(ignoreListPath);
  695. var ignoreList = JsonConvert.DeserializeObject<string[]>(ignoreListJson);
  696. doguHashSet.UnionWith(ignoreList);
  697. }
  698. catch (IOException e)
  699. {
  700. Utility.LogWarning($"Could not open ignored BG database because {e.Message}");
  701. }
  702. catch (Exception e)
  703. {
  704. Utility.LogError($"Could not parse ignored BG database because {e.Message}");
  705. }
  706. foreach (var doguList in DoguDict.Values)
  707. doguHashSet.UnionWith(doguList);
  708. var category = CustomDoguCategories[DoguCategory.HandItem];
  709. for (var i = 0; i < menuDataBase.GetDataSize(); i++)
  710. {
  711. menuDataBase.SetIndex(i);
  712. if ((MPN)menuDataBase.GetCategoryMpn() is not MPN.handitem)
  713. continue;
  714. var menuFileName = menuDataBase.GetMenuFileName();
  715. if (menuDataBase.GetBoDelOnly() || menuFileName.EndsWith("_del.menu"))
  716. continue;
  717. var handItemAsOdogu = Utility.HandItemToOdogu(menuFileName);
  718. var isolatedHandItem = menuFileName.Substring(menuFileName.IndexOf('_') + 1);
  719. if (doguHashSet.Contains(handItemAsOdogu) || doguHashSet.Contains(isolatedHandItem))
  720. continue;
  721. doguHashSet.Add(isolatedHandItem);
  722. DoguDict[category].Add(menuFileName);
  723. // Check for a half deck of cards to add the full deck as well
  724. if (menuFileName is "handitemd_cards_i_.menu")
  725. DoguDict[category].Add("handiteml_cards_i_.menu");
  726. }
  727. HandItemsInitialized = true;
  728. OnMenuFilesChange(MenuFilesEventArgs.EventType.HandItems);
  729. }
  730. private static void InitializeMpnAttachProps()
  731. {
  732. if (MpnAttachInitialized)
  733. return;
  734. if (!MenuFilesReady)
  735. {
  736. if (beginMpnAttachInit)
  737. return;
  738. beginMpnAttachInit = true;
  739. MenuFilesReadyChange += (_, _) =>
  740. InitializeMpnAttachProps();
  741. return;
  742. }
  743. var menuDataBase = GameMain.Instance.MenuDataBase;
  744. var attachMpn = new[] { MPN.kousoku_lower, MPN.kousoku_upper };
  745. for (var i = 0; i < menuDataBase.GetDataSize(); i++)
  746. {
  747. menuDataBase.SetIndex(i);
  748. var itemMpn = (MPN)menuDataBase.GetCategoryMpn();
  749. if (!attachMpn.Any(mpn => mpn == itemMpn))
  750. continue;
  751. var menuFileName = menuDataBase.GetMenuFileName();
  752. var mpnTag = menuDataBase.GetCategoryMpnText();
  753. if (menuDataBase.GetBoDelOnly() || menuFileName.EndsWith("_del.menu"))
  754. continue;
  755. MpnAttachPropList.Add(new(itemMpn, menuFileName));
  756. }
  757. MpnAttachInitialized = true;
  758. OnMenuFilesChange(MenuFilesEventArgs.EventType.MpnAttach);
  759. }
  760. private static void InitializeMyRoomProps()
  761. {
  762. PlacementData.CreateData();
  763. var myRoomData = PlacementData.GetAllDatas(false);
  764. myRoomData.Sort(MyRoomDataComparator);
  765. foreach (var data in myRoomData)
  766. {
  767. var category = PlacementData.GetCategoryName(data.categoryID);
  768. if (!MyRoomPropDict.ContainsKey(category))
  769. {
  770. MyRoomPropCategories.Add(category);
  771. MyRoomPropDict[category] = new();
  772. }
  773. var asset = !string.IsNullOrEmpty(data.resourceName) ? data.resourceName : data.assetName;
  774. var item = new MyRoomItem()
  775. {
  776. PrefabName = asset,
  777. ID = data.ID,
  778. };
  779. MyRoomPropDict[category].Add(item);
  780. }
  781. static int MyRoomDataComparator(PlacementData.Data a, PlacementData.Data b)
  782. {
  783. var res = a.categoryID.CompareTo(b.categoryID);
  784. if (res is 0)
  785. res = a.ID.CompareTo(b.ID);
  786. return res;
  787. }
  788. }
  789. private static void InitializeModProps()
  790. {
  791. for (var i = 1; i < MenuCategories.Length; i++)
  792. ModPropDict[MenuCategories[i]] = new();
  793. if (!PropManager.ModItemsOnly)
  794. {
  795. var menuDatabase = GameMain.Instance.MenuDataBase;
  796. for (var i = 0; i < menuDatabase.GetDataSize(); i++)
  797. {
  798. menuDatabase.SetIndex(i);
  799. var modItem = new ModItem();
  800. if (ParseNativeMenuFile(i, modItem))
  801. ModPropDict[modItem.Category].Add(modItem);
  802. }
  803. }
  804. var cache = new MenuFileCache();
  805. foreach (var modMenuFile in GameUty.ModOnlysMenuFiles)
  806. {
  807. ModItem modItem;
  808. if (cache.Has(modMenuFile))
  809. {
  810. modItem = cache[modMenuFile];
  811. }
  812. else
  813. {
  814. modItem = ModItem.Mod(modMenuFile);
  815. ParseMenuFile(modMenuFile, modItem);
  816. cache[modMenuFile] = modItem;
  817. }
  818. if (ValidBG2MenuFile(modItem))
  819. ModPropDict[modItem.Category].Add(modItem);
  820. }
  821. cache.Serialize();
  822. foreach (var modFile in Menu.GetModFiles())
  823. {
  824. var modItem = ModItem.OfficialMod(modFile);
  825. if (ParseModMenuFile(modFile, modItem))
  826. ModPropDict[modItem.Category].Add(modItem);
  827. }
  828. MenuFilesInitialized = true;
  829. }
  830. // TODO: This could leak on failure.
  831. private static CsvParser OpenCsvParser(string nei, AFileSystemBase fs)
  832. {
  833. try
  834. {
  835. if (!fs.IsExistentFile(nei))
  836. return null;
  837. var file = fs.FileOpen(nei);
  838. var csvParser = new CsvParser();
  839. if (csvParser.Open(file))
  840. return csvParser;
  841. file?.Dispose();
  842. return null;
  843. }
  844. catch
  845. {
  846. // Ignored.
  847. }
  848. return null;
  849. }
  850. private static void OnMenuFilesChange(MenuFilesEventArgs.EventType eventType) =>
  851. MenuFilesChange?.Invoke(null, new(eventType));
  852. private static int KeepAtTop(string a, string b, string topItem)
  853. {
  854. if (a == b)
  855. return 0;
  856. if (a == topItem)
  857. return -1;
  858. if (b == topItem)
  859. return 1;
  860. return WindowsLogicalComparer.StrCmpLogicalW(a, b);
  861. }
  862. private class SerializePoseList
  863. {
  864. public string UIName { get; set; }
  865. public List<string> PoseList { get; set; }
  866. }
  867. }